Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 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> |
Christian König | a9f87f6 | 2017-03-30 14:03:59 +0200 | [diff] [blame] | 34 | #include <linux/interval_tree.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 35 | #include <drm/drmP.h> |
| 36 | #include <drm/drm.h> |
| 37 | |
| 38 | #include "amdgpu.h" |
| 39 | |
| 40 | struct amdgpu_mn { |
| 41 | /* constant after initialisation */ |
| 42 | struct amdgpu_device *adev; |
| 43 | struct mm_struct *mm; |
| 44 | struct mmu_notifier mn; |
| 45 | |
| 46 | /* only used on destruction */ |
| 47 | struct work_struct work; |
| 48 | |
| 49 | /* protected by adev->mn_lock */ |
| 50 | struct hlist_node node; |
| 51 | |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 52 | /* objects protected by lock */ |
| 53 | struct mutex lock; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 54 | struct rb_root objects; |
| 55 | }; |
| 56 | |
| 57 | struct amdgpu_mn_node { |
| 58 | struct interval_tree_node it; |
| 59 | struct list_head bos; |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * amdgpu_mn_destroy - destroy the rmn |
| 64 | * |
| 65 | * @work: previously sheduled work item |
| 66 | * |
| 67 | * Lazy destroys the notifier from a work item |
| 68 | */ |
| 69 | static void amdgpu_mn_destroy(struct work_struct *work) |
| 70 | { |
| 71 | struct amdgpu_mn *rmn = container_of(work, struct amdgpu_mn, work); |
| 72 | struct amdgpu_device *adev = rmn->adev; |
| 73 | struct amdgpu_mn_node *node, *next_node; |
| 74 | struct amdgpu_bo *bo, *next_bo; |
| 75 | |
| 76 | mutex_lock(&adev->mn_lock); |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 77 | mutex_lock(&rmn->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 78 | hash_del(&rmn->node); |
| 79 | rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects, |
| 80 | it.rb) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 81 | list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) { |
| 82 | bo->mn = NULL; |
| 83 | list_del_init(&bo->mn_list); |
| 84 | } |
| 85 | kfree(node); |
| 86 | } |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 87 | mutex_unlock(&rmn->lock); |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 88 | mutex_unlock(&adev->mn_lock); |
Felix Kuehling | fa5b500 | 2016-01-14 00:35:08 -0500 | [diff] [blame] | 89 | mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 90 | kfree(rmn); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * amdgpu_mn_release - callback to notify about mm destruction |
| 95 | * |
| 96 | * @mn: our notifier |
| 97 | * @mn: the mm this callback is about |
| 98 | * |
| 99 | * Shedule a work item to lazy destroy our notifier. |
| 100 | */ |
| 101 | static void amdgpu_mn_release(struct mmu_notifier *mn, |
| 102 | struct mm_struct *mm) |
| 103 | { |
| 104 | struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn); |
| 105 | INIT_WORK(&rmn->work, amdgpu_mn_destroy); |
| 106 | schedule_work(&rmn->work); |
| 107 | } |
| 108 | |
| 109 | /** |
Christian König | ae20f12 | 2016-03-18 19:29:52 +0100 | [diff] [blame] | 110 | * amdgpu_mn_invalidate_node - unmap all BOs of a node |
| 111 | * |
| 112 | * @node: the node with the BOs to unmap |
| 113 | * |
| 114 | * We block for all BOs and unmap them by move them |
| 115 | * into system domain again. |
| 116 | */ |
| 117 | static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node, |
| 118 | unsigned long start, |
| 119 | unsigned long end) |
| 120 | { |
| 121 | struct amdgpu_bo *bo; |
| 122 | long r; |
| 123 | |
| 124 | list_for_each_entry(bo, &node->bos, mn_list) { |
| 125 | |
| 126 | if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end)) |
| 127 | continue; |
| 128 | |
| 129 | r = amdgpu_bo_reserve(bo, true); |
| 130 | if (r) { |
| 131 | DRM_ERROR("(%ld) failed to reserve user bo\n", r); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | r = reservation_object_wait_timeout_rcu(bo->tbo.resv, |
| 136 | true, false, MAX_SCHEDULE_TIMEOUT); |
| 137 | if (r <= 0) |
| 138 | DRM_ERROR("(%ld) failed to wait for user bo\n", r); |
| 139 | |
Christian König | 1b0c0f9 | 2017-09-05 14:36:44 +0200 | [diff] [blame^] | 140 | amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm); |
Christian König | ae20f12 | 2016-03-18 19:29:52 +0100 | [diff] [blame] | 141 | |
| 142 | amdgpu_bo_unreserve(bo); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * amdgpu_mn_invalidate_page - callback to notify about mm change |
| 148 | * |
| 149 | * @mn: our notifier |
| 150 | * @mn: the mm this callback is about |
| 151 | * @address: address of invalidate page |
| 152 | * |
| 153 | * Invalidation of a single page. Blocks for all BOs mapping it |
| 154 | * and unmap them by move them into system domain again. |
| 155 | */ |
| 156 | static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn, |
| 157 | struct mm_struct *mm, |
| 158 | unsigned long address) |
| 159 | { |
| 160 | struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn); |
| 161 | struct interval_tree_node *it; |
| 162 | |
| 163 | mutex_lock(&rmn->lock); |
| 164 | |
| 165 | it = interval_tree_iter_first(&rmn->objects, address, address); |
| 166 | if (it) { |
| 167 | struct amdgpu_mn_node *node; |
| 168 | |
| 169 | node = container_of(it, struct amdgpu_mn_node, it); |
| 170 | amdgpu_mn_invalidate_node(node, address, address); |
| 171 | } |
| 172 | |
| 173 | mutex_unlock(&rmn->lock); |
| 174 | } |
| 175 | |
| 176 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 177 | * amdgpu_mn_invalidate_range_start - callback to notify about mm change |
| 178 | * |
| 179 | * @mn: our notifier |
| 180 | * @mn: the mm this callback is about |
| 181 | * @start: start of updated range |
| 182 | * @end: end of updated range |
| 183 | * |
| 184 | * We block for all BOs between start and end to be idle and |
| 185 | * unmap them by move them into system domain again. |
| 186 | */ |
| 187 | static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn, |
| 188 | struct mm_struct *mm, |
| 189 | unsigned long start, |
| 190 | unsigned long end) |
| 191 | { |
| 192 | struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn); |
| 193 | struct interval_tree_node *it; |
| 194 | |
| 195 | /* notification is exclusive, but interval is inclusive */ |
| 196 | end -= 1; |
| 197 | |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 198 | mutex_lock(&rmn->lock); |
| 199 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 200 | it = interval_tree_iter_first(&rmn->objects, start, end); |
| 201 | while (it) { |
| 202 | struct amdgpu_mn_node *node; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 203 | |
| 204 | node = container_of(it, struct amdgpu_mn_node, it); |
| 205 | it = interval_tree_iter_next(it, start, end); |
| 206 | |
Christian König | ae20f12 | 2016-03-18 19:29:52 +0100 | [diff] [blame] | 207 | amdgpu_mn_invalidate_node(node, start, end); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 208 | } |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 209 | |
| 210 | mutex_unlock(&rmn->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static const struct mmu_notifier_ops amdgpu_mn_ops = { |
| 214 | .release = amdgpu_mn_release, |
Christian König | ae20f12 | 2016-03-18 19:29:52 +0100 | [diff] [blame] | 215 | .invalidate_page = amdgpu_mn_invalidate_page, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 216 | .invalidate_range_start = amdgpu_mn_invalidate_range_start, |
| 217 | }; |
| 218 | |
| 219 | /** |
| 220 | * amdgpu_mn_get - create notifier context |
| 221 | * |
| 222 | * @adev: amdgpu device pointer |
| 223 | * |
| 224 | * Creates a notifier context for current->mm. |
| 225 | */ |
| 226 | static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) |
| 227 | { |
| 228 | struct mm_struct *mm = current->mm; |
| 229 | struct amdgpu_mn *rmn; |
| 230 | int r; |
| 231 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 232 | mutex_lock(&adev->mn_lock); |
Michal Hocko | b563705 | 2016-05-23 16:26:17 -0700 | [diff] [blame] | 233 | if (down_write_killable(&mm->mmap_sem)) { |
| 234 | mutex_unlock(&adev->mn_lock); |
| 235 | return ERR_PTR(-EINTR); |
| 236 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 237 | |
| 238 | hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm) |
| 239 | if (rmn->mm == mm) |
| 240 | goto release_locks; |
| 241 | |
| 242 | rmn = kzalloc(sizeof(*rmn), GFP_KERNEL); |
| 243 | if (!rmn) { |
| 244 | rmn = ERR_PTR(-ENOMEM); |
| 245 | goto release_locks; |
| 246 | } |
| 247 | |
| 248 | rmn->adev = adev; |
| 249 | rmn->mm = mm; |
| 250 | rmn->mn.ops = &amdgpu_mn_ops; |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 251 | mutex_init(&rmn->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 252 | rmn->objects = RB_ROOT; |
| 253 | |
| 254 | r = __mmu_notifier_register(&rmn->mn, mm); |
| 255 | if (r) |
| 256 | goto free_rmn; |
| 257 | |
| 258 | hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm); |
| 259 | |
| 260 | release_locks: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 261 | up_write(&mm->mmap_sem); |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 262 | mutex_unlock(&adev->mn_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 263 | |
| 264 | return rmn; |
| 265 | |
| 266 | free_rmn: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 267 | up_write(&mm->mmap_sem); |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 268 | mutex_unlock(&adev->mn_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 269 | kfree(rmn); |
| 270 | |
| 271 | return ERR_PTR(r); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * amdgpu_mn_register - register a BO for notifier updates |
| 276 | * |
| 277 | * @bo: amdgpu buffer object |
| 278 | * @addr: userptr addr we should monitor |
| 279 | * |
| 280 | * Registers an MMU notifier for the given BO at the specified address. |
| 281 | * Returns 0 on success, -ERRNO if anything goes wrong. |
| 282 | */ |
| 283 | int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr) |
| 284 | { |
| 285 | unsigned long end = addr + amdgpu_bo_size(bo) - 1; |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 286 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 287 | struct amdgpu_mn *rmn; |
| 288 | struct amdgpu_mn_node *node = NULL; |
| 289 | struct list_head bos; |
| 290 | struct interval_tree_node *it; |
| 291 | |
| 292 | rmn = amdgpu_mn_get(adev); |
| 293 | if (IS_ERR(rmn)) |
| 294 | return PTR_ERR(rmn); |
| 295 | |
| 296 | INIT_LIST_HEAD(&bos); |
| 297 | |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 298 | mutex_lock(&rmn->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 299 | |
| 300 | while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) { |
| 301 | kfree(node); |
| 302 | node = container_of(it, struct amdgpu_mn_node, it); |
| 303 | interval_tree_remove(&node->it, &rmn->objects); |
| 304 | addr = min(it->start, addr); |
| 305 | end = max(it->last, end); |
| 306 | list_splice(&node->bos, &bos); |
| 307 | } |
| 308 | |
| 309 | if (!node) { |
| 310 | node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL); |
| 311 | if (!node) { |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 312 | mutex_unlock(&rmn->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 313 | return -ENOMEM; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | bo->mn = rmn; |
| 318 | |
| 319 | node->it.start = addr; |
| 320 | node->it.last = end; |
| 321 | INIT_LIST_HEAD(&node->bos); |
| 322 | list_splice(&bos, &node->bos); |
| 323 | list_add(&bo->mn_list, &node->bos); |
| 324 | |
| 325 | interval_tree_insert(&node->it, &rmn->objects); |
| 326 | |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 327 | mutex_unlock(&rmn->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * amdgpu_mn_unregister - unregister a BO for notifier updates |
| 334 | * |
| 335 | * @bo: amdgpu buffer object |
| 336 | * |
| 337 | * Remove any registration of MMU notifier updates from the buffer object. |
| 338 | */ |
| 339 | void amdgpu_mn_unregister(struct amdgpu_bo *bo) |
| 340 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 341 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 342 | struct amdgpu_mn *rmn; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 343 | struct list_head *head; |
| 344 | |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 345 | mutex_lock(&adev->mn_lock); |
| 346 | |
| 347 | rmn = bo->mn; |
| 348 | if (rmn == NULL) { |
| 349 | mutex_unlock(&adev->mn_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 350 | return; |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 351 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 352 | |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 353 | mutex_lock(&rmn->lock); |
Christian König | c41d271 | 2016-02-09 16:13:37 +0100 | [diff] [blame] | 354 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 355 | /* save the next list entry for later */ |
| 356 | head = bo->mn_list.next; |
| 357 | |
| 358 | bo->mn = NULL; |
Felix Kuehling | 68c9793d | 2017-08-01 22:34:55 -0400 | [diff] [blame] | 359 | list_del_init(&bo->mn_list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 360 | |
| 361 | if (list_empty(head)) { |
| 362 | struct amdgpu_mn_node *node; |
| 363 | node = container_of(head, struct amdgpu_mn_node, bos); |
| 364 | interval_tree_remove(&node->it, &rmn->objects); |
| 365 | kfree(node); |
| 366 | } |
| 367 | |
Christian König | 0d2b42b | 2016-03-18 19:29:51 +0100 | [diff] [blame] | 368 | mutex_unlock(&rmn->lock); |
Felix Kuehling | b8ea378 | 2016-02-16 15:29:23 -0500 | [diff] [blame] | 369 | mutex_unlock(&adev->mn_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 370 | } |