1d8d019ccSFelix Kuehling /*
2d8d019ccSFelix Kuehling * Copyright 2016-2018 Advanced Micro Devices, Inc.
3d8d019ccSFelix Kuehling *
4d8d019ccSFelix Kuehling * Permission is hereby granted, free of charge, to any person obtaining a
5d8d019ccSFelix Kuehling * copy of this software and associated documentation files (the "Software"),
6d8d019ccSFelix Kuehling * to deal in the Software without restriction, including without limitation
7d8d019ccSFelix Kuehling * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d8d019ccSFelix Kuehling * and/or sell copies of the Software, and to permit persons to whom the
9d8d019ccSFelix Kuehling * Software is furnished to do so, subject to the following conditions:
10d8d019ccSFelix Kuehling *
11d8d019ccSFelix Kuehling * The above copyright notice and this permission notice shall be included in
12d8d019ccSFelix Kuehling * all copies or substantial portions of the Software.
13d8d019ccSFelix Kuehling *
14d8d019ccSFelix Kuehling * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d8d019ccSFelix Kuehling * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d8d019ccSFelix Kuehling * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17d8d019ccSFelix Kuehling * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18d8d019ccSFelix Kuehling * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19d8d019ccSFelix Kuehling * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20d8d019ccSFelix Kuehling * OTHER DEALINGS IN THE SOFTWARE.
21d8d019ccSFelix Kuehling */
22d8d019ccSFelix Kuehling
23d8d019ccSFelix Kuehling #include <linux/dma-fence.h>
24d8d019ccSFelix Kuehling #include <linux/spinlock.h>
25d8d019ccSFelix Kuehling #include <linux/atomic.h>
26d8d019ccSFelix Kuehling #include <linux/stacktrace.h>
27d8d019ccSFelix Kuehling #include <linux/sched.h>
28d8d019ccSFelix Kuehling #include <linux/slab.h>
29d8d019ccSFelix Kuehling #include <linux/sched/mm.h>
30d8d019ccSFelix Kuehling #include "amdgpu_amdkfd.h"
31485bea1fSAlex Sierra #include "kfd_svm.h"
32d8d019ccSFelix Kuehling
33d8d019ccSFelix Kuehling static const struct dma_fence_ops amdkfd_fence_ops;
34d8d019ccSFelix Kuehling static atomic_t fence_seq = ATOMIC_INIT(0);
35d8d019ccSFelix Kuehling
36d8d019ccSFelix Kuehling /* Eviction Fence
37d8d019ccSFelix Kuehling * Fence helper functions to deal with KFD memory eviction.
38d8d019ccSFelix Kuehling * Big Idea - Since KFD submissions are done by user queues, a BO cannot be
39d8d019ccSFelix Kuehling * evicted unless all the user queues for that process are evicted.
40d8d019ccSFelix Kuehling *
41d8d019ccSFelix Kuehling * All the BOs in a process share an eviction fence. When process X wants
42d8d019ccSFelix Kuehling * to map VRAM memory but TTM can't find enough space, TTM will attempt to
43d8d019ccSFelix Kuehling * evict BOs from its LRU list. TTM checks if the BO is valuable to evict
448af8a109SChristian König * by calling ttm_device_funcs->eviction_valuable().
45d8d019ccSFelix Kuehling *
468af8a109SChristian König * ttm_device_funcs->eviction_valuable() - will return false if the BO belongs
47d8d019ccSFelix Kuehling * to process X. Otherwise, it will return true to indicate BO can be
48d8d019ccSFelix Kuehling * evicted by TTM.
49d8d019ccSFelix Kuehling *
508af8a109SChristian König * If ttm_device_funcs->eviction_valuable returns true, then TTM will continue
51d8d019ccSFelix Kuehling * the evcition process for that BO by calling ttm_bo_evict --> amdgpu_bo_move
52d8d019ccSFelix Kuehling * --> amdgpu_copy_buffer(). This sets up job in GPU scheduler.
53d8d019ccSFelix Kuehling *
54d8d019ccSFelix Kuehling * GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to
55d8d019ccSFelix Kuehling * nofity when the BO is free to move. fence_add_callback --> enable_signaling
56d8d019ccSFelix Kuehling * --> amdgpu_amdkfd_fence.enable_signaling
57d8d019ccSFelix Kuehling *
58d8d019ccSFelix Kuehling * amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
59d8d019ccSFelix Kuehling * user queues and signal fence. The work item will also start another delayed
60d8d019ccSFelix Kuehling * work item to restore BOs
61d8d019ccSFelix Kuehling */
62d8d019ccSFelix Kuehling
amdgpu_amdkfd_fence_create(u64 context,struct mm_struct * mm,struct svm_range_bo * svm_bo)63d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
64eb2cec55SAlex Sierra struct mm_struct *mm,
65eb2cec55SAlex Sierra struct svm_range_bo *svm_bo)
66d8d019ccSFelix Kuehling {
67d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *fence;
68d8d019ccSFelix Kuehling
69d8d019ccSFelix Kuehling fence = kzalloc(sizeof(*fence), GFP_KERNEL);
70d8d019ccSFelix Kuehling if (fence == NULL)
71d8d019ccSFelix Kuehling return NULL;
72d8d019ccSFelix Kuehling
73d8d019ccSFelix Kuehling /* This reference gets released in amdkfd_fence_release */
74d8d019ccSFelix Kuehling mmgrab(mm);
75d8d019ccSFelix Kuehling fence->mm = mm;
76d8d019ccSFelix Kuehling get_task_comm(fence->timeline_name, current);
77d8d019ccSFelix Kuehling spin_lock_init(&fence->lock);
78eb2cec55SAlex Sierra fence->svm_bo = svm_bo;
79d8d019ccSFelix Kuehling dma_fence_init(&fence->base, &amdkfd_fence_ops, &fence->lock,
80d8d019ccSFelix Kuehling context, atomic_inc_return(&fence_seq));
81d8d019ccSFelix Kuehling
82d8d019ccSFelix Kuehling return fence;
83d8d019ccSFelix Kuehling }
84d8d019ccSFelix Kuehling
to_amdgpu_amdkfd_fence(struct dma_fence * f)85d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
86d8d019ccSFelix Kuehling {
87d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *fence;
88d8d019ccSFelix Kuehling
89d8d019ccSFelix Kuehling if (!f)
90d8d019ccSFelix Kuehling return NULL;
91d8d019ccSFelix Kuehling
92d8d019ccSFelix Kuehling fence = container_of(f, struct amdgpu_amdkfd_fence, base);
93*ff5aefbbSSrinivasan Shanmugam if (f->ops == &amdkfd_fence_ops)
94d8d019ccSFelix Kuehling return fence;
95d8d019ccSFelix Kuehling
96d8d019ccSFelix Kuehling return NULL;
97d8d019ccSFelix Kuehling }
98d8d019ccSFelix Kuehling
amdkfd_fence_get_driver_name(struct dma_fence * f)99d8d019ccSFelix Kuehling static const char *amdkfd_fence_get_driver_name(struct dma_fence *f)
100d8d019ccSFelix Kuehling {
101d8d019ccSFelix Kuehling return "amdgpu_amdkfd_fence";
102d8d019ccSFelix Kuehling }
103d8d019ccSFelix Kuehling
amdkfd_fence_get_timeline_name(struct dma_fence * f)104d8d019ccSFelix Kuehling static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
105d8d019ccSFelix Kuehling {
106d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
107d8d019ccSFelix Kuehling
108d8d019ccSFelix Kuehling return fence->timeline_name;
109d8d019ccSFelix Kuehling }
110d8d019ccSFelix Kuehling
111d8d019ccSFelix Kuehling /**
112d8d019ccSFelix Kuehling * amdkfd_fence_enable_signaling - This gets called when TTM wants to evict
113d8d019ccSFelix Kuehling * a KFD BO and schedules a job to move the BO.
114d8d019ccSFelix Kuehling * If fence is already signaled return true.
115d8d019ccSFelix Kuehling * If fence is not signaled schedule a evict KFD process work item.
1161fdbbc12SFabio M. De Francesco *
1171fdbbc12SFabio M. De Francesco * @f: dma_fence
118d8d019ccSFelix Kuehling */
amdkfd_fence_enable_signaling(struct dma_fence * f)119d8d019ccSFelix Kuehling static bool amdkfd_fence_enable_signaling(struct dma_fence *f)
120d8d019ccSFelix Kuehling {
121d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
122d8d019ccSFelix Kuehling
123d8d019ccSFelix Kuehling if (!fence)
124d8d019ccSFelix Kuehling return false;
125d8d019ccSFelix Kuehling
126d8d019ccSFelix Kuehling if (dma_fence_is_signaled(f))
127d8d019ccSFelix Kuehling return true;
128d8d019ccSFelix Kuehling
129485bea1fSAlex Sierra if (!fence->svm_bo) {
1308e07e267SAmber Lin if (!kgd2kfd_schedule_evict_and_restore_process(fence->mm, f))
131d8d019ccSFelix Kuehling return true;
132485bea1fSAlex Sierra } else {
133485bea1fSAlex Sierra if (!svm_range_schedule_evict_svm_bo(fence))
134485bea1fSAlex Sierra return true;
135485bea1fSAlex Sierra }
136d8d019ccSFelix Kuehling return false;
137d8d019ccSFelix Kuehling }
138d8d019ccSFelix Kuehling
139d8d019ccSFelix Kuehling /**
140d8d019ccSFelix Kuehling * amdkfd_fence_release - callback that fence can be freed
141d8d019ccSFelix Kuehling *
1421fdbbc12SFabio M. De Francesco * @f: dma_fence
143d8d019ccSFelix Kuehling *
144d8d019ccSFelix Kuehling * This function is called when the reference count becomes zero.
145d8d019ccSFelix Kuehling * Drops the mm_struct reference and RCU schedules freeing up the fence.
146d8d019ccSFelix Kuehling */
amdkfd_fence_release(struct dma_fence * f)147d8d019ccSFelix Kuehling static void amdkfd_fence_release(struct dma_fence *f)
148d8d019ccSFelix Kuehling {
149d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
150d8d019ccSFelix Kuehling
151d8d019ccSFelix Kuehling /* Unconditionally signal the fence. The process is getting
152d8d019ccSFelix Kuehling * terminated.
153d8d019ccSFelix Kuehling */
154d8d019ccSFelix Kuehling if (WARN_ON(!fence))
155d8d019ccSFelix Kuehling return; /* Not an amdgpu_amdkfd_fence */
156d8d019ccSFelix Kuehling
157d8d019ccSFelix Kuehling mmdrop(fence->mm);
158d8d019ccSFelix Kuehling kfree_rcu(f, rcu);
159d8d019ccSFelix Kuehling }
160d8d019ccSFelix Kuehling
161d8d019ccSFelix Kuehling /**
16279b2c54fSPhilip Yang * amdkfd_fence_check_mm - Check whether to prevent eviction of @f by @mm
163d8d019ccSFelix Kuehling *
164d8d019ccSFelix Kuehling * @f: [IN] fence
165d8d019ccSFelix Kuehling * @mm: [IN] mm that needs to be verified
16679b2c54fSPhilip Yang *
16779b2c54fSPhilip Yang * Check if @mm is same as that of the fence @f, if same return TRUE else
16879b2c54fSPhilip Yang * return FALSE.
16979b2c54fSPhilip Yang * For svm bo, which support vram overcommitment, always return FALSE.
170d8d019ccSFelix Kuehling */
amdkfd_fence_check_mm(struct dma_fence * f,struct mm_struct * mm)171d8d019ccSFelix Kuehling bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm)
172d8d019ccSFelix Kuehling {
173d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
174d8d019ccSFelix Kuehling
175d8d019ccSFelix Kuehling if (!fence)
176d8d019ccSFelix Kuehling return false;
17779b2c54fSPhilip Yang else if (fence->mm == mm && !fence->svm_bo)
178d8d019ccSFelix Kuehling return true;
179d8d019ccSFelix Kuehling
180d8d019ccSFelix Kuehling return false;
181d8d019ccSFelix Kuehling }
182d8d019ccSFelix Kuehling
183d8d019ccSFelix Kuehling static const struct dma_fence_ops amdkfd_fence_ops = {
184d8d019ccSFelix Kuehling .get_driver_name = amdkfd_fence_get_driver_name,
185d8d019ccSFelix Kuehling .get_timeline_name = amdkfd_fence_get_timeline_name,
186d8d019ccSFelix Kuehling .enable_signaling = amdkfd_fence_enable_signaling,
187d8d019ccSFelix Kuehling .release = amdkfd_fence_release,
188d8d019ccSFelix Kuehling };
189