Newer
Older
barebox / commands / memtest.c
@Sascha Hauer Sascha Hauer on 3 Nov 2015 3 KB rework remap_range
/*
 * memtest - Perform a memory test
 *
 * (C) Copyright 2013
 * Alexander Aring <aar@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <command.h>
#include <getopt.h>
#include <memory.h>
#include <malloc.h>
#include <common.h>
#include <errno.h>
#include <memtest.h>
#include <mmu.h>

static int __do_memtest(struct list_head *memtest_regions,
		int bus_only, unsigned cache_flag)
{
	struct mem_test_resource *r;
	int ret;

	list_for_each_entry(r, memtest_regions, list) {
		printf("Testing memory space: "
				"0x%08x -> 0x%08x:\n",
				r->r->start,  r->r->end);
		remap_range((void *)r->r->start, r->r->end -
				r->r->start + 1, cache_flag);

		ret = mem_test(r->r->start, r->r->end, bus_only);
		if (ret < 0)
			return ret;
		printf("done.\n\n");
	}

	return 0;
}

static int do_memtest(int argc, char *argv[])
{
	int bus_only = 0, ret, opt;
	uint32_t i, max_i = 1;
	struct list_head memtest_used_regions;

	while ((opt = getopt(argc, argv, "i:b")) > 0) {
		switch (opt) {
		case 'i':
			max_i = simple_strtoul(optarg, NULL, 0);
			break;
		case 'b':
			bus_only = 1;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (optind > argc)
		return COMMAND_ERROR_USAGE;

	INIT_LIST_HEAD(&memtest_used_regions);

	ret = mem_test_request_regions(&memtest_used_regions);
	if (ret < 0)
		goto out;

	for (i = 1; (i <= max_i) || !max_i; i++) {
		if (max_i)
			printf("Start iteration %u of %u.\n", i, max_i);

		if (arch_can_remap()) {
			/*
			 * First try a memtest with caching enabled.
			 */
			printf("Do memtest with caching enabled.\n");
			ret = __do_memtest(&memtest_used_regions,
					bus_only, MAP_CACHED);
			if (ret < 0)
				goto out;

			/*
			 * Second try a memtest with caching disabled.
			 */
			printf("Do memtest with caching disabled.\n");
			ret = __do_memtest(&memtest_used_regions,
					bus_only, MAP_UNCACHED);
			if (ret < 0)
				goto out;
		} else {
			ret = __do_memtest(&memtest_used_regions,
					bus_only, MAP_DEFAULT);
			if (ret < 0)
				goto out;
		}
	}

out:
	mem_test_release_regions(&memtest_used_regions);

	if (ret < 0) {
		/*
		 * Set cursor to newline, because mem_test failed at
		 * drawing of progressbar.
		 */
		if (ret == -EINTR)
			printf("\n");

		printf("Memtest failed. Error: %d\n", ret);
		return 1;
	}

	printf("Memtest successful.\n");
	return 0;
}


BAREBOX_CMD_HELP_START(memtest)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-i ITERATIONS", "perform number of iterations (default 1, 0 is endless)")
BAREBOX_CMD_HELP_OPT("-b", "perform only a test on bus lines")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(memtest)
	.cmd		= do_memtest,
	BAREBOX_CMD_DESC("extensive memory test")
	BAREBOX_CMD_OPTS("[-ib]")
	BAREBOX_CMD_GROUP(CMD_GRP_MEM)
	BAREBOX_CMD_HELP(cmd_memtest_help)
BAREBOX_CMD_END