2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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>
38 #include "kfd_device_queue_manager.h"
39 #include "kfd_dbgmgr.h"
40 #include "kfd_iommu.h"
43 * List of struct kfd_process (field kfd_process).
44 * Unique/indexed by mm_struct*
46 DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
47 static DEFINE_MUTEX(kfd_processes_mutex);
49 DEFINE_SRCU(kfd_processes_srcu);
51 /* For process termination handling */
52 static struct workqueue_struct *kfd_process_wq;
54 /* Ordered, single-threaded workqueue for restoring evicted
55 * processes. Restoring multiple processes concurrently under memory
56 * pressure can lead to processes blocking each other from validating
57 * their BOs and result in a live-lock situation where processes
58 * remain evicted indefinitely.
60 static struct workqueue_struct *kfd_restore_wq;
62 static struct kfd_process *find_process(const struct task_struct *thread);
63 static void kfd_process_ref_release(struct kref *ref);
64 static struct kfd_process *create_process(const struct task_struct *thread,
67 static void evict_process_worker(struct work_struct *work);
68 static void restore_process_worker(struct work_struct *work);
71 int kfd_process_create_wq(void)
74 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
76 kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq", 0);
78 if (!kfd_process_wq || !kfd_restore_wq) {
79 kfd_process_destroy_wq();
86 void kfd_process_destroy_wq(void)
89 destroy_workqueue(kfd_process_wq);
90 kfd_process_wq = NULL;
93 destroy_workqueue(kfd_restore_wq);
94 kfd_restore_wq = NULL;
98 static void kfd_process_free_gpuvm(struct kgd_mem *mem,
99 struct kfd_process_device *pdd)
101 struct kfd_dev *dev = pdd->dev;
103 dev->kfd2kgd->unmap_memory_to_gpu(dev->kgd, mem, pdd->vm);
104 dev->kfd2kgd->free_memory_of_gpu(dev->kgd, mem);
107 /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
108 * This function should be only called right after the process
109 * is created and when kfd_processes_mutex is still being held
110 * to avoid concurrency. Because of that exclusiveness, we do
111 * not need to take p->mutex.
113 static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
114 uint64_t gpu_va, uint32_t size,
115 uint32_t flags, void **kptr)
117 struct kfd_dev *kdev = pdd->dev;
118 struct kgd_mem *mem = NULL;
122 err = kdev->kfd2kgd->alloc_memory_of_gpu(kdev->kgd, gpu_va, size,
123 pdd->vm, &mem, NULL, flags);
127 err = kdev->kfd2kgd->map_memory_to_gpu(kdev->kgd, mem, pdd->vm);
131 err = kdev->kfd2kgd->sync_memory(kdev->kgd, mem, true);
133 pr_debug("Sync memory failed, wait interrupted by user signal\n");
134 goto sync_memory_failed;
137 /* Create an obj handle so kfd_process_device_remove_obj_handle
138 * will take care of the bo removal when the process finishes.
139 * We do not need to take p->mutex, because the process is just
140 * created and the ioctls have not had the chance to run.
142 handle = kfd_process_device_create_obj_handle(pdd, mem);
150 err = kdev->kfd2kgd->map_gtt_bo_to_kernel(kdev->kgd,
151 (struct kgd_mem *)mem, kptr, NULL);
153 pr_debug("Map GTT BO to kernel failed\n");
154 goto free_obj_handle;
161 kfd_process_device_remove_obj_handle(pdd, handle);
164 kfd_process_free_gpuvm(mem, pdd);
168 kdev->kfd2kgd->free_memory_of_gpu(kdev->kgd, mem);
174 /* kfd_process_device_reserve_ib_mem - Reserve memory inside the
175 * process for IB usage The memory reserved is for KFD to submit
176 * IB to AMDGPU from kernel. If the memory is reserved
177 * successfully, ib_kaddr will have the CPU/kernel
178 * address. Check ib_kaddr before accessing the memory.
180 static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
182 struct qcm_process_device *qpd = &pdd->qpd;
183 uint32_t flags = ALLOC_MEM_FLAGS_GTT |
184 ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
185 ALLOC_MEM_FLAGS_WRITABLE |
186 ALLOC_MEM_FLAGS_EXECUTABLE;
190 if (qpd->ib_kaddr || !qpd->ib_base)
193 /* ib_base is only set for dGPU */
194 ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
199 qpd->ib_kaddr = kaddr;
204 struct kfd_process *kfd_create_process(struct file *filep)
206 struct kfd_process *process;
207 struct task_struct *thread = current;
210 return ERR_PTR(-EINVAL);
212 /* Only the pthreads threading model is supported. */
213 if (thread->group_leader->mm != thread->mm)
214 return ERR_PTR(-EINVAL);
217 * take kfd processes mutex before starting of process creation
218 * so there won't be a case where two threads of the same process
219 * create two kfd_process structures
221 mutex_lock(&kfd_processes_mutex);
223 /* A prior open of /dev/kfd could have already created the process. */
224 process = find_process(thread);
226 pr_debug("Process already found\n");
228 process = create_process(thread, filep);
230 mutex_unlock(&kfd_processes_mutex);
235 struct kfd_process *kfd_get_process(const struct task_struct *thread)
237 struct kfd_process *process;
240 return ERR_PTR(-EINVAL);
242 /* Only the pthreads threading model is supported. */
243 if (thread->group_leader->mm != thread->mm)
244 return ERR_PTR(-EINVAL);
246 process = find_process(thread);
251 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
253 struct kfd_process *process;
255 hash_for_each_possible_rcu(kfd_processes_table, process,
256 kfd_processes, (uintptr_t)mm)
257 if (process->mm == mm)
263 static struct kfd_process *find_process(const struct task_struct *thread)
265 struct kfd_process *p;
268 idx = srcu_read_lock(&kfd_processes_srcu);
269 p = find_process_by_mm(thread->mm);
270 srcu_read_unlock(&kfd_processes_srcu, idx);
275 void kfd_unref_process(struct kfd_process *p)
277 kref_put(&p->ref, kfd_process_ref_release);
280 static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
282 struct kfd_process *p = pdd->process;
287 * Remove all handles from idr and release appropriate
288 * local memory object
290 idr_for_each_entry(&pdd->alloc_idr, mem, id) {
291 struct kfd_process_device *peer_pdd;
293 list_for_each_entry(peer_pdd, &p->per_device_data,
297 peer_pdd->dev->kfd2kgd->unmap_memory_to_gpu(
298 peer_pdd->dev->kgd, mem, peer_pdd->vm);
301 pdd->dev->kfd2kgd->free_memory_of_gpu(pdd->dev->kgd, mem);
302 kfd_process_device_remove_obj_handle(pdd, id);
306 static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
308 struct kfd_process_device *pdd;
310 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
311 kfd_process_device_free_bos(pdd);
314 static void kfd_process_destroy_pdds(struct kfd_process *p)
316 struct kfd_process_device *pdd, *temp;
318 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
320 pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
321 pdd->dev->id, p->pasid);
326 pdd->dev->kfd2kgd->destroy_process_vm(
327 pdd->dev->kgd, pdd->vm);
329 list_del(&pdd->per_device_list);
331 if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
332 free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
333 get_order(KFD_CWSR_TBA_TMA_SIZE));
335 idr_destroy(&pdd->alloc_idr);
341 /* No process locking is needed in this function, because the process
342 * is not findable any more. We must assume that no other thread is
343 * using it any more, otherwise we couldn't safely free the process
344 * structure in the end.
346 static void kfd_process_wq_release(struct work_struct *work)
348 struct kfd_process *p = container_of(work, struct kfd_process,
351 kfd_iommu_unbind_process(p);
353 kfd_process_free_outstanding_kfd_bos(p);
355 kfd_process_destroy_pdds(p);
356 dma_fence_put(p->ef);
358 kfd_event_free_process(p);
360 kfd_pasid_free(p->pasid);
361 kfd_free_process_doorbells(p);
363 mutex_destroy(&p->mutex);
365 put_task_struct(p->lead_thread);
370 static void kfd_process_ref_release(struct kref *ref)
372 struct kfd_process *p = container_of(ref, struct kfd_process, ref);
374 INIT_WORK(&p->release_work, kfd_process_wq_release);
375 queue_work(kfd_process_wq, &p->release_work);
378 static void kfd_process_destroy_delayed(struct rcu_head *rcu)
380 struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
382 kfd_unref_process(p);
385 static void kfd_process_notifier_release(struct mmu_notifier *mn,
386 struct mm_struct *mm)
388 struct kfd_process *p;
389 struct kfd_process_device *pdd = NULL;
392 * The kfd_process structure can not be free because the
393 * mmu_notifier srcu is read locked
395 p = container_of(mn, struct kfd_process, mmu_notifier);
396 if (WARN_ON(p->mm != mm))
399 mutex_lock(&kfd_processes_mutex);
400 hash_del_rcu(&p->kfd_processes);
401 mutex_unlock(&kfd_processes_mutex);
402 synchronize_srcu(&kfd_processes_srcu);
404 cancel_delayed_work_sync(&p->eviction_work);
405 cancel_delayed_work_sync(&p->restore_work);
407 mutex_lock(&p->mutex);
409 /* Iterate over all process device data structures and if the
410 * pdd is in debug mode, we should first force unregistration,
411 * then we will be able to destroy the queues
413 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
414 struct kfd_dev *dev = pdd->dev;
416 mutex_lock(kfd_get_dbgmgr_mutex());
417 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
418 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
419 kfd_dbgmgr_destroy(dev->dbgmgr);
423 mutex_unlock(kfd_get_dbgmgr_mutex());
426 kfd_process_dequeue_from_all_devices(p);
429 /* Indicate to other users that MM is no longer valid */
432 mutex_unlock(&p->mutex);
434 mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
435 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
438 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
439 .release = kfd_process_notifier_release,
442 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
444 unsigned long offset;
445 struct kfd_process_device *pdd;
447 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
448 struct kfd_dev *dev = pdd->dev;
449 struct qcm_process_device *qpd = &pdd->qpd;
451 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
454 offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
455 qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
456 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
459 if (IS_ERR_VALUE(qpd->tba_addr)) {
460 int err = qpd->tba_addr;
462 pr_err("Failure to set tba address. error %d.\n", err);
464 qpd->cwsr_kaddr = NULL;
468 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
470 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
471 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
472 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
478 static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
480 struct kfd_dev *dev = pdd->dev;
481 struct qcm_process_device *qpd = &pdd->qpd;
482 uint32_t flags = ALLOC_MEM_FLAGS_GTT |
483 ALLOC_MEM_FLAGS_NO_SUBSTITUTE | ALLOC_MEM_FLAGS_EXECUTABLE;
487 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
490 /* cwsr_base is only set for dGPU */
491 ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
492 KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
496 qpd->cwsr_kaddr = kaddr;
497 qpd->tba_addr = qpd->cwsr_base;
499 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
501 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
502 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
503 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
508 static struct kfd_process *create_process(const struct task_struct *thread,
511 struct kfd_process *process;
514 process = kzalloc(sizeof(*process), GFP_KERNEL);
517 goto err_alloc_process;
519 process->pasid = kfd_pasid_alloc();
520 if (process->pasid == 0)
521 goto err_alloc_pasid;
523 if (kfd_alloc_process_doorbells(process) < 0)
524 goto err_alloc_doorbells;
526 kref_init(&process->ref);
528 mutex_init(&process->mutex);
530 process->mm = thread->mm;
532 /* register notifier */
533 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
534 err = mmu_notifier_register(&process->mmu_notifier, process->mm);
536 goto err_mmu_notifier;
538 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
539 (uintptr_t)process->mm);
541 process->lead_thread = thread->group_leader;
542 get_task_struct(process->lead_thread);
544 INIT_LIST_HEAD(&process->per_device_data);
546 kfd_event_init_process(process);
548 err = pqm_init(&process->pqm, process);
550 goto err_process_pqm_init;
552 /* init process apertures*/
553 process->is_32bit_user_mode = in_compat_syscall();
554 err = kfd_init_apertures(process);
556 goto err_init_apertures;
558 INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
559 INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
560 process->last_restore_timestamp = get_jiffies_64();
562 err = kfd_process_init_cwsr_apu(process, filep);
569 kfd_process_free_outstanding_kfd_bos(process);
570 kfd_process_destroy_pdds(process);
572 pqm_uninit(&process->pqm);
573 err_process_pqm_init:
574 hash_del_rcu(&process->kfd_processes);
576 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
578 mutex_destroy(&process->mutex);
579 kfd_free_process_doorbells(process);
581 kfd_pasid_free(process->pasid);
588 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
589 struct kfd_process *p)
591 struct kfd_process_device *pdd = NULL;
593 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
600 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
601 struct kfd_process *p)
603 struct kfd_process_device *pdd = NULL;
605 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
610 INIT_LIST_HEAD(&pdd->qpd.queues_list);
611 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
612 pdd->qpd.dqm = dev->dqm;
613 pdd->qpd.pqm = &p->pqm;
614 pdd->qpd.evicted = 0;
616 pdd->bound = PDD_UNBOUND;
617 pdd->already_dequeued = false;
618 list_add(&pdd->per_device_list, &p->per_device_data);
620 /* Init idr used for memory handle translation */
621 idr_init(&pdd->alloc_idr);
627 * kfd_process_device_init_vm - Initialize a VM for a process-device
629 * @pdd: The process-device
630 * @drm_file: Optional pointer to a DRM file descriptor
632 * If @drm_file is specified, it will be used to acquire the VM from
633 * that file descriptor. If successful, the @pdd takes ownership of
634 * the file descriptor.
636 * If @drm_file is NULL, a new VM is created.
638 * Returns 0 on success, -errno on failure.
640 int kfd_process_device_init_vm(struct kfd_process_device *pdd,
641 struct file *drm_file)
643 struct kfd_process *p;
648 return drm_file ? -EBUSY : 0;
654 ret = dev->kfd2kgd->acquire_process_vm(
656 &pdd->vm, &p->kgd_process_info, &p->ef);
658 ret = dev->kfd2kgd->create_process_vm(
659 dev->kgd, &pdd->vm, &p->kgd_process_info, &p->ef);
661 pr_err("Failed to create process VM object\n");
665 ret = kfd_process_device_reserve_ib_mem(pdd);
667 goto err_reserve_ib_mem;
668 ret = kfd_process_device_init_cwsr_dgpu(pdd);
672 pdd->drm_file = drm_file;
678 kfd_process_device_free_bos(pdd);
680 dev->kfd2kgd->destroy_process_vm(dev->kgd, pdd->vm);
687 * Direct the IOMMU to bind the process (specifically the pasid->mm)
689 * Unbinding occurs when the process dies or the device is removed.
691 * Assumes that the process lock is held.
693 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
694 struct kfd_process *p)
696 struct kfd_process_device *pdd;
699 pdd = kfd_get_process_device_data(dev, p);
701 pr_err("Process device data doesn't exist\n");
702 return ERR_PTR(-ENOMEM);
705 err = kfd_iommu_bind_process_to_device(pdd);
709 err = kfd_process_device_init_vm(pdd, NULL);
716 struct kfd_process_device *kfd_get_first_process_device_data(
717 struct kfd_process *p)
719 return list_first_entry(&p->per_device_data,
720 struct kfd_process_device,
724 struct kfd_process_device *kfd_get_next_process_device_data(
725 struct kfd_process *p,
726 struct kfd_process_device *pdd)
728 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
730 return list_next_entry(pdd, per_device_list);
733 bool kfd_has_process_device_data(struct kfd_process *p)
735 return !(list_empty(&p->per_device_data));
738 /* Create specific handle mapped to mem from process local memory idr
739 * Assumes that the process lock is held.
741 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
744 return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
747 /* Translate specific handle from process local memory idr
748 * Assumes that the process lock is held.
750 void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
756 return idr_find(&pdd->alloc_idr, handle);
759 /* Remove specific handle from process local memory idr
760 * Assumes that the process lock is held.
762 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
766 idr_remove(&pdd->alloc_idr, handle);
769 /* This increments the process->ref counter. */
770 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
772 struct kfd_process *p, *ret_p = NULL;
775 int idx = srcu_read_lock(&kfd_processes_srcu);
777 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
778 if (p->pasid == pasid) {
785 srcu_read_unlock(&kfd_processes_srcu, idx);
790 /* This increments the process->ref counter. */
791 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
793 struct kfd_process *p;
795 int idx = srcu_read_lock(&kfd_processes_srcu);
797 p = find_process_by_mm(mm);
801 srcu_read_unlock(&kfd_processes_srcu, idx);
806 /* process_evict_queues - Evict all user queues of a process
808 * Eviction is reference-counted per process-device. This means multiple
809 * evictions from different sources can be nested safely.
811 static int process_evict_queues(struct kfd_process *p)
813 struct kfd_process_device *pdd;
815 unsigned int n_evicted = 0;
817 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
818 r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
821 pr_err("Failed to evict process queues\n");
830 /* To keep state consistent, roll back partial eviction by
833 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
836 if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
838 pr_err("Failed to restore queues\n");
846 /* process_restore_queues - Restore all user queues of a process */
847 static int process_restore_queues(struct kfd_process *p)
849 struct kfd_process_device *pdd;
852 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
853 r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
856 pr_err("Failed to restore process queues\n");
865 static void evict_process_worker(struct work_struct *work)
868 struct kfd_process *p;
869 struct delayed_work *dwork;
871 dwork = to_delayed_work(work);
873 /* Process termination destroys this worker thread. So during the
874 * lifetime of this thread, kfd_process p will be valid
876 p = container_of(dwork, struct kfd_process, eviction_work);
877 WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
878 "Eviction fence mismatch\n");
880 /* Narrow window of overlap between restore and evict work
881 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
882 * unreserves KFD BOs, it is possible to evicted again. But
883 * restore has few more steps of finish. So lets wait for any
884 * previous restore work to complete
886 flush_delayed_work(&p->restore_work);
888 pr_debug("Started evicting pasid %d\n", p->pasid);
889 ret = process_evict_queues(p);
891 dma_fence_signal(p->ef);
892 dma_fence_put(p->ef);
894 queue_delayed_work(kfd_restore_wq, &p->restore_work,
895 msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
897 pr_debug("Finished evicting pasid %d\n", p->pasid);
899 pr_err("Failed to evict queues of pasid %d\n", p->pasid);
902 static void restore_process_worker(struct work_struct *work)
904 struct delayed_work *dwork;
905 struct kfd_process *p;
906 struct kfd_process_device *pdd;
909 dwork = to_delayed_work(work);
911 /* Process termination destroys this worker thread. So during the
912 * lifetime of this thread, kfd_process p will be valid
914 p = container_of(dwork, struct kfd_process, restore_work);
916 /* Call restore_process_bos on the first KGD device. This function
917 * takes care of restoring the whole process including other devices.
918 * Restore can fail if enough memory is not available. If so,
921 pdd = list_first_entry(&p->per_device_data,
922 struct kfd_process_device,
925 pr_debug("Started restoring pasid %d\n", p->pasid);
927 /* Setting last_restore_timestamp before successful restoration.
928 * Otherwise this would have to be set by KGD (restore_process_bos)
929 * before KFD BOs are unreserved. If not, the process can be evicted
930 * again before the timestamp is set.
931 * If restore fails, the timestamp will be set again in the next
932 * attempt. This would mean that the minimum GPU quanta would be
933 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
937 p->last_restore_timestamp = get_jiffies_64();
938 ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info,
941 pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n",
942 p->pasid, PROCESS_BACK_OFF_TIME_MS);
943 ret = queue_delayed_work(kfd_restore_wq, &p->restore_work,
944 msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
945 WARN(!ret, "reschedule restore work failed\n");
949 ret = process_restore_queues(p);
951 pr_debug("Finished restoring pasid %d\n", p->pasid);
953 pr_err("Failed to restore queues of pasid %d\n", p->pasid);
956 void kfd_suspend_all_processes(void)
958 struct kfd_process *p;
960 int idx = srcu_read_lock(&kfd_processes_srcu);
962 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
963 cancel_delayed_work_sync(&p->eviction_work);
964 cancel_delayed_work_sync(&p->restore_work);
966 if (process_evict_queues(p))
967 pr_err("Failed to suspend process %d\n", p->pasid);
968 dma_fence_signal(p->ef);
969 dma_fence_put(p->ef);
972 srcu_read_unlock(&kfd_processes_srcu, idx);
975 int kfd_resume_all_processes(void)
977 struct kfd_process *p;
979 int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
981 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
982 if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) {
983 pr_err("Restore process %d failed during resume\n",
988 srcu_read_unlock(&kfd_processes_srcu, idx);
992 int kfd_reserved_mem_mmap(struct kfd_process *process,
993 struct vm_area_struct *vma)
995 struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
996 struct kfd_process_device *pdd;
997 struct qcm_process_device *qpd;
1001 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
1002 pr_err("Incorrect CWSR mapping size.\n");
1006 pdd = kfd_get_process_device_data(dev, process);
1011 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1012 get_order(KFD_CWSR_TBA_TMA_SIZE));
1013 if (!qpd->cwsr_kaddr) {
1014 pr_err("Error allocating per process CWSR buffer.\n");
1018 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
1019 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
1020 /* Mapping pages to user process */
1021 return remap_pfn_range(vma, vma->vm_start,
1022 PFN_DOWN(__pa(qpd->cwsr_kaddr)),
1023 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
1026 void kfd_flush_tlb(struct kfd_process_device *pdd)
1028 struct kfd_dev *dev = pdd->dev;
1029 const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
1031 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
1032 /* Nothing to flush until a VMID is assigned, which
1033 * only happens when the first queue is created.
1036 f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid);
1038 f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid);
1042 #if defined(CONFIG_DEBUG_FS)
1044 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
1046 struct kfd_process *p;
1050 int idx = srcu_read_lock(&kfd_processes_srcu);
1052 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1053 seq_printf(m, "Process %d PASID %d:\n",
1054 p->lead_thread->tgid, p->pasid);
1056 mutex_lock(&p->mutex);
1057 r = pqm_debugfs_mqds(m, &p->pqm);
1058 mutex_unlock(&p->mutex);
1064 srcu_read_unlock(&kfd_processes_srcu, idx);