blob: 35a0f39fd09ba7bd59c50e44c90bde8c9537474a [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/**
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700101 * struct rpmsg_channel_info - channel info representation
102 * @name: name of service
103 * @src: local address
104 * @dst: destination address
105 */
106struct rpmsg_channel_info {
107 char name[RPMSG_NAME_SIZE];
108 u32 src;
109 u32 dst;
110};
111
112/**
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700113 * rpmsg_device - device that belong to the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200114 * @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
121 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700122struct rpmsg_device {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200123 struct virtproc_info *vrp;
124 struct device dev;
125 struct rpmsg_device_id id;
126 u32 src;
127 u32 dst;
128 struct rpmsg_endpoint *ept;
129 bool announce;
130};
131
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700132typedef void (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200133
134/**
135 * struct rpmsg_endpoint - binds a local rpmsg address to its user
136 * @rpdev: rpmsg channel device
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300137 * @refcount: when this drops to zero, the ept is deallocated
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200138 * @cb: rx callback handler
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300139 * @cb_lock: must be taken before accessing/changing @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200140 * @addr: local rpmsg address
141 * @priv: private data for the driver's use
142 *
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.
145 *
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.
152 *
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()).
156 */
157struct rpmsg_endpoint {
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700158 struct rpmsg_device *rpdev;
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300159 struct kref refcount;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200160 rpmsg_rx_cb_t cb;
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300161 struct mutex cb_lock;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200162 u32 addr;
163 void *priv;
164};
165
166/**
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
173 */
174struct rpmsg_driver {
175 struct device_driver drv;
176 const struct rpmsg_device_id *id_table;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700177 int (*probe)(struct rpmsg_device *dev);
178 void (*remove)(struct rpmsg_device *dev);
179 void (*callback)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200180};
181
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700182int register_rpmsg_device(struct rpmsg_device *dev);
183void unregister_rpmsg_device(struct rpmsg_device *dev);
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500184int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200185void unregister_rpmsg_driver(struct rpmsg_driver *drv);
186void rpmsg_destroy_ept(struct rpmsg_endpoint *);
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700187struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700188 rpmsg_rx_cb_t cb, void *priv,
189 struct rpmsg_channel_info chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200190int
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700191rpmsg_send_offchannel_raw(struct rpmsg_device *, u32, u32, void *, int, bool);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200192
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500193/* use a macro to avoid include chaining to get THIS_MODULE */
194#define register_rpmsg_driver(drv) \
195 __register_rpmsg_driver(drv, THIS_MODULE)
196
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200197/**
Andrew F. Davisf3d9f1c2016-05-04 17:01:38 -0500198 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
199 * @__rpmsg_driver: rpmsg_driver struct
200 *
201 * Helper macro for rpmsg drivers which do not do anything special in module
202 * init/exit. This eliminates a lot of boilerplate. Each module may only
203 * use this macro once, and calling it replaces module_init() and module_exit()
204 */
205#define module_rpmsg_driver(__rpmsg_driver) \
206 module_driver(__rpmsg_driver, register_rpmsg_driver, \
207 unregister_rpmsg_driver)
208
209/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200210 * rpmsg_send() - send a message across to the remote processor
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700211 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200212 * @data: payload of message
213 * @len: length of payload
214 *
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700215 * This function sends @data of length @len on the @ept endpoint.
216 * The message will be sent to the remote processor which the @ept
217 * endpoint belongs to, using @ept's address and its associated rpmsg
218 * device destination addresses.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200219 * In case there are no TX buffers available, the function will block until
220 * one becomes available, or a timeout of 15 seconds elapses. When the latter
221 * happens, -ERESTARTSYS is returned.
222 *
223 * Can only be called from process context (for now).
224 *
225 * Returns 0 on success and an appropriate error value on failure.
226 */
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700227static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200228{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700229 struct rpmsg_device *rpdev = ept->rpdev;
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700230 u32 src = ept->addr, dst = rpdev->dst;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200231
232 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
233}
234
235/**
236 * rpmsg_sendto() - send a message across to the remote processor, specify dst
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700237 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200238 * @data: payload of message
239 * @len: length of payload
240 * @dst: destination address
241 *
242 * This function sends @data of length @len to the remote @dst address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700243 * The message will be sent to the remote processor which the @ept
244 * endpoint belongs to, using @ept's address as source.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200245 * In case there are no TX buffers available, the function will block until
246 * one becomes available, or a timeout of 15 seconds elapses. When the latter
247 * happens, -ERESTARTSYS is returned.
248 *
249 * Can only be called from process context (for now).
250 *
251 * Returns 0 on success and an appropriate error value on failure.
252 */
253static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700254int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200255{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700256 struct rpmsg_device *rpdev = ept->rpdev;
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700257 u32 src = ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200258
259 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
260}
261
262/**
263 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700264 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200265 * @src: source address
266 * @dst: destination address
267 * @data: payload of message
268 * @len: length of payload
269 *
270 * This function sends @data of length @len to the remote @dst address,
271 * and uses @src as the source address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700272 * The message will be sent to the remote processor which the @ept
273 * endpoint belongs to.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200274 * In case there are no TX buffers available, the function will block until
275 * one becomes available, or a timeout of 15 seconds elapses. When the latter
276 * happens, -ERESTARTSYS is returned.
277 *
278 * Can only be called from process context (for now).
279 *
280 * Returns 0 on success and an appropriate error value on failure.
281 */
282static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700283int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500284 void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200285{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700286 struct rpmsg_device *rpdev = ept->rpdev;
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700287
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200288 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
289}
290
291/**
292 * rpmsg_send() - send a message across to the remote processor
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700293 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200294 * @data: payload of message
295 * @len: length of payload
296 *
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700297 * This function sends @data of length @len on the @ept endpoint.
298 * The message will be sent to the remote processor which the @ept
299 * endpoint belongs to, using @ept's address as source and its associated
300 * rpdev's address as destination.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200301 * In case there are no TX buffers available, the function will immediately
302 * return -ENOMEM without waiting until one becomes available.
303 *
304 * Can only be called from process context (for now).
305 *
306 * Returns 0 on success and an appropriate error value on failure.
307 */
308static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700309int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200310{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700311 struct rpmsg_device *rpdev = ept->rpdev;
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700312 u32 src = ept->addr, dst = rpdev->dst;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200313
314 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
315}
316
317/**
318 * rpmsg_sendto() - send a message across to the remote processor, specify dst
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700319 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200320 * @data: payload of message
321 * @len: length of payload
322 * @dst: destination address
323 *
324 * This function sends @data of length @len to the remote @dst address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700325 * The message will be sent to the remote processor which the @ept
326 * endpoint belongs to, using @ept's address as source.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200327 * In case there are no TX buffers available, the function will immediately
328 * return -ENOMEM without waiting until one becomes available.
329 *
330 * Can only be called from process context (for now).
331 *
332 * Returns 0 on success and an appropriate error value on failure.
333 */
334static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700335int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200336{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700337 struct rpmsg_device *rpdev = ept->rpdev;
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700338 u32 src = ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200339
340 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
341}
342
343/**
344 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700345 * @ept: the rpmsg endpoint
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200346 * @src: source address
347 * @dst: destination address
348 * @data: payload of message
349 * @len: length of payload
350 *
351 * This function sends @data of length @len to the remote @dst address,
352 * and uses @src as the source address.
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700353 * The message will be sent to the remote processor which the @ept
354 * endpoint belongs to.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200355 * In case there are no TX buffers available, the function will immediately
356 * return -ENOMEM without waiting until one becomes available.
357 *
358 * Can only be called from process context (for now).
359 *
360 * Returns 0 on success and an appropriate error value on failure.
361 */
362static inline
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700363int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500364 void *data, int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200365{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700366 struct rpmsg_device *rpdev = ept->rpdev;
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700367
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200368 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
369}
370
371#endif /* _LINUX_RPMSG_H */