xref: /openbsd-src/sys/dev/pci/drm/amd/amdkfd/kfd_process.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
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 #include <linux/mutex.h>
24 #include <linux/log2.h>
25 #include <linux/sched.h>
26 #include <linux/sched/mm.h>
27 #include <linux/sched/task.h>
28 #include <linux/slab.h>
29 #include <linux/amd-iommu.h>
30 #include <linux/notifier.h>
31 #include <linux/compat.h>
32 #include <linux/mman.h>
33 #include <linux/file.h>
34 #include <linux/pm_runtime.h>
35 #include "amdgpu_amdkfd.h"
36 #include "amdgpu.h"
37 
38 struct mm_struct;
39 
40 #include "kfd_priv.h"
41 #include "kfd_device_queue_manager.h"
42 #include "kfd_dbgmgr.h"
43 #include "kfd_iommu.h"
44 
45 /*
46  * List of struct kfd_process (field kfd_process).
47  * Unique/indexed by mm_struct*
48  */
49 DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
50 static DEFINE_MUTEX(kfd_processes_mutex);
51 
52 DEFINE_SRCU(kfd_processes_srcu);
53 
54 /* For process termination handling */
55 static struct workqueue_struct *kfd_process_wq;
56 
57 /* Ordered, single-threaded workqueue for restoring evicted
58  * processes. Restoring multiple processes concurrently under memory
59  * pressure can lead to processes blocking each other from validating
60  * their BOs and result in a live-lock situation where processes
61  * remain evicted indefinitely.
62  */
63 static struct workqueue_struct *kfd_restore_wq;
64 
65 static struct kfd_process *find_process(const struct task_struct *thread);
66 static void kfd_process_ref_release(struct kref *ref);
67 static struct kfd_process *create_process(const struct task_struct *thread);
68 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep);
69 
70 static void evict_process_worker(struct work_struct *work);
71 static void restore_process_worker(struct work_struct *work);
72 
73 struct kfd_procfs_tree {
74 	struct kobject *kobj;
75 };
76 
77 static struct kfd_procfs_tree procfs;
78 
79 static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr,
80 			       char *buffer)
81 {
82 	int val = 0;
83 
84 	if (strcmp(attr->name, "pasid") == 0) {
85 		struct kfd_process *p = container_of(attr, struct kfd_process,
86 						     attr_pasid);
87 		val = p->pasid;
88 	} else {
89 		pr_err("Invalid attribute");
90 		return -EINVAL;
91 	}
92 
93 	return snprintf(buffer, PAGE_SIZE, "%d\n", val);
94 }
95 
96 static void kfd_procfs_kobj_release(struct kobject *kobj)
97 {
98 	kfree(kobj);
99 }
100 
101 static const struct sysfs_ops kfd_procfs_ops = {
102 	.show = kfd_procfs_show,
103 };
104 
105 static struct kobj_type procfs_type = {
106 	.release = kfd_procfs_kobj_release,
107 	.sysfs_ops = &kfd_procfs_ops,
108 };
109 
110 void kfd_procfs_init(void)
111 {
112 	int ret = 0;
113 
114 	procfs.kobj = kfd_alloc_struct(procfs.kobj);
115 	if (!procfs.kobj)
116 		return;
117 
118 	ret = kobject_init_and_add(procfs.kobj, &procfs_type,
119 				   &kfd_device->kobj, "proc");
120 	if (ret) {
121 		pr_warn("Could not create procfs proc folder");
122 		/* If we fail to create the procfs, clean up */
123 		kfd_procfs_shutdown();
124 	}
125 }
126 
127 void kfd_procfs_shutdown(void)
128 {
129 	if (procfs.kobj) {
130 		kobject_del(procfs.kobj);
131 		kobject_put(procfs.kobj);
132 		procfs.kobj = NULL;
133 	}
134 }
135 
136 static ssize_t kfd_procfs_queue_show(struct kobject *kobj,
137 				     struct attribute *attr, char *buffer)
138 {
139 	struct queue *q = container_of(kobj, struct queue, kobj);
140 
141 	if (!strcmp(attr->name, "size"))
142 		return snprintf(buffer, PAGE_SIZE, "%llu",
143 				q->properties.queue_size);
144 	else if (!strcmp(attr->name, "type"))
145 		return snprintf(buffer, PAGE_SIZE, "%d", q->properties.type);
146 	else if (!strcmp(attr->name, "gpuid"))
147 		return snprintf(buffer, PAGE_SIZE, "%u", q->device->id);
148 	else
149 		pr_err("Invalid attribute");
150 
151 	return 0;
152 }
153 
154 static struct attribute attr_queue_size = {
155 	.name = "size",
156 	.mode = KFD_SYSFS_FILE_MODE
157 };
158 
159 static struct attribute attr_queue_type = {
160 	.name = "type",
161 	.mode = KFD_SYSFS_FILE_MODE
162 };
163 
164 static struct attribute attr_queue_gpuid = {
165 	.name = "gpuid",
166 	.mode = KFD_SYSFS_FILE_MODE
167 };
168 
169 static struct attribute *procfs_queue_attrs[] = {
170 	&attr_queue_size,
171 	&attr_queue_type,
172 	&attr_queue_gpuid,
173 	NULL
174 };
175 
176 static const struct sysfs_ops procfs_queue_ops = {
177 	.show = kfd_procfs_queue_show,
178 };
179 
180 static struct kobj_type procfs_queue_type = {
181 	.sysfs_ops = &procfs_queue_ops,
182 	.default_attrs = procfs_queue_attrs,
183 };
184 
185 int kfd_procfs_add_queue(struct queue *q)
186 {
187 	struct kfd_process *proc;
188 	int ret;
189 
190 	if (!q || !q->process)
191 		return -EINVAL;
192 	proc = q->process;
193 
194 	/* Create proc/<pid>/queues/<queue id> folder */
195 	if (!proc->kobj_queues)
196 		return -EFAULT;
197 	ret = kobject_init_and_add(&q->kobj, &procfs_queue_type,
198 			proc->kobj_queues, "%u", q->properties.queue_id);
199 	if (ret < 0) {
200 		pr_warn("Creating proc/<pid>/queues/%u failed",
201 			q->properties.queue_id);
202 		kobject_put(&q->kobj);
203 		return ret;
204 	}
205 
206 	return 0;
207 }
208 
209 void kfd_procfs_del_queue(struct queue *q)
210 {
211 	if (!q)
212 		return;
213 
214 	kobject_del(&q->kobj);
215 	kobject_put(&q->kobj);
216 }
217 
218 int kfd_process_create_wq(void)
219 {
220 	if (!kfd_process_wq)
221 		kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
222 	if (!kfd_restore_wq)
223 		kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq", 0);
224 
225 	if (!kfd_process_wq || !kfd_restore_wq) {
226 		kfd_process_destroy_wq();
227 		return -ENOMEM;
228 	}
229 
230 	return 0;
231 }
232 
233 void kfd_process_destroy_wq(void)
234 {
235 	if (kfd_process_wq) {
236 		destroy_workqueue(kfd_process_wq);
237 		kfd_process_wq = NULL;
238 	}
239 	if (kfd_restore_wq) {
240 		destroy_workqueue(kfd_restore_wq);
241 		kfd_restore_wq = NULL;
242 	}
243 }
244 
245 static void kfd_process_free_gpuvm(struct kgd_mem *mem,
246 			struct kfd_process_device *pdd)
247 {
248 	struct kfd_dev *dev = pdd->dev;
249 
250 	amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->kgd, mem, pdd->vm);
251 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, mem);
252 }
253 
254 /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
255  *	This function should be only called right after the process
256  *	is created and when kfd_processes_mutex is still being held
257  *	to avoid concurrency. Because of that exclusiveness, we do
258  *	not need to take p->mutex.
259  */
260 static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
261 				   uint64_t gpu_va, uint32_t size,
262 				   uint32_t flags, void **kptr)
263 {
264 	struct kfd_dev *kdev = pdd->dev;
265 	struct kgd_mem *mem = NULL;
266 	int handle;
267 	int err;
268 
269 	err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(kdev->kgd, gpu_va, size,
270 						 pdd->vm, &mem, NULL, flags);
271 	if (err)
272 		goto err_alloc_mem;
273 
274 	err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(kdev->kgd, mem, pdd->vm);
275 	if (err)
276 		goto err_map_mem;
277 
278 	err = amdgpu_amdkfd_gpuvm_sync_memory(kdev->kgd, mem, true);
279 	if (err) {
280 		pr_debug("Sync memory failed, wait interrupted by user signal\n");
281 		goto sync_memory_failed;
282 	}
283 
284 	/* Create an obj handle so kfd_process_device_remove_obj_handle
285 	 * will take care of the bo removal when the process finishes.
286 	 * We do not need to take p->mutex, because the process is just
287 	 * created and the ioctls have not had the chance to run.
288 	 */
289 	handle = kfd_process_device_create_obj_handle(pdd, mem);
290 
291 	if (handle < 0) {
292 		err = handle;
293 		goto free_gpuvm;
294 	}
295 
296 	if (kptr) {
297 		err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kdev->kgd,
298 				(struct kgd_mem *)mem, kptr, NULL);
299 		if (err) {
300 			pr_debug("Map GTT BO to kernel failed\n");
301 			goto free_obj_handle;
302 		}
303 	}
304 
305 	return err;
306 
307 free_obj_handle:
308 	kfd_process_device_remove_obj_handle(pdd, handle);
309 free_gpuvm:
310 sync_memory_failed:
311 	kfd_process_free_gpuvm(mem, pdd);
312 	return err;
313 
314 err_map_mem:
315 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->kgd, mem);
316 err_alloc_mem:
317 	*kptr = NULL;
318 	return err;
319 }
320 
321 /* kfd_process_device_reserve_ib_mem - Reserve memory inside the
322  *	process for IB usage The memory reserved is for KFD to submit
323  *	IB to AMDGPU from kernel.  If the memory is reserved
324  *	successfully, ib_kaddr will have the CPU/kernel
325  *	address. Check ib_kaddr before accessing the memory.
326  */
327 static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
328 {
329 	struct qcm_process_device *qpd = &pdd->qpd;
330 	uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT |
331 			KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
332 			KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE |
333 			KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
334 	void *kaddr;
335 	int ret;
336 
337 	if (qpd->ib_kaddr || !qpd->ib_base)
338 		return 0;
339 
340 	/* ib_base is only set for dGPU */
341 	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
342 				      &kaddr);
343 	if (ret)
344 		return ret;
345 
346 	qpd->ib_kaddr = kaddr;
347 
348 	return 0;
349 }
350 
351 struct kfd_process *kfd_create_process(struct file *filep)
352 {
353 	struct kfd_process *process;
354 	struct task_struct *thread = current;
355 	int ret;
356 
357 	if (!thread->mm)
358 		return ERR_PTR(-EINVAL);
359 
360 	/* Only the pthreads threading model is supported. */
361 	if (thread->group_leader->mm != thread->mm)
362 		return ERR_PTR(-EINVAL);
363 
364 	/*
365 	 * take kfd processes mutex before starting of process creation
366 	 * so there won't be a case where two threads of the same process
367 	 * create two kfd_process structures
368 	 */
369 	mutex_lock(&kfd_processes_mutex);
370 
371 	/* A prior open of /dev/kfd could have already created the process. */
372 	process = find_process(thread);
373 	if (process) {
374 		pr_debug("Process already found\n");
375 	} else {
376 		process = create_process(thread);
377 		if (IS_ERR(process))
378 			goto out;
379 
380 		ret = kfd_process_init_cwsr_apu(process, filep);
381 		if (ret) {
382 			process = ERR_PTR(ret);
383 			goto out;
384 		}
385 
386 		if (!procfs.kobj)
387 			goto out;
388 
389 		process->kobj = kfd_alloc_struct(process->kobj);
390 		if (!process->kobj) {
391 			pr_warn("Creating procfs kobject failed");
392 			goto out;
393 		}
394 		ret = kobject_init_and_add(process->kobj, &procfs_type,
395 					   procfs.kobj, "%d",
396 					   (int)process->lead_thread->pid);
397 		if (ret) {
398 			pr_warn("Creating procfs pid directory failed");
399 			kobject_put(process->kobj);
400 			goto out;
401 		}
402 
403 		process->attr_pasid.name = "pasid";
404 		process->attr_pasid.mode = KFD_SYSFS_FILE_MODE;
405 		sysfs_attr_init(&process->attr_pasid);
406 		ret = sysfs_create_file(process->kobj, &process->attr_pasid);
407 		if (ret)
408 			pr_warn("Creating pasid for pid %d failed",
409 					(int)process->lead_thread->pid);
410 
411 		process->kobj_queues = kobject_create_and_add("queues",
412 							process->kobj);
413 		if (!process->kobj_queues)
414 			pr_warn("Creating KFD proc/queues folder failed");
415 	}
416 out:
417 	if (!IS_ERR(process))
418 		kref_get(&process->ref);
419 	mutex_unlock(&kfd_processes_mutex);
420 
421 	return process;
422 }
423 
424 struct kfd_process *kfd_get_process(const struct task_struct *thread)
425 {
426 	struct kfd_process *process;
427 
428 	if (!thread->mm)
429 		return ERR_PTR(-EINVAL);
430 
431 	/* Only the pthreads threading model is supported. */
432 	if (thread->group_leader->mm != thread->mm)
433 		return ERR_PTR(-EINVAL);
434 
435 	process = find_process(thread);
436 	if (!process)
437 		return ERR_PTR(-EINVAL);
438 
439 	return process;
440 }
441 
442 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
443 {
444 	struct kfd_process *process;
445 
446 	hash_for_each_possible_rcu(kfd_processes_table, process,
447 					kfd_processes, (uintptr_t)mm)
448 		if (process->mm == mm)
449 			return process;
450 
451 	return NULL;
452 }
453 
454 static struct kfd_process *find_process(const struct task_struct *thread)
455 {
456 	struct kfd_process *p;
457 	int idx;
458 
459 	idx = srcu_read_lock(&kfd_processes_srcu);
460 	p = find_process_by_mm(thread->mm);
461 	srcu_read_unlock(&kfd_processes_srcu, idx);
462 
463 	return p;
464 }
465 
466 void kfd_unref_process(struct kfd_process *p)
467 {
468 	kref_put(&p->ref, kfd_process_ref_release);
469 }
470 
471 static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
472 {
473 	struct kfd_process *p = pdd->process;
474 	void *mem;
475 	int id;
476 
477 	/*
478 	 * Remove all handles from idr and release appropriate
479 	 * local memory object
480 	 */
481 	idr_for_each_entry(&pdd->alloc_idr, mem, id) {
482 		struct kfd_process_device *peer_pdd;
483 
484 		list_for_each_entry(peer_pdd, &p->per_device_data,
485 				    per_device_list) {
486 			if (!peer_pdd->vm)
487 				continue;
488 			amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
489 				peer_pdd->dev->kgd, mem, peer_pdd->vm);
490 		}
491 
492 		amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem);
493 		kfd_process_device_remove_obj_handle(pdd, id);
494 	}
495 }
496 
497 static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
498 {
499 	struct kfd_process_device *pdd;
500 
501 	list_for_each_entry(pdd, &p->per_device_data, per_device_list)
502 		kfd_process_device_free_bos(pdd);
503 }
504 
505 static void kfd_process_destroy_pdds(struct kfd_process *p)
506 {
507 	struct kfd_process_device *pdd, *temp;
508 
509 	list_for_each_entry_safe(pdd, temp, &p->per_device_data,
510 				 per_device_list) {
511 		pr_debug("Releasing pdd (topology id %d) for process (pasid 0x%x)\n",
512 				pdd->dev->id, p->pasid);
513 
514 		if (pdd->drm_file) {
515 			amdgpu_amdkfd_gpuvm_release_process_vm(
516 					pdd->dev->kgd, pdd->vm);
517 			fput(pdd->drm_file);
518 		}
519 		else if (pdd->vm)
520 			amdgpu_amdkfd_gpuvm_destroy_process_vm(
521 				pdd->dev->kgd, pdd->vm);
522 
523 		list_del(&pdd->per_device_list);
524 
525 		if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
526 			free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
527 				get_order(KFD_CWSR_TBA_TMA_SIZE));
528 
529 		kfree(pdd->qpd.doorbell_bitmap);
530 		idr_destroy(&pdd->alloc_idr);
531 
532 		/*
533 		 * before destroying pdd, make sure to report availability
534 		 * for auto suspend
535 		 */
536 		if (pdd->runtime_inuse) {
537 			pm_runtime_mark_last_busy(pdd->dev->ddev->dev);
538 			pm_runtime_put_autosuspend(pdd->dev->ddev->dev);
539 			pdd->runtime_inuse = false;
540 		}
541 
542 		kfree(pdd);
543 	}
544 }
545 
546 /* No process locking is needed in this function, because the process
547  * is not findable any more. We must assume that no other thread is
548  * using it any more, otherwise we couldn't safely free the process
549  * structure in the end.
550  */
551 static void kfd_process_wq_release(struct work_struct *work)
552 {
553 	struct kfd_process *p = container_of(work, struct kfd_process,
554 					     release_work);
555 
556 	/* Remove the procfs files */
557 	if (p->kobj) {
558 		sysfs_remove_file(p->kobj, &p->attr_pasid);
559 		kobject_del(p->kobj_queues);
560 		kobject_put(p->kobj_queues);
561 		p->kobj_queues = NULL;
562 		kobject_del(p->kobj);
563 		kobject_put(p->kobj);
564 		p->kobj = NULL;
565 	}
566 
567 	kfd_iommu_unbind_process(p);
568 
569 	kfd_process_free_outstanding_kfd_bos(p);
570 
571 	kfd_process_destroy_pdds(p);
572 	dma_fence_put(p->ef);
573 
574 	kfd_event_free_process(p);
575 
576 	kfd_pasid_free(p->pasid);
577 	kfd_free_process_doorbells(p);
578 
579 	mutex_destroy(&p->mutex);
580 
581 	put_task_struct(p->lead_thread);
582 
583 	kfree(p);
584 }
585 
586 static void kfd_process_ref_release(struct kref *ref)
587 {
588 	struct kfd_process *p = container_of(ref, struct kfd_process, ref);
589 
590 	INIT_WORK(&p->release_work, kfd_process_wq_release);
591 	queue_work(kfd_process_wq, &p->release_work);
592 }
593 
594 static void kfd_process_free_notifier(struct mmu_notifier *mn)
595 {
596 	kfd_unref_process(container_of(mn, struct kfd_process, mmu_notifier));
597 }
598 
599 static void kfd_process_notifier_release(struct mmu_notifier *mn,
600 					struct mm_struct *mm)
601 {
602 	struct kfd_process *p;
603 	struct kfd_process_device *pdd = NULL;
604 
605 	/*
606 	 * The kfd_process structure can not be free because the
607 	 * mmu_notifier srcu is read locked
608 	 */
609 	p = container_of(mn, struct kfd_process, mmu_notifier);
610 	if (WARN_ON(p->mm != mm))
611 		return;
612 
613 	mutex_lock(&kfd_processes_mutex);
614 	hash_del_rcu(&p->kfd_processes);
615 	mutex_unlock(&kfd_processes_mutex);
616 	synchronize_srcu(&kfd_processes_srcu);
617 
618 	cancel_delayed_work_sync(&p->eviction_work);
619 	cancel_delayed_work_sync(&p->restore_work);
620 
621 	mutex_lock(&p->mutex);
622 
623 	/* Iterate over all process device data structures and if the
624 	 * pdd is in debug mode, we should first force unregistration,
625 	 * then we will be able to destroy the queues
626 	 */
627 	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
628 		struct kfd_dev *dev = pdd->dev;
629 
630 		mutex_lock(kfd_get_dbgmgr_mutex());
631 		if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
632 			if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
633 				kfd_dbgmgr_destroy(dev->dbgmgr);
634 				dev->dbgmgr = NULL;
635 			}
636 		}
637 		mutex_unlock(kfd_get_dbgmgr_mutex());
638 	}
639 
640 	kfd_process_dequeue_from_all_devices(p);
641 	pqm_uninit(&p->pqm);
642 
643 	/* Indicate to other users that MM is no longer valid */
644 	p->mm = NULL;
645 	/* Signal the eviction fence after user mode queues are
646 	 * destroyed. This allows any BOs to be freed without
647 	 * triggering pointless evictions or waiting for fences.
648 	 */
649 	dma_fence_signal(p->ef);
650 
651 	mutex_unlock(&p->mutex);
652 
653 	mmu_notifier_put(&p->mmu_notifier);
654 }
655 
656 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
657 	.release = kfd_process_notifier_release,
658 	.free_notifier = kfd_process_free_notifier,
659 };
660 
661 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
662 {
663 	unsigned long  offset;
664 	struct kfd_process_device *pdd;
665 
666 	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
667 		struct kfd_dev *dev = pdd->dev;
668 		struct qcm_process_device *qpd = &pdd->qpd;
669 
670 		if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
671 			continue;
672 
673 		offset = KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id);
674 		qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
675 			KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
676 			MAP_SHARED, offset);
677 
678 		if (IS_ERR_VALUE(qpd->tba_addr)) {
679 			int err = qpd->tba_addr;
680 
681 			pr_err("Failure to set tba address. error %d.\n", err);
682 			qpd->tba_addr = 0;
683 			qpd->cwsr_kaddr = NULL;
684 			return err;
685 		}
686 
687 		memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
688 
689 		qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
690 		pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
691 			qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
692 	}
693 
694 	return 0;
695 }
696 
697 static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
698 {
699 	struct kfd_dev *dev = pdd->dev;
700 	struct qcm_process_device *qpd = &pdd->qpd;
701 	uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT
702 			| KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE
703 			| KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
704 	void *kaddr;
705 	int ret;
706 
707 	if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
708 		return 0;
709 
710 	/* cwsr_base is only set for dGPU */
711 	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
712 				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
713 	if (ret)
714 		return ret;
715 
716 	qpd->cwsr_kaddr = kaddr;
717 	qpd->tba_addr = qpd->cwsr_base;
718 
719 	memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
720 
721 	qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
722 	pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
723 		 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
724 
725 	return 0;
726 }
727 
728 /*
729  * On return the kfd_process is fully operational and will be freed when the
730  * mm is released
731  */
732 static struct kfd_process *create_process(const struct task_struct *thread)
733 {
734 	struct kfd_process *process;
735 	int err = -ENOMEM;
736 
737 	process = kzalloc(sizeof(*process), GFP_KERNEL);
738 	if (!process)
739 		goto err_alloc_process;
740 
741 	kref_init(&process->ref);
742 	rw_init(&process->mutex, "kfdproc");
743 	process->mm = thread->mm;
744 	process->lead_thread = thread->group_leader;
745 	INIT_LIST_HEAD(&process->per_device_data);
746 	INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
747 	INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
748 	process->last_restore_timestamp = get_jiffies_64();
749 	kfd_event_init_process(process);
750 	process->is_32bit_user_mode = in_compat_syscall();
751 
752 	process->pasid = kfd_pasid_alloc();
753 	if (process->pasid == 0)
754 		goto err_alloc_pasid;
755 
756 	if (kfd_alloc_process_doorbells(process) < 0)
757 		goto err_alloc_doorbells;
758 
759 	err = pqm_init(&process->pqm, process);
760 	if (err != 0)
761 		goto err_process_pqm_init;
762 
763 	/* init process apertures*/
764 	err = kfd_init_apertures(process);
765 	if (err != 0)
766 		goto err_init_apertures;
767 
768 	/* Must be last, have to use release destruction after this */
769 	process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
770 	err = mmu_notifier_register(&process->mmu_notifier, process->mm);
771 	if (err)
772 		goto err_register_notifier;
773 
774 	get_task_struct(process->lead_thread);
775 	hash_add_rcu(kfd_processes_table, &process->kfd_processes,
776 			(uintptr_t)process->mm);
777 
778 	return process;
779 
780 err_register_notifier:
781 	kfd_process_free_outstanding_kfd_bos(process);
782 	kfd_process_destroy_pdds(process);
783 err_init_apertures:
784 	pqm_uninit(&process->pqm);
785 err_process_pqm_init:
786 	kfd_free_process_doorbells(process);
787 err_alloc_doorbells:
788 	kfd_pasid_free(process->pasid);
789 err_alloc_pasid:
790 	mutex_destroy(&process->mutex);
791 	kfree(process);
792 err_alloc_process:
793 	return ERR_PTR(err);
794 }
795 
796 static int init_doorbell_bitmap(struct qcm_process_device *qpd,
797 			struct kfd_dev *dev)
798 {
799 	unsigned int i;
800 	int range_start = dev->shared_resources.non_cp_doorbells_start;
801 	int range_end = dev->shared_resources.non_cp_doorbells_end;
802 
803 	if (!KFD_IS_SOC15(dev->device_info->asic_family))
804 		return 0;
805 
806 	qpd->doorbell_bitmap =
807 		kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
808 				     BITS_PER_BYTE), GFP_KERNEL);
809 	if (!qpd->doorbell_bitmap)
810 		return -ENOMEM;
811 
812 	/* Mask out doorbells reserved for SDMA, IH, and VCN on SOC15. */
813 	pr_debug("reserved doorbell 0x%03x - 0x%03x\n", range_start, range_end);
814 	pr_debug("reserved doorbell 0x%03x - 0x%03x\n",
815 			range_start + KFD_QUEUE_DOORBELL_MIRROR_OFFSET,
816 			range_end + KFD_QUEUE_DOORBELL_MIRROR_OFFSET);
817 
818 	for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS / 2; i++) {
819 		if (i >= range_start && i <= range_end) {
820 			set_bit(i, qpd->doorbell_bitmap);
821 			set_bit(i + KFD_QUEUE_DOORBELL_MIRROR_OFFSET,
822 				qpd->doorbell_bitmap);
823 		}
824 	}
825 
826 	return 0;
827 }
828 
829 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
830 							struct kfd_process *p)
831 {
832 	struct kfd_process_device *pdd = NULL;
833 
834 	list_for_each_entry(pdd, &p->per_device_data, per_device_list)
835 		if (pdd->dev == dev)
836 			return pdd;
837 
838 	return NULL;
839 }
840 
841 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
842 							struct kfd_process *p)
843 {
844 	struct kfd_process_device *pdd = NULL;
845 
846 	pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
847 	if (!pdd)
848 		return NULL;
849 
850 	if (init_doorbell_bitmap(&pdd->qpd, dev)) {
851 		pr_err("Failed to init doorbell for process\n");
852 		kfree(pdd);
853 		return NULL;
854 	}
855 
856 	pdd->dev = dev;
857 	INIT_LIST_HEAD(&pdd->qpd.queues_list);
858 	INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
859 	pdd->qpd.dqm = dev->dqm;
860 	pdd->qpd.pqm = &p->pqm;
861 	pdd->qpd.evicted = 0;
862 	pdd->process = p;
863 	pdd->bound = PDD_UNBOUND;
864 	pdd->already_dequeued = false;
865 	pdd->runtime_inuse = false;
866 	list_add(&pdd->per_device_list, &p->per_device_data);
867 
868 	/* Init idr used for memory handle translation */
869 	idr_init(&pdd->alloc_idr);
870 
871 	return pdd;
872 }
873 
874 /**
875  * kfd_process_device_init_vm - Initialize a VM for a process-device
876  *
877  * @pdd: The process-device
878  * @drm_file: Optional pointer to a DRM file descriptor
879  *
880  * If @drm_file is specified, it will be used to acquire the VM from
881  * that file descriptor. If successful, the @pdd takes ownership of
882  * the file descriptor.
883  *
884  * If @drm_file is NULL, a new VM is created.
885  *
886  * Returns 0 on success, -errno on failure.
887  */
888 int kfd_process_device_init_vm(struct kfd_process_device *pdd,
889 			       struct file *drm_file)
890 {
891 	struct kfd_process *p;
892 	struct kfd_dev *dev;
893 	int ret;
894 
895 	if (pdd->vm)
896 		return drm_file ? -EBUSY : 0;
897 
898 	p = pdd->process;
899 	dev = pdd->dev;
900 
901 	if (drm_file)
902 		ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(
903 			dev->kgd, drm_file, p->pasid,
904 			&pdd->vm, &p->kgd_process_info, &p->ef);
905 	else
906 		ret = amdgpu_amdkfd_gpuvm_create_process_vm(dev->kgd, p->pasid,
907 			&pdd->vm, &p->kgd_process_info, &p->ef);
908 	if (ret) {
909 		pr_err("Failed to create process VM object\n");
910 		return ret;
911 	}
912 
913 	amdgpu_vm_set_task_info(pdd->vm);
914 
915 	ret = kfd_process_device_reserve_ib_mem(pdd);
916 	if (ret)
917 		goto err_reserve_ib_mem;
918 	ret = kfd_process_device_init_cwsr_dgpu(pdd);
919 	if (ret)
920 		goto err_init_cwsr;
921 
922 	pdd->drm_file = drm_file;
923 
924 	return 0;
925 
926 err_init_cwsr:
927 err_reserve_ib_mem:
928 	kfd_process_device_free_bos(pdd);
929 	if (!drm_file)
930 		amdgpu_amdkfd_gpuvm_destroy_process_vm(dev->kgd, pdd->vm);
931 	pdd->vm = NULL;
932 
933 	return ret;
934 }
935 
936 /*
937  * Direct the IOMMU to bind the process (specifically the pasid->mm)
938  * to the device.
939  * Unbinding occurs when the process dies or the device is removed.
940  *
941  * Assumes that the process lock is held.
942  */
943 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
944 							struct kfd_process *p)
945 {
946 	struct kfd_process_device *pdd;
947 	int err;
948 
949 	pdd = kfd_get_process_device_data(dev, p);
950 	if (!pdd) {
951 		pr_err("Process device data doesn't exist\n");
952 		return ERR_PTR(-ENOMEM);
953 	}
954 
955 	/*
956 	 * signal runtime-pm system to auto resume and prevent
957 	 * further runtime suspend once device pdd is created until
958 	 * pdd is destroyed.
959 	 */
960 	if (!pdd->runtime_inuse) {
961 		err = pm_runtime_get_sync(dev->ddev->dev);
962 		if (err < 0)
963 			return ERR_PTR(err);
964 	}
965 
966 	err = kfd_iommu_bind_process_to_device(pdd);
967 	if (err)
968 		goto out;
969 
970 	err = kfd_process_device_init_vm(pdd, NULL);
971 	if (err)
972 		goto out;
973 
974 	/*
975 	 * make sure that runtime_usage counter is incremented just once
976 	 * per pdd
977 	 */
978 	pdd->runtime_inuse = true;
979 
980 	return pdd;
981 
982 out:
983 	/* balance runpm reference count and exit with error */
984 	if (!pdd->runtime_inuse) {
985 		pm_runtime_mark_last_busy(dev->ddev->dev);
986 		pm_runtime_put_autosuspend(dev->ddev->dev);
987 	}
988 
989 	return ERR_PTR(err);
990 }
991 
992 struct kfd_process_device *kfd_get_first_process_device_data(
993 						struct kfd_process *p)
994 {
995 	return list_first_entry(&p->per_device_data,
996 				struct kfd_process_device,
997 				per_device_list);
998 }
999 
1000 struct kfd_process_device *kfd_get_next_process_device_data(
1001 						struct kfd_process *p,
1002 						struct kfd_process_device *pdd)
1003 {
1004 	if (list_is_last(&pdd->per_device_list, &p->per_device_data))
1005 		return NULL;
1006 	return list_next_entry(pdd, per_device_list);
1007 }
1008 
1009 bool kfd_has_process_device_data(struct kfd_process *p)
1010 {
1011 	return !(list_empty(&p->per_device_data));
1012 }
1013 
1014 /* Create specific handle mapped to mem from process local memory idr
1015  * Assumes that the process lock is held.
1016  */
1017 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
1018 					void *mem)
1019 {
1020 	return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
1021 }
1022 
1023 /* Translate specific handle from process local memory idr
1024  * Assumes that the process lock is held.
1025  */
1026 void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
1027 					int handle)
1028 {
1029 	if (handle < 0)
1030 		return NULL;
1031 
1032 	return idr_find(&pdd->alloc_idr, handle);
1033 }
1034 
1035 /* Remove specific handle from process local memory idr
1036  * Assumes that the process lock is held.
1037  */
1038 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
1039 					int handle)
1040 {
1041 	if (handle >= 0)
1042 		idr_remove(&pdd->alloc_idr, handle);
1043 }
1044 
1045 /* This increments the process->ref counter. */
1046 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
1047 {
1048 	struct kfd_process *p, *ret_p = NULL;
1049 	unsigned int temp;
1050 
1051 	int idx = srcu_read_lock(&kfd_processes_srcu);
1052 
1053 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1054 		if (p->pasid == pasid) {
1055 			kref_get(&p->ref);
1056 			ret_p = p;
1057 			break;
1058 		}
1059 	}
1060 
1061 	srcu_read_unlock(&kfd_processes_srcu, idx);
1062 
1063 	return ret_p;
1064 }
1065 
1066 /* This increments the process->ref counter. */
1067 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
1068 {
1069 	struct kfd_process *p;
1070 
1071 	int idx = srcu_read_lock(&kfd_processes_srcu);
1072 
1073 	p = find_process_by_mm(mm);
1074 	if (p)
1075 		kref_get(&p->ref);
1076 
1077 	srcu_read_unlock(&kfd_processes_srcu, idx);
1078 
1079 	return p;
1080 }
1081 
1082 /* process_evict_queues - Evict all user queues of a process
1083  *
1084  * Eviction is reference-counted per process-device. This means multiple
1085  * evictions from different sources can be nested safely.
1086  */
1087 int kfd_process_evict_queues(struct kfd_process *p)
1088 {
1089 	struct kfd_process_device *pdd;
1090 	int r = 0;
1091 	unsigned int n_evicted = 0;
1092 
1093 	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
1094 		r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
1095 							    &pdd->qpd);
1096 		if (r) {
1097 			pr_err("Failed to evict process queues\n");
1098 			goto fail;
1099 		}
1100 		n_evicted++;
1101 	}
1102 
1103 	return r;
1104 
1105 fail:
1106 	/* To keep state consistent, roll back partial eviction by
1107 	 * restoring queues
1108 	 */
1109 	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
1110 		if (n_evicted == 0)
1111 			break;
1112 		if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
1113 							      &pdd->qpd))
1114 			pr_err("Failed to restore queues\n");
1115 
1116 		n_evicted--;
1117 	}
1118 
1119 	return r;
1120 }
1121 
1122 /* process_restore_queues - Restore all user queues of a process */
1123 int kfd_process_restore_queues(struct kfd_process *p)
1124 {
1125 	struct kfd_process_device *pdd;
1126 	int r, ret = 0;
1127 
1128 	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
1129 		r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
1130 							      &pdd->qpd);
1131 		if (r) {
1132 			pr_err("Failed to restore process queues\n");
1133 			if (!ret)
1134 				ret = r;
1135 		}
1136 	}
1137 
1138 	return ret;
1139 }
1140 
1141 static void evict_process_worker(struct work_struct *work)
1142 {
1143 	int ret;
1144 	struct kfd_process *p;
1145 	struct delayed_work *dwork;
1146 
1147 	dwork = to_delayed_work(work);
1148 
1149 	/* Process termination destroys this worker thread. So during the
1150 	 * lifetime of this thread, kfd_process p will be valid
1151 	 */
1152 	p = container_of(dwork, struct kfd_process, eviction_work);
1153 	WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
1154 		  "Eviction fence mismatch\n");
1155 
1156 	/* Narrow window of overlap between restore and evict work
1157 	 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
1158 	 * unreserves KFD BOs, it is possible to evicted again. But
1159 	 * restore has few more steps of finish. So lets wait for any
1160 	 * previous restore work to complete
1161 	 */
1162 	flush_delayed_work(&p->restore_work);
1163 
1164 	pr_debug("Started evicting pasid 0x%x\n", p->pasid);
1165 	ret = kfd_process_evict_queues(p);
1166 	if (!ret) {
1167 		dma_fence_signal(p->ef);
1168 		dma_fence_put(p->ef);
1169 		p->ef = NULL;
1170 		queue_delayed_work(kfd_restore_wq, &p->restore_work,
1171 				msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
1172 
1173 		pr_debug("Finished evicting pasid 0x%x\n", p->pasid);
1174 	} else
1175 		pr_err("Failed to evict queues of pasid 0x%x\n", p->pasid);
1176 }
1177 
1178 static void restore_process_worker(struct work_struct *work)
1179 {
1180 	struct delayed_work *dwork;
1181 	struct kfd_process *p;
1182 	int ret = 0;
1183 
1184 	dwork = to_delayed_work(work);
1185 
1186 	/* Process termination destroys this worker thread. So during the
1187 	 * lifetime of this thread, kfd_process p will be valid
1188 	 */
1189 	p = container_of(dwork, struct kfd_process, restore_work);
1190 	pr_debug("Started restoring pasid 0x%x\n", p->pasid);
1191 
1192 	/* Setting last_restore_timestamp before successful restoration.
1193 	 * Otherwise this would have to be set by KGD (restore_process_bos)
1194 	 * before KFD BOs are unreserved. If not, the process can be evicted
1195 	 * again before the timestamp is set.
1196 	 * If restore fails, the timestamp will be set again in the next
1197 	 * attempt. This would mean that the minimum GPU quanta would be
1198 	 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
1199 	 * functions)
1200 	 */
1201 
1202 	p->last_restore_timestamp = get_jiffies_64();
1203 	ret = amdgpu_amdkfd_gpuvm_restore_process_bos(p->kgd_process_info,
1204 						     &p->ef);
1205 	if (ret) {
1206 		pr_debug("Failed to restore BOs of pasid 0x%x, retry after %d ms\n",
1207 			 p->pasid, PROCESS_BACK_OFF_TIME_MS);
1208 		ret = queue_delayed_work(kfd_restore_wq, &p->restore_work,
1209 				msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
1210 		WARN(!ret, "reschedule restore work failed\n");
1211 		return;
1212 	}
1213 
1214 	ret = kfd_process_restore_queues(p);
1215 	if (!ret)
1216 		pr_debug("Finished restoring pasid 0x%x\n", p->pasid);
1217 	else
1218 		pr_err("Failed to restore queues of pasid 0x%x\n", p->pasid);
1219 }
1220 
1221 void kfd_suspend_all_processes(void)
1222 {
1223 	struct kfd_process *p;
1224 	unsigned int temp;
1225 	int idx = srcu_read_lock(&kfd_processes_srcu);
1226 
1227 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1228 		cancel_delayed_work_sync(&p->eviction_work);
1229 		cancel_delayed_work_sync(&p->restore_work);
1230 
1231 		if (kfd_process_evict_queues(p))
1232 			pr_err("Failed to suspend process 0x%x\n", p->pasid);
1233 		dma_fence_signal(p->ef);
1234 		dma_fence_put(p->ef);
1235 		p->ef = NULL;
1236 	}
1237 	srcu_read_unlock(&kfd_processes_srcu, idx);
1238 }
1239 
1240 int kfd_resume_all_processes(void)
1241 {
1242 	struct kfd_process *p;
1243 	unsigned int temp;
1244 	int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
1245 
1246 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1247 		if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) {
1248 			pr_err("Restore process %d failed during resume\n",
1249 			       p->pasid);
1250 			ret = -EFAULT;
1251 		}
1252 	}
1253 	srcu_read_unlock(&kfd_processes_srcu, idx);
1254 	return ret;
1255 }
1256 
1257 int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
1258 			  struct vm_area_struct *vma)
1259 {
1260 	struct kfd_process_device *pdd;
1261 	struct qcm_process_device *qpd;
1262 
1263 	if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
1264 		pr_err("Incorrect CWSR mapping size.\n");
1265 		return -EINVAL;
1266 	}
1267 
1268 	pdd = kfd_get_process_device_data(dev, process);
1269 	if (!pdd)
1270 		return -EINVAL;
1271 	qpd = &pdd->qpd;
1272 
1273 	qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1274 					get_order(KFD_CWSR_TBA_TMA_SIZE));
1275 	if (!qpd->cwsr_kaddr) {
1276 		pr_err("Error allocating per process CWSR buffer.\n");
1277 		return -ENOMEM;
1278 	}
1279 
1280 	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
1281 		| VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
1282 	/* Mapping pages to user process */
1283 	return remap_pfn_range(vma, vma->vm_start,
1284 			       PFN_DOWN(__pa(qpd->cwsr_kaddr)),
1285 			       KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
1286 }
1287 
1288 void kfd_flush_tlb(struct kfd_process_device *pdd)
1289 {
1290 	struct kfd_dev *dev = pdd->dev;
1291 
1292 	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
1293 		/* Nothing to flush until a VMID is assigned, which
1294 		 * only happens when the first queue is created.
1295 		 */
1296 		if (pdd->qpd.vmid)
1297 			amdgpu_amdkfd_flush_gpu_tlb_vmid(dev->kgd,
1298 							pdd->qpd.vmid);
1299 	} else {
1300 		amdgpu_amdkfd_flush_gpu_tlb_pasid(dev->kgd,
1301 						pdd->process->pasid);
1302 	}
1303 }
1304 
1305 #if defined(CONFIG_DEBUG_FS)
1306 
1307 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
1308 {
1309 	struct kfd_process *p;
1310 	unsigned int temp;
1311 	int r = 0;
1312 
1313 	int idx = srcu_read_lock(&kfd_processes_srcu);
1314 
1315 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1316 		seq_printf(m, "Process %d PASID 0x%x:\n",
1317 			   p->lead_thread->tgid, p->pasid);
1318 
1319 		mutex_lock(&p->mutex);
1320 		r = pqm_debugfs_mqds(m, &p->pqm);
1321 		mutex_unlock(&p->mutex);
1322 
1323 		if (r)
1324 			break;
1325 	}
1326 
1327 	srcu_read_unlock(&kfd_processes_srcu, idx);
1328 
1329 	return r;
1330 }
1331 
1332 #endif
1333 
1334