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/rpmsg.h"
39 #include "metal/mutex.h"
40 #include "metal/list.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_INIT (RPMSG_ERROR_BASE - 7)
80 #define RPMSG_ERR_DEV_ADDR (RPMSG_ERROR_BASE - 8)
82 /* Zero-Copy extension macros */
83 #define RPMSG_HDR_FROM_BUF(buf) (struct rpmsg_hdr *)((char*)buf - \
84 sizeof(struct rpmsg_hdr))
86 #if (RPMSG_DEBUG == true)
87 #define RPMSG_ASSERT(_exp, _msg) do{ \
88 if (!(_exp)){ openamp_print("%s - "_msg, __func__); while(1);} \
89 } while(0)
90 #else
91 #define RPMSG_ASSERT(_exp, _msg) if (!(_exp)) while(1)
92 #endif
94 struct rpmsg_channel;
95 typedef void (*rpmsg_rx_cb_t) (struct rpmsg_channel *, void *, int, void *,
96 unsigned long);
97 typedef void (*rpmsg_chnl_cb_t) (struct rpmsg_channel * rp_chl);
98 /**
99 * remote_device
100 *
101 * This structure is maintained by RPMSG driver to represent remote device/core.
102 *
103 * @virtd_dev - virtio device for remote core
104 * @rvq - Rx virtqueue for virtio device
105 * @tvq - Tx virtqueue for virtio device
106 * @proc - reference to remote processor
107 * @rp_channels - rpmsg channels list for the device
108 * @rp_endpoints - rpmsg endpoints list for the device
109 * @mem_pool - shared memory pool
110 * @bitmap - bitmap for channels addresses
111 * @channel_created - create channel callback
112 * @channel_destroyed - delete channel callback
113 * @default_cb - default callback handler for RX data on channel
114 * @lock - remote device mutex
115 * @role - role of the remote device, RPMSG_MASTER/RPMSG_REMOTE
116 * @state - remote device state, IDLE/ACTIVE
117 * @support_ns - if device supports name service announcement
118 *
119 */
120 struct remote_device {
121 struct virtio_device virt_dev;
122 struct virtqueue *rvq;
123 struct virtqueue *tvq;
124 struct hil_proc *proc;
125 struct metal_list rp_channels;
126 struct metal_list rp_endpoints;
127 struct sh_mem_pool *mem_pool;
128 unsigned long bitmap[RPMSG_ADDR_BMP_SIZE];
129 rpmsg_chnl_cb_t channel_created;
130 rpmsg_chnl_cb_t channel_destroyed;
131 rpmsg_rx_cb_t default_cb;
132 metal_mutex_t lock;
133 unsigned int role;
134 unsigned int state;
135 int support_ns;
136 };
138 /* Core functions */
139 int rpmsg_start_ipc(struct remote_device *rdev);
140 struct rpmsg_channel *_rpmsg_create_channel(struct remote_device *rdev,
141 char *name, unsigned long src,
142 unsigned long dst);
143 void _rpmsg_delete_channel(struct rpmsg_channel *rp_chnl);
144 struct rpmsg_endpoint *_create_endpoint(struct remote_device *rdev,
145 rpmsg_rx_cb_t cb, void *priv,
146 unsigned long addr);
147 void _destroy_endpoint(struct remote_device *rdev,
148 struct rpmsg_endpoint *rp_ept);
149 int rpmsg_send_ns_message(struct remote_device *rdev,
150 struct rpmsg_channel *rp_chnl, unsigned long flags);
151 int rpmsg_enqueue_buffer(struct remote_device *rdev, void *buffer,
152 unsigned long len, unsigned short idx);
153 void rpmsg_return_buffer(struct remote_device *rdev, void *buffer,
154 unsigned long len, unsigned short idx);
155 void *rpmsg_get_tx_buffer(struct remote_device *rdev, unsigned long *len,
156 unsigned short *idx);
157 void rpmsg_free_buffer(struct remote_device *rdev, void *buffer);
158 void rpmsg_free_channel(struct rpmsg_channel *rp_chnl);
159 void *rpmsg_get_rx_buffer(struct remote_device *rdev, unsigned long *len,
160 unsigned short *idx);
161 int rpmsg_get_address(unsigned long *bitmap, int size);
162 int rpmsg_release_address(unsigned long *bitmap, int size, int addr);
163 int rpmsg_is_address_set(unsigned long *bitmap, int size, int addr);
164 int rpmsg_set_address(unsigned long *bitmap, int size, int addr);
165 void rpmsg_ns_callback(struct rpmsg_channel *server_chnl,
166 void *data, int len, void *priv, unsigned long src);
168 /* Remote device functions */
169 int rpmsg_rdev_init(struct hil_proc *proc,
170 struct remote_device **rdev, int role,
171 rpmsg_chnl_cb_t channel_created,
172 rpmsg_chnl_cb_t channel_destroyed,
173 rpmsg_rx_cb_t default_cb);
174 void rpmsg_rdev_deinit(struct remote_device *rdev);
175 int rpmsg_rdev_remote_ready(struct remote_device *rdev);
176 struct rpmsg_channel *rpmsg_rdev_get_chnl_from_id(struct remote_device *rdev,
177 char *rp_chnl_id);
178 struct rpmsg_endpoint *rpmsg_rdev_get_endpoint_from_addr(
179 struct remote_device *rdev,
180 unsigned long addr);
181 int rpmsg_rdev_notify(struct remote_device *rdev);
182 int rpmsg_rdev_create_virtqueues(struct virtio_device *dev, int flags, int nvqs,
183 const char *names[], vq_callback * callbacks[],
184 struct virtqueue *vqs[]);
185 unsigned char rpmsg_rdev_get_status(struct virtio_device *dev);
187 void rpmsg_rdev_set_status(struct virtio_device *dev, unsigned char status);
189 uint32_t rpmsg_rdev_get_feature(struct virtio_device *dev);
191 void rpmsg_rdev_set_feature(struct virtio_device *dev, uint32_t feature);
193 uint32_t rpmsg_rdev_negotiate_feature(struct virtio_device *dev,
194 uint32_t features);
195 /*
196 * Read/write a variable amount from the device specific (ie, network)
197 * configuration region. This region is encoded in the same endian as
198 * the guest.
199 */
200 void rpmsg_rdev_read_config(struct virtio_device *dev, uint32_t offset,
201 void *dst, int length);
202 void rpmsg_rdev_write_config(struct virtio_device *dev, uint32_t offset,
203 void *src, int length);
204 void rpmsg_rdev_reset(struct virtio_device *dev);
206 #endif /* _RPMSG_CORE_H_ */