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 bool wait_for_ready(struct igt_wakeup *w)
278 set_bit(IDLE, &w->flags);
279 if (atomic_dec_and_test(w->done))
280 wake_up_var(w->done);
282 if (test_bit(STOP, &w->flags))
286 prepare_to_wait(w->wq, &ready, TASK_INTERRUPTIBLE);
287 if (atomic_read(w->ready) == 0)
292 finish_wait(w->wq, &ready);
295 clear_bit(IDLE, &w->flags);
296 if (atomic_dec_and_test(w->set))
299 return !test_bit(STOP, &w->flags);
302 static int igt_wakeup_thread(void *arg)
304 struct igt_wakeup *w = arg;
305 struct intel_wait wait;
307 while (wait_for_ready(w)) {
308 GEM_BUG_ON(kthread_should_stop());
310 intel_wait_init_for_seqno(&wait, w->seqno);
311 intel_engine_add_wait(w->engine, &wait);
313 set_current_state(TASK_UNINTERRUPTIBLE);
314 if (i915_seqno_passed(intel_engine_get_seqno(w->engine),
318 if (test_bit(STOP, &w->flags)) /* emergency escape */
323 intel_engine_remove_wait(w->engine, &wait);
324 __set_current_state(TASK_RUNNING);
330 static void igt_wake_all_sync(atomic_t *ready,
333 wait_queue_head_t *wq,
336 atomic_set(set, count);
337 atomic_set(ready, 0);
340 wait_var_event(set, !atomic_read(set));
341 atomic_set(ready, count);
342 atomic_set(done, count);
345 static int igt_wakeup(void *arg)
347 I915_RND_STATE(prng);
348 struct intel_engine_cs *engine = arg;
349 struct igt_wakeup *waiters;
350 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
351 const int count = 4096;
352 const u32 max_seqno = count / 4;
353 atomic_t ready, set, done;
357 mock_engine_reset(engine);
359 waiters = kvmalloc_array(count, sizeof(*waiters), GFP_KERNEL);
363 /* Create a large number of threads, each waiting on a random seqno.
364 * Multiple waiters will be waiting for the same seqno.
366 atomic_set(&ready, count);
367 for (n = 0; n < count; n++) {
369 waiters[n].ready = &ready;
370 waiters[n].set = &set;
371 waiters[n].done = &done;
372 waiters[n].engine = engine;
373 waiters[n].flags = BIT(IDLE);
375 waiters[n].tsk = kthread_run(igt_wakeup_thread, &waiters[n],
377 if (IS_ERR(waiters[n].tsk))
380 get_task_struct(waiters[n].tsk);
383 for (step = 1; step <= max_seqno; step <<= 1) {
386 /* The waiter threads start paused as we assign them a random
387 * seqno and reset the engine. Once the engine is reset,
388 * we signal that the threads may begin their wait upon their
391 for (n = 0; n < count; n++) {
392 GEM_BUG_ON(!test_bit(IDLE, &waiters[n].flags));
394 1 + prandom_u32_state(&prng) % max_seqno;
396 mock_seqno_advance(engine, 0);
397 igt_wake_all_sync(&ready, &set, &done, &wq, count);
399 /* Simulate the GPU doing chunks of work, with one or more
400 * seqno appearing to finish at the same time. A random number
401 * of threads will be waiting upon the update and hopefully be
404 for (seqno = 1; seqno <= max_seqno + step; seqno += step) {
405 usleep_range(50, 500);
406 mock_seqno_advance(engine, seqno);
408 GEM_BUG_ON(intel_engine_get_seqno(engine) < 1 + max_seqno);
410 /* With the seqno now beyond any of the waiting threads, they
411 * should all be woken, see that they are complete and signal
412 * that they are ready for the next test. We wait until all
413 * threads are complete and waiting for us (i.e. not a seqno).
415 if (!wait_var_event_timeout(&done,
416 !atomic_read(&done), 10 * HZ)) {
417 pr_err("Timed out waiting for %d remaining waiters\n",
423 err = check_rbtree_empty(engine);
429 for (n = 0; n < count; n++) {
430 if (IS_ERR(waiters[n].tsk))
433 set_bit(STOP, &waiters[n].flags);
435 mock_seqno_advance(engine, INT_MAX); /* wakeup any broken waiters */
436 igt_wake_all_sync(&ready, &set, &done, &wq, n);
438 for (n = 0; n < count; n++) {
439 if (IS_ERR(waiters[n].tsk))
442 kthread_stop(waiters[n].tsk);
443 put_task_struct(waiters[n].tsk);
448 mock_engine_flush(engine);
452 int intel_breadcrumbs_mock_selftests(void)
454 static const struct i915_subtest tests[] = {
455 SUBTEST(igt_random_insert_remove),
456 SUBTEST(igt_insert_complete),
459 struct drm_i915_private *i915;
462 i915 = mock_gem_device();
466 err = i915_subtests(tests, i915->engine[RCS]);
467 drm_dev_put(&i915->drm);