blob: 83e344fbb50a2e86f1c73f0a1d674c8717ddeb2d [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26/*
27 * Authors:
28 * Christian König <christian.koenig@amd.com>
29 */
30
31#include <linux/firmware.h>
32#include <linux/module.h>
33#include <linux/mmu_notifier.h>
Christian Königa9f87f62017-03-30 14:03:59 +020034#include <linux/interval_tree.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040035#include <drm/drmP.h>
36#include <drm/drm.h>
37
38#include "amdgpu.h"
Felix Kuehlinge52482d2018-03-23 15:32:28 -040039#include "amdgpu_amdkfd.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040
41struct amdgpu_mn {
42 /* constant after initialisation */
43 struct amdgpu_device *adev;
44 struct mm_struct *mm;
45 struct mmu_notifier mn;
Felix Kuehlinge52482d2018-03-23 15:32:28 -040046 enum amdgpu_mn_type type;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040047
48 /* only used on destruction */
49 struct work_struct work;
50
51 /* protected by adev->mn_lock */
52 struct hlist_node node;
53
Christian König0d2b42b2016-03-18 19:29:51 +010054 /* objects protected by lock */
Christian König60de1c12017-09-05 14:50:24 +020055 struct rw_semaphore lock;
Davidlohr Buesof808c132017-09-08 16:15:08 -070056 struct rb_root_cached objects;
Christian König1ed3d252017-09-05 17:30:46 +020057 struct mutex read_lock;
58 atomic_t recursion;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059};
60
61struct amdgpu_mn_node {
62 struct interval_tree_node it;
63 struct list_head bos;
64};
65
66/**
67 * amdgpu_mn_destroy - destroy the rmn
68 *
69 * @work: previously sheduled work item
70 *
71 * Lazy destroys the notifier from a work item
72 */
73static void amdgpu_mn_destroy(struct work_struct *work)
74{
75 struct amdgpu_mn *rmn = container_of(work, struct amdgpu_mn, work);
76 struct amdgpu_device *adev = rmn->adev;
77 struct amdgpu_mn_node *node, *next_node;
78 struct amdgpu_bo *bo, *next_bo;
79
80 mutex_lock(&adev->mn_lock);
Christian König60de1c12017-09-05 14:50:24 +020081 down_write(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 hash_del(&rmn->node);
Davidlohr Buesof808c132017-09-08 16:15:08 -070083 rbtree_postorder_for_each_entry_safe(node, next_node,
84 &rmn->objects.rb_root, it.rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
86 bo->mn = NULL;
87 list_del_init(&bo->mn_list);
88 }
89 kfree(node);
90 }
Christian König60de1c12017-09-05 14:50:24 +020091 up_write(&rmn->lock);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -050092 mutex_unlock(&adev->mn_lock);
Felix Kuehlingfa5b5002016-01-14 00:35:08 -050093 mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 kfree(rmn);
95}
96
97/**
98 * amdgpu_mn_release - callback to notify about mm destruction
99 *
100 * @mn: our notifier
101 * @mn: the mm this callback is about
102 *
103 * Shedule a work item to lazy destroy our notifier.
104 */
105static void amdgpu_mn_release(struct mmu_notifier *mn,
106 struct mm_struct *mm)
107{
108 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
109 INIT_WORK(&rmn->work, amdgpu_mn_destroy);
110 schedule_work(&rmn->work);
111}
112
Christian König3fe89772017-09-12 14:25:14 -0400113
114/**
115 * amdgpu_mn_lock - take the write side lock for this mn
116 */
117void amdgpu_mn_lock(struct amdgpu_mn *mn)
118{
119 if (mn)
120 down_write(&mn->lock);
121}
122
123/**
124 * amdgpu_mn_unlock - drop the write side lock for this mn
125 */
126void amdgpu_mn_unlock(struct amdgpu_mn *mn)
127{
128 if (mn)
129 up_write(&mn->lock);
130}
131
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132/**
Christian König1ed3d252017-09-05 17:30:46 +0200133 * amdgpu_mn_read_lock - take the rmn read lock
134 *
135 * @rmn: our notifier
136 *
137 * Take the rmn read side lock.
138 */
139static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
140{
141 mutex_lock(&rmn->read_lock);
142 if (atomic_inc_return(&rmn->recursion) == 1)
143 down_read_non_owner(&rmn->lock);
144 mutex_unlock(&rmn->read_lock);
145}
146
147/**
148 * amdgpu_mn_read_unlock - drop the rmn read lock
149 *
150 * @rmn: our notifier
151 *
152 * Drop the rmn read side lock.
153 */
154static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
155{
156 if (atomic_dec_return(&rmn->recursion) == 0)
157 up_read_non_owner(&rmn->lock);
158}
159
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160/**
Christian Königae20f122016-03-18 19:29:52 +0100161 * amdgpu_mn_invalidate_node - unmap all BOs of a node
162 *
163 * @node: the node with the BOs to unmap
164 *
165 * We block for all BOs and unmap them by move them
166 * into system domain again.
167 */
168static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
169 unsigned long start,
170 unsigned long end)
171{
172 struct amdgpu_bo *bo;
173 long r;
174
175 list_for_each_entry(bo, &node->bos, mn_list) {
176
177 if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
178 continue;
179
Christian Königae20f122016-03-18 19:29:52 +0100180 r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
181 true, false, MAX_SCHEDULE_TIMEOUT);
182 if (r <= 0)
183 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
184
Christian König1b0c0f92017-09-05 14:36:44 +0200185 amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
Christian Königae20f122016-03-18 19:29:52 +0100186 }
187}
188
189/**
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400190 * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 *
192 * @mn: our notifier
193 * @mn: the mm this callback is about
194 * @start: start of updated range
195 * @end: end of updated range
196 *
197 * We block for all BOs between start and end to be idle and
198 * unmap them by move them into system domain again.
199 */
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400200static void amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
201 struct mm_struct *mm,
202 unsigned long start,
203 unsigned long end)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204{
205 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
206 struct interval_tree_node *it;
207
208 /* notification is exclusive, but interval is inclusive */
209 end -= 1;
210
Christian König1ed3d252017-09-05 17:30:46 +0200211 amdgpu_mn_read_lock(rmn);
Christian König0d2b42b2016-03-18 19:29:51 +0100212
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400213 it = interval_tree_iter_first(&rmn->objects, start, end);
214 while (it) {
215 struct amdgpu_mn_node *node;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216
217 node = container_of(it, struct amdgpu_mn_node, it);
218 it = interval_tree_iter_next(it, start, end);
219
Christian Königae20f122016-03-18 19:29:52 +0100220 amdgpu_mn_invalidate_node(node, start, end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221 }
Christian König1ed3d252017-09-05 17:30:46 +0200222}
Christian König0d2b42b2016-03-18 19:29:51 +0100223
Christian König1ed3d252017-09-05 17:30:46 +0200224/**
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400225 * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
226 *
227 * @mn: our notifier
228 * @mn: the mm this callback is about
229 * @start: start of updated range
230 * @end: end of updated range
231 *
232 * We temporarily evict all BOs between start and end. This
233 * necessitates evicting all user-mode queues of the process. The BOs
234 * are restorted in amdgpu_mn_invalidate_range_end_hsa.
235 */
236static void amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
237 struct mm_struct *mm,
238 unsigned long start,
239 unsigned long end)
240{
241 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
242 struct interval_tree_node *it;
243
244 /* notification is exclusive, but interval is inclusive */
245 end -= 1;
246
247 amdgpu_mn_read_lock(rmn);
248
249 it = interval_tree_iter_first(&rmn->objects, start, end);
250 while (it) {
251 struct amdgpu_mn_node *node;
252 struct amdgpu_bo *bo;
253
254 node = container_of(it, struct amdgpu_mn_node, it);
255 it = interval_tree_iter_next(it, start, end);
256
257 list_for_each_entry(bo, &node->bos, mn_list) {
258 struct kgd_mem *mem = bo->kfd_bo;
259
260 if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
261 start, end))
262 amdgpu_amdkfd_evict_userptr(mem, mm);
263 }
264 }
265}
266
267/**
Christian König1ed3d252017-09-05 17:30:46 +0200268 * amdgpu_mn_invalidate_range_end - callback to notify about mm change
269 *
270 * @mn: our notifier
271 * @mn: the mm this callback is about
272 * @start: start of updated range
273 * @end: end of updated range
274 *
275 * Release the lock again to allow new command submissions.
276 */
277static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
278 struct mm_struct *mm,
279 unsigned long start,
280 unsigned long end)
281{
282 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
283
284 amdgpu_mn_read_unlock(rmn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285}
286
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400287static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
288 [AMDGPU_MN_TYPE_GFX] = {
289 .release = amdgpu_mn_release,
290 .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
291 .invalidate_range_end = amdgpu_mn_invalidate_range_end,
292 },
293 [AMDGPU_MN_TYPE_HSA] = {
294 .release = amdgpu_mn_release,
295 .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
296 .invalidate_range_end = amdgpu_mn_invalidate_range_end,
297 },
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298};
299
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400300/* Low bits of any reasonable mm pointer will be unused due to struct
301 * alignment. Use these bits to make a unique key from the mm pointer
302 * and notifier type.
303 */
304#define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
305
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306/**
307 * amdgpu_mn_get - create notifier context
308 *
309 * @adev: amdgpu device pointer
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400310 * @type: type of MMU notifier context
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400311 *
312 * Creates a notifier context for current->mm.
313 */
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400314struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
315 enum amdgpu_mn_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400316{
317 struct mm_struct *mm = current->mm;
318 struct amdgpu_mn *rmn;
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400319 unsigned long key = AMDGPU_MN_KEY(mm, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400320 int r;
321
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400322 mutex_lock(&adev->mn_lock);
Michal Hockob5637052016-05-23 16:26:17 -0700323 if (down_write_killable(&mm->mmap_sem)) {
324 mutex_unlock(&adev->mn_lock);
325 return ERR_PTR(-EINTR);
326 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400327
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400328 hash_for_each_possible(adev->mn_hash, rmn, node, key)
329 if (AMDGPU_MN_KEY(rmn->mm, rmn->type) == key)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400330 goto release_locks;
331
332 rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
333 if (!rmn) {
334 rmn = ERR_PTR(-ENOMEM);
335 goto release_locks;
336 }
337
338 rmn->adev = adev;
339 rmn->mm = mm;
Christian König60de1c12017-09-05 14:50:24 +0200340 init_rwsem(&rmn->lock);
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400341 rmn->type = type;
342 rmn->mn.ops = &amdgpu_mn_ops[type];
Davidlohr Buesof808c132017-09-08 16:15:08 -0700343 rmn->objects = RB_ROOT_CACHED;
Christian König1ed3d252017-09-05 17:30:46 +0200344 mutex_init(&rmn->read_lock);
345 atomic_set(&rmn->recursion, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346
347 r = __mmu_notifier_register(&rmn->mn, mm);
348 if (r)
349 goto free_rmn;
350
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400351 hash_add(adev->mn_hash, &rmn->node, AMDGPU_MN_KEY(mm, type));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352
353release_locks:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354 up_write(&mm->mmap_sem);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500355 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356
357 return rmn;
358
359free_rmn:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400360 up_write(&mm->mmap_sem);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500361 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400362 kfree(rmn);
363
364 return ERR_PTR(r);
365}
366
367/**
368 * amdgpu_mn_register - register a BO for notifier updates
369 *
370 * @bo: amdgpu buffer object
371 * @addr: userptr addr we should monitor
372 *
373 * Registers an MMU notifier for the given BO at the specified address.
374 * Returns 0 on success, -ERRNO if anything goes wrong.
375 */
376int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
377{
378 unsigned long end = addr + amdgpu_bo_size(bo) - 1;
Christian Königa7d64de2016-09-15 14:58:48 +0200379 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400380 enum amdgpu_mn_type type =
381 bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382 struct amdgpu_mn *rmn;
Felix Kuehling6e08e092018-03-23 15:32:30 -0400383 struct amdgpu_mn_node *node = NULL, *new_node;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400384 struct list_head bos;
385 struct interval_tree_node *it;
386
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400387 rmn = amdgpu_mn_get(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388 if (IS_ERR(rmn))
389 return PTR_ERR(rmn);
390
Felix Kuehling6e08e092018-03-23 15:32:30 -0400391 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
392 if (!new_node)
393 return -ENOMEM;
394
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400395 INIT_LIST_HEAD(&bos);
396
Christian König60de1c12017-09-05 14:50:24 +0200397 down_write(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400398
399 while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
400 kfree(node);
401 node = container_of(it, struct amdgpu_mn_node, it);
402 interval_tree_remove(&node->it, &rmn->objects);
403 addr = min(it->start, addr);
404 end = max(it->last, end);
405 list_splice(&node->bos, &bos);
406 }
407
Felix Kuehling6e08e092018-03-23 15:32:30 -0400408 if (!node)
409 node = new_node;
410 else
411 kfree(new_node);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400412
413 bo->mn = rmn;
414
415 node->it.start = addr;
416 node->it.last = end;
417 INIT_LIST_HEAD(&node->bos);
418 list_splice(&bos, &node->bos);
419 list_add(&bo->mn_list, &node->bos);
420
421 interval_tree_insert(&node->it, &rmn->objects);
422
Christian König60de1c12017-09-05 14:50:24 +0200423 up_write(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424
425 return 0;
426}
427
428/**
429 * amdgpu_mn_unregister - unregister a BO for notifier updates
430 *
431 * @bo: amdgpu buffer object
432 *
433 * Remove any registration of MMU notifier updates from the buffer object.
434 */
435void amdgpu_mn_unregister(struct amdgpu_bo *bo)
436{
Christian Königa7d64de2016-09-15 14:58:48 +0200437 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500438 struct amdgpu_mn *rmn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439 struct list_head *head;
440
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500441 mutex_lock(&adev->mn_lock);
442
443 rmn = bo->mn;
444 if (rmn == NULL) {
445 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400446 return;
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500447 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448
Christian König60de1c12017-09-05 14:50:24 +0200449 down_write(&rmn->lock);
Christian Königc41d2712016-02-09 16:13:37 +0100450
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 /* save the next list entry for later */
452 head = bo->mn_list.next;
453
454 bo->mn = NULL;
Felix Kuehling68c9793d2017-08-01 22:34:55 -0400455 list_del_init(&bo->mn_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456
457 if (list_empty(head)) {
458 struct amdgpu_mn_node *node;
459 node = container_of(head, struct amdgpu_mn_node, bos);
460 interval_tree_remove(&node->it, &rmn->objects);
461 kfree(node);
462 }
463
Christian König60de1c12017-09-05 14:50:24 +0200464 up_write(&rmn->lock);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500465 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466}
Christian König3fe89772017-09-12 14:25:14 -0400467