summaryrefslogtreecommitdiffstats
path: root/commands/memtest.c
blob: 99d4864e706159ffedba8169e6f9d72f579afc1a (plain) (blame)
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
/*
 * 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_test_one_area(struct mem_test_resource *r, int bus_only,
		unsigned cache_flag)
{
	int ret;

	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_bus_integrity(r->r->start, r->r->end);
	if (ret < 0)
		return ret;

	if (bus_only)
		return 0;

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

	return 0;
}

static int do_memtest_thorough(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) {
		ret = do_test_one_area(r, bus_only, cache_flag);
		if (ret)
			return ret;
	}

	return 0;
}

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

	r = mem_test_biggest_region(memtest_regions);
	if (!r)
		return -EINVAL;

	return do_test_one_area(r, bus_only, cache_flag);
}

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;
	int (*memtest)(struct list_head *, int, unsigned);
	int cached = 0, uncached = 0;

	memtest = do_memtest_biggest;

	while ((opt = getopt(argc, argv, "i:btcu")) > 0) {
		switch (opt) {
		case 'i':
			max_i = simple_strtoul(optarg, NULL, 0);
			break;
		case 'b':
			bus_only = 1;
			break;
		case 't':
			memtest = do_memtest_thorough;
			break;
		case 'c':
			cached = 1;
			break;
		case 'u':
			uncached = 1;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (!arch_can_remap() && (cached || uncached)) {
		printf("Cannot map cached or uncached\n");
		return -EINVAL;
	}

	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++) {
		printf("Start iteration %u", i);
		if (max_i)
			printf(" of %u.\n", max_i);
		else
			putchar('\n');

		if (cached) {
			printf("Do memtest with caching enabled.\n");
			ret = memtest(&memtest_used_regions,
					bus_only, MAP_CACHED);
			if (ret < 0)
				goto out;
		}

		if (uncached) {
			printf("Do memtest with caching disabled.\n");
			ret = memtest(&memtest_used_regions,
					bus_only, MAP_UNCACHED);
			if (ret < 0)
				goto out;
		}

		if (!cached && !uncached) {
			ret = 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_OPT("-c", "cached. Test using cached memory")
BAREBOX_CMD_HELP_OPT("-u", "uncached. Test using uncached memory")
BAREBOX_CMD_HELP_OPT("-t", "thorough. test all free areas. If unset, only test biggest free area")
BAREBOX_CMD_HELP_END

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