Home | History | Annotate | Line # | Download | only in i915
i915_gem_gtt.c revision 1.19
      1 /*	$NetBSD: i915_gem_gtt.c,v 1.19 2021/12/18 23:45:28 riastradh Exp $	*/
      2 
      3 // SPDX-License-Identifier: MIT
      4 /*
      5  * Copyright  2010 Daniel Vetter
      6  * Copyright  2020 Intel Corporation
      7  */
      8 
      9 #include <sys/cdefs.h>
     10 __KERNEL_RCSID(0, "$NetBSD: i915_gem_gtt.c,v 1.19 2021/12/18 23:45:28 riastradh Exp $");
     11 
     12 #include <linux/slab.h> /* fault-inject.h is not standalone! */
     13 
     14 #include <linux/fault-inject.h>
     15 #include <linux/log2.h>
     16 #include <linux/random.h>
     17 #include <linux/seq_file.h>
     18 #include <linux/stop_machine.h>
     19 
     20 #include <asm/set_memory.h>
     21 #include <asm/smp.h>
     22 
     23 #include <drm/i915_drm.h>
     24 
     25 #include "display/intel_frontbuffer.h"
     26 #include "gt/intel_gt.h"
     27 #include "gt/intel_gt_requests.h"
     28 
     29 #include "i915_drv.h"
     30 #include "i915_scatterlist.h"
     31 #include "i915_trace.h"
     32 #include "i915_vgpu.h"
     33 
     34 #ifdef __NetBSD__
     35 #include <drm/bus_dma_hacks.h>
     36 #include <x86/machdep.h>
     37 #include <x86/pte.h>
     38 #define	_PAGE_PRESENT	PTE_P	/* 0x01 PTE is present */
     39 #define	_PAGE_RW	PTE_W	/* 0x02 read/write */
     40 #define	_PAGE_PWT	PTE_PWT	/* 0x08 page write-through */
     41 #define	_PAGE_PCD	PTE_PCD	/* 0x10 page cache disabled */
     42 #define	_PAGE_PAT	PTE_PAT	/* 0x80 page attribute table on PTE */
     43 #endif
     44 
     45 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
     46 			       struct sg_table *pages)
     47 {
     48 	do {
     49 #ifdef __NetBSD__
     50 		KASSERT(0 < obj->base.size);
     51 		/* XXX errno NetBSD->Linux */
     52 		return -bus_dmamap_load_pglist(obj->base.dev->dmat, obj->pages,
     53 		    &obj->pageq, obj->base.size, BUS_DMA_NOWAIT);
     54 #else
     55 		if (dma_map_sg_attrs(&obj->base.dev->pdev->dev,
     56 				     pages->sgl, pages->nents,
     57 				     PCI_DMA_BIDIRECTIONAL,
     58 				     DMA_ATTR_NO_WARN))
     59 			return 0;
     60 #endif
     61 
     62 		/*
     63 		 * If the DMA remap fails, one cause can be that we have
     64 		 * too many objects pinned in a small remapping table,
     65 		 * such as swiotlb. Incrementally purge all other objects and
     66 		 * try again - if there are no more pages to remove from
     67 		 * the DMA remapper, i915_gem_shrink will return 0.
     68 		 */
     69 		GEM_BUG_ON(obj->mm.pages == pages);
     70 	} while (i915_gem_shrink(to_i915(obj->base.dev),
     71 				 obj->base.size >> PAGE_SHIFT, NULL,
     72 				 I915_SHRINK_BOUND |
     73 				 I915_SHRINK_UNBOUND));
     74 
     75 	return -ENOSPC;
     76 }
     77 
     78 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
     79 			       struct sg_table *pages)
     80 {
     81 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
     82 	struct device *kdev = &dev_priv->drm.pdev->dev;
     83 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
     84 
     85 	if (unlikely(ggtt->do_idle_maps)) {
     86 		/* XXX This does not prevent more requests being submitted! */
     87 		if (intel_gt_retire_requests_timeout(ggtt->vm.gt,
     88 						     -MAX_SCHEDULE_TIMEOUT)) {
     89 			DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
     90 			/* Wait a bit, in hopes it avoids the hang */
     91 			udelay(10);
     92 		}
     93 	}
     94 
     95 	dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
     96 }
     97 
     98 /**
     99  * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
    100  * @vm: the &struct i915_address_space
    101  * @node: the &struct drm_mm_node (typically i915_vma.mode)
    102  * @size: how much space to allocate inside the GTT,
    103  *        must be #I915_GTT_PAGE_SIZE aligned
    104  * @offset: where to insert inside the GTT,
    105  *          must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
    106  *          (@offset + @size) must fit within the address space
    107  * @color: color to apply to node, if this node is not from a VMA,
    108  *         color must be #I915_COLOR_UNEVICTABLE
    109  * @flags: control search and eviction behaviour
    110  *
    111  * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
    112  * the address space (using @size and @color). If the @node does not fit, it
    113  * tries to evict any overlapping nodes from the GTT, including any
    114  * neighbouring nodes if the colors do not match (to ensure guard pages between
    115  * differing domains). See i915_gem_evict_for_node() for the gory details
    116  * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
    117  * evicting active overlapping objects, and any overlapping node that is pinned
    118  * or marked as unevictable will also result in failure.
    119  *
    120  * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
    121  * asked to wait for eviction and interrupted.
    122  */
    123 int i915_gem_gtt_reserve(struct i915_address_space *vm,
    124 			 struct drm_mm_node *node,
    125 			 u64 size, u64 offset, unsigned long color,
    126 			 unsigned int flags)
    127 {
    128 	int err;
    129 
    130 	GEM_BUG_ON(!size);
    131 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
    132 	GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
    133 	GEM_BUG_ON(range_overflows(offset, size, vm->total));
    134 	GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
    135 	GEM_BUG_ON(drm_mm_node_allocated(node));
    136 
    137 	node->size = size;
    138 	node->start = offset;
    139 	node->color = color;
    140 
    141 	err = drm_mm_reserve_node(&vm->mm, node);
    142 	if (err != -ENOSPC)
    143 		return err;
    144 
    145 	if (flags & PIN_NOEVICT)
    146 		return -ENOSPC;
    147 
    148 	err = i915_gem_evict_for_node(vm, node, flags);
    149 	if (err == 0)
    150 		err = drm_mm_reserve_node(&vm->mm, node);
    151 
    152 	return err;
    153 }
    154 
    155 static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
    156 {
    157 	u64 range, addr;
    158 
    159 	GEM_BUG_ON(range_overflows(start, len, end));
    160 	GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
    161 
    162 	range = round_down(end - len, align) - round_up(start, align);
    163 	if (range) {
    164 		if (sizeof(unsigned long) == sizeof(u64)) {
    165 			addr = get_random_long();
    166 		} else {
    167 			addr = get_random_int();
    168 			if (range > U32_MAX) {
    169 				addr <<= 32;
    170 				addr |= get_random_int();
    171 			}
    172 		}
    173 		div64_u64_rem(addr, range, &addr);
    174 		start += addr;
    175 	}
    176 
    177 	return round_up(start, align);
    178 }
    179 
    180 /**
    181  * i915_gem_gtt_insert - insert a node into an address_space (GTT)
    182  * @vm: the &struct i915_address_space
    183  * @node: the &struct drm_mm_node (typically i915_vma.node)
    184  * @size: how much space to allocate inside the GTT,
    185  *        must be #I915_GTT_PAGE_SIZE aligned
    186  * @alignment: required alignment of starting offset, may be 0 but
    187  *             if specified, this must be a power-of-two and at least
    188  *             #I915_GTT_MIN_ALIGNMENT
    189  * @color: color to apply to node
    190  * @start: start of any range restriction inside GTT (0 for all),
    191  *         must be #I915_GTT_PAGE_SIZE aligned
    192  * @end: end of any range restriction inside GTT (U64_MAX for all),
    193  *       must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
    194  * @flags: control search and eviction behaviour
    195  *
    196  * i915_gem_gtt_insert() first searches for an available hole into which
    197  * is can insert the node. The hole address is aligned to @alignment and
    198  * its @size must then fit entirely within the [@start, @end] bounds. The
    199  * nodes on either side of the hole must match @color, or else a guard page
    200  * will be inserted between the two nodes (or the node evicted). If no
    201  * suitable hole is found, first a victim is randomly selected and tested
    202  * for eviction, otherwise then the LRU list of objects within the GTT
    203  * is scanned to find the first set of replacement nodes to create the hole.
    204  * Those old overlapping nodes are evicted from the GTT (and so must be
    205  * rebound before any future use). Any node that is currently pinned cannot
    206  * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
    207  * active and #PIN_NONBLOCK is specified, that node is also skipped when
    208  * searching for an eviction candidate. See i915_gem_evict_something() for
    209  * the gory details on the eviction algorithm.
    210  *
    211  * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
    212  * asked to wait for eviction and interrupted.
    213  */
    214 int i915_gem_gtt_insert(struct i915_address_space *vm,
    215 			struct drm_mm_node *node,
    216 			u64 size, u64 alignment, unsigned long color,
    217 			u64 start, u64 end, unsigned int flags)
    218 {
    219 	enum drm_mm_insert_mode mode;
    220 	u64 offset;
    221 	int err;
    222 
    223 	lockdep_assert_held(&vm->mutex);
    224 
    225 	GEM_BUG_ON(!size);
    226 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
    227 	GEM_BUG_ON(alignment && !is_power_of_2(alignment));
    228 	GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
    229 	GEM_BUG_ON(start >= end);
    230 	GEM_BUG_ON(start > 0  && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
    231 	GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
    232 	GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
    233 	GEM_BUG_ON(drm_mm_node_allocated(node));
    234 
    235 	if (unlikely(range_overflows(start, size, end)))
    236 		return -ENOSPC;
    237 
    238 	if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
    239 		return -ENOSPC;
    240 
    241 	mode = DRM_MM_INSERT_BEST;
    242 	if (flags & PIN_HIGH)
    243 		mode = DRM_MM_INSERT_HIGHEST;
    244 	if (flags & PIN_MAPPABLE)
    245 		mode = DRM_MM_INSERT_LOW;
    246 
    247 	/* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
    248 	 * so we know that we always have a minimum alignment of 4096.
    249 	 * The drm_mm range manager is optimised to return results
    250 	 * with zero alignment, so where possible use the optimal
    251 	 * path.
    252 	 */
    253 	BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
    254 	if (alignment <= I915_GTT_MIN_ALIGNMENT)
    255 		alignment = 0;
    256 
    257 	err = drm_mm_insert_node_in_range(&vm->mm, node,
    258 					  size, alignment, color,
    259 					  start, end, mode);
    260 	if (err != -ENOSPC)
    261 		return err;
    262 
    263 	if (mode & DRM_MM_INSERT_ONCE) {
    264 		err = drm_mm_insert_node_in_range(&vm->mm, node,
    265 						  size, alignment, color,
    266 						  start, end,
    267 						  DRM_MM_INSERT_BEST);
    268 		if (err != -ENOSPC)
    269 			return err;
    270 	}
    271 
    272 	if (flags & PIN_NOEVICT)
    273 		return -ENOSPC;
    274 
    275 	/*
    276 	 * No free space, pick a slot at random.
    277 	 *
    278 	 * There is a pathological case here using a GTT shared between
    279 	 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
    280 	 *
    281 	 *    |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
    282 	 *         (64k objects)             (448k objects)
    283 	 *
    284 	 * Now imagine that the eviction LRU is ordered top-down (just because
    285 	 * pathology meets real life), and that we need to evict an object to
    286 	 * make room inside the aperture. The eviction scan then has to walk
    287 	 * the 448k list before it finds one within range. And now imagine that
    288 	 * it has to search for a new hole between every byte inside the memcpy,
    289 	 * for several simultaneous clients.
    290 	 *
    291 	 * On a full-ppgtt system, if we have run out of available space, there
    292 	 * will be lots and lots of objects in the eviction list! Again,
    293 	 * searching that LRU list may be slow if we are also applying any
    294 	 * range restrictions (e.g. restriction to low 4GiB) and so, for
    295 	 * simplicity and similarilty between different GTT, try the single
    296 	 * random replacement first.
    297 	 */
    298 	offset = random_offset(start, end,
    299 			       size, alignment ?: I915_GTT_MIN_ALIGNMENT);
    300 	err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
    301 	if (err != -ENOSPC)
    302 		return err;
    303 
    304 	if (flags & PIN_NOSEARCH)
    305 		return -ENOSPC;
    306 
    307 	/* Randomly selected placement is pinned, do a search */
    308 	err = i915_gem_evict_something(vm, size, alignment, color,
    309 				       start, end, flags);
    310 	if (err)
    311 		return err;
    312 
    313 	return drm_mm_insert_node_in_range(&vm->mm, node,
    314 					   size, alignment, color,
    315 					   start, end, DRM_MM_INSERT_EVICT);
    316 }
    317 
    318 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
    319 #include "selftests/i915_gem_gtt.c"
    320 #endif
    321