162306a36Sopenharmony_ci// SPDX-License-Identifier: MIT
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright © 2010 Daniel Vetter
462306a36Sopenharmony_ci * Copyright © 2020 Intel Corporation
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/slab.h> /* fault-inject.h is not standalone! */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/fault-inject.h>
1062306a36Sopenharmony_ci#include <linux/log2.h>
1162306a36Sopenharmony_ci#include <linux/random.h>
1262306a36Sopenharmony_ci#include <linux/seq_file.h>
1362306a36Sopenharmony_ci#include <linux/stop_machine.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <asm/set_memory.h>
1662306a36Sopenharmony_ci#include <asm/smp.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include "display/intel_frontbuffer.h"
1962306a36Sopenharmony_ci#include "gt/intel_gt.h"
2062306a36Sopenharmony_ci#include "gt/intel_gt_requests.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#include "i915_drv.h"
2362306a36Sopenharmony_ci#include "i915_gem_evict.h"
2462306a36Sopenharmony_ci#include "i915_scatterlist.h"
2562306a36Sopenharmony_ci#include "i915_trace.h"
2662306a36Sopenharmony_ci#include "i915_vgpu.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciint i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2962306a36Sopenharmony_ci			       struct sg_table *pages)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	do {
3262306a36Sopenharmony_ci		if (dma_map_sg_attrs(obj->base.dev->dev,
3362306a36Sopenharmony_ci				     pages->sgl, pages->nents,
3462306a36Sopenharmony_ci				     DMA_BIDIRECTIONAL,
3562306a36Sopenharmony_ci				     DMA_ATTR_SKIP_CPU_SYNC |
3662306a36Sopenharmony_ci				     DMA_ATTR_NO_KERNEL_MAPPING |
3762306a36Sopenharmony_ci				     DMA_ATTR_NO_WARN))
3862306a36Sopenharmony_ci			return 0;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci		/*
4162306a36Sopenharmony_ci		 * If the DMA remap fails, one cause can be that we have
4262306a36Sopenharmony_ci		 * too many objects pinned in a small remapping table,
4362306a36Sopenharmony_ci		 * such as swiotlb. Incrementally purge all other objects and
4462306a36Sopenharmony_ci		 * try again - if there are no more pages to remove from
4562306a36Sopenharmony_ci		 * the DMA remapper, i915_gem_shrink will return 0.
4662306a36Sopenharmony_ci		 */
4762306a36Sopenharmony_ci		GEM_BUG_ON(obj->mm.pages == pages);
4862306a36Sopenharmony_ci	} while (i915_gem_shrink(NULL, to_i915(obj->base.dev),
4962306a36Sopenharmony_ci				 obj->base.size >> PAGE_SHIFT, NULL,
5062306a36Sopenharmony_ci				 I915_SHRINK_BOUND |
5162306a36Sopenharmony_ci				 I915_SHRINK_UNBOUND));
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	return -ENOSPC;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_civoid i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
5762306a36Sopenharmony_ci			       struct sg_table *pages)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	struct drm_i915_private *i915 = to_i915(obj->base.dev);
6062306a36Sopenharmony_ci	struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	/* XXX This does not prevent more requests being submitted! */
6362306a36Sopenharmony_ci	if (unlikely(ggtt->do_idle_maps))
6462306a36Sopenharmony_ci		/* Wait a bit, in the hope it avoids the hang */
6562306a36Sopenharmony_ci		usleep_range(100, 250);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	dma_unmap_sg(i915->drm.dev, pages->sgl, pages->nents,
6862306a36Sopenharmony_ci		     DMA_BIDIRECTIONAL);
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/**
7262306a36Sopenharmony_ci * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
7362306a36Sopenharmony_ci * @vm: the &struct i915_address_space
7462306a36Sopenharmony_ci * @ww: An optional struct i915_gem_ww_ctx.
7562306a36Sopenharmony_ci * @node: the &struct drm_mm_node (typically i915_vma.mode)
7662306a36Sopenharmony_ci * @size: how much space to allocate inside the GTT,
7762306a36Sopenharmony_ci *        must be #I915_GTT_PAGE_SIZE aligned
7862306a36Sopenharmony_ci * @offset: where to insert inside the GTT,
7962306a36Sopenharmony_ci *          must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
8062306a36Sopenharmony_ci *          (@offset + @size) must fit within the address space
8162306a36Sopenharmony_ci * @color: color to apply to node, if this node is not from a VMA,
8262306a36Sopenharmony_ci *         color must be #I915_COLOR_UNEVICTABLE
8362306a36Sopenharmony_ci * @flags: control search and eviction behaviour
8462306a36Sopenharmony_ci *
8562306a36Sopenharmony_ci * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
8662306a36Sopenharmony_ci * the address space (using @size and @color). If the @node does not fit, it
8762306a36Sopenharmony_ci * tries to evict any overlapping nodes from the GTT, including any
8862306a36Sopenharmony_ci * neighbouring nodes if the colors do not match (to ensure guard pages between
8962306a36Sopenharmony_ci * differing domains). See i915_gem_evict_for_node() for the gory details
9062306a36Sopenharmony_ci * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
9162306a36Sopenharmony_ci * evicting active overlapping objects, and any overlapping node that is pinned
9262306a36Sopenharmony_ci * or marked as unevictable will also result in failure.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
9562306a36Sopenharmony_ci * asked to wait for eviction and interrupted.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ciint i915_gem_gtt_reserve(struct i915_address_space *vm,
9862306a36Sopenharmony_ci			 struct i915_gem_ww_ctx *ww,
9962306a36Sopenharmony_ci			 struct drm_mm_node *node,
10062306a36Sopenharmony_ci			 u64 size, u64 offset, unsigned long color,
10162306a36Sopenharmony_ci			 unsigned int flags)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	int err;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	GEM_BUG_ON(!size);
10662306a36Sopenharmony_ci	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
10762306a36Sopenharmony_ci	GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
10862306a36Sopenharmony_ci	GEM_BUG_ON(range_overflows(offset, size, vm->total));
10962306a36Sopenharmony_ci	GEM_BUG_ON(vm == &to_gt(vm->i915)->ggtt->alias->vm);
11062306a36Sopenharmony_ci	GEM_BUG_ON(drm_mm_node_allocated(node));
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	node->size = size;
11362306a36Sopenharmony_ci	node->start = offset;
11462306a36Sopenharmony_ci	node->color = color;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	err = drm_mm_reserve_node(&vm->mm, node);
11762306a36Sopenharmony_ci	if (err != -ENOSPC)
11862306a36Sopenharmony_ci		return err;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	if (flags & PIN_NOEVICT)
12162306a36Sopenharmony_ci		return -ENOSPC;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	err = i915_gem_evict_for_node(vm, ww, node, flags);
12462306a36Sopenharmony_ci	if (err == 0)
12562306a36Sopenharmony_ci		err = drm_mm_reserve_node(&vm->mm, node);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	return err;
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic u64 random_offset(u64 start, u64 end, u64 len, u64 align)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	u64 range, addr;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	GEM_BUG_ON(range_overflows(start, len, end));
13562306a36Sopenharmony_ci	GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	range = round_down(end - len, align) - round_up(start, align);
13862306a36Sopenharmony_ci	if (range) {
13962306a36Sopenharmony_ci		if (sizeof(unsigned long) == sizeof(u64)) {
14062306a36Sopenharmony_ci			addr = get_random_u64();
14162306a36Sopenharmony_ci		} else {
14262306a36Sopenharmony_ci			addr = get_random_u32();
14362306a36Sopenharmony_ci			if (range > U32_MAX) {
14462306a36Sopenharmony_ci				addr <<= 32;
14562306a36Sopenharmony_ci				addr |= get_random_u32();
14662306a36Sopenharmony_ci			}
14762306a36Sopenharmony_ci		}
14862306a36Sopenharmony_ci		div64_u64_rem(addr, range, &addr);
14962306a36Sopenharmony_ci		start += addr;
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	return round_up(start, align);
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/**
15662306a36Sopenharmony_ci * i915_gem_gtt_insert - insert a node into an address_space (GTT)
15762306a36Sopenharmony_ci * @vm: the &struct i915_address_space
15862306a36Sopenharmony_ci * @ww: An optional struct i915_gem_ww_ctx.
15962306a36Sopenharmony_ci * @node: the &struct drm_mm_node (typically i915_vma.node)
16062306a36Sopenharmony_ci * @size: how much space to allocate inside the GTT,
16162306a36Sopenharmony_ci *        must be #I915_GTT_PAGE_SIZE aligned
16262306a36Sopenharmony_ci * @alignment: required alignment of starting offset, may be 0 but
16362306a36Sopenharmony_ci *             if specified, this must be a power-of-two and at least
16462306a36Sopenharmony_ci *             #I915_GTT_MIN_ALIGNMENT
16562306a36Sopenharmony_ci * @color: color to apply to node
16662306a36Sopenharmony_ci * @start: start of any range restriction inside GTT (0 for all),
16762306a36Sopenharmony_ci *         must be #I915_GTT_PAGE_SIZE aligned
16862306a36Sopenharmony_ci * @end: end of any range restriction inside GTT (U64_MAX for all),
16962306a36Sopenharmony_ci *       must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
17062306a36Sopenharmony_ci * @flags: control search and eviction behaviour
17162306a36Sopenharmony_ci *
17262306a36Sopenharmony_ci * i915_gem_gtt_insert() first searches for an available hole into which
17362306a36Sopenharmony_ci * is can insert the node. The hole address is aligned to @alignment and
17462306a36Sopenharmony_ci * its @size must then fit entirely within the [@start, @end] bounds. The
17562306a36Sopenharmony_ci * nodes on either side of the hole must match @color, or else a guard page
17662306a36Sopenharmony_ci * will be inserted between the two nodes (or the node evicted). If no
17762306a36Sopenharmony_ci * suitable hole is found, first a victim is randomly selected and tested
17862306a36Sopenharmony_ci * for eviction, otherwise then the LRU list of objects within the GTT
17962306a36Sopenharmony_ci * is scanned to find the first set of replacement nodes to create the hole.
18062306a36Sopenharmony_ci * Those old overlapping nodes are evicted from the GTT (and so must be
18162306a36Sopenharmony_ci * rebound before any future use). Any node that is currently pinned cannot
18262306a36Sopenharmony_ci * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
18362306a36Sopenharmony_ci * active and #PIN_NONBLOCK is specified, that node is also skipped when
18462306a36Sopenharmony_ci * searching for an eviction candidate. See i915_gem_evict_something() for
18562306a36Sopenharmony_ci * the gory details on the eviction algorithm.
18662306a36Sopenharmony_ci *
18762306a36Sopenharmony_ci * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
18862306a36Sopenharmony_ci * asked to wait for eviction and interrupted.
18962306a36Sopenharmony_ci */
19062306a36Sopenharmony_ciint i915_gem_gtt_insert(struct i915_address_space *vm,
19162306a36Sopenharmony_ci			struct i915_gem_ww_ctx *ww,
19262306a36Sopenharmony_ci			struct drm_mm_node *node,
19362306a36Sopenharmony_ci			u64 size, u64 alignment, unsigned long color,
19462306a36Sopenharmony_ci			u64 start, u64 end, unsigned int flags)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	enum drm_mm_insert_mode mode;
19762306a36Sopenharmony_ci	u64 offset;
19862306a36Sopenharmony_ci	int err;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	lockdep_assert_held(&vm->mutex);
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	GEM_BUG_ON(!size);
20362306a36Sopenharmony_ci	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
20462306a36Sopenharmony_ci	GEM_BUG_ON(alignment && !is_power_of_2(alignment));
20562306a36Sopenharmony_ci	GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
20662306a36Sopenharmony_ci	GEM_BUG_ON(start >= end);
20762306a36Sopenharmony_ci	GEM_BUG_ON(start > 0  && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
20862306a36Sopenharmony_ci	GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
20962306a36Sopenharmony_ci	GEM_BUG_ON(vm == &to_gt(vm->i915)->ggtt->alias->vm);
21062306a36Sopenharmony_ci	GEM_BUG_ON(drm_mm_node_allocated(node));
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	if (unlikely(range_overflows(start, size, end)))
21362306a36Sopenharmony_ci		return -ENOSPC;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
21662306a36Sopenharmony_ci		return -ENOSPC;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	mode = DRM_MM_INSERT_BEST;
21962306a36Sopenharmony_ci	if (flags & PIN_HIGH)
22062306a36Sopenharmony_ci		mode = DRM_MM_INSERT_HIGHEST;
22162306a36Sopenharmony_ci	if (flags & PIN_MAPPABLE)
22262306a36Sopenharmony_ci		mode = DRM_MM_INSERT_LOW;
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	/* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
22562306a36Sopenharmony_ci	 * so we know that we always have a minimum alignment of 4096.
22662306a36Sopenharmony_ci	 * The drm_mm range manager is optimised to return results
22762306a36Sopenharmony_ci	 * with zero alignment, so where possible use the optimal
22862306a36Sopenharmony_ci	 * path.
22962306a36Sopenharmony_ci	 */
23062306a36Sopenharmony_ci	BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
23162306a36Sopenharmony_ci	if (alignment <= I915_GTT_MIN_ALIGNMENT)
23262306a36Sopenharmony_ci		alignment = 0;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	err = drm_mm_insert_node_in_range(&vm->mm, node,
23562306a36Sopenharmony_ci					  size, alignment, color,
23662306a36Sopenharmony_ci					  start, end, mode);
23762306a36Sopenharmony_ci	if (err != -ENOSPC)
23862306a36Sopenharmony_ci		return err;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	if (mode & DRM_MM_INSERT_ONCE) {
24162306a36Sopenharmony_ci		err = drm_mm_insert_node_in_range(&vm->mm, node,
24262306a36Sopenharmony_ci						  size, alignment, color,
24362306a36Sopenharmony_ci						  start, end,
24462306a36Sopenharmony_ci						  DRM_MM_INSERT_BEST);
24562306a36Sopenharmony_ci		if (err != -ENOSPC)
24662306a36Sopenharmony_ci			return err;
24762306a36Sopenharmony_ci	}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	if (flags & PIN_NOEVICT)
25062306a36Sopenharmony_ci		return -ENOSPC;
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	/*
25362306a36Sopenharmony_ci	 * No free space, pick a slot at random.
25462306a36Sopenharmony_ci	 *
25562306a36Sopenharmony_ci	 * There is a pathological case here using a GTT shared between
25662306a36Sopenharmony_ci	 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
25762306a36Sopenharmony_ci	 *
25862306a36Sopenharmony_ci	 *    |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
25962306a36Sopenharmony_ci	 *         (64k objects)             (448k objects)
26062306a36Sopenharmony_ci	 *
26162306a36Sopenharmony_ci	 * Now imagine that the eviction LRU is ordered top-down (just because
26262306a36Sopenharmony_ci	 * pathology meets real life), and that we need to evict an object to
26362306a36Sopenharmony_ci	 * make room inside the aperture. The eviction scan then has to walk
26462306a36Sopenharmony_ci	 * the 448k list before it finds one within range. And now imagine that
26562306a36Sopenharmony_ci	 * it has to search for a new hole between every byte inside the memcpy,
26662306a36Sopenharmony_ci	 * for several simultaneous clients.
26762306a36Sopenharmony_ci	 *
26862306a36Sopenharmony_ci	 * On a full-ppgtt system, if we have run out of available space, there
26962306a36Sopenharmony_ci	 * will be lots and lots of objects in the eviction list! Again,
27062306a36Sopenharmony_ci	 * searching that LRU list may be slow if we are also applying any
27162306a36Sopenharmony_ci	 * range restrictions (e.g. restriction to low 4GiB) and so, for
27262306a36Sopenharmony_ci	 * simplicity and similarilty between different GTT, try the single
27362306a36Sopenharmony_ci	 * random replacement first.
27462306a36Sopenharmony_ci	 */
27562306a36Sopenharmony_ci	offset = random_offset(start, end,
27662306a36Sopenharmony_ci			       size, alignment ?: I915_GTT_MIN_ALIGNMENT);
27762306a36Sopenharmony_ci	err = i915_gem_gtt_reserve(vm, ww, node, size, offset, color, flags);
27862306a36Sopenharmony_ci	if (err != -ENOSPC)
27962306a36Sopenharmony_ci		return err;
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci	if (flags & PIN_NOSEARCH)
28262306a36Sopenharmony_ci		return -ENOSPC;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	/* Randomly selected placement is pinned, do a search */
28562306a36Sopenharmony_ci	err = i915_gem_evict_something(vm, ww, size, alignment, color,
28662306a36Sopenharmony_ci				       start, end, flags);
28762306a36Sopenharmony_ci	if (err)
28862306a36Sopenharmony_ci		return err;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	return drm_mm_insert_node_in_range(&vm->mm, node,
29162306a36Sopenharmony_ci					   size, alignment, color,
29262306a36Sopenharmony_ci					   start, end, DRM_MM_INSERT_EVICT);
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
29662306a36Sopenharmony_ci#include "selftests/i915_gem_gtt.c"
29762306a36Sopenharmony_ci#endif
298