hw_fence_drv_priv.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #ifndef __HW_FENCE_DRV_INTERNAL_H
  6. #define __HW_FENCE_DRV_INTERNAL_H
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/types.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/soc/qcom/msm_hw_fence.h>
  12. #include <linux/dma-fence-array.h>
  13. #include <linux/slab.h>
  14. /* Add define only for platforms that support IPCC in dpu-hw */
  15. #define HW_DPU_IPCC 1
  16. /* max u64 to indicate invalid fence */
  17. #define HW_FENCE_INVALID_PARENT_FENCE (~0ULL)
  18. /* hash algorithm constants */
  19. #define HW_FENCE_HASH_A_MULT 4969 /* a multiplier for Hash algorithm */
  20. #define HW_FENCE_HASH_C_MULT 907 /* c multiplier for Hash algorithm */
  21. /* number of queues per type (i.e. ctrl or client queues) */
  22. #define HW_FENCE_CTRL_QUEUES 2 /* Rx and Tx Queues */
  23. #define HW_FENCE_CLIENT_QUEUES 2 /* Rx and Tx Queues */
  24. /* hfi headers calculation */
  25. #define HW_FENCE_HFI_TABLE_HEADER_SIZE (sizeof(struct msm_hw_fence_hfi_queue_table_header))
  26. #define HW_FENCE_HFI_QUEUE_HEADER_SIZE (sizeof(struct msm_hw_fence_hfi_queue_header))
  27. #define HW_FENCE_HFI_CTRL_HEADERS_SIZE (HW_FENCE_HFI_TABLE_HEADER_SIZE + \
  28. (HW_FENCE_HFI_QUEUE_HEADER_SIZE * HW_FENCE_CTRL_QUEUES))
  29. #define HW_FENCE_HFI_CLIENT_HEADERS_SIZE (HW_FENCE_HFI_TABLE_HEADER_SIZE + \
  30. (HW_FENCE_HFI_QUEUE_HEADER_SIZE * HW_FENCE_CLIENT_QUEUES))
  31. /*
  32. * Max Payload size is the bigest size of the message that we can have in the CTRL queue
  33. * in this case the max message is calculated like following, using 32-bits elements:
  34. * 1 header + 1 msg-type + 1 client_id + 2 hash + 1 error
  35. */
  36. #define HW_FENCE_CTRL_QUEUE_MAX_PAYLOAD_SIZE ((1 + 1 + 1 + 2 + 1) * sizeof(u32))
  37. #define HW_FENCE_CTRL_QUEUE_PAYLOAD HW_FENCE_CTRL_QUEUE_MAX_PAYLOAD_SIZE
  38. #define HW_FENCE_CLIENT_QUEUE_PAYLOAD (sizeof(struct msm_hw_fence_queue_payload))
  39. /* Locks area for all the clients */
  40. #define HW_FENCE_MEM_LOCKS_SIZE (sizeof(u64) * (HW_FENCE_CLIENT_MAX - 1))
  41. #define HW_FENCE_TX_QUEUE 1
  42. #define HW_FENCE_RX_QUEUE 2
  43. /* ClientID for the internal join fence, this is used by the framework when creating a join-fence */
  44. #define HW_FENCE_JOIN_FENCE_CLIENT_ID (~(u32)0)
  45. /**
  46. * msm hw fence flags:
  47. * MSM_HW_FENCE_FLAG_SIGNAL - Flag set when the hw-fence is signaled
  48. */
  49. #define MSM_HW_FENCE_FLAG_SIGNAL BIT(0)
  50. /**
  51. * MSM_HW_FENCE_MAX_JOIN_PARENTS:
  52. * Maximum number of parents that a fence can have for a join-fence
  53. */
  54. #define MSM_HW_FENCE_MAX_JOIN_PARENTS 3
  55. /**
  56. * HW_FENCE_PAYLOAD_REV:
  57. * Payload version with major and minor version information
  58. */
  59. #define HW_FENCE_PAYLOAD_REV(major, minor) (major << 8 | (minor & 0xFF))
  60. enum hw_fence_lookup_ops {
  61. HW_FENCE_LOOKUP_OP_CREATE = 0x1,
  62. HW_FENCE_LOOKUP_OP_DESTROY,
  63. HW_FENCE_LOOKUP_OP_CREATE_JOIN,
  64. HW_FENCE_LOOKUP_OP_FIND_FENCE
  65. };
  66. /**
  67. * enum hw_fence_loopback_id - Enum with the clients having a loopback signal (i.e AP to AP signal).
  68. * HW_FENCE_LOOPBACK_DPU_CTL_0: dpu client 0. Used in platforms with no dpu-ipc.
  69. * HW_FENCE_LOOPBACK_DPU_CTL_1: dpu client 1. Used in platforms with no dpu-ipc.
  70. * HW_FENCE_LOOPBACK_DPU_CTL_2: dpu client 2. Used in platforms with no dpu-ipc.
  71. * HW_FENCE_LOOPBACK_DPU_CTL_3: dpu client 3. Used in platforms with no dpu-ipc.
  72. * HW_FENCE_LOOPBACK_DPU_CTL_4: dpu client 4. Used in platforms with no dpu-ipc.
  73. * HW_FENCE_LOOPBACK_DPU_CTL_5: dpu client 5. Used in platforms with no dpu-ipc.
  74. * HW_FENCE_LOOPBACK_DPU_CTX_0: gfx client 0. Used in platforms with no gmu support.
  75. * HW_FENCE_LOOPBACK_VAL_0: debug validation client 0.
  76. * HW_FENCE_LOOPBACK_VAL_1: debug validation client 1.
  77. * HW_FENCE_LOOPBACK_VAL_2: debug validation client 2.
  78. * HW_FENCE_LOOPBACK_VAL_3: debug validation client 3.
  79. * HW_FENCE_LOOPBACK_VAL_4: debug validation client 4.
  80. * HW_FENCE_LOOPBACK_VAL_5: debug validation client 5.
  81. * HW_FENCE_LOOPBACK_VAL_6: debug validation client 6.
  82. */
  83. enum hw_fence_loopback_id {
  84. HW_FENCE_LOOPBACK_DPU_CTL_0,
  85. HW_FENCE_LOOPBACK_DPU_CTL_1,
  86. HW_FENCE_LOOPBACK_DPU_CTL_2,
  87. HW_FENCE_LOOPBACK_DPU_CTL_3,
  88. HW_FENCE_LOOPBACK_DPU_CTL_4,
  89. HW_FENCE_LOOPBACK_DPU_CTL_5,
  90. HW_FENCE_LOOPBACK_GFX_CTX_0,
  91. #if IS_ENABLED(CONFIG_DEBUG_FS)
  92. HW_FENCE_LOOPBACK_VAL_0,
  93. HW_FENCE_LOOPBACK_VAL_1,
  94. HW_FENCE_LOOPBACK_VAL_2,
  95. HW_FENCE_LOOPBACK_VAL_3,
  96. HW_FENCE_LOOPBACK_VAL_4,
  97. HW_FENCE_LOOPBACK_VAL_5,
  98. HW_FENCE_LOOPBACK_VAL_6,
  99. #endif /* CONFIG_DEBUG_FS */
  100. HW_FENCE_LOOPBACK_MAX,
  101. };
  102. #define HW_FENCE_MAX_DPU_LOOPBACK_CLIENTS (HW_FENCE_LOOPBACK_DPU_CTL_5 + 1)
  103. /**
  104. * struct msm_hw_fence_queue - Structure holding the data of the hw fence queues.
  105. * @va_queue: pointer to the virtual address of the queue elements
  106. * @q_size_bytes: size of the queue
  107. * @va_header: pointer to the hfi header virtual address
  108. * @pa_queue: physical address of the queue
  109. */
  110. struct msm_hw_fence_queue {
  111. void *va_queue;
  112. u32 q_size_bytes;
  113. void *va_header;
  114. phys_addr_t pa_queue;
  115. };
  116. /**
  117. * enum payload_type - Enum with the queue payload types.
  118. */
  119. enum payload_type {
  120. HW_FENCE_PAYLOAD_TYPE_1 = 1
  121. };
  122. /**
  123. * struct msm_hw_fence_client - Structure holding the per-Client allocated resources.
  124. * @client_id: id of the client
  125. * @mem_descriptor: hfi header memory descriptor
  126. * @queues: queues descriptor
  127. * @ipc_signal_id: id of the signal to be triggered for this client
  128. * @ipc_client_vid: virtual id of the ipc client for this hw fence driver client
  129. * @ipc_client_pid: physical id of the ipc client for this hw fence driver client
  130. * @update_rxq: bool to indicate if client uses rx-queue
  131. * @send_ipc: bool to indicate if client requires ipc interrupt for already signaled fences
  132. * @wait_queue: wait queue for the validation clients
  133. * @val_signal: doorbell flag to signal the validation clients in the wait queue
  134. */
  135. struct msm_hw_fence_client {
  136. enum hw_fence_client_id client_id;
  137. struct msm_hw_fence_mem_addr mem_descriptor;
  138. struct msm_hw_fence_queue queues[HW_FENCE_CLIENT_QUEUES];
  139. int ipc_signal_id;
  140. int ipc_client_vid;
  141. int ipc_client_pid;
  142. bool update_rxq;
  143. bool send_ipc;
  144. #if IS_ENABLED(CONFIG_DEBUG_FS)
  145. wait_queue_head_t wait_queue;
  146. atomic_t val_signal;
  147. #endif /* CONFIG_DEBUG_FS */
  148. };
  149. /**
  150. * struct msm_hw_fence_mem_data - Structure holding internal memory attributes
  151. *
  152. * @attrs: attributes for the memory allocation
  153. */
  154. struct msm_hw_fence_mem_data {
  155. unsigned long attrs;
  156. };
  157. /**
  158. * struct msm_hw_fence_dbg_data - Structure holding debugfs data
  159. *
  160. * @root: debugfs root
  161. * @entry_rd: flag to indicate if debugfs dumps a single line or table
  162. * @context_rd: debugfs setting to indicate which context id to dump
  163. * @seqno_rd: debugfs setting to indicate which seqno to dump
  164. * @hw_fence_sim_release_delay: delay in micro seconds for the debugfs node that simulates the
  165. * hw-fences behavior, to release the hw-fences
  166. * @create_hw_fences: boolean to continuosly create hw-fences within debugfs
  167. * @clients_list: list of debug clients registered
  168. * @clients_list_lock: lock to synchronize access to the clients list
  169. * @lock_wake_cnt: number of times that driver triggers wake-up ipcc to unlock inter-vm try-lock
  170. */
  171. struct msm_hw_fence_dbg_data {
  172. struct dentry *root;
  173. bool entry_rd;
  174. u64 context_rd;
  175. u64 seqno_rd;
  176. u32 hw_fence_sim_release_delay;
  177. bool create_hw_fences;
  178. struct list_head clients_list;
  179. struct mutex clients_list_lock;
  180. u64 lock_wake_cnt;
  181. };
  182. /**
  183. * struct hw_fence_driver_data - Structure holding internal hw-fence driver data
  184. *
  185. * @dev: device driver pointer
  186. * @resources_ready: value set by driver at end of probe, once all resources are ready
  187. * @hw_fence_table_entries: total number of hw-fences in the global table
  188. * @hw_fence_mem_fences_table_size: hw-fences global table total size
  189. * @hw_fence_queue_entries: total number of entries that can be available in the queue
  190. * @hw_fence_ctrl_queue_size: size of the ctrl queue for the payload
  191. * @hw_fence_mem_ctrl_queues_size: total size of ctrl queues, including: header + rxq + txq
  192. * @hw_fence_client_queue_size: size of the client queue for the payload
  193. * @hw_fence_mem_clients_queues_size: total size of client queues, including: header + rxq + txq
  194. * @hw_fences_tbl: pointer to the hw-fences table
  195. * @hw_fences_tbl_cnt: number of elements in the hw-fence table
  196. * @client_lock_tbl: pointer to the per-client locks table
  197. * @client_lock_tbl_cnt: number of elements in the locks table
  198. * @hw_fences_mem_desc: memory descriptor for the hw-fence table
  199. * @clients_locks_mem_desc: memory descriptor for the locks table
  200. * @ctrl_queue_mem_desc: memory descriptor for the ctrl queues
  201. * @ctrl_queues: pointer to the ctrl queues
  202. * @io_mem_base: pointer to the carved-out io memory
  203. * @res: resources for the carved out memory
  204. * @size: size of the carved-out memory
  205. * @label: label for the carved-out memory (this is used by SVM to find the memory)
  206. * @peer_name: peer name for this carved-out memory
  207. * @rm_nb: hyp resource manager notifier
  208. * @memparcel: memparcel for the allocated memory
  209. * @db_label: doorbell label
  210. * @rx_dbl: handle to the Rx doorbell
  211. * @debugfs_data: debugfs info
  212. * @ipcc_reg_base: base for ipcc regs mapping
  213. * @ipcc_io_mem: base for the ipcc io mem map
  214. * @ipcc_size: size of the ipcc io mem mapping
  215. * @protocol_id: ipcc protocol id used by this driver
  216. * @ipcc_client_vid: ipcc client virtual-id for this driver
  217. * @ipcc_client_pid: ipcc client physical-id for this driver
  218. * @ipc_clients_table: table with the ipcc mapping for each client of this driver
  219. * @qtime_reg_base: qtimer register base address
  220. * @qtime_io_mem: qtimer io mem map
  221. * @qtime_size: qtimer io mem map size
  222. * @ctl_start_ptr: pointer to the ctl_start registers of the display hw (platforms with no dpu-ipc)
  223. * @ctl_start_size: size of the ctl_start registers of the display hw (platforms with no dpu-ipc)
  224. * @client_id_mask: bitmask for tracking registered client_ids
  225. * @clients_register_lock: lock to synchronize clients registration and deregistration
  226. * @msm_hw_fence_client: table with the handles of the registered clients
  227. * @vm_ready: flag to indicate if vm has been initialized
  228. * @ipcc_dpu_initialized: flag to indicate if dpu hw is initialized
  229. */
  230. struct hw_fence_driver_data {
  231. struct device *dev;
  232. bool resources_ready;
  233. /* Table & Queues info */
  234. u32 hw_fence_table_entries;
  235. u32 hw_fence_mem_fences_table_size;
  236. u32 hw_fence_queue_entries;
  237. /* ctrl queues */
  238. u32 hw_fence_ctrl_queue_size;
  239. u32 hw_fence_mem_ctrl_queues_size;
  240. /* client queues */
  241. u32 hw_fence_client_queue_size;
  242. u32 hw_fence_mem_clients_queues_size;
  243. /* HW Fences Table VA */
  244. struct msm_hw_fence *hw_fences_tbl;
  245. u32 hw_fences_tbl_cnt;
  246. /* Table with a Per-Client Lock */
  247. u64 *client_lock_tbl;
  248. u32 client_lock_tbl_cnt;
  249. /* Memory Descriptors */
  250. struct msm_hw_fence_mem_addr hw_fences_mem_desc;
  251. struct msm_hw_fence_mem_addr clients_locks_mem_desc;
  252. struct msm_hw_fence_mem_addr ctrl_queue_mem_desc;
  253. struct msm_hw_fence_queue ctrl_queues[HW_FENCE_CTRL_QUEUES];
  254. /* carved out memory */
  255. void __iomem *io_mem_base;
  256. struct resource res;
  257. size_t size;
  258. u32 label;
  259. u32 peer_name;
  260. struct notifier_block rm_nb;
  261. u32 memparcel;
  262. /* doorbell */
  263. u32 db_label;
  264. /* VM virq */
  265. void *rx_dbl;
  266. /* debugfs */
  267. struct msm_hw_fence_dbg_data debugfs_data;
  268. /* ipcc regs */
  269. phys_addr_t ipcc_reg_base;
  270. void __iomem *ipcc_io_mem;
  271. uint32_t ipcc_size;
  272. u32 protocol_id;
  273. u32 ipcc_client_vid;
  274. u32 ipcc_client_pid;
  275. /* table with mapping of ipc client for each hw-fence client */
  276. struct hw_fence_client_ipc_map *ipc_clients_table;
  277. /* qtime reg */
  278. phys_addr_t qtime_reg_base;
  279. void __iomem *qtime_io_mem;
  280. uint32_t qtime_size;
  281. /* base address for dpu ctl start regs */
  282. void *ctl_start_ptr[HW_FENCE_MAX_DPU_LOOPBACK_CLIENTS];
  283. uint32_t ctl_start_size[HW_FENCE_MAX_DPU_LOOPBACK_CLIENTS];
  284. /* synchronize client_ids registration and deregistration */
  285. struct mutex clients_register_lock;
  286. /* table with registered client handles */
  287. struct msm_hw_fence_client *clients[HW_FENCE_CLIENT_MAX];
  288. bool vm_ready;
  289. #ifdef HW_DPU_IPCC
  290. /* state variables */
  291. bool ipcc_dpu_initialized;
  292. #endif /* HW_DPU_IPCC */
  293. };
  294. /**
  295. * struct msm_hw_fence_queue_payload - hardware fence clients queues payload.
  296. * @size: size of queue payload
  297. * @type: type of queue payload
  298. * @version: version of queue payload. High eight bits are for major and lower eight
  299. * bits are for minor version
  300. * @ctxt_id: context id of the dma fence
  301. * @seqno: sequence number of the dma fence
  302. * @hash: fence hash
  303. * @flags: see MSM_HW_FENCE_FLAG_* flags descriptions
  304. * @client_data: data passed from and returned to waiting client upon fence signaling
  305. * @error: error code for this fence, fence controller receives this
  306. * error from the signaling client through the tx queue and
  307. * propagates the error to the waiting client through rx queue
  308. * @timestamp_lo: low 32-bits of qtime of when the payload is written into the queue
  309. * @timestamp_hi: high 32-bits of qtime of when the payload is written into the queue
  310. */
  311. struct msm_hw_fence_queue_payload {
  312. u32 size;
  313. u16 type;
  314. u16 version;
  315. u64 ctxt_id;
  316. u64 seqno;
  317. u64 hash;
  318. u64 flags;
  319. u64 client_data;
  320. u32 error;
  321. u32 timestamp_lo;
  322. u32 timestamp_hi;
  323. u32 reserve;
  324. };
  325. /**
  326. * struct msm_hw_fence - structure holding each hw fence data.
  327. * @valid: field updated when a hw-fence is reserved. True if hw-fence is in use
  328. * @error: field to hold a hw-fence error
  329. * @ctx_id: context id
  330. * @seq_id: sequence id
  331. * @wait_client_mask: bitmask holding the waiting-clients of the fence
  332. * @fence_allocator: field to indicate the client_id that reserved the fence
  333. * @fence_signal-client:
  334. * @lock: this field is required to share information between the Driver & Driver ||
  335. * Driver & FenceCTL. Needs to be 64-bit atomic inter-processor lock.
  336. * @flags: field to indicate the state of the fence
  337. * @parent_list: list of indexes with the parents for a child-fence in a join-fence
  338. * @parent_cnt: total number of parents for a child-fence in a join-fence
  339. * @pending_child_cnt: children refcount for a parent-fence in a join-fence. Access must be atomic
  340. * or locked
  341. * @fence_create_time: debug info with the create time timestamp
  342. * @fence_trigger_time: debug info with the trigger time timestamp
  343. * @fence_wait_time: debug info with the register-for-wait timestamp
  344. * @debug_refcount: refcount used for debugging
  345. */
  346. struct msm_hw_fence {
  347. u32 valid;
  348. u32 error;
  349. u64 ctx_id;
  350. u64 seq_id;
  351. u64 wait_client_mask;
  352. u32 fence_allocator;
  353. u32 fence_signal_client;
  354. u64 lock; /* Datatype must be 64-bit. */
  355. u64 flags;
  356. u64 parent_list[MSM_HW_FENCE_MAX_JOIN_PARENTS];
  357. u32 parents_cnt;
  358. u32 pending_child_cnt;
  359. u64 fence_create_time;
  360. u64 fence_trigger_time;
  361. u64 fence_wait_time;
  362. u64 debug_refcount;
  363. };
  364. int hw_fence_init(struct hw_fence_driver_data *drv_data);
  365. int hw_fence_alloc_client_resources(struct hw_fence_driver_data *drv_data,
  366. struct msm_hw_fence_client *hw_fence_client,
  367. struct msm_hw_fence_mem_addr *mem_descriptor);
  368. int hw_fence_init_controller_signal(struct hw_fence_driver_data *drv_data,
  369. struct msm_hw_fence_client *hw_fence_client);
  370. int hw_fence_init_controller_resources(struct msm_hw_fence_client *hw_fence_client);
  371. void hw_fence_cleanup_client(struct hw_fence_driver_data *drv_data,
  372. struct msm_hw_fence_client *hw_fence_client);
  373. int hw_fence_create(struct hw_fence_driver_data *drv_data,
  374. struct msm_hw_fence_client *hw_fence_client,
  375. u64 context, u64 seqno, u64 *hash);
  376. int hw_fence_destroy(struct hw_fence_driver_data *drv_data,
  377. struct msm_hw_fence_client *hw_fence_client,
  378. u64 context, u64 seqno);
  379. int hw_fence_process_fence_array(struct hw_fence_driver_data *drv_data,
  380. struct msm_hw_fence_client *hw_fence_client,
  381. struct dma_fence_array *array);
  382. int hw_fence_process_fence(struct hw_fence_driver_data *drv_data,
  383. struct msm_hw_fence_client *hw_fence_client, struct dma_fence *fence);
  384. int hw_fence_update_queue(struct hw_fence_driver_data *drv_data,
  385. struct msm_hw_fence_client *hw_fence_client, u64 ctxt_id, u64 seqno, u64 hash,
  386. u64 flags, u32 error, int queue_type);
  387. inline u64 hw_fence_get_qtime(struct hw_fence_driver_data *drv_data);
  388. int hw_fence_read_queue(struct msm_hw_fence_client *hw_fence_client,
  389. struct msm_hw_fence_queue_payload *payload, int queue_type);
  390. int hw_fence_register_wait_client(struct hw_fence_driver_data *drv_data,
  391. struct dma_fence *fence, struct msm_hw_fence_client *hw_fence_client, u64 context,
  392. u64 seqno);
  393. struct msm_hw_fence *msm_hw_fence_find(struct hw_fence_driver_data *drv_data,
  394. struct msm_hw_fence_client *hw_fence_client,
  395. u64 context, u64 seqno, u64 *hash);
  396. #endif /* __HW_FENCE_DRV_INTERNAL_H */