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>
41 /* constant after initialisation */
42 struct amdgpu_device *adev;
44 struct mmu_notifier mn;
46 /* only used on destruction */
47 struct work_struct work;
49 /* protected by adev->mn_lock */
50 struct hlist_node node;
52 /* objects protected by lock */
53 struct rw_semaphore lock;
54 struct rb_root objects;
55 struct mutex read_lock;
59 struct amdgpu_mn_node {
60 struct interval_tree_node it;
65 * amdgpu_mn_destroy - destroy the rmn
67 * @work: previously sheduled work item
69 * Lazy destroys the notifier from a work item
71 static void amdgpu_mn_destroy(struct work_struct *work)
73 struct amdgpu_mn *rmn = container_of(work, struct amdgpu_mn, work);
74 struct amdgpu_device *adev = rmn->adev;
75 struct amdgpu_mn_node *node, *next_node;
76 struct amdgpu_bo *bo, *next_bo;
78 mutex_lock(&adev->mn_lock);
79 down_write(&rmn->lock);
81 rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
83 list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
85 list_del_init(&bo->mn_list);
90 mutex_unlock(&adev->mn_lock);
91 mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
96 * amdgpu_mn_release - callback to notify about mm destruction
99 * @mn: the mm this callback is about
101 * Shedule a work item to lazy destroy our notifier.
103 static void amdgpu_mn_release(struct mmu_notifier *mn,
104 struct mm_struct *mm)
106 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
107 INIT_WORK(&rmn->work, amdgpu_mn_destroy);
108 schedule_work(&rmn->work);
113 * amdgpu_mn_lock - take the write side lock for this mn
115 void amdgpu_mn_lock(struct amdgpu_mn *mn)
118 down_write(&mn->lock);
122 * amdgpu_mn_unlock - drop the write side lock for this mn
124 void amdgpu_mn_unlock(struct amdgpu_mn *mn)
131 * amdgpu_mn_read_lock - take the rmn read lock
135 * Take the rmn read side lock.
137 static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
139 mutex_lock(&rmn->read_lock);
140 if (atomic_inc_return(&rmn->recursion) == 1)
141 down_read_non_owner(&rmn->lock);
142 mutex_unlock(&rmn->read_lock);
146 * amdgpu_mn_read_unlock - drop the rmn read lock
150 * Drop the rmn read side lock.
152 static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
154 if (atomic_dec_return(&rmn->recursion) == 0)
155 up_read_non_owner(&rmn->lock);
159 * amdgpu_mn_invalidate_node - unmap all BOs of a node
161 * @node: the node with the BOs to unmap
163 * We block for all BOs and unmap them by move them
164 * into system domain again.
166 static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
170 struct amdgpu_bo *bo;
173 list_for_each_entry(bo, &node->bos, mn_list) {
175 if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
178 r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
179 true, false, MAX_SCHEDULE_TIMEOUT);
181 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
183 amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
188 * amdgpu_mn_invalidate_page - callback to notify about mm change
191 * @mn: the mm this callback is about
192 * @address: address of invalidate page
194 * Invalidation of a single page. Blocks for all BOs mapping it
195 * and unmap them by move them into system domain again.
197 static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
198 struct mm_struct *mm,
199 unsigned long address)
201 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
202 struct interval_tree_node *it;
204 amdgpu_mn_read_lock(rmn);
206 it = interval_tree_iter_first(&rmn->objects, address, address);
208 struct amdgpu_mn_node *node;
210 node = container_of(it, struct amdgpu_mn_node, it);
211 amdgpu_mn_invalidate_node(node, address, address);
214 amdgpu_mn_read_unlock(rmn);
218 * amdgpu_mn_invalidate_range_start - callback to notify about mm change
221 * @mn: the mm this callback is about
222 * @start: start of updated range
223 * @end: end of updated range
225 * We block for all BOs between start and end to be idle and
226 * unmap them by move them into system domain again.
228 static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
229 struct mm_struct *mm,
233 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
234 struct interval_tree_node *it;
236 /* notification is exclusive, but interval is inclusive */
239 amdgpu_mn_read_lock(rmn);
241 it = interval_tree_iter_first(&rmn->objects, start, end);
243 struct amdgpu_mn_node *node;
245 node = container_of(it, struct amdgpu_mn_node, it);
246 it = interval_tree_iter_next(it, start, end);
248 amdgpu_mn_invalidate_node(node, start, end);
253 * amdgpu_mn_invalidate_range_end - callback to notify about mm change
256 * @mn: the mm this callback is about
257 * @start: start of updated range
258 * @end: end of updated range
260 * Release the lock again to allow new command submissions.
262 static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
263 struct mm_struct *mm,
267 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
269 amdgpu_mn_read_unlock(rmn);
272 static const struct mmu_notifier_ops amdgpu_mn_ops = {
273 .release = amdgpu_mn_release,
274 .invalidate_page = amdgpu_mn_invalidate_page,
275 .invalidate_range_start = amdgpu_mn_invalidate_range_start,
276 .invalidate_range_end = amdgpu_mn_invalidate_range_end,
280 * amdgpu_mn_get - create notifier context
282 * @adev: amdgpu device pointer
284 * Creates a notifier context for current->mm.
286 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
288 struct mm_struct *mm = current->mm;
289 struct amdgpu_mn *rmn;
292 mutex_lock(&adev->mn_lock);
293 if (down_write_killable(&mm->mmap_sem)) {
294 mutex_unlock(&adev->mn_lock);
295 return ERR_PTR(-EINTR);
298 hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm)
302 rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
304 rmn = ERR_PTR(-ENOMEM);
310 rmn->mn.ops = &amdgpu_mn_ops;
311 init_rwsem(&rmn->lock);
312 rmn->objects = RB_ROOT;
313 mutex_init(&rmn->read_lock);
314 atomic_set(&rmn->recursion, 0);
316 r = __mmu_notifier_register(&rmn->mn, mm);
320 hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm);
323 up_write(&mm->mmap_sem);
324 mutex_unlock(&adev->mn_lock);
329 up_write(&mm->mmap_sem);
330 mutex_unlock(&adev->mn_lock);
337 * amdgpu_mn_register - register a BO for notifier updates
339 * @bo: amdgpu buffer object
340 * @addr: userptr addr we should monitor
342 * Registers an MMU notifier for the given BO at the specified address.
343 * Returns 0 on success, -ERRNO if anything goes wrong.
345 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
347 unsigned long end = addr + amdgpu_bo_size(bo) - 1;
348 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
349 struct amdgpu_mn *rmn;
350 struct amdgpu_mn_node *node = NULL;
351 struct list_head bos;
352 struct interval_tree_node *it;
354 rmn = amdgpu_mn_get(adev);
358 INIT_LIST_HEAD(&bos);
360 down_write(&rmn->lock);
362 while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
364 node = container_of(it, struct amdgpu_mn_node, it);
365 interval_tree_remove(&node->it, &rmn->objects);
366 addr = min(it->start, addr);
367 end = max(it->last, end);
368 list_splice(&node->bos, &bos);
372 node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
374 up_write(&rmn->lock);
381 node->it.start = addr;
383 INIT_LIST_HEAD(&node->bos);
384 list_splice(&bos, &node->bos);
385 list_add(&bo->mn_list, &node->bos);
387 interval_tree_insert(&node->it, &rmn->objects);
389 up_write(&rmn->lock);
395 * amdgpu_mn_unregister - unregister a BO for notifier updates
397 * @bo: amdgpu buffer object
399 * Remove any registration of MMU notifier updates from the buffer object.
401 void amdgpu_mn_unregister(struct amdgpu_bo *bo)
403 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
404 struct amdgpu_mn *rmn;
405 struct list_head *head;
407 mutex_lock(&adev->mn_lock);
411 mutex_unlock(&adev->mn_lock);
415 down_write(&rmn->lock);
417 /* save the next list entry for later */
418 head = bo->mn_list.next;
421 list_del_init(&bo->mn_list);
423 if (list_empty(head)) {
424 struct amdgpu_mn_node *node;
425 node = container_of(head, struct amdgpu_mn_node, bos);
426 interval_tree_remove(&node->it, &rmn->objects);
430 up_write(&rmn->lock);
431 mutex_unlock(&adev->mn_lock);