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 struct mmu_notifier mn;
42 /* objects protected by lock */
44 struct rb_root_cached objects;
47 struct radeon_mn_node {
48 struct interval_tree_node it;
53 * radeon_mn_invalidate_range_start - callback to notify about mm change
56 * @mn: the mm this callback is about
57 * @start: start of updated range
58 * @end: end of updated range
60 * We block for all BOs between start and end to be idle and
61 * unmap them by move them into system domain again.
63 static int radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
64 const struct mmu_notifier_range *range)
66 struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
67 struct ttm_operation_ctx ctx = { false, false };
68 struct interval_tree_node *it;
72 /* notification is exclusive, but interval is inclusive */
75 /* TODO we should be able to split locking for interval tree and
78 if (mmu_notifier_range_blockable(range))
79 mutex_lock(&rmn->lock);
80 else if (!mutex_trylock(&rmn->lock))
83 it = interval_tree_iter_first(&rmn->objects, range->start, end);
85 struct radeon_mn_node *node;
89 if (!mmu_notifier_range_blockable(range)) {
94 node = container_of(it, struct radeon_mn_node, it);
95 it = interval_tree_iter_next(it, range->start, end);
97 list_for_each_entry(bo, &node->bos, mn_list) {
99 if (!bo->tbo.ttm || bo->tbo.ttm->state != tt_bound)
102 r = radeon_bo_reserve(bo, true);
104 DRM_ERROR("(%ld) failed to reserve user bo\n", r);
108 r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv,
109 true, false, MAX_SCHEDULE_TIMEOUT);
111 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
113 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
114 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
116 DRM_ERROR("(%ld) failed to validate user bo\n", r);
118 radeon_bo_unreserve(bo);
123 mutex_unlock(&rmn->lock);
128 static void radeon_mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
130 struct mmu_notifier_range range = {
135 .event = MMU_NOTIFY_UNMAP,
138 radeon_mn_invalidate_range_start(mn, &range);
141 static struct mmu_notifier *radeon_mn_alloc_notifier(struct mm_struct *mm)
143 struct radeon_mn *rmn;
145 rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
149 mutex_init(&rmn->lock);
150 rmn->objects = RB_ROOT_CACHED;
154 static void radeon_mn_free_notifier(struct mmu_notifier *mn)
156 kfree(container_of(mn, struct radeon_mn, mn));
159 static const struct mmu_notifier_ops radeon_mn_ops = {
160 .release = radeon_mn_release,
161 .invalidate_range_start = radeon_mn_invalidate_range_start,
162 .alloc_notifier = radeon_mn_alloc_notifier,
163 .free_notifier = radeon_mn_free_notifier,
167 * radeon_mn_register - register a BO for notifier updates
169 * @bo: radeon buffer object
170 * @addr: userptr addr we should monitor
172 * Registers an MMU notifier for the given BO at the specified address.
173 * Returns 0 on success, -ERRNO if anything goes wrong.
175 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
177 unsigned long end = addr + radeon_bo_size(bo) - 1;
178 struct mmu_notifier *mn;
179 struct radeon_mn *rmn;
180 struct radeon_mn_node *node = NULL;
181 struct list_head bos;
182 struct interval_tree_node *it;
184 mn = mmu_notifier_get(&radeon_mn_ops, current->mm);
187 rmn = container_of(mn, struct radeon_mn, mn);
189 INIT_LIST_HEAD(&bos);
191 mutex_lock(&rmn->lock);
193 while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
195 node = container_of(it, struct radeon_mn_node, it);
196 interval_tree_remove(&node->it, &rmn->objects);
197 addr = min(it->start, addr);
198 end = max(it->last, end);
199 list_splice(&node->bos, &bos);
203 node = kmalloc(sizeof(struct radeon_mn_node), GFP_KERNEL);
205 mutex_unlock(&rmn->lock);
212 node->it.start = addr;
214 INIT_LIST_HEAD(&node->bos);
215 list_splice(&bos, &node->bos);
216 list_add(&bo->mn_list, &node->bos);
218 interval_tree_insert(&node->it, &rmn->objects);
220 mutex_unlock(&rmn->lock);
226 * radeon_mn_unregister - unregister a BO for notifier updates
228 * @bo: radeon buffer object
230 * Remove any registration of MMU notifier updates from the buffer object.
232 void radeon_mn_unregister(struct radeon_bo *bo)
234 struct radeon_mn *rmn = bo->mn;
235 struct list_head *head;
240 mutex_lock(&rmn->lock);
241 /* save the next list entry for later */
242 head = bo->mn_list.next;
244 list_del(&bo->mn_list);
246 if (list_empty(head)) {
247 struct radeon_mn_node *node;
248 node = container_of(head, struct radeon_mn_node, bos);
249 interval_tree_remove(&node->it, &rmn->objects);
253 mutex_unlock(&rmn->lock);
255 mmu_notifier_put(&rmn->mn);