2 * Remote processor messaging
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name Texas Instruments nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef _LINUX_RPMSG_H
36 #define _LINUX_RPMSG_H
38 #include <linux/types.h>
39 #include <linux/device.h>
40 #include <linux/mod_devicetable.h>
41 #include <linux/kref.h>
42 #include <linux/mutex.h>
44 /* The feature bitmap for virtio rpmsg */
45 #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
48 * struct rpmsg_hdr - common header for all rpmsg messages
49 * @src: source address
50 * @dst: destination address
51 * @reserved: reserved for future use
52 * @len: length of payload (in bytes)
53 * @flags: message flags
54 * @data: @len bytes of message payload data
56 * Every message sent(/received) on the rpmsg bus begins with this header.
68 * struct rpmsg_ns_msg - dynamic name service announcement message
69 * @name: name of remote service that is published
70 * @addr: address of remote service that is published
71 * @flags: indicates whether service is created or destroyed
73 * This message is sent across to publish a new service, or announce
74 * about its removal. When we receive these messages, an appropriate
75 * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
76 * or ->remove() handler of the appropriate rpmsg driver will be invoked
77 * (if/as-soon-as one is registered).
80 char name[RPMSG_NAME_SIZE];
86 * enum rpmsg_ns_flags - dynamic name service announcement flags
88 * @RPMSG_NS_CREATE: a new remote service was just created
89 * @RPMSG_NS_DESTROY: a known remote service was just destroyed
96 #define RPMSG_ADDR_ANY 0xFFFFFFFF
101 * struct rpmsg_channel_info - channel info representation
102 * @name: name of service
103 * @src: local address
104 * @dst: destination address
106 struct rpmsg_channel_info {
107 char name[RPMSG_NAME_SIZE];
113 * rpmsg_channel - devices that belong to the rpmsg bus are called channels
114 * @vrp: the remote processor this channel belongs to
115 * @dev: the device struct
116 * @id: device id (used to match between rpmsg drivers and devices)
117 * @src: local address
118 * @dst: destination address
119 * @ept: the rpmsg endpoint of this channel
120 * @announce: if set, rpmsg will announce the creation/removal of this channel
122 struct rpmsg_channel {
123 struct virtproc_info *vrp;
125 struct rpmsg_device_id id;
128 struct rpmsg_endpoint *ept;
132 typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
135 * struct rpmsg_endpoint - binds a local rpmsg address to its user
136 * @rpdev: rpmsg channel device
137 * @refcount: when this drops to zero, the ept is deallocated
138 * @cb: rx callback handler
139 * @cb_lock: must be taken before accessing/changing @cb
140 * @addr: local rpmsg address
141 * @priv: private data for the driver's use
143 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
144 * it binds an rpmsg address with an rx callback handler.
146 * Simple rpmsg drivers shouldn't use this struct directly, because
147 * things just work: every rpmsg driver provides an rx callback upon
148 * registering to the bus, and that callback is then bound to its rpmsg
149 * address when the driver is probed. When relevant inbound messages arrive
150 * (i.e. messages which their dst address equals to the src address of
151 * the rpmsg channel), the driver's handler is invoked to process it.
153 * More complicated drivers though, that do need to allocate additional rpmsg
154 * addresses, and bind them to different rx callbacks, must explicitly
155 * create additional endpoints by themselves (see rpmsg_create_ept()).
157 struct rpmsg_endpoint {
158 struct rpmsg_channel *rpdev;
159 struct kref refcount;
161 struct mutex cb_lock;
167 * struct rpmsg_driver - rpmsg driver struct
168 * @drv: underlying device driver
169 * @id_table: rpmsg ids serviced by this driver
170 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
171 * @remove: invoked when the rpmsg channel is removed
172 * @callback: invoked when an inbound message is received on the channel
174 struct rpmsg_driver {
175 struct device_driver drv;
176 const struct rpmsg_device_id *id_table;
177 int (*probe)(struct rpmsg_channel *dev);
178 void (*remove)(struct rpmsg_channel *dev);
179 void (*callback)(struct rpmsg_channel *, void *, int, void *, u32);
182 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
183 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
184 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
185 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *,
186 rpmsg_rx_cb_t cb, void *priv,
187 struct rpmsg_channel_info chinfo);
189 rpmsg_send_offchannel_raw(struct rpmsg_channel *, u32, u32, void *, int, bool);
191 /* use a macro to avoid include chaining to get THIS_MODULE */
192 #define register_rpmsg_driver(drv) \
193 __register_rpmsg_driver(drv, THIS_MODULE)
196 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
197 * @__rpmsg_driver: rpmsg_driver struct
199 * Helper macro for rpmsg drivers which do not do anything special in module
200 * init/exit. This eliminates a lot of boilerplate. Each module may only
201 * use this macro once, and calling it replaces module_init() and module_exit()
203 #define module_rpmsg_driver(__rpmsg_driver) \
204 module_driver(__rpmsg_driver, register_rpmsg_driver, \
205 unregister_rpmsg_driver)
208 * rpmsg_send() - send a message across to the remote processor
209 * @ept: the rpmsg endpoint
210 * @data: payload of message
211 * @len: length of payload
213 * This function sends @data of length @len on the @ept endpoint.
214 * The message will be sent to the remote processor which the @ept
215 * endpoint belongs to, using @ept's address and its associated rpmsg
216 * device destination addresses.
217 * In case there are no TX buffers available, the function will block until
218 * one becomes available, or a timeout of 15 seconds elapses. When the latter
219 * happens, -ERESTARTSYS is returned.
221 * Can only be called from process context (for now).
223 * Returns 0 on success and an appropriate error value on failure.
225 static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
227 struct rpmsg_channel *rpdev = ept->rpdev;
228 u32 src = ept->addr, dst = rpdev->dst;
230 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
234 * rpmsg_sendto() - send a message across to the remote processor, specify dst
235 * @ept: the rpmsg endpoint
236 * @data: payload of message
237 * @len: length of payload
238 * @dst: destination address
240 * This function sends @data of length @len to the remote @dst address.
241 * The message will be sent to the remote processor which the @ept
242 * endpoint belongs to, using @ept's address as source.
243 * In case there are no TX buffers available, the function will block until
244 * one becomes available, or a timeout of 15 seconds elapses. When the latter
245 * happens, -ERESTARTSYS is returned.
247 * Can only be called from process context (for now).
249 * Returns 0 on success and an appropriate error value on failure.
252 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
254 struct rpmsg_channel *rpdev = ept->rpdev;
257 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
261 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
262 * @ept: the rpmsg endpoint
263 * @src: source address
264 * @dst: destination address
265 * @data: payload of message
266 * @len: length of payload
268 * This function sends @data of length @len to the remote @dst address,
269 * and uses @src as the source address.
270 * The message will be sent to the remote processor which the @ept
271 * endpoint belongs to.
272 * In case there are no TX buffers available, the function will block until
273 * one becomes available, or a timeout of 15 seconds elapses. When the latter
274 * happens, -ERESTARTSYS is returned.
276 * Can only be called from process context (for now).
278 * Returns 0 on success and an appropriate error value on failure.
281 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
284 struct rpmsg_channel *rpdev = ept->rpdev;
286 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
290 * rpmsg_send() - send a message across to the remote processor
291 * @ept: the rpmsg endpoint
292 * @data: payload of message
293 * @len: length of payload
295 * This function sends @data of length @len on the @ept endpoint.
296 * The message will be sent to the remote processor which the @ept
297 * endpoint belongs to, using @ept's address as source and its associated
298 * rpdev's address as destination.
299 * In case there are no TX buffers available, the function will immediately
300 * return -ENOMEM without waiting until one becomes available.
302 * Can only be called from process context (for now).
304 * Returns 0 on success and an appropriate error value on failure.
307 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
309 struct rpmsg_channel *rpdev = ept->rpdev;
310 u32 src = ept->addr, dst = rpdev->dst;
312 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
316 * rpmsg_sendto() - send a message across to the remote processor, specify dst
317 * @ept: the rpmsg endpoint
318 * @data: payload of message
319 * @len: length of payload
320 * @dst: destination address
322 * This function sends @data of length @len to the remote @dst address.
323 * The message will be sent to the remote processor which the @ept
324 * endpoint belongs to, using @ept's address as source.
325 * In case there are no TX buffers available, the function will immediately
326 * return -ENOMEM without waiting until one becomes available.
328 * Can only be called from process context (for now).
330 * Returns 0 on success and an appropriate error value on failure.
333 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
335 struct rpmsg_channel *rpdev = ept->rpdev;
338 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
342 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
343 * @ept: the rpmsg endpoint
344 * @src: source address
345 * @dst: destination address
346 * @data: payload of message
347 * @len: length of payload
349 * This function sends @data of length @len to the remote @dst address,
350 * and uses @src as the source address.
351 * The message will be sent to the remote processor which the @ept
352 * endpoint belongs to.
353 * In case there are no TX buffers available, the function will immediately
354 * return -ENOMEM without waiting until one becomes available.
356 * Can only be called from process context (for now).
358 * Returns 0 on success and an appropriate error value on failure.
361 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
364 struct rpmsg_channel *rpdev = ept->rpdev;
366 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
369 #endif /* _LINUX_RPMSG_H */