2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/slab.h>
25 #include <linux/list.h>
26 #include "kfd_device_queue_manager.h"
28 #include "kfd_kernel_queue.h"
29 #include "amdgpu_amdkfd.h"
31 static inline struct process_queue_node *get_queue_by_qid(
32 struct process_queue_manager *pqm, unsigned int qid)
34 struct process_queue_node *pqn;
36 list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
37 if ((pqn->q && pqn->q->properties.queue_id == qid) ||
38 (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
45 static int find_available_queue_slot(struct process_queue_manager *pqm,
50 found = find_first_zero_bit(pqm->queue_slot_bitmap,
51 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
53 pr_debug("The new slot id %lu\n", found);
55 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
56 pr_info("Cannot open more queues for process with pasid 0x%x\n",
61 set_bit(found, pqm->queue_slot_bitmap);
67 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
69 struct kfd_dev *dev = pdd->dev;
71 if (pdd->already_dequeued)
74 dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
75 pdd->already_dequeued = true;
78 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
81 struct kfd_dev *dev = NULL;
82 struct process_queue_node *pqn;
83 struct kfd_process_device *pdd;
84 struct kgd_mem *mem = NULL;
87 pqn = get_queue_by_qid(pqm, qid);
89 pr_err("Queue id does not match any known queue\n");
98 pdd = kfd_get_process_device_data(dev, pqm->process);
100 pr_err("Process device data doesn't exist\n");
104 /* Only allow one queue per process can have GWS assigned */
105 if (gws && pdd->qpd.num_gws)
108 if (!gws && pdd->qpd.num_gws == 0)
112 ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
115 ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
121 pdd->qpd.num_gws = gws ? amdgpu_amdkfd_get_num_gws(dev->kgd) : 0;
123 return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
127 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
131 for (i = 0; i < p->n_pdds; i++)
132 kfd_process_dequeue_from_device(p->pdds[i]);
135 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
137 INIT_LIST_HEAD(&pqm->queues);
138 pqm->queue_slot_bitmap =
139 kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
140 BITS_PER_BYTE), GFP_KERNEL);
141 if (!pqm->queue_slot_bitmap)
148 void pqm_uninit(struct process_queue_manager *pqm)
150 struct process_queue_node *pqn, *next;
152 list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
153 if (pqn->q && pqn->q->gws)
154 amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
156 kfd_procfs_del_queue(pqn->q);
157 uninit_queue(pqn->q);
158 list_del(&pqn->process_queue_list);
162 kfree(pqm->queue_slot_bitmap);
163 pqm->queue_slot_bitmap = NULL;
166 static int init_user_queue(struct process_queue_manager *pqm,
167 struct kfd_dev *dev, struct queue **q,
168 struct queue_properties *q_properties,
169 struct file *f, unsigned int qid)
173 /* Doorbell initialized in user space*/
174 q_properties->doorbell_ptr = NULL;
176 /* let DQM handle it*/
177 q_properties->vmid = 0;
178 q_properties->queue_id = qid;
180 retval = init_queue(q, q_properties);
185 (*q)->process = pqm->process;
187 pr_debug("PQM After init queue");
192 int pqm_create_queue(struct process_queue_manager *pqm,
195 struct queue_properties *properties,
197 uint32_t *p_doorbell_offset_in_process)
200 struct kfd_process_device *pdd;
202 struct process_queue_node *pqn;
203 struct kernel_queue *kq;
204 enum kfd_queue_type type = properties->type;
205 unsigned int max_queues = 127; /* HWS limit */
210 pdd = kfd_get_process_device_data(dev, pqm->process);
212 pr_err("Process device data doesn't exist\n");
217 * for debug process, verify that it is within the static queues limit
218 * currently limit is set to half of the total avail HQD slots
219 * If we are just about to create DIQ, the is_debug flag is not set yet
220 * Hence we also check the type as well
222 if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
223 max_queues = dev->device_info->max_no_of_hqd/2;
225 if (pdd->qpd.queue_count >= max_queues)
228 retval = find_available_queue_slot(pqm, qid);
232 if (list_empty(&pdd->qpd.queues_list) &&
233 list_empty(&pdd->qpd.priv_queue_list))
234 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
236 pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
239 goto err_allocate_pqn;
243 case KFD_QUEUE_TYPE_SDMA:
244 case KFD_QUEUE_TYPE_SDMA_XGMI:
245 /* SDMA queues are always allocated statically no matter
246 * which scheduler mode is used. We also do not need to
247 * check whether a SDMA queue can be allocated here, because
248 * allocate_sdma_queue() in create_queue() has the
249 * corresponding check logic.
251 retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
253 goto err_create_queue;
256 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
260 case KFD_QUEUE_TYPE_COMPUTE:
261 /* check if there is over subscription */
262 if ((dev->dqm->sched_policy ==
263 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
264 ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
265 (dev->dqm->active_queue_count >= get_cp_queues_num(dev->dqm)))) {
266 pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
268 goto err_create_queue;
271 retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
273 goto err_create_queue;
276 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
279 case KFD_QUEUE_TYPE_DIQ:
280 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
283 goto err_create_queue;
285 kq->queue->properties.queue_id = *qid;
288 retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
292 WARN(1, "Invalid queue type %d", type);
297 pr_err("Pasid 0x%x DQM create queue type %d failed. ret %d\n",
298 pqm->process->pasid, type, retval);
299 goto err_create_queue;
302 if (q && p_doorbell_offset_in_process)
303 /* Return the doorbell offset within the doorbell page
304 * to the caller so it can be passed up to user mode
306 * There are always 1024 doorbells per process, so in case
307 * of 8-byte doorbells, there are two doorbell pages per
310 *p_doorbell_offset_in_process =
311 (q->properties.doorbell_off * sizeof(uint32_t)) &
312 (kfd_doorbell_process_slice(dev) - 1);
314 pr_debug("PQM After DQM create queue\n");
316 list_add(&pqn->process_queue_list, &pqm->queues);
319 pr_debug("PQM done creating queue\n");
320 kfd_procfs_add_queue(q);
321 print_queue_properties(&q->properties);
329 kernel_queue_uninit(kq, false);
332 /* check if queues list is empty unregister process from device */
333 clear_bit(*qid, pqm->queue_slot_bitmap);
334 if (list_empty(&pdd->qpd.queues_list) &&
335 list_empty(&pdd->qpd.priv_queue_list))
336 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
340 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
342 struct process_queue_node *pqn;
343 struct kfd_process_device *pdd;
344 struct device_queue_manager *dqm;
352 pqn = get_queue_by_qid(pqm, qid);
354 pr_err("Queue id does not match any known queue\n");
362 dev = pqn->q->device;
366 pdd = kfd_get_process_device_data(dev, pqm->process);
368 pr_err("Process device data doesn't exist\n");
373 /* destroy kernel queue (DIQ) */
374 dqm = pqn->kq->dev->dqm;
375 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
376 kernel_queue_uninit(pqn->kq, false);
380 kfd_procfs_del_queue(pqn->q);
381 dqm = pqn->q->device->dqm;
382 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
384 pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
386 pqn->q->properties.queue_id, retval);
387 if (retval != -ETIME)
388 goto err_destroy_queue;
392 amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
394 pdd->qpd.num_gws = 0;
397 kfree(pqn->q->properties.cu_mask);
398 pqn->q->properties.cu_mask = NULL;
399 uninit_queue(pqn->q);
402 list_del(&pqn->process_queue_list);
404 clear_bit(qid, pqm->queue_slot_bitmap);
406 if (list_empty(&pdd->qpd.queues_list) &&
407 list_empty(&pdd->qpd.priv_queue_list))
408 dqm->ops.unregister_process(dqm, &pdd->qpd);
414 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
415 struct queue_properties *p)
418 struct process_queue_node *pqn;
420 pqn = get_queue_by_qid(pqm, qid);
422 pr_debug("No queue %d exists for update operation\n", qid);
426 pqn->q->properties.queue_address = p->queue_address;
427 pqn->q->properties.queue_size = p->queue_size;
428 pqn->q->properties.queue_percent = p->queue_percent;
429 pqn->q->properties.priority = p->priority;
431 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
439 int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid,
440 struct queue_properties *p)
443 struct process_queue_node *pqn;
445 pqn = get_queue_by_qid(pqm, qid);
447 pr_debug("No queue %d exists for update operation\n", qid);
451 /* Free the old CU mask memory if it is already allocated, then
452 * allocate memory for the new CU mask.
454 kfree(pqn->q->properties.cu_mask);
456 pqn->q->properties.cu_mask_count = p->cu_mask_count;
457 pqn->q->properties.cu_mask = p->cu_mask;
459 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
467 struct kernel_queue *pqm_get_kernel_queue(
468 struct process_queue_manager *pqm,
471 struct process_queue_node *pqn;
473 pqn = get_queue_by_qid(pqm, qid);
480 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
483 struct process_queue_node *pqn;
485 pqn = get_queue_by_qid(pqm, qid);
486 return pqn ? pqn->q : NULL;
489 int pqm_get_wave_state(struct process_queue_manager *pqm,
491 void __user *ctl_stack,
492 u32 *ctl_stack_used_size,
493 u32 *save_area_used_size)
495 struct process_queue_node *pqn;
497 pqn = get_queue_by_qid(pqm, qid);
499 pr_debug("amdkfd: No queue %d exists for operation\n",
504 return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
508 save_area_used_size);
511 #if defined(CONFIG_DEBUG_FS)
513 int pqm_debugfs_mqds(struct seq_file *m, void *data)
515 struct process_queue_manager *pqm = data;
516 struct process_queue_node *pqn;
518 enum KFD_MQD_TYPE mqd_type;
519 struct mqd_manager *mqd_mgr;
522 list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
525 switch (q->properties.type) {
526 case KFD_QUEUE_TYPE_SDMA:
527 case KFD_QUEUE_TYPE_SDMA_XGMI:
528 seq_printf(m, " SDMA queue on device %x\n",
530 mqd_type = KFD_MQD_TYPE_SDMA;
532 case KFD_QUEUE_TYPE_COMPUTE:
533 seq_printf(m, " Compute queue on device %x\n",
535 mqd_type = KFD_MQD_TYPE_CP;
539 " Bad user queue type %d on device %x\n",
540 q->properties.type, q->device->id);
543 mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
544 } else if (pqn->kq) {
546 mqd_mgr = pqn->kq->mqd_mgr;
547 switch (q->properties.type) {
548 case KFD_QUEUE_TYPE_DIQ:
549 seq_printf(m, " DIQ on device %x\n",
554 " Bad kernel queue type %d on device %x\n",
561 " Weird: Queue node with neither kernel nor user queue\n");
565 r = mqd_mgr->debugfs_show_mqd(m, q->mqd);