kfd_process_queue_manager.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. q_properties->doorbell_off =
  100. kfd_queue_id_to_doorbell(dev, pqm->process, qid);
  101. /* let DQM handle it*/
  102. q_properties->vmid = 0;
  103. q_properties->queue_id = qid;
  104. retval = init_queue(q, q_properties);
  105. if (retval != 0)
  106. return retval;
  107. (*q)->device = dev;
  108. (*q)->process = pqm->process;
  109. pr_debug("PQM After init queue");
  110. return retval;
  111. }
  112. int pqm_create_queue(struct process_queue_manager *pqm,
  113. struct kfd_dev *dev,
  114. struct file *f,
  115. struct queue_properties *properties,
  116. unsigned int *qid)
  117. {
  118. int retval;
  119. struct kfd_process_device *pdd;
  120. struct queue *q;
  121. struct process_queue_node *pqn;
  122. struct kernel_queue *kq;
  123. enum kfd_queue_type type = properties->type;
  124. unsigned int max_queues = 127; /* HWS limit */
  125. q = NULL;
  126. kq = NULL;
  127. pdd = kfd_get_process_device_data(dev, pqm->process);
  128. if (!pdd) {
  129. pr_err("Process device data doesn't exist\n");
  130. return -1;
  131. }
  132. /*
  133. * for debug process, verify that it is within the static queues limit
  134. * currently limit is set to half of the total avail HQD slots
  135. * If we are just about to create DIQ, the is_debug flag is not set yet
  136. * Hence we also check the type as well
  137. */
  138. if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
  139. max_queues = dev->device_info->max_no_of_hqd/2;
  140. if (pdd->qpd.queue_count >= max_queues)
  141. return -ENOSPC;
  142. retval = find_available_queue_slot(pqm, qid);
  143. if (retval != 0)
  144. return retval;
  145. if (list_empty(&pdd->qpd.queues_list) &&
  146. list_empty(&pdd->qpd.priv_queue_list))
  147. dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
  148. pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
  149. if (!pqn) {
  150. retval = -ENOMEM;
  151. goto err_allocate_pqn;
  152. }
  153. switch (type) {
  154. case KFD_QUEUE_TYPE_SDMA:
  155. if (dev->dqm->queue_count >=
  156. CIK_SDMA_QUEUES_PER_ENGINE * CIK_SDMA_ENGINE_NUM) {
  157. pr_err("Over-subscription is not allowed for SDMA.\n");
  158. retval = -EPERM;
  159. goto err_create_queue;
  160. }
  161. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  162. if (retval != 0)
  163. goto err_create_queue;
  164. pqn->q = q;
  165. pqn->kq = NULL;
  166. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
  167. pr_debug("DQM returned %d for create_queue\n", retval);
  168. print_queue(q);
  169. break;
  170. case KFD_QUEUE_TYPE_COMPUTE:
  171. /* check if there is over subscription */
  172. if ((dev->dqm->sched_policy ==
  173. KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
  174. ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
  175. (dev->dqm->queue_count >= get_queues_num(dev->dqm)))) {
  176. pr_err("Over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
  177. retval = -EPERM;
  178. goto err_create_queue;
  179. }
  180. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  181. if (retval != 0)
  182. goto err_create_queue;
  183. pqn->q = q;
  184. pqn->kq = NULL;
  185. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
  186. pr_debug("DQM returned %d for create_queue\n", retval);
  187. print_queue(q);
  188. break;
  189. case KFD_QUEUE_TYPE_DIQ:
  190. kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
  191. if (!kq) {
  192. retval = -ENOMEM;
  193. goto err_create_queue;
  194. }
  195. kq->queue->properties.queue_id = *qid;
  196. pqn->kq = kq;
  197. pqn->q = NULL;
  198. retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
  199. kq, &pdd->qpd);
  200. break;
  201. default:
  202. WARN(1, "Invalid queue type %d", type);
  203. retval = -EINVAL;
  204. }
  205. if (retval != 0) {
  206. pr_err("DQM create queue failed\n");
  207. goto err_create_queue;
  208. }
  209. pr_debug("PQM After DQM create queue\n");
  210. list_add(&pqn->process_queue_list, &pqm->queues);
  211. if (q) {
  212. pr_debug("PQM done creating queue\n");
  213. print_queue_properties(&q->properties);
  214. }
  215. return retval;
  216. err_create_queue:
  217. kfree(pqn);
  218. err_allocate_pqn:
  219. /* check if queues list is empty unregister process from device */
  220. clear_bit(*qid, pqm->queue_slot_bitmap);
  221. if (list_empty(&pdd->qpd.queues_list) &&
  222. list_empty(&pdd->qpd.priv_queue_list))
  223. dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
  224. return retval;
  225. }
  226. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
  227. {
  228. struct process_queue_node *pqn;
  229. struct kfd_process_device *pdd;
  230. struct device_queue_manager *dqm;
  231. struct kfd_dev *dev;
  232. int retval;
  233. dqm = NULL;
  234. retval = 0;
  235. pqn = get_queue_by_qid(pqm, qid);
  236. if (!pqn) {
  237. pr_err("Queue id does not match any known queue\n");
  238. return -EINVAL;
  239. }
  240. dev = NULL;
  241. if (pqn->kq)
  242. dev = pqn->kq->dev;
  243. if (pqn->q)
  244. dev = pqn->q->device;
  245. if (WARN_ON(!dev))
  246. return -ENODEV;
  247. pdd = kfd_get_process_device_data(dev, pqm->process);
  248. if (!pdd) {
  249. pr_err("Process device data doesn't exist\n");
  250. return -1;
  251. }
  252. if (pqn->kq) {
  253. /* destroy kernel queue (DIQ) */
  254. dqm = pqn->kq->dev->dqm;
  255. dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
  256. kernel_queue_uninit(pqn->kq);
  257. }
  258. if (pqn->q) {
  259. dqm = pqn->q->device->dqm;
  260. retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
  261. if (retval) {
  262. pr_debug("Destroy queue failed, returned %d\n", retval);
  263. goto err_destroy_queue;
  264. }
  265. uninit_queue(pqn->q);
  266. }
  267. list_del(&pqn->process_queue_list);
  268. kfree(pqn);
  269. clear_bit(qid, pqm->queue_slot_bitmap);
  270. if (list_empty(&pdd->qpd.queues_list) &&
  271. list_empty(&pdd->qpd.priv_queue_list))
  272. dqm->ops.unregister_process(dqm, &pdd->qpd);
  273. err_destroy_queue:
  274. return retval;
  275. }
  276. int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
  277. struct queue_properties *p)
  278. {
  279. int retval;
  280. struct process_queue_node *pqn;
  281. pqn = get_queue_by_qid(pqm, qid);
  282. if (!pqn) {
  283. pr_debug("No queue %d exists for update operation\n", qid);
  284. return -EFAULT;
  285. }
  286. pqn->q->properties.queue_address = p->queue_address;
  287. pqn->q->properties.queue_size = p->queue_size;
  288. pqn->q->properties.queue_percent = p->queue_percent;
  289. pqn->q->properties.priority = p->priority;
  290. retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
  291. pqn->q);
  292. if (retval != 0)
  293. return retval;
  294. return 0;
  295. }
  296. struct kernel_queue *pqm_get_kernel_queue(
  297. struct process_queue_manager *pqm,
  298. unsigned int qid)
  299. {
  300. struct process_queue_node *pqn;
  301. pqn = get_queue_by_qid(pqm, qid);
  302. if (pqn && pqn->kq)
  303. return pqn->kq;
  304. return NULL;
  305. }
  306. #if defined(CONFIG_DEBUG_FS)
  307. int pqm_debugfs_mqds(struct seq_file *m, void *data)
  308. {
  309. struct process_queue_manager *pqm = data;
  310. struct process_queue_node *pqn;
  311. struct queue *q;
  312. enum KFD_MQD_TYPE mqd_type;
  313. struct mqd_manager *mqd_manager;
  314. int r = 0;
  315. list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
  316. if (pqn->q) {
  317. q = pqn->q;
  318. switch (q->properties.type) {
  319. case KFD_QUEUE_TYPE_SDMA:
  320. seq_printf(m, " SDMA queue on device %x\n",
  321. q->device->id);
  322. mqd_type = KFD_MQD_TYPE_SDMA;
  323. break;
  324. case KFD_QUEUE_TYPE_COMPUTE:
  325. seq_printf(m, " Compute queue on device %x\n",
  326. q->device->id);
  327. mqd_type = KFD_MQD_TYPE_CP;
  328. break;
  329. default:
  330. seq_printf(m,
  331. " Bad user queue type %d on device %x\n",
  332. q->properties.type, q->device->id);
  333. continue;
  334. }
  335. mqd_manager = q->device->dqm->ops.get_mqd_manager(
  336. q->device->dqm, mqd_type);
  337. } else if (pqn->kq) {
  338. q = pqn->kq->queue;
  339. mqd_manager = pqn->kq->mqd;
  340. switch (q->properties.type) {
  341. case KFD_QUEUE_TYPE_DIQ:
  342. seq_printf(m, " DIQ on device %x\n",
  343. pqn->kq->dev->id);
  344. mqd_type = KFD_MQD_TYPE_HIQ;
  345. break;
  346. default:
  347. seq_printf(m,
  348. " Bad kernel queue type %d on device %x\n",
  349. q->properties.type,
  350. pqn->kq->dev->id);
  351. continue;
  352. }
  353. } else {
  354. seq_printf(m,
  355. " Weird: Queue node with neither kernel nor user queue\n");
  356. continue;
  357. }
  358. r = mqd_manager->debugfs_show_mqd(m, q->mqd);
  359. if (r != 0)
  360. break;
  361. }
  362. return r;
  363. }
  364. #endif