summaryrefslogtreecommitdiffstats
Commit message (Expand)AuthorAge
...
* Revert "gpu: nvgpu: fix allocator_init() calls"Bharat Nihalani2016-12-27
* gpu: nvgpu: Disable channel when writing syncpt idTerje Bergstrom2016-12-27
* gpu: nvgpu: Expose preemption flags to user spaceTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Use correct PBDMA sigTerje Bergstrom2016-12-27
* gpu: nvgpu: fix allocator_init() callsDeepak Nibade2016-12-27
* gpu:nvgpu: gp10b: update channel_setup_ramfcSeshendra Gadagottu2016-12-27
* gpu: nvgpu: gp10b: add hwpm registersLeonid Moiseichuk2016-12-27
* gpu: nvgpu: gp10b: Wait for preempted or emptyTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Fix PDE/PTE address handlingTerje Bergstrom2016-12-27
* Revert "Revert "gpu: nvgpu: gp10b part of new VA allocator""Alex Waterman2016-12-27
* Revert "gpu: nvgpu: gp10b part of new VA allocator"Terje Bergstrom2016-12-27
* gpu: nvgpu: gp10b part of new VA allocatorAlex Waterman2016-12-27
* gpu:nvgpu:gp10b: support secure gpccs changesVijayakumar2016-12-27
* gpu: nvgpu: gp10b: Define VPR allocatorTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Fix comptag index calculationTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Enable new page table formatTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Fix caching attributeTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Dynamic GfxP buffer sizeTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Use betacb size from debugfsTerje Bergstrom2016-12-27
* gpu: nvgpu: Implement syncpt protectionTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: fix sparse warnings of static symbolDeepak Nibade2016-12-27
* gpu: nvgpu: gp10b: Enable SMMU bypassTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Gating reglistTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Make page mapping scatter awareTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: update elpg sequencing valueMahantesh Kumbar2016-12-27
* gpu: nvgpu: secure boot flag, default disabledMahantesh Kumbar2016-12-27
* gpu: nvgpu: gp10b:inherit gm20b acr init wpr funcMahantesh Kumbar2016-12-27
* gpu: nvgpu: gp10b: Implement new page table formatTerje Bergstrom2016-12-27
* gpu: nvgpu: set zbc format field properlyKonsta Holtta2016-12-27
* gpu: nvgpu: gp10b: fix sparse warnings of static symbolDeepak Nibade2016-12-27
* gpu: nvgpu: gp10b: Fix include pathAlex Van Brunt2016-12-27
* gpu: nvgpu: gp10b: Define compressible page sizeTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Correct steady state CB sizeTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Program stream id to LTCTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Regenerate HW headersTerje Bergstrom2016-12-27
* gpu: nvgpu: add get_iova_addr() for gp10bDeepak Nibade2016-12-27
* include: uapi: nvgpu: add flag for IO coherenceDeepak Nibade2016-12-27
* gpu: nvgpu: gp10b: Do not clear compbit store sizeTerje Bergstrom2016-12-27
* gpu: nvgpu: zbc: disable activity only from ioctlKonsta Holtta2016-12-27
* gpu: nvgpu: accessor for memfmt exceptionDeepak Nibade2016-12-27
* gpu: nvgpu: gp10b: Use mem_desc for buffersTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Fix offset for preemption ptrTerje Bergstrom2016-12-27
* gpu: nvgpu: gp10b: Use gp10b version of phys bitsTerje Bergstrom2016-12-27
* gpu: nvgpu: add exception registers to dumpDeepak Nibade2016-12-27
* gpu: nvgpu: gp10b: fix swdx_rm_spill size and pointerKirill Artamonov2016-12-27
* gpu: nvgpu: gp10b: Add replayable pagefault bufferSeshendra Gadagottu2016-12-27
* gpu: nvgpu: gp10b: support for replayable faultsSeshendra Gadagottu2016-12-27
* gpu: nvgpu: gp10b: setup mm hw initSeshendra Gadagottu2016-12-27
* gpu: nvgpu: gp10b: update fb headersSeshendra Gadagottu2016-12-27
* gpu: nvgpu: gp10b: update headersSeshendra Gadagottu2016-12-27
22 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 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
/*
 * Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
 *
 * 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 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 <stdlib.h>

#include <unit/io.h>
#include <unit/unit.h>

#include <nvgpu/posix/bitops.h>

#define NUM_WORDS 4

static unsigned long single_ulong_maps[] = {
	0UL,
	~0UL,
	0xff00ff00UL,
	0x00ff00ffUL,
	0xa5a5a5a5UL,
	0x0000ffffUL,
	0xffff0000UL,
	0x1UL,
	0x80000000UL,
	BIT(16),
};

/*
 * Can't fail - just some info prints.
 */
static int test_bitmap_info(struct unit_module *m, struct gk20a *g, void *args)
{
	unit_info(m, "sizeof(unsigned long) = %zu\n", sizeof(unsigned long));
	unit_info(m, "BITS_PER_LONG         = %lu\n", BITS_PER_LONG);

	return UNIT_SUCCESS;
}

static int test_ffs(struct unit_module *m, struct gk20a *g, void *args)
{
#define CHECK_FFS_WORD(w, answer)					\
	do {								\
		unsigned long ret = ffs(w);				\
									\
		if (ret != (answer))					\
			unit_return_fail(m,				\
					 "ffs(0x%016lx) = %lu "		\
					 "[expected %lu]\n",		\
					 w, ret, answer);		\
	} while (0)

	unsigned long i;

	CHECK_FFS_WORD(single_ulong_maps[0], BITS_PER_LONG - 1UL);
	CHECK_FFS_WORD(single_ulong_maps[1], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[2], 8UL);
	CHECK_FFS_WORD(single_ulong_maps[3], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[4], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[5], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[6], 16UL);
	CHECK_FFS_WORD(single_ulong_maps[7], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[8], 31UL);
	CHECK_FFS_WORD(single_ulong_maps[9], 16UL);

#undef CHECK_FFS_WORD

	/*
	 * Also just test every bit to make sure we definitely cover all
	 * possible return values of the function.
	 */
	for (i = 0; i < BITS_PER_LONG; i++) {
		if (ffs(BIT(i)) != i)
			unit_return_fail(m, "ffs(1 << %lu) != %lu [%lu]!\n",
					 i, i, ffs(BIT(i)));
	}

	return UNIT_SUCCESS;
}

static int test_fls(struct unit_module *m, struct gk20a *g, void *args)
{
#define CHECK_FLS_WORD(w, answer)					\
	do {								\
		unsigned long ret = fls(w);				\
									\
		if (ret != (answer))					\
			unit_return_fail(m,				\
					 "fls(0x%016lx) = %lu "		\
					 "[expected = %lu]\n",		\
					 w, ret, answer);		\
	} while (0)

	unsigned long i;

	CHECK_FLS_WORD(single_ulong_maps[0], 0UL);
	CHECK_FLS_WORD(single_ulong_maps[1], BITS_PER_LONG - 1UL);
	CHECK_FLS_WORD(single_ulong_maps[2], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[3], 23UL);
	CHECK_FLS_WORD(single_ulong_maps[4], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[5], 15UL);
	CHECK_FLS_WORD(single_ulong_maps[6], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[7], 0UL);
	CHECK_FLS_WORD(single_ulong_maps[8], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[9], 16UL);

#undef CHECK_FLS_WORD

	for (i = 0; i < BITS_PER_LONG; i++) {
		if (fls(BIT(i)) != i)
			unit_return_fail(m, "fls(1 << %lu) != %lu! [%lu]\n",
					 i, i, fls(BIT(i)));
	}

	return UNIT_SUCCESS;
}

static int test_ffz(struct unit_module *m, struct gk20a *g, void *args)
{
	unsigned long i;

	/*
	 * Since ffz(w) is implemented as ffs(~w) this does less extensive
	 * testing; but it should still cover every line of ffs().
	 */

	for (i = 0; i < BITS_PER_LONG; i++) {
		if (ffz(~BIT(i)) != i)
			unit_return_fail(m, "ffz(~(1 << %lu)) != %lu! [%lu]\n",
					 i, i, ffz(BIT(i)));
	}

	return UNIT_SUCCESS;
}

struct test_find_bit_args {
	bool find_zeros;
};

static struct test_find_bit_args first_bit_args = {
	.find_zeros = false
};

static struct test_find_bit_args first_zero_args = {
	.find_zeros = true
};

static int test_find_first_bit(struct unit_module *m,
			       struct gk20a *g, void *__args)
{
	struct test_find_bit_args *args = __args;
	unsigned long words[NUM_WORDS];
	unsigned long word_idx, bit_idx;
	unsigned long (*finder_function)(const unsigned long *, unsigned long);
	unsigned long result;

	if (args->find_zeros)
		finder_function = find_first_zero_bit;
	else
		finder_function = find_first_bit;

	/*
	 * First test: verify that the size parameter works. We only need the
	 * first word for this.
	 */
	words[0] = ~0xffffUL;
	if (args->find_zeros)
		words[0] = 0xffff;

	if (finder_function(words, 8UL) != 8UL)
		unit_return_fail(m,
				 "find_first_%s(0x%lx, 8) -> %lu [WRONG]\n",
				 args->find_zeros ? "zero_bit" : "bit",
				 words[0], finder_function(words, 8UL));

	if (finder_function(words, 20UL) != 16UL)
		unit_return_fail(m,
				 "find_first_%s(0x%lx, 16) -> %lu [WRONG]\n",
				 args->find_zeros ? "zero_bit" : "bit",
				 words[0], finder_function(words, 20UL));

	/*
	 * Now make sure that for full/empty bitmap find_next_*() returns
	 * the size parameter.
	 */
	memset(words, args->find_zeros ? 0xff : 0x00, sizeof(words));
	result = finder_function(words, NUM_WORDS * BITS_PER_LONG);
	if (result != NUM_WORDS * BITS_PER_LONG)
		unit_return_fail(m, "find_first_%s() failed with empty map\n",
				 args->find_zeros ? "zero_bit" : "bit");

	/*
	 * Third test: set (or zero) the entire bitmap and incrementally clear
	 * bits. Check that we are correct even with multiple words.
	 */

	memset(words, args->find_zeros ? 0x00 : 0xff, sizeof(words));
	for (word_idx = 0; word_idx < NUM_WORDS; word_idx++) {
		for (bit_idx = 0; bit_idx < BITS_PER_LONG; bit_idx++) {
			unsigned long check =
				(word_idx * BITS_PER_LONG) + bit_idx;
			unsigned long answer =
				finder_function(words,
						NUM_WORDS * BITS_PER_LONG);

			if (answer != check)
				unit_return_fail(m,
						 "find_first_%s loop: "
						 "word_idx = %lu bit_idx = %lu "
						 "-> %lu [WRONG]\n",
						 args->find_zeros ? "zero_bit" : "bit",
						 word_idx, bit_idx, answer);

			/*
			 * Now set/clear this bit in preparation for the next
			 * test.
			 */
			if (args->find_zeros)
				words[word_idx] |= BIT(bit_idx);
			else
				words[word_idx] &= ~BIT(bit_idx);
		}
	}

	return UNIT_SUCCESS;
}

/*
 * Note: the find_first_bit() test also effectively tests the underlying
 * find_next_bit() code since find_first_bit() is just find_next_bit()
 * with a 0 start.
 */

static int test_find_next_bit(struct unit_module *m,
			      struct gk20a *g, void *__args)
{
	unsigned long words[NUM_WORDS];
	unsigned long i, result;

	/*
	 * Fully unset list. Should always return size.
	 */
	memset(words, 0x00, sizeof(words));
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		result = find_next_bit(words, NUM_WORDS * BITS_PER_LONG, i);

		if (result != NUM_WORDS * BITS_PER_LONG)
			unit_return_fail(m, "Fail: empty map (%lu)\n", i);
	}

	/*
	 * Use a fully set list but increment the offset.
	 */
	memset(words, 0xff, sizeof(words));
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		unsigned long first =
			find_next_bit(words, NUM_WORDS * BITS_PER_LONG, i);

		if (first != i)
			unit_return_fail(m,
					 "Fail: first = %lu; should be %lu\n",
					 first, i);
	}

	/*
	 * Start > n should return n.
	 */
#define TEST_START_GREATER_THAN_N(m, map, n, start)			\
	do {								\
		if (find_next_bit(map, n, start) != n)			\
			unit_return_fail(m,				\
					 "Start not greater than N ?? "	\
					 "start=%lu, N=%lu\n",		\
					 start, n);			\
	} while (0)

	TEST_START_GREATER_THAN_N(m, words, BITS_PER_LONG, BITS_PER_LONG + 1);
	TEST_START_GREATER_THAN_N(m, words, 32UL, 64UL);
	TEST_START_GREATER_THAN_N(m, words,
				  BITS_PER_LONG * 2, (BITS_PER_LONG * 2) + 1);
	TEST_START_GREATER_THAN_N(m, words, 0UL, 1UL);
	TEST_START_GREATER_THAN_N(m, words, 0UL, BITS_PER_LONG * 2);
	TEST_START_GREATER_THAN_N(m, words, 0UL, BITS_PER_LONG * NUM_WORDS + 1);

#undef TEST_START_GREATER_THAN_N

	return UNIT_SUCCESS;
}

#define TEST_BITMAP_SIZE (BITS_PER_LONG * 4)
/*
 * 32/64 bit invarient.
 */
static DECLARE_BITMAP(bmap_all_zeros, TEST_BITMAP_SIZE) =
{
	0x0UL, 0x0UL, 0x0UL, 0x0UL
};
static DECLARE_BITMAP(bmap_all_ones, TEST_BITMAP_SIZE) =
{
	~0x0UL, ~0x0UL, ~0x0UL, ~0x0UL
};

static int test_find_zero_area(struct unit_module *m,
			       struct gk20a *g, void *unused)
{
#define FAIL_MSG "Fail: bmap-test='%s' (i=%lu)\n"
#define FAIL_MSG_EX "Fail: bmap-test='%s' (i=%lu, j=%lu)\n"
	unsigned long i, j, result;
	unsigned long words[NUM_WORDS];

	for (i = 0; i < TEST_BITMAP_SIZE; i++) {
		result = bitmap_find_next_zero_area_off(bmap_all_zeros,
							TEST_BITMAP_SIZE,
							i,
							TEST_BITMAP_SIZE - i,
							0, 0);
		if (result != i)
			unit_return_fail(m, FAIL_MSG,
					 "all_zeros: alloc-to-end", i);

		result = bitmap_find_next_zero_area_off(bmap_all_zeros,
							TEST_BITMAP_SIZE,
							i,
							1,
							0, 0);
		if (result != i)
			unit_return_fail(m, FAIL_MSG,
					 "all_zeros: alloc-one-bit", i);

		result = bitmap_find_next_zero_area_off(bmap_all_zeros,
							TEST_BITMAP_SIZE,
							0,
							TEST_BITMAP_SIZE - i,
							0, 0);
		if (result != 0)
			unit_return_fail(m, FAIL_MSG,
					 "all_zeros: alloc-i-bits-at-0", i);
	}

	/*
	 * For the all ones bit map not a single alloc should succeed. We can
	 * just iterate through them all and make sure they all fail.
	 */
	for (i = 0; i < TEST_BITMAP_SIZE; i++) {
		for (j = 0; j < (TEST_BITMAP_SIZE - i); j++) {
			result = bitmap_find_next_zero_area_off(bmap_all_ones,
							TEST_BITMAP_SIZE,
							i,
							j,
							0, 0);
			if (result != TEST_BITMAP_SIZE)
				unit_return_fail(m, FAIL_MSG_EX,
						 "all_ones: failed", i, j);
		}
	}

	/*
	 * Alternating nibbles (4 bits). Make sure we don't start searching from
	 * too high in the bitmap since that will cause failures that are
	 * actually valid. This keeps the logic in the below loop a little more
	 * simple.
	 */