xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_hmm.c (revision efcc4ebceba0f2884a238f9549916e08c0162abc)
1f005ef32Sjsg /*
2f005ef32Sjsg  * Copyright 2014 Advanced Micro Devices, Inc.
3f005ef32Sjsg  * All Rights Reserved.
4f005ef32Sjsg  *
5f005ef32Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
6f005ef32Sjsg  * copy of this software and associated documentation files (the
7f005ef32Sjsg  * "Software"), to deal in the Software without restriction, including
8f005ef32Sjsg  * without limitation the rights to use, copy, modify, merge, publish,
9f005ef32Sjsg  * distribute, sub license, and/or sell copies of the Software, and to
10f005ef32Sjsg  * permit persons to whom the Software is furnished to do so, subject to
11f005ef32Sjsg  * the following conditions:
12f005ef32Sjsg  *
13f005ef32Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14f005ef32Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15f005ef32Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16f005ef32Sjsg  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17f005ef32Sjsg  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18f005ef32Sjsg  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19f005ef32Sjsg  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20f005ef32Sjsg  *
21f005ef32Sjsg  * The above copyright notice and this permission notice (including the
22f005ef32Sjsg  * next paragraph) shall be included in all copies or substantial portions
23f005ef32Sjsg  * of the Software.
24f005ef32Sjsg  *
25f005ef32Sjsg  */
26f005ef32Sjsg /*
27f005ef32Sjsg  * Authors:
28f005ef32Sjsg  *    Christian König <christian.koenig@amd.com>
29f005ef32Sjsg  */
30f005ef32Sjsg 
31f005ef32Sjsg /**
32f005ef32Sjsg  * DOC: MMU Notifier
33f005ef32Sjsg  *
34f005ef32Sjsg  * For coherent userptr handling registers an MMU notifier to inform the driver
35f005ef32Sjsg  * about updates on the page tables of a process.
36f005ef32Sjsg  *
37f005ef32Sjsg  * When somebody tries to invalidate the page tables we block the update until
38f005ef32Sjsg  * all operations on the pages in question are completed, then those pages are
39f005ef32Sjsg  * marked as accessed and also dirty if it wasn't a read only access.
40f005ef32Sjsg  *
41f005ef32Sjsg  * New command submissions using the userptrs in question are delayed until all
42f005ef32Sjsg  * page table invalidation are completed and we once more see a coherent process
43f005ef32Sjsg  * address space.
44f005ef32Sjsg  */
45f005ef32Sjsg 
46f005ef32Sjsg #include <linux/firmware.h>
47f005ef32Sjsg #include <linux/module.h>
48f005ef32Sjsg #include <drm/drm.h>
49f005ef32Sjsg 
50f005ef32Sjsg #include "amdgpu.h"
51f005ef32Sjsg #include "amdgpu_amdkfd.h"
52f005ef32Sjsg #include "amdgpu_hmm.h"
53f005ef32Sjsg 
54f005ef32Sjsg #define MAX_WALK_BYTE	(2UL << 30)
55f005ef32Sjsg 
56f005ef32Sjsg /**
57f005ef32Sjsg  * amdgpu_hmm_invalidate_gfx - callback to notify about mm change
58f005ef32Sjsg  *
59f005ef32Sjsg  * @mni: the range (mm) is about to update
60f005ef32Sjsg  * @range: details on the invalidation
61f005ef32Sjsg  * @cur_seq: Value to pass to mmu_interval_set_seq()
62f005ef32Sjsg  *
63f005ef32Sjsg  * Block for operations on BOs to finish and mark pages as accessed and
64f005ef32Sjsg  * potentially dirty.
65f005ef32Sjsg  */
amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier * mni,const struct mmu_notifier_range * range,unsigned long cur_seq)66f005ef32Sjsg static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni,
67f005ef32Sjsg 				      const struct mmu_notifier_range *range,
68f005ef32Sjsg 				      unsigned long cur_seq)
69f005ef32Sjsg {
70f005ef32Sjsg 	struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
71f005ef32Sjsg 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
72f005ef32Sjsg 	long r;
73f005ef32Sjsg 
74f005ef32Sjsg 	if (!mmu_notifier_range_blockable(range))
75f005ef32Sjsg 		return false;
76f005ef32Sjsg 
77f005ef32Sjsg 	mutex_lock(&adev->notifier_lock);
78f005ef32Sjsg 
79f005ef32Sjsg 	mmu_interval_set_seq(mni, cur_seq);
80f005ef32Sjsg 
81f005ef32Sjsg 	r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
82f005ef32Sjsg 				  false, MAX_SCHEDULE_TIMEOUT);
83f005ef32Sjsg 	mutex_unlock(&adev->notifier_lock);
84f005ef32Sjsg 	if (r <= 0)
85f005ef32Sjsg 		DRM_ERROR("(%ld) failed to wait for user bo\n", r);
86f005ef32Sjsg 	return true;
87f005ef32Sjsg }
88f005ef32Sjsg 
89f005ef32Sjsg static const struct mmu_interval_notifier_ops amdgpu_hmm_gfx_ops = {
90f005ef32Sjsg 	.invalidate = amdgpu_hmm_invalidate_gfx,
91f005ef32Sjsg };
92f005ef32Sjsg 
93f005ef32Sjsg /**
94f005ef32Sjsg  * amdgpu_hmm_invalidate_hsa - callback to notify about mm change
95f005ef32Sjsg  *
96f005ef32Sjsg  * @mni: the range (mm) is about to update
97f005ef32Sjsg  * @range: details on the invalidation
98f005ef32Sjsg  * @cur_seq: Value to pass to mmu_interval_set_seq()
99f005ef32Sjsg  *
100f005ef32Sjsg  * We temporarily evict the BO attached to this range. This necessitates
101f005ef32Sjsg  * evicting all user-mode queues of the process.
102f005ef32Sjsg  */
amdgpu_hmm_invalidate_hsa(struct mmu_interval_notifier * mni,const struct mmu_notifier_range * range,unsigned long cur_seq)103f005ef32Sjsg static bool amdgpu_hmm_invalidate_hsa(struct mmu_interval_notifier *mni,
104f005ef32Sjsg 				      const struct mmu_notifier_range *range,
105f005ef32Sjsg 				      unsigned long cur_seq)
106f005ef32Sjsg {
107f005ef32Sjsg 	struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
108f005ef32Sjsg 
109f005ef32Sjsg 	if (!mmu_notifier_range_blockable(range))
110f005ef32Sjsg 		return false;
111f005ef32Sjsg 
112f005ef32Sjsg 	amdgpu_amdkfd_evict_userptr(mni, cur_seq, bo->kfd_bo);
113f005ef32Sjsg 
114f005ef32Sjsg 	return true;
115f005ef32Sjsg }
116f005ef32Sjsg 
117f005ef32Sjsg static const struct mmu_interval_notifier_ops amdgpu_hmm_hsa_ops = {
118f005ef32Sjsg 	.invalidate = amdgpu_hmm_invalidate_hsa,
119f005ef32Sjsg };
120f005ef32Sjsg 
121f005ef32Sjsg /**
122f005ef32Sjsg  * amdgpu_hmm_register - register a BO for notifier updates
123f005ef32Sjsg  *
124f005ef32Sjsg  * @bo: amdgpu buffer object
125f005ef32Sjsg  * @addr: userptr addr we should monitor
126f005ef32Sjsg  *
127f005ef32Sjsg  * Registers a mmu_notifier for the given BO at the specified address.
128f005ef32Sjsg  * Returns 0 on success, -ERRNO if anything goes wrong.
129f005ef32Sjsg  */
amdgpu_hmm_register(struct amdgpu_bo * bo,unsigned long addr)130f005ef32Sjsg int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)
131f005ef32Sjsg {
132*efcc4ebcSjsg 	int r;
133*efcc4ebcSjsg 
134f005ef32Sjsg 	if (bo->kfd_bo)
135*efcc4ebcSjsg 		r = mmu_interval_notifier_insert(&bo->notifier, current->mm,
136f005ef32Sjsg 						    addr, amdgpu_bo_size(bo),
137f005ef32Sjsg 						    &amdgpu_hmm_hsa_ops);
138*efcc4ebcSjsg 	else
139*efcc4ebcSjsg 		r = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
140f005ef32Sjsg 							amdgpu_bo_size(bo),
141f005ef32Sjsg 							&amdgpu_hmm_gfx_ops);
142*efcc4ebcSjsg 	if (r)
143*efcc4ebcSjsg 		/*
144*efcc4ebcSjsg 		 * Make sure amdgpu_hmm_unregister() doesn't call
145*efcc4ebcSjsg 		 * mmu_interval_notifier_remove() when the notifier isn't properly
146*efcc4ebcSjsg 		 * initialized.
147*efcc4ebcSjsg 		 */
148*efcc4ebcSjsg 		bo->notifier.mm = NULL;
149*efcc4ebcSjsg 
150*efcc4ebcSjsg 	return r;
151f005ef32Sjsg }
152f005ef32Sjsg 
153f005ef32Sjsg /**
154f005ef32Sjsg  * amdgpu_hmm_unregister - unregister a BO for notifier updates
155f005ef32Sjsg  *
156f005ef32Sjsg  * @bo: amdgpu buffer object
157f005ef32Sjsg  *
158f005ef32Sjsg  * Remove any registration of mmu notifier updates from the buffer object.
159f005ef32Sjsg  */
amdgpu_hmm_unregister(struct amdgpu_bo * bo)160f005ef32Sjsg void amdgpu_hmm_unregister(struct amdgpu_bo *bo)
161f005ef32Sjsg {
162f005ef32Sjsg 	if (!bo->notifier.mm)
163f005ef32Sjsg 		return;
164f005ef32Sjsg 	mmu_interval_notifier_remove(&bo->notifier);
165f005ef32Sjsg 	bo->notifier.mm = NULL;
166f005ef32Sjsg }
167f005ef32Sjsg 
amdgpu_hmm_range_get_pages(struct mmu_interval_notifier * notifier,uint64_t start,uint64_t npages,bool readonly,void * owner,struct vm_page ** pages,struct hmm_range ** phmm_range)168f005ef32Sjsg int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
169f005ef32Sjsg 			       uint64_t start, uint64_t npages, bool readonly,
170f005ef32Sjsg 			       void *owner, struct vm_page **pages,
171f005ef32Sjsg 			       struct hmm_range **phmm_range)
172f005ef32Sjsg {
173f005ef32Sjsg 	struct hmm_range *hmm_range;
174f005ef32Sjsg 	unsigned long end;
175f005ef32Sjsg 	unsigned long timeout;
176f005ef32Sjsg 	unsigned long i;
177f005ef32Sjsg 	unsigned long *pfns;
178f005ef32Sjsg 	int r = 0;
179f005ef32Sjsg 
180f005ef32Sjsg 	hmm_range = kzalloc(sizeof(*hmm_range), GFP_KERNEL);
181f005ef32Sjsg 	if (unlikely(!hmm_range))
182f005ef32Sjsg 		return -ENOMEM;
183f005ef32Sjsg 
184f005ef32Sjsg 	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
185f005ef32Sjsg 	if (unlikely(!pfns)) {
186f005ef32Sjsg 		r = -ENOMEM;
187f005ef32Sjsg 		goto out_free_range;
188f005ef32Sjsg 	}
189f005ef32Sjsg 
190f005ef32Sjsg 	hmm_range->notifier = notifier;
191f005ef32Sjsg 	hmm_range->default_flags = HMM_PFN_REQ_FAULT;
192f005ef32Sjsg 	if (!readonly)
193f005ef32Sjsg 		hmm_range->default_flags |= HMM_PFN_REQ_WRITE;
194f005ef32Sjsg 	hmm_range->hmm_pfns = pfns;
195f005ef32Sjsg 	hmm_range->start = start;
196f005ef32Sjsg 	end = start + npages * PAGE_SIZE;
197f005ef32Sjsg 	hmm_range->dev_private_owner = owner;
198f005ef32Sjsg 
199f005ef32Sjsg 	do {
200f005ef32Sjsg 		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
201f005ef32Sjsg 
202f005ef32Sjsg 		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
203f005ef32Sjsg 			hmm_range->start, hmm_range->end);
204f005ef32Sjsg 
205f005ef32Sjsg 		/* Assuming 128MB takes maximum 1 second to fault page address */
206f005ef32Sjsg 		timeout = max((hmm_range->end - hmm_range->start) >> 27, 1UL);
207f005ef32Sjsg 		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
208f005ef32Sjsg 		timeout = jiffies + msecs_to_jiffies(timeout);
209f005ef32Sjsg 
210f005ef32Sjsg retry:
211f005ef32Sjsg 		hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
212f005ef32Sjsg 		r = hmm_range_fault(hmm_range);
213f005ef32Sjsg 		if (unlikely(r)) {
214f005ef32Sjsg 			/*
215f005ef32Sjsg 			 * FIXME: This timeout should encompass the retry from
216f005ef32Sjsg 			 * mmu_interval_read_retry() as well.
217f005ef32Sjsg 			 */
218f005ef32Sjsg 			if (r == -EBUSY && !time_after(jiffies, timeout))
219f005ef32Sjsg 				goto retry;
220f005ef32Sjsg 			goto out_free_pfns;
221f005ef32Sjsg 		}
222f005ef32Sjsg 
223f005ef32Sjsg 		if (hmm_range->end == end)
224f005ef32Sjsg 			break;
225f005ef32Sjsg 		hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
226f005ef32Sjsg 		hmm_range->start = hmm_range->end;
227f005ef32Sjsg 		schedule();
228f005ef32Sjsg 	} while (hmm_range->end < end);
229f005ef32Sjsg 
230f005ef32Sjsg 	hmm_range->start = start;
231f005ef32Sjsg 	hmm_range->hmm_pfns = pfns;
232f005ef32Sjsg 
233f005ef32Sjsg 	/*
234f005ef32Sjsg 	 * Due to default_flags, all pages are HMM_PFN_VALID or
235f005ef32Sjsg 	 * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
236f005ef32Sjsg 	 * the notifier_lock, and mmu_interval_read_retry() must be done first.
237f005ef32Sjsg 	 */
238f005ef32Sjsg 	for (i = 0; pages && i < npages; i++)
239f005ef32Sjsg 		pages[i] = hmm_pfn_to_page(pfns[i]);
240f005ef32Sjsg 
241f005ef32Sjsg 	*phmm_range = hmm_range;
242f005ef32Sjsg 
243f005ef32Sjsg 	return 0;
244f005ef32Sjsg 
245f005ef32Sjsg out_free_pfns:
246f005ef32Sjsg 	kvfree(pfns);
247f005ef32Sjsg out_free_range:
248f005ef32Sjsg 	kfree(hmm_range);
249f005ef32Sjsg 
250f005ef32Sjsg 	return r;
251f005ef32Sjsg }
252f005ef32Sjsg 
amdgpu_hmm_range_get_pages_done(struct hmm_range * hmm_range)253f005ef32Sjsg bool amdgpu_hmm_range_get_pages_done(struct hmm_range *hmm_range)
254f005ef32Sjsg {
255f005ef32Sjsg 	bool r;
256f005ef32Sjsg 
257f005ef32Sjsg 	r = mmu_interval_read_retry(hmm_range->notifier,
258f005ef32Sjsg 				    hmm_range->notifier_seq);
259f005ef32Sjsg 	kvfree(hmm_range->hmm_pfns);
260f005ef32Sjsg 	kfree(hmm_range);
261f005ef32Sjsg 
262f005ef32Sjsg 	return r;
263f005ef32Sjsg }
264