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 "../i915_selftest.h"
26 #include "i915_random.h"
28 #include "mock_gem_device.h"
29 #include "mock_engine.h"
31 static int check_rbtree(struct intel_engine_cs
*engine
,
32 const unsigned long *bitmap
,
33 const struct intel_wait
*waiters
,
36 struct intel_breadcrumbs
*b
= &engine
->breadcrumbs
;
40 if (&b
->irq_wait
->node
!= rb_first(&b
->waiters
)) {
41 pr_err("First waiter does not match first element of wait-tree\n");
45 n
= find_first_bit(bitmap
, count
);
46 for (rb
= rb_first(&b
->waiters
); rb
; rb
= rb_next(rb
)) {
47 struct intel_wait
*w
= container_of(rb
, typeof(*w
), node
);
48 int idx
= w
- waiters
;
50 if (!test_bit(idx
, bitmap
)) {
51 pr_err("waiter[%d, seqno=%d] removed but still in wait-tree\n",
57 pr_err("waiter[%d, seqno=%d] does not match expected next element in tree [%d]\n",
62 n
= find_next_bit(bitmap
, count
, n
+ 1);
68 static int check_completion(struct intel_engine_cs
*engine
,
69 const unsigned long *bitmap
,
70 const struct intel_wait
*waiters
,
75 for (n
= 0; n
< count
; n
++) {
76 if (intel_wait_complete(&waiters
[n
]) != !!test_bit(n
, bitmap
))
79 pr_err("waiter[%d, seqno=%d] is %s, but expected %s\n",
81 intel_wait_complete(&waiters
[n
]) ? "complete" : "active",
82 test_bit(n
, bitmap
) ? "active" : "complete");
89 static int check_rbtree_empty(struct intel_engine_cs
*engine
)
91 struct intel_breadcrumbs
*b
= &engine
->breadcrumbs
;
94 pr_err("Empty breadcrumbs still has a waiter\n");
98 if (!RB_EMPTY_ROOT(&b
->waiters
)) {
99 pr_err("Empty breadcrumbs, but wait-tree not empty\n");
106 static int igt_random_insert_remove(void *arg
)
108 const u32 seqno_bias
= 0x1000;
109 I915_RND_STATE(prng
);
110 struct intel_engine_cs
*engine
= arg
;
111 struct intel_wait
*waiters
;
112 const int count
= 4096;
114 unsigned long *bitmap
;
118 mock_engine_reset(engine
);
120 waiters
= kvmalloc_array(count
, sizeof(*waiters
), GFP_KERNEL
);
124 bitmap
= kcalloc(DIV_ROUND_UP(count
, BITS_PER_LONG
), sizeof(*bitmap
),
129 order
= i915_random_order(count
, &prng
);
133 for (n
= 0; n
< count
; n
++)
134 intel_wait_init_for_seqno(&waiters
[n
], seqno_bias
+ n
);
136 err
= check_rbtree(engine
, bitmap
, waiters
, count
);
140 /* Add and remove waiters into the rbtree in random order. At each
141 * step, we verify that the rbtree is correctly ordered.
143 for (n
= 0; n
< count
; n
++) {
146 intel_engine_add_wait(engine
, &waiters
[i
]);
147 __set_bit(i
, bitmap
);
149 err
= check_rbtree(engine
, bitmap
, waiters
, count
);
154 i915_random_reorder(order
, count
, &prng
);
155 for (n
= 0; n
< count
; n
++) {
158 intel_engine_remove_wait(engine
, &waiters
[i
]);
159 __clear_bit(i
, bitmap
);
161 err
= check_rbtree(engine
, bitmap
, waiters
, count
);
166 err
= check_rbtree_empty(engine
);
174 mock_engine_flush(engine
);
178 static int igt_insert_complete(void *arg
)
180 const u32 seqno_bias
= 0x1000;
181 struct intel_engine_cs
*engine
= arg
;
182 struct intel_wait
*waiters
;
183 const int count
= 4096;
184 unsigned long *bitmap
;
188 mock_engine_reset(engine
);
190 waiters
= kvmalloc_array(count
, sizeof(*waiters
), GFP_KERNEL
);
194 bitmap
= kcalloc(DIV_ROUND_UP(count
, BITS_PER_LONG
), sizeof(*bitmap
),
199 for (n
= 0; n
< count
; n
++) {
200 intel_wait_init_for_seqno(&waiters
[n
], n
+ seqno_bias
);
201 intel_engine_add_wait(engine
, &waiters
[n
]);
202 __set_bit(n
, bitmap
);
204 err
= check_rbtree(engine
, bitmap
, waiters
, count
);
208 /* On each step, we advance the seqno so that several waiters are then
209 * complete (we increase the seqno by increasingly larger values to
210 * retire more and more waiters at once). All retired waiters should
211 * be woken and removed from the rbtree, and so that we check.
213 for (n
= 0; n
< count
; n
= m
) {
216 GEM_BUG_ON(find_first_bit(bitmap
, count
) != n
);
218 if (intel_wait_complete(&waiters
[n
])) {
219 pr_err("waiter[%d, seqno=%d] completed too early\n",
220 n
, waiters
[n
].seqno
);
225 /* complete the following waiters */
226 mock_seqno_advance(engine
, seqno
+ seqno_bias
);
227 for (m
= n
; m
<= seqno
; m
++) {
231 GEM_BUG_ON(!test_bit(m
, bitmap
));
232 __clear_bit(m
, bitmap
);
235 intel_engine_remove_wait(engine
, &waiters
[n
]);
236 RB_CLEAR_NODE(&waiters
[n
].node
);
238 err
= check_rbtree(engine
, bitmap
, waiters
, count
);
240 pr_err("rbtree corrupt after seqno advance to %d\n",
245 err
= check_completion(engine
, bitmap
, waiters
, count
);
247 pr_err("completions after seqno advance to %d failed\n",
253 err
= check_rbtree_empty(engine
);
259 mock_engine_flush(engine
);
264 struct task_struct
*tsk
;
265 atomic_t
*ready
, *set
, *done
;
266 struct intel_engine_cs
*engine
;
270 wait_queue_head_t
*wq
;
274 static int wait_atomic_timeout(atomic_t
*p
, unsigned int mode
)
276 return schedule_timeout(10 * HZ
) ? 0 : -ETIMEDOUT
;
279 static bool wait_for_ready(struct igt_wakeup
*w
)
283 set_bit(IDLE
, &w
->flags
);
284 if (atomic_dec_and_test(w
->done
))
285 wake_up_atomic_t(w
->done
);
287 if (test_bit(STOP
, &w
->flags
))
291 prepare_to_wait(w
->wq
, &ready
, TASK_INTERRUPTIBLE
);
292 if (atomic_read(w
->ready
) == 0)
297 finish_wait(w
->wq
, &ready
);
300 clear_bit(IDLE
, &w
->flags
);
301 if (atomic_dec_and_test(w
->set
))
302 wake_up_atomic_t(w
->set
);
304 return !test_bit(STOP
, &w
->flags
);
307 static int igt_wakeup_thread(void *arg
)
309 struct igt_wakeup
*w
= arg
;
310 struct intel_wait wait
;
312 while (wait_for_ready(w
)) {
313 GEM_BUG_ON(kthread_should_stop());
315 intel_wait_init_for_seqno(&wait
, w
->seqno
);
316 intel_engine_add_wait(w
->engine
, &wait
);
318 set_current_state(TASK_UNINTERRUPTIBLE
);
319 if (i915_seqno_passed(intel_engine_get_seqno(w
->engine
),
323 if (test_bit(STOP
, &w
->flags
)) /* emergency escape */
328 intel_engine_remove_wait(w
->engine
, &wait
);
329 __set_current_state(TASK_RUNNING
);
335 static void igt_wake_all_sync(atomic_t
*ready
,
338 wait_queue_head_t
*wq
,
341 atomic_set(set
, count
);
342 atomic_set(ready
, 0);
345 wait_on_atomic_t(set
, atomic_t_wait
, TASK_UNINTERRUPTIBLE
);
346 atomic_set(ready
, count
);
347 atomic_set(done
, count
);
350 static int igt_wakeup(void *arg
)
352 I915_RND_STATE(prng
);
353 const int state
= TASK_UNINTERRUPTIBLE
;
354 struct intel_engine_cs
*engine
= arg
;
355 struct igt_wakeup
*waiters
;
356 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
357 const int count
= 4096;
358 const u32 max_seqno
= count
/ 4;
359 atomic_t ready
, set
, done
;
363 mock_engine_reset(engine
);
365 waiters
= kvmalloc_array(count
, sizeof(*waiters
), GFP_KERNEL
);
369 /* Create a large number of threads, each waiting on a random seqno.
370 * Multiple waiters will be waiting for the same seqno.
372 atomic_set(&ready
, count
);
373 for (n
= 0; n
< count
; n
++) {
375 waiters
[n
].ready
= &ready
;
376 waiters
[n
].set
= &set
;
377 waiters
[n
].done
= &done
;
378 waiters
[n
].engine
= engine
;
379 waiters
[n
].flags
= BIT(IDLE
);
381 waiters
[n
].tsk
= kthread_run(igt_wakeup_thread
, &waiters
[n
],
383 if (IS_ERR(waiters
[n
].tsk
))
386 get_task_struct(waiters
[n
].tsk
);
389 for (step
= 1; step
<= max_seqno
; step
<<= 1) {
392 /* The waiter threads start paused as we assign them a random
393 * seqno and reset the engine. Once the engine is reset,
394 * we signal that the threads may begin their wait upon their
397 for (n
= 0; n
< count
; n
++) {
398 GEM_BUG_ON(!test_bit(IDLE
, &waiters
[n
].flags
));
400 1 + prandom_u32_state(&prng
) % max_seqno
;
402 mock_seqno_advance(engine
, 0);
403 igt_wake_all_sync(&ready
, &set
, &done
, &wq
, count
);
405 /* Simulate the GPU doing chunks of work, with one or more
406 * seqno appearing to finish at the same time. A random number
407 * of threads will be waiting upon the update and hopefully be
410 for (seqno
= 1; seqno
<= max_seqno
+ step
; seqno
+= step
) {
411 usleep_range(50, 500);
412 mock_seqno_advance(engine
, seqno
);
414 GEM_BUG_ON(intel_engine_get_seqno(engine
) < 1 + max_seqno
);
416 /* With the seqno now beyond any of the waiting threads, they
417 * should all be woken, see that they are complete and signal
418 * that they are ready for the next test. We wait until all
419 * threads are complete and waiting for us (i.e. not a seqno).
421 err
= wait_on_atomic_t(&done
, wait_atomic_timeout
, state
);
423 pr_err("Timed out waiting for %d remaining waiters\n",
428 err
= check_rbtree_empty(engine
);
434 for (n
= 0; n
< count
; n
++) {
435 if (IS_ERR(waiters
[n
].tsk
))
438 set_bit(STOP
, &waiters
[n
].flags
);
440 mock_seqno_advance(engine
, INT_MAX
); /* wakeup any broken waiters */
441 igt_wake_all_sync(&ready
, &set
, &done
, &wq
, n
);
443 for (n
= 0; n
< count
; n
++) {
444 if (IS_ERR(waiters
[n
].tsk
))
447 kthread_stop(waiters
[n
].tsk
);
448 put_task_struct(waiters
[n
].tsk
);
453 mock_engine_flush(engine
);
457 int intel_breadcrumbs_mock_selftests(void)
459 static const struct i915_subtest tests
[] = {
460 SUBTEST(igt_random_insert_remove
),
461 SUBTEST(igt_insert_complete
),
464 struct drm_i915_private
*i915
;
467 i915
= mock_gem_device();
471 err
= i915_subtests(tests
, i915
->engine
[RCS
]);
472 drm_dev_unref(&i915
->drm
);