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