OpenAMP Library  353
rpmsg.h
Go to the documentation of this file.
1 /*
2  * Remote processor messaging
3  *
4  * Copyright (C) 2011 Texas Instruments, Inc.
5  * Copyright (C) 2011 Google, Inc.
6  * All rights reserved.
7  * Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  */
11 
12 #ifndef _RPMSG_H_
13 #define _RPMSG_H_
14 
15 #include <metal/compiler.h>
16 #include <metal/mutex.h>
17 #include <metal/list.h>
18 #include <metal/utilities.h>
19 #include <string.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 
23 #if defined __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Configurable parameters */
28 #define RPMSG_NAME_SIZE (32)
29 #define RPMSG_ADDR_BMP_SIZE (128)
30 
31 #define RPMSG_NS_EPT_ADDR (0x35)
32 #define RPMSG_RESERVED_ADDRESSES (1024)
33 #define RPMSG_ADDR_ANY 0xFFFFFFFF
34 
35 /* Error macros. */
36 #define RPMSG_SUCCESS 0
37 #define RPMSG_ERROR_BASE -2000
38 #define RPMSG_ERR_NO_MEM (RPMSG_ERROR_BASE - 1)
39 #define RPMSG_ERR_NO_BUFF (RPMSG_ERROR_BASE - 2)
40 #define RPMSG_ERR_PARAM (RPMSG_ERROR_BASE - 3)
41 #define RPMSG_ERR_DEV_STATE (RPMSG_ERROR_BASE - 4)
42 #define RPMSG_ERR_BUFF_SIZE (RPMSG_ERROR_BASE - 5)
43 #define RPMSG_ERR_INIT (RPMSG_ERROR_BASE - 6)
44 #define RPMSG_ERR_ADDR (RPMSG_ERROR_BASE - 7)
45 #define RPMSG_ERR_PERM (RPMSG_ERROR_BASE - 8)
46 #define RPMSG_EOPNOTSUPP (RPMSG_ERROR_BASE - 9)
47 
48 struct rpmsg_endpoint;
49 struct rpmsg_device;
50 
51 /* Returns positive value on success or negative error value on failure */
52 typedef int (*rpmsg_ept_cb)(struct rpmsg_endpoint *ept, void *data,
53  size_t len, uint32_t src, void *priv);
54 typedef void (*rpmsg_ept_release_cb)(struct rpmsg_endpoint *ept);
55 typedef void (*rpmsg_ns_unbind_cb)(struct rpmsg_endpoint *ept);
56 typedef void (*rpmsg_ns_bind_cb)(struct rpmsg_device *rdev,
57  const char *name, uint32_t dest);
58 
68 
70  struct rpmsg_device *rdev;
71 
73  uint32_t addr;
74 
76  uint32_t dest_addr;
77 
79  uint32_t refcnt;
80 
83 
89 
92 
94  struct metal_list node;
95 
97  void *priv;
98 };
99 
103  int (*send_offchannel_raw)(struct rpmsg_device *rdev,
104  uint32_t src, uint32_t dst,
105  const void *data, int len, int wait);
106 
108  void (*hold_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf);
109 
111  void (*release_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf);
112 
114  void *(*get_tx_payload_buffer)(struct rpmsg_device *rdev,
115  uint32_t *len, int wait);
116 
119  uint32_t src, uint32_t dst,
120  const void *data, int len);
121 
123  int (*release_tx_buffer)(struct rpmsg_device *rdev, void *txbuf);
124 
126  int (*get_rx_buffer_size)(struct rpmsg_device *rdev);
127 
129  int (*get_tx_buffer_size)(struct rpmsg_device *rdev);
130 };
131 
133 struct rpmsg_device {
135  struct metal_list endpoints;
136 
138  struct rpmsg_endpoint ns_ept;
139 
141  unsigned long bitmap[metal_bitmap_longs(RPMSG_ADDR_BMP_SIZE)];
142  unsigned int bitnext;
143 
145  metal_mutex_t lock;
146 
149 
152 
154  struct rpmsg_device_ops ops;
155 
158 };
159 
178 int rpmsg_send_offchannel_raw(struct rpmsg_endpoint *ept, uint32_t src,
179  uint32_t dst, const void *data, int len,
180  int wait);
181 
198 static inline int rpmsg_send(struct rpmsg_endpoint *ept, const void *data,
199  int len)
200 {
201  if (!ept)
202  return RPMSG_ERR_PARAM;
203 
204  return rpmsg_send_offchannel_raw(ept, ept->addr, ept->dest_addr, data,
205  len, true);
206 }
207 
225 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, const void *data,
226  int len, uint32_t dst)
227 {
228  if (!ept)
229  return RPMSG_ERR_PARAM;
230 
231  return rpmsg_send_offchannel_raw(ept, ept->addr, dst, data, len, true);
232 }
233 
253 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept,
254  uint32_t src, uint32_t dst,
255  const void *data, int len)
256 {
257  return rpmsg_send_offchannel_raw(ept, src, dst, data, len, true);
258 }
259 
275 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, const void *data,
276  int len)
277 {
278  if (!ept)
279  return RPMSG_ERR_PARAM;
280 
281  return rpmsg_send_offchannel_raw(ept, ept->addr, ept->dest_addr, data,
282  len, false);
283 }
284 
301 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, const void *data,
302  int len, uint32_t dst)
303 {
304  if (!ept)
305  return RPMSG_ERR_PARAM;
306 
307  return rpmsg_send_offchannel_raw(ept, ept->addr, dst, data, len, false);
308 }
309 
328 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept,
329  uint32_t src, uint32_t dst,
330  const void *data, int len)
331 {
332  return rpmsg_send_offchannel_raw(ept, src, dst, data, len, false);
333 }
334 
352 void rpmsg_hold_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf);
353 
365 void rpmsg_release_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf);
366 
388  uint32_t *len, int wait);
389 
411 int rpmsg_release_tx_buffer(struct rpmsg_endpoint *ept, void *txbuf);
412 
424 
436 
469 int rpmsg_send_offchannel_nocopy(struct rpmsg_endpoint *ept, uint32_t src,
470  uint32_t dst, const void *data, int len);
471 
501 static inline int rpmsg_sendto_nocopy(struct rpmsg_endpoint *ept,
502  const void *data, int len, uint32_t dst)
503 {
504  if (!ept)
505  return RPMSG_ERR_PARAM;
506 
507  return rpmsg_send_offchannel_nocopy(ept, ept->addr, dst, data, len);
508 }
509 
538 static inline int rpmsg_send_nocopy(struct rpmsg_endpoint *ept,
539  const void *data, int len)
540 {
541  if (!ept)
542  return RPMSG_ERR_PARAM;
543 
544  return rpmsg_send_offchannel_nocopy(ept, ept->addr,
545  ept->dest_addr, data, len);
546 }
547 
576 int rpmsg_create_ept(struct rpmsg_endpoint *ept, struct rpmsg_device *rdev,
577  const char *name, uint32_t src, uint32_t dest,
578  rpmsg_ept_cb cb, rpmsg_ns_unbind_cb ns_unbind_cb);
579 
588 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
589 
598 static inline unsigned int is_rpmsg_ept_ready(struct rpmsg_endpoint *ept)
599 {
600  return ept && ept->rdev && ept->dest_addr != RPMSG_ADDR_ANY;
601 }
602 
603 #if defined __cplusplus
604 }
605 #endif
606 
607 #endif /* _RPMSG_H_ */
#define RPMSG_NAME_SIZE
Definition: rpmsg.h:28
static int rpmsg_sendto(struct rpmsg_endpoint *ept, const void *data, int len, uint32_t dst)
Send a message across to the remote processor, specify dst.
Definition: rpmsg.h:225
void(* rpmsg_ns_unbind_cb)(struct rpmsg_endpoint *ept)
Definition: rpmsg.h:55
#define RPMSG_ERR_PARAM
Definition: rpmsg.h:40
#define RPMSG_ADDR_BMP_SIZE
Definition: rpmsg.h:29
int rpmsg_get_rx_buffer_size(struct rpmsg_endpoint *ept)
Get RPMsg Rx buffer size.
Definition: rpmsg.c:227
static int rpmsg_send(struct rpmsg_endpoint *ept, const void *data, int len)
Send a message across to the remote processor.
Definition: rpmsg.h:198
#define RPMSG_ADDR_ANY
Definition: rpmsg.h:33
void(* rpmsg_ns_bind_cb)(struct rpmsg_device *rdev, const char *name, uint32_t dest)
Definition: rpmsg.h:56
int rpmsg_send_offchannel_nocopy(struct rpmsg_endpoint *ept, uint32_t src, uint32_t dst, const void *data, int len)
Send a message in tx buffer reserved by rpmsg_get_tx_payload_buffer() across to the remote processor.
Definition: rpmsg.c:242
void rpmsg_hold_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf)
Holds the rx buffer for usage outside the receive callback.
Definition: rpmsg.c:155
static int rpmsg_sendto_nocopy(struct rpmsg_endpoint *ept, const void *data, int len, uint32_t dst)
Sends a message in tx buffer allocated by rpmsg_get_tx_payload_buffer() across to the remote processo...
Definition: rpmsg.h:501
static int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, uint32_t src, uint32_t dst, const void *data, int len)
Send a message using explicit src/dst addresses.
Definition: rpmsg.h:328
static int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, uint32_t src, uint32_t dst, const void *data, int len)
Send a message using explicit src/dst addresses.
Definition: rpmsg.h:253
void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
Destroy rpmsg endpoint and unregister it from rpmsg device.
Definition: rpmsg.c:379
void rpmsg_release_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf)
Releases the rx buffer for future reuse in vring.
Definition: rpmsg.c:168
int rpmsg_release_tx_buffer(struct rpmsg_endpoint *ept, void *txbuf)
Releases unused buffer.
Definition: rpmsg.c:181
void * rpmsg_get_tx_payload_buffer(struct rpmsg_endpoint *ept, uint32_t *len, int wait)
Gets the tx buffer for message payload.
Definition: rpmsg.c:196
static int rpmsg_trysend(struct rpmsg_endpoint *ept, const void *data, int len)
Send a message across to the remote processor.
Definition: rpmsg.h:275
int rpmsg_create_ept(struct rpmsg_endpoint *ept, struct rpmsg_device *rdev, const char *name, uint32_t src, uint32_t dest, rpmsg_ept_cb cb, rpmsg_ns_unbind_cb ns_unbind_cb)
Create rpmsg endpoint and register it to rpmsg device.
Definition: rpmsg.c:324
int rpmsg_send_offchannel_raw(struct rpmsg_endpoint *ept, uint32_t src, uint32_t dst, const void *data, int len, int wait)
Send a message across to the remote processor, specifying source and destination address.
Definition: rpmsg.c:120
int rpmsg_get_tx_buffer_size(struct rpmsg_endpoint *ept)
Get RPMsg Tx buffer size.
Definition: rpmsg.c:212
void(* rpmsg_ept_release_cb)(struct rpmsg_endpoint *ept)
Definition: rpmsg.h:54
static unsigned int is_rpmsg_ept_ready(struct rpmsg_endpoint *ept)
Check if the rpmsg endpoint ready to send.
Definition: rpmsg.h:598
static int rpmsg_trysendto(struct rpmsg_endpoint *ept, const void *data, int len, uint32_t dst)
Send a message across to the remote processor, specify dst.
Definition: rpmsg.h:301
int(* rpmsg_ept_cb)(struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv)
Definition: rpmsg.h:52
static int rpmsg_send_nocopy(struct rpmsg_endpoint *ept, const void *data, int len)
Send a message in tx buffer reserved by rpmsg_get_tx_payload_buffer() across to the remote processor.
Definition: rpmsg.h:538
RPMsg device operations.
Definition: rpmsg.h:101
void(* release_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf)
Release RPMsg RX buffer.
Definition: rpmsg.h:111
int(* send_offchannel_nocopy)(struct rpmsg_device *rdev, uint32_t src, uint32_t dst, const void *data, int len)
Send RPMsg data without copy.
Definition: rpmsg.h:118
int(* send_offchannel_raw)(struct rpmsg_device *rdev, uint32_t src, uint32_t dst, const void *data, int len, int wait)
Send RPMsg data.
Definition: rpmsg.h:103
int(* release_tx_buffer)(struct rpmsg_device *rdev, void *txbuf)
Release RPMsg TX buffer.
Definition: rpmsg.h:123
int(* get_tx_buffer_size)(struct rpmsg_device *rdev)
Get RPMsg TX buffer size.
Definition: rpmsg.h:129
void(* hold_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf)
Hold RPMsg RX buffer.
Definition: rpmsg.h:108
int(* get_rx_buffer_size)(struct rpmsg_device *rdev)
Get RPMsg RX buffer size.
Definition: rpmsg.h:126
Representation of a RPMsg device.
Definition: rpmsg.h:133
metal_mutex_t lock
Mutex lock for RPMsg management.
Definition: rpmsg.h:145
bool support_ns
Create/destroy namespace message.
Definition: rpmsg.h:157
unsigned long bitmap[metal_bitmap_longs(RPMSG_ADDR_BMP_SIZE)]
Table endpoint address allocation.
Definition: rpmsg.h:141
rpmsg_ns_bind_cb ns_bind_cb
Callback handler for name service announcement without local epts waiting to bind.
Definition: rpmsg.h:148
struct rpmsg_device_ops ops
RPMsg device operations.
Definition: rpmsg.h:154
unsigned int bitnext
Definition: rpmsg.h:142
struct metal_list endpoints
List of endpoints.
Definition: rpmsg.h:135
struct rpmsg_endpoint ns_ept
Name service endpoint.
Definition: rpmsg.h:138
rpmsg_ns_bind_cb ns_unbind_cb
Callback handler for name service announcement, called when remote ept is destroyed.
Definition: rpmsg.h:151
Structure that binds a local RPMsg address to its user.
Definition: rpmsg.h:65
struct rpmsg_device * rdev
Pointer to the RPMsg device.
Definition: rpmsg.h:70
void * priv
Private data for the driver's use.
Definition: rpmsg.h:97
char name[RPMSG_NAME_SIZE]
Name of the service supported.
Definition: rpmsg.h:67
rpmsg_ept_release_cb release_cb
Callback to inform the user that the endpoint allocation can be safely removed.
Definition: rpmsg.h:82
rpmsg_ept_cb cb
User rx callback, return value of this callback is reserved for future use, for now,...
Definition: rpmsg.h:88
uint32_t addr
Local address of the endpoint.
Definition: rpmsg.h:73
rpmsg_ns_unbind_cb ns_unbind_cb
Endpoint service unbind callback, called when remote ept is destroyed.
Definition: rpmsg.h:91
uint32_t dest_addr
Address of the default remote endpoint binded.
Definition: rpmsg.h:76
uint32_t refcnt
Reference count for determining whether the endpoint can be deallocated.
Definition: rpmsg.h:79
struct metal_list node
Endpoint node.
Definition: rpmsg.h:94