2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <linux/list_sort.h>
26 #include <linux/prime_numbers.h>
28 #include "gem/i915_gem_context.h"
29 #include "gem/selftests/mock_context.h"
30 #include "gt/intel_context.h"
31 #include "gt/intel_gpu_commands.h"
33 #include "i915_random.h"
34 #include "i915_selftest.h"
37 #include "mock_gem_device.h"
39 #include "igt_flush_test.h"
41 static void cleanup_freed_objects(struct drm_i915_private *i915)
43 i915_gem_drain_freed_objects(i915);
46 static void fake_free_pages(struct drm_i915_gem_object *obj,
47 struct sg_table *pages)
53 static int fake_get_pages(struct drm_i915_gem_object *obj)
55 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
56 #define PFN_BIAS 0x1000
57 struct sg_table *pages;
58 struct scatterlist *sg;
59 unsigned int sg_page_sizes;
60 typeof(obj->base.size) rem;
62 pages = kmalloc(sizeof(*pages), GFP);
66 rem = round_up(obj->base.size, BIT(31)) >> 31;
67 if (sg_alloc_table(pages, rem, GFP)) {
74 for (sg = pages->sgl; sg; sg = sg_next(sg)) {
75 unsigned long len = min_t(typeof(rem), rem, BIT(31));
78 sg_set_page(sg, pfn_to_page(PFN_BIAS), len, 0);
79 sg_dma_address(sg) = page_to_phys(sg_page(sg));
87 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
93 static void fake_put_pages(struct drm_i915_gem_object *obj,
94 struct sg_table *pages)
96 fake_free_pages(obj, pages);
97 obj->mm.dirty = false;
100 static const struct drm_i915_gem_object_ops fake_ops = {
102 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
103 .get_pages = fake_get_pages,
104 .put_pages = fake_put_pages,
107 static struct drm_i915_gem_object *
108 fake_dma_object(struct drm_i915_private *i915, u64 size)
110 static struct lock_class_key lock_class;
111 struct drm_i915_gem_object *obj;
114 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
116 if (overflows_type(size, obj->base.size))
117 return ERR_PTR(-E2BIG);
119 obj = i915_gem_object_alloc();
123 drm_gem_private_object_init(&i915->drm, &obj->base, size);
124 i915_gem_object_init(obj, &fake_ops, &lock_class, 0);
126 i915_gem_object_set_volatile(obj);
128 obj->write_domain = I915_GEM_DOMAIN_CPU;
129 obj->read_domains = I915_GEM_DOMAIN_CPU;
130 obj->cache_level = I915_CACHE_NONE;
132 /* Preallocate the "backing storage" */
133 if (i915_gem_object_pin_pages_unlocked(obj))
136 i915_gem_object_unpin_pages(obj);
140 i915_gem_object_put(obj);
142 return ERR_PTR(-ENOMEM);
145 static int igt_ppgtt_alloc(void *arg)
147 struct drm_i915_private *dev_priv = arg;
148 struct i915_ppgtt *ppgtt;
149 struct i915_gem_ww_ctx ww;
150 u64 size, last, limit;
153 /* Allocate a ppggt and try to fill the entire range */
155 if (!HAS_PPGTT(dev_priv))
158 ppgtt = i915_ppgtt_create(&dev_priv->gt);
160 return PTR_ERR(ppgtt);
162 if (!ppgtt->vm.allocate_va_range)
163 goto err_ppgtt_cleanup;
166 * While we only allocate the page tables here and so we could
167 * address a much larger GTT than we could actually fit into
168 * RAM, a practical limit is the amount of physical pages in the system.
169 * This should ensure that we do not run into the oomkiller during
170 * the test and take down the machine wilfully.
172 limit = totalram_pages() << PAGE_SHIFT;
173 limit = min(ppgtt->vm.total, limit);
175 i915_gem_ww_ctx_init(&ww, false);
177 err = i915_vm_lock_objects(&ppgtt->vm, &ww);
179 goto err_ppgtt_cleanup;
181 /* Check we can allocate the entire range */
182 for (size = 4096; size <= limit; size <<= 2) {
183 struct i915_vm_pt_stash stash = {};
185 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, size);
187 goto err_ppgtt_cleanup;
189 err = i915_vm_pin_pt_stash(&ppgtt->vm, &stash);
191 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
192 goto err_ppgtt_cleanup;
195 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, size);
198 ppgtt->vm.clear_range(&ppgtt->vm, 0, size);
200 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
203 /* Check we can incrementally allocate the entire range */
204 for (last = 0, size = 4096; size <= limit; last = size, size <<= 2) {
205 struct i915_vm_pt_stash stash = {};
207 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, size - last);
209 goto err_ppgtt_cleanup;
211 err = i915_vm_pin_pt_stash(&ppgtt->vm, &stash);
213 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
214 goto err_ppgtt_cleanup;
217 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash,
221 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
225 if (err == -EDEADLK) {
226 err = i915_gem_ww_ctx_backoff(&ww);
230 i915_gem_ww_ctx_fini(&ww);
232 i915_vm_put(&ppgtt->vm);
236 static int lowlevel_hole(struct i915_address_space *vm,
237 u64 hole_start, u64 hole_end,
238 unsigned long end_time)
240 I915_RND_STATE(seed_prng);
241 struct i915_vma *mock_vma;
244 mock_vma = kzalloc(sizeof(*mock_vma), GFP_KERNEL);
248 /* Keep creating larger objects until one cannot fit into the hole */
249 for (size = 12; (hole_end - hole_start) >> size; size++) {
250 I915_RND_SUBSTATE(prng, seed_prng);
251 struct drm_i915_gem_object *obj;
252 unsigned int *order, count, n;
255 hole_size = (hole_end - hole_start) >> size;
256 if (hole_size > KMALLOC_MAX_SIZE / sizeof(u32))
257 hole_size = KMALLOC_MAX_SIZE / sizeof(u32);
258 count = hole_size >> 1;
260 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
261 __func__, hole_start, hole_end, size, hole_size);
266 order = i915_random_order(count, &prng);
269 } while (count >>= 1);
276 GEM_BUG_ON(count * BIT_ULL(size) > vm->total);
277 GEM_BUG_ON(hole_start + count * BIT_ULL(size) > hole_end);
279 /* Ignore allocation failures (i.e. don't report them as
280 * a test failure) as we are purposefully allocating very
281 * large objects without checking that we have sufficient
282 * memory. We expect to hit -ENOMEM.
285 obj = fake_dma_object(vm->i915, BIT_ULL(size));
291 GEM_BUG_ON(obj->base.size != BIT_ULL(size));
293 if (i915_gem_object_pin_pages_unlocked(obj)) {
294 i915_gem_object_put(obj);
299 for (n = 0; n < count; n++) {
300 u64 addr = hole_start + order[n] * BIT_ULL(size);
301 intel_wakeref_t wakeref;
303 GEM_BUG_ON(addr + BIT_ULL(size) > vm->total);
305 if (igt_timeout(end_time,
306 "%s timed out before %d/%d\n",
307 __func__, n, count)) {
308 hole_end = hole_start; /* quit */
312 if (vm->allocate_va_range) {
313 struct i915_vm_pt_stash stash = {};
314 struct i915_gem_ww_ctx ww;
317 i915_gem_ww_ctx_init(&ww, false);
319 err = i915_vm_lock_objects(vm, &ww);
324 if (i915_vm_alloc_pt_stash(vm, &stash,
328 err = i915_vm_pin_pt_stash(vm, &stash);
330 vm->allocate_va_range(vm, &stash,
331 addr, BIT_ULL(size));
333 i915_vm_free_pt_stash(vm, &stash);
335 if (err == -EDEADLK) {
336 err = i915_gem_ww_ctx_backoff(&ww);
340 i915_gem_ww_ctx_fini(&ww);
346 mock_vma->pages = obj->mm.pages;
347 mock_vma->node.size = BIT_ULL(size);
348 mock_vma->node.start = addr;
350 with_intel_runtime_pm(vm->gt->uncore->rpm, wakeref)
351 vm->insert_entries(vm, mock_vma,
356 i915_random_reorder(order, count, &prng);
357 for (n = 0; n < count; n++) {
358 u64 addr = hole_start + order[n] * BIT_ULL(size);
359 intel_wakeref_t wakeref;
361 GEM_BUG_ON(addr + BIT_ULL(size) > vm->total);
362 with_intel_runtime_pm(vm->gt->uncore->rpm, wakeref)
363 vm->clear_range(vm, addr, BIT_ULL(size));
366 i915_gem_object_unpin_pages(obj);
367 i915_gem_object_put(obj);
371 cleanup_freed_objects(vm->i915);
378 static void close_object_list(struct list_head *objects,
379 struct i915_address_space *vm)
381 struct drm_i915_gem_object *obj, *on;
384 list_for_each_entry_safe(obj, on, objects, st_link) {
385 struct i915_vma *vma;
387 vma = i915_vma_instance(obj, vm, NULL);
389 ignored = i915_vma_unbind(vma);
391 list_del(&obj->st_link);
392 i915_gem_object_put(obj);
396 static int fill_hole(struct i915_address_space *vm,
397 u64 hole_start, u64 hole_end,
398 unsigned long end_time)
400 const u64 hole_size = hole_end - hole_start;
401 struct drm_i915_gem_object *obj;
402 const unsigned long max_pages =
403 min_t(u64, ULONG_MAX - 1, hole_size/2 >> PAGE_SHIFT);
404 const unsigned long max_step = max(int_sqrt(max_pages), 2UL);
405 unsigned long npages, prime, flags;
406 struct i915_vma *vma;
410 /* Try binding many VMA working inwards from either edge */
412 flags = PIN_OFFSET_FIXED | PIN_USER;
413 if (i915_is_ggtt(vm))
416 for_each_prime_number_from(prime, 2, max_step) {
417 for (npages = 1; npages <= max_pages; npages *= prime) {
418 const u64 full_size = npages << PAGE_SHIFT;
424 { "top-down", hole_end, -1, },
425 { "bottom-up", hole_start, 1, },
429 obj = fake_dma_object(vm->i915, full_size);
433 list_add(&obj->st_link, &objects);
435 /* Align differing sized objects against the edges, and
436 * check we don't walk off into the void when binding
439 for (p = phases; p->name; p++) {
443 list_for_each_entry(obj, &objects, st_link) {
444 vma = i915_vma_instance(obj, vm, NULL);
449 if (offset < hole_start + obj->base.size)
451 offset -= obj->base.size;
454 err = i915_vma_pin(vma, 0, 0, offset | flags);
456 pr_err("%s(%s) pin (forward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
457 __func__, p->name, err, npages, prime, offset);
461 if (!drm_mm_node_allocated(&vma->node) ||
462 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
463 pr_err("%s(%s) (forward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
464 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
473 if (offset + obj->base.size > hole_end)
475 offset += obj->base.size;
480 list_for_each_entry(obj, &objects, st_link) {
481 vma = i915_vma_instance(obj, vm, NULL);
486 if (offset < hole_start + obj->base.size)
488 offset -= obj->base.size;
491 if (!drm_mm_node_allocated(&vma->node) ||
492 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
493 pr_err("%s(%s) (forward) moved vma.node=%llx + %llx, expected offset %llx\n",
494 __func__, p->name, vma->node.start, vma->node.size,
500 err = i915_vma_unbind(vma);
502 pr_err("%s(%s) (forward) unbind of vma.node=%llx + %llx failed with err=%d\n",
503 __func__, p->name, vma->node.start, vma->node.size,
509 if (offset + obj->base.size > hole_end)
511 offset += obj->base.size;
516 list_for_each_entry_reverse(obj, &objects, st_link) {
517 vma = i915_vma_instance(obj, vm, NULL);
522 if (offset < hole_start + obj->base.size)
524 offset -= obj->base.size;
527 err = i915_vma_pin(vma, 0, 0, offset | flags);
529 pr_err("%s(%s) pin (backward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
530 __func__, p->name, err, npages, prime, offset);
534 if (!drm_mm_node_allocated(&vma->node) ||
535 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
536 pr_err("%s(%s) (backward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
537 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
546 if (offset + obj->base.size > hole_end)
548 offset += obj->base.size;
553 list_for_each_entry_reverse(obj, &objects, st_link) {
554 vma = i915_vma_instance(obj, vm, NULL);
559 if (offset < hole_start + obj->base.size)
561 offset -= obj->base.size;
564 if (!drm_mm_node_allocated(&vma->node) ||
565 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
566 pr_err("%s(%s) (backward) moved vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
567 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
573 err = i915_vma_unbind(vma);
575 pr_err("%s(%s) (backward) unbind of vma.node=%llx + %llx failed with err=%d\n",
576 __func__, p->name, vma->node.start, vma->node.size,
582 if (offset + obj->base.size > hole_end)
584 offset += obj->base.size;
589 if (igt_timeout(end_time, "%s timed out (npages=%lu, prime=%lu)\n",
590 __func__, npages, prime)) {
596 close_object_list(&objects, vm);
597 cleanup_freed_objects(vm->i915);
603 close_object_list(&objects, vm);
607 static int walk_hole(struct i915_address_space *vm,
608 u64 hole_start, u64 hole_end,
609 unsigned long end_time)
611 const u64 hole_size = hole_end - hole_start;
612 const unsigned long max_pages =
613 min_t(u64, ULONG_MAX - 1, hole_size >> PAGE_SHIFT);
617 /* Try binding a single VMA in different positions within the hole */
619 flags = PIN_OFFSET_FIXED | PIN_USER;
620 if (i915_is_ggtt(vm))
623 for_each_prime_number_from(size, 1, max_pages) {
624 struct drm_i915_gem_object *obj;
625 struct i915_vma *vma;
629 obj = fake_dma_object(vm->i915, size << PAGE_SHIFT);
633 vma = i915_vma_instance(obj, vm, NULL);
639 for (addr = hole_start;
640 addr + obj->base.size < hole_end;
641 addr += obj->base.size) {
642 err = i915_vma_pin(vma, 0, 0, addr | flags);
644 pr_err("%s bind failed at %llx + %llx [hole %llx- %llx] with err=%d\n",
645 __func__, addr, vma->size,
646 hole_start, hole_end, err);
651 if (!drm_mm_node_allocated(&vma->node) ||
652 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
653 pr_err("%s incorrect at %llx + %llx\n",
654 __func__, addr, vma->size);
659 err = i915_vma_unbind(vma);
661 pr_err("%s unbind failed at %llx + %llx with err=%d\n",
662 __func__, addr, vma->size, err);
666 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
668 if (igt_timeout(end_time,
669 "%s timed out at %llx\n",
677 i915_gem_object_put(obj);
681 cleanup_freed_objects(vm->i915);
687 static int pot_hole(struct i915_address_space *vm,
688 u64 hole_start, u64 hole_end,
689 unsigned long end_time)
691 struct drm_i915_gem_object *obj;
692 struct i915_vma *vma;
697 flags = PIN_OFFSET_FIXED | PIN_USER;
698 if (i915_is_ggtt(vm))
701 obj = i915_gem_object_create_internal(vm->i915, 2 * I915_GTT_PAGE_SIZE);
705 vma = i915_vma_instance(obj, vm, NULL);
711 /* Insert a pair of pages across every pot boundary within the hole */
712 for (pot = fls64(hole_end - 1) - 1;
713 pot > ilog2(2 * I915_GTT_PAGE_SIZE);
715 u64 step = BIT_ULL(pot);
718 for (addr = round_up(hole_start + I915_GTT_PAGE_SIZE, step) - I915_GTT_PAGE_SIZE;
719 addr <= round_down(hole_end - 2*I915_GTT_PAGE_SIZE, step) - I915_GTT_PAGE_SIZE;
721 err = i915_vma_pin(vma, 0, 0, addr | flags);
723 pr_err("%s failed to pin object at %llx in hole [%llx - %llx], with err=%d\n",
726 hole_start, hole_end,
731 if (!drm_mm_node_allocated(&vma->node) ||
732 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
733 pr_err("%s incorrect at %llx + %llx\n",
734 __func__, addr, vma->size);
736 err = i915_vma_unbind(vma);
742 err = i915_vma_unbind(vma);
746 if (igt_timeout(end_time,
747 "%s timed out after %d/%d\n",
748 __func__, pot, fls64(hole_end - 1) - 1)) {
755 i915_gem_object_put(obj);
759 static int drunk_hole(struct i915_address_space *vm,
760 u64 hole_start, u64 hole_end,
761 unsigned long end_time)
763 I915_RND_STATE(prng);
767 flags = PIN_OFFSET_FIXED | PIN_USER;
768 if (i915_is_ggtt(vm))
771 /* Keep creating larger objects until one cannot fit into the hole */
772 for (size = 12; (hole_end - hole_start) >> size; size++) {
773 struct drm_i915_gem_object *obj;
774 unsigned int *order, count, n;
775 struct i915_vma *vma;
779 hole_size = (hole_end - hole_start) >> size;
780 if (hole_size > KMALLOC_MAX_SIZE / sizeof(u32))
781 hole_size = KMALLOC_MAX_SIZE / sizeof(u32);
782 count = hole_size >> 1;
784 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
785 __func__, hole_start, hole_end, size, hole_size);
790 order = i915_random_order(count, &prng);
793 } while (count >>= 1);
798 /* Ignore allocation failures (i.e. don't report them as
799 * a test failure) as we are purposefully allocating very
800 * large objects without checking that we have sufficient
801 * memory. We expect to hit -ENOMEM.
804 obj = fake_dma_object(vm->i915, BIT_ULL(size));
810 vma = i915_vma_instance(obj, vm, NULL);
816 GEM_BUG_ON(vma->size != BIT_ULL(size));
818 for (n = 0; n < count; n++) {
819 u64 addr = hole_start + order[n] * BIT_ULL(size);
821 err = i915_vma_pin(vma, 0, 0, addr | flags);
823 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
826 hole_start, hole_end,
831 if (!drm_mm_node_allocated(&vma->node) ||
832 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
833 pr_err("%s incorrect at %llx + %llx\n",
834 __func__, addr, BIT_ULL(size));
836 err = i915_vma_unbind(vma);
842 err = i915_vma_unbind(vma);
845 if (igt_timeout(end_time,
846 "%s timed out after %d/%d\n",
847 __func__, n, count)) {
854 i915_gem_object_put(obj);
859 cleanup_freed_objects(vm->i915);
865 static int __shrink_hole(struct i915_address_space *vm,
866 u64 hole_start, u64 hole_end,
867 unsigned long end_time)
869 struct drm_i915_gem_object *obj;
870 unsigned long flags = PIN_OFFSET_FIXED | PIN_USER;
871 unsigned int order = 12;
876 /* Keep creating larger objects until one cannot fit into the hole */
877 for (addr = hole_start; addr < hole_end; ) {
878 struct i915_vma *vma;
879 u64 size = BIT_ULL(order++);
881 size = min(size, hole_end - addr);
882 obj = fake_dma_object(vm->i915, size);
888 list_add(&obj->st_link, &objects);
890 vma = i915_vma_instance(obj, vm, NULL);
896 GEM_BUG_ON(vma->size != size);
898 err = i915_vma_pin(vma, 0, 0, addr | flags);
900 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
901 __func__, addr, size, hole_start, hole_end, err);
905 if (!drm_mm_node_allocated(&vma->node) ||
906 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
907 pr_err("%s incorrect at %llx + %llx\n",
908 __func__, addr, size);
910 err = i915_vma_unbind(vma);
919 * Since we are injecting allocation faults at random intervals,
920 * wait for this allocation to complete before we change the
923 err = i915_vma_sync(vma);
927 if (igt_timeout(end_time,
928 "%s timed out at ofset %llx [%llx - %llx]\n",
929 __func__, addr, hole_start, hole_end)) {
935 close_object_list(&objects, vm);
936 cleanup_freed_objects(vm->i915);
940 static int shrink_hole(struct i915_address_space *vm,
941 u64 hole_start, u64 hole_end,
942 unsigned long end_time)
947 vm->fault_attr.probability = 999;
948 atomic_set(&vm->fault_attr.times, -1);
950 for_each_prime_number_from(prime, 0, ULONG_MAX - 1) {
951 vm->fault_attr.interval = prime;
952 err = __shrink_hole(vm, hole_start, hole_end, end_time);
957 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
962 static int shrink_boom(struct i915_address_space *vm,
963 u64 hole_start, u64 hole_end,
964 unsigned long end_time)
966 unsigned int sizes[] = { SZ_2M, SZ_1G };
967 struct drm_i915_gem_object *purge;
968 struct drm_i915_gem_object *explode;
973 * Catch the case which shrink_hole seems to miss. The setup here
974 * requires invoking the shrinker as we do the alloc_pt/alloc_pd, while
975 * ensuring that all vma assiocated with the respective pd/pdp are
976 * unpinned at the time.
979 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
980 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
981 unsigned int size = sizes[i];
982 struct i915_vma *vma;
984 purge = fake_dma_object(vm->i915, size);
986 return PTR_ERR(purge);
988 vma = i915_vma_instance(purge, vm, NULL);
994 err = i915_vma_pin(vma, 0, 0, flags);
998 /* Should now be ripe for purging */
1001 explode = fake_dma_object(vm->i915, size);
1002 if (IS_ERR(explode)) {
1003 err = PTR_ERR(explode);
1007 vm->fault_attr.probability = 100;
1008 vm->fault_attr.interval = 1;
1009 atomic_set(&vm->fault_attr.times, -1);
1011 vma = i915_vma_instance(explode, vm, NULL);
1017 err = i915_vma_pin(vma, 0, 0, flags | size);
1021 i915_vma_unpin(vma);
1023 i915_gem_object_put(purge);
1024 i915_gem_object_put(explode);
1026 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
1027 cleanup_freed_objects(vm->i915);
1033 i915_gem_object_put(explode);
1035 i915_gem_object_put(purge);
1036 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
1040 static int exercise_ppgtt(struct drm_i915_private *dev_priv,
1041 int (*func)(struct i915_address_space *vm,
1042 u64 hole_start, u64 hole_end,
1043 unsigned long end_time))
1045 struct i915_ppgtt *ppgtt;
1046 IGT_TIMEOUT(end_time);
1050 if (!HAS_FULL_PPGTT(dev_priv))
1053 file = mock_file(dev_priv);
1055 return PTR_ERR(file);
1057 ppgtt = i915_ppgtt_create(&dev_priv->gt);
1058 if (IS_ERR(ppgtt)) {
1059 err = PTR_ERR(ppgtt);
1062 GEM_BUG_ON(offset_in_page(ppgtt->vm.total));
1063 GEM_BUG_ON(!atomic_read(&ppgtt->vm.open));
1065 err = func(&ppgtt->vm, 0, ppgtt->vm.total, end_time);
1067 i915_vm_put(&ppgtt->vm);
1074 static int igt_ppgtt_fill(void *arg)
1076 return exercise_ppgtt(arg, fill_hole);
1079 static int igt_ppgtt_walk(void *arg)
1081 return exercise_ppgtt(arg, walk_hole);
1084 static int igt_ppgtt_pot(void *arg)
1086 return exercise_ppgtt(arg, pot_hole);
1089 static int igt_ppgtt_drunk(void *arg)
1091 return exercise_ppgtt(arg, drunk_hole);
1094 static int igt_ppgtt_lowlevel(void *arg)
1096 return exercise_ppgtt(arg, lowlevel_hole);
1099 static int igt_ppgtt_shrink(void *arg)
1101 return exercise_ppgtt(arg, shrink_hole);
1104 static int igt_ppgtt_shrink_boom(void *arg)
1106 return exercise_ppgtt(arg, shrink_boom);
1109 static int sort_holes(void *priv, struct list_head *A, struct list_head *B)
1111 struct drm_mm_node *a = list_entry(A, typeof(*a), hole_stack);
1112 struct drm_mm_node *b = list_entry(B, typeof(*b), hole_stack);
1114 if (a->start < b->start)
1120 static int exercise_ggtt(struct drm_i915_private *i915,
1121 int (*func)(struct i915_address_space *vm,
1122 u64 hole_start, u64 hole_end,
1123 unsigned long end_time))
1125 struct i915_ggtt *ggtt = &i915->ggtt;
1126 u64 hole_start, hole_end, last = 0;
1127 struct drm_mm_node *node;
1128 IGT_TIMEOUT(end_time);
1132 list_sort(NULL, &ggtt->vm.mm.hole_stack, sort_holes);
1133 drm_mm_for_each_hole(node, &ggtt->vm.mm, hole_start, hole_end) {
1134 if (hole_start < last)
1137 if (ggtt->vm.mm.color_adjust)
1138 ggtt->vm.mm.color_adjust(node, 0,
1139 &hole_start, &hole_end);
1140 if (hole_start >= hole_end)
1143 err = func(&ggtt->vm, hole_start, hole_end, end_time);
1147 /* As we have manipulated the drm_mm, the list may be corrupt */
1155 static int igt_ggtt_fill(void *arg)
1157 return exercise_ggtt(arg, fill_hole);
1160 static int igt_ggtt_walk(void *arg)
1162 return exercise_ggtt(arg, walk_hole);
1165 static int igt_ggtt_pot(void *arg)
1167 return exercise_ggtt(arg, pot_hole);
1170 static int igt_ggtt_drunk(void *arg)
1172 return exercise_ggtt(arg, drunk_hole);
1175 static int igt_ggtt_lowlevel(void *arg)
1177 return exercise_ggtt(arg, lowlevel_hole);
1180 static int igt_ggtt_page(void *arg)
1182 const unsigned int count = PAGE_SIZE/sizeof(u32);
1183 I915_RND_STATE(prng);
1184 struct drm_i915_private *i915 = arg;
1185 struct i915_ggtt *ggtt = &i915->ggtt;
1186 struct drm_i915_gem_object *obj;
1187 intel_wakeref_t wakeref;
1188 struct drm_mm_node tmp;
1189 unsigned int *order, n;
1192 if (!i915_ggtt_has_aperture(ggtt))
1195 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
1197 return PTR_ERR(obj);
1199 err = i915_gem_object_pin_pages_unlocked(obj);
1203 memset(&tmp, 0, sizeof(tmp));
1204 mutex_lock(&ggtt->vm.mutex);
1205 err = drm_mm_insert_node_in_range(&ggtt->vm.mm, &tmp,
1206 count * PAGE_SIZE, 0,
1207 I915_COLOR_UNEVICTABLE,
1208 0, ggtt->mappable_end,
1210 mutex_unlock(&ggtt->vm.mutex);
1214 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1216 for (n = 0; n < count; n++) {
1217 u64 offset = tmp.start + n * PAGE_SIZE;
1219 ggtt->vm.insert_page(&ggtt->vm,
1220 i915_gem_object_get_dma_address(obj, 0),
1221 offset, I915_CACHE_NONE, 0);
1224 order = i915_random_order(count, &prng);
1230 for (n = 0; n < count; n++) {
1231 u64 offset = tmp.start + order[n] * PAGE_SIZE;
1234 vaddr = io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1235 iowrite32(n, vaddr + n);
1236 io_mapping_unmap_atomic(vaddr);
1238 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
1240 i915_random_reorder(order, count, &prng);
1241 for (n = 0; n < count; n++) {
1242 u64 offset = tmp.start + order[n] * PAGE_SIZE;
1246 vaddr = io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1247 val = ioread32(vaddr + n);
1248 io_mapping_unmap_atomic(vaddr);
1251 pr_err("insert page failed: found %d, expected %d\n",
1260 ggtt->vm.clear_range(&ggtt->vm, tmp.start, tmp.size);
1261 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1262 mutex_lock(&ggtt->vm.mutex);
1263 drm_mm_remove_node(&tmp);
1264 mutex_unlock(&ggtt->vm.mutex);
1266 i915_gem_object_unpin_pages(obj);
1268 i915_gem_object_put(obj);
1272 static void track_vma_bind(struct i915_vma *vma)
1274 struct drm_i915_gem_object *obj = vma->obj;
1276 __i915_gem_object_pin_pages(obj);
1278 GEM_BUG_ON(vma->pages);
1279 atomic_set(&vma->pages_count, I915_VMA_PAGES_ACTIVE);
1280 __i915_gem_object_pin_pages(obj);
1281 vma->pages = obj->mm.pages;
1283 mutex_lock(&vma->vm->mutex);
1284 list_add_tail(&vma->vm_link, &vma->vm->bound_list);
1285 mutex_unlock(&vma->vm->mutex);
1288 static int exercise_mock(struct drm_i915_private *i915,
1289 int (*func)(struct i915_address_space *vm,
1290 u64 hole_start, u64 hole_end,
1291 unsigned long end_time))
1293 const u64 limit = totalram_pages() << PAGE_SHIFT;
1294 struct i915_address_space *vm;
1295 struct i915_gem_context *ctx;
1296 IGT_TIMEOUT(end_time);
1299 ctx = mock_context(i915, "mock");
1303 vm = i915_gem_context_get_vm_rcu(ctx);
1304 err = func(vm, 0, min(vm->total, limit), end_time);
1307 mock_context_close(ctx);
1311 static int igt_mock_fill(void *arg)
1313 struct i915_ggtt *ggtt = arg;
1315 return exercise_mock(ggtt->vm.i915, fill_hole);
1318 static int igt_mock_walk(void *arg)
1320 struct i915_ggtt *ggtt = arg;
1322 return exercise_mock(ggtt->vm.i915, walk_hole);
1325 static int igt_mock_pot(void *arg)
1327 struct i915_ggtt *ggtt = arg;
1329 return exercise_mock(ggtt->vm.i915, pot_hole);
1332 static int igt_mock_drunk(void *arg)
1334 struct i915_ggtt *ggtt = arg;
1336 return exercise_mock(ggtt->vm.i915, drunk_hole);
1339 static int igt_gtt_reserve(void *arg)
1341 struct i915_ggtt *ggtt = arg;
1342 struct drm_i915_gem_object *obj, *on;
1343 I915_RND_STATE(prng);
1348 /* i915_gem_gtt_reserve() tries to reserve the precise range
1349 * for the node, and evicts if it has to. So our test checks that
1350 * it can give us the requsted space and prevent overlaps.
1353 /* Start by filling the GGTT */
1355 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1356 total += 2 * I915_GTT_PAGE_SIZE) {
1357 struct i915_vma *vma;
1359 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1366 err = i915_gem_object_pin_pages_unlocked(obj);
1368 i915_gem_object_put(obj);
1372 list_add(&obj->st_link, &objects);
1374 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1380 mutex_lock(&ggtt->vm.mutex);
1381 err = i915_gem_gtt_reserve(&ggtt->vm, &vma->node,
1386 mutex_unlock(&ggtt->vm.mutex);
1388 pr_err("i915_gem_gtt_reserve (pass 1) failed at %llu/%llu with err=%d\n",
1389 total, ggtt->vm.total, err);
1392 track_vma_bind(vma);
1394 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1395 if (vma->node.start != total ||
1396 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1397 pr_err("i915_gem_gtt_reserve (pass 1) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1398 vma->node.start, vma->node.size,
1399 total, 2*I915_GTT_PAGE_SIZE);
1405 /* Now we start forcing evictions */
1406 for (total = I915_GTT_PAGE_SIZE;
1407 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1408 total += 2 * I915_GTT_PAGE_SIZE) {
1409 struct i915_vma *vma;
1411 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1418 err = i915_gem_object_pin_pages_unlocked(obj);
1420 i915_gem_object_put(obj);
1424 list_add(&obj->st_link, &objects);
1426 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1432 mutex_lock(&ggtt->vm.mutex);
1433 err = i915_gem_gtt_reserve(&ggtt->vm, &vma->node,
1438 mutex_unlock(&ggtt->vm.mutex);
1440 pr_err("i915_gem_gtt_reserve (pass 2) failed at %llu/%llu with err=%d\n",
1441 total, ggtt->vm.total, err);
1444 track_vma_bind(vma);
1446 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1447 if (vma->node.start != total ||
1448 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1449 pr_err("i915_gem_gtt_reserve (pass 2) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1450 vma->node.start, vma->node.size,
1451 total, 2*I915_GTT_PAGE_SIZE);
1457 /* And then try at random */
1458 list_for_each_entry_safe(obj, on, &objects, st_link) {
1459 struct i915_vma *vma;
1462 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1468 err = i915_vma_unbind(vma);
1470 pr_err("i915_vma_unbind failed with err=%d!\n", err);
1474 offset = igt_random_offset(&prng,
1476 2 * I915_GTT_PAGE_SIZE,
1477 I915_GTT_MIN_ALIGNMENT);
1479 mutex_lock(&ggtt->vm.mutex);
1480 err = i915_gem_gtt_reserve(&ggtt->vm, &vma->node,
1485 mutex_unlock(&ggtt->vm.mutex);
1487 pr_err("i915_gem_gtt_reserve (pass 3) failed at %llu/%llu with err=%d\n",
1488 total, ggtt->vm.total, err);
1491 track_vma_bind(vma);
1493 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1494 if (vma->node.start != offset ||
1495 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1496 pr_err("i915_gem_gtt_reserve (pass 3) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1497 vma->node.start, vma->node.size,
1498 offset, 2*I915_GTT_PAGE_SIZE);
1505 list_for_each_entry_safe(obj, on, &objects, st_link) {
1506 i915_gem_object_unpin_pages(obj);
1507 i915_gem_object_put(obj);
1512 static int igt_gtt_insert(void *arg)
1514 struct i915_ggtt *ggtt = arg;
1515 struct drm_i915_gem_object *obj, *on;
1516 struct drm_mm_node tmp = {};
1517 const struct invalid_insert {
1521 } invalid_insert[] = {
1523 ggtt->vm.total + I915_GTT_PAGE_SIZE, 0,
1527 2*I915_GTT_PAGE_SIZE, 0,
1528 0, I915_GTT_PAGE_SIZE,
1531 -(u64)I915_GTT_PAGE_SIZE, 0,
1532 0, 4*I915_GTT_PAGE_SIZE,
1535 -(u64)2*I915_GTT_PAGE_SIZE, 2*I915_GTT_PAGE_SIZE,
1536 0, 4*I915_GTT_PAGE_SIZE,
1539 I915_GTT_PAGE_SIZE, I915_GTT_MIN_ALIGNMENT << 1,
1540 I915_GTT_MIN_ALIGNMENT, I915_GTT_MIN_ALIGNMENT << 1,
1548 /* i915_gem_gtt_insert() tries to allocate some free space in the GTT
1549 * to the node, evicting if required.
1552 /* Check a couple of obviously invalid requests */
1553 for (ii = invalid_insert; ii->size; ii++) {
1554 mutex_lock(&ggtt->vm.mutex);
1555 err = i915_gem_gtt_insert(&ggtt->vm, &tmp,
1556 ii->size, ii->alignment,
1557 I915_COLOR_UNEVICTABLE,
1560 mutex_unlock(&ggtt->vm.mutex);
1561 if (err != -ENOSPC) {
1562 pr_err("Invalid i915_gem_gtt_insert(.size=%llx, .alignment=%llx, .start=%llx, .end=%llx) succeeded (err=%d)\n",
1563 ii->size, ii->alignment, ii->start, ii->end,
1569 /* Start by filling the GGTT */
1571 total + I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1572 total += I915_GTT_PAGE_SIZE) {
1573 struct i915_vma *vma;
1575 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1576 I915_GTT_PAGE_SIZE);
1582 err = i915_gem_object_pin_pages_unlocked(obj);
1584 i915_gem_object_put(obj);
1588 list_add(&obj->st_link, &objects);
1590 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1596 mutex_lock(&ggtt->vm.mutex);
1597 err = i915_gem_gtt_insert(&ggtt->vm, &vma->node,
1598 obj->base.size, 0, obj->cache_level,
1601 mutex_unlock(&ggtt->vm.mutex);
1602 if (err == -ENOSPC) {
1603 /* maxed out the GGTT space */
1604 i915_gem_object_put(obj);
1608 pr_err("i915_gem_gtt_insert (pass 1) failed at %llu/%llu with err=%d\n",
1609 total, ggtt->vm.total, err);
1612 track_vma_bind(vma);
1613 __i915_vma_pin(vma);
1615 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1618 list_for_each_entry(obj, &objects, st_link) {
1619 struct i915_vma *vma;
1621 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1627 if (!drm_mm_node_allocated(&vma->node)) {
1628 pr_err("VMA was unexpectedly evicted!\n");
1633 __i915_vma_unpin(vma);
1636 /* If we then reinsert, we should find the same hole */
1637 list_for_each_entry_safe(obj, on, &objects, st_link) {
1638 struct i915_vma *vma;
1641 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1647 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1648 offset = vma->node.start;
1650 err = i915_vma_unbind(vma);
1652 pr_err("i915_vma_unbind failed with err=%d!\n", err);
1656 mutex_lock(&ggtt->vm.mutex);
1657 err = i915_gem_gtt_insert(&ggtt->vm, &vma->node,
1658 obj->base.size, 0, obj->cache_level,
1661 mutex_unlock(&ggtt->vm.mutex);
1663 pr_err("i915_gem_gtt_insert (pass 2) failed at %llu/%llu with err=%d\n",
1664 total, ggtt->vm.total, err);
1667 track_vma_bind(vma);
1669 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1670 if (vma->node.start != offset) {
1671 pr_err("i915_gem_gtt_insert did not return node to its previous location (the only hole), expected address %llx, found %llx\n",
1672 offset, vma->node.start);
1678 /* And then force evictions */
1680 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1681 total += 2 * I915_GTT_PAGE_SIZE) {
1682 struct i915_vma *vma;
1684 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1685 2 * I915_GTT_PAGE_SIZE);
1691 err = i915_gem_object_pin_pages_unlocked(obj);
1693 i915_gem_object_put(obj);
1697 list_add(&obj->st_link, &objects);
1699 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1705 mutex_lock(&ggtt->vm.mutex);
1706 err = i915_gem_gtt_insert(&ggtt->vm, &vma->node,
1707 obj->base.size, 0, obj->cache_level,
1710 mutex_unlock(&ggtt->vm.mutex);
1712 pr_err("i915_gem_gtt_insert (pass 3) failed at %llu/%llu with err=%d\n",
1713 total, ggtt->vm.total, err);
1716 track_vma_bind(vma);
1718 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1722 list_for_each_entry_safe(obj, on, &objects, st_link) {
1723 i915_gem_object_unpin_pages(obj);
1724 i915_gem_object_put(obj);
1729 int i915_gem_gtt_mock_selftests(void)
1731 static const struct i915_subtest tests[] = {
1732 SUBTEST(igt_mock_drunk),
1733 SUBTEST(igt_mock_walk),
1734 SUBTEST(igt_mock_pot),
1735 SUBTEST(igt_mock_fill),
1736 SUBTEST(igt_gtt_reserve),
1737 SUBTEST(igt_gtt_insert),
1739 struct drm_i915_private *i915;
1740 struct i915_ggtt *ggtt;
1743 i915 = mock_gem_device();
1747 ggtt = kmalloc(sizeof(*ggtt), GFP_KERNEL);
1752 mock_init_ggtt(i915, ggtt);
1754 err = i915_subtests(tests, ggtt);
1756 mock_device_flush(i915);
1757 i915_gem_drain_freed_objects(i915);
1758 mock_fini_ggtt(ggtt);
1761 mock_destroy_device(i915);
1765 static int context_sync(struct intel_context *ce)
1767 struct i915_request *rq;
1770 rq = intel_context_create_request(ce);
1774 i915_request_get(rq);
1775 i915_request_add(rq);
1777 timeout = i915_request_wait(rq, 0, HZ / 5);
1778 i915_request_put(rq);
1780 return timeout < 0 ? -EIO : 0;
1783 static struct i915_request *
1784 submit_batch(struct intel_context *ce, u64 addr)
1786 struct i915_request *rq;
1789 rq = intel_context_create_request(ce);
1794 if (rq->engine->emit_init_breadcrumb) /* detect a hang */
1795 err = rq->engine->emit_init_breadcrumb(rq);
1797 err = rq->engine->emit_bb_start(rq, addr, 0, 0);
1800 i915_request_get(rq);
1801 i915_request_add(rq);
1803 return err ? ERR_PTR(err) : rq;
1806 static u32 *spinner(u32 *batch, int i)
1808 return batch + i * 64 / sizeof(*batch) + 4;
1811 static void end_spin(u32 *batch, int i)
1813 *spinner(batch, i) = MI_BATCH_BUFFER_END;
1817 static int igt_cs_tlb(void *arg)
1819 const unsigned int count = PAGE_SIZE / 64;
1820 const unsigned int chunk_size = count * PAGE_SIZE;
1821 struct drm_i915_private *i915 = arg;
1822 struct drm_i915_gem_object *bbe, *act, *out;
1823 struct i915_gem_engines_iter it;
1824 struct i915_address_space *vm;
1825 struct i915_gem_context *ctx;
1826 struct intel_context *ce;
1827 struct i915_vma *vma;
1828 I915_RND_STATE(prng);
1836 * Our mission here is to fool the hardware to execute something
1837 * from scratch as it has not seen the batch move (due to missing
1838 * the TLB invalidate).
1841 file = mock_file(i915);
1843 return PTR_ERR(file);
1845 ctx = live_context(i915, file);
1851 vm = i915_gem_context_get_vm_rcu(ctx);
1852 if (i915_is_ggtt(vm))
1855 /* Create two pages; dummy we prefill the TLB, and intended */
1856 bbe = i915_gem_object_create_internal(i915, PAGE_SIZE);
1862 batch = i915_gem_object_pin_map_unlocked(bbe, I915_MAP_WC);
1863 if (IS_ERR(batch)) {
1864 err = PTR_ERR(batch);
1867 memset32(batch, MI_BATCH_BUFFER_END, PAGE_SIZE / sizeof(u32));
1868 i915_gem_object_flush_map(bbe);
1869 i915_gem_object_unpin_map(bbe);
1871 act = i915_gem_object_create_internal(i915, PAGE_SIZE);
1877 /* Track the execution of each request by writing into different slot */
1878 batch = i915_gem_object_pin_map_unlocked(act, I915_MAP_WC);
1879 if (IS_ERR(batch)) {
1880 err = PTR_ERR(batch);
1883 for (i = 0; i < count; i++) {
1884 u32 *cs = batch + i * 64 / sizeof(*cs);
1885 u64 addr = (vm->total - PAGE_SIZE) + i * sizeof(u32);
1887 GEM_BUG_ON(INTEL_GEN(i915) < 6);
1888 cs[0] = MI_STORE_DWORD_IMM_GEN4;
1889 if (INTEL_GEN(i915) >= 8) {
1890 cs[1] = lower_32_bits(addr);
1891 cs[2] = upper_32_bits(addr);
1894 cs[5] = MI_BATCH_BUFFER_START_GEN8;
1897 cs[2] = lower_32_bits(addr);
1900 cs[5] = MI_BATCH_BUFFER_START;
1904 out = i915_gem_object_create_internal(i915, PAGE_SIZE);
1909 i915_gem_object_set_cache_coherency(out, I915_CACHING_CACHED);
1911 vma = i915_vma_instance(out, vm, NULL);
1917 err = i915_vma_pin(vma, 0, 0,
1920 (vm->total - PAGE_SIZE));
1923 GEM_BUG_ON(vma->node.start != vm->total - PAGE_SIZE);
1925 result = i915_gem_object_pin_map_unlocked(out, I915_MAP_WB);
1926 if (IS_ERR(result)) {
1927 err = PTR_ERR(result);
1931 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1932 IGT_TIMEOUT(end_time);
1933 unsigned long pass = 0;
1935 if (!intel_engine_can_store_dword(ce->engine))
1938 while (!__igt_timeout(end_time, NULL)) {
1939 struct i915_vm_pt_stash stash = {};
1940 struct i915_request *rq;
1941 struct i915_gem_ww_ctx ww;
1944 offset = igt_random_offset(&prng,
1945 0, vm->total - PAGE_SIZE,
1946 chunk_size, PAGE_SIZE);
1948 memset32(result, STACK_MAGIC, PAGE_SIZE / sizeof(u32));
1950 vma = i915_vma_instance(bbe, vm, NULL);
1956 err = vma->ops->set_pages(vma);
1960 i915_gem_ww_ctx_init(&ww, false);
1962 err = i915_vm_lock_objects(vm, &ww);
1966 err = i915_vm_alloc_pt_stash(vm, &stash, chunk_size);
1970 err = i915_vm_pin_pt_stash(vm, &stash);
1972 vm->allocate_va_range(vm, &stash, offset, chunk_size);
1974 i915_vm_free_pt_stash(vm, &stash);
1976 if (err == -EDEADLK) {
1977 err = i915_gem_ww_ctx_backoff(&ww);
1981 i915_gem_ww_ctx_fini(&ww);
1985 /* Prime the TLB with the dummy pages */
1986 for (i = 0; i < count; i++) {
1987 vma->node.start = offset + i * PAGE_SIZE;
1988 vm->insert_entries(vm, vma, I915_CACHE_NONE, 0);
1990 rq = submit_batch(ce, vma->node.start);
1995 i915_request_put(rq);
1998 vma->ops->clear_pages(vma);
2000 err = context_sync(ce);
2002 pr_err("%s: dummy setup timed out\n",
2007 vma = i915_vma_instance(act, vm, NULL);
2013 err = vma->ops->set_pages(vma);
2017 /* Replace the TLB with target batches */
2018 for (i = 0; i < count; i++) {
2019 struct i915_request *rq;
2020 u32 *cs = batch + i * 64 / sizeof(*cs);
2023 vma->node.start = offset + i * PAGE_SIZE;
2024 vm->insert_entries(vm, vma, I915_CACHE_NONE, 0);
2026 addr = vma->node.start + i * 64;
2028 cs[6] = lower_32_bits(addr);
2029 cs[7] = upper_32_bits(addr);
2032 rq = submit_batch(ce, addr);
2038 /* Wait until the context chain has started */
2040 while (READ_ONCE(result[i]) &&
2041 !i915_request_completed(rq))
2044 end_spin(batch, i - 1);
2047 i915_request_put(rq);
2049 end_spin(batch, count - 1);
2051 vma->ops->clear_pages(vma);
2053 err = context_sync(ce);
2055 pr_err("%s: writes timed out\n",
2060 for (i = 0; i < count; i++) {
2061 if (result[i] != i) {
2062 pr_err("%s: Write lost on pass %lu, at offset %llx, index %d, found %x, expected %x\n",
2063 ce->engine->name, pass,
2064 offset, i, result[i], i);
2070 vm->clear_range(vm, offset, chunk_size);
2075 if (igt_flush_test(i915))
2077 i915_gem_context_unlock_engines(ctx);
2078 i915_gem_object_unpin_map(out);
2080 i915_gem_object_put(out);
2082 i915_gem_object_unpin_map(act);
2084 i915_gem_object_put(act);
2086 i915_gem_object_put(bbe);
2094 int i915_gem_gtt_live_selftests(struct drm_i915_private *i915)
2096 static const struct i915_subtest tests[] = {
2097 SUBTEST(igt_ppgtt_alloc),
2098 SUBTEST(igt_ppgtt_lowlevel),
2099 SUBTEST(igt_ppgtt_drunk),
2100 SUBTEST(igt_ppgtt_walk),
2101 SUBTEST(igt_ppgtt_pot),
2102 SUBTEST(igt_ppgtt_fill),
2103 SUBTEST(igt_ppgtt_shrink),
2104 SUBTEST(igt_ppgtt_shrink_boom),
2105 SUBTEST(igt_ggtt_lowlevel),
2106 SUBTEST(igt_ggtt_drunk),
2107 SUBTEST(igt_ggtt_walk),
2108 SUBTEST(igt_ggtt_pot),
2109 SUBTEST(igt_ggtt_fill),
2110 SUBTEST(igt_ggtt_page),
2111 SUBTEST(igt_cs_tlb),
2114 GEM_BUG_ON(offset_in_page(i915->ggtt.vm.total));
2116 return i915_subtests(tests, i915);