Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / radeon / radeon_mn.c
blobe37c9a57a7c36f9f1157274fde77d9ef10121eb8
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
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
23 * of the Software.
27 * Authors:
28 * Christian König <christian.koenig@amd.com>
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/mmu_notifier.h>
35 #include <drm/drm.h>
37 #include "radeon.h"
39 /**
40 * radeon_mn_invalidate - callback to notify about mm change
42 * @mn: our notifier
43 * @range: the VMA under invalidation
44 * @cur_seq: Value to pass to mmu_interval_set_seq()
46 * We block for all BOs between start and end to be idle and
47 * unmap them by move them into system domain again.
49 static bool radeon_mn_invalidate(struct mmu_interval_notifier *mn,
50 const struct mmu_notifier_range *range,
51 unsigned long cur_seq)
53 struct radeon_bo *bo = container_of(mn, struct radeon_bo, notifier);
54 struct ttm_operation_ctx ctx = { false, false };
55 long r;
57 if (!bo->tbo.ttm || !radeon_ttm_tt_is_bound(bo->tbo.bdev, bo->tbo.ttm))
58 return true;
60 if (!mmu_notifier_range_blockable(range))
61 return false;
63 r = radeon_bo_reserve(bo, true);
64 if (r) {
65 DRM_ERROR("(%ld) failed to reserve user bo\n", r);
66 return true;
69 r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv, true, false,
70 MAX_SCHEDULE_TIMEOUT);
71 if (r <= 0)
72 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
74 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
75 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
76 if (r)
77 DRM_ERROR("(%ld) failed to validate user bo\n", r);
79 radeon_bo_unreserve(bo);
80 return true;
83 static const struct mmu_interval_notifier_ops radeon_mn_ops = {
84 .invalidate = radeon_mn_invalidate,
87 /**
88 * radeon_mn_register - register a BO for notifier updates
90 * @bo: radeon buffer object
91 * @addr: userptr addr we should monitor
93 * Registers an MMU notifier for the given BO at the specified address.
94 * Returns 0 on success, -ERRNO if anything goes wrong.
96 int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
98 int ret;
100 ret = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
101 radeon_bo_size(bo), &radeon_mn_ops);
102 if (ret)
103 return ret;
106 * FIXME: radeon appears to allow get_user_pages to run during
107 * invalidate_range_start/end, which is not a safe way to read the
108 * PTEs. It should use the mmu_interval_read_begin() scheme around the
109 * get_user_pages to ensure that the PTEs are read properly
111 mmu_interval_read_begin(&bo->notifier);
112 return 0;
116 * radeon_mn_unregister - unregister a BO for notifier updates
118 * @bo: radeon buffer object
120 * Remove any registration of MMU notifier updates from the buffer object.
122 void radeon_mn_unregister(struct radeon_bo *bo)
124 if (!bo->notifier.mm)
125 return;
126 mmu_interval_notifier_remove(&bo->notifier);
127 bo->notifier.mm = NULL;