kfd_mqd_manager_cik.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/printk.h>
  24. #include <linux/slab.h>
  25. #include "kfd_priv.h"
  26. #include "kfd_mqd_manager.h"
  27. #include "cik_regs.h"
  28. #include "cik_structs.h"
  29. #include "oss/oss_2_4_sh_mask.h"
  30. static inline struct cik_mqd *get_mqd(void *mqd)
  31. {
  32. return (struct cik_mqd *)mqd;
  33. }
  34. static int init_mqd(struct mqd_manager *mm, void **mqd,
  35. struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
  36. struct queue_properties *q)
  37. {
  38. uint64_t addr;
  39. struct cik_mqd *m;
  40. int retval;
  41. BUG_ON(!mm || !q || !mqd);
  42. pr_debug("kfd: In func %s\n", __func__);
  43. retval = kfd_gtt_sa_allocate(mm->dev, sizeof(struct cik_mqd),
  44. mqd_mem_obj);
  45. if (retval != 0)
  46. return -ENOMEM;
  47. m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
  48. addr = (*mqd_mem_obj)->gpu_addr;
  49. memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
  50. m->header = 0xC0310800;
  51. m->compute_pipelinestat_enable = 1;
  52. m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
  53. m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
  54. m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
  55. m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
  56. /*
  57. * Make sure to use the last queue state saved on mqd when the cp
  58. * reassigns the queue, so when queue is switched on/off (e.g over
  59. * subscription or quantum timeout) the context will be consistent
  60. */
  61. m->cp_hqd_persistent_state =
  62. DEFAULT_CP_HQD_PERSISTENT_STATE | PRELOAD_REQ;
  63. m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
  64. m->cp_mqd_base_addr_lo = lower_32_bits(addr);
  65. m->cp_mqd_base_addr_hi = upper_32_bits(addr);
  66. m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE | IB_ATC_EN;
  67. /* Although WinKFD writes this, I suspect it should not be necessary */
  68. m->cp_hqd_ib_control = IB_ATC_EN | DEFAULT_MIN_IB_AVAIL_SIZE;
  69. m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
  70. QUANTUM_DURATION(10);
  71. /*
  72. * Pipe Priority
  73. * Identifies the pipe relative priority when this queue is connected
  74. * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
  75. * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
  76. * 0 = CS_LOW (typically below GFX)
  77. * 1 = CS_MEDIUM (typically between HP3D and GFX
  78. * 2 = CS_HIGH (typically above HP3D)
  79. */
  80. m->cp_hqd_pipe_priority = 1;
  81. m->cp_hqd_queue_priority = 15;
  82. if (q->format == KFD_QUEUE_FORMAT_AQL)
  83. m->cp_hqd_iq_rptr = AQL_ENABLE;
  84. *mqd = m;
  85. if (gart_addr != NULL)
  86. *gart_addr = addr;
  87. retval = mm->update_mqd(mm, m, q);
  88. return retval;
  89. }
  90. static int init_mqd_sdma(struct mqd_manager *mm, void **mqd,
  91. struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
  92. struct queue_properties *q)
  93. {
  94. int retval;
  95. struct cik_sdma_rlc_registers *m;
  96. BUG_ON(!mm || !mqd || !mqd_mem_obj);
  97. retval = kfd_gtt_sa_allocate(mm->dev,
  98. sizeof(struct cik_sdma_rlc_registers),
  99. mqd_mem_obj);
  100. if (retval != 0)
  101. return -ENOMEM;
  102. m = (struct cik_sdma_rlc_registers *) (*mqd_mem_obj)->cpu_ptr;
  103. memset(m, 0, sizeof(struct cik_sdma_rlc_registers));
  104. *mqd = m;
  105. if (gart_addr != NULL)
  106. *gart_addr = (*mqd_mem_obj)->gpu_addr;
  107. retval = mm->update_mqd(mm, m, q);
  108. return retval;
  109. }
  110. static void uninit_mqd(struct mqd_manager *mm, void *mqd,
  111. struct kfd_mem_obj *mqd_mem_obj)
  112. {
  113. BUG_ON(!mm || !mqd);
  114. kfd_gtt_sa_free(mm->dev, mqd_mem_obj);
  115. }
  116. static void uninit_mqd_sdma(struct mqd_manager *mm, void *mqd,
  117. struct kfd_mem_obj *mqd_mem_obj)
  118. {
  119. BUG_ON(!mm || !mqd);
  120. kfd_gtt_sa_free(mm->dev, mqd_mem_obj);
  121. }
  122. static int load_mqd(struct mqd_manager *mm, void *mqd, uint32_t pipe_id,
  123. uint32_t queue_id, uint32_t __user *wptr)
  124. {
  125. return mm->dev->kfd2kgd->hqd_load
  126. (mm->dev->kgd, mqd, pipe_id, queue_id, wptr);
  127. }
  128. static int load_mqd_sdma(struct mqd_manager *mm, void *mqd,
  129. uint32_t pipe_id, uint32_t queue_id,
  130. uint32_t __user *wptr)
  131. {
  132. return mm->dev->kfd2kgd->hqd_sdma_load(mm->dev->kgd, mqd);
  133. }
  134. static int update_mqd(struct mqd_manager *mm, void *mqd,
  135. struct queue_properties *q)
  136. {
  137. struct cik_mqd *m;
  138. BUG_ON(!mm || !q || !mqd);
  139. pr_debug("kfd: In func %s\n", __func__);
  140. m = get_mqd(mqd);
  141. m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
  142. DEFAULT_MIN_AVAIL_SIZE | PQ_ATC_EN;
  143. /*
  144. * Calculating queue size which is log base 2 of actual queue size -1
  145. * dwords and another -1 for ffs
  146. */
  147. m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
  148. - 1 - 1;
  149. m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
  150. m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
  151. m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
  152. m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
  153. m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
  154. DOORBELL_OFFSET(q->doorbell_off);
  155. m->cp_hqd_vmid = q->vmid;
  156. if (q->format == KFD_QUEUE_FORMAT_AQL) {
  157. m->cp_hqd_pq_control |= NO_UPDATE_RPTR;
  158. }
  159. m->cp_hqd_active = 0;
  160. q->is_active = false;
  161. if (q->queue_size > 0 &&
  162. q->queue_address != 0 &&
  163. q->queue_percent > 0) {
  164. m->cp_hqd_active = 1;
  165. q->is_active = true;
  166. }
  167. return 0;
  168. }
  169. static int update_mqd_sdma(struct mqd_manager *mm, void *mqd,
  170. struct queue_properties *q)
  171. {
  172. struct cik_sdma_rlc_registers *m;
  173. BUG_ON(!mm || !mqd || !q);
  174. m = get_sdma_mqd(mqd);
  175. m->sdma_rlc_rb_cntl = (ffs(q->queue_size / sizeof(unsigned int)) - 1)
  176. << SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT |
  177. q->vmid << SDMA0_RLC0_RB_CNTL__RB_VMID__SHIFT |
  178. 1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
  179. 6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT;
  180. m->sdma_rlc_rb_base = lower_32_bits(q->queue_address >> 8);
  181. m->sdma_rlc_rb_base_hi = upper_32_bits(q->queue_address >> 8);
  182. m->sdma_rlc_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
  183. m->sdma_rlc_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
  184. m->sdma_rlc_doorbell = q->doorbell_off <<
  185. SDMA0_RLC0_DOORBELL__OFFSET__SHIFT |
  186. 1 << SDMA0_RLC0_DOORBELL__ENABLE__SHIFT;
  187. m->sdma_rlc_virtual_addr = q->sdma_vm_addr;
  188. m->sdma_engine_id = q->sdma_engine_id;
  189. m->sdma_queue_id = q->sdma_queue_id;
  190. q->is_active = false;
  191. if (q->queue_size > 0 &&
  192. q->queue_address != 0 &&
  193. q->queue_percent > 0) {
  194. m->sdma_rlc_rb_cntl |=
  195. 1 << SDMA0_RLC0_RB_CNTL__RB_ENABLE__SHIFT;
  196. q->is_active = true;
  197. }
  198. return 0;
  199. }
  200. static int destroy_mqd(struct mqd_manager *mm, void *mqd,
  201. enum kfd_preempt_type type,
  202. unsigned int timeout, uint32_t pipe_id,
  203. uint32_t queue_id)
  204. {
  205. return mm->dev->kfd2kgd->hqd_destroy(mm->dev->kgd, type, timeout,
  206. pipe_id, queue_id);
  207. }
  208. /*
  209. * preempt type here is ignored because there is only one way
  210. * to preempt sdma queue
  211. */
  212. static int destroy_mqd_sdma(struct mqd_manager *mm, void *mqd,
  213. enum kfd_preempt_type type,
  214. unsigned int timeout, uint32_t pipe_id,
  215. uint32_t queue_id)
  216. {
  217. return mm->dev->kfd2kgd->hqd_sdma_destroy(mm->dev->kgd, mqd, timeout);
  218. }
  219. static bool is_occupied(struct mqd_manager *mm, void *mqd,
  220. uint64_t queue_address, uint32_t pipe_id,
  221. uint32_t queue_id)
  222. {
  223. return mm->dev->kfd2kgd->hqd_is_occupied(mm->dev->kgd, queue_address,
  224. pipe_id, queue_id);
  225. }
  226. static bool is_occupied_sdma(struct mqd_manager *mm, void *mqd,
  227. uint64_t queue_address, uint32_t pipe_id,
  228. uint32_t queue_id)
  229. {
  230. return mm->dev->kfd2kgd->hqd_sdma_is_occupied(mm->dev->kgd, mqd);
  231. }
  232. /*
  233. * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
  234. * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
  235. * queues but with different initial values.
  236. */
  237. static int init_mqd_hiq(struct mqd_manager *mm, void **mqd,
  238. struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
  239. struct queue_properties *q)
  240. {
  241. uint64_t addr;
  242. struct cik_mqd *m;
  243. int retval;
  244. BUG_ON(!mm || !q || !mqd || !mqd_mem_obj);
  245. pr_debug("kfd: In func %s\n", __func__);
  246. retval = kfd_gtt_sa_allocate(mm->dev, sizeof(struct cik_mqd),
  247. mqd_mem_obj);
  248. if (retval != 0)
  249. return -ENOMEM;
  250. m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
  251. addr = (*mqd_mem_obj)->gpu_addr;
  252. memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
  253. m->header = 0xC0310800;
  254. m->compute_pipelinestat_enable = 1;
  255. m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
  256. m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
  257. m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
  258. m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
  259. m->cp_hqd_persistent_state = DEFAULT_CP_HQD_PERSISTENT_STATE |
  260. PRELOAD_REQ;
  261. m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
  262. QUANTUM_DURATION(10);
  263. m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
  264. m->cp_mqd_base_addr_lo = lower_32_bits(addr);
  265. m->cp_mqd_base_addr_hi = upper_32_bits(addr);
  266. m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE;
  267. /*
  268. * Pipe Priority
  269. * Identifies the pipe relative priority when this queue is connected
  270. * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
  271. * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
  272. * 0 = CS_LOW (typically below GFX)
  273. * 1 = CS_MEDIUM (typically between HP3D and GFX
  274. * 2 = CS_HIGH (typically above HP3D)
  275. */
  276. m->cp_hqd_pipe_priority = 1;
  277. m->cp_hqd_queue_priority = 15;
  278. *mqd = m;
  279. if (gart_addr)
  280. *gart_addr = addr;
  281. retval = mm->update_mqd(mm, m, q);
  282. return retval;
  283. }
  284. static int update_mqd_hiq(struct mqd_manager *mm, void *mqd,
  285. struct queue_properties *q)
  286. {
  287. struct cik_mqd *m;
  288. BUG_ON(!mm || !q || !mqd);
  289. pr_debug("kfd: In func %s\n", __func__);
  290. m = get_mqd(mqd);
  291. m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
  292. DEFAULT_MIN_AVAIL_SIZE |
  293. PRIV_STATE |
  294. KMD_QUEUE;
  295. /*
  296. * Calculating queue size which is log base 2 of actual queue
  297. * size -1 dwords
  298. */
  299. m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
  300. - 1 - 1;
  301. m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
  302. m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
  303. m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
  304. m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
  305. m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
  306. DOORBELL_OFFSET(q->doorbell_off);
  307. m->cp_hqd_vmid = q->vmid;
  308. m->cp_hqd_active = 0;
  309. q->is_active = false;
  310. if (q->queue_size > 0 &&
  311. q->queue_address != 0 &&
  312. q->queue_percent > 0) {
  313. m->cp_hqd_active = 1;
  314. q->is_active = true;
  315. }
  316. return 0;
  317. }
  318. struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd)
  319. {
  320. struct cik_sdma_rlc_registers *m;
  321. BUG_ON(!mqd);
  322. m = (struct cik_sdma_rlc_registers *)mqd;
  323. return m;
  324. }
  325. struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
  326. struct kfd_dev *dev)
  327. {
  328. struct mqd_manager *mqd;
  329. BUG_ON(!dev);
  330. BUG_ON(type >= KFD_MQD_TYPE_MAX);
  331. pr_debug("kfd: In func %s\n", __func__);
  332. mqd = kzalloc(sizeof(struct mqd_manager), GFP_KERNEL);
  333. if (!mqd)
  334. return NULL;
  335. mqd->dev = dev;
  336. switch (type) {
  337. case KFD_MQD_TYPE_CP:
  338. case KFD_MQD_TYPE_COMPUTE:
  339. mqd->init_mqd = init_mqd;
  340. mqd->uninit_mqd = uninit_mqd;
  341. mqd->load_mqd = load_mqd;
  342. mqd->update_mqd = update_mqd;
  343. mqd->destroy_mqd = destroy_mqd;
  344. mqd->is_occupied = is_occupied;
  345. break;
  346. case KFD_MQD_TYPE_HIQ:
  347. mqd->init_mqd = init_mqd_hiq;
  348. mqd->uninit_mqd = uninit_mqd;
  349. mqd->load_mqd = load_mqd;
  350. mqd->update_mqd = update_mqd_hiq;
  351. mqd->destroy_mqd = destroy_mqd;
  352. mqd->is_occupied = is_occupied;
  353. break;
  354. case KFD_MQD_TYPE_SDMA:
  355. mqd->init_mqd = init_mqd_sdma;
  356. mqd->uninit_mqd = uninit_mqd_sdma;
  357. mqd->load_mqd = load_mqd_sdma;
  358. mqd->update_mqd = update_mqd_sdma;
  359. mqd->destroy_mqd = destroy_mqd_sdma;
  360. mqd->is_occupied = is_occupied_sdma;
  361. break;
  362. default:
  363. kfree(mqd);
  364. return NULL;
  365. }
  366. return mqd;
  367. }