1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2020 Intel Corporation
6 static struct intel_ring
*mock_ring(unsigned long sz
)
8 struct intel_ring
*ring
;
10 ring
= kzalloc(sizeof(*ring
) + sz
, GFP_KERNEL
);
14 kref_init(&ring
->ref
);
16 ring
->wrap
= BITS_PER_TYPE(ring
->size
) - ilog2(sz
);
17 ring
->effective_size
= sz
;
18 ring
->vaddr
= (void *)(ring
+ 1);
19 atomic_set(&ring
->pin_count
, 1);
21 intel_ring_update_space(ring
);
26 static void mock_ring_free(struct intel_ring
*ring
)
31 static int check_ring_direction(struct intel_ring
*ring
,
37 result
= intel_ring_direction(ring
, next
, prev
);
43 if (result
!= expected
) {
44 pr_err("intel_ring_direction(%u, %u):%d != %d\n",
45 next
, prev
, result
, expected
);
52 static int check_ring_step(struct intel_ring
*ring
, u32 x
, u32 step
)
54 u32 prev
= x
, next
= intel_ring_wrap(ring
, x
+ step
);
57 err
|= check_ring_direction(ring
, next
, next
, 0);
58 err
|= check_ring_direction(ring
, prev
, prev
, 0);
59 err
|= check_ring_direction(ring
, next
, prev
, 1);
60 err
|= check_ring_direction(ring
, prev
, next
, -1);
65 static int check_ring_offset(struct intel_ring
*ring
, u32 x
, u32 step
)
69 err
|= check_ring_step(ring
, x
, step
);
70 err
|= check_ring_step(ring
, intel_ring_wrap(ring
, x
+ 1), step
);
71 err
|= check_ring_step(ring
, intel_ring_wrap(ring
, x
- 1), step
);
76 static int igt_ring_direction(void *dummy
)
78 struct intel_ring
*ring
;
79 unsigned int half
= 2048;
82 ring
= mock_ring(2 * half
);
86 GEM_BUG_ON(ring
->size
!= 2 * half
);
88 /* Precision of wrap detection is limited to ring->size / 2 */
89 for (step
= 1; step
< half
; step
<<= 1) {
90 err
|= check_ring_offset(ring
, 0, step
);
91 err
|= check_ring_offset(ring
, half
, step
);
93 err
|= check_ring_step(ring
, 0, half
- 64);
95 /* And check unwrapped handling for good measure */
96 err
|= check_ring_offset(ring
, 0, 2 * half
+ 64);
97 err
|= check_ring_offset(ring
, 3 * half
, 1);
103 int intel_ring_mock_selftests(void)
105 static const struct i915_subtest tests
[] = {
106 SUBTEST(igt_ring_direction
),
109 return i915_subtests(tests
, NULL
);