Merge tag 'efi-next-for-v6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi
[linux-block.git] / include / linux / rpmsg.h
CommitLineData
3e79bfd6 1/* SPDX-License-Identifier: BSD-3-Clause */
bcabbcca
OBC
2/*
3 * Remote processor messaging
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 * All rights reserved.
bcabbcca
OBC
8 */
9
10#ifndef _LINUX_RPMSG_H
11#define _LINUX_RPMSG_H
12
13#include <linux/types.h>
14#include <linux/device.h>
2c8a5708 15#include <linux/err.h>
bcabbcca 16#include <linux/mod_devicetable.h>
5a081caa 17#include <linux/kref.h>
15fd943a 18#include <linux/mutex.h>
84d58132 19#include <linux/poll.h>
6bef0380 20#include <linux/rpmsg/byteorder.h>
3093c3c7 21#include <uapi/linux/rpmsg.h>
bcabbcca 22
8a228ecf 23struct rpmsg_device;
36b72c7d
BA
24struct rpmsg_endpoint;
25struct rpmsg_device_ops;
8a228ecf 26struct rpmsg_endpoint_ops;
bcabbcca 27
2b263d24
BA
28/**
29 * struct rpmsg_channel_info - channel info representation
30 * @name: name of service
31 * @src: local address
32 * @dst: destination address
33 */
34struct rpmsg_channel_info {
35 char name[RPMSG_NAME_SIZE];
36 u32 src;
37 u32 dst;
38};
39
bcabbcca 40/**
92e1de51 41 * rpmsg_device - device that belong to the rpmsg bus
bcabbcca
OBC
42 * @dev: the device struct
43 * @id: device id (used to match between rpmsg drivers and devices)
42cd402b
KK
44 * @driver_override: driver name to force a match; do not set directly,
45 * because core frees it; use driver_set_override() to
46 * set or clear it.
bcabbcca
OBC
47 * @src: local address
48 * @dst: destination address
49 * @ept: the rpmsg endpoint of this channel
50 * @announce: if set, rpmsg will announce the creation/removal of this channel
6bef0380 51 * @little_endian: True if transport is using little endian byte representation
bcabbcca 52 */
92e1de51 53struct rpmsg_device {
bcabbcca
OBC
54 struct device dev;
55 struct rpmsg_device_id id;
42cd402b 56 const char *driver_override;
bcabbcca
OBC
57 u32 src;
58 u32 dst;
59 struct rpmsg_endpoint *ept;
60 bool announce;
6bef0380 61 bool little_endian;
36b72c7d
BA
62
63 const struct rpmsg_device_ops *ops;
bcabbcca
OBC
64};
65
4b83c52a 66typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
bcabbcca
OBC
67
68/**
69 * struct rpmsg_endpoint - binds a local rpmsg address to its user
70 * @rpdev: rpmsg channel device
5a081caa 71 * @refcount: when this drops to zero, the ept is deallocated
bcabbcca 72 * @cb: rx callback handler
15fd943a 73 * @cb_lock: must be taken before accessing/changing @cb
bcabbcca
OBC
74 * @addr: local rpmsg address
75 * @priv: private data for the driver's use
76 *
77 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
78 * it binds an rpmsg address with an rx callback handler.
79 *
80 * Simple rpmsg drivers shouldn't use this struct directly, because
81 * things just work: every rpmsg driver provides an rx callback upon
82 * registering to the bus, and that callback is then bound to its rpmsg
83 * address when the driver is probed. When relevant inbound messages arrive
84 * (i.e. messages which their dst address equals to the src address of
85 * the rpmsg channel), the driver's handler is invoked to process it.
86 *
87 * More complicated drivers though, that do need to allocate additional rpmsg
88 * addresses, and bind them to different rx callbacks, must explicitly
89 * create additional endpoints by themselves (see rpmsg_create_ept()).
90 */
91struct rpmsg_endpoint {
92e1de51 92 struct rpmsg_device *rpdev;
5a081caa 93 struct kref refcount;
bcabbcca 94 rpmsg_rx_cb_t cb;
15fd943a 95 struct mutex cb_lock;
bcabbcca
OBC
96 u32 addr;
97 void *priv;
8a228ecf
BA
98
99 const struct rpmsg_endpoint_ops *ops;
100};
101
bcabbcca
OBC
102/**
103 * struct rpmsg_driver - rpmsg driver struct
104 * @drv: underlying device driver
105 * @id_table: rpmsg ids serviced by this driver
106 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
107 * @remove: invoked when the rpmsg channel is removed
108 * @callback: invoked when an inbound message is received on the channel
109 */
110struct rpmsg_driver {
111 struct device_driver drv;
112 const struct rpmsg_device_id *id_table;
92e1de51
BA
113 int (*probe)(struct rpmsg_device *dev);
114 void (*remove)(struct rpmsg_device *dev);
4b83c52a 115 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
bcabbcca
OBC
116};
117
6bef0380
MP
118static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val)
119{
120 if (!rpdev)
121 return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val);
122 else
123 return __rpmsg16_to_cpu(rpdev->little_endian, val);
124}
125
126static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val)
127{
128 if (!rpdev)
129 return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val);
130 else
131 return __cpu_to_rpmsg16(rpdev->little_endian, val);
132}
133
134static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val)
135{
136 if (!rpdev)
137 return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val);
138 else
139 return __rpmsg32_to_cpu(rpdev->little_endian, val);
140}
141
142static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val)
143{
144 if (!rpdev)
145 return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val);
146 else
147 return __cpu_to_rpmsg32(rpdev->little_endian, val);
148}
149
150static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val)
151{
152 if (!rpdev)
153 return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val);
154 else
155 return __rpmsg64_to_cpu(rpdev->little_endian, val);
156}
157
158static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val)
159{
160 if (!rpdev)
161 return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val);
162 else
163 return __cpu_to_rpmsg64(rpdev->little_endian, val);
164}
165
2c8a5708
BA
166#if IS_ENABLED(CONFIG_RPMSG)
167
bb17d110
KK
168int rpmsg_register_device_override(struct rpmsg_device *rpdev,
169 const char *driver_override);
55488110
MP
170int rpmsg_register_device(struct rpmsg_device *rpdev);
171int rpmsg_unregister_device(struct device *parent,
172 struct rpmsg_channel_info *chinfo);
bc3c57c1 173int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
bcabbcca
OBC
174void unregister_rpmsg_driver(struct rpmsg_driver *drv);
175void rpmsg_destroy_ept(struct rpmsg_endpoint *);
92e1de51 176struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
2b263d24
BA
177 rpmsg_rx_cb_t cb, void *priv,
178 struct rpmsg_channel_info chinfo);
bcabbcca 179
2c8a5708
BA
180int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
181int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
182int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
183 void *data, int len);
184
185int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
186int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
187int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
188 void *data, int len);
189
afc9a42b 190__poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
84d58132
BA
191 poll_table *wait);
192
e279317e
AP
193ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept);
194
2c8a5708
BA
195#else
196
bb17d110
KK
197static inline int rpmsg_register_device_override(struct rpmsg_device *rpdev,
198 const char *driver_override)
199{
200 return -ENXIO;
201}
202
55488110 203static inline int rpmsg_register_device(struct rpmsg_device *rpdev)
2c8a5708
BA
204{
205 return -ENXIO;
206}
207
55488110
MP
208static inline int rpmsg_unregister_device(struct device *parent,
209 struct rpmsg_channel_info *chinfo)
2c8a5708
BA
210{
211 /* This shouldn't be possible */
212 WARN_ON(1);
55488110
MP
213
214 return -ENXIO;
2c8a5708
BA
215}
216
217static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
218 struct module *owner)
219{
220 /* This shouldn't be possible */
221 WARN_ON(1);
222
223 return -ENXIO;
224}
225
226static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
227{
228 /* This shouldn't be possible */
229 WARN_ON(1);
230}
231
232static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
233{
234 /* This shouldn't be possible */
235 WARN_ON(1);
236}
237
238static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
239 rpmsg_rx_cb_t cb,
240 void *priv,
241 struct rpmsg_channel_info chinfo)
242{
243 /* This shouldn't be possible */
244 WARN_ON(1);
245
537d3af1 246 return NULL;
2c8a5708
BA
247}
248
249static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
250{
251 /* This shouldn't be possible */
252 WARN_ON(1);
253
254 return -ENXIO;
255}
256
257static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
258 u32 dst)
259{
260 /* This shouldn't be possible */
261 WARN_ON(1);
262
263 return -ENXIO;
264
265}
266
267static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
268 u32 dst, void *data, int len)
269{
270 /* This shouldn't be possible */
271 WARN_ON(1);
272
273 return -ENXIO;
274}
275
276static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
277{
278 /* This shouldn't be possible */
279 WARN_ON(1);
280
281 return -ENXIO;
282}
283
284static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
285 int len, u32 dst)
286{
287 /* This shouldn't be possible */
288 WARN_ON(1);
289
290 return -ENXIO;
291}
292
293static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
294 u32 dst, void *data, int len)
295{
296 /* This shouldn't be possible */
297 WARN_ON(1);
298
299 return -ENXIO;
300}
301
afc9a42b 302static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
84d58132
BA
303 struct file *filp, poll_table *wait)
304{
305 /* This shouldn't be possible */
306 WARN_ON(1);
307
308 return 0;
309}
310
e279317e
AP
311static inline ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
312{
313 /* This shouldn't be possible */
314 WARN_ON(1);
315
316 return -ENXIO;
317}
318
2c8a5708
BA
319#endif /* IS_ENABLED(CONFIG_RPMSG) */
320
bc3c57c1
AD
321/* use a macro to avoid include chaining to get THIS_MODULE */
322#define register_rpmsg_driver(drv) \
323 __register_rpmsg_driver(drv, THIS_MODULE)
324
f3d9f1ce
AD
325/**
326 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
327 * @__rpmsg_driver: rpmsg_driver struct
328 *
329 * Helper macro for rpmsg drivers which do not do anything special in module
330 * init/exit. This eliminates a lot of boilerplate. Each module may only
331 * use this macro once, and calling it replaces module_init() and module_exit()
332 */
333#define module_rpmsg_driver(__rpmsg_driver) \
334 module_driver(__rpmsg_driver, register_rpmsg_driver, \
335 unregister_rpmsg_driver)
336
bcabbcca 337#endif /* _LINUX_RPMSG_H */