Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 * Copyright © 2017 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

#include "../i915_selftest.h"
#include "i915_random.h"

static char *
__sync_print(struct i915_syncmap *p,
	     char *buf, unsigned long *sz,
	     unsigned int depth,
	     unsigned int last,
	     unsigned int idx)
{
	unsigned long len;
	unsigned int i, X;

	if (depth) {
		unsigned int d;

		for (d = 0; d < depth - 1; d++) {
			if (last & BIT(depth - d - 1))
				len = scnprintf(buf, *sz, "|   ");
			else
				len = scnprintf(buf, *sz, "    ");
			buf += len;
			*sz -= len;
		}
		len = scnprintf(buf, *sz, "%x-> ", idx);
		buf += len;
		*sz -= len;
	}

	/* We mark bits after the prefix as "X" */
	len = scnprintf(buf, *sz, "0x%016llx", p->prefix << p->height << SHIFT);
	buf += len;
	*sz -= len;
	X = (p->height + SHIFT) / 4;
	scnprintf(buf - X, *sz + X, "%*s", X, "XXXXXXXXXXXXXXXXX");

	if (!p->height) {
		for_each_set_bit(i, (unsigned long *)&p->bitmap, KSYNCMAP) {
			len = scnprintf(buf, *sz, " %x:%x,",
					i, __sync_seqno(p)[i]);
			buf += len;
			*sz -= len;
		}
		buf -= 1;
		*sz += 1;
	}

	len = scnprintf(buf, *sz, "\n");
	buf += len;
	*sz -= len;

	if (p->height) {
		for_each_set_bit(i, (unsigned long *)&p->bitmap, KSYNCMAP) {
			buf = __sync_print(__sync_child(p)[i], buf, sz,
					   depth + 1,
					   last << 1 | !!(p->bitmap >> (i + 1)),
					   i);
		}
	}

	return buf;
}

static bool
i915_syncmap_print_to_buf(struct i915_syncmap *p, char *buf, unsigned long sz)
{
	if (!p)
		return false;

	while (p->parent)
		p = p->parent;

	__sync_print(p, buf, &sz, 0, 1, 0);
	return true;
}

static int check_syncmap_free(struct i915_syncmap **sync)
{
	i915_syncmap_free(sync);
	if (*sync) {
		pr_err("sync not cleared after free\n");
		return -EINVAL;
	}

	return 0;
}

static int dump_syncmap(struct i915_syncmap *sync, int err)
{
	char *buf;

	if (!err)
		return check_syncmap_free(&sync);

	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!buf)
		goto skip;

	if (i915_syncmap_print_to_buf(sync, buf, PAGE_SIZE))
		pr_err("%s", buf);

	kfree(buf);

skip:
	i915_syncmap_free(&sync);
	return err;
}

static int igt_syncmap_init(void *arg)
{
	struct i915_syncmap *sync = (void *)~0ul;

	/*
	 * Cursory check that we can initialise a random pointer and transform
	 * it into the root pointer of a syncmap.
	 */

	i915_syncmap_init(&sync);
	return check_syncmap_free(&sync);
}

static int check_seqno(struct i915_syncmap *leaf, unsigned int idx, u32 seqno)
{
	if (leaf->height) {
		pr_err("%s: not a leaf, height is %d\n",
		       __func__, leaf->height);
		return -EINVAL;
	}

	if (__sync_seqno(leaf)[idx] != seqno) {
		pr_err("%s: seqno[%d], found %x, expected %x\n",
		       __func__, idx, __sync_seqno(leaf)[idx], seqno);
		return -EINVAL;
	}

	return 0;
}

static int check_one(struct i915_syncmap **sync, u64 context, u32 seqno)
{
	int err;

	err = i915_syncmap_set(sync, context, seqno);
	if (err)
		return err;

	if ((*sync)->height) {
		pr_err("Inserting first context=%llx did not return leaf (height=%d, prefix=%llx\n",
		       context, (*sync)->height, (*sync)->prefix);
		return -EINVAL;
	}

	if ((*sync)->parent) {
		pr_err("Inserting first context=%llx created branches!\n",
		       context);
		return -EINVAL;
	}

	if (hweight32((*sync)->bitmap) != 1) {
		pr_err("First bitmap does not contain a single entry, found %x (count=%d)!\n",
		       (*sync)->bitmap, hweight32((*sync)->bitmap));
		return -EINVAL;
	}

	err = check_seqno((*sync), ilog2((*sync)->bitmap), seqno);
	if (err)
		return err;

	if (!i915_syncmap_is_later(sync, context, seqno)) {
		pr_err("Lookup of first context=%llx/seqno=%x failed!\n",
		       context, seqno);
		return -EINVAL;
	}

	return 0;
}

static int igt_syncmap_one(void *arg)
{
	I915_RND_STATE(prng);
	IGT_TIMEOUT(end_time);
	struct i915_syncmap *sync;
	unsigned long max = 1;
	int err;

	/*
	 * Check that inserting a new id, creates a leaf and only that leaf.
	 */

	i915_syncmap_init(&sync);

	do {
		u64 context = i915_prandom_u64_state(&prng);
		unsigned long loop;

		err = check_syncmap_free(&sync);
		if (err)
			goto out;

		for (loop = 0; loop <= max; loop++) {
			err = check_one(&sync, context,
					prandom_u32_state(&prng));
			if (err)
				goto out;
		}
		max++;
	} while (!__igt_timeout(end_time, NULL));
	pr_debug("%s: Completed %lu single insertions\n",
		 __func__, max * (max - 1) / 2);
out:
	return dump_syncmap(sync, err);
}

static int check_leaf(struct i915_syncmap **sync, u64 context, u32 seqno)
{
	int err;

	err = i915_syncmap_set(sync, context, seqno);
	if (err)
		return err;

	if ((*sync)->height) {
		pr_err("Inserting context=%llx did not return leaf (height=%d, prefix=%llx\n",
		       context, (*sync)->height, (*sync)->prefix);
		return -EINVAL;
	}

	if (hweight32((*sync)->bitmap) != 1) {
		pr_err("First entry into leaf (context=%llx) does not contain a single entry, found %x (count=%d)!\n",
		       context, (*sync)->bitmap, hweight32((*sync)->bitmap));
		return -EINVAL;
	}

	err = check_seqno((*sync), ilog2((*sync)->bitmap), seqno);
	if (err)
		return err;

	if (!i915_syncmap_is_later(sync, context, seqno)) {
		pr_err("Lookup of first entry context=%llx/seqno=%x failed!\n",
		       context, seqno);
		return -EINVAL;
	}

	return 0;
}

static int igt_syncmap_join_above(void *arg)
{
	struct i915_syncmap *sync;
	unsigned int pass, order;
	int err;

	i915_syncmap_init(&sync);

	/*
	 * When we have a new id that doesn't fit inside the existing tree,
	 * we need to add a new layer above.
	 *
	 * 1: 0x00000001
	 * 2: 0x00000010
	 * 3: 0x00000100
	 * 4: 0x00001000
	 * ...
	 * Each pass the common prefix shrinks and we have to insert a join.
	 * Each join will only contain two branches, the latest of which
	 * is always a leaf.
	 *
	 * If we then reuse the same set of contexts, we expect to build an
	 * identical tree.
	 */
	for (pass = 0; pass < 3; pass++) {
		for (order = 0; order < 64; order += SHIFT) {
			u64 context = BIT_ULL(order);
			struct i915_syncmap *join;

			err = check_leaf(&sync, context, 0);
			if (err)
				goto out;

			join = sync->parent;
			if (!join) /* very first insert will have no parents */
				continue;

			if (!join->height) {
				pr_err("Parent with no height!\n");
				err = -EINVAL;
				goto out;
			}

			if (hweight32(join->bitmap) != 2) {
				pr_err("Join does not have 2 children: %x (%d)\n",
				       join->bitmap, hweight32(join->bitmap));
				err = -EINVAL;
				goto out;
			}

			if (__sync_child(join)[__sync_branch_idx(join, context)] != sync) {
				pr_err("Leaf misplaced in parent!\n");
				err = -EINVAL;
				goto out;
			}
		}
	}
out:
	return dump_syncmap(sync, err);
}

static int igt_syncmap_join_below(void *arg)
{
	struct i915_syncmap *sync;
	unsigned int step, order, idx;
	int err = -ENODEV;

	i915_syncmap_init(&sync);

	/*
	 * Check that we can split a compacted branch by replacing it with
	 * a join.
	 */
	for (step = 0; step < KSYNCMAP; step++) {
		for (order = 64 - SHIFT; order > 0; order -= SHIFT) {
			u64 context = step * BIT_ULL(order);

			err = i915_syncmap_set(&sync, context, 0);
			if (err)
				goto out;

			if (sync->height) {
				pr_err("Inserting context=%llx (order=%d, step=%d) did not return leaf (height=%d, prefix=%llx\n",
				       context, order, step, sync->height, sync->prefix);
				err = -EINVAL;
				goto out;
			}
		}
	}

	for (step = 0; step < KSYNCMAP; step++) {
		for (order = SHIFT; order < 64; order += SHIFT) {
			u64 context = step * BIT_ULL(order);

			if (!i915_syncmap_is_later(&sync, context, 0)) {
				pr_err("1: context %llx (order=%d, step=%d) not found\n",
				       context, order, step);
				err = -EINVAL;
				goto out;
			}

			for (idx = 1; idx < KSYNCMAP; idx++) {
				if (i915_syncmap_is_later(&sync, context + idx, 0)) {
					pr_err("1: context %llx (order=%d, step=%d) should not exist\n",
					       context + idx, order, step);
					err = -EINVAL;
					goto out;
				}
			}
		}
	}

	for (order = SHIFT; order < 64; order += SHIFT) {
		for (step = 0; step < KSYNCMAP; step++) {
			u64 context = step * BIT_ULL(order);

			if (!i915_syncmap_is_later(&sync, context, 0)) {
				pr_err("2: context %llx (order=%d, step=%d) not found\n",
				       context, order, step);
				err = -EINVAL;
				goto out;
			}
		}
	}

out:
	return dump_syncmap(sync, err);
}

static int igt_syncmap_neighbours(void *arg)
{
	I915_RND_STATE(prng);
	IGT_TIMEOUT(end_time);
	struct i915_syncmap *sync;
	int err = -ENODEV;

	/*
	 * Each leaf holds KSYNCMAP seqno. Check that when we create KSYNCMAP
	 * neighbouring ids, they all fit into the same leaf.
	 */

	i915_syncmap_init(&sync);
	do {
		u64 context = i915_prandom_u64_state(&prng) & ~MASK;
		unsigned int idx;

		if (i915_syncmap_is_later(&sync, context, 0)) /* Skip repeats */
			continue;

		for (idx = 0; idx < KSYNCMAP; idx++) {
			err = i915_syncmap_set(&sync, context + idx, 0);
			if (err)
				goto out;

			if (sync->height) {
				pr_err("Inserting context=%llx did not return leaf (height=%d, prefix=%llx\n",
				       context, sync->height, sync->prefix);
				err = -EINVAL;
				goto out;
			}

			if (sync->bitmap != BIT(idx + 1) - 1) {
				pr_err("Inserting neighbouring context=0x%llx+%d, did not fit into the same leaf bitmap=%x (%d), expected %lx (%d)\n",
				       context, idx,
				       sync->bitmap, hweight32(sync->bitmap),
				       BIT(idx + 1) - 1, idx + 1);
				err = -EINVAL;
				goto out;
			}
		}
	} while (!__igt_timeout(end_time, NULL));
out:
	return dump_syncmap(sync, err);
}

static int igt_syncmap_compact(void *arg)
{
	struct i915_syncmap *sync;
	unsigned int idx, order;
	int err = -ENODEV;

	i915_syncmap_init(&sync);

	/*
	 * The syncmap are "space efficient" compressed radix trees - any
	 * branch with only one child is skipped and replaced by the child.
	 *
	 * If we construct a tree with ids that are neighbouring at a non-zero
	 * height, we form a join but each child of that join is directly a
	 * leaf holding the single id.
	 */
	for (order = SHIFT; order < 64; order += SHIFT) {
		err = check_syncmap_free(&sync);
		if (err)
			goto out;

		/* Create neighbours in the parent */
		for (idx = 0; idx < KSYNCMAP; idx++) {
			u64 context = idx * BIT_ULL(order) + idx;

			err = i915_syncmap_set(&sync, context, 0);
			if (err)
				goto out;

			if (sync->height) {
				pr_err("Inserting context=%llx (order=%d, idx=%d) did not return leaf (height=%d, prefix=%llx\n",
				       context, order, idx,
				       sync->height, sync->prefix);
				err = -EINVAL;
				goto out;
			}
		}

		sync = sync->parent;
		if (sync->parent) {
			pr_err("Parent (join) of last leaf was not the sync!\n");
			err = -EINVAL;
			goto out;
		}

		if (sync->height != order) {
			pr_err("Join does not have the expected height, found %d, expected %d\n",
			       sync->height, order);
			err = -EINVAL;
			goto out;
		}

		if (sync->bitmap != BIT(KSYNCMAP) - 1) {
			pr_err("Join is not full!, found %x (%d) expected %lx (%d)\n",
			       sync->bitmap, hweight32(sync->bitmap),
			       BIT(KSYNCMAP) - 1, KSYNCMAP);
			err = -EINVAL;
			goto out;
		}

		/* Each of our children should be a leaf */
		for (idx = 0; idx < KSYNCMAP; idx++) {
			struct i915_syncmap *leaf = __sync_child(sync)[idx];

			if (leaf->height) {
				pr_err("Child %d is a not leaf!\n", idx);
				err = -EINVAL;
				goto out;
			}

			if (leaf->parent != sync) {
				pr_err("Child %d is not attached to us!\n",
				       idx);
				err = -EINVAL;
				goto out;
			}

			if (!is_power_of_2(leaf->bitmap)) {
				pr_err("Child %d holds more than one id, found %x (%d)\n",
				       idx, leaf->bitmap, hweight32(leaf->bitmap));
				err = -EINVAL;
				goto out;
			}

			if (leaf->bitmap != BIT(idx)) {
				pr_err("Child %d has wrong seqno idx, found %d, expected %d\n",
				       idx, ilog2(leaf->bitmap), idx);
				err = -EINVAL;
				goto out;
			}
		}
	}
out:
	return dump_syncmap(sync, err);
}

static int igt_syncmap_random(void *arg)
{
	I915_RND_STATE(prng);
	IGT_TIMEOUT(end_time);
	struct i915_syncmap *sync;
	unsigned long count, phase, i;
	u32 seqno;
	int err;

	i915_syncmap_init(&sync);

	/*
	 * Having tried to test the individual operations within i915_syncmap,
	 * run a smoketest exploring the entire u64 space with random
	 * insertions.
	 */

	count = 0;
	phase = jiffies + HZ/100 + 1;
	do {
		u64 context = i915_prandom_u64_state(&prng);

		err = i915_syncmap_set(&sync, context, 0);
		if (err)
			goto out;

		count++;
	} while (!time_after(jiffies, phase));
	seqno = 0;

	phase = 0;
	do {
		I915_RND_STATE(ctx);
		u32 last_seqno = seqno;
		bool expect;

		seqno = prandom_u32_state(&prng);
		expect = seqno_later(last_seqno, seqno);

		for (i = 0; i < count; i++) {
			u64 context = i915_prandom_u64_state(&ctx);

			if (i915_syncmap_is_later(&sync, context, seqno) != expect) {
				pr_err("context=%llu, last=%u this=%u did not match expectation (%d)\n",
				       context, last_seqno, seqno, expect);
				err = -EINVAL;
				goto out;
			}

			err = i915_syncmap_set(&sync, context, seqno);
			if (err)
				goto out;
		}

		phase++;
	} while (!__igt_timeout(end_time, NULL));
	pr_debug("Completed %lu passes, each of %lu contexts\n", phase, count);
out:
	return dump_syncmap(sync, err);
}

int i915_syncmap_mock_selftests(void)
{
	static const struct i915_subtest tests[] = {
		SUBTEST(igt_syncmap_init),
		SUBTEST(igt_syncmap_one),
		SUBTEST(igt_syncmap_join_above),
		SUBTEST(igt_syncmap_join_below),
		SUBTEST(igt_syncmap_neighbours),
		SUBTEST(igt_syncmap_compact),
		SUBTEST(igt_syncmap_random),
	};

	return i915_subtests(tests, NULL);
}