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_kernel_queue.h"
30 #include "amdgpu_amdkfd.h"
31 
get_queue_by_qid(struct process_queue_manager * pqm,unsigned int qid)32 static inline struct process_queue_node *get_queue_by_qid(
33 			struct process_queue_manager *pqm, unsigned int qid)
34 {
35 	struct process_queue_node *pqn;
36 
37 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
38 		if ((pqn->q && pqn->q->properties.queue_id == qid) ||
39 		    (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
40 			return pqn;
41 	}
42 
43 	return NULL;
44 }
45 
assign_queue_slot_by_qid(struct process_queue_manager * pqm,unsigned int qid)46 static int assign_queue_slot_by_qid(struct process_queue_manager *pqm,
47 				    unsigned int qid)
48 {
49 	if (qid >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
50 		return -EINVAL;
51 
52 	if (__test_and_set_bit(qid, pqm->queue_slot_bitmap)) {
53 		pr_err("Cannot create new queue because requested qid(%u) is in use\n", qid);
54 		return -ENOSPC;
55 	}
56 
57 	return 0;
58 }
59 
find_available_queue_slot(struct process_queue_manager * pqm,unsigned int * qid)60 static int find_available_queue_slot(struct process_queue_manager *pqm,
61 					unsigned int *qid)
62 {
63 	unsigned long found;
64 
65 	found = find_first_zero_bit(pqm->queue_slot_bitmap,
66 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
67 
68 	pr_debug("The new slot id %lu\n", found);
69 
70 	if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
71 		pr_info("Cannot open more queues for process with pasid 0x%x\n",
72 				pqm->process->pasid);
73 		return -ENOMEM;
74 	}
75 
76 	set_bit(found, pqm->queue_slot_bitmap);
77 	*qid = found;
78 
79 	return 0;
80 }
81 
kfd_process_dequeue_from_device(struct kfd_process_device * pdd)82 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
83 {
84 	struct kfd_dev *dev = pdd->dev;
85 
86 	if (pdd->already_dequeued)
87 		return;
88 
89 	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
90 	pdd->already_dequeued = true;
91 }
92 
pqm_set_gws(struct process_queue_manager * pqm,unsigned int qid,void * gws)93 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
94 			void *gws)
95 {
96 	struct kfd_dev *dev = NULL;
97 	struct process_queue_node *pqn;
98 	struct kfd_process_device *pdd;
99 	struct kgd_mem *mem = NULL;
100 	int ret;
101 
102 	pqn = get_queue_by_qid(pqm, qid);
103 	if (!pqn) {
104 		pr_err("Queue id does not match any known queue\n");
105 		return -EINVAL;
106 	}
107 
108 	if (pqn->q)
109 		dev = pqn->q->device;
110 	if (WARN_ON(!dev))
111 		return -ENODEV;
112 
113 	pdd = kfd_get_process_device_data(dev, pqm->process);
114 	if (!pdd) {
115 		pr_err("Process device data doesn't exist\n");
116 		return -EINVAL;
117 	}
118 
119 	/* Only allow one queue per process can have GWS assigned */
120 	if (gws && pdd->qpd.num_gws)
121 		return -EBUSY;
122 
123 	if (!gws && pdd->qpd.num_gws == 0)
124 		return -EINVAL;
125 
126 	if (gws)
127 		ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
128 			gws, &mem);
129 	else
130 		ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
131 			pqn->q->gws);
132 	if (unlikely(ret))
133 		return ret;
134 
135 	pqn->q->gws = mem;
136 	pdd->qpd.num_gws = gws ? dev->adev->gds.gws_size : 0;
137 
138 	return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
139 							pqn->q, NULL);
140 }
141 
kfd_process_dequeue_from_all_devices(struct kfd_process * p)142 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
143 {
144 	int i;
145 
146 	for (i = 0; i < p->n_pdds; i++)
147 		kfd_process_dequeue_from_device(p->pdds[i]);
148 }
149 
pqm_init(struct process_queue_manager * pqm,struct kfd_process * p)150 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
151 {
152 	INIT_LIST_HEAD(&pqm->queues);
153 	pqm->queue_slot_bitmap = bitmap_zalloc(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
154 					       GFP_KERNEL);
155 	if (!pqm->queue_slot_bitmap)
156 		return -ENOMEM;
157 	pqm->process = p;
158 
159 	return 0;
160 }
161 
pqm_uninit(struct process_queue_manager * pqm)162 void pqm_uninit(struct process_queue_manager *pqm)
163 {
164 	struct process_queue_node *pqn, *next;
165 
166 	list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
167 		if (pqn->q && pqn->q->gws)
168 			amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
169 				pqn->q->gws);
170 		kfd_procfs_del_queue(pqn->q);
171 		uninit_queue(pqn->q);
172 		list_del(&pqn->process_queue_list);
173 		kfree(pqn);
174 	}
175 
176 	bitmap_free(pqm->queue_slot_bitmap);
177 	pqm->queue_slot_bitmap = NULL;
178 }
179 
init_user_queue(struct process_queue_manager * pqm,struct kfd_dev * dev,struct queue ** q,struct queue_properties * q_properties,struct file * f,unsigned int qid)180 static int init_user_queue(struct process_queue_manager *pqm,
181 				struct kfd_dev *dev, struct queue **q,
182 				struct queue_properties *q_properties,
183 				struct file *f, unsigned int qid)
184 {
185 	int retval;
186 
187 	/* Doorbell initialized in user space*/
188 	q_properties->doorbell_ptr = NULL;
189 
190 	/* let DQM handle it*/
191 	q_properties->vmid = 0;
192 	q_properties->queue_id = qid;
193 
194 	retval = init_queue(q, q_properties);
195 	if (retval != 0)
196 		return retval;
197 
198 	(*q)->device = dev;
199 	(*q)->process = pqm->process;
200 
201 	if (dev->shared_resources.enable_mes) {
202 		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
203 						AMDGPU_MES_GANG_CTX_SIZE,
204 						&(*q)->gang_ctx_bo,
205 						&(*q)->gang_ctx_gpu_addr,
206 						&(*q)->gang_ctx_cpu_ptr,
207 						false);
208 		if (retval) {
209 			pr_err("failed to allocate gang context bo\n");
210 			goto cleanup;
211 		}
212 		memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
213 	}
214 
215 	pr_debug("PQM After init queue");
216 	return 0;
217 
218 cleanup:
219 	if (dev->shared_resources.enable_mes)
220 		uninit_queue(*q);
221 	return retval;
222 }
223 
pqm_create_queue(struct process_queue_manager * pqm,struct kfd_dev * dev,struct file * f,struct queue_properties * properties,unsigned int * qid,const struct kfd_criu_queue_priv_data * q_data,const void * restore_mqd,const void * restore_ctl_stack,uint32_t * p_doorbell_offset_in_process)224 int pqm_create_queue(struct process_queue_manager *pqm,
225 			    struct kfd_dev *dev,
226 			    struct file *f,
227 			    struct queue_properties *properties,
228 			    unsigned int *qid,
229 			    const struct kfd_criu_queue_priv_data *q_data,
230 			    const void *restore_mqd,
231 			    const void *restore_ctl_stack,
232 			    uint32_t *p_doorbell_offset_in_process)
233 {
234 	int retval;
235 	struct kfd_process_device *pdd;
236 	struct queue *q;
237 	struct process_queue_node *pqn;
238 	struct kernel_queue *kq;
239 	enum kfd_queue_type type = properties->type;
240 	unsigned int max_queues = 127; /* HWS limit */
241 
242 	q = NULL;
243 	kq = NULL;
244 
245 	pdd = kfd_get_process_device_data(dev, pqm->process);
246 	if (!pdd) {
247 		pr_err("Process device data doesn't exist\n");
248 		return -1;
249 	}
250 
251 	/*
252 	 * for debug process, verify that it is within the static queues limit
253 	 * currently limit is set to half of the total avail HQD slots
254 	 * If we are just about to create DIQ, the is_debug flag is not set yet
255 	 * Hence we also check the type as well
256 	 */
257 	if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
258 		max_queues = dev->device_info.max_no_of_hqd/2;
259 
260 	if (pdd->qpd.queue_count >= max_queues)
261 		return -ENOSPC;
262 
263 	if (q_data) {
264 		retval = assign_queue_slot_by_qid(pqm, q_data->q_id);
265 		*qid = q_data->q_id;
266 	} else
267 		retval = find_available_queue_slot(pqm, qid);
268 
269 	if (retval != 0)
270 		return retval;
271 
272 	if (list_empty(&pdd->qpd.queues_list) &&
273 	    list_empty(&pdd->qpd.priv_queue_list))
274 		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
275 
276 	pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
277 	if (!pqn) {
278 		retval = -ENOMEM;
279 		goto err_allocate_pqn;
280 	}
281 
282 	switch (type) {
283 	case KFD_QUEUE_TYPE_SDMA:
284 	case KFD_QUEUE_TYPE_SDMA_XGMI:
285 		/* SDMA queues are always allocated statically no matter
286 		 * which scheduler mode is used. We also do not need to
287 		 * check whether a SDMA queue can be allocated here, because
288 		 * allocate_sdma_queue() in create_queue() has the
289 		 * corresponding check logic.
290 		 */
291 		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
292 		if (retval != 0)
293 			goto err_create_queue;
294 		pqn->q = q;
295 		pqn->kq = NULL;
296 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
297 						    restore_mqd, restore_ctl_stack);
298 		print_queue(q);
299 		break;
300 
301 	case KFD_QUEUE_TYPE_COMPUTE:
302 		/* check if there is over subscription */
303 		if ((dev->dqm->sched_policy ==
304 		     KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
305 		((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
306 		(dev->dqm->active_queue_count >= get_cp_queues_num(dev->dqm)))) {
307 			pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
308 			retval = -EPERM;
309 			goto err_create_queue;
310 		}
311 
312 		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
313 		if (retval != 0)
314 			goto err_create_queue;
315 		pqn->q = q;
316 		pqn->kq = NULL;
317 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
318 						    restore_mqd, restore_ctl_stack);
319 		print_queue(q);
320 		break;
321 	case KFD_QUEUE_TYPE_DIQ:
322 		kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
323 		if (!kq) {
324 			retval = -ENOMEM;
325 			goto err_create_queue;
326 		}
327 		kq->queue->properties.queue_id = *qid;
328 		pqn->kq = kq;
329 		pqn->q = NULL;
330 		retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
331 							kq, &pdd->qpd);
332 		break;
333 	default:
334 		WARN(1, "Invalid queue type %d", type);
335 		retval = -EINVAL;
336 	}
337 
338 	if (retval != 0) {
339 		pr_err("Pasid 0x%x DQM create queue type %d failed. ret %d\n",
340 			pqm->process->pasid, type, retval);
341 		goto err_create_queue;
342 	}
343 
344 	if (q && p_doorbell_offset_in_process)
345 		/* Return the doorbell offset within the doorbell page
346 		 * to the caller so it can be passed up to user mode
347 		 * (in bytes).
348 		 * There are always 1024 doorbells per process, so in case
349 		 * of 8-byte doorbells, there are two doorbell pages per
350 		 * process.
351 		 */
352 		*p_doorbell_offset_in_process =
353 			(q->properties.doorbell_off * sizeof(uint32_t)) &
354 			(kfd_doorbell_process_slice(dev) - 1);
355 
356 	pr_debug("PQM After DQM create queue\n");
357 
358 	list_add(&pqn->process_queue_list, &pqm->queues);
359 
360 	if (q) {
361 		pr_debug("PQM done creating queue\n");
362 		kfd_procfs_add_queue(q);
363 		print_queue_properties(&q->properties);
364 	}
365 
366 	return retval;
367 
368 err_create_queue:
369 	uninit_queue(q);
370 	if (kq)
371 		kernel_queue_uninit(kq, false);
372 	kfree(pqn);
373 err_allocate_pqn:
374 	/* check if queues list is empty unregister process from device */
375 	clear_bit(*qid, pqm->queue_slot_bitmap);
376 	if (list_empty(&pdd->qpd.queues_list) &&
377 	    list_empty(&pdd->qpd.priv_queue_list))
378 		dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
379 	return retval;
380 }
381 
pqm_destroy_queue(struct process_queue_manager * pqm,unsigned int qid)382 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
383 {
384 	struct process_queue_node *pqn;
385 	struct kfd_process_device *pdd;
386 	struct device_queue_manager *dqm;
387 	struct kfd_dev *dev;
388 	int retval;
389 
390 	dqm = NULL;
391 
392 	retval = 0;
393 
394 	pqn = get_queue_by_qid(pqm, qid);
395 	if (!pqn) {
396 		pr_err("Queue id does not match any known queue\n");
397 		return -EINVAL;
398 	}
399 
400 	dev = NULL;
401 	if (pqn->kq)
402 		dev = pqn->kq->dev;
403 	if (pqn->q)
404 		dev = pqn->q->device;
405 	if (WARN_ON(!dev))
406 		return -ENODEV;
407 
408 	pdd = kfd_get_process_device_data(dev, pqm->process);
409 	if (!pdd) {
410 		pr_err("Process device data doesn't exist\n");
411 		return -1;
412 	}
413 
414 	if (pqn->kq) {
415 		/* destroy kernel queue (DIQ) */
416 		dqm = pqn->kq->dev->dqm;
417 		dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
418 		kernel_queue_uninit(pqn->kq, false);
419 	}
420 
421 	if (pqn->q) {
422 		kfd_procfs_del_queue(pqn->q);
423 		dqm = pqn->q->device->dqm;
424 		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
425 		if (retval) {
426 			pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
427 				pqm->process->pasid,
428 				pqn->q->properties.queue_id, retval);
429 			if (retval != -ETIME)
430 				goto err_destroy_queue;
431 		}
432 
433 		if (pqn->q->gws) {
434 			amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
435 				pqn->q->gws);
436 			pdd->qpd.num_gws = 0;
437 		}
438 
439 		if (dev->shared_resources.enable_mes)
440 			amdgpu_amdkfd_free_gtt_mem(dev->adev,
441 						   pqn->q->gang_ctx_bo);
442 		uninit_queue(pqn->q);
443 	}
444 
445 	list_del(&pqn->process_queue_list);
446 	kfree(pqn);
447 	clear_bit(qid, pqm->queue_slot_bitmap);
448 
449 	if (list_empty(&pdd->qpd.queues_list) &&
450 	    list_empty(&pdd->qpd.priv_queue_list))
451 		dqm->ops.unregister_process(dqm, &pdd->qpd);
452 
453 err_destroy_queue:
454 	return retval;
455 }
456 
pqm_update_queue_properties(struct process_queue_manager * pqm,unsigned int qid,struct queue_properties * p)457 int pqm_update_queue_properties(struct process_queue_manager *pqm,
458 				unsigned int qid, struct queue_properties *p)
459 {
460 	int retval;
461 	struct process_queue_node *pqn;
462 
463 	pqn = get_queue_by_qid(pqm, qid);
464 	if (!pqn) {
465 		pr_debug("No queue %d exists for update operation\n", qid);
466 		return -EFAULT;
467 	}
468 
469 	pqn->q->properties.queue_address = p->queue_address;
470 	pqn->q->properties.queue_size = p->queue_size;
471 	pqn->q->properties.queue_percent = p->queue_percent;
472 	pqn->q->properties.priority = p->priority;
473 
474 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
475 							pqn->q, NULL);
476 	if (retval != 0)
477 		return retval;
478 
479 	return 0;
480 }
481 
pqm_update_mqd(struct process_queue_manager * pqm,unsigned int qid,struct mqd_update_info * minfo)482 int pqm_update_mqd(struct process_queue_manager *pqm,
483 				unsigned int qid, struct mqd_update_info *minfo)
484 {
485 	int retval;
486 	struct process_queue_node *pqn;
487 
488 	pqn = get_queue_by_qid(pqm, qid);
489 	if (!pqn) {
490 		pr_debug("No queue %d exists for update operation\n", qid);
491 		return -EFAULT;
492 	}
493 
494 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
495 							pqn->q, minfo);
496 	if (retval != 0)
497 		return retval;
498 
499 	return 0;
500 }
501 
pqm_get_kernel_queue(struct process_queue_manager * pqm,unsigned int qid)502 struct kernel_queue *pqm_get_kernel_queue(
503 					struct process_queue_manager *pqm,
504 					unsigned int qid)
505 {
506 	struct process_queue_node *pqn;
507 
508 	pqn = get_queue_by_qid(pqm, qid);
509 	if (pqn && pqn->kq)
510 		return pqn->kq;
511 
512 	return NULL;
513 }
514 
pqm_get_user_queue(struct process_queue_manager * pqm,unsigned int qid)515 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
516 					unsigned int qid)
517 {
518 	struct process_queue_node *pqn;
519 
520 	pqn = get_queue_by_qid(pqm, qid);
521 	return pqn ? pqn->q : NULL;
522 }
523 
pqm_get_wave_state(struct process_queue_manager * pqm,unsigned int qid,void __user * ctl_stack,u32 * ctl_stack_used_size,u32 * save_area_used_size)524 int pqm_get_wave_state(struct process_queue_manager *pqm,
525 		       unsigned int qid,
526 		       void __user *ctl_stack,
527 		       u32 *ctl_stack_used_size,
528 		       u32 *save_area_used_size)
529 {
530 	struct process_queue_node *pqn;
531 
532 	pqn = get_queue_by_qid(pqm, qid);
533 	if (!pqn) {
534 		pr_debug("amdkfd: No queue %d exists for operation\n",
535 			 qid);
536 		return -EFAULT;
537 	}
538 
539 	return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
540 						       pqn->q,
541 						       ctl_stack,
542 						       ctl_stack_used_size,
543 						       save_area_used_size);
544 }
545 
get_queue_data_sizes(struct kfd_process_device * pdd,struct queue * q,uint32_t * mqd_size,uint32_t * ctl_stack_size)546 static int get_queue_data_sizes(struct kfd_process_device *pdd,
547 				struct queue *q,
548 				uint32_t *mqd_size,
549 				uint32_t *ctl_stack_size)
550 {
551 	int ret;
552 
553 	ret = pqm_get_queue_checkpoint_info(&pdd->process->pqm,
554 					    q->properties.queue_id,
555 					    mqd_size,
556 					    ctl_stack_size);
557 	if (ret)
558 		pr_err("Failed to get queue dump info (%d)\n", ret);
559 
560 	return ret;
561 }
562 
kfd_process_get_queue_info(struct kfd_process * p,uint32_t * num_queues,uint64_t * priv_data_sizes)563 int kfd_process_get_queue_info(struct kfd_process *p,
564 			       uint32_t *num_queues,
565 			       uint64_t *priv_data_sizes)
566 {
567 	uint32_t extra_data_sizes = 0;
568 	struct queue *q;
569 	int i;
570 	int ret;
571 
572 	*num_queues = 0;
573 
574 	/* Run over all PDDs of the process */
575 	for (i = 0; i < p->n_pdds; i++) {
576 		struct kfd_process_device *pdd = p->pdds[i];
577 
578 		list_for_each_entry(q, &pdd->qpd.queues_list, list) {
579 			if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
580 				q->properties.type == KFD_QUEUE_TYPE_SDMA ||
581 				q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
582 				uint32_t mqd_size, ctl_stack_size;
583 
584 				*num_queues = *num_queues + 1;
585 
586 				ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
587 				if (ret)
588 					return ret;
589 
590 				extra_data_sizes += mqd_size + ctl_stack_size;
591 			} else {
592 				pr_err("Unsupported queue type (%d)\n", q->properties.type);
593 				return -EOPNOTSUPP;
594 			}
595 		}
596 	}
597 	*priv_data_sizes = extra_data_sizes +
598 				(*num_queues * sizeof(struct kfd_criu_queue_priv_data));
599 
600 	return 0;
601 }
602 
pqm_checkpoint_mqd(struct process_queue_manager * pqm,unsigned int qid,void * mqd,void * ctl_stack)603 static int pqm_checkpoint_mqd(struct process_queue_manager *pqm,
604 			      unsigned int qid,
605 			      void *mqd,
606 			      void *ctl_stack)
607 {
608 	struct process_queue_node *pqn;
609 
610 	pqn = get_queue_by_qid(pqm, qid);
611 	if (!pqn) {
612 		pr_debug("amdkfd: No queue %d exists for operation\n", qid);
613 		return -EFAULT;
614 	}
615 
616 	if (!pqn->q->device->dqm->ops.checkpoint_mqd) {
617 		pr_err("amdkfd: queue dumping not supported on this device\n");
618 		return -EOPNOTSUPP;
619 	}
620 
621 	return pqn->q->device->dqm->ops.checkpoint_mqd(pqn->q->device->dqm,
622 						       pqn->q, mqd, ctl_stack);
623 }
624 
criu_checkpoint_queue(struct kfd_process_device * pdd,struct queue * q,struct kfd_criu_queue_priv_data * q_data)625 static int criu_checkpoint_queue(struct kfd_process_device *pdd,
626 			   struct queue *q,
627 			   struct kfd_criu_queue_priv_data *q_data)
628 {
629 	uint8_t *mqd, *ctl_stack;
630 	int ret;
631 
632 	mqd = (void *)(q_data + 1);
633 	ctl_stack = mqd + q_data->mqd_size;
634 
635 	q_data->gpu_id = pdd->user_gpu_id;
636 	q_data->type = q->properties.type;
637 	q_data->format = q->properties.format;
638 	q_data->q_id =  q->properties.queue_id;
639 	q_data->q_address = q->properties.queue_address;
640 	q_data->q_size = q->properties.queue_size;
641 	q_data->priority = q->properties.priority;
642 	q_data->q_percent = q->properties.queue_percent;
643 	q_data->read_ptr_addr = (uint64_t)q->properties.read_ptr;
644 	q_data->write_ptr_addr = (uint64_t)q->properties.write_ptr;
645 	q_data->doorbell_id = q->doorbell_id;
646 
647 	q_data->sdma_id = q->sdma_id;
648 
649 	q_data->eop_ring_buffer_address =
650 		q->properties.eop_ring_buffer_address;
651 
652 	q_data->eop_ring_buffer_size = q->properties.eop_ring_buffer_size;
653 
654 	q_data->ctx_save_restore_area_address =
655 		q->properties.ctx_save_restore_area_address;
656 
657 	q_data->ctx_save_restore_area_size =
658 		q->properties.ctx_save_restore_area_size;
659 
660 	q_data->gws = !!q->gws;
661 
662 	ret = pqm_checkpoint_mqd(&pdd->process->pqm, q->properties.queue_id, mqd, ctl_stack);
663 	if (ret) {
664 		pr_err("Failed checkpoint queue_mqd (%d)\n", ret);
665 		return ret;
666 	}
667 
668 	pr_debug("Dumping Queue: gpu_id:%x queue_id:%u\n", q_data->gpu_id, q_data->q_id);
669 	return ret;
670 }
671 
criu_checkpoint_queues_device(struct kfd_process_device * pdd,uint8_t __user * user_priv,unsigned int * q_index,uint64_t * queues_priv_data_offset)672 static int criu_checkpoint_queues_device(struct kfd_process_device *pdd,
673 				   uint8_t __user *user_priv,
674 				   unsigned int *q_index,
675 				   uint64_t *queues_priv_data_offset)
676 {
677 	unsigned int q_private_data_size = 0;
678 	uint8_t *q_private_data = NULL; /* Local buffer to store individual queue private data */
679 	struct queue *q;
680 	int ret = 0;
681 
682 	list_for_each_entry(q, &pdd->qpd.queues_list, list) {
683 		struct kfd_criu_queue_priv_data *q_data;
684 		uint64_t q_data_size;
685 		uint32_t mqd_size;
686 		uint32_t ctl_stack_size;
687 
688 		if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE &&
689 			q->properties.type != KFD_QUEUE_TYPE_SDMA &&
690 			q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI) {
691 
692 			pr_err("Unsupported queue type (%d)\n", q->properties.type);
693 			ret = -EOPNOTSUPP;
694 			break;
695 		}
696 
697 		ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
698 		if (ret)
699 			break;
700 
701 		q_data_size = sizeof(*q_data) + mqd_size + ctl_stack_size;
702 
703 		/* Increase local buffer space if needed */
704 		if (q_private_data_size < q_data_size) {
705 			kfree(q_private_data);
706 
707 			q_private_data = kzalloc(q_data_size, GFP_KERNEL);
708 			if (!q_private_data) {
709 				ret = -ENOMEM;
710 				break;
711 			}
712 			q_private_data_size = q_data_size;
713 		}
714 
715 		q_data = (struct kfd_criu_queue_priv_data *)q_private_data;
716 
717 		/* data stored in this order: priv_data, mqd, ctl_stack */
718 		q_data->mqd_size = mqd_size;
719 		q_data->ctl_stack_size = ctl_stack_size;
720 
721 		ret = criu_checkpoint_queue(pdd, q, q_data);
722 		if (ret)
723 			break;
724 
725 		q_data->object_type = KFD_CRIU_OBJECT_TYPE_QUEUE;
726 
727 		ret = copy_to_user(user_priv + *queues_priv_data_offset,
728 				q_data, q_data_size);
729 		if (ret) {
730 			ret = -EFAULT;
731 			break;
732 		}
733 		*queues_priv_data_offset += q_data_size;
734 		*q_index = *q_index + 1;
735 	}
736 
737 	kfree(q_private_data);
738 
739 	return ret;
740 }
741 
kfd_criu_checkpoint_queues(struct kfd_process * p,uint8_t __user * user_priv_data,uint64_t * priv_data_offset)742 int kfd_criu_checkpoint_queues(struct kfd_process *p,
743 			 uint8_t __user *user_priv_data,
744 			 uint64_t *priv_data_offset)
745 {
746 	int ret = 0, pdd_index, q_index = 0;
747 
748 	for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
749 		struct kfd_process_device *pdd = p->pdds[pdd_index];
750 
751 		/*
752 		 * criu_checkpoint_queues_device will copy data to user and update q_index and
753 		 * queues_priv_data_offset
754 		 */
755 		ret = criu_checkpoint_queues_device(pdd, user_priv_data, &q_index,
756 					      priv_data_offset);
757 
758 		if (ret)
759 			break;
760 	}
761 
762 	return ret;
763 }
764 
set_queue_properties_from_criu(struct queue_properties * qp,struct kfd_criu_queue_priv_data * q_data)765 static void set_queue_properties_from_criu(struct queue_properties *qp,
766 					  struct kfd_criu_queue_priv_data *q_data)
767 {
768 	qp->is_interop = false;
769 	qp->queue_percent = q_data->q_percent;
770 	qp->priority = q_data->priority;
771 	qp->queue_address = q_data->q_address;
772 	qp->queue_size = q_data->q_size;
773 	qp->read_ptr = (uint32_t *) q_data->read_ptr_addr;
774 	qp->write_ptr = (uint32_t *) q_data->write_ptr_addr;
775 	qp->eop_ring_buffer_address = q_data->eop_ring_buffer_address;
776 	qp->eop_ring_buffer_size = q_data->eop_ring_buffer_size;
777 	qp->ctx_save_restore_area_address = q_data->ctx_save_restore_area_address;
778 	qp->ctx_save_restore_area_size = q_data->ctx_save_restore_area_size;
779 	qp->ctl_stack_size = q_data->ctl_stack_size;
780 	qp->type = q_data->type;
781 	qp->format = q_data->format;
782 }
783 
kfd_criu_restore_queue(struct kfd_process * p,uint8_t __user * user_priv_ptr,uint64_t * priv_data_offset,uint64_t max_priv_data_size)784 int kfd_criu_restore_queue(struct kfd_process *p,
785 			   uint8_t __user *user_priv_ptr,
786 			   uint64_t *priv_data_offset,
787 			   uint64_t max_priv_data_size)
788 {
789 	uint8_t *mqd, *ctl_stack, *q_extra_data = NULL;
790 	struct kfd_criu_queue_priv_data *q_data;
791 	struct kfd_process_device *pdd;
792 	uint64_t q_extra_data_size;
793 	struct queue_properties qp;
794 	unsigned int queue_id;
795 	int ret = 0;
796 
797 	if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size)
798 		return -EINVAL;
799 
800 	q_data = kmalloc(sizeof(*q_data), GFP_KERNEL);
801 	if (!q_data)
802 		return -ENOMEM;
803 
804 	ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data));
805 	if (ret) {
806 		ret = -EFAULT;
807 		goto exit;
808 	}
809 
810 	*priv_data_offset += sizeof(*q_data);
811 	q_extra_data_size = (uint64_t)q_data->ctl_stack_size + q_data->mqd_size;
812 
813 	if (*priv_data_offset + q_extra_data_size > max_priv_data_size) {
814 		ret = -EINVAL;
815 		goto exit;
816 	}
817 
818 	q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL);
819 	if (!q_extra_data) {
820 		ret = -ENOMEM;
821 		goto exit;
822 	}
823 
824 	ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size);
825 	if (ret) {
826 		ret = -EFAULT;
827 		goto exit;
828 	}
829 
830 	*priv_data_offset += q_extra_data_size;
831 
832 	pdd = kfd_process_device_data_by_id(p, q_data->gpu_id);
833 	if (!pdd) {
834 		pr_err("Failed to get pdd\n");
835 		ret = -EINVAL;
836 		goto exit;
837 	}
838 	/* data stored in this order: mqd, ctl_stack */
839 	mqd = q_extra_data;
840 	ctl_stack = mqd + q_data->mqd_size;
841 
842 	memset(&qp, 0, sizeof(qp));
843 	set_queue_properties_from_criu(&qp, q_data);
844 
845 	print_queue_properties(&qp);
846 
847 	ret = pqm_create_queue(&p->pqm, pdd->dev, NULL, &qp, &queue_id, q_data, mqd, ctl_stack,
848 				NULL);
849 	if (ret) {
850 		pr_err("Failed to create new queue err:%d\n", ret);
851 		goto exit;
852 	}
853 
854 	if (q_data->gws)
855 		ret = pqm_set_gws(&p->pqm, q_data->q_id, pdd->dev->gws);
856 
857 exit:
858 	if (ret)
859 		pr_err("Failed to restore queue (%d)\n", ret);
860 	else
861 		pr_debug("Queue id %d was restored successfully\n", queue_id);
862 
863 	kfree(q_data);
864 
865 	return ret;
866 }
867 
pqm_get_queue_checkpoint_info(struct process_queue_manager * pqm,unsigned int qid,uint32_t * mqd_size,uint32_t * ctl_stack_size)868 int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
869 				  unsigned int qid,
870 				  uint32_t *mqd_size,
871 				  uint32_t *ctl_stack_size)
872 {
873 	struct process_queue_node *pqn;
874 
875 	pqn = get_queue_by_qid(pqm, qid);
876 	if (!pqn) {
877 		pr_debug("amdkfd: No queue %d exists for operation\n", qid);
878 		return -EFAULT;
879 	}
880 
881 	if (!pqn->q->device->dqm->ops.get_queue_checkpoint_info) {
882 		pr_err("amdkfd: queue dumping not supported on this device\n");
883 		return -EOPNOTSUPP;
884 	}
885 
886 	pqn->q->device->dqm->ops.get_queue_checkpoint_info(pqn->q->device->dqm,
887 						       pqn->q, mqd_size,
888 						       ctl_stack_size);
889 	return 0;
890 }
891 
892 #if defined(CONFIG_DEBUG_FS)
893 
pqm_debugfs_mqds(struct seq_file * m,void * data)894 int pqm_debugfs_mqds(struct seq_file *m, void *data)
895 {
896 	struct process_queue_manager *pqm = data;
897 	struct process_queue_node *pqn;
898 	struct queue *q;
899 	enum KFD_MQD_TYPE mqd_type;
900 	struct mqd_manager *mqd_mgr;
901 	int r = 0;
902 
903 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
904 		if (pqn->q) {
905 			q = pqn->q;
906 			switch (q->properties.type) {
907 			case KFD_QUEUE_TYPE_SDMA:
908 			case KFD_QUEUE_TYPE_SDMA_XGMI:
909 				seq_printf(m, "  SDMA queue on device %x\n",
910 					   q->device->id);
911 				mqd_type = KFD_MQD_TYPE_SDMA;
912 				break;
913 			case KFD_QUEUE_TYPE_COMPUTE:
914 				seq_printf(m, "  Compute queue on device %x\n",
915 					   q->device->id);
916 				mqd_type = KFD_MQD_TYPE_CP;
917 				break;
918 			default:
919 				seq_printf(m,
920 				"  Bad user queue type %d on device %x\n",
921 					   q->properties.type, q->device->id);
922 				continue;
923 			}
924 			mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
925 		} else if (pqn->kq) {
926 			q = pqn->kq->queue;
927 			mqd_mgr = pqn->kq->mqd_mgr;
928 			switch (q->properties.type) {
929 			case KFD_QUEUE_TYPE_DIQ:
930 				seq_printf(m, "  DIQ on device %x\n",
931 					   pqn->kq->dev->id);
932 				break;
933 			default:
934 				seq_printf(m,
935 				"  Bad kernel queue type %d on device %x\n",
936 					   q->properties.type,
937 					   pqn->kq->dev->id);
938 				continue;
939 			}
940 		} else {
941 			seq_printf(m,
942 		"  Weird: Queue node with neither kernel nor user queue\n");
943 			continue;
944 		}
945 
946 		r = mqd_mgr->debugfs_show_mqd(m, q->mqd);
947 		if (r != 0)
948 			break;
949 	}
950 
951 	return r;
952 }
953 
954 #endif
955