blob: 2b97c711a5e324513b8661fcd96b32861b35260a [file] [log] [blame]
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001/*
2 * Remote processor messaging
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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
17 * distribution.
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.
21 *
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.
33 */
34
35#ifndef _LINUX_RPMSG_H
36#define _LINUX_RPMSG_H
37
38#include <linux/types.h>
39#include <linux/device.h>
40#include <linux/mod_devicetable.h>
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +030041#include <linux/kref.h>
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +030042#include <linux/mutex.h>
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020043
44/* The feature bitmap for virtio rpmsg */
45#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
46
47/**
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
55 *
56 * Every message sent(/received) on the rpmsg bus begins with this header.
57 */
58struct rpmsg_hdr {
59 u32 src;
60 u32 dst;
61 u32 reserved;
62 u16 len;
63 u16 flags;
64 u8 data[0];
65} __packed;
66
67/**
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
72 *
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).
78 */
79struct rpmsg_ns_msg {
80 char name[RPMSG_NAME_SIZE];
81 u32 addr;
82 u32 flags;
83} __packed;
84
85/**
86 * enum rpmsg_ns_flags - dynamic name service announcement flags
87 *
88 * @RPMSG_NS_CREATE: a new remote service was just created
89 * @RPMSG_NS_DESTROY: a known remote service was just destroyed
90 */
91enum rpmsg_ns_flags {
92 RPMSG_NS_CREATE = 0,
93 RPMSG_NS_DESTROY = 1,
94};
95
96#define RPMSG_ADDR_ANY 0xFFFFFFFF
97
98struct virtproc_info;
99
100/**
101 * rpmsg_channel - devices that belong to the rpmsg bus are called channels
102 * @vrp: the remote processor this channel belongs to
103 * @dev: the device struct
104 * @id: device id (used to match between rpmsg drivers and devices)
105 * @src: local address
106 * @dst: destination address
107 * @ept: the rpmsg endpoint of this channel
108 * @announce: if set, rpmsg will announce the creation/removal of this channel
109 */
110struct rpmsg_channel {
111 struct virtproc_info *vrp;
112 struct device dev;
113 struct rpmsg_device_id id;
114 u32 src;
115 u32 dst;
116 struct rpmsg_endpoint *ept;
117 bool announce;
118};
119
120typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
121
122/**
123 * struct rpmsg_endpoint - binds a local rpmsg address to its user
124 * @rpdev: rpmsg channel device
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300125 * @refcount: when this drops to zero, the ept is deallocated
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200126 * @cb: rx callback handler
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300127 * @cb_lock: must be taken before accessing/changing @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200128 * @addr: local rpmsg address
129 * @priv: private data for the driver's use
130 *
131 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
132 * it binds an rpmsg address with an rx callback handler.
133 *
134 * Simple rpmsg drivers shouldn't use this struct directly, because
135 * things just work: every rpmsg driver provides an rx callback upon
136 * registering to the bus, and that callback is then bound to its rpmsg
137 * address when the driver is probed. When relevant inbound messages arrive
138 * (i.e. messages which their dst address equals to the src address of
139 * the rpmsg channel), the driver's handler is invoked to process it.
140 *
141 * More complicated drivers though, that do need to allocate additional rpmsg
142 * addresses, and bind them to different rx callbacks, must explicitly
143 * create additional endpoints by themselves (see rpmsg_create_ept()).
144 */
145struct rpmsg_endpoint {
146 struct rpmsg_channel *rpdev;
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300147 struct kref refcount;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200148 rpmsg_rx_cb_t cb;
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300149 struct mutex cb_lock;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200150 u32 addr;
151 void *priv;
152};
153
154/**
155 * struct rpmsg_driver - rpmsg driver struct
156 * @drv: underlying device driver
157 * @id_table: rpmsg ids serviced by this driver
158 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
159 * @remove: invoked when the rpmsg channel is removed
160 * @callback: invoked when an inbound message is received on the channel
161 */
162struct rpmsg_driver {
163 struct device_driver drv;
164 const struct rpmsg_device_id *id_table;
165 int (*probe)(struct rpmsg_channel *dev);
166 void (*remove)(struct rpmsg_channel *dev);
167 void (*callback)(struct rpmsg_channel *, void *, int, void *, u32);
168};
169
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500170int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200171void unregister_rpmsg_driver(struct rpmsg_driver *drv);
172void rpmsg_destroy_ept(struct rpmsg_endpoint *);
173struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *,
Anna, Suman09636792016-08-12 18:42:26 -0500174 rpmsg_rx_cb_t cb, void *priv, u32 addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200175int
176rpmsg_send_offchannel_raw(struct rpmsg_channel *, u32, u32, void *, int, bool);
177
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500178/* use a macro to avoid include chaining to get THIS_MODULE */
179#define register_rpmsg_driver(drv) \
180 __register_rpmsg_driver(drv, THIS_MODULE)
181
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200182/**
Andrew F. Davisf3d9f1c2016-05-04 17:01:38 -0500183 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
184 * @__rpmsg_driver: rpmsg_driver struct
185 *
186 * Helper macro for rpmsg drivers which do not do anything special in module
187 * init/exit. This eliminates a lot of boilerplate. Each module may only
188 * use this macro once, and calling it replaces module_init() and module_exit()
189 */
190#define module_rpmsg_driver(__rpmsg_driver) \
191 module_driver(__rpmsg_driver, register_rpmsg_driver, \
192 unregister_rpmsg_driver)
193
194/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200195 * rpmsg_send() - send a message across to the remote processor
196 * @rpdev: the rpmsg channel
197 * @data: payload of message
198 * @len: length of payload
199 *
200 * This function sends @data of length @len on the @rpdev channel.
201 * The message will be sent to the remote processor which the @rpdev
202 * channel belongs to, using @rpdev's source and destination addresses.
203 * In case there are no TX buffers available, the function will block until
204 * one becomes available, or a timeout of 15 seconds elapses. When the latter
205 * happens, -ERESTARTSYS is returned.
206 *
207 * Can only be called from process context (for now).
208 *
209 * Returns 0 on success and an appropriate error value on failure.
210 */
211static inline int rpmsg_send(struct rpmsg_channel *rpdev, void *data, int len)
212{
213 u32 src = rpdev->src, dst = rpdev->dst;
214
215 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
216}
217
218/**
219 * rpmsg_sendto() - send a message across to the remote processor, specify dst
220 * @rpdev: the rpmsg channel
221 * @data: payload of message
222 * @len: length of payload
223 * @dst: destination address
224 *
225 * This function sends @data of length @len to the remote @dst address.
226 * The message will be sent to the remote processor which the @rpdev
227 * channel belongs to, using @rpdev's source address.
228 * In case there are no TX buffers available, the function will block until
229 * one becomes available, or a timeout of 15 seconds elapses. When the latter
230 * happens, -ERESTARTSYS is returned.
231 *
232 * Can only be called from process context (for now).
233 *
234 * Returns 0 on success and an appropriate error value on failure.
235 */
236static inline
237int rpmsg_sendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
238{
239 u32 src = rpdev->src;
240
241 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
242}
243
244/**
245 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
246 * @rpdev: the rpmsg channel
247 * @src: source address
248 * @dst: destination address
249 * @data: payload of message
250 * @len: length of payload
251 *
252 * This function sends @data of length @len to the remote @dst address,
253 * and uses @src as the source address.
254 * The message will be sent to the remote processor which the @rpdev
255 * channel belongs to.
256 * In case there are no TX buffers available, the function will block until
257 * one becomes available, or a timeout of 15 seconds elapses. When the latter
258 * happens, -ERESTARTSYS is returned.
259 *
260 * Can only be called from process context (for now).
261 *
262 * Returns 0 on success and an appropriate error value on failure.
263 */
264static inline
265int rpmsg_send_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500266 void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200267{
268 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
269}
270
271/**
272 * rpmsg_send() - send a message across to the remote processor
273 * @rpdev: the rpmsg channel
274 * @data: payload of message
275 * @len: length of payload
276 *
277 * This function sends @data of length @len on the @rpdev channel.
278 * The message will be sent to the remote processor which the @rpdev
279 * channel belongs to, using @rpdev's source and destination addresses.
280 * In case there are no TX buffers available, the function will immediately
281 * return -ENOMEM without waiting until one becomes available.
282 *
283 * Can only be called from process context (for now).
284 *
285 * Returns 0 on success and an appropriate error value on failure.
286 */
287static inline
288int rpmsg_trysend(struct rpmsg_channel *rpdev, void *data, int len)
289{
290 u32 src = rpdev->src, dst = rpdev->dst;
291
292 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
293}
294
295/**
296 * rpmsg_sendto() - send a message across to the remote processor, specify dst
297 * @rpdev: the rpmsg channel
298 * @data: payload of message
299 * @len: length of payload
300 * @dst: destination address
301 *
302 * This function sends @data of length @len to the remote @dst address.
303 * The message will be sent to the remote processor which the @rpdev
304 * channel belongs to, using @rpdev's source address.
305 * In case there are no TX buffers available, the function will immediately
306 * return -ENOMEM without waiting until one becomes available.
307 *
308 * Can only be called from process context (for now).
309 *
310 * Returns 0 on success and an appropriate error value on failure.
311 */
312static inline
313int rpmsg_trysendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
314{
315 u32 src = rpdev->src;
316
317 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
318}
319
320/**
321 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
322 * @rpdev: the rpmsg channel
323 * @src: source address
324 * @dst: destination address
325 * @data: payload of message
326 * @len: length of payload
327 *
328 * This function sends @data of length @len to the remote @dst address,
329 * and uses @src as the source address.
330 * The message will be sent to the remote processor which the @rpdev
331 * channel belongs to.
332 * In case there are no TX buffers available, the function will immediately
333 * return -ENOMEM without waiting until one becomes available.
334 *
335 * Can only be called from process context (for now).
336 *
337 * Returns 0 on success and an appropriate error value on failure.
338 */
339static inline
340int rpmsg_trysend_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500341 void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200342{
343 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
344}
345
346#endif /* _LINUX_RPMSG_H */