Linux Audio

Check our new training course

Loading...
v4.6
  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
 24#include <linux/slab.h>
 25#include <linux/list.h>
 26#include "kfd_device_queue_manager.h"
 27#include "kfd_priv.h"
 28#include "kfd_kernel_queue.h"
 
 29
 30static inline struct process_queue_node *get_queue_by_qid(
 31			struct process_queue_manager *pqm, unsigned int qid)
 32{
 33	struct process_queue_node *pqn;
 34
 35	BUG_ON(!pqm);
 36
 37	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
 38		if (pqn->q && pqn->q->properties.queue_id == qid)
 39			return pqn;
 40		if (pqn->kq && pqn->kq->queue->properties.queue_id == qid)
 41			return pqn;
 42	}
 43
 44	return NULL;
 45}
 46
 47static int find_available_queue_slot(struct process_queue_manager *pqm,
 48					unsigned int *qid)
 49{
 50	unsigned long found;
 51
 52	BUG_ON(!pqm || !qid);
 53
 54	pr_debug("kfd: in %s\n", __func__);
 55
 56	found = find_first_zero_bit(pqm->queue_slot_bitmap,
 57			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
 58
 59	pr_debug("kfd: the new slot id %lu\n", found);
 60
 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",
 63				pqm->process->pasid);
 64		return -ENOMEM;
 65	}
 66
 67	set_bit(found, pqm->queue_slot_bitmap);
 68	*qid = found;
 69
 70	return 0;
 71}
 72
 73int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
 
 
 
 
 
 
 
 
 
 
 
 
 74{
 75	BUG_ON(!pqm);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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)
 82		return -ENOMEM;
 83	pqm->process = p;
 84
 85	return 0;
 86}
 87
 88void pqm_uninit(struct process_queue_manager *pqm)
 89{
 90	int retval;
 91	struct process_queue_node *pqn, *next;
 92
 93	BUG_ON(!pqm);
 94
 95	pr_debug("In func %s\n", __func__);
 96
 97	list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
 98		retval = pqm_destroy_queue(
 99				pqm,
100				(pqn->q != NULL) ?
101					pqn->q->properties.queue_id :
102					pqn->kq->queue->properties.queue_id);
103
104		if (retval != 0) {
105			pr_err("kfd: failed to destroy queue\n");
106			return;
107		}
108	}
 
109	kfree(pqm->queue_slot_bitmap);
110	pqm->queue_slot_bitmap = NULL;
111}
112
113static 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)
117{
118	int retval;
119
120	retval = 0;
121
122	/* Doorbell initialized in user space*/
123	q_properties->doorbell_ptr = NULL;
124
125	q_properties->doorbell_off =
126			kfd_queue_id_to_doorbell(dev, pqm->process, qid);
127
128	/* let DQM handle it*/
129	q_properties->vmid = 0;
130	q_properties->queue_id = qid;
131
132	retval = init_queue(q, *q_properties);
133	if (retval != 0)
134		goto err_init_queue;
135
136	(*q)->device = dev;
137	(*q)->process = pqm->process;
138
139	pr_debug("kfd: PQM After init queue");
140
141	return retval;
142
143err_init_queue:
144	return retval;
145}
146
147int pqm_create_queue(struct process_queue_manager *pqm,
148			    struct kfd_dev *dev,
149			    struct file *f,
150			    struct queue_properties *properties,
151			    unsigned int flags,
152			    enum kfd_queue_type type,
153			    unsigned int *qid)
154{
155	int retval;
156	struct kfd_process_device *pdd;
157	struct queue_properties q_properties;
158	struct queue *q;
159	struct process_queue_node *pqn;
160	struct kernel_queue *kq;
161	int num_queues = 0;
162	struct queue *cur;
163
164	BUG_ON(!pqm || !dev || !properties || !qid);
165
166	memset(&q_properties, 0, sizeof(struct queue_properties));
167	memcpy(&q_properties, properties, sizeof(struct queue_properties));
168	q = NULL;
169	kq = NULL;
170
171	pdd = kfd_get_process_device_data(dev, pqm->process);
172	if (!pdd) {
173		pr_err("Process device data doesn't exist\n");
174		return -1;
175	}
176
177	/*
178	 * for debug process, verify that it is within the static queues limit
179	 * currently limit is set to half of the total avail HQD slots
180	 * If we are just about to create DIQ, the is_debug flag is not set yet
181	 * Hence we also check the type as well
182	 */
183	if ((pdd->qpd.is_debug) ||
184		(type == KFD_QUEUE_TYPE_DIQ)) {
185		list_for_each_entry(cur, &pdd->qpd.queues_list, list)
186			num_queues++;
187		if (num_queues >= dev->device_info->max_no_of_hqd/2)
188			return (-ENOSPC);
189	}
190
191	retval = find_available_queue_slot(pqm, qid);
192	if (retval != 0)
193		return retval;
194
195	if (list_empty(&pqm->queues)) {
196		pdd->qpd.pqm = pqm;
197		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
198	}
199
200	pqn = kzalloc(sizeof(struct process_queue_node), GFP_KERNEL);
201	if (!pqn) {
202		retval = -ENOMEM;
203		goto err_allocate_pqn;
204	}
205
206	switch (type) {
207	case KFD_QUEUE_TYPE_SDMA:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208	case KFD_QUEUE_TYPE_COMPUTE:
209		/* check if there is over subscription */
210		if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
211		((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
212		(dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) {
213			pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
 
214			retval = -EPERM;
215			goto err_create_queue;
216		}
217
218		retval = create_cp_queue(pqm, dev, &q, &q_properties, f, *qid);
219		if (retval != 0)
220			goto err_create_queue;
221		pqn->q = q;
222		pqn->kq = NULL;
223		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
224						&q->properties.vmid);
225		pr_debug("DQM returned %d for create_queue\n", retval);
226		print_queue(q);
227		break;
228	case KFD_QUEUE_TYPE_DIQ:
229		kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
230		if (kq == NULL) {
231			retval = -ENOMEM;
232			goto err_create_queue;
233		}
234		kq->queue->properties.queue_id = *qid;
235		pqn->kq = kq;
236		pqn->q = NULL;
237		retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
238							kq, &pdd->qpd);
239		break;
240	default:
241		BUG();
242		break;
243	}
244
245	if (retval != 0) {
246		pr_debug("Error dqm create queue\n");
 
247		goto err_create_queue;
248	}
249
250	pr_debug("kfd: PQM After DQM create queue\n");
 
 
 
 
 
 
 
 
 
 
 
 
251
252	list_add(&pqn->process_queue_list, &pqm->queues);
253
254	if (q) {
255		*properties = q->properties;
256		pr_debug("kfd: PQM done creating queue\n");
257		print_queue_properties(properties);
258	}
259
260	return retval;
261
262err_create_queue:
 
 
 
263	kfree(pqn);
264err_allocate_pqn:
265	/* check if queues list is empty unregister process from device */
266	clear_bit(*qid, pqm->queue_slot_bitmap);
267	if (list_empty(&pqm->queues))
 
268		dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
269	return retval;
270}
271
272int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
273{
274	struct process_queue_node *pqn;
275	struct kfd_process_device *pdd;
276	struct device_queue_manager *dqm;
277	struct kfd_dev *dev;
278	int retval;
279
280	dqm = NULL;
281
282	BUG_ON(!pqm);
283	retval = 0;
284
285	pr_debug("kfd: In Func %s\n", __func__);
286
287	pqn = get_queue_by_qid(pqm, qid);
288	if (pqn == NULL) {
289		pr_err("kfd: queue id does not match any known queue\n");
290		return -EINVAL;
291	}
292
293	dev = NULL;
294	if (pqn->kq)
295		dev = pqn->kq->dev;
296	if (pqn->q)
297		dev = pqn->q->device;
298	BUG_ON(!dev);
 
299
300	pdd = kfd_get_process_device_data(dev, pqm->process);
301	if (!pdd) {
302		pr_err("Process device data doesn't exist\n");
303		return -1;
304	}
305
306	if (pqn->kq) {
307		/* destroy kernel queue (DIQ) */
308		dqm = pqn->kq->dev->dqm;
309		dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
310		kernel_queue_uninit(pqn->kq);
311	}
312
313	if (pqn->q) {
 
314		dqm = pqn->q->device->dqm;
315		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
316		if (retval != 0)
317			return retval;
 
 
 
 
 
318
 
 
 
 
 
 
 
 
319		uninit_queue(pqn->q);
320	}
321
322	list_del(&pqn->process_queue_list);
323	kfree(pqn);
324	clear_bit(qid, pqm->queue_slot_bitmap);
325
326	if (list_empty(&pqm->queues))
 
327		dqm->ops.unregister_process(dqm, &pdd->qpd);
328
 
329	return retval;
330}
331
332int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
333			struct queue_properties *p)
334{
335	int retval;
336	struct process_queue_node *pqn;
337
338	BUG_ON(!pqm);
339
340	pqn = get_queue_by_qid(pqm, qid);
341	if (!pqn) {
342		pr_debug("amdkfd: No queue %d exists for update operation\n",
343				qid);
344		return -EFAULT;
345	}
346
347	pqn->q->properties.queue_address = p->queue_address;
348	pqn->q->properties.queue_size = p->queue_size;
349	pqn->q->properties.queue_percent = p->queue_percent;
350	pqn->q->properties.priority = p->priority;
351
352	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
353							pqn->q);
354	if (retval != 0)
355		return retval;
356
357	return 0;
358}
359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360struct kernel_queue *pqm_get_kernel_queue(
361					struct process_queue_manager *pqm,
362					unsigned int qid)
363{
364	struct process_queue_node *pqn;
365
366	BUG_ON(!pqm);
367
368	pqn = get_queue_by_qid(pqm, qid);
369	if (pqn && pqn->kq)
370		return pqn->kq;
371
372	return NULL;
373}
374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
v5.14.15
  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
 24#include <linux/slab.h>
 25#include <linux/list.h>
 26#include "kfd_device_queue_manager.h"
 27#include "kfd_priv.h"
 28#include "kfd_kernel_queue.h"
 29#include "amdgpu_amdkfd.h"
 30
 31static inline struct process_queue_node *get_queue_by_qid(
 32			struct process_queue_manager *pqm, unsigned int qid)
 33{
 34	struct process_queue_node *pqn;
 35
 
 
 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))
 
 39			return pqn;
 40	}
 41
 42	return NULL;
 43}
 44
 45static int find_available_queue_slot(struct process_queue_manager *pqm,
 46					unsigned int *qid)
 47{
 48	unsigned long found;
 49
 
 
 
 
 50	found = find_first_zero_bit(pqm->queue_slot_bitmap,
 51			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
 52
 53	pr_debug("The new slot id %lu\n", found);
 54
 55	if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
 56		pr_info("Cannot open more queues for process with pasid 0x%x\n",
 57				pqm->process->pasid);
 58		return -ENOMEM;
 59	}
 60
 61	set_bit(found, pqm->queue_slot_bitmap);
 62	*qid = found;
 63
 64	return 0;
 65}
 66
 67void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
 68{
 69	struct kfd_dev *dev = pdd->dev;
 70
 71	if (pdd->already_dequeued)
 72		return;
 73
 74	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
 75	pdd->already_dequeued = true;
 76}
 77
 78int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
 79			void *gws)
 80{
 81	struct kfd_dev *dev = NULL;
 82	struct process_queue_node *pqn;
 83	struct kfd_process_device *pdd;
 84	struct kgd_mem *mem = NULL;
 85	int ret;
 86
 87	pqn = get_queue_by_qid(pqm, qid);
 88	if (!pqn) {
 89		pr_err("Queue id does not match any known queue\n");
 90		return -EINVAL;
 91	}
 92
 93	if (pqn->q)
 94		dev = pqn->q->device;
 95	if (WARN_ON(!dev))
 96		return -ENODEV;
 97
 98	pdd = kfd_get_process_device_data(dev, pqm->process);
 99	if (!pdd) {
100		pr_err("Process device data doesn't exist\n");
101		return -EINVAL;
102	}
103
104	/* Only allow one queue per process can have GWS assigned */
105	if (gws && pdd->qpd.num_gws)
106		return -EBUSY;
107
108	if (!gws && pdd->qpd.num_gws == 0)
109		return -EINVAL;
110
111	if (gws)
112		ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
113			gws, &mem);
114	else
115		ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
116			pqn->q->gws);
117	if (unlikely(ret))
118		return ret;
119
120	pqn->q->gws = mem;
121	pdd->qpd.num_gws = gws ? amdgpu_amdkfd_get_num_gws(dev->kgd) : 0;
122
123	return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
124							pqn->q);
125}
126
127void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
128{
129	int i;
130
131	for (i = 0; i < p->n_pdds; i++)
132		kfd_process_dequeue_from_device(p->pdds[i]);
133}
134
135int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
136{
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)
142		return -ENOMEM;
143	pqm->process = p;
144
145	return 0;
146}
147
148void pqm_uninit(struct process_queue_manager *pqm)
149{
 
150	struct process_queue_node *pqn, *next;
151
 
 
 
 
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,
155				pqn->q->gws);
156		kfd_procfs_del_queue(pqn->q);
157		uninit_queue(pqn->q);
158		list_del(&pqn->process_queue_list);
159		kfree(pqn);
 
 
 
160	}
161
162	kfree(pqm->queue_slot_bitmap);
163	pqm->queue_slot_bitmap = NULL;
164}
165
166static 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)
170{
171	int retval;
172
 
 
173	/* Doorbell initialized in user space*/
174	q_properties->doorbell_ptr = NULL;
175
 
 
 
176	/* let DQM handle it*/
177	q_properties->vmid = 0;
178	q_properties->queue_id = qid;
179
180	retval = init_queue(q, q_properties);
181	if (retval != 0)
182		return retval;
183
184	(*q)->device = dev;
185	(*q)->process = pqm->process;
186
187	pr_debug("PQM After init queue");
188
189	return retval;
 
 
 
190}
191
192int pqm_create_queue(struct process_queue_manager *pqm,
193			    struct kfd_dev *dev,
194			    struct file *f,
195			    struct queue_properties *properties,
196			    unsigned int *qid,
197			    uint32_t *p_doorbell_offset_in_process)
 
198{
199	int retval;
200	struct kfd_process_device *pdd;
 
201	struct queue *q;
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 */
206
 
 
 
 
207	q = NULL;
208	kq = NULL;
209
210	pdd = kfd_get_process_device_data(dev, pqm->process);
211	if (!pdd) {
212		pr_err("Process device data doesn't exist\n");
213		return -1;
214	}
215
216	/*
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
221	 */
222	if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
223		max_queues = dev->device_info->max_no_of_hqd/2;
224
225	if (pdd->qpd.queue_count >= max_queues)
226		return -ENOSPC;
 
 
227
228	retval = find_available_queue_slot(pqm, qid);
229	if (retval != 0)
230		return retval;
231
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);
 
235
236	pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
237	if (!pqn) {
238		retval = -ENOMEM;
239		goto err_allocate_pqn;
240	}
241
242	switch (type) {
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.
250		 */
251		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
252		if (retval != 0)
253			goto err_create_queue;
254		pqn->q = q;
255		pqn->kq = NULL;
256		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
257		print_queue(q);
258		break;
259
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");
267			retval = -EPERM;
268			goto err_create_queue;
269		}
270
271		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
272		if (retval != 0)
273			goto err_create_queue;
274		pqn->q = q;
275		pqn->kq = NULL;
276		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
 
 
277		print_queue(q);
278		break;
279	case KFD_QUEUE_TYPE_DIQ:
280		kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
281		if (!kq) {
282			retval = -ENOMEM;
283			goto err_create_queue;
284		}
285		kq->queue->properties.queue_id = *qid;
286		pqn->kq = kq;
287		pqn->q = NULL;
288		retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
289							kq, &pdd->qpd);
290		break;
291	default:
292		WARN(1, "Invalid queue type %d", type);
293		retval = -EINVAL;
294	}
295
296	if (retval != 0) {
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;
300	}
301
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
305		 * (in bytes).
306		 * There are always 1024 doorbells per process, so in case
307		 * of 8-byte doorbells, there are two doorbell pages per
308		 * process.
309		 */
310		*p_doorbell_offset_in_process =
311			(q->properties.doorbell_off * sizeof(uint32_t)) &
312			(kfd_doorbell_process_slice(dev) - 1);
313
314	pr_debug("PQM After DQM create queue\n");
315
316	list_add(&pqn->process_queue_list, &pqm->queues);
317
318	if (q) {
319		pr_debug("PQM done creating queue\n");
320		kfd_procfs_add_queue(q);
321		print_queue_properties(&q->properties);
322	}
323
324	return retval;
325
326err_create_queue:
327	uninit_queue(q);
328	if (kq)
329		kernel_queue_uninit(kq, false);
330	kfree(pqn);
331err_allocate_pqn:
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);
337	return retval;
338}
339
340int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
341{
342	struct process_queue_node *pqn;
343	struct kfd_process_device *pdd;
344	struct device_queue_manager *dqm;
345	struct kfd_dev *dev;
346	int retval;
347
348	dqm = NULL;
349
 
350	retval = 0;
351
 
 
352	pqn = get_queue_by_qid(pqm, qid);
353	if (!pqn) {
354		pr_err("Queue id does not match any known queue\n");
355		return -EINVAL;
356	}
357
358	dev = NULL;
359	if (pqn->kq)
360		dev = pqn->kq->dev;
361	if (pqn->q)
362		dev = pqn->q->device;
363	if (WARN_ON(!dev))
364		return -ENODEV;
365
366	pdd = kfd_get_process_device_data(dev, pqm->process);
367	if (!pdd) {
368		pr_err("Process device data doesn't exist\n");
369		return -1;
370	}
371
372	if (pqn->kq) {
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);
377	}
378
379	if (pqn->q) {
380		kfd_procfs_del_queue(pqn->q);
381		dqm = pqn->q->device->dqm;
382		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
383		if (retval) {
384			pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
385				pqm->process->pasid,
386				pqn->q->properties.queue_id, retval);
387			if (retval != -ETIME)
388				goto err_destroy_queue;
389		}
390
391		if (pqn->q->gws) {
392			amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
393				pqn->q->gws);
394			pdd->qpd.num_gws = 0;
395		}
396
397		kfree(pqn->q->properties.cu_mask);
398		pqn->q->properties.cu_mask = NULL;
399		uninit_queue(pqn->q);
400	}
401
402	list_del(&pqn->process_queue_list);
403	kfree(pqn);
404	clear_bit(qid, pqm->queue_slot_bitmap);
405
406	if (list_empty(&pdd->qpd.queues_list) &&
407	    list_empty(&pdd->qpd.priv_queue_list))
408		dqm->ops.unregister_process(dqm, &pdd->qpd);
409
410err_destroy_queue:
411	return retval;
412}
413
414int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
415			struct queue_properties *p)
416{
417	int retval;
418	struct process_queue_node *pqn;
419
 
 
420	pqn = get_queue_by_qid(pqm, qid);
421	if (!pqn) {
422		pr_debug("No queue %d exists for update operation\n", qid);
 
423		return -EFAULT;
424	}
425
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;
430
431	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
432							pqn->q);
433	if (retval != 0)
434		return retval;
435
436	return 0;
437}
438
439int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid,
440			struct queue_properties *p)
441{
442	int retval;
443	struct process_queue_node *pqn;
444
445	pqn = get_queue_by_qid(pqm, qid);
446	if (!pqn) {
447		pr_debug("No queue %d exists for update operation\n", qid);
448		return -EFAULT;
449	}
450
451	/* Free the old CU mask memory if it is already allocated, then
452	 * allocate memory for the new CU mask.
453	 */
454	kfree(pqn->q->properties.cu_mask);
455
456	pqn->q->properties.cu_mask_count = p->cu_mask_count;
457	pqn->q->properties.cu_mask = p->cu_mask;
458
459	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
460							pqn->q);
461	if (retval != 0)
462		return retval;
463
464	return 0;
465}
466
467struct kernel_queue *pqm_get_kernel_queue(
468					struct process_queue_manager *pqm,
469					unsigned int qid)
470{
471	struct process_queue_node *pqn;
472
 
 
473	pqn = get_queue_by_qid(pqm, qid);
474	if (pqn && pqn->kq)
475		return pqn->kq;
476
477	return NULL;
478}
479
480struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
481					unsigned int qid)
482{
483	struct process_queue_node *pqn;
484
485	pqn = get_queue_by_qid(pqm, qid);
486	return pqn ? pqn->q : NULL;
487}
488
489int pqm_get_wave_state(struct process_queue_manager *pqm,
490		       unsigned int qid,
491		       void __user *ctl_stack,
492		       u32 *ctl_stack_used_size,
493		       u32 *save_area_used_size)
494{
495	struct process_queue_node *pqn;
496
497	pqn = get_queue_by_qid(pqm, qid);
498	if (!pqn) {
499		pr_debug("amdkfd: No queue %d exists for operation\n",
500			 qid);
501		return -EFAULT;
502	}
503
504	return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
505						       pqn->q,
506						       ctl_stack,
507						       ctl_stack_used_size,
508						       save_area_used_size);
509}
510
511#if defined(CONFIG_DEBUG_FS)
512
513int pqm_debugfs_mqds(struct seq_file *m, void *data)
514{
515	struct process_queue_manager *pqm = data;
516	struct process_queue_node *pqn;
517	struct queue *q;
518	enum KFD_MQD_TYPE mqd_type;
519	struct mqd_manager *mqd_mgr;
520	int r = 0;
521
522	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
523		if (pqn->q) {
524			q = pqn->q;
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",
529					   q->device->id);
530				mqd_type = KFD_MQD_TYPE_SDMA;
531				break;
532			case KFD_QUEUE_TYPE_COMPUTE:
533				seq_printf(m, "  Compute queue on device %x\n",
534					   q->device->id);
535				mqd_type = KFD_MQD_TYPE_CP;
536				break;
537			default:
538				seq_printf(m,
539				"  Bad user queue type %d on device %x\n",
540					   q->properties.type, q->device->id);
541				continue;
542			}
543			mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
544		} else if (pqn->kq) {
545			q = pqn->kq->queue;
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",
550					   pqn->kq->dev->id);
551				break;
552			default:
553				seq_printf(m,
554				"  Bad kernel queue type %d on device %x\n",
555					   q->properties.type,
556					   pqn->kq->dev->id);
557				continue;
558			}
559		} else {
560			seq_printf(m,
561		"  Weird: Queue node with neither kernel nor user queue\n");
562			continue;
563		}
564
565		r = mqd_mgr->debugfs_show_mqd(m, q->mqd);
566		if (r != 0)
567			break;
568	}
569
570	return r;
571}
572
573#endif