kfd_packet_manager.c 16 KB

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