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 static int igt_ppgtt_exhaust_huge(void *arg
)
1213 struct i915_gem_context
*ctx
= arg
;
1214 struct drm_i915_private
*i915
= ctx
->i915
;
1215 unsigned long supported
= INTEL_INFO(i915
)->page_sizes
;
1216 static unsigned int pages
[ARRAY_SIZE(page_sizes
)];
1217 struct drm_i915_gem_object
*obj
;
1218 unsigned int size_mask
;
1219 unsigned int page_mask
;
1223 if (supported
== I915_GTT_PAGE_SIZE_4K
)
1227 * Sanity check creating objects with a varying mix of page sizes --
1228 * ensuring that our writes lands in the right place.
1232 for_each_set_bit(i
, &supported
, ilog2(I915_GTT_MAX_PAGE_SIZE
) + 1)
1233 pages
[n
++] = BIT(i
);
1235 for (size_mask
= 2; size_mask
< BIT(n
); size_mask
++) {
1236 unsigned int size
= 0;
1238 for (i
= 0; i
< n
; i
++) {
1239 if (size_mask
& BIT(i
))
1244 * For our page mask we want to enumerate all the page-size
1245 * combinations which will fit into our chosen object size.
1247 for (page_mask
= 2; page_mask
<= size_mask
; page_mask
++) {
1248 unsigned int page_sizes
= 0;
1250 for (i
= 0; i
< n
; i
++) {
1251 if (page_mask
& BIT(i
))
1252 page_sizes
|= pages
[i
];
1256 * Ensure that we can actually fill the given object
1257 * with our chosen page mask.
1259 if (!IS_ALIGNED(size
, BIT(__ffs(page_sizes
))))
1262 obj
= huge_pages_object(i915
, size
, page_sizes
);
1268 err
= i915_gem_object_pin_pages(obj
);
1270 i915_gem_object_put(obj
);
1272 if (err
== -ENOMEM
) {
1273 pr_info("unable to get pages, size=%u, pages=%u\n",
1279 pr_err("pin_pages failed, size=%u, pages=%u\n",
1280 size_mask
, page_mask
);
1285 /* Force the page-size for the gtt insertion */
1286 obj
->mm
.page_sizes
.sg
= page_sizes
;
1288 err
= igt_write_huge(ctx
, obj
);
1290 pr_err("exhaust write-huge failed with size=%u\n",
1295 i915_gem_object_unpin_pages(obj
);
1296 __i915_gem_object_put_pages(obj
);
1297 i915_gem_object_put(obj
);
1304 i915_gem_object_unpin_pages(obj
);
1305 i915_gem_object_put(obj
);
1307 mkwrite_device_info(i915
)->page_sizes
= supported
;
1312 typedef struct drm_i915_gem_object
*
1313 (*igt_create_fn
)(struct drm_i915_private
*i915
, u32 size
, u32 flags
);
1315 static inline bool igt_can_allocate_thp(struct drm_i915_private
*i915
)
1317 return i915
->mm
.gemfs
&& has_transparent_hugepage();
1320 static struct drm_i915_gem_object
*
1321 igt_create_shmem(struct drm_i915_private
*i915
, u32 size
, u32 flags
)
1323 if (!igt_can_allocate_thp(i915
)) {
1324 pr_info("%s missing THP support, skipping\n", __func__
);
1325 return ERR_PTR(-ENODEV
);
1328 return i915_gem_object_create_shmem(i915
, size
);
1331 static struct drm_i915_gem_object
*
1332 igt_create_internal(struct drm_i915_private
*i915
, u32 size
, u32 flags
)
1334 return i915_gem_object_create_internal(i915
, size
);
1337 static struct drm_i915_gem_object
*
1338 igt_create_system(struct drm_i915_private
*i915
, u32 size
, u32 flags
)
1340 return huge_pages_object(i915
, size
, size
);
1343 static struct drm_i915_gem_object
*
1344 igt_create_local(struct drm_i915_private
*i915
, u32 size
, u32 flags
)
1346 return i915_gem_object_create_lmem(i915
, size
, flags
);
1349 static u32
igt_random_size(struct rnd_state
*prng
,
1356 GEM_BUG_ON(!is_power_of_2(min_page_size
));
1357 GEM_BUG_ON(!is_power_of_2(max_page_size
));
1358 GEM_BUG_ON(min_page_size
< PAGE_SIZE
);
1359 GEM_BUG_ON(min_page_size
> max_page_size
);
1361 mask
= ((max_page_size
<< 1ULL) - 1) & PAGE_MASK
;
1362 size
= prandom_u32_state(prng
) & mask
;
1363 if (size
< min_page_size
)
1364 size
|= min_page_size
;
1369 static int igt_ppgtt_smoke_huge(void *arg
)
1371 struct i915_gem_context
*ctx
= arg
;
1372 struct drm_i915_private
*i915
= ctx
->i915
;
1373 struct drm_i915_gem_object
*obj
;
1374 I915_RND_STATE(prng
);
1380 { igt_create_internal
, SZ_64K
, SZ_2M
, },
1381 { igt_create_shmem
, SZ_64K
, SZ_32M
, },
1382 { igt_create_local
, SZ_64K
, SZ_1G
, },
1388 * Sanity check that the HW uses huge pages correctly through our
1389 * various backends -- ensure that our writes land in the right place.
1392 for (i
= 0; i
< ARRAY_SIZE(backends
); ++i
) {
1393 u32 min
= backends
[i
].min
;
1394 u32 max
= backends
[i
].max
;
1397 size
= igt_random_size(&prng
, min
, rounddown_pow_of_two(size
));
1399 obj
= backends
[i
].fn(i915
, size
, 0);
1402 if (err
== -E2BIG
) {
1405 } else if (err
== -ENODEV
) {
1413 err
= i915_gem_object_pin_pages(obj
);
1415 if (err
== -ENXIO
|| err
== -E2BIG
) {
1416 i915_gem_object_put(obj
);
1423 if (obj
->mm
.page_sizes
.phys
< min
) {
1424 pr_info("%s unable to allocate huge-page(s) with size=%u, i=%d\n",
1430 err
= igt_write_huge(ctx
, obj
);
1432 pr_err("%s write-huge failed with size=%u, i=%d\n",
1436 i915_gem_object_unpin_pages(obj
);
1437 __i915_gem_object_put_pages(obj
);
1439 i915_gem_object_put(obj
);
1441 if (err
== -ENOMEM
|| err
== -ENXIO
)
1453 static int igt_ppgtt_sanity_check(void *arg
)
1455 struct i915_gem_context
*ctx
= arg
;
1456 struct drm_i915_private
*i915
= ctx
->i915
;
1457 unsigned int supported
= INTEL_INFO(i915
)->page_sizes
;
1462 { igt_create_system
, 0, },
1463 { igt_create_local
, I915_BO_ALLOC_CONTIGUOUS
, },
1472 { SZ_2M
- SZ_64K
, SZ_64K
},
1473 { SZ_2M
- SZ_4K
, SZ_64K
| SZ_4K
},
1474 { SZ_2M
+ SZ_4K
, SZ_64K
| SZ_4K
},
1475 { SZ_2M
+ SZ_4K
, SZ_2M
| SZ_4K
},
1476 { SZ_2M
+ SZ_64K
, SZ_2M
| SZ_64K
},
1481 if (supported
== I915_GTT_PAGE_SIZE_4K
)
1485 * Sanity check that the HW behaves with a limited set of combinations.
1486 * We already have a bunch of randomised testing, which should give us
1487 * a decent amount of variation between runs, however we should keep
1488 * this to limit the chances of introducing a temporary regression, by
1489 * testing the most obvious cases that might make something blow up.
1492 for (i
= 0; i
< ARRAY_SIZE(backends
); ++i
) {
1493 for (j
= 0; j
< ARRAY_SIZE(combos
); ++j
) {
1494 struct drm_i915_gem_object
*obj
;
1495 u32 size
= combos
[j
].size
;
1496 u32 pages
= combos
[j
].pages
;
1498 obj
= backends
[i
].fn(i915
, size
, backends
[i
].flags
);
1501 if (err
== -ENODEV
) {
1502 pr_info("Device lacks local memory, skipping\n");
1510 err
= i915_gem_object_pin_pages(obj
);
1512 i915_gem_object_put(obj
);
1516 GEM_BUG_ON(pages
> obj
->base
.size
);
1517 pages
= pages
& supported
;
1520 obj
->mm
.page_sizes
.sg
= pages
;
1522 err
= igt_write_huge(ctx
, obj
);
1524 i915_gem_object_unpin_pages(obj
);
1525 __i915_gem_object_put_pages(obj
);
1526 i915_gem_object_put(obj
);
1529 pr_err("%s write-huge failed with size=%u pages=%u i=%d, j=%d\n",
1530 __func__
, size
, pages
, i
, j
);
1545 static int igt_ppgtt_pin_update(void *arg
)
1547 struct i915_gem_context
*ctx
= arg
;
1548 struct drm_i915_private
*dev_priv
= ctx
->i915
;
1549 unsigned long supported
= INTEL_INFO(dev_priv
)->page_sizes
;
1550 struct drm_i915_gem_object
*obj
;
1551 struct i915_gem_engines_iter it
;
1552 struct i915_address_space
*vm
;
1553 struct intel_context
*ce
;
1554 struct i915_vma
*vma
;
1555 unsigned int flags
= PIN_USER
| PIN_OFFSET_FIXED
;
1561 * Make sure there's no funny business when doing a PIN_UPDATE -- in the
1562 * past we had a subtle issue with being able to incorrectly do multiple
1563 * alloc va ranges on the same object when doing a PIN_UPDATE, which
1564 * resulted in some pretty nasty bugs, though only when using
1568 vm
= i915_gem_context_get_vm_rcu(ctx
);
1569 if (!i915_vm_is_4lvl(vm
)) {
1570 pr_info("48b PPGTT not supported, skipping\n");
1574 first
= ilog2(I915_GTT_PAGE_SIZE_64K
);
1575 last
= ilog2(I915_GTT_PAGE_SIZE_2M
);
1577 for_each_set_bit_from(first
, &supported
, last
+ 1) {
1578 unsigned int page_size
= BIT(first
);
1580 obj
= i915_gem_object_create_internal(dev_priv
, page_size
);
1582 return PTR_ERR(obj
);
1584 vma
= i915_vma_instance(obj
, vm
, NULL
);
1590 err
= i915_vma_pin(vma
, SZ_2M
, 0, flags
);
1594 if (vma
->page_sizes
.sg
< page_size
) {
1595 pr_info("Unable to allocate page-size %x, finishing test early\n",
1600 err
= igt_check_page_sizes(vma
);
1604 if (vma
->page_sizes
.gtt
!= page_size
) {
1605 dma_addr_t addr
= i915_gem_object_get_dma_address(obj
, 0);
1608 * The only valid reason for this to ever fail would be
1609 * if the dma-mapper screwed us over when we did the
1610 * dma_map_sg(), since it has the final say over the dma
1613 if (IS_ALIGNED(addr
, page_size
)) {
1614 pr_err("page_sizes.gtt=%u, expected=%u\n",
1615 vma
->page_sizes
.gtt
, page_size
);
1618 pr_info("dma address misaligned, finishing test early\n");
1624 err
= i915_vma_bind(vma
, I915_CACHE_NONE
, PIN_UPDATE
, NULL
);
1628 i915_vma_unpin(vma
);
1629 i915_vma_close(vma
);
1631 i915_gem_object_put(obj
);
1634 obj
= i915_gem_object_create_internal(dev_priv
, PAGE_SIZE
);
1636 return PTR_ERR(obj
);
1638 vma
= i915_vma_instance(obj
, vm
, NULL
);
1644 err
= i915_vma_pin(vma
, 0, 0, flags
);
1649 * Make sure we don't end up with something like where the pde is still
1650 * pointing to the 2M page, and the pt we just filled-in is dangling --
1651 * we can check this by writing to the first page where it would then
1652 * land in the now stale 2M page.
1656 for_each_gem_engine(ce
, i915_gem_context_lock_engines(ctx
), it
) {
1657 if (!intel_engine_can_store_dword(ce
->engine
))
1660 err
= gpu_write(ce
, vma
, n
++, 0xdeadbeaf);
1664 i915_gem_context_unlock_engines(ctx
);
1669 err
= cpu_check(obj
, n
, 0xdeadbeaf);
1675 i915_vma_unpin(vma
);
1677 i915_vma_close(vma
);
1679 i915_gem_object_put(obj
);
1686 static int igt_tmpfs_fallback(void *arg
)
1688 struct i915_gem_context
*ctx
= arg
;
1689 struct drm_i915_private
*i915
= ctx
->i915
;
1690 struct vfsmount
*gemfs
= i915
->mm
.gemfs
;
1691 struct i915_address_space
*vm
= i915_gem_context_get_vm_rcu(ctx
);
1692 struct drm_i915_gem_object
*obj
;
1693 struct i915_vma
*vma
;
1698 * Make sure that we don't burst into a ball of flames upon falling back
1699 * to tmpfs, which we rely on if on the off-chance we encouter a failure
1700 * when setting up gemfs.
1703 i915
->mm
.gemfs
= NULL
;
1705 obj
= i915_gem_object_create_shmem(i915
, PAGE_SIZE
);
1711 vaddr
= i915_gem_object_pin_map(obj
, I915_MAP_WB
);
1712 if (IS_ERR(vaddr
)) {
1713 err
= PTR_ERR(vaddr
);
1716 *vaddr
= 0xdeadbeaf;
1718 __i915_gem_object_flush_map(obj
, 0, 64);
1719 i915_gem_object_unpin_map(obj
);
1721 vma
= i915_vma_instance(obj
, vm
, NULL
);
1727 err
= i915_vma_pin(vma
, 0, 0, PIN_USER
);
1731 err
= igt_check_page_sizes(vma
);
1733 i915_vma_unpin(vma
);
1735 i915_vma_close(vma
);
1737 i915_gem_object_put(obj
);
1739 i915
->mm
.gemfs
= gemfs
;
1745 static int igt_shrink_thp(void *arg
)
1747 struct i915_gem_context
*ctx
= arg
;
1748 struct drm_i915_private
*i915
= ctx
->i915
;
1749 struct i915_address_space
*vm
= i915_gem_context_get_vm_rcu(ctx
);
1750 struct drm_i915_gem_object
*obj
;
1751 struct i915_gem_engines_iter it
;
1752 struct intel_context
*ce
;
1753 struct i915_vma
*vma
;
1754 unsigned int flags
= PIN_USER
;
1759 * Sanity check shrinking huge-paged object -- make sure nothing blows
1763 if (!igt_can_allocate_thp(i915
)) {
1764 pr_info("missing THP support, skipping\n");
1768 obj
= i915_gem_object_create_shmem(i915
, SZ_2M
);
1774 vma
= i915_vma_instance(obj
, vm
, NULL
);
1780 err
= i915_vma_pin(vma
, 0, 0, flags
);
1784 if (obj
->mm
.page_sizes
.phys
< I915_GTT_PAGE_SIZE_2M
) {
1785 pr_info("failed to allocate THP, finishing test early\n");
1789 err
= igt_check_page_sizes(vma
);
1795 for_each_gem_engine(ce
, i915_gem_context_lock_engines(ctx
), it
) {
1796 if (!intel_engine_can_store_dword(ce
->engine
))
1799 err
= gpu_write(ce
, vma
, n
++, 0xdeadbeaf);
1803 i915_gem_context_unlock_engines(ctx
);
1804 i915_vma_unpin(vma
);
1809 * Now that the pages are *unpinned* shrink-all should invoke
1810 * shmem to truncate our pages.
1812 i915_gem_shrink_all(i915
);
1813 if (i915_gem_object_has_pages(obj
)) {
1814 pr_err("shrink-all didn't truncate the pages\n");
1819 if (obj
->mm
.page_sizes
.sg
|| obj
->mm
.page_sizes
.phys
) {
1820 pr_err("residual page-size bits left\n");
1825 err
= i915_vma_pin(vma
, 0, 0, flags
);
1830 err
= cpu_check(obj
, n
, 0xdeadbeaf);
1836 i915_vma_unpin(vma
);
1838 i915_vma_close(vma
);
1840 i915_gem_object_put(obj
);
1847 int i915_gem_huge_page_mock_selftests(void)
1849 static const struct i915_subtest tests
[] = {
1850 SUBTEST(igt_mock_exhaust_device_supported_pages
),
1851 SUBTEST(igt_mock_memory_region_huge_pages
),
1852 SUBTEST(igt_mock_ppgtt_misaligned_dma
),
1853 SUBTEST(igt_mock_ppgtt_huge_fill
),
1854 SUBTEST(igt_mock_ppgtt_64K
),
1856 struct drm_i915_private
*dev_priv
;
1857 struct i915_ppgtt
*ppgtt
;
1860 dev_priv
= mock_gem_device();
1864 /* Pretend to be a device which supports the 48b PPGTT */
1865 mkwrite_device_info(dev_priv
)->ppgtt_type
= INTEL_PPGTT_FULL
;
1866 mkwrite_device_info(dev_priv
)->ppgtt_size
= 48;
1868 ppgtt
= i915_ppgtt_create(&dev_priv
->gt
);
1869 if (IS_ERR(ppgtt
)) {
1870 err
= PTR_ERR(ppgtt
);
1874 if (!i915_vm_is_4lvl(&ppgtt
->vm
)) {
1875 pr_err("failed to create 48b PPGTT\n");
1880 /* If we were ever hit this then it's time to mock the 64K scratch */
1881 if (!i915_vm_has_scratch_64K(&ppgtt
->vm
)) {
1882 pr_err("PPGTT missing 64K scratch page\n");
1887 err
= i915_subtests(tests
, ppgtt
);
1890 i915_vm_put(&ppgtt
->vm
);
1893 drm_dev_put(&dev_priv
->drm
);
1897 int i915_gem_huge_page_live_selftests(struct drm_i915_private
*i915
)
1899 static const struct i915_subtest tests
[] = {
1900 SUBTEST(igt_shrink_thp
),
1901 SUBTEST(igt_ppgtt_pin_update
),
1902 SUBTEST(igt_tmpfs_fallback
),
1903 SUBTEST(igt_ppgtt_exhaust_huge
),
1904 SUBTEST(igt_ppgtt_smoke_huge
),
1905 SUBTEST(igt_ppgtt_sanity_check
),
1907 struct i915_gem_context
*ctx
;
1908 struct i915_address_space
*vm
;
1912 if (!HAS_PPGTT(i915
)) {
1913 pr_info("PPGTT not supported, skipping live-selftests\n");
1917 if (intel_gt_is_wedged(&i915
->gt
))
1920 file
= mock_file(i915
);
1922 return PTR_ERR(file
);
1924 ctx
= live_context(i915
, file
);
1930 mutex_lock(&ctx
->mutex
);
1931 vm
= i915_gem_context_vm(ctx
);
1933 WRITE_ONCE(vm
->scrub_64K
, true);
1934 mutex_unlock(&ctx
->mutex
);
1936 err
= i915_subtests(tests
, ctx
);