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