2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Christian König <christian.koenig@amd.com>
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/mmu_notifier.h>
40 * radeon_mn_invalidate - callback to notify about mm change
43 * @range: the VMA under invalidation
45 * We block for all BOs between start and end to be idle and
46 * unmap them by move them into system domain again.
48 static bool radeon_mn_invalidate(struct mmu_interval_notifier *mn,
49 const struct mmu_notifier_range *range,
50 unsigned long cur_seq)
52 struct radeon_bo *bo = container_of(mn, struct radeon_bo, notifier);
53 struct ttm_operation_ctx ctx = { false, false };
56 if (!bo->tbo.ttm || !radeon_ttm_tt_is_bound(bo->tbo.bdev, bo->tbo.ttm))
59 if (!mmu_notifier_range_blockable(range))
62 r = radeon_bo_reserve(bo, true);
64 DRM_ERROR("(%ld) failed to reserve user bo\n", r);
68 r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv, true, false,
69 MAX_SCHEDULE_TIMEOUT);
71 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
73 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
74 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
76 DRM_ERROR("(%ld) failed to validate user bo\n", r);
78 radeon_bo_unreserve(bo);
82 static const struct mmu_interval_notifier_ops radeon_mn_ops = {
83 .invalidate = radeon_mn_invalidate,
87 * radeon_mn_register - register a BO for notifier updates
89 * @bo: radeon buffer object
90 * @addr: userptr addr we should monitor
92 * Registers an MMU notifier for the given BO at the specified address.
93 * Returns 0 on success, -ERRNO if anything goes wrong.
95 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
99 ret = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
100 radeon_bo_size(bo), &radeon_mn_ops);
105 * FIXME: radeon appears to allow get_user_pages to run during
106 * invalidate_range_start/end, which is not a safe way to read the
107 * PTEs. It should use the mmu_interval_read_begin() scheme around the
108 * get_user_pages to ensure that the PTEs are read properly
110 mmu_interval_read_begin(&bo->notifier);
115 * radeon_mn_unregister - unregister a BO for notifier updates
117 * @bo: radeon buffer object
119 * Remove any registration of MMU notifier updates from the buffer object.
121 void radeon_mn_unregister(struct radeon_bo *bo)
123 if (!bo->notifier.mm)
125 mmu_interval_notifier_remove(&bo->notifier);
126 bo->notifier.mm = NULL;