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
34 * For coherent userptr handling registers an MMU notifier to inform the driver
35 * about updates on the page tables of a process.
37 * When somebody tries to invalidate the page tables we block the update until
38 * all operations on the pages in question are completed, then those pages are
39 * marked as accessed and also dirty if it wasn't a read only access.
41 * New command submissions using the userptrs in question are delayed until all
42 * page table invalidation are completed and we once more see a coherent process
46 #include <linux/firmware.h>
47 #include <linux/module.h>
51 #include "amdgpu_amdkfd.h"
52 #include "amdgpu_hmm.h"
54 #define MAX_WALK_BYTE (2UL << 30)
57 * amdgpu_hmm_invalidate_gfx - callback to notify about mm change
59 * @mni: the range (mm) is about to update
60 * @range: details on the invalidation
61 * @cur_seq: Value to pass to mmu_interval_set_seq()
63 * Block for operations on BOs to finish and mark pages as accessed and
66 static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni,
67 const struct mmu_notifier_range *range,
68 unsigned long cur_seq)
70 struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
71 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
74 if (!mmu_notifier_range_blockable(range))
77 mutex_lock(&adev->notifier_lock);
79 mmu_interval_set_seq(mni, cur_seq);
81 r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
82 false, MAX_SCHEDULE_TIMEOUT);
83 mutex_unlock(&adev->notifier_lock);
85 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
89 static const struct mmu_interval_notifier_ops amdgpu_hmm_gfx_ops = {
90 .invalidate = amdgpu_hmm_invalidate_gfx,
94 * amdgpu_hmm_invalidate_hsa - callback to notify about mm change
96 * @mni: the range (mm) is about to update
97 * @range: details on the invalidation
98 * @cur_seq: Value to pass to mmu_interval_set_seq()
100 * We temporarily evict the BO attached to this range. This necessitates
101 * evicting all user-mode queues of the process.
103 static bool amdgpu_hmm_invalidate_hsa(struct mmu_interval_notifier *mni,
104 const struct mmu_notifier_range *range,
105 unsigned long cur_seq)
107 struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
109 if (!mmu_notifier_range_blockable(range))
112 amdgpu_amdkfd_evict_userptr(mni, cur_seq, bo->kfd_bo);
117 static const struct mmu_interval_notifier_ops amdgpu_hmm_hsa_ops = {
118 .invalidate = amdgpu_hmm_invalidate_hsa,
122 * amdgpu_hmm_register - register a BO for notifier updates
124 * @bo: amdgpu buffer object
125 * @addr: userptr addr we should monitor
127 * Registers a mmu_notifier for the given BO at the specified address.
128 * Returns 0 on success, -ERRNO if anything goes wrong.
130 int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)
135 r = mmu_interval_notifier_insert(&bo->notifier, current->mm,
136 addr, amdgpu_bo_size(bo),
137 &amdgpu_hmm_hsa_ops);
139 r = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
141 &amdgpu_hmm_gfx_ops);
144 * Make sure amdgpu_hmm_unregister() doesn't call
145 * mmu_interval_notifier_remove() when the notifier isn't properly
148 bo->notifier.mm = NULL;
154 * amdgpu_hmm_unregister - unregister a BO for notifier updates
156 * @bo: amdgpu buffer object
158 * Remove any registration of mmu notifier updates from the buffer object.
160 void amdgpu_hmm_unregister(struct amdgpu_bo *bo)
162 if (!bo->notifier.mm)
164 mmu_interval_notifier_remove(&bo->notifier);
165 bo->notifier.mm = NULL;
168 int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
169 uint64_t start, uint64_t npages, bool readonly,
170 void *owner, struct page **pages,
171 struct hmm_range **phmm_range)
173 struct hmm_range *hmm_range;
175 unsigned long timeout;
180 hmm_range = kzalloc(sizeof(*hmm_range), GFP_KERNEL);
181 if (unlikely(!hmm_range))
184 pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
185 if (unlikely(!pfns)) {
190 hmm_range->notifier = notifier;
191 hmm_range->default_flags = HMM_PFN_REQ_FAULT;
193 hmm_range->default_flags |= HMM_PFN_REQ_WRITE;
194 hmm_range->hmm_pfns = pfns;
195 hmm_range->start = start;
196 end = start + npages * PAGE_SIZE;
197 hmm_range->dev_private_owner = owner;
200 hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
202 pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
203 hmm_range->start, hmm_range->end);
205 timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
208 hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
209 r = hmm_range_fault(hmm_range);
211 if (r == -EBUSY && !time_after(jiffies, timeout))
216 if (hmm_range->end == end)
218 hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
219 hmm_range->start = hmm_range->end;
220 } while (hmm_range->end < end);
222 hmm_range->start = start;
223 hmm_range->hmm_pfns = pfns;
226 * Due to default_flags, all pages are HMM_PFN_VALID or
227 * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
228 * the notifier_lock, and mmu_interval_read_retry() must be done first.
230 for (i = 0; pages && i < npages; i++)
231 pages[i] = hmm_pfn_to_page(pfns[i]);
233 *phmm_range = hmm_range;
247 bool amdgpu_hmm_range_get_pages_done(struct hmm_range *hmm_range)
251 r = mmu_interval_read_retry(hmm_range->notifier,
252 hmm_range->notifier_seq);
253 kvfree(hmm_range->hmm_pfns);