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