OpenAMP Library
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 
47 struct rpmsg_endpoint;
48 struct rpmsg_device;
49 
50 /* Returns positive value on success or negative error value on failure */
51 typedef int (*rpmsg_ept_cb)(struct rpmsg_endpoint *ept, void *data,
52  size_t len, uint32_t src, void *priv);
53 typedef void (*rpmsg_ns_unbind_cb)(struct rpmsg_endpoint *ept);
54 typedef void (*rpmsg_ns_bind_cb)(struct rpmsg_device *rdev,
55  const char *name, uint32_t dest);
56 
75  struct rpmsg_device *rdev;
76  uint32_t addr;
77  uint32_t dest_addr;
80  struct metal_list node;
81  void *priv;
82 };
83 
94  int (*send_offchannel_raw)(struct rpmsg_device *rdev,
95  uint32_t src, uint32_t dst,
96  const void *data, int len, int wait);
97  void (*hold_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf);
98  void (*release_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf);
99  void *(*get_tx_payload_buffer)(struct rpmsg_device *rdev,
100  uint32_t *len, int wait);
102  uint32_t src, uint32_t dst,
103  const void *data, int len);
104  int (*release_tx_buffer)(struct rpmsg_device *rdev, void *txbuf);
105 };
106 
120 struct rpmsg_device {
121  struct metal_list endpoints;
122  struct rpmsg_endpoint ns_ept;
123  unsigned long bitmap[metal_bitmap_longs(RPMSG_ADDR_BMP_SIZE)];
124  metal_mutex_t lock;
127  struct rpmsg_device_ops ops;
129 };
130 
149 int rpmsg_send_offchannel_raw(struct rpmsg_endpoint *ept, uint32_t src,
150  uint32_t dst, const void *data, int len,
151  int wait);
152 
169 static inline int rpmsg_send(struct rpmsg_endpoint *ept, const void *data,
170  int len)
171 {
172  if (!ept)
173  return RPMSG_ERR_PARAM;
174 
175  return rpmsg_send_offchannel_raw(ept, ept->addr, ept->dest_addr, data,
176  len, true);
177 }
178 
196 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, const void *data,
197  int len, uint32_t dst)
198 {
199  if (!ept)
200  return RPMSG_ERR_PARAM;
201 
202  return rpmsg_send_offchannel_raw(ept, ept->addr, dst, data, len, true);
203 }
204 
224 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept,
225  uint32_t src, uint32_t dst,
226  const void *data, int len)
227 {
228  return rpmsg_send_offchannel_raw(ept, src, dst, data, len, true);
229 }
230 
246 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, const void *data,
247  int len)
248 {
249  if (!ept)
250  return RPMSG_ERR_PARAM;
251 
252  return rpmsg_send_offchannel_raw(ept, ept->addr, ept->dest_addr, data,
253  len, false);
254 }
255 
272 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, const void *data,
273  int len, uint32_t dst)
274 {
275  if (!ept)
276  return RPMSG_ERR_PARAM;
277 
278  return rpmsg_send_offchannel_raw(ept, ept->addr, dst, data, len, false);
279 }
280 
299 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept,
300  uint32_t src, uint32_t dst,
301  const void *data, int len)
302 {
303  return rpmsg_send_offchannel_raw(ept, src, dst, data, len, false);
304 }
305 
323 void rpmsg_hold_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf);
324 
336 void rpmsg_release_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf);
337 
359  uint32_t *len, int wait);
360 
382 int rpmsg_release_tx_buffer(struct rpmsg_endpoint *ept, void *txbuf);
383 
416 int rpmsg_send_offchannel_nocopy(struct rpmsg_endpoint *ept, uint32_t src,
417  uint32_t dst, const void *data, int len);
418 
448 static inline int rpmsg_sendto_nocopy(struct rpmsg_endpoint *ept,
449  const void *data, int len, uint32_t dst)
450 {
451  if (!ept)
452  return RPMSG_ERR_PARAM;
453 
454  return rpmsg_send_offchannel_nocopy(ept, ept->addr, dst, data, len);
455 }
456 
485 static inline int rpmsg_send_nocopy(struct rpmsg_endpoint *ept,
486  const void *data, int len)
487 {
488  if (!ept)
489  return RPMSG_ERR_PARAM;
490 
491  return rpmsg_send_offchannel_nocopy(ept, ept->addr,
492  ept->dest_addr, data, len);
493 }
494 
523 int rpmsg_create_ept(struct rpmsg_endpoint *ept, struct rpmsg_device *rdev,
524  const char *name, uint32_t src, uint32_t dest,
525  rpmsg_ept_cb cb, rpmsg_ns_unbind_cb ns_unbind_cb);
526 
535 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
536 
545 static inline unsigned int is_rpmsg_ept_ready(struct rpmsg_endpoint *ept)
546 {
547  return ept && ept->rdev && ept->dest_addr != RPMSG_ADDR_ANY;
548 }
549 
550 #if defined __cplusplus
551 }
552 #endif
553 
554 #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:196
void(* rpmsg_ns_unbind_cb)(struct rpmsg_endpoint *ept)
Definition: rpmsg.h:53
#define RPMSG_ERR_PARAM
Definition: rpmsg.h:40
#define RPMSG_ADDR_BMP_SIZE
Definition: rpmsg.h:29
static int rpmsg_send(struct rpmsg_endpoint *ept, const void *data, int len)
Send a message across to the remote processor.
Definition: rpmsg.h:169
#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:54
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:192
void rpmsg_hold_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf)
Holds the rx buffer for usage outside the receive callback.
Definition: rpmsg.c:135
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:448
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:299
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:224
void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
Destroy rpmsg endpoint and unregister it from rpmsg device.
Definition: rpmsg.c:322
void rpmsg_release_rx_buffer(struct rpmsg_endpoint *ept, void *rxbuf)
Releases the rx buffer for future reuse in vring.
Definition: rpmsg.c:148
int rpmsg_release_tx_buffer(struct rpmsg_endpoint *ept, void *txbuf)
Releases unused buffer.
Definition: rpmsg.c:161
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:176
static int rpmsg_trysend(struct rpmsg_endpoint *ept, const void *data, int len)
Send a message across to the remote processor.
Definition: rpmsg.h:246
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:268
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:100
static unsigned int is_rpmsg_ept_ready(struct rpmsg_endpoint *ept)
Check if the rpmsg endpoint ready to send.
Definition: rpmsg.h:545
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:272
int(* rpmsg_ept_cb)(struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv)
Definition: rpmsg.h:51
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:485
Definition: rpmsg.h:93
void(* release_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf)
Definition: rpmsg.h:98
int(* send_offchannel_nocopy)(struct rpmsg_device *rdev, uint32_t src, uint32_t dst, const void *data, int len)
Definition: rpmsg.h:101
int(* send_offchannel_raw)(struct rpmsg_device *rdev, uint32_t src, uint32_t dst, const void *data, int len, int wait)
Definition: rpmsg.h:94
int(* release_tx_buffer)(struct rpmsg_device *rdev, void *txbuf)
Definition: rpmsg.h:104
void(* hold_rx_buffer)(struct rpmsg_device *rdev, void *rxbuf)
Definition: rpmsg.h:97
Definition: rpmsg.h:120
metal_mutex_t lock
Definition: rpmsg.h:124
bool support_ns
Definition: rpmsg.h:128
unsigned long bitmap[metal_bitmap_longs(RPMSG_ADDR_BMP_SIZE)]
Definition: rpmsg.h:123
rpmsg_ns_bind_cb ns_bind_cb
Definition: rpmsg.h:125
struct rpmsg_device_ops ops
Definition: rpmsg.h:127
struct metal_list endpoints
Definition: rpmsg.h:121
struct rpmsg_endpoint ns_ept
Definition: rpmsg.h:122
rpmsg_ns_bind_cb ns_unbind_cb
Definition: rpmsg.h:126
Definition: rpmsg.h:73
struct rpmsg_device * rdev
Definition: rpmsg.h:75
void * priv
Definition: rpmsg.h:81
char name[RPMSG_NAME_SIZE]
Definition: rpmsg.h:74
rpmsg_ept_cb cb
Definition: rpmsg.h:78
uint32_t addr
Definition: rpmsg.h:76
rpmsg_ns_unbind_cb ns_unbind_cb
Definition: rpmsg.h:79
uint32_t dest_addr
Definition: rpmsg.h:77
struct metal_list node
Definition: rpmsg.h:80