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"
30 static inline struct process_queue_node *get_queue_by_qid(
31 struct process_queue_manager *pqm, unsigned int qid)
33 struct process_queue_node *pqn;
37 list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
38 if (pqn->q && pqn->q->properties.queue_id == qid)
40 if (pqn->kq && pqn->kq->queue->properties.queue_id == qid)
47 static int find_available_queue_slot(struct process_queue_manager *pqm,
54 pr_debug("kfd: in %s\n", __func__);
56 found = find_first_zero_bit(pqm->queue_slot_bitmap,
57 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
59 pr_debug("kfd: the new slot id %lu\n", found);
61 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
62 pr_info("amdkfd: Can not open more queues for process with pasid %d\n",
67 set_bit(found, pqm->queue_slot_bitmap);
73 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
77 INIT_LIST_HEAD(&pqm->queues);
78 pqm->queue_slot_bitmap =
79 kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
80 BITS_PER_BYTE), GFP_KERNEL);
81 if (pqm->queue_slot_bitmap == NULL)
88 void pqm_uninit(struct process_queue_manager *pqm)
91 struct process_queue_node *pqn, *next;
95 pr_debug("In func %s\n", __func__);
97 list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
98 retval = pqm_destroy_queue(
101 pqn->q->properties.queue_id :
102 pqn->kq->queue->properties.queue_id);
105 pr_err("kfd: failed to destroy queue\n");
109 kfree(pqm->queue_slot_bitmap);
110 pqm->queue_slot_bitmap = NULL;
113 static int create_cp_queue(struct process_queue_manager *pqm,
114 struct kfd_dev *dev, struct queue **q,
115 struct queue_properties *q_properties,
116 struct file *f, unsigned int qid)
122 /* Doorbell initialized in user space*/
123 q_properties->doorbell_ptr = NULL;
125 q_properties->doorbell_off =
126 kfd_queue_id_to_doorbell(dev, pqm->process, qid);
128 /* let DQM handle it*/
129 q_properties->vmid = 0;
130 q_properties->queue_id = qid;
131 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
133 retval = init_queue(q, *q_properties);
138 (*q)->process = pqm->process;
140 pr_debug("kfd: PQM After init queue");
148 int pqm_create_queue(struct process_queue_manager *pqm,
151 struct queue_properties *properties,
153 enum kfd_queue_type type,
157 struct kfd_process_device *pdd;
158 struct queue_properties q_properties;
160 struct process_queue_node *pqn;
161 struct kernel_queue *kq;
163 BUG_ON(!pqm || !dev || !properties || !qid);
165 memset(&q_properties, 0, sizeof(struct queue_properties));
166 memcpy(&q_properties, properties, sizeof(struct queue_properties));
170 pdd = kfd_get_process_device_data(dev, pqm->process, 1);
173 retval = find_available_queue_slot(pqm, qid);
177 if (list_empty(&pqm->queues)) {
179 dev->dqm->register_process(dev->dqm, &pdd->qpd);
182 pqn = kzalloc(sizeof(struct process_queue_node), GFP_KERNEL);
185 goto err_allocate_pqn;
189 case KFD_QUEUE_TYPE_COMPUTE:
190 /* check if there is over subscription */
191 if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
192 ((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
193 (dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) {
194 pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
196 goto err_create_queue;
199 retval = create_cp_queue(pqm, dev, &q, &q_properties, f, *qid);
201 goto err_create_queue;
204 retval = dev->dqm->create_queue(dev->dqm, q, &pdd->qpd,
205 &q->properties.vmid);
206 pr_debug("DQM returned %d for create_queue\n", retval);
209 case KFD_QUEUE_TYPE_DIQ:
210 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
213 goto err_create_queue;
215 kq->queue->properties.queue_id = *qid;
218 retval = dev->dqm->create_kernel_queue(dev->dqm, kq, &pdd->qpd);
226 pr_debug("Error dqm create queue\n");
227 goto err_create_queue;
230 pr_debug("kfd: PQM After DQM create queue\n");
232 list_add(&pqn->process_queue_list, &pqm->queues);
235 *properties = q->properties;
236 pr_debug("kfd: PQM done creating queue\n");
237 print_queue_properties(properties);
245 /* check if queues list is empty unregister process from device */
246 clear_bit(*qid, pqm->queue_slot_bitmap);
247 if (list_empty(&pqm->queues))
248 dev->dqm->unregister_process(dev->dqm, &pdd->qpd);
252 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
254 struct process_queue_node *pqn;
255 struct kfd_process_device *pdd;
256 struct device_queue_manager *dqm;
265 pr_debug("kfd: In Func %s\n", __func__);
267 pqn = get_queue_by_qid(pqm, qid);
269 pr_err("kfd: queue id does not match any known queue\n");
277 dev = pqn->q->device;
280 pdd = kfd_get_process_device_data(dev, pqm->process, 1);
284 /* destroy kernel queue (DIQ) */
285 dqm = pqn->kq->dev->dqm;
286 dqm->destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
287 kernel_queue_uninit(pqn->kq);
291 dqm = pqn->q->device->dqm;
292 retval = dqm->destroy_queue(dqm, &pdd->qpd, pqn->q);
296 uninit_queue(pqn->q);
299 list_del(&pqn->process_queue_list);
301 clear_bit(qid, pqm->queue_slot_bitmap);
303 if (list_empty(&pqm->queues))
304 dqm->unregister_process(dqm, &pdd->qpd);
309 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
310 struct queue_properties *p)
313 struct process_queue_node *pqn;
317 pqn = get_queue_by_qid(pqm, qid);
320 pqn->q->properties.queue_address = p->queue_address;
321 pqn->q->properties.queue_size = p->queue_size;
322 pqn->q->properties.queue_percent = p->queue_percent;
323 pqn->q->properties.priority = p->priority;
325 retval = pqn->q->device->dqm->update_queue(pqn->q->device->dqm, pqn->q);
332 static __attribute__((unused)) struct kernel_queue *pqm_get_kernel_queue(
333 struct process_queue_manager *pqm,
336 struct process_queue_node *pqn;
340 pqn = get_queue_by_qid(pqm, qid);