aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorFelix Kuehling <Felix.Kuehling@amd.com>2018-02-06 20:32:45 -0500
committerOded Gabbay <oded.gabbay@gmail.com>2018-02-06 20:32:45 -0500
commit26103436da003327017af325483b6150a3b855cc (patch)
treee7249d2b7e73e3969067680dd6496945981220f0 /drivers
parent403575c44e61722ae443b47df66e188b367d7324 (diff)
drm/amdkfd: Implement KFD process eviction/restore
When the TTM memory manager in KGD evicts BOs, all user mode queues potentially accessing these BOs must be evicted temporarily. Once user mode queues are evicted, the eviction fence is signaled, allowing the migration of the BO to proceed. A delayed worker is scheduled to restore all the BOs belonging to the evicted process and restart its queues. During suspend/resume of the GPU we also evict all processes to allow KGD to save BOs in system memory, since VRAM will be lost. v2: * Account for eviction when updating of q->is_active in MQD manager Signed-off-by: Harish Kasiviswanathan <Harish.Kasiviswanathan@amd.com> Signed-off-by: Felix Kuehling <Felix.Kuehling@amd.com> Acked-by: Oded Gabbay <oded.gabbay@gmail.com> Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_device.c65
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c219
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h9
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_module.c2
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c9
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c6
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_priv.h32
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_process.c213
8 files changed, 547 insertions, 8 deletions
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device.c b/drivers/gpu/drm/amd/amdkfd/kfd_device.c
index 4ac2d61b65d5..3346699960dd 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device.c
@@ -33,6 +33,7 @@
33#include "kfd_iommu.h" 33#include "kfd_iommu.h"
34 34
35#define MQD_SIZE_ALIGNED 768 35#define MQD_SIZE_ALIGNED 768
36static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
36 37
37#ifdef KFD_SUPPORT_IOMMU_V2 38#ifdef KFD_SUPPORT_IOMMU_V2
38static const struct kfd_device_info kaveri_device_info = { 39static const struct kfd_device_info kaveri_device_info = {
@@ -469,6 +470,10 @@ void kgd2kfd_suspend(struct kfd_dev *kfd)
469 if (!kfd->init_complete) 470 if (!kfd->init_complete)
470 return; 471 return;
471 472
473 /* For first KFD device suspend all the KFD processes */
474 if (atomic_inc_return(&kfd_device_suspended) == 1)
475 kfd_suspend_all_processes();
476
472 kfd->dqm->ops.stop(kfd->dqm); 477 kfd->dqm->ops.stop(kfd->dqm);
473 478
474 kfd_iommu_suspend(kfd); 479 kfd_iommu_suspend(kfd);
@@ -476,11 +481,21 @@ void kgd2kfd_suspend(struct kfd_dev *kfd)
476 481
477int kgd2kfd_resume(struct kfd_dev *kfd) 482int kgd2kfd_resume(struct kfd_dev *kfd)
478{ 483{
484 int ret, count;
485
479 if (!kfd->init_complete) 486 if (!kfd->init_complete)
480 return 0; 487 return 0;
481 488
482 return kfd_resume(kfd); 489 ret = kfd_resume(kfd);
490 if (ret)
491 return ret;
492
493 count = atomic_dec_return(&kfd_device_suspended);
494 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
495 if (count == 0)
496 ret = kfd_resume_all_processes();
483 497
498 return ret;
484} 499}
485 500
486static int kfd_resume(struct kfd_dev *kfd) 501static int kfd_resume(struct kfd_dev *kfd)
@@ -526,6 +541,54 @@ void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
526 spin_unlock(&kfd->interrupt_lock); 541 spin_unlock(&kfd->interrupt_lock);
527} 542}
528 543
544/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
545 * prepare for safe eviction of KFD BOs that belong to the specified
546 * process.
547 *
548 * @mm: mm_struct that identifies the specified KFD process
549 * @fence: eviction fence attached to KFD process BOs
550 *
551 */
552int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
553 struct dma_fence *fence)
554{
555 struct kfd_process *p;
556 unsigned long active_time;
557 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
558
559 if (!fence)
560 return -EINVAL;
561
562 if (dma_fence_is_signaled(fence))
563 return 0;
564
565 p = kfd_lookup_process_by_mm(mm);
566 if (!p)
567 return -ENODEV;
568
569 if (fence->seqno == p->last_eviction_seqno)
570 goto out;
571
572 p->last_eviction_seqno = fence->seqno;
573
574 /* Avoid KFD process starvation. Wait for at least
575 * PROCESS_ACTIVE_TIME_MS before evicting the process again
576 */
577 active_time = get_jiffies_64() - p->last_restore_timestamp;
578 if (delay_jiffies > active_time)
579 delay_jiffies -= active_time;
580 else
581 delay_jiffies = 0;
582
583 /* During process initialization eviction_work.dwork is initialized
584 * to kfd_evict_bo_worker
585 */
586 schedule_delayed_work(&p->eviction_work, delay_jiffies);
587out:
588 kfd_unref_process(p);
589 return 0;
590}
591
529static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 592static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
530 unsigned int chunk_size) 593 unsigned int chunk_size)
531{ 594{
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
index b7d06395d592..b3b6dab71638 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
@@ -21,10 +21,11 @@
21 * 21 *
22 */ 22 */
23 23
24#include <linux/ratelimit.h>
25#include <linux/printk.h>
24#include <linux/slab.h> 26#include <linux/slab.h>
25#include <linux/list.h> 27#include <linux/list.h>
26#include <linux/types.h> 28#include <linux/types.h>
27#include <linux/printk.h>
28#include <linux/bitops.h> 29#include <linux/bitops.h>
29#include <linux/sched.h> 30#include <linux/sched.h>
30#include "kfd_priv.h" 31#include "kfd_priv.h"
@@ -180,6 +181,14 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm,
180 goto out_unlock; 181 goto out_unlock;
181 } 182 }
182 q->properties.vmid = qpd->vmid; 183 q->properties.vmid = qpd->vmid;
184 /*
185 * Eviction state logic: we only mark active queues as evicted
186 * to avoid the overhead of restoring inactive queues later
187 */
188 if (qpd->evicted)
189 q->properties.is_evicted = (q->properties.queue_size > 0 &&
190 q->properties.queue_percent > 0 &&
191 q->properties.queue_address != 0);
183 192
184 q->properties.tba_addr = qpd->tba_addr; 193 q->properties.tba_addr = qpd->tba_addr;
185 q->properties.tma_addr = qpd->tma_addr; 194 q->properties.tma_addr = qpd->tma_addr;
@@ -377,15 +386,29 @@ static int update_queue(struct device_queue_manager *dqm, struct queue *q)
377{ 386{
378 int retval; 387 int retval;
379 struct mqd_manager *mqd; 388 struct mqd_manager *mqd;
389 struct kfd_process_device *pdd;
380 bool prev_active = false; 390 bool prev_active = false;
381 391
382 mutex_lock(&dqm->lock); 392 mutex_lock(&dqm->lock);
393 pdd = kfd_get_process_device_data(q->device, q->process);
394 if (!pdd) {
395 retval = -ENODEV;
396 goto out_unlock;
397 }
383 mqd = dqm->ops.get_mqd_manager(dqm, 398 mqd = dqm->ops.get_mqd_manager(dqm,
384 get_mqd_type_from_queue_type(q->properties.type)); 399 get_mqd_type_from_queue_type(q->properties.type));
385 if (!mqd) { 400 if (!mqd) {
386 retval = -ENOMEM; 401 retval = -ENOMEM;
387 goto out_unlock; 402 goto out_unlock;
388 } 403 }
404 /*
405 * Eviction state logic: we only mark active queues as evicted
406 * to avoid the overhead of restoring inactive queues later
407 */
408 if (pdd->qpd.evicted)
409 q->properties.is_evicted = (q->properties.queue_size > 0 &&
410 q->properties.queue_percent > 0 &&
411 q->properties.queue_address != 0);
389 412
390 /* Save previous activity state for counters */ 413 /* Save previous activity state for counters */
391 prev_active = q->properties.is_active; 414 prev_active = q->properties.is_active;
@@ -457,6 +480,187 @@ static struct mqd_manager *get_mqd_manager(
457 return mqd; 480 return mqd;
458} 481}
459 482
483static int evict_process_queues_nocpsch(struct device_queue_manager *dqm,
484 struct qcm_process_device *qpd)
485{
486 struct queue *q;
487 struct mqd_manager *mqd;
488 struct kfd_process_device *pdd;
489 int retval = 0;
490
491 mutex_lock(&dqm->lock);
492 if (qpd->evicted++ > 0) /* already evicted, do nothing */
493 goto out;
494
495 pdd = qpd_to_pdd(qpd);
496 pr_info_ratelimited("Evicting PASID %u queues\n",
497 pdd->process->pasid);
498
499 /* unactivate all active queues on the qpd */
500 list_for_each_entry(q, &qpd->queues_list, list) {
501 if (!q->properties.is_active)
502 continue;
503 mqd = dqm->ops.get_mqd_manager(dqm,
504 get_mqd_type_from_queue_type(q->properties.type));
505 if (!mqd) { /* should not be here */
506 pr_err("Cannot evict queue, mqd mgr is NULL\n");
507 retval = -ENOMEM;
508 goto out;
509 }
510 q->properties.is_evicted = true;
511 q->properties.is_active = false;
512 retval = mqd->destroy_mqd(mqd, q->mqd,
513 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN,
514 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
515 if (retval)
516 goto out;
517 dqm->queue_count--;
518 }
519
520out:
521 mutex_unlock(&dqm->lock);
522 return retval;
523}
524
525static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
526 struct qcm_process_device *qpd)
527{
528 struct queue *q;
529 struct kfd_process_device *pdd;
530 int retval = 0;
531
532 mutex_lock(&dqm->lock);
533 if (qpd->evicted++ > 0) /* already evicted, do nothing */
534 goto out;
535
536 pdd = qpd_to_pdd(qpd);
537 pr_info_ratelimited("Evicting PASID %u queues\n",
538 pdd->process->pasid);
539
540 /* unactivate all active queues on the qpd */
541 list_for_each_entry(q, &qpd->queues_list, list) {
542 if (!q->properties.is_active)
543 continue;
544 q->properties.is_evicted = true;
545 q->properties.is_active = false;
546 dqm->queue_count--;
547 }
548 retval = execute_queues_cpsch(dqm,
549 qpd->is_debug ?
550 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES :
551 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
552
553out:
554 mutex_unlock(&dqm->lock);
555 return retval;
556}
557
558static int restore_process_queues_nocpsch(struct device_queue_manager *dqm,
559 struct qcm_process_device *qpd)
560{
561 struct queue *q;
562 struct mqd_manager *mqd;
563 struct kfd_process_device *pdd;
564 uint32_t pd_base;
565 int retval = 0;
566
567 pdd = qpd_to_pdd(qpd);
568 /* Retrieve PD base */
569 pd_base = dqm->dev->kfd2kgd->get_process_page_dir(pdd->vm);
570
571 mutex_lock(&dqm->lock);
572 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
573 goto out;
574 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
575 qpd->evicted--;
576 goto out;
577 }
578
579 pr_info_ratelimited("Restoring PASID %u queues\n",
580 pdd->process->pasid);
581
582 /* Update PD Base in QPD */
583 qpd->page_table_base = pd_base;
584 pr_debug("Updated PD address to 0x%08x\n", pd_base);
585
586 if (!list_empty(&qpd->queues_list)) {
587 dqm->dev->kfd2kgd->set_vm_context_page_table_base(
588 dqm->dev->kgd,
589 qpd->vmid,
590 qpd->page_table_base);
591 kfd_flush_tlb(pdd);
592 }
593
594 /* activate all active queues on the qpd */
595 list_for_each_entry(q, &qpd->queues_list, list) {
596 if (!q->properties.is_evicted)
597 continue;
598 mqd = dqm->ops.get_mqd_manager(dqm,
599 get_mqd_type_from_queue_type(q->properties.type));
600 if (!mqd) { /* should not be here */
601 pr_err("Cannot restore queue, mqd mgr is NULL\n");
602 retval = -ENOMEM;
603 goto out;
604 }
605 q->properties.is_evicted = false;
606 q->properties.is_active = true;
607 retval = mqd->load_mqd(mqd, q->mqd, q->pipe,
608 q->queue, &q->properties,
609 q->process->mm);
610 if (retval)
611 goto out;
612 dqm->queue_count++;
613 }
614 qpd->evicted = 0;
615out:
616 mutex_unlock(&dqm->lock);
617 return retval;
618}
619
620static int restore_process_queues_cpsch(struct device_queue_manager *dqm,
621 struct qcm_process_device *qpd)
622{
623 struct queue *q;
624 struct kfd_process_device *pdd;
625 uint32_t pd_base;
626 int retval = 0;
627
628 pdd = qpd_to_pdd(qpd);
629 /* Retrieve PD base */
630 pd_base = dqm->dev->kfd2kgd->get_process_page_dir(pdd->vm);
631
632 mutex_lock(&dqm->lock);
633 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
634 goto out;
635 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
636 qpd->evicted--;
637 goto out;
638 }
639
640 pr_info_ratelimited("Restoring PASID %u queues\n",
641 pdd->process->pasid);
642
643 /* Update PD Base in QPD */
644 qpd->page_table_base = pd_base;
645 pr_debug("Updated PD address to 0x%08x\n", pd_base);
646
647 /* activate all active queues on the qpd */
648 list_for_each_entry(q, &qpd->queues_list, list) {
649 if (!q->properties.is_evicted)
650 continue;
651 q->properties.is_evicted = false;
652 q->properties.is_active = true;
653 dqm->queue_count++;
654 }
655 retval = execute_queues_cpsch(dqm,
656 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
657 if (!retval)
658 qpd->evicted = 0;
659out:
660 mutex_unlock(&dqm->lock);
661 return retval;
662}
663
460static int register_process(struct device_queue_manager *dqm, 664static int register_process(struct device_queue_manager *dqm,
461 struct qcm_process_device *qpd) 665 struct qcm_process_device *qpd)
462{ 666{
@@ -853,6 +1057,14 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
853 retval = -ENOMEM; 1057 retval = -ENOMEM;
854 goto out; 1058 goto out;
855 } 1059 }
1060 /*
1061 * Eviction state logic: we only mark active queues as evicted
1062 * to avoid the overhead of restoring inactive queues later
1063 */
1064 if (qpd->evicted)
1065 q->properties.is_evicted = (q->properties.queue_size > 0 &&
1066 q->properties.queue_percent > 0 &&
1067 q->properties.queue_address != 0);
856 1068
857 dqm->asic_ops.init_sdma_vm(dqm, q, qpd); 1069 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
858 1070
@@ -1291,6 +1503,8 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev)
1291 dqm->ops.set_cache_memory_policy = set_cache_memory_policy; 1503 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
1292 dqm->ops.set_trap_handler = set_trap_handler; 1504 dqm->ops.set_trap_handler = set_trap_handler;
1293 dqm->ops.process_termination = process_termination_cpsch; 1505 dqm->ops.process_termination = process_termination_cpsch;
1506 dqm->ops.evict_process_queues = evict_process_queues_cpsch;
1507 dqm->ops.restore_process_queues = restore_process_queues_cpsch;
1294 break; 1508 break;
1295 case KFD_SCHED_POLICY_NO_HWS: 1509 case KFD_SCHED_POLICY_NO_HWS:
1296 /* initialize dqm for no cp scheduling */ 1510 /* initialize dqm for no cp scheduling */
@@ -1307,6 +1521,9 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev)
1307 dqm->ops.set_cache_memory_policy = set_cache_memory_policy; 1521 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
1308 dqm->ops.set_trap_handler = set_trap_handler; 1522 dqm->ops.set_trap_handler = set_trap_handler;
1309 dqm->ops.process_termination = process_termination_nocpsch; 1523 dqm->ops.process_termination = process_termination_nocpsch;
1524 dqm->ops.evict_process_queues = evict_process_queues_nocpsch;
1525 dqm->ops.restore_process_queues =
1526 restore_process_queues_nocpsch;
1310 break; 1527 break;
1311 default: 1528 default:
1312 pr_err("Invalid scheduling policy %d\n", dqm->sched_policy); 1529 pr_err("Invalid scheduling policy %d\n", dqm->sched_policy);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h
index 68be0aaff3f4..412beff3281d 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h
@@ -79,6 +79,10 @@ struct device_process_node {
79 * 79 *
80 * @process_termination: Clears all process queues belongs to that device. 80 * @process_termination: Clears all process queues belongs to that device.
81 * 81 *
82 * @evict_process_queues: Evict all active queues of a process
83 *
84 * @restore_process_queues: Restore all evicted queues queues of a process
85 *
82 */ 86 */
83 87
84struct device_queue_manager_ops { 88struct device_queue_manager_ops {
@@ -129,6 +133,11 @@ struct device_queue_manager_ops {
129 133
130 int (*process_termination)(struct device_queue_manager *dqm, 134 int (*process_termination)(struct device_queue_manager *dqm,
131 struct qcm_process_device *qpd); 135 struct qcm_process_device *qpd);
136
137 int (*evict_process_queues)(struct device_queue_manager *dqm,
138 struct qcm_process_device *qpd);
139 int (*restore_process_queues)(struct device_queue_manager *dqm,
140 struct qcm_process_device *qpd);
132}; 141};
133 142
134struct device_queue_manager_asic_ops { 143struct device_queue_manager_asic_ops {
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_module.c b/drivers/gpu/drm/amd/amdkfd/kfd_module.c
index 3ac72bed4f31..65574c6a10b3 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_module.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_module.c
@@ -43,6 +43,8 @@ static const struct kgd2kfd_calls kgd2kfd = {
43 .interrupt = kgd2kfd_interrupt, 43 .interrupt = kgd2kfd_interrupt,
44 .suspend = kgd2kfd_suspend, 44 .suspend = kgd2kfd_suspend,
45 .resume = kgd2kfd_resume, 45 .resume = kgd2kfd_resume,
46 .schedule_evict_and_restore_process =
47 kgd2kfd_schedule_evict_and_restore_process,
46}; 48};
47 49
48int sched_policy = KFD_SCHED_POLICY_HWS; 50int sched_policy = KFD_SCHED_POLICY_HWS;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c
index fbe3f83ba685..c00c325ed3c9 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c
@@ -202,7 +202,8 @@ static int __update_mqd(struct mqd_manager *mm, void *mqd,
202 202
203 q->is_active = (q->queue_size > 0 && 203 q->is_active = (q->queue_size > 0 &&
204 q->queue_address != 0 && 204 q->queue_address != 0 &&
205 q->queue_percent > 0); 205 q->queue_percent > 0 &&
206 !q->is_evicted);
206 207
207 return 0; 208 return 0;
208} 209}
@@ -245,7 +246,8 @@ static int update_mqd_sdma(struct mqd_manager *mm, void *mqd,
245 246
246 q->is_active = (q->queue_size > 0 && 247 q->is_active = (q->queue_size > 0 &&
247 q->queue_address != 0 && 248 q->queue_address != 0 &&
248 q->queue_percent > 0); 249 q->queue_percent > 0 &&
250 !q->is_evicted);
249 251
250 return 0; 252 return 0;
251} 253}
@@ -377,7 +379,8 @@ static int update_mqd_hiq(struct mqd_manager *mm, void *mqd,
377 379
378 q->is_active = (q->queue_size > 0 && 380 q->is_active = (q->queue_size > 0 &&
379 q->queue_address != 0 && 381 q->queue_address != 0 &&
380 q->queue_percent > 0); 382 q->queue_percent > 0 &&
383 !q->is_evicted);
381 384
382 return 0; 385 return 0;
383} 386}
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c
index 58221c1fc917..89e4242e43e7 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c
@@ -198,7 +198,8 @@ static int __update_mqd(struct mqd_manager *mm, void *mqd,
198 198
199 q->is_active = (q->queue_size > 0 && 199 q->is_active = (q->queue_size > 0 &&
200 q->queue_address != 0 && 200 q->queue_address != 0 &&
201 q->queue_percent > 0); 201 q->queue_percent > 0 &&
202 !q->is_evicted);
202 203
203 return 0; 204 return 0;
204} 205}
@@ -342,7 +343,8 @@ static int update_mqd_sdma(struct mqd_manager *mm, void *mqd,
342 343
343 q->is_active = (q->queue_size > 0 && 344 q->is_active = (q->queue_size > 0 &&
344 q->queue_address != 0 && 345 q->queue_address != 0 &&
345 q->queue_percent > 0); 346 q->queue_percent > 0 &&
347 !q->is_evicted);
346 348
347 return 0; 349 return 0;
348} 350}
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 56c2e368f702..cac7aa258162 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -335,7 +335,11 @@ enum kfd_queue_format {
335 * @is_interop: Defines if this is a interop queue. Interop queue means that 335 * @is_interop: Defines if this is a interop queue. Interop queue means that
336 * the queue can access both graphics and compute resources. 336 * the queue can access both graphics and compute resources.
337 * 337 *
338 * @is_active: Defines if the queue is active or not. 338 * @is_evicted: Defines if the queue is evicted. Only active queues
339 * are evicted, rendering them inactive.
340 *
341 * @is_active: Defines if the queue is active or not. @is_active and
342 * @is_evicted are protected by the DQM lock.
339 * 343 *
340 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid 344 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
341 * of the queue. 345 * of the queue.
@@ -357,6 +361,7 @@ struct queue_properties {
357 uint32_t __iomem *doorbell_ptr; 361 uint32_t __iomem *doorbell_ptr;
358 uint32_t doorbell_off; 362 uint32_t doorbell_off;
359 bool is_interop; 363 bool is_interop;
364 bool is_evicted;
360 bool is_active; 365 bool is_active;
361 /* Not relevant for user mode queues in cp scheduling */ 366 /* Not relevant for user mode queues in cp scheduling */
362 unsigned int vmid; 367 unsigned int vmid;
@@ -460,6 +465,7 @@ struct qcm_process_device {
460 unsigned int queue_count; 465 unsigned int queue_count;
461 unsigned int vmid; 466 unsigned int vmid;
462 bool is_debug; 467 bool is_debug;
468 unsigned int evicted; /* eviction counter, 0=active */
463 469
464 /* This flag tells if we should reset all wavefronts on 470 /* This flag tells if we should reset all wavefronts on
465 * process termination 471 * process termination
@@ -486,6 +492,17 @@ struct qcm_process_device {
486 uint64_t tma_addr; 492 uint64_t tma_addr;
487}; 493};
488 494
495/* KFD Memory Eviction */
496
497/* Approx. wait time before attempting to restore evicted BOs */
498#define PROCESS_RESTORE_TIME_MS 100
499/* Approx. back off time if restore fails due to lack of memory */
500#define PROCESS_BACK_OFF_TIME_MS 100
501/* Approx. time before evicting the process again */
502#define PROCESS_ACTIVE_TIME_MS 10
503
504int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
505 struct dma_fence *fence);
489 506
490enum kfd_pdd_bound { 507enum kfd_pdd_bound {
491 PDD_UNBOUND = 0, 508 PDD_UNBOUND = 0,
@@ -600,6 +617,16 @@ struct kfd_process {
600 * during restore 617 * during restore
601 */ 618 */
602 struct dma_fence *ef; 619 struct dma_fence *ef;
620
621 /* Work items for evicting and restoring BOs */
622 struct delayed_work eviction_work;
623 struct delayed_work restore_work;
624 /* seqno of the last scheduled eviction */
625 unsigned int last_eviction_seqno;
626 /* Approx. the last timestamp (in jiffies) when the process was
627 * restored after an eviction
628 */
629 unsigned long last_restore_timestamp;
603}; 630};
604 631
605#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */ 632#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
@@ -629,7 +656,10 @@ void kfd_process_destroy_wq(void);
629struct kfd_process *kfd_create_process(struct file *filep); 656struct kfd_process *kfd_create_process(struct file *filep);
630struct kfd_process *kfd_get_process(const struct task_struct *); 657struct kfd_process *kfd_get_process(const struct task_struct *);
631struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid); 658struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
659struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
632void kfd_unref_process(struct kfd_process *p); 660void kfd_unref_process(struct kfd_process *p);
661void kfd_suspend_all_processes(void);
662int kfd_resume_all_processes(void);
633 663
634struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, 664struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
635 struct kfd_process *p); 665 struct kfd_process *p);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index cf4fa25cc430..18b2b86ad503 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -55,6 +55,9 @@ static struct kfd_process *create_process(const struct task_struct *thread,
55 struct file *filep); 55 struct file *filep);
56static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep); 56static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep);
57 57
58static void evict_process_worker(struct work_struct *work);
59static void restore_process_worker(struct work_struct *work);
60
58 61
59void kfd_process_create_wq(void) 62void kfd_process_create_wq(void)
60{ 63{
@@ -230,6 +233,9 @@ static void kfd_process_notifier_release(struct mmu_notifier *mn,
230 mutex_unlock(&kfd_processes_mutex); 233 mutex_unlock(&kfd_processes_mutex);
231 synchronize_srcu(&kfd_processes_srcu); 234 synchronize_srcu(&kfd_processes_srcu);
232 235
236 cancel_delayed_work_sync(&p->eviction_work);
237 cancel_delayed_work_sync(&p->restore_work);
238
233 mutex_lock(&p->mutex); 239 mutex_lock(&p->mutex);
234 240
235 /* Iterate over all process device data structures and if the 241 /* Iterate over all process device data structures and if the
@@ -351,6 +357,10 @@ static struct kfd_process *create_process(const struct task_struct *thread,
351 if (err != 0) 357 if (err != 0)
352 goto err_init_apertures; 358 goto err_init_apertures;
353 359
360 INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
361 INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
362 process->last_restore_timestamp = get_jiffies_64();
363
354 err = kfd_process_init_cwsr(process, filep); 364 err = kfd_process_init_cwsr(process, filep);
355 if (err) 365 if (err)
356 goto err_init_cwsr; 366 goto err_init_cwsr;
@@ -402,6 +412,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
402 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); 412 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
403 pdd->qpd.dqm = dev->dqm; 413 pdd->qpd.dqm = dev->dqm;
404 pdd->qpd.pqm = &p->pqm; 414 pdd->qpd.pqm = &p->pqm;
415 pdd->qpd.evicted = 0;
405 pdd->process = p; 416 pdd->process = p;
406 pdd->bound = PDD_UNBOUND; 417 pdd->bound = PDD_UNBOUND;
407 pdd->already_dequeued = false; 418 pdd->already_dequeued = false;
@@ -490,6 +501,208 @@ struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
490 return ret_p; 501 return ret_p;
491} 502}
492 503
504/* This increments the process->ref counter. */
505struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
506{
507 struct kfd_process *p;
508
509 int idx = srcu_read_lock(&kfd_processes_srcu);
510
511 p = find_process_by_mm(mm);
512 if (p)
513 kref_get(&p->ref);
514
515 srcu_read_unlock(&kfd_processes_srcu, idx);
516
517 return p;
518}
519
520/* process_evict_queues - Evict all user queues of a process
521 *
522 * Eviction is reference-counted per process-device. This means multiple
523 * evictions from different sources can be nested safely.
524 */
525static int process_evict_queues(struct kfd_process *p)
526{
527 struct kfd_process_device *pdd;
528 int r = 0;
529 unsigned int n_evicted = 0;
530
531 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
532 r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
533 &pdd->qpd);
534 if (r) {
535 pr_err("Failed to evict process queues\n");
536 goto fail;
537 }
538 n_evicted++;
539 }
540
541 return r;
542
543fail:
544 /* To keep state consistent, roll back partial eviction by
545 * restoring queues
546 */
547 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
548 if (n_evicted == 0)
549 break;
550 if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
551 &pdd->qpd))
552 pr_err("Failed to restore queues\n");
553
554 n_evicted--;
555 }
556
557 return r;
558}
559
560/* process_restore_queues - Restore all user queues of a process */
561static int process_restore_queues(struct kfd_process *p)
562{
563 struct kfd_process_device *pdd;
564 int r, ret = 0;
565
566 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
567 r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
568 &pdd->qpd);
569 if (r) {
570 pr_err("Failed to restore process queues\n");
571 if (!ret)
572 ret = r;
573 }
574 }
575
576 return ret;
577}
578
579static void evict_process_worker(struct work_struct *work)
580{
581 int ret;
582 struct kfd_process *p;
583 struct delayed_work *dwork;
584
585 dwork = to_delayed_work(work);
586
587 /* Process termination destroys this worker thread. So during the
588 * lifetime of this thread, kfd_process p will be valid
589 */
590 p = container_of(dwork, struct kfd_process, eviction_work);
591 WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
592 "Eviction fence mismatch\n");
593
594 /* Narrow window of overlap between restore and evict work
595 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
596 * unreserves KFD BOs, it is possible to evicted again. But
597 * restore has few more steps of finish. So lets wait for any
598 * previous restore work to complete
599 */
600 flush_delayed_work(&p->restore_work);
601
602 pr_debug("Started evicting pasid %d\n", p->pasid);
603 ret = process_evict_queues(p);
604 if (!ret) {
605 dma_fence_signal(p->ef);
606 dma_fence_put(p->ef);
607 p->ef = NULL;
608 schedule_delayed_work(&p->restore_work,
609 msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
610
611 pr_debug("Finished evicting pasid %d\n", p->pasid);
612 } else
613 pr_err("Failed to evict queues of pasid %d\n", p->pasid);
614}
615
616static void restore_process_worker(struct work_struct *work)
617{
618 struct delayed_work *dwork;
619 struct kfd_process *p;
620 struct kfd_process_device *pdd;
621 int ret = 0;
622
623 dwork = to_delayed_work(work);
624
625 /* Process termination destroys this worker thread. So during the
626 * lifetime of this thread, kfd_process p will be valid
627 */
628 p = container_of(dwork, struct kfd_process, restore_work);
629
630 /* Call restore_process_bos on the first KGD device. This function
631 * takes care of restoring the whole process including other devices.
632 * Restore can fail if enough memory is not available. If so,
633 * reschedule again.
634 */
635 pdd = list_first_entry(&p->per_device_data,
636 struct kfd_process_device,
637 per_device_list);
638
639 pr_debug("Started restoring pasid %d\n", p->pasid);
640
641 /* Setting last_restore_timestamp before successful restoration.
642 * Otherwise this would have to be set by KGD (restore_process_bos)
643 * before KFD BOs are unreserved. If not, the process can be evicted
644 * again before the timestamp is set.
645 * If restore fails, the timestamp will be set again in the next
646 * attempt. This would mean that the minimum GPU quanta would be
647 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
648 * functions)
649 */
650
651 p->last_restore_timestamp = get_jiffies_64();
652 ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info,
653 &p->ef);
654 if (ret) {
655 pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n",
656 p->pasid, PROCESS_BACK_OFF_TIME_MS);
657 ret = schedule_delayed_work(&p->restore_work,
658 msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
659 WARN(!ret, "reschedule restore work failed\n");
660 return;
661 }
662
663 ret = process_restore_queues(p);
664 if (!ret)
665 pr_debug("Finished restoring pasid %d\n", p->pasid);
666 else
667 pr_err("Failed to restore queues of pasid %d\n", p->pasid);
668}
669
670void kfd_suspend_all_processes(void)
671{
672 struct kfd_process *p;
673 unsigned int temp;
674 int idx = srcu_read_lock(&kfd_processes_srcu);
675
676 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
677 cancel_delayed_work_sync(&p->eviction_work);
678 cancel_delayed_work_sync(&p->restore_work);
679
680 if (process_evict_queues(p))
681 pr_err("Failed to suspend process %d\n", p->pasid);
682 dma_fence_signal(p->ef);
683 dma_fence_put(p->ef);
684 p->ef = NULL;
685 }
686 srcu_read_unlock(&kfd_processes_srcu, idx);
687}
688
689int kfd_resume_all_processes(void)
690{
691 struct kfd_process *p;
692 unsigned int temp;
693 int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
694
695 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
696 if (!schedule_delayed_work(&p->restore_work, 0)) {
697 pr_err("Restore process %d failed during resume\n",
698 p->pasid);
699 ret = -EFAULT;
700 }
701 }
702 srcu_read_unlock(&kfd_processes_srcu, idx);
703 return ret;
704}
705
493int kfd_reserved_mem_mmap(struct kfd_process *process, 706int kfd_reserved_mem_mmap(struct kfd_process *process,
494 struct vm_area_struct *vma) 707 struct vm_area_struct *vma)
495{ 708{