xref: /linux/drivers/gpu/drm/radeon/radeon_mn.c (revision 22e2e86560c5fca6f9b9d078f221fcdab9947a5e)
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
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:
12  *
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.
20  *
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.
24  *
25  */
26 /*
27  * Authors:
28  *    Christian König <christian.koenig@amd.com>
29  */
30 
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>
36 
37 #include "radeon.h"
38 
39 struct radeon_mn {
40 	/* constant after initialisation */
41 	struct radeon_device	*rdev;
42 	struct mm_struct	*mm;
43 	struct mmu_notifier	mn;
44 
45 	/* only used on destruction */
46 	struct work_struct	work;
47 
48 	/* protected by rdev->mn_lock */
49 	struct hlist_node	node;
50 
51 	/* objects protected by lock */
52 	struct mutex		lock;
53 	struct rb_root		objects;
54 };
55 
56 /**
57  * radeon_mn_destroy - destroy the rmn
58  *
59  * @work: previously sheduled work item
60  *
61  * Lazy destroys the notifier from a work item
62  */
63 static void radeon_mn_destroy(struct work_struct *work)
64 {
65 	struct radeon_mn *rmn = container_of(work, struct radeon_mn, work);
66 	struct radeon_device *rdev = rmn->rdev;
67 	struct radeon_bo *bo, *next;
68 
69 	mutex_lock(&rdev->mn_lock);
70 	mutex_lock(&rmn->lock);
71 	hash_del(&rmn->node);
72 	rbtree_postorder_for_each_entry_safe(bo, next, &rmn->objects, mn_it.rb) {
73 		interval_tree_remove(&bo->mn_it, &rmn->objects);
74 		bo->mn = NULL;
75 	}
76 	mutex_unlock(&rmn->lock);
77 	mutex_unlock(&rdev->mn_lock);
78 	mmu_notifier_unregister(&rmn->mn, rmn->mm);
79 	kfree(rmn);
80 }
81 
82 /**
83  * radeon_mn_release - callback to notify about mm destruction
84  *
85  * @mn: our notifier
86  * @mn: the mm this callback is about
87  *
88  * Shedule a work item to lazy destroy our notifier.
89  */
90 static void radeon_mn_release(struct mmu_notifier *mn,
91 			      struct mm_struct *mm)
92 {
93 	struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
94 	INIT_WORK(&rmn->work, radeon_mn_destroy);
95 	schedule_work(&rmn->work);
96 }
97 
98 /**
99  * radeon_mn_invalidate_range_start - callback to notify about mm change
100  *
101  * @mn: our notifier
102  * @mn: the mm this callback is about
103  * @start: start of updated range
104  * @end: end of updated range
105  *
106  * We block for all BOs between start and end to be idle and
107  * unmap them by move them into system domain again.
108  */
109 static void radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
110 					     struct mm_struct *mm,
111 					     unsigned long start,
112 					     unsigned long end)
113 {
114 	struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
115 	struct interval_tree_node *it;
116 
117 	/* notification is exclusive, but interval is inclusive */
118 	end -= 1;
119 
120 	mutex_lock(&rmn->lock);
121 
122 	it = interval_tree_iter_first(&rmn->objects, start, end);
123 	while (it) {
124 		struct radeon_bo *bo;
125 		int r;
126 
127 		bo = container_of(it, struct radeon_bo, mn_it);
128 		it = interval_tree_iter_next(it, start, end);
129 
130 		r = radeon_bo_reserve(bo, true);
131 		if (r) {
132 			DRM_ERROR("(%d) failed to reserve user bo\n", r);
133 			continue;
134 		}
135 
136 		r = reservation_object_wait_timeout_rcu(bo->tbo.resv, true,
137 			false, MAX_SCHEDULE_TIMEOUT);
138 		if (r)
139 			DRM_ERROR("(%d) failed to wait for user bo\n", r);
140 
141 		radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
142 		r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
143 		if (r)
144 			DRM_ERROR("(%d) failed to validate user bo\n", r);
145 
146 		radeon_bo_unreserve(bo);
147 	}
148 
149 	mutex_unlock(&rmn->lock);
150 }
151 
152 static const struct mmu_notifier_ops radeon_mn_ops = {
153 	.release = radeon_mn_release,
154 	.invalidate_range_start = radeon_mn_invalidate_range_start,
155 };
156 
157 /**
158  * radeon_mn_get - create notifier context
159  *
160  * @rdev: radeon device pointer
161  *
162  * Creates a notifier context for current->mm.
163  */
164 static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
165 {
166 	struct mm_struct *mm = current->mm;
167 	struct radeon_mn *rmn;
168 	int r;
169 
170 	down_write(&mm->mmap_sem);
171 	mutex_lock(&rdev->mn_lock);
172 
173 	hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
174 		if (rmn->mm == mm)
175 			goto release_locks;
176 
177 	rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
178 	if (!rmn) {
179 		rmn = ERR_PTR(-ENOMEM);
180 		goto release_locks;
181 	}
182 
183 	rmn->rdev = rdev;
184 	rmn->mm = mm;
185 	rmn->mn.ops = &radeon_mn_ops;
186 	mutex_init(&rmn->lock);
187 	rmn->objects = RB_ROOT;
188 
189 	r = __mmu_notifier_register(&rmn->mn, mm);
190 	if (r)
191 		goto free_rmn;
192 
193 	hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
194 
195 release_locks:
196 	mutex_unlock(&rdev->mn_lock);
197 	up_write(&mm->mmap_sem);
198 
199 	return rmn;
200 
201 free_rmn:
202 	mutex_unlock(&rdev->mn_lock);
203 	up_write(&mm->mmap_sem);
204 	kfree(rmn);
205 
206 	return ERR_PTR(r);
207 }
208 
209 /**
210  * radeon_mn_register - register a BO for notifier updates
211  *
212  * @bo: radeon buffer object
213  * @addr: userptr addr we should monitor
214  *
215  * Registers an MMU notifier for the given BO at the specified address.
216  * Returns 0 on success, -ERRNO if anything goes wrong.
217  */
218 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
219 {
220 	unsigned long end = addr + radeon_bo_size(bo) - 1;
221 	struct radeon_device *rdev = bo->rdev;
222 	struct radeon_mn *rmn;
223 	struct interval_tree_node *it;
224 
225 	rmn = radeon_mn_get(rdev);
226 	if (IS_ERR(rmn))
227 		return PTR_ERR(rmn);
228 
229 	mutex_lock(&rmn->lock);
230 
231 	it = interval_tree_iter_first(&rmn->objects, addr, end);
232 	if (it) {
233 		mutex_unlock(&rmn->lock);
234 		return -EEXIST;
235 	}
236 
237 	bo->mn = rmn;
238 	bo->mn_it.start = addr;
239 	bo->mn_it.last = end;
240 	interval_tree_insert(&bo->mn_it, &rmn->objects);
241 
242 	mutex_unlock(&rmn->lock);
243 
244 	return 0;
245 }
246 
247 /**
248  * radeon_mn_unregister - unregister a BO for notifier updates
249  *
250  * @bo: radeon buffer object
251  *
252  * Remove any registration of MMU notifier updates from the buffer object.
253  */
254 void radeon_mn_unregister(struct radeon_bo *bo)
255 {
256 	struct radeon_device *rdev = bo->rdev;
257 	struct radeon_mn *rmn;
258 
259 	mutex_lock(&rdev->mn_lock);
260 	rmn = bo->mn;
261 	if (rmn == NULL) {
262 		mutex_unlock(&rdev->mn_lock);
263 		return;
264 	}
265 
266 	mutex_lock(&rmn->lock);
267 	interval_tree_remove(&bo->mn_it, &rmn->objects);
268 	bo->mn = NULL;
269 	mutex_unlock(&rmn->lock);
270 	mutex_unlock(&rdev->mn_lock);
271 }
272