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>
30 #include "i915_random.h"
32 static const unsigned int page_sizes[] = {
33 I915_GTT_PAGE_SIZE_2M,
34 I915_GTT_PAGE_SIZE_64K,
35 I915_GTT_PAGE_SIZE_4K,
38 static unsigned int get_largest_page_size(struct drm_i915_private *i915,
43 for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
44 unsigned int page_size = page_sizes[i];
46 if (HAS_PAGE_SIZES(i915, page_size) && rem >= page_size)
53 static void huge_pages_free_pages(struct sg_table *st)
55 struct scatterlist *sg;
57 for (sg = st->sgl; sg; sg = __sg_next(sg)) {
59 __free_pages(sg_page(sg), get_order(sg->length));
66 static int get_huge_pages(struct drm_i915_gem_object *obj)
68 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
69 unsigned int page_mask = obj->mm.page_mask;
71 struct scatterlist *sg;
72 unsigned int sg_page_sizes;
75 st = kmalloc(sizeof(*st), GFP);
79 if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
90 * Our goal here is simple, we want to greedily fill the object from
91 * largest to smallest page-size, while ensuring that we use *every*
92 * page-size as per the given page-mask.
95 unsigned int bit = ilog2(page_mask);
96 unsigned int page_size = BIT(bit);
97 int order = get_order(page_size);
102 GEM_BUG_ON(order >= MAX_ORDER);
103 page = alloc_pages(GFP | __GFP_ZERO, order);
107 sg_set_page(sg, page, page_size, 0);
108 sg_page_sizes |= page_size;
118 } while ((rem - ((page_size-1) & page_mask)) >= page_size);
120 page_mask &= (page_size-1);
123 if (i915_gem_gtt_prepare_pages(obj, st))
126 obj->mm.madv = I915_MADV_DONTNEED;
128 GEM_BUG_ON(sg_page_sizes != obj->mm.page_mask);
129 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
134 sg_set_page(sg, NULL, 0, 0);
136 huge_pages_free_pages(st);
141 static void put_huge_pages(struct drm_i915_gem_object *obj,
142 struct sg_table *pages)
144 i915_gem_gtt_finish_pages(obj, pages);
145 huge_pages_free_pages(pages);
147 obj->mm.dirty = false;
148 obj->mm.madv = I915_MADV_WILLNEED;
151 static const struct drm_i915_gem_object_ops huge_page_ops = {
152 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
153 I915_GEM_OBJECT_IS_SHRINKABLE,
154 .get_pages = get_huge_pages,
155 .put_pages = put_huge_pages,
158 static struct drm_i915_gem_object *
159 huge_pages_object(struct drm_i915_private *i915,
161 unsigned int page_mask)
163 struct drm_i915_gem_object *obj;
166 GEM_BUG_ON(!IS_ALIGNED(size, BIT(__ffs(page_mask))));
168 if (size >> PAGE_SHIFT > INT_MAX)
169 return ERR_PTR(-E2BIG);
171 if (overflows_type(size, obj->base.size))
172 return ERR_PTR(-E2BIG);
174 obj = i915_gem_object_alloc(i915);
176 return ERR_PTR(-ENOMEM);
178 drm_gem_private_object_init(&i915->drm, &obj->base, size);
179 i915_gem_object_init(obj, &huge_page_ops);
181 obj->write_domain = I915_GEM_DOMAIN_CPU;
182 obj->read_domains = I915_GEM_DOMAIN_CPU;
183 obj->cache_level = I915_CACHE_NONE;
185 obj->mm.page_mask = page_mask;
190 static int fake_get_huge_pages(struct drm_i915_gem_object *obj)
192 struct drm_i915_private *i915 = to_i915(obj->base.dev);
193 const u64 max_len = rounddown_pow_of_two(UINT_MAX);
195 struct scatterlist *sg;
196 unsigned int sg_page_sizes;
199 st = kmalloc(sizeof(*st), GFP);
203 if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
208 /* Use optimal page sized chunks to fill in the sg table */
209 rem = obj->base.size;
214 unsigned int page_size = get_largest_page_size(i915, rem);
215 unsigned int len = min(page_size * div_u64(rem, page_size),
218 GEM_BUG_ON(!page_size);
222 sg_dma_len(sg) = len;
223 sg_dma_address(sg) = page_size;
225 sg_page_sizes |= len;
238 obj->mm.madv = I915_MADV_DONTNEED;
240 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
245 static int fake_get_huge_pages_single(struct drm_i915_gem_object *obj)
247 struct drm_i915_private *i915 = to_i915(obj->base.dev);
249 struct scatterlist *sg;
250 unsigned int page_size;
252 st = kmalloc(sizeof(*st), GFP);
256 if (sg_alloc_table(st, 1, GFP)) {
264 page_size = get_largest_page_size(i915, obj->base.size);
265 GEM_BUG_ON(!page_size);
268 sg->length = obj->base.size;
269 sg_dma_len(sg) = obj->base.size;
270 sg_dma_address(sg) = page_size;
272 obj->mm.madv = I915_MADV_DONTNEED;
274 __i915_gem_object_set_pages(obj, st, sg->length);
280 static void fake_free_huge_pages(struct drm_i915_gem_object *obj,
281 struct sg_table *pages)
283 sg_free_table(pages);
287 static void fake_put_huge_pages(struct drm_i915_gem_object *obj,
288 struct sg_table *pages)
290 fake_free_huge_pages(obj, pages);
291 obj->mm.dirty = false;
292 obj->mm.madv = I915_MADV_WILLNEED;
295 static const struct drm_i915_gem_object_ops fake_ops = {
296 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
297 .get_pages = fake_get_huge_pages,
298 .put_pages = fake_put_huge_pages,
301 static const struct drm_i915_gem_object_ops fake_ops_single = {
302 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
303 .get_pages = fake_get_huge_pages_single,
304 .put_pages = fake_put_huge_pages,
307 static struct drm_i915_gem_object *
308 fake_huge_pages_object(struct drm_i915_private *i915, u64 size, bool single)
310 struct drm_i915_gem_object *obj;
313 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
315 if (size >> PAGE_SHIFT > UINT_MAX)
316 return ERR_PTR(-E2BIG);
318 if (overflows_type(size, obj->base.size))
319 return ERR_PTR(-E2BIG);
321 obj = i915_gem_object_alloc(i915);
323 return ERR_PTR(-ENOMEM);
325 drm_gem_private_object_init(&i915->drm, &obj->base, size);
328 i915_gem_object_init(obj, &fake_ops_single);
330 i915_gem_object_init(obj, &fake_ops);
332 obj->write_domain = I915_GEM_DOMAIN_CPU;
333 obj->read_domains = I915_GEM_DOMAIN_CPU;
334 obj->cache_level = I915_CACHE_NONE;
339 static int igt_check_page_sizes(struct i915_vma *vma)
341 struct drm_i915_private *i915 = vma->vm->i915;
342 unsigned int supported = INTEL_INFO(i915)->page_sizes;
343 struct drm_i915_gem_object *obj = vma->obj;
346 if (!HAS_PAGE_SIZES(i915, vma->page_sizes.sg)) {
347 pr_err("unsupported page_sizes.sg=%u, supported=%u\n",
348 vma->page_sizes.sg & ~supported, supported);
352 if (!HAS_PAGE_SIZES(i915, vma->page_sizes.gtt)) {
353 pr_err("unsupported page_sizes.gtt=%u, supported=%u\n",
354 vma->page_sizes.gtt & ~supported, supported);
358 if (vma->page_sizes.phys != obj->mm.page_sizes.phys) {
359 pr_err("vma->page_sizes.phys(%u) != obj->mm.page_sizes.phys(%u)\n",
360 vma->page_sizes.phys, obj->mm.page_sizes.phys);
364 if (vma->page_sizes.sg != obj->mm.page_sizes.sg) {
365 pr_err("vma->page_sizes.sg(%u) != obj->mm.page_sizes.sg(%u)\n",
366 vma->page_sizes.sg, obj->mm.page_sizes.sg);
370 if (obj->mm.page_sizes.gtt) {
371 pr_err("obj->page_sizes.gtt(%u) should never be set\n",
372 obj->mm.page_sizes.gtt);
379 static int igt_mock_exhaust_device_supported_pages(void *arg)
381 struct i915_hw_ppgtt *ppgtt = arg;
382 struct drm_i915_private *i915 = ppgtt->vm.i915;
383 unsigned int saved_mask = INTEL_INFO(i915)->page_sizes;
384 struct drm_i915_gem_object *obj;
385 struct i915_vma *vma;
390 * Sanity check creating objects with every valid page support
391 * combination for our mock device.
394 for (i = 1; i < BIT(ARRAY_SIZE(page_sizes)); i++) {
395 unsigned int combination = 0;
397 for (j = 0; j < ARRAY_SIZE(page_sizes); j++) {
399 combination |= page_sizes[j];
402 mkwrite_device_info(i915)->page_sizes = combination;
404 for (single = 0; single <= 1; ++single) {
405 obj = fake_huge_pages_object(i915, combination, !!single);
411 if (obj->base.size != combination) {
412 pr_err("obj->base.size=%zu, expected=%u\n",
413 obj->base.size, combination);
418 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
424 err = i915_vma_pin(vma, 0, 0, PIN_USER);
428 err = igt_check_page_sizes(vma);
430 if (vma->page_sizes.sg != combination) {
431 pr_err("page_sizes.sg=%u, expected=%u\n",
432 vma->page_sizes.sg, combination);
439 i915_gem_object_put(obj);
451 i915_gem_object_put(obj);
453 mkwrite_device_info(i915)->page_sizes = saved_mask;
458 static int igt_mock_ppgtt_misaligned_dma(void *arg)
460 struct i915_hw_ppgtt *ppgtt = arg;
461 struct drm_i915_private *i915 = ppgtt->vm.i915;
462 unsigned long supported = INTEL_INFO(i915)->page_sizes;
463 struct drm_i915_gem_object *obj;
468 * Sanity check dma misalignment for huge pages -- the dma addresses we
469 * insert into the paging structures need to always respect the page
473 bit = ilog2(I915_GTT_PAGE_SIZE_64K);
475 for_each_set_bit_from(bit, &supported,
476 ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
477 IGT_TIMEOUT(end_time);
478 unsigned int page_size = BIT(bit);
479 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
482 round_up(page_size, I915_GTT_PAGE_SIZE_2M) << 1;
483 struct i915_vma *vma;
485 obj = fake_huge_pages_object(i915, size, true);
489 if (obj->base.size != size) {
490 pr_err("obj->base.size=%zu, expected=%u\n",
491 obj->base.size, size);
496 err = i915_gem_object_pin_pages(obj);
500 /* Force the page size for this object */
501 obj->mm.page_sizes.sg = page_size;
503 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
509 err = i915_vma_pin(vma, 0, 0, flags);
516 err = igt_check_page_sizes(vma);
518 if (vma->page_sizes.gtt != page_size) {
519 pr_err("page_sizes.gtt=%u, expected %u\n",
520 vma->page_sizes.gtt, page_size);
532 * Try all the other valid offsets until the next
533 * boundary -- should always fall back to using 4K
536 for (offset = 4096; offset < page_size; offset += 4096) {
537 err = i915_vma_unbind(vma);
543 err = i915_vma_pin(vma, 0, 0, flags | offset);
549 err = igt_check_page_sizes(vma);
551 if (vma->page_sizes.gtt != I915_GTT_PAGE_SIZE_4K) {
552 pr_err("page_sizes.gtt=%u, expected %llu\n",
553 vma->page_sizes.gtt, I915_GTT_PAGE_SIZE_4K);
564 if (igt_timeout(end_time,
565 "%s timed out at offset %x with page-size %x\n",
566 __func__, offset, page_size))
572 i915_gem_object_unpin_pages(obj);
573 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
574 i915_gem_object_put(obj);
580 i915_gem_object_unpin_pages(obj);
582 i915_gem_object_put(obj);
587 static void close_object_list(struct list_head *objects,
588 struct i915_hw_ppgtt *ppgtt)
590 struct drm_i915_gem_object *obj, *on;
592 list_for_each_entry_safe(obj, on, objects, st_link) {
593 struct i915_vma *vma;
595 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
599 list_del(&obj->st_link);
600 i915_gem_object_unpin_pages(obj);
601 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
602 i915_gem_object_put(obj);
606 static int igt_mock_ppgtt_huge_fill(void *arg)
608 struct i915_hw_ppgtt *ppgtt = arg;
609 struct drm_i915_private *i915 = ppgtt->vm.i915;
610 unsigned long max_pages = ppgtt->vm.total >> PAGE_SHIFT;
611 unsigned long page_num;
614 IGT_TIMEOUT(end_time);
617 for_each_prime_number_from(page_num, 1, max_pages) {
618 struct drm_i915_gem_object *obj;
619 u64 size = page_num << PAGE_SHIFT;
620 struct i915_vma *vma;
621 unsigned int expected_gtt = 0;
624 obj = fake_huge_pages_object(i915, size, single);
630 if (obj->base.size != size) {
631 pr_err("obj->base.size=%zd, expected=%llu\n",
632 obj->base.size, size);
633 i915_gem_object_put(obj);
638 err = i915_gem_object_pin_pages(obj);
640 i915_gem_object_put(obj);
644 list_add(&obj->st_link, &objects);
646 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
652 err = i915_vma_pin(vma, 0, 0, PIN_USER);
656 err = igt_check_page_sizes(vma);
663 * Figure out the expected gtt page size knowing that we go from
664 * largest to smallest page size sg chunks, and that we align to
665 * the largest page size.
667 for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
668 unsigned int page_size = page_sizes[i];
670 if (HAS_PAGE_SIZES(i915, page_size) &&
672 expected_gtt |= page_size;
677 GEM_BUG_ON(!expected_gtt);
680 if (expected_gtt & I915_GTT_PAGE_SIZE_4K)
681 expected_gtt &= ~I915_GTT_PAGE_SIZE_64K;
685 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
686 if (!IS_ALIGNED(vma->node.start,
687 I915_GTT_PAGE_SIZE_2M)) {
688 pr_err("node.start(%llx) not aligned to 2M\n",
694 if (!IS_ALIGNED(vma->node.size,
695 I915_GTT_PAGE_SIZE_2M)) {
696 pr_err("node.size(%llx) not aligned to 2M\n",
703 if (vma->page_sizes.gtt != expected_gtt) {
704 pr_err("gtt=%u, expected=%u, size=%zd, single=%s\n",
705 vma->page_sizes.gtt, expected_gtt,
706 obj->base.size, yesno(!!single));
711 if (igt_timeout(end_time,
712 "%s timed out at size %zd\n",
713 __func__, obj->base.size))
719 close_object_list(&objects, ppgtt);
721 if (err == -ENOMEM || err == -ENOSPC)
727 static int igt_mock_ppgtt_64K(void *arg)
729 struct i915_hw_ppgtt *ppgtt = arg;
730 struct drm_i915_private *i915 = ppgtt->vm.i915;
731 struct drm_i915_gem_object *obj;
732 const struct object_info {
737 /* Cases with forced padding/alignment */
740 .gtt = I915_GTT_PAGE_SIZE_64K,
744 .size = SZ_64K + SZ_4K,
745 .gtt = I915_GTT_PAGE_SIZE_4K,
749 .size = SZ_64K - SZ_4K,
750 .gtt = I915_GTT_PAGE_SIZE_4K,
755 .gtt = I915_GTT_PAGE_SIZE_64K,
759 .size = SZ_2M - SZ_4K,
760 .gtt = I915_GTT_PAGE_SIZE_4K,
764 .size = SZ_2M + SZ_4K,
765 .gtt = I915_GTT_PAGE_SIZE_64K | I915_GTT_PAGE_SIZE_4K,
769 .size = SZ_2M + SZ_64K,
770 .gtt = I915_GTT_PAGE_SIZE_64K,
774 .size = SZ_2M - SZ_64K,
775 .gtt = I915_GTT_PAGE_SIZE_64K,
778 /* Try without any forced padding/alignment */
782 .gtt = I915_GTT_PAGE_SIZE_4K,
786 .offset = SZ_2M - SZ_64K,
787 .gtt = I915_GTT_PAGE_SIZE_4K,
790 struct i915_vma *vma;
795 * Sanity check some of the trickiness with 64K pages -- either we can
796 * safely mark the whole page-table(2M block) as 64K, or we have to
797 * always fallback to 4K.
800 if (!HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K))
803 for (i = 0; i < ARRAY_SIZE(objects); ++i) {
804 unsigned int size = objects[i].size;
805 unsigned int expected_gtt = objects[i].gtt;
806 unsigned int offset = objects[i].offset;
807 unsigned int flags = PIN_USER;
809 for (single = 0; single <= 1; single++) {
810 obj = fake_huge_pages_object(i915, size, !!single);
814 err = i915_gem_object_pin_pages(obj);
819 * Disable 2M pages -- We only want to use 64K/4K pages
822 obj->mm.page_sizes.sg &= ~I915_GTT_PAGE_SIZE_2M;
824 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
827 goto out_object_unpin;
831 flags |= PIN_OFFSET_FIXED | offset;
833 err = i915_vma_pin(vma, 0, 0, flags);
837 err = igt_check_page_sizes(vma);
841 if (!offset && vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
842 if (!IS_ALIGNED(vma->node.start,
843 I915_GTT_PAGE_SIZE_2M)) {
844 pr_err("node.start(%llx) not aligned to 2M\n",
850 if (!IS_ALIGNED(vma->node.size,
851 I915_GTT_PAGE_SIZE_2M)) {
852 pr_err("node.size(%llx) not aligned to 2M\n",
859 if (vma->page_sizes.gtt != expected_gtt) {
860 pr_err("gtt=%u, expected=%u, i=%d, single=%s\n",
861 vma->page_sizes.gtt, expected_gtt, i,
870 i915_gem_object_unpin_pages(obj);
871 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
872 i915_gem_object_put(obj);
883 i915_gem_object_unpin_pages(obj);
885 i915_gem_object_put(obj);
890 static struct i915_vma *
891 gpu_write_dw(struct i915_vma *vma, u64 offset, u32 val)
893 struct drm_i915_private *i915 = vma->vm->i915;
894 const int gen = INTEL_GEN(i915);
895 unsigned int count = vma->size >> PAGE_SHIFT;
896 struct drm_i915_gem_object *obj;
897 struct i915_vma *batch;
903 size = (1 + 4 * count) * sizeof(u32);
904 size = round_up(size, PAGE_SIZE);
905 obj = i915_gem_object_create_internal(i915, size);
907 return ERR_CAST(obj);
909 cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
915 offset += vma->node.start;
917 for (n = 0; n < count; n++) {
919 *cmd++ = MI_STORE_DWORD_IMM_GEN4;
920 *cmd++ = lower_32_bits(offset);
921 *cmd++ = upper_32_bits(offset);
923 } else if (gen >= 4) {
924 *cmd++ = MI_STORE_DWORD_IMM_GEN4 |
925 (gen < 6 ? MI_USE_GGTT : 0);
930 *cmd++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
938 *cmd = MI_BATCH_BUFFER_END;
940 i915_gem_object_unpin_map(obj);
942 err = i915_gem_object_set_to_gtt_domain(obj, false);
946 batch = i915_vma_instance(obj, vma->vm, NULL);
948 err = PTR_ERR(batch);
952 err = i915_vma_pin(batch, 0, 0, PIN_USER);
959 i915_gem_object_put(obj);
964 static int gpu_write(struct i915_vma *vma,
965 struct i915_gem_context *ctx,
966 struct intel_engine_cs *engine,
970 struct i915_request *rq;
971 struct i915_vma *batch;
975 GEM_BUG_ON(!intel_engine_can_store_dword(engine));
977 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
981 rq = i915_request_alloc(engine, ctx);
985 batch = gpu_write_dw(vma, dword * sizeof(u32), value);
987 err = PTR_ERR(batch);
991 err = i915_vma_move_to_active(batch, rq, 0);
995 i915_gem_object_set_active_reference(batch->obj);
996 i915_vma_unpin(batch);
997 i915_vma_close(batch);
999 err = engine->emit_bb_start(rq,
1000 batch->node.start, batch->node.size,
1005 err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
1007 i915_request_skip(rq, err);
1010 i915_request_add(rq);
1015 static int cpu_check(struct drm_i915_gem_object *obj, u32 dword, u32 val)
1017 unsigned int needs_flush;
1021 err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
1025 for (n = 0; n < obj->base.size >> PAGE_SHIFT; ++n) {
1026 u32 *ptr = kmap_atomic(i915_gem_object_get_page(obj, n));
1028 if (needs_flush & CLFLUSH_BEFORE)
1029 drm_clflush_virt_range(ptr, PAGE_SIZE);
1031 if (ptr[dword] != val) {
1032 pr_err("n=%lu ptr[%u]=%u, val=%u\n",
1033 n, dword, ptr[dword], val);
1042 i915_gem_obj_finish_shmem_access(obj);
1047 static int __igt_write_huge(struct i915_gem_context *ctx,
1048 struct intel_engine_cs *engine,
1049 struct drm_i915_gem_object *obj,
1050 u64 size, u64 offset,
1053 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1054 struct i915_address_space *vm =
1055 ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
1056 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1057 struct i915_vma *vma;
1060 vma = i915_vma_instance(obj, vm, NULL);
1062 return PTR_ERR(vma);
1064 err = i915_vma_unbind(vma);
1068 err = i915_vma_pin(vma, size, 0, flags | offset);
1071 * The ggtt may have some pages reserved so
1072 * refrain from erroring out.
1074 if (err == -ENOSPC && i915_is_ggtt(vm))
1080 err = igt_check_page_sizes(vma);
1084 err = gpu_write(vma, ctx, engine, dword, val);
1086 pr_err("gpu-write failed at offset=%llx\n", offset);
1090 err = cpu_check(obj, dword, val);
1092 pr_err("cpu-check failed at offset=%llx\n", offset);
1097 i915_vma_unpin(vma);
1099 i915_vma_destroy(vma);
1104 static int igt_write_huge(struct i915_gem_context *ctx,
1105 struct drm_i915_gem_object *obj)
1107 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1108 struct i915_address_space *vm =
1109 ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
1110 static struct intel_engine_cs *engines[I915_NUM_ENGINES];
1111 struct intel_engine_cs *engine;
1112 I915_RND_STATE(prng);
1113 IGT_TIMEOUT(end_time);
1114 unsigned int max_page_size;
1123 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1125 size = obj->base.size;
1126 if (obj->mm.page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
1127 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
1129 max_page_size = rounddown_pow_of_two(obj->mm.page_sizes.sg);
1130 max = div_u64((vm->total - size), max_page_size);
1133 for_each_engine(engine, i915, id) {
1134 if (!intel_engine_can_store_dword(engine)) {
1135 pr_info("store-dword-imm not supported on engine=%u\n", id);
1138 engines[n++] = engine;
1145 * To keep things interesting when alternating between engines in our
1146 * randomized order, lets also make feeding to the same engine a few
1147 * times in succession a possibility by enlarging the permutation array.
1149 order = i915_random_order(n * I915_NUM_ENGINES, &prng);
1154 * Try various offsets in an ascending/descending fashion until we
1155 * timeout -- we want to avoid issues hidden by effectively always using
1159 for_each_prime_number_from(num, 0, max) {
1160 u64 offset_low = num * max_page_size;
1161 u64 offset_high = (max - num) * max_page_size;
1162 u32 dword = offset_in_page(num) / 4;
1164 engine = engines[order[i] % n];
1165 i = (i + 1) % (n * I915_NUM_ENGINES);
1167 err = __igt_write_huge(ctx, engine, obj, size, offset_low, dword, num + 1);
1171 err = __igt_write_huge(ctx, engine, obj, size, offset_high, dword, num + 1);
1175 if (igt_timeout(end_time,
1176 "%s timed out on engine=%u, offset_low=%llx offset_high=%llx, max_page_size=%x\n",
1177 __func__, engine->id, offset_low, offset_high, max_page_size))
1186 static int igt_ppgtt_exhaust_huge(void *arg)
1188 struct i915_gem_context *ctx = arg;
1189 struct drm_i915_private *i915 = ctx->i915;
1190 unsigned long supported = INTEL_INFO(i915)->page_sizes;
1191 static unsigned int pages[ARRAY_SIZE(page_sizes)];
1192 struct drm_i915_gem_object *obj;
1193 unsigned int size_mask;
1194 unsigned int page_mask;
1198 if (supported == I915_GTT_PAGE_SIZE_4K)
1202 * Sanity check creating objects with a varying mix of page sizes --
1203 * ensuring that our writes lands in the right place.
1207 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1)
1208 pages[n++] = BIT(i);
1210 for (size_mask = 2; size_mask < BIT(n); size_mask++) {
1211 unsigned int size = 0;
1213 for (i = 0; i < n; i++) {
1214 if (size_mask & BIT(i))
1219 * For our page mask we want to enumerate all the page-size
1220 * combinations which will fit into our chosen object size.
1222 for (page_mask = 2; page_mask <= size_mask; page_mask++) {
1223 unsigned int page_sizes = 0;
1225 for (i = 0; i < n; i++) {
1226 if (page_mask & BIT(i))
1227 page_sizes |= pages[i];
1231 * Ensure that we can actually fill the given object
1232 * with our chosen page mask.
1234 if (!IS_ALIGNED(size, BIT(__ffs(page_sizes))))
1237 obj = huge_pages_object(i915, size, page_sizes);
1243 err = i915_gem_object_pin_pages(obj);
1245 i915_gem_object_put(obj);
1247 if (err == -ENOMEM) {
1248 pr_info("unable to get pages, size=%u, pages=%u\n",
1254 pr_err("pin_pages failed, size=%u, pages=%u\n",
1255 size_mask, page_mask);
1260 /* Force the page-size for the gtt insertion */
1261 obj->mm.page_sizes.sg = page_sizes;
1263 err = igt_write_huge(ctx, obj);
1265 pr_err("exhaust write-huge failed with size=%u\n",
1270 i915_gem_object_unpin_pages(obj);
1271 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
1272 i915_gem_object_put(obj);
1279 i915_gem_object_unpin_pages(obj);
1280 i915_gem_object_put(obj);
1282 mkwrite_device_info(i915)->page_sizes = supported;
1287 static int igt_ppgtt_internal_huge(void *arg)
1289 struct i915_gem_context *ctx = arg;
1290 struct drm_i915_private *i915 = ctx->i915;
1291 struct drm_i915_gem_object *obj;
1292 static const unsigned int sizes[] = {
1304 * Sanity check that the HW uses huge pages correctly through internal
1305 * -- ensure that our writes land in the right place.
1308 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
1309 unsigned int size = sizes[i];
1311 obj = i915_gem_object_create_internal(i915, size);
1313 return PTR_ERR(obj);
1315 err = i915_gem_object_pin_pages(obj);
1319 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_64K) {
1320 pr_info("internal unable to allocate huge-page(s) with size=%u\n",
1325 err = igt_write_huge(ctx, obj);
1327 pr_err("internal write-huge failed with size=%u\n",
1332 i915_gem_object_unpin_pages(obj);
1333 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
1334 i915_gem_object_put(obj);
1340 i915_gem_object_unpin_pages(obj);
1342 i915_gem_object_put(obj);
1347 static inline bool igt_can_allocate_thp(struct drm_i915_private *i915)
1349 return i915->mm.gemfs && has_transparent_hugepage();
1352 static int igt_ppgtt_gemfs_huge(void *arg)
1354 struct i915_gem_context *ctx = arg;
1355 struct drm_i915_private *i915 = ctx->i915;
1356 struct drm_i915_gem_object *obj;
1357 static const unsigned int sizes[] = {
1368 * Sanity check that the HW uses huge pages correctly through gemfs --
1369 * ensure that our writes land in the right place.
1372 if (!igt_can_allocate_thp(i915)) {
1373 pr_info("missing THP support, skipping\n");
1377 for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
1378 unsigned int size = sizes[i];
1380 obj = i915_gem_object_create(i915, size);
1382 return PTR_ERR(obj);
1384 err = i915_gem_object_pin_pages(obj);
1388 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
1389 pr_info("finishing test early, gemfs unable to allocate huge-page(s) with size=%u\n",
1394 err = igt_write_huge(ctx, obj);
1396 pr_err("gemfs write-huge failed with size=%u\n",
1401 i915_gem_object_unpin_pages(obj);
1402 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
1403 i915_gem_object_put(obj);
1409 i915_gem_object_unpin_pages(obj);
1411 i915_gem_object_put(obj);
1416 static int igt_ppgtt_pin_update(void *arg)
1418 struct i915_gem_context *ctx = arg;
1419 struct drm_i915_private *dev_priv = ctx->i915;
1420 unsigned long supported = INTEL_INFO(dev_priv)->page_sizes;
1421 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
1422 struct drm_i915_gem_object *obj;
1423 struct i915_vma *vma;
1424 unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
1429 * Make sure there's no funny business when doing a PIN_UPDATE -- in the
1430 * past we had a subtle issue with being able to incorrectly do multiple
1431 * alloc va ranges on the same object when doing a PIN_UPDATE, which
1432 * resulted in some pretty nasty bugs, though only when using
1436 if (!USES_FULL_48BIT_PPGTT(dev_priv)) {
1437 pr_info("48b PPGTT not supported, skipping\n");
1441 first = ilog2(I915_GTT_PAGE_SIZE_64K);
1442 last = ilog2(I915_GTT_PAGE_SIZE_2M);
1444 for_each_set_bit_from(first, &supported, last + 1) {
1445 unsigned int page_size = BIT(first);
1447 obj = i915_gem_object_create_internal(dev_priv, page_size);
1449 return PTR_ERR(obj);
1451 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
1457 err = i915_vma_pin(vma, SZ_2M, 0, flags);
1461 if (vma->page_sizes.sg < page_size) {
1462 pr_info("Unable to allocate page-size %x, finishing test early\n",
1467 err = igt_check_page_sizes(vma);
1471 if (vma->page_sizes.gtt != page_size) {
1472 dma_addr_t addr = i915_gem_object_get_dma_address(obj, 0);
1475 * The only valid reason for this to ever fail would be
1476 * if the dma-mapper screwed us over when we did the
1477 * dma_map_sg(), since it has the final say over the dma
1480 if (IS_ALIGNED(addr, page_size)) {
1481 pr_err("page_sizes.gtt=%u, expected=%u\n",
1482 vma->page_sizes.gtt, page_size);
1485 pr_info("dma address misaligned, finishing test early\n");
1491 err = i915_vma_bind(vma, I915_CACHE_NONE, PIN_UPDATE);
1495 i915_vma_unpin(vma);
1496 i915_vma_close(vma);
1498 i915_gem_object_put(obj);
1501 obj = i915_gem_object_create_internal(dev_priv, PAGE_SIZE);
1503 return PTR_ERR(obj);
1505 vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
1511 err = i915_vma_pin(vma, 0, 0, flags);
1516 * Make sure we don't end up with something like where the pde is still
1517 * pointing to the 2M page, and the pt we just filled-in is dangling --
1518 * we can check this by writing to the first page where it would then
1519 * land in the now stale 2M page.
1522 err = gpu_write(vma, ctx, dev_priv->engine[RCS], 0, 0xdeadbeaf);
1526 err = cpu_check(obj, 0, 0xdeadbeaf);
1529 i915_vma_unpin(vma);
1531 i915_vma_close(vma);
1533 i915_gem_object_put(obj);
1538 static int igt_tmpfs_fallback(void *arg)
1540 struct i915_gem_context *ctx = arg;
1541 struct drm_i915_private *i915 = ctx->i915;
1542 struct vfsmount *gemfs = i915->mm.gemfs;
1543 struct i915_address_space *vm =
1544 ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
1545 struct drm_i915_gem_object *obj;
1546 struct i915_vma *vma;
1551 * Make sure that we don't burst into a ball of flames upon falling back
1552 * to tmpfs, which we rely on if on the off-chance we encouter a failure
1553 * when setting up gemfs.
1556 i915->mm.gemfs = NULL;
1558 obj = i915_gem_object_create(i915, PAGE_SIZE);
1564 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1565 if (IS_ERR(vaddr)) {
1566 err = PTR_ERR(vaddr);
1569 *vaddr = 0xdeadbeaf;
1571 i915_gem_object_unpin_map(obj);
1573 vma = i915_vma_instance(obj, vm, NULL);
1579 err = i915_vma_pin(vma, 0, 0, PIN_USER);
1583 err = igt_check_page_sizes(vma);
1585 i915_vma_unpin(vma);
1587 i915_vma_close(vma);
1589 i915_gem_object_put(obj);
1591 i915->mm.gemfs = gemfs;
1596 static int igt_shrink_thp(void *arg)
1598 struct i915_gem_context *ctx = arg;
1599 struct drm_i915_private *i915 = ctx->i915;
1600 struct i915_address_space *vm =
1601 ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
1602 struct drm_i915_gem_object *obj;
1603 struct i915_vma *vma;
1604 unsigned int flags = PIN_USER;
1608 * Sanity check shrinking huge-paged object -- make sure nothing blows
1612 if (!igt_can_allocate_thp(i915)) {
1613 pr_info("missing THP support, skipping\n");
1617 obj = i915_gem_object_create(i915, SZ_2M);
1619 return PTR_ERR(obj);
1621 vma = i915_vma_instance(obj, vm, NULL);
1627 err = i915_vma_pin(vma, 0, 0, flags);
1631 if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
1632 pr_info("failed to allocate THP, finishing test early\n");
1636 err = igt_check_page_sizes(vma);
1640 err = gpu_write(vma, ctx, i915->engine[RCS], 0, 0xdeadbeaf);
1644 i915_vma_unpin(vma);
1647 * Now that the pages are *unpinned* shrink-all should invoke
1648 * shmem to truncate our pages.
1650 i915_gem_shrink_all(i915);
1651 if (i915_gem_object_has_pages(obj)) {
1652 pr_err("shrink-all didn't truncate the pages\n");
1657 if (obj->mm.page_sizes.sg || obj->mm.page_sizes.phys) {
1658 pr_err("residual page-size bits left\n");
1663 err = i915_vma_pin(vma, 0, 0, flags);
1667 err = cpu_check(obj, 0, 0xdeadbeaf);
1670 i915_vma_unpin(vma);
1672 i915_vma_close(vma);
1674 i915_gem_object_put(obj);
1679 int i915_gem_huge_page_mock_selftests(void)
1681 static const struct i915_subtest tests[] = {
1682 SUBTEST(igt_mock_exhaust_device_supported_pages),
1683 SUBTEST(igt_mock_ppgtt_misaligned_dma),
1684 SUBTEST(igt_mock_ppgtt_huge_fill),
1685 SUBTEST(igt_mock_ppgtt_64K),
1687 int saved_ppgtt = i915_modparams.enable_ppgtt;
1688 struct drm_i915_private *dev_priv;
1689 struct pci_dev *pdev;
1690 struct i915_hw_ppgtt *ppgtt;
1693 dev_priv = mock_gem_device();
1697 /* Pretend to be a device which supports the 48b PPGTT */
1698 i915_modparams.enable_ppgtt = 3;
1700 pdev = dev_priv->drm.pdev;
1701 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(39));
1703 mutex_lock(&dev_priv->drm.struct_mutex);
1704 ppgtt = i915_ppgtt_create(dev_priv, ERR_PTR(-ENODEV));
1705 if (IS_ERR(ppgtt)) {
1706 err = PTR_ERR(ppgtt);
1710 if (!i915_vm_is_48bit(&ppgtt->vm)) {
1711 pr_err("failed to create 48b PPGTT\n");
1716 /* If we were ever hit this then it's time to mock the 64K scratch */
1717 if (!i915_vm_has_scratch_64K(&ppgtt->vm)) {
1718 pr_err("PPGTT missing 64K scratch page\n");
1723 err = i915_subtests(tests, ppgtt);
1726 i915_ppgtt_close(&ppgtt->vm);
1727 i915_ppgtt_put(ppgtt);
1730 mutex_unlock(&dev_priv->drm.struct_mutex);
1732 i915_modparams.enable_ppgtt = saved_ppgtt;
1734 drm_dev_put(&dev_priv->drm);
1739 int i915_gem_huge_page_live_selftests(struct drm_i915_private *dev_priv)
1741 static const struct i915_subtest tests[] = {
1742 SUBTEST(igt_shrink_thp),
1743 SUBTEST(igt_ppgtt_pin_update),
1744 SUBTEST(igt_tmpfs_fallback),
1745 SUBTEST(igt_ppgtt_exhaust_huge),
1746 SUBTEST(igt_ppgtt_gemfs_huge),
1747 SUBTEST(igt_ppgtt_internal_huge),
1749 struct drm_file *file;
1750 struct i915_gem_context *ctx;
1753 if (!USES_PPGTT(dev_priv)) {
1754 pr_info("PPGTT not supported, skipping live-selftests\n");
1758 if (i915_terminally_wedged(&dev_priv->gpu_error))
1761 file = mock_file(dev_priv);
1763 return PTR_ERR(file);
1765 mutex_lock(&dev_priv->drm.struct_mutex);
1766 intel_runtime_pm_get(dev_priv);
1768 ctx = live_context(dev_priv, file);
1775 ctx->ppgtt->vm.scrub_64K = true;
1777 err = i915_subtests(tests, ctx);
1780 intel_runtime_pm_put(dev_priv);
1781 mutex_unlock(&dev_priv->drm.struct_mutex);
1783 mock_file_free(dev_priv, file);