kfd_process_queue_manager.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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/list.h>
  25. #include "kfd_device_queue_manager.h"
  26. #include "kfd_priv.h"
  27. #include "kfd_kernel_queue.h"
  28. static inline struct process_queue_node *get_queue_by_qid(
  29. struct process_queue_manager *pqm, unsigned int qid)
  30. {
  31. struct process_queue_node *pqn;
  32. list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
  33. if ((pqn->q && pqn->q->properties.queue_id == qid) ||
  34. (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
  35. return pqn;
  36. }
  37. return NULL;
  38. }
  39. static int find_available_queue_slot(struct process_queue_manager *pqm,
  40. unsigned int *qid)
  41. {
  42. unsigned long found;
  43. found = find_first_zero_bit(pqm->queue_slot_bitmap,
  44. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
  45. pr_debug("The new slot id %lu\n", found);
  46. if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
  47. pr_info("Cannot open more queues for process with pasid %d\n",
  48. pqm->process->pasid);
  49. return -ENOMEM;
  50. }
  51. set_bit(found, pqm->queue_slot_bitmap);
  52. *qid = found;
  53. return 0;
  54. }
  55. void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
  56. {
  57. struct kfd_dev *dev = pdd->dev;
  58. if (pdd->already_dequeued)
  59. return;
  60. dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
  61. pdd->already_dequeued = true;
  62. }
  63. void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
  64. {
  65. struct kfd_process_device *pdd;
  66. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  67. kfd_process_dequeue_from_device(pdd);
  68. }
  69. int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
  70. {
  71. INIT_LIST_HEAD(&pqm->queues);
  72. pqm->queue_slot_bitmap =
  73. kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  74. BITS_PER_BYTE), GFP_KERNEL);
  75. if (!pqm->queue_slot_bitmap)
  76. return -ENOMEM;
  77. pqm->process = p;
  78. return 0;
  79. }
  80. void pqm_uninit(struct process_queue_manager *pqm)
  81. {
  82. struct process_queue_node *pqn, *next;
  83. list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
  84. uninit_queue(pqn->q);
  85. list_del(&pqn->process_queue_list);
  86. kfree(pqn);
  87. }
  88. kfree(pqm->queue_slot_bitmap);
  89. pqm->queue_slot_bitmap = NULL;
  90. }
  91. static int create_cp_queue(struct process_queue_manager *pqm,
  92. struct kfd_dev *dev, struct queue **q,
  93. struct queue_properties *q_properties,
  94. struct file *f, unsigned int qid)
  95. {
  96. int retval;
  97. /* Doorbell initialized in user space*/
  98. q_properties->doorbell_ptr = NULL;
  99. /* let DQM handle it*/
  100. q_properties->vmid = 0;
  101. q_properties->queue_id = qid;
  102. retval = init_queue(q, q_properties);
  103. if (retval != 0)
  104. return retval;
  105. (*q)->device = dev;
  106. (*q)->process = pqm->process;
  107. pr_debug("PQM After init queue");
  108. return retval;
  109. }
  110. int pqm_create_queue(struct process_queue_manager *pqm,
  111. struct kfd_dev *dev,
  112. struct file *f,
  113. struct queue_properties *properties,
  114. unsigned int *qid)
  115. {
  116. int retval;
  117. struct kfd_process_device *pdd;
  118. struct queue *q;
  119. struct process_queue_node *pqn;
  120. struct kernel_queue *kq;
  121. enum kfd_queue_type type = properties->type;
  122. unsigned int max_queues = 127; /* HWS limit */
  123. q = NULL;
  124. kq = NULL;
  125. pdd = kfd_get_process_device_data(dev, pqm->process);
  126. if (!pdd) {
  127. pr_err("Process device data doesn't exist\n");
  128. return -1;
  129. }
  130. /*
  131. * for debug process, verify that it is within the static queues limit
  132. * currently limit is set to half of the total avail HQD slots
  133. * If we are just about to create DIQ, the is_debug flag is not set yet
  134. * Hence we also check the type as well
  135. */
  136. if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
  137. max_queues = dev->device_info->max_no_of_hqd/2;
  138. if (pdd->qpd.queue_count >= max_queues)
  139. return -ENOSPC;
  140. retval = find_available_queue_slot(pqm, qid);
  141. if (retval != 0)
  142. return retval;
  143. if (list_empty(&pdd->qpd.queues_list) &&
  144. list_empty(&pdd->qpd.priv_queue_list))
  145. dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
  146. pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
  147. if (!pqn) {
  148. retval = -ENOMEM;
  149. goto err_allocate_pqn;
  150. }
  151. switch (type) {
  152. case KFD_QUEUE_TYPE_SDMA:
  153. if (dev->dqm->queue_count >= get_num_sdma_queues(dev->dqm)) {
  154. pr_err("Over-subscription is not allowed for SDMA.\n");
  155. retval = -EPERM;
  156. goto err_create_queue;
  157. }
  158. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  159. if (retval != 0)
  160. goto err_create_queue;
  161. pqn->q = q;
  162. pqn->kq = NULL;
  163. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
  164. pr_debug("DQM returned %d for create_queue\n", retval);
  165. print_queue(q);
  166. break;
  167. case KFD_QUEUE_TYPE_COMPUTE:
  168. /* check if there is over subscription */
  169. if ((dev->dqm->sched_policy ==
  170. KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
  171. ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
  172. (dev->dqm->queue_count >= get_queues_num(dev->dqm)))) {
  173. pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
  174. retval = -EPERM;
  175. goto err_create_queue;
  176. }
  177. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  178. if (retval != 0)
  179. goto err_create_queue;
  180. pqn->q = q;
  181. pqn->kq = NULL;
  182. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
  183. pr_debug("DQM returned %d for create_queue\n", retval);
  184. print_queue(q);
  185. break;
  186. case KFD_QUEUE_TYPE_DIQ:
  187. kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
  188. if (!kq) {
  189. retval = -ENOMEM;
  190. goto err_create_queue;
  191. }
  192. kq->queue->properties.queue_id = *qid;
  193. pqn->kq = kq;
  194. pqn->q = NULL;
  195. retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
  196. kq, &pdd->qpd);
  197. break;
  198. default:
  199. WARN(1, "Invalid queue type %d", type);
  200. retval = -EINVAL;
  201. }
  202. if (retval != 0) {
  203. pr_err("Pasid %d DQM create queue %d failed. ret %d\n",
  204. pqm->process->pasid, type, retval);
  205. goto err_create_queue;
  206. }
  207. if (q)
  208. /* Return the doorbell offset within the doorbell page
  209. * to the caller so it can be passed up to user mode
  210. * (in bytes).
  211. */
  212. properties->doorbell_off =
  213. (q->properties.doorbell_off * sizeof(uint32_t)) &
  214. (kfd_doorbell_process_slice(dev) - 1);
  215. pr_debug("PQM After DQM create queue\n");
  216. list_add(&pqn->process_queue_list, &pqm->queues);
  217. if (q) {
  218. pr_debug("PQM done creating queue\n");
  219. print_queue_properties(&q->properties);
  220. }
  221. return retval;
  222. err_create_queue:
  223. kfree(pqn);
  224. err_allocate_pqn:
  225. /* check if queues list is empty unregister process from device */
  226. clear_bit(*qid, pqm->queue_slot_bitmap);
  227. if (list_empty(&pdd->qpd.queues_list) &&
  228. list_empty(&pdd->qpd.priv_queue_list))
  229. dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
  230. return retval;
  231. }
  232. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
  233. {
  234. struct process_queue_node *pqn;
  235. struct kfd_process_device *pdd;
  236. struct device_queue_manager *dqm;
  237. struct kfd_dev *dev;
  238. int retval;
  239. dqm = NULL;
  240. retval = 0;
  241. pqn = get_queue_by_qid(pqm, qid);
  242. if (!pqn) {
  243. pr_err("Queue id does not match any known queue\n");
  244. return -EINVAL;
  245. }
  246. dev = NULL;
  247. if (pqn->kq)
  248. dev = pqn->kq->dev;
  249. if (pqn->q)
  250. dev = pqn->q->device;
  251. if (WARN_ON(!dev))
  252. return -ENODEV;
  253. pdd = kfd_get_process_device_data(dev, pqm->process);
  254. if (!pdd) {
  255. pr_err("Process device data doesn't exist\n");
  256. return -1;
  257. }
  258. if (pqn->kq) {
  259. /* destroy kernel queue (DIQ) */
  260. dqm = pqn->kq->dev->dqm;
  261. dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
  262. kernel_queue_uninit(pqn->kq);
  263. }
  264. if (pqn->q) {
  265. dqm = pqn->q->device->dqm;
  266. retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
  267. if (retval) {
  268. pr_err("Pasid %d destroy queue %d failed, ret %d\n",
  269. pqm->process->pasid,
  270. pqn->q->properties.queue_id, retval);
  271. if (retval != -ETIME)
  272. goto err_destroy_queue;
  273. }
  274. kfree(pqn->q->properties.cu_mask);
  275. pqn->q->properties.cu_mask = NULL;
  276. uninit_queue(pqn->q);
  277. }
  278. list_del(&pqn->process_queue_list);
  279. kfree(pqn);
  280. clear_bit(qid, pqm->queue_slot_bitmap);
  281. if (list_empty(&pdd->qpd.queues_list) &&
  282. list_empty(&pdd->qpd.priv_queue_list))
  283. dqm->ops.unregister_process(dqm, &pdd->qpd);
  284. err_destroy_queue:
  285. return retval;
  286. }
  287. int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
  288. struct queue_properties *p)
  289. {
  290. int retval;
  291. struct process_queue_node *pqn;
  292. pqn = get_queue_by_qid(pqm, qid);
  293. if (!pqn) {
  294. pr_debug("No queue %d exists for update operation\n", qid);
  295. return -EFAULT;
  296. }
  297. pqn->q->properties.queue_address = p->queue_address;
  298. pqn->q->properties.queue_size = p->queue_size;
  299. pqn->q->properties.queue_percent = p->queue_percent;
  300. pqn->q->properties.priority = p->priority;
  301. retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
  302. pqn->q);
  303. if (retval != 0)
  304. return retval;
  305. return 0;
  306. }
  307. int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid,
  308. struct queue_properties *p)
  309. {
  310. int retval;
  311. struct process_queue_node *pqn;
  312. pqn = get_queue_by_qid(pqm, qid);
  313. if (!pqn) {
  314. pr_debug("No queue %d exists for update operation\n", qid);
  315. return -EFAULT;
  316. }
  317. /* Free the old CU mask memory if it is already allocated, then
  318. * allocate memory for the new CU mask.
  319. */
  320. kfree(pqn->q->properties.cu_mask);
  321. pqn->q->properties.cu_mask_count = p->cu_mask_count;
  322. pqn->q->properties.cu_mask = p->cu_mask;
  323. retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
  324. pqn->q);
  325. if (retval != 0)
  326. return retval;
  327. return 0;
  328. }
  329. struct kernel_queue *pqm_get_kernel_queue(
  330. struct process_queue_manager *pqm,
  331. unsigned int qid)
  332. {
  333. struct process_queue_node *pqn;
  334. pqn = get_queue_by_qid(pqm, qid);
  335. if (pqn && pqn->kq)
  336. return pqn->kq;
  337. return NULL;
  338. }
  339. int pqm_get_wave_state(struct process_queue_manager *pqm,
  340. unsigned int qid,
  341. void __user *ctl_stack,
  342. u32 *ctl_stack_used_size,
  343. u32 *save_area_used_size)
  344. {
  345. struct process_queue_node *pqn;
  346. pqn = get_queue_by_qid(pqm, qid);
  347. if (!pqn) {
  348. pr_debug("amdkfd: No queue %d exists for operation\n",
  349. qid);
  350. return -EFAULT;
  351. }
  352. return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
  353. pqn->q,
  354. ctl_stack,
  355. ctl_stack_used_size,
  356. save_area_used_size);
  357. }
  358. #if defined(CONFIG_DEBUG_FS)
  359. int pqm_debugfs_mqds(struct seq_file *m, void *data)
  360. {
  361. struct process_queue_manager *pqm = data;
  362. struct process_queue_node *pqn;
  363. struct queue *q;
  364. enum KFD_MQD_TYPE mqd_type;
  365. struct mqd_manager *mqd_mgr;
  366. int r = 0;
  367. list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
  368. if (pqn->q) {
  369. q = pqn->q;
  370. switch (q->properties.type) {
  371. case KFD_QUEUE_TYPE_SDMA:
  372. seq_printf(m, " SDMA queue on device %x\n",
  373. q->device->id);
  374. mqd_type = KFD_MQD_TYPE_SDMA;
  375. break;
  376. case KFD_QUEUE_TYPE_COMPUTE:
  377. seq_printf(m, " Compute queue on device %x\n",
  378. q->device->id);
  379. mqd_type = KFD_MQD_TYPE_CP;
  380. break;
  381. default:
  382. seq_printf(m,
  383. " Bad user queue type %d on device %x\n",
  384. q->properties.type, q->device->id);
  385. continue;
  386. }
  387. mqd_mgr = q->device->dqm->ops.get_mqd_manager(
  388. q->device->dqm, mqd_type);
  389. } else if (pqn->kq) {
  390. q = pqn->kq->queue;
  391. mqd_mgr = pqn->kq->mqd_mgr;
  392. switch (q->properties.type) {
  393. case KFD_QUEUE_TYPE_DIQ:
  394. seq_printf(m, " DIQ on device %x\n",
  395. pqn->kq->dev->id);
  396. mqd_type = KFD_MQD_TYPE_HIQ;
  397. break;
  398. default:
  399. seq_printf(m,
  400. " Bad kernel queue type %d on device %x\n",
  401. q->properties.type,
  402. pqn->kq->dev->id);
  403. continue;
  404. }
  405. } else {
  406. seq_printf(m,
  407. " Weird: Queue node with neither kernel nor user queue\n");
  408. continue;
  409. }
  410. r = mqd_mgr->debugfs_show_mqd(m, q->mqd);
  411. if (r != 0)
  412. break;
  413. }
  414. return r;
  415. }
  416. #endif