mhi_ep.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2022, Linaro Ltd.
  4. *
  5. */
  6. #ifndef _MHI_EP_H_
  7. #define _MHI_EP_H_
  8. #include <linux/dma-direction.h>
  9. #include <linux/mhi.h>
  10. #define MHI_EP_DEFAULT_MTU 0x8000
  11. /**
  12. * struct mhi_ep_channel_config - Channel configuration structure for controller
  13. * @name: The name of this channel
  14. * @num: The number assigned to this channel
  15. * @num_elements: The number of elements that can be queued to this channel
  16. * @dir: Direction that data may flow on this channel
  17. */
  18. struct mhi_ep_channel_config {
  19. char *name;
  20. u32 num;
  21. u32 num_elements;
  22. enum dma_data_direction dir;
  23. };
  24. /**
  25. * struct mhi_ep_cntrl_config - MHI Endpoint controller configuration
  26. * @mhi_version: MHI spec version supported by the controller
  27. * @max_channels: Maximum number of channels supported
  28. * @num_channels: Number of channels defined in @ch_cfg
  29. * @ch_cfg: Array of defined channels
  30. */
  31. struct mhi_ep_cntrl_config {
  32. u32 mhi_version;
  33. u32 max_channels;
  34. u32 num_channels;
  35. const struct mhi_ep_channel_config *ch_cfg;
  36. };
  37. /**
  38. * struct mhi_ep_db_info - MHI Endpoint doorbell info
  39. * @mask: Mask of the doorbell interrupt
  40. * @status: Status of the doorbell interrupt
  41. */
  42. struct mhi_ep_db_info {
  43. u32 mask;
  44. u32 status;
  45. };
  46. /**
  47. * struct mhi_ep_cntrl - MHI Endpoint controller structure
  48. * @cntrl_dev: Pointer to the struct device of physical bus acting as the MHI
  49. * Endpoint controller
  50. * @mhi_dev: MHI Endpoint device instance for the controller
  51. * @mmio: MMIO region containing the MHI registers
  52. * @mhi_chan: Points to the channel configuration table
  53. * @mhi_event: Points to the event ring configurations table
  54. * @mhi_cmd: Points to the command ring configurations table
  55. * @sm: MHI Endpoint state machine
  56. * @ch_ctx_cache: Cache of host channel context data structure
  57. * @ev_ctx_cache: Cache of host event context data structure
  58. * @cmd_ctx_cache: Cache of host command context data structure
  59. * @ch_ctx_host_pa: Physical address of host channel context data structure
  60. * @ev_ctx_host_pa: Physical address of host event context data structure
  61. * @cmd_ctx_host_pa: Physical address of host command context data structure
  62. * @ch_ctx_cache_phys: Physical address of the host channel context cache
  63. * @ev_ctx_cache_phys: Physical address of the host event context cache
  64. * @cmd_ctx_cache_phys: Physical address of the host command context cache
  65. * @chdb: Array of channel doorbell interrupt info
  66. * @event_lock: Lock for protecting event rings
  67. * @state_lock: Lock for protecting state transitions
  68. * @list_lock: Lock for protecting state transition and channel doorbell lists
  69. * @st_transition_list: List of state transitions
  70. * @ch_db_list: List of queued channel doorbells
  71. * @wq: Dedicated workqueue for handling rings and state changes
  72. * @state_work: State transition worker
  73. * @reset_work: Worker for MHI Endpoint reset
  74. * @cmd_ring_work: Worker for processing command rings
  75. * @ch_ring_work: Worker for processing channel rings
  76. * @raise_irq: CB function for raising IRQ to the host
  77. * @alloc_map: CB function for allocating memory in endpoint for storing host context and mapping it
  78. * @unmap_free: CB function to unmap and free the allocated memory in endpoint for storing host context
  79. * @read_from_host: CB function for reading from host memory from endpoint
  80. * @write_to_host: CB function for writing to host memory from endpoint
  81. * @mhi_state: MHI Endpoint state
  82. * @max_chan: Maximum channels supported by the endpoint controller
  83. * @mru: MRU (Maximum Receive Unit) value of the endpoint controller
  84. * @event_rings: Number of event rings supported by the endpoint controller
  85. * @hw_event_rings: Number of hardware event rings supported by the endpoint controller
  86. * @chdb_offset: Channel doorbell offset set by the host
  87. * @erdb_offset: Event ring doorbell offset set by the host
  88. * @index: MHI Endpoint controller index
  89. * @irq: IRQ used by the endpoint controller
  90. * @enabled: Check if the endpoint controller is enabled or not
  91. */
  92. struct mhi_ep_cntrl {
  93. struct device *cntrl_dev;
  94. struct mhi_ep_device *mhi_dev;
  95. void __iomem *mmio;
  96. struct mhi_ep_chan *mhi_chan;
  97. struct mhi_ep_event *mhi_event;
  98. struct mhi_ep_cmd *mhi_cmd;
  99. struct mhi_ep_sm *sm;
  100. struct mhi_chan_ctxt *ch_ctx_cache;
  101. struct mhi_event_ctxt *ev_ctx_cache;
  102. struct mhi_cmd_ctxt *cmd_ctx_cache;
  103. u64 ch_ctx_host_pa;
  104. u64 ev_ctx_host_pa;
  105. u64 cmd_ctx_host_pa;
  106. phys_addr_t ch_ctx_cache_phys;
  107. phys_addr_t ev_ctx_cache_phys;
  108. phys_addr_t cmd_ctx_cache_phys;
  109. struct mhi_ep_db_info chdb[4];
  110. struct mutex event_lock;
  111. struct mutex state_lock;
  112. spinlock_t list_lock;
  113. struct list_head st_transition_list;
  114. struct list_head ch_db_list;
  115. struct workqueue_struct *wq;
  116. struct work_struct state_work;
  117. struct work_struct reset_work;
  118. struct work_struct cmd_ring_work;
  119. struct work_struct ch_ring_work;
  120. void (*raise_irq)(struct mhi_ep_cntrl *mhi_cntrl, u32 vector);
  121. int (*alloc_map)(struct mhi_ep_cntrl *mhi_cntrl, u64 pci_addr, phys_addr_t *phys_ptr,
  122. void __iomem **virt, size_t size);
  123. void (*unmap_free)(struct mhi_ep_cntrl *mhi_cntrl, u64 pci_addr, phys_addr_t phys,
  124. void __iomem *virt, size_t size);
  125. int (*read_from_host)(struct mhi_ep_cntrl *mhi_cntrl, u64 from, void *to, size_t size);
  126. int (*write_to_host)(struct mhi_ep_cntrl *mhi_cntrl, void *from, u64 to, size_t size);
  127. enum mhi_state mhi_state;
  128. u32 max_chan;
  129. u32 mru;
  130. u32 event_rings;
  131. u32 hw_event_rings;
  132. u32 chdb_offset;
  133. u32 erdb_offset;
  134. u32 index;
  135. int irq;
  136. bool enabled;
  137. };
  138. /**
  139. * struct mhi_ep_device - Structure representing an MHI Endpoint device that binds
  140. * to channels or is associated with controllers
  141. * @dev: Driver model device node for the MHI Endpoint device
  142. * @mhi_cntrl: Controller the device belongs to
  143. * @id: Pointer to MHI Endpoint device ID struct
  144. * @name: Name of the associated MHI Endpoint device
  145. * @ul_chan: UL (from host to endpoint) channel for the device
  146. * @dl_chan: DL (from endpoint to host) channel for the device
  147. * @dev_type: MHI device type
  148. */
  149. struct mhi_ep_device {
  150. struct device dev;
  151. struct mhi_ep_cntrl *mhi_cntrl;
  152. const struct mhi_device_id *id;
  153. const char *name;
  154. struct mhi_ep_chan *ul_chan;
  155. struct mhi_ep_chan *dl_chan;
  156. enum mhi_device_type dev_type;
  157. };
  158. /**
  159. * struct mhi_ep_driver - Structure representing a MHI Endpoint client driver
  160. * @id_table: Pointer to MHI Endpoint device ID table
  161. * @driver: Device driver model driver
  162. * @probe: CB function for client driver probe function
  163. * @remove: CB function for client driver remove function
  164. * @ul_xfer_cb: CB function for UL (from host to endpoint) data transfer
  165. * @dl_xfer_cb: CB function for DL (from endpoint to host) data transfer
  166. */
  167. struct mhi_ep_driver {
  168. const struct mhi_device_id *id_table;
  169. struct device_driver driver;
  170. int (*probe)(struct mhi_ep_device *mhi_ep,
  171. const struct mhi_device_id *id);
  172. void (*remove)(struct mhi_ep_device *mhi_ep);
  173. void (*ul_xfer_cb)(struct mhi_ep_device *mhi_dev,
  174. struct mhi_result *result);
  175. void (*dl_xfer_cb)(struct mhi_ep_device *mhi_dev,
  176. struct mhi_result *result);
  177. };
  178. #define to_mhi_ep_device(dev) container_of(dev, struct mhi_ep_device, dev)
  179. #define to_mhi_ep_driver(drv) container_of(drv, struct mhi_ep_driver, driver)
  180. /*
  181. * module_mhi_ep_driver() - Helper macro for drivers that don't do
  182. * anything special other than using default mhi_ep_driver_register() and
  183. * mhi_ep_driver_unregister(). This eliminates a lot of boilerplate.
  184. * Each module may only use this macro once.
  185. */
  186. #define module_mhi_ep_driver(mhi_drv) \
  187. module_driver(mhi_drv, mhi_ep_driver_register, \
  188. mhi_ep_driver_unregister)
  189. /*
  190. * Macro to avoid include chaining to get THIS_MODULE
  191. */
  192. #define mhi_ep_driver_register(mhi_drv) \
  193. __mhi_ep_driver_register(mhi_drv, THIS_MODULE)
  194. /**
  195. * __mhi_ep_driver_register - Register a driver with MHI Endpoint bus
  196. * @mhi_drv: Driver to be associated with the device
  197. * @owner: The module owner
  198. *
  199. * Return: 0 if driver registrations succeeds, a negative error code otherwise.
  200. */
  201. int __mhi_ep_driver_register(struct mhi_ep_driver *mhi_drv, struct module *owner);
  202. /**
  203. * mhi_ep_driver_unregister - Unregister a driver from MHI Endpoint bus
  204. * @mhi_drv: Driver associated with the device
  205. */
  206. void mhi_ep_driver_unregister(struct mhi_ep_driver *mhi_drv);
  207. /**
  208. * mhi_ep_register_controller - Register MHI Endpoint controller
  209. * @mhi_cntrl: MHI Endpoint controller to register
  210. * @config: Configuration to use for the controller
  211. *
  212. * Return: 0 if controller registrations succeeds, a negative error code otherwise.
  213. */
  214. int mhi_ep_register_controller(struct mhi_ep_cntrl *mhi_cntrl,
  215. const struct mhi_ep_cntrl_config *config);
  216. /**
  217. * mhi_ep_unregister_controller - Unregister MHI Endpoint controller
  218. * @mhi_cntrl: MHI Endpoint controller to unregister
  219. */
  220. void mhi_ep_unregister_controller(struct mhi_ep_cntrl *mhi_cntrl);
  221. /**
  222. * mhi_ep_power_up - Power up the MHI endpoint stack
  223. * @mhi_cntrl: MHI Endpoint controller
  224. *
  225. * Return: 0 if power up succeeds, a negative error code otherwise.
  226. */
  227. int mhi_ep_power_up(struct mhi_ep_cntrl *mhi_cntrl);
  228. /**
  229. * mhi_ep_power_down - Power down the MHI endpoint stack
  230. * @mhi_cntrl: MHI controller
  231. */
  232. void mhi_ep_power_down(struct mhi_ep_cntrl *mhi_cntrl);
  233. /**
  234. * mhi_ep_queue_is_empty - Determine whether the transfer queue is empty
  235. * @mhi_dev: Device associated with the channels
  236. * @dir: DMA direction for the channel
  237. *
  238. * Return: true if the queue is empty, false otherwise.
  239. */
  240. bool mhi_ep_queue_is_empty(struct mhi_ep_device *mhi_dev, enum dma_data_direction dir);
  241. /**
  242. * mhi_ep_queue_skb - Send SKBs to host over MHI Endpoint
  243. * @mhi_dev: Device associated with the DL channel
  244. * @skb: SKBs to be queued
  245. *
  246. * Return: 0 if the SKBs has been sent successfully, a negative error code otherwise.
  247. */
  248. int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb);
  249. #endif