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 | * |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 77 | * @param start System memory offset, aligned to 128bytes |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 78 | */ |
| 79 | static int ram_bitset_nodie(unsigned long start) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 80 | { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 81 | unsigned long addr, value, value2; |
| 82 | unsigned short int idx; |
| 83 | unsigned char failed, failures; |
| 84 | uint8_t verbose = 0; |
| 85 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 86 | printk(BIOS_DEBUG, "DRAM bitset write: 0x%08lx\n", start); |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 87 | for (idx = 0; idx < 0x400; idx += 4) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 88 | test_pattern(idx, &addr, &value); |
| 89 | write_phys(start + addr, value); |
| 90 | } |
| 91 | |
| 92 | /* Make sure we don't read before we wrote */ |
| 93 | phys_memory_barrier(); |
| 94 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 95 | printk(BIOS_DEBUG, "DRAM bitset verify: 0x%08lx\n", start); |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 96 | failures = 0; |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 97 | for (idx = 0; idx < 0x400; idx += 4) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 98 | test_pattern(idx, &addr, &value); |
| 99 | value2 = read_phys(start + addr); |
| 100 | |
| 101 | failed = (value2 != value); |
| 102 | failures |= failed; |
| 103 | if (failed && !verbose) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 104 | printk(BIOS_ERR, "0x%08lx wr: 0x%08lx rd: 0x%08lx FAIL\n", |
| 105 | start + addr, value, value2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 106 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 107 | if (verbose) { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 108 | if ((addr & 0x0f) == 0) |
| 109 | printk(BIOS_DEBUG, "%08lx wr: %08lx rd:", |
| 110 | start + addr, value); |
| 111 | if (failed) |
| 112 | printk(BIOS_DEBUG, " %08lx!", value2); |
| 113 | else |
| 114 | printk(BIOS_DEBUG, " %08lx ", value2); |
| 115 | if ((addr & 0x0f) == 0xc) |
| 116 | printk(BIOS_DEBUG, "\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 119 | if (failures) { |
| 120 | post_code(0xea); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 121 | printk(BIOS_DEBUG, "\nDRAM did _NOT_ verify!\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 122 | return 1; |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 123 | } |
Lee Leahy | 3e1cab4 | 2017-03-10 17:48:31 -0800 | [diff] [blame] | 124 | printk(BIOS_DEBUG, "\nDRAM range verified.\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 125 | return 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 129 | void ram_check(unsigned long start, unsigned long stop) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 130 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 131 | /* |
| 132 | * This is much more of a "Is my DRAM properly configured?" |
| 133 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 134 | * are tested. -Tyson |
| 135 | */ |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 136 | printk(BIOS_DEBUG, "Testing DRAM at: %08lx\n", start); |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 137 | if (ram_bitset_nodie(start)) |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 138 | die("DRAM ERROR"); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 139 | printk(BIOS_DEBUG, "Done.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 142 | |
| 143 | int ram_check_nodie(unsigned long start, unsigned long stop) |
| 144 | { |
| 145 | int ret; |
| 146 | /* |
| 147 | * This is much more of a "Is my DRAM properly configured?" |
| 148 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 149 | * are tested. -Tyson |
| 150 | */ |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 151 | printk(BIOS_DEBUG, "Testing DRAM at : %08lx\n", start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 152 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 153 | ret = ram_bitset_nodie(start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 154 | printk(BIOS_DEBUG, "Done.\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 158 | int ram_check_noprint_nodie(unsigned long start, unsigned long stop) |
| 159 | { |
| 160 | unsigned long addr, value, value2; |
| 161 | unsigned short int idx; |
| 162 | unsigned char failed, failures; |
| 163 | |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 164 | for (idx = 0; idx < 0x400; idx += 4) { |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 165 | test_pattern(idx, &addr, &value); |
| 166 | write_phys(start + addr, value); |
| 167 | } |
| 168 | |
| 169 | /* Make sure we don't read before we wrote */ |
| 170 | phys_memory_barrier(); |
| 171 | |
| 172 | failures = 0; |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 173 | for (idx = 0; idx < 0x400; idx += 4) { |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 174 | test_pattern(idx, &addr, &value); |
| 175 | value2 = read_phys(start + addr); |
| 176 | |
| 177 | failed = (value2 != value); |
| 178 | failures |= failed; |
| 179 | } |
| 180 | return failures; |
| 181 | } |
| 182 | |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 183 | static void __quick_ram_check(uintptr_t dst) |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 184 | { |
| 185 | int fail = 0; |
| 186 | u32 backup; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 187 | backup = read_phys(dst); |
| 188 | write_phys(dst, 0x55555555); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 189 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 190 | if (read_phys(dst) != 0x55555555) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 191 | fail = 1; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 192 | write_phys(dst, 0xaaaaaaaa); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 193 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 194 | if (read_phys(dst) != 0xaaaaaaaa) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 195 | fail = 1; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 196 | write_phys(dst, 0x00000000); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 197 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 198 | if (read_phys(dst) != 0x00000000) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 199 | fail = 1; |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 200 | write_phys(dst, 0xffffffff); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 201 | phys_memory_barrier(); |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 202 | if (read_phys(dst) != 0xffffffff) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 203 | fail = 1; |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 204 | |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 205 | write_phys(dst, backup); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 206 | if (fail) { |
| 207 | post_code(0xea); |
| 208 | die("RAM INIT FAILURE!\n"); |
| 209 | } |
| 210 | phys_memory_barrier(); |
| 211 | } |
Kyösti Mälkki | 19652e6 | 2016-06-17 23:31:42 +0300 | [diff] [blame] | 212 | |
| 213 | void quick_ram_check(void) |
| 214 | { |
| 215 | __quick_ram_check(0x100000); |
| 216 | } |