/src/barebox/common/memtest.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * memtest.c |
4 | | * |
5 | | * Copyright (C) 2013 Alexander Aring <aar@pengutronix.de>, Pengutronix |
6 | | * |
7 | | * (C) Copyright 2000 |
8 | | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
9 | | */ |
10 | | |
11 | | #include <progress.h> |
12 | | #include <common.h> |
13 | | #include <memory.h> |
14 | | #include <types.h> |
15 | | #include <linux/sizes.h> |
16 | | #include <errno.h> |
17 | | #include <memtest.h> |
18 | | #include <malloc.h> |
19 | | #include <mmu.h> |
20 | | |
21 | | static int alloc_memtest_region(struct list_head *list, |
22 | | resource_size_t start, resource_size_t size) |
23 | 0 | { |
24 | 0 | struct resource *r_new; |
25 | 0 | struct mem_test_resource *r; |
26 | |
|
27 | 0 | r = xzalloc(sizeof(struct mem_test_resource)); |
28 | 0 | r_new = request_sdram_region("memtest", start, size, |
29 | 0 | MEMTYPE_BOOT_SERVICES_DATA, |
30 | 0 | MEMATTRS_RW); |
31 | 0 | if (!r_new) |
32 | 0 | return -EINVAL; |
33 | | |
34 | 0 | r->r = r_new; |
35 | 0 | list_add_tail(&r->list, list); |
36 | |
|
37 | 0 | return 0; |
38 | 0 | } |
39 | | |
40 | | int mem_test_request_regions(struct list_head *list) |
41 | 0 | { |
42 | 0 | int ret; |
43 | 0 | struct memory_bank *bank; |
44 | 0 | struct resource *r, *r_prev = NULL; |
45 | 0 | resource_size_t start, end, size; |
46 | |
|
47 | 0 | for_each_memory_bank(bank) { |
48 | | /* |
49 | | * If we don't have any allocated region on bank, |
50 | | * we use the whole bank boundary |
51 | | */ |
52 | 0 | if (list_empty(&bank->res->children)) { |
53 | 0 | start = PAGE_ALIGN(bank->res->start); |
54 | 0 | size = PAGE_ALIGN_DOWN(bank->res->end - start + 1); |
55 | |
|
56 | 0 | if (size) { |
57 | 0 | ret = alloc_memtest_region(list, start, size); |
58 | 0 | if (ret < 0) |
59 | 0 | return ret; |
60 | 0 | } |
61 | | |
62 | 0 | continue; |
63 | 0 | } |
64 | | |
65 | 0 | r = list_first_entry(&bank->res->children, |
66 | 0 | struct resource, sibling); |
67 | 0 | start = PAGE_ALIGN(bank->res->start); |
68 | 0 | end = PAGE_ALIGN_DOWN(r->start); |
69 | 0 | r_prev = r; |
70 | 0 | if (start != end) { |
71 | 0 | size = end - start; |
72 | 0 | ret = alloc_memtest_region(list, start, size); |
73 | 0 | if (ret < 0) |
74 | 0 | return ret; |
75 | 0 | } |
76 | | /* |
77 | | * We assume that the regions are sorted in this list |
78 | | * So the first element has start boundary on bank->res->start |
79 | | * and the last element hast end boundary on bank->res->end. |
80 | | * |
81 | | * Between used regions. Start from second entry. |
82 | | */ |
83 | 0 | list_for_each_entry_continue(r, &bank->res->children, sibling) { |
84 | 0 | start = PAGE_ALIGN(r_prev->end + 1); |
85 | 0 | end = r->start - 1; |
86 | 0 | r_prev = r; |
87 | 0 | if (start >= end) |
88 | 0 | continue; |
89 | | |
90 | 0 | size = PAGE_ALIGN_DOWN(end - start + 1); |
91 | 0 | if (size == 0) |
92 | 0 | continue; |
93 | 0 | ret = alloc_memtest_region(list, start, size); |
94 | 0 | if (ret < 0) |
95 | 0 | return ret; |
96 | 0 | } |
97 | | |
98 | | /* |
99 | | * Do on head element for bank boundary. |
100 | | */ |
101 | 0 | r = list_last_entry(&bank->res->children, |
102 | 0 | struct resource, sibling); |
103 | 0 | start = PAGE_ALIGN(r->end); |
104 | 0 | end = bank->res->end; |
105 | 0 | size = PAGE_ALIGN_DOWN(end - start + 1); |
106 | 0 | if (size && start < end && start > r->end) { |
107 | 0 | ret = alloc_memtest_region(list, start, size); |
108 | 0 | if (ret < 0) |
109 | 0 | return ret; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | 0 | return 0; |
114 | 0 | } |
115 | | |
116 | | void mem_test_release_regions(struct list_head *list) |
117 | 0 | { |
118 | 0 | struct mem_test_resource *r, *r_tmp; |
119 | |
|
120 | 0 | list_for_each_entry_safe(r, r_tmp, list, list) { |
121 | | /* |
122 | | * Ensure to leave with a cached on non used sdram regions. |
123 | | */ |
124 | 0 | remap_range((void *)r->r->start, resource_size(r->r), |
125 | 0 | MAP_DEFAULT); |
126 | |
|
127 | 0 | release_sdram_region(r->r); |
128 | 0 | free(r); |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | struct mem_test_resource *mem_test_biggest_region(struct list_head *list) |
133 | 0 | { |
134 | 0 | struct mem_test_resource *r, *best = NULL; |
135 | 0 | resource_size_t size = 0; |
136 | |
|
137 | 0 | list_for_each_entry(r, list, list) { |
138 | 0 | resource_size_t now = resource_size(r->r); |
139 | 0 | if (now > size) { |
140 | 0 | size = now; |
141 | 0 | best = r; |
142 | 0 | } |
143 | 0 | } |
144 | |
|
145 | 0 | return best; |
146 | 0 | } |
147 | | |
148 | | static void mem_test_report_failure(const char *failure_description, |
149 | | resource_size_t expected_value, |
150 | | resource_size_t actual_value, |
151 | | volatile resource_size_t *address) |
152 | 0 | { |
153 | | /* |
154 | | * expected_value and actual_value below are not really |
155 | | * pointers, but we want them to be printed exactly the same |
156 | | * as pointers would, so we use %pa regardless |
157 | | */ |
158 | 0 | printf("FAILURE (%s): " |
159 | 0 | "expected %pa, actual %pa at address %pa.\n", |
160 | 0 | failure_description, &expected_value, &actual_value, |
161 | 0 | &address); |
162 | 0 | } |
163 | | |
164 | | int mem_test_bus_integrity(resource_size_t _start, |
165 | | resource_size_t _end, unsigned int flags) |
166 | 0 | { |
167 | 0 | static const uint64_t bitpattern[] = { |
168 | 0 | 0x0000000000000001ULL, /* single bit */ |
169 | 0 | 0x0000000000000003ULL, /* two adjacent bits */ |
170 | 0 | 0x0000000000000007ULL, /* three adjacent bits */ |
171 | 0 | 0x000000000000000FULL, /* four adjacent bits */ |
172 | 0 | 0x0000000000000005ULL, /* two non-adjacent bits */ |
173 | 0 | 0x0000000000000015ULL, /* three non-adjacent bits */ |
174 | 0 | 0x0000000000000055ULL, /* four non-adjacent bits */ |
175 | 0 | 0xAAAAAAAAAAAAAAAAULL, /* alternating 1/0 */ |
176 | 0 | }; |
177 | |
|
178 | 0 | volatile resource_size_t *start, *dummy, num_words, val, readback, offset, |
179 | 0 | offset2, pattern, temp, anti_pattern; |
180 | 0 | int i; |
181 | |
|
182 | 0 | _start = ALIGN(_start, sizeof(resource_size_t)); |
183 | 0 | _end = ALIGN_DOWN(_end, sizeof(resource_size_t)) - 1; |
184 | |
|
185 | 0 | if (_end <= _start) |
186 | 0 | return -EINVAL; |
187 | | |
188 | 0 | start = (resource_size_t *)_start; |
189 | | /* |
190 | | * Point the dummy to start[1] |
191 | | */ |
192 | 0 | dummy = start + 1; |
193 | 0 | num_words = (_end - _start + 1)/sizeof(resource_size_t); |
194 | |
|
195 | 0 | if (flags & MEMTEST_VERBOSE) |
196 | 0 | printf("Starting data line test.\n"); |
197 | | |
198 | | /* |
199 | | * Data line test: write a pattern to the first |
200 | | * location, write the 1's complement to a 'parking' |
201 | | * address (changes the state of the data bus so a |
202 | | * floating bus doesn't give a false OK), and then |
203 | | * read the value back. Note that we read it back |
204 | | * into a variable because the next time we read it, |
205 | | * it might be right (been there, tough to explain to |
206 | | * the quality guys why it prints a failure when the |
207 | | * "is" and "should be" are obviously the same in the |
208 | | * error message). |
209 | | * |
210 | | * Rather than exhaustively testing, we test some |
211 | | * patterns by shifting '1' bits through a field of |
212 | | * '0's and '0' bits through a field of '1's (i.e. |
213 | | * pattern and ~pattern). |
214 | | */ |
215 | 0 | for (i = 0; i < ARRAY_SIZE(bitpattern); i++) { |
216 | 0 | val = (resource_size_t)bitpattern[i]; |
217 | |
|
218 | 0 | for (; val != 0; val <<= 1) { |
219 | 0 | *start = val; |
220 | | /* clear the test data off of the bus */ |
221 | 0 | *dummy = ~val; |
222 | 0 | readback = *start; |
223 | 0 | if (readback != val) { |
224 | 0 | mem_test_report_failure("data line", |
225 | 0 | val, readback, start); |
226 | 0 | return -EIO; |
227 | 0 | } |
228 | | |
229 | 0 | *start = ~val; |
230 | 0 | *dummy = val; |
231 | 0 | readback = *start; |
232 | 0 | if (readback != ~val) { |
233 | 0 | mem_test_report_failure("data line", |
234 | 0 | ~val, readback, start); |
235 | 0 | return -EIO; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | | |
241 | | /* |
242 | | * Based on code whose Original Author and Copyright |
243 | | * information follows: Copyright (c) 1998 by Michael |
244 | | * Barr. This software is placed into the public |
245 | | * domain and may be used for any purpose. However, |
246 | | * this notice must not be changed or removed and no |
247 | | * warranty is either expressed or implied by its |
248 | | * publication or distribution. |
249 | | */ |
250 | | |
251 | | /* |
252 | | * Address line test |
253 | | * |
254 | | * Description: Test the address bus wiring in a |
255 | | * memory region by performing a walking |
256 | | * 1's test on the relevant bits of the |
257 | | * address and checking for aliasing. |
258 | | * This test will find single-bit |
259 | | * address failures such as stuck -high, |
260 | | * stuck-low, and shorted pins. The base |
261 | | * address and size of the region are |
262 | | * selected by the caller. |
263 | | * |
264 | | * Notes: For best results, the selected base |
265 | | * address should have enough LSB 0's to |
266 | | * guarantee single address bit changes. |
267 | | * For example, to test a 64-Kbyte |
268 | | * region, select a base address on a |
269 | | * 64-Kbyte boundary. Also, select the |
270 | | * region size as a power-of-two if at |
271 | | * all possible. |
272 | | * |
273 | | * ## NOTE ## Be sure to specify start and end |
274 | | * addresses such that num_words has |
275 | | * lots of bits set. For example an |
276 | | * address range of 01000000 02000000 is |
277 | | * bad while a range of 01000000 |
278 | | * 01ffffff is perfect. |
279 | | */ |
280 | | |
281 | 0 | pattern = (resource_size_t)0xAAAAAAAAAAAAAAAAULL; |
282 | 0 | anti_pattern = (resource_size_t)0x5555555555555555ULL; |
283 | | |
284 | | /* |
285 | | * Write the default pattern at each of the |
286 | | * power-of-two offsets. |
287 | | */ |
288 | 0 | for (offset = 1; offset <= num_words; offset <<= 1) |
289 | 0 | start[offset] = pattern; |
290 | | |
291 | | /* |
292 | | * Now write anti-pattern at offset 0. If during the previous |
293 | | * step one of the address lines got stuck high this |
294 | | * operation would result in a memory cell at power-of-two |
295 | | * offset being set to anti-pattern which hopefully would be |
296 | | * detected byt the loop that follows. |
297 | | */ |
298 | 0 | start[0] = anti_pattern; |
299 | |
|
300 | 0 | if (flags & MEMTEST_VERBOSE) |
301 | 0 | printf("Check for address bits stuck high.\n"); |
302 | | |
303 | | /* |
304 | | * Check for address bits stuck high. |
305 | | */ |
306 | 0 | for (offset = 1; offset <= num_words; offset <<= 1) { |
307 | 0 | temp = start[offset]; |
308 | 0 | if (temp != pattern) { |
309 | 0 | mem_test_report_failure("address bit stuck high", |
310 | 0 | pattern, temp, &start[offset]); |
311 | 0 | return -EIO; |
312 | 0 | } |
313 | 0 | } |
314 | | |
315 | | /* |
316 | | Restore original value |
317 | | */ |
318 | 0 | start[0] = pattern; |
319 | |
|
320 | 0 | if (flags & MEMTEST_VERBOSE) |
321 | 0 | printf("Check for address bits stuck low or shorted.\n"); |
322 | | |
323 | | /* |
324 | | * Check for address bits stuck low or shorted. |
325 | | */ |
326 | 0 | for (offset2 = 1; offset2 <= num_words; offset2 <<= 1) { |
327 | 0 | start[offset2] = anti_pattern; |
328 | |
|
329 | 0 | for (offset = 0; offset <= num_words; |
330 | 0 | offset = (offset) ? offset << 1 : 1) { |
331 | 0 | temp = start[offset]; |
332 | |
|
333 | 0 | if ((temp != pattern) && |
334 | 0 | (offset != offset2)) { |
335 | 0 | mem_test_report_failure( |
336 | 0 | "address bit stuck low or shorted", |
337 | 0 | pattern, temp, &start[offset]); |
338 | 0 | return -EIO; |
339 | 0 | } |
340 | 0 | } |
341 | 0 | start[offset2] = pattern; |
342 | 0 | } |
343 | | |
344 | 0 | return 0; |
345 | 0 | } |
346 | | |
347 | | static int update_progress(resource_size_t offset, unsigned flags) |
348 | 0 | { |
349 | | /* Only check every 4k to reduce overhead */ |
350 | 0 | if (offset & (SZ_4K - 1)) |
351 | 0 | return 0; |
352 | | |
353 | 0 | if (ctrlc()) |
354 | 0 | return -EINTR; |
355 | | |
356 | 0 | if (flags & MEMTEST_VERBOSE) |
357 | 0 | show_progress(offset); |
358 | |
|
359 | 0 | return 0; |
360 | 0 | } |
361 | | |
362 | | int mem_test_moving_inversions(resource_size_t _start, resource_size_t _end, |
363 | | unsigned flags) |
364 | 0 | { |
365 | 0 | volatile resource_size_t *start, num_words, offset, temp, anti_pattern; |
366 | 0 | int ret; |
367 | |
|
368 | 0 | _start = ALIGN(_start, sizeof(resource_size_t)); |
369 | 0 | _end = ALIGN_DOWN(_end, sizeof(resource_size_t)) - 1; |
370 | |
|
371 | 0 | if (_end <= _start) |
372 | 0 | return -EINVAL; |
373 | | |
374 | 0 | start = (resource_size_t *)_start; |
375 | 0 | num_words = (_end - _start + 1)/sizeof(resource_size_t); |
376 | |
|
377 | 0 | if (flags & MEMTEST_VERBOSE) { |
378 | 0 | printf("Starting moving inversions test of RAM:\n" |
379 | 0 | "Fill with address, compare, fill with inverted address, compare again\n"); |
380 | |
|
381 | 0 | init_progression_bar(3 * num_words); |
382 | 0 | } |
383 | | |
384 | | /* |
385 | | * Description: Test the integrity of a physical |
386 | | * memory device by performing an |
387 | | * increment/decrement test over the |
388 | | * entire region. In the process every |
389 | | * storage bit in the device is tested |
390 | | * as a zero and a one. The base address |
391 | | * and the size of the region are |
392 | | * selected by the caller. |
393 | | */ |
394 | | |
395 | | /* Fill memory with a known pattern */ |
396 | 0 | for (offset = 0; offset < num_words; offset++) { |
397 | 0 | ret = update_progress(offset, flags); |
398 | 0 | if (ret) |
399 | 0 | return ret; |
400 | 0 | start[offset] = offset + 1; |
401 | 0 | } |
402 | | |
403 | | /* Check each location and invert it for the second pass */ |
404 | 0 | for (offset = 0; offset < num_words; offset++) { |
405 | 0 | ret = update_progress(num_words + offset, flags); |
406 | 0 | if (ret) |
407 | 0 | return ret; |
408 | | |
409 | 0 | temp = start[offset]; |
410 | 0 | if (temp != (offset + 1)) { |
411 | 0 | printf("\n"); |
412 | 0 | mem_test_report_failure("read/write", |
413 | 0 | (offset + 1), |
414 | 0 | temp, &start[offset]); |
415 | 0 | return -EIO; |
416 | 0 | } |
417 | | |
418 | 0 | anti_pattern = ~(offset + 1); |
419 | 0 | start[offset] = anti_pattern; |
420 | 0 | } |
421 | | |
422 | | /* Check each location for the inverted pattern and zero it */ |
423 | 0 | for (offset = 0; offset < num_words; offset++) { |
424 | 0 | ret = update_progress(2 * num_words + offset, flags); |
425 | 0 | if (ret) |
426 | 0 | return ret; |
427 | | |
428 | 0 | anti_pattern = ~(offset + 1); |
429 | 0 | temp = start[offset]; |
430 | |
|
431 | 0 | if (temp != anti_pattern) { |
432 | 0 | printf("\n"); |
433 | 0 | mem_test_report_failure("read/write", |
434 | 0 | anti_pattern, |
435 | 0 | temp, &start[offset]); |
436 | 0 | return -EIO; |
437 | 0 | } |
438 | | |
439 | 0 | start[offset] = 0; |
440 | 0 | } |
441 | 0 | if (flags & MEMTEST_VERBOSE) { |
442 | 0 | show_progress(3 * num_words); |
443 | | |
444 | | /* end of progressbar */ |
445 | 0 | printf("\n"); |
446 | 0 | } |
447 | |
|
448 | 0 | return 0; |
449 | 0 | } |