2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/mmu_notifier.h>
34 #include <linux/interval_tree.h>
39 #include "amdgpu_amdkfd.h"
42 /* constant after initialisation */
43 struct amdgpu_device *adev;
45 struct mmu_notifier mn;
46 enum amdgpu_mn_type type;
48 /* only used on destruction */
49 struct work_struct work;
51 /* protected by adev->mn_lock */
52 struct hlist_node node;
54 /* objects protected by lock */
55 struct rw_semaphore lock;
56 struct rb_root_cached objects;
57 struct mutex read_lock;
61 struct amdgpu_mn_node {
62 struct interval_tree_node it;
67 * amdgpu_mn_destroy - destroy the rmn
69 * @work: previously sheduled work item
71 * Lazy destroys the notifier from a work item
73 static void amdgpu_mn_destroy(struct work_struct *work)
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;
80 mutex_lock(&adev->mn_lock);
81 down_write(&rmn->lock);
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) {
87 list_del_init(&bo->mn_list);
92 mutex_unlock(&adev->mn_lock);
93 mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
98 * amdgpu_mn_release - callback to notify about mm destruction
101 * @mn: the mm this callback is about
103 * Shedule a work item to lazy destroy our notifier.
105 static void amdgpu_mn_release(struct mmu_notifier *mn,
106 struct mm_struct *mm)
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);
115 * amdgpu_mn_lock - take the write side lock for this mn
117 void amdgpu_mn_lock(struct amdgpu_mn *mn)
120 down_write(&mn->lock);
124 * amdgpu_mn_unlock - drop the write side lock for this mn
126 void amdgpu_mn_unlock(struct amdgpu_mn *mn)
133 * amdgpu_mn_read_lock - take the rmn read lock
137 * Take the rmn read side lock.
139 static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
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);
148 * amdgpu_mn_read_unlock - drop the rmn read lock
152 * Drop the rmn read side lock.
154 static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
156 if (atomic_dec_return(&rmn->recursion) == 0)
157 up_read_non_owner(&rmn->lock);
161 * amdgpu_mn_invalidate_node - unmap all BOs of a node
163 * @node: the node with the BOs to unmap
165 * We block for all BOs and unmap them by move them
166 * into system domain again.
168 static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
172 struct amdgpu_bo *bo;
175 list_for_each_entry(bo, &node->bos, mn_list) {
177 if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
180 r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
181 true, false, MAX_SCHEDULE_TIMEOUT);
183 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
185 amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
190 * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
193 * @mn: the mm this callback is about
194 * @start: start of updated range
195 * @end: end of updated range
197 * We block for all BOs between start and end to be idle and
198 * unmap them by move them into system domain again.
200 static void amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
201 struct mm_struct *mm,
205 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
206 struct interval_tree_node *it;
208 /* notification is exclusive, but interval is inclusive */
211 amdgpu_mn_read_lock(rmn);
213 it = interval_tree_iter_first(&rmn->objects, start, end);
215 struct amdgpu_mn_node *node;
217 node = container_of(it, struct amdgpu_mn_node, it);
218 it = interval_tree_iter_next(it, start, end);
220 amdgpu_mn_invalidate_node(node, start, end);
225 * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
228 * @mn: the mm this callback is about
229 * @start: start of updated range
230 * @end: end of updated range
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.
236 static void amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
237 struct mm_struct *mm,
241 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
242 struct interval_tree_node *it;
244 /* notification is exclusive, but interval is inclusive */
247 amdgpu_mn_read_lock(rmn);
249 it = interval_tree_iter_first(&rmn->objects, start, end);
251 struct amdgpu_mn_node *node;
252 struct amdgpu_bo *bo;
254 node = container_of(it, struct amdgpu_mn_node, it);
255 it = interval_tree_iter_next(it, start, end);
257 list_for_each_entry(bo, &node->bos, mn_list) {
258 struct kgd_mem *mem = bo->kfd_bo;
260 if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
262 amdgpu_amdkfd_evict_userptr(mem, mm);
268 * amdgpu_mn_invalidate_range_end - callback to notify about mm change
271 * @mn: the mm this callback is about
272 * @start: start of updated range
273 * @end: end of updated range
275 * Release the lock again to allow new command submissions.
277 static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
278 struct mm_struct *mm,
282 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
284 amdgpu_mn_read_unlock(rmn);
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,
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,
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
304 #define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
307 * amdgpu_mn_get - create notifier context
309 * @adev: amdgpu device pointer
310 * @type: type of MMU notifier context
312 * Creates a notifier context for current->mm.
314 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
315 enum amdgpu_mn_type type)
317 struct mm_struct *mm = current->mm;
318 struct amdgpu_mn *rmn;
319 unsigned long key = AMDGPU_MN_KEY(mm, type);
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);
328 hash_for_each_possible(adev->mn_hash, rmn, node, key)
329 if (AMDGPU_MN_KEY(rmn->mm, rmn->type) == key)
332 rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
334 rmn = ERR_PTR(-ENOMEM);
340 init_rwsem(&rmn->lock);
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);
347 r = __mmu_notifier_register(&rmn->mn, mm);
351 hash_add(adev->mn_hash, &rmn->node, AMDGPU_MN_KEY(mm, type));
354 up_write(&mm->mmap_sem);
355 mutex_unlock(&adev->mn_lock);
360 up_write(&mm->mmap_sem);
361 mutex_unlock(&adev->mn_lock);
368 * amdgpu_mn_register - register a BO for notifier updates
370 * @bo: amdgpu buffer object
371 * @addr: userptr addr we should monitor
373 * Registers an MMU notifier for the given BO at the specified address.
374 * Returns 0 on success, -ERRNO if anything goes wrong.
376 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
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;
387 rmn = amdgpu_mn_get(adev, type);
391 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
395 INIT_LIST_HEAD(&bos);
397 down_write(&rmn->lock);
399 while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
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);
415 node->it.start = addr;
417 INIT_LIST_HEAD(&node->bos);
418 list_splice(&bos, &node->bos);
419 list_add(&bo->mn_list, &node->bos);
421 interval_tree_insert(&node->it, &rmn->objects);
423 up_write(&rmn->lock);
429 * amdgpu_mn_unregister - unregister a BO for notifier updates
431 * @bo: amdgpu buffer object
433 * Remove any registration of MMU notifier updates from the buffer object.
435 void amdgpu_mn_unregister(struct amdgpu_bo *bo)
437 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
438 struct amdgpu_mn *rmn;
439 struct list_head *head;
441 mutex_lock(&adev->mn_lock);
445 mutex_unlock(&adev->mn_lock);
449 down_write(&rmn->lock);
451 /* save the next list entry for later */
452 head = bo->mn_list.next;
455 list_del_init(&bo->mn_list);
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);
464 up_write(&rmn->lock);
465 mutex_unlock(&adev->mn_lock);