2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <linux/list_sort.h>
26 #include <linux/prime_numbers.h>
28 #include "gem/i915_gem_context.h"
29 #include "gem/selftests/mock_context.h"
30 #include "gt/intel_context.h"
32 #include "i915_random.h"
33 #include "i915_selftest.h"
36 #include "mock_gem_device.h"
38 #include "igt_flush_test.h"
40 static void cleanup_freed_objects(struct drm_i915_private
*i915
)
42 i915_gem_drain_freed_objects(i915
);
45 static void fake_free_pages(struct drm_i915_gem_object
*obj
,
46 struct sg_table
*pages
)
52 static int fake_get_pages(struct drm_i915_gem_object
*obj
)
54 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
55 #define PFN_BIAS 0x1000
56 struct sg_table
*pages
;
57 struct scatterlist
*sg
;
58 unsigned int sg_page_sizes
;
59 typeof(obj
->base
.size
) rem
;
61 pages
= kmalloc(sizeof(*pages
), GFP
);
65 rem
= round_up(obj
->base
.size
, BIT(31)) >> 31;
66 if (sg_alloc_table(pages
, rem
, GFP
)) {
73 for (sg
= pages
->sgl
; sg
; sg
= sg_next(sg
)) {
74 unsigned long len
= min_t(typeof(rem
), rem
, BIT(31));
77 sg_set_page(sg
, pfn_to_page(PFN_BIAS
), len
, 0);
78 sg_dma_address(sg
) = page_to_phys(sg_page(sg
));
86 __i915_gem_object_set_pages(obj
, pages
, sg_page_sizes
);
92 static void fake_put_pages(struct drm_i915_gem_object
*obj
,
93 struct sg_table
*pages
)
95 fake_free_pages(obj
, pages
);
96 obj
->mm
.dirty
= false;
99 static const struct drm_i915_gem_object_ops fake_ops
= {
100 .flags
= I915_GEM_OBJECT_IS_SHRINKABLE
,
101 .get_pages
= fake_get_pages
,
102 .put_pages
= fake_put_pages
,
105 static struct drm_i915_gem_object
*
106 fake_dma_object(struct drm_i915_private
*i915
, u64 size
)
108 static struct lock_class_key lock_class
;
109 struct drm_i915_gem_object
*obj
;
112 GEM_BUG_ON(!IS_ALIGNED(size
, I915_GTT_PAGE_SIZE
));
114 if (overflows_type(size
, obj
->base
.size
))
115 return ERR_PTR(-E2BIG
);
117 obj
= i915_gem_object_alloc();
121 drm_gem_private_object_init(&i915
->drm
, &obj
->base
, size
);
122 i915_gem_object_init(obj
, &fake_ops
, &lock_class
);
124 i915_gem_object_set_volatile(obj
);
126 obj
->write_domain
= I915_GEM_DOMAIN_CPU
;
127 obj
->read_domains
= I915_GEM_DOMAIN_CPU
;
128 obj
->cache_level
= I915_CACHE_NONE
;
130 /* Preallocate the "backing storage" */
131 if (i915_gem_object_pin_pages(obj
))
134 i915_gem_object_unpin_pages(obj
);
138 i915_gem_object_put(obj
);
140 return ERR_PTR(-ENOMEM
);
143 static int igt_ppgtt_alloc(void *arg
)
145 struct drm_i915_private
*dev_priv
= arg
;
146 struct i915_ppgtt
*ppgtt
;
147 u64 size
, last
, limit
;
150 /* Allocate a ppggt and try to fill the entire range */
152 if (!HAS_PPGTT(dev_priv
))
155 ppgtt
= i915_ppgtt_create(&dev_priv
->gt
);
157 return PTR_ERR(ppgtt
);
159 if (!ppgtt
->vm
.allocate_va_range
)
160 goto err_ppgtt_cleanup
;
163 * While we only allocate the page tables here and so we could
164 * address a much larger GTT than we could actually fit into
165 * RAM, a practical limit is the amount of physical pages in the system.
166 * This should ensure that we do not run into the oomkiller during
167 * the test and take down the machine wilfully.
169 limit
= totalram_pages() << PAGE_SHIFT
;
170 limit
= min(ppgtt
->vm
.total
, limit
);
172 /* Check we can allocate the entire range */
173 for (size
= 4096; size
<= limit
; size
<<= 2) {
174 err
= ppgtt
->vm
.allocate_va_range(&ppgtt
->vm
, 0, size
);
176 if (err
== -ENOMEM
) {
177 pr_info("[1] Ran out of memory for va_range [0 + %llx] [bit %d]\n",
179 err
= 0; /* virtual space too large! */
181 goto err_ppgtt_cleanup
;
186 ppgtt
->vm
.clear_range(&ppgtt
->vm
, 0, size
);
189 /* Check we can incrementally allocate the entire range */
190 for (last
= 0, size
= 4096; size
<= limit
; last
= size
, size
<<= 2) {
191 err
= ppgtt
->vm
.allocate_va_range(&ppgtt
->vm
,
194 if (err
== -ENOMEM
) {
195 pr_info("[2] Ran out of memory for va_range [%llx + %llx] [bit %d]\n",
196 last
, size
- last
, ilog2(size
));
197 err
= 0; /* virtual space too large! */
199 goto err_ppgtt_cleanup
;
206 i915_vm_put(&ppgtt
->vm
);
210 static int lowlevel_hole(struct i915_address_space
*vm
,
211 u64 hole_start
, u64 hole_end
,
212 unsigned long end_time
)
214 I915_RND_STATE(seed_prng
);
215 struct i915_vma
*mock_vma
;
218 mock_vma
= kzalloc(sizeof(*mock_vma
), GFP_KERNEL
);
222 /* Keep creating larger objects until one cannot fit into the hole */
223 for (size
= 12; (hole_end
- hole_start
) >> size
; size
++) {
224 I915_RND_SUBSTATE(prng
, seed_prng
);
225 struct drm_i915_gem_object
*obj
;
226 unsigned int *order
, count
, n
;
229 hole_size
= (hole_end
- hole_start
) >> size
;
230 if (hole_size
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
231 hole_size
= KMALLOC_MAX_SIZE
/ sizeof(u32
);
232 count
= hole_size
>> 1;
234 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
235 __func__
, hole_start
, hole_end
, size
, hole_size
);
240 order
= i915_random_order(count
, &prng
);
243 } while (count
>>= 1);
250 GEM_BUG_ON(count
* BIT_ULL(size
) > vm
->total
);
251 GEM_BUG_ON(hole_start
+ count
* BIT_ULL(size
) > hole_end
);
253 /* Ignore allocation failures (i.e. don't report them as
254 * a test failure) as we are purposefully allocating very
255 * large objects without checking that we have sufficient
256 * memory. We expect to hit -ENOMEM.
259 obj
= fake_dma_object(vm
->i915
, BIT_ULL(size
));
265 GEM_BUG_ON(obj
->base
.size
!= BIT_ULL(size
));
267 if (i915_gem_object_pin_pages(obj
)) {
268 i915_gem_object_put(obj
);
273 for (n
= 0; n
< count
; n
++) {
274 u64 addr
= hole_start
+ order
[n
] * BIT_ULL(size
);
275 intel_wakeref_t wakeref
;
277 GEM_BUG_ON(addr
+ BIT_ULL(size
) > vm
->total
);
279 if (igt_timeout(end_time
,
280 "%s timed out before %d/%d\n",
281 __func__
, n
, count
)) {
282 hole_end
= hole_start
; /* quit */
286 if (vm
->allocate_va_range
&&
287 vm
->allocate_va_range(vm
, addr
, BIT_ULL(size
)))
290 mock_vma
->pages
= obj
->mm
.pages
;
291 mock_vma
->node
.size
= BIT_ULL(size
);
292 mock_vma
->node
.start
= addr
;
294 with_intel_runtime_pm(vm
->gt
->uncore
->rpm
, wakeref
)
295 vm
->insert_entries(vm
, mock_vma
,
300 i915_random_reorder(order
, count
, &prng
);
301 for (n
= 0; n
< count
; n
++) {
302 u64 addr
= hole_start
+ order
[n
] * BIT_ULL(size
);
303 intel_wakeref_t wakeref
;
305 GEM_BUG_ON(addr
+ BIT_ULL(size
) > vm
->total
);
306 with_intel_runtime_pm(vm
->gt
->uncore
->rpm
, wakeref
)
307 vm
->clear_range(vm
, addr
, BIT_ULL(size
));
310 i915_gem_object_unpin_pages(obj
);
311 i915_gem_object_put(obj
);
315 cleanup_freed_objects(vm
->i915
);
322 static void close_object_list(struct list_head
*objects
,
323 struct i915_address_space
*vm
)
325 struct drm_i915_gem_object
*obj
, *on
;
328 list_for_each_entry_safe(obj
, on
, objects
, st_link
) {
329 struct i915_vma
*vma
;
331 vma
= i915_vma_instance(obj
, vm
, NULL
);
333 ignored
= i915_vma_unbind(vma
);
334 /* Only ppgtt vma may be closed before the object is freed */
335 if (!IS_ERR(vma
) && !i915_vma_is_ggtt(vma
))
338 list_del(&obj
->st_link
);
339 i915_gem_object_put(obj
);
343 static int fill_hole(struct i915_address_space
*vm
,
344 u64 hole_start
, u64 hole_end
,
345 unsigned long end_time
)
347 const u64 hole_size
= hole_end
- hole_start
;
348 struct drm_i915_gem_object
*obj
;
349 const unsigned long max_pages
=
350 min_t(u64
, ULONG_MAX
- 1, hole_size
/2 >> PAGE_SHIFT
);
351 const unsigned long max_step
= max(int_sqrt(max_pages
), 2UL);
352 unsigned long npages
, prime
, flags
;
353 struct i915_vma
*vma
;
357 /* Try binding many VMA working inwards from either edge */
359 flags
= PIN_OFFSET_FIXED
| PIN_USER
;
360 if (i915_is_ggtt(vm
))
363 for_each_prime_number_from(prime
, 2, max_step
) {
364 for (npages
= 1; npages
<= max_pages
; npages
*= prime
) {
365 const u64 full_size
= npages
<< PAGE_SHIFT
;
371 { "top-down", hole_end
, -1, },
372 { "bottom-up", hole_start
, 1, },
376 obj
= fake_dma_object(vm
->i915
, full_size
);
380 list_add(&obj
->st_link
, &objects
);
382 /* Align differing sized objects against the edges, and
383 * check we don't walk off into the void when binding
386 for (p
= phases
; p
->name
; p
++) {
390 list_for_each_entry(obj
, &objects
, st_link
) {
391 vma
= i915_vma_instance(obj
, vm
, NULL
);
396 if (offset
< hole_start
+ obj
->base
.size
)
398 offset
-= obj
->base
.size
;
401 err
= i915_vma_pin(vma
, 0, 0, offset
| flags
);
403 pr_err("%s(%s) pin (forward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
404 __func__
, p
->name
, err
, npages
, prime
, offset
);
408 if (!drm_mm_node_allocated(&vma
->node
) ||
409 i915_vma_misplaced(vma
, 0, 0, offset
| flags
)) {
410 pr_err("%s(%s) (forward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
411 __func__
, p
->name
, vma
->node
.start
, vma
->node
.size
, drm_mm_node_allocated(&vma
->node
),
420 if (offset
+ obj
->base
.size
> hole_end
)
422 offset
+= obj
->base
.size
;
427 list_for_each_entry(obj
, &objects
, st_link
) {
428 vma
= i915_vma_instance(obj
, vm
, NULL
);
433 if (offset
< hole_start
+ obj
->base
.size
)
435 offset
-= obj
->base
.size
;
438 if (!drm_mm_node_allocated(&vma
->node
) ||
439 i915_vma_misplaced(vma
, 0, 0, offset
| flags
)) {
440 pr_err("%s(%s) (forward) moved vma.node=%llx + %llx, expected offset %llx\n",
441 __func__
, p
->name
, vma
->node
.start
, vma
->node
.size
,
447 err
= i915_vma_unbind(vma
);
449 pr_err("%s(%s) (forward) unbind of vma.node=%llx + %llx failed with err=%d\n",
450 __func__
, p
->name
, vma
->node
.start
, vma
->node
.size
,
456 if (offset
+ obj
->base
.size
> hole_end
)
458 offset
+= obj
->base
.size
;
463 list_for_each_entry_reverse(obj
, &objects
, st_link
) {
464 vma
= i915_vma_instance(obj
, vm
, NULL
);
469 if (offset
< hole_start
+ obj
->base
.size
)
471 offset
-= obj
->base
.size
;
474 err
= i915_vma_pin(vma
, 0, 0, offset
| flags
);
476 pr_err("%s(%s) pin (backward) failed with err=%d on size=%lu pages (prime=%lu), offset=%llx\n",
477 __func__
, p
->name
, err
, npages
, prime
, offset
);
481 if (!drm_mm_node_allocated(&vma
->node
) ||
482 i915_vma_misplaced(vma
, 0, 0, offset
| flags
)) {
483 pr_err("%s(%s) (backward) insert failed: vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
484 __func__
, p
->name
, vma
->node
.start
, vma
->node
.size
, drm_mm_node_allocated(&vma
->node
),
493 if (offset
+ obj
->base
.size
> hole_end
)
495 offset
+= obj
->base
.size
;
500 list_for_each_entry_reverse(obj
, &objects
, st_link
) {
501 vma
= i915_vma_instance(obj
, vm
, NULL
);
506 if (offset
< hole_start
+ obj
->base
.size
)
508 offset
-= obj
->base
.size
;
511 if (!drm_mm_node_allocated(&vma
->node
) ||
512 i915_vma_misplaced(vma
, 0, 0, offset
| flags
)) {
513 pr_err("%s(%s) (backward) moved vma.node=%llx + %llx [allocated? %d], expected offset %llx\n",
514 __func__
, p
->name
, vma
->node
.start
, vma
->node
.size
, drm_mm_node_allocated(&vma
->node
),
520 err
= i915_vma_unbind(vma
);
522 pr_err("%s(%s) (backward) unbind of vma.node=%llx + %llx failed with err=%d\n",
523 __func__
, p
->name
, vma
->node
.start
, vma
->node
.size
,
529 if (offset
+ obj
->base
.size
> hole_end
)
531 offset
+= obj
->base
.size
;
536 if (igt_timeout(end_time
, "%s timed out (npages=%lu, prime=%lu)\n",
537 __func__
, npages
, prime
)) {
543 close_object_list(&objects
, vm
);
544 cleanup_freed_objects(vm
->i915
);
550 close_object_list(&objects
, vm
);
554 static int walk_hole(struct i915_address_space
*vm
,
555 u64 hole_start
, u64 hole_end
,
556 unsigned long end_time
)
558 const u64 hole_size
= hole_end
- hole_start
;
559 const unsigned long max_pages
=
560 min_t(u64
, ULONG_MAX
- 1, hole_size
>> PAGE_SHIFT
);
564 /* Try binding a single VMA in different positions within the hole */
566 flags
= PIN_OFFSET_FIXED
| PIN_USER
;
567 if (i915_is_ggtt(vm
))
570 for_each_prime_number_from(size
, 1, max_pages
) {
571 struct drm_i915_gem_object
*obj
;
572 struct i915_vma
*vma
;
576 obj
= fake_dma_object(vm
->i915
, size
<< PAGE_SHIFT
);
580 vma
= i915_vma_instance(obj
, vm
, NULL
);
586 for (addr
= hole_start
;
587 addr
+ obj
->base
.size
< hole_end
;
588 addr
+= obj
->base
.size
) {
589 err
= i915_vma_pin(vma
, 0, 0, addr
| flags
);
591 pr_err("%s bind failed at %llx + %llx [hole %llx- %llx] with err=%d\n",
592 __func__
, addr
, vma
->size
,
593 hole_start
, hole_end
, err
);
598 if (!drm_mm_node_allocated(&vma
->node
) ||
599 i915_vma_misplaced(vma
, 0, 0, addr
| flags
)) {
600 pr_err("%s incorrect at %llx + %llx\n",
601 __func__
, addr
, vma
->size
);
606 err
= i915_vma_unbind(vma
);
608 pr_err("%s unbind failed at %llx + %llx with err=%d\n",
609 __func__
, addr
, vma
->size
, err
);
613 GEM_BUG_ON(drm_mm_node_allocated(&vma
->node
));
615 if (igt_timeout(end_time
,
616 "%s timed out at %llx\n",
624 if (!i915_vma_is_ggtt(vma
))
627 i915_gem_object_put(obj
);
631 cleanup_freed_objects(vm
->i915
);
637 static int pot_hole(struct i915_address_space
*vm
,
638 u64 hole_start
, u64 hole_end
,
639 unsigned long end_time
)
641 struct drm_i915_gem_object
*obj
;
642 struct i915_vma
*vma
;
647 flags
= PIN_OFFSET_FIXED
| PIN_USER
;
648 if (i915_is_ggtt(vm
))
651 obj
= i915_gem_object_create_internal(vm
->i915
, 2 * I915_GTT_PAGE_SIZE
);
655 vma
= i915_vma_instance(obj
, vm
, NULL
);
661 /* Insert a pair of pages across every pot boundary within the hole */
662 for (pot
= fls64(hole_end
- 1) - 1;
663 pot
> ilog2(2 * I915_GTT_PAGE_SIZE
);
665 u64 step
= BIT_ULL(pot
);
668 for (addr
= round_up(hole_start
+ I915_GTT_PAGE_SIZE
, step
) - I915_GTT_PAGE_SIZE
;
669 addr
<= round_down(hole_end
- 2*I915_GTT_PAGE_SIZE
, step
) - I915_GTT_PAGE_SIZE
;
671 err
= i915_vma_pin(vma
, 0, 0, addr
| flags
);
673 pr_err("%s failed to pin object at %llx in hole [%llx - %llx], with err=%d\n",
676 hole_start
, hole_end
,
681 if (!drm_mm_node_allocated(&vma
->node
) ||
682 i915_vma_misplaced(vma
, 0, 0, addr
| flags
)) {
683 pr_err("%s incorrect at %llx + %llx\n",
684 __func__
, addr
, vma
->size
);
686 err
= i915_vma_unbind(vma
);
692 err
= i915_vma_unbind(vma
);
696 if (igt_timeout(end_time
,
697 "%s timed out after %d/%d\n",
698 __func__
, pot
, fls64(hole_end
- 1) - 1)) {
705 if (!i915_vma_is_ggtt(vma
))
708 i915_gem_object_put(obj
);
712 static int drunk_hole(struct i915_address_space
*vm
,
713 u64 hole_start
, u64 hole_end
,
714 unsigned long end_time
)
716 I915_RND_STATE(prng
);
720 flags
= PIN_OFFSET_FIXED
| PIN_USER
;
721 if (i915_is_ggtt(vm
))
724 /* Keep creating larger objects until one cannot fit into the hole */
725 for (size
= 12; (hole_end
- hole_start
) >> size
; size
++) {
726 struct drm_i915_gem_object
*obj
;
727 unsigned int *order
, count
, n
;
728 struct i915_vma
*vma
;
732 hole_size
= (hole_end
- hole_start
) >> size
;
733 if (hole_size
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
734 hole_size
= KMALLOC_MAX_SIZE
/ sizeof(u32
);
735 count
= hole_size
>> 1;
737 pr_debug("%s: hole is too small [%llx - %llx] >> %d: %lld\n",
738 __func__
, hole_start
, hole_end
, size
, hole_size
);
743 order
= i915_random_order(count
, &prng
);
746 } while (count
>>= 1);
751 /* Ignore allocation failures (i.e. don't report them as
752 * a test failure) as we are purposefully allocating very
753 * large objects without checking that we have sufficient
754 * memory. We expect to hit -ENOMEM.
757 obj
= fake_dma_object(vm
->i915
, BIT_ULL(size
));
763 vma
= i915_vma_instance(obj
, vm
, NULL
);
769 GEM_BUG_ON(vma
->size
!= BIT_ULL(size
));
771 for (n
= 0; n
< count
; n
++) {
772 u64 addr
= hole_start
+ order
[n
] * BIT_ULL(size
);
774 err
= i915_vma_pin(vma
, 0, 0, addr
| flags
);
776 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
779 hole_start
, hole_end
,
784 if (!drm_mm_node_allocated(&vma
->node
) ||
785 i915_vma_misplaced(vma
, 0, 0, addr
| flags
)) {
786 pr_err("%s incorrect at %llx + %llx\n",
787 __func__
, addr
, BIT_ULL(size
));
789 err
= i915_vma_unbind(vma
);
795 err
= i915_vma_unbind(vma
);
798 if (igt_timeout(end_time
,
799 "%s timed out after %d/%d\n",
800 __func__
, n
, count
)) {
807 if (!i915_vma_is_ggtt(vma
))
810 i915_gem_object_put(obj
);
815 cleanup_freed_objects(vm
->i915
);
821 static int __shrink_hole(struct i915_address_space
*vm
,
822 u64 hole_start
, u64 hole_end
,
823 unsigned long end_time
)
825 struct drm_i915_gem_object
*obj
;
826 unsigned long flags
= PIN_OFFSET_FIXED
| PIN_USER
;
827 unsigned int order
= 12;
832 /* Keep creating larger objects until one cannot fit into the hole */
833 for (addr
= hole_start
; addr
< hole_end
; ) {
834 struct i915_vma
*vma
;
835 u64 size
= BIT_ULL(order
++);
837 size
= min(size
, hole_end
- addr
);
838 obj
= fake_dma_object(vm
->i915
, size
);
844 list_add(&obj
->st_link
, &objects
);
846 vma
= i915_vma_instance(obj
, vm
, NULL
);
852 GEM_BUG_ON(vma
->size
!= size
);
854 err
= i915_vma_pin(vma
, 0, 0, addr
| flags
);
856 pr_err("%s failed to pin object at %llx + %llx in hole [%llx - %llx], with err=%d\n",
857 __func__
, addr
, size
, hole_start
, hole_end
, err
);
861 if (!drm_mm_node_allocated(&vma
->node
) ||
862 i915_vma_misplaced(vma
, 0, 0, addr
| flags
)) {
863 pr_err("%s incorrect at %llx + %llx\n",
864 __func__
, addr
, size
);
866 err
= i915_vma_unbind(vma
);
875 * Since we are injecting allocation faults at random intervals,
876 * wait for this allocation to complete before we change the
879 err
= i915_vma_sync(vma
);
883 if (igt_timeout(end_time
,
884 "%s timed out at ofset %llx [%llx - %llx]\n",
885 __func__
, addr
, hole_start
, hole_end
)) {
891 close_object_list(&objects
, vm
);
892 cleanup_freed_objects(vm
->i915
);
896 static int shrink_hole(struct i915_address_space
*vm
,
897 u64 hole_start
, u64 hole_end
,
898 unsigned long end_time
)
903 vm
->fault_attr
.probability
= 999;
904 atomic_set(&vm
->fault_attr
.times
, -1);
906 for_each_prime_number_from(prime
, 0, ULONG_MAX
- 1) {
907 vm
->fault_attr
.interval
= prime
;
908 err
= __shrink_hole(vm
, hole_start
, hole_end
, end_time
);
913 memset(&vm
->fault_attr
, 0, sizeof(vm
->fault_attr
));
918 static int shrink_boom(struct i915_address_space
*vm
,
919 u64 hole_start
, u64 hole_end
,
920 unsigned long end_time
)
922 unsigned int sizes
[] = { SZ_2M
, SZ_1G
};
923 struct drm_i915_gem_object
*purge
;
924 struct drm_i915_gem_object
*explode
;
929 * Catch the case which shrink_hole seems to miss. The setup here
930 * requires invoking the shrinker as we do the alloc_pt/alloc_pd, while
931 * ensuring that all vma assiocated with the respective pd/pdp are
932 * unpinned at the time.
935 for (i
= 0; i
< ARRAY_SIZE(sizes
); ++i
) {
936 unsigned int flags
= PIN_USER
| PIN_OFFSET_FIXED
;
937 unsigned int size
= sizes
[i
];
938 struct i915_vma
*vma
;
940 purge
= fake_dma_object(vm
->i915
, size
);
942 return PTR_ERR(purge
);
944 vma
= i915_vma_instance(purge
, vm
, NULL
);
950 err
= i915_vma_pin(vma
, 0, 0, flags
);
954 /* Should now be ripe for purging */
957 explode
= fake_dma_object(vm
->i915
, size
);
958 if (IS_ERR(explode
)) {
959 err
= PTR_ERR(explode
);
963 vm
->fault_attr
.probability
= 100;
964 vm
->fault_attr
.interval
= 1;
965 atomic_set(&vm
->fault_attr
.times
, -1);
967 vma
= i915_vma_instance(explode
, vm
, NULL
);
973 err
= i915_vma_pin(vma
, 0, 0, flags
| size
);
979 i915_gem_object_put(purge
);
980 i915_gem_object_put(explode
);
982 memset(&vm
->fault_attr
, 0, sizeof(vm
->fault_attr
));
983 cleanup_freed_objects(vm
->i915
);
989 i915_gem_object_put(explode
);
991 i915_gem_object_put(purge
);
992 memset(&vm
->fault_attr
, 0, sizeof(vm
->fault_attr
));
996 static int exercise_ppgtt(struct drm_i915_private
*dev_priv
,
997 int (*func
)(struct i915_address_space
*vm
,
998 u64 hole_start
, u64 hole_end
,
999 unsigned long end_time
))
1001 struct i915_ppgtt
*ppgtt
;
1002 IGT_TIMEOUT(end_time
);
1006 if (!HAS_FULL_PPGTT(dev_priv
))
1009 file
= mock_file(dev_priv
);
1011 return PTR_ERR(file
);
1013 ppgtt
= i915_ppgtt_create(&dev_priv
->gt
);
1014 if (IS_ERR(ppgtt
)) {
1015 err
= PTR_ERR(ppgtt
);
1018 GEM_BUG_ON(offset_in_page(ppgtt
->vm
.total
));
1019 GEM_BUG_ON(!atomic_read(&ppgtt
->vm
.open
));
1021 err
= func(&ppgtt
->vm
, 0, ppgtt
->vm
.total
, end_time
);
1023 i915_vm_put(&ppgtt
->vm
);
1030 static int igt_ppgtt_fill(void *arg
)
1032 return exercise_ppgtt(arg
, fill_hole
);
1035 static int igt_ppgtt_walk(void *arg
)
1037 return exercise_ppgtt(arg
, walk_hole
);
1040 static int igt_ppgtt_pot(void *arg
)
1042 return exercise_ppgtt(arg
, pot_hole
);
1045 static int igt_ppgtt_drunk(void *arg
)
1047 return exercise_ppgtt(arg
, drunk_hole
);
1050 static int igt_ppgtt_lowlevel(void *arg
)
1052 return exercise_ppgtt(arg
, lowlevel_hole
);
1055 static int igt_ppgtt_shrink(void *arg
)
1057 return exercise_ppgtt(arg
, shrink_hole
);
1060 static int igt_ppgtt_shrink_boom(void *arg
)
1062 return exercise_ppgtt(arg
, shrink_boom
);
1065 static int sort_holes(void *priv
, struct list_head
*A
, struct list_head
*B
)
1067 struct drm_mm_node
*a
= list_entry(A
, typeof(*a
), hole_stack
);
1068 struct drm_mm_node
*b
= list_entry(B
, typeof(*b
), hole_stack
);
1070 if (a
->start
< b
->start
)
1076 static int exercise_ggtt(struct drm_i915_private
*i915
,
1077 int (*func
)(struct i915_address_space
*vm
,
1078 u64 hole_start
, u64 hole_end
,
1079 unsigned long end_time
))
1081 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
1082 u64 hole_start
, hole_end
, last
= 0;
1083 struct drm_mm_node
*node
;
1084 IGT_TIMEOUT(end_time
);
1088 list_sort(NULL
, &ggtt
->vm
.mm
.hole_stack
, sort_holes
);
1089 drm_mm_for_each_hole(node
, &ggtt
->vm
.mm
, hole_start
, hole_end
) {
1090 if (hole_start
< last
)
1093 if (ggtt
->vm
.mm
.color_adjust
)
1094 ggtt
->vm
.mm
.color_adjust(node
, 0,
1095 &hole_start
, &hole_end
);
1096 if (hole_start
>= hole_end
)
1099 err
= func(&ggtt
->vm
, hole_start
, hole_end
, end_time
);
1103 /* As we have manipulated the drm_mm, the list may be corrupt */
1111 static int igt_ggtt_fill(void *arg
)
1113 return exercise_ggtt(arg
, fill_hole
);
1116 static int igt_ggtt_walk(void *arg
)
1118 return exercise_ggtt(arg
, walk_hole
);
1121 static int igt_ggtt_pot(void *arg
)
1123 return exercise_ggtt(arg
, pot_hole
);
1126 static int igt_ggtt_drunk(void *arg
)
1128 return exercise_ggtt(arg
, drunk_hole
);
1131 static int igt_ggtt_lowlevel(void *arg
)
1133 return exercise_ggtt(arg
, lowlevel_hole
);
1136 static int igt_ggtt_page(void *arg
)
1138 const unsigned int count
= PAGE_SIZE
/sizeof(u32
);
1139 I915_RND_STATE(prng
);
1140 struct drm_i915_private
*i915
= arg
;
1141 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
1142 struct drm_i915_gem_object
*obj
;
1143 intel_wakeref_t wakeref
;
1144 struct drm_mm_node tmp
;
1145 unsigned int *order
, n
;
1148 if (!i915_ggtt_has_aperture(ggtt
))
1151 obj
= i915_gem_object_create_internal(i915
, PAGE_SIZE
);
1153 return PTR_ERR(obj
);
1155 err
= i915_gem_object_pin_pages(obj
);
1159 memset(&tmp
, 0, sizeof(tmp
));
1160 mutex_lock(&ggtt
->vm
.mutex
);
1161 err
= drm_mm_insert_node_in_range(&ggtt
->vm
.mm
, &tmp
,
1162 count
* PAGE_SIZE
, 0,
1163 I915_COLOR_UNEVICTABLE
,
1164 0, ggtt
->mappable_end
,
1166 mutex_unlock(&ggtt
->vm
.mutex
);
1170 wakeref
= intel_runtime_pm_get(&i915
->runtime_pm
);
1172 for (n
= 0; n
< count
; n
++) {
1173 u64 offset
= tmp
.start
+ n
* PAGE_SIZE
;
1175 ggtt
->vm
.insert_page(&ggtt
->vm
,
1176 i915_gem_object_get_dma_address(obj
, 0),
1177 offset
, I915_CACHE_NONE
, 0);
1180 order
= i915_random_order(count
, &prng
);
1186 for (n
= 0; n
< count
; n
++) {
1187 u64 offset
= tmp
.start
+ order
[n
] * PAGE_SIZE
;
1190 vaddr
= io_mapping_map_atomic_wc(&ggtt
->iomap
, offset
);
1191 iowrite32(n
, vaddr
+ n
);
1192 io_mapping_unmap_atomic(vaddr
);
1194 intel_gt_flush_ggtt_writes(ggtt
->vm
.gt
);
1196 i915_random_reorder(order
, count
, &prng
);
1197 for (n
= 0; n
< count
; n
++) {
1198 u64 offset
= tmp
.start
+ order
[n
] * PAGE_SIZE
;
1202 vaddr
= io_mapping_map_atomic_wc(&ggtt
->iomap
, offset
);
1203 val
= ioread32(vaddr
+ n
);
1204 io_mapping_unmap_atomic(vaddr
);
1207 pr_err("insert page failed: found %d, expected %d\n",
1216 ggtt
->vm
.clear_range(&ggtt
->vm
, tmp
.start
, tmp
.size
);
1217 intel_runtime_pm_put(&i915
->runtime_pm
, wakeref
);
1218 mutex_lock(&ggtt
->vm
.mutex
);
1219 drm_mm_remove_node(&tmp
);
1220 mutex_unlock(&ggtt
->vm
.mutex
);
1222 i915_gem_object_unpin_pages(obj
);
1224 i915_gem_object_put(obj
);
1228 static void track_vma_bind(struct i915_vma
*vma
)
1230 struct drm_i915_gem_object
*obj
= vma
->obj
;
1232 atomic_inc(&obj
->bind_count
); /* track for eviction later */
1233 __i915_gem_object_pin_pages(obj
);
1235 GEM_BUG_ON(vma
->pages
);
1236 atomic_set(&vma
->pages_count
, I915_VMA_PAGES_ACTIVE
);
1237 __i915_gem_object_pin_pages(obj
);
1238 vma
->pages
= obj
->mm
.pages
;
1240 mutex_lock(&vma
->vm
->mutex
);
1241 list_add_tail(&vma
->vm_link
, &vma
->vm
->bound_list
);
1242 mutex_unlock(&vma
->vm
->mutex
);
1245 static int exercise_mock(struct drm_i915_private
*i915
,
1246 int (*func
)(struct i915_address_space
*vm
,
1247 u64 hole_start
, u64 hole_end
,
1248 unsigned long end_time
))
1250 const u64 limit
= totalram_pages() << PAGE_SHIFT
;
1251 struct i915_address_space
*vm
;
1252 struct i915_gem_context
*ctx
;
1253 IGT_TIMEOUT(end_time
);
1256 ctx
= mock_context(i915
, "mock");
1260 vm
= i915_gem_context_get_vm_rcu(ctx
);
1261 err
= func(vm
, 0, min(vm
->total
, limit
), end_time
);
1264 mock_context_close(ctx
);
1268 static int igt_mock_fill(void *arg
)
1270 struct i915_ggtt
*ggtt
= arg
;
1272 return exercise_mock(ggtt
->vm
.i915
, fill_hole
);
1275 static int igt_mock_walk(void *arg
)
1277 struct i915_ggtt
*ggtt
= arg
;
1279 return exercise_mock(ggtt
->vm
.i915
, walk_hole
);
1282 static int igt_mock_pot(void *arg
)
1284 struct i915_ggtt
*ggtt
= arg
;
1286 return exercise_mock(ggtt
->vm
.i915
, pot_hole
);
1289 static int igt_mock_drunk(void *arg
)
1291 struct i915_ggtt
*ggtt
= arg
;
1293 return exercise_mock(ggtt
->vm
.i915
, drunk_hole
);
1296 static int igt_gtt_reserve(void *arg
)
1298 struct i915_ggtt
*ggtt
= arg
;
1299 struct drm_i915_gem_object
*obj
, *on
;
1300 I915_RND_STATE(prng
);
1305 /* i915_gem_gtt_reserve() tries to reserve the precise range
1306 * for the node, and evicts if it has to. So our test checks that
1307 * it can give us the requsted space and prevent overlaps.
1310 /* Start by filling the GGTT */
1312 total
+ 2 * I915_GTT_PAGE_SIZE
<= ggtt
->vm
.total
;
1313 total
+= 2 * I915_GTT_PAGE_SIZE
) {
1314 struct i915_vma
*vma
;
1316 obj
= i915_gem_object_create_internal(ggtt
->vm
.i915
,
1323 err
= i915_gem_object_pin_pages(obj
);
1325 i915_gem_object_put(obj
);
1329 list_add(&obj
->st_link
, &objects
);
1331 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1337 mutex_lock(&ggtt
->vm
.mutex
);
1338 err
= i915_gem_gtt_reserve(&ggtt
->vm
, &vma
->node
,
1343 mutex_unlock(&ggtt
->vm
.mutex
);
1345 pr_err("i915_gem_gtt_reserve (pass 1) failed at %llu/%llu with err=%d\n",
1346 total
, ggtt
->vm
.total
, err
);
1349 track_vma_bind(vma
);
1351 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1352 if (vma
->node
.start
!= total
||
1353 vma
->node
.size
!= 2*I915_GTT_PAGE_SIZE
) {
1354 pr_err("i915_gem_gtt_reserve (pass 1) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1355 vma
->node
.start
, vma
->node
.size
,
1356 total
, 2*I915_GTT_PAGE_SIZE
);
1362 /* Now we start forcing evictions */
1363 for (total
= I915_GTT_PAGE_SIZE
;
1364 total
+ 2 * I915_GTT_PAGE_SIZE
<= ggtt
->vm
.total
;
1365 total
+= 2 * I915_GTT_PAGE_SIZE
) {
1366 struct i915_vma
*vma
;
1368 obj
= i915_gem_object_create_internal(ggtt
->vm
.i915
,
1375 err
= i915_gem_object_pin_pages(obj
);
1377 i915_gem_object_put(obj
);
1381 list_add(&obj
->st_link
, &objects
);
1383 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1389 mutex_lock(&ggtt
->vm
.mutex
);
1390 err
= i915_gem_gtt_reserve(&ggtt
->vm
, &vma
->node
,
1395 mutex_unlock(&ggtt
->vm
.mutex
);
1397 pr_err("i915_gem_gtt_reserve (pass 2) failed at %llu/%llu with err=%d\n",
1398 total
, ggtt
->vm
.total
, err
);
1401 track_vma_bind(vma
);
1403 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1404 if (vma
->node
.start
!= total
||
1405 vma
->node
.size
!= 2*I915_GTT_PAGE_SIZE
) {
1406 pr_err("i915_gem_gtt_reserve (pass 2) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1407 vma
->node
.start
, vma
->node
.size
,
1408 total
, 2*I915_GTT_PAGE_SIZE
);
1414 /* And then try at random */
1415 list_for_each_entry_safe(obj
, on
, &objects
, st_link
) {
1416 struct i915_vma
*vma
;
1419 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1425 err
= i915_vma_unbind(vma
);
1427 pr_err("i915_vma_unbind failed with err=%d!\n", err
);
1431 offset
= igt_random_offset(&prng
,
1433 2 * I915_GTT_PAGE_SIZE
,
1434 I915_GTT_MIN_ALIGNMENT
);
1436 mutex_lock(&ggtt
->vm
.mutex
);
1437 err
= i915_gem_gtt_reserve(&ggtt
->vm
, &vma
->node
,
1442 mutex_unlock(&ggtt
->vm
.mutex
);
1444 pr_err("i915_gem_gtt_reserve (pass 3) failed at %llu/%llu with err=%d\n",
1445 total
, ggtt
->vm
.total
, err
);
1448 track_vma_bind(vma
);
1450 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1451 if (vma
->node
.start
!= offset
||
1452 vma
->node
.size
!= 2*I915_GTT_PAGE_SIZE
) {
1453 pr_err("i915_gem_gtt_reserve (pass 3) placement failed, found (%llx + %llx), expected (%llx + %llx)\n",
1454 vma
->node
.start
, vma
->node
.size
,
1455 offset
, 2*I915_GTT_PAGE_SIZE
);
1462 list_for_each_entry_safe(obj
, on
, &objects
, st_link
) {
1463 i915_gem_object_unpin_pages(obj
);
1464 i915_gem_object_put(obj
);
1469 static int igt_gtt_insert(void *arg
)
1471 struct i915_ggtt
*ggtt
= arg
;
1472 struct drm_i915_gem_object
*obj
, *on
;
1473 struct drm_mm_node tmp
= {};
1474 const struct invalid_insert
{
1478 } invalid_insert
[] = {
1480 ggtt
->vm
.total
+ I915_GTT_PAGE_SIZE
, 0,
1484 2*I915_GTT_PAGE_SIZE
, 0,
1485 0, I915_GTT_PAGE_SIZE
,
1488 -(u64
)I915_GTT_PAGE_SIZE
, 0,
1489 0, 4*I915_GTT_PAGE_SIZE
,
1492 -(u64
)2*I915_GTT_PAGE_SIZE
, 2*I915_GTT_PAGE_SIZE
,
1493 0, 4*I915_GTT_PAGE_SIZE
,
1496 I915_GTT_PAGE_SIZE
, I915_GTT_MIN_ALIGNMENT
<< 1,
1497 I915_GTT_MIN_ALIGNMENT
, I915_GTT_MIN_ALIGNMENT
<< 1,
1505 /* i915_gem_gtt_insert() tries to allocate some free space in the GTT
1506 * to the node, evicting if required.
1509 /* Check a couple of obviously invalid requests */
1510 for (ii
= invalid_insert
; ii
->size
; ii
++) {
1511 mutex_lock(&ggtt
->vm
.mutex
);
1512 err
= i915_gem_gtt_insert(&ggtt
->vm
, &tmp
,
1513 ii
->size
, ii
->alignment
,
1514 I915_COLOR_UNEVICTABLE
,
1517 mutex_unlock(&ggtt
->vm
.mutex
);
1518 if (err
!= -ENOSPC
) {
1519 pr_err("Invalid i915_gem_gtt_insert(.size=%llx, .alignment=%llx, .start=%llx, .end=%llx) succeeded (err=%d)\n",
1520 ii
->size
, ii
->alignment
, ii
->start
, ii
->end
,
1526 /* Start by filling the GGTT */
1528 total
+ I915_GTT_PAGE_SIZE
<= ggtt
->vm
.total
;
1529 total
+= I915_GTT_PAGE_SIZE
) {
1530 struct i915_vma
*vma
;
1532 obj
= i915_gem_object_create_internal(ggtt
->vm
.i915
,
1533 I915_GTT_PAGE_SIZE
);
1539 err
= i915_gem_object_pin_pages(obj
);
1541 i915_gem_object_put(obj
);
1545 list_add(&obj
->st_link
, &objects
);
1547 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1553 mutex_lock(&ggtt
->vm
.mutex
);
1554 err
= i915_gem_gtt_insert(&ggtt
->vm
, &vma
->node
,
1555 obj
->base
.size
, 0, obj
->cache_level
,
1558 mutex_unlock(&ggtt
->vm
.mutex
);
1559 if (err
== -ENOSPC
) {
1560 /* maxed out the GGTT space */
1561 i915_gem_object_put(obj
);
1565 pr_err("i915_gem_gtt_insert (pass 1) failed at %llu/%llu with err=%d\n",
1566 total
, ggtt
->vm
.total
, err
);
1569 track_vma_bind(vma
);
1570 __i915_vma_pin(vma
);
1572 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1575 list_for_each_entry(obj
, &objects
, st_link
) {
1576 struct i915_vma
*vma
;
1578 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1584 if (!drm_mm_node_allocated(&vma
->node
)) {
1585 pr_err("VMA was unexpectedly evicted!\n");
1590 __i915_vma_unpin(vma
);
1593 /* If we then reinsert, we should find the same hole */
1594 list_for_each_entry_safe(obj
, on
, &objects
, st_link
) {
1595 struct i915_vma
*vma
;
1598 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1604 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1605 offset
= vma
->node
.start
;
1607 err
= i915_vma_unbind(vma
);
1609 pr_err("i915_vma_unbind failed with err=%d!\n", err
);
1613 mutex_lock(&ggtt
->vm
.mutex
);
1614 err
= i915_gem_gtt_insert(&ggtt
->vm
, &vma
->node
,
1615 obj
->base
.size
, 0, obj
->cache_level
,
1618 mutex_unlock(&ggtt
->vm
.mutex
);
1620 pr_err("i915_gem_gtt_insert (pass 2) failed at %llu/%llu with err=%d\n",
1621 total
, ggtt
->vm
.total
, err
);
1624 track_vma_bind(vma
);
1626 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1627 if (vma
->node
.start
!= offset
) {
1628 pr_err("i915_gem_gtt_insert did not return node to its previous location (the only hole), expected address %llx, found %llx\n",
1629 offset
, vma
->node
.start
);
1635 /* And then force evictions */
1637 total
+ 2 * I915_GTT_PAGE_SIZE
<= ggtt
->vm
.total
;
1638 total
+= 2 * I915_GTT_PAGE_SIZE
) {
1639 struct i915_vma
*vma
;
1641 obj
= i915_gem_object_create_internal(ggtt
->vm
.i915
,
1642 2 * I915_GTT_PAGE_SIZE
);
1648 err
= i915_gem_object_pin_pages(obj
);
1650 i915_gem_object_put(obj
);
1654 list_add(&obj
->st_link
, &objects
);
1656 vma
= i915_vma_instance(obj
, &ggtt
->vm
, NULL
);
1662 mutex_lock(&ggtt
->vm
.mutex
);
1663 err
= i915_gem_gtt_insert(&ggtt
->vm
, &vma
->node
,
1664 obj
->base
.size
, 0, obj
->cache_level
,
1667 mutex_unlock(&ggtt
->vm
.mutex
);
1669 pr_err("i915_gem_gtt_insert (pass 3) failed at %llu/%llu with err=%d\n",
1670 total
, ggtt
->vm
.total
, err
);
1673 track_vma_bind(vma
);
1675 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
1679 list_for_each_entry_safe(obj
, on
, &objects
, st_link
) {
1680 i915_gem_object_unpin_pages(obj
);
1681 i915_gem_object_put(obj
);
1686 int i915_gem_gtt_mock_selftests(void)
1688 static const struct i915_subtest tests
[] = {
1689 SUBTEST(igt_mock_drunk
),
1690 SUBTEST(igt_mock_walk
),
1691 SUBTEST(igt_mock_pot
),
1692 SUBTEST(igt_mock_fill
),
1693 SUBTEST(igt_gtt_reserve
),
1694 SUBTEST(igt_gtt_insert
),
1696 struct drm_i915_private
*i915
;
1697 struct i915_ggtt
*ggtt
;
1700 i915
= mock_gem_device();
1704 ggtt
= kmalloc(sizeof(*ggtt
), GFP_KERNEL
);
1709 mock_init_ggtt(i915
, ggtt
);
1711 err
= i915_subtests(tests
, ggtt
);
1713 mock_device_flush(i915
);
1714 i915_gem_drain_freed_objects(i915
);
1715 mock_fini_ggtt(ggtt
);
1718 drm_dev_put(&i915
->drm
);
1722 static int context_sync(struct intel_context
*ce
)
1724 struct i915_request
*rq
;
1727 rq
= intel_context_create_request(ce
);
1731 i915_request_get(rq
);
1732 i915_request_add(rq
);
1734 timeout
= i915_request_wait(rq
, 0, HZ
/ 5);
1735 i915_request_put(rq
);
1737 return timeout
< 0 ? -EIO
: 0;
1740 static struct i915_request
*
1741 submit_batch(struct intel_context
*ce
, u64 addr
)
1743 struct i915_request
*rq
;
1746 rq
= intel_context_create_request(ce
);
1751 if (rq
->engine
->emit_init_breadcrumb
) /* detect a hang */
1752 err
= rq
->engine
->emit_init_breadcrumb(rq
);
1754 err
= rq
->engine
->emit_bb_start(rq
, addr
, 0, 0);
1757 i915_request_get(rq
);
1758 i915_request_add(rq
);
1760 return err
? ERR_PTR(err
) : rq
;
1763 static u32
*spinner(u32
*batch
, int i
)
1765 return batch
+ i
* 64 / sizeof(*batch
) + 4;
1768 static void end_spin(u32
*batch
, int i
)
1770 *spinner(batch
, i
) = MI_BATCH_BUFFER_END
;
1774 static int igt_cs_tlb(void *arg
)
1776 const unsigned int count
= PAGE_SIZE
/ 64;
1777 const unsigned int chunk_size
= count
* PAGE_SIZE
;
1778 struct drm_i915_private
*i915
= arg
;
1779 struct drm_i915_gem_object
*bbe
, *act
, *out
;
1780 struct i915_gem_engines_iter it
;
1781 struct i915_address_space
*vm
;
1782 struct i915_gem_context
*ctx
;
1783 struct intel_context
*ce
;
1784 struct i915_vma
*vma
;
1785 I915_RND_STATE(prng
);
1793 * Our mission here is to fool the hardware to execute something
1794 * from scratch as it has not seen the batch move (due to missing
1795 * the TLB invalidate).
1798 file
= mock_file(i915
);
1800 return PTR_ERR(file
);
1802 ctx
= live_context(i915
, file
);
1808 vm
= i915_gem_context_get_vm_rcu(ctx
);
1809 if (i915_is_ggtt(vm
))
1812 /* Create two pages; dummy we prefill the TLB, and intended */
1813 bbe
= i915_gem_object_create_internal(i915
, PAGE_SIZE
);
1819 batch
= i915_gem_object_pin_map(bbe
, I915_MAP_WC
);
1820 if (IS_ERR(batch
)) {
1821 err
= PTR_ERR(batch
);
1824 memset32(batch
, MI_BATCH_BUFFER_END
, PAGE_SIZE
/ sizeof(u32
));
1825 i915_gem_object_flush_map(bbe
);
1826 i915_gem_object_unpin_map(bbe
);
1828 act
= i915_gem_object_create_internal(i915
, PAGE_SIZE
);
1834 /* Track the execution of each request by writing into different slot */
1835 batch
= i915_gem_object_pin_map(act
, I915_MAP_WC
);
1836 if (IS_ERR(batch
)) {
1837 err
= PTR_ERR(batch
);
1840 for (i
= 0; i
< count
; i
++) {
1841 u32
*cs
= batch
+ i
* 64 / sizeof(*cs
);
1842 u64 addr
= (vm
->total
- PAGE_SIZE
) + i
* sizeof(u32
);
1844 GEM_BUG_ON(INTEL_GEN(i915
) < 6);
1845 cs
[0] = MI_STORE_DWORD_IMM_GEN4
;
1846 if (INTEL_GEN(i915
) >= 8) {
1847 cs
[1] = lower_32_bits(addr
);
1848 cs
[2] = upper_32_bits(addr
);
1851 cs
[5] = MI_BATCH_BUFFER_START_GEN8
;
1854 cs
[2] = lower_32_bits(addr
);
1857 cs
[5] = MI_BATCH_BUFFER_START
;
1861 out
= i915_gem_object_create_internal(i915
, PAGE_SIZE
);
1866 i915_gem_object_set_cache_coherency(out
, I915_CACHING_CACHED
);
1868 vma
= i915_vma_instance(out
, vm
, NULL
);
1874 err
= i915_vma_pin(vma
, 0, 0,
1877 (vm
->total
- PAGE_SIZE
));
1880 GEM_BUG_ON(vma
->node
.start
!= vm
->total
- PAGE_SIZE
);
1882 result
= i915_gem_object_pin_map(out
, I915_MAP_WB
);
1883 if (IS_ERR(result
)) {
1884 err
= PTR_ERR(result
);
1888 for_each_gem_engine(ce
, i915_gem_context_lock_engines(ctx
), it
) {
1889 IGT_TIMEOUT(end_time
);
1890 unsigned long pass
= 0;
1892 if (!intel_engine_can_store_dword(ce
->engine
))
1895 while (!__igt_timeout(end_time
, NULL
)) {
1896 struct i915_request
*rq
;
1899 offset
= igt_random_offset(&prng
,
1900 0, vm
->total
- PAGE_SIZE
,
1901 chunk_size
, PAGE_SIZE
);
1903 err
= vm
->allocate_va_range(vm
, offset
, chunk_size
);
1907 memset32(result
, STACK_MAGIC
, PAGE_SIZE
/ sizeof(u32
));
1909 vma
= i915_vma_instance(bbe
, vm
, NULL
);
1915 err
= vma
->ops
->set_pages(vma
);
1919 /* Prime the TLB with the dummy pages */
1920 for (i
= 0; i
< count
; i
++) {
1921 vma
->node
.start
= offset
+ i
* PAGE_SIZE
;
1922 vm
->insert_entries(vm
, vma
, I915_CACHE_NONE
, 0);
1924 rq
= submit_batch(ce
, vma
->node
.start
);
1929 i915_request_put(rq
);
1932 vma
->ops
->clear_pages(vma
);
1934 err
= context_sync(ce
);
1936 pr_err("%s: dummy setup timed out\n",
1941 vma
= i915_vma_instance(act
, vm
, NULL
);
1947 err
= vma
->ops
->set_pages(vma
);
1951 /* Replace the TLB with target batches */
1952 for (i
= 0; i
< count
; i
++) {
1953 struct i915_request
*rq
;
1954 u32
*cs
= batch
+ i
* 64 / sizeof(*cs
);
1957 vma
->node
.start
= offset
+ i
* PAGE_SIZE
;
1958 vm
->insert_entries(vm
, vma
, I915_CACHE_NONE
, 0);
1960 addr
= vma
->node
.start
+ i
* 64;
1962 cs
[6] = lower_32_bits(addr
);
1963 cs
[7] = upper_32_bits(addr
);
1966 rq
= submit_batch(ce
, addr
);
1972 /* Wait until the context chain has started */
1974 while (READ_ONCE(result
[i
]) &&
1975 !i915_request_completed(rq
))
1978 end_spin(batch
, i
- 1);
1981 i915_request_put(rq
);
1983 end_spin(batch
, count
- 1);
1985 vma
->ops
->clear_pages(vma
);
1987 err
= context_sync(ce
);
1989 pr_err("%s: writes timed out\n",
1994 for (i
= 0; i
< count
; i
++) {
1995 if (result
[i
] != i
) {
1996 pr_err("%s: Write lost on pass %lu, at offset %llx, index %d, found %x, expected %x\n",
1997 ce
->engine
->name
, pass
,
1998 offset
, i
, result
[i
], i
);
2004 vm
->clear_range(vm
, offset
, chunk_size
);
2009 if (igt_flush_test(i915
))
2011 i915_gem_context_unlock_engines(ctx
);
2012 i915_gem_object_unpin_map(out
);
2014 i915_gem_object_put(out
);
2016 i915_gem_object_unpin_map(act
);
2018 i915_gem_object_put(act
);
2020 i915_gem_object_put(bbe
);
2028 int i915_gem_gtt_live_selftests(struct drm_i915_private
*i915
)
2030 static const struct i915_subtest tests
[] = {
2031 SUBTEST(igt_ppgtt_alloc
),
2032 SUBTEST(igt_ppgtt_lowlevel
),
2033 SUBTEST(igt_ppgtt_drunk
),
2034 SUBTEST(igt_ppgtt_walk
),
2035 SUBTEST(igt_ppgtt_pot
),
2036 SUBTEST(igt_ppgtt_fill
),
2037 SUBTEST(igt_ppgtt_shrink
),
2038 SUBTEST(igt_ppgtt_shrink_boom
),
2039 SUBTEST(igt_ggtt_lowlevel
),
2040 SUBTEST(igt_ggtt_drunk
),
2041 SUBTEST(igt_ggtt_walk
),
2042 SUBTEST(igt_ggtt_pot
),
2043 SUBTEST(igt_ggtt_fill
),
2044 SUBTEST(igt_ggtt_page
),
2045 SUBTEST(igt_cs_tlb
),
2048 GEM_BUG_ON(offset_in_page(i915
->ggtt
.vm
.total
));
2050 return i915_subtests(tests
, i915
);