kfd_process_queue_manager.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. BUG_ON(!pqm);
  33. list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
  34. if (pqn->q && pqn->q->properties.queue_id == qid)
  35. return pqn;
  36. if (pqn->kq && pqn->kq->queue->properties.queue_id == qid)
  37. return pqn;
  38. }
  39. return NULL;
  40. }
  41. static int find_available_queue_slot(struct process_queue_manager *pqm,
  42. unsigned int *qid)
  43. {
  44. unsigned long found;
  45. BUG_ON(!pqm || !qid);
  46. pr_debug("kfd: in %s\n", __func__);
  47. found = find_first_zero_bit(pqm->queue_slot_bitmap,
  48. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
  49. pr_debug("kfd: the new slot id %lu\n", found);
  50. if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
  51. pr_info("amdkfd: Can not open more queues for process with pasid %d\n",
  52. pqm->process->pasid);
  53. return -ENOMEM;
  54. }
  55. set_bit(found, pqm->queue_slot_bitmap);
  56. *qid = found;
  57. return 0;
  58. }
  59. int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
  60. {
  61. BUG_ON(!pqm);
  62. INIT_LIST_HEAD(&pqm->queues);
  63. pqm->queue_slot_bitmap =
  64. kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  65. BITS_PER_BYTE), GFP_KERNEL);
  66. if (pqm->queue_slot_bitmap == NULL)
  67. return -ENOMEM;
  68. pqm->process = p;
  69. return 0;
  70. }
  71. void pqm_uninit(struct process_queue_manager *pqm)
  72. {
  73. int retval;
  74. struct process_queue_node *pqn, *next;
  75. BUG_ON(!pqm);
  76. pr_debug("In func %s\n", __func__);
  77. list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
  78. retval = pqm_destroy_queue(
  79. pqm,
  80. (pqn->q != NULL) ?
  81. pqn->q->properties.queue_id :
  82. pqn->kq->queue->properties.queue_id);
  83. if (retval != 0) {
  84. pr_err("kfd: failed to destroy queue\n");
  85. return;
  86. }
  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. retval = 0;
  98. /* Doorbell initialized in user space*/
  99. q_properties->doorbell_ptr = NULL;
  100. q_properties->doorbell_off =
  101. kfd_queue_id_to_doorbell(dev, pqm->process, qid);
  102. /* let DQM handle it*/
  103. q_properties->vmid = 0;
  104. q_properties->queue_id = qid;
  105. retval = init_queue(q, *q_properties);
  106. if (retval != 0)
  107. goto err_init_queue;
  108. (*q)->device = dev;
  109. (*q)->process = pqm->process;
  110. pr_debug("kfd: PQM After init queue");
  111. return retval;
  112. err_init_queue:
  113. return retval;
  114. }
  115. int pqm_create_queue(struct process_queue_manager *pqm,
  116. struct kfd_dev *dev,
  117. struct file *f,
  118. struct queue_properties *properties,
  119. unsigned int flags,
  120. enum kfd_queue_type type,
  121. unsigned int *qid)
  122. {
  123. int retval;
  124. struct kfd_process_device *pdd;
  125. struct queue_properties q_properties;
  126. struct queue *q;
  127. struct process_queue_node *pqn;
  128. struct kernel_queue *kq;
  129. int num_queues = 0;
  130. struct queue *cur;
  131. BUG_ON(!pqm || !dev || !properties || !qid);
  132. memset(&q_properties, 0, sizeof(struct queue_properties));
  133. memcpy(&q_properties, properties, sizeof(struct queue_properties));
  134. q = NULL;
  135. kq = NULL;
  136. pdd = kfd_get_process_device_data(dev, pqm->process);
  137. if (!pdd) {
  138. pr_err("Process device data doesn't exist\n");
  139. return -1;
  140. }
  141. /*
  142. * for debug process, verify that it is within the static queues limit
  143. * currently limit is set to half of the total avail HQD slots
  144. * If we are just about to create DIQ, the is_debug flag is not set yet
  145. * Hence we also check the type as well
  146. */
  147. if ((pdd->qpd.is_debug) ||
  148. (type == KFD_QUEUE_TYPE_DIQ)) {
  149. list_for_each_entry(cur, &pdd->qpd.queues_list, list)
  150. num_queues++;
  151. if (num_queues >= dev->device_info->max_no_of_hqd/2)
  152. return (-ENOSPC);
  153. }
  154. retval = find_available_queue_slot(pqm, qid);
  155. if (retval != 0)
  156. return retval;
  157. if (list_empty(&pqm->queues)) {
  158. pdd->qpd.pqm = pqm;
  159. dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
  160. }
  161. pqn = kzalloc(sizeof(struct process_queue_node), GFP_KERNEL);
  162. if (!pqn) {
  163. retval = -ENOMEM;
  164. goto err_allocate_pqn;
  165. }
  166. switch (type) {
  167. case KFD_QUEUE_TYPE_SDMA:
  168. if (dev->dqm->queue_count >=
  169. CIK_SDMA_QUEUES_PER_ENGINE * CIK_SDMA_ENGINE_NUM) {
  170. pr_err("Over-subscription is not allowed for SDMA.\n");
  171. retval = -EPERM;
  172. goto err_create_queue;
  173. }
  174. retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
  175. if (retval != 0)
  176. goto err_create_queue;
  177. pqn->q = q;
  178. pqn->kq = NULL;
  179. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
  180. &q->properties.vmid);
  181. pr_debug("DQM returned %d for create_queue\n", retval);
  182. print_queue(q);
  183. break;
  184. case KFD_QUEUE_TYPE_COMPUTE:
  185. /* check if there is over subscription */
  186. if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
  187. ((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
  188. (dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) {
  189. pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
  190. retval = -EPERM;
  191. goto err_create_queue;
  192. }
  193. retval = create_cp_queue(pqm, dev, &q, &q_properties, f, *qid);
  194. if (retval != 0)
  195. goto err_create_queue;
  196. pqn->q = q;
  197. pqn->kq = NULL;
  198. retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
  199. &q->properties.vmid);
  200. pr_debug("DQM returned %d for create_queue\n", retval);
  201. print_queue(q);
  202. break;
  203. case KFD_QUEUE_TYPE_DIQ:
  204. kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
  205. if (kq == NULL) {
  206. retval = -ENOMEM;
  207. goto err_create_queue;
  208. }
  209. kq->queue->properties.queue_id = *qid;
  210. pqn->kq = kq;
  211. pqn->q = NULL;
  212. retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
  213. kq, &pdd->qpd);
  214. break;
  215. default:
  216. BUG();
  217. break;
  218. }
  219. if (retval != 0) {
  220. pr_debug("Error dqm create queue\n");
  221. goto err_create_queue;
  222. }
  223. pr_debug("kfd: PQM After DQM create queue\n");
  224. list_add(&pqn->process_queue_list, &pqm->queues);
  225. if (q) {
  226. *properties = q->properties;
  227. pr_debug("kfd: PQM done creating queue\n");
  228. print_queue_properties(properties);
  229. }
  230. return retval;
  231. err_create_queue:
  232. kfree(pqn);
  233. err_allocate_pqn:
  234. /* check if queues list is empty unregister process from device */
  235. clear_bit(*qid, pqm->queue_slot_bitmap);
  236. if (list_empty(&pqm->queues))
  237. dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
  238. return retval;
  239. }
  240. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
  241. {
  242. struct process_queue_node *pqn;
  243. struct kfd_process_device *pdd;
  244. struct device_queue_manager *dqm;
  245. struct kfd_dev *dev;
  246. int retval;
  247. dqm = NULL;
  248. BUG_ON(!pqm);
  249. retval = 0;
  250. pr_debug("kfd: In Func %s\n", __func__);
  251. pqn = get_queue_by_qid(pqm, qid);
  252. if (pqn == NULL) {
  253. pr_err("kfd: queue id does not match any known queue\n");
  254. return -EINVAL;
  255. }
  256. dev = NULL;
  257. if (pqn->kq)
  258. dev = pqn->kq->dev;
  259. if (pqn->q)
  260. dev = pqn->q->device;
  261. BUG_ON(!dev);
  262. pdd = kfd_get_process_device_data(dev, pqm->process);
  263. if (!pdd) {
  264. pr_err("Process device data doesn't exist\n");
  265. return -1;
  266. }
  267. if (pqn->kq) {
  268. /* destroy kernel queue (DIQ) */
  269. dqm = pqn->kq->dev->dqm;
  270. dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
  271. kernel_queue_uninit(pqn->kq);
  272. }
  273. if (pqn->q) {
  274. dqm = pqn->q->device->dqm;
  275. retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
  276. if (retval != 0)
  277. return retval;
  278. uninit_queue(pqn->q);
  279. }
  280. list_del(&pqn->process_queue_list);
  281. kfree(pqn);
  282. clear_bit(qid, pqm->queue_slot_bitmap);
  283. if (list_empty(&pqm->queues))
  284. dqm->ops.unregister_process(dqm, &pdd->qpd);
  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. BUG_ON(!pqm);
  293. pqn = get_queue_by_qid(pqm, qid);
  294. if (!pqn) {
  295. pr_debug("amdkfd: No queue %d exists for update operation\n",
  296. qid);
  297. return -EFAULT;
  298. }
  299. pqn->q->properties.queue_address = p->queue_address;
  300. pqn->q->properties.queue_size = p->queue_size;
  301. pqn->q->properties.queue_percent = p->queue_percent;
  302. pqn->q->properties.priority = p->priority;
  303. retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
  304. pqn->q);
  305. if (retval != 0)
  306. return retval;
  307. return 0;
  308. }
  309. struct kernel_queue *pqm_get_kernel_queue(
  310. struct process_queue_manager *pqm,
  311. unsigned int qid)
  312. {
  313. struct process_queue_node *pqn;
  314. BUG_ON(!pqm);
  315. pqn = get_queue_by_qid(pqm, qid);
  316. if (pqn && pqn->kq)
  317. return pqn->kq;
  318. return NULL;
  319. }