xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include "kfd_device_queue_manager.h"
28 #include "kfd_priv.h"
29 #include "kfd_topology.h"
30 #include "kfd_kernel_queue.h"
31 #include "amdgpu_amdkfd.h"
32 #include "amdgpu_reset.h"
33 
34 static inline struct process_queue_node *get_queue_by_qid(
35 			struct process_queue_manager *pqm, unsigned int qid)
36 {
37 	struct process_queue_node *pqn;
38 
39 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
40 		if ((pqn->q && pqn->q->properties.queue_id == qid) ||
41 		    (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
42 			return pqn;
43 	}
44 
45 	return NULL;
46 }
47 
48 static int assign_queue_slot_by_qid(struct process_queue_manager *pqm,
49 				    unsigned int qid)
50 {
51 	if (qid >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
52 		return -EINVAL;
53 
54 	if (__test_and_set_bit(qid, pqm->queue_slot_bitmap)) {
55 		pr_err("Cannot create new queue because requested qid(%u) is in use\n", qid);
56 		return -ENOSPC;
57 	}
58 
59 	return 0;
60 }
61 
62 static int find_available_queue_slot(struct process_queue_manager *pqm,
63 					unsigned int *qid)
64 {
65 	unsigned long found;
66 
67 	found = find_first_zero_bit(pqm->queue_slot_bitmap,
68 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
69 
70 	pr_debug("The new slot id %lu\n", found);
71 
72 	if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
73 		pr_info("Cannot open more queues for process with pid %d\n",
74 			pqm->process->lead_thread->pid);
75 		return -ENOMEM;
76 	}
77 
78 	set_bit(found, pqm->queue_slot_bitmap);
79 	*qid = found;
80 
81 	return 0;
82 }
83 
84 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
85 {
86 	struct kfd_node *dev = pdd->dev;
87 
88 	if (pdd->already_dequeued)
89 		return;
90 	/* The MES context flush needs to filter out the case which the
91 	 * KFD process is created without setting up the MES context and
92 	 * queue for creating a compute queue.
93 	 */
94 	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
95 	if (dev->kfd->shared_resources.enable_mes && !!pdd->proc_ctx_gpu_addr &&
96 	    down_read_trylock(&dev->adev->reset_domain->sem)) {
97 		amdgpu_mes_flush_shader_debugger(dev->adev,
98 						 pdd->proc_ctx_gpu_addr,
99 						 ffs(pdd->dev->xcc_mask) - 1);
100 		up_read(&dev->adev->reset_domain->sem);
101 	}
102 	pdd->already_dequeued = true;
103 }
104 
105 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
106 			void *gws)
107 {
108 	struct mqd_update_info minfo = {0};
109 	struct kfd_node *dev = NULL;
110 	struct process_queue_node *pqn;
111 	struct kfd_process_device *pdd;
112 	struct kgd_mem *mem = NULL;
113 	int ret;
114 
115 	pqn = get_queue_by_qid(pqm, qid);
116 	if (!pqn) {
117 		pr_err("Queue id does not match any known queue\n");
118 		return -EINVAL;
119 	}
120 
121 	if (pqn->q)
122 		dev = pqn->q->device;
123 	if (WARN_ON(!dev))
124 		return -ENODEV;
125 
126 	pdd = kfd_get_process_device_data(dev, pqm->process);
127 	if (!pdd) {
128 		pr_err("Process device data doesn't exist\n");
129 		return -EINVAL;
130 	}
131 
132 	/* Only allow one queue per process can have GWS assigned */
133 	if (gws && pdd->qpd.num_gws)
134 		return -EBUSY;
135 
136 	if (!gws && pdd->qpd.num_gws == 0)
137 		return -EINVAL;
138 
139 	if ((KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 3) &&
140 	     KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 4) &&
141 	     KFD_GC_VERSION(dev) != IP_VERSION(9, 5, 0)) &&
142 	    !dev->kfd->shared_resources.enable_mes) {
143 		if (gws)
144 			ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
145 				gws, &mem);
146 		else
147 			ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
148 				pqn->q->gws);
149 		if (unlikely(ret))
150 			return ret;
151 		pqn->q->gws = mem;
152 	} else {
153 		/*
154 		 * Intentionally set GWS to a non-NULL value
155 		 * for devices that do not use GWS for global wave
156 		 * synchronization but require the formality
157 		 * of setting GWS for cooperative groups.
158 		 */
159 		pqn->q->gws = gws ? ERR_PTR(-ENOMEM) : NULL;
160 	}
161 
162 	pdd->qpd.num_gws = gws ? dev->adev->gds.gws_size : 0;
163 	minfo.update_flag = gws ? UPDATE_FLAG_IS_GWS : 0;
164 
165 	return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
166 							pqn->q, &minfo);
167 }
168 
169 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
170 {
171 	int i;
172 
173 	for (i = 0; i < p->n_pdds; i++)
174 		kfd_process_dequeue_from_device(p->pdds[i]);
175 }
176 
177 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
178 {
179 	INIT_LIST_HEAD(&pqm->queues);
180 	pqm->queue_slot_bitmap = bitmap_zalloc(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
181 					       GFP_KERNEL);
182 	if (!pqm->queue_slot_bitmap)
183 		return -ENOMEM;
184 	pqm->process = p;
185 
186 	return 0;
187 }
188 
189 static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
190 				     struct process_queue_node *pqn)
191 {
192 	struct kfd_node *dev;
193 	struct kfd_process_device *pdd;
194 
195 	dev = pqn->q->device;
196 
197 	pdd = kfd_get_process_device_data(dev, pqm->process);
198 	if (!pdd) {
199 		pr_err("Process device data doesn't exist\n");
200 		return;
201 	}
202 
203 	if (pqn->q->gws) {
204 		if (KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 3) &&
205 		    KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 4) &&
206 		    KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 5, 0) &&
207 		    !dev->kfd->shared_resources.enable_mes)
208 			amdgpu_amdkfd_remove_gws_from_process(
209 				pqm->process->kgd_process_info, pqn->q->gws);
210 		pdd->qpd.num_gws = 0;
211 	}
212 
213 	if (dev->kfd->shared_resources.enable_mes) {
214 		amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index);
215 		amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
216 		amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
217 	}
218 }
219 
220 void pqm_uninit(struct process_queue_manager *pqm)
221 {
222 	struct process_queue_node *pqn, *next;
223 
224 	list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
225 		if (pqn->q) {
226 			struct kfd_process_device *pdd = kfd_get_process_device_data(pqn->q->device,
227 										     pqm->process);
228 			if (pdd) {
229 				kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
230 				kfd_queue_release_buffers(pdd, &pqn->q->properties);
231 			} else {
232 				WARN_ON(!pdd);
233 			}
234 			pqm_clean_queue_resource(pqm, pqn);
235 		}
236 
237 		kfd_procfs_del_queue(pqn->q);
238 		uninit_queue(pqn->q);
239 		list_del(&pqn->process_queue_list);
240 		kfree(pqn);
241 	}
242 
243 	bitmap_free(pqm->queue_slot_bitmap);
244 	pqm->queue_slot_bitmap = NULL;
245 }
246 
247 static int init_user_queue(struct process_queue_manager *pqm,
248 				struct kfd_node *dev, struct queue **q,
249 				struct queue_properties *q_properties,
250 				unsigned int qid)
251 {
252 	int retval;
253 
254 	/* Doorbell initialized in user space*/
255 	q_properties->doorbell_ptr = NULL;
256 	q_properties->exception_status = KFD_EC_MASK(EC_QUEUE_NEW);
257 
258 	/* let DQM handle it*/
259 	q_properties->vmid = 0;
260 	q_properties->queue_id = qid;
261 
262 	retval = init_queue(q, q_properties);
263 	if (retval != 0)
264 		return retval;
265 
266 	(*q)->device = dev;
267 	(*q)->process = pqm->process;
268 
269 	if (dev->kfd->shared_resources.enable_mes) {
270 		if (!q_properties->wptr_bo) {
271 			pr_debug("Queue initialization with shared MES requires queue buffers to be initialized\n");
272 			return -EINVAL;
273 		}
274 
275 		retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
276 						AMDGPU_MES_GANG_CTX_SIZE,
277 						AMDGPU_GEM_DOMAIN_GTT,
278 						&(*q)->gang_ctx_bo,
279 						&(*q)->gang_ctx_gpu_addr,
280 						&(*q)->gang_ctx_cpu_ptr,
281 						false);
282 		if (retval) {
283 			pr_err("failed to allocate gang context bo\n");
284 			goto cleanup;
285 		}
286 		memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
287 		/* Bind one MES gang context slot per queue (gang). */
288 		if (dev->adev->mes.use_rs64mem) {
289 			retval = amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
290 						&(*q)->gang_ctx_array_index);
291 			if (retval) {
292 				pr_err("failed to allocate gang context index slot\n");
293 				goto cleanup;
294 			}
295 		}
296 		/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
297 		 * on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
298 		 */
299 		if (dev->adev != amdgpu_ttm_adev(q_properties->wptr_bo->tbo.bdev)) {
300 			pr_err("Queue memory allocated to wrong device\n");
301 			retval = -EINVAL;
302 			goto free_gang_ctx_bo;
303 		}
304 
305 		retval = amdgpu_amdkfd_map_gtt_bo_to_gart(q_properties->wptr_bo,
306 							  &(*q)->wptr_bo_gart);
307 		if (retval) {
308 			pr_err("Failed to map wptr bo to GART\n");
309 			goto free_gang_ctx_bo;
310 		}
311 	}
312 
313 	pr_debug("PQM After init queue");
314 	return 0;
315 
316 free_gang_ctx_bo:
317 	amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
318 	amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
319 cleanup:
320 	uninit_queue(*q);
321 	*q = NULL;
322 	return retval;
323 }
324 
325 int pqm_create_queue(struct process_queue_manager *pqm,
326 			    struct kfd_node *dev,
327 			    struct queue_properties *properties,
328 			    unsigned int *qid,
329 			    const struct kfd_criu_queue_priv_data *q_data,
330 			    const void *restore_mqd,
331 			    const void *restore_ctl_stack,
332 			    uint32_t *p_doorbell_offset_in_process)
333 {
334 	int retval;
335 	struct kfd_process_device *pdd;
336 	struct queue *q;
337 	struct process_queue_node *pqn;
338 	struct kernel_queue *kq;
339 	enum kfd_queue_type type = properties->type;
340 	unsigned int max_queues = 127; /* HWS limit */
341 
342 	/*
343 	 * On GFX 9.4.3/9.5.0/12.1.0, increase the number of queues that
344 	 * can be created to 255. No HWS limit on GFX 9.4.3/9.5.0/12.1.0.
345 	 */
346 	if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3) ||
347 	    KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4) ||
348 	    KFD_GC_VERSION(dev) == IP_VERSION(9, 5, 0) ||
349 	    KFD_GC_VERSION(dev) == IP_VERSION(12, 1, 0))
350 		max_queues = 255;
351 
352 	q = NULL;
353 	kq = NULL;
354 
355 	pdd = kfd_get_process_device_data(dev, pqm->process);
356 	if (!pdd) {
357 		pr_err("Process device data doesn't exist\n");
358 		return -1;
359 	}
360 
361 	/*
362 	 * for debug process, verify that it is within the static queues limit
363 	 * currently limit is set to half of the total avail HQD slots
364 	 * If we are just about to create DIQ, the is_debug flag is not set yet
365 	 * Hence we also check the type as well
366 	 */
367 	if (pdd->qpd.is_debug)
368 		max_queues = dev->kfd->device_info.max_no_of_hqd/2;
369 
370 	if (pdd->qpd.queue_count >= max_queues)
371 		return -ENOSPC;
372 
373 	if (q_data) {
374 		retval = assign_queue_slot_by_qid(pqm, q_data->q_id);
375 		*qid = q_data->q_id;
376 	} else
377 		retval = find_available_queue_slot(pqm, qid);
378 
379 	if (retval != 0)
380 		return retval;
381 
382 	/* Register process if this is the first queue */
383 	if (list_empty(&pdd->qpd.queues_list) &&
384 	    list_empty(&pdd->qpd.priv_queue_list))
385 		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
386 
387 	/* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */
388 	if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
389 		retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
390 						     AMDGPU_MES_PROC_CTX_SIZE,
391 						     AMDGPU_GEM_DOMAIN_GTT,
392 						     &pdd->proc_ctx_bo,
393 						     &pdd->proc_ctx_gpu_addr,
394 						     &pdd->proc_ctx_cpu_ptr,
395 						     false);
396 		if (retval) {
397 			dev_err(dev->adev->dev, "failed to allocate process context bo\n");
398 			goto err_allocate_pqn;
399 		}
400 		memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
401 		/* Bind one MES process context slot to the whole process
402 		 * (per device); every queue of this process reuses it.
403 		 */
404 		if (dev->adev->mes.use_rs64mem) {
405 			retval = amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
406 						&pdd->proc_ctx_array_index);
407 			if (retval) {
408 				dev_err(dev->adev->dev, "failed to allocate process context index\n");
409 				goto err_allocate_pqn;
410 			}
411 		}
412 	}
413 
414 	pqn = kzalloc_obj(*pqn);
415 	if (!pqn) {
416 		retval = -ENOMEM;
417 		goto err_allocate_pqn;
418 	}
419 
420 	switch (type) {
421 	case KFD_QUEUE_TYPE_SDMA:
422 	case KFD_QUEUE_TYPE_SDMA_XGMI:
423 	case KFD_QUEUE_TYPE_SDMA_BY_ENG_ID:
424 		/* SDMA queues are always allocated statically no matter
425 		 * which scheduler mode is used. We also do not need to
426 		 * check whether a SDMA queue can be allocated here, because
427 		 * allocate_sdma_queue() in create_queue() has the
428 		 * corresponding check logic.
429 		 */
430 		retval = init_user_queue(pqm, dev, &q, properties, *qid);
431 		if (retval != 0)
432 			goto err_create_queue;
433 		pqn->q = q;
434 		pqn->kq = NULL;
435 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
436 						    restore_mqd, restore_ctl_stack);
437 		print_queue(q);
438 		break;
439 
440 	case KFD_QUEUE_TYPE_COMPUTE:
441 		/* check if there is over subscription */
442 		if ((dev->dqm->sched_policy ==
443 		     KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
444 		((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
445 		(dev->dqm->active_queue_count >= get_cp_queues_num(dev->dqm)))) {
446 			pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
447 			retval = -EPERM;
448 			goto err_create_queue;
449 		}
450 
451 		retval = init_user_queue(pqm, dev, &q, properties, *qid);
452 		if (retval != 0)
453 			goto err_create_queue;
454 		pqn->q = q;
455 		pqn->kq = NULL;
456 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
457 						    restore_mqd, restore_ctl_stack);
458 		print_queue(q);
459 		break;
460 	default:
461 		WARN(1, "Invalid queue type %d", type);
462 		retval = -EINVAL;
463 	}
464 
465 	if (retval != 0) {
466 		if ((type == KFD_QUEUE_TYPE_SDMA ||
467 		    type == KFD_QUEUE_TYPE_SDMA_XGMI ||
468 		    type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) &&
469 		    retval == -ENOMEM)
470 			pr_warn("process pid %d DQM create queue type %d failed. ret %d\n",
471 				pqm->process->lead_thread->pid, type, retval);
472 		else
473 			pr_err("process pid %d DQM create queue type %d failed. ret %d\n",
474 				pqm->process->lead_thread->pid, type, retval);
475 		goto err_create_queue;
476 	}
477 
478 	if (q && p_doorbell_offset_in_process) {
479 		/* Return the doorbell offset within the doorbell page
480 		 * to the caller so it can be passed up to user mode
481 		 * (in bytes).
482 		 * relative doorbell index = Absolute doorbell index -
483 		 * absolute index of first doorbell in the page.
484 		 */
485 		uint32_t first_db_index = amdgpu_doorbell_index_on_bar(pdd->dev->adev,
486 								       pdd->qpd.proc_doorbells,
487 								       0,
488 								       pdd->dev->kfd->device_info.doorbell_size);
489 
490 		*p_doorbell_offset_in_process = (q->properties.doorbell_off
491 						- first_db_index) * sizeof(uint32_t);
492 	}
493 
494 	pr_debug("PQM After DQM create queue\n");
495 
496 	list_add(&pqn->process_queue_list, &pqm->queues);
497 
498 	if (q) {
499 		pr_debug("PQM done creating queue\n");
500 		kfd_procfs_add_queue(q);
501 		print_queue_properties(&q->properties);
502 	}
503 
504 	return retval;
505 
506 err_create_queue:
507 	uninit_queue(q);
508 	if (kq)
509 		kernel_queue_uninit(kq);
510 	kfree(pqn);
511 err_allocate_pqn:
512 	/* check if queues list is empty unregister process from device */
513 	clear_bit(*qid, pqm->queue_slot_bitmap);
514 	if (list_empty(&pdd->qpd.queues_list) &&
515 	    list_empty(&pdd->qpd.priv_queue_list))
516 		dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
517 	return retval;
518 }
519 
520 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
521 {
522 	struct process_queue_node *pqn;
523 	struct kfd_process_device *pdd;
524 	struct device_queue_manager *dqm;
525 	struct kfd_node *dev;
526 	int retval;
527 
528 	dqm = NULL;
529 
530 	retval = 0;
531 
532 	pqn = get_queue_by_qid(pqm, qid);
533 	if (!pqn) {
534 		pr_err("Queue id does not match any known queue\n");
535 		return -EINVAL;
536 	}
537 
538 	dev = NULL;
539 	if (pqn->kq)
540 		dev = pqn->kq->dev;
541 	if (pqn->q)
542 		dev = pqn->q->device;
543 	if (WARN_ON(!dev))
544 		return -ENODEV;
545 
546 	pdd = kfd_get_process_device_data(dev, pqm->process);
547 	if (!pdd) {
548 		pr_err("Process device data doesn't exist\n");
549 		return -1;
550 	}
551 
552 	if (pqn->kq) {
553 		/* destroy kernel queue (DIQ) */
554 		dqm = pqn->kq->dev->dqm;
555 		dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
556 		kernel_queue_uninit(pqn->kq);
557 	}
558 
559 	if (pqn->q) {
560 		retval = kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
561 		if (retval)
562 			goto err_destroy_queue;
563 
564 		dqm = pqn->q->device->dqm;
565 		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
566 		if (retval) {
567 			pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
568 				pdd->pasid,
569 				pqn->q->properties.queue_id, retval);
570 			if (retval != -ETIME && retval != -EIO)
571 				goto err_destroy_queue;
572 		}
573 		kfd_procfs_del_queue(pqn->q);
574 		kfd_queue_release_buffers(pdd, &pqn->q->properties);
575 		pqm_clean_queue_resource(pqm, pqn);
576 		uninit_queue(pqn->q);
577 	}
578 
579 	list_del(&pqn->process_queue_list);
580 	kfree(pqn);
581 	clear_bit(qid, pqm->queue_slot_bitmap);
582 
583 	if (list_empty(&pdd->qpd.queues_list) &&
584 	    list_empty(&pdd->qpd.priv_queue_list))
585 		dqm->ops.unregister_process(dqm, &pdd->qpd);
586 
587 err_destroy_queue:
588 	return retval;
589 }
590 
591 int pqm_update_queue_properties(struct process_queue_manager *pqm,
592 				unsigned int qid, struct queue_properties *p)
593 {
594 	int retval;
595 	struct process_queue_node *pqn;
596 
597 	pqn = get_queue_by_qid(pqm, qid);
598 	if (!pqn || !pqn->q) {
599 		pr_debug("No queue %d exists for update operation\n", qid);
600 		return -EFAULT;
601 	}
602 
603 	/*
604 	 * Update with NULL ring address is used to disable the queue
605 	 */
606 	if (p->queue_address && p->queue_size) {
607 		struct kfd_process_device *pdd;
608 		struct amdgpu_vm *vm;
609 		struct queue *q = pqn->q;
610 		int err;
611 
612 		pdd = kfd_get_process_device_data(q->device, q->process);
613 		if (!pdd)
614 			return -ENODEV;
615 		vm = drm_priv_to_vm(pdd->drm_priv);
616 		err = amdgpu_bo_reserve(vm->root.bo, false);
617 		if (err)
618 			return err;
619 
620 		if (kfd_queue_buffer_get(vm, (void *)p->queue_address, &p->ring_bo,
621 					 p->queue_size +
622 					 pqn->q->properties.metadata_queue_size)) {
623 			pr_debug("ring buf 0x%llx size 0x%llx not mapped on GPU\n",
624 				 p->queue_address, p->queue_size);
625 			amdgpu_bo_unreserve(vm->root.bo);
626 			return -EFAULT;
627 		}
628 
629 		kfd_queue_unref_bo_va(vm, &pqn->q->properties.ring_bo);
630 		kfd_queue_buffer_put(&pqn->q->properties.ring_bo);
631 		amdgpu_bo_unreserve(vm->root.bo);
632 
633 		pqn->q->properties.ring_bo = p->ring_bo;
634 	}
635 
636 	pqn->q->properties.queue_address = p->queue_address;
637 	pqn->q->properties.queue_size = p->queue_size;
638 	pqn->q->properties.queue_percent = p->queue_percent;
639 	pqn->q->properties.priority = p->priority;
640 	pqn->q->properties.pm4_target_xcc = p->pm4_target_xcc;
641 
642 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
643 							pqn->q, NULL);
644 	if (retval != 0)
645 		return retval;
646 
647 	return 0;
648 }
649 
650 int pqm_update_mqd(struct process_queue_manager *pqm,
651 				unsigned int qid, struct mqd_update_info *minfo)
652 {
653 	int retval;
654 	struct process_queue_node *pqn;
655 
656 	pqn = get_queue_by_qid(pqm, qid);
657 	if (!pqn) {
658 		pr_debug("No queue %d exists for update operation\n", qid);
659 		return -EFAULT;
660 	}
661 
662 	/* CUs are masked for debugger requirements so deny user mask  */
663 	if (pqn->q->properties.is_dbg_wa && minfo && minfo->cu_mask.ptr)
664 		return -EBUSY;
665 
666 	/* ASICs that have WGPs must enforce pairwise enabled mask checks. */
667 	if (minfo && minfo->cu_mask.ptr &&
668 			KFD_GC_VERSION(pqn->q->device) >= IP_VERSION(10, 0, 0) &&
669 			KFD_GC_VERSION(pqn->q->device) < IP_VERSION(12, 1, 0)) {
670 		int i;
671 
672 		for (i = 0; i < minfo->cu_mask.count; i += 2) {
673 			uint32_t cu_pair = (minfo->cu_mask.ptr[i / 32] >> (i % 32)) & 0x3;
674 
675 			if (cu_pair && cu_pair != 0x3) {
676 				pr_debug("CUs must be adjacent pairwise enabled.\n");
677 				return -EINVAL;
678 			}
679 		}
680 	}
681 
682 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
683 							pqn->q, minfo);
684 	if (retval != 0)
685 		return retval;
686 
687 	if (minfo && minfo->cu_mask.ptr)
688 		pqn->q->properties.is_user_cu_masked = true;
689 
690 	return 0;
691 }
692 
693 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
694 					unsigned int qid)
695 {
696 	struct process_queue_node *pqn;
697 
698 	pqn = get_queue_by_qid(pqm, qid);
699 	return pqn ? pqn->q : NULL;
700 }
701 
702 int pqm_get_wave_state(struct process_queue_manager *pqm,
703 		       unsigned int qid,
704 		       void __user *ctl_stack,
705 		       u32 *ctl_stack_used_size,
706 		       u32 *save_area_used_size)
707 {
708 	struct process_queue_node *pqn;
709 
710 	pqn = get_queue_by_qid(pqm, qid);
711 	if (!pqn) {
712 		pr_debug("amdkfd: No queue %d exists for operation\n",
713 			 qid);
714 		return -EFAULT;
715 	}
716 
717 	return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
718 						       pqn->q,
719 						       ctl_stack,
720 						       ctl_stack_used_size,
721 						       save_area_used_size);
722 }
723 
724 int pqm_get_queue_snapshot(struct process_queue_manager *pqm,
725 			   uint64_t exception_clear_mask,
726 			   void __user *buf,
727 			   int *num_qss_entries,
728 			   uint32_t *entry_size)
729 {
730 	struct process_queue_node *pqn;
731 	struct kfd_queue_snapshot_entry src;
732 	uint32_t tmp_entry_size = *entry_size, tmp_qss_entries = *num_qss_entries;
733 	int r = 0;
734 
735 	*num_qss_entries = 0;
736 	if (!(*entry_size))
737 		return -EINVAL;
738 
739 	*entry_size = min_t(size_t, *entry_size, sizeof(struct kfd_queue_snapshot_entry));
740 	mutex_lock(&pqm->process->event_mutex);
741 
742 	memset(&src, 0, sizeof(src));
743 
744 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
745 		if (!pqn->q)
746 			continue;
747 
748 		if (*num_qss_entries < tmp_qss_entries) {
749 			set_queue_snapshot_entry(pqn->q, exception_clear_mask, &src);
750 
751 			if (copy_to_user(buf, &src, *entry_size)) {
752 				r = -EFAULT;
753 				break;
754 			}
755 			buf += tmp_entry_size;
756 		}
757 		*num_qss_entries += 1;
758 	}
759 
760 	mutex_unlock(&pqm->process->event_mutex);
761 	return r;
762 }
763 
764 static int get_queue_data_sizes(struct kfd_process_device *pdd,
765 				struct queue *q,
766 				uint32_t *mqd_size,
767 				uint32_t *ctl_stack_size)
768 {
769 	int ret;
770 
771 	ret = pqm_get_queue_checkpoint_info(&pdd->process->pqm,
772 					    q->properties.queue_id,
773 					    mqd_size,
774 					    ctl_stack_size);
775 	if (ret)
776 		pr_err("Failed to get queue dump info (%d)\n", ret);
777 
778 	return ret;
779 }
780 
781 int kfd_process_get_queue_info(struct kfd_process *p,
782 			       uint32_t *num_queues,
783 			       uint64_t *priv_data_sizes)
784 {
785 	uint32_t extra_data_sizes = 0;
786 	struct queue *q;
787 	int i;
788 	int ret;
789 
790 	*num_queues = 0;
791 
792 	/* Run over all PDDs of the process */
793 	for (i = 0; i < p->n_pdds; i++) {
794 		struct kfd_process_device *pdd = p->pdds[i];
795 
796 		list_for_each_entry(q, &pdd->qpd.queues_list, list) {
797 			if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
798 				q->properties.type == KFD_QUEUE_TYPE_SDMA ||
799 				q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
800 				uint32_t mqd_size, ctl_stack_size;
801 
802 				*num_queues = *num_queues + 1;
803 
804 				ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
805 				if (ret)
806 					return ret;
807 
808 				extra_data_sizes += mqd_size + ctl_stack_size;
809 			} else {
810 				pr_err("Unsupported queue type (%d)\n", q->properties.type);
811 				return -EOPNOTSUPP;
812 			}
813 		}
814 	}
815 	*priv_data_sizes = extra_data_sizes +
816 				(*num_queues * sizeof(struct kfd_criu_queue_priv_data));
817 
818 	return 0;
819 }
820 
821 static int pqm_checkpoint_mqd(struct process_queue_manager *pqm,
822 			      unsigned int qid,
823 			      void *mqd,
824 			      void *ctl_stack)
825 {
826 	struct process_queue_node *pqn;
827 
828 	pqn = get_queue_by_qid(pqm, qid);
829 	if (!pqn) {
830 		pr_debug("amdkfd: No queue %d exists for operation\n", qid);
831 		return -EFAULT;
832 	}
833 
834 	if (!pqn->q->device->dqm->ops.checkpoint_mqd) {
835 		pr_err("amdkfd: queue dumping not supported on this device\n");
836 		return -EOPNOTSUPP;
837 	}
838 
839 	return pqn->q->device->dqm->ops.checkpoint_mqd(pqn->q->device->dqm,
840 						       pqn->q, mqd, ctl_stack);
841 }
842 
843 static int criu_checkpoint_queue(struct kfd_process_device *pdd,
844 			   struct queue *q,
845 			   struct kfd_criu_queue_priv_data *q_data)
846 {
847 	uint8_t *mqd, *ctl_stack;
848 	int ret;
849 
850 	mqd = (void *)(q_data + 1);
851 	ctl_stack = mqd + q_data->mqd_size;
852 
853 	q_data->gpu_id = pdd->user_gpu_id;
854 	q_data->type = q->properties.type;
855 	q_data->format = q->properties.format;
856 	q_data->q_id =  q->properties.queue_id;
857 	q_data->q_address = q->properties.queue_address;
858 	q_data->q_size = q->properties.queue_size;
859 	q_data->priority = q->properties.priority;
860 	q_data->q_percent = q->properties.queue_percent;
861 	q_data->read_ptr_addr = (uint64_t)q->properties.read_ptr;
862 	q_data->write_ptr_addr = (uint64_t)q->properties.write_ptr;
863 	q_data->doorbell_id = q->doorbell_id;
864 
865 	q_data->sdma_id = q->sdma_id;
866 
867 	q_data->eop_ring_buffer_address =
868 		q->properties.eop_ring_buffer_address;
869 
870 	q_data->eop_ring_buffer_size = q->properties.eop_ring_buffer_size;
871 
872 	q_data->ctx_save_restore_area_address =
873 		q->properties.ctx_save_restore_area_address;
874 
875 	q_data->ctx_save_restore_area_size =
876 		q->properties.ctx_save_restore_area_size;
877 
878 	q_data->gws = !!q->gws;
879 
880 	ret = pqm_checkpoint_mqd(&pdd->process->pqm, q->properties.queue_id, mqd, ctl_stack);
881 	if (ret) {
882 		pr_err("Failed checkpoint queue_mqd (%d)\n", ret);
883 		return ret;
884 	}
885 
886 	pr_debug("Dumping Queue: gpu_id:%x queue_id:%u\n", q_data->gpu_id, q_data->q_id);
887 	return ret;
888 }
889 
890 static int criu_checkpoint_queues_device(struct kfd_process_device *pdd,
891 				   uint8_t __user *user_priv,
892 				   unsigned int *q_index,
893 				   uint64_t *queues_priv_data_offset)
894 {
895 	unsigned int q_private_data_size = 0;
896 	uint8_t *q_private_data = NULL; /* Local buffer to store individual queue private data */
897 	struct queue *q;
898 	int ret = 0;
899 
900 	list_for_each_entry(q, &pdd->qpd.queues_list, list) {
901 		struct kfd_criu_queue_priv_data *q_data;
902 		uint64_t q_data_size;
903 		uint32_t mqd_size;
904 		uint32_t ctl_stack_size;
905 
906 		if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE &&
907 			q->properties.type != KFD_QUEUE_TYPE_SDMA &&
908 			q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI) {
909 
910 			pr_err("Unsupported queue type (%d)\n", q->properties.type);
911 			ret = -EOPNOTSUPP;
912 			break;
913 		}
914 
915 		ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
916 		if (ret)
917 			break;
918 
919 		q_data_size = sizeof(*q_data) + mqd_size + ctl_stack_size;
920 
921 		/* Increase local buffer space if needed */
922 		if (q_private_data_size < q_data_size) {
923 			kfree(q_private_data);
924 
925 			q_private_data = kzalloc(q_data_size, GFP_KERNEL);
926 			if (!q_private_data) {
927 				ret = -ENOMEM;
928 				break;
929 			}
930 			q_private_data_size = q_data_size;
931 		}
932 
933 		q_data = (struct kfd_criu_queue_priv_data *)q_private_data;
934 
935 		/*
936 		 * data stored in this order:
937 		 * priv_data, mqd[xcc0], mqd[xcc1],..., ctl_stack[xcc0], ctl_stack[xcc1]...
938 		 */
939 		q_data->mqd_size = mqd_size;
940 		q_data->ctl_stack_size = ctl_stack_size;
941 
942 		ret = criu_checkpoint_queue(pdd, q, q_data);
943 		if (ret)
944 			break;
945 
946 		q_data->object_type = KFD_CRIU_OBJECT_TYPE_QUEUE;
947 
948 		ret = copy_to_user(user_priv + *queues_priv_data_offset,
949 				q_data, q_data_size);
950 		if (ret) {
951 			ret = -EFAULT;
952 			break;
953 		}
954 		*queues_priv_data_offset += q_data_size;
955 		*q_index = *q_index + 1;
956 	}
957 
958 	kfree(q_private_data);
959 
960 	return ret;
961 }
962 
963 int kfd_criu_checkpoint_queues(struct kfd_process *p,
964 			 uint8_t __user *user_priv_data,
965 			 uint64_t *priv_data_offset)
966 {
967 	int ret = 0, pdd_index, q_index = 0;
968 
969 	for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
970 		struct kfd_process_device *pdd = p->pdds[pdd_index];
971 
972 		/*
973 		 * criu_checkpoint_queues_device will copy data to user and update q_index and
974 		 * queues_priv_data_offset
975 		 */
976 		ret = criu_checkpoint_queues_device(pdd, user_priv_data, &q_index,
977 					      priv_data_offset);
978 
979 		if (ret)
980 			break;
981 	}
982 
983 	return ret;
984 }
985 
986 static void set_queue_properties_from_criu(struct queue_properties *qp,
987 					  struct kfd_criu_queue_priv_data *q_data)
988 {
989 	qp->is_interop = false;
990 	qp->queue_percent = q_data->q_percent;
991 	qp->priority = q_data->priority;
992 	qp->queue_address = q_data->q_address;
993 	qp->queue_size = q_data->q_size;
994 	qp->read_ptr = (void __user *)q_data->read_ptr_addr;
995 	qp->write_ptr = (void __user *)q_data->write_ptr_addr;
996 	qp->eop_ring_buffer_address = q_data->eop_ring_buffer_address;
997 	qp->eop_ring_buffer_size = q_data->eop_ring_buffer_size;
998 	qp->ctx_save_restore_area_address = q_data->ctx_save_restore_area_address;
999 	qp->ctx_save_restore_area_size = q_data->ctx_save_restore_area_size;
1000 	qp->type = q_data->type;
1001 	qp->format = q_data->format;
1002 }
1003 
1004 int kfd_criu_restore_queue(struct kfd_process *p,
1005 			   uint8_t __user *user_priv_ptr,
1006 			   uint64_t *priv_data_offset,
1007 			   uint64_t max_priv_data_size)
1008 {
1009 	uint8_t *mqd, *ctl_stack, *q_extra_data = NULL;
1010 	struct kfd_criu_queue_priv_data *q_data;
1011 	struct kfd_topology_device *topo_dev;
1012 	struct kfd_process_device *pdd;
1013 	uint64_t q_extra_data_size;
1014 	struct queue_properties qp;
1015 	unsigned int queue_id;
1016 	int ret = 0;
1017 
1018 	if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size)
1019 		return -EINVAL;
1020 
1021 	q_data = kmalloc_obj(*q_data);
1022 	if (!q_data)
1023 		return -ENOMEM;
1024 
1025 	ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data));
1026 	if (ret) {
1027 		ret = -EFAULT;
1028 		goto exit;
1029 	}
1030 
1031 	pdd = kfd_process_device_data_by_id(p, q_data->gpu_id);
1032 	if (!pdd) {
1033 		pr_err("Failed to get pdd\n");
1034 		ret = -EINVAL;
1035 		goto exit;
1036 	}
1037 
1038 	if (q_data->type >= KFD_QUEUE_TYPE_MAX) {
1039 		ret = -EINVAL;
1040 		goto exit;
1041 	}
1042 
1043 	if (q_data->mqd_size != mqd_size_from_queue_type(pdd->dev->dqm, q_data->type)) {
1044 		ret = -EINVAL;
1045 		goto exit;
1046 	}
1047 
1048 	memset(&qp, 0, sizeof(qp));
1049 	set_queue_properties_from_criu(&qp, q_data);
1050 
1051 	/* Per-XCC control stack size from topology, as create-queue requires */
1052 	if (qp.type == KFD_QUEUE_TYPE_COMPUTE && pdd->dev->kfd->cwsr_enabled) {
1053 		topo_dev = kfd_topology_device_by_id(pdd->dev->id);
1054 		if (!topo_dev) {
1055 			ret = -EINVAL;
1056 			goto exit;
1057 		}
1058 		qp.ctl_stack_size = topo_dev->node_props.ctl_stack_size;
1059 	}
1060 
1061 	if (q_data->ctl_stack_size > (u64)qp.ctl_stack_size * NUM_XCC(pdd->dev->xcc_mask)) {
1062 		ret = -EINVAL;
1063 		goto exit;
1064 	}
1065 
1066 	*priv_data_offset += sizeof(*q_data);
1067 	q_extra_data_size = (uint64_t)q_data->ctl_stack_size + q_data->mqd_size;
1068 
1069 	if (*priv_data_offset + q_extra_data_size > max_priv_data_size) {
1070 		ret = -EINVAL;
1071 		goto exit;
1072 	}
1073 
1074 	q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL);
1075 	if (!q_extra_data) {
1076 		ret = -ENOMEM;
1077 		goto exit;
1078 	}
1079 
1080 	ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size);
1081 	if (ret) {
1082 		ret = -EFAULT;
1083 		goto exit;
1084 	}
1085 
1086 	*priv_data_offset += q_extra_data_size;
1087 
1088 	/*
1089 	 * data stored in this order:
1090 	 * mqd[xcc0], mqd[xcc1],..., ctl_stack[xcc0], ctl_stack[xcc1]...
1091 	 */
1092 	mqd = q_extra_data;
1093 	ctl_stack = mqd + q_data->mqd_size;
1094 
1095 	print_queue_properties(&qp);
1096 
1097 	ret = pqm_create_queue(&p->pqm, pdd->dev, &qp, &queue_id, q_data, mqd, ctl_stack, NULL);
1098 	if (ret) {
1099 		pr_err("Failed to create new queue err:%d\n", ret);
1100 		goto exit;
1101 	}
1102 
1103 	if (q_data->gws)
1104 		ret = pqm_set_gws(&p->pqm, q_data->q_id, pdd->dev->gws);
1105 
1106 exit:
1107 	if (ret)
1108 		pr_err("Failed to restore queue (%d)\n", ret);
1109 	else
1110 		pr_debug("Queue id %d was restored successfully\n", queue_id);
1111 
1112 	kfree(q_data);
1113 	kfree(q_extra_data);
1114 
1115 	return ret;
1116 }
1117 
1118 int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
1119 				  unsigned int qid,
1120 				  uint32_t *mqd_size,
1121 				  uint32_t *ctl_stack_size)
1122 {
1123 	struct process_queue_node *pqn;
1124 	int ret;
1125 
1126 	pqn = get_queue_by_qid(pqm, qid);
1127 	if (!pqn) {
1128 		pr_debug("amdkfd: No queue %d exists for operation\n", qid);
1129 		return -EFAULT;
1130 	}
1131 
1132 	if (!pqn->q->device->dqm->ops.get_queue_checkpoint_info) {
1133 		pr_err("amdkfd: queue dumping not supported on this device\n");
1134 		return -EOPNOTSUPP;
1135 	}
1136 
1137 	ret = pqn->q->device->dqm->ops.get_queue_checkpoint_info(pqn->q->device->dqm,
1138 						       pqn->q, mqd_size,
1139 						       ctl_stack_size);
1140 	if (ret) {
1141 		pr_debug("amdkfd: Overflow while computing stack size for queue %d\n", qid);
1142 		return ret;
1143 	}
1144 
1145 	return 0;
1146 }
1147 
1148 #if defined(CONFIG_DEBUG_FS)
1149 
1150 int pqm_debugfs_mqds(struct seq_file *m, void *data)
1151 {
1152 	struct process_queue_manager *pqm = data;
1153 	struct process_queue_node *pqn;
1154 	struct queue *q;
1155 	enum KFD_MQD_TYPE mqd_type;
1156 	struct mqd_manager *mqd_mgr;
1157 	int r = 0, xcc, num_xccs = 1;
1158 	void *mqd;
1159 	uint64_t size = 0;
1160 
1161 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
1162 		if (pqn->q) {
1163 			q = pqn->q;
1164 			switch (q->properties.type) {
1165 			case KFD_QUEUE_TYPE_SDMA:
1166 			case KFD_QUEUE_TYPE_SDMA_XGMI:
1167 				seq_printf(m, "  SDMA queue on device %x\n",
1168 					   q->device->id);
1169 				mqd_type = KFD_MQD_TYPE_SDMA;
1170 				break;
1171 			case KFD_QUEUE_TYPE_COMPUTE:
1172 				seq_printf(m, "  Compute queue on device %x\n",
1173 					   q->device->id);
1174 				mqd_type = KFD_MQD_TYPE_CP;
1175 				num_xccs = NUM_XCC(q->device->xcc_mask);
1176 				break;
1177 			default:
1178 				seq_printf(m,
1179 				"  Queue node with bad user queue type %d on device %x\n",
1180 					   q->properties.type, q->device->id);
1181 				continue;
1182 			}
1183 			mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
1184 			size = mqd_mgr->mqd_stride(mqd_mgr,
1185 							&q->properties);
1186 
1187 			for (xcc = 0; xcc < num_xccs; xcc++) {
1188 				mqd = q->mqd + size * xcc;
1189 				r = mqd_mgr->debugfs_show_mqd(m, mqd);
1190 				if (r != 0)
1191 					break;
1192 			}
1193 		}
1194 	}
1195 
1196 	return r;
1197 }
1198 
1199 #endif
1200