blob: d54458effd5490896a9c205d81208fa6b0f9cec5 [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;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070099struct rpmsg_device;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700100struct rpmsg_endpoint;
101struct rpmsg_device_ops;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700102struct rpmsg_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200103
104/**
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700105 * struct rpmsg_channel_info - channel info representation
106 * @name: name of service
107 * @src: local address
108 * @dst: destination address
109 */
110struct rpmsg_channel_info {
111 char name[RPMSG_NAME_SIZE];
112 u32 src;
113 u32 dst;
114};
115
116/**
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700117 * rpmsg_device - device that belong to the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200118 * @vrp: the remote processor this channel belongs to
119 * @dev: the device struct
120 * @id: device id (used to match between rpmsg drivers and devices)
121 * @src: local address
122 * @dst: destination address
123 * @ept: the rpmsg endpoint of this channel
124 * @announce: if set, rpmsg will announce the creation/removal of this channel
125 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700126struct rpmsg_device {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200127 struct virtproc_info *vrp;
128 struct device dev;
129 struct rpmsg_device_id id;
130 u32 src;
131 u32 dst;
132 struct rpmsg_endpoint *ept;
133 bool announce;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700134
135 const struct rpmsg_device_ops *ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200136};
137
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700138typedef void (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200139
140/**
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700141 * struct rpmsg_device_ops - indirection table for the rpmsg_device operations
142 * @create_ept: create backend-specific endpoint, requried
143 * @announce_create: announce presence of new channel, optional
144 * @announce_destroy: announce destruction of channel, optional
145 *
146 * Indirection table for the operations that a rpmsg backend should implement.
147 * @announce_create and @announce_destroy are optional as the backend might
148 * advertise new channels implicitly by creating the endpoints.
149 */
150struct rpmsg_device_ops {
151 struct rpmsg_endpoint *(*create_ept)(struct rpmsg_device *rpdev,
152 rpmsg_rx_cb_t cb, void *priv,
153 struct rpmsg_channel_info chinfo);
154
155 int (*announce_create)(struct rpmsg_device *ept);
156 int (*announce_destroy)(struct rpmsg_device *ept);
157};
158
159/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200160 * struct rpmsg_endpoint - binds a local rpmsg address to its user
161 * @rpdev: rpmsg channel device
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300162 * @refcount: when this drops to zero, the ept is deallocated
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200163 * @cb: rx callback handler
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300164 * @cb_lock: must be taken before accessing/changing @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200165 * @addr: local rpmsg address
166 * @priv: private data for the driver's use
167 *
168 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
169 * it binds an rpmsg address with an rx callback handler.
170 *
171 * Simple rpmsg drivers shouldn't use this struct directly, because
172 * things just work: every rpmsg driver provides an rx callback upon
173 * registering to the bus, and that callback is then bound to its rpmsg
174 * address when the driver is probed. When relevant inbound messages arrive
175 * (i.e. messages which their dst address equals to the src address of
176 * the rpmsg channel), the driver's handler is invoked to process it.
177 *
178 * More complicated drivers though, that do need to allocate additional rpmsg
179 * addresses, and bind them to different rx callbacks, must explicitly
180 * create additional endpoints by themselves (see rpmsg_create_ept()).
181 */
182struct rpmsg_endpoint {
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700183 struct rpmsg_device *rpdev;
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300184 struct kref refcount;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200185 rpmsg_rx_cb_t cb;
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300186 struct mutex cb_lock;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200187 u32 addr;
188 void *priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700189
190 const struct rpmsg_endpoint_ops *ops;
191};
192
193/**
194 * struct rpmsg_endpoint_ops - indirection table for rpmsg_endpoint operations
195 * @destroy_ept: destroy the given endpoint, required
196 * @send: see @rpmsg_send(), required
197 * @sendto: see @rpmsg_sendto(), optional
198 * @send_offchannel: see @rpmsg_send_offchannel(), optional
199 * @trysend: see @rpmsg_trysend(), required
200 * @trysendto: see @rpmsg_trysendto(), optional
201 * @trysend_offchannel: see @rpmsg_trysend_offchannel(), optional
202 *
203 * Indirection table for the operations that a rpmsg backend should implement.
204 * In addition to @destroy_ept, the backend must at least implement @send and
205 * @trysend, while the variants sending data off-channel are optional.
206 */
207struct rpmsg_endpoint_ops {
208 void (*destroy_ept)(struct rpmsg_endpoint *ept);
209
210 int (*send)(struct rpmsg_endpoint *ept, void *data, int len);
211 int (*sendto)(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
212 int (*send_offchannel)(struct rpmsg_endpoint *ept, u32 src, u32 dst,
213 void *data, int len);
214
215 int (*trysend)(struct rpmsg_endpoint *ept, void *data, int len);
216 int (*trysendto)(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
217 int (*trysend_offchannel)(struct rpmsg_endpoint *ept, u32 src, u32 dst,
218 void *data, int len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200219};
220
221/**
222 * struct rpmsg_driver - rpmsg driver struct
223 * @drv: underlying device driver
224 * @id_table: rpmsg ids serviced by this driver
225 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
226 * @remove: invoked when the rpmsg channel is removed
227 * @callback: invoked when an inbound message is received on the channel
228 */
229struct rpmsg_driver {
230 struct device_driver drv;
231 const struct rpmsg_device_id *id_table;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700232 int (*probe)(struct rpmsg_device *dev);
233 void (*remove)(struct rpmsg_device *dev);
234 void (*callback)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200235};
236
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700237int register_rpmsg_device(struct rpmsg_device *dev);
238void unregister_rpmsg_device(struct rpmsg_device *dev);
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500239int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200240void unregister_rpmsg_driver(struct rpmsg_driver *drv);
241void rpmsg_destroy_ept(struct rpmsg_endpoint *);
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700242struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700243 rpmsg_rx_cb_t cb, void *priv,
244 struct rpmsg_channel_info chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200245
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500246/* use a macro to avoid include chaining to get THIS_MODULE */
247#define register_rpmsg_driver(drv) \
248 __register_rpmsg_driver(drv, THIS_MODULE)
249
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200250/**
Andrew F. Davisf3d9f1c2016-05-04 17:01:38 -0500251 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
252 * @__rpmsg_driver: rpmsg_driver struct
253 *
254 * Helper macro for rpmsg drivers which do not do anything special in module
255 * init/exit. This eliminates a lot of boilerplate. Each module may only
256 * use this macro once, and calling it replaces module_init() and module_exit()
257 */
258#define module_rpmsg_driver(__rpmsg_driver) \
259 module_driver(__rpmsg_driver, register_rpmsg_driver, \
260 unregister_rpmsg_driver)
261
262/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200263 * rpmsg_send() - send a message across to the remote processor
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700264 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200265 * @data: payload of message
266 * @len: length of payload
267 *
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700268 * This function sends @data of length @len on the @ept endpoint.
269 * The message will be sent to the remote processor which the @ept
270 * endpoint belongs to, using @ept's address and its associated rpmsg
271 * device destination addresses.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200272 * 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.
275 *
276 * Can only be called from process context (for now).
277 *
278 * Returns 0 on success and an appropriate error value on failure.
279 */
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700280static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200281{
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700282 return ept->ops->send(ept, data, len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200283}
284
285/**
286 * rpmsg_sendto() - send a message across to the remote processor, specify dst
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700287 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200288 * @data: payload of message
289 * @len: length of payload
290 * @dst: destination address
291 *
292 * This function sends @data of length @len to the remote @dst address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700293 * The message will be sent to the remote processor which the @ept
294 * endpoint belongs to, using @ept's address as source.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200295 * In case there are no TX buffers available, the function will block until
296 * one becomes available, or a timeout of 15 seconds elapses. When the latter
297 * happens, -ERESTARTSYS is returned.
298 *
299 * Can only be called from process context (for now).
300 *
301 * Returns 0 on success and an appropriate error value on failure.
302 */
303static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700304int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200305{
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700306 return ept->ops->sendto(ept, data, len, dst);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200307}
308
309/**
310 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700311 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200312 * @src: source address
313 * @dst: destination address
314 * @data: payload of message
315 * @len: length of payload
316 *
317 * This function sends @data of length @len to the remote @dst address,
318 * and uses @src as the source address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700319 * The message will be sent to the remote processor which the @ept
320 * endpoint belongs to.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200321 * In case there are no TX buffers available, the function will block until
322 * one becomes available, or a timeout of 15 seconds elapses. When the latter
323 * happens, -ERESTARTSYS is returned.
324 *
325 * Can only be called from process context (for now).
326 *
327 * Returns 0 on success and an appropriate error value on failure.
328 */
329static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700330int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500331 void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200332{
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700333 return ept->ops->send_offchannel(ept, src, dst, data, len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200334}
335
336/**
337 * rpmsg_send() - send a message across to the remote processor
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700338 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200339 * @data: payload of message
340 * @len: length of payload
341 *
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700342 * This function sends @data of length @len on the @ept endpoint.
343 * The message will be sent to the remote processor which the @ept
344 * endpoint belongs to, using @ept's address as source and its associated
345 * rpdev's address as destination.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200346 * In case there are no TX buffers available, the function will immediately
347 * return -ENOMEM without waiting until one becomes available.
348 *
349 * Can only be called from process context (for now).
350 *
351 * Returns 0 on success and an appropriate error value on failure.
352 */
353static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700354int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200355{
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700356 return ept->ops->trysend(ept, data, len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200357}
358
359/**
360 * rpmsg_sendto() - send a message across to the remote processor, specify dst
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700361 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200362 * @data: payload of message
363 * @len: length of payload
364 * @dst: destination address
365 *
366 * This function sends @data of length @len to the remote @dst address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700367 * The message will be sent to the remote processor which the @ept
368 * endpoint belongs to, using @ept's address as source.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200369 * In case there are no TX buffers available, the function will immediately
370 * return -ENOMEM without waiting until one becomes available.
371 *
372 * Can only be called from process context (for now).
373 *
374 * Returns 0 on success and an appropriate error value on failure.
375 */
376static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700377int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200378{
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700379 return ept->ops->trysendto(ept, data, len, dst);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200380}
381
382/**
383 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700384 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200385 * @src: source address
386 * @dst: destination address
387 * @data: payload of message
388 * @len: length of payload
389 *
390 * This function sends @data of length @len to the remote @dst address,
391 * and uses @src as the source address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700392 * The message will be sent to the remote processor which the @ept
393 * endpoint belongs to.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200394 * In case there are no TX buffers available, the function will immediately
395 * return -ENOMEM without waiting until one becomes available.
396 *
397 * Can only be called from process context (for now).
398 *
399 * Returns 0 on success and an appropriate error value on failure.
400 */
401static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700402int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500403 void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200404{
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700405 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200406}
407
408#endif /* _LINUX_RPMSG_H */