1 /*
2  * Copyright (c) 2014, Mentor Graphics Corporation
3  * Copyright (c) 2015 Xilinx, Inc.
4  * Copyright (c) 2016 Freescale Semiconductor, Inc.
5  * Copyright 2016-2022 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_QUEUE_H_
35 #define RPMSG_QUEUE_H_
36 
37 #include "rpmsg_lite.h"
38 
39 //! @addtogroup rpmsg_queue
40 //! @{
41 
42 /*! \typedef rpmsg_queue_handle
43     \brief Rpmsg queue handle type.
44 */
45 typedef void *rpmsg_queue_handle;
46 
47 /* RL_API_HAS_ZEROCOPY has to be enabled for RPMsg Queue to work */
48 #if defined(RL_API_HAS_ZEROCOPY) && (RL_API_HAS_ZEROCOPY == 1)
49 
50 /*******************************************************************************
51  * API
52  ******************************************************************************/
53 
54 /* Exported API functions */
55 
56 #if defined(__cplusplus)
57 extern "C" {
58 #endif
59 
60 /*!
61  * @brief
62  * This callback needs to be registered with an endpoint
63  *
64  * @param payload Pointer to the buffer containing received data
65  * @param payload_len Size of data received, in bytes
66  * @param src Pointer to address of the endpoint from which data is received
67  * @param priv Private data provided during endpoint creation
68  *
69  * @return RL_HOLD or RL_RELEASE to release or hold the buffer in payload
70  */
71 int32_t rpmsg_queue_rx_cb(void *payload, uint32_t payload_len, uint32_t src, void *priv);
72 
73 /*!
74  * @brief
75  * Create a RPMsg queue which can be used
76  * for blocking reception.
77  *
78  * @param rpmsg_lite_dev   RPMsg Lite instance
79  * @param queue_storage    RPMsg Lite queue static storage pointer
80  * @param queue_ctxt       RPMsg Lite queue static context holder
81  *
82  * @return RPMsg queue handle or RL_NULL
83  *
84  */
85 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
86 rpmsg_queue_handle rpmsg_queue_create(struct rpmsg_lite_instance *rpmsg_lite_dev,
87                                       uint8_t *queue_storage,
88                                       rpmsg_static_queue_ctxt *queue_ctxt);
89 #else
90 rpmsg_queue_handle rpmsg_queue_create(struct rpmsg_lite_instance *rpmsg_lite_dev);
91 #endif
92 
93 /*!
94  * @brief
95  * Destroy a queue and clean up.
96  * Do not destroy a queue which is registered with an active endpoint!
97  *
98  * @param rpmsg_lite_dev    RPMsg-Lite instance
99  * @param[in] q             RPMsg queue handle to destroy
100  *
101  * @return Status of function execution
102  *
103  */
104 int32_t rpmsg_queue_destroy(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_queue_handle q);
105 
106 /*!
107  * @brief
108  * blocking receive function - blocking version of the received function that can be called from an RTOS task.
109  * The data is copied from the receive buffer into the user supplied buffer.
110  *
111  * This is the "receive with copy" version of the RPMsg receive function. This version is simple
112  * to use but it requires copying data from shared memory into the user space buffer.
113  * The user has no obligation or burden to manage the shared memory buffers.
114  *
115  * @param rpmsg_lite_dev    RPMsg-Lite instance
116  * @param[in] q             RPMsg queue handle to listen on
117  * @param[in] data          Pointer to the user buffer the received data are copied to
118  * @param[out] len          Pointer to an int variable that will contain the number of bytes actually copied into the
119  * buffer
120  * @param[in] maxlen        Maximum number of bytes to copy (received buffer size)
121  * @param[out] src          Pointer to address of the endpoint from which data is received
122  * @param[in] timeout       Timeout, in milliseconds, to wait for a message. A value of 0 means don't wait (non-blocking
123  * call).
124  *                          A value of 0xffffffff means wait forever (blocking call).
125  *
126  * @return Status of function execution
127  *
128  * @see rpmsg_queue_recv_nocopy
129  */
130 int32_t rpmsg_queue_recv(struct rpmsg_lite_instance *rpmsg_lite_dev,
131                          rpmsg_queue_handle q,
132                          uint32_t *src,
133                          char *data,
134                          uint32_t maxlen,
135                          uint32_t *len,
136                          uintptr_t timeout);
137 
138 /*!
139  * @brief
140  * blocking receive function - blocking version of the received function that can be called from an RTOS task.
141  * The data is NOT copied into the user-app. buffer.
142  *
143  * This is the "zero-copy receive" version of the RPMsg receive function. No data is copied.
144  * Only the pointer to the data is returned. This version is fast, but it requires the user to manage
145  * buffer allocation. Specifically, the user must decide when a buffer is no longer in use and
146  * make the appropriate API call to free it, see rpmsg_queue_nocopy_free().
147  *
148  * @param rpmsg_lite_dev    RPMsg Lite instance
149  * @param[in] q             RPMsg queue handle to listen on
150  * @param[out] data         Pointer to the RPMsg buffer of the shared memory where the received data is stored
151  * @param[out] len          Pointer to an int variable that that will contain the number of valid bytes in the RPMsg
152  * buffer
153  * @param[out] src          Pointer to address of the endpoint from which data is received
154  * @param[in] timeout       Timeout, in milliseconds, to wait for a message. A value of 0 means don't wait (non-blocking
155  * call).
156  *                          A value of 0xffffffff means wait forever (blocking call).
157  *
158  * @return Status of function execution.
159  *
160  * @see rpmsg_queue_nocopy_free
161  * @see rpmsg_queue_recv
162  */
163 int32_t rpmsg_queue_recv_nocopy(struct rpmsg_lite_instance *rpmsg_lite_dev,
164                                 rpmsg_queue_handle q,
165                                 uint32_t *src,
166                                 char **data,
167                                 uint32_t *len,
168                                 uintptr_t timeout);
169 
170 /*!
171  * @brief This function frees a buffer previously returned by rpmsg_queue_recv_nocopy().
172  *
173  * Once the zero-copy mechanism of receiving data is used, this function
174  * has to be called to free a buffer and to make it available for the next data
175  * transfer.
176  *
177  * @param rpmsg_lite_dev    RPMsg-Lite instance
178  * @param[in] data          Pointer to the RPMsg buffer of the shared memory that has to be freed
179  *
180  * @return Status of function execution.
181  *
182  * @see rpmsg_queue_recv_nocopy
183  */
184 int32_t rpmsg_queue_nocopy_free(struct rpmsg_lite_instance *rpmsg_lite_dev, void *data);
185 
186 /*!
187  * @brief This function returns the number of pending messages in the queue.
188  *
189  * @param[in] q             RPMsg queue handle
190  *
191  * @return Number of pending messages in the queue.
192  */
193 int32_t rpmsg_queue_get_current_size(rpmsg_queue_handle q);
194 
195 //! @}
196 
197 #if defined(__cplusplus)
198 }
199 #endif
200 
201 #endif /* RL_API_HAS_ZEROCOPY */
202 
203 #endif /* RPMSG_QUEUE_H_ */
204