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/i915_gem_internal.h"
30 #include "gem/i915_gem_region.h"
31 #include "gem/selftests/mock_context.h"
32 #include "gt/intel_context.h"
33 #include "gt/intel_gpu_commands.h"
34 #include "gt/intel_gtt.h"
36 #include "i915_random.h"
37 #include "i915_selftest.h"
38 #include "i915_vma_resource.h"
41 #include "mock_gem_device.h"
43 #include "igt_flush_test.h"
45 static void cleanup_freed_objects(struct drm_i915_private *i915)
47 i915_gem_drain_freed_objects(i915);
50 static void fake_free_pages(struct drm_i915_gem_object *obj,
51 struct sg_table *pages)
57 static int fake_get_pages(struct drm_i915_gem_object *obj)
59 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
60 #define PFN_BIAS 0x1000
61 struct sg_table *pages;
62 struct scatterlist *sg;
63 unsigned int sg_page_sizes;
64 typeof(obj->base.size) rem;
66 pages = kmalloc(sizeof(*pages), GFP);
70 rem = round_up(obj->base.size, BIT(31)) >> 31;
71 if (sg_alloc_table(pages, rem, GFP)) {
78 for (sg = pages->sgl; sg; sg = sg_next(sg)) {
79 unsigned long len = min_t(typeof(rem), rem, BIT(31));
82 sg_set_page(sg, pfn_to_page(PFN_BIAS), len, 0);
83 sg_dma_address(sg) = page_to_phys(sg_page(sg));
91 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
97 static void fake_put_pages(struct drm_i915_gem_object *obj,
98 struct sg_table *pages)
100 fake_free_pages(obj, pages);
101 obj->mm.dirty = false;
104 static const struct drm_i915_gem_object_ops fake_ops = {
106 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
107 .get_pages = fake_get_pages,
108 .put_pages = fake_put_pages,
111 static struct drm_i915_gem_object *
112 fake_dma_object(struct drm_i915_private *i915, u64 size)
114 static struct lock_class_key lock_class;
115 struct drm_i915_gem_object *obj;
118 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
120 if (overflows_type(size, obj->base.size))
121 return ERR_PTR(-E2BIG);
123 obj = i915_gem_object_alloc();
127 drm_gem_private_object_init(&i915->drm, &obj->base, size);
128 i915_gem_object_init(obj, &fake_ops, &lock_class, 0);
130 i915_gem_object_set_volatile(obj);
132 obj->write_domain = I915_GEM_DOMAIN_CPU;
133 obj->read_domains = I915_GEM_DOMAIN_CPU;
134 obj->cache_level = I915_CACHE_NONE;
136 /* Preallocate the "backing storage" */
137 if (i915_gem_object_pin_pages_unlocked(obj))
140 i915_gem_object_unpin_pages(obj);
144 i915_gem_object_put(obj);
146 return ERR_PTR(-ENOMEM);
149 static int igt_ppgtt_alloc(void *arg)
151 struct drm_i915_private *dev_priv = arg;
152 struct i915_ppgtt *ppgtt;
153 struct i915_gem_ww_ctx ww;
154 u64 size, last, limit;
157 /* Allocate a ppggt and try to fill the entire range */
159 if (!HAS_PPGTT(dev_priv))
162 ppgtt = i915_ppgtt_create(to_gt(dev_priv), 0);
164 return PTR_ERR(ppgtt);
166 if (!ppgtt->vm.allocate_va_range)
167 goto err_ppgtt_cleanup;
170 * While we only allocate the page tables here and so we could
171 * address a much larger GTT than we could actually fit into
172 * RAM, a practical limit is the amount of physical pages in the system.
173 * This should ensure that we do not run into the oomkiller during
174 * the test and take down the machine wilfully.
176 limit = totalram_pages() << PAGE_SHIFT;
177 limit = min(ppgtt->vm.total, limit);
179 i915_gem_ww_ctx_init(&ww, false);
181 err = i915_vm_lock_objects(&ppgtt->vm, &ww);
183 goto err_ppgtt_cleanup;
185 /* Check we can allocate the entire range */
186 for (size = 4096; size <= limit; size <<= 2) {
187 struct i915_vm_pt_stash stash = {};
189 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, size);
191 goto err_ppgtt_cleanup;
193 err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
195 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
196 goto err_ppgtt_cleanup;
199 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, size);
202 ppgtt->vm.clear_range(&ppgtt->vm, 0, size);
204 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
207 /* Check we can incrementally allocate the entire range */
208 for (last = 0, size = 4096; size <= limit; last = size, size <<= 2) {
209 struct i915_vm_pt_stash stash = {};
211 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, size - last);
213 goto err_ppgtt_cleanup;
215 err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
217 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
218 goto err_ppgtt_cleanup;
221 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash,
225 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
229 if (err == -EDEADLK) {
230 err = i915_gem_ww_ctx_backoff(&ww);
234 i915_gem_ww_ctx_fini(&ww);
236 i915_vm_put(&ppgtt->vm);
240 static int lowlevel_hole(struct i915_address_space *vm,
241 u64 hole_start, u64 hole_end,
242 unsigned long end_time)
244 const unsigned int min_alignment =
245 i915_vm_min_alignment(vm, INTEL_MEMORY_SYSTEM);
246 I915_RND_STATE(seed_prng);
247 struct i915_vma_resource *mock_vma_res;
250 mock_vma_res = kzalloc(sizeof(*mock_vma_res), GFP_KERNEL);
254 /* Keep creating larger objects until one cannot fit into the hole */
255 for (size = 12; (hole_end - hole_start) >> size; size++) {
256 I915_RND_SUBSTATE(prng, seed_prng);
257 struct drm_i915_gem_object *obj;
258 unsigned int *order, count, n;
259 u64 hole_size, aligned_size;
261 aligned_size = max_t(u32, ilog2(min_alignment), size);
262 hole_size = (hole_end - hole_start) >> aligned_size;
263 if (hole_size > KMALLOC_MAX_SIZE / sizeof(u32))
264 hole_size = KMALLOC_MAX_SIZE / sizeof(u32);
265 count = hole_size >> 1;
267 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
268 __func__, hole_start, hole_end, size, hole_size);
273 order = i915_random_order(count, &prng);
276 } while (count >>= 1);
283 GEM_BUG_ON(count * BIT_ULL(aligned_size) > vm->total);
284 GEM_BUG_ON(hole_start + count * BIT_ULL(aligned_size) > hole_end);
286 /* Ignore allocation failures (i.e. don't report them as
287 * a test failure) as we are purposefully allocating very
288 * large objects without checking that we have sufficient
289 * memory. We expect to hit -ENOMEM.
292 obj = fake_dma_object(vm->i915, BIT_ULL(size));
298 GEM_BUG_ON(obj->base.size != BIT_ULL(size));
300 if (i915_gem_object_pin_pages_unlocked(obj)) {
301 i915_gem_object_put(obj);
306 for (n = 0; n < count; n++) {
307 u64 addr = hole_start + order[n] * BIT_ULL(aligned_size);
308 intel_wakeref_t wakeref;
310 GEM_BUG_ON(addr + BIT_ULL(aligned_size) > vm->total);
312 if (igt_timeout(end_time,
313 "%s timed out before %d/%d\n",
314 __func__, n, count)) {
315 hole_end = hole_start; /* quit */
319 if (vm->allocate_va_range) {
320 struct i915_vm_pt_stash stash = {};
321 struct i915_gem_ww_ctx ww;
324 i915_gem_ww_ctx_init(&ww, false);
326 err = i915_vm_lock_objects(vm, &ww);
331 if (i915_vm_alloc_pt_stash(vm, &stash,
335 err = i915_vm_map_pt_stash(vm, &stash);
337 vm->allocate_va_range(vm, &stash,
338 addr, BIT_ULL(size));
339 i915_vm_free_pt_stash(vm, &stash);
341 if (err == -EDEADLK) {
342 err = i915_gem_ww_ctx_backoff(&ww);
346 i915_gem_ww_ctx_fini(&ww);
352 mock_vma_res->bi.pages = obj->mm.pages;
353 mock_vma_res->node_size = BIT_ULL(aligned_size);
354 mock_vma_res->start = addr;
356 with_intel_runtime_pm(vm->gt->uncore->rpm, wakeref)
357 vm->insert_entries(vm, mock_vma_res,
362 i915_random_reorder(order, count, &prng);
363 for (n = 0; n < count; n++) {
364 u64 addr = hole_start + order[n] * BIT_ULL(aligned_size);
365 intel_wakeref_t wakeref;
367 GEM_BUG_ON(addr + BIT_ULL(size) > vm->total);
368 with_intel_runtime_pm(vm->gt->uncore->rpm, wakeref)
369 vm->clear_range(vm, addr, BIT_ULL(size));
372 i915_gem_object_unpin_pages(obj);
373 i915_gem_object_put(obj);
377 cleanup_freed_objects(vm->i915);
384 static void close_object_list(struct list_head *objects,
385 struct i915_address_space *vm)
387 struct drm_i915_gem_object *obj, *on;
390 list_for_each_entry_safe(obj, on, objects, st_link) {
391 struct i915_vma *vma;
393 vma = i915_vma_instance(obj, vm, NULL);
395 ignored = i915_vma_unbind_unlocked(vma);
397 list_del(&obj->st_link);
398 i915_gem_object_put(obj);
402 static int fill_hole(struct i915_address_space *vm,
403 u64 hole_start, u64 hole_end,
404 unsigned long end_time)
406 const u64 hole_size = hole_end - hole_start;
407 struct drm_i915_gem_object *obj;
408 const unsigned int min_alignment =
409 i915_vm_min_alignment(vm, INTEL_MEMORY_SYSTEM);
410 const unsigned long max_pages =
411 min_t(u64, ULONG_MAX - 1, (hole_size / 2) >> ilog2(min_alignment));
412 const unsigned long max_step = max(int_sqrt(max_pages), 2UL);
413 unsigned long npages, prime, flags;
414 struct i915_vma *vma;
418 /* Try binding many VMA working inwards from either edge */
420 flags = PIN_OFFSET_FIXED | PIN_USER;
421 if (i915_is_ggtt(vm))
424 for_each_prime_number_from(prime, 2, max_step) {
425 for (npages = 1; npages <= max_pages; npages *= prime) {
426 const u64 full_size = npages << PAGE_SHIFT;
432 { "top-down", hole_end, -1, },
433 { "bottom-up", hole_start, 1, },
437 obj = fake_dma_object(vm->i915, full_size);
441 list_add(&obj->st_link, &objects);
443 /* Align differing sized objects against the edges, and
444 * check we don't walk off into the void when binding
447 for (p = phases; p->name; p++) {
451 list_for_each_entry(obj, &objects, st_link) {
452 u64 aligned_size = round_up(obj->base.size,
455 vma = i915_vma_instance(obj, vm, NULL);
460 if (offset < hole_start + aligned_size)
462 offset -= aligned_size;
465 err = i915_vma_pin(vma, 0, 0, offset | flags);
467 pr_err("%s(%s) pin (forward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
468 __func__, p->name, err, npages, prime, offset);
472 if (!drm_mm_node_allocated(&vma->node) ||
473 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
474 pr_err("%s(%s) (forward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
475 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
484 if (offset + aligned_size > hole_end)
486 offset += aligned_size;
491 list_for_each_entry(obj, &objects, st_link) {
492 u64 aligned_size = round_up(obj->base.size,
495 vma = i915_vma_instance(obj, vm, NULL);
500 if (offset < hole_start + aligned_size)
502 offset -= aligned_size;
505 if (!drm_mm_node_allocated(&vma->node) ||
506 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
507 pr_err("%s(%s) (forward) moved vma.node=%llx + %llx, expected offset %llx\n",
508 __func__, p->name, vma->node.start, vma->node.size,
514 err = i915_vma_unbind_unlocked(vma);
516 pr_err("%s(%s) (forward) unbind of vma.node=%llx + %llx failed with err=%d\n",
517 __func__, p->name, vma->node.start, vma->node.size,
523 if (offset + aligned_size > hole_end)
525 offset += aligned_size;
530 list_for_each_entry_reverse(obj, &objects, st_link) {
531 u64 aligned_size = round_up(obj->base.size,
534 vma = i915_vma_instance(obj, vm, NULL);
539 if (offset < hole_start + aligned_size)
541 offset -= aligned_size;
544 err = i915_vma_pin(vma, 0, 0, offset | flags);
546 pr_err("%s(%s) pin (backward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
547 __func__, p->name, err, npages, prime, offset);
551 if (!drm_mm_node_allocated(&vma->node) ||
552 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
553 pr_err("%s(%s) (backward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
554 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
563 if (offset + aligned_size > hole_end)
565 offset += aligned_size;
570 list_for_each_entry_reverse(obj, &objects, st_link) {
571 u64 aligned_size = round_up(obj->base.size,
574 vma = i915_vma_instance(obj, vm, NULL);
579 if (offset < hole_start + aligned_size)
581 offset -= aligned_size;
584 if (!drm_mm_node_allocated(&vma->node) ||
585 i915_vma_misplaced(vma, 0, 0, offset | flags)) {
586 pr_err("%s(%s) (backward) moved vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
587 __func__, p->name, vma->node.start, vma->node.size, drm_mm_node_allocated(&vma->node),
593 err = i915_vma_unbind_unlocked(vma);
595 pr_err("%s(%s) (backward) unbind of vma.node=%llx + %llx failed with err=%d\n",
596 __func__, p->name, vma->node.start, vma->node.size,
602 if (offset + aligned_size > hole_end)
604 offset += aligned_size;
609 if (igt_timeout(end_time, "%s timed out (npages=%lu, prime=%lu)\n",
610 __func__, npages, prime)) {
616 close_object_list(&objects, vm);
617 cleanup_freed_objects(vm->i915);
623 close_object_list(&objects, vm);
627 static int walk_hole(struct i915_address_space *vm,
628 u64 hole_start, u64 hole_end,
629 unsigned long end_time)
631 const u64 hole_size = hole_end - hole_start;
632 const unsigned long max_pages =
633 min_t(u64, ULONG_MAX - 1, hole_size >> PAGE_SHIFT);
634 unsigned long min_alignment;
638 /* Try binding a single VMA in different positions within the hole */
640 flags = PIN_OFFSET_FIXED | PIN_USER;
641 if (i915_is_ggtt(vm))
644 min_alignment = i915_vm_min_alignment(vm, INTEL_MEMORY_SYSTEM);
646 for_each_prime_number_from(size, 1, max_pages) {
647 struct drm_i915_gem_object *obj;
648 struct i915_vma *vma;
652 obj = fake_dma_object(vm->i915, size << PAGE_SHIFT);
656 vma = i915_vma_instance(obj, vm, NULL);
662 for (addr = hole_start;
663 addr + obj->base.size < hole_end;
664 addr += round_up(obj->base.size, min_alignment)) {
665 err = i915_vma_pin(vma, 0, 0, addr | flags);
667 pr_err("%s bind failed at %llx + %llx [hole %llx- %llx] with err=%d\n",
668 __func__, addr, vma->size,
669 hole_start, hole_end, err);
674 if (!drm_mm_node_allocated(&vma->node) ||
675 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
676 pr_err("%s incorrect at %llx + %llx\n",
677 __func__, addr, vma->size);
682 err = i915_vma_unbind_unlocked(vma);
684 pr_err("%s unbind failed at %llx + %llx with err=%d\n",
685 __func__, addr, vma->size, err);
689 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
691 if (igt_timeout(end_time,
692 "%s timed out at %llx\n",
700 i915_gem_object_put(obj);
704 cleanup_freed_objects(vm->i915);
710 static int pot_hole(struct i915_address_space *vm,
711 u64 hole_start, u64 hole_end,
712 unsigned long end_time)
714 struct drm_i915_gem_object *obj;
715 struct i915_vma *vma;
716 unsigned int min_alignment;
721 flags = PIN_OFFSET_FIXED | PIN_USER;
722 if (i915_is_ggtt(vm))
725 min_alignment = i915_vm_min_alignment(vm, INTEL_MEMORY_SYSTEM);
727 obj = i915_gem_object_create_internal(vm->i915, 2 * I915_GTT_PAGE_SIZE);
731 vma = i915_vma_instance(obj, vm, NULL);
737 /* Insert a pair of pages across every pot boundary within the hole */
738 for (pot = fls64(hole_end - 1) - 1;
739 pot > ilog2(2 * min_alignment);
741 u64 step = BIT_ULL(pot);
744 for (addr = round_up(hole_start + min_alignment, step) - min_alignment;
745 hole_end > addr && hole_end - addr >= 2 * min_alignment;
747 err = i915_vma_pin(vma, 0, 0, addr | flags);
749 pr_err("%s failed to pin object at %llx in hole [%llx - %llx], with err=%d\n",
752 hole_start, hole_end,
757 if (!drm_mm_node_allocated(&vma->node) ||
758 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
759 pr_err("%s incorrect at %llx + %llx\n",
760 __func__, addr, vma->size);
762 err = i915_vma_unbind_unlocked(vma);
768 err = i915_vma_unbind_unlocked(vma);
772 if (igt_timeout(end_time,
773 "%s timed out after %d/%d\n",
774 __func__, pot, fls64(hole_end - 1) - 1)) {
781 i915_gem_object_put(obj);
785 static int drunk_hole(struct i915_address_space *vm,
786 u64 hole_start, u64 hole_end,
787 unsigned long end_time)
789 I915_RND_STATE(prng);
790 unsigned int min_alignment;
794 flags = PIN_OFFSET_FIXED | PIN_USER;
795 if (i915_is_ggtt(vm))
798 min_alignment = i915_vm_min_alignment(vm, INTEL_MEMORY_SYSTEM);
800 /* Keep creating larger objects until one cannot fit into the hole */
801 for (size = 12; (hole_end - hole_start) >> size; size++) {
802 struct drm_i915_gem_object *obj;
803 unsigned int *order, count, n;
804 struct i915_vma *vma;
805 u64 hole_size, aligned_size;
808 aligned_size = max_t(u32, ilog2(min_alignment), size);
809 hole_size = (hole_end - hole_start) >> aligned_size;
810 if (hole_size > KMALLOC_MAX_SIZE / sizeof(u32))
811 hole_size = KMALLOC_MAX_SIZE / sizeof(u32);
812 count = hole_size >> 1;
814 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
815 __func__, hole_start, hole_end, size, hole_size);
820 order = i915_random_order(count, &prng);
823 } while (count >>= 1);
828 /* Ignore allocation failures (i.e. don't report them as
829 * a test failure) as we are purposefully allocating very
830 * large objects without checking that we have sufficient
831 * memory. We expect to hit -ENOMEM.
834 obj = fake_dma_object(vm->i915, BIT_ULL(size));
840 vma = i915_vma_instance(obj, vm, NULL);
846 GEM_BUG_ON(vma->size != BIT_ULL(size));
848 for (n = 0; n < count; n++) {
849 u64 addr = hole_start + order[n] * BIT_ULL(aligned_size);
851 err = i915_vma_pin(vma, 0, 0, addr | flags);
853 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
856 hole_start, hole_end,
861 if (!drm_mm_node_allocated(&vma->node) ||
862 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
863 pr_err("%s incorrect at %llx + %llx\n",
864 __func__, addr, BIT_ULL(size));
866 err = i915_vma_unbind_unlocked(vma);
872 err = i915_vma_unbind_unlocked(vma);
875 if (igt_timeout(end_time,
876 "%s timed out after %d/%d\n",
877 __func__, n, count)) {
884 i915_gem_object_put(obj);
889 cleanup_freed_objects(vm->i915);
895 static int __shrink_hole(struct i915_address_space *vm,
896 u64 hole_start, u64 hole_end,
897 unsigned long end_time)
899 struct drm_i915_gem_object *obj;
900 unsigned long flags = PIN_OFFSET_FIXED | PIN_USER;
901 unsigned int min_alignment;
902 unsigned int order = 12;
907 min_alignment = i915_vm_min_alignment(vm, INTEL_MEMORY_SYSTEM);
909 /* Keep creating larger objects until one cannot fit into the hole */
910 for (addr = hole_start; addr < hole_end; ) {
911 struct i915_vma *vma;
912 u64 size = BIT_ULL(order++);
914 size = min(size, hole_end - addr);
915 obj = fake_dma_object(vm->i915, size);
921 list_add(&obj->st_link, &objects);
923 vma = i915_vma_instance(obj, vm, NULL);
929 GEM_BUG_ON(vma->size != size);
931 err = i915_vma_pin(vma, 0, 0, addr | flags);
933 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
934 __func__, addr, size, hole_start, hole_end, err);
938 if (!drm_mm_node_allocated(&vma->node) ||
939 i915_vma_misplaced(vma, 0, 0, addr | flags)) {
940 pr_err("%s incorrect at %llx + %llx\n",
941 __func__, addr, size);
943 err = i915_vma_unbind_unlocked(vma);
949 addr += round_up(size, min_alignment);
952 * Since we are injecting allocation faults at random intervals,
953 * wait for this allocation to complete before we change the
956 err = i915_vma_sync(vma);
960 if (igt_timeout(end_time,
961 "%s timed out at ofset %llx [%llx - %llx]\n",
962 __func__, addr, hole_start, hole_end)) {
968 close_object_list(&objects, vm);
969 cleanup_freed_objects(vm->i915);
973 static int shrink_hole(struct i915_address_space *vm,
974 u64 hole_start, u64 hole_end,
975 unsigned long end_time)
980 vm->fault_attr.probability = 999;
981 atomic_set(&vm->fault_attr.times, -1);
983 for_each_prime_number_from(prime, 0, ULONG_MAX - 1) {
984 vm->fault_attr.interval = prime;
985 err = __shrink_hole(vm, hole_start, hole_end, end_time);
990 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
995 static int shrink_boom(struct i915_address_space *vm,
996 u64 hole_start, u64 hole_end,
997 unsigned long end_time)
999 unsigned int sizes[] = { SZ_2M, SZ_1G };
1000 struct drm_i915_gem_object *purge;
1001 struct drm_i915_gem_object *explode;
1006 * Catch the case which shrink_hole seems to miss. The setup here
1007 * requires invoking the shrinker as we do the alloc_pt/alloc_pd, while
1008 * ensuring that all vma assiocated with the respective pd/pdp are
1009 * unpinned at the time.
1012 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
1013 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1014 unsigned int size = sizes[i];
1015 struct i915_vma *vma;
1017 purge = fake_dma_object(vm->i915, size);
1019 return PTR_ERR(purge);
1021 vma = i915_vma_instance(purge, vm, NULL);
1027 err = i915_vma_pin(vma, 0, 0, flags);
1031 /* Should now be ripe for purging */
1032 i915_vma_unpin(vma);
1034 explode = fake_dma_object(vm->i915, size);
1035 if (IS_ERR(explode)) {
1036 err = PTR_ERR(explode);
1040 vm->fault_attr.probability = 100;
1041 vm->fault_attr.interval = 1;
1042 atomic_set(&vm->fault_attr.times, -1);
1044 vma = i915_vma_instance(explode, vm, NULL);
1050 err = i915_vma_pin(vma, 0, 0, flags | size);
1054 i915_vma_unpin(vma);
1056 i915_gem_object_put(purge);
1057 i915_gem_object_put(explode);
1059 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
1060 cleanup_freed_objects(vm->i915);
1066 i915_gem_object_put(explode);
1068 i915_gem_object_put(purge);
1069 memset(&vm->fault_attr, 0, sizeof(vm->fault_attr));
1073 static int misaligned_case(struct i915_address_space *vm, struct intel_memory_region *mr,
1074 u64 addr, u64 size, unsigned long flags)
1076 struct drm_i915_gem_object *obj;
1077 struct i915_vma *vma;
1079 u64 expected_vma_size, expected_node_size;
1080 bool is_stolen = mr->type == INTEL_MEMORY_STOLEN_SYSTEM ||
1081 mr->type == INTEL_MEMORY_STOLEN_LOCAL;
1083 obj = i915_gem_object_create_region(mr, size, 0, 0);
1085 /* if iGVT-g or DMAR is active, stolen mem will be uninitialized */
1086 if (PTR_ERR(obj) == -ENODEV && is_stolen)
1088 return PTR_ERR(obj);
1091 vma = i915_vma_instance(obj, vm, NULL);
1097 err = i915_vma_pin(vma, 0, 0, addr | flags);
1100 i915_vma_unpin(vma);
1102 if (!drm_mm_node_allocated(&vma->node)) {
1107 if (i915_vma_misplaced(vma, 0, 0, addr | flags)) {
1112 expected_vma_size = round_up(size, 1 << (ffs(vma->resource->page_sizes_gtt) - 1));
1113 expected_node_size = expected_vma_size;
1115 if (HAS_64K_PAGES(vm->i915) && i915_gem_object_is_lmem(obj)) {
1117 * The compact-pt should expand lmem node to 2MB for the ppGTT,
1118 * for all other cases we should only expect 64K.
1120 expected_vma_size = round_up(size, I915_GTT_PAGE_SIZE_64K);
1121 if (NEEDS_COMPACT_PT(vm->i915) && !i915_is_ggtt(vm))
1122 expected_node_size = round_up(size, I915_GTT_PAGE_SIZE_2M);
1124 expected_node_size = round_up(size, I915_GTT_PAGE_SIZE_64K);
1127 if (vma->size != expected_vma_size || vma->node.size != expected_node_size) {
1128 err = i915_vma_unbind_unlocked(vma);
1133 err = i915_vma_unbind_unlocked(vma);
1137 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
1140 i915_gem_object_put(obj);
1141 cleanup_freed_objects(vm->i915);
1145 static int misaligned_pin(struct i915_address_space *vm,
1146 u64 hole_start, u64 hole_end,
1147 unsigned long end_time)
1149 struct intel_memory_region *mr;
1150 enum intel_region_id id;
1151 unsigned long flags = PIN_OFFSET_FIXED | PIN_USER;
1153 u64 hole_size = hole_end - hole_start;
1155 if (i915_is_ggtt(vm))
1156 flags |= PIN_GLOBAL;
1158 for_each_memory_region(mr, vm->i915, id) {
1159 u64 min_alignment = i915_vm_min_alignment(vm, mr->type);
1160 u64 size = min_alignment;
1161 u64 addr = round_down(hole_start + (hole_size / 2), min_alignment);
1163 /* avoid -ENOSPC on very small hole setups */
1164 if (hole_size < 3 * min_alignment)
1167 /* we can't test < 4k alignment due to flags being encoded in lower bits */
1168 if (min_alignment != I915_GTT_PAGE_SIZE_4K) {
1169 err = misaligned_case(vm, mr, addr + (min_alignment / 2), size, flags);
1170 /* misaligned should error with -EINVAL*/
1177 /* test for vma->size expansion to min page size */
1178 err = misaligned_case(vm, mr, addr, PAGE_SIZE, flags);
1182 /* test for intermediate size not expanding vma->size for large alignments */
1183 err = misaligned_case(vm, mr, addr, size / 2, flags);
1191 static int exercise_ppgtt(struct drm_i915_private *dev_priv,
1192 int (*func)(struct i915_address_space *vm,
1193 u64 hole_start, u64 hole_end,
1194 unsigned long end_time))
1196 struct i915_ppgtt *ppgtt;
1197 IGT_TIMEOUT(end_time);
1201 if (!HAS_FULL_PPGTT(dev_priv))
1204 file = mock_file(dev_priv);
1206 return PTR_ERR(file);
1208 ppgtt = i915_ppgtt_create(to_gt(dev_priv), 0);
1209 if (IS_ERR(ppgtt)) {
1210 err = PTR_ERR(ppgtt);
1213 GEM_BUG_ON(offset_in_page(ppgtt->vm.total));
1214 assert_vm_alive(&ppgtt->vm);
1216 err = func(&ppgtt->vm, 0, ppgtt->vm.total, end_time);
1218 i915_vm_put(&ppgtt->vm);
1225 static int igt_ppgtt_fill(void *arg)
1227 return exercise_ppgtt(arg, fill_hole);
1230 static int igt_ppgtt_walk(void *arg)
1232 return exercise_ppgtt(arg, walk_hole);
1235 static int igt_ppgtt_pot(void *arg)
1237 return exercise_ppgtt(arg, pot_hole);
1240 static int igt_ppgtt_drunk(void *arg)
1242 return exercise_ppgtt(arg, drunk_hole);
1245 static int igt_ppgtt_lowlevel(void *arg)
1247 return exercise_ppgtt(arg, lowlevel_hole);
1250 static int igt_ppgtt_shrink(void *arg)
1252 return exercise_ppgtt(arg, shrink_hole);
1255 static int igt_ppgtt_shrink_boom(void *arg)
1257 return exercise_ppgtt(arg, shrink_boom);
1260 static int igt_ppgtt_misaligned_pin(void *arg)
1262 return exercise_ppgtt(arg, misaligned_pin);
1265 static int sort_holes(void *priv, const struct list_head *A,
1266 const struct list_head *B)
1268 struct drm_mm_node *a = list_entry(A, typeof(*a), hole_stack);
1269 struct drm_mm_node *b = list_entry(B, typeof(*b), hole_stack);
1271 if (a->start < b->start)
1277 static int exercise_ggtt(struct drm_i915_private *i915,
1278 int (*func)(struct i915_address_space *vm,
1279 u64 hole_start, u64 hole_end,
1280 unsigned long end_time))
1282 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1283 u64 hole_start, hole_end, last = 0;
1284 struct drm_mm_node *node;
1285 IGT_TIMEOUT(end_time);
1289 list_sort(NULL, &ggtt->vm.mm.hole_stack, sort_holes);
1290 drm_mm_for_each_hole(node, &ggtt->vm.mm, hole_start, hole_end) {
1291 if (hole_start < last)
1294 if (ggtt->vm.mm.color_adjust)
1295 ggtt->vm.mm.color_adjust(node, 0,
1296 &hole_start, &hole_end);
1297 if (hole_start >= hole_end)
1300 err = func(&ggtt->vm, hole_start, hole_end, end_time);
1304 /* As we have manipulated the drm_mm, the list may be corrupt */
1312 static int igt_ggtt_fill(void *arg)
1314 return exercise_ggtt(arg, fill_hole);
1317 static int igt_ggtt_walk(void *arg)
1319 return exercise_ggtt(arg, walk_hole);
1322 static int igt_ggtt_pot(void *arg)
1324 return exercise_ggtt(arg, pot_hole);
1327 static int igt_ggtt_drunk(void *arg)
1329 return exercise_ggtt(arg, drunk_hole);
1332 static int igt_ggtt_lowlevel(void *arg)
1334 return exercise_ggtt(arg, lowlevel_hole);
1337 static int igt_ggtt_misaligned_pin(void *arg)
1339 return exercise_ggtt(arg, misaligned_pin);
1342 static int igt_ggtt_page(void *arg)
1344 const unsigned int count = PAGE_SIZE/sizeof(u32);
1345 I915_RND_STATE(prng);
1346 struct drm_i915_private *i915 = arg;
1347 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1348 struct drm_i915_gem_object *obj;
1349 intel_wakeref_t wakeref;
1350 struct drm_mm_node tmp;
1351 unsigned int *order, n;
1354 if (!i915_ggtt_has_aperture(ggtt))
1357 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
1359 return PTR_ERR(obj);
1361 err = i915_gem_object_pin_pages_unlocked(obj);
1365 memset(&tmp, 0, sizeof(tmp));
1366 mutex_lock(&ggtt->vm.mutex);
1367 err = drm_mm_insert_node_in_range(&ggtt->vm.mm, &tmp,
1368 count * PAGE_SIZE, 0,
1369 I915_COLOR_UNEVICTABLE,
1370 0, ggtt->mappable_end,
1372 mutex_unlock(&ggtt->vm.mutex);
1376 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1378 for (n = 0; n < count; n++) {
1379 u64 offset = tmp.start + n * PAGE_SIZE;
1381 ggtt->vm.insert_page(&ggtt->vm,
1382 i915_gem_object_get_dma_address(obj, 0),
1383 offset, I915_CACHE_NONE, 0);
1386 order = i915_random_order(count, &prng);
1392 for (n = 0; n < count; n++) {
1393 u64 offset = tmp.start + order[n] * PAGE_SIZE;
1396 vaddr = io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1397 iowrite32(n, vaddr + n);
1398 io_mapping_unmap_atomic(vaddr);
1400 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
1402 i915_random_reorder(order, count, &prng);
1403 for (n = 0; n < count; n++) {
1404 u64 offset = tmp.start + order[n] * PAGE_SIZE;
1408 vaddr = io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1409 val = ioread32(vaddr + n);
1410 io_mapping_unmap_atomic(vaddr);
1413 pr_err("insert page failed: found %d, expected %d\n",
1422 ggtt->vm.clear_range(&ggtt->vm, tmp.start, tmp.size);
1423 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1424 mutex_lock(&ggtt->vm.mutex);
1425 drm_mm_remove_node(&tmp);
1426 mutex_unlock(&ggtt->vm.mutex);
1428 i915_gem_object_unpin_pages(obj);
1430 i915_gem_object_put(obj);
1434 static void track_vma_bind(struct i915_vma *vma)
1436 struct drm_i915_gem_object *obj = vma->obj;
1438 __i915_gem_object_pin_pages(obj);
1440 GEM_BUG_ON(atomic_read(&vma->pages_count));
1441 atomic_set(&vma->pages_count, I915_VMA_PAGES_ACTIVE);
1442 __i915_gem_object_pin_pages(obj);
1443 vma->pages = obj->mm.pages;
1444 vma->resource->bi.pages = vma->pages;
1446 mutex_lock(&vma->vm->mutex);
1447 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
1448 mutex_unlock(&vma->vm->mutex);
1451 static int exercise_mock(struct drm_i915_private *i915,
1452 int (*func)(struct i915_address_space *vm,
1453 u64 hole_start, u64 hole_end,
1454 unsigned long end_time))
1456 const u64 limit = totalram_pages() << PAGE_SHIFT;
1457 struct i915_address_space *vm;
1458 struct i915_gem_context *ctx;
1459 IGT_TIMEOUT(end_time);
1462 ctx = mock_context(i915, "mock");
1466 vm = i915_gem_context_get_eb_vm(ctx);
1467 err = func(vm, 0, min(vm->total, limit), end_time);
1470 mock_context_close(ctx);
1474 static int igt_mock_fill(void *arg)
1476 struct i915_ggtt *ggtt = arg;
1478 return exercise_mock(ggtt->vm.i915, fill_hole);
1481 static int igt_mock_walk(void *arg)
1483 struct i915_ggtt *ggtt = arg;
1485 return exercise_mock(ggtt->vm.i915, walk_hole);
1488 static int igt_mock_pot(void *arg)
1490 struct i915_ggtt *ggtt = arg;
1492 return exercise_mock(ggtt->vm.i915, pot_hole);
1495 static int igt_mock_drunk(void *arg)
1497 struct i915_ggtt *ggtt = arg;
1499 return exercise_mock(ggtt->vm.i915, drunk_hole);
1502 static int reserve_gtt_with_resource(struct i915_vma *vma, u64 offset)
1504 struct i915_address_space *vm = vma->vm;
1505 struct i915_vma_resource *vma_res;
1506 struct drm_i915_gem_object *obj = vma->obj;
1509 vma_res = i915_vma_resource_alloc();
1510 if (IS_ERR(vma_res))
1511 return PTR_ERR(vma_res);
1513 mutex_lock(&vm->mutex);
1514 err = i915_gem_gtt_reserve(vm, NULL, &vma->node, obj->base.size,
1519 i915_vma_resource_init_from_vma(vma_res, vma);
1520 vma->resource = vma_res;
1524 mutex_unlock(&vm->mutex);
1529 static int igt_gtt_reserve(void *arg)
1531 struct i915_ggtt *ggtt = arg;
1532 struct drm_i915_gem_object *obj, *on;
1533 I915_RND_STATE(prng);
1538 /* i915_gem_gtt_reserve() tries to reserve the precise range
1539 * for the node, and evicts if it has to. So our test checks that
1540 * it can give us the requsted space and prevent overlaps.
1543 /* Start by filling the GGTT */
1545 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1546 total += 2 * I915_GTT_PAGE_SIZE) {
1547 struct i915_vma *vma;
1549 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1556 err = i915_gem_object_pin_pages_unlocked(obj);
1558 i915_gem_object_put(obj);
1562 list_add(&obj->st_link, &objects);
1563 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1569 err = reserve_gtt_with_resource(vma, total);
1571 pr_err("i915_gem_gtt_reserve (pass 1) failed at %llu/%llu with err=%d\n",
1572 total, ggtt->vm.total, err);
1575 track_vma_bind(vma);
1577 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1578 if (vma->node.start != total ||
1579 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1580 pr_err("i915_gem_gtt_reserve (pass 1) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1581 vma->node.start, vma->node.size,
1582 total, 2*I915_GTT_PAGE_SIZE);
1588 /* Now we start forcing evictions */
1589 for (total = I915_GTT_PAGE_SIZE;
1590 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1591 total += 2 * I915_GTT_PAGE_SIZE) {
1592 struct i915_vma *vma;
1594 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1601 err = i915_gem_object_pin_pages_unlocked(obj);
1603 i915_gem_object_put(obj);
1607 list_add(&obj->st_link, &objects);
1609 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1615 err = reserve_gtt_with_resource(vma, total);
1617 pr_err("i915_gem_gtt_reserve (pass 2) failed at %llu/%llu with err=%d\n",
1618 total, ggtt->vm.total, err);
1621 track_vma_bind(vma);
1623 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1624 if (vma->node.start != total ||
1625 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1626 pr_err("i915_gem_gtt_reserve (pass 2) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1627 vma->node.start, vma->node.size,
1628 total, 2*I915_GTT_PAGE_SIZE);
1634 /* And then try at random */
1635 list_for_each_entry_safe(obj, on, &objects, st_link) {
1636 struct i915_vma *vma;
1639 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1645 err = i915_vma_unbind_unlocked(vma);
1647 pr_err("i915_vma_unbind failed with err=%d!\n", err);
1651 offset = igt_random_offset(&prng,
1653 2 * I915_GTT_PAGE_SIZE,
1654 I915_GTT_MIN_ALIGNMENT);
1656 err = reserve_gtt_with_resource(vma, offset);
1658 pr_err("i915_gem_gtt_reserve (pass 3) failed at %llu/%llu with err=%d\n",
1659 total, ggtt->vm.total, err);
1662 track_vma_bind(vma);
1664 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1665 if (vma->node.start != offset ||
1666 vma->node.size != 2*I915_GTT_PAGE_SIZE) {
1667 pr_err("i915_gem_gtt_reserve (pass 3) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1668 vma->node.start, vma->node.size,
1669 offset, 2*I915_GTT_PAGE_SIZE);
1676 list_for_each_entry_safe(obj, on, &objects, st_link) {
1677 i915_gem_object_unpin_pages(obj);
1678 i915_gem_object_put(obj);
1683 static int insert_gtt_with_resource(struct i915_vma *vma)
1685 struct i915_address_space *vm = vma->vm;
1686 struct i915_vma_resource *vma_res;
1687 struct drm_i915_gem_object *obj = vma->obj;
1690 vma_res = i915_vma_resource_alloc();
1691 if (IS_ERR(vma_res))
1692 return PTR_ERR(vma_res);
1694 mutex_lock(&vm->mutex);
1695 err = i915_gem_gtt_insert(vm, NULL, &vma->node, obj->base.size, 0,
1696 obj->cache_level, 0, vm->total, 0);
1698 i915_vma_resource_init_from_vma(vma_res, vma);
1699 vma->resource = vma_res;
1703 mutex_unlock(&vm->mutex);
1708 static int igt_gtt_insert(void *arg)
1710 struct i915_ggtt *ggtt = arg;
1711 struct drm_i915_gem_object *obj, *on;
1712 struct drm_mm_node tmp = {};
1713 const struct invalid_insert {
1717 } invalid_insert[] = {
1719 ggtt->vm.total + I915_GTT_PAGE_SIZE, 0,
1723 2*I915_GTT_PAGE_SIZE, 0,
1724 0, I915_GTT_PAGE_SIZE,
1727 -(u64)I915_GTT_PAGE_SIZE, 0,
1728 0, 4*I915_GTT_PAGE_SIZE,
1731 -(u64)2*I915_GTT_PAGE_SIZE, 2*I915_GTT_PAGE_SIZE,
1732 0, 4*I915_GTT_PAGE_SIZE,
1735 I915_GTT_PAGE_SIZE, I915_GTT_MIN_ALIGNMENT << 1,
1736 I915_GTT_MIN_ALIGNMENT, I915_GTT_MIN_ALIGNMENT << 1,
1744 /* i915_gem_gtt_insert() tries to allocate some free space in the GTT
1745 * to the node, evicting if required.
1748 /* Check a couple of obviously invalid requests */
1749 for (ii = invalid_insert; ii->size; ii++) {
1750 mutex_lock(&ggtt->vm.mutex);
1751 err = i915_gem_gtt_insert(&ggtt->vm, NULL, &tmp,
1752 ii->size, ii->alignment,
1753 I915_COLOR_UNEVICTABLE,
1756 mutex_unlock(&ggtt->vm.mutex);
1757 if (err != -ENOSPC) {
1758 pr_err("Invalid i915_gem_gtt_insert(.size=%llx, .alignment=%llx, .start=%llx, .end=%llx) succeeded (err=%d)\n",
1759 ii->size, ii->alignment, ii->start, ii->end,
1765 /* Start by filling the GGTT */
1767 total + I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1768 total += I915_GTT_PAGE_SIZE) {
1769 struct i915_vma *vma;
1771 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1772 I915_GTT_PAGE_SIZE);
1778 err = i915_gem_object_pin_pages_unlocked(obj);
1780 i915_gem_object_put(obj);
1784 list_add(&obj->st_link, &objects);
1786 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1792 err = insert_gtt_with_resource(vma);
1793 if (err == -ENOSPC) {
1794 /* maxed out the GGTT space */
1795 i915_gem_object_put(obj);
1799 pr_err("i915_gem_gtt_insert (pass 1) failed at %llu/%llu with err=%d\n",
1800 total, ggtt->vm.total, err);
1803 track_vma_bind(vma);
1804 __i915_vma_pin(vma);
1806 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1809 list_for_each_entry(obj, &objects, st_link) {
1810 struct i915_vma *vma;
1812 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1818 if (!drm_mm_node_allocated(&vma->node)) {
1819 pr_err("VMA was unexpectedly evicted!\n");
1824 __i915_vma_unpin(vma);
1827 /* If we then reinsert, we should find the same hole */
1828 list_for_each_entry_safe(obj, on, &objects, st_link) {
1829 struct i915_vma *vma;
1832 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1838 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1839 offset = vma->node.start;
1841 err = i915_vma_unbind_unlocked(vma);
1843 pr_err("i915_vma_unbind failed with err=%d!\n", err);
1847 err = insert_gtt_with_resource(vma);
1849 pr_err("i915_gem_gtt_insert (pass 2) failed at %llu/%llu with err=%d\n",
1850 total, ggtt->vm.total, err);
1853 track_vma_bind(vma);
1855 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1856 if (vma->node.start != offset) {
1857 pr_err("i915_gem_gtt_insert did not return node to its previous location (the only hole), expected address %llx, found %llx\n",
1858 offset, vma->node.start);
1864 /* And then force evictions */
1866 total + 2 * I915_GTT_PAGE_SIZE <= ggtt->vm.total;
1867 total += 2 * I915_GTT_PAGE_SIZE) {
1868 struct i915_vma *vma;
1870 obj = i915_gem_object_create_internal(ggtt->vm.i915,
1871 2 * I915_GTT_PAGE_SIZE);
1877 err = i915_gem_object_pin_pages_unlocked(obj);
1879 i915_gem_object_put(obj);
1883 list_add(&obj->st_link, &objects);
1885 vma = i915_vma_instance(obj, &ggtt->vm, NULL);
1891 err = insert_gtt_with_resource(vma);
1893 pr_err("i915_gem_gtt_insert (pass 3) failed at %llu/%llu with err=%d\n",
1894 total, ggtt->vm.total, err);
1897 track_vma_bind(vma);
1899 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1903 list_for_each_entry_safe(obj, on, &objects, st_link) {
1904 i915_gem_object_unpin_pages(obj);
1905 i915_gem_object_put(obj);
1910 int i915_gem_gtt_mock_selftests(void)
1912 static const struct i915_subtest tests[] = {
1913 SUBTEST(igt_mock_drunk),
1914 SUBTEST(igt_mock_walk),
1915 SUBTEST(igt_mock_pot),
1916 SUBTEST(igt_mock_fill),
1917 SUBTEST(igt_gtt_reserve),
1918 SUBTEST(igt_gtt_insert),
1920 struct drm_i915_private *i915;
1921 struct intel_gt *gt;
1924 i915 = mock_gem_device();
1928 /* allocate the ggtt */
1929 err = intel_gt_assign_ggtt(to_gt(i915));
1937 err = i915_subtests(tests, gt->ggtt);
1939 mock_device_flush(i915);
1940 i915_gem_drain_freed_objects(i915);
1941 mock_fini_ggtt(gt->ggtt);
1944 mock_destroy_device(i915);
1948 static int context_sync(struct intel_context *ce)
1950 struct i915_request *rq;
1953 rq = intel_context_create_request(ce);
1957 i915_request_get(rq);
1958 i915_request_add(rq);
1960 timeout = i915_request_wait(rq, 0, HZ / 5);
1961 i915_request_put(rq);
1963 return timeout < 0 ? -EIO : 0;
1966 static struct i915_request *
1967 submit_batch(struct intel_context *ce, u64 addr)
1969 struct i915_request *rq;
1972 rq = intel_context_create_request(ce);
1977 if (rq->engine->emit_init_breadcrumb) /* detect a hang */
1978 err = rq->engine->emit_init_breadcrumb(rq);
1980 err = rq->engine->emit_bb_start(rq, addr, 0, 0);
1983 i915_request_get(rq);
1984 i915_request_add(rq);
1986 return err ? ERR_PTR(err) : rq;
1989 static u32 *spinner(u32 *batch, int i)
1991 return batch + i * 64 / sizeof(*batch) + 4;
1994 static void end_spin(u32 *batch, int i)
1996 *spinner(batch, i) = MI_BATCH_BUFFER_END;
2000 static int igt_cs_tlb(void *arg)
2002 const unsigned int count = PAGE_SIZE / 64;
2003 const unsigned int chunk_size = count * PAGE_SIZE;
2004 struct drm_i915_private *i915 = arg;
2005 struct drm_i915_gem_object *bbe, *act, *out;
2006 struct i915_gem_engines_iter it;
2007 struct i915_address_space *vm;
2008 struct i915_gem_context *ctx;
2009 struct intel_context *ce;
2010 struct i915_vma *vma;
2011 I915_RND_STATE(prng);
2019 * Our mission here is to fool the hardware to execute something
2020 * from scratch as it has not seen the batch move (due to missing
2021 * the TLB invalidate).
2024 file = mock_file(i915);
2026 return PTR_ERR(file);
2028 ctx = live_context(i915, file);
2034 vm = i915_gem_context_get_eb_vm(ctx);
2035 if (i915_is_ggtt(vm))
2038 /* Create two pages; dummy we prefill the TLB, and intended */
2039 bbe = i915_gem_object_create_internal(i915, PAGE_SIZE);
2045 batch = i915_gem_object_pin_map_unlocked(bbe, I915_MAP_WC);
2046 if (IS_ERR(batch)) {
2047 err = PTR_ERR(batch);
2050 memset32(batch, MI_BATCH_BUFFER_END, PAGE_SIZE / sizeof(u32));
2051 i915_gem_object_flush_map(bbe);
2052 i915_gem_object_unpin_map(bbe);
2054 act = i915_gem_object_create_internal(i915, PAGE_SIZE);
2060 /* Track the execution of each request by writing into different slot */
2061 batch = i915_gem_object_pin_map_unlocked(act, I915_MAP_WC);
2062 if (IS_ERR(batch)) {
2063 err = PTR_ERR(batch);
2066 for (i = 0; i < count; i++) {
2067 u32 *cs = batch + i * 64 / sizeof(*cs);
2068 u64 addr = (vm->total - PAGE_SIZE) + i * sizeof(u32);
2070 GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
2071 cs[0] = MI_STORE_DWORD_IMM_GEN4;
2072 if (GRAPHICS_VER(i915) >= 8) {
2073 cs[1] = lower_32_bits(addr);
2074 cs[2] = upper_32_bits(addr);
2077 cs[5] = MI_BATCH_BUFFER_START_GEN8;
2080 cs[2] = lower_32_bits(addr);
2083 cs[5] = MI_BATCH_BUFFER_START;
2087 out = i915_gem_object_create_internal(i915, PAGE_SIZE);
2092 i915_gem_object_set_cache_coherency(out, I915_CACHING_CACHED);
2094 vma = i915_vma_instance(out, vm, NULL);
2100 err = i915_vma_pin(vma, 0, 0,
2103 (vm->total - PAGE_SIZE));
2106 GEM_BUG_ON(vma->node.start != vm->total - PAGE_SIZE);
2108 result = i915_gem_object_pin_map_unlocked(out, I915_MAP_WB);
2109 if (IS_ERR(result)) {
2110 err = PTR_ERR(result);
2114 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
2115 IGT_TIMEOUT(end_time);
2116 unsigned long pass = 0;
2118 if (!intel_engine_can_store_dword(ce->engine))
2121 while (!__igt_timeout(end_time, NULL)) {
2122 struct i915_vm_pt_stash stash = {};
2123 struct i915_request *rq;
2124 struct i915_gem_ww_ctx ww;
2125 struct i915_vma_resource *vma_res;
2128 offset = igt_random_offset(&prng,
2129 0, vm->total - PAGE_SIZE,
2130 chunk_size, PAGE_SIZE);
2132 memset32(result, STACK_MAGIC, PAGE_SIZE / sizeof(u32));
2134 vma = i915_vma_instance(bbe, vm, NULL);
2140 i915_gem_object_lock(bbe, NULL);
2141 err = i915_vma_get_pages(vma);
2142 i915_gem_object_unlock(bbe);
2146 vma_res = i915_vma_resource_alloc();
2147 if (IS_ERR(vma_res)) {
2148 i915_vma_put_pages(vma);
2149 err = PTR_ERR(vma_res);
2153 i915_gem_ww_ctx_init(&ww, false);
2155 err = i915_vm_lock_objects(vm, &ww);
2159 err = i915_vm_alloc_pt_stash(vm, &stash, chunk_size);
2163 err = i915_vm_map_pt_stash(vm, &stash);
2165 vm->allocate_va_range(vm, &stash, offset, chunk_size);
2166 i915_vm_free_pt_stash(vm, &stash);
2168 if (err == -EDEADLK) {
2169 err = i915_gem_ww_ctx_backoff(&ww);
2173 i915_gem_ww_ctx_fini(&ww);
2179 i915_vma_resource_init_from_vma(vma_res, vma);
2180 /* Prime the TLB with the dummy pages */
2181 for (i = 0; i < count; i++) {
2182 vma_res->start = offset + i * PAGE_SIZE;
2183 vm->insert_entries(vm, vma_res, I915_CACHE_NONE,
2186 rq = submit_batch(ce, vma_res->start);
2189 i915_vma_resource_fini(vma_res);
2193 i915_request_put(rq);
2195 i915_vma_resource_fini(vma_res);
2196 i915_vma_put_pages(vma);
2198 err = context_sync(ce);
2200 pr_err("%s: dummy setup timed out\n",
2206 vma = i915_vma_instance(act, vm, NULL);
2213 i915_gem_object_lock(act, NULL);
2214 err = i915_vma_get_pages(vma);
2215 i915_gem_object_unlock(act);
2221 i915_vma_resource_init_from_vma(vma_res, vma);
2222 /* Replace the TLB with target batches */
2223 for (i = 0; i < count; i++) {
2224 struct i915_request *rq;
2225 u32 *cs = batch + i * 64 / sizeof(*cs);
2228 vma_res->start = offset + i * PAGE_SIZE;
2229 vm->insert_entries(vm, vma_res, I915_CACHE_NONE, 0);
2231 addr = vma_res->start + i * 64;
2233 cs[6] = lower_32_bits(addr);
2234 cs[7] = upper_32_bits(addr);
2237 rq = submit_batch(ce, addr);
2240 i915_vma_resource_fini(vma_res);
2245 /* Wait until the context chain has started */
2247 while (READ_ONCE(result[i]) &&
2248 !i915_request_completed(rq))
2251 end_spin(batch, i - 1);
2254 i915_request_put(rq);
2256 end_spin(batch, count - 1);
2258 i915_vma_resource_fini(vma_res);
2260 i915_vma_put_pages(vma);
2262 err = context_sync(ce);
2264 pr_err("%s: writes timed out\n",
2269 for (i = 0; i < count; i++) {
2270 if (result[i] != i) {
2271 pr_err("%s: Write lost on pass %lu, at offset %llx, index %d, found %x, expected %x\n",
2272 ce->engine->name, pass,
2273 offset, i, result[i], i);
2279 vm->clear_range(vm, offset, chunk_size);
2284 if (igt_flush_test(i915))
2286 i915_gem_context_unlock_engines(ctx);
2287 i915_gem_object_unpin_map(out);
2289 i915_gem_object_put(out);
2291 i915_gem_object_unpin_map(act);
2293 i915_gem_object_put(act);
2295 i915_gem_object_put(bbe);
2303 int i915_gem_gtt_live_selftests(struct drm_i915_private *i915)
2305 static const struct i915_subtest tests[] = {
2306 SUBTEST(igt_ppgtt_alloc),
2307 SUBTEST(igt_ppgtt_lowlevel),
2308 SUBTEST(igt_ppgtt_drunk),
2309 SUBTEST(igt_ppgtt_walk),
2310 SUBTEST(igt_ppgtt_pot),
2311 SUBTEST(igt_ppgtt_fill),
2312 SUBTEST(igt_ppgtt_shrink),
2313 SUBTEST(igt_ppgtt_shrink_boom),
2314 SUBTEST(igt_ppgtt_misaligned_pin),
2315 SUBTEST(igt_ggtt_lowlevel),
2316 SUBTEST(igt_ggtt_drunk),
2317 SUBTEST(igt_ggtt_walk),
2318 SUBTEST(igt_ggtt_pot),
2319 SUBTEST(igt_ggtt_fill),
2320 SUBTEST(igt_ggtt_page),
2321 SUBTEST(igt_ggtt_misaligned_pin),
2322 SUBTEST(igt_cs_tlb),
2325 GEM_BUG_ON(offset_in_page(to_gt(i915)->ggtt->vm.total));
2327 return i915_subtests(tests, i915);