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