kfd_process_queue_manager.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 >=
  154. CIK_SDMA_QUEUES_PER_ENGINE * CIK_SDMA_ENGINE_NUM) {
  155. pr_err("Over-subscription is not allowed for SDMA.\n");
  156. retval = -EPERM;
  157. goto err_create_queue;
  158. }
  159. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  160. if (retval != 0)
  161. goto err_create_queue;
  162. pqn->q = q;
  163. pqn->kq = NULL;
  164. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
  165. pr_debug("DQM returned %d for create_queue\n", retval);
  166. print_queue(q);
  167. break;
  168. case KFD_QUEUE_TYPE_COMPUTE:
  169. /* check if there is over subscription */
  170. if ((dev->dqm->sched_policy ==
  171. KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
  172. ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
  173. (dev->dqm->queue_count >= get_queues_num(dev->dqm)))) {
  174. pr_err("Over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
  175. retval = -EPERM;
  176. goto err_create_queue;
  177. }
  178. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  179. if (retval != 0)
  180. goto err_create_queue;
  181. pqn->q = q;
  182. pqn->kq = NULL;
  183. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
  184. pr_debug("DQM returned %d for create_queue\n", retval);
  185. print_queue(q);
  186. break;
  187. case KFD_QUEUE_TYPE_DIQ:
  188. kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
  189. if (!kq) {
  190. retval = -ENOMEM;
  191. goto err_create_queue;
  192. }
  193. kq->queue->properties.queue_id = *qid;
  194. pqn->kq = kq;
  195. pqn->q = NULL;
  196. retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
  197. kq, &pdd->qpd);
  198. break;
  199. default:
  200. WARN(1, "Invalid queue type %d", type);
  201. retval = -EINVAL;
  202. }
  203. if (retval != 0) {
  204. pr_err("Pasid %d DQM create queue %d failed. ret %d\n",
  205. pqm->process->pasid, type, retval);
  206. goto err_create_queue;
  207. }
  208. if (q)
  209. /* Return the doorbell offset within the doorbell page
  210. * to the caller so it can be passed up to user mode
  211. * (in bytes).
  212. */
  213. properties->doorbell_off =
  214. (q->properties.doorbell_off * sizeof(uint32_t)) &
  215. (kfd_doorbell_process_slice(dev) - 1);
  216. pr_debug("PQM After DQM create queue\n");
  217. list_add(&pqn->process_queue_list, &pqm->queues);
  218. if (q) {
  219. pr_debug("PQM done creating queue\n");
  220. print_queue_properties(&q->properties);
  221. }
  222. return retval;
  223. err_create_queue:
  224. kfree(pqn);
  225. err_allocate_pqn:
  226. /* check if queues list is empty unregister process from device */
  227. clear_bit(*qid, pqm->queue_slot_bitmap);
  228. if (list_empty(&pdd->qpd.queues_list) &&
  229. list_empty(&pdd->qpd.priv_queue_list))
  230. dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
  231. return retval;
  232. }
  233. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
  234. {
  235. struct process_queue_node *pqn;
  236. struct kfd_process_device *pdd;
  237. struct device_queue_manager *dqm;
  238. struct kfd_dev *dev;
  239. int retval;
  240. dqm = NULL;
  241. retval = 0;
  242. pqn = get_queue_by_qid(pqm, qid);
  243. if (!pqn) {
  244. pr_err("Queue id does not match any known queue\n");
  245. return -EINVAL;
  246. }
  247. dev = NULL;
  248. if (pqn->kq)
  249. dev = pqn->kq->dev;
  250. if (pqn->q)
  251. dev = pqn->q->device;
  252. if (WARN_ON(!dev))
  253. return -ENODEV;
  254. pdd = kfd_get_process_device_data(dev, pqm->process);
  255. if (!pdd) {
  256. pr_err("Process device data doesn't exist\n");
  257. return -1;
  258. }
  259. if (pqn->kq) {
  260. /* destroy kernel queue (DIQ) */
  261. dqm = pqn->kq->dev->dqm;
  262. dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
  263. kernel_queue_uninit(pqn->kq);
  264. }
  265. if (pqn->q) {
  266. dqm = pqn->q->device->dqm;
  267. retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
  268. if (retval) {
  269. pr_err("Pasid %d destroy queue %d failed, ret %d\n",
  270. pqm->process->pasid,
  271. pqn->q->properties.queue_id, retval);
  272. if (retval != -ETIME)
  273. goto err_destroy_queue;
  274. }
  275. uninit_queue(pqn->q);
  276. }
  277. list_del(&pqn->process_queue_list);
  278. kfree(pqn);
  279. clear_bit(qid, pqm->queue_slot_bitmap);
  280. if (list_empty(&pdd->qpd.queues_list) &&
  281. list_empty(&pdd->qpd.priv_queue_list))
  282. dqm->ops.unregister_process(dqm, &pdd->qpd);
  283. err_destroy_queue:
  284. return retval;
  285. }
  286. int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
  287. struct queue_properties *p)
  288. {
  289. int retval;
  290. struct process_queue_node *pqn;
  291. pqn = get_queue_by_qid(pqm, qid);
  292. if (!pqn) {
  293. pr_debug("No queue %d exists for update operation\n", qid);
  294. return -EFAULT;
  295. }
  296. pqn->q->properties.queue_address = p->queue_address;
  297. pqn->q->properties.queue_size = p->queue_size;
  298. pqn->q->properties.queue_percent = p->queue_percent;
  299. pqn->q->properties.priority = p->priority;
  300. retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
  301. pqn->q);
  302. if (retval != 0)
  303. return retval;
  304. return 0;
  305. }
  306. struct kernel_queue *pqm_get_kernel_queue(
  307. struct process_queue_manager *pqm,
  308. unsigned int qid)
  309. {
  310. struct process_queue_node *pqn;
  311. pqn = get_queue_by_qid(pqm, qid);
  312. if (pqn && pqn->kq)
  313. return pqn->kq;
  314. return NULL;
  315. }
  316. #if defined(CONFIG_DEBUG_FS)
  317. int pqm_debugfs_mqds(struct seq_file *m, void *data)
  318. {
  319. struct process_queue_manager *pqm = data;
  320. struct process_queue_node *pqn;
  321. struct queue *q;
  322. enum KFD_MQD_TYPE mqd_type;
  323. struct mqd_manager *mqd_manager;
  324. int r = 0;
  325. list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
  326. if (pqn->q) {
  327. q = pqn->q;
  328. switch (q->properties.type) {
  329. case KFD_QUEUE_TYPE_SDMA:
  330. seq_printf(m, " SDMA queue on device %x\n",
  331. q->device->id);
  332. mqd_type = KFD_MQD_TYPE_SDMA;
  333. break;
  334. case KFD_QUEUE_TYPE_COMPUTE:
  335. seq_printf(m, " Compute queue on device %x\n",
  336. q->device->id);
  337. mqd_type = KFD_MQD_TYPE_CP;
  338. break;
  339. default:
  340. seq_printf(m,
  341. " Bad user queue type %d on device %x\n",
  342. q->properties.type, q->device->id);
  343. continue;
  344. }
  345. mqd_manager = q->device->dqm->ops.get_mqd_manager(
  346. q->device->dqm, mqd_type);
  347. } else if (pqn->kq) {
  348. q = pqn->kq->queue;
  349. mqd_manager = pqn->kq->mqd;
  350. switch (q->properties.type) {
  351. case KFD_QUEUE_TYPE_DIQ:
  352. seq_printf(m, " DIQ on device %x\n",
  353. pqn->kq->dev->id);
  354. mqd_type = KFD_MQD_TYPE_HIQ;
  355. break;
  356. default:
  357. seq_printf(m,
  358. " Bad kernel queue type %d on device %x\n",
  359. q->properties.type,
  360. pqn->kq->dev->id);
  361. continue;
  362. }
  363. } else {
  364. seq_printf(m,
  365. " Weird: Queue node with neither kernel nor user queue\n");
  366. continue;
  367. }
  368. r = mqd_manager->debugfs_show_mqd(m, q->mqd);
  369. if (r != 0)
  370. break;
  371. }
  372. return r;
  373. }
  374. #endif