xref: /dragonfly/sys/dev/drm/radeon/radeon_mn.c (revision 3f2dd94a)
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 struct radeon_mn_node {
57 	struct interval_tree_node	it;
58 	struct list_head		bos;
59 };
60 
61 /**
62  * radeon_mn_destroy - destroy the rmn
63  *
64  * @work: previously sheduled work item
65  *
66  * Lazy destroys the notifier from a work item
67  */
radeon_mn_destroy(struct work_struct * work)68 static void radeon_mn_destroy(struct work_struct *work)
69 {
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;
74 
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) {
80 
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);
85 		}
86 		kfree(node);
87 	}
88 	mutex_unlock(&rmn->lock);
89 	mutex_unlock(&rdev->mn_lock);
90 	mmu_notifier_unregister(&rmn->mn, rmn->mm);
91 	kfree(rmn);
92 }
93 
94 /**
95  * radeon_mn_release - callback to notify about mm destruction
96  *
97  * @mn: our notifier
98  * @mn: the mm this callback is about
99  *
100  * Shedule a work item to lazy destroy our notifier.
101  */
radeon_mn_release(struct mmu_notifier * mn,struct mm_struct * mm)102 static void radeon_mn_release(struct mmu_notifier *mn,
103 			      struct mm_struct *mm)
104 {
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);
108 }
109 
110 /**
111  * radeon_mn_invalidate_range_start - callback to notify about mm change
112  *
113  * @mn: our notifier
114  * @mn: the mm this callback is about
115  * @start: start of updated range
116  * @end: end of updated range
117  *
118  * We block for all BOs between start and end to be idle and
119  * unmap them by move them into system domain again.
120  */
radeon_mn_invalidate_range_start(struct mmu_notifier * mn,struct mm_struct * mm,unsigned long start,unsigned long end)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)
125 {
126 	struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
127 	struct interval_tree_node *it;
128 
129 	/* notification is exclusive, but interval is inclusive */
130 	end -= 1;
131 
132 	mutex_lock(&rmn->lock);
133 
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;
139 
140 		node = container_of(it, struct radeon_mn_node, it);
141 		it = interval_tree_iter_next(it, start, end);
142 
143 		list_for_each_entry(bo, &node->bos, mn_list) {
144 
145 			if (!bo->tbo.ttm || bo->tbo.ttm->state != tt_bound)
146 				continue;
147 
148 			r = radeon_bo_reserve(bo, true);
149 			if (r) {
150 				DRM_ERROR("(%ld) failed to reserve user bo\n", r);
151 				continue;
152 			}
153 
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);
158 
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);
163 
164 			radeon_bo_unreserve(bo);
165 		}
166 	}
167 
168 	mutex_unlock(&rmn->lock);
169 }
170 
171 static const struct mmu_notifier_ops radeon_mn_ops = {
172 	.release = radeon_mn_release,
173 	.invalidate_range_start = radeon_mn_invalidate_range_start,
174 };
175 
176 /**
177  * radeon_mn_get - create notifier context
178  *
179  * @rdev: radeon device pointer
180  *
181  * Creates a notifier context for current->mm.
182  */
radeon_mn_get(struct radeon_device * rdev)183 static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
184 {
185 	struct mm_struct *mm = current->mm;
186 	struct radeon_mn *rmn;
187 	int r;
188 
189 	if (down_write_killable(&mm->mmap_sem))
190 		return ERR_PTR(-EINTR);
191 
192 	mutex_lock(&rdev->mn_lock);
193 
194 	hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
195 		if (rmn->mm == mm)
196 			goto release_locks;
197 
198 	rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
199 	if (!rmn) {
200 		rmn = ERR_PTR(-ENOMEM);
201 		goto release_locks;
202 	}
203 
204 	rmn->rdev = rdev;
205 	rmn->mm = mm;
206 	rmn->mn.ops = &radeon_mn_ops;
207 	mutex_init(&rmn->lock);
208 	rmn->objects = LINUX_RB_ROOT;
209 
210 	r = __mmu_notifier_register(&rmn->mn, mm);
211 	if (r)
212 		goto free_rmn;
213 
214 	hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
215 
216 release_locks:
217 	mutex_unlock(&rdev->mn_lock);
218 	up_write(&mm->mmap_sem);
219 
220 	return rmn;
221 
222 free_rmn:
223 	mutex_unlock(&rdev->mn_lock);
224 	up_write(&mm->mmap_sem);
225 	kfree(rmn);
226 
227 	return ERR_PTR(r);
228 }
229 
230 /**
231  * radeon_mn_register - register a BO for notifier updates
232  *
233  * @bo: radeon buffer object
234  * @addr: userptr addr we should monitor
235  *
236  * Registers an MMU notifier for the given BO at the specified address.
237  * Returns 0 on success, -ERRNO if anything goes wrong.
238  */
radeon_mn_register(struct radeon_bo * bo,unsigned long addr)239 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
240 {
241 	unsigned long end = addr + radeon_bo_size(bo) - 1;
242 	struct radeon_device *rdev = bo->rdev;
243 	struct radeon_mn *rmn;
244 	struct radeon_mn_node *node = NULL;
245 	struct list_head bos;
246 	struct interval_tree_node *it;
247 
248 	rmn = radeon_mn_get(rdev);
249 	if (IS_ERR(rmn))
250 		return PTR_ERR(rmn);
251 
252 	INIT_LIST_HEAD(&bos);
253 
254 	mutex_lock(&rmn->lock);
255 
256 	while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
257 		kfree(node);
258 		node = container_of(it, struct radeon_mn_node, it);
259 		interval_tree_remove(&node->it, &rmn->objects);
260 		addr = min(it->start, addr);
261 		end = max(it->last, end);
262 		list_splice(&node->bos, &bos);
263 	}
264 
265 	if (!node) {
266 		node = kmalloc(sizeof(struct radeon_mn_node), M_DRM,
267 			       GFP_KERNEL);
268 		if (!node) {
269 			mutex_unlock(&rmn->lock);
270 			return -ENOMEM;
271 		}
272 	}
273 
274 	bo->mn = rmn;
275 
276 	node->it.start = addr;
277 	node->it.last = end;
278 	INIT_LIST_HEAD(&node->bos);
279 	list_splice(&bos, &node->bos);
280 	list_add(&bo->mn_list, &node->bos);
281 
282 	interval_tree_insert(&node->it, &rmn->objects);
283 
284 	mutex_unlock(&rmn->lock);
285 
286 	return 0;
287 }
288 
289 /**
290  * radeon_mn_unregister - unregister a BO for notifier updates
291  *
292  * @bo: radeon buffer object
293  *
294  * Remove any registration of MMU notifier updates from the buffer object.
295  */
radeon_mn_unregister(struct radeon_bo * bo)296 void radeon_mn_unregister(struct radeon_bo *bo)
297 {
298 	struct radeon_device *rdev = bo->rdev;
299 	struct radeon_mn *rmn;
300 	struct list_head *head;
301 
302 	mutex_lock(&rdev->mn_lock);
303 	rmn = bo->mn;
304 	if (rmn == NULL) {
305 		mutex_unlock(&rdev->mn_lock);
306 		return;
307 	}
308 
309 	mutex_lock(&rmn->lock);
310 	/* save the next list entry for later */
311 	head = bo->mn_list.next;
312 
313 	bo->mn = NULL;
314 	list_del(&bo->mn_list);
315 
316 	if (list_empty(head)) {
317 		struct radeon_mn_node *node;
318 		node = container_of(head, struct radeon_mn_node, bos);
319 		interval_tree_remove(&node->it, &rmn->objects);
320 		kfree(node);
321 	}
322 
323 	mutex_unlock(&rmn->lock);
324 	mutex_unlock(&rdev->mn_lock);
325 }
326