kfd_process_queue_manager.c 11 KB

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