kfd_packet_manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2014-2022 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/mutex.h>
  26. #include "kfd_device_queue_manager.h"
  27. #include "kfd_kernel_queue.h"
  28. #include "kfd_priv.h"
  29. static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
  30. unsigned int buffer_size_bytes)
  31. {
  32. unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
  33. WARN((temp * sizeof(uint32_t)) > buffer_size_bytes,
  34. "Runlist IB overflow");
  35. *wptr = temp;
  36. }
  37. static void pm_calc_rlib_size(struct packet_manager *pm,
  38. unsigned int *rlib_size,
  39. bool *over_subscription)
  40. {
  41. unsigned int process_count, queue_count, compute_queue_count, gws_queue_count;
  42. unsigned int map_queue_size;
  43. unsigned int max_proc_per_quantum = 1;
  44. struct kfd_node *node = pm->dqm->dev;
  45. struct device *dev = node->adev->dev;
  46. process_count = pm->dqm->processes_count;
  47. queue_count = pm->dqm->active_queue_count;
  48. compute_queue_count = pm->dqm->active_cp_queue_count;
  49. gws_queue_count = pm->dqm->gws_queue_count;
  50. /* check if there is over subscription
  51. * Note: the arbitration between the number of VMIDs and
  52. * hws_max_conc_proc has been done in
  53. * kgd2kfd_device_init().
  54. */
  55. *over_subscription = false;
  56. if (node->max_proc_per_quantum > 1)
  57. max_proc_per_quantum = node->max_proc_per_quantum;
  58. if ((process_count > max_proc_per_quantum) ||
  59. compute_queue_count > get_cp_queues_num(pm->dqm) ||
  60. gws_queue_count > 1) {
  61. *over_subscription = true;
  62. dev_dbg(dev, "Over subscribed runlist\n");
  63. }
  64. map_queue_size = pm->pmf->map_queues_size;
  65. /* calculate run list ib allocation size */
  66. *rlib_size = process_count * pm->pmf->map_process_size +
  67. queue_count * map_queue_size;
  68. /*
  69. * Increase the allocation size in case we need a chained run list
  70. * when over subscription
  71. */
  72. if (*over_subscription)
  73. *rlib_size += pm->pmf->runlist_size;
  74. dev_dbg(dev, "runlist ib size %d\n", *rlib_size);
  75. }
  76. static int pm_allocate_runlist_ib(struct packet_manager *pm,
  77. unsigned int **rl_buffer,
  78. uint64_t *rl_gpu_buffer,
  79. unsigned int *rl_buffer_size,
  80. bool *is_over_subscription)
  81. {
  82. struct kfd_node *node = pm->dqm->dev;
  83. struct device *dev = node->adev->dev;
  84. int retval;
  85. if (WARN_ON(pm->allocated))
  86. return -EINVAL;
  87. pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
  88. mutex_lock(&pm->lock);
  89. retval = kfd_gtt_sa_allocate(node, *rl_buffer_size, &pm->ib_buffer_obj);
  90. if (retval) {
  91. dev_err(dev, "Failed to allocate runlist IB\n");
  92. goto out;
  93. }
  94. *(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
  95. *rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
  96. memset(*rl_buffer, 0, *rl_buffer_size);
  97. pm->allocated = true;
  98. out:
  99. mutex_unlock(&pm->lock);
  100. return retval;
  101. }
  102. static int pm_create_runlist_ib(struct packet_manager *pm,
  103. struct list_head *queues,
  104. uint64_t *rl_gpu_addr,
  105. size_t *rl_size_bytes)
  106. {
  107. unsigned int alloc_size_bytes;
  108. unsigned int *rl_buffer, rl_wptr, i;
  109. struct kfd_node *node = pm->dqm->dev;
  110. struct device *dev = node->adev->dev;
  111. int retval, processes_mapped;
  112. struct device_process_node *cur;
  113. struct qcm_process_device *qpd;
  114. struct queue *q;
  115. struct kernel_queue *kq;
  116. bool is_over_subscription;
  117. rl_wptr = retval = processes_mapped = 0;
  118. retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
  119. &alloc_size_bytes, &is_over_subscription);
  120. if (retval)
  121. return retval;
  122. *rl_size_bytes = alloc_size_bytes;
  123. pm->ib_size_bytes = alloc_size_bytes;
  124. dev_dbg(dev, "Building runlist ib process count: %d queues count %d\n",
  125. pm->dqm->processes_count, pm->dqm->active_queue_count);
  126. /* build the run list ib packet */
  127. list_for_each_entry(cur, queues, list) {
  128. qpd = cur->qpd;
  129. /* build map process packet */
  130. if (processes_mapped >= pm->dqm->processes_count) {
  131. dev_dbg(dev, "Not enough space left in runlist IB\n");
  132. pm_release_ib(pm);
  133. return -ENOMEM;
  134. }
  135. retval = pm->pmf->map_process(pm, &rl_buffer[rl_wptr], qpd);
  136. if (retval)
  137. return retval;
  138. processes_mapped++;
  139. inc_wptr(&rl_wptr, pm->pmf->map_process_size,
  140. alloc_size_bytes);
  141. list_for_each_entry(kq, &qpd->priv_queue_list, list) {
  142. if (!kq->queue->properties.is_active)
  143. continue;
  144. dev_dbg(dev,
  145. "static_queue, mapping kernel q %d, is debug status %d\n",
  146. kq->queue->queue, qpd->is_debug);
  147. retval = pm->pmf->map_queues(pm,
  148. &rl_buffer[rl_wptr],
  149. kq->queue,
  150. qpd->is_debug);
  151. if (retval)
  152. return retval;
  153. inc_wptr(&rl_wptr,
  154. pm->pmf->map_queues_size,
  155. alloc_size_bytes);
  156. }
  157. list_for_each_entry(q, &qpd->queues_list, list) {
  158. if (!q->properties.is_active)
  159. continue;
  160. dev_dbg(dev,
  161. "static_queue, mapping user queue %d, is debug status %d\n",
  162. q->queue, qpd->is_debug);
  163. retval = pm->pmf->map_queues(pm,
  164. &rl_buffer[rl_wptr],
  165. q,
  166. qpd->is_debug);
  167. if (retval)
  168. return retval;
  169. inc_wptr(&rl_wptr,
  170. pm->pmf->map_queues_size,
  171. alloc_size_bytes);
  172. }
  173. }
  174. dev_dbg(dev, "Finished map process and queues to runlist\n");
  175. if (is_over_subscription) {
  176. if (!pm->is_over_subscription)
  177. dev_warn(
  178. dev,
  179. "Runlist is getting oversubscribed. Expect reduced ROCm performance.\n");
  180. retval = pm->pmf->runlist(pm, &rl_buffer[rl_wptr],
  181. *rl_gpu_addr,
  182. alloc_size_bytes / sizeof(uint32_t),
  183. true);
  184. }
  185. pm->is_over_subscription = is_over_subscription;
  186. for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
  187. pr_debug("0x%2X ", rl_buffer[i]);
  188. pr_debug("\n");
  189. return retval;
  190. }
  191. int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
  192. {
  193. switch (dqm->dev->adev->asic_type) {
  194. case CHIP_KAVERI:
  195. case CHIP_HAWAII:
  196. /* PM4 packet structures on CIK are the same as on VI */
  197. case CHIP_CARRIZO:
  198. case CHIP_TONGA:
  199. case CHIP_FIJI:
  200. case CHIP_POLARIS10:
  201. case CHIP_POLARIS11:
  202. case CHIP_POLARIS12:
  203. case CHIP_VEGAM:
  204. pm->pmf = &kfd_vi_pm_funcs;
  205. break;
  206. default:
  207. if (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 2) ||
  208. KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3) ||
  209. KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 4))
  210. pm->pmf = &kfd_aldebaran_pm_funcs;
  211. else if (KFD_GC_VERSION(dqm->dev) >= IP_VERSION(9, 0, 1))
  212. pm->pmf = &kfd_v9_pm_funcs;
  213. else {
  214. WARN(1, "Unexpected ASIC family %u",
  215. dqm->dev->adev->asic_type);
  216. return -EINVAL;
  217. }
  218. }
  219. pm->dqm = dqm;
  220. mutex_init(&pm->lock);
  221. pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
  222. if (!pm->priv_queue) {
  223. mutex_destroy(&pm->lock);
  224. return -ENOMEM;
  225. }
  226. pm->allocated = false;
  227. return 0;
  228. }
  229. void pm_uninit(struct packet_manager *pm)
  230. {
  231. mutex_destroy(&pm->lock);
  232. kernel_queue_uninit(pm->priv_queue);
  233. pm->priv_queue = NULL;
  234. }
  235. int pm_send_set_resources(struct packet_manager *pm,
  236. struct scheduling_resources *res)
  237. {
  238. struct kfd_node *node = pm->dqm->dev;
  239. struct device *dev = node->adev->dev;
  240. uint32_t *buffer, size;
  241. int retval = 0;
  242. size = pm->pmf->set_resources_size;
  243. mutex_lock(&pm->lock);
  244. kq_acquire_packet_buffer(pm->priv_queue,
  245. size / sizeof(uint32_t),
  246. (unsigned int **)&buffer);
  247. if (!buffer) {
  248. dev_err(dev, "Failed to allocate buffer on kernel queue\n");
  249. retval = -ENOMEM;
  250. goto out;
  251. }
  252. retval = pm->pmf->set_resources(pm, buffer, res);
  253. if (!retval)
  254. retval = kq_submit_packet(pm->priv_queue);
  255. else
  256. kq_rollback_packet(pm->priv_queue);
  257. out:
  258. mutex_unlock(&pm->lock);
  259. return retval;
  260. }
  261. int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
  262. {
  263. uint64_t rl_gpu_ib_addr;
  264. uint32_t *rl_buffer;
  265. size_t rl_ib_size, packet_size_dwords;
  266. int retval;
  267. retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
  268. &rl_ib_size);
  269. if (retval)
  270. goto fail_create_runlist_ib;
  271. pr_debug("runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
  272. packet_size_dwords = pm->pmf->runlist_size / sizeof(uint32_t);
  273. mutex_lock(&pm->lock);
  274. retval = kq_acquire_packet_buffer(pm->priv_queue,
  275. packet_size_dwords, &rl_buffer);
  276. if (retval)
  277. goto fail_acquire_packet_buffer;
  278. retval = pm->pmf->runlist(pm, rl_buffer, rl_gpu_ib_addr,
  279. rl_ib_size / sizeof(uint32_t), false);
  280. if (retval)
  281. goto fail_create_runlist;
  282. retval = kq_submit_packet(pm->priv_queue);
  283. mutex_unlock(&pm->lock);
  284. return retval;
  285. fail_create_runlist:
  286. kq_rollback_packet(pm->priv_queue);
  287. fail_acquire_packet_buffer:
  288. mutex_unlock(&pm->lock);
  289. fail_create_runlist_ib:
  290. pm_release_ib(pm);
  291. return retval;
  292. }
  293. int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
  294. uint64_t fence_value)
  295. {
  296. struct kfd_node *node = pm->dqm->dev;
  297. struct device *dev = node->adev->dev;
  298. uint32_t *buffer, size;
  299. int retval = 0;
  300. if (WARN_ON(!fence_address))
  301. return -EFAULT;
  302. size = pm->pmf->query_status_size;
  303. mutex_lock(&pm->lock);
  304. kq_acquire_packet_buffer(pm->priv_queue,
  305. size / sizeof(uint32_t), (unsigned int **)&buffer);
  306. if (!buffer) {
  307. dev_err(dev, "Failed to allocate buffer on kernel queue\n");
  308. retval = -ENOMEM;
  309. goto out;
  310. }
  311. retval = pm->pmf->query_status(pm, buffer, fence_address, fence_value);
  312. if (!retval)
  313. retval = kq_submit_packet(pm->priv_queue);
  314. else
  315. kq_rollback_packet(pm->priv_queue);
  316. out:
  317. mutex_unlock(&pm->lock);
  318. return retval;
  319. }
  320. int pm_update_grace_period(struct packet_manager *pm, uint32_t grace_period)
  321. {
  322. struct kfd_node *node = pm->dqm->dev;
  323. struct device *dev = node->adev->dev;
  324. int retval = 0;
  325. uint32_t *buffer, size;
  326. size = pm->pmf->set_grace_period_size;
  327. mutex_lock(&pm->lock);
  328. if (size) {
  329. kq_acquire_packet_buffer(pm->priv_queue,
  330. size / sizeof(uint32_t),
  331. (unsigned int **)&buffer);
  332. if (!buffer) {
  333. dev_err(dev,
  334. "Failed to allocate buffer on kernel queue\n");
  335. retval = -ENOMEM;
  336. goto out;
  337. }
  338. retval = pm->pmf->set_grace_period(pm, buffer, grace_period);
  339. if (!retval)
  340. retval = kq_submit_packet(pm->priv_queue);
  341. else
  342. kq_rollback_packet(pm->priv_queue);
  343. }
  344. out:
  345. mutex_unlock(&pm->lock);
  346. return retval;
  347. }
  348. int pm_send_unmap_queue(struct packet_manager *pm,
  349. enum kfd_unmap_queues_filter filter,
  350. uint32_t filter_param, bool reset)
  351. {
  352. struct kfd_node *node = pm->dqm->dev;
  353. struct device *dev = node->adev->dev;
  354. uint32_t *buffer, size;
  355. int retval = 0;
  356. size = pm->pmf->unmap_queues_size;
  357. mutex_lock(&pm->lock);
  358. kq_acquire_packet_buffer(pm->priv_queue,
  359. size / sizeof(uint32_t), (unsigned int **)&buffer);
  360. if (!buffer) {
  361. dev_err(dev, "Failed to allocate buffer on kernel queue\n");
  362. retval = -ENOMEM;
  363. goto out;
  364. }
  365. retval = pm->pmf->unmap_queues(pm, buffer, filter, filter_param, reset);
  366. if (!retval)
  367. retval = kq_submit_packet(pm->priv_queue);
  368. else
  369. kq_rollback_packet(pm->priv_queue);
  370. out:
  371. mutex_unlock(&pm->lock);
  372. return retval;
  373. }
  374. void pm_release_ib(struct packet_manager *pm)
  375. {
  376. mutex_lock(&pm->lock);
  377. if (pm->allocated) {
  378. kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
  379. pm->allocated = false;
  380. }
  381. mutex_unlock(&pm->lock);
  382. }
  383. #if defined(CONFIG_DEBUG_FS)
  384. int pm_debugfs_runlist(struct seq_file *m, void *data)
  385. {
  386. struct packet_manager *pm = data;
  387. mutex_lock(&pm->lock);
  388. if (!pm->allocated) {
  389. seq_puts(m, " No active runlist\n");
  390. goto out;
  391. }
  392. seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4,
  393. pm->ib_buffer_obj->cpu_ptr, pm->ib_size_bytes, false);
  394. out:
  395. mutex_unlock(&pm->lock);
  396. return 0;
  397. }
  398. int pm_debugfs_hang_hws(struct packet_manager *pm)
  399. {
  400. struct kfd_node *node = pm->dqm->dev;
  401. struct device *dev = node->adev->dev;
  402. uint32_t *buffer, size;
  403. int r = 0;
  404. if (!pm->priv_queue)
  405. return -EAGAIN;
  406. size = pm->pmf->query_status_size;
  407. mutex_lock(&pm->lock);
  408. kq_acquire_packet_buffer(pm->priv_queue,
  409. size / sizeof(uint32_t), (unsigned int **)&buffer);
  410. if (!buffer) {
  411. dev_err(dev, "Failed to allocate buffer on kernel queue\n");
  412. r = -ENOMEM;
  413. goto out;
  414. }
  415. memset(buffer, 0x55, size);
  416. kq_submit_packet(pm->priv_queue);
  417. dev_info(dev, "Submitting %x %x %x %x %x %x %x to HIQ to hang the HWS.",
  418. buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
  419. buffer[5], buffer[6]);
  420. out:
  421. mutex_unlock(&pm->lock);
  422. return r;
  423. }
  424. #endif