Patrick Georgi | d083595 | 2010-10-05 09:07:10 +0000 | [diff] [blame] | 1 | #include <stdint.h> |
Myles Watson | 3426195 | 2010-03-19 02:33:40 +0000 | [diff] [blame] | 2 | #include <lib.h> /* Prototypes */ |
Patrick Georgi | d083595 | 2010-10-05 09:07:10 +0000 | [diff] [blame] | 3 | #include <console/console.h> |
Myles Watson | 3426195 | 2010-03-19 02:33:40 +0000 | [diff] [blame] | 4 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 5 | static void write_phys(unsigned long addr, u32 value) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | { |
Stefan Reinauer | a7acc51 | 2010-02-25 13:40:49 +0000 | [diff] [blame] | 7 | // Assembler in lib/ is very ugly. But we properly guarded |
| 8 | // it so let's obey this one for now |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 9 | #if IS_ENABLED(CONFIG_SSE2) |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 10 | asm volatile( |
| 11 | "movnti %1, (%0)" |
| 12 | : /* outputs */ |
| 13 | : "r" (addr), "r" (value) /* inputs */ |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 14 | #ifndef __GNUC__ /* GCC does not like empty clobbers? */ |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 15 | : /* clobbers */ |
Stefan Reinauer | 7671293 | 2004-05-27 11:13:24 +0000 | [diff] [blame] | 16 | #endif |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 17 | ); |
| 18 | #else |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 19 | volatile unsigned long *ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 20 | ptr = (void *)addr; |
| 21 | *ptr = value; |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 22 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 25 | static u32 read_phys(unsigned long addr) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 26 | { |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 27 | volatile unsigned long *ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | ptr = (void *)addr; |
| 29 | return *ptr; |
| 30 | } |
| 31 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 32 | static void phys_memory_barrier(void) |
| 33 | { |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 34 | #if IS_ENABLED(CONFIG_SSE2) |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 35 | // Needed for movnti |
| 36 | asm volatile ( |
| 37 | "sfence" |
| 38 | :: |
| 39 | #ifdef __GNUC__ /* ROMCC does not like memory clobbers */ |
| 40 | : "memory" |
| 41 | #endif |
| 42 | ); |
| 43 | #else |
| 44 | #ifdef __GNUC__ /* ROMCC does not like empty asm statements */ |
| 45 | asm volatile ("" ::: "memory"); |
| 46 | #endif |
| 47 | #endif |
| 48 | } |
| 49 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 50 | /** |
| 51 | * Rotate ones test pattern that access every bit on a 128bit wide |
| 52 | * memory bus. To test most address lines, addresses are scattered |
| 53 | * using 256B, 4kB and 64kB increments. |
| 54 | * |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 55 | * @param idx Index to test pattern (0=<idx<0x400) |
| 56 | * @param addr Memory to access on idx |
| 57 | * @param value Value to write or read at addr |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 58 | */ |
| 59 | static inline void test_pattern(unsigned short int idx, |
| 60 | unsigned long *addr, unsigned long *value) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 61 | { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 62 | uint8_t j, k; |
| 63 | |
| 64 | k = (idx >> 8) + 1; |
| 65 | j = (idx >> 4) & 0x0f; |
| 66 | *addr = idx & 0x0f; |
| 67 | *addr |= j << (4*k); |
| 68 | *value = 0x01010101 << (j & 7); |
| 69 | if (j & 8) |
| 70 | *value = ~(*value); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 73 | /** |
| 74 | * Simple write-read-verify memory test. See console debug output for |
| 75 | * any dislocated bytes. |
| 76 | * |
Patrick Rudolph | 3dbd284 | 2017-10-27 11:49:29 +0200 | [diff] [blame] | 77 | * Tests 1MiB of memory starting from start. |
| 78 | * |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 79 | * @param start System memory offset, aligned to 128bytes |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 80 | */ |
| 81 | static int ram_bitset_nodie(unsigned long start) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 82 | { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 83 | unsigned long addr, value, value2; |
| 84 | unsigned short int idx; |
| 85 | unsigned char failed, failures; |
| 86 | uint8_t verbose = 0; |
| 87 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 88 | printk(BIOS_DEBUG, "DRAM bitset write: 0x%08lx\n", start); |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 89 | for (idx = 0; idx < 0x400; idx += 4) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 90 | test_pattern(idx, &addr, &value); |
| 91 | write_phys(start + addr, value); |
| 92 | } |
| 93 | |
| 94 | /* Make sure we don't read before we wrote */ |
| 95 | phys_memory_barrier(); |
| 96 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 97 | printk(BIOS_DEBUG, "DRAM bitset verify: 0x%08lx\n", start); |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 98 | failures = 0; |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 99 | for (idx = 0; idx < 0x400; idx += 4) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 100 | test_pattern(idx, &addr, &value); |
| 101 | value2 = read_phys(start + addr); |
| 102 | |
| 103 | failed = (value2 != value); |
| 104 | failures |= failed; |
Elyes HAOUAS | 1943f37 | 2018-05-04 16:30:39 +0200 | [diff] [blame] | 105 | if (failed && !verbose) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 106 | printk(BIOS_ERR, "0x%08lx wr: 0x%08lx rd: 0x%08lx FAIL\n", |
| 107 | start + addr, value, value2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 108 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 109 | if (verbose) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 110 | if ((addr & 0x0f) == 0) |
| 111 | printk(BIOS_DEBUG, "%08lx wr: %08lx rd:", |
| 112 | start + addr, value); |
| 113 | if (failed) |
| 114 | printk(BIOS_DEBUG, " %08lx!", value2); |
| 115 | else |
| 116 | printk(BIOS_DEBUG, " %08lx ", value2); |
| 117 | if ((addr & 0x0f) == 0xc) |
| 118 | printk(BIOS_DEBUG, "\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 121 | if (failures) { |
| 122 | post_code(0xea); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 123 | printk(BIOS_DEBUG, "\nDRAM did _NOT_ verify!\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 124 | return 1; |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 125 | } |
Lee Leahy | 3e1cab4 | 2017-03-10 17:48:31 -0800 | [diff] [blame] | 126 | printk(BIOS_DEBUG, "\nDRAM range verified.\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 127 | return 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 131 | void ram_check(unsigned long start, unsigned long stop) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 132 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 133 | /* |
| 134 | * This is much more of a "Is my DRAM properly configured?" |
| 135 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 136 | * are tested. -Tyson |
| 137 | */ |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 138 | printk(BIOS_DEBUG, "Testing DRAM at: %08lx\n", start); |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 139 | if (ram_bitset_nodie(start)) |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 140 | die("DRAM ERROR"); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 141 | printk(BIOS_DEBUG, "Done.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 144 | |
| 145 | int ram_check_nodie(unsigned long start, unsigned long stop) |
| 146 | { |
| 147 | int ret; |
| 148 | /* |
| 149 | * This is much more of a "Is my DRAM properly configured?" |
| 150 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 151 | * are tested. -Tyson |
| 152 | */ |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 153 | printk(BIOS_DEBUG, "Testing DRAM at : %08lx\n", start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 154 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 155 | ret = ram_bitset_nodie(start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 156 | printk(BIOS_DEBUG, "Done.\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 157 | return ret; |
| 158 | } |
| 159 | |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 160 | int ram_check_noprint_nodie(unsigned long start, unsigned long stop) |
| 161 | { |
| 162 | unsigned long addr, value, value2; |
| 163 | unsigned short int idx; |
| 164 | unsigned char failed, failures; |
| 165 | |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 166 | for (idx = 0; idx < 0x400; idx += 4) { |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 167 | test_pattern(idx, &addr, &value); |
| 168 | write_phys(start + addr, value); |
| 169 | } |
| 170 | |
| 171 | /* Make sure we don't read before we wrote */ |
| 172 | phys_memory_barrier(); |
| 173 | |
| 174 | failures = 0; |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 175 | for (idx = 0; idx < 0x400; idx += 4) { |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 176 | test_pattern(idx, &addr, &value); |
| 177 | value2 = read_phys(start + addr); |
| 178 | |
| 179 | failed = (value2 != value); |
| 180 | failures |= failed; |
| 181 | } |
| 182 | return failures; |
| 183 | } |
| 184 | |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 185 | static void __quick_ram_check(uintptr_t dst) |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 186 | { |
| 187 | int fail = 0; |
| 188 | u32 backup; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 189 | backup = read_phys(dst); |
| 190 | write_phys(dst, 0x55555555); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 191 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 192 | if (read_phys(dst) != 0x55555555) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 193 | fail = 1; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 194 | write_phys(dst, 0xaaaaaaaa); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 195 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 196 | if (read_phys(dst) != 0xaaaaaaaa) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 197 | fail = 1; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 198 | write_phys(dst, 0x00000000); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 199 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 200 | if (read_phys(dst) != 0x00000000) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 201 | fail = 1; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 202 | write_phys(dst, 0xffffffff); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 203 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 204 | if (read_phys(dst) != 0xffffffff) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 205 | fail = 1; |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 206 | |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 207 | write_phys(dst, backup); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 208 | if (fail) { |
| 209 | post_code(0xea); |
| 210 | die("RAM INIT FAILURE!\n"); |
| 211 | } |
| 212 | phys_memory_barrier(); |
| 213 | } |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 214 | |
| 215 | void quick_ram_check(void) |
| 216 | { |
| 217 | __quick_ram_check(0x100000); |
| 218 | } |