rpmsg_rpc_internal.h 15 KB

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