rpmsg_rpc_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
  2. /*
  3. * Remote Processor Procedure Call Driver
  4. *
  5. * Copyright (C) 2012-2019 Texas Instruments Incorporated - http://www.ti.com/
  6. */
  7. #ifndef _RPMSG_RPC_INTERNAL_H_
  8. #define _RPMSG_RPC_INTERNAL_H_
  9. #include <linux/cdev.h>
  10. #include <linux/idr.h>
  11. #include <linux/wait.h>
  12. #include <linux/fs.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rpmsg.h>
  15. typedef u32 virt_addr_t;
  16. typedef u32 dev_addr_t;
  17. /**
  18. * struct rppc_device - The per-device (server) data
  19. * @cdev: character device
  20. * @dev: device
  21. * @rpdev: rpmsg channel device associated with the remote server
  22. * @instances: list of currently opened/connected instances
  23. * @lock: mutex for protection of device variables
  24. * @comp: completion signal used for unblocking users during a
  25. * remote processor recovery
  26. * @sig_attr: array of device attributes to use with the publishing of
  27. * function information in sysfs for all the functions
  28. * associated with this remote server device.
  29. * @signatures: function signatures for the functions published by this
  30. * remote server device
  31. * @minor: minor number for the character device
  32. * @num_funcs: number of functions published by this remote server device
  33. * @cur_func: counter used while querying information for each function
  34. * associated with this remote server device
  35. * @desc: description of the exposed service
  36. *
  37. * A rppc_device indicates the base remote server device that supports the
  38. * execution of a bunch of remote functions. Each such remote server device
  39. * has an associated character device that is used by the userland apps to
  40. * connect to it, and request the execution of any of these remote functions.
  41. */
  42. struct rppc_device {
  43. struct cdev cdev;
  44. struct device *dev;
  45. struct rpmsg_device *rpdev;
  46. struct list_head instances;
  47. struct mutex lock; /* device state variables lock */
  48. struct completion comp;
  49. struct device_attribute *sig_attr;
  50. struct rppc_func_signature *signatures;
  51. unsigned int minor;
  52. u32 num_funcs;
  53. u32 cur_func;
  54. char desc[RPMSG_NAME_SIZE];
  55. };
  56. /**
  57. * struct rppc_instance - The per-instance data structure (per user)
  58. * @list: list node
  59. * @rppcdev: the rppc device (remote server instance) handle
  60. * @dev: local device reference pointer of the rppc device
  61. * @queue: queue of buffers waiting to be read by the user
  62. * @lock: mutex for protecting instance variables
  63. * @readq: wait queue of blocked user threads waiting to read data
  64. * @reply_arrived: signal for unblocking the user thread
  65. * @ept: rpmsg endpoint associated with the rppc device
  66. * @in_transition: flag for storing a pending connection request
  67. * @dst: destination end-point of the remote server instance
  68. * @state: state of the opened instance, see enum rppc_state
  69. * @dma_idr: idr structure storing the imported buffers
  70. * @msg_id: last/current active message id tagged on a message sent
  71. * to the remote processor
  72. * @fxn_list: list of functions published by the remote server instance
  73. *
  74. * This structure is created whenever the user opens the driver. The
  75. * various elements of the structure are used to store its state and
  76. * information about the remote server it is connected to.
  77. */
  78. struct rppc_instance {
  79. struct list_head list;
  80. struct rppc_device *rppcdev;
  81. struct device *dev;
  82. struct sk_buff_head queue;
  83. struct mutex lock; /* instance state variables lock */
  84. wait_queue_head_t readq;
  85. struct completion reply_arrived;
  86. struct rpmsg_endpoint *ept;
  87. int in_transition;
  88. u32 dst;
  89. int state;
  90. struct idr dma_idr;
  91. u16 msg_id;
  92. struct list_head fxn_list;
  93. };
  94. /**
  95. * struct rppc_function_list - outstanding function descriptor
  96. * @list: list node
  97. * @function: current remote function descriptor
  98. * @msg_id: message id for the function invocation
  99. *
  100. * This structure is used for storing the information about outstanding
  101. * functions that the remote side is executing. This provides the host
  102. * side a means to track every outstanding function, and a means to process
  103. * the responses received from the remote processor.
  104. */
  105. struct rppc_function_list {
  106. struct list_head list;
  107. struct rppc_function *function;
  108. u16 msg_id;
  109. };
  110. /**
  111. * struct rppc_dma_buf - a rppc dma_buf descriptor for buffers imported by rppc
  112. * @fd: file descriptor of a buffer used to import the dma_buf
  113. * @id: idr index value for this descriptor
  114. * @buf: imported dma_buf handle for the buffer
  115. * @attach: attachment structure returned by exporter upon attaching to
  116. * the buffer by the rppc driver
  117. * @sgt: the scatter-gather structure associated with @buf
  118. * @pa: the physical address associated with the imported buffer
  119. * @autoreg: mode of how the descriptor is created
  120. *
  121. * This structure is used for storing the information relevant to the imported
  122. * buffer. The rpmsg rpc driver acts as a proxy on behalf of the remote core
  123. * and attaches itself to the driver while the remote processor/accelerators are
  124. * operating on the buffer.
  125. */
  126. struct rppc_dma_buf {
  127. int fd;
  128. int id;
  129. struct dma_buf *buf;
  130. struct dma_buf_attachment *attach;
  131. struct sg_table *sgt;
  132. phys_addr_t pa;
  133. int autoreg;
  134. };
  135. /**
  136. * enum rppc_msg_type - message types exchanged between host and remote server
  137. * @RPPC_MSGTYPE_DEVINFO_REQ: request remote server for channel information
  138. * @RPPC_MSGTYPE_DEVINFO_RESP: response message from remote server for a
  139. * request of type RPPC_MSGTYPE_DEVINFO_REQ
  140. * @RPPC_MSGTYPE_FUNCTION_QUERY: request remote server for information about a
  141. * specific function
  142. * @RPPC_MSGTYPE_FUNCTION_INFO: response message from remote server for a prior
  143. * request of type RPPC_MSGTYPE_FUNCTION_QUERY
  144. * @RPPC_MSGTYPE_CREATE_REQ: request the remote server manager to create a new
  145. * remote server instance. No secondary data is
  146. * needed
  147. * @RPPC_MSGTYPE_CREATE_RESP: response message from remote server manager for a
  148. * request of type RPPC_MSGTYPE_CREATE_REQ. The
  149. * message contains the new endpoint address in the
  150. * rppc_instance_handle
  151. * @RPPC_MSGTYPE_DELETE_REQ: request the remote server manager to delete a
  152. * remote server instance
  153. * @RPPC_MSGTYPE_DELETE_RESP: response message from remote server manager to a
  154. * request of type RPPC_MSGTYPE_DELETE_REQ. The
  155. * message contains the old endpoint address in the
  156. * rppc_instance_handle
  157. * @RPPC_MSGTYPE_FUNCTION_CALL: request remote server to execute a specific
  158. * function
  159. * @RPPC_MSGTYPE_FUNCTION_RET: response message carrying the return status of a
  160. * specific function execution
  161. * @RPPC_MSGTYPE_ERROR: an error response message sent by either the remote
  162. * server manager or remote server instance while
  163. * processing any request messages
  164. * @RPPC_MSGTYPE_MAX: limit value to define the maximum message type value
  165. *
  166. * Every message exchanged between the host-side and the remote-side is
  167. * identified through a message type defined in this enum. The message type
  168. * is specified through the msg_type field of the struct rppc_msg_header,
  169. * which is the common header for rppc messages.
  170. */
  171. enum rppc_msg_type {
  172. RPPC_MSGTYPE_DEVINFO_REQ = 0,
  173. RPPC_MSGTYPE_DEVINFO_RESP = 1,
  174. RPPC_MSGTYPE_FUNCTION_QUERY = 2,
  175. RPPC_MSGTYPE_FUNCTION_INFO = 3,
  176. RPPC_MSGTYPE_CREATE_REQ = 6,
  177. RPPC_MSGTYPE_CREATE_RESP = 8,
  178. RPPC_MSGTYPE_DELETE_REQ = 4,
  179. RPPC_MSGTYPE_DELETE_RESP = 7,
  180. RPPC_MSGTYPE_FUNCTION_CALL = 5,
  181. RPPC_MSGTYPE_FUNCTION_RET = 9,
  182. RPPC_MSGTYPE_ERROR = 10,
  183. RPPC_MSGTYPE_MAX
  184. };
  185. /**
  186. * enum rppc_infotype - function information query type
  187. * @RPPC_INFOTYPE_FUNC_SIGNATURE: function signature
  188. * @RPPC_INFOTYPE_NUM_CALLS: the number of times a function has been invoked
  189. * @RPPC_INFOTYPE_MAX: limit value to define the maximum info type
  190. *
  191. * This enum is used for identifying the type of information queried
  192. * from the remote processor. Only RPPC_INFOTYPE_FUNC_SIGNATURE is
  193. * currently used.
  194. */
  195. enum rppc_infotype {
  196. RPPC_INFOTYPE_FUNC_SIGNATURE = 1,
  197. RPPC_INFOTYPE_NUM_CALLS,
  198. RPPC_INFOTYPE_MAX
  199. };
  200. /**
  201. * struct rppc_instance_handle - rppc instance information
  202. * @endpoint_address: end-point address of the remote server instance
  203. * @status: status of the request
  204. *
  205. * This structure indicates the format of the message payload exchanged
  206. * between the host and the remote sides for messages pertaining to
  207. * creation and deletion of the remote server instances. This payload
  208. * is associated with messages of type RPPC_MSGTYPE_CREATE_RESP and
  209. * RPPC_MSGTYPE_DELETE_RESP.
  210. */
  211. struct rppc_instance_handle {
  212. u32 endpoint_address;
  213. u32 status;
  214. } __packed;
  215. /**
  216. * struct rppc_param_signature - parameter descriptor
  217. * @direction: input or output classifier, see enum rppc_param_direction
  218. * @type: parameter data type, see enum rppc_param_type
  219. * @count: used to do some basic sanity checking on array bounds
  220. */
  221. struct rppc_param_signature {
  222. u32 direction;
  223. u32 type;
  224. u32 count;
  225. };
  226. /**
  227. * struct rppc_func_signature - remote function signature descriptor
  228. * @name: name of the function
  229. * @num_param: number of parameters to the function
  230. * @params: parameter descriptors for each of the parameters
  231. *
  232. * This structure contains the indicates the format of the message payload
  233. * exchanged between the host and the remote sides for messages pertaining
  234. * to creation and deletion of the remote server instances. This payload
  235. * is associated with messages of type RPPC_MSGTYPE_CREATE_RESP and
  236. * RPPC_MSGTYPE_FUNCTION_INFO.
  237. */
  238. struct rppc_func_signature {
  239. char name[RPPC_MAX_CHANNEL_NAMELEN];
  240. u32 num_param;
  241. struct rppc_param_signature params[RPPC_MAX_NUM_PARAMS + 1];
  242. };
  243. /**
  244. * struct rppc_query_function - function info packet structure
  245. * @info_type: type of the function information requested, see
  246. * enum rppc_infotype
  247. * @fxn_id: function identifier on this specific remote server instance
  248. * @num_calls: number of types function is invoked, filled in during a response
  249. * (only valid for rppc_infotype RPPC_INFOTYPE_NUM_CALLS)
  250. * @signature: the signature of the function including its return type,
  251. * parameters and their description
  252. * (only valid for rppc_infotype RPPC_INFOTYPE_FUNC_SIGNATURE)
  253. *
  254. * This structure indicates the format of the message payload exchanged
  255. * between the host and the remote sides for messages pertaining to
  256. * information about each function supported by the remote server instance.
  257. * This payload is associated with messages of type RPPC_MSGTYPE_FUNCTION_QUERY
  258. * and RPPC_MSGTYPE_FUNCTION_INFO.
  259. */
  260. struct rppc_query_function {
  261. u32 info_type;
  262. u32 fxn_id;
  263. union {
  264. u32 num_calls;
  265. struct rppc_func_signature signature;
  266. } info;
  267. };
  268. /**
  269. * enum rppc_translate_direction - pointer translation direction
  270. * @RPPC_UVA_TO_RPA: user virtual address to remote device address translation
  271. * @RPPC_RPA_TO_UVA: remote device address to user virtual address translation
  272. *
  273. * An enum used for identifying the rppc function message direction, whether
  274. * it is going to the remote side, or is a response from the remote side. This
  275. * is used in translating the pointers from the host-side to the remote-side
  276. * and vice versa depending on the packet direction.
  277. */
  278. enum rppc_translate_direction {
  279. RPPC_UVA_TO_RPA,
  280. RPPC_RPA_TO_UVA,
  281. };
  282. /**
  283. * enum rppc_state - rppc instance state
  284. * @RPPC_STATE_DISCONNECTED: uninitialized state
  285. * @RPPC_STATE_CONNECTED: initialized state
  286. * @RPPC_STATE_STALE: invalid or stale state
  287. * @RPPC_STATE_MAX: limit value for the different state values
  288. *
  289. * This enum value is used to define the status values of a
  290. * rppc_instance object.
  291. */
  292. enum rppc_state {
  293. RPPC_STATE_DISCONNECTED,
  294. RPPC_STATE_CONNECTED,
  295. RPPC_STATE_STALE,
  296. RPPC_STATE_MAX
  297. };
  298. /**
  299. * struct rppc_device_info - rppc remote server device info
  300. * @num_funcs: number of functions supported by a remote server instance
  301. *
  302. * This structure indicates the format of the message payload responded by
  303. * the remote side upon a request for message type RPPC_MSGTYPE_DEVINFO_REQ.
  304. * This payload is associated with messages of type RPPC_MSGTYPE_DEVINFO_RESP.
  305. */
  306. struct rppc_device_info {
  307. u32 num_funcs;
  308. };
  309. /**
  310. * struct rppc_error - rppc error information
  311. * @endpoint_address: end-point address of the remote server instance
  312. * @status: status of the request
  313. *
  314. * This structure indicates the format of the message payload exchanged
  315. * between the host and the remote sides for error messages. This payload
  316. * is associated with messages of type RPPC_MSGTYPE_ERROR
  317. * XXX: check if this is needed still, not used anywhere at present
  318. */
  319. struct rppc_error {
  320. u32 endpoint_address;
  321. u32 status;
  322. } __packed;
  323. /**
  324. * struct rppc_param_data - marshalled parameter data structure
  325. * @size: size of the parameter data type
  326. * @data: actual parameter data
  327. *
  328. * Each function parameter is marshalled in this form between the host
  329. * and remote sides. The @data field would contain the actual value of
  330. * of the parameter if it is a scalar argument type, or the remote-side
  331. * device address (virtual address) of the pointer if the argument is
  332. * of pointer type.
  333. */
  334. struct rppc_param_data {
  335. size_t size;
  336. size_t data;
  337. } __packed;
  338. /**
  339. * struct rppc_msg_header - generic rpmsg rpc message header
  340. * @msg_type: message type, see enum rppc_msg_type
  341. * @msg_len: length of the message payload in bytes
  342. * @msg_data: the actual message payload (depends on message type)
  343. *
  344. * All RPPC messages will start with this common header (which will begin
  345. * right after the standard rpmsg header ends).
  346. */
  347. struct rppc_msg_header {
  348. u32 msg_type;
  349. u32 msg_len;
  350. u8 msg_data[0];
  351. } __packed;
  352. #define RPPC_PAYLOAD(ptr, type) \
  353. ((struct type *)&(ptr)[sizeof(struct rppc_msg_header)])
  354. /* from rpmsg_rpc.c */
  355. dev_addr_t rppc_local_to_remote_da(struct rppc_instance *rpc, phys_addr_t pa);
  356. /* from rpmsg_rpc_dmabuf.c */
  357. struct rppc_dma_buf *rppc_alloc_dmabuf(struct rppc_instance *rpc,
  358. int fd, bool autoreg);
  359. struct rppc_dma_buf *rppc_find_dmabuf(struct rppc_instance *rpc, int fd);
  360. int rppc_free_dmabuf(int id, void *p, void *data);
  361. dev_addr_t rppc_buffer_lookup(struct rppc_instance *rpc, virt_addr_t uva,
  362. virt_addr_t buva, int fd);
  363. int rppc_xlate_buffers(struct rppc_instance *rpc, struct rppc_function *func,
  364. int direction);
  365. /* from rpmsg_rpc_sysfs.c */
  366. int rppc_create_sysfs(struct rppc_device *rppcdev);
  367. int rppc_remove_sysfs(struct rppc_device *rppcdev);
  368. #endif