xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/radeon/radeon_mn.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: radeon_mn.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 Advanced Micro Devices, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * The above copyright notice and this permission notice (including the
24  * next paragraph) shall be included in all copies or substantial portions
25  * of the Software.
26  *
27  */
28 /*
29  * Authors:
30  *    Christian König <christian.koenig@amd.com>
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: radeon_mn.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $");
35 
36 #include <linux/firmware.h>
37 #include <linux/module.h>
38 #include <linux/mmu_notifier.h>
39 #include <drm/drmP.h>
40 #include <drm/drm.h>
41 
42 #include "radeon.h"
43 
44 struct radeon_mn {
45 	/* constant after initialisation */
46 	struct radeon_device	*rdev;
47 	struct mm_struct	*mm;
48 	struct mmu_notifier	mn;
49 
50 	/* only used on destruction */
51 	struct work_struct	work;
52 
53 	/* protected by rdev->mn_lock */
54 	struct hlist_node	node;
55 
56 	/* objects protected by lock */
57 	struct mutex		lock;
58 	struct rb_root		objects;
59 };
60 
61 struct radeon_mn_node {
62 	struct interval_tree_node	it;
63 	struct list_head		bos;
64 };
65 
66 /**
67  * radeon_mn_destroy - destroy the rmn
68  *
69  * @work: previously sheduled work item
70  *
71  * Lazy destroys the notifier from a work item
72  */
73 static void radeon_mn_destroy(struct work_struct *work)
74 {
75 	struct radeon_mn *rmn = container_of(work, struct radeon_mn, work);
76 	struct radeon_device *rdev = rmn->rdev;
77 	struct radeon_mn_node *node, *next_node;
78 	struct radeon_bo *bo, *next_bo;
79 
80 	mutex_lock(&rdev->mn_lock);
81 	mutex_lock(&rmn->lock);
82 	hash_del(&rmn->node);
83 	rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
84 					     it.rb) {
85 
86 		interval_tree_remove(&node->it, &rmn->objects);
87 		list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
88 			bo->mn = NULL;
89 			list_del_init(&bo->mn_list);
90 		}
91 		kfree(node);
92 	}
93 	mutex_unlock(&rmn->lock);
94 	mutex_unlock(&rdev->mn_lock);
95 	mmu_notifier_unregister(&rmn->mn, rmn->mm);
96 	kfree(rmn);
97 }
98 
99 /**
100  * radeon_mn_release - callback to notify about mm destruction
101  *
102  * @mn: our notifier
103  * @mn: the mm this callback is about
104  *
105  * Shedule a work item to lazy destroy our notifier.
106  */
107 static void radeon_mn_release(struct mmu_notifier *mn,
108 			      struct mm_struct *mm)
109 {
110 	struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
111 	INIT_WORK(&rmn->work, radeon_mn_destroy);
112 	schedule_work(&rmn->work);
113 }
114 
115 /**
116  * radeon_mn_invalidate_range_start - callback to notify about mm change
117  *
118  * @mn: our notifier
119  * @mn: the mm this callback is about
120  * @start: start of updated range
121  * @end: end of updated range
122  *
123  * We block for all BOs between start and end to be idle and
124  * unmap them by move them into system domain again.
125  */
126 static void radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
127 					     struct mm_struct *mm,
128 					     unsigned long start,
129 					     unsigned long end)
130 {
131 	struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
132 	struct interval_tree_node *it;
133 
134 	/* notification is exclusive, but interval is inclusive */
135 	end -= 1;
136 
137 	mutex_lock(&rmn->lock);
138 
139 	it = interval_tree_iter_first(&rmn->objects, start, end);
140 	while (it) {
141 		struct radeon_mn_node *node;
142 		struct radeon_bo *bo;
143 		long r;
144 
145 		node = container_of(it, struct radeon_mn_node, it);
146 		it = interval_tree_iter_next(it, start, end);
147 
148 		list_for_each_entry(bo, &node->bos, mn_list) {
149 
150 			if (!bo->tbo.ttm || bo->tbo.ttm->state != tt_bound)
151 				continue;
152 
153 			r = radeon_bo_reserve(bo, true);
154 			if (r) {
155 				DRM_ERROR("(%ld) failed to reserve user bo\n", r);
156 				continue;
157 			}
158 
159 			r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
160 				true, false, MAX_SCHEDULE_TIMEOUT);
161 			if (r <= 0)
162 				DRM_ERROR("(%ld) failed to wait for user bo\n", r);
163 
164 			radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
165 			r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
166 			if (r)
167 				DRM_ERROR("(%ld) failed to validate user bo\n", r);
168 
169 			radeon_bo_unreserve(bo);
170 		}
171 	}
172 
173 	mutex_unlock(&rmn->lock);
174 }
175 
176 static const struct mmu_notifier_ops radeon_mn_ops = {
177 	.release = radeon_mn_release,
178 	.invalidate_range_start = radeon_mn_invalidate_range_start,
179 };
180 
181 /**
182  * radeon_mn_get - create notifier context
183  *
184  * @rdev: radeon device pointer
185  *
186  * Creates a notifier context for current->mm.
187  */
188 static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
189 {
190 	struct mm_struct *mm = current->mm;
191 	struct radeon_mn *rmn;
192 	int r;
193 
194 	down_write(&mm->mmap_sem);
195 	mutex_lock(&rdev->mn_lock);
196 
197 	hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
198 		if (rmn->mm == mm)
199 			goto release_locks;
200 
201 	rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
202 	if (!rmn) {
203 		rmn = ERR_PTR(-ENOMEM);
204 		goto release_locks;
205 	}
206 
207 	rmn->rdev = rdev;
208 	rmn->mm = mm;
209 	rmn->mn.ops = &radeon_mn_ops;
210 	mutex_init(&rmn->lock);
211 	rmn->objects = RB_ROOT;
212 
213 	r = __mmu_notifier_register(&rmn->mn, mm);
214 	if (r)
215 		goto free_rmn;
216 
217 	hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
218 
219 release_locks:
220 	mutex_unlock(&rdev->mn_lock);
221 	up_write(&mm->mmap_sem);
222 
223 	return rmn;
224 
225 free_rmn:
226 	mutex_unlock(&rdev->mn_lock);
227 	up_write(&mm->mmap_sem);
228 	kfree(rmn);
229 
230 	return ERR_PTR(r);
231 }
232 
233 /**
234  * radeon_mn_register - register a BO for notifier updates
235  *
236  * @bo: radeon buffer object
237  * @addr: userptr addr we should monitor
238  *
239  * Registers an MMU notifier for the given BO at the specified address.
240  * Returns 0 on success, -ERRNO if anything goes wrong.
241  */
242 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
243 {
244 	unsigned long end = addr + radeon_bo_size(bo) - 1;
245 	struct radeon_device *rdev = bo->rdev;
246 	struct radeon_mn *rmn;
247 	struct radeon_mn_node *node = NULL;
248 	struct list_head bos;
249 	struct interval_tree_node *it;
250 
251 	rmn = radeon_mn_get(rdev);
252 	if (IS_ERR(rmn))
253 		return PTR_ERR(rmn);
254 
255 	INIT_LIST_HEAD(&bos);
256 
257 	mutex_lock(&rmn->lock);
258 
259 	while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
260 		kfree(node);
261 		node = container_of(it, struct radeon_mn_node, it);
262 		interval_tree_remove(&node->it, &rmn->objects);
263 		addr = min(it->start, addr);
264 		end = max(it->last, end);
265 		list_splice(&node->bos, &bos);
266 	}
267 
268 	if (!node) {
269 		node = kmalloc(sizeof(struct radeon_mn_node), GFP_KERNEL);
270 		if (!node) {
271 			mutex_unlock(&rmn->lock);
272 			return -ENOMEM;
273 		}
274 	}
275 
276 	bo->mn = rmn;
277 
278 	node->it.start = addr;
279 	node->it.last = end;
280 	INIT_LIST_HEAD(&node->bos);
281 	list_splice(&bos, &node->bos);
282 	list_add(&bo->mn_list, &node->bos);
283 
284 	interval_tree_insert(&node->it, &rmn->objects);
285 
286 	mutex_unlock(&rmn->lock);
287 
288 	return 0;
289 }
290 
291 /**
292  * radeon_mn_unregister - unregister a BO for notifier updates
293  *
294  * @bo: radeon buffer object
295  *
296  * Remove any registration of MMU notifier updates from the buffer object.
297  */
298 void radeon_mn_unregister(struct radeon_bo *bo)
299 {
300 	struct radeon_device *rdev = bo->rdev;
301 	struct radeon_mn *rmn;
302 	struct list_head *head;
303 
304 	mutex_lock(&rdev->mn_lock);
305 	rmn = bo->mn;
306 	if (rmn == NULL) {
307 		mutex_unlock(&rdev->mn_lock);
308 		return;
309 	}
310 
311 	mutex_lock(&rmn->lock);
312 	/* save the next list entry for later */
313 	head = bo->mn_list.next;
314 
315 	bo->mn = NULL;
316 	list_del(&bo->mn_list);
317 
318 	if (list_empty(head)) {
319 		struct radeon_mn_node *node;
320 		node = container_of(head, struct radeon_mn_node, bos);
321 		interval_tree_remove(&node->it, &rmn->objects);
322 		kfree(node);
323 	}
324 
325 	mutex_unlock(&rmn->lock);
326 	mutex_unlock(&rdev->mn_lock);
327 }
328