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);
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(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 u64 size, last, limit;
152 /* Allocate a ppggt and try to fill the entire range */
154 if (!HAS_PPGTT(dev_priv))
157 ppgtt = i915_ppgtt_create(&dev_priv->gt);
159 return PTR_ERR(ppgtt);
161 if (!ppgtt->vm.allocate_va_range)
162 goto err_ppgtt_cleanup;
165 * While we only allocate the page tables here and so we could
166 * address a much larger GTT than we could actually fit into
167 * RAM, a practical limit is the amount of physical pages in the system.
168 * This should ensure that we do not run into the oomkiller during
169 * the test and take down the machine wilfully.
171 limit = totalram_pages() << PAGE_SHIFT;
172 limit = min(ppgtt->vm.total, limit);
174 /* Check we can allocate the entire range */
175 for (size = 4096; size <= limit; size <<= 2) {
176 struct i915_vm_pt_stash stash = {};
178 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, size);
180 goto err_ppgtt_cleanup;
182 err = i915_vm_pin_pt_stash(&ppgtt->vm, &stash);
184 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
185 goto err_ppgtt_cleanup;
188 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, size);
191 ppgtt->vm.clear_range(&ppgtt->vm, 0, size);
193 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
196 /* Check we can incrementally allocate the entire range */
197 for (last = 0, size = 4096; size <= limit; last = size, size <<= 2) {
198 struct i915_vm_pt_stash stash = {};
200 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, size - last);
202 goto err_ppgtt_cleanup;
204 err = i915_vm_pin_pt_stash(&ppgtt->vm, &stash);
206 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
207 goto err_ppgtt_cleanup;
210 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash,
214 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
218 i915_vm_put(&ppgtt->vm);
222 static int lowlevel_hole(struct i915_address_space *vm,
223 u64 hole_start, u64 hole_end,
224 unsigned long end_time)
226 I915_RND_STATE(seed_prng);
227 struct i915_vma *mock_vma;
230 mock_vma = kzalloc(sizeof(*mock_vma), GFP_KERNEL);
234 /* Keep creating larger objects until one cannot fit into the hole */
235 for (size = 12; (hole_end - hole_start) >> size; size++) {
236 I915_RND_SUBSTATE(prng, seed_prng);
237 struct drm_i915_gem_object *obj;
238 unsigned int *order, count, n;
241 hole_size = (hole_end - hole_start) >> size;
242 if (hole_size > KMALLOC_MAX_SIZE / sizeof(u32))
243 hole_size = KMALLOC_MAX_SIZE / sizeof(u32);
244 count = hole_size >> 1;
246 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
247 __func__, hole_start, hole_end, size, hole_size);
252 order = i915_random_order(count, &prng);
255 } while (count >>= 1);
262 GEM_BUG_ON(count * BIT_ULL(size) > vm->total);
263 GEM_BUG_ON(hole_start + count * BIT_ULL(size) > hole_end);
265 /* Ignore allocation failures (i.e. don't report them as
266 * a test failure) as we are purposefully allocating very
267 * large objects without checking that we have sufficient
268 * memory. We expect to hit -ENOMEM.
271 obj = fake_dma_object(vm->i915, BIT_ULL(size));
277 GEM_BUG_ON(obj->base.size != BIT_ULL(size));
279 if (i915_gem_object_pin_pages(obj)) {
280 i915_gem_object_put(obj);
285 for (n = 0; n < count; n++) {
286 u64 addr = hole_start + order[n] * BIT_ULL(size);
287 intel_wakeref_t wakeref;
289 GEM_BUG_ON(addr + BIT_ULL(size) > vm->total);
291 if (igt_timeout(end_time,
292 "%s timed out before %d/%d\n",
293 __func__, n, count)) {
294 hole_end = hole_start; /* quit */
298 if (vm->allocate_va_range) {
299 struct i915_vm_pt_stash stash = {};
301 if (i915_vm_alloc_pt_stash(vm, &stash,
305 if (i915_vm_pin_pt_stash(vm, &stash)) {
306 i915_vm_free_pt_stash(vm, &stash);
310 vm->allocate_va_range(vm, &stash,
311 addr, BIT_ULL(size));
313 i915_vm_free_pt_stash(vm, &stash);
316 mock_vma->pages = obj->mm.pages;
317 mock_vma->node.size = BIT_ULL(size);
318 mock_vma->node.start = addr;
320 with_intel_runtime_pm(vm->gt->uncore->rpm, wakeref)
321 vm->insert_entries(vm, mock_vma,
326 i915_random_reorder(order, count, &prng);
327 for (n = 0; n < count; n++) {
328 u64 addr = hole_start + order[n] * BIT_ULL(size);
329 intel_wakeref_t wakeref;
331 GEM_BUG_ON(addr + BIT_ULL(size) > vm->total);
332 with_intel_runtime_pm(vm->gt->uncore->rpm, wakeref)
333 vm->clear_range(vm, addr, BIT_ULL(size));
336 i915_gem_object_unpin_pages(obj);
337 i915_gem_object_put(obj);
341 cleanup_freed_objects(vm->i915);
348 static void close_object_list(struct list_head *objects,
349 struct i915_address_space *vm)
351 struct drm_i915_gem_object *obj, *on;
354 list_for_each_entry_safe(obj, on, objects, st_link) {
355 struct i915_vma *vma;
357 vma = i915_vma_instance(obj, vm, NULL);
359 ignored = i915_vma_unbind(vma);
361 list_del(&obj->st_link);
362 i915_gem_object_put(obj);
366 static int fill_hole(struct i915_address_space *vm,
367 u64 hole_start, u64 hole_end,
368 unsigned long end_time)
370 const u64 hole_size = hole_end - hole_start;
371 struct drm_i915_gem_object *obj;
372 const unsigned long max_pages =
373 min_t(u64, ULONG_MAX - 1, hole_size/2 >> PAGE_SHIFT);
374 const unsigned long max_step = max(int_sqrt(max_pages), 2UL);
375 unsigned long npages, prime, flags;
376 struct i915_vma *vma;
380 /* Try binding many VMA working inwards from either edge */
382 flags = PIN_OFFSET_FIXED | PIN_USER;
383 if (i915_is_ggtt(vm))
386 for_each_prime_number_from(prime, 2, max_step) {
387 for (npages = 1; npages <= max_pages; npages *= prime) {
388 const u64 full_size = npages << PAGE_SHIFT;
394 { "top-down", hole_end, -1, },
395 { "bottom-up", hole_start, 1, },
399 obj = fake_dma_object(vm->i915, full_size);
403 list_add(&obj->st_link, &objects);
405 /* Align differing sized objects against the edges, and
406 * check we don't walk off into the void when binding
409 for (p = phases; p->name; p++) {
413 list_for_each_entry(obj, &objects, st_link) {
414 vma = i915_vma_instance(obj, vm, NULL);
419 if (offset < hole_start + obj->base.size)
421 offset -= obj->base.size;
424 err = i915_vma_pin(vma, 0, 0, offset | flags);
426 pr_err("%s(%s) pin (forward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
427 __func__, p->name, err, npages, prime, offset);
431 if (!drm_mm_node_allocated(&vma->node) ||
432 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
433 pr_err("%s(%s) (forward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
434 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
443 if (offset + obj->base.size > hole_end)
445 offset += obj->base.size;
450 list_for_each_entry(obj, &objects, st_link) {
451 vma = i915_vma_instance(obj, vm, NULL);
456 if (offset < hole_start + obj->base.size)
458 offset -= obj->base.size;
461 if (!drm_mm_node_allocated(&vma->node) ||
462 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
463 pr_err("%s(%s) (forward) moved vma.node=%llx + %llx, expected offset %llx\n",
464 __func__, p->name, vma->node.start, vma->node.size,
470 err = i915_vma_unbind(vma);
472 pr_err("%s(%s) (forward) unbind of vma.node=%llx + %llx failed with err=%d\n",
473 __func__, p->name, vma->node.start, vma->node.size,
479 if (offset + obj->base.size > hole_end)
481 offset += obj->base.size;
486 list_for_each_entry_reverse(obj, &objects, st_link) {
487 vma = i915_vma_instance(obj, vm, NULL);
492 if (offset < hole_start + obj->base.size)
494 offset -= obj->base.size;
497 err = i915_vma_pin(vma, 0, 0, offset | flags);
499 pr_err("%s(%s) pin (backward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
500 __func__, p->name, err, npages, prime, offset);
504 if (!drm_mm_node_allocated(&vma->node) ||
505 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
506 pr_err("%s(%s) (backward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
507 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
516 if (offset + obj->base.size > hole_end)
518 offset += obj->base.size;
523 list_for_each_entry_reverse(obj, &objects, st_link) {
524 vma = i915_vma_instance(obj, vm, NULL);
529 if (offset < hole_start + obj->base.size)
531 offset -= obj->base.size;
534 if (!drm_mm_node_allocated(&vma->node) ||
535 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
536 pr_err("%s(%s) (backward) moved 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),
543 err = i915_vma_unbind(vma);
545 pr_err("%s(%s) (backward) unbind of vma.node=%llx + %llx failed with err=%d\n",
546 __func__, p->name, vma->node.start, vma->node.size,
552 if (offset + obj->base.size > hole_end)
554 offset += obj->base.size;
559 if (igt_timeout(end_time, "%s timed out (npages=%lu, prime=%lu)\n",
560 __func__, npages, prime)) {
566 close_object_list(&objects, vm);
567 cleanup_freed_objects(vm->i915);
573 close_object_list(&objects, vm);
577 static int walk_hole(struct i915_address_space *vm,
578 u64 hole_start, u64 hole_end,
579 unsigned long end_time)
581 const u64 hole_size = hole_end - hole_start;
582 const unsigned long max_pages =
583 min_t(u64, ULONG_MAX - 1, hole_size >> PAGE_SHIFT);
587 /* Try binding a single VMA in different positions within the hole */
589 flags = PIN_OFFSET_FIXED | PIN_USER;
590 if (i915_is_ggtt(vm))
593 for_each_prime_number_from(size, 1, max_pages) {
594 struct drm_i915_gem_object *obj;
595 struct i915_vma *vma;
599 obj = fake_dma_object(vm->i915, size << PAGE_SHIFT);
603 vma = i915_vma_instance(obj, vm, NULL);
609 for (addr = hole_start;
610 addr + obj->base.size < hole_end;
611 addr += obj->base.size) {
612 err = i915_vma_pin(vma, 0, 0, addr | flags);
614 pr_err("%s bind failed at %llx + %llx [hole %llx- %llx] with err=%d\n",
615 __func__, addr, vma->size,
616 hole_start, hole_end, err);
621 if (!drm_mm_node_allocated(&vma->node) ||
622 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
623 pr_err("%s incorrect at %llx + %llx\n",
624 __func__, addr, vma->size);
629 err = i915_vma_unbind(vma);
631 pr_err("%s unbind failed at %llx + %llx with err=%d\n",
632 __func__, addr, vma->size, err);
636 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
638 if (igt_timeout(end_time,
639 "%s timed out at %llx\n",
647 i915_gem_object_put(obj);
651 cleanup_freed_objects(vm->i915);
657 static int pot_hole(struct i915_address_space *vm,
658 u64 hole_start, u64 hole_end,
659 unsigned long end_time)
661 struct drm_i915_gem_object *obj;
662 struct i915_vma *vma;
667 flags = PIN_OFFSET_FIXED | PIN_USER;
668 if (i915_is_ggtt(vm))
671 obj = i915_gem_object_create_internal(vm->i915, 2 * I915_GTT_PAGE_SIZE);
675 vma = i915_vma_instance(obj, vm, NULL);
681 /* Insert a pair of pages across every pot boundary within the hole */
682 for (pot = fls64(hole_end - 1) - 1;
683 pot > ilog2(2 * I915_GTT_PAGE_SIZE);
685 u64 step = BIT_ULL(pot);
688 for (addr = round_up(hole_start + I915_GTT_PAGE_SIZE, step) - I915_GTT_PAGE_SIZE;
689 addr <= round_down(hole_end - 2*I915_GTT_PAGE_SIZE, step) - I915_GTT_PAGE_SIZE;
691 err = i915_vma_pin(vma, 0, 0, addr | flags);
693 pr_err("%s failed to pin object at %llx in hole [%llx - %llx], with err=%d\n",
696 hole_start, hole_end,
701 if (!drm_mm_node_allocated(&vma->node) ||
702 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
703 pr_err("%s incorrect at %llx + %llx\n",
704 __func__, addr, vma->size);
706 err = i915_vma_unbind(vma);
712 err = i915_vma_unbind(vma);
716 if (igt_timeout(end_time,
717 "%s timed out after %d/%d\n",
718 __func__, pot, fls64(hole_end - 1) - 1)) {
725 i915_gem_object_put(obj);
729 static int drunk_hole(struct i915_address_space *vm,
730 u64 hole_start, u64 hole_end,
731 unsigned long end_time)
733 I915_RND_STATE(prng);
737 flags = PIN_OFFSET_FIXED | PIN_USER;
738 if (i915_is_ggtt(vm))
741 /* Keep creating larger objects until one cannot fit into the hole */
742 for (size = 12; (hole_end - hole_start) >> size; size++) {
743 struct drm_i915_gem_object *obj;
744 unsigned int *order, count, n;
745 struct i915_vma *vma;
749 hole_size = (hole_end - hole_start) >> size;
750 if (hole_size > KMALLOC_MAX_SIZE / sizeof(u32))
751 hole_size = KMALLOC_MAX_SIZE / sizeof(u32);
752 count = hole_size >> 1;
754 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
755 __func__, hole_start, hole_end, size, hole_size);
760 order = i915_random_order(count, &prng);
763 } while (count >>= 1);
768 /* Ignore allocation failures (i.e. don't report them as
769 * a test failure) as we are purposefully allocating very
770 * large objects without checking that we have sufficient
771 * memory. We expect to hit -ENOMEM.
774 obj = fake_dma_object(vm->i915, BIT_ULL(size));
780 vma = i915_vma_instance(obj, vm, NULL);
786 GEM_BUG_ON(vma->size != BIT_ULL(size));
788 for (n = 0; n < count; n++) {
789 u64 addr = hole_start + order[n] * BIT_ULL(size);
791 err = i915_vma_pin(vma, 0, 0, addr | flags);
793 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
796 hole_start, hole_end,
801 if (!drm_mm_node_allocated(&vma->node) ||
802 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
803 pr_err("%s incorrect at %llx + %llx\n",
804 __func__, addr, BIT_ULL(size));
806 err = i915_vma_unbind(vma);
812 err = i915_vma_unbind(vma);
815 if (igt_timeout(end_time,
816 "%s timed out after %d/%d\n",
817 __func__, n, count)) {
824 i915_gem_object_put(obj);
829 cleanup_freed_objects(vm->i915);
835 static int __shrink_hole(struct i915_address_space *vm,
836 u64 hole_start, u64 hole_end,
837 unsigned long end_time)
839 struct drm_i915_gem_object *obj;
840 unsigned long flags = PIN_OFFSET_FIXED | PIN_USER;
841 unsigned int order = 12;
846 /* Keep creating larger objects until one cannot fit into the hole */
847 for (addr = hole_start; addr < hole_end; ) {
848 struct i915_vma *vma;
849 u64 size = BIT_ULL(order++);
851 size = min(size, hole_end - addr);
852 obj = fake_dma_object(vm->i915, size);
858 list_add(&obj->st_link, &objects);
860 vma = i915_vma_instance(obj, vm, NULL);
866 GEM_BUG_ON(vma->size != size);
868 err = i915_vma_pin(vma, 0, 0, addr | flags);
870 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
871 __func__, addr, size, hole_start, hole_end, err);
875 if (!drm_mm_node_allocated(&vma->node) ||
876 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
877 pr_err("%s incorrect at %llx + %llx\n",
878 __func__, addr, size);
880 err = i915_vma_unbind(vma);
889 * Since we are injecting allocation faults at random intervals,
890 * wait for this allocation to complete before we change the
893 err = i915_vma_sync(vma);
897 if (igt_timeout(end_time,
898 "%s timed out at ofset %llx [%llx - %llx]\n",
899 __func__, addr, hole_start, hole_end)) {
905 close_object_list(&objects, vm);
906 cleanup_freed_objects(vm->i915);
910 static int shrink_hole(struct i915_address_space *vm,
911 u64 hole_start, u64 hole_end,
912 unsigned long end_time)
917 vm->fault_attr.probability = 999;
918 atomic_set(&vm->fault_attr.times, -1);
920 for_each_prime_number_from(prime, 0, ULONG_MAX - 1) {
921 vm->fault_attr.interval = prime;
922 err = __shrink_hole(vm, hole_start, hole_end, end_time);
927 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
932 static int shrink_boom(struct i915_address_space *vm,
933 u64 hole_start, u64 hole_end,
934 unsigned long end_time)
936 unsigned int sizes[] = { SZ_2M, SZ_1G };
937 struct drm_i915_gem_object *purge;
938 struct drm_i915_gem_object *explode;
943 * Catch the case which shrink_hole seems to miss. The setup here
944 * requires invoking the shrinker as we do the alloc_pt/alloc_pd, while
945 * ensuring that all vma assiocated with the respective pd/pdp are
946 * unpinned at the time.
949 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
950 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
951 unsigned int size = sizes[i];
952 struct i915_vma *vma;
954 purge = fake_dma_object(vm->i915, size);
956 return PTR_ERR(purge);
958 vma = i915_vma_instance(purge, vm, NULL);
964 err = i915_vma_pin(vma, 0, 0, flags);
968 /* Should now be ripe for purging */
971 explode = fake_dma_object(vm->i915, size);
972 if (IS_ERR(explode)) {
973 err = PTR_ERR(explode);
977 vm->fault_attr.probability = 100;
978 vm->fault_attr.interval = 1;
979 atomic_set(&vm->fault_attr.times, -1);
981 vma = i915_vma_instance(explode, vm, NULL);
987 err = i915_vma_pin(vma, 0, 0, flags | size);
993 i915_gem_object_put(purge);
994 i915_gem_object_put(explode);
996 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
997 cleanup_freed_objects(vm->i915);
1003 i915_gem_object_put(explode);
1005 i915_gem_object_put(purge);
1006 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
1010 static int exercise_ppgtt(struct drm_i915_private *dev_priv,
1011 int (*func)(struct i915_address_space *vm,
1012 u64 hole_start, u64 hole_end,
1013 unsigned long end_time))
1015 struct i915_ppgtt *ppgtt;
1016 IGT_TIMEOUT(end_time);
1020 if (!HAS_FULL_PPGTT(dev_priv))
1023 file = mock_file(dev_priv);
1025 return PTR_ERR(file);
1027 ppgtt = i915_ppgtt_create(&dev_priv->gt);
1028 if (IS_ERR(ppgtt)) {
1029 err = PTR_ERR(ppgtt);
1032 GEM_BUG_ON(offset_in_page(ppgtt->vm.total));
1033 GEM_BUG_ON(!atomic_read(&ppgtt->vm.open));
1035 err = func(&ppgtt->vm, 0, ppgtt->vm.total, end_time);
1037 i915_vm_put(&ppgtt->vm);
1044 static int igt_ppgtt_fill(void *arg)
1046 return exercise_ppgtt(arg, fill_hole);
1049 static int igt_ppgtt_walk(void *arg)
1051 return exercise_ppgtt(arg, walk_hole);
1054 static int igt_ppgtt_pot(void *arg)
1056 return exercise_ppgtt(arg, pot_hole);
1059 static int igt_ppgtt_drunk(void *arg)
1061 return exercise_ppgtt(arg, drunk_hole);
1064 static int igt_ppgtt_lowlevel(void *arg)
1066 return exercise_ppgtt(arg, lowlevel_hole);
1069 static int igt_ppgtt_shrink(void *arg)
1071 return exercise_ppgtt(arg, shrink_hole);
1074 static int igt_ppgtt_shrink_boom(void *arg)
1076 return exercise_ppgtt(arg, shrink_boom);
1079 static int sort_holes(void *priv, struct list_head *A, struct list_head *B)
1081 struct drm_mm_node *a = list_entry(A, typeof(*a), hole_stack);
1082 struct drm_mm_node *b = list_entry(B, typeof(*b), hole_stack);
1084 if (a->start < b->start)
1090 static int exercise_ggtt(struct drm_i915_private *i915,
1091 int (*func)(struct i915_address_space *vm,
1092 u64 hole_start, u64 hole_end,
1093 unsigned long end_time))
1095 struct i915_ggtt *ggtt = &i915->ggtt;
1096 u64 hole_start, hole_end, last = 0;
1097 struct drm_mm_node *node;
1098 IGT_TIMEOUT(end_time);
1102 list_sort(NULL, &ggtt->vm.mm.hole_stack, sort_holes);
1103 drm_mm_for_each_hole(node, &ggtt->vm.mm, hole_start, hole_end) {
1104 if (hole_start < last)
1107 if (ggtt->vm.mm.color_adjust)
1108 ggtt->vm.mm.color_adjust(node, 0,
1109 &hole_start, &hole_end);
1110 if (hole_start >= hole_end)
1113 err = func(&ggtt->vm, hole_start, hole_end, end_time);
1117 /* As we have manipulated the drm_mm, the list may be corrupt */
1125 static int igt_ggtt_fill(void *arg)
1127 return exercise_ggtt(arg, fill_hole);
1130 static int igt_ggtt_walk(void *arg)
1132 return exercise_ggtt(arg, walk_hole);
1135 static int igt_ggtt_pot(void *arg)
1137 return exercise_ggtt(arg, pot_hole);
1140 static int igt_ggtt_drunk(void *arg)
1142 return exercise_ggtt(arg, drunk_hole);
1145 static int igt_ggtt_lowlevel(void *arg)
1147 return exercise_ggtt(arg, lowlevel_hole);
1150 static int igt_ggtt_page(void *arg)
1152 const unsigned int count = PAGE_SIZE/sizeof(u32);
1153 I915_RND_STATE(prng);
1154 struct drm_i915_private *i915 = arg;
1155 struct i915_ggtt *ggtt = &i915->ggtt;
1156 struct drm_i915_gem_object *obj;
1157 intel_wakeref_t wakeref;
1158 struct drm_mm_node tmp;
1159 unsigned int *order, n;
1162 if (!i915_ggtt_has_aperture(ggtt))
1165 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
1167 return PTR_ERR(obj);
1169 err = i915_gem_object_pin_pages(obj);
1173 memset(&tmp, 0, sizeof(tmp));
1174 mutex_lock(&ggtt->vm.mutex);
1175 err = drm_mm_insert_node_in_range(&ggtt->vm.mm, &tmp,
1176 count * PAGE_SIZE, 0,
1177 I915_COLOR_UNEVICTABLE,
1178 0, ggtt->mappable_end,
1180 mutex_unlock(&ggtt->vm.mutex);
1184 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1186 for (n = 0; n < count; n++) {
1187 u64 offset = tmp.start + n * PAGE_SIZE;
1189 ggtt->vm.insert_page(&ggtt->vm,
1190 i915_gem_object_get_dma_address(obj, 0),
1191 offset, I915_CACHE_NONE, 0);
1194 order = i915_random_order(count, &prng);
1200 for (n = 0; n < count; n++) {
1201 u64 offset = tmp.start + order[n] * PAGE_SIZE;
1204 vaddr = io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1205 iowrite32(n, vaddr + n);
1206 io_mapping_unmap_atomic(vaddr);
1208 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
1210 i915_random_reorder(order, count, &prng);
1211 for (n = 0; n < count; n++) {
1212 u64 offset = tmp.start + order[n] * PAGE_SIZE;
1216 vaddr = io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1217 val = ioread32(vaddr + n);
1218 io_mapping_unmap_atomic(vaddr);
1221 pr_err("insert page failed: found %d, expected %d\n",
1230 ggtt->vm.clear_range(&ggtt->vm, tmp.start, tmp.size);
1231 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1232 mutex_lock(&ggtt->vm.mutex);
1233 drm_mm_remove_node(&tmp);
1234 mutex_unlock(&ggtt->vm.mutex);
1236 i915_gem_object_unpin_pages(obj);
1238 i915_gem_object_put(obj);
1242 static void track_vma_bind(struct i915_vma *vma)
1244 struct drm_i915_gem_object *obj = vma->obj;
1246 __i915_gem_object_pin_pages(obj);
1248 GEM_BUG_ON(vma->pages);
1249 atomic_set(&vma->pages_count, I915_VMA_PAGES_ACTIVE);
1250 __i915_gem_object_pin_pages(obj);
1251 vma->pages = obj->mm.pages;
1253 mutex_lock(&vma->vm->mutex);
1254 list_add_tail(&vma->vm_link, &vma->vm->bound_list);
1255 mutex_unlock(&vma->vm->mutex);
1258 static int exercise_mock(struct drm_i915_private *i915,
1259 int (*func)(struct i915_address_space *vm,
1260 u64 hole_start, u64 hole_end,
1261 unsigned long end_time))
1263 const u64 limit = totalram_pages() << PAGE_SHIFT;
1264 struct i915_address_space *vm;
1265 struct i915_gem_context *ctx;
1266 IGT_TIMEOUT(end_time);
1269 ctx = mock_context(i915, "mock");
1273 vm = i915_gem_context_get_vm_rcu(ctx);
1274 err = func(vm, 0, min(vm->total, limit), end_time);
1277 mock_context_close(ctx);
1281 static int igt_mock_fill(void *arg)
1283 struct i915_ggtt *ggtt = arg;
1285 return exercise_mock(ggtt->vm.i915, fill_hole);
1288 static int igt_mock_walk(void *arg)
1290 struct i915_ggtt *ggtt = arg;
1292 return exercise_mock(ggtt->vm.i915, walk_hole);
1295 static int igt_mock_pot(void *arg)
1297 struct i915_ggtt *ggtt = arg;
1299 return exercise_mock(ggtt->vm.i915, pot_hole);
1302 static int igt_mock_drunk(void *arg)
1304 struct i915_ggtt *ggtt = arg;
1306 return exercise_mock(ggtt->vm.i915, drunk_hole);
1309 static int igt_gtt_reserve(void *arg)
1311 struct i915_ggtt *ggtt = arg;
1312 struct drm_i915_gem_object *obj, *on;
1313 I915_RND_STATE(prng);
1318 /* i915_gem_gtt_reserve() tries to reserve the precise range
1319 * for the node, and evicts if it has to. So our test checks that
1320 * it can give us the requsted space and prevent overlaps.
1323 /* Start by filling the GGTT */
1325 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1326 total += 2 * I915_GTT_PAGE_SIZE) {
1327 struct i915_vma *vma;
1329 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1336 err = i915_gem_object_pin_pages(obj);
1338 i915_gem_object_put(obj);
1342 list_add(&obj->st_link, &objects);
1344 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1350 mutex_lock(&ggtt->vm.mutex);
1351 err = i915_gem_gtt_reserve(&ggtt->vm, &vma->node,
1356 mutex_unlock(&ggtt->vm.mutex);
1358 pr_err("i915_gem_gtt_reserve (pass 1) failed at %llu/%llu with err=%d\n",
1359 total, ggtt->vm.total, err);
1362 track_vma_bind(vma);
1364 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1365 if (vma->node.start != total ||
1366 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1367 pr_err("i915_gem_gtt_reserve (pass 1) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1368 vma->node.start, vma->node.size,
1369 total, 2*I915_GTT_PAGE_SIZE);
1375 /* Now we start forcing evictions */
1376 for (total = I915_GTT_PAGE_SIZE;
1377 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1378 total += 2 * I915_GTT_PAGE_SIZE) {
1379 struct i915_vma *vma;
1381 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1388 err = i915_gem_object_pin_pages(obj);
1390 i915_gem_object_put(obj);
1394 list_add(&obj->st_link, &objects);
1396 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1402 mutex_lock(&ggtt->vm.mutex);
1403 err = i915_gem_gtt_reserve(&ggtt->vm, &vma->node,
1408 mutex_unlock(&ggtt->vm.mutex);
1410 pr_err("i915_gem_gtt_reserve (pass 2) failed at %llu/%llu with err=%d\n",
1411 total, ggtt->vm.total, err);
1414 track_vma_bind(vma);
1416 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1417 if (vma->node.start != total ||
1418 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1419 pr_err("i915_gem_gtt_reserve (pass 2) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1420 vma->node.start, vma->node.size,
1421 total, 2*I915_GTT_PAGE_SIZE);
1427 /* And then try at random */
1428 list_for_each_entry_safe(obj, on, &objects, st_link) {
1429 struct i915_vma *vma;
1432 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1438 err = i915_vma_unbind(vma);
1440 pr_err("i915_vma_unbind failed with err=%d!\n", err);
1444 offset = igt_random_offset(&prng,
1446 2 * I915_GTT_PAGE_SIZE,
1447 I915_GTT_MIN_ALIGNMENT);
1449 mutex_lock(&ggtt->vm.mutex);
1450 err = i915_gem_gtt_reserve(&ggtt->vm, &vma->node,
1455 mutex_unlock(&ggtt->vm.mutex);
1457 pr_err("i915_gem_gtt_reserve (pass 3) failed at %llu/%llu with err=%d\n",
1458 total, ggtt->vm.total, err);
1461 track_vma_bind(vma);
1463 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1464 if (vma->node.start != offset ||
1465 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1466 pr_err("i915_gem_gtt_reserve (pass 3) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1467 vma->node.start, vma->node.size,
1468 offset, 2*I915_GTT_PAGE_SIZE);
1475 list_for_each_entry_safe(obj, on, &objects, st_link) {
1476 i915_gem_object_unpin_pages(obj);
1477 i915_gem_object_put(obj);
1482 static int igt_gtt_insert(void *arg)
1484 struct i915_ggtt *ggtt = arg;
1485 struct drm_i915_gem_object *obj, *on;
1486 struct drm_mm_node tmp = {};
1487 const struct invalid_insert {
1491 } invalid_insert[] = {
1493 ggtt->vm.total + I915_GTT_PAGE_SIZE, 0,
1497 2*I915_GTT_PAGE_SIZE, 0,
1498 0, I915_GTT_PAGE_SIZE,
1501 -(u64)I915_GTT_PAGE_SIZE, 0,
1502 0, 4*I915_GTT_PAGE_SIZE,
1505 -(u64)2*I915_GTT_PAGE_SIZE, 2*I915_GTT_PAGE_SIZE,
1506 0, 4*I915_GTT_PAGE_SIZE,
1509 I915_GTT_PAGE_SIZE, I915_GTT_MIN_ALIGNMENT << 1,
1510 I915_GTT_MIN_ALIGNMENT, I915_GTT_MIN_ALIGNMENT << 1,
1518 /* i915_gem_gtt_insert() tries to allocate some free space in the GTT
1519 * to the node, evicting if required.
1522 /* Check a couple of obviously invalid requests */
1523 for (ii = invalid_insert; ii->size; ii++) {
1524 mutex_lock(&ggtt->vm.mutex);
1525 err = i915_gem_gtt_insert(&ggtt->vm, &tmp,
1526 ii->size, ii->alignment,
1527 I915_COLOR_UNEVICTABLE,
1530 mutex_unlock(&ggtt->vm.mutex);
1531 if (err != -ENOSPC) {
1532 pr_err("Invalid i915_gem_gtt_insert(.size=%llx, .alignment=%llx, .start=%llx, .end=%llx) succeeded (err=%d)\n",
1533 ii->size, ii->alignment, ii->start, ii->end,
1539 /* Start by filling the GGTT */
1541 total + I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1542 total += I915_GTT_PAGE_SIZE) {
1543 struct i915_vma *vma;
1545 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1546 I915_GTT_PAGE_SIZE);
1552 err = i915_gem_object_pin_pages(obj);
1554 i915_gem_object_put(obj);
1558 list_add(&obj->st_link, &objects);
1560 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1566 mutex_lock(&ggtt->vm.mutex);
1567 err = i915_gem_gtt_insert(&ggtt->vm, &vma->node,
1568 obj->base.size, 0, obj->cache_level,
1571 mutex_unlock(&ggtt->vm.mutex);
1572 if (err == -ENOSPC) {
1573 /* maxed out the GGTT space */
1574 i915_gem_object_put(obj);
1578 pr_err("i915_gem_gtt_insert (pass 1) failed at %llu/%llu with err=%d\n",
1579 total, ggtt->vm.total, err);
1582 track_vma_bind(vma);
1583 __i915_vma_pin(vma);
1585 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1588 list_for_each_entry(obj, &objects, st_link) {
1589 struct i915_vma *vma;
1591 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1597 if (!drm_mm_node_allocated(&vma->node)) {
1598 pr_err("VMA was unexpectedly evicted!\n");
1603 __i915_vma_unpin(vma);
1606 /* If we then reinsert, we should find the same hole */
1607 list_for_each_entry_safe(obj, on, &objects, st_link) {
1608 struct i915_vma *vma;
1611 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1617 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1618 offset = vma->node.start;
1620 err = i915_vma_unbind(vma);
1622 pr_err("i915_vma_unbind failed with err=%d!\n", err);
1626 mutex_lock(&ggtt->vm.mutex);
1627 err = i915_gem_gtt_insert(&ggtt->vm, &vma->node,
1628 obj->base.size, 0, obj->cache_level,
1631 mutex_unlock(&ggtt->vm.mutex);
1633 pr_err("i915_gem_gtt_insert (pass 2) failed at %llu/%llu with err=%d\n",
1634 total, ggtt->vm.total, err);
1637 track_vma_bind(vma);
1639 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1640 if (vma->node.start != offset) {
1641 pr_err("i915_gem_gtt_insert did not return node to its previous location (the only hole), expected address %llx, found %llx\n",
1642 offset, vma->node.start);
1648 /* And then force evictions */
1650 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1651 total += 2 * I915_GTT_PAGE_SIZE) {
1652 struct i915_vma *vma;
1654 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1655 2 * I915_GTT_PAGE_SIZE);
1661 err = i915_gem_object_pin_pages(obj);
1663 i915_gem_object_put(obj);
1667 list_add(&obj->st_link, &objects);
1669 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1675 mutex_lock(&ggtt->vm.mutex);
1676 err = i915_gem_gtt_insert(&ggtt->vm, &vma->node,
1677 obj->base.size, 0, obj->cache_level,
1680 mutex_unlock(&ggtt->vm.mutex);
1682 pr_err("i915_gem_gtt_insert (pass 3) failed at %llu/%llu with err=%d\n",
1683 total, ggtt->vm.total, err);
1686 track_vma_bind(vma);
1688 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1692 list_for_each_entry_safe(obj, on, &objects, st_link) {
1693 i915_gem_object_unpin_pages(obj);
1694 i915_gem_object_put(obj);
1699 int i915_gem_gtt_mock_selftests(void)
1701 static const struct i915_subtest tests[] = {
1702 SUBTEST(igt_mock_drunk),
1703 SUBTEST(igt_mock_walk),
1704 SUBTEST(igt_mock_pot),
1705 SUBTEST(igt_mock_fill),
1706 SUBTEST(igt_gtt_reserve),
1707 SUBTEST(igt_gtt_insert),
1709 struct drm_i915_private *i915;
1710 struct i915_ggtt *ggtt;
1713 i915 = mock_gem_device();
1717 ggtt = kmalloc(sizeof(*ggtt), GFP_KERNEL);
1722 mock_init_ggtt(i915, ggtt);
1724 err = i915_subtests(tests, ggtt);
1726 mock_device_flush(i915);
1727 i915_gem_drain_freed_objects(i915);
1728 mock_fini_ggtt(ggtt);
1731 mock_destroy_device(i915);
1735 static int context_sync(struct intel_context *ce)
1737 struct i915_request *rq;
1740 rq = intel_context_create_request(ce);
1744 i915_request_get(rq);
1745 i915_request_add(rq);
1747 timeout = i915_request_wait(rq, 0, HZ / 5);
1748 i915_request_put(rq);
1750 return timeout < 0 ? -EIO : 0;
1753 static struct i915_request *
1754 submit_batch(struct intel_context *ce, u64 addr)
1756 struct i915_request *rq;
1759 rq = intel_context_create_request(ce);
1764 if (rq->engine->emit_init_breadcrumb) /* detect a hang */
1765 err = rq->engine->emit_init_breadcrumb(rq);
1767 err = rq->engine->emit_bb_start(rq, addr, 0, 0);
1770 i915_request_get(rq);
1771 i915_request_add(rq);
1773 return err ? ERR_PTR(err) : rq;
1776 static u32 *spinner(u32 *batch, int i)
1778 return batch + i * 64 / sizeof(*batch) + 4;
1781 static void end_spin(u32 *batch, int i)
1783 *spinner(batch, i) = MI_BATCH_BUFFER_END;
1787 static int igt_cs_tlb(void *arg)
1789 const unsigned int count = PAGE_SIZE / 64;
1790 const unsigned int chunk_size = count * PAGE_SIZE;
1791 struct drm_i915_private *i915 = arg;
1792 struct drm_i915_gem_object *bbe, *act, *out;
1793 struct i915_gem_engines_iter it;
1794 struct i915_address_space *vm;
1795 struct i915_gem_context *ctx;
1796 struct intel_context *ce;
1797 struct i915_vma *vma;
1798 I915_RND_STATE(prng);
1806 * Our mission here is to fool the hardware to execute something
1807 * from scratch as it has not seen the batch move (due to missing
1808 * the TLB invalidate).
1811 file = mock_file(i915);
1813 return PTR_ERR(file);
1815 ctx = live_context(i915, file);
1821 vm = i915_gem_context_get_vm_rcu(ctx);
1822 if (i915_is_ggtt(vm))
1825 /* Create two pages; dummy we prefill the TLB, and intended */
1826 bbe = i915_gem_object_create_internal(i915, PAGE_SIZE);
1832 batch = i915_gem_object_pin_map(bbe, I915_MAP_WC);
1833 if (IS_ERR(batch)) {
1834 err = PTR_ERR(batch);
1837 memset32(batch, MI_BATCH_BUFFER_END, PAGE_SIZE / sizeof(u32));
1838 i915_gem_object_flush_map(bbe);
1839 i915_gem_object_unpin_map(bbe);
1841 act = i915_gem_object_create_internal(i915, PAGE_SIZE);
1847 /* Track the execution of each request by writing into different slot */
1848 batch = i915_gem_object_pin_map(act, I915_MAP_WC);
1849 if (IS_ERR(batch)) {
1850 err = PTR_ERR(batch);
1853 for (i = 0; i < count; i++) {
1854 u32 *cs = batch + i * 64 / sizeof(*cs);
1855 u64 addr = (vm->total - PAGE_SIZE) + i * sizeof(u32);
1857 GEM_BUG_ON(INTEL_GEN(i915) < 6);
1858 cs[0] = MI_STORE_DWORD_IMM_GEN4;
1859 if (INTEL_GEN(i915) >= 8) {
1860 cs[1] = lower_32_bits(addr);
1861 cs[2] = upper_32_bits(addr);
1864 cs[5] = MI_BATCH_BUFFER_START_GEN8;
1867 cs[2] = lower_32_bits(addr);
1870 cs[5] = MI_BATCH_BUFFER_START;
1874 out = i915_gem_object_create_internal(i915, PAGE_SIZE);
1879 i915_gem_object_set_cache_coherency(out, I915_CACHING_CACHED);
1881 vma = i915_vma_instance(out, vm, NULL);
1887 err = i915_vma_pin(vma, 0, 0,
1890 (vm->total - PAGE_SIZE));
1893 GEM_BUG_ON(vma->node.start != vm->total - PAGE_SIZE);
1895 result = i915_gem_object_pin_map(out, I915_MAP_WB);
1896 if (IS_ERR(result)) {
1897 err = PTR_ERR(result);
1901 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1902 IGT_TIMEOUT(end_time);
1903 unsigned long pass = 0;
1905 if (!intel_engine_can_store_dword(ce->engine))
1908 while (!__igt_timeout(end_time, NULL)) {
1909 struct i915_vm_pt_stash stash = {};
1910 struct i915_request *rq;
1913 offset = igt_random_offset(&prng,
1914 0, vm->total - PAGE_SIZE,
1915 chunk_size, PAGE_SIZE);
1917 memset32(result, STACK_MAGIC, PAGE_SIZE / sizeof(u32));
1919 vma = i915_vma_instance(bbe, vm, NULL);
1925 err = vma->ops->set_pages(vma);
1929 err = i915_vm_alloc_pt_stash(vm, &stash, chunk_size);
1933 err = i915_vm_pin_pt_stash(vm, &stash);
1935 i915_vm_free_pt_stash(vm, &stash);
1939 vm->allocate_va_range(vm, &stash, offset, chunk_size);
1941 i915_vm_free_pt_stash(vm, &stash);
1943 /* Prime the TLB with the dummy pages */
1944 for (i = 0; i < count; i++) {
1945 vma->node.start = offset + i * PAGE_SIZE;
1946 vm->insert_entries(vm, vma, I915_CACHE_NONE, 0);
1948 rq = submit_batch(ce, vma->node.start);
1953 i915_request_put(rq);
1956 vma->ops->clear_pages(vma);
1958 err = context_sync(ce);
1960 pr_err("%s: dummy setup timed out\n",
1965 vma = i915_vma_instance(act, vm, NULL);
1971 err = vma->ops->set_pages(vma);
1975 /* Replace the TLB with target batches */
1976 for (i = 0; i < count; i++) {
1977 struct i915_request *rq;
1978 u32 *cs = batch + i * 64 / sizeof(*cs);
1981 vma->node.start = offset + i * PAGE_SIZE;
1982 vm->insert_entries(vm, vma, I915_CACHE_NONE, 0);
1984 addr = vma->node.start + i * 64;
1986 cs[6] = lower_32_bits(addr);
1987 cs[7] = upper_32_bits(addr);
1990 rq = submit_batch(ce, addr);
1996 /* Wait until the context chain has started */
1998 while (READ_ONCE(result[i]) &&
1999 !i915_request_completed(rq))
2002 end_spin(batch, i - 1);
2005 i915_request_put(rq);
2007 end_spin(batch, count - 1);
2009 vma->ops->clear_pages(vma);
2011 err = context_sync(ce);
2013 pr_err("%s: writes timed out\n",
2018 for (i = 0; i < count; i++) {
2019 if (result[i] != i) {
2020 pr_err("%s: Write lost on pass %lu, at offset %llx, index %d, found %x, expected %x\n",
2021 ce->engine->name, pass,
2022 offset, i, result[i], i);
2028 vm->clear_range(vm, offset, chunk_size);
2033 if (igt_flush_test(i915))
2035 i915_gem_context_unlock_engines(ctx);
2036 i915_gem_object_unpin_map(out);
2038 i915_gem_object_put(out);
2040 i915_gem_object_unpin_map(act);
2042 i915_gem_object_put(act);
2044 i915_gem_object_put(bbe);
2052 int i915_gem_gtt_live_selftests(struct drm_i915_private *i915)
2054 static const struct i915_subtest tests[] = {
2055 SUBTEST(igt_ppgtt_alloc),
2056 SUBTEST(igt_ppgtt_lowlevel),
2057 SUBTEST(igt_ppgtt_drunk),
2058 SUBTEST(igt_ppgtt_walk),
2059 SUBTEST(igt_ppgtt_pot),
2060 SUBTEST(igt_ppgtt_fill),
2061 SUBTEST(igt_ppgtt_shrink),
2062 SUBTEST(igt_ppgtt_shrink_boom),
2063 SUBTEST(igt_ggtt_lowlevel),
2064 SUBTEST(igt_ggtt_drunk),
2065 SUBTEST(igt_ggtt_walk),
2066 SUBTEST(igt_ggtt_pot),
2067 SUBTEST(igt_ggtt_fill),
2068 SUBTEST(igt_ggtt_page),
2069 SUBTEST(igt_cs_tlb),
2072 GEM_BUG_ON(offset_in_page(i915->ggtt.vm.total));
2074 return i915_subtests(tests, i915);