blob: c47f22224a650c8a24b50c0b375813d92bfa2bb0 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 "amdgpu.h"
38
39struct amdgpu_mn {
40 /* constant after initialisation */
41 struct amdgpu_device *adev;
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 adev->mn_lock */
49 struct hlist_node node;
50
Christian König0d2b42b2016-03-18 19:29:51 +010051 /* objects protected by lock */
52 struct mutex lock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053 struct rb_root objects;
54};
55
56struct amdgpu_mn_node {
57 struct interval_tree_node it;
58 struct list_head bos;
59};
60
61/**
62 * amdgpu_mn_destroy - destroy the rmn
63 *
64 * @work: previously sheduled work item
65 *
66 * Lazy destroys the notifier from a work item
67 */
68static void amdgpu_mn_destroy(struct work_struct *work)
69{
70 struct amdgpu_mn *rmn = container_of(work, struct amdgpu_mn, work);
71 struct amdgpu_device *adev = rmn->adev;
72 struct amdgpu_mn_node *node, *next_node;
73 struct amdgpu_bo *bo, *next_bo;
74
75 mutex_lock(&adev->mn_lock);
Christian König0d2b42b2016-03-18 19:29:51 +010076 mutex_lock(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 hash_del(&rmn->node);
78 rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
79 it.rb) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080 list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
81 bo->mn = NULL;
82 list_del_init(&bo->mn_list);
83 }
84 kfree(node);
85 }
Christian König0d2b42b2016-03-18 19:29:51 +010086 mutex_unlock(&rmn->lock);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -050087 mutex_unlock(&adev->mn_lock);
Felix Kuehlingfa5b5002016-01-14 00:35:08 -050088 mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 kfree(rmn);
90}
91
92/**
93 * amdgpu_mn_release - callback to notify about mm destruction
94 *
95 * @mn: our notifier
96 * @mn: the mm this callback is about
97 *
98 * Shedule a work item to lazy destroy our notifier.
99 */
100static void amdgpu_mn_release(struct mmu_notifier *mn,
101 struct mm_struct *mm)
102{
103 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
104 INIT_WORK(&rmn->work, amdgpu_mn_destroy);
105 schedule_work(&rmn->work);
106}
107
108/**
109 * amdgpu_mn_invalidate_range_start - callback to notify about mm change
110 *
111 * @mn: our notifier
112 * @mn: the mm this callback is about
113 * @start: start of updated range
114 * @end: end of updated range
115 *
116 * We block for all BOs between start and end to be idle and
117 * unmap them by move them into system domain again.
118 */
119static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
120 struct mm_struct *mm,
121 unsigned long start,
122 unsigned long end)
123{
124 struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
125 struct interval_tree_node *it;
126
127 /* notification is exclusive, but interval is inclusive */
128 end -= 1;
129
Christian König0d2b42b2016-03-18 19:29:51 +0100130 mutex_lock(&rmn->lock);
131
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132 it = interval_tree_iter_first(&rmn->objects, start, end);
133 while (it) {
134 struct amdgpu_mn_node *node;
135 struct amdgpu_bo *bo;
Jack Xiao7ab7e8a2015-04-27 13:45:40 +0800136 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137
138 node = container_of(it, struct amdgpu_mn_node, it);
139 it = interval_tree_iter_next(it, start, end);
140
141 list_for_each_entry(bo, &node->bos, mn_list) {
142
Christian Königd7006962016-02-08 10:57:22 +0100143 if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start,
144 end))
Christian Königa961ea7342015-05-04 13:20:36 +0200145 continue;
146
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 r = amdgpu_bo_reserve(bo, true);
148 if (r) {
Jack Xiao7ab7e8a2015-04-27 13:45:40 +0800149 DRM_ERROR("(%ld) failed to reserve user bo\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150 continue;
151 }
152
153 r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
154 true, false, MAX_SCHEDULE_TIMEOUT);
Jack Xiao7ab7e8a2015-04-27 13:45:40 +0800155 if (r <= 0)
156 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157
158 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
159 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
160 if (r)
Jack Xiao7ab7e8a2015-04-27 13:45:40 +0800161 DRM_ERROR("(%ld) failed to validate user bo\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162
163 amdgpu_bo_unreserve(bo);
164 }
165 }
Christian König0d2b42b2016-03-18 19:29:51 +0100166
167 mutex_unlock(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168}
169
170static const struct mmu_notifier_ops amdgpu_mn_ops = {
171 .release = amdgpu_mn_release,
172 .invalidate_range_start = amdgpu_mn_invalidate_range_start,
173};
174
175/**
176 * amdgpu_mn_get - create notifier context
177 *
178 * @adev: amdgpu device pointer
179 *
180 * Creates a notifier context for current->mm.
181 */
182static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
183{
184 struct mm_struct *mm = current->mm;
185 struct amdgpu_mn *rmn;
186 int r;
187
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188 mutex_lock(&adev->mn_lock);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500189 down_write(&mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190
191 hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm)
192 if (rmn->mm == mm)
193 goto release_locks;
194
195 rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
196 if (!rmn) {
197 rmn = ERR_PTR(-ENOMEM);
198 goto release_locks;
199 }
200
201 rmn->adev = adev;
202 rmn->mm = mm;
203 rmn->mn.ops = &amdgpu_mn_ops;
Christian König0d2b42b2016-03-18 19:29:51 +0100204 mutex_init(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205 rmn->objects = RB_ROOT;
206
207 r = __mmu_notifier_register(&rmn->mn, mm);
208 if (r)
209 goto free_rmn;
210
211 hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm);
212
213release_locks:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214 up_write(&mm->mmap_sem);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500215 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216
217 return rmn;
218
219free_rmn:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220 up_write(&mm->mmap_sem);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500221 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400222 kfree(rmn);
223
224 return ERR_PTR(r);
225}
226
227/**
228 * amdgpu_mn_register - register a BO for notifier updates
229 *
230 * @bo: amdgpu buffer object
231 * @addr: userptr addr we should monitor
232 *
233 * Registers an MMU notifier for the given BO at the specified address.
234 * Returns 0 on success, -ERRNO if anything goes wrong.
235 */
236int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
237{
238 unsigned long end = addr + amdgpu_bo_size(bo) - 1;
239 struct amdgpu_device *adev = bo->adev;
240 struct amdgpu_mn *rmn;
241 struct amdgpu_mn_node *node = NULL;
242 struct list_head bos;
243 struct interval_tree_node *it;
244
245 rmn = amdgpu_mn_get(adev);
246 if (IS_ERR(rmn))
247 return PTR_ERR(rmn);
248
249 INIT_LIST_HEAD(&bos);
250
Christian König0d2b42b2016-03-18 19:29:51 +0100251 mutex_lock(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252
253 while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
254 kfree(node);
255 node = container_of(it, struct amdgpu_mn_node, it);
256 interval_tree_remove(&node->it, &rmn->objects);
257 addr = min(it->start, addr);
258 end = max(it->last, end);
259 list_splice(&node->bos, &bos);
260 }
261
262 if (!node) {
263 node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
264 if (!node) {
Christian König0d2b42b2016-03-18 19:29:51 +0100265 mutex_unlock(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 return -ENOMEM;
267 }
268 }
269
270 bo->mn = rmn;
271
272 node->it.start = addr;
273 node->it.last = end;
274 INIT_LIST_HEAD(&node->bos);
275 list_splice(&bos, &node->bos);
276 list_add(&bo->mn_list, &node->bos);
277
278 interval_tree_insert(&node->it, &rmn->objects);
279
Christian König0d2b42b2016-03-18 19:29:51 +0100280 mutex_unlock(&rmn->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281
282 return 0;
283}
284
285/**
286 * amdgpu_mn_unregister - unregister a BO for notifier updates
287 *
288 * @bo: amdgpu buffer object
289 *
290 * Remove any registration of MMU notifier updates from the buffer object.
291 */
292void amdgpu_mn_unregister(struct amdgpu_bo *bo)
293{
294 struct amdgpu_device *adev = bo->adev;
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500295 struct amdgpu_mn *rmn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 struct list_head *head;
297
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500298 mutex_lock(&adev->mn_lock);
299
300 rmn = bo->mn;
301 if (rmn == NULL) {
302 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 return;
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500304 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305
Christian König0d2b42b2016-03-18 19:29:51 +0100306 mutex_lock(&rmn->lock);
Christian Königc41d2712016-02-09 16:13:37 +0100307
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308 /* save the next list entry for later */
309 head = bo->mn_list.next;
310
311 bo->mn = NULL;
312 list_del(&bo->mn_list);
313
314 if (list_empty(head)) {
315 struct amdgpu_mn_node *node;
316 node = container_of(head, struct amdgpu_mn_node, bos);
317 interval_tree_remove(&node->it, &rmn->objects);
318 kfree(node);
319 }
320
Christian König0d2b42b2016-03-18 19:29:51 +0100321 mutex_unlock(&rmn->lock);
Felix Kuehlingb8ea3782016-02-16 15:29:23 -0500322 mutex_unlock(&adev->mn_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400323}