xref: /linux-6.15/include/linux/rpmsg.h (revision fade037e)
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>
41 #include <linux/kref.h>
42 #include <linux/mutex.h>
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  */
58 struct 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  */
79 struct 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  */
91 enum rpmsg_ns_flags {
92 	RPMSG_NS_CREATE		= 0,
93 	RPMSG_NS_DESTROY	= 1,
94 };
95 
96 #define RPMSG_ADDR_ANY		0xFFFFFFFF
97 
98 struct virtproc_info;
99 struct rpmsg_device;
100 struct rpmsg_endpoint;
101 struct rpmsg_device_ops;
102 struct rpmsg_endpoint_ops;
103 
104 /**
105  * struct rpmsg_channel_info - channel info representation
106  * @name: name of service
107  * @src: local address
108  * @dst: destination address
109  */
110 struct rpmsg_channel_info {
111 	char name[RPMSG_NAME_SIZE];
112 	u32 src;
113 	u32 dst;
114 };
115 
116 /**
117  * rpmsg_device - device that belong to the rpmsg bus
118  * @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  */
126 struct rpmsg_device {
127 	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;
134 
135 	const struct rpmsg_device_ops *ops;
136 };
137 
138 typedef void (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
139 
140 /**
141  * struct rpmsg_endpoint - binds a local rpmsg address to its user
142  * @rpdev: rpmsg channel device
143  * @refcount: when this drops to zero, the ept is deallocated
144  * @cb: rx callback handler
145  * @cb_lock: must be taken before accessing/changing @cb
146  * @addr: local rpmsg address
147  * @priv: private data for the driver's use
148  *
149  * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
150  * it binds an rpmsg address with an rx callback handler.
151  *
152  * Simple rpmsg drivers shouldn't use this struct directly, because
153  * things just work: every rpmsg driver provides an rx callback upon
154  * registering to the bus, and that callback is then bound to its rpmsg
155  * address when the driver is probed. When relevant inbound messages arrive
156  * (i.e. messages which their dst address equals to the src address of
157  * the rpmsg channel), the driver's handler is invoked to process it.
158  *
159  * More complicated drivers though, that do need to allocate additional rpmsg
160  * addresses, and bind them to different rx callbacks, must explicitly
161  * create additional endpoints by themselves (see rpmsg_create_ept()).
162  */
163 struct rpmsg_endpoint {
164 	struct rpmsg_device *rpdev;
165 	struct kref refcount;
166 	rpmsg_rx_cb_t cb;
167 	struct mutex cb_lock;
168 	u32 addr;
169 	void *priv;
170 
171 	const struct rpmsg_endpoint_ops *ops;
172 };
173 
174 /**
175  * struct rpmsg_driver - rpmsg driver struct
176  * @drv: underlying device driver
177  * @id_table: rpmsg ids serviced by this driver
178  * @probe: invoked when a matching rpmsg channel (i.e. device) is found
179  * @remove: invoked when the rpmsg channel is removed
180  * @callback: invoked when an inbound message is received on the channel
181  */
182 struct rpmsg_driver {
183 	struct device_driver drv;
184 	const struct rpmsg_device_id *id_table;
185 	int (*probe)(struct rpmsg_device *dev);
186 	void (*remove)(struct rpmsg_device *dev);
187 	void (*callback)(struct rpmsg_device *, void *, int, void *, u32);
188 };
189 
190 int register_rpmsg_device(struct rpmsg_device *dev);
191 void unregister_rpmsg_device(struct rpmsg_device *dev);
192 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
193 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
194 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
195 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
196 					rpmsg_rx_cb_t cb, void *priv,
197 					struct rpmsg_channel_info chinfo);
198 
199 /* use a macro to avoid include chaining to get THIS_MODULE */
200 #define register_rpmsg_driver(drv) \
201 	__register_rpmsg_driver(drv, THIS_MODULE)
202 
203 /**
204  * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
205  * @__rpmsg_driver: rpmsg_driver struct
206  *
207  * Helper macro for rpmsg drivers which do not do anything special in module
208  * init/exit. This eliminates a lot of boilerplate.  Each module may only
209  * use this macro once, and calling it replaces module_init() and module_exit()
210  */
211 #define module_rpmsg_driver(__rpmsg_driver) \
212 	module_driver(__rpmsg_driver, register_rpmsg_driver, \
213 			unregister_rpmsg_driver)
214 
215 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
216 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
217 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
218 			  void *data, int len);
219 
220 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
221 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
222 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
223 			     void *data, int len);
224 
225 #endif /* _LINUX_RPMSG_H */
226