kfd_packet_manager.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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/slab.h>
  24. #include <linux/mutex.h>
  25. #include "kfd_device_queue_manager.h"
  26. #include "kfd_kernel_queue.h"
  27. #include "kfd_priv.h"
  28. #include "kfd_pm4_headers.h"
  29. #include "kfd_pm4_opcodes.h"
  30. static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
  31. unsigned int buffer_size_bytes)
  32. {
  33. unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
  34. BUG_ON((temp * sizeof(uint32_t)) > buffer_size_bytes);
  35. *wptr = temp;
  36. }
  37. static unsigned int build_pm4_header(unsigned int opcode, size_t packet_size)
  38. {
  39. union PM4_MES_TYPE_3_HEADER header;
  40. header.u32all = 0;
  41. header.opcode = opcode;
  42. header.count = packet_size/sizeof(uint32_t) - 2;
  43. header.type = PM4_TYPE_3;
  44. return header.u32all;
  45. }
  46. static void pm_calc_rlib_size(struct packet_manager *pm,
  47. unsigned int *rlib_size,
  48. bool *over_subscription)
  49. {
  50. unsigned int process_count, queue_count;
  51. BUG_ON(!pm || !rlib_size || !over_subscription);
  52. process_count = pm->dqm->processes_count;
  53. queue_count = pm->dqm->queue_count;
  54. /* check if there is over subscription*/
  55. *over_subscription = false;
  56. if ((process_count > 1) ||
  57. queue_count > PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE) {
  58. *over_subscription = true;
  59. pr_debug("kfd: over subscribed runlist\n");
  60. }
  61. /* calculate run list ib allocation size */
  62. *rlib_size = process_count * sizeof(struct pm4_map_process) +
  63. queue_count * sizeof(struct pm4_map_queues);
  64. /*
  65. * Increase the allocation size in case we need a chained run list
  66. * when over subscription
  67. */
  68. if (*over_subscription)
  69. *rlib_size += sizeof(struct pm4_runlist);
  70. pr_debug("kfd: runlist ib size %d\n", *rlib_size);
  71. }
  72. static int pm_allocate_runlist_ib(struct packet_manager *pm,
  73. unsigned int **rl_buffer,
  74. uint64_t *rl_gpu_buffer,
  75. unsigned int *rl_buffer_size,
  76. bool *is_over_subscription)
  77. {
  78. int retval;
  79. BUG_ON(!pm);
  80. BUG_ON(pm->allocated == true);
  81. BUG_ON(is_over_subscription == NULL);
  82. pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
  83. retval = kfd2kgd->allocate_mem(pm->dqm->dev->kgd,
  84. *rl_buffer_size,
  85. PAGE_SIZE,
  86. KFD_MEMPOOL_SYSTEM_WRITECOMBINE,
  87. (struct kgd_mem **) &pm->ib_buffer_obj);
  88. if (retval != 0) {
  89. pr_err("kfd: failed to allocate runlist IB\n");
  90. return retval;
  91. }
  92. *(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
  93. *rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
  94. memset(*rl_buffer, 0, *rl_buffer_size);
  95. pm->allocated = true;
  96. return retval;
  97. }
  98. static int pm_create_runlist(struct packet_manager *pm, uint32_t *buffer,
  99. uint64_t ib, size_t ib_size_in_dwords, bool chain)
  100. {
  101. struct pm4_runlist *packet;
  102. BUG_ON(!pm || !buffer || !ib);
  103. packet = (struct pm4_runlist *)buffer;
  104. memset(buffer, 0, sizeof(struct pm4_runlist));
  105. packet->header.u32all = build_pm4_header(IT_RUN_LIST,
  106. sizeof(struct pm4_runlist));
  107. packet->bitfields4.ib_size = ib_size_in_dwords;
  108. packet->bitfields4.chain = chain ? 1 : 0;
  109. packet->bitfields4.offload_polling = 0;
  110. packet->bitfields4.valid = 1;
  111. packet->ordinal2 = lower_32_bits(ib);
  112. packet->bitfields3.ib_base_hi = upper_32_bits(ib);
  113. return 0;
  114. }
  115. static int pm_create_map_process(struct packet_manager *pm, uint32_t *buffer,
  116. struct qcm_process_device *qpd)
  117. {
  118. struct pm4_map_process *packet;
  119. struct queue *cur;
  120. uint32_t num_queues;
  121. BUG_ON(!pm || !buffer || !qpd);
  122. packet = (struct pm4_map_process *)buffer;
  123. pr_debug("kfd: In func %s\n", __func__);
  124. memset(buffer, 0, sizeof(struct pm4_map_process));
  125. packet->header.u32all = build_pm4_header(IT_MAP_PROCESS,
  126. sizeof(struct pm4_map_process));
  127. packet->bitfields2.diq_enable = (qpd->is_debug) ? 1 : 0;
  128. packet->bitfields2.process_quantum = 1;
  129. packet->bitfields2.pasid = qpd->pqm->process->pasid;
  130. packet->bitfields3.page_table_base = qpd->page_table_base;
  131. packet->bitfields10.gds_size = qpd->gds_size;
  132. packet->bitfields10.num_gws = qpd->num_gws;
  133. packet->bitfields10.num_oac = qpd->num_oac;
  134. num_queues = 0;
  135. list_for_each_entry(cur, &qpd->queues_list, list)
  136. num_queues++;
  137. packet->bitfields10.num_queues = num_queues;
  138. packet->sh_mem_config = qpd->sh_mem_config;
  139. packet->sh_mem_bases = qpd->sh_mem_bases;
  140. packet->sh_mem_ape1_base = qpd->sh_mem_ape1_base;
  141. packet->sh_mem_ape1_limit = qpd->sh_mem_ape1_limit;
  142. packet->gds_addr_lo = lower_32_bits(qpd->gds_context_area);
  143. packet->gds_addr_hi = upper_32_bits(qpd->gds_context_area);
  144. return 0;
  145. }
  146. static int pm_create_map_queue(struct packet_manager *pm, uint32_t *buffer,
  147. struct queue *q)
  148. {
  149. struct pm4_map_queues *packet;
  150. BUG_ON(!pm || !buffer || !q);
  151. pr_debug("kfd: In func %s\n", __func__);
  152. packet = (struct pm4_map_queues *)buffer;
  153. memset(buffer, 0, sizeof(struct pm4_map_queues));
  154. packet->header.u32all = build_pm4_header(IT_MAP_QUEUES,
  155. sizeof(struct pm4_map_queues));
  156. packet->bitfields2.alloc_format =
  157. alloc_format__mes_map_queues__one_per_pipe;
  158. packet->bitfields2.num_queues = 1;
  159. packet->bitfields2.queue_sel =
  160. queue_sel__mes_map_queues__map_to_hws_determined_queue_slots;
  161. packet->bitfields2.vidmem = (q->properties.is_interop) ?
  162. vidmem__mes_map_queues__uses_video_memory :
  163. vidmem__mes_map_queues__uses_no_video_memory;
  164. switch (q->properties.type) {
  165. case KFD_QUEUE_TYPE_COMPUTE:
  166. case KFD_QUEUE_TYPE_DIQ:
  167. packet->bitfields2.engine_sel =
  168. engine_sel__mes_map_queues__compute;
  169. break;
  170. case KFD_QUEUE_TYPE_SDMA:
  171. packet->bitfields2.engine_sel =
  172. engine_sel__mes_map_queues__sdma0;
  173. break;
  174. default:
  175. BUG();
  176. break;
  177. }
  178. packet->mes_map_queues_ordinals[0].bitfields3.doorbell_offset =
  179. q->properties.doorbell_off;
  180. packet->mes_map_queues_ordinals[0].mqd_addr_lo =
  181. lower_32_bits(q->gart_mqd_addr);
  182. packet->mes_map_queues_ordinals[0].mqd_addr_hi =
  183. upper_32_bits(q->gart_mqd_addr);
  184. packet->mes_map_queues_ordinals[0].wptr_addr_lo =
  185. lower_32_bits((uint64_t)q->properties.write_ptr);
  186. packet->mes_map_queues_ordinals[0].wptr_addr_hi =
  187. upper_32_bits((uint64_t)q->properties.write_ptr);
  188. return 0;
  189. }
  190. static int pm_create_runlist_ib(struct packet_manager *pm,
  191. struct list_head *queues,
  192. uint64_t *rl_gpu_addr,
  193. size_t *rl_size_bytes)
  194. {
  195. unsigned int alloc_size_bytes;
  196. unsigned int *rl_buffer, rl_wptr, i;
  197. int retval, proccesses_mapped;
  198. struct device_process_node *cur;
  199. struct qcm_process_device *qpd;
  200. struct queue *q;
  201. struct kernel_queue *kq;
  202. bool is_over_subscription;
  203. BUG_ON(!pm || !queues || !rl_size_bytes || !rl_gpu_addr);
  204. rl_wptr = retval = proccesses_mapped = 0;
  205. retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
  206. &alloc_size_bytes, &is_over_subscription);
  207. if (retval != 0)
  208. return retval;
  209. *rl_size_bytes = alloc_size_bytes;
  210. pr_debug("kfd: In func %s\n", __func__);
  211. pr_debug("kfd: building runlist ib process count: %d queues count %d\n",
  212. pm->dqm->processes_count, pm->dqm->queue_count);
  213. /* build the run list ib packet */
  214. list_for_each_entry(cur, queues, list) {
  215. qpd = cur->qpd;
  216. /* build map process packet */
  217. if (proccesses_mapped >= pm->dqm->processes_count) {
  218. pr_debug("kfd: not enough space left in runlist IB\n");
  219. pm_release_ib(pm);
  220. return -ENOMEM;
  221. }
  222. retval = pm_create_map_process(pm, &rl_buffer[rl_wptr], qpd);
  223. if (retval != 0)
  224. return retval;
  225. proccesses_mapped++;
  226. inc_wptr(&rl_wptr, sizeof(struct pm4_map_process),
  227. alloc_size_bytes);
  228. list_for_each_entry(kq, &qpd->priv_queue_list, list) {
  229. if (kq->queue->properties.is_active != true)
  230. continue;
  231. retval = pm_create_map_queue(pm, &rl_buffer[rl_wptr],
  232. kq->queue);
  233. if (retval != 0)
  234. return retval;
  235. inc_wptr(&rl_wptr, sizeof(struct pm4_map_queues),
  236. alloc_size_bytes);
  237. }
  238. list_for_each_entry(q, &qpd->queues_list, list) {
  239. if (q->properties.is_active != true)
  240. continue;
  241. retval = pm_create_map_queue(pm,
  242. &rl_buffer[rl_wptr], q);
  243. if (retval != 0)
  244. return retval;
  245. inc_wptr(&rl_wptr, sizeof(struct pm4_map_queues),
  246. alloc_size_bytes);
  247. }
  248. }
  249. pr_debug("kfd: finished map process and queues to runlist\n");
  250. if (is_over_subscription)
  251. pm_create_runlist(pm, &rl_buffer[rl_wptr], *rl_gpu_addr,
  252. alloc_size_bytes / sizeof(uint32_t), true);
  253. for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
  254. pr_debug("0x%2X ", rl_buffer[i]);
  255. pr_debug("\n");
  256. return 0;
  257. }
  258. int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
  259. {
  260. BUG_ON(!dqm);
  261. pm->dqm = dqm;
  262. mutex_init(&pm->lock);
  263. pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
  264. if (pm->priv_queue == NULL) {
  265. mutex_destroy(&pm->lock);
  266. return -ENOMEM;
  267. }
  268. pm->allocated = false;
  269. return 0;
  270. }
  271. void pm_uninit(struct packet_manager *pm)
  272. {
  273. BUG_ON(!pm);
  274. mutex_destroy(&pm->lock);
  275. kernel_queue_uninit(pm->priv_queue);
  276. }
  277. int pm_send_set_resources(struct packet_manager *pm,
  278. struct scheduling_resources *res)
  279. {
  280. struct pm4_set_resources *packet;
  281. BUG_ON(!pm || !res);
  282. pr_debug("kfd: In func %s\n", __func__);
  283. mutex_lock(&pm->lock);
  284. pm->priv_queue->acquire_packet_buffer(pm->priv_queue,
  285. sizeof(*packet) / sizeof(uint32_t),
  286. (unsigned int **)&packet);
  287. if (packet == NULL) {
  288. mutex_unlock(&pm->lock);
  289. pr_err("kfd: failed to allocate buffer on kernel queue\n");
  290. return -ENOMEM;
  291. }
  292. memset(packet, 0, sizeof(struct pm4_set_resources));
  293. packet->header.u32all = build_pm4_header(IT_SET_RESOURCES,
  294. sizeof(struct pm4_set_resources));
  295. packet->bitfields2.queue_type =
  296. queue_type__mes_set_resources__hsa_interface_queue_hiq;
  297. packet->bitfields2.vmid_mask = res->vmid_mask;
  298. packet->bitfields2.unmap_latency = KFD_UNMAP_LATENCY;
  299. packet->bitfields7.oac_mask = res->oac_mask;
  300. packet->bitfields8.gds_heap_base = res->gds_heap_base;
  301. packet->bitfields8.gds_heap_size = res->gds_heap_size;
  302. packet->gws_mask_lo = lower_32_bits(res->gws_mask);
  303. packet->gws_mask_hi = upper_32_bits(res->gws_mask);
  304. packet->queue_mask_lo = lower_32_bits(res->queue_mask);
  305. packet->queue_mask_hi = upper_32_bits(res->queue_mask);
  306. pm->priv_queue->submit_packet(pm->priv_queue);
  307. pm->priv_queue->sync_with_hw(pm->priv_queue, KFD_HIQ_TIMEOUT);
  308. mutex_unlock(&pm->lock);
  309. return 0;
  310. }
  311. int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
  312. {
  313. uint64_t rl_gpu_ib_addr;
  314. uint32_t *rl_buffer;
  315. size_t rl_ib_size, packet_size_dwords;
  316. int retval;
  317. BUG_ON(!pm || !dqm_queues);
  318. retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
  319. &rl_ib_size);
  320. if (retval != 0)
  321. goto fail_create_runlist_ib;
  322. pr_debug("kfd: runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
  323. packet_size_dwords = sizeof(struct pm4_runlist) / sizeof(uint32_t);
  324. mutex_lock(&pm->lock);
  325. retval = pm->priv_queue->acquire_packet_buffer(pm->priv_queue,
  326. packet_size_dwords, &rl_buffer);
  327. if (retval != 0)
  328. goto fail_acquire_packet_buffer;
  329. retval = pm_create_runlist(pm, rl_buffer, rl_gpu_ib_addr,
  330. rl_ib_size / sizeof(uint32_t), false);
  331. if (retval != 0)
  332. goto fail_create_runlist;
  333. pm->priv_queue->submit_packet(pm->priv_queue);
  334. pm->priv_queue->sync_with_hw(pm->priv_queue, KFD_HIQ_TIMEOUT);
  335. mutex_unlock(&pm->lock);
  336. return retval;
  337. fail_create_runlist:
  338. pm->priv_queue->rollback_packet(pm->priv_queue);
  339. fail_acquire_packet_buffer:
  340. mutex_unlock(&pm->lock);
  341. fail_create_runlist_ib:
  342. if (pm->allocated == true)
  343. pm_release_ib(pm);
  344. return retval;
  345. }
  346. int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
  347. uint32_t fence_value)
  348. {
  349. int retval;
  350. struct pm4_query_status *packet;
  351. BUG_ON(!pm || !fence_address);
  352. mutex_lock(&pm->lock);
  353. retval = pm->priv_queue->acquire_packet_buffer(
  354. pm->priv_queue,
  355. sizeof(struct pm4_query_status) / sizeof(uint32_t),
  356. (unsigned int **)&packet);
  357. if (retval != 0)
  358. goto fail_acquire_packet_buffer;
  359. packet->header.u32all = build_pm4_header(IT_QUERY_STATUS,
  360. sizeof(struct pm4_query_status));
  361. packet->bitfields2.context_id = 0;
  362. packet->bitfields2.interrupt_sel =
  363. interrupt_sel__mes_query_status__completion_status;
  364. packet->bitfields2.command =
  365. command__mes_query_status__fence_only_after_write_ack;
  366. packet->addr_hi = upper_32_bits((uint64_t)fence_address);
  367. packet->addr_lo = lower_32_bits((uint64_t)fence_address);
  368. packet->data_hi = upper_32_bits((uint64_t)fence_value);
  369. packet->data_lo = lower_32_bits((uint64_t)fence_value);
  370. pm->priv_queue->submit_packet(pm->priv_queue);
  371. pm->priv_queue->sync_with_hw(pm->priv_queue, KFD_HIQ_TIMEOUT);
  372. mutex_unlock(&pm->lock);
  373. return 0;
  374. fail_acquire_packet_buffer:
  375. mutex_unlock(&pm->lock);
  376. return retval;
  377. }
  378. int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
  379. enum kfd_preempt_type_filter mode,
  380. uint32_t filter_param, bool reset,
  381. unsigned int sdma_engine)
  382. {
  383. int retval;
  384. uint32_t *buffer;
  385. struct pm4_unmap_queues *packet;
  386. BUG_ON(!pm);
  387. mutex_lock(&pm->lock);
  388. retval = pm->priv_queue->acquire_packet_buffer(
  389. pm->priv_queue,
  390. sizeof(struct pm4_unmap_queues) / sizeof(uint32_t),
  391. &buffer);
  392. if (retval != 0)
  393. goto err_acquire_packet_buffer;
  394. packet = (struct pm4_unmap_queues *)buffer;
  395. memset(buffer, 0, sizeof(struct pm4_unmap_queues));
  396. packet->header.u32all = build_pm4_header(IT_UNMAP_QUEUES,
  397. sizeof(struct pm4_unmap_queues));
  398. switch (type) {
  399. case KFD_QUEUE_TYPE_COMPUTE:
  400. case KFD_QUEUE_TYPE_DIQ:
  401. packet->bitfields2.engine_sel =
  402. engine_sel__mes_unmap_queues__compute;
  403. break;
  404. case KFD_QUEUE_TYPE_SDMA:
  405. packet->bitfields2.engine_sel =
  406. engine_sel__mes_unmap_queues__sdma0 + sdma_engine;
  407. break;
  408. default:
  409. BUG();
  410. break;
  411. }
  412. if (reset)
  413. packet->bitfields2.action =
  414. action__mes_unmap_queues__reset_queues;
  415. else
  416. packet->bitfields2.action =
  417. action__mes_unmap_queues__preempt_queues;
  418. switch (mode) {
  419. case KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE:
  420. packet->bitfields2.queue_sel =
  421. queue_sel__mes_unmap_queues__perform_request_on_specified_queues;
  422. packet->bitfields2.num_queues = 1;
  423. packet->bitfields3b.doorbell_offset0 = filter_param;
  424. break;
  425. case KFD_PREEMPT_TYPE_FILTER_BY_PASID:
  426. packet->bitfields2.queue_sel =
  427. queue_sel__mes_unmap_queues__perform_request_on_pasid_queues;
  428. packet->bitfields3a.pasid = filter_param;
  429. break;
  430. case KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES:
  431. packet->bitfields2.queue_sel =
  432. queue_sel__mes_unmap_queues__perform_request_on_all_active_queues;
  433. break;
  434. default:
  435. BUG();
  436. break;
  437. };
  438. pm->priv_queue->submit_packet(pm->priv_queue);
  439. pm->priv_queue->sync_with_hw(pm->priv_queue, KFD_HIQ_TIMEOUT);
  440. mutex_unlock(&pm->lock);
  441. return 0;
  442. err_acquire_packet_buffer:
  443. mutex_unlock(&pm->lock);
  444. return retval;
  445. }
  446. void pm_release_ib(struct packet_manager *pm)
  447. {
  448. BUG_ON(!pm);
  449. mutex_lock(&pm->lock);
  450. if (pm->allocated) {
  451. kfd2kgd->free_mem(pm->dqm->dev->kgd,
  452. (struct kgd_mem *) pm->ib_buffer_obj);
  453. pm->allocated = false;
  454. }
  455. mutex_unlock(&pm->lock);
  456. }