2 * Copyright © 2017 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"
29 __sync_print(struct i915_syncmap *p,
30 char *buf, unsigned long *sz,
41 for (d = 0; d < depth - 1; d++) {
42 if (last & BIT(depth - d - 1))
43 len = scnprintf(buf, *sz, "| ");
45 len = scnprintf(buf, *sz, " ");
49 len = scnprintf(buf, *sz, "%x-> ", idx);
54 /* We mark bits after the prefix as "X" */
55 len = scnprintf(buf, *sz, "0x%016llx", p->prefix << p->height << SHIFT);
58 X = (p->height + SHIFT) / 4;
59 scnprintf(buf - X, *sz + X, "%*s", X, "XXXXXXXXXXXXXXXXX");
62 for_each_set_bit(i, (unsigned long *)&p->bitmap, KSYNCMAP) {
63 len = scnprintf(buf, *sz, " %x:%x,",
64 i, __sync_seqno(p)[i]);
72 len = scnprintf(buf, *sz, "\n");
77 for_each_set_bit(i, (unsigned long *)&p->bitmap, KSYNCMAP) {
78 buf = __sync_print(__sync_child(p)[i], buf, sz,
80 last << 1 | ((p->bitmap >> (i + 1)) ? 1 : 0),
89 i915_syncmap_print_to_buf(struct i915_syncmap *p, char *buf, unsigned long sz)
97 __sync_print(p, buf, &sz, 0, 1, 0);
101 static int check_syncmap_free(struct i915_syncmap **sync)
103 i915_syncmap_free(sync);
105 pr_err("sync not cleared after free\n");
112 static int dump_syncmap(struct i915_syncmap *sync, int err)
117 return check_syncmap_free(&sync);
119 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
123 if (i915_syncmap_print_to_buf(sync, buf, PAGE_SIZE))
129 i915_syncmap_free(&sync);
133 static int igt_syncmap_init(void *arg)
135 struct i915_syncmap *sync = (void *)~0ul;
138 * Cursory check that we can initialise a random pointer and transform
139 * it into the root pointer of a syncmap.
142 i915_syncmap_init(&sync);
143 return check_syncmap_free(&sync);
146 static int check_seqno(struct i915_syncmap *leaf, unsigned int idx, u32 seqno)
149 pr_err("%s: not a leaf, height is %d\n",
150 __func__, leaf->height);
154 if (__sync_seqno(leaf)[idx] != seqno) {
155 pr_err("%s: seqno[%d], found %x, expected %x\n",
156 __func__, idx, __sync_seqno(leaf)[idx], seqno);
163 static int check_one(struct i915_syncmap **sync, u64 context, u32 seqno)
167 err = i915_syncmap_set(sync, context, seqno);
171 if ((*sync)->height) {
172 pr_err("Inserting first context=%llx did not return leaf (height=%d, prefix=%llx\n",
173 context, (*sync)->height, (*sync)->prefix);
177 if ((*sync)->parent) {
178 pr_err("Inserting first context=%llx created branches!\n",
183 if (hweight32((*sync)->bitmap) != 1) {
184 pr_err("First bitmap does not contain a single entry, found %x (count=%d)!\n",
185 (*sync)->bitmap, hweight32((*sync)->bitmap));
189 err = check_seqno((*sync), ilog2((*sync)->bitmap), seqno);
193 if (!i915_syncmap_is_later(sync, context, seqno)) {
194 pr_err("Lookup of first context=%llx/seqno=%x failed!\n",
202 static int igt_syncmap_one(void *arg)
204 I915_RND_STATE(prng);
205 IGT_TIMEOUT(end_time);
206 struct i915_syncmap *sync;
207 unsigned long max = 1;
211 * Check that inserting a new id, creates a leaf and only that leaf.
214 i915_syncmap_init(&sync);
217 u64 context = i915_prandom_u64_state(&prng);
220 err = check_syncmap_free(&sync);
224 for (loop = 0; loop <= max; loop++) {
225 err = check_one(&sync, context,
226 prandom_u32_state(&prng));
231 } while (!__igt_timeout(end_time, NULL));
232 pr_debug("%s: Completed %lu single insertions\n",
233 __func__, max * (max - 1) / 2);
235 return dump_syncmap(sync, err);
238 static int check_leaf(struct i915_syncmap **sync, u64 context, u32 seqno)
242 err = i915_syncmap_set(sync, context, seqno);
246 if ((*sync)->height) {
247 pr_err("Inserting context=%llx did not return leaf (height=%d, prefix=%llx\n",
248 context, (*sync)->height, (*sync)->prefix);
252 if (hweight32((*sync)->bitmap) != 1) {
253 pr_err("First entry into leaf (context=%llx) does not contain a single entry, found %x (count=%d)!\n",
254 context, (*sync)->bitmap, hweight32((*sync)->bitmap));
258 err = check_seqno((*sync), ilog2((*sync)->bitmap), seqno);
262 if (!i915_syncmap_is_later(sync, context, seqno)) {
263 pr_err("Lookup of first entry context=%llx/seqno=%x failed!\n",
271 static int igt_syncmap_join_above(void *arg)
273 struct i915_syncmap *sync;
274 unsigned int pass, order;
277 i915_syncmap_init(&sync);
280 * When we have a new id that doesn't fit inside the existing tree,
281 * we need to add a new layer above.
288 * Each pass the common prefix shrinks and we have to insert a join.
289 * Each join will only contain two branches, the latest of which
292 * If we then reuse the same set of contexts, we expect to build an
295 for (pass = 0; pass < 3; pass++) {
296 for (order = 0; order < 64; order += SHIFT) {
297 u64 context = BIT_ULL(order);
298 struct i915_syncmap *join;
300 err = check_leaf(&sync, context, 0);
305 if (!join) /* very first insert will have no parents */
309 pr_err("Parent with no height!\n");
314 if (hweight32(join->bitmap) != 2) {
315 pr_err("Join does not have 2 children: %x (%d)\n",
316 join->bitmap, hweight32(join->bitmap));
321 if (__sync_child(join)[__sync_branch_idx(join, context)] != sync) {
322 pr_err("Leaf misplaced in parent!\n");
329 return dump_syncmap(sync, err);
332 static int igt_syncmap_join_below(void *arg)
334 struct i915_syncmap *sync;
335 unsigned int step, order, idx;
338 i915_syncmap_init(&sync);
341 * Check that we can split a compacted branch by replacing it with
344 for (step = 0; step < KSYNCMAP; step++) {
345 for (order = 64 - SHIFT; order > 0; order -= SHIFT) {
346 u64 context = step * BIT_ULL(order);
348 err = i915_syncmap_set(&sync, context, 0);
353 pr_err("Inserting context=%llx (order=%d, step=%d) did not return leaf (height=%d, prefix=%llx\n",
354 context, order, step, sync->height, sync->prefix);
361 for (step = 0; step < KSYNCMAP; step++) {
362 for (order = SHIFT; order < 64; order += SHIFT) {
363 u64 context = step * BIT_ULL(order);
365 if (!i915_syncmap_is_later(&sync, context, 0)) {
366 pr_err("1: context %llx (order=%d, step=%d) not found\n",
367 context, order, step);
372 for (idx = 1; idx < KSYNCMAP; idx++) {
373 if (i915_syncmap_is_later(&sync, context + idx, 0)) {
374 pr_err("1: context %llx (order=%d, step=%d) should not exist\n",
375 context + idx, order, step);
383 for (order = SHIFT; order < 64; order += SHIFT) {
384 for (step = 0; step < KSYNCMAP; step++) {
385 u64 context = step * BIT_ULL(order);
387 if (!i915_syncmap_is_later(&sync, context, 0)) {
388 pr_err("2: context %llx (order=%d, step=%d) not found\n",
389 context, order, step);
397 return dump_syncmap(sync, err);
400 static int igt_syncmap_neighbours(void *arg)
402 I915_RND_STATE(prng);
403 IGT_TIMEOUT(end_time);
404 struct i915_syncmap *sync;
408 * Each leaf holds KSYNCMAP seqno. Check that when we create KSYNCMAP
409 * neighbouring ids, they all fit into the same leaf.
412 i915_syncmap_init(&sync);
414 u64 context = i915_prandom_u64_state(&prng) & ~MASK;
417 if (i915_syncmap_is_later(&sync, context, 0)) /* Skip repeats */
420 for (idx = 0; idx < KSYNCMAP; idx++) {
421 err = i915_syncmap_set(&sync, context + idx, 0);
426 pr_err("Inserting context=%llx did not return leaf (height=%d, prefix=%llx\n",
427 context, sync->height, sync->prefix);
432 if (sync->bitmap != BIT(idx + 1) - 1) {
433 pr_err("Inserting neighbouring context=0x%llx+%d, did not fit into the same leaf bitmap=%x (%d), expected %lx (%d)\n",
435 sync->bitmap, hweight32(sync->bitmap),
436 BIT(idx + 1) - 1, idx + 1);
441 } while (!__igt_timeout(end_time, NULL));
443 return dump_syncmap(sync, err);
446 static int igt_syncmap_compact(void *arg)
448 struct i915_syncmap *sync;
449 unsigned int idx, order;
452 i915_syncmap_init(&sync);
455 * The syncmap are "space efficient" compressed radix trees - any
456 * branch with only one child is skipped and replaced by the child.
458 * If we construct a tree with ids that are neighbouring at a non-zero
459 * height, we form a join but each child of that join is directly a
460 * leaf holding the single id.
462 for (order = SHIFT; order < 64; order += SHIFT) {
463 err = check_syncmap_free(&sync);
467 /* Create neighbours in the parent */
468 for (idx = 0; idx < KSYNCMAP; idx++) {
469 u64 context = idx * BIT_ULL(order) + idx;
471 err = i915_syncmap_set(&sync, context, 0);
476 pr_err("Inserting context=%llx (order=%d, idx=%d) did not return leaf (height=%d, prefix=%llx\n",
478 sync->height, sync->prefix);
486 pr_err("Parent (join) of last leaf was not the sync!\n");
491 if (sync->height != order) {
492 pr_err("Join does not have the expected height, found %d, expected %d\n",
493 sync->height, order);
498 if (sync->bitmap != BIT(KSYNCMAP) - 1) {
499 pr_err("Join is not full!, found %x (%d) expected %lx (%d)\n",
500 sync->bitmap, hweight32(sync->bitmap),
501 BIT(KSYNCMAP) - 1, KSYNCMAP);
506 /* Each of our children should be a leaf */
507 for (idx = 0; idx < KSYNCMAP; idx++) {
508 struct i915_syncmap *leaf = __sync_child(sync)[idx];
511 pr_err("Child %d is a not leaf!\n", idx);
516 if (leaf->parent != sync) {
517 pr_err("Child %d is not attached to us!\n",
523 if (!is_power_of_2(leaf->bitmap)) {
524 pr_err("Child %d holds more than one id, found %x (%d)\n",
525 idx, leaf->bitmap, hweight32(leaf->bitmap));
530 if (leaf->bitmap != BIT(idx)) {
531 pr_err("Child %d has wrong seqno idx, found %d, expected %d\n",
532 idx, ilog2(leaf->bitmap), idx);
539 return dump_syncmap(sync, err);
542 static int igt_syncmap_random(void *arg)
544 I915_RND_STATE(prng);
545 IGT_TIMEOUT(end_time);
546 struct i915_syncmap *sync;
547 unsigned long count, phase, i;
551 i915_syncmap_init(&sync);
554 * Having tried to test the individual operations within i915_syncmap,
555 * run a smoketest exploring the entire u64 space with random
560 phase = jiffies + HZ/100 + 1;
562 u64 context = i915_prandom_u64_state(&prng);
564 err = i915_syncmap_set(&sync, context, 0);
569 } while (!time_after(jiffies, phase));
575 u32 last_seqno = seqno;
578 seqno = prandom_u32_state(&prng);
579 expect = seqno_later(last_seqno, seqno);
581 for (i = 0; i < count; i++) {
582 u64 context = i915_prandom_u64_state(&ctx);
584 if (i915_syncmap_is_later(&sync, context, seqno) != expect) {
585 pr_err("context=%llu, last=%u this=%u did not match expectation (%d)\n",
586 context, last_seqno, seqno, expect);
591 err = i915_syncmap_set(&sync, context, seqno);
597 } while (!__igt_timeout(end_time, NULL));
598 pr_debug("Completed %lu passes, each of %lu contexts\n", phase, count);
600 return dump_syncmap(sync, err);
603 int i915_syncmap_mock_selftests(void)
605 static const struct i915_subtest tests[] = {
606 SUBTEST(igt_syncmap_init),
607 SUBTEST(igt_syncmap_one),
608 SUBTEST(igt_syncmap_join_above),
609 SUBTEST(igt_syncmap_join_below),
610 SUBTEST(igt_syncmap_neighbours),
611 SUBTEST(igt_syncmap_compact),
612 SUBTEST(igt_syncmap_random),
615 return i915_subtests(tests, NULL);