aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Kuehling <Felix.Kuehling@amd.com>2018-02-06 20:32:35 -0500
committerOded Gabbay <oded.gabbay@gmail.com>2018-02-06 20:32:35 -0500
commitd8d019ccffb838bb0dd98e583b5c25ccc0bc6ece (patch)
tree660d1ea42b915579fc4b12ad717bcbcaf49254f6
parent1029a3f33678afb8978285209ec5cfe153fe44ef (diff)
drm/amdgpu: Add KFD eviction fence
This fence is used by KFD to keep memory resident while user mode queues are enabled. Trying to evict memory will trigger the enable_signaling callback, which starts a KFD eviction, which involves preempting user mode queues before signaling the fence. There is one such fence per process. v2: * Grab a reference to mm_struct * Dereference fence after NULL check * Simplify fence release, no need to signal without anyone waiting * Added signed-off-by Harish, who is the original author of this code v3: * update MAINTAINERS file * change amd_kfd_ prefix to amdkfd_ * remove useless initialization of variable to NULL v4: * set amdkfd_fence_ops to be static * Suggested by: Fengguang Wu <fengguang.wu@intel.com> Signed-off-by: Harish Kasiviswanathan <Harish.Kasiviswanathan@amd.com> Signed-off-by: Felix Kuehling <Felix.Kuehling@amd.com> Reviewed-by: Oded Gabbay <oded.gabbay@gmail.com> Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
-rw-r--r--MAINTAINERS1
-rw-r--r--drivers/gpu/drm/amd/amdgpu/Makefile1
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h15
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c179
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h5
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c21
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c18
-rw-r--r--drivers/gpu/drm/amd/include/kgd_kfd_interface.h6
8 files changed, 242 insertions, 4 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 13c8ec11135a..9e376c7f15b1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -766,6 +766,7 @@ F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c
766F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h 766F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
767F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v7.c 767F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v7.c
768F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c 768F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c
769F: drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c
769F: drivers/gpu/drm/amd/amdkfd/ 770F: drivers/gpu/drm/amd/amdkfd/
770F: drivers/gpu/drm/amd/include/cik_structs.h 771F: drivers/gpu/drm/amd/include/cik_structs.h
771F: drivers/gpu/drm/amd/include/kgd_kfd_interface.h 772F: drivers/gpu/drm/amd/include/kgd_kfd_interface.h
diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile b/drivers/gpu/drm/amd/amdgpu/Makefile
index 353c937d947d..5dd317579e77 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -129,6 +129,7 @@ amdgpu-y += \
129# add amdkfd interfaces 129# add amdkfd interfaces
130amdgpu-y += \ 130amdgpu-y += \
131 amdgpu_amdkfd.o \ 131 amdgpu_amdkfd.o \
132 amdgpu_amdkfd_fence.o \
132 amdgpu_amdkfd_gfx_v8.o 133 amdgpu_amdkfd_gfx_v8.o
133 134
134# add cgs 135# add cgs
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
index 2a519f9062ee..833dc26d402f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
@@ -29,6 +29,8 @@
29#include <linux/mmu_context.h> 29#include <linux/mmu_context.h>
30#include <kgd_kfd_interface.h> 30#include <kgd_kfd_interface.h>
31 31
32extern const struct kgd2kfd_calls *kgd2kfd;
33
32struct amdgpu_device; 34struct amdgpu_device;
33 35
34struct kgd_mem { 36struct kgd_mem {
@@ -37,6 +39,19 @@ struct kgd_mem {
37 void *cpu_ptr; 39 void *cpu_ptr;
38}; 40};
39 41
42/* KFD Memory Eviction */
43struct amdgpu_amdkfd_fence {
44 struct dma_fence base;
45 struct mm_struct *mm;
46 spinlock_t lock;
47 char timeline_name[TASK_COMM_LEN];
48};
49
50struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
51 struct mm_struct *mm);
52bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
53struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
54
40int amdgpu_amdkfd_init(void); 55int amdgpu_amdkfd_init(void);
41void amdgpu_amdkfd_fini(void); 56void amdgpu_amdkfd_fini(void);
42 57
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c
new file mode 100644
index 000000000000..2c14025e5e76
--- /dev/null
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c
@@ -0,0 +1,179 @@
1/*
2 * Copyright 2016-2018 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/dma-fence.h>
24#include <linux/spinlock.h>
25#include <linux/atomic.h>
26#include <linux/stacktrace.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/sched/mm.h>
30#include "amdgpu_amdkfd.h"
31
32static const struct dma_fence_ops amdkfd_fence_ops;
33static atomic_t fence_seq = ATOMIC_INIT(0);
34
35/* Eviction Fence
36 * Fence helper functions to deal with KFD memory eviction.
37 * Big Idea - Since KFD submissions are done by user queues, a BO cannot be
38 * evicted unless all the user queues for that process are evicted.
39 *
40 * All the BOs in a process share an eviction fence. When process X wants
41 * to map VRAM memory but TTM can't find enough space, TTM will attempt to
42 * evict BOs from its LRU list. TTM checks if the BO is valuable to evict
43 * by calling ttm_bo_driver->eviction_valuable().
44 *
45 * ttm_bo_driver->eviction_valuable() - will return false if the BO belongs
46 * to process X. Otherwise, it will return true to indicate BO can be
47 * evicted by TTM.
48 *
49 * If ttm_bo_driver->eviction_valuable returns true, then TTM will continue
50 * the evcition process for that BO by calling ttm_bo_evict --> amdgpu_bo_move
51 * --> amdgpu_copy_buffer(). This sets up job in GPU scheduler.
52 *
53 * GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to
54 * nofity when the BO is free to move. fence_add_callback --> enable_signaling
55 * --> amdgpu_amdkfd_fence.enable_signaling
56 *
57 * amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
58 * user queues and signal fence. The work item will also start another delayed
59 * work item to restore BOs
60 */
61
62struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
63 struct mm_struct *mm)
64{
65 struct amdgpu_amdkfd_fence *fence;
66
67 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
68 if (fence == NULL)
69 return NULL;
70
71 /* This reference gets released in amdkfd_fence_release */
72 mmgrab(mm);
73 fence->mm = mm;
74 get_task_comm(fence->timeline_name, current);
75 spin_lock_init(&fence->lock);
76
77 dma_fence_init(&fence->base, &amdkfd_fence_ops, &fence->lock,
78 context, atomic_inc_return(&fence_seq));
79
80 return fence;
81}
82
83struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
84{
85 struct amdgpu_amdkfd_fence *fence;
86
87 if (!f)
88 return NULL;
89
90 fence = container_of(f, struct amdgpu_amdkfd_fence, base);
91 if (fence && f->ops == &amdkfd_fence_ops)
92 return fence;
93
94 return NULL;
95}
96
97static const char *amdkfd_fence_get_driver_name(struct dma_fence *f)
98{
99 return "amdgpu_amdkfd_fence";
100}
101
102static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
103{
104 struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
105
106 return fence->timeline_name;
107}
108
109/**
110 * amdkfd_fence_enable_signaling - This gets called when TTM wants to evict
111 * a KFD BO and schedules a job to move the BO.
112 * If fence is already signaled return true.
113 * If fence is not signaled schedule a evict KFD process work item.
114 */
115static bool amdkfd_fence_enable_signaling(struct dma_fence *f)
116{
117 struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
118
119 if (!fence)
120 return false;
121
122 if (dma_fence_is_signaled(f))
123 return true;
124
125 if (!kgd2kfd->schedule_evict_and_restore_process(fence->mm, f))
126 return true;
127
128 return false;
129}
130
131/**
132 * amdkfd_fence_release - callback that fence can be freed
133 *
134 * @fence: fence
135 *
136 * This function is called when the reference count becomes zero.
137 * Drops the mm_struct reference and RCU schedules freeing up the fence.
138 */
139static void amdkfd_fence_release(struct dma_fence *f)
140{
141 struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
142
143 /* Unconditionally signal the fence. The process is getting
144 * terminated.
145 */
146 if (WARN_ON(!fence))
147 return; /* Not an amdgpu_amdkfd_fence */
148
149 mmdrop(fence->mm);
150 kfree_rcu(f, rcu);
151}
152
153/**
154 * amdkfd_fence_check_mm - Check if @mm is same as that of the fence @f
155 * if same return TRUE else return FALSE.
156 *
157 * @f: [IN] fence
158 * @mm: [IN] mm that needs to be verified
159 */
160bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm)
161{
162 struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
163
164 if (!fence)
165 return false;
166 else if (fence->mm == mm)
167 return true;
168
169 return false;
170}
171
172static const struct dma_fence_ops amdkfd_fence_ops = {
173 .get_driver_name = amdkfd_fence_get_driver_name,
174 .get_timeline_name = amdkfd_fence_get_timeline_name,
175 .enable_signaling = amdkfd_fence_enable_signaling,
176 .signaled = NULL,
177 .wait = dma_fence_default_wait,
178 .release = amdkfd_fence_release,
179};
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
index 06436c3ebd2b..1a5911882657 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
@@ -36,8 +36,9 @@
36#define AMDGPU_MAX_UVD_ENC_RINGS 2 36#define AMDGPU_MAX_UVD_ENC_RINGS 2
37 37
38/* some special values for the owner field */ 38/* some special values for the owner field */
39#define AMDGPU_FENCE_OWNER_UNDEFINED ((void*)0ul) 39#define AMDGPU_FENCE_OWNER_UNDEFINED ((void *)0ul)
40#define AMDGPU_FENCE_OWNER_VM ((void*)1ul) 40#define AMDGPU_FENCE_OWNER_VM ((void *)1ul)
41#define AMDGPU_FENCE_OWNER_KFD ((void *)2ul)
41 42
42#define AMDGPU_FENCE_FLAG_64BIT (1 << 0) 43#define AMDGPU_FENCE_FLAG_64BIT (1 << 0)
43#define AMDGPU_FENCE_FLAG_INT (1 << 1) 44#define AMDGPU_FENCE_FLAG_INT (1 << 1)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c
index df65c66dc956..b8d3b87fd43e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c
@@ -31,6 +31,7 @@
31#include <drm/drmP.h> 31#include <drm/drmP.h>
32#include "amdgpu.h" 32#include "amdgpu.h"
33#include "amdgpu_trace.h" 33#include "amdgpu_trace.h"
34#include "amdgpu_amdkfd.h"
34 35
35struct amdgpu_sync_entry { 36struct amdgpu_sync_entry {
36 struct hlist_node node; 37 struct hlist_node node;
@@ -85,11 +86,20 @@ static bool amdgpu_sync_same_dev(struct amdgpu_device *adev,
85 */ 86 */
86static void *amdgpu_sync_get_owner(struct dma_fence *f) 87static void *amdgpu_sync_get_owner(struct dma_fence *f)
87{ 88{
88 struct drm_sched_fence *s_fence = to_drm_sched_fence(f); 89 struct drm_sched_fence *s_fence;
90 struct amdgpu_amdkfd_fence *kfd_fence;
91
92 if (!f)
93 return AMDGPU_FENCE_OWNER_UNDEFINED;
89 94
95 s_fence = to_drm_sched_fence(f);
90 if (s_fence) 96 if (s_fence)
91 return s_fence->owner; 97 return s_fence->owner;
92 98
99 kfd_fence = to_amdgpu_amdkfd_fence(f);
100 if (kfd_fence)
101 return AMDGPU_FENCE_OWNER_KFD;
102
93 return AMDGPU_FENCE_OWNER_UNDEFINED; 103 return AMDGPU_FENCE_OWNER_UNDEFINED;
94} 104}
95 105
@@ -204,11 +214,18 @@ int amdgpu_sync_resv(struct amdgpu_device *adev,
204 for (i = 0; i < flist->shared_count; ++i) { 214 for (i = 0; i < flist->shared_count; ++i) {
205 f = rcu_dereference_protected(flist->shared[i], 215 f = rcu_dereference_protected(flist->shared[i],
206 reservation_object_held(resv)); 216 reservation_object_held(resv));
217 /* We only want to trigger KFD eviction fences on
218 * evict or move jobs. Skip KFD fences otherwise.
219 */
220 fence_owner = amdgpu_sync_get_owner(f);
221 if (fence_owner == AMDGPU_FENCE_OWNER_KFD &&
222 owner != AMDGPU_FENCE_OWNER_UNDEFINED)
223 continue;
224
207 if (amdgpu_sync_same_dev(adev, f)) { 225 if (amdgpu_sync_same_dev(adev, f)) {
208 /* VM updates are only interesting 226 /* VM updates are only interesting
209 * for other VM updates and moves. 227 * for other VM updates and moves.
210 */ 228 */
211 fence_owner = amdgpu_sync_get_owner(f);
212 if ((owner != AMDGPU_FENCE_OWNER_UNDEFINED) && 229 if ((owner != AMDGPU_FENCE_OWNER_UNDEFINED) &&
213 (fence_owner != AMDGPU_FENCE_OWNER_UNDEFINED) && 230 (fence_owner != AMDGPU_FENCE_OWNER_UNDEFINED) &&
214 ((owner == AMDGPU_FENCE_OWNER_VM) != 231 ((owner == AMDGPU_FENCE_OWNER_VM) !=
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 28c33d711bab..5fcb3488a595 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -46,6 +46,7 @@
46#include "amdgpu.h" 46#include "amdgpu.h"
47#include "amdgpu_object.h" 47#include "amdgpu_object.h"
48#include "amdgpu_trace.h" 48#include "amdgpu_trace.h"
49#include "amdgpu_amdkfd.h"
49#include "bif/bif_4_1_d.h" 50#include "bif/bif_4_1_d.h"
50 51
51#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT) 52#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
@@ -1171,6 +1172,23 @@ static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1171{ 1172{
1172 unsigned long num_pages = bo->mem.num_pages; 1173 unsigned long num_pages = bo->mem.num_pages;
1173 struct drm_mm_node *node = bo->mem.mm_node; 1174 struct drm_mm_node *node = bo->mem.mm_node;
1175 struct reservation_object_list *flist;
1176 struct dma_fence *f;
1177 int i;
1178
1179 /* If bo is a KFD BO, check if the bo belongs to the current process.
1180 * If true, then return false as any KFD process needs all its BOs to
1181 * be resident to run successfully
1182 */
1183 flist = reservation_object_get_list(bo->resv);
1184 if (flist) {
1185 for (i = 0; i < flist->shared_count; ++i) {
1186 f = rcu_dereference_protected(flist->shared[i],
1187 reservation_object_held(bo->resv));
1188 if (amdkfd_fence_check_mm(f, current->mm))
1189 return false;
1190 }
1191 }
1174 1192
1175 switch (bo->mem.mem_type) { 1193 switch (bo->mem.mem_type) {
1176 case TTM_PL_TT: 1194 case TTM_PL_TT:
diff --git a/drivers/gpu/drm/amd/include/kgd_kfd_interface.h b/drivers/gpu/drm/amd/include/kgd_kfd_interface.h
index 94eab54864bf..9e352499c409 100644
--- a/drivers/gpu/drm/amd/include/kgd_kfd_interface.h
+++ b/drivers/gpu/drm/amd/include/kgd_kfd_interface.h
@@ -30,6 +30,7 @@
30 30
31#include <linux/types.h> 31#include <linux/types.h>
32#include <linux/bitmap.h> 32#include <linux/bitmap.h>
33#include <linux/dma-fence.h>
33 34
34struct pci_dev; 35struct pci_dev;
35 36
@@ -286,6 +287,9 @@ struct kfd2kgd_calls {
286 * 287 *
287 * @resume: Notifies amdkfd about a resume action done to a kgd device 288 * @resume: Notifies amdkfd about a resume action done to a kgd device
288 * 289 *
290 * @schedule_evict_and_restore_process: Schedules work queue that will prepare
291 * for safe eviction of KFD BOs that belong to the specified process.
292 *
289 * This structure contains function callback pointers so the kgd driver 293 * This structure contains function callback pointers so the kgd driver
290 * will notify to the amdkfd about certain status changes. 294 * will notify to the amdkfd about certain status changes.
291 * 295 *
@@ -300,6 +304,8 @@ struct kgd2kfd_calls {
300 void (*interrupt)(struct kfd_dev *kfd, const void *ih_ring_entry); 304 void (*interrupt)(struct kfd_dev *kfd, const void *ih_ring_entry);
301 void (*suspend)(struct kfd_dev *kfd); 305 void (*suspend)(struct kfd_dev *kfd);
302 int (*resume)(struct kfd_dev *kfd); 306 int (*resume)(struct kfd_dev *kfd);
307 int (*schedule_evict_and_restore_process)(struct mm_struct *mm,
308 struct dma_fence *fence);
303}; 309};
304 310
305int kgd2kfd_init(unsigned interface_version, 311int kgd2kfd_init(unsigned interface_version,