kfd_process_queue_manager.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. case KFD_QUEUE_TYPE_COMPUTE:
  169. /* check if there is over subscription */
  170. if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
  171. ((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
  172. (dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) {
  173. pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
  174. retval = -EPERM;
  175. goto err_create_queue;
  176. }
  177. retval = create_cp_queue(pqm, dev, &q, &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. &q->properties.vmid);
  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 == NULL) {
  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. BUG();
  201. break;
  202. }
  203. if (retval != 0) {
  204. pr_debug("Error dqm create queue\n");
  205. goto err_create_queue;
  206. }
  207. pr_debug("kfd: PQM After DQM create queue\n");
  208. list_add(&pqn->process_queue_list, &pqm->queues);
  209. if (q) {
  210. *properties = q->properties;
  211. pr_debug("kfd: PQM done creating queue\n");
  212. print_queue_properties(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(&pqm->queues))
  221. dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
  222. return retval;
  223. }
  224. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
  225. {
  226. struct process_queue_node *pqn;
  227. struct kfd_process_device *pdd;
  228. struct device_queue_manager *dqm;
  229. struct kfd_dev *dev;
  230. int retval;
  231. dqm = NULL;
  232. BUG_ON(!pqm);
  233. retval = 0;
  234. pr_debug("kfd: In Func %s\n", __func__);
  235. pqn = get_queue_by_qid(pqm, qid);
  236. if (pqn == NULL) {
  237. pr_err("kfd: 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. BUG_ON(!dev);
  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 != 0)
  261. return retval;
  262. uninit_queue(pqn->q);
  263. }
  264. list_del(&pqn->process_queue_list);
  265. kfree(pqn);
  266. clear_bit(qid, pqm->queue_slot_bitmap);
  267. if (list_empty(&pqm->queues))
  268. dqm->ops.unregister_process(dqm, &pdd->qpd);
  269. return retval;
  270. }
  271. int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
  272. struct queue_properties *p)
  273. {
  274. int retval;
  275. struct process_queue_node *pqn;
  276. BUG_ON(!pqm);
  277. pqn = get_queue_by_qid(pqm, qid);
  278. if (!pqn) {
  279. pr_debug("amdkfd: No queue %d exists for update operation\n",
  280. qid);
  281. return -EFAULT;
  282. }
  283. pqn->q->properties.queue_address = p->queue_address;
  284. pqn->q->properties.queue_size = p->queue_size;
  285. pqn->q->properties.queue_percent = p->queue_percent;
  286. pqn->q->properties.priority = p->priority;
  287. retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
  288. pqn->q);
  289. if (retval != 0)
  290. return retval;
  291. return 0;
  292. }
  293. struct kernel_queue *pqm_get_kernel_queue(
  294. struct process_queue_manager *pqm,
  295. unsigned int qid)
  296. {
  297. struct process_queue_node *pqn;
  298. BUG_ON(!pqm);
  299. pqn = get_queue_by_qid(pqm, qid);
  300. if (pqn && pqn->kq)
  301. return pqn->kq;
  302. return NULL;
  303. }