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