]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c
Merge tag 'pci-v4.18-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_mn.c
1 /*
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 <[email protected]>
29  */
30
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/mmu_notifier.h>
34 #include <linux/interval_tree.h>
35 #include <drm/drmP.h>
36 #include <drm/drm.h>
37
38 #include "amdgpu.h"
39 #include "amdgpu_amdkfd.h"
40
41 struct amdgpu_mn {
42         /* constant after initialisation */
43         struct amdgpu_device    *adev;
44         struct mm_struct        *mm;
45         struct mmu_notifier     mn;
46         enum amdgpu_mn_type     type;
47
48         /* only used on destruction */
49         struct work_struct      work;
50
51         /* protected by adev->mn_lock */
52         struct hlist_node       node;
53
54         /* objects protected by lock */
55         struct rw_semaphore     lock;
56         struct rb_root_cached   objects;
57         struct mutex            read_lock;
58         atomic_t                recursion;
59 };
60
61 struct 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  */
73 static 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);
81         down_write(&rmn->lock);
82         hash_del(&rmn->node);
83         rbtree_postorder_for_each_entry_safe(node, next_node,
84                                              &rmn->objects.rb_root, it.rb) {
85                 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         }
91         up_write(&rmn->lock);
92         mutex_unlock(&adev->mn_lock);
93         mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
94         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  */
105 static 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
113
114 /**
115  * amdgpu_mn_lock - take the write side lock for this mn
116  */
117 void 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  */
126 void amdgpu_mn_unlock(struct amdgpu_mn *mn)
127 {
128         if (mn)
129                 up_write(&mn->lock);
130 }
131
132 /**
133  * amdgpu_mn_read_lock - take the rmn read lock
134  *
135  * @rmn: our notifier
136  *
137  * Take the rmn read side lock.
138  */
139 static 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  */
154 static 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
160 /**
161  * 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  */
168 static 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
180                 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
185                 amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
186         }
187 }
188
189 /**
190  * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
191  *
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  */
200 static void amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
201                                                  struct mm_struct *mm,
202                                                  unsigned long start,
203                                                  unsigned long end)
204 {
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
211         amdgpu_mn_read_lock(rmn);
212
213         it = interval_tree_iter_first(&rmn->objects, start, end);
214         while (it) {
215                 struct amdgpu_mn_node *node;
216
217                 node = container_of(it, struct amdgpu_mn_node, it);
218                 it = interval_tree_iter_next(it, start, end);
219
220                 amdgpu_mn_invalidate_node(node, start, end);
221         }
222 }
223
224 /**
225  * 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  */
236 static 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 /**
268  * 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  */
277 static 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);
285 }
286
287 static 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         },
298 };
299
300 /* 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
306 /**
307  * amdgpu_mn_get - create notifier context
308  *
309  * @adev: amdgpu device pointer
310  * @type: type of MMU notifier context
311  *
312  * Creates a notifier context for current->mm.
313  */
314 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
315                                 enum amdgpu_mn_type type)
316 {
317         struct mm_struct *mm = current->mm;
318         struct amdgpu_mn *rmn;
319         unsigned long key = AMDGPU_MN_KEY(mm, type);
320         int r;
321
322         mutex_lock(&adev->mn_lock);
323         if (down_write_killable(&mm->mmap_sem)) {
324                 mutex_unlock(&adev->mn_lock);
325                 return ERR_PTR(-EINTR);
326         }
327
328         hash_for_each_possible(adev->mn_hash, rmn, node, key)
329                 if (AMDGPU_MN_KEY(rmn->mm, rmn->type) == key)
330                         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;
340         init_rwsem(&rmn->lock);
341         rmn->type = type;
342         rmn->mn.ops = &amdgpu_mn_ops[type];
343         rmn->objects = RB_ROOT_CACHED;
344         mutex_init(&rmn->read_lock);
345         atomic_set(&rmn->recursion, 0);
346
347         r = __mmu_notifier_register(&rmn->mn, mm);
348         if (r)
349                 goto free_rmn;
350
351         hash_add(adev->mn_hash, &rmn->node, AMDGPU_MN_KEY(mm, type));
352
353 release_locks:
354         up_write(&mm->mmap_sem);
355         mutex_unlock(&adev->mn_lock);
356
357         return rmn;
358
359 free_rmn:
360         up_write(&mm->mmap_sem);
361         mutex_unlock(&adev->mn_lock);
362         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  */
376 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
377 {
378         unsigned long end = addr + amdgpu_bo_size(bo) - 1;
379         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
380         enum amdgpu_mn_type type =
381                 bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
382         struct amdgpu_mn *rmn;
383         struct amdgpu_mn_node *node = NULL, *new_node;
384         struct list_head bos;
385         struct interval_tree_node *it;
386
387         rmn = amdgpu_mn_get(adev, type);
388         if (IS_ERR(rmn))
389                 return PTR_ERR(rmn);
390
391         new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
392         if (!new_node)
393                 return -ENOMEM;
394
395         INIT_LIST_HEAD(&bos);
396
397         down_write(&rmn->lock);
398
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
408         if (!node)
409                 node = new_node;
410         else
411                 kfree(new_node);
412
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
423         up_write(&rmn->lock);
424
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  */
435 void amdgpu_mn_unregister(struct amdgpu_bo *bo)
436 {
437         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
438         struct amdgpu_mn *rmn;
439         struct list_head *head;
440
441         mutex_lock(&adev->mn_lock);
442
443         rmn = bo->mn;
444         if (rmn == NULL) {
445                 mutex_unlock(&adev->mn_lock);
446                 return;
447         }
448
449         down_write(&rmn->lock);
450
451         /* save the next list entry for later */
452         head = bo->mn_list.next;
453
454         bo->mn = NULL;
455         list_del_init(&bo->mn_list);
456
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
464         up_write(&rmn->lock);
465         mutex_unlock(&adev->mn_lock);
466 }
467
This page took 0.062387 seconds and 4 git commands to generate.