hw_fence_drv_priv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. enum hw_fence_lookup_ops {
  56. HW_FENCE_LOOKUP_OP_CREATE = 0x1,
  57. HW_FENCE_LOOKUP_OP_DESTROY,
  58. HW_FENCE_LOOKUP_OP_CREATE_JOIN,
  59. HW_FENCE_LOOKUP_OP_FIND_FENCE
  60. };
  61. /**
  62. * enum hw_fence_loopback_id - Enum with the clients having a loopback signal (i.e AP to AP signal).
  63. * HW_FENCE_LOOPBACK_DPU_CTL_0: dpu client 0. Used in platforms with no dpu-ipc.
  64. * HW_FENCE_LOOPBACK_DPU_CTL_1: dpu client 1. Used in platforms with no dpu-ipc.
  65. * HW_FENCE_LOOPBACK_DPU_CTL_2: dpu client 2. Used in platforms with no dpu-ipc.
  66. * HW_FENCE_LOOPBACK_DPU_CTL_3: dpu client 3. Used in platforms with no dpu-ipc.
  67. * HW_FENCE_LOOPBACK_DPU_CTL_4: dpu client 4. Used in platforms with no dpu-ipc.
  68. * HW_FENCE_LOOPBACK_DPU_CTL_5: dpu client 5. Used in platforms with no dpu-ipc.
  69. * HW_FENCE_LOOPBACK_DPU_CTX_0: gfx client 0. Used in platforms with no gmu support.
  70. */
  71. enum hw_fence_loopback_id {
  72. HW_FENCE_LOOPBACK_DPU_CTL_0,
  73. HW_FENCE_LOOPBACK_DPU_CTL_1,
  74. HW_FENCE_LOOPBACK_DPU_CTL_2,
  75. HW_FENCE_LOOPBACK_DPU_CTL_3,
  76. HW_FENCE_LOOPBACK_DPU_CTL_4,
  77. HW_FENCE_LOOPBACK_DPU_CTL_5,
  78. HW_FENCE_LOOPBACK_GFX_CTX_0,
  79. HW_FENCE_LOOPBACK_MAX,
  80. };
  81. #define HW_FENCE_MAX_DPU_LOOPBACK_CLIENTS (HW_FENCE_LOOPBACK_DPU_CTL_5 + 1)
  82. /**
  83. * struct msm_hw_fence_queue - Structure holding the data of the hw fence queues.
  84. * @va_queue: pointer to the virtual address of the queue elements
  85. * @q_size_bytes: size of the queue
  86. * @va_header: pointer to the hfi header virtual address
  87. * @pa_queue: physical address of the queue
  88. */
  89. struct msm_hw_fence_queue {
  90. void *va_queue;
  91. u32 q_size_bytes;
  92. void *va_header;
  93. phys_addr_t pa_queue;
  94. };
  95. /**
  96. * struct msm_hw_fence_client - Structure holding the per-Client allocated resources.
  97. * @client_id: id of the client
  98. * @mem_descriptor: hfi header memory descriptor
  99. * @queues: queues descriptor
  100. * @ipc_signal_id: id of the signal to be triggered for this client
  101. * @ipc_client_id: id of the ipc client for this hw fence driver client
  102. * @update_rxq: bool to indicate if client uses rx-queue
  103. */
  104. struct msm_hw_fence_client {
  105. enum hw_fence_client_id client_id;
  106. struct msm_hw_fence_mem_addr mem_descriptor;
  107. struct msm_hw_fence_queue queues[HW_FENCE_CLIENT_QUEUES];
  108. int ipc_signal_id;
  109. int ipc_client_id;
  110. bool update_rxq;
  111. };
  112. /**
  113. * struct msm_hw_fence_mem_data - Structure holding internal memory attributes
  114. *
  115. * @attrs: attributes for the memory allocation
  116. */
  117. struct msm_hw_fence_mem_data {
  118. unsigned long attrs;
  119. };
  120. /**
  121. * struct msm_hw_fence_dbg_data - Structure holding debugfs data
  122. *
  123. * @root: debugfs root
  124. * @entry_rd: flag to indicate if debugfs dumps a single line or table
  125. * @context_rd: debugfs setting to indicate which context id to dump
  126. * @seqno_rd: debugfs setting to indicate which seqno to dump
  127. * @hw_fence_sim_release_delay: delay in micro seconds for the debugfs node that simulates the
  128. * hw-fences behavior, to release the hw-fences
  129. * @create_hw_fences: boolean to continuosly create hw-fences within debugfs
  130. * @clients_list: list of debug clients registered
  131. * @clients_list_lock: lock to synchronize access to the clients list
  132. */
  133. struct msm_hw_fence_dbg_data {
  134. struct dentry *root;
  135. bool entry_rd;
  136. u64 context_rd;
  137. u64 seqno_rd;
  138. u32 hw_fence_sim_release_delay;
  139. bool create_hw_fences;
  140. struct list_head clients_list;
  141. struct mutex clients_list_lock;
  142. };
  143. /**
  144. * struct hw_fence_driver_data - Structure holding internal hw-fence driver data
  145. *
  146. * @dev: device driver pointer
  147. * @resources_ready: value set by driver at end of probe, once all resources are ready
  148. * @hw_fence_table_entries: total number of hw-fences in the global table
  149. * @hw_fence_mem_fences_table_size: hw-fences global table total size
  150. * @hw_fence_queue_entries: total number of entries that can be available in the queue
  151. * @hw_fence_ctrl_queue_size: size of the ctrl queue for the payload
  152. * @hw_fence_mem_ctrl_queues_size: total size of ctrl queues, including: header + rxq + txq
  153. * @hw_fence_client_queue_size: size of the client queue for the payload
  154. * @hw_fence_mem_clients_queues_size: total size of client queues, including: header + rxq + txq
  155. * @hw_fences_tbl: pointer to the hw-fences table
  156. * @hw_fences_tbl_cnt: number of elements in the hw-fence table
  157. * @client_lock_tbl: pointer to the per-client locks table
  158. * @client_lock_tbl_cnt: number of elements in the locks table
  159. * @hw_fences_mem_desc: memory descriptor for the hw-fence table
  160. * @clients_locks_mem_desc: memory descriptor for the locks table
  161. * @ctrl_queue_mem_desc: memory descriptor for the ctrl queues
  162. * @ctrl_queues: pointer to the ctrl queues
  163. * @io_mem_base: pointer to the carved-out io memory
  164. * @res: resources for the carved out memory
  165. * @size: size of the carved-out memory
  166. * @label: label for the carved-out memory (this is used by SVM to find the memory)
  167. * @peer_name: peer name for this carved-out memory
  168. * @rm_nb: hyp resource manager notifier
  169. * @memparcel: memparcel for the allocated memory
  170. * @db_label: doorbell label
  171. * @rx_dbl: handle to the Rx doorbell
  172. * @debugfs_data: debugfs info
  173. * @ipcc_reg_base: base for ipcc regs mapping
  174. * @ipcc_io_mem: base for the ipcc io mem map
  175. * @ipcc_size: size of the ipcc io mem mapping
  176. * @protocol_id: ipcc protocol id used by this driver
  177. * @ipcc_client_id: ipcc client id for this driver
  178. * @ipc_clients_table: table with the ipcc mapping for each client of this driver
  179. * @qtime_reg_base: qtimer register base address
  180. * @qtime_io_mem: qtimer io mem map
  181. * @qtime_size: qtimer io mem map size
  182. * @ctl_start_ptr: pointer to the ctl_start registers of the display hw (platforms with no dpu-ipc)
  183. * @ctl_start_size: size of the ctl_start registers of the display hw (platforms with no dpu-ipc)
  184. * @client_id_mask: bitmask for tracking registered client_ids
  185. * @clients_mask_lock: lock to synchronize access to the clients mask
  186. * @msm_hw_fence_client: table with the handles of the registered clients
  187. * @vm_ready: flag to indicate if vm has been initialized
  188. * @ipcc_dpu_initialized: flag to indicate if dpu hw is initialized
  189. */
  190. struct hw_fence_driver_data {
  191. struct device *dev;
  192. bool resources_ready;
  193. /* Table & Queues info */
  194. u32 hw_fence_table_entries;
  195. u32 hw_fence_mem_fences_table_size;
  196. u32 hw_fence_queue_entries;
  197. /* ctrl queues */
  198. u32 hw_fence_ctrl_queue_size;
  199. u32 hw_fence_mem_ctrl_queues_size;
  200. /* client queues */
  201. u32 hw_fence_client_queue_size;
  202. u32 hw_fence_mem_clients_queues_size;
  203. /* HW Fences Table VA */
  204. struct msm_hw_fence *hw_fences_tbl;
  205. u32 hw_fences_tbl_cnt;
  206. /* Table with a Per-Client Lock */
  207. u64 *client_lock_tbl;
  208. u32 client_lock_tbl_cnt;
  209. /* Memory Descriptors */
  210. struct msm_hw_fence_mem_addr hw_fences_mem_desc;
  211. struct msm_hw_fence_mem_addr clients_locks_mem_desc;
  212. struct msm_hw_fence_mem_addr ctrl_queue_mem_desc;
  213. struct msm_hw_fence_queue ctrl_queues[HW_FENCE_CTRL_QUEUES];
  214. /* carved out memory */
  215. void __iomem *io_mem_base;
  216. struct resource res;
  217. size_t size;
  218. u32 label;
  219. u32 peer_name;
  220. struct notifier_block rm_nb;
  221. u32 memparcel;
  222. /* doorbell */
  223. u32 db_label;
  224. /* VM virq */
  225. void *rx_dbl;
  226. /* debugfs */
  227. struct msm_hw_fence_dbg_data debugfs_data;
  228. /* ipcc regs */
  229. phys_addr_t ipcc_reg_base;
  230. void __iomem *ipcc_io_mem;
  231. uint32_t ipcc_size;
  232. u32 protocol_id;
  233. u32 ipcc_client_id;
  234. /* table with mapping of ipc client for each hw-fence client */
  235. struct hw_fence_client_ipc_map *ipc_clients_table;
  236. /* qtime reg */
  237. phys_addr_t qtime_reg_base;
  238. void __iomem *qtime_io_mem;
  239. uint32_t qtime_size;
  240. /* base address for dpu ctl start regs */
  241. void *ctl_start_ptr[HW_FENCE_MAX_DPU_LOOPBACK_CLIENTS];
  242. uint32_t ctl_start_size[HW_FENCE_MAX_DPU_LOOPBACK_CLIENTS];
  243. /* bitmask for tracking registered client_ids */
  244. u64 client_id_mask;
  245. struct mutex clients_mask_lock;
  246. /* table with registered client handles */
  247. struct msm_hw_fence_client *clients[HW_FENCE_CLIENT_MAX];
  248. bool vm_ready;
  249. #ifdef HW_DPU_IPCC
  250. /* state variables */
  251. bool ipcc_dpu_initialized;
  252. #endif /* HW_DPU_IPCC */
  253. };
  254. /**
  255. * struct msm_hw_fence_queue_payload - hardware fence clients queues payload.
  256. * @ctxt_id: context id of the dma fence
  257. * @seqno: sequence number of the dma fence
  258. * @hash: fence hash
  259. * @flags: see MSM_HW_FENCE_FLAG_* flags descriptions
  260. * @error: error code for this fence, fence controller receives this
  261. * error from the signaling client through the tx queue and
  262. * propagates the error to the waiting client through rx queue
  263. */
  264. struct msm_hw_fence_queue_payload {
  265. u64 ctxt_id;
  266. u64 seqno;
  267. u64 hash;
  268. u64 flags;
  269. u32 error;
  270. u32 unused; /* align to 64-bit */
  271. };
  272. /**
  273. * struct msm_hw_fence - structure holding each hw fence data.
  274. * @valid: field updated when a hw-fence is reserved. True if hw-fence is in use
  275. * @error: field to hold a hw-fence error
  276. * @ctx_id: context id
  277. * @seq_id: sequence id
  278. * @wait_client_mask: bitmask holding the waiting-clients of the fence
  279. * @fence_allocator: field to indicate the client_id that reserved the fence
  280. * @fence_signal-client:
  281. * @lock: this field is required to share information between the Driver & Driver ||
  282. * Driver & FenceCTL. Needs to be 64-bit atomic inter-processor lock.
  283. * @flags: field to indicate the state of the fence
  284. * @parent_list: list of indexes with the parents for a child-fence in a join-fence
  285. * @parent_cnt: total number of parents for a child-fence in a join-fence
  286. * @pending_child_cnt: children refcount for a parent-fence in a join-fence. Access must be atomic
  287. * or locked
  288. * @fence_create_time: debug info with the create time timestamp
  289. * @fence_trigger_time: debug info with the trigger time timestamp
  290. * @fence_wait_time: debug info with the register-for-wait timestamp
  291. * @debug_refcount: refcount used for debugging
  292. */
  293. struct msm_hw_fence {
  294. u32 valid;
  295. u32 error;
  296. u64 ctx_id;
  297. u64 seq_id;
  298. u64 wait_client_mask;
  299. u32 fence_allocator;
  300. u32 fence_signal_client;
  301. u64 lock; /* Datatype must be 64-bit. */
  302. u64 flags;
  303. u64 parent_list[MSM_HW_FENCE_MAX_JOIN_PARENTS];
  304. u32 parents_cnt;
  305. u32 pending_child_cnt;
  306. u64 fence_create_time;
  307. u64 fence_trigger_time;
  308. u64 fence_wait_time;
  309. u64 debug_refcount;
  310. };
  311. int hw_fence_init(struct hw_fence_driver_data *drv_data);
  312. int hw_fence_alloc_client_resources(struct hw_fence_driver_data *drv_data,
  313. struct msm_hw_fence_client *hw_fence_client,
  314. struct msm_hw_fence_mem_addr *mem_descriptor);
  315. int hw_fence_init_controller_signal(struct hw_fence_driver_data *drv_data,
  316. struct msm_hw_fence_client *hw_fence_client);
  317. int hw_fence_init_controller_resources(struct msm_hw_fence_client *hw_fence_client);
  318. void hw_fence_cleanup_client(struct hw_fence_driver_data *drv_data,
  319. struct msm_hw_fence_client *hw_fence_client);
  320. int hw_fence_create(struct hw_fence_driver_data *drv_data,
  321. struct msm_hw_fence_client *hw_fence_client,
  322. u64 context, u64 seqno, u64 *hash);
  323. int hw_fence_destroy(struct hw_fence_driver_data *drv_data,
  324. struct msm_hw_fence_client *hw_fence_client,
  325. u64 context, u64 seqno);
  326. int hw_fence_process_fence_array(struct hw_fence_driver_data *drv_data,
  327. struct msm_hw_fence_client *hw_fence_client,
  328. struct dma_fence_array *array);
  329. int hw_fence_process_fence(struct hw_fence_driver_data *drv_data,
  330. struct msm_hw_fence_client *hw_fence_client, struct dma_fence *fence);
  331. int hw_fence_update_queue(struct hw_fence_driver_data *drv_data,
  332. struct msm_hw_fence_client *hw_fence_client, u64 ctxt_id, u64 seqno, u64 hash,
  333. u64 flags, u32 error, int queue_type);
  334. inline u64 hw_fence_get_qtime(struct hw_fence_driver_data *drv_data);
  335. int hw_fence_read_queue(struct msm_hw_fence_client *hw_fence_client,
  336. struct msm_hw_fence_queue_payload *payload, int queue_type);
  337. int hw_fence_register_wait_client(struct hw_fence_driver_data *drv_data,
  338. struct msm_hw_fence_client *hw_fence_client, u64 context, u64 seqno);
  339. struct msm_hw_fence *msm_hw_fence_find(struct hw_fence_driver_data *drv_data,
  340. struct msm_hw_fence_client *hw_fence_client,
  341. u64 context, u64 seqno, u64 *hash);
  342. #endif /* __HW_FENCE_DRV_INTERNAL_H */