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