]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/rpmsg.h
rpmsg: Move rpmsg_device API to new file
[mirror_ubuntu-bionic-kernel.git] / include / linux / rpmsg.h
CommitLineData
bcabbcca
OBC
1/*
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>
5a081caa 41#include <linux/kref.h>
15fd943a 42#include <linux/mutex.h>
bcabbcca
OBC
43
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;
36b72c7d
BA
99struct rpmsg_endpoint;
100struct rpmsg_device_ops;
bcabbcca 101
2b263d24
BA
102/**
103 * struct rpmsg_channel_info - channel info representation
104 * @name: name of service
105 * @src: local address
106 * @dst: destination address
107 */
108struct rpmsg_channel_info {
109 char name[RPMSG_NAME_SIZE];
110 u32 src;
111 u32 dst;
112};
113
bcabbcca 114/**
92e1de51 115 * rpmsg_device - device that belong to the rpmsg bus
bcabbcca
OBC
116 * @vrp: the remote processor this channel belongs to
117 * @dev: the device struct
118 * @id: device id (used to match between rpmsg drivers and devices)
119 * @src: local address
120 * @dst: destination address
121 * @ept: the rpmsg endpoint of this channel
122 * @announce: if set, rpmsg will announce the creation/removal of this channel
123 */
92e1de51 124struct rpmsg_device {
bcabbcca
OBC
125 struct virtproc_info *vrp;
126 struct device dev;
127 struct rpmsg_device_id id;
128 u32 src;
129 u32 dst;
130 struct rpmsg_endpoint *ept;
131 bool announce;
36b72c7d
BA
132
133 const struct rpmsg_device_ops *ops;
bcabbcca
OBC
134};
135
92e1de51 136typedef void (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
bcabbcca 137
36b72c7d
BA
138/**
139 * struct rpmsg_device_ops - indirection table for the rpmsg_device operations
140 * @create_ept: create backend-specific endpoint, requried
141 * @announce_create: announce presence of new channel, optional
142 * @announce_destroy: announce destruction of channel, optional
143 *
144 * Indirection table for the operations that a rpmsg backend should implement.
145 * @announce_create and @announce_destroy are optional as the backend might
146 * advertise new channels implicitly by creating the endpoints.
147 */
148struct rpmsg_device_ops {
149 struct rpmsg_endpoint *(*create_ept)(struct rpmsg_device *rpdev,
150 rpmsg_rx_cb_t cb, void *priv,
151 struct rpmsg_channel_info chinfo);
152
153 int (*announce_create)(struct rpmsg_device *ept);
154 int (*announce_destroy)(struct rpmsg_device *ept);
155};
156
bcabbcca
OBC
157/**
158 * struct rpmsg_endpoint - binds a local rpmsg address to its user
159 * @rpdev: rpmsg channel device
5a081caa 160 * @refcount: when this drops to zero, the ept is deallocated
bcabbcca 161 * @cb: rx callback handler
15fd943a 162 * @cb_lock: must be taken before accessing/changing @cb
bcabbcca
OBC
163 * @addr: local rpmsg address
164 * @priv: private data for the driver's use
165 *
166 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
167 * it binds an rpmsg address with an rx callback handler.
168 *
169 * Simple rpmsg drivers shouldn't use this struct directly, because
170 * things just work: every rpmsg driver provides an rx callback upon
171 * registering to the bus, and that callback is then bound to its rpmsg
172 * address when the driver is probed. When relevant inbound messages arrive
173 * (i.e. messages which their dst address equals to the src address of
174 * the rpmsg channel), the driver's handler is invoked to process it.
175 *
176 * More complicated drivers though, that do need to allocate additional rpmsg
177 * addresses, and bind them to different rx callbacks, must explicitly
178 * create additional endpoints by themselves (see rpmsg_create_ept()).
179 */
180struct rpmsg_endpoint {
92e1de51 181 struct rpmsg_device *rpdev;
5a081caa 182 struct kref refcount;
bcabbcca 183 rpmsg_rx_cb_t cb;
15fd943a 184 struct mutex cb_lock;
bcabbcca
OBC
185 u32 addr;
186 void *priv;
187};
188
189/**
190 * struct rpmsg_driver - rpmsg driver struct
191 * @drv: underlying device driver
192 * @id_table: rpmsg ids serviced by this driver
193 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
194 * @remove: invoked when the rpmsg channel is removed
195 * @callback: invoked when an inbound message is received on the channel
196 */
197struct rpmsg_driver {
198 struct device_driver drv;
199 const struct rpmsg_device_id *id_table;
92e1de51
BA
200 int (*probe)(struct rpmsg_device *dev);
201 void (*remove)(struct rpmsg_device *dev);
202 void (*callback)(struct rpmsg_device *, void *, int, void *, u32);
bcabbcca
OBC
203};
204
92e1de51
BA
205int register_rpmsg_device(struct rpmsg_device *dev);
206void unregister_rpmsg_device(struct rpmsg_device *dev);
bc3c57c1 207int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
bcabbcca
OBC
208void unregister_rpmsg_driver(struct rpmsg_driver *drv);
209void rpmsg_destroy_ept(struct rpmsg_endpoint *);
92e1de51 210struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
2b263d24
BA
211 rpmsg_rx_cb_t cb, void *priv,
212 struct rpmsg_channel_info chinfo);
bcabbcca 213int
92e1de51 214rpmsg_send_offchannel_raw(struct rpmsg_device *, u32, u32, void *, int, bool);
bcabbcca 215
bc3c57c1
AD
216/* use a macro to avoid include chaining to get THIS_MODULE */
217#define register_rpmsg_driver(drv) \
218 __register_rpmsg_driver(drv, THIS_MODULE)
219
f3d9f1ce
AD
220/**
221 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
222 * @__rpmsg_driver: rpmsg_driver struct
223 *
224 * Helper macro for rpmsg drivers which do not do anything special in module
225 * init/exit. This eliminates a lot of boilerplate. Each module may only
226 * use this macro once, and calling it replaces module_init() and module_exit()
227 */
228#define module_rpmsg_driver(__rpmsg_driver) \
229 module_driver(__rpmsg_driver, register_rpmsg_driver, \
230 unregister_rpmsg_driver)
231
bcabbcca
OBC
232/**
233 * rpmsg_send() - send a message across to the remote processor
2a48d732 234 * @ept: the rpmsg endpoint
bcabbcca
OBC
235 * @data: payload of message
236 * @len: length of payload
237 *
2a48d732
BA
238 * This function sends @data of length @len on the @ept endpoint.
239 * The message will be sent to the remote processor which the @ept
240 * endpoint belongs to, using @ept's address and its associated rpmsg
241 * device destination addresses.
bcabbcca
OBC
242 * In case there are no TX buffers available, the function will block until
243 * one becomes available, or a timeout of 15 seconds elapses. When the latter
244 * happens, -ERESTARTSYS is returned.
245 *
246 * Can only be called from process context (for now).
247 *
248 * Returns 0 on success and an appropriate error value on failure.
249 */
2a48d732 250static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
bcabbcca 251{
92e1de51 252 struct rpmsg_device *rpdev = ept->rpdev;
2a48d732 253 u32 src = ept->addr, dst = rpdev->dst;
bcabbcca
OBC
254
255 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
256}
257
258/**
259 * rpmsg_sendto() - send a message across to the remote processor, specify dst
2a48d732 260 * @ept: the rpmsg endpoint
bcabbcca
OBC
261 * @data: payload of message
262 * @len: length of payload
263 * @dst: destination address
264 *
265 * This function sends @data of length @len to the remote @dst address.
2a48d732
BA
266 * The message will be sent to the remote processor which the @ept
267 * endpoint belongs to, using @ept's address as source.
bcabbcca
OBC
268 * In case there are no TX buffers available, the function will block until
269 * one becomes available, or a timeout of 15 seconds elapses. When the latter
270 * happens, -ERESTARTSYS is returned.
271 *
272 * Can only be called from process context (for now).
273 *
274 * Returns 0 on success and an appropriate error value on failure.
275 */
276static inline
2a48d732 277int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
bcabbcca 278{
92e1de51 279 struct rpmsg_device *rpdev = ept->rpdev;
2a48d732 280 u32 src = ept->addr;
bcabbcca
OBC
281
282 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
283}
284
285/**
286 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
2a48d732 287 * @ept: the rpmsg endpoint
bcabbcca
OBC
288 * @src: source address
289 * @dst: destination address
290 * @data: payload of message
291 * @len: length of payload
292 *
293 * This function sends @data of length @len to the remote @dst address,
294 * and uses @src as the source address.
2a48d732
BA
295 * The message will be sent to the remote processor which the @ept
296 * endpoint belongs to.
bcabbcca
OBC
297 * In case there are no TX buffers available, the function will block until
298 * one becomes available, or a timeout of 15 seconds elapses. When the latter
299 * happens, -ERESTARTSYS is returned.
300 *
301 * Can only be called from process context (for now).
302 *
303 * Returns 0 on success and an appropriate error value on failure.
304 */
305static inline
2a48d732 306int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
0963679c 307 void *data, int len)
bcabbcca 308{
92e1de51 309 struct rpmsg_device *rpdev = ept->rpdev;
2a48d732 310
bcabbcca
OBC
311 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
312}
313
314/**
315 * rpmsg_send() - send a message across to the remote processor
2a48d732 316 * @ept: the rpmsg endpoint
bcabbcca
OBC
317 * @data: payload of message
318 * @len: length of payload
319 *
2a48d732
BA
320 * This function sends @data of length @len on the @ept endpoint.
321 * The message will be sent to the remote processor which the @ept
322 * endpoint belongs to, using @ept's address as source and its associated
323 * rpdev's address as destination.
bcabbcca
OBC
324 * In case there are no TX buffers available, the function will immediately
325 * return -ENOMEM without waiting until one becomes available.
326 *
327 * Can only be called from process context (for now).
328 *
329 * Returns 0 on success and an appropriate error value on failure.
330 */
331static inline
2a48d732 332int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
bcabbcca 333{
92e1de51 334 struct rpmsg_device *rpdev = ept->rpdev;
2a48d732 335 u32 src = ept->addr, dst = rpdev->dst;
bcabbcca
OBC
336
337 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
338}
339
340/**
341 * rpmsg_sendto() - send a message across to the remote processor, specify dst
2a48d732 342 * @ept: the rpmsg endpoint
bcabbcca
OBC
343 * @data: payload of message
344 * @len: length of payload
345 * @dst: destination address
346 *
347 * This function sends @data of length @len to the remote @dst address.
2a48d732
BA
348 * The message will be sent to the remote processor which the @ept
349 * endpoint belongs to, using @ept's address as source.
bcabbcca
OBC
350 * In case there are no TX buffers available, the function will immediately
351 * return -ENOMEM without waiting until one becomes available.
352 *
353 * Can only be called from process context (for now).
354 *
355 * Returns 0 on success and an appropriate error value on failure.
356 */
357static inline
2a48d732 358int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
bcabbcca 359{
92e1de51 360 struct rpmsg_device *rpdev = ept->rpdev;
2a48d732 361 u32 src = ept->addr;
bcabbcca
OBC
362
363 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
364}
365
366/**
367 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
2a48d732 368 * @ept: the rpmsg endpoint
bcabbcca
OBC
369 * @src: source address
370 * @dst: destination address
371 * @data: payload of message
372 * @len: length of payload
373 *
374 * This function sends @data of length @len to the remote @dst address,
375 * and uses @src as the source address.
2a48d732
BA
376 * The message will be sent to the remote processor which the @ept
377 * endpoint belongs to.
bcabbcca
OBC
378 * In case there are no TX buffers available, the function will immediately
379 * return -ENOMEM without waiting until one becomes available.
380 *
381 * Can only be called from process context (for now).
382 *
383 * Returns 0 on success and an appropriate error value on failure.
384 */
385static inline
2a48d732 386int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
0963679c 387 void *data, int len)
bcabbcca 388{
92e1de51 389 struct rpmsg_device *rpdev = ept->rpdev;
2a48d732 390
bcabbcca
OBC
391 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
392}
393
394#endif /* _LINUX_RPMSG_H */