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
28 * Christian König <christian.koenig@amd.com>
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/mmu_notifier.h>
40 /* constant after initialisation */
41 struct radeon_device
*rdev
;
43 struct mmu_notifier mn
;
45 /* only used on destruction */
46 struct work_struct work
;
48 /* protected by rdev->mn_lock */
49 struct hlist_node node
;
51 /* objects protected by lock */
53 struct rb_root_cached objects
;
56 struct radeon_mn_node
{
57 struct interval_tree_node it
;
62 * radeon_mn_destroy - destroy the rmn
64 * @work: previously sheduled work item
66 * Lazy destroys the notifier from a work item
68 static void radeon_mn_destroy(struct work_struct
*work
)
70 struct radeon_mn
*rmn
= container_of(work
, struct radeon_mn
, work
);
71 struct radeon_device
*rdev
= rmn
->rdev
;
72 struct radeon_mn_node
*node
, *next_node
;
73 struct radeon_bo
*bo
, *next_bo
;
75 mutex_lock(&rdev
->mn_lock
);
76 mutex_lock(&rmn
->lock
);
78 rbtree_postorder_for_each_entry_safe(node
, next_node
,
79 &rmn
->objects
.rb_root
, it
.rb
) {
81 interval_tree_remove(&node
->it
, &rmn
->objects
);
82 list_for_each_entry_safe(bo
, next_bo
, &node
->bos
, mn_list
) {
84 list_del_init(&bo
->mn_list
);
88 mutex_unlock(&rmn
->lock
);
89 mutex_unlock(&rdev
->mn_lock
);
90 mmu_notifier_unregister(&rmn
->mn
, rmn
->mm
);
95 * radeon_mn_release - callback to notify about mm destruction
98 * @mn: the mm this callback is about
100 * Shedule a work item to lazy destroy our notifier.
102 static void radeon_mn_release(struct mmu_notifier
*mn
,
103 struct mm_struct
*mm
)
105 struct radeon_mn
*rmn
= container_of(mn
, struct radeon_mn
, mn
);
106 INIT_WORK(&rmn
->work
, radeon_mn_destroy
);
107 schedule_work(&rmn
->work
);
111 * radeon_mn_invalidate_range_start - callback to notify about mm change
114 * @mn: the mm this callback is about
115 * @start: start of updated range
116 * @end: end of updated range
118 * We block for all BOs between start and end to be idle and
119 * unmap them by move them into system domain again.
121 static int radeon_mn_invalidate_range_start(struct mmu_notifier
*mn
,
122 struct mm_struct
*mm
,
127 struct radeon_mn
*rmn
= container_of(mn
, struct radeon_mn
, mn
);
128 struct ttm_operation_ctx ctx
= { false, false };
129 struct interval_tree_node
*it
;
132 /* notification is exclusive, but interval is inclusive */
135 /* TODO we should be able to split locking for interval tree and
139 mutex_lock(&rmn
->lock
);
140 else if (!mutex_trylock(&rmn
->lock
))
143 it
= interval_tree_iter_first(&rmn
->objects
, start
, end
);
145 struct radeon_mn_node
*node
;
146 struct radeon_bo
*bo
;
154 node
= container_of(it
, struct radeon_mn_node
, it
);
155 it
= interval_tree_iter_next(it
, start
, end
);
157 list_for_each_entry(bo
, &node
->bos
, mn_list
) {
159 if (!bo
->tbo
.ttm
|| bo
->tbo
.ttm
->state
!= tt_bound
)
162 r
= radeon_bo_reserve(bo
, true);
164 DRM_ERROR("(%ld) failed to reserve user bo\n", r
);
168 r
= reservation_object_wait_timeout_rcu(bo
->tbo
.resv
,
169 true, false, MAX_SCHEDULE_TIMEOUT
);
171 DRM_ERROR("(%ld) failed to wait for user bo\n", r
);
173 radeon_ttm_placement_from_domain(bo
, RADEON_GEM_DOMAIN_CPU
);
174 r
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, &ctx
);
176 DRM_ERROR("(%ld) failed to validate user bo\n", r
);
178 radeon_bo_unreserve(bo
);
183 mutex_unlock(&rmn
->lock
);
188 static const struct mmu_notifier_ops radeon_mn_ops
= {
189 .release
= radeon_mn_release
,
190 .invalidate_range_start
= radeon_mn_invalidate_range_start
,
194 * radeon_mn_get - create notifier context
196 * @rdev: radeon device pointer
198 * Creates a notifier context for current->mm.
200 static struct radeon_mn
*radeon_mn_get(struct radeon_device
*rdev
)
202 struct mm_struct
*mm
= current
->mm
;
203 struct radeon_mn
*rmn
;
206 if (down_write_killable(&mm
->mmap_sem
))
207 return ERR_PTR(-EINTR
);
209 mutex_lock(&rdev
->mn_lock
);
211 hash_for_each_possible(rdev
->mn_hash
, rmn
, node
, (unsigned long)mm
)
215 rmn
= kzalloc(sizeof(*rmn
), GFP_KERNEL
);
217 rmn
= ERR_PTR(-ENOMEM
);
223 rmn
->mn
.ops
= &radeon_mn_ops
;
224 mutex_init(&rmn
->lock
);
225 rmn
->objects
= RB_ROOT_CACHED
;
227 r
= __mmu_notifier_register(&rmn
->mn
, mm
);
231 hash_add(rdev
->mn_hash
, &rmn
->node
, (unsigned long)mm
);
234 mutex_unlock(&rdev
->mn_lock
);
235 up_write(&mm
->mmap_sem
);
240 mutex_unlock(&rdev
->mn_lock
);
241 up_write(&mm
->mmap_sem
);
248 * radeon_mn_register - register a BO for notifier updates
250 * @bo: radeon buffer object
251 * @addr: userptr addr we should monitor
253 * Registers an MMU notifier for the given BO at the specified address.
254 * Returns 0 on success, -ERRNO if anything goes wrong.
256 int radeon_mn_register(struct radeon_bo
*bo
, unsigned long addr
)
258 unsigned long end
= addr
+ radeon_bo_size(bo
) - 1;
259 struct radeon_device
*rdev
= bo
->rdev
;
260 struct radeon_mn
*rmn
;
261 struct radeon_mn_node
*node
= NULL
;
262 struct list_head bos
;
263 struct interval_tree_node
*it
;
265 rmn
= radeon_mn_get(rdev
);
269 INIT_LIST_HEAD(&bos
);
271 mutex_lock(&rmn
->lock
);
273 while ((it
= interval_tree_iter_first(&rmn
->objects
, addr
, end
))) {
275 node
= container_of(it
, struct radeon_mn_node
, it
);
276 interval_tree_remove(&node
->it
, &rmn
->objects
);
277 addr
= min(it
->start
, addr
);
278 end
= max(it
->last
, end
);
279 list_splice(&node
->bos
, &bos
);
283 node
= kmalloc(sizeof(struct radeon_mn_node
), GFP_KERNEL
);
285 mutex_unlock(&rmn
->lock
);
292 node
->it
.start
= addr
;
294 INIT_LIST_HEAD(&node
->bos
);
295 list_splice(&bos
, &node
->bos
);
296 list_add(&bo
->mn_list
, &node
->bos
);
298 interval_tree_insert(&node
->it
, &rmn
->objects
);
300 mutex_unlock(&rmn
->lock
);
306 * radeon_mn_unregister - unregister a BO for notifier updates
308 * @bo: radeon buffer object
310 * Remove any registration of MMU notifier updates from the buffer object.
312 void radeon_mn_unregister(struct radeon_bo
*bo
)
314 struct radeon_device
*rdev
= bo
->rdev
;
315 struct radeon_mn
*rmn
;
316 struct list_head
*head
;
318 mutex_lock(&rdev
->mn_lock
);
321 mutex_unlock(&rdev
->mn_lock
);
325 mutex_lock(&rmn
->lock
);
326 /* save the next list entry for later */
327 head
= bo
->mn_list
.next
;
330 list_del(&bo
->mn_list
);
332 if (list_empty(head
)) {
333 struct radeon_mn_node
*node
;
334 node
= container_of(head
, struct radeon_mn_node
, bos
);
335 interval_tree_remove(&node
->it
, &rmn
->objects
);
339 mutex_unlock(&rmn
->lock
);
340 mutex_unlock(&rdev
->mn_lock
);