26ff14f03ec5b4debaa0a5f3f643904ce0b65c32
1 /*
2 * Copyright (c) 2014, Mentor Graphics Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Mentor Graphics Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
30 #ifndef _RPMSG_CORE_H_
31 #define _RPMSG_CORE_H_
33 #include "openamp/compiler.h"
34 #include "openamp/env.h"
35 #include "openamp/virtio.h"
36 #include "openamp/hil.h"
37 #include "openamp/sh_mem.h"
38 #include "openamp/llist.h"
39 #include "openamp/rpmsg.h"
40 #include "metal/mutex.h"
42 /* Configurable parameters */
43 #define RPMSG_BUFFER_SIZE 512
44 #define RPMSG_MAX_VQ_PER_RDEV 2
45 #define RPMSG_NS_EPT_ADDR 0x35
46 #define RPMSG_ADDR_BMP_SIZE 4
48 /* Definitions for device types , null pointer, etc.*/
49 #define RPMSG_SUCCESS 0
50 #define RPMSG_NULL (void *)0
51 #define RPMSG_REMOTE 0
52 #define RPMSG_MASTER 1
53 #define RPMSG_TRUE 1
54 #define RPMSG_FALSE 0
56 /* RPMSG channel states. */
57 #define RPMSG_CHNL_STATE_IDLE 0
58 #define RPMSG_CHNL_STATE_NS 1
59 #define RPMSG_CHNL_STATE_ACTIVE 2
61 /* Remote processor/device states. */
62 #define RPMSG_DEV_STATE_IDLE 0
63 #define RPMSG_DEV_STATE_ACTIVE 1
65 /* Total tick count for 15secs - 1msec tick. */
66 #define RPMSG_TICK_COUNT 15000
68 /* Time to wait - In multiple of 10 msecs. */
69 #define RPMSG_TICKS_PER_INTERVAL 10
71 /* Error macros. */
72 #define RPMSG_ERROR_BASE -2000
73 #define RPMSG_ERR_NO_MEM (RPMSG_ERROR_BASE - 1)
74 #define RPMSG_ERR_NO_BUFF (RPMSG_ERROR_BASE - 2)
75 #define RPMSG_ERR_MAX_VQ (RPMSG_ERROR_BASE - 3)
76 #define RPMSG_ERR_PARAM (RPMSG_ERROR_BASE - 4)
77 #define RPMSG_ERR_DEV_STATE (RPMSG_ERROR_BASE - 5)
78 #define RPMSG_ERR_BUFF_SIZE (RPMSG_ERROR_BASE - 6)
79 #define RPMSG_ERR_DEV_ID (RPMSG_ERROR_BASE - 7)
80 #define RPMSG_ERR_DEV_ADDR (RPMSG_ERROR_BASE - 8)
82 #if (RPMSG_DEBUG == true)
83 #define RPMSG_ASSERT(_exp, _msg) do{ \
84 if (!(_exp)){ printf("%s - "_msg, __func__); while(1);} \
85 } while(0)
86 #else
87 #define RPMSG_ASSERT(_exp, _msg) if (!(_exp)) while(1)
88 #endif
90 struct rpmsg_channel;
91 typedef void (*rpmsg_rx_cb_t) (struct rpmsg_channel *, void *, int, void *,
92 unsigned long);
93 typedef void (*rpmsg_chnl_cb_t) (struct rpmsg_channel * rp_chl);
94 /**
95 * remote_device
96 *
97 * This structure is maintained by RPMSG driver to represent remote device/core.
98 *
99 * @virtd_dev - virtio device for remote core
100 * @rvq - Rx virtqueue for virtio device
101 * @tvq - Tx virtqueue for virtio device
102 * @proc - reference to remote processor
103 * @rp_channels - rpmsg channels list for the device
104 * @rp_endpoints - rpmsg endpoints list for the device
105 * @mem_pool - shared memory pool
106 * @bitmap - bitmap for channels addresses
107 * @channel_created - create channel callback
108 * @channel_destroyed - delete channel callback
109 * @default_cb - default callback handler for RX data on channel
110 * @lock - remote device mutex
111 * @role - role of the remote device, RPMSG_MASTER/RPMSG_REMOTE
112 * @state - remote device state, IDLE/ACTIVE
113 * @support_ns - if device supports name service announcement
114 *
115 */
116 struct remote_device {
117 struct virtio_device virt_dev;
118 struct virtqueue *rvq;
119 struct virtqueue *tvq;
120 struct hil_proc *proc;
121 struct llist *rp_channels;
122 struct llist *rp_endpoints;
123 struct sh_mem_pool *mem_pool;
124 unsigned long bitmap[RPMSG_ADDR_BMP_SIZE];
125 rpmsg_chnl_cb_t channel_created;
126 rpmsg_chnl_cb_t channel_destroyed;
127 rpmsg_rx_cb_t default_cb;
128 metal_mutex_t lock;
129 unsigned int role;
130 unsigned int state;
131 int support_ns;
132 };
134 /* Core functions */
135 int rpmsg_start_ipc(struct remote_device *rdev);
136 struct rpmsg_channel *_rpmsg_create_channel(struct remote_device *rdev,
137 char *name, unsigned long src,
138 unsigned long dst);
139 void _rpmsg_delete_channel(struct rpmsg_channel *rp_chnl);
140 struct rpmsg_endpoint *_create_endpoint(struct remote_device *rdev,
141 rpmsg_rx_cb_t cb, void *priv,
142 unsigned long addr);
143 void _destroy_endpoint(struct remote_device *rdev,
144 struct rpmsg_endpoint *rp_ept);
145 void rpmsg_send_ns_message(struct remote_device *rdev,
146 struct rpmsg_channel *rp_chnl, unsigned long flags);
147 int rpmsg_enqueue_buffer(struct remote_device *rdev, void *buffer,
148 unsigned long len, unsigned short idx);
149 void rpmsg_return_buffer(struct remote_device *rdev, void *buffer,
150 unsigned long len, unsigned short idx);
151 void *rpmsg_get_tx_buffer(struct remote_device *rdev, unsigned long *len,
152 unsigned short *idx);
153 void rpmsg_free_buffer(struct remote_device *rdev, void *buffer);
154 void rpmsg_free_channel(struct rpmsg_channel *rp_chnl);
155 void *rpmsg_get_rx_buffer(struct remote_device *rdev, unsigned long *len,
156 unsigned short *idx);
157 int rpmsg_get_address(unsigned long *bitmap, int size);
158 int rpmsg_release_address(unsigned long *bitmap, int size, int addr);
159 int rpmsg_is_address_set(unsigned long *bitmap, int size, int addr);
160 int rpmsg_set_address(unsigned long *bitmap, int size, int addr);
161 void rpmsg_ns_callback(struct rpmsg_channel *server_chnl,
162 void *data, int len, void *priv, unsigned long src);
164 /* Remote device functions */
165 int rpmsg_rdev_init(struct remote_device **rdev, int dev_id, int role,
166 rpmsg_chnl_cb_t channel_created,
167 rpmsg_chnl_cb_t channel_destroyed,
168 rpmsg_rx_cb_t default_cb);
169 void rpmsg_rdev_deinit(struct remote_device *rdev);
170 struct llist *rpmsg_rdev_get_chnl_node_from_id(struct remote_device *rdev,
171 char *rp_chnl_id);
172 struct llist *rpmsg_rdev_get_endpoint_from_addr(struct remote_device *rdev,
173 unsigned long addr);
174 int rpmsg_rdev_notify(struct remote_device *rdev);
175 int rpmsg_rdev_create_virtqueues(struct virtio_device *dev, int flags, int nvqs,
176 const char *names[], vq_callback * callbacks[],
177 struct virtqueue *vqs[]);
178 unsigned char rpmsg_rdev_get_status(struct virtio_device *dev);
180 void rpmsg_rdev_set_status(struct virtio_device *dev, unsigned char status);
182 uint32_t rpmsg_rdev_get_feature(struct virtio_device *dev);
184 void rpmsg_rdev_set_feature(struct virtio_device *dev, uint32_t feature);
186 uint32_t rpmsg_rdev_negotiate_feature(struct virtio_device *dev,
187 uint32_t features);
188 /*
189 * Read/write a variable amount from the device specific (ie, network)
190 * configuration region. This region is encoded in the same endian as
191 * the guest.
192 */
193 void rpmsg_rdev_read_config(struct virtio_device *dev, uint32_t offset,
194 void *dst, int length);
195 void rpmsg_rdev_write_config(struct virtio_device *dev, uint32_t offset,
196 void *src, int length);
197 void rpmsg_rdev_reset(struct virtio_device *dev);
199 #endif /* _RPMSG_CORE_H_ */