2 * Copyright © 2017 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 "../i915_selftest.h"
27 #include <linux/prime_numbers.h>
31 static const unsigned int page_sizes[] = {
32 I915_GTT_PAGE_SIZE_2M,
33 I915_GTT_PAGE_SIZE_64K,
34 I915_GTT_PAGE_SIZE_4K,
37 static unsigned int get_largest_page_size(struct drm_i915_private *i915,
42 for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
43 unsigned int page_size = page_sizes[i];
45 if (HAS_PAGE_SIZES(i915, page_size) && rem >= page_size)
52 static void huge_pages_free_pages(struct sg_table *st)
54 struct scatterlist *sg;
56 for (sg = st->sgl; sg; sg = __sg_next(sg)) {
58 __free_pages(sg_page(sg), get_order(sg->length));
65 static int get_huge_pages(struct drm_i915_gem_object *obj)
67 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
68 unsigned int page_mask = obj->mm.page_mask;
70 struct scatterlist *sg;
71 unsigned int sg_page_sizes;
74 st = kmalloc(sizeof(*st), GFP);
78 if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
89 * Our goal here is simple, we want to greedily fill the object from
90 * largest to smallest page-size, while ensuring that we use *every*
91 * page-size as per the given page-mask.
94 unsigned int bit = ilog2(page_mask);
95 unsigned int page_size = BIT(bit);
96 int order = get_order(page_size);
101 GEM_BUG_ON(order >= MAX_ORDER);
102 page = alloc_pages(GFP | __GFP_ZERO, order);
106 sg_set_page(sg, page, page_size, 0);
107 sg_page_sizes |= page_size;
117 } while ((rem - ((page_size-1) & page_mask)) >= page_size);
119 page_mask &= (page_size-1);
122 if (i915_gem_gtt_prepare_pages(obj, st))
125 obj->mm.madv = I915_MADV_DONTNEED;
127 GEM_BUG_ON(sg_page_sizes != obj->mm.page_mask);
128 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
133 sg_set_page(sg, NULL, 0, 0);
135 huge_pages_free_pages(st);
140 static void put_huge_pages(struct drm_i915_gem_object *obj,
141 struct sg_table *pages)
143 i915_gem_gtt_finish_pages(obj, pages);
144 huge_pages_free_pages(pages);
146 obj->mm.dirty = false;
147 obj->mm.madv = I915_MADV_WILLNEED;
150 static const struct drm_i915_gem_object_ops huge_page_ops = {
151 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
152 I915_GEM_OBJECT_IS_SHRINKABLE,
153 .get_pages = get_huge_pages,
154 .put_pages = put_huge_pages,
157 static struct drm_i915_gem_object *
158 huge_pages_object(struct drm_i915_private *i915,
160 unsigned int page_mask)
162 struct drm_i915_gem_object *obj;
165 GEM_BUG_ON(!IS_ALIGNED(size, BIT(__ffs(page_mask))));
167 if (size >> PAGE_SHIFT > INT_MAX)
168 return ERR_PTR(-E2BIG);
170 if (overflows_type(size, obj->base.size))
171 return ERR_PTR(-E2BIG);
173 obj = i915_gem_object_alloc(i915);
175 return ERR_PTR(-ENOMEM);
177 drm_gem_private_object_init(&i915->drm, &obj->base, size);
178 i915_gem_object_init(obj, &huge_page_ops);
180 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
181 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
182 obj->cache_level = I915_CACHE_NONE;
184 obj->mm.page_mask = page_mask;
189 static int fake_get_huge_pages(struct drm_i915_gem_object *obj)
191 struct drm_i915_private *i915 = to_i915(obj->base.dev);
192 const u64 max_len = rounddown_pow_of_two(UINT_MAX);
194 struct scatterlist *sg;
195 unsigned int sg_page_sizes;
198 st = kmalloc(sizeof(*st), GFP);
202 if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
207 /* Use optimal page sized chunks to fill in the sg table */
208 rem = obj->base.size;
213 unsigned int page_size = get_largest_page_size(i915, rem);
214 unsigned int len = min(page_size * div_u64(rem, page_size),
217 GEM_BUG_ON(!page_size);
221 sg_dma_len(sg) = len;
222 sg_dma_address(sg) = page_size;
224 sg_page_sizes |= len;
237 obj->mm.madv = I915_MADV_DONTNEED;
239 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
244 static int fake_get_huge_pages_single(struct drm_i915_gem_object *obj)
246 struct drm_i915_private *i915 = to_i915(obj->base.dev);
248 struct scatterlist *sg;
249 unsigned int page_size;
251 st = kmalloc(sizeof(*st), GFP);
255 if (sg_alloc_table(st, 1, GFP)) {
263 page_size = get_largest_page_size(i915, obj->base.size);
264 GEM_BUG_ON(!page_size);
267 sg->length = obj->base.size;
268 sg_dma_len(sg) = obj->base.size;
269 sg_dma_address(sg) = page_size;
271 obj->mm.madv = I915_MADV_DONTNEED;
273 __i915_gem_object_set_pages(obj, st, sg->length);
279 static void fake_free_huge_pages(struct drm_i915_gem_object *obj,
280 struct sg_table *pages)
282 sg_free_table(pages);
286 static void fake_put_huge_pages(struct drm_i915_gem_object *obj,
287 struct sg_table *pages)
289 fake_free_huge_pages(obj, pages);
290 obj->mm.dirty = false;
291 obj->mm.madv = I915_MADV_WILLNEED;
294 static const struct drm_i915_gem_object_ops fake_ops = {
295 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
296 .get_pages = fake_get_huge_pages,
297 .put_pages = fake_put_huge_pages,
300 static const struct drm_i915_gem_object_ops fake_ops_single = {
301 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
302 .get_pages = fake_get_huge_pages_single,
303 .put_pages = fake_put_huge_pages,
306 static struct drm_i915_gem_object *
307 fake_huge_pages_object(struct drm_i915_private *i915, u64 size, bool single)
309 struct drm_i915_gem_object *obj;
312 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
314 if (size >> PAGE_SHIFT > UINT_MAX)
315 return ERR_PTR(-E2BIG);
317 if (overflows_type(size, obj->base.size))
318 return ERR_PTR(-E2BIG);
320 obj = i915_gem_object_alloc(i915);
322 return ERR_PTR(-ENOMEM);
324 drm_gem_private_object_init(&i915->drm, &obj->base, size);
327 i915_gem_object_init(obj, &fake_ops_single);
329 i915_gem_object_init(obj, &fake_ops);
331 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
332 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
333 obj->cache_level = I915_CACHE_NONE;
338 static int igt_check_page_sizes(struct i915_vma *vma)
340 struct drm_i915_private *i915 = to_i915(vma->obj->base.dev);
341 unsigned int supported = INTEL_INFO(i915)->page_sizes;
342 struct drm_i915_gem_object *obj = vma->obj;
345 if (!HAS_PAGE_SIZES(i915, vma->page_sizes.sg)) {
346 pr_err("unsupported page_sizes.sg=%u, supported=%u\n",
347 vma->page_sizes.sg & ~supported, supported);
351 if (!HAS_PAGE_SIZES(i915, vma->page_sizes.gtt)) {
352 pr_err("unsupported page_sizes.gtt=%u, supported=%u\n",
353 vma->page_sizes.gtt & ~supported, supported);
357 if (vma->page_sizes.phys != obj->mm.page_sizes.phys) {
358 pr_err("vma->page_sizes.phys(%u) != obj->mm.page_sizes.phys(%u)\n",
359 vma->page_sizes.phys, obj->mm.page_sizes.phys);
363 if (vma->page_sizes.sg != obj->mm.page_sizes.sg) {
364 pr_err("vma->page_sizes.sg(%u) != obj->mm.page_sizes.sg(%u)\n",
365 vma->page_sizes.sg, obj->mm.page_sizes.sg);
369 if (obj->mm.page_sizes.gtt) {
370 pr_err("obj->page_sizes.gtt(%u) should never be set\n",
371 obj->mm.page_sizes.gtt);
378 static int igt_mock_exhaust_device_supported_pages(void *arg)
380 struct i915_hw_ppgtt *ppgtt = arg;
381 struct drm_i915_private *i915 = ppgtt->base.i915;
382 unsigned int saved_mask = INTEL_INFO(i915)->page_sizes;
383 struct drm_i915_gem_object *obj;
384 struct i915_vma *vma;
389 * Sanity check creating objects with every valid page support
390 * combination for our mock device.
393 for (i = 1; i < BIT(ARRAY_SIZE(page_sizes)); i++) {
394 unsigned int combination = 0;
396 for (j = 0; j < ARRAY_SIZE(page_sizes); j++) {
398 combination |= page_sizes[j];
401 mkwrite_device_info(i915)->page_sizes = combination;
403 for (single = 0; single <= 1; ++single) {
404 obj = fake_huge_pages_object(i915, combination, !!single);
410 if (obj->base.size != combination) {
411 pr_err("obj->base.size=%zu, expected=%u\n",
412 obj->base.size, combination);
417 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
423 err = i915_vma_pin(vma, 0, 0, PIN_USER);
427 err = igt_check_page_sizes(vma);
429 if (vma->page_sizes.sg != combination) {
430 pr_err("page_sizes.sg=%u, expected=%u\n",
431 vma->page_sizes.sg, combination);
438 i915_gem_object_put(obj);
450 i915_gem_object_put(obj);
452 mkwrite_device_info(i915)->page_sizes = saved_mask;
457 static int igt_mock_ppgtt_misaligned_dma(void *arg)
459 struct i915_hw_ppgtt *ppgtt = arg;
460 struct drm_i915_private *i915 = ppgtt->base.i915;
461 unsigned long supported = INTEL_INFO(i915)->page_sizes;
462 struct drm_i915_gem_object *obj;
467 * Sanity check dma misalignment for huge pages -- the dma addresses we
468 * insert into the paging structures need to always respect the page
472 bit = ilog2(I915_GTT_PAGE_SIZE_64K);
474 for_each_set_bit_from(bit, &supported,
475 ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
476 IGT_TIMEOUT(end_time);
477 unsigned int page_size = BIT(bit);
478 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
481 round_up(page_size, I915_GTT_PAGE_SIZE_2M) << 1;
482 struct i915_vma *vma;
484 obj = fake_huge_pages_object(i915, size, true);
488 if (obj->base.size != size) {
489 pr_err("obj->base.size=%zu, expected=%u\n",
490 obj->base.size, size);
495 err = i915_gem_object_pin_pages(obj);
499 /* Force the page size for this object */
500 obj->mm.page_sizes.sg = page_size;
502 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
508 err = i915_vma_pin(vma, 0, 0, flags);
515 err = igt_check_page_sizes(vma);
517 if (vma->page_sizes.gtt != page_size) {
518 pr_err("page_sizes.gtt=%u, expected %u\n",
519 vma->page_sizes.gtt, page_size);
531 * Try all the other valid offsets until the next
532 * boundary -- should always fall back to using 4K
535 for (offset = 4096; offset < page_size; offset += 4096) {
536 err = i915_vma_unbind(vma);
542 err = i915_vma_pin(vma, 0, 0, flags | offset);
548 err = igt_check_page_sizes(vma);
550 if (vma->page_sizes.gtt != I915_GTT_PAGE_SIZE_4K) {
551 pr_err("page_sizes.gtt=%u, expected %lu\n",
552 vma->page_sizes.gtt, I915_GTT_PAGE_SIZE_4K);
563 if (igt_timeout(end_time,
564 "%s timed out at offset %x with page-size %x\n",
565 __func__, offset, page_size))
571 i915_gem_object_unpin_pages(obj);
572 i915_gem_object_put(obj);
578 i915_gem_object_unpin_pages(obj);
580 i915_gem_object_put(obj);
585 static void close_object_list(struct list_head *objects,
586 struct i915_hw_ppgtt *ppgtt)
588 struct drm_i915_gem_object *obj, *on;
590 list_for_each_entry_safe(obj, on, objects, st_link) {
591 struct i915_vma *vma;
593 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
597 list_del(&obj->st_link);
598 i915_gem_object_unpin_pages(obj);
599 i915_gem_object_put(obj);
603 static int igt_mock_ppgtt_huge_fill(void *arg)
605 struct i915_hw_ppgtt *ppgtt = arg;
606 struct drm_i915_private *i915 = ppgtt->base.i915;
607 unsigned long max_pages = ppgtt->base.total >> PAGE_SHIFT;
608 unsigned long page_num;
611 IGT_TIMEOUT(end_time);
614 for_each_prime_number_from(page_num, 1, max_pages) {
615 struct drm_i915_gem_object *obj;
616 u64 size = page_num << PAGE_SHIFT;
617 struct i915_vma *vma;
618 unsigned int expected_gtt = 0;
621 obj = fake_huge_pages_object(i915, size, single);
627 if (obj->base.size != size) {
628 pr_err("obj->base.size=%zd, expected=%llu\n",
629 obj->base.size, size);
630 i915_gem_object_put(obj);
635 err = i915_gem_object_pin_pages(obj);
637 i915_gem_object_put(obj);
641 list_add(&obj->st_link, &objects);
643 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
649 err = i915_vma_pin(vma, 0, 0, PIN_USER);
653 err = igt_check_page_sizes(vma);
660 * Figure out the expected gtt page size knowing that we go from
661 * largest to smallest page size sg chunks, and that we align to
662 * the largest page size.
664 for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
665 unsigned int page_size = page_sizes[i];
667 if (HAS_PAGE_SIZES(i915, page_size) &&
669 expected_gtt |= page_size;
674 GEM_BUG_ON(!expected_gtt);
677 if (expected_gtt & I915_GTT_PAGE_SIZE_4K)
678 expected_gtt &= ~I915_GTT_PAGE_SIZE_64K;
682 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
683 if (!IS_ALIGNED(vma->node.start,
684 I915_GTT_PAGE_SIZE_2M)) {
685 pr_err("node.start(%llx) not aligned to 2M\n",
691 if (!IS_ALIGNED(vma->node.size,
692 I915_GTT_PAGE_SIZE_2M)) {
693 pr_err("node.size(%llx) not aligned to 2M\n",
700 if (vma->page_sizes.gtt != expected_gtt) {
701 pr_err("gtt=%u, expected=%u, size=%zd, single=%s\n",
702 vma->page_sizes.gtt, expected_gtt,
703 obj->base.size, yesno(!!single));
708 if (igt_timeout(end_time,
709 "%s timed out at size %zd\n",
710 __func__, obj->base.size))
716 close_object_list(&objects, ppgtt);
718 if (err == -ENOMEM || err == -ENOSPC)
724 static int igt_mock_ppgtt_64K(void *arg)
726 struct i915_hw_ppgtt *ppgtt = arg;
727 struct drm_i915_private *i915 = ppgtt->base.i915;
728 struct drm_i915_gem_object *obj;
729 const struct object_info {
734 /* Cases with forced padding/alignment */
737 .gtt = I915_GTT_PAGE_SIZE_64K,
741 .size = SZ_64K + SZ_4K,
742 .gtt = I915_GTT_PAGE_SIZE_4K,
746 .size = SZ_64K - SZ_4K,
747 .gtt = I915_GTT_PAGE_SIZE_4K,
752 .gtt = I915_GTT_PAGE_SIZE_64K,
756 .size = SZ_2M - SZ_4K,
757 .gtt = I915_GTT_PAGE_SIZE_4K,
761 .size = SZ_2M + SZ_4K,
762 .gtt = I915_GTT_PAGE_SIZE_64K | I915_GTT_PAGE_SIZE_4K,
766 .size = SZ_2M + SZ_64K,
767 .gtt = I915_GTT_PAGE_SIZE_64K,
771 .size = SZ_2M - SZ_64K,
772 .gtt = I915_GTT_PAGE_SIZE_64K,
775 /* Try without any forced padding/alignment */
779 .gtt = I915_GTT_PAGE_SIZE_4K,
783 .offset = SZ_2M - SZ_64K,
784 .gtt = I915_GTT_PAGE_SIZE_4K,
787 struct i915_vma *vma;
792 * Sanity check some of the trickiness with 64K pages -- either we can
793 * safely mark the whole page-table(2M block) as 64K, or we have to
794 * always fallback to 4K.
797 if (!HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K))
800 for (i = 0; i < ARRAY_SIZE(objects); ++i) {
801 unsigned int size = objects[i].size;
802 unsigned int expected_gtt = objects[i].gtt;
803 unsigned int offset = objects[i].offset;
804 unsigned int flags = PIN_USER;
806 for (single = 0; single <= 1; single++) {
807 obj = fake_huge_pages_object(i915, size, !!single);
811 err = i915_gem_object_pin_pages(obj);
816 * Disable 2M pages -- We only want to use 64K/4K pages
819 obj->mm.page_sizes.sg &= ~I915_GTT_PAGE_SIZE_2M;
821 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
824 goto out_object_unpin;
828 flags |= PIN_OFFSET_FIXED | offset;
830 err = i915_vma_pin(vma, 0, 0, flags);
834 err = igt_check_page_sizes(vma);
838 if (!offset && vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
839 if (!IS_ALIGNED(vma->node.start,
840 I915_GTT_PAGE_SIZE_2M)) {
841 pr_err("node.start(%llx) not aligned to 2M\n",
847 if (!IS_ALIGNED(vma->node.size,
848 I915_GTT_PAGE_SIZE_2M)) {
849 pr_err("node.size(%llx) not aligned to 2M\n",
856 if (vma->page_sizes.gtt != expected_gtt) {
857 pr_err("gtt=%u, expected=%u, i=%d, single=%s\n",
858 vma->page_sizes.gtt, expected_gtt, i,
867 i915_gem_object_unpin_pages(obj);
868 i915_gem_object_put(obj);
879 i915_gem_object_unpin_pages(obj);
881 i915_gem_object_put(obj);
886 static struct i915_vma *
887 gpu_write_dw(struct i915_vma *vma, u64 offset, u32 val)
889 struct drm_i915_private *i915 = to_i915(vma->obj->base.dev);
890 const int gen = INTEL_GEN(vma->vm->i915);
891 unsigned int count = vma->size >> PAGE_SHIFT;
892 struct drm_i915_gem_object *obj;
893 struct i915_vma *batch;
899 size = (1 + 4 * count) * sizeof(u32);
900 size = round_up(size, PAGE_SIZE);
901 obj = i915_gem_object_create_internal(i915, size);
903 return ERR_CAST(obj);
905 cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
911 offset += vma->node.start;
913 for (n = 0; n < count; n++) {
915 *cmd++ = MI_STORE_DWORD_IMM_GEN4;
916 *cmd++ = lower_32_bits(offset);
917 *cmd++ = upper_32_bits(offset);
919 } else if (gen >= 4) {
920 *cmd++ = MI_STORE_DWORD_IMM_GEN4 |
921 (gen < 6 ? 1 << 22 : 0);
926 *cmd++ = MI_STORE_DWORD_IMM | 1 << 22;
934 *cmd = MI_BATCH_BUFFER_END;
936 i915_gem_object_unpin_map(obj);
938 err = i915_gem_object_set_to_gtt_domain(obj, false);
942 batch = i915_vma_instance(obj, vma->vm, NULL);
944 err = PTR_ERR(batch);
948 err = i915_vma_pin(batch, 0, 0, PIN_USER);
955 i915_gem_object_put(obj);
960 static int gpu_write(struct i915_vma *vma,
961 struct i915_gem_context *ctx,
962 struct intel_engine_cs *engine,
966 struct drm_i915_gem_request *rq;
967 struct i915_vma *batch;
971 GEM_BUG_ON(!intel_engine_can_store_dword(engine));
973 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
977 rq = i915_gem_request_alloc(engine, ctx);
981 batch = gpu_write_dw(vma, dword * sizeof(u32), value);
983 err = PTR_ERR(batch);
987 i915_vma_move_to_active(batch, rq, 0);
988 i915_gem_object_set_active_reference(batch->obj);
989 i915_vma_unpin(batch);
990 i915_vma_close(batch);
992 err = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
996 err = i915_switch_context(rq);
1000 err = rq->engine->emit_bb_start(rq,
1001 batch->node.start, batch->node.size,
1006 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
1008 reservation_object_lock(vma->resv, NULL);
1009 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1010 reservation_object_unlock(vma->resv);
1013 __i915_add_request(rq, err == 0);
1018 static int cpu_check(struct drm_i915_gem_object *obj, u32 dword, u32 val)
1020 unsigned int needs_flush;
1024 err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
1028 for (n = 0; n < obj->base.size >> PAGE_SHIFT; ++n) {
1029 u32 *ptr = kmap_atomic(i915_gem_object_get_page(obj, n));
1031 if (needs_flush & CLFLUSH_BEFORE)
1032 drm_clflush_virt_range(ptr, PAGE_SIZE);
1034 if (ptr[dword] != val) {
1035 pr_err("n=%lu ptr[%u]=%u, val=%u\n",
1036 n, dword, ptr[dword], val);
1045 i915_gem_obj_finish_shmem_access(obj);
1050 static int igt_write_huge(struct i915_gem_context *ctx,
1051 struct drm_i915_gem_object *obj)
1053 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1054 struct i915_address_space *vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
1055 struct intel_engine_cs *engine;
1056 struct i915_vma *vma;
1057 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1058 unsigned int max_page_size;
1065 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1067 size = obj->base.size;
1068 if (obj->mm.page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
1069 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
1071 max_page_size = rounddown_pow_of_two(obj->mm.page_sizes.sg);
1072 max = div_u64((vm->total - size), max_page_size);
1074 vma = i915_vma_instance(obj, vm, NULL);
1076 return PTR_ERR(vma);
1078 for_each_engine(engine, i915, id) {
1079 IGT_TIMEOUT(end_time);
1081 if (!intel_engine_can_store_dword(engine)) {
1082 pr_info("store-dword-imm not supported on engine=%u\n",
1088 * Try various offsets until we timeout -- we want to avoid
1089 * issues hidden by effectively always using offset = 0.
1091 for_each_prime_number_from(num, 0, max) {
1092 u64 offset = num * max_page_size;
1095 err = i915_vma_unbind(vma);
1099 err = i915_vma_pin(vma, size, max_page_size, flags | offset);
1102 * The ggtt may have some pages reserved so
1103 * refrain from erroring out.
1105 if (err == -ENOSPC && i915_is_ggtt(vm)) {
1113 err = igt_check_page_sizes(vma);
1117 dword = offset_in_page(num) / 4;
1119 err = gpu_write(vma, ctx, engine, dword, num + 1);
1121 pr_err("gpu-write failed at offset=%llx", offset);
1125 err = cpu_check(obj, dword, num + 1);
1127 pr_err("cpu-check failed at offset=%llx", offset);
1131 i915_vma_unpin(vma);
1134 igt_timeout(end_time,
1135 "%s timed out on engine=%u at offset=%llx, max_page_size=%x\n",
1136 __func__, id, offset, max_page_size))
1142 if (i915_vma_is_pinned(vma))
1143 i915_vma_unpin(vma);
1145 i915_vma_close(vma);
1150 static int igt_ppgtt_exhaust_huge(void *arg)
1152 struct i915_gem_context *ctx = arg;
1153 struct drm_i915_private *i915 = ctx->i915;
1154 unsigned long supported = INTEL_INFO(i915)->page_sizes;
1155 static unsigned int pages[ARRAY_SIZE(page_sizes)];
1156 struct drm_i915_gem_object *obj;
1157 unsigned int size_mask;
1158 unsigned int page_mask;
1163 * Sanity check creating objects with a varying mix of page sizes --
1164 * ensuring that our writes lands in the right place.
1168 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1)
1169 pages[n++] = BIT(i);
1171 for (size_mask = 2; size_mask < BIT(n); size_mask++) {
1172 unsigned int size = 0;
1174 for (i = 0; i < n; i++) {
1175 if (size_mask & BIT(i))
1180 * For our page mask we want to enumerate all the page-size
1181 * combinations which will fit into our chosen object size.
1183 for (page_mask = 2; page_mask <= size_mask; page_mask++) {
1184 unsigned int page_sizes = 0;
1186 for (i = 0; i < n; i++) {
1187 if (page_mask & BIT(i))
1188 page_sizes |= pages[i];
1192 * Ensure that we can actually fill the given object
1193 * with our chosen page mask.
1195 if (!IS_ALIGNED(size, BIT(__ffs(page_sizes))))
1198 obj = huge_pages_object(i915, size, page_sizes);
1204 err = i915_gem_object_pin_pages(obj);
1206 i915_gem_object_put(obj);
1208 if (err == -ENOMEM) {
1209 pr_info("unable to get pages, size=%u, pages=%u\n",
1215 pr_err("pin_pages failed, size=%u, pages=%u\n",
1216 size_mask, page_mask);
1221 /* Force the page-size for the gtt insertion */
1222 obj->mm.page_sizes.sg = page_sizes;
1224 err = igt_write_huge(ctx, obj);
1226 pr_err("exhaust write-huge failed with size=%u\n",
1231 i915_gem_object_unpin_pages(obj);
1232 i915_gem_object_put(obj);
1239 i915_gem_object_unpin_pages(obj);
1240 i915_gem_object_put(obj);
1242 mkwrite_device_info(i915)->page_sizes = supported;
1247 static int igt_ppgtt_internal_huge(void *arg)
1249 struct i915_gem_context *ctx = arg;
1250 struct drm_i915_private *i915 = ctx->i915;
1251 struct drm_i915_gem_object *obj;
1252 static const unsigned int sizes[] = {
1264 * Sanity check that the HW uses huge pages correctly through internal
1265 * -- ensure that our writes land in the right place.
1268 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
1269 unsigned int size = sizes[i];
1271 obj = i915_gem_object_create_internal(i915, size);
1273 return PTR_ERR(obj);
1275 err = i915_gem_object_pin_pages(obj);
1279 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_64K) {
1280 pr_info("internal unable to allocate huge-page(s) with size=%u\n",
1285 err = igt_write_huge(ctx, obj);
1287 pr_err("internal write-huge failed with size=%u\n",
1292 i915_gem_object_unpin_pages(obj);
1293 i915_gem_object_put(obj);
1299 i915_gem_object_unpin_pages(obj);
1301 i915_gem_object_put(obj);
1306 static inline bool igt_can_allocate_thp(struct drm_i915_private *i915)
1308 return i915->mm.gemfs && has_transparent_hugepage();
1311 static int igt_ppgtt_gemfs_huge(void *arg)
1313 struct i915_gem_context *ctx = arg;
1314 struct drm_i915_private *i915 = ctx->i915;
1315 struct drm_i915_gem_object *obj;
1316 static const unsigned int sizes[] = {
1327 * Sanity check that the HW uses huge pages correctly through gemfs --
1328 * ensure that our writes land in the right place.
1331 if (!igt_can_allocate_thp(i915)) {
1332 pr_info("missing THP support, skipping\n");
1336 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
1337 unsigned int size = sizes[i];
1339 obj = i915_gem_object_create(i915, size);
1341 return PTR_ERR(obj);
1343 err = i915_gem_object_pin_pages(obj);
1347 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
1348 pr_info("finishing test early, gemfs unable to allocate huge-page(s) with size=%u\n",
1353 err = igt_write_huge(ctx, obj);
1355 pr_err("gemfs write-huge failed with size=%u\n",
1360 i915_gem_object_unpin_pages(obj);
1361 i915_gem_object_put(obj);
1367 i915_gem_object_unpin_pages(obj);
1369 i915_gem_object_put(obj);
1374 static int igt_ppgtt_pin_update(void *arg)
1376 struct i915_gem_context *ctx = arg;
1377 struct drm_i915_private *dev_priv = ctx->i915;
1378 unsigned long supported = INTEL_INFO(dev_priv)->page_sizes;
1379 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
1380 struct drm_i915_gem_object *obj;
1381 struct i915_vma *vma;
1382 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1387 * Make sure there's no funny business when doing a PIN_UPDATE -- in the
1388 * past we had a subtle issue with being able to incorrectly do multiple
1389 * alloc va ranges on the same object when doing a PIN_UPDATE, which
1390 * resulted in some pretty nasty bugs, though only when using
1394 if (!USES_FULL_48BIT_PPGTT(dev_priv)) {
1395 pr_info("48b PPGTT not supported, skipping\n");
1399 first = ilog2(I915_GTT_PAGE_SIZE_64K);
1400 last = ilog2(I915_GTT_PAGE_SIZE_2M);
1402 for_each_set_bit_from(first, &supported, last + 1) {
1403 unsigned int page_size = BIT(first);
1405 obj = i915_gem_object_create_internal(dev_priv, page_size);
1407 return PTR_ERR(obj);
1409 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
1415 err = i915_vma_pin(vma, SZ_2M, 0, flags);
1419 if (vma->page_sizes.sg < page_size) {
1420 pr_info("Unable to allocate page-size %x, finishing test early\n",
1425 err = igt_check_page_sizes(vma);
1429 if (vma->page_sizes.gtt != page_size) {
1430 dma_addr_t addr = i915_gem_object_get_dma_address(obj, 0);
1433 * The only valid reason for this to ever fail would be
1434 * if the dma-mapper screwed us over when we did the
1435 * dma_map_sg(), since it has the final say over the dma
1438 if (IS_ALIGNED(addr, page_size)) {
1439 pr_err("page_sizes.gtt=%u, expected=%u\n",
1440 vma->page_sizes.gtt, page_size);
1443 pr_info("dma address misaligned, finishing test early\n");
1449 err = i915_vma_bind(vma, I915_CACHE_NONE, PIN_UPDATE);
1453 i915_vma_unpin(vma);
1454 i915_vma_close(vma);
1456 i915_gem_object_put(obj);
1459 obj = i915_gem_object_create_internal(dev_priv, PAGE_SIZE);
1461 return PTR_ERR(obj);
1463 vma = i915_vma_instance(obj, &ppgtt->base, NULL);
1469 err = i915_vma_pin(vma, 0, 0, flags);
1474 * Make sure we don't end up with something like where the pde is still
1475 * pointing to the 2M page, and the pt we just filled-in is dangling --
1476 * we can check this by writing to the first page where it would then
1477 * land in the now stale 2M page.
1480 err = gpu_write(vma, ctx, dev_priv->engine[RCS], 0, 0xdeadbeaf);
1484 err = cpu_check(obj, 0, 0xdeadbeaf);
1487 i915_vma_unpin(vma);
1489 i915_vma_close(vma);
1491 i915_gem_object_put(obj);
1496 static int igt_tmpfs_fallback(void *arg)
1498 struct i915_gem_context *ctx = arg;
1499 struct drm_i915_private *i915 = ctx->i915;
1500 struct vfsmount *gemfs = i915->mm.gemfs;
1501 struct i915_address_space *vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
1502 struct drm_i915_gem_object *obj;
1503 struct i915_vma *vma;
1508 * Make sure that we don't burst into a ball of flames upon falling back
1509 * to tmpfs, which we rely on if on the off-chance we encouter a failure
1510 * when setting up gemfs.
1513 i915->mm.gemfs = NULL;
1515 obj = i915_gem_object_create(i915, PAGE_SIZE);
1521 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1522 if (IS_ERR(vaddr)) {
1523 err = PTR_ERR(vaddr);
1526 *vaddr = 0xdeadbeaf;
1528 i915_gem_object_unpin_map(obj);
1530 vma = i915_vma_instance(obj, vm, NULL);
1536 err = i915_vma_pin(vma, 0, 0, PIN_USER);
1540 err = igt_check_page_sizes(vma);
1542 i915_vma_unpin(vma);
1544 i915_vma_close(vma);
1546 i915_gem_object_put(obj);
1548 i915->mm.gemfs = gemfs;
1553 static int igt_shrink_thp(void *arg)
1555 struct i915_gem_context *ctx = arg;
1556 struct drm_i915_private *i915 = ctx->i915;
1557 struct i915_address_space *vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
1558 struct drm_i915_gem_object *obj;
1559 struct i915_vma *vma;
1560 unsigned int flags = PIN_USER;
1564 * Sanity check shrinking huge-paged object -- make sure nothing blows
1568 if (!igt_can_allocate_thp(i915)) {
1569 pr_info("missing THP support, skipping\n");
1573 obj = i915_gem_object_create(i915, SZ_2M);
1575 return PTR_ERR(obj);
1577 vma = i915_vma_instance(obj, vm, NULL);
1583 err = i915_vma_pin(vma, 0, 0, flags);
1587 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
1588 pr_info("failed to allocate THP, finishing test early\n");
1592 err = igt_check_page_sizes(vma);
1596 err = gpu_write(vma, ctx, i915->engine[RCS], 0, 0xdeadbeaf);
1600 i915_vma_unpin(vma);
1603 * Now that the pages are *unpinned* shrink-all should invoke
1604 * shmem to truncate our pages.
1606 i915_gem_shrink_all(i915);
1607 if (!IS_ERR_OR_NULL(obj->mm.pages)) {
1608 pr_err("shrink-all didn't truncate the pages\n");
1613 if (obj->mm.page_sizes.sg || obj->mm.page_sizes.phys) {
1614 pr_err("residual page-size bits left\n");
1619 err = i915_vma_pin(vma, 0, 0, flags);
1623 err = cpu_check(obj, 0, 0xdeadbeaf);
1626 i915_vma_unpin(vma);
1628 i915_vma_close(vma);
1630 i915_gem_object_put(obj);
1635 int i915_gem_huge_page_mock_selftests(void)
1637 static const struct i915_subtest tests[] = {
1638 SUBTEST(igt_mock_exhaust_device_supported_pages),
1639 SUBTEST(igt_mock_ppgtt_misaligned_dma),
1640 SUBTEST(igt_mock_ppgtt_huge_fill),
1641 SUBTEST(igt_mock_ppgtt_64K),
1643 int saved_ppgtt = i915_modparams.enable_ppgtt;
1644 struct drm_i915_private *dev_priv;
1645 struct pci_dev *pdev;
1646 struct i915_hw_ppgtt *ppgtt;
1649 dev_priv = mock_gem_device();
1653 /* Pretend to be a device which supports the 48b PPGTT */
1654 i915_modparams.enable_ppgtt = 3;
1656 pdev = dev_priv->drm.pdev;
1657 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(39));
1659 mutex_lock(&dev_priv->drm.struct_mutex);
1660 ppgtt = i915_ppgtt_create(dev_priv, ERR_PTR(-ENODEV), "mock");
1661 if (IS_ERR(ppgtt)) {
1662 err = PTR_ERR(ppgtt);
1666 if (!i915_vm_is_48bit(&ppgtt->base)) {
1667 pr_err("failed to create 48b PPGTT\n");
1672 /* If we were ever hit this then it's time to mock the 64K scratch */
1673 if (!i915_vm_has_scratch_64K(&ppgtt->base)) {
1674 pr_err("PPGTT missing 64K scratch page\n");
1679 err = i915_subtests(tests, ppgtt);
1682 i915_ppgtt_close(&ppgtt->base);
1683 i915_ppgtt_put(ppgtt);
1686 mutex_unlock(&dev_priv->drm.struct_mutex);
1688 i915_modparams.enable_ppgtt = saved_ppgtt;
1690 drm_dev_unref(&dev_priv->drm);
1695 int i915_gem_huge_page_live_selftests(struct drm_i915_private *dev_priv)
1697 static const struct i915_subtest tests[] = {
1698 SUBTEST(igt_shrink_thp),
1699 SUBTEST(igt_ppgtt_pin_update),
1700 SUBTEST(igt_tmpfs_fallback),
1701 SUBTEST(igt_ppgtt_exhaust_huge),
1702 SUBTEST(igt_ppgtt_gemfs_huge),
1703 SUBTEST(igt_ppgtt_internal_huge),
1705 struct drm_file *file;
1706 struct i915_gem_context *ctx;
1709 if (!USES_PPGTT(dev_priv)) {
1710 pr_info("PPGTT not supported, skipping live-selftests\n");
1714 file = mock_file(dev_priv);
1716 return PTR_ERR(file);
1718 mutex_lock(&dev_priv->drm.struct_mutex);
1720 ctx = live_context(dev_priv, file);
1726 err = i915_subtests(tests, ctx);
1729 mutex_unlock(&dev_priv->drm.struct_mutex);
1731 mock_file_free(dev_priv, file);