2 * SPDX-License-Identifier: MIT
4 * Copyright © 2017 Intel Corporation
7 #include <linux/prime_numbers.h>
9 #include "i915_selftest.h"
11 #include "gem/i915_gem_region.h"
12 #include "gem/i915_gem_lmem.h"
13 #include "gem/i915_gem_pm.h"
15 #include "gt/intel_gt.h"
17 #include "igt_gem_utils.h"
18 #include "mock_context.h"
20 #include "selftests/mock_drm.h"
21 #include "selftests/mock_gem_device.h"
22 #include "selftests/mock_region.h"
23 #include "selftests/i915_random.h"
25 static const unsigned int page_sizes[] = {
26 I915_GTT_PAGE_SIZE_2M,
27 I915_GTT_PAGE_SIZE_64K,
28 I915_GTT_PAGE_SIZE_4K,
31 static unsigned int get_largest_page_size(struct drm_i915_private *i915,
36 for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
37 unsigned int page_size = page_sizes[i];
39 if (HAS_PAGE_SIZES(i915, page_size) && rem >= page_size)
46 static void huge_pages_free_pages(struct sg_table *st)
48 struct scatterlist *sg;
50 for (sg = st->sgl; sg; sg = __sg_next(sg)) {
52 __free_pages(sg_page(sg), get_order(sg->length));
59 static int get_huge_pages(struct drm_i915_gem_object *obj)
61 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
62 unsigned int page_mask = obj->mm.page_mask;
64 struct scatterlist *sg;
65 unsigned int sg_page_sizes;
68 st = kmalloc(sizeof(*st), GFP);
72 if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
83 * Our goal here is simple, we want to greedily fill the object from
84 * largest to smallest page-size, while ensuring that we use *every*
85 * page-size as per the given page-mask.
88 unsigned int bit = ilog2(page_mask);
89 unsigned int page_size = BIT(bit);
90 int order = get_order(page_size);
95 GEM_BUG_ON(order >= MAX_ORDER);
96 page = alloc_pages(GFP | __GFP_ZERO, order);
100 sg_set_page(sg, page, page_size, 0);
101 sg_page_sizes |= page_size;
111 } while ((rem - ((page_size-1) & page_mask)) >= page_size);
113 page_mask &= (page_size-1);
116 if (i915_gem_gtt_prepare_pages(obj, st))
119 GEM_BUG_ON(sg_page_sizes != obj->mm.page_mask);
120 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
125 sg_set_page(sg, NULL, 0, 0);
127 huge_pages_free_pages(st);
132 static void put_huge_pages(struct drm_i915_gem_object *obj,
133 struct sg_table *pages)
135 i915_gem_gtt_finish_pages(obj, pages);
136 huge_pages_free_pages(pages);
138 obj->mm.dirty = false;
141 static const struct drm_i915_gem_object_ops huge_page_ops = {
142 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
143 I915_GEM_OBJECT_IS_SHRINKABLE,
144 .get_pages = get_huge_pages,
145 .put_pages = put_huge_pages,
148 static struct drm_i915_gem_object *
149 huge_pages_object(struct drm_i915_private *i915,
151 unsigned int page_mask)
153 static struct lock_class_key lock_class;
154 struct drm_i915_gem_object *obj;
157 GEM_BUG_ON(!IS_ALIGNED(size, BIT(__ffs(page_mask))));
159 if (size >> PAGE_SHIFT > INT_MAX)
160 return ERR_PTR(-E2BIG);
162 if (overflows_type(size, obj->base.size))
163 return ERR_PTR(-E2BIG);
165 obj = i915_gem_object_alloc();
167 return ERR_PTR(-ENOMEM);
169 drm_gem_private_object_init(&i915->drm, &obj->base, size);
170 i915_gem_object_init(obj, &huge_page_ops, &lock_class);
172 i915_gem_object_set_volatile(obj);
174 obj->write_domain = I915_GEM_DOMAIN_CPU;
175 obj->read_domains = I915_GEM_DOMAIN_CPU;
176 obj->cache_level = I915_CACHE_NONE;
178 obj->mm.page_mask = page_mask;
183 static int fake_get_huge_pages(struct drm_i915_gem_object *obj)
185 struct drm_i915_private *i915 = to_i915(obj->base.dev);
186 const u64 max_len = rounddown_pow_of_two(UINT_MAX);
188 struct scatterlist *sg;
189 unsigned int sg_page_sizes;
192 st = kmalloc(sizeof(*st), GFP);
196 if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
201 /* Use optimal page sized chunks to fill in the sg table */
202 rem = obj->base.size;
207 unsigned int page_size = get_largest_page_size(i915, rem);
208 unsigned int len = min(page_size * div_u64(rem, page_size),
211 GEM_BUG_ON(!page_size);
215 sg_dma_len(sg) = len;
216 sg_dma_address(sg) = page_size;
218 sg_page_sizes |= len;
233 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
238 static int fake_get_huge_pages_single(struct drm_i915_gem_object *obj)
240 struct drm_i915_private *i915 = to_i915(obj->base.dev);
242 struct scatterlist *sg;
243 unsigned int page_size;
245 st = kmalloc(sizeof(*st), GFP);
249 if (sg_alloc_table(st, 1, GFP)) {
257 page_size = get_largest_page_size(i915, obj->base.size);
258 GEM_BUG_ON(!page_size);
261 sg->length = obj->base.size;
262 sg_dma_len(sg) = obj->base.size;
263 sg_dma_address(sg) = page_size;
265 __i915_gem_object_set_pages(obj, st, sg->length);
271 static void fake_free_huge_pages(struct drm_i915_gem_object *obj,
272 struct sg_table *pages)
274 sg_free_table(pages);
278 static void fake_put_huge_pages(struct drm_i915_gem_object *obj,
279 struct sg_table *pages)
281 fake_free_huge_pages(obj, pages);
282 obj->mm.dirty = false;
285 static const struct drm_i915_gem_object_ops fake_ops = {
286 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
287 .get_pages = fake_get_huge_pages,
288 .put_pages = fake_put_huge_pages,
291 static const struct drm_i915_gem_object_ops fake_ops_single = {
292 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
293 .get_pages = fake_get_huge_pages_single,
294 .put_pages = fake_put_huge_pages,
297 static struct drm_i915_gem_object *
298 fake_huge_pages_object(struct drm_i915_private *i915, u64 size, bool single)
300 static struct lock_class_key lock_class;
301 struct drm_i915_gem_object *obj;
304 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
306 if (size >> PAGE_SHIFT > UINT_MAX)
307 return ERR_PTR(-E2BIG);
309 if (overflows_type(size, obj->base.size))
310 return ERR_PTR(-E2BIG);
312 obj = i915_gem_object_alloc();
314 return ERR_PTR(-ENOMEM);
316 drm_gem_private_object_init(&i915->drm, &obj->base, size);
319 i915_gem_object_init(obj, &fake_ops_single, &lock_class);
321 i915_gem_object_init(obj, &fake_ops, &lock_class);
323 i915_gem_object_set_volatile(obj);
325 obj->write_domain = I915_GEM_DOMAIN_CPU;
326 obj->read_domains = I915_GEM_DOMAIN_CPU;
327 obj->cache_level = I915_CACHE_NONE;
332 static int igt_check_page_sizes(struct i915_vma *vma)
334 struct drm_i915_private *i915 = vma->vm->i915;
335 unsigned int supported = INTEL_INFO(i915)->page_sizes;
336 struct drm_i915_gem_object *obj = vma->obj;
339 /* We have to wait for the async bind to complete before our asserts */
340 err = i915_vma_sync(vma);
344 if (!HAS_PAGE_SIZES(i915, vma->page_sizes.sg)) {
345 pr_err("unsupported page_sizes.sg=%u, supported=%u\n",
346 vma->page_sizes.sg & ~supported, supported);
350 if (!HAS_PAGE_SIZES(i915, vma->page_sizes.gtt)) {
351 pr_err("unsupported page_sizes.gtt=%u, supported=%u\n",
352 vma->page_sizes.gtt & ~supported, supported);
356 if (vma->page_sizes.phys != obj->mm.page_sizes.phys) {
357 pr_err("vma->page_sizes.phys(%u) != obj->mm.page_sizes.phys(%u)\n",
358 vma->page_sizes.phys, obj->mm.page_sizes.phys);
362 if (vma->page_sizes.sg != obj->mm.page_sizes.sg) {
363 pr_err("vma->page_sizes.sg(%u) != obj->mm.page_sizes.sg(%u)\n",
364 vma->page_sizes.sg, obj->mm.page_sizes.sg);
368 if (obj->mm.page_sizes.gtt) {
369 pr_err("obj->page_sizes.gtt(%u) should never be set\n",
370 obj->mm.page_sizes.gtt);
377 static int igt_mock_exhaust_device_supported_pages(void *arg)
379 struct i915_ppgtt *ppgtt = arg;
380 struct drm_i915_private *i915 = ppgtt->vm.i915;
381 unsigned int saved_mask = INTEL_INFO(i915)->page_sizes;
382 struct drm_i915_gem_object *obj;
383 struct i915_vma *vma;
388 * Sanity check creating objects with every valid page support
389 * combination for our mock device.
392 for (i = 1; i < BIT(ARRAY_SIZE(page_sizes)); i++) {
393 unsigned int combination = 0;
395 for (j = 0; j < ARRAY_SIZE(page_sizes); j++) {
397 combination |= page_sizes[j];
400 mkwrite_device_info(i915)->page_sizes = combination;
402 for (single = 0; single <= 1; ++single) {
403 obj = fake_huge_pages_object(i915, combination, !!single);
409 if (obj->base.size != combination) {
410 pr_err("obj->base.size=%zu, expected=%u\n",
411 obj->base.size, combination);
416 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
422 err = i915_vma_pin(vma, 0, 0, PIN_USER);
426 err = igt_check_page_sizes(vma);
428 if (vma->page_sizes.sg != combination) {
429 pr_err("page_sizes.sg=%u, expected=%u\n",
430 vma->page_sizes.sg, combination);
437 i915_gem_object_put(obj);
449 i915_gem_object_put(obj);
451 mkwrite_device_info(i915)->page_sizes = saved_mask;
456 static int igt_mock_memory_region_huge_pages(void *arg)
458 const unsigned int flags[] = { 0, I915_BO_ALLOC_CONTIGUOUS };
459 struct i915_ppgtt *ppgtt = arg;
460 struct drm_i915_private *i915 = ppgtt->vm.i915;
461 unsigned long supported = INTEL_INFO(i915)->page_sizes;
462 struct intel_memory_region *mem;
463 struct drm_i915_gem_object *obj;
464 struct i915_vma *vma;
468 mem = mock_region_create(i915, 0, SZ_2G, I915_GTT_PAGE_SIZE_4K, 0);
470 pr_err("%s failed to create memory region\n", __func__);
474 for_each_set_bit(bit, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
475 unsigned int page_size = BIT(bit);
476 resource_size_t phys;
479 for (i = 0; i < ARRAY_SIZE(flags); ++i) {
480 obj = i915_gem_object_create_region(mem, page_size,
487 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
493 err = i915_vma_pin(vma, 0, 0, PIN_USER);
497 err = igt_check_page_sizes(vma);
501 phys = i915_gem_object_get_dma_address(obj, 0);
502 if (!IS_ALIGNED(phys, page_size)) {
503 pr_err("%s addr misaligned(%pa) page_size=%u\n",
504 __func__, &phys, page_size);
509 if (vma->page_sizes.gtt != page_size) {
510 pr_err("%s page_sizes.gtt=%u, expected=%u\n",
511 __func__, vma->page_sizes.gtt,
520 __i915_gem_object_put_pages(obj);
521 i915_gem_object_put(obj);
532 i915_gem_object_put(obj);
534 intel_memory_region_put(mem);
538 static int igt_mock_ppgtt_misaligned_dma(void *arg)
540 struct i915_ppgtt *ppgtt = arg;
541 struct drm_i915_private *i915 = ppgtt->vm.i915;
542 unsigned long supported = INTEL_INFO(i915)->page_sizes;
543 struct drm_i915_gem_object *obj;
548 * Sanity check dma misalignment for huge pages -- the dma addresses we
549 * insert into the paging structures need to always respect the page
553 bit = ilog2(I915_GTT_PAGE_SIZE_64K);
555 for_each_set_bit_from(bit, &supported,
556 ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
557 IGT_TIMEOUT(end_time);
558 unsigned int page_size = BIT(bit);
559 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
562 round_up(page_size, I915_GTT_PAGE_SIZE_2M) << 1;
563 struct i915_vma *vma;
565 obj = fake_huge_pages_object(i915, size, true);
569 if (obj->base.size != size) {
570 pr_err("obj->base.size=%zu, expected=%u\n",
571 obj->base.size, size);
576 err = i915_gem_object_pin_pages(obj);
580 /* Force the page size for this object */
581 obj->mm.page_sizes.sg = page_size;
583 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
589 err = i915_vma_pin(vma, 0, 0, flags);
596 err = igt_check_page_sizes(vma);
598 if (vma->page_sizes.gtt != page_size) {
599 pr_err("page_sizes.gtt=%u, expected %u\n",
600 vma->page_sizes.gtt, page_size);
612 * Try all the other valid offsets until the next
613 * boundary -- should always fall back to using 4K
616 for (offset = 4096; offset < page_size; offset += 4096) {
617 err = i915_vma_unbind(vma);
623 err = i915_vma_pin(vma, 0, 0, flags | offset);
629 err = igt_check_page_sizes(vma);
631 if (vma->page_sizes.gtt != I915_GTT_PAGE_SIZE_4K) {
632 pr_err("page_sizes.gtt=%u, expected %llu\n",
633 vma->page_sizes.gtt, I915_GTT_PAGE_SIZE_4K);
644 if (igt_timeout(end_time,
645 "%s timed out at offset %x with page-size %x\n",
646 __func__, offset, page_size))
652 i915_gem_object_unpin_pages(obj);
653 __i915_gem_object_put_pages(obj);
654 i915_gem_object_put(obj);
660 i915_gem_object_unpin_pages(obj);
662 i915_gem_object_put(obj);
667 static void close_object_list(struct list_head *objects,
668 struct i915_ppgtt *ppgtt)
670 struct drm_i915_gem_object *obj, *on;
672 list_for_each_entry_safe(obj, on, objects, st_link) {
673 struct i915_vma *vma;
675 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
679 list_del(&obj->st_link);
680 i915_gem_object_unpin_pages(obj);
681 __i915_gem_object_put_pages(obj);
682 i915_gem_object_put(obj);
686 static int igt_mock_ppgtt_huge_fill(void *arg)
688 struct i915_ppgtt *ppgtt = arg;
689 struct drm_i915_private *i915 = ppgtt->vm.i915;
690 unsigned long max_pages = ppgtt->vm.total >> PAGE_SHIFT;
691 unsigned long page_num;
694 IGT_TIMEOUT(end_time);
697 for_each_prime_number_from(page_num, 1, max_pages) {
698 struct drm_i915_gem_object *obj;
699 u64 size = page_num << PAGE_SHIFT;
700 struct i915_vma *vma;
701 unsigned int expected_gtt = 0;
704 obj = fake_huge_pages_object(i915, size, single);
710 if (obj->base.size != size) {
711 pr_err("obj->base.size=%zd, expected=%llu\n",
712 obj->base.size, size);
713 i915_gem_object_put(obj);
718 err = i915_gem_object_pin_pages(obj);
720 i915_gem_object_put(obj);
724 list_add(&obj->st_link, &objects);
726 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
732 err = i915_vma_pin(vma, 0, 0, PIN_USER);
736 err = igt_check_page_sizes(vma);
743 * Figure out the expected gtt page size knowing that we go from
744 * largest to smallest page size sg chunks, and that we align to
745 * the largest page size.
747 for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
748 unsigned int page_size = page_sizes[i];
750 if (HAS_PAGE_SIZES(i915, page_size) &&
752 expected_gtt |= page_size;
757 GEM_BUG_ON(!expected_gtt);
760 if (expected_gtt & I915_GTT_PAGE_SIZE_4K)
761 expected_gtt &= ~I915_GTT_PAGE_SIZE_64K;
765 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
766 if (!IS_ALIGNED(vma->node.start,
767 I915_GTT_PAGE_SIZE_2M)) {
768 pr_err("node.start(%llx) not aligned to 2M\n",
774 if (!IS_ALIGNED(vma->node.size,
775 I915_GTT_PAGE_SIZE_2M)) {
776 pr_err("node.size(%llx) not aligned to 2M\n",
783 if (vma->page_sizes.gtt != expected_gtt) {
784 pr_err("gtt=%u, expected=%u, size=%zd, single=%s\n",
785 vma->page_sizes.gtt, expected_gtt,
786 obj->base.size, yesno(!!single));
791 if (igt_timeout(end_time,
792 "%s timed out at size %zd\n",
793 __func__, obj->base.size))
799 close_object_list(&objects, ppgtt);
801 if (err == -ENOMEM || err == -ENOSPC)
807 static int igt_mock_ppgtt_64K(void *arg)
809 struct i915_ppgtt *ppgtt = arg;
810 struct drm_i915_private *i915 = ppgtt->vm.i915;
811 struct drm_i915_gem_object *obj;
812 const struct object_info {
817 /* Cases with forced padding/alignment */
820 .gtt = I915_GTT_PAGE_SIZE_64K,
824 .size = SZ_64K + SZ_4K,
825 .gtt = I915_GTT_PAGE_SIZE_4K,
829 .size = SZ_64K - SZ_4K,
830 .gtt = I915_GTT_PAGE_SIZE_4K,
835 .gtt = I915_GTT_PAGE_SIZE_64K,
839 .size = SZ_2M - SZ_4K,
840 .gtt = I915_GTT_PAGE_SIZE_4K,
844 .size = SZ_2M + SZ_4K,
845 .gtt = I915_GTT_PAGE_SIZE_64K | I915_GTT_PAGE_SIZE_4K,
849 .size = SZ_2M + SZ_64K,
850 .gtt = I915_GTT_PAGE_SIZE_64K,
854 .size = SZ_2M - SZ_64K,
855 .gtt = I915_GTT_PAGE_SIZE_64K,
858 /* Try without any forced padding/alignment */
862 .gtt = I915_GTT_PAGE_SIZE_4K,
866 .offset = SZ_2M - SZ_64K,
867 .gtt = I915_GTT_PAGE_SIZE_4K,
870 struct i915_vma *vma;
875 * Sanity check some of the trickiness with 64K pages -- either we can
876 * safely mark the whole page-table(2M block) as 64K, or we have to
877 * always fallback to 4K.
880 if (!HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K))
883 for (i = 0; i < ARRAY_SIZE(objects); ++i) {
884 unsigned int size = objects[i].size;
885 unsigned int expected_gtt = objects[i].gtt;
886 unsigned int offset = objects[i].offset;
887 unsigned int flags = PIN_USER;
889 for (single = 0; single <= 1; single++) {
890 obj = fake_huge_pages_object(i915, size, !!single);
894 err = i915_gem_object_pin_pages(obj);
899 * Disable 2M pages -- We only want to use 64K/4K pages
902 obj->mm.page_sizes.sg &= ~I915_GTT_PAGE_SIZE_2M;
904 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
907 goto out_object_unpin;
911 flags |= PIN_OFFSET_FIXED | offset;
913 err = i915_vma_pin(vma, 0, 0, flags);
917 err = igt_check_page_sizes(vma);
921 if (!offset && vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
922 if (!IS_ALIGNED(vma->node.start,
923 I915_GTT_PAGE_SIZE_2M)) {
924 pr_err("node.start(%llx) not aligned to 2M\n",
930 if (!IS_ALIGNED(vma->node.size,
931 I915_GTT_PAGE_SIZE_2M)) {
932 pr_err("node.size(%llx) not aligned to 2M\n",
939 if (vma->page_sizes.gtt != expected_gtt) {
940 pr_err("gtt=%u, expected=%u, i=%d, single=%s\n",
941 vma->page_sizes.gtt, expected_gtt, i,
950 i915_gem_object_unpin_pages(obj);
951 __i915_gem_object_put_pages(obj);
952 i915_gem_object_put(obj);
963 i915_gem_object_unpin_pages(obj);
965 i915_gem_object_put(obj);
970 static int gpu_write(struct intel_context *ce,
971 struct i915_vma *vma,
977 i915_gem_object_lock(vma->obj);
978 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
979 i915_gem_object_unlock(vma->obj);
983 return igt_gpu_fill_dw(ce, vma, dw * sizeof(u32),
984 vma->size >> PAGE_SHIFT, val);
988 __cpu_check_shmem(struct drm_i915_gem_object *obj, u32 dword, u32 val)
990 unsigned int needs_flush;
994 err = i915_gem_object_prepare_read(obj, &needs_flush);
998 for (n = 0; n < obj->base.size >> PAGE_SHIFT; ++n) {
999 u32 *ptr = kmap_atomic(i915_gem_object_get_page(obj, n));
1001 if (needs_flush & CLFLUSH_BEFORE)
1002 drm_clflush_virt_range(ptr, PAGE_SIZE);
1004 if (ptr[dword] != val) {
1005 pr_err("n=%lu ptr[%u]=%u, val=%u\n",
1006 n, dword, ptr[dword], val);
1015 i915_gem_object_finish_access(obj);
1020 static int __cpu_check_vmap(struct drm_i915_gem_object *obj, u32 dword, u32 val)
1022 unsigned long n = obj->base.size >> PAGE_SHIFT;
1026 err = i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT);
1030 ptr = i915_gem_object_pin_map(obj, I915_MAP_WC);
1032 return PTR_ERR(ptr);
1037 pr_err("base[%u]=%08x, val=%08x\n",
1043 ptr += PAGE_SIZE / sizeof(*ptr);
1046 i915_gem_object_unpin_map(obj);
1050 static int cpu_check(struct drm_i915_gem_object *obj, u32 dword, u32 val)
1052 if (i915_gem_object_has_struct_page(obj))
1053 return __cpu_check_shmem(obj, dword, val);
1055 return __cpu_check_vmap(obj, dword, val);
1058 static int __igt_write_huge(struct intel_context *ce,
1059 struct drm_i915_gem_object *obj,
1060 u64 size, u64 offset,
1063 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1064 struct i915_vma *vma;
1067 vma = i915_vma_instance(obj, ce->vm, NULL);
1069 return PTR_ERR(vma);
1071 err = i915_vma_unbind(vma);
1075 err = i915_vma_pin(vma, size, 0, flags | offset);
1078 * The ggtt may have some pages reserved so
1079 * refrain from erroring out.
1081 if (err == -ENOSPC && i915_is_ggtt(ce->vm))
1087 err = igt_check_page_sizes(vma);
1091 err = gpu_write(ce, vma, dword, val);
1093 pr_err("gpu-write failed at offset=%llx\n", offset);
1097 err = cpu_check(obj, dword, val);
1099 pr_err("cpu-check failed at offset=%llx\n", offset);
1104 i915_vma_unpin(vma);
1106 __i915_vma_put(vma);
1110 static int igt_write_huge(struct i915_gem_context *ctx,
1111 struct drm_i915_gem_object *obj)
1113 struct i915_gem_engines *engines;
1114 struct i915_gem_engines_iter it;
1115 struct intel_context *ce;
1116 I915_RND_STATE(prng);
1117 IGT_TIMEOUT(end_time);
1118 unsigned int max_page_size;
1127 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1129 size = obj->base.size;
1130 if (obj->mm.page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
1131 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
1136 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1138 if (!intel_engine_can_store_dword(ce->engine))
1141 max = min(max, ce->vm->total);
1144 i915_gem_context_unlock_engines(ctx);
1149 * To keep things interesting when alternating between engines in our
1150 * randomized order, lets also make feeding to the same engine a few
1151 * times in succession a possibility by enlarging the permutation array.
1153 order = i915_random_order(count * count, &prng);
1157 max_page_size = rounddown_pow_of_two(obj->mm.page_sizes.sg);
1158 max = div_u64(max - size, max_page_size);
1161 * Try various offsets in an ascending/descending fashion until we
1162 * timeout -- we want to avoid issues hidden by effectively always using
1166 engines = i915_gem_context_lock_engines(ctx);
1167 for_each_prime_number_from(num, 0, max) {
1168 u64 offset_low = num * max_page_size;
1169 u64 offset_high = (max - num) * max_page_size;
1170 u32 dword = offset_in_page(num) / 4;
1171 struct intel_context *ce;
1173 ce = engines->engines[order[i] % engines->num_engines];
1174 i = (i + 1) % (count * count);
1175 if (!ce || !intel_engine_can_store_dword(ce->engine))
1179 * In order to utilize 64K pages we need to both pad the vma
1180 * size and ensure the vma offset is at the start of the pt
1181 * boundary, however to improve coverage we opt for testing both
1182 * aligned and unaligned offsets.
1184 if (obj->mm.page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
1185 offset_low = round_down(offset_low,
1186 I915_GTT_PAGE_SIZE_2M);
1188 err = __igt_write_huge(ce, obj, size, offset_low,
1193 err = __igt_write_huge(ce, obj, size, offset_high,
1198 if (igt_timeout(end_time,
1199 "%s timed out on %s, offset_low=%llx offset_high=%llx, max_page_size=%x\n",
1200 __func__, ce->engine->name, offset_low, offset_high,
1204 i915_gem_context_unlock_engines(ctx);
1211 typedef struct drm_i915_gem_object *
1212 (*igt_create_fn)(struct drm_i915_private *i915, u32 size, u32 flags);
1214 static inline bool igt_can_allocate_thp(struct drm_i915_private *i915)
1216 return i915->mm.gemfs && has_transparent_hugepage();
1219 static struct drm_i915_gem_object *
1220 igt_create_shmem(struct drm_i915_private *i915, u32 size, u32 flags)
1222 if (!igt_can_allocate_thp(i915)) {
1223 pr_info("%s missing THP support, skipping\n", __func__);
1224 return ERR_PTR(-ENODEV);
1227 return i915_gem_object_create_shmem(i915, size);
1230 static struct drm_i915_gem_object *
1231 igt_create_internal(struct drm_i915_private *i915, u32 size, u32 flags)
1233 return i915_gem_object_create_internal(i915, size);
1236 static struct drm_i915_gem_object *
1237 igt_create_system(struct drm_i915_private *i915, u32 size, u32 flags)
1239 return huge_pages_object(i915, size, size);
1242 static struct drm_i915_gem_object *
1243 igt_create_local(struct drm_i915_private *i915, u32 size, u32 flags)
1245 return i915_gem_object_create_lmem(i915, size, flags);
1248 static u32 igt_random_size(struct rnd_state *prng,
1255 GEM_BUG_ON(!is_power_of_2(min_page_size));
1256 GEM_BUG_ON(!is_power_of_2(max_page_size));
1257 GEM_BUG_ON(min_page_size < PAGE_SIZE);
1258 GEM_BUG_ON(min_page_size > max_page_size);
1260 mask = ((max_page_size << 1ULL) - 1) & PAGE_MASK;
1261 size = prandom_u32_state(prng) & mask;
1262 if (size < min_page_size)
1263 size |= min_page_size;
1268 static int igt_ppgtt_smoke_huge(void *arg)
1270 struct i915_gem_context *ctx = arg;
1271 struct drm_i915_private *i915 = ctx->i915;
1272 struct drm_i915_gem_object *obj;
1273 I915_RND_STATE(prng);
1279 { igt_create_internal, SZ_64K, SZ_2M, },
1280 { igt_create_shmem, SZ_64K, SZ_32M, },
1281 { igt_create_local, SZ_64K, SZ_1G, },
1287 * Sanity check that the HW uses huge pages correctly through our
1288 * various backends -- ensure that our writes land in the right place.
1291 for (i = 0; i < ARRAY_SIZE(backends); ++i) {
1292 u32 min = backends[i].min;
1293 u32 max = backends[i].max;
1296 size = igt_random_size(&prng, min, rounddown_pow_of_two(size));
1298 obj = backends[i].fn(i915, size, 0);
1301 if (err == -E2BIG) {
1304 } else if (err == -ENODEV) {
1312 err = i915_gem_object_pin_pages(obj);
1314 if (err == -ENXIO || err == -E2BIG) {
1315 i915_gem_object_put(obj);
1322 if (obj->mm.page_sizes.phys < min) {
1323 pr_info("%s unable to allocate huge-page(s) with size=%u, i=%d\n",
1329 err = igt_write_huge(ctx, obj);
1331 pr_err("%s write-huge failed with size=%u, i=%d\n",
1335 i915_gem_object_unpin_pages(obj);
1336 __i915_gem_object_put_pages(obj);
1338 i915_gem_object_put(obj);
1340 if (err == -ENOMEM || err == -ENXIO)
1352 static int igt_ppgtt_sanity_check(void *arg)
1354 struct i915_gem_context *ctx = arg;
1355 struct drm_i915_private *i915 = ctx->i915;
1356 unsigned int supported = INTEL_INFO(i915)->page_sizes;
1361 { igt_create_system, 0, },
1362 { igt_create_local, I915_BO_ALLOC_CONTIGUOUS, },
1371 { SZ_2M - SZ_64K, SZ_64K },
1372 { SZ_2M - SZ_4K, SZ_64K | SZ_4K },
1373 { SZ_2M + SZ_4K, SZ_64K | SZ_4K },
1374 { SZ_2M + SZ_4K, SZ_2M | SZ_4K },
1375 { SZ_2M + SZ_64K, SZ_2M | SZ_64K },
1380 if (supported == I915_GTT_PAGE_SIZE_4K)
1384 * Sanity check that the HW behaves with a limited set of combinations.
1385 * We already have a bunch of randomised testing, which should give us
1386 * a decent amount of variation between runs, however we should keep
1387 * this to limit the chances of introducing a temporary regression, by
1388 * testing the most obvious cases that might make something blow up.
1391 for (i = 0; i < ARRAY_SIZE(backends); ++i) {
1392 for (j = 0; j < ARRAY_SIZE(combos); ++j) {
1393 struct drm_i915_gem_object *obj;
1394 u32 size = combos[j].size;
1395 u32 pages = combos[j].pages;
1397 obj = backends[i].fn(i915, size, backends[i].flags);
1400 if (err == -ENODEV) {
1401 pr_info("Device lacks local memory, skipping\n");
1409 err = i915_gem_object_pin_pages(obj);
1411 i915_gem_object_put(obj);
1415 GEM_BUG_ON(pages > obj->base.size);
1416 pages = pages & supported;
1419 obj->mm.page_sizes.sg = pages;
1421 err = igt_write_huge(ctx, obj);
1423 i915_gem_object_unpin_pages(obj);
1424 __i915_gem_object_put_pages(obj);
1425 i915_gem_object_put(obj);
1428 pr_err("%s write-huge failed with size=%u pages=%u i=%d, j=%d\n",
1429 __func__, size, pages, i, j);
1444 static int igt_ppgtt_pin_update(void *arg)
1446 struct i915_gem_context *ctx = arg;
1447 struct drm_i915_private *dev_priv = ctx->i915;
1448 unsigned long supported = INTEL_INFO(dev_priv)->page_sizes;
1449 struct drm_i915_gem_object *obj;
1450 struct i915_gem_engines_iter it;
1451 struct i915_address_space *vm;
1452 struct intel_context *ce;
1453 struct i915_vma *vma;
1454 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1460 * Make sure there's no funny business when doing a PIN_UPDATE -- in the
1461 * past we had a subtle issue with being able to incorrectly do multiple
1462 * alloc va ranges on the same object when doing a PIN_UPDATE, which
1463 * resulted in some pretty nasty bugs, though only when using
1467 vm = i915_gem_context_get_vm_rcu(ctx);
1468 if (!i915_vm_is_4lvl(vm)) {
1469 pr_info("48b PPGTT not supported, skipping\n");
1473 first = ilog2(I915_GTT_PAGE_SIZE_64K);
1474 last = ilog2(I915_GTT_PAGE_SIZE_2M);
1476 for_each_set_bit_from(first, &supported, last + 1) {
1477 unsigned int page_size = BIT(first);
1479 obj = i915_gem_object_create_internal(dev_priv, page_size);
1481 return PTR_ERR(obj);
1483 vma = i915_vma_instance(obj, vm, NULL);
1489 err = i915_vma_pin(vma, SZ_2M, 0, flags);
1493 if (vma->page_sizes.sg < page_size) {
1494 pr_info("Unable to allocate page-size %x, finishing test early\n",
1499 err = igt_check_page_sizes(vma);
1503 if (vma->page_sizes.gtt != page_size) {
1504 dma_addr_t addr = i915_gem_object_get_dma_address(obj, 0);
1507 * The only valid reason for this to ever fail would be
1508 * if the dma-mapper screwed us over when we did the
1509 * dma_map_sg(), since it has the final say over the dma
1512 if (IS_ALIGNED(addr, page_size)) {
1513 pr_err("page_sizes.gtt=%u, expected=%u\n",
1514 vma->page_sizes.gtt, page_size);
1517 pr_info("dma address misaligned, finishing test early\n");
1523 err = i915_vma_bind(vma, I915_CACHE_NONE, PIN_UPDATE, NULL);
1527 i915_vma_unpin(vma);
1528 i915_vma_close(vma);
1530 i915_gem_object_put(obj);
1533 obj = i915_gem_object_create_internal(dev_priv, PAGE_SIZE);
1535 return PTR_ERR(obj);
1537 vma = i915_vma_instance(obj, vm, NULL);
1543 err = i915_vma_pin(vma, 0, 0, flags);
1548 * Make sure we don't end up with something like where the pde is still
1549 * pointing to the 2M page, and the pt we just filled-in is dangling --
1550 * we can check this by writing to the first page where it would then
1551 * land in the now stale 2M page.
1555 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1556 if (!intel_engine_can_store_dword(ce->engine))
1559 err = gpu_write(ce, vma, n++, 0xdeadbeaf);
1563 i915_gem_context_unlock_engines(ctx);
1568 err = cpu_check(obj, n, 0xdeadbeaf);
1574 i915_vma_unpin(vma);
1576 i915_vma_close(vma);
1578 i915_gem_object_put(obj);
1585 static int igt_tmpfs_fallback(void *arg)
1587 struct i915_gem_context *ctx = arg;
1588 struct drm_i915_private *i915 = ctx->i915;
1589 struct vfsmount *gemfs = i915->mm.gemfs;
1590 struct i915_address_space *vm = i915_gem_context_get_vm_rcu(ctx);
1591 struct drm_i915_gem_object *obj;
1592 struct i915_vma *vma;
1597 * Make sure that we don't burst into a ball of flames upon falling back
1598 * to tmpfs, which we rely on if on the off-chance we encouter a failure
1599 * when setting up gemfs.
1602 i915->mm.gemfs = NULL;
1604 obj = i915_gem_object_create_shmem(i915, PAGE_SIZE);
1610 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1611 if (IS_ERR(vaddr)) {
1612 err = PTR_ERR(vaddr);
1615 *vaddr = 0xdeadbeaf;
1617 __i915_gem_object_flush_map(obj, 0, 64);
1618 i915_gem_object_unpin_map(obj);
1620 vma = i915_vma_instance(obj, vm, NULL);
1626 err = i915_vma_pin(vma, 0, 0, PIN_USER);
1630 err = igt_check_page_sizes(vma);
1632 i915_vma_unpin(vma);
1634 i915_vma_close(vma);
1636 i915_gem_object_put(obj);
1638 i915->mm.gemfs = gemfs;
1644 static int igt_shrink_thp(void *arg)
1646 struct i915_gem_context *ctx = arg;
1647 struct drm_i915_private *i915 = ctx->i915;
1648 struct i915_address_space *vm = i915_gem_context_get_vm_rcu(ctx);
1649 struct drm_i915_gem_object *obj;
1650 struct i915_gem_engines_iter it;
1651 struct intel_context *ce;
1652 struct i915_vma *vma;
1653 unsigned int flags = PIN_USER;
1658 * Sanity check shrinking huge-paged object -- make sure nothing blows
1662 if (!igt_can_allocate_thp(i915)) {
1663 pr_info("missing THP support, skipping\n");
1667 obj = i915_gem_object_create_shmem(i915, SZ_2M);
1673 vma = i915_vma_instance(obj, vm, NULL);
1679 err = i915_vma_pin(vma, 0, 0, flags);
1683 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
1684 pr_info("failed to allocate THP, finishing test early\n");
1688 err = igt_check_page_sizes(vma);
1694 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1695 if (!intel_engine_can_store_dword(ce->engine))
1698 err = gpu_write(ce, vma, n++, 0xdeadbeaf);
1702 i915_gem_context_unlock_engines(ctx);
1703 i915_vma_unpin(vma);
1708 * Now that the pages are *unpinned* shrink-all should invoke
1709 * shmem to truncate our pages.
1711 i915_gem_shrink_all(i915);
1712 if (i915_gem_object_has_pages(obj)) {
1713 pr_err("shrink-all didn't truncate the pages\n");
1718 if (obj->mm.page_sizes.sg || obj->mm.page_sizes.phys) {
1719 pr_err("residual page-size bits left\n");
1724 err = i915_vma_pin(vma, 0, 0, flags);
1729 err = cpu_check(obj, n, 0xdeadbeaf);
1735 i915_vma_unpin(vma);
1737 i915_vma_close(vma);
1739 i915_gem_object_put(obj);
1746 int i915_gem_huge_page_mock_selftests(void)
1748 static const struct i915_subtest tests[] = {
1749 SUBTEST(igt_mock_exhaust_device_supported_pages),
1750 SUBTEST(igt_mock_memory_region_huge_pages),
1751 SUBTEST(igt_mock_ppgtt_misaligned_dma),
1752 SUBTEST(igt_mock_ppgtt_huge_fill),
1753 SUBTEST(igt_mock_ppgtt_64K),
1755 struct drm_i915_private *dev_priv;
1756 struct i915_ppgtt *ppgtt;
1759 dev_priv = mock_gem_device();
1763 /* Pretend to be a device which supports the 48b PPGTT */
1764 mkwrite_device_info(dev_priv)->ppgtt_type = INTEL_PPGTT_FULL;
1765 mkwrite_device_info(dev_priv)->ppgtt_size = 48;
1767 ppgtt = i915_ppgtt_create(&dev_priv->gt);
1768 if (IS_ERR(ppgtt)) {
1769 err = PTR_ERR(ppgtt);
1773 if (!i915_vm_is_4lvl(&ppgtt->vm)) {
1774 pr_err("failed to create 48b PPGTT\n");
1779 /* If we were ever hit this then it's time to mock the 64K scratch */
1780 if (!i915_vm_has_scratch_64K(&ppgtt->vm)) {
1781 pr_err("PPGTT missing 64K scratch page\n");
1786 err = i915_subtests(tests, ppgtt);
1789 i915_vm_put(&ppgtt->vm);
1792 drm_dev_put(&dev_priv->drm);
1796 int i915_gem_huge_page_live_selftests(struct drm_i915_private *i915)
1798 static const struct i915_subtest tests[] = {
1799 SUBTEST(igt_shrink_thp),
1800 SUBTEST(igt_ppgtt_pin_update),
1801 SUBTEST(igt_tmpfs_fallback),
1802 SUBTEST(igt_ppgtt_smoke_huge),
1803 SUBTEST(igt_ppgtt_sanity_check),
1805 struct i915_gem_context *ctx;
1806 struct i915_address_space *vm;
1810 if (!HAS_PPGTT(i915)) {
1811 pr_info("PPGTT not supported, skipping live-selftests\n");
1815 if (intel_gt_is_wedged(&i915->gt))
1818 file = mock_file(i915);
1820 return PTR_ERR(file);
1822 ctx = live_context(i915, file);
1828 mutex_lock(&ctx->mutex);
1829 vm = i915_gem_context_vm(ctx);
1831 WRITE_ONCE(vm->scrub_64K, true);
1832 mutex_unlock(&ctx->mutex);
1834 err = i915_subtests(tests, ctx);