Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2016 Intel Corporation |
| 3 | * |
| 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: |
| 10 | * |
| 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 |
| 13 | * Software. |
| 14 | * |
| 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 |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "../i915_selftest.h" |
| 26 | #include "i915_random.h" |
| 27 | |
| 28 | #include "mock_engine.h" |
| 29 | |
| 30 | static int check_rbtree(struct intel_engine_cs *engine, |
| 31 | const unsigned long *bitmap, |
| 32 | const struct intel_wait *waiters, |
| 33 | const int count) |
| 34 | { |
| 35 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 36 | struct rb_node *rb; |
| 37 | int n; |
| 38 | |
| 39 | if (&b->first_wait->node != rb_first(&b->waiters)) { |
| 40 | pr_err("First waiter does not match first element of wait-tree\n"); |
| 41 | return -EINVAL; |
| 42 | } |
| 43 | |
| 44 | n = find_first_bit(bitmap, count); |
| 45 | for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) { |
| 46 | struct intel_wait *w = container_of(rb, typeof(*w), node); |
| 47 | int idx = w - waiters; |
| 48 | |
| 49 | if (!test_bit(idx, bitmap)) { |
| 50 | pr_err("waiter[%d, seqno=%d] removed but still in wait-tree\n", |
| 51 | idx, w->seqno); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | if (n != idx) { |
| 56 | pr_err("waiter[%d, seqno=%d] does not match expected next element in tree [%d]\n", |
| 57 | idx, w->seqno, n); |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | n = find_next_bit(bitmap, count, n + 1); |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int check_rbtree_empty(struct intel_engine_cs *engine) |
| 68 | { |
| 69 | struct intel_breadcrumbs *b = &engine->breadcrumbs; |
| 70 | |
| 71 | if (b->first_wait) { |
| 72 | pr_err("Empty breadcrumbs still has a waiter\n"); |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
| 76 | if (!RB_EMPTY_ROOT(&b->waiters)) { |
| 77 | pr_err("Empty breadcrumbs, but wait-tree not empty\n"); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int igt_random_insert_remove(void *arg) |
| 85 | { |
| 86 | const u32 seqno_bias = 0x1000; |
| 87 | I915_RND_STATE(prng); |
| 88 | struct intel_engine_cs *engine = arg; |
| 89 | struct intel_wait *waiters; |
| 90 | const int count = 4096; |
| 91 | unsigned int *order; |
| 92 | unsigned long *bitmap; |
| 93 | int err = -ENOMEM; |
| 94 | int n; |
| 95 | |
| 96 | mock_engine_reset(engine); |
| 97 | |
| 98 | waiters = drm_malloc_gfp(count, sizeof(*waiters), GFP_TEMPORARY); |
| 99 | if (!waiters) |
| 100 | goto out_engines; |
| 101 | |
| 102 | bitmap = kcalloc(DIV_ROUND_UP(count, BITS_PER_LONG), sizeof(*bitmap), |
| 103 | GFP_TEMPORARY); |
| 104 | if (!bitmap) |
| 105 | goto out_waiters; |
| 106 | |
| 107 | order = i915_random_order(count, &prng); |
| 108 | if (!order) |
| 109 | goto out_bitmap; |
| 110 | |
| 111 | for (n = 0; n < count; n++) |
| 112 | intel_wait_init(&waiters[n], seqno_bias + n); |
| 113 | |
| 114 | err = check_rbtree(engine, bitmap, waiters, count); |
| 115 | if (err) |
| 116 | goto out_order; |
| 117 | |
| 118 | /* Add and remove waiters into the rbtree in random order. At each |
| 119 | * step, we verify that the rbtree is correctly ordered. |
| 120 | */ |
| 121 | for (n = 0; n < count; n++) { |
| 122 | int i = order[n]; |
| 123 | |
| 124 | intel_engine_add_wait(engine, &waiters[i]); |
| 125 | __set_bit(i, bitmap); |
| 126 | |
| 127 | err = check_rbtree(engine, bitmap, waiters, count); |
| 128 | if (err) |
| 129 | goto out_order; |
| 130 | } |
| 131 | |
| 132 | i915_random_reorder(order, count, &prng); |
| 133 | for (n = 0; n < count; n++) { |
| 134 | int i = order[n]; |
| 135 | |
| 136 | intel_engine_remove_wait(engine, &waiters[i]); |
| 137 | __clear_bit(i, bitmap); |
| 138 | |
| 139 | err = check_rbtree(engine, bitmap, waiters, count); |
| 140 | if (err) |
| 141 | goto out_order; |
| 142 | } |
| 143 | |
| 144 | err = check_rbtree_empty(engine); |
| 145 | out_order: |
| 146 | kfree(order); |
| 147 | out_bitmap: |
| 148 | kfree(bitmap); |
| 149 | out_waiters: |
| 150 | drm_free_large(waiters); |
| 151 | out_engines: |
| 152 | mock_engine_flush(engine); |
| 153 | return err; |
| 154 | } |
| 155 | |
| 156 | int intel_breadcrumbs_mock_selftests(void) |
| 157 | { |
| 158 | static const struct i915_subtest tests[] = { |
| 159 | SUBTEST(igt_random_insert_remove), |
| 160 | }; |
| 161 | struct intel_engine_cs *engine; |
| 162 | int err; |
| 163 | |
| 164 | engine = mock_engine("mock"); |
| 165 | if (!engine) |
| 166 | return -ENOMEM; |
| 167 | |
| 168 | err = i915_subtests(tests, engine); |
| 169 | kfree(engine); |
| 170 | |
| 171 | return err; |
| 172 | } |