Linux 4.1.16
[linux/fpc-iii.git] / drivers / gpu / drm / radeon / radeon_mn.c
blobeef006c4858499dd207c6eb70ad5fcbc05c53941
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
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
23 * of the Software.
27 * Authors:
28 * Christian König <christian.koenig@amd.com>
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/mmu_notifier.h>
34 #include <drm/drmP.h>
35 #include <drm/drm.h>
37 #include "radeon.h"
39 struct radeon_mn {
40 /* constant after initialisation */
41 struct radeon_device *rdev;
42 struct mm_struct *mm;
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 */
52 struct mutex lock;
53 struct rb_root objects;
56 struct radeon_mn_node {
57 struct interval_tree_node it;
58 struct list_head bos;
61 /**
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);
77 hash_del(&rmn->node);
78 rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
79 it.rb) {
81 interval_tree_remove(&node->it, &rmn->objects);
82 list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
83 bo->mn = NULL;
84 list_del_init(&bo->mn_list);
86 kfree(node);
88 mutex_unlock(&rmn->lock);
89 mutex_unlock(&rdev->mn_lock);
90 mmu_notifier_unregister(&rmn->mn, rmn->mm);
91 kfree(rmn);
94 /**
95 * radeon_mn_release - callback to notify about mm destruction
97 * @mn: our notifier
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
113 * @mn: our notifier
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 void radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
122 struct mm_struct *mm,
123 unsigned long start,
124 unsigned long end)
126 struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
127 struct interval_tree_node *it;
129 /* notification is exclusive, but interval is inclusive */
130 end -= 1;
132 mutex_lock(&rmn->lock);
134 it = interval_tree_iter_first(&rmn->objects, start, end);
135 while (it) {
136 struct radeon_mn_node *node;
137 struct radeon_bo *bo;
138 long r;
140 node = container_of(it, struct radeon_mn_node, it);
141 it = interval_tree_iter_next(it, start, end);
143 list_for_each_entry(bo, &node->bos, mn_list) {
145 if (!bo->tbo.ttm || bo->tbo.ttm->state != tt_bound)
146 continue;
148 r = radeon_bo_reserve(bo, true);
149 if (r) {
150 DRM_ERROR("(%ld) failed to reserve user bo\n", r);
151 continue;
154 r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
155 true, false, MAX_SCHEDULE_TIMEOUT);
156 if (r <= 0)
157 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
159 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
160 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
161 if (r)
162 DRM_ERROR("(%ld) failed to validate user bo\n", r);
164 radeon_bo_unreserve(bo);
168 mutex_unlock(&rmn->lock);
171 static const struct mmu_notifier_ops radeon_mn_ops = {
172 .release = radeon_mn_release,
173 .invalidate_range_start = radeon_mn_invalidate_range_start,
177 * radeon_mn_get - create notifier context
179 * @rdev: radeon device pointer
181 * Creates a notifier context for current->mm.
183 static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
185 struct mm_struct *mm = current->mm;
186 struct radeon_mn *rmn;
187 int r;
189 down_write(&mm->mmap_sem);
190 mutex_lock(&rdev->mn_lock);
192 hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
193 if (rmn->mm == mm)
194 goto release_locks;
196 rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
197 if (!rmn) {
198 rmn = ERR_PTR(-ENOMEM);
199 goto release_locks;
202 rmn->rdev = rdev;
203 rmn->mm = mm;
204 rmn->mn.ops = &radeon_mn_ops;
205 mutex_init(&rmn->lock);
206 rmn->objects = RB_ROOT;
208 r = __mmu_notifier_register(&rmn->mn, mm);
209 if (r)
210 goto free_rmn;
212 hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
214 release_locks:
215 mutex_unlock(&rdev->mn_lock);
216 up_write(&mm->mmap_sem);
218 return rmn;
220 free_rmn:
221 mutex_unlock(&rdev->mn_lock);
222 up_write(&mm->mmap_sem);
223 kfree(rmn);
225 return ERR_PTR(r);
229 * radeon_mn_register - register a BO for notifier updates
231 * @bo: radeon buffer object
232 * @addr: userptr addr we should monitor
234 * Registers an MMU notifier for the given BO at the specified address.
235 * Returns 0 on success, -ERRNO if anything goes wrong.
237 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
239 unsigned long end = addr + radeon_bo_size(bo) - 1;
240 struct radeon_device *rdev = bo->rdev;
241 struct radeon_mn *rmn;
242 struct radeon_mn_node *node = NULL;
243 struct list_head bos;
244 struct interval_tree_node *it;
246 rmn = radeon_mn_get(rdev);
247 if (IS_ERR(rmn))
248 return PTR_ERR(rmn);
250 INIT_LIST_HEAD(&bos);
252 mutex_lock(&rmn->lock);
254 while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
255 kfree(node);
256 node = container_of(it, struct radeon_mn_node, it);
257 interval_tree_remove(&node->it, &rmn->objects);
258 addr = min(it->start, addr);
259 end = max(it->last, end);
260 list_splice(&node->bos, &bos);
263 if (!node) {
264 node = kmalloc(sizeof(struct radeon_mn_node), GFP_KERNEL);
265 if (!node) {
266 mutex_unlock(&rmn->lock);
267 return -ENOMEM;
271 bo->mn = rmn;
273 node->it.start = addr;
274 node->it.last = end;
275 INIT_LIST_HEAD(&node->bos);
276 list_splice(&bos, &node->bos);
277 list_add(&bo->mn_list, &node->bos);
279 interval_tree_insert(&node->it, &rmn->objects);
281 mutex_unlock(&rmn->lock);
283 return 0;
287 * radeon_mn_unregister - unregister a BO for notifier updates
289 * @bo: radeon buffer object
291 * Remove any registration of MMU notifier updates from the buffer object.
293 void radeon_mn_unregister(struct radeon_bo *bo)
295 struct radeon_device *rdev = bo->rdev;
296 struct radeon_mn *rmn;
297 struct list_head *head;
299 mutex_lock(&rdev->mn_lock);
300 rmn = bo->mn;
301 if (rmn == NULL) {
302 mutex_unlock(&rdev->mn_lock);
303 return;
306 mutex_lock(&rmn->lock);
307 /* save the next list entry for later */
308 head = bo->mn_list.next;
310 bo->mn = NULL;
311 list_del(&bo->mn_list);
313 if (list_empty(head)) {
314 struct radeon_mn_node *node;
315 node = container_of(head, struct radeon_mn_node, bos);
316 interval_tree_remove(&node->it, &rmn->objects);
317 kfree(node);
320 mutex_unlock(&rmn->lock);
321 mutex_unlock(&rdev->mn_lock);