162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci * All Rights Reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
662306a36Sopenharmony_ci * copy of this software and associated documentation files (the
762306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
862306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
962306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
1062306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1162306a36Sopenharmony_ci * the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1462306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1562306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1662306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
1762306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1862306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
1962306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
2262306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
2362306a36Sopenharmony_ci * of the Software.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * Authors:
2862306a36Sopenharmony_ci *    Christian König <christian.koenig@amd.com>
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci * DOC: MMU Notifier
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * For coherent userptr handling registers an MMU notifier to inform the driver
3562306a36Sopenharmony_ci * about updates on the page tables of a process.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * When somebody tries to invalidate the page tables we block the update until
3862306a36Sopenharmony_ci * all operations on the pages in question are completed, then those pages are
3962306a36Sopenharmony_ci * marked as accessed and also dirty if it wasn't a read only access.
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * New command submissions using the userptrs in question are delayed until all
4262306a36Sopenharmony_ci * page table invalidation are completed and we once more see a coherent process
4362306a36Sopenharmony_ci * address space.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#include <linux/firmware.h>
4762306a36Sopenharmony_ci#include <linux/module.h>
4862306a36Sopenharmony_ci#include <drm/drm.h>
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#include "amdgpu.h"
5162306a36Sopenharmony_ci#include "amdgpu_amdkfd.h"
5262306a36Sopenharmony_ci#include "amdgpu_hmm.h"
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define MAX_WALK_BYTE	(2UL << 30)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/**
5762306a36Sopenharmony_ci * amdgpu_hmm_invalidate_gfx - callback to notify about mm change
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * @mni: the range (mm) is about to update
6062306a36Sopenharmony_ci * @range: details on the invalidation
6162306a36Sopenharmony_ci * @cur_seq: Value to pass to mmu_interval_set_seq()
6262306a36Sopenharmony_ci *
6362306a36Sopenharmony_ci * Block for operations on BOs to finish and mark pages as accessed and
6462306a36Sopenharmony_ci * potentially dirty.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_cistatic bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni,
6762306a36Sopenharmony_ci				      const struct mmu_notifier_range *range,
6862306a36Sopenharmony_ci				      unsigned long cur_seq)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
7162306a36Sopenharmony_ci	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
7262306a36Sopenharmony_ci	long r;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (!mmu_notifier_range_blockable(range))
7562306a36Sopenharmony_ci		return false;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	mutex_lock(&adev->notifier_lock);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	mmu_interval_set_seq(mni, cur_seq);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
8262306a36Sopenharmony_ci				  false, MAX_SCHEDULE_TIMEOUT);
8362306a36Sopenharmony_ci	mutex_unlock(&adev->notifier_lock);
8462306a36Sopenharmony_ci	if (r <= 0)
8562306a36Sopenharmony_ci		DRM_ERROR("(%ld) failed to wait for user bo\n", r);
8662306a36Sopenharmony_ci	return true;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic const struct mmu_interval_notifier_ops amdgpu_hmm_gfx_ops = {
9062306a36Sopenharmony_ci	.invalidate = amdgpu_hmm_invalidate_gfx,
9162306a36Sopenharmony_ci};
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/**
9462306a36Sopenharmony_ci * amdgpu_hmm_invalidate_hsa - callback to notify about mm change
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * @mni: the range (mm) is about to update
9762306a36Sopenharmony_ci * @range: details on the invalidation
9862306a36Sopenharmony_ci * @cur_seq: Value to pass to mmu_interval_set_seq()
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * We temporarily evict the BO attached to this range. This necessitates
10162306a36Sopenharmony_ci * evicting all user-mode queues of the process.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_cistatic bool amdgpu_hmm_invalidate_hsa(struct mmu_interval_notifier *mni,
10462306a36Sopenharmony_ci				      const struct mmu_notifier_range *range,
10562306a36Sopenharmony_ci				      unsigned long cur_seq)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	if (!mmu_notifier_range_blockable(range))
11062306a36Sopenharmony_ci		return false;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	amdgpu_amdkfd_evict_userptr(mni, cur_seq, bo->kfd_bo);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	return true;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic const struct mmu_interval_notifier_ops amdgpu_hmm_hsa_ops = {
11862306a36Sopenharmony_ci	.invalidate = amdgpu_hmm_invalidate_hsa,
11962306a36Sopenharmony_ci};
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci/**
12262306a36Sopenharmony_ci * amdgpu_hmm_register - register a BO for notifier updates
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * @bo: amdgpu buffer object
12562306a36Sopenharmony_ci * @addr: userptr addr we should monitor
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci * Registers a mmu_notifier for the given BO at the specified address.
12862306a36Sopenharmony_ci * Returns 0 on success, -ERRNO if anything goes wrong.
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_ciint amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	if (bo->kfd_bo)
13362306a36Sopenharmony_ci		return mmu_interval_notifier_insert(&bo->notifier, current->mm,
13462306a36Sopenharmony_ci						    addr, amdgpu_bo_size(bo),
13562306a36Sopenharmony_ci						    &amdgpu_hmm_hsa_ops);
13662306a36Sopenharmony_ci	return mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
13762306a36Sopenharmony_ci					    amdgpu_bo_size(bo),
13862306a36Sopenharmony_ci					    &amdgpu_hmm_gfx_ops);
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci/**
14262306a36Sopenharmony_ci * amdgpu_hmm_unregister - unregister a BO for notifier updates
14362306a36Sopenharmony_ci *
14462306a36Sopenharmony_ci * @bo: amdgpu buffer object
14562306a36Sopenharmony_ci *
14662306a36Sopenharmony_ci * Remove any registration of mmu notifier updates from the buffer object.
14762306a36Sopenharmony_ci */
14862306a36Sopenharmony_civoid amdgpu_hmm_unregister(struct amdgpu_bo *bo)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	if (!bo->notifier.mm)
15162306a36Sopenharmony_ci		return;
15262306a36Sopenharmony_ci	mmu_interval_notifier_remove(&bo->notifier);
15362306a36Sopenharmony_ci	bo->notifier.mm = NULL;
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciint amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
15762306a36Sopenharmony_ci			       uint64_t start, uint64_t npages, bool readonly,
15862306a36Sopenharmony_ci			       void *owner, struct page **pages,
15962306a36Sopenharmony_ci			       struct hmm_range **phmm_range)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	struct hmm_range *hmm_range;
16262306a36Sopenharmony_ci	unsigned long end;
16362306a36Sopenharmony_ci	unsigned long timeout;
16462306a36Sopenharmony_ci	unsigned long i;
16562306a36Sopenharmony_ci	unsigned long *pfns;
16662306a36Sopenharmony_ci	int r = 0;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	hmm_range = kzalloc(sizeof(*hmm_range), GFP_KERNEL);
16962306a36Sopenharmony_ci	if (unlikely(!hmm_range))
17062306a36Sopenharmony_ci		return -ENOMEM;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
17362306a36Sopenharmony_ci	if (unlikely(!pfns)) {
17462306a36Sopenharmony_ci		r = -ENOMEM;
17562306a36Sopenharmony_ci		goto out_free_range;
17662306a36Sopenharmony_ci	}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	hmm_range->notifier = notifier;
17962306a36Sopenharmony_ci	hmm_range->default_flags = HMM_PFN_REQ_FAULT;
18062306a36Sopenharmony_ci	if (!readonly)
18162306a36Sopenharmony_ci		hmm_range->default_flags |= HMM_PFN_REQ_WRITE;
18262306a36Sopenharmony_ci	hmm_range->hmm_pfns = pfns;
18362306a36Sopenharmony_ci	hmm_range->start = start;
18462306a36Sopenharmony_ci	end = start + npages * PAGE_SIZE;
18562306a36Sopenharmony_ci	hmm_range->dev_private_owner = owner;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	do {
18862306a36Sopenharmony_ci		hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci		pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
19162306a36Sopenharmony_ci			hmm_range->start, hmm_range->end);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci		/* Assuming 128MB takes maximum 1 second to fault page address */
19462306a36Sopenharmony_ci		timeout = max((hmm_range->end - hmm_range->start) >> 27, 1UL);
19562306a36Sopenharmony_ci		timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
19662306a36Sopenharmony_ci		timeout = jiffies + msecs_to_jiffies(timeout);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciretry:
19962306a36Sopenharmony_ci		hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
20062306a36Sopenharmony_ci		r = hmm_range_fault(hmm_range);
20162306a36Sopenharmony_ci		if (unlikely(r)) {
20262306a36Sopenharmony_ci			/*
20362306a36Sopenharmony_ci			 * FIXME: This timeout should encompass the retry from
20462306a36Sopenharmony_ci			 * mmu_interval_read_retry() as well.
20562306a36Sopenharmony_ci			 */
20662306a36Sopenharmony_ci			if (r == -EBUSY && !time_after(jiffies, timeout))
20762306a36Sopenharmony_ci				goto retry;
20862306a36Sopenharmony_ci			goto out_free_pfns;
20962306a36Sopenharmony_ci		}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci		if (hmm_range->end == end)
21262306a36Sopenharmony_ci			break;
21362306a36Sopenharmony_ci		hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
21462306a36Sopenharmony_ci		hmm_range->start = hmm_range->end;
21562306a36Sopenharmony_ci		schedule();
21662306a36Sopenharmony_ci	} while (hmm_range->end < end);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	hmm_range->start = start;
21962306a36Sopenharmony_ci	hmm_range->hmm_pfns = pfns;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	/*
22262306a36Sopenharmony_ci	 * Due to default_flags, all pages are HMM_PFN_VALID or
22362306a36Sopenharmony_ci	 * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
22462306a36Sopenharmony_ci	 * the notifier_lock, and mmu_interval_read_retry() must be done first.
22562306a36Sopenharmony_ci	 */
22662306a36Sopenharmony_ci	for (i = 0; pages && i < npages; i++)
22762306a36Sopenharmony_ci		pages[i] = hmm_pfn_to_page(pfns[i]);
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	*phmm_range = hmm_range;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	return 0;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ciout_free_pfns:
23462306a36Sopenharmony_ci	kvfree(pfns);
23562306a36Sopenharmony_ciout_free_range:
23662306a36Sopenharmony_ci	kfree(hmm_range);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	return r;
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cibool amdgpu_hmm_range_get_pages_done(struct hmm_range *hmm_range)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	bool r;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	r = mmu_interval_read_retry(hmm_range->notifier,
24662306a36Sopenharmony_ci				    hmm_range->notifier_seq);
24762306a36Sopenharmony_ci	kvfree(hmm_range->hmm_pfns);
24862306a36Sopenharmony_ci	kfree(hmm_range);
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	return r;
25162306a36Sopenharmony_ci}
252