RPMsg-Lite User's Guide  Rev. 5.1.1
NXP Semiconductors
rpmsg_lite.h
1 /*
2  * Copyright (c) 2014, Mentor Graphics Corporation
3  * Copyright (c) 2015 Xilinx, Inc.
4  * Copyright (c) 2016 Freescale Semiconductor, Inc.
5  * Copyright 2016-2023 NXP
6  * Copyright 2021 ACRIOS Systems s.r.o.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef RPMSG_LITE_H_
35 #define RPMSG_LITE_H_
36 
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40 
41 #include <stddef.h>
42 #include "rpmsg_compiler.h"
43 #include "virtqueue.h"
44 #include "rpmsg_env.h"
45 #include "llist.h"
46 #include "rpmsg_default_config.h"
47 
50 
51 /*******************************************************************************
52  * Definitions
53  ******************************************************************************/
54 
55 #define RL_VERSION "5.1.1"
57 /* Shared memory "allocator" parameters */
58 #define RL_WORD_SIZE (sizeof(uint32_t))
59 #define RL_WORD_ALIGN_UP(a) \
60  (((((uintptr_t)(a)) & (RL_WORD_SIZE - 1U)) != 0U) ? ((((uintptr_t)(a)) & (~(RL_WORD_SIZE - 1U))) + 4U) : \
61  ((uintptr_t)(a)))
62 #define RL_WORD_ALIGN_DOWN(a) \
63  (((((uintptr_t)(a)) & (RL_WORD_SIZE - 1U)) != 0U) ? (((uintptr_t)(a)) & (~(RL_WORD_SIZE - 1U))) : ((uintptr_t)(a)))
64 
65 /* Definitions for device types , null pointer, etc.*/
66 #define RL_SUCCESS (0)
67 #define RL_NULL ((void *)0)
68 #define RL_REMOTE (0)
69 #define RL_MASTER (1)
70 #define RL_TRUE (1UL)
71 #define RL_FALSE (0UL)
72 #define RL_ADDR_ANY (0xFFFFFFFFU)
73 #define RL_RELEASE (0)
74 #define RL_HOLD (1)
75 #define RL_DONT_BLOCK (0)
76 #define RL_BLOCK (~0UL)
77 
78 /* Error macros. */
79 #define RL_ERRORS_BASE (-5000)
80 #define RL_ERR_NO_MEM (RL_ERRORS_BASE - 1)
81 #define RL_ERR_BUFF_SIZE (RL_ERRORS_BASE - 2)
82 #define RL_ERR_PARAM (RL_ERRORS_BASE - 3)
83 #define RL_ERR_DEV_ID (RL_ERRORS_BASE - 4)
84 #define RL_ERR_MAX_VQ (RL_ERRORS_BASE - 5)
85 #define RL_ERR_NO_BUFF (RL_ERRORS_BASE - 6)
86 #define RL_NOT_READY (RL_ERRORS_BASE - 7)
87 #define RL_ALREADY_DONE (RL_ERRORS_BASE - 8)
88 
89 /* Init flags */
90 #define RL_NO_FLAGS (0U)
91 
92 /* rpmsg_std_hdr contains a reserved field,
93  * this implementation of RPMSG uses this reserved
94  * field to hold the idx and totlen of the buffer
95  * not being returned to the vring in the receive
96  * callback function. This way, the no-copy API
97  * can use this field to return the buffer later.
98  */
100 {
101  uint16_t rfu; /* reserved for future usage */
102  uint16_t idx;
103 };
104 
105 RL_PACKED_BEGIN
111 {
112  uint32_t src;
113  uint32_t dst;
114  struct rpmsg_hdr_reserved reserved;
115  uint16_t len;
116  uint16_t flags;
117 } RL_PACKED_END;
118 
119 RL_PACKED_BEGIN
125 {
126  struct rpmsg_std_hdr hdr;
127  uint8_t data[1];
128 } RL_PACKED_END;
129 
133 typedef int32_t (*rl_ept_rx_cb_t)(void *payload, uint32_t payload_len, uint32_t src, void *priv);
134 
139 {
140  uint32_t addr;
142  void *rx_cb_data;
143  void *rfu;
144  /* 16 bytes aligned on 32bit architecture */
145 };
146 
151 {
152  struct rpmsg_lite_endpoint ept;
153  struct llist node;
154 };
155 
163 {
164  struct virtqueue *rvq;
165  struct virtqueue *tvq;
166  struct llist *rl_endpoints;
167  LOCK *lock;
168 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
169  LOCK_STATIC_CONTEXT lock_static_ctxt;
170 #endif
171  uint32_t link_state;
172  char *sh_mem_base;
173  uint32_t sh_mem_remaining;
174  uint32_t sh_mem_total;
175  struct virtqueue_ops const *vq_ops;
176 #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
177  void *env;
178 #endif
179 
180 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
181  struct vq_static_context vq_ctxt[2];
182 #endif
183  uint32_t link_id;
184 };
185 
186 /*******************************************************************************
187  * API
188  ******************************************************************************/
189 
190 /* Exported API functions */
191 
208 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
209 struct rpmsg_lite_instance *rpmsg_lite_master_init(void *shmem_addr,
210  size_t shmem_length,
211  uint32_t link_id,
212  uint32_t init_flags,
213  struct rpmsg_lite_instance *static_context);
214 #elif defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
216  void *shmem_addr, size_t shmem_length, uint32_t link_id, uint32_t init_flags, void *env_cfg);
217 #else
218 struct rpmsg_lite_instance *rpmsg_lite_master_init(void *shmem_addr,
219  size_t shmem_length,
220  uint32_t link_id,
221  uint32_t init_flags);
222 #endif
223 
239 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
240 struct rpmsg_lite_instance *rpmsg_lite_remote_init(void *shmem_addr,
241  uint32_t link_id,
242  uint32_t init_flags,
243  struct rpmsg_lite_instance *static_context);
244 #elif defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
245 struct rpmsg_lite_instance *rpmsg_lite_remote_init(void *shmem_addr,
246  uint32_t link_id,
247  uint32_t init_flags,
248  void *env_cfg);
249 #else
250 struct rpmsg_lite_instance *rpmsg_lite_remote_init(void *shmem_addr, uint32_t link_id, uint32_t init_flags);
251 #endif
252 
264 int32_t rpmsg_lite_deinit(struct rpmsg_lite_instance *rpmsg_lite_dev);
265 
279 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
280 struct rpmsg_lite_endpoint *rpmsg_lite_create_ept(struct rpmsg_lite_instance *rpmsg_lite_dev,
281  uint32_t addr,
283  void *rx_cb_data,
284  struct rpmsg_lite_ept_static_context *ept_context);
285 #else
286 struct rpmsg_lite_endpoint *rpmsg_lite_create_ept(struct rpmsg_lite_instance *rpmsg_lite_dev,
287  uint32_t addr,
289  void *rx_cb_data);
290 #endif
291 
299 int32_t rpmsg_lite_destroy_ept(struct rpmsg_lite_instance *rpmsg_lite_dev, struct rpmsg_lite_endpoint *rl_ept);
300 
318 int32_t rpmsg_lite_send(struct rpmsg_lite_instance *rpmsg_lite_dev,
319  struct rpmsg_lite_endpoint *ept,
320  uint32_t dst,
321  char *data,
322  uint32_t size,
323  uintptr_t timeout);
324 
333 uint32_t rpmsg_lite_is_link_up(struct rpmsg_lite_instance *rpmsg_lite_dev);
334 
345 uint32_t rpmsg_lite_wait_for_link_up(struct rpmsg_lite_instance *rpmsg_lite_dev, uint32_t timeout);
346 
347 #if defined(RL_API_HAS_ZEROCOPY) && (RL_API_HAS_ZEROCOPY == 1)
348 
359 int32_t rpmsg_lite_release_rx_buffer(struct rpmsg_lite_instance *rpmsg_lite_dev, void *rxbuf);
360 
377 void *rpmsg_lite_alloc_tx_buffer(struct rpmsg_lite_instance *rpmsg_lite_dev, uint32_t *size, uintptr_t timeout);
378 
404 int32_t rpmsg_lite_send_nocopy(struct rpmsg_lite_instance *rpmsg_lite_dev,
405  struct rpmsg_lite_endpoint *ept,
406  uint32_t dst,
407  void *data,
408  uint32_t size);
409 #endif /* RL_API_HAS_ZEROCOPY */
410 
412 
413 #if defined(__cplusplus)
414 }
415 #endif
416 
417 #endif /* RPMSG_LITE_H_ */
LOCK * lock
Definition: rpmsg_lite.h:167
Definition: rpmsg_lite.h:124
Definition: rpmsg_lite.h:162
int32_t rpmsg_lite_destroy_ept(struct rpmsg_lite_instance *rpmsg_lite_dev, struct rpmsg_lite_endpoint *rl_ept)
This function deletes rpmsg endpoint and performs cleanup.
uint16_t flags
Definition: rpmsg_lite.h:116
int32_t rpmsg_lite_send_nocopy(struct rpmsg_lite_instance *rpmsg_lite_dev, struct rpmsg_lite_endpoint *ept, uint32_t dst, void *data, uint32_t size)
Sends a message in tx buffer allocated by rpmsg_lite_alloc_tx_buffer()
char * sh_mem_base
Definition: rpmsg_lite.h:172
void * rfu
Definition: rpmsg_lite.h:143
Definition: rpmsg_lite.h:150
struct virtqueue_ops const * vq_ops
Definition: rpmsg_lite.h:175
struct rpmsg_lite_endpoint * rpmsg_lite_create_ept(struct rpmsg_lite_instance *rpmsg_lite_dev, uint32_t addr, rl_ept_rx_cb_t rx_cb, void *rx_cb_data)
Create a new rpmsg endpoint, which can be used for communication.
int32_t rpmsg_lite_release_rx_buffer(struct rpmsg_lite_instance *rpmsg_lite_dev, void *rxbuf)
Releases the rx buffer for future reuse in vring. This API can be called at process context when the ...
int32_t(* rl_ept_rx_cb_t)(void *payload, uint32_t payload_len, uint32_t src, void *priv)
Receive callback function type.
Definition: rpmsg_lite.h:133
Definition: rpmsg_lite.h:110
uint16_t len
Definition: rpmsg_lite.h:115
int32_t rpmsg_lite_deinit(struct rpmsg_lite_instance *rpmsg_lite_dev)
Deinitialized the RPMsg-Lite communication stack This function always succeeds. rpmsg_lite_init() can...
uint32_t src
Definition: rpmsg_lite.h:112
void * rx_cb_data
Definition: rpmsg_lite.h:142
uint32_t rpmsg_lite_wait_for_link_up(struct rpmsg_lite_instance *rpmsg_lite_dev, uint32_t timeout)
Function to wait until the link is up. Returns RL_TRUE once the link_state is set or RL_FALSE in case...
uint32_t dst
Definition: rpmsg_lite.h:113
struct rpmsg_lite_instance * rpmsg_lite_master_init(void *shmem_addr, size_t shmem_length, uint32_t link_id, uint32_t init_flags)
Initializes the RPMsg-Lite communication stack. Must be called prior to any other RPMSG lite API...
struct rpmsg_lite_instance * rpmsg_lite_remote_init(void *shmem_addr, uint32_t link_id, uint32_t init_flags)
Initializes the RPMsg-Lite communication stack. Must be called prior to any other RPMsg-Lite API...
uint32_t rpmsg_lite_is_link_up(struct rpmsg_lite_instance *rpmsg_lite_dev)
Function to get the link state.
struct virtqueue * rvq
Definition: rpmsg_lite.h:164
Definition: rpmsg_lite.h:99
int32_t rpmsg_lite_send(struct rpmsg_lite_instance *rpmsg_lite_dev, struct rpmsg_lite_endpoint *ept, uint32_t dst, char *data, uint32_t size, uintptr_t timeout)
Sends a message contained in data field of length size to the remote endpoint with address dst...
Definition: rpmsg_lite.h:138
void * rpmsg_lite_alloc_tx_buffer(struct rpmsg_lite_instance *rpmsg_lite_dev, uint32_t *size, uintptr_t timeout)
Allocates the tx buffer for message payload.
uint32_t sh_mem_total
Definition: rpmsg_lite.h:174
struct llist * rl_endpoints
Definition: rpmsg_lite.h:166
struct virtqueue * tvq
Definition: rpmsg_lite.h:165
uint32_t addr
Definition: rpmsg_lite.h:140
uint32_t link_state
Definition: rpmsg_lite.h:171
rl_ept_rx_cb_t rx_cb
Definition: rpmsg_lite.h:141
uint32_t link_id
Definition: rpmsg_lite.h:183
uint32_t sh_mem_remaining
Definition: rpmsg_lite.h:173