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