qaic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* SPDX-License-Identifier: GPL-2.0-only
  2. *
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef _QAIC_H_
  7. #define _QAIC_H_
  8. #include <linux/interrupt.h>
  9. #include <linux/kref.h>
  10. #include <linux/mhi.h>
  11. #include <linux/mutex.h>
  12. #include <linux/pci.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/srcu.h>
  15. #include <linux/wait.h>
  16. #include <linux/workqueue.h>
  17. #include <drm/drm_device.h>
  18. #include <drm/drm_gem.h>
  19. #define QAIC_DBC_BASE SZ_128K
  20. #define QAIC_DBC_SIZE SZ_4K
  21. #define QAIC_SSR_DBC_SENTINEL U32_MAX /* No ongoing SSR sentinel */
  22. #define QAIC_NO_PARTITION -1
  23. #define QAIC_DBC_OFF(i) ((i) * QAIC_DBC_SIZE + QAIC_DBC_BASE)
  24. #define to_qaic_bo(obj) container_of(obj, struct qaic_bo, base)
  25. #define to_qaic_drm_device(dev) container_of(dev, struct qaic_drm_device, drm)
  26. #define to_drm(qddev) (&(qddev)->drm)
  27. #define to_accel_kdev(qddev) (to_drm(qddev)->accel->kdev) /* Return Linux device of accel node */
  28. #define to_qaic_device(dev) (to_qaic_drm_device((dev))->qdev)
  29. enum aic_families {
  30. FAMILY_AIC100,
  31. FAMILY_AIC200,
  32. FAMILY_MAX,
  33. };
  34. enum __packed dev_states {
  35. /* Device is offline or will be very soon */
  36. QAIC_OFFLINE,
  37. /* Device is booting, not clear if it's in a usable state */
  38. QAIC_BOOT,
  39. /* Device is fully operational */
  40. QAIC_ONLINE,
  41. };
  42. enum dbc_states {
  43. /* DBC is free and can be activated */
  44. DBC_STATE_IDLE,
  45. /* DBC is activated and a workload is running on device */
  46. DBC_STATE_ASSIGNED,
  47. /* Sub-system associated with this workload has crashed and it will shutdown soon */
  48. DBC_STATE_BEFORE_SHUTDOWN,
  49. /* Sub-system associated with this workload has crashed and it has shutdown */
  50. DBC_STATE_AFTER_SHUTDOWN,
  51. /* Sub-system associated with this workload is shutdown and it will be powered up soon */
  52. DBC_STATE_BEFORE_POWER_UP,
  53. /* Sub-system associated with this workload is now powered up */
  54. DBC_STATE_AFTER_POWER_UP,
  55. DBC_STATE_MAX,
  56. };
  57. extern bool datapath_polling;
  58. struct qaic_user {
  59. /* Uniquely identifies this user for the device */
  60. int handle;
  61. struct kref ref_count;
  62. /* Char device opened by this user */
  63. struct qaic_drm_device *qddev;
  64. /* Node in list of users that opened this drm device */
  65. struct list_head node;
  66. /* SRCU used to synchronize this user during cleanup */
  67. struct srcu_struct qddev_lock;
  68. atomic_t chunk_id;
  69. };
  70. struct dma_bridge_chan {
  71. /* Pointer to device strcut maintained by driver */
  72. struct qaic_device *qdev;
  73. /* ID of this DMA bridge channel(DBC) */
  74. unsigned int id;
  75. /* Synchronizes access to xfer_list */
  76. spinlock_t xfer_lock;
  77. /* Base address of request queue */
  78. void *req_q_base;
  79. /* Base address of response queue */
  80. void *rsp_q_base;
  81. /*
  82. * Base bus address of request queue. Response queue bus address can be
  83. * calculated by adding request queue size to this variable
  84. */
  85. dma_addr_t dma_addr;
  86. /* Total size of request and response queue in byte */
  87. u32 total_size;
  88. /* Capacity of request/response queue */
  89. u32 nelem;
  90. /* The user that opened this DBC */
  91. struct qaic_user *usr;
  92. /*
  93. * Request ID of next memory handle that goes in request queue. One
  94. * memory handle can enqueue more than one request elements, all
  95. * this requests that belong to same memory handle have same request ID
  96. */
  97. u16 next_req_id;
  98. /* true: DBC is in use; false: DBC not in use */
  99. bool in_use;
  100. /*
  101. * Base address of device registers. Used to read/write request and
  102. * response queue's head and tail pointer of this DBC.
  103. */
  104. void __iomem *dbc_base;
  105. /* Synchronizes access to Request queue's head and tail pointer */
  106. struct mutex req_lock;
  107. /* Head of list where each node is a memory handle queued in request queue */
  108. struct list_head xfer_list;
  109. /* Synchronizes DBC readers during cleanup */
  110. struct srcu_struct ch_lock;
  111. /*
  112. * When this DBC is released, any thread waiting on this wait queue is
  113. * woken up
  114. */
  115. wait_queue_head_t dbc_release;
  116. /* Head of list where each node is a bo associated with this DBC */
  117. struct list_head bo_lists;
  118. /* The irq line for this DBC. Used for polling */
  119. unsigned int irq;
  120. /* Polling work item to simulate interrupts */
  121. struct work_struct poll_work;
  122. /* Represents various states of this DBC from enum dbc_states */
  123. unsigned int state;
  124. };
  125. struct qaic_device {
  126. /* Pointer to base PCI device struct of our physical device */
  127. struct pci_dev *pdev;
  128. /* Req. ID of request that will be queued next in MHI control device */
  129. u32 next_seq_num;
  130. /* Base address of the MHI bar */
  131. void __iomem *bar_mhi;
  132. /* Base address of the DBCs bar */
  133. void __iomem *bar_dbc;
  134. /* Controller structure for MHI devices */
  135. struct mhi_controller *mhi_cntrl;
  136. /* MHI control channel device */
  137. struct mhi_device *cntl_ch;
  138. /* List of requests queued in MHI control device */
  139. struct list_head cntl_xfer_list;
  140. /* Synchronizes MHI control device transactions and its xfer list */
  141. struct mutex cntl_mutex;
  142. /* Array of DBC struct of this device */
  143. struct dma_bridge_chan *dbc;
  144. /* Work queue for tasks related to MHI control device */
  145. struct workqueue_struct *cntl_wq;
  146. /* Synchronizes all the users of device during cleanup */
  147. struct srcu_struct dev_lock;
  148. /* Track the state of the device during resets */
  149. enum dev_states dev_state;
  150. /* true: single MSI is used to operate device */
  151. bool single_msi;
  152. /*
  153. * true: A tx MHI transaction has failed and a rx buffer is still queued
  154. * in control device. Such a buffer is considered lost rx buffer
  155. * false: No rx buffer is lost in control device
  156. */
  157. bool cntl_lost_buf;
  158. /* Maximum number of DBC supported by this device */
  159. u32 num_dbc;
  160. /* Reference to the drm_device for this device when it is created */
  161. struct qaic_drm_device *qddev;
  162. /* Generate the CRC of a control message */
  163. u32 (*gen_crc)(void *msg);
  164. /* Validate the CRC of a control message */
  165. bool (*valid_crc)(void *msg);
  166. /* MHI "QAIC_TIMESYNC" channel device */
  167. struct mhi_device *qts_ch;
  168. /* Work queue for tasks related to MHI "QAIC_TIMESYNC" channel */
  169. struct workqueue_struct *qts_wq;
  170. /* MHI "QAIC_TIMESYNC_PERIODIC" channel device */
  171. struct mhi_device *mqts_ch;
  172. /* Head of list of page allocated by MHI bootlog device */
  173. struct list_head bootlog;
  174. /* MHI bootlog channel device */
  175. struct mhi_device *bootlog_ch;
  176. /* Work queue for tasks related to MHI bootlog device */
  177. struct workqueue_struct *bootlog_wq;
  178. /* Synchronizes access of pages in MHI bootlog device */
  179. struct mutex bootlog_mutex;
  180. /* MHI RAS channel device */
  181. struct mhi_device *ras_ch;
  182. /* Correctable error count */
  183. unsigned int ce_count;
  184. /* Un-correctable error count */
  185. unsigned int ue_count;
  186. /* Un-correctable non-fatal error count */
  187. unsigned int ue_nf_count;
  188. /* MHI SSR channel device */
  189. struct mhi_device *ssr_ch;
  190. /* Work queue for tasks related to MHI SSR device */
  191. struct workqueue_struct *ssr_wq;
  192. /* Buffer to collect SSR crashdump via SSR MHI channel */
  193. void *ssr_mhi_buf;
  194. /* DBC which is under SSR. Sentinel U32_MAX would mean that no SSR in progress */
  195. u32 ssr_dbc;
  196. };
  197. struct qaic_drm_device {
  198. /* The drm device struct of this drm device */
  199. struct drm_device drm;
  200. /* Pointer to the root device struct driven by this driver */
  201. struct qaic_device *qdev;
  202. /*
  203. * The physical device can be partition in number of logical devices.
  204. * And each logical device is given a partition id. This member stores
  205. * that id. QAIC_NO_PARTITION is a sentinel used to mark that this drm
  206. * device is the actual physical device
  207. */
  208. s32 partition_id;
  209. /* Head in list of users who have opened this drm device */
  210. struct list_head users;
  211. /* Synchronizes access to users list */
  212. struct mutex users_mutex;
  213. /* Pointer to array of DBC sysfs attributes */
  214. void *sysfs_attrs;
  215. };
  216. struct qaic_bo {
  217. struct drm_gem_object base;
  218. /* Scatter/gather table for allocate/imported BO */
  219. struct sg_table *sgt;
  220. /* Head in list of slices of this BO */
  221. struct list_head slices;
  222. /* Total nents, for all slices of this BO */
  223. int total_slice_nents;
  224. /*
  225. * Direction of transfer. It can assume only two value DMA_TO_DEVICE and
  226. * DMA_FROM_DEVICE.
  227. */
  228. int dir;
  229. /* The pointer of the DBC which operates on this BO */
  230. struct dma_bridge_chan *dbc;
  231. /* Number of slice that belongs to this buffer */
  232. u32 nr_slice;
  233. /* Number of slice that have been transferred by DMA engine */
  234. u32 nr_slice_xfer_done;
  235. /*
  236. * If true then user has attached slicing information to this BO by
  237. * calling DRM_IOCTL_QAIC_ATTACH_SLICE_BO ioctl.
  238. */
  239. bool sliced;
  240. /* Request ID of this BO if it is queued for execution */
  241. u16 req_id;
  242. /* Wait on this for completion of DMA transfer of this BO */
  243. struct completion xfer_done;
  244. /*
  245. * Node in linked list where head is dbc->xfer_list.
  246. * This link list contain BO's that are queued for DMA transfer.
  247. */
  248. struct list_head xfer_list;
  249. /*
  250. * Node in linked list where head is dbc->bo_lists.
  251. * This link list contain BO's that are associated with the DBC it is
  252. * linked to.
  253. */
  254. struct list_head bo_list;
  255. struct {
  256. /*
  257. * Latest timestamp(ns) at which kernel received a request to
  258. * execute this BO
  259. */
  260. u64 req_received_ts;
  261. /*
  262. * Latest timestamp(ns) at which kernel enqueued requests of
  263. * this BO for execution in DMA queue
  264. */
  265. u64 req_submit_ts;
  266. /*
  267. * Latest timestamp(ns) at which kernel received a completion
  268. * interrupt for requests of this BO
  269. */
  270. u64 req_processed_ts;
  271. /*
  272. * Number of elements already enqueued in DMA queue before
  273. * enqueuing requests of this BO
  274. */
  275. u32 queue_level_before;
  276. } perf_stats;
  277. /* Synchronizes BO operations */
  278. struct mutex lock;
  279. };
  280. struct bo_slice {
  281. /* Mapped pages */
  282. struct sg_table *sgt;
  283. /* Number of requests required to queue in DMA queue */
  284. int nents;
  285. /* See enum dma_data_direction */
  286. int dir;
  287. /* Actual requests that will be copied in DMA queue */
  288. struct dbc_req *reqs;
  289. struct kref ref_count;
  290. /* true: No DMA transfer required */
  291. bool no_xfer;
  292. /* Pointer to the parent BO handle */
  293. struct qaic_bo *bo;
  294. /* Node in list of slices maintained by parent BO */
  295. struct list_head slice;
  296. /* Size of this slice in bytes */
  297. u64 size;
  298. /* Offset of this slice in buffer */
  299. u64 offset;
  300. };
  301. int get_dbc_req_elem_size(void);
  302. int get_dbc_rsp_elem_size(void);
  303. int get_cntl_version(struct qaic_device *qdev, struct qaic_user *usr, u16 *major, u16 *minor);
  304. int qaic_manage_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  305. void qaic_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result);
  306. void qaic_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result);
  307. int qaic_control_open(struct qaic_device *qdev);
  308. void qaic_control_close(struct qaic_device *qdev);
  309. void qaic_release_usr(struct qaic_device *qdev, struct qaic_user *usr);
  310. irqreturn_t dbc_irq_threaded_fn(int irq, void *data);
  311. irqreturn_t dbc_irq_handler(int irq, void *data);
  312. int disable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr);
  313. void enable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr);
  314. void wakeup_dbc(struct qaic_device *qdev, u32 dbc_id);
  315. void release_dbc(struct qaic_device *qdev, u32 dbc_id);
  316. void qaic_data_get_fifo_info(struct dma_bridge_chan *dbc, u32 *head, u32 *tail);
  317. void wake_all_cntl(struct qaic_device *qdev);
  318. void qaic_dev_reset_clean_local_state(struct qaic_device *qdev);
  319. struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
  320. int qaic_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  321. int qaic_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  322. int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  323. int qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  324. int qaic_partial_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  325. int qaic_wait_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  326. int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  327. int qaic_detach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
  328. void qaic_irq_polling_work(struct work_struct *work);
  329. void qaic_dbc_enter_ssr(struct qaic_device *qdev, u32 dbc_id);
  330. void qaic_dbc_exit_ssr(struct qaic_device *qdev);
  331. /* qaic_sysfs.c */
  332. int qaic_sysfs_init(struct qaic_drm_device *qddev);
  333. void qaic_sysfs_remove(struct qaic_drm_device *qddev);
  334. void set_dbc_state(struct qaic_device *qdev, u32 dbc_id, unsigned int state);
  335. #endif /* _QAIC_H_ */