]>
Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
2bc65418 | 2 | #include "amd64_edac.h" |
23ac4ae8 | 3 | #include <asm/amd_nb.h> |
2bc65418 | 4 | |
d1ea71cd | 5 | static struct edac_pci_ctl_info *pci_ctl; |
2bc65418 | 6 | |
2bc65418 DT |
7 | /* |
8 | * Set by command line parameter. If BIOS has enabled the ECC, this override is | |
9 | * cleared to prevent re-enabling the hardware by this driver. | |
10 | */ | |
11 | static int ecc_enable_override; | |
12 | module_param(ecc_enable_override, int, 0644); | |
13 | ||
a29d8b8e | 14 | static struct msr __percpu *msrs; |
50542251 | 15 | |
38ddd4d1 YG |
16 | static struct amd64_family_type *fam_type; |
17 | ||
2ec591ac | 18 | /* Per-node stuff */ |
ae7bb7c6 | 19 | static struct ecc_settings **ecc_stngs; |
2bc65418 | 20 | |
706657b1 BP |
21 | /* Device for the PCI component */ |
22 | static struct device *pci_ctl_dev; | |
23 | ||
b70ef010 BP |
24 | /* |
25 | * Valid scrub rates for the K8 hardware memory scrubber. We map the scrubbing | |
26 | * bandwidth to a valid bit pattern. The 'set' operation finds the 'matching- | |
27 | * or higher value'. | |
28 | * | |
29 | *FIXME: Produce a better mapping/linearisation. | |
30 | */ | |
c7e5301a | 31 | static const struct scrubrate { |
39094443 BP |
32 | u32 scrubval; /* bit pattern for scrub rate */ |
33 | u32 bandwidth; /* bandwidth consumed (bytes/sec) */ | |
34 | } scrubrates[] = { | |
b70ef010 BP |
35 | { 0x01, 1600000000UL}, |
36 | { 0x02, 800000000UL}, | |
37 | { 0x03, 400000000UL}, | |
38 | { 0x04, 200000000UL}, | |
39 | { 0x05, 100000000UL}, | |
40 | { 0x06, 50000000UL}, | |
41 | { 0x07, 25000000UL}, | |
42 | { 0x08, 12284069UL}, | |
43 | { 0x09, 6274509UL}, | |
44 | { 0x0A, 3121951UL}, | |
45 | { 0x0B, 1560975UL}, | |
46 | { 0x0C, 781440UL}, | |
47 | { 0x0D, 390720UL}, | |
48 | { 0x0E, 195300UL}, | |
49 | { 0x0F, 97650UL}, | |
50 | { 0x10, 48854UL}, | |
51 | { 0x11, 24427UL}, | |
52 | { 0x12, 12213UL}, | |
53 | { 0x13, 6101UL}, | |
54 | { 0x14, 3051UL}, | |
55 | { 0x15, 1523UL}, | |
56 | { 0x16, 761UL}, | |
57 | { 0x00, 0UL}, /* scrubbing off */ | |
58 | }; | |
59 | ||
66fed2d4 BP |
60 | int __amd64_read_pci_cfg_dword(struct pci_dev *pdev, int offset, |
61 | u32 *val, const char *func) | |
b2b0c605 BP |
62 | { |
63 | int err = 0; | |
64 | ||
65 | err = pci_read_config_dword(pdev, offset, val); | |
66 | if (err) | |
67 | amd64_warn("%s: error reading F%dx%03x.\n", | |
68 | func, PCI_FUNC(pdev->devfn), offset); | |
69 | ||
70 | return err; | |
71 | } | |
72 | ||
73 | int __amd64_write_pci_cfg_dword(struct pci_dev *pdev, int offset, | |
74 | u32 val, const char *func) | |
75 | { | |
76 | int err = 0; | |
77 | ||
78 | err = pci_write_config_dword(pdev, offset, val); | |
79 | if (err) | |
80 | amd64_warn("%s: error writing to F%dx%03x.\n", | |
81 | func, PCI_FUNC(pdev->devfn), offset); | |
82 | ||
83 | return err; | |
84 | } | |
85 | ||
7981a28f AG |
86 | /* |
87 | * Select DCT to which PCI cfg accesses are routed | |
88 | */ | |
89 | static void f15h_select_dct(struct amd64_pvt *pvt, u8 dct) | |
90 | { | |
91 | u32 reg = 0; | |
92 | ||
93 | amd64_read_pci_cfg(pvt->F1, DCT_CFG_SEL, ®); | |
94 | reg &= (pvt->model == 0x30) ? ~3 : ~1; | |
95 | reg |= dct; | |
96 | amd64_write_pci_cfg(pvt->F1, DCT_CFG_SEL, reg); | |
97 | } | |
98 | ||
b2b0c605 BP |
99 | /* |
100 | * | |
101 | * Depending on the family, F2 DCT reads need special handling: | |
102 | * | |
7981a28f | 103 | * K8: has a single DCT only and no address offsets >= 0x100 |
b2b0c605 BP |
104 | * |
105 | * F10h: each DCT has its own set of regs | |
106 | * DCT0 -> F2x040.. | |
107 | * DCT1 -> F2x140.. | |
108 | * | |
94c1acf2 | 109 | * F16h: has only 1 DCT |
7981a28f AG |
110 | * |
111 | * F15h: we select which DCT we access using F1x10C[DctCfgSel] | |
b2b0c605 | 112 | */ |
7981a28f AG |
113 | static inline int amd64_read_dct_pci_cfg(struct amd64_pvt *pvt, u8 dct, |
114 | int offset, u32 *val) | |
b2b0c605 | 115 | { |
7981a28f AG |
116 | switch (pvt->fam) { |
117 | case 0xf: | |
118 | if (dct || offset >= 0x100) | |
119 | return -EINVAL; | |
120 | break; | |
b2b0c605 | 121 | |
7981a28f AG |
122 | case 0x10: |
123 | if (dct) { | |
124 | /* | |
125 | * Note: If ganging is enabled, barring the regs | |
126 | * F2x[1,0]98 and F2x[1,0]9C; reads reads to F2x1xx | |
127 | * return 0. (cf. Section 2.8.1 F10h BKDG) | |
128 | */ | |
129 | if (dct_ganging_enabled(pvt)) | |
130 | return 0; | |
b2b0c605 | 131 | |
7981a28f AG |
132 | offset += 0x100; |
133 | } | |
134 | break; | |
73ba8593 | 135 | |
7981a28f AG |
136 | case 0x15: |
137 | /* | |
138 | * F15h: F2x1xx addresses do not map explicitly to DCT1. | |
139 | * We should select which DCT we access using F1x10C[DctCfgSel] | |
140 | */ | |
141 | dct = (dct && pvt->model == 0x30) ? 3 : dct; | |
142 | f15h_select_dct(pvt, dct); | |
143 | break; | |
73ba8593 | 144 | |
7981a28f AG |
145 | case 0x16: |
146 | if (dct) | |
147 | return -EINVAL; | |
148 | break; | |
b2b0c605 | 149 | |
7981a28f AG |
150 | default: |
151 | break; | |
b2b0c605 | 152 | } |
7981a28f | 153 | return amd64_read_pci_cfg(pvt->F2, offset, val); |
b2b0c605 BP |
154 | } |
155 | ||
2bc65418 DT |
156 | /* |
157 | * Memory scrubber control interface. For K8, memory scrubbing is handled by | |
158 | * hardware and can involve L2 cache, dcache as well as the main memory. With | |
159 | * F10, this is extended to L3 cache scrubbing on CPU models sporting that | |
160 | * functionality. | |
161 | * | |
162 | * This causes the "units" for the scrubbing speed to vary from 64 byte blocks | |
163 | * (dram) over to cache lines. This is nasty, so we will use bandwidth in | |
164 | * bytes/sec for the setting. | |
165 | * | |
166 | * Currently, we only do dram scrubbing. If the scrubbing is done in software on | |
167 | * other archs, we might not have access to the caches directly. | |
168 | */ | |
169 | ||
8051c0af YG |
170 | static inline void __f17h_set_scrubval(struct amd64_pvt *pvt, u32 scrubval) |
171 | { | |
172 | /* | |
173 | * Fam17h supports scrub values between 0x5 and 0x14. Also, the values | |
174 | * are shifted down by 0x5, so scrubval 0x5 is written to the register | |
175 | * as 0x0, scrubval 0x6 as 0x1, etc. | |
176 | */ | |
177 | if (scrubval >= 0x5 && scrubval <= 0x14) { | |
178 | scrubval -= 0x5; | |
179 | pci_write_bits32(pvt->F6, F17H_SCR_LIMIT_ADDR, scrubval, 0xF); | |
180 | pci_write_bits32(pvt->F6, F17H_SCR_BASE_ADDR, 1, 0x1); | |
181 | } else { | |
182 | pci_write_bits32(pvt->F6, F17H_SCR_BASE_ADDR, 0, 0x1); | |
183 | } | |
184 | } | |
2bc65418 | 185 | /* |
8051c0af | 186 | * Scan the scrub rate mapping table for a close or matching bandwidth value to |
2bc65418 DT |
187 | * issue. If requested is too big, then use last maximum value found. |
188 | */ | |
da92110d | 189 | static int __set_scrub_rate(struct amd64_pvt *pvt, u32 new_bw, u32 min_rate) |
2bc65418 DT |
190 | { |
191 | u32 scrubval; | |
192 | int i; | |
193 | ||
194 | /* | |
195 | * map the configured rate (new_bw) to a value specific to the AMD64 | |
196 | * memory controller and apply to register. Search for the first | |
197 | * bandwidth entry that is greater or equal than the setting requested | |
198 | * and program that. If at last entry, turn off DRAM scrubbing. | |
168bfeef AM |
199 | * |
200 | * If no suitable bandwidth is found, turn off DRAM scrubbing entirely | |
201 | * by falling back to the last element in scrubrates[]. | |
2bc65418 | 202 | */ |
168bfeef | 203 | for (i = 0; i < ARRAY_SIZE(scrubrates) - 1; i++) { |
2bc65418 DT |
204 | /* |
205 | * skip scrub rates which aren't recommended | |
206 | * (see F10 BKDG, F3x58) | |
207 | */ | |
395ae783 | 208 | if (scrubrates[i].scrubval < min_rate) |
2bc65418 DT |
209 | continue; |
210 | ||
211 | if (scrubrates[i].bandwidth <= new_bw) | |
212 | break; | |
2bc65418 DT |
213 | } |
214 | ||
215 | scrubval = scrubrates[i].scrubval; | |
2bc65418 | 216 | |
dcd01394 | 217 | if (pvt->umc) { |
8051c0af YG |
218 | __f17h_set_scrubval(pvt, scrubval); |
219 | } else if (pvt->fam == 0x15 && pvt->model == 0x60) { | |
da92110d AG |
220 | f15h_select_dct(pvt, 0); |
221 | pci_write_bits32(pvt->F2, F15H_M60H_SCRCTRL, scrubval, 0x001F); | |
222 | f15h_select_dct(pvt, 1); | |
223 | pci_write_bits32(pvt->F2, F15H_M60H_SCRCTRL, scrubval, 0x001F); | |
224 | } else { | |
225 | pci_write_bits32(pvt->F3, SCRCTRL, scrubval, 0x001F); | |
226 | } | |
2bc65418 | 227 | |
39094443 BP |
228 | if (scrubval) |
229 | return scrubrates[i].bandwidth; | |
230 | ||
2bc65418 DT |
231 | return 0; |
232 | } | |
233 | ||
d1ea71cd | 234 | static int set_scrub_rate(struct mem_ctl_info *mci, u32 bw) |
2bc65418 DT |
235 | { |
236 | struct amd64_pvt *pvt = mci->pvt_info; | |
87b3e0e6 | 237 | u32 min_scrubrate = 0x5; |
2bc65418 | 238 | |
a4b4bedc | 239 | if (pvt->fam == 0xf) |
87b3e0e6 BP |
240 | min_scrubrate = 0x0; |
241 | ||
da92110d AG |
242 | if (pvt->fam == 0x15) { |
243 | /* Erratum #505 */ | |
244 | if (pvt->model < 0x10) | |
245 | f15h_select_dct(pvt, 0); | |
73ba8593 | 246 | |
da92110d AG |
247 | if (pvt->model == 0x60) |
248 | min_scrubrate = 0x6; | |
249 | } | |
250 | return __set_scrub_rate(pvt, bw, min_scrubrate); | |
2bc65418 DT |
251 | } |
252 | ||
d1ea71cd | 253 | static int get_scrub_rate(struct mem_ctl_info *mci) |
2bc65418 DT |
254 | { |
255 | struct amd64_pvt *pvt = mci->pvt_info; | |
39094443 | 256 | int i, retval = -EINVAL; |
8051c0af | 257 | u32 scrubval = 0; |
2bc65418 | 258 | |
dcd01394 | 259 | if (pvt->umc) { |
8051c0af YG |
260 | amd64_read_pci_cfg(pvt->F6, F17H_SCR_BASE_ADDR, &scrubval); |
261 | if (scrubval & BIT(0)) { | |
262 | amd64_read_pci_cfg(pvt->F6, F17H_SCR_LIMIT_ADDR, &scrubval); | |
263 | scrubval &= 0xF; | |
264 | scrubval += 0x5; | |
265 | } else { | |
266 | scrubval = 0; | |
267 | } | |
dcd01394 YG |
268 | } else if (pvt->fam == 0x15) { |
269 | /* Erratum #505 */ | |
270 | if (pvt->model < 0x10) | |
271 | f15h_select_dct(pvt, 0); | |
8051c0af | 272 | |
dcd01394 YG |
273 | if (pvt->model == 0x60) |
274 | amd64_read_pci_cfg(pvt->F2, F15H_M60H_SCRCTRL, &scrubval); | |
ee470bb2 BP |
275 | else |
276 | amd64_read_pci_cfg(pvt->F3, SCRCTRL, &scrubval); | |
dcd01394 | 277 | } else { |
da92110d | 278 | amd64_read_pci_cfg(pvt->F3, SCRCTRL, &scrubval); |
8051c0af | 279 | } |
2bc65418 DT |
280 | |
281 | scrubval = scrubval & 0x001F; | |
282 | ||
926311fd | 283 | for (i = 0; i < ARRAY_SIZE(scrubrates); i++) { |
2bc65418 | 284 | if (scrubrates[i].scrubval == scrubval) { |
39094443 | 285 | retval = scrubrates[i].bandwidth; |
2bc65418 DT |
286 | break; |
287 | } | |
288 | } | |
39094443 | 289 | return retval; |
2bc65418 DT |
290 | } |
291 | ||
6775763a | 292 | /* |
7f19bf75 BP |
293 | * returns true if the SysAddr given by sys_addr matches the |
294 | * DRAM base/limit associated with node_id | |
6775763a | 295 | */ |
d1ea71cd | 296 | static bool base_limit_match(struct amd64_pvt *pvt, u64 sys_addr, u8 nid) |
6775763a | 297 | { |
7f19bf75 | 298 | u64 addr; |
6775763a DT |
299 | |
300 | /* The K8 treats this as a 40-bit value. However, bits 63-40 will be | |
301 | * all ones if the most significant implemented address bit is 1. | |
302 | * Here we discard bits 63-40. See section 3.4.2 of AMD publication | |
303 | * 24592: AMD x86-64 Architecture Programmer's Manual Volume 1 | |
304 | * Application Programming. | |
305 | */ | |
306 | addr = sys_addr & 0x000000ffffffffffull; | |
307 | ||
7f19bf75 BP |
308 | return ((addr >= get_dram_base(pvt, nid)) && |
309 | (addr <= get_dram_limit(pvt, nid))); | |
6775763a DT |
310 | } |
311 | ||
312 | /* | |
313 | * Attempt to map a SysAddr to a node. On success, return a pointer to the | |
314 | * mem_ctl_info structure for the node that the SysAddr maps to. | |
315 | * | |
316 | * On failure, return NULL. | |
317 | */ | |
318 | static struct mem_ctl_info *find_mc_by_sys_addr(struct mem_ctl_info *mci, | |
319 | u64 sys_addr) | |
320 | { | |
321 | struct amd64_pvt *pvt; | |
c7e5301a | 322 | u8 node_id; |
6775763a DT |
323 | u32 intlv_en, bits; |
324 | ||
325 | /* | |
326 | * Here we use the DRAM Base (section 3.4.4.1) and DRAM Limit (section | |
327 | * 3.4.4.2) registers to map the SysAddr to a node ID. | |
328 | */ | |
329 | pvt = mci->pvt_info; | |
330 | ||
331 | /* | |
332 | * The value of this field should be the same for all DRAM Base | |
333 | * registers. Therefore we arbitrarily choose to read it from the | |
334 | * register for node 0. | |
335 | */ | |
7f19bf75 | 336 | intlv_en = dram_intlv_en(pvt, 0); |
6775763a DT |
337 | |
338 | if (intlv_en == 0) { | |
7f19bf75 | 339 | for (node_id = 0; node_id < DRAM_RANGES; node_id++) { |
d1ea71cd | 340 | if (base_limit_match(pvt, sys_addr, node_id)) |
8edc5445 | 341 | goto found; |
6775763a | 342 | } |
8edc5445 | 343 | goto err_no_match; |
6775763a DT |
344 | } |
345 | ||
72f158fe BP |
346 | if (unlikely((intlv_en != 0x01) && |
347 | (intlv_en != 0x03) && | |
348 | (intlv_en != 0x07))) { | |
24f9a7fe | 349 | amd64_warn("DRAM Base[IntlvEn] junk value: 0x%x, BIOS bug?\n", intlv_en); |
6775763a DT |
350 | return NULL; |
351 | } | |
352 | ||
353 | bits = (((u32) sys_addr) >> 12) & intlv_en; | |
354 | ||
355 | for (node_id = 0; ; ) { | |
7f19bf75 | 356 | if ((dram_intlv_sel(pvt, node_id) & intlv_en) == bits) |
6775763a DT |
357 | break; /* intlv_sel field matches */ |
358 | ||
7f19bf75 | 359 | if (++node_id >= DRAM_RANGES) |
6775763a DT |
360 | goto err_no_match; |
361 | } | |
362 | ||
363 | /* sanity test for sys_addr */ | |
d1ea71cd | 364 | if (unlikely(!base_limit_match(pvt, sys_addr, node_id))) { |
24f9a7fe BP |
365 | amd64_warn("%s: sys_addr 0x%llx falls outside base/limit address" |
366 | "range for node %d with node interleaving enabled.\n", | |
367 | __func__, sys_addr, node_id); | |
6775763a DT |
368 | return NULL; |
369 | } | |
370 | ||
371 | found: | |
b487c33e | 372 | return edac_mc_find((int)node_id); |
6775763a DT |
373 | |
374 | err_no_match: | |
956b9ba1 JP |
375 | edac_dbg(2, "sys_addr 0x%lx doesn't match any node\n", |
376 | (unsigned long)sys_addr); | |
6775763a DT |
377 | |
378 | return NULL; | |
379 | } | |
e2ce7255 DT |
380 | |
381 | /* | |
11c75ead BP |
382 | * compute the CS base address of the @csrow on the DRAM controller @dct. |
383 | * For details see F2x[5C:40] in the processor's BKDG | |
e2ce7255 | 384 | */ |
11c75ead BP |
385 | static void get_cs_base_and_mask(struct amd64_pvt *pvt, int csrow, u8 dct, |
386 | u64 *base, u64 *mask) | |
e2ce7255 | 387 | { |
11c75ead BP |
388 | u64 csbase, csmask, base_bits, mask_bits; |
389 | u8 addr_shift; | |
e2ce7255 | 390 | |
18b94f66 | 391 | if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) { |
11c75ead BP |
392 | csbase = pvt->csels[dct].csbases[csrow]; |
393 | csmask = pvt->csels[dct].csmasks[csrow]; | |
10ef6b0d CG |
394 | base_bits = GENMASK_ULL(31, 21) | GENMASK_ULL(15, 9); |
395 | mask_bits = GENMASK_ULL(29, 21) | GENMASK_ULL(15, 9); | |
11c75ead | 396 | addr_shift = 4; |
94c1acf2 AG |
397 | |
398 | /* | |
18b94f66 AG |
399 | * F16h and F15h, models 30h and later need two addr_shift values: |
400 | * 8 for high and 6 for low (cf. F16h BKDG). | |
401 | */ | |
402 | } else if (pvt->fam == 0x16 || | |
403 | (pvt->fam == 0x15 && pvt->model >= 0x30)) { | |
94c1acf2 AG |
404 | csbase = pvt->csels[dct].csbases[csrow]; |
405 | csmask = pvt->csels[dct].csmasks[csrow >> 1]; | |
406 | ||
10ef6b0d CG |
407 | *base = (csbase & GENMASK_ULL(15, 5)) << 6; |
408 | *base |= (csbase & GENMASK_ULL(30, 19)) << 8; | |
94c1acf2 AG |
409 | |
410 | *mask = ~0ULL; | |
411 | /* poke holes for the csmask */ | |
10ef6b0d CG |
412 | *mask &= ~((GENMASK_ULL(15, 5) << 6) | |
413 | (GENMASK_ULL(30, 19) << 8)); | |
94c1acf2 | 414 | |
10ef6b0d CG |
415 | *mask |= (csmask & GENMASK_ULL(15, 5)) << 6; |
416 | *mask |= (csmask & GENMASK_ULL(30, 19)) << 8; | |
94c1acf2 AG |
417 | |
418 | return; | |
11c75ead BP |
419 | } else { |
420 | csbase = pvt->csels[dct].csbases[csrow]; | |
421 | csmask = pvt->csels[dct].csmasks[csrow >> 1]; | |
422 | addr_shift = 8; | |
e2ce7255 | 423 | |
a4b4bedc | 424 | if (pvt->fam == 0x15) |
10ef6b0d CG |
425 | base_bits = mask_bits = |
426 | GENMASK_ULL(30,19) | GENMASK_ULL(13,5); | |
11c75ead | 427 | else |
10ef6b0d CG |
428 | base_bits = mask_bits = |
429 | GENMASK_ULL(28,19) | GENMASK_ULL(13,5); | |
11c75ead | 430 | } |
e2ce7255 | 431 | |
11c75ead | 432 | *base = (csbase & base_bits) << addr_shift; |
e2ce7255 | 433 | |
11c75ead BP |
434 | *mask = ~0ULL; |
435 | /* poke holes for the csmask */ | |
436 | *mask &= ~(mask_bits << addr_shift); | |
437 | /* OR them in */ | |
438 | *mask |= (csmask & mask_bits) << addr_shift; | |
e2ce7255 DT |
439 | } |
440 | ||
11c75ead BP |
441 | #define for_each_chip_select(i, dct, pvt) \ |
442 | for (i = 0; i < pvt->csels[dct].b_cnt; i++) | |
443 | ||
614ec9d8 BP |
444 | #define chip_select_base(i, dct, pvt) \ |
445 | pvt->csels[dct].csbases[i] | |
446 | ||
11c75ead BP |
447 | #define for_each_chip_select_mask(i, dct, pvt) \ |
448 | for (i = 0; i < pvt->csels[dct].m_cnt; i++) | |
449 | ||
4d30d2bc | 450 | #define for_each_umc(i) \ |
5e4c5527 | 451 | for (i = 0; i < fam_type->max_mcs; i++) |
4d30d2bc | 452 | |
e2ce7255 DT |
453 | /* |
454 | * @input_addr is an InputAddr associated with the node given by mci. Return the | |
455 | * csrow that input_addr maps to, or -1 on failure (no csrow claims input_addr). | |
456 | */ | |
457 | static int input_addr_to_csrow(struct mem_ctl_info *mci, u64 input_addr) | |
458 | { | |
459 | struct amd64_pvt *pvt; | |
460 | int csrow; | |
461 | u64 base, mask; | |
462 | ||
463 | pvt = mci->pvt_info; | |
464 | ||
11c75ead BP |
465 | for_each_chip_select(csrow, 0, pvt) { |
466 | if (!csrow_enabled(csrow, 0, pvt)) | |
e2ce7255 DT |
467 | continue; |
468 | ||
11c75ead BP |
469 | get_cs_base_and_mask(pvt, csrow, 0, &base, &mask); |
470 | ||
471 | mask = ~mask; | |
e2ce7255 DT |
472 | |
473 | if ((input_addr & mask) == (base & mask)) { | |
956b9ba1 JP |
474 | edac_dbg(2, "InputAddr 0x%lx matches csrow %d (node %d)\n", |
475 | (unsigned long)input_addr, csrow, | |
476 | pvt->mc_node_id); | |
e2ce7255 DT |
477 | |
478 | return csrow; | |
479 | } | |
480 | } | |
956b9ba1 JP |
481 | edac_dbg(2, "no matching csrow for InputAddr 0x%lx (MC node %d)\n", |
482 | (unsigned long)input_addr, pvt->mc_node_id); | |
e2ce7255 DT |
483 | |
484 | return -1; | |
485 | } | |
486 | ||
e2ce7255 DT |
487 | /* |
488 | * Obtain info from the DRAM Hole Address Register (section 3.4.8, pub #26094) | |
489 | * for the node represented by mci. Info is passed back in *hole_base, | |
490 | * *hole_offset, and *hole_size. Function returns 0 if info is valid or 1 if | |
491 | * info is invalid. Info may be invalid for either of the following reasons: | |
492 | * | |
493 | * - The revision of the node is not E or greater. In this case, the DRAM Hole | |
494 | * Address Register does not exist. | |
495 | * | |
496 | * - The DramHoleValid bit is cleared in the DRAM Hole Address Register, | |
497 | * indicating that its contents are not valid. | |
498 | * | |
499 | * The values passed back in *hole_base, *hole_offset, and *hole_size are | |
500 | * complete 32-bit values despite the fact that the bitfields in the DHAR | |
501 | * only represent bits 31-24 of the base and offset values. | |
502 | */ | |
2a28ceef BP |
503 | static int get_dram_hole_info(struct mem_ctl_info *mci, u64 *hole_base, |
504 | u64 *hole_offset, u64 *hole_size) | |
e2ce7255 DT |
505 | { |
506 | struct amd64_pvt *pvt = mci->pvt_info; | |
e2ce7255 DT |
507 | |
508 | /* only revE and later have the DRAM Hole Address Register */ | |
a4b4bedc | 509 | if (pvt->fam == 0xf && pvt->ext_model < K8_REV_E) { |
956b9ba1 JP |
510 | edac_dbg(1, " revision %d for node %d does not support DHAR\n", |
511 | pvt->ext_model, pvt->mc_node_id); | |
e2ce7255 DT |
512 | return 1; |
513 | } | |
514 | ||
bc21fa57 | 515 | /* valid for Fam10h and above */ |
a4b4bedc | 516 | if (pvt->fam >= 0x10 && !dhar_mem_hoist_valid(pvt)) { |
956b9ba1 | 517 | edac_dbg(1, " Dram Memory Hoisting is DISABLED on this system\n"); |
e2ce7255 DT |
518 | return 1; |
519 | } | |
520 | ||
c8e518d5 | 521 | if (!dhar_valid(pvt)) { |
956b9ba1 JP |
522 | edac_dbg(1, " Dram Memory Hoisting is DISABLED on this node %d\n", |
523 | pvt->mc_node_id); | |
e2ce7255 DT |
524 | return 1; |
525 | } | |
526 | ||
527 | /* This node has Memory Hoisting */ | |
528 | ||
529 | /* +------------------+--------------------+--------------------+----- | |
530 | * | memory | DRAM hole | relocated | | |
531 | * | [0, (x - 1)] | [x, 0xffffffff] | addresses from | | |
532 | * | | | DRAM hole | | |
533 | * | | | [0x100000000, | | |
534 | * | | | (0x100000000+ | | |
535 | * | | | (0xffffffff-x))] | | |
536 | * +------------------+--------------------+--------------------+----- | |
537 | * | |
538 | * Above is a diagram of physical memory showing the DRAM hole and the | |
539 | * relocated addresses from the DRAM hole. As shown, the DRAM hole | |
540 | * starts at address x (the base address) and extends through address | |
541 | * 0xffffffff. The DRAM Hole Address Register (DHAR) relocates the | |
542 | * addresses in the hole so that they start at 0x100000000. | |
543 | */ | |
544 | ||
1f31677e BP |
545 | *hole_base = dhar_base(pvt); |
546 | *hole_size = (1ULL << 32) - *hole_base; | |
e2ce7255 | 547 | |
a4b4bedc BP |
548 | *hole_offset = (pvt->fam > 0xf) ? f10_dhar_offset(pvt) |
549 | : k8_dhar_offset(pvt); | |
e2ce7255 | 550 | |
956b9ba1 JP |
551 | edac_dbg(1, " DHAR info for node %d base 0x%lx offset 0x%lx size 0x%lx\n", |
552 | pvt->mc_node_id, (unsigned long)*hole_base, | |
553 | (unsigned long)*hole_offset, (unsigned long)*hole_size); | |
e2ce7255 DT |
554 | |
555 | return 0; | |
556 | } | |
2a28ceef BP |
557 | |
558 | #ifdef CONFIG_EDAC_DEBUG | |
559 | #define EDAC_DCT_ATTR_SHOW(reg) \ | |
560 | static ssize_t reg##_show(struct device *dev, \ | |
561 | struct device_attribute *mattr, char *data) \ | |
562 | { \ | |
563 | struct mem_ctl_info *mci = to_mci(dev); \ | |
564 | struct amd64_pvt *pvt = mci->pvt_info; \ | |
565 | \ | |
566 | return sprintf(data, "0x%016llx\n", (u64)pvt->reg); \ | |
567 | } | |
568 | ||
569 | EDAC_DCT_ATTR_SHOW(dhar); | |
570 | EDAC_DCT_ATTR_SHOW(dbam0); | |
571 | EDAC_DCT_ATTR_SHOW(top_mem); | |
572 | EDAC_DCT_ATTR_SHOW(top_mem2); | |
573 | ||
574 | static ssize_t hole_show(struct device *dev, struct device_attribute *mattr, | |
575 | char *data) | |
576 | { | |
577 | struct mem_ctl_info *mci = to_mci(dev); | |
578 | ||
579 | u64 hole_base = 0; | |
580 | u64 hole_offset = 0; | |
581 | u64 hole_size = 0; | |
582 | ||
583 | get_dram_hole_info(mci, &hole_base, &hole_offset, &hole_size); | |
584 | ||
585 | return sprintf(data, "%llx %llx %llx\n", hole_base, hole_offset, | |
586 | hole_size); | |
587 | } | |
588 | ||
589 | /* | |
590 | * update NUM_DBG_ATTRS in case you add new members | |
591 | */ | |
592 | static DEVICE_ATTR(dhar, S_IRUGO, dhar_show, NULL); | |
593 | static DEVICE_ATTR(dbam, S_IRUGO, dbam0_show, NULL); | |
594 | static DEVICE_ATTR(topmem, S_IRUGO, top_mem_show, NULL); | |
595 | static DEVICE_ATTR(topmem2, S_IRUGO, top_mem2_show, NULL); | |
596 | static DEVICE_ATTR(dram_hole, S_IRUGO, hole_show, NULL); | |
597 | ||
598 | static struct attribute *dbg_attrs[] = { | |
599 | &dev_attr_dhar.attr, | |
600 | &dev_attr_dbam.attr, | |
601 | &dev_attr_topmem.attr, | |
602 | &dev_attr_topmem2.attr, | |
603 | &dev_attr_dram_hole.attr, | |
604 | NULL | |
605 | }; | |
606 | ||
607 | static const struct attribute_group dbg_group = { | |
608 | .attrs = dbg_attrs, | |
609 | }; | |
610 | #endif /* CONFIG_EDAC_DEBUG */ | |
611 | ||
e2ce7255 | 612 | |
93c2df58 DT |
613 | /* |
614 | * Return the DramAddr that the SysAddr given by @sys_addr maps to. It is | |
615 | * assumed that sys_addr maps to the node given by mci. | |
616 | * | |
617 | * The first part of section 3.4.4 (p. 70) shows how the DRAM Base (section | |
618 | * 3.4.4.1) and DRAM Limit (section 3.4.4.2) registers are used to translate a | |
619 | * SysAddr to a DramAddr. If the DRAM Hole Address Register (DHAR) is enabled, | |
620 | * then it is also involved in translating a SysAddr to a DramAddr. Sections | |
621 | * 3.4.8 and 3.5.8.2 describe the DHAR and how it is used for memory hoisting. | |
622 | * These parts of the documentation are unclear. I interpret them as follows: | |
623 | * | |
624 | * When node n receives a SysAddr, it processes the SysAddr as follows: | |
625 | * | |
626 | * 1. It extracts the DRAMBase and DRAMLimit values from the DRAM Base and DRAM | |
627 | * Limit registers for node n. If the SysAddr is not within the range | |
628 | * specified by the base and limit values, then node n ignores the Sysaddr | |
629 | * (since it does not map to node n). Otherwise continue to step 2 below. | |
630 | * | |
631 | * 2. If the DramHoleValid bit of the DHAR for node n is clear, the DHAR is | |
632 | * disabled so skip to step 3 below. Otherwise see if the SysAddr is within | |
633 | * the range of relocated addresses (starting at 0x100000000) from the DRAM | |
634 | * hole. If not, skip to step 3 below. Else get the value of the | |
635 | * DramHoleOffset field from the DHAR. To obtain the DramAddr, subtract the | |
636 | * offset defined by this value from the SysAddr. | |
637 | * | |
638 | * 3. Obtain the base address for node n from the DRAMBase field of the DRAM | |
639 | * Base register for node n. To obtain the DramAddr, subtract the base | |
640 | * address from the SysAddr, as shown near the start of section 3.4.4 (p.70). | |
641 | */ | |
642 | static u64 sys_addr_to_dram_addr(struct mem_ctl_info *mci, u64 sys_addr) | |
643 | { | |
7f19bf75 | 644 | struct amd64_pvt *pvt = mci->pvt_info; |
93c2df58 | 645 | u64 dram_base, hole_base, hole_offset, hole_size, dram_addr; |
1f31677e | 646 | int ret; |
93c2df58 | 647 | |
7f19bf75 | 648 | dram_base = get_dram_base(pvt, pvt->mc_node_id); |
93c2df58 | 649 | |
2a28ceef | 650 | ret = get_dram_hole_info(mci, &hole_base, &hole_offset, &hole_size); |
93c2df58 | 651 | if (!ret) { |
1f31677e BP |
652 | if ((sys_addr >= (1ULL << 32)) && |
653 | (sys_addr < ((1ULL << 32) + hole_size))) { | |
93c2df58 DT |
654 | /* use DHAR to translate SysAddr to DramAddr */ |
655 | dram_addr = sys_addr - hole_offset; | |
656 | ||
956b9ba1 JP |
657 | edac_dbg(2, "using DHAR to translate SysAddr 0x%lx to DramAddr 0x%lx\n", |
658 | (unsigned long)sys_addr, | |
659 | (unsigned long)dram_addr); | |
93c2df58 DT |
660 | |
661 | return dram_addr; | |
662 | } | |
663 | } | |
664 | ||
665 | /* | |
666 | * Translate the SysAddr to a DramAddr as shown near the start of | |
667 | * section 3.4.4 (p. 70). Although sys_addr is a 64-bit value, the k8 | |
668 | * only deals with 40-bit values. Therefore we discard bits 63-40 of | |
669 | * sys_addr below. If bit 39 of sys_addr is 1 then the bits we | |
670 | * discard are all 1s. Otherwise the bits we discard are all 0s. See | |
671 | * section 3.4.2 of AMD publication 24592: AMD x86-64 Architecture | |
672 | * Programmer's Manual Volume 1 Application Programming. | |
673 | */ | |
10ef6b0d | 674 | dram_addr = (sys_addr & GENMASK_ULL(39, 0)) - dram_base; |
93c2df58 | 675 | |
956b9ba1 JP |
676 | edac_dbg(2, "using DRAM Base register to translate SysAddr 0x%lx to DramAddr 0x%lx\n", |
677 | (unsigned long)sys_addr, (unsigned long)dram_addr); | |
93c2df58 DT |
678 | return dram_addr; |
679 | } | |
680 | ||
681 | /* | |
682 | * @intlv_en is the value of the IntlvEn field from a DRAM Base register | |
683 | * (section 3.4.4.1). Return the number of bits from a SysAddr that are used | |
684 | * for node interleaving. | |
685 | */ | |
686 | static int num_node_interleave_bits(unsigned intlv_en) | |
687 | { | |
688 | static const int intlv_shift_table[] = { 0, 1, 0, 2, 0, 0, 0, 3 }; | |
689 | int n; | |
690 | ||
691 | BUG_ON(intlv_en > 7); | |
692 | n = intlv_shift_table[intlv_en]; | |
693 | return n; | |
694 | } | |
695 | ||
696 | /* Translate the DramAddr given by @dram_addr to an InputAddr. */ | |
697 | static u64 dram_addr_to_input_addr(struct mem_ctl_info *mci, u64 dram_addr) | |
698 | { | |
699 | struct amd64_pvt *pvt; | |
700 | int intlv_shift; | |
701 | u64 input_addr; | |
702 | ||
703 | pvt = mci->pvt_info; | |
704 | ||
705 | /* | |
706 | * See the start of section 3.4.4 (p. 70, BKDG #26094, K8, revA-E) | |
707 | * concerning translating a DramAddr to an InputAddr. | |
708 | */ | |
7f19bf75 | 709 | intlv_shift = num_node_interleave_bits(dram_intlv_en(pvt, 0)); |
10ef6b0d | 710 | input_addr = ((dram_addr >> intlv_shift) & GENMASK_ULL(35, 12)) + |
f678b8cc | 711 | (dram_addr & 0xfff); |
93c2df58 | 712 | |
956b9ba1 JP |
713 | edac_dbg(2, " Intlv Shift=%d DramAddr=0x%lx maps to InputAddr=0x%lx\n", |
714 | intlv_shift, (unsigned long)dram_addr, | |
715 | (unsigned long)input_addr); | |
93c2df58 DT |
716 | |
717 | return input_addr; | |
718 | } | |
719 | ||
720 | /* | |
721 | * Translate the SysAddr represented by @sys_addr to an InputAddr. It is | |
722 | * assumed that @sys_addr maps to the node given by mci. | |
723 | */ | |
724 | static u64 sys_addr_to_input_addr(struct mem_ctl_info *mci, u64 sys_addr) | |
725 | { | |
726 | u64 input_addr; | |
727 | ||
728 | input_addr = | |
729 | dram_addr_to_input_addr(mci, sys_addr_to_dram_addr(mci, sys_addr)); | |
730 | ||
c19ca6cb | 731 | edac_dbg(2, "SysAddr 0x%lx translates to InputAddr 0x%lx\n", |
956b9ba1 | 732 | (unsigned long)sys_addr, (unsigned long)input_addr); |
93c2df58 DT |
733 | |
734 | return input_addr; | |
735 | } | |
736 | ||
93c2df58 DT |
737 | /* Map the Error address to a PAGE and PAGE OFFSET. */ |
738 | static inline void error_address_to_page_and_offset(u64 error_address, | |
33ca0643 | 739 | struct err_info *err) |
93c2df58 | 740 | { |
33ca0643 BP |
741 | err->page = (u32) (error_address >> PAGE_SHIFT); |
742 | err->offset = ((u32) error_address) & ~PAGE_MASK; | |
93c2df58 DT |
743 | } |
744 | ||
745 | /* | |
746 | * @sys_addr is an error address (a SysAddr) extracted from the MCA NB Address | |
747 | * Low (section 3.6.4.5) and MCA NB Address High (section 3.6.4.6) registers | |
748 | * of a node that detected an ECC memory error. mci represents the node that | |
749 | * the error address maps to (possibly different from the node that detected | |
750 | * the error). Return the number of the csrow that sys_addr maps to, or -1 on | |
751 | * error. | |
752 | */ | |
753 | static int sys_addr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr) | |
754 | { | |
755 | int csrow; | |
756 | ||
757 | csrow = input_addr_to_csrow(mci, sys_addr_to_input_addr(mci, sys_addr)); | |
758 | ||
759 | if (csrow == -1) | |
24f9a7fe BP |
760 | amd64_mc_err(mci, "Failed to translate InputAddr to csrow for " |
761 | "address 0x%lx\n", (unsigned long)sys_addr); | |
93c2df58 DT |
762 | return csrow; |
763 | } | |
e2ce7255 | 764 | |
bfc04aec | 765 | static int get_channel_from_ecc_syndrome(struct mem_ctl_info *, u16); |
2da11654 | 766 | |
2da11654 DT |
767 | /* |
768 | * Determine if the DIMMs have ECC enabled. ECC is enabled ONLY if all the DIMMs | |
769 | * are ECC capable. | |
770 | */ | |
d1ea71cd | 771 | static unsigned long determine_edac_cap(struct amd64_pvt *pvt) |
2da11654 | 772 | { |
1f6189ed | 773 | unsigned long edac_cap = EDAC_FLAG_NONE; |
d27f3a34 YG |
774 | u8 bit; |
775 | ||
776 | if (pvt->umc) { | |
777 | u8 i, umc_en_mask = 0, dimm_ecc_en_mask = 0; | |
2da11654 | 778 | |
4d30d2bc | 779 | for_each_umc(i) { |
d27f3a34 YG |
780 | if (!(pvt->umc[i].sdp_ctrl & UMC_SDP_INIT)) |
781 | continue; | |
2da11654 | 782 | |
d27f3a34 YG |
783 | umc_en_mask |= BIT(i); |
784 | ||
785 | /* UMC Configuration bit 12 (DimmEccEn) */ | |
786 | if (pvt->umc[i].umc_cfg & BIT(12)) | |
787 | dimm_ecc_en_mask |= BIT(i); | |
788 | } | |
789 | ||
790 | if (umc_en_mask == dimm_ecc_en_mask) | |
791 | edac_cap = EDAC_FLAG_SECDED; | |
792 | } else { | |
793 | bit = (pvt->fam > 0xf || pvt->ext_model >= K8_REV_F) | |
794 | ? 19 | |
795 | : 17; | |
796 | ||
797 | if (pvt->dclr0 & BIT(bit)) | |
798 | edac_cap = EDAC_FLAG_SECDED; | |
799 | } | |
2da11654 DT |
800 | |
801 | return edac_cap; | |
802 | } | |
803 | ||
d1ea71cd | 804 | static void debug_display_dimm_sizes(struct amd64_pvt *, u8); |
2da11654 | 805 | |
d1ea71cd | 806 | static void debug_dump_dramcfg_low(struct amd64_pvt *pvt, u32 dclr, int chan) |
68798e17 | 807 | { |
956b9ba1 | 808 | edac_dbg(1, "F2x%d90 (DRAM Cfg Low): 0x%08x\n", chan, dclr); |
68798e17 | 809 | |
a597d2a5 AG |
810 | if (pvt->dram_type == MEM_LRDDR3) { |
811 | u32 dcsm = pvt->csels[chan].csmasks[0]; | |
812 | /* | |
813 | * It's assumed all LRDIMMs in a DCT are going to be of | |
814 | * same 'type' until proven otherwise. So, use a cs | |
815 | * value of '0' here to get dcsm value. | |
816 | */ | |
817 | edac_dbg(1, " LRDIMM %dx rank multiply\n", (dcsm & 0x3)); | |
818 | } | |
819 | ||
820 | edac_dbg(1, "All DIMMs support ECC:%s\n", | |
821 | (dclr & BIT(19)) ? "yes" : "no"); | |
822 | ||
68798e17 | 823 | |
956b9ba1 JP |
824 | edac_dbg(1, " PAR/ERR parity: %s\n", |
825 | (dclr & BIT(8)) ? "enabled" : "disabled"); | |
68798e17 | 826 | |
a4b4bedc | 827 | if (pvt->fam == 0x10) |
956b9ba1 JP |
828 | edac_dbg(1, " DCT 128bit mode width: %s\n", |
829 | (dclr & BIT(11)) ? "128b" : "64b"); | |
68798e17 | 830 | |
956b9ba1 JP |
831 | edac_dbg(1, " x4 logical DIMMs present: L0: %s L1: %s L2: %s L3: %s\n", |
832 | (dclr & BIT(12)) ? "yes" : "no", | |
833 | (dclr & BIT(13)) ? "yes" : "no", | |
834 | (dclr & BIT(14)) ? "yes" : "no", | |
835 | (dclr & BIT(15)) ? "yes" : "no"); | |
68798e17 BP |
836 | } |
837 | ||
e53a3b26 YG |
838 | #define CS_EVEN_PRIMARY BIT(0) |
839 | #define CS_ODD_PRIMARY BIT(1) | |
81f5090d YG |
840 | #define CS_EVEN_SECONDARY BIT(2) |
841 | #define CS_ODD_SECONDARY BIT(3) | |
e53a3b26 | 842 | |
81f5090d YG |
843 | #define CS_EVEN (CS_EVEN_PRIMARY | CS_EVEN_SECONDARY) |
844 | #define CS_ODD (CS_ODD_PRIMARY | CS_ODD_SECONDARY) | |
e53a3b26 YG |
845 | |
846 | static int f17_get_cs_mode(int dimm, u8 ctrl, struct amd64_pvt *pvt) | |
fc00c6a4 | 847 | { |
e53a3b26 | 848 | int cs_mode = 0; |
fc00c6a4 | 849 | |
e53a3b26 YG |
850 | if (csrow_enabled(2 * dimm, ctrl, pvt)) |
851 | cs_mode |= CS_EVEN_PRIMARY; | |
fc00c6a4 | 852 | |
e53a3b26 YG |
853 | if (csrow_enabled(2 * dimm + 1, ctrl, pvt)) |
854 | cs_mode |= CS_ODD_PRIMARY; | |
855 | ||
81f5090d YG |
856 | /* Asymmetric dual-rank DIMM support. */ |
857 | if (csrow_sec_enabled(2 * dimm + 1, ctrl, pvt)) | |
858 | cs_mode |= CS_ODD_SECONDARY; | |
859 | ||
e53a3b26 | 860 | return cs_mode; |
fc00c6a4 YG |
861 | } |
862 | ||
07ed82ef YG |
863 | static void debug_display_dimm_sizes_df(struct amd64_pvt *pvt, u8 ctrl) |
864 | { | |
e53a3b26 | 865 | int dimm, size0, size1, cs0, cs1, cs_mode; |
07ed82ef YG |
866 | |
867 | edac_printk(KERN_DEBUG, EDAC_MC, "UMC%d chip selects:\n", ctrl); | |
868 | ||
d971e28e | 869 | for (dimm = 0; dimm < 2; dimm++) { |
eb77e6b8 | 870 | cs0 = dimm * 2; |
eb77e6b8 YG |
871 | cs1 = dimm * 2 + 1; |
872 | ||
e53a3b26 YG |
873 | cs_mode = f17_get_cs_mode(dimm, ctrl, pvt); |
874 | ||
875 | size0 = pvt->ops->dbam_to_cs(pvt, ctrl, cs_mode, cs0); | |
876 | size1 = pvt->ops->dbam_to_cs(pvt, ctrl, cs_mode, cs1); | |
07ed82ef YG |
877 | |
878 | amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n", | |
eb77e6b8 YG |
879 | cs0, size0, |
880 | cs1, size1); | |
07ed82ef YG |
881 | } |
882 | } | |
883 | ||
884 | static void __dump_misc_regs_df(struct amd64_pvt *pvt) | |
885 | { | |
886 | struct amd64_umc *umc; | |
887 | u32 i, tmp, umc_base; | |
888 | ||
4d30d2bc | 889 | for_each_umc(i) { |
07ed82ef YG |
890 | umc_base = get_umc_base(i); |
891 | umc = &pvt->umc[i]; | |
892 | ||
893 | edac_dbg(1, "UMC%d DIMM cfg: 0x%x\n", i, umc->dimm_cfg); | |
894 | edac_dbg(1, "UMC%d UMC cfg: 0x%x\n", i, umc->umc_cfg); | |
895 | edac_dbg(1, "UMC%d SDP ctrl: 0x%x\n", i, umc->sdp_ctrl); | |
896 | edac_dbg(1, "UMC%d ECC ctrl: 0x%x\n", i, umc->ecc_ctrl); | |
897 | ||
898 | amd_smn_read(pvt->mc_node_id, umc_base + UMCCH_ECC_BAD_SYMBOL, &tmp); | |
899 | edac_dbg(1, "UMC%d ECC bad symbol: 0x%x\n", i, tmp); | |
900 | ||
901 | amd_smn_read(pvt->mc_node_id, umc_base + UMCCH_UMC_CAP, &tmp); | |
902 | edac_dbg(1, "UMC%d UMC cap: 0x%x\n", i, tmp); | |
903 | edac_dbg(1, "UMC%d UMC cap high: 0x%x\n", i, umc->umc_cap_hi); | |
904 | ||
905 | edac_dbg(1, "UMC%d ECC capable: %s, ChipKill ECC capable: %s\n", | |
906 | i, (umc->umc_cap_hi & BIT(30)) ? "yes" : "no", | |
907 | (umc->umc_cap_hi & BIT(31)) ? "yes" : "no"); | |
908 | edac_dbg(1, "UMC%d All DIMMs support ECC: %s\n", | |
909 | i, (umc->umc_cfg & BIT(12)) ? "yes" : "no"); | |
910 | edac_dbg(1, "UMC%d x4 DIMMs present: %s\n", | |
911 | i, (umc->dimm_cfg & BIT(6)) ? "yes" : "no"); | |
912 | edac_dbg(1, "UMC%d x16 DIMMs present: %s\n", | |
913 | i, (umc->dimm_cfg & BIT(7)) ? "yes" : "no"); | |
914 | ||
915 | if (pvt->dram_type == MEM_LRDDR4) { | |
916 | amd_smn_read(pvt->mc_node_id, umc_base + UMCCH_ADDR_CFG, &tmp); | |
917 | edac_dbg(1, "UMC%d LRDIMM %dx rank multiply\n", | |
918 | i, 1 << ((tmp >> 4) & 0x3)); | |
919 | } | |
920 | ||
921 | debug_display_dimm_sizes_df(pvt, i); | |
922 | } | |
923 | ||
924 | edac_dbg(1, "F0x104 (DRAM Hole Address): 0x%08x, base: 0x%08x\n", | |
925 | pvt->dhar, dhar_base(pvt)); | |
926 | } | |
927 | ||
2da11654 | 928 | /* Display and decode various NB registers for debug purposes. */ |
07ed82ef | 929 | static void __dump_misc_regs(struct amd64_pvt *pvt) |
2da11654 | 930 | { |
956b9ba1 | 931 | edac_dbg(1, "F3xE8 (NB Cap): 0x%08x\n", pvt->nbcap); |
68798e17 | 932 | |
956b9ba1 JP |
933 | edac_dbg(1, " NB two channel DRAM capable: %s\n", |
934 | (pvt->nbcap & NBCAP_DCT_DUAL) ? "yes" : "no"); | |
2da11654 | 935 | |
956b9ba1 JP |
936 | edac_dbg(1, " ECC capable: %s, ChipKill ECC capable: %s\n", |
937 | (pvt->nbcap & NBCAP_SECDED) ? "yes" : "no", | |
938 | (pvt->nbcap & NBCAP_CHIPKILL) ? "yes" : "no"); | |
68798e17 | 939 | |
d1ea71cd | 940 | debug_dump_dramcfg_low(pvt, pvt->dclr0, 0); |
2da11654 | 941 | |
956b9ba1 | 942 | edac_dbg(1, "F3xB0 (Online Spare): 0x%08x\n", pvt->online_spare); |
2da11654 | 943 | |
956b9ba1 JP |
944 | edac_dbg(1, "F1xF0 (DRAM Hole Address): 0x%08x, base: 0x%08x, offset: 0x%08x\n", |
945 | pvt->dhar, dhar_base(pvt), | |
a4b4bedc BP |
946 | (pvt->fam == 0xf) ? k8_dhar_offset(pvt) |
947 | : f10_dhar_offset(pvt)); | |
2da11654 | 948 | |
d1ea71cd | 949 | debug_display_dimm_sizes(pvt, 0); |
4d796364 | 950 | |
8de1d91e | 951 | /* everything below this point is Fam10h and above */ |
a4b4bedc | 952 | if (pvt->fam == 0xf) |
2da11654 | 953 | return; |
4d796364 | 954 | |
d1ea71cd | 955 | debug_display_dimm_sizes(pvt, 1); |
2da11654 | 956 | |
8de1d91e | 957 | /* Only if NOT ganged does dclr1 have valid info */ |
68798e17 | 958 | if (!dct_ganging_enabled(pvt)) |
d1ea71cd | 959 | debug_dump_dramcfg_low(pvt, pvt->dclr1, 1); |
2da11654 DT |
960 | } |
961 | ||
07ed82ef YG |
962 | /* Display and decode various NB registers for debug purposes. */ |
963 | static void dump_misc_regs(struct amd64_pvt *pvt) | |
964 | { | |
965 | if (pvt->umc) | |
966 | __dump_misc_regs_df(pvt); | |
967 | else | |
968 | __dump_misc_regs(pvt); | |
969 | ||
970 | edac_dbg(1, " DramHoleValid: %s\n", dhar_valid(pvt) ? "yes" : "no"); | |
971 | ||
7835961d | 972 | amd64_info("using x%u syndromes.\n", pvt->ecc_sym_sz); |
07ed82ef YG |
973 | } |
974 | ||
94be4bff | 975 | /* |
18b94f66 | 976 | * See BKDG, F2x[1,0][5C:40], F2[1,0][6C:60] |
94be4bff | 977 | */ |
11c75ead | 978 | static void prep_chip_selects(struct amd64_pvt *pvt) |
94be4bff | 979 | { |
18b94f66 | 980 | if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) { |
11c75ead BP |
981 | pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8; |
982 | pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 8; | |
a597d2a5 | 983 | } else if (pvt->fam == 0x15 && pvt->model == 0x30) { |
18b94f66 AG |
984 | pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 4; |
985 | pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 2; | |
d971e28e YG |
986 | } else if (pvt->fam >= 0x17) { |
987 | int umc; | |
988 | ||
989 | for_each_umc(umc) { | |
990 | pvt->csels[umc].b_cnt = 4; | |
991 | pvt->csels[umc].m_cnt = 2; | |
992 | } | |
993 | ||
9d858bb1 | 994 | } else { |
11c75ead BP |
995 | pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8; |
996 | pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 4; | |
94be4bff DT |
997 | } |
998 | } | |
999 | ||
d971e28e YG |
1000 | static void read_umc_base_mask(struct amd64_pvt *pvt) |
1001 | { | |
7574729e YG |
1002 | u32 umc_base_reg, umc_base_reg_sec; |
1003 | u32 umc_mask_reg, umc_mask_reg_sec; | |
1004 | u32 base_reg, base_reg_sec; | |
1005 | u32 mask_reg, mask_reg_sec; | |
1006 | u32 *base, *base_sec; | |
1007 | u32 *mask, *mask_sec; | |
d971e28e YG |
1008 | int cs, umc; |
1009 | ||
1010 | for_each_umc(umc) { | |
1011 | umc_base_reg = get_umc_base(umc) + UMCCH_BASE_ADDR; | |
7574729e | 1012 | umc_base_reg_sec = get_umc_base(umc) + UMCCH_BASE_ADDR_SEC; |
d971e28e YG |
1013 | |
1014 | for_each_chip_select(cs, umc, pvt) { | |
1015 | base = &pvt->csels[umc].csbases[cs]; | |
7574729e | 1016 | base_sec = &pvt->csels[umc].csbases_sec[cs]; |
d971e28e YG |
1017 | |
1018 | base_reg = umc_base_reg + (cs * 4); | |
7574729e | 1019 | base_reg_sec = umc_base_reg_sec + (cs * 4); |
d971e28e YG |
1020 | |
1021 | if (!amd_smn_read(pvt->mc_node_id, base_reg, base)) | |
1022 | edac_dbg(0, " DCSB%d[%d]=0x%08x reg: 0x%x\n", | |
1023 | umc, cs, *base, base_reg); | |
7574729e YG |
1024 | |
1025 | if (!amd_smn_read(pvt->mc_node_id, base_reg_sec, base_sec)) | |
1026 | edac_dbg(0, " DCSB_SEC%d[%d]=0x%08x reg: 0x%x\n", | |
1027 | umc, cs, *base_sec, base_reg_sec); | |
d971e28e YG |
1028 | } |
1029 | ||
1030 | umc_mask_reg = get_umc_base(umc) + UMCCH_ADDR_MASK; | |
7574729e | 1031 | umc_mask_reg_sec = get_umc_base(umc) + UMCCH_ADDR_MASK_SEC; |
d971e28e YG |
1032 | |
1033 | for_each_chip_select_mask(cs, umc, pvt) { | |
1034 | mask = &pvt->csels[umc].csmasks[cs]; | |
7574729e | 1035 | mask_sec = &pvt->csels[umc].csmasks_sec[cs]; |
d971e28e YG |
1036 | |
1037 | mask_reg = umc_mask_reg + (cs * 4); | |
7574729e | 1038 | mask_reg_sec = umc_mask_reg_sec + (cs * 4); |
d971e28e YG |
1039 | |
1040 | if (!amd_smn_read(pvt->mc_node_id, mask_reg, mask)) | |
1041 | edac_dbg(0, " DCSM%d[%d]=0x%08x reg: 0x%x\n", | |
1042 | umc, cs, *mask, mask_reg); | |
7574729e YG |
1043 | |
1044 | if (!amd_smn_read(pvt->mc_node_id, mask_reg_sec, mask_sec)) | |
1045 | edac_dbg(0, " DCSM_SEC%d[%d]=0x%08x reg: 0x%x\n", | |
1046 | umc, cs, *mask_sec, mask_reg_sec); | |
d971e28e YG |
1047 | } |
1048 | } | |
1049 | } | |
1050 | ||
94be4bff | 1051 | /* |
11c75ead | 1052 | * Function 2 Offset F10_DCSB0; read in the DCS Base and DCS Mask registers |
94be4bff | 1053 | */ |
b2b0c605 | 1054 | static void read_dct_base_mask(struct amd64_pvt *pvt) |
94be4bff | 1055 | { |
d971e28e | 1056 | int cs; |
94be4bff | 1057 | |
11c75ead | 1058 | prep_chip_selects(pvt); |
94be4bff | 1059 | |
d971e28e YG |
1060 | if (pvt->umc) |
1061 | return read_umc_base_mask(pvt); | |
b64ce7cd | 1062 | |
11c75ead | 1063 | for_each_chip_select(cs, 0, pvt) { |
d971e28e YG |
1064 | int reg0 = DCSB0 + (cs * 4); |
1065 | int reg1 = DCSB1 + (cs * 4); | |
11c75ead BP |
1066 | u32 *base0 = &pvt->csels[0].csbases[cs]; |
1067 | u32 *base1 = &pvt->csels[1].csbases[cs]; | |
b2b0c605 | 1068 | |
d971e28e YG |
1069 | if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, base0)) |
1070 | edac_dbg(0, " DCSB0[%d]=0x%08x reg: F2x%x\n", | |
1071 | cs, *base0, reg0); | |
8de9930a | 1072 | |
d971e28e YG |
1073 | if (pvt->fam == 0xf) |
1074 | continue; | |
b64ce7cd | 1075 | |
d971e28e YG |
1076 | if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, base1)) |
1077 | edac_dbg(0, " DCSB1[%d]=0x%08x reg: F2x%x\n", | |
1078 | cs, *base1, (pvt->fam == 0x10) ? reg1 | |
1079 | : reg0); | |
94be4bff DT |
1080 | } |
1081 | ||
11c75ead | 1082 | for_each_chip_select_mask(cs, 0, pvt) { |
d971e28e YG |
1083 | int reg0 = DCSM0 + (cs * 4); |
1084 | int reg1 = DCSM1 + (cs * 4); | |
11c75ead BP |
1085 | u32 *mask0 = &pvt->csels[0].csmasks[cs]; |
1086 | u32 *mask1 = &pvt->csels[1].csmasks[cs]; | |
b2b0c605 | 1087 | |
d971e28e YG |
1088 | if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, mask0)) |
1089 | edac_dbg(0, " DCSM0[%d]=0x%08x reg: F2x%x\n", | |
1090 | cs, *mask0, reg0); | |
b64ce7cd | 1091 | |
d971e28e YG |
1092 | if (pvt->fam == 0xf) |
1093 | continue; | |
8de9930a | 1094 | |
d971e28e YG |
1095 | if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, mask1)) |
1096 | edac_dbg(0, " DCSM1[%d]=0x%08x reg: F2x%x\n", | |
1097 | cs, *mask1, (pvt->fam == 0x10) ? reg1 | |
1098 | : reg0); | |
94be4bff DT |
1099 | } |
1100 | } | |
1101 | ||
a597d2a5 | 1102 | static void determine_memory_type(struct amd64_pvt *pvt) |
94be4bff | 1103 | { |
a597d2a5 | 1104 | u32 dram_ctrl, dcsm; |
94be4bff | 1105 | |
dcd01394 YG |
1106 | if (pvt->umc) { |
1107 | if ((pvt->umc[0].dimm_cfg | pvt->umc[1].dimm_cfg) & BIT(5)) | |
1108 | pvt->dram_type = MEM_LRDDR4; | |
1109 | else if ((pvt->umc[0].dimm_cfg | pvt->umc[1].dimm_cfg) & BIT(4)) | |
1110 | pvt->dram_type = MEM_RDDR4; | |
1111 | else | |
1112 | pvt->dram_type = MEM_DDR4; | |
1113 | return; | |
1114 | } | |
1115 | ||
a597d2a5 AG |
1116 | switch (pvt->fam) { |
1117 | case 0xf: | |
1118 | if (pvt->ext_model >= K8_REV_F) | |
1119 | goto ddr3; | |
1120 | ||
1121 | pvt->dram_type = (pvt->dclr0 & BIT(18)) ? MEM_DDR : MEM_RDDR; | |
1122 | return; | |
1123 | ||
1124 | case 0x10: | |
6b4c0bde | 1125 | if (pvt->dchr0 & DDR3_MODE) |
a597d2a5 AG |
1126 | goto ddr3; |
1127 | ||
1128 | pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR2 : MEM_RDDR2; | |
1129 | return; | |
1130 | ||
1131 | case 0x15: | |
1132 | if (pvt->model < 0x60) | |
1133 | goto ddr3; | |
1134 | ||
1135 | /* | |
1136 | * Model 0x60h needs special handling: | |
1137 | * | |
1138 | * We use a Chip Select value of '0' to obtain dcsm. | |
1139 | * Theoretically, it is possible to populate LRDIMMs of different | |
1140 | * 'Rank' value on a DCT. But this is not the common case. So, | |
1141 | * it's reasonable to assume all DIMMs are going to be of same | |
1142 | * 'type' until proven otherwise. | |
1143 | */ | |
1144 | amd64_read_dct_pci_cfg(pvt, 0, DRAM_CONTROL, &dram_ctrl); | |
1145 | dcsm = pvt->csels[0].csmasks[0]; | |
1146 | ||
1147 | if (((dram_ctrl >> 8) & 0x7) == 0x2) | |
1148 | pvt->dram_type = MEM_DDR4; | |
1149 | else if (pvt->dclr0 & BIT(16)) | |
1150 | pvt->dram_type = MEM_DDR3; | |
1151 | else if (dcsm & 0x3) | |
1152 | pvt->dram_type = MEM_LRDDR3; | |
6b4c0bde | 1153 | else |
a597d2a5 | 1154 | pvt->dram_type = MEM_RDDR3; |
94be4bff | 1155 | |
a597d2a5 AG |
1156 | return; |
1157 | ||
1158 | case 0x16: | |
1159 | goto ddr3; | |
1160 | ||
1161 | default: | |
1162 | WARN(1, KERN_ERR "%s: Family??? 0x%x\n", __func__, pvt->fam); | |
1163 | pvt->dram_type = MEM_EMPTY; | |
1164 | } | |
1165 | return; | |
94be4bff | 1166 | |
a597d2a5 AG |
1167 | ddr3: |
1168 | pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR3 : MEM_RDDR3; | |
94be4bff DT |
1169 | } |
1170 | ||
cb328507 | 1171 | /* Get the number of DCT channels the memory controller is using. */ |
ddff876d DT |
1172 | static int k8_early_channel_count(struct amd64_pvt *pvt) |
1173 | { | |
cb328507 | 1174 | int flag; |
ddff876d | 1175 | |
9f56da0e | 1176 | if (pvt->ext_model >= K8_REV_F) |
ddff876d | 1177 | /* RevF (NPT) and later */ |
41d8bfab | 1178 | flag = pvt->dclr0 & WIDTH_128; |
9f56da0e | 1179 | else |
ddff876d DT |
1180 | /* RevE and earlier */ |
1181 | flag = pvt->dclr0 & REVE_WIDTH_128; | |
ddff876d DT |
1182 | |
1183 | /* not used */ | |
1184 | pvt->dclr1 = 0; | |
1185 | ||
1186 | return (flag) ? 2 : 1; | |
1187 | } | |
1188 | ||
70046624 | 1189 | /* On F10h and later ErrAddr is MC4_ADDR[47:1] */ |
a4b4bedc | 1190 | static u64 get_error_address(struct amd64_pvt *pvt, struct mce *m) |
ddff876d | 1191 | { |
db970bd2 | 1192 | u16 mce_nid = topology_die_id(m->extcpu); |
2ec591ac | 1193 | struct mem_ctl_info *mci; |
70046624 BP |
1194 | u8 start_bit = 1; |
1195 | u8 end_bit = 47; | |
2ec591ac BP |
1196 | u64 addr; |
1197 | ||
1198 | mci = edac_mc_find(mce_nid); | |
1199 | if (!mci) | |
1200 | return 0; | |
1201 | ||
1202 | pvt = mci->pvt_info; | |
70046624 | 1203 | |
a4b4bedc | 1204 | if (pvt->fam == 0xf) { |
70046624 BP |
1205 | start_bit = 3; |
1206 | end_bit = 39; | |
1207 | } | |
1208 | ||
10ef6b0d | 1209 | addr = m->addr & GENMASK_ULL(end_bit, start_bit); |
c1ae6830 BP |
1210 | |
1211 | /* | |
1212 | * Erratum 637 workaround | |
1213 | */ | |
a4b4bedc | 1214 | if (pvt->fam == 0x15) { |
c1ae6830 BP |
1215 | u64 cc6_base, tmp_addr; |
1216 | u32 tmp; | |
8b84c8df | 1217 | u8 intlv_en; |
c1ae6830 | 1218 | |
10ef6b0d | 1219 | if ((addr & GENMASK_ULL(47, 24)) >> 24 != 0x00fdf7) |
c1ae6830 BP |
1220 | return addr; |
1221 | ||
c1ae6830 BP |
1222 | |
1223 | amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_LIM, &tmp); | |
1224 | intlv_en = tmp >> 21 & 0x7; | |
1225 | ||
1226 | /* add [47:27] + 3 trailing bits */ | |
10ef6b0d | 1227 | cc6_base = (tmp & GENMASK_ULL(20, 0)) << 3; |
c1ae6830 BP |
1228 | |
1229 | /* reverse and add DramIntlvEn */ | |
1230 | cc6_base |= intlv_en ^ 0x7; | |
1231 | ||
1232 | /* pin at [47:24] */ | |
1233 | cc6_base <<= 24; | |
1234 | ||
1235 | if (!intlv_en) | |
10ef6b0d | 1236 | return cc6_base | (addr & GENMASK_ULL(23, 0)); |
c1ae6830 BP |
1237 | |
1238 | amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_BASE, &tmp); | |
1239 | ||
1240 | /* faster log2 */ | |
10ef6b0d | 1241 | tmp_addr = (addr & GENMASK_ULL(23, 12)) << __fls(intlv_en + 1); |
c1ae6830 BP |
1242 | |
1243 | /* OR DramIntlvSel into bits [14:12] */ | |
10ef6b0d | 1244 | tmp_addr |= (tmp & GENMASK_ULL(23, 21)) >> 9; |
c1ae6830 BP |
1245 | |
1246 | /* add remaining [11:0] bits from original MC4_ADDR */ | |
10ef6b0d | 1247 | tmp_addr |= addr & GENMASK_ULL(11, 0); |
c1ae6830 BP |
1248 | |
1249 | return cc6_base | tmp_addr; | |
1250 | } | |
1251 | ||
1252 | return addr; | |
ddff876d DT |
1253 | } |
1254 | ||
e2c0bffe DB |
1255 | static struct pci_dev *pci_get_related_function(unsigned int vendor, |
1256 | unsigned int device, | |
1257 | struct pci_dev *related) | |
1258 | { | |
1259 | struct pci_dev *dev = NULL; | |
1260 | ||
1261 | while ((dev = pci_get_device(vendor, device, dev))) { | |
1262 | if (pci_domain_nr(dev->bus) == pci_domain_nr(related->bus) && | |
1263 | (dev->bus->number == related->bus->number) && | |
1264 | (PCI_SLOT(dev->devfn) == PCI_SLOT(related->devfn))) | |
1265 | break; | |
1266 | } | |
1267 | ||
1268 | return dev; | |
1269 | } | |
1270 | ||
7f19bf75 | 1271 | static void read_dram_base_limit_regs(struct amd64_pvt *pvt, unsigned range) |
ddff876d | 1272 | { |
e2c0bffe | 1273 | struct amd_northbridge *nb; |
18b94f66 AG |
1274 | struct pci_dev *f1 = NULL; |
1275 | unsigned int pci_func; | |
71d2a32e | 1276 | int off = range << 3; |
e2c0bffe | 1277 | u32 llim; |
ddff876d | 1278 | |
7f19bf75 BP |
1279 | amd64_read_pci_cfg(pvt->F1, DRAM_BASE_LO + off, &pvt->ranges[range].base.lo); |
1280 | amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_LO + off, &pvt->ranges[range].lim.lo); | |
ddff876d | 1281 | |
18b94f66 | 1282 | if (pvt->fam == 0xf) |
7f19bf75 | 1283 | return; |
ddff876d | 1284 | |
7f19bf75 BP |
1285 | if (!dram_rw(pvt, range)) |
1286 | return; | |
ddff876d | 1287 | |
7f19bf75 BP |
1288 | amd64_read_pci_cfg(pvt->F1, DRAM_BASE_HI + off, &pvt->ranges[range].base.hi); |
1289 | amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_HI + off, &pvt->ranges[range].lim.hi); | |
f08e457c | 1290 | |
e2c0bffe | 1291 | /* F15h: factor in CC6 save area by reading dst node's limit reg */ |
18b94f66 | 1292 | if (pvt->fam != 0x15) |
e2c0bffe | 1293 | return; |
f08e457c | 1294 | |
e2c0bffe DB |
1295 | nb = node_to_amd_nb(dram_dst_node(pvt, range)); |
1296 | if (WARN_ON(!nb)) | |
1297 | return; | |
f08e457c | 1298 | |
a597d2a5 AG |
1299 | if (pvt->model == 0x60) |
1300 | pci_func = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1; | |
1301 | else if (pvt->model == 0x30) | |
1302 | pci_func = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1; | |
1303 | else | |
1304 | pci_func = PCI_DEVICE_ID_AMD_15H_NB_F1; | |
18b94f66 AG |
1305 | |
1306 | f1 = pci_get_related_function(nb->misc->vendor, pci_func, nb->misc); | |
e2c0bffe DB |
1307 | if (WARN_ON(!f1)) |
1308 | return; | |
f08e457c | 1309 | |
e2c0bffe | 1310 | amd64_read_pci_cfg(f1, DRAM_LOCAL_NODE_LIM, &llim); |
f08e457c | 1311 | |
10ef6b0d | 1312 | pvt->ranges[range].lim.lo &= GENMASK_ULL(15, 0); |
f08e457c | 1313 | |
e2c0bffe DB |
1314 | /* {[39:27],111b} */ |
1315 | pvt->ranges[range].lim.lo |= ((llim & 0x1fff) << 3 | 0x7) << 16; | |
f08e457c | 1316 | |
10ef6b0d | 1317 | pvt->ranges[range].lim.hi &= GENMASK_ULL(7, 0); |
f08e457c | 1318 | |
e2c0bffe DB |
1319 | /* [47:40] */ |
1320 | pvt->ranges[range].lim.hi |= llim >> 13; | |
1321 | ||
1322 | pci_dev_put(f1); | |
ddff876d DT |
1323 | } |
1324 | ||
f192c7b1 | 1325 | static void k8_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr, |
33ca0643 | 1326 | struct err_info *err) |
ddff876d | 1327 | { |
f192c7b1 | 1328 | struct amd64_pvt *pvt = mci->pvt_info; |
ddff876d | 1329 | |
33ca0643 | 1330 | error_address_to_page_and_offset(sys_addr, err); |
ab5a503c MCC |
1331 | |
1332 | /* | |
1333 | * Find out which node the error address belongs to. This may be | |
1334 | * different from the node that detected the error. | |
1335 | */ | |
33ca0643 BP |
1336 | err->src_mci = find_mc_by_sys_addr(mci, sys_addr); |
1337 | if (!err->src_mci) { | |
ab5a503c MCC |
1338 | amd64_mc_err(mci, "failed to map error addr 0x%lx to a node\n", |
1339 | (unsigned long)sys_addr); | |
33ca0643 | 1340 | err->err_code = ERR_NODE; |
ab5a503c MCC |
1341 | return; |
1342 | } | |
1343 | ||
1344 | /* Now map the sys_addr to a CSROW */ | |
33ca0643 BP |
1345 | err->csrow = sys_addr_to_csrow(err->src_mci, sys_addr); |
1346 | if (err->csrow < 0) { | |
1347 | err->err_code = ERR_CSROW; | |
ab5a503c MCC |
1348 | return; |
1349 | } | |
1350 | ||
ddff876d | 1351 | /* CHIPKILL enabled */ |
f192c7b1 | 1352 | if (pvt->nbcfg & NBCFG_CHIPKILL) { |
33ca0643 BP |
1353 | err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome); |
1354 | if (err->channel < 0) { | |
ddff876d DT |
1355 | /* |
1356 | * Syndrome didn't map, so we don't know which of the | |
1357 | * 2 DIMMs is in error. So we need to ID 'both' of them | |
1358 | * as suspect. | |
1359 | */ | |
33ca0643 | 1360 | amd64_mc_warn(err->src_mci, "unknown syndrome 0x%04x - " |
ab5a503c | 1361 | "possible error reporting race\n", |
33ca0643 BP |
1362 | err->syndrome); |
1363 | err->err_code = ERR_CHANNEL; | |
ddff876d DT |
1364 | return; |
1365 | } | |
1366 | } else { | |
1367 | /* | |
1368 | * non-chipkill ecc mode | |
1369 | * | |
1370 | * The k8 documentation is unclear about how to determine the | |
1371 | * channel number when using non-chipkill memory. This method | |
1372 | * was obtained from email communication with someone at AMD. | |
1373 | * (Wish the email was placed in this comment - norsk) | |
1374 | */ | |
33ca0643 | 1375 | err->channel = ((sys_addr & BIT(3)) != 0); |
ddff876d | 1376 | } |
ddff876d DT |
1377 | } |
1378 | ||
41d8bfab | 1379 | static int ddr2_cs_size(unsigned i, bool dct_width) |
ddff876d | 1380 | { |
41d8bfab | 1381 | unsigned shift = 0; |
ddff876d | 1382 | |
41d8bfab BP |
1383 | if (i <= 2) |
1384 | shift = i; | |
1385 | else if (!(i & 0x1)) | |
1386 | shift = i >> 1; | |
1433eb99 | 1387 | else |
41d8bfab | 1388 | shift = (i + 1) >> 1; |
ddff876d | 1389 | |
41d8bfab BP |
1390 | return 128 << (shift + !!dct_width); |
1391 | } | |
1392 | ||
1393 | static int k8_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct, | |
a597d2a5 | 1394 | unsigned cs_mode, int cs_mask_nr) |
41d8bfab BP |
1395 | { |
1396 | u32 dclr = dct ? pvt->dclr1 : pvt->dclr0; | |
1397 | ||
1398 | if (pvt->ext_model >= K8_REV_F) { | |
1399 | WARN_ON(cs_mode > 11); | |
1400 | return ddr2_cs_size(cs_mode, dclr & WIDTH_128); | |
1401 | } | |
1402 | else if (pvt->ext_model >= K8_REV_D) { | |
11b0a314 | 1403 | unsigned diff; |
41d8bfab BP |
1404 | WARN_ON(cs_mode > 10); |
1405 | ||
11b0a314 BP |
1406 | /* |
1407 | * the below calculation, besides trying to win an obfuscated C | |
1408 | * contest, maps cs_mode values to DIMM chip select sizes. The | |
1409 | * mappings are: | |
1410 | * | |
1411 | * cs_mode CS size (mb) | |
1412 | * ======= ============ | |
1413 | * 0 32 | |
1414 | * 1 64 | |
1415 | * 2 128 | |
1416 | * 3 128 | |
1417 | * 4 256 | |
1418 | * 5 512 | |
1419 | * 6 256 | |
1420 | * 7 512 | |
1421 | * 8 1024 | |
1422 | * 9 1024 | |
1423 | * 10 2048 | |
1424 | * | |
1425 | * Basically, it calculates a value with which to shift the | |
1426 | * smallest CS size of 32MB. | |
1427 | * | |
1428 | * ddr[23]_cs_size have a similar purpose. | |
1429 | */ | |
1430 | diff = cs_mode/3 + (unsigned)(cs_mode > 5); | |
1431 | ||
1432 | return 32 << (cs_mode - diff); | |
41d8bfab BP |
1433 | } |
1434 | else { | |
1435 | WARN_ON(cs_mode > 6); | |
1436 | return 32 << cs_mode; | |
1437 | } | |
ddff876d DT |
1438 | } |
1439 | ||
1afd3c98 DT |
1440 | /* |
1441 | * Get the number of DCT channels in use. | |
1442 | * | |
1443 | * Return: | |
1444 | * number of Memory Channels in operation | |
1445 | * Pass back: | |
1446 | * contents of the DCL0_LOW register | |
1447 | */ | |
7d20d14d | 1448 | static int f1x_early_channel_count(struct amd64_pvt *pvt) |
1afd3c98 | 1449 | { |
6ba5dcdc | 1450 | int i, j, channels = 0; |
1afd3c98 | 1451 | |
7d20d14d | 1452 | /* On F10h, if we are in 128 bit mode, then we are using 2 channels */ |
a4b4bedc | 1453 | if (pvt->fam == 0x10 && (pvt->dclr0 & WIDTH_128)) |
7d20d14d | 1454 | return 2; |
1afd3c98 DT |
1455 | |
1456 | /* | |
d16149e8 BP |
1457 | * Need to check if in unganged mode: In such, there are 2 channels, |
1458 | * but they are not in 128 bit mode and thus the above 'dclr0' status | |
1459 | * bit will be OFF. | |
1afd3c98 DT |
1460 | * |
1461 | * Need to check DCT0[0] and DCT1[0] to see if only one of them has | |
1462 | * their CSEnable bit on. If so, then SINGLE DIMM case. | |
1463 | */ | |
956b9ba1 | 1464 | edac_dbg(0, "Data width is not 128 bits - need more decoding\n"); |
ddff876d | 1465 | |
1afd3c98 DT |
1466 | /* |
1467 | * Check DRAM Bank Address Mapping values for each DIMM to see if there | |
1468 | * is more than just one DIMM present in unganged mode. Need to check | |
1469 | * both controllers since DIMMs can be placed in either one. | |
1470 | */ | |
525a1b20 BP |
1471 | for (i = 0; i < 2; i++) { |
1472 | u32 dbam = (i ? pvt->dbam1 : pvt->dbam0); | |
1afd3c98 | 1473 | |
57a30854 WW |
1474 | for (j = 0; j < 4; j++) { |
1475 | if (DBAM_DIMM(j, dbam) > 0) { | |
1476 | channels++; | |
1477 | break; | |
1478 | } | |
1479 | } | |
1afd3c98 DT |
1480 | } |
1481 | ||
d16149e8 BP |
1482 | if (channels > 2) |
1483 | channels = 2; | |
1484 | ||
24f9a7fe | 1485 | amd64_info("MCT channel count: %d\n", channels); |
1afd3c98 DT |
1486 | |
1487 | return channels; | |
1afd3c98 DT |
1488 | } |
1489 | ||
f1cbbec9 YG |
1490 | static int f17_early_channel_count(struct amd64_pvt *pvt) |
1491 | { | |
1492 | int i, channels = 0; | |
1493 | ||
1494 | /* SDP Control bit 31 (SdpInit) is clear for unused UMC channels */ | |
4d30d2bc | 1495 | for_each_umc(i) |
f1cbbec9 YG |
1496 | channels += !!(pvt->umc[i].sdp_ctrl & UMC_SDP_INIT); |
1497 | ||
1498 | amd64_info("MCT channel count: %d\n", channels); | |
1499 | ||
1500 | return channels; | |
1501 | } | |
1502 | ||
41d8bfab | 1503 | static int ddr3_cs_size(unsigned i, bool dct_width) |
1afd3c98 | 1504 | { |
41d8bfab BP |
1505 | unsigned shift = 0; |
1506 | int cs_size = 0; | |
1507 | ||
1508 | if (i == 0 || i == 3 || i == 4) | |
1509 | cs_size = -1; | |
1510 | else if (i <= 2) | |
1511 | shift = i; | |
1512 | else if (i == 12) | |
1513 | shift = 7; | |
1514 | else if (!(i & 0x1)) | |
1515 | shift = i >> 1; | |
1516 | else | |
1517 | shift = (i + 1) >> 1; | |
1518 | ||
1519 | if (cs_size != -1) | |
1520 | cs_size = (128 * (1 << !!dct_width)) << shift; | |
1521 | ||
1522 | return cs_size; | |
1523 | } | |
1524 | ||
a597d2a5 AG |
1525 | static int ddr3_lrdimm_cs_size(unsigned i, unsigned rank_multiply) |
1526 | { | |
1527 | unsigned shift = 0; | |
1528 | int cs_size = 0; | |
1529 | ||
1530 | if (i < 4 || i == 6) | |
1531 | cs_size = -1; | |
1532 | else if (i == 12) | |
1533 | shift = 7; | |
1534 | else if (!(i & 0x1)) | |
1535 | shift = i >> 1; | |
1536 | else | |
1537 | shift = (i + 1) >> 1; | |
1538 | ||
1539 | if (cs_size != -1) | |
1540 | cs_size = rank_multiply * (128 << shift); | |
1541 | ||
1542 | return cs_size; | |
1543 | } | |
1544 | ||
1545 | static int ddr4_cs_size(unsigned i) | |
1546 | { | |
1547 | int cs_size = 0; | |
1548 | ||
1549 | if (i == 0) | |
1550 | cs_size = -1; | |
1551 | else if (i == 1) | |
1552 | cs_size = 1024; | |
1553 | else | |
1554 | /* Min cs_size = 1G */ | |
1555 | cs_size = 1024 * (1 << (i >> 1)); | |
1556 | ||
1557 | return cs_size; | |
1558 | } | |
1559 | ||
41d8bfab | 1560 | static int f10_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct, |
a597d2a5 | 1561 | unsigned cs_mode, int cs_mask_nr) |
41d8bfab BP |
1562 | { |
1563 | u32 dclr = dct ? pvt->dclr1 : pvt->dclr0; | |
1564 | ||
1565 | WARN_ON(cs_mode > 11); | |
1433eb99 BP |
1566 | |
1567 | if (pvt->dchr0 & DDR3_MODE || pvt->dchr1 & DDR3_MODE) | |
41d8bfab | 1568 | return ddr3_cs_size(cs_mode, dclr & WIDTH_128); |
1433eb99 | 1569 | else |
41d8bfab BP |
1570 | return ddr2_cs_size(cs_mode, dclr & WIDTH_128); |
1571 | } | |
1572 | ||
1573 | /* | |
1574 | * F15h supports only 64bit DCT interfaces | |
1575 | */ | |
1576 | static int f15_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct, | |
a597d2a5 | 1577 | unsigned cs_mode, int cs_mask_nr) |
41d8bfab BP |
1578 | { |
1579 | WARN_ON(cs_mode > 12); | |
1433eb99 | 1580 | |
41d8bfab | 1581 | return ddr3_cs_size(cs_mode, false); |
1afd3c98 DT |
1582 | } |
1583 | ||
a597d2a5 AG |
1584 | /* F15h M60h supports DDR4 mapping as well.. */ |
1585 | static int f15_m60h_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct, | |
1586 | unsigned cs_mode, int cs_mask_nr) | |
1587 | { | |
1588 | int cs_size; | |
1589 | u32 dcsm = pvt->csels[dct].csmasks[cs_mask_nr]; | |
1590 | ||
1591 | WARN_ON(cs_mode > 12); | |
1592 | ||
1593 | if (pvt->dram_type == MEM_DDR4) { | |
1594 | if (cs_mode > 9) | |
1595 | return -1; | |
1596 | ||
1597 | cs_size = ddr4_cs_size(cs_mode); | |
1598 | } else if (pvt->dram_type == MEM_LRDDR3) { | |
1599 | unsigned rank_multiply = dcsm & 0xf; | |
1600 | ||
1601 | if (rank_multiply == 3) | |
1602 | rank_multiply = 4; | |
1603 | cs_size = ddr3_lrdimm_cs_size(cs_mode, rank_multiply); | |
1604 | } else { | |
1605 | /* Minimum cs size is 512mb for F15hM60h*/ | |
1606 | if (cs_mode == 0x1) | |
1607 | return -1; | |
1608 | ||
1609 | cs_size = ddr3_cs_size(cs_mode, false); | |
1610 | } | |
1611 | ||
1612 | return cs_size; | |
1613 | } | |
1614 | ||
94c1acf2 | 1615 | /* |
18b94f66 | 1616 | * F16h and F15h model 30h have only limited cs_modes. |
94c1acf2 AG |
1617 | */ |
1618 | static int f16_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct, | |
a597d2a5 | 1619 | unsigned cs_mode, int cs_mask_nr) |
94c1acf2 AG |
1620 | { |
1621 | WARN_ON(cs_mode > 12); | |
1622 | ||
1623 | if (cs_mode == 6 || cs_mode == 8 || | |
1624 | cs_mode == 9 || cs_mode == 12) | |
1625 | return -1; | |
1626 | else | |
1627 | return ddr3_cs_size(cs_mode, false); | |
1628 | } | |
1629 | ||
e53a3b26 | 1630 | static int f17_addr_mask_to_cs_size(struct amd64_pvt *pvt, u8 umc, |
f1cbbec9 YG |
1631 | unsigned int cs_mode, int csrow_nr) |
1632 | { | |
e53a3b26 YG |
1633 | u32 addr_mask_orig, addr_mask_deinterleaved; |
1634 | u32 msb, weight, num_zero_bits; | |
1635 | int dimm, size = 0; | |
f1cbbec9 | 1636 | |
e53a3b26 YG |
1637 | /* No Chip Selects are enabled. */ |
1638 | if (!cs_mode) | |
1639 | return size; | |
f1cbbec9 | 1640 | |
e53a3b26 YG |
1641 | /* Requested size of an even CS but none are enabled. */ |
1642 | if (!(cs_mode & CS_EVEN) && !(csrow_nr & 1)) | |
1643 | return size; | |
f1cbbec9 | 1644 | |
e53a3b26 YG |
1645 | /* Requested size of an odd CS but none are enabled. */ |
1646 | if (!(cs_mode & CS_ODD) && (csrow_nr & 1)) | |
1647 | return size; | |
1648 | ||
1649 | /* | |
1650 | * There is one mask per DIMM, and two Chip Selects per DIMM. | |
1651 | * CS0 and CS1 -> DIMM0 | |
1652 | * CS2 and CS3 -> DIMM1 | |
1653 | */ | |
1654 | dimm = csrow_nr >> 1; | |
1655 | ||
81f5090d YG |
1656 | /* Asymmetric dual-rank DIMM support. */ |
1657 | if ((csrow_nr & 1) && (cs_mode & CS_ODD_SECONDARY)) | |
1658 | addr_mask_orig = pvt->csels[umc].csmasks_sec[dimm]; | |
1659 | else | |
1660 | addr_mask_orig = pvt->csels[umc].csmasks[dimm]; | |
e53a3b26 YG |
1661 | |
1662 | /* | |
1663 | * The number of zero bits in the mask is equal to the number of bits | |
1664 | * in a full mask minus the number of bits in the current mask. | |
1665 | * | |
1666 | * The MSB is the number of bits in the full mask because BIT[0] is | |
1667 | * always 0. | |
1668 | */ | |
1669 | msb = fls(addr_mask_orig) - 1; | |
1670 | weight = hweight_long(addr_mask_orig); | |
1671 | num_zero_bits = msb - weight; | |
1672 | ||
1673 | /* Take the number of zero bits off from the top of the mask. */ | |
1674 | addr_mask_deinterleaved = GENMASK_ULL(msb - num_zero_bits, 1); | |
1675 | ||
1676 | edac_dbg(1, "CS%d DIMM%d AddrMasks:\n", csrow_nr, dimm); | |
1677 | edac_dbg(1, " Original AddrMask: 0x%x\n", addr_mask_orig); | |
1678 | edac_dbg(1, " Deinterleaved AddrMask: 0x%x\n", addr_mask_deinterleaved); | |
1679 | ||
1680 | /* Register [31:1] = Address [39:9]. Size is in kBs here. */ | |
1681 | size = (addr_mask_deinterleaved >> 2) + 1; | |
f1cbbec9 YG |
1682 | |
1683 | /* Return size in MBs. */ | |
1684 | return size >> 10; | |
1685 | } | |
1686 | ||
5a5d2371 | 1687 | static void read_dram_ctl_register(struct amd64_pvt *pvt) |
6163b5d4 | 1688 | { |
6163b5d4 | 1689 | |
a4b4bedc | 1690 | if (pvt->fam == 0xf) |
5a5d2371 BP |
1691 | return; |
1692 | ||
7981a28f | 1693 | if (!amd64_read_pci_cfg(pvt->F2, DCT_SEL_LO, &pvt->dct_sel_lo)) { |
956b9ba1 JP |
1694 | edac_dbg(0, "F2x110 (DCTSelLow): 0x%08x, High range addrs at: 0x%x\n", |
1695 | pvt->dct_sel_lo, dct_sel_baseaddr(pvt)); | |
72381bd5 | 1696 | |
956b9ba1 JP |
1697 | edac_dbg(0, " DCTs operate in %s mode\n", |
1698 | (dct_ganging_enabled(pvt) ? "ganged" : "unganged")); | |
72381bd5 BP |
1699 | |
1700 | if (!dct_ganging_enabled(pvt)) | |
956b9ba1 JP |
1701 | edac_dbg(0, " Address range split per DCT: %s\n", |
1702 | (dct_high_range_enabled(pvt) ? "yes" : "no")); | |
72381bd5 | 1703 | |
956b9ba1 JP |
1704 | edac_dbg(0, " data interleave for ECC: %s, DRAM cleared since last warm reset: %s\n", |
1705 | (dct_data_intlv_enabled(pvt) ? "enabled" : "disabled"), | |
1706 | (dct_memory_cleared(pvt) ? "yes" : "no")); | |
72381bd5 | 1707 | |
956b9ba1 JP |
1708 | edac_dbg(0, " channel interleave: %s, " |
1709 | "interleave bits selector: 0x%x\n", | |
1710 | (dct_interleave_enabled(pvt) ? "enabled" : "disabled"), | |
1711 | dct_sel_interleave_addr(pvt)); | |
6163b5d4 DT |
1712 | } |
1713 | ||
7981a28f | 1714 | amd64_read_pci_cfg(pvt->F2, DCT_SEL_HI, &pvt->dct_sel_hi); |
6163b5d4 DT |
1715 | } |
1716 | ||
18b94f66 AG |
1717 | /* |
1718 | * Determine channel (DCT) based on the interleaving mode (see F15h M30h BKDG, | |
1719 | * 2.10.12 Memory Interleaving Modes). | |
1720 | */ | |
1721 | static u8 f15_m30h_determine_channel(struct amd64_pvt *pvt, u64 sys_addr, | |
1722 | u8 intlv_en, int num_dcts_intlv, | |
1723 | u32 dct_sel) | |
1724 | { | |
1725 | u8 channel = 0; | |
1726 | u8 select; | |
1727 | ||
1728 | if (!(intlv_en)) | |
1729 | return (u8)(dct_sel); | |
1730 | ||
1731 | if (num_dcts_intlv == 2) { | |
1732 | select = (sys_addr >> 8) & 0x3; | |
1733 | channel = select ? 0x3 : 0; | |
9d0e8d83 AG |
1734 | } else if (num_dcts_intlv == 4) { |
1735 | u8 intlv_addr = dct_sel_interleave_addr(pvt); | |
1736 | switch (intlv_addr) { | |
1737 | case 0x4: | |
1738 | channel = (sys_addr >> 8) & 0x3; | |
1739 | break; | |
1740 | case 0x5: | |
1741 | channel = (sys_addr >> 9) & 0x3; | |
1742 | break; | |
1743 | } | |
1744 | } | |
18b94f66 AG |
1745 | return channel; |
1746 | } | |
1747 | ||
f71d0a05 | 1748 | /* |
229a7a11 | 1749 | * Determine channel (DCT) based on the interleaving mode: F10h BKDG, 2.8.9 Memory |
f71d0a05 DT |
1750 | * Interleaving Modes. |
1751 | */ | |
b15f0fca | 1752 | static u8 f1x_determine_channel(struct amd64_pvt *pvt, u64 sys_addr, |
229a7a11 | 1753 | bool hi_range_sel, u8 intlv_en) |
6163b5d4 | 1754 | { |
151fa71c | 1755 | u8 dct_sel_high = (pvt->dct_sel_lo >> 1) & 1; |
6163b5d4 DT |
1756 | |
1757 | if (dct_ganging_enabled(pvt)) | |
229a7a11 | 1758 | return 0; |
6163b5d4 | 1759 | |
229a7a11 BP |
1760 | if (hi_range_sel) |
1761 | return dct_sel_high; | |
6163b5d4 | 1762 | |
229a7a11 BP |
1763 | /* |
1764 | * see F2x110[DctSelIntLvAddr] - channel interleave mode | |
1765 | */ | |
1766 | if (dct_interleave_enabled(pvt)) { | |
1767 | u8 intlv_addr = dct_sel_interleave_addr(pvt); | |
1768 | ||
1769 | /* return DCT select function: 0=DCT0, 1=DCT1 */ | |
1770 | if (!intlv_addr) | |
1771 | return sys_addr >> 6 & 1; | |
1772 | ||
1773 | if (intlv_addr & 0x2) { | |
1774 | u8 shift = intlv_addr & 0x1 ? 9 : 6; | |
dc0a50a8 | 1775 | u32 temp = hweight_long((u32) ((sys_addr >> 16) & 0x1F)) & 1; |
229a7a11 BP |
1776 | |
1777 | return ((sys_addr >> shift) & 1) ^ temp; | |
1778 | } | |
1779 | ||
dc0a50a8 YG |
1780 | if (intlv_addr & 0x4) { |
1781 | u8 shift = intlv_addr & 0x1 ? 9 : 8; | |
1782 | ||
1783 | return (sys_addr >> shift) & 1; | |
1784 | } | |
1785 | ||
229a7a11 BP |
1786 | return (sys_addr >> (12 + hweight8(intlv_en))) & 1; |
1787 | } | |
1788 | ||
1789 | if (dct_high_range_enabled(pvt)) | |
1790 | return ~dct_sel_high & 1; | |
6163b5d4 DT |
1791 | |
1792 | return 0; | |
1793 | } | |
1794 | ||
c8e518d5 | 1795 | /* Convert the sys_addr to the normalized DCT address */ |
c7e5301a | 1796 | static u64 f1x_get_norm_dct_addr(struct amd64_pvt *pvt, u8 range, |
c8e518d5 BP |
1797 | u64 sys_addr, bool hi_rng, |
1798 | u32 dct_sel_base_addr) | |
6163b5d4 DT |
1799 | { |
1800 | u64 chan_off; | |
c8e518d5 BP |
1801 | u64 dram_base = get_dram_base(pvt, range); |
1802 | u64 hole_off = f10_dhar_offset(pvt); | |
6f3508f6 | 1803 | u64 dct_sel_base_off = (u64)(pvt->dct_sel_hi & 0xFFFFFC00) << 16; |
6163b5d4 | 1804 | |
c8e518d5 BP |
1805 | if (hi_rng) { |
1806 | /* | |
1807 | * if | |
1808 | * base address of high range is below 4Gb | |
1809 | * (bits [47:27] at [31:11]) | |
1810 | * DRAM address space on this DCT is hoisted above 4Gb && | |
1811 | * sys_addr > 4Gb | |
1812 | * | |
1813 | * remove hole offset from sys_addr | |
1814 | * else | |
1815 | * remove high range offset from sys_addr | |
1816 | */ | |
1817 | if ((!(dct_sel_base_addr >> 16) || | |
1818 | dct_sel_base_addr < dhar_base(pvt)) && | |
972ea17a | 1819 | dhar_valid(pvt) && |
c8e518d5 | 1820 | (sys_addr >= BIT_64(32))) |
bc21fa57 | 1821 | chan_off = hole_off; |
6163b5d4 DT |
1822 | else |
1823 | chan_off = dct_sel_base_off; | |
1824 | } else { | |
c8e518d5 BP |
1825 | /* |
1826 | * if | |
1827 | * we have a valid hole && | |
1828 | * sys_addr > 4Gb | |
1829 | * | |
1830 | * remove hole | |
1831 | * else | |
1832 | * remove dram base to normalize to DCT address | |
1833 | */ | |
972ea17a | 1834 | if (dhar_valid(pvt) && (sys_addr >= BIT_64(32))) |
bc21fa57 | 1835 | chan_off = hole_off; |
6163b5d4 | 1836 | else |
c8e518d5 | 1837 | chan_off = dram_base; |
6163b5d4 DT |
1838 | } |
1839 | ||
10ef6b0d | 1840 | return (sys_addr & GENMASK_ULL(47,6)) - (chan_off & GENMASK_ULL(47,23)); |
6163b5d4 DT |
1841 | } |
1842 | ||
6163b5d4 DT |
1843 | /* |
1844 | * checks if the csrow passed in is marked as SPARED, if so returns the new | |
1845 | * spare row | |
1846 | */ | |
11c75ead | 1847 | static int f10_process_possible_spare(struct amd64_pvt *pvt, u8 dct, int csrow) |
6163b5d4 | 1848 | { |
614ec9d8 BP |
1849 | int tmp_cs; |
1850 | ||
1851 | if (online_spare_swap_done(pvt, dct) && | |
1852 | csrow == online_spare_bad_dramcs(pvt, dct)) { | |
1853 | ||
1854 | for_each_chip_select(tmp_cs, dct, pvt) { | |
1855 | if (chip_select_base(tmp_cs, dct, pvt) & 0x2) { | |
1856 | csrow = tmp_cs; | |
1857 | break; | |
1858 | } | |
1859 | } | |
6163b5d4 DT |
1860 | } |
1861 | return csrow; | |
1862 | } | |
1863 | ||
1864 | /* | |
1865 | * Iterate over the DRAM DCT "base" and "mask" registers looking for a | |
1866 | * SystemAddr match on the specified 'ChannelSelect' and 'NodeID' | |
1867 | * | |
1868 | * Return: | |
1869 | * -EINVAL: NOT FOUND | |
1870 | * 0..csrow = Chip-Select Row | |
1871 | */ | |
c7e5301a | 1872 | static int f1x_lookup_addr_in_dct(u64 in_addr, u8 nid, u8 dct) |
6163b5d4 DT |
1873 | { |
1874 | struct mem_ctl_info *mci; | |
1875 | struct amd64_pvt *pvt; | |
11c75ead | 1876 | u64 cs_base, cs_mask; |
6163b5d4 DT |
1877 | int cs_found = -EINVAL; |
1878 | int csrow; | |
1879 | ||
2ec591ac | 1880 | mci = edac_mc_find(nid); |
6163b5d4 DT |
1881 | if (!mci) |
1882 | return cs_found; | |
1883 | ||
1884 | pvt = mci->pvt_info; | |
1885 | ||
956b9ba1 | 1886 | edac_dbg(1, "input addr: 0x%llx, DCT: %d\n", in_addr, dct); |
6163b5d4 | 1887 | |
11c75ead BP |
1888 | for_each_chip_select(csrow, dct, pvt) { |
1889 | if (!csrow_enabled(csrow, dct, pvt)) | |
6163b5d4 DT |
1890 | continue; |
1891 | ||
11c75ead | 1892 | get_cs_base_and_mask(pvt, csrow, dct, &cs_base, &cs_mask); |
6163b5d4 | 1893 | |
956b9ba1 JP |
1894 | edac_dbg(1, " CSROW=%d CSBase=0x%llx CSMask=0x%llx\n", |
1895 | csrow, cs_base, cs_mask); | |
6163b5d4 | 1896 | |
11c75ead | 1897 | cs_mask = ~cs_mask; |
6163b5d4 | 1898 | |
956b9ba1 JP |
1899 | edac_dbg(1, " (InputAddr & ~CSMask)=0x%llx (CSBase & ~CSMask)=0x%llx\n", |
1900 | (in_addr & cs_mask), (cs_base & cs_mask)); | |
6163b5d4 | 1901 | |
11c75ead | 1902 | if ((in_addr & cs_mask) == (cs_base & cs_mask)) { |
18b94f66 AG |
1903 | if (pvt->fam == 0x15 && pvt->model >= 0x30) { |
1904 | cs_found = csrow; | |
1905 | break; | |
1906 | } | |
11c75ead | 1907 | cs_found = f10_process_possible_spare(pvt, dct, csrow); |
6163b5d4 | 1908 | |
956b9ba1 | 1909 | edac_dbg(1, " MATCH csrow=%d\n", cs_found); |
6163b5d4 DT |
1910 | break; |
1911 | } | |
1912 | } | |
1913 | return cs_found; | |
1914 | } | |
1915 | ||
95b0ef55 BP |
1916 | /* |
1917 | * See F2x10C. Non-interleaved graphics framebuffer memory under the 16G is | |
1918 | * swapped with a region located at the bottom of memory so that the GPU can use | |
1919 | * the interleaved region and thus two channels. | |
1920 | */ | |
b15f0fca | 1921 | static u64 f1x_swap_interleaved_region(struct amd64_pvt *pvt, u64 sys_addr) |
95b0ef55 BP |
1922 | { |
1923 | u32 swap_reg, swap_base, swap_limit, rgn_size, tmp_addr; | |
1924 | ||
a4b4bedc | 1925 | if (pvt->fam == 0x10) { |
95b0ef55 | 1926 | /* only revC3 and revE have that feature */ |
a4b4bedc | 1927 | if (pvt->model < 4 || (pvt->model < 0xa && pvt->stepping < 3)) |
95b0ef55 BP |
1928 | return sys_addr; |
1929 | } | |
1930 | ||
7981a28f | 1931 | amd64_read_pci_cfg(pvt->F2, SWAP_INTLV_REG, &swap_reg); |
95b0ef55 BP |
1932 | |
1933 | if (!(swap_reg & 0x1)) | |
1934 | return sys_addr; | |
1935 | ||
1936 | swap_base = (swap_reg >> 3) & 0x7f; | |
1937 | swap_limit = (swap_reg >> 11) & 0x7f; | |
1938 | rgn_size = (swap_reg >> 20) & 0x7f; | |
1939 | tmp_addr = sys_addr >> 27; | |
1940 | ||
1941 | if (!(sys_addr >> 34) && | |
1942 | (((tmp_addr >= swap_base) && | |
1943 | (tmp_addr <= swap_limit)) || | |
1944 | (tmp_addr < rgn_size))) | |
1945 | return sys_addr ^ (u64)swap_base << 27; | |
1946 | ||
1947 | return sys_addr; | |
1948 | } | |
1949 | ||
f71d0a05 | 1950 | /* For a given @dram_range, check if @sys_addr falls within it. */ |
e761359a | 1951 | static int f1x_match_to_this_node(struct amd64_pvt *pvt, unsigned range, |
33ca0643 | 1952 | u64 sys_addr, int *chan_sel) |
f71d0a05 | 1953 | { |
229a7a11 | 1954 | int cs_found = -EINVAL; |
c8e518d5 | 1955 | u64 chan_addr; |
5d4b58e8 | 1956 | u32 dct_sel_base; |
11c75ead | 1957 | u8 channel; |
229a7a11 | 1958 | bool high_range = false; |
f71d0a05 | 1959 | |
7f19bf75 | 1960 | u8 node_id = dram_dst_node(pvt, range); |
229a7a11 | 1961 | u8 intlv_en = dram_intlv_en(pvt, range); |
7f19bf75 | 1962 | u32 intlv_sel = dram_intlv_sel(pvt, range); |
f71d0a05 | 1963 | |
956b9ba1 JP |
1964 | edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n", |
1965 | range, sys_addr, get_dram_limit(pvt, range)); | |
f71d0a05 | 1966 | |
355fba60 BP |
1967 | if (dhar_valid(pvt) && |
1968 | dhar_base(pvt) <= sys_addr && | |
1969 | sys_addr < BIT_64(32)) { | |
1970 | amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n", | |
1971 | sys_addr); | |
1972 | return -EINVAL; | |
1973 | } | |
1974 | ||
f030ddfb | 1975 | if (intlv_en && (intlv_sel != ((sys_addr >> 12) & intlv_en))) |
f71d0a05 DT |
1976 | return -EINVAL; |
1977 | ||
b15f0fca | 1978 | sys_addr = f1x_swap_interleaved_region(pvt, sys_addr); |
95b0ef55 | 1979 | |
f71d0a05 DT |
1980 | dct_sel_base = dct_sel_baseaddr(pvt); |
1981 | ||
1982 | /* | |
1983 | * check whether addresses >= DctSelBaseAddr[47:27] are to be used to | |
1984 | * select between DCT0 and DCT1. | |
1985 | */ | |
1986 | if (dct_high_range_enabled(pvt) && | |
1987 | !dct_ganging_enabled(pvt) && | |
1988 | ((sys_addr >> 27) >= (dct_sel_base >> 11))) | |
229a7a11 | 1989 | high_range = true; |
f71d0a05 | 1990 | |
b15f0fca | 1991 | channel = f1x_determine_channel(pvt, sys_addr, high_range, intlv_en); |
f71d0a05 | 1992 | |
b15f0fca | 1993 | chan_addr = f1x_get_norm_dct_addr(pvt, range, sys_addr, |
c8e518d5 | 1994 | high_range, dct_sel_base); |
f71d0a05 | 1995 | |
e2f79dbd BP |
1996 | /* Remove node interleaving, see F1x120 */ |
1997 | if (intlv_en) | |
1998 | chan_addr = ((chan_addr >> (12 + hweight8(intlv_en))) << 12) | | |
1999 | (chan_addr & 0xfff); | |
f71d0a05 | 2000 | |
5d4b58e8 | 2001 | /* remove channel interleave */ |
f71d0a05 DT |
2002 | if (dct_interleave_enabled(pvt) && |
2003 | !dct_high_range_enabled(pvt) && | |
2004 | !dct_ganging_enabled(pvt)) { | |
5d4b58e8 BP |
2005 | |
2006 | if (dct_sel_interleave_addr(pvt) != 1) { | |
2007 | if (dct_sel_interleave_addr(pvt) == 0x3) | |
2008 | /* hash 9 */ | |
2009 | chan_addr = ((chan_addr >> 10) << 9) | | |
2010 | (chan_addr & 0x1ff); | |
2011 | else | |
2012 | /* A[6] or hash 6 */ | |
2013 | chan_addr = ((chan_addr >> 7) << 6) | | |
2014 | (chan_addr & 0x3f); | |
2015 | } else | |
2016 | /* A[12] */ | |
2017 | chan_addr = ((chan_addr >> 13) << 12) | | |
2018 | (chan_addr & 0xfff); | |
f71d0a05 DT |
2019 | } |
2020 | ||
956b9ba1 | 2021 | edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr); |
f71d0a05 | 2022 | |
b15f0fca | 2023 | cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, channel); |
f71d0a05 | 2024 | |
33ca0643 | 2025 | if (cs_found >= 0) |
f71d0a05 | 2026 | *chan_sel = channel; |
33ca0643 | 2027 | |
f71d0a05 DT |
2028 | return cs_found; |
2029 | } | |
2030 | ||
18b94f66 AG |
2031 | static int f15_m30h_match_to_this_node(struct amd64_pvt *pvt, unsigned range, |
2032 | u64 sys_addr, int *chan_sel) | |
2033 | { | |
2034 | int cs_found = -EINVAL; | |
2035 | int num_dcts_intlv = 0; | |
2036 | u64 chan_addr, chan_offset; | |
2037 | u64 dct_base, dct_limit; | |
2038 | u32 dct_cont_base_reg, dct_cont_limit_reg, tmp; | |
2039 | u8 channel, alias_channel, leg_mmio_hole, dct_sel, dct_offset_en; | |
2040 | ||
2041 | u64 dhar_offset = f10_dhar_offset(pvt); | |
2042 | u8 intlv_addr = dct_sel_interleave_addr(pvt); | |
2043 | u8 node_id = dram_dst_node(pvt, range); | |
2044 | u8 intlv_en = dram_intlv_en(pvt, range); | |
2045 | ||
2046 | amd64_read_pci_cfg(pvt->F1, DRAM_CONT_BASE, &dct_cont_base_reg); | |
2047 | amd64_read_pci_cfg(pvt->F1, DRAM_CONT_LIMIT, &dct_cont_limit_reg); | |
2048 | ||
2049 | dct_offset_en = (u8) ((dct_cont_base_reg >> 3) & BIT(0)); | |
2050 | dct_sel = (u8) ((dct_cont_base_reg >> 4) & 0x7); | |
2051 | ||
2052 | edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n", | |
2053 | range, sys_addr, get_dram_limit(pvt, range)); | |
2054 | ||
2055 | if (!(get_dram_base(pvt, range) <= sys_addr) && | |
2056 | !(get_dram_limit(pvt, range) >= sys_addr)) | |
2057 | return -EINVAL; | |
2058 | ||
2059 | if (dhar_valid(pvt) && | |
2060 | dhar_base(pvt) <= sys_addr && | |
2061 | sys_addr < BIT_64(32)) { | |
2062 | amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n", | |
2063 | sys_addr); | |
2064 | return -EINVAL; | |
2065 | } | |
2066 | ||
2067 | /* Verify sys_addr is within DCT Range. */ | |
4fc06b31 AG |
2068 | dct_base = (u64) dct_sel_baseaddr(pvt); |
2069 | dct_limit = (dct_cont_limit_reg >> 11) & 0x1FFF; | |
18b94f66 AG |
2070 | |
2071 | if (!(dct_cont_base_reg & BIT(0)) && | |
4fc06b31 AG |
2072 | !(dct_base <= (sys_addr >> 27) && |
2073 | dct_limit >= (sys_addr >> 27))) | |
18b94f66 AG |
2074 | return -EINVAL; |
2075 | ||
2076 | /* Verify number of dct's that participate in channel interleaving. */ | |
2077 | num_dcts_intlv = (int) hweight8(intlv_en); | |
2078 | ||
2079 | if (!(num_dcts_intlv % 2 == 0) || (num_dcts_intlv > 4)) | |
2080 | return -EINVAL; | |
2081 | ||
dc0a50a8 YG |
2082 | if (pvt->model >= 0x60) |
2083 | channel = f1x_determine_channel(pvt, sys_addr, false, intlv_en); | |
2084 | else | |
2085 | channel = f15_m30h_determine_channel(pvt, sys_addr, intlv_en, | |
2086 | num_dcts_intlv, dct_sel); | |
18b94f66 AG |
2087 | |
2088 | /* Verify we stay within the MAX number of channels allowed */ | |
7f3f5240 | 2089 | if (channel > 3) |
18b94f66 AG |
2090 | return -EINVAL; |
2091 | ||
2092 | leg_mmio_hole = (u8) (dct_cont_base_reg >> 1 & BIT(0)); | |
2093 | ||
2094 | /* Get normalized DCT addr */ | |
2095 | if (leg_mmio_hole && (sys_addr >= BIT_64(32))) | |
2096 | chan_offset = dhar_offset; | |
2097 | else | |
4fc06b31 | 2098 | chan_offset = dct_base << 27; |
18b94f66 AG |
2099 | |
2100 | chan_addr = sys_addr - chan_offset; | |
2101 | ||
2102 | /* remove channel interleave */ | |
2103 | if (num_dcts_intlv == 2) { | |
2104 | if (intlv_addr == 0x4) | |
2105 | chan_addr = ((chan_addr >> 9) << 8) | | |
2106 | (chan_addr & 0xff); | |
2107 | else if (intlv_addr == 0x5) | |
2108 | chan_addr = ((chan_addr >> 10) << 9) | | |
2109 | (chan_addr & 0x1ff); | |
2110 | else | |
2111 | return -EINVAL; | |
2112 | ||
2113 | } else if (num_dcts_intlv == 4) { | |
2114 | if (intlv_addr == 0x4) | |
2115 | chan_addr = ((chan_addr >> 10) << 8) | | |
2116 | (chan_addr & 0xff); | |
2117 | else if (intlv_addr == 0x5) | |
2118 | chan_addr = ((chan_addr >> 11) << 9) | | |
2119 | (chan_addr & 0x1ff); | |
2120 | else | |
2121 | return -EINVAL; | |
2122 | } | |
2123 | ||
2124 | if (dct_offset_en) { | |
2125 | amd64_read_pci_cfg(pvt->F1, | |
2126 | DRAM_CONT_HIGH_OFF + (int) channel * 4, | |
2127 | &tmp); | |
4fc06b31 | 2128 | chan_addr += (u64) ((tmp >> 11) & 0xfff) << 27; |
18b94f66 AG |
2129 | } |
2130 | ||
2131 | f15h_select_dct(pvt, channel); | |
2132 | ||
2133 | edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr); | |
2134 | ||
2135 | /* | |
2136 | * Find Chip select: | |
2137 | * if channel = 3, then alias it to 1. This is because, in F15 M30h, | |
2138 | * there is support for 4 DCT's, but only 2 are currently functional. | |
2139 | * They are DCT0 and DCT3. But we have read all registers of DCT3 into | |
2140 | * pvt->csels[1]. So we need to use '1' here to get correct info. | |
2141 | * Refer F15 M30h BKDG Section 2.10 and 2.10.3 for clarifications. | |
2142 | */ | |
2143 | alias_channel = (channel == 3) ? 1 : channel; | |
2144 | ||
2145 | cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, alias_channel); | |
2146 | ||
2147 | if (cs_found >= 0) | |
2148 | *chan_sel = alias_channel; | |
2149 | ||
2150 | return cs_found; | |
2151 | } | |
2152 | ||
2153 | static int f1x_translate_sysaddr_to_cs(struct amd64_pvt *pvt, | |
2154 | u64 sys_addr, | |
2155 | int *chan_sel) | |
f71d0a05 | 2156 | { |
e761359a BP |
2157 | int cs_found = -EINVAL; |
2158 | unsigned range; | |
f71d0a05 | 2159 | |
7f19bf75 | 2160 | for (range = 0; range < DRAM_RANGES; range++) { |
7f19bf75 | 2161 | if (!dram_rw(pvt, range)) |
f71d0a05 DT |
2162 | continue; |
2163 | ||
18b94f66 AG |
2164 | if (pvt->fam == 0x15 && pvt->model >= 0x30) |
2165 | cs_found = f15_m30h_match_to_this_node(pvt, range, | |
2166 | sys_addr, | |
2167 | chan_sel); | |
f71d0a05 | 2168 | |
18b94f66 AG |
2169 | else if ((get_dram_base(pvt, range) <= sys_addr) && |
2170 | (get_dram_limit(pvt, range) >= sys_addr)) { | |
b15f0fca | 2171 | cs_found = f1x_match_to_this_node(pvt, range, |
33ca0643 | 2172 | sys_addr, chan_sel); |
f71d0a05 DT |
2173 | if (cs_found >= 0) |
2174 | break; | |
2175 | } | |
2176 | } | |
2177 | return cs_found; | |
2178 | } | |
2179 | ||
2180 | /* | |
bdc30a0c BP |
2181 | * For reference see "2.8.5 Routing DRAM Requests" in F10 BKDG. This code maps |
2182 | * a @sys_addr to NodeID, DCT (channel) and chip select (CSROW). | |
f71d0a05 | 2183 | * |
bdc30a0c BP |
2184 | * The @sys_addr is usually an error address received from the hardware |
2185 | * (MCX_ADDR). | |
f71d0a05 | 2186 | */ |
b15f0fca | 2187 | static void f1x_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr, |
33ca0643 | 2188 | struct err_info *err) |
f71d0a05 DT |
2189 | { |
2190 | struct amd64_pvt *pvt = mci->pvt_info; | |
f71d0a05 | 2191 | |
33ca0643 | 2192 | error_address_to_page_and_offset(sys_addr, err); |
ab5a503c | 2193 | |
33ca0643 BP |
2194 | err->csrow = f1x_translate_sysaddr_to_cs(pvt, sys_addr, &err->channel); |
2195 | if (err->csrow < 0) { | |
2196 | err->err_code = ERR_CSROW; | |
bdc30a0c BP |
2197 | return; |
2198 | } | |
2199 | ||
bdc30a0c BP |
2200 | /* |
2201 | * We need the syndromes for channel detection only when we're | |
2202 | * ganged. Otherwise @chan should already contain the channel at | |
2203 | * this point. | |
2204 | */ | |
a97fa68e | 2205 | if (dct_ganging_enabled(pvt)) |
33ca0643 | 2206 | err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome); |
f71d0a05 DT |
2207 | } |
2208 | ||
f71d0a05 | 2209 | /* |
8566c4df | 2210 | * debug routine to display the memory sizes of all logical DIMMs and its |
cb328507 | 2211 | * CSROWs |
f71d0a05 | 2212 | */ |
d1ea71cd | 2213 | static void debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl) |
f71d0a05 | 2214 | { |
bb89f5a0 | 2215 | int dimm, size0, size1; |
525a1b20 BP |
2216 | u32 *dcsb = ctrl ? pvt->csels[1].csbases : pvt->csels[0].csbases; |
2217 | u32 dbam = ctrl ? pvt->dbam1 : pvt->dbam0; | |
f71d0a05 | 2218 | |
a4b4bedc | 2219 | if (pvt->fam == 0xf) { |
8566c4df | 2220 | /* K8 families < revF not supported yet */ |
1433eb99 | 2221 | if (pvt->ext_model < K8_REV_F) |
8566c4df BP |
2222 | return; |
2223 | else | |
2224 | WARN_ON(ctrl != 0); | |
2225 | } | |
2226 | ||
7981a28f AG |
2227 | if (pvt->fam == 0x10) { |
2228 | dbam = (ctrl && !dct_ganging_enabled(pvt)) ? pvt->dbam1 | |
2229 | : pvt->dbam0; | |
2230 | dcsb = (ctrl && !dct_ganging_enabled(pvt)) ? | |
2231 | pvt->csels[1].csbases : | |
2232 | pvt->csels[0].csbases; | |
2233 | } else if (ctrl) { | |
2234 | dbam = pvt->dbam0; | |
2235 | dcsb = pvt->csels[1].csbases; | |
2236 | } | |
956b9ba1 JP |
2237 | edac_dbg(1, "F2x%d80 (DRAM Bank Address Mapping): 0x%08x\n", |
2238 | ctrl, dbam); | |
f71d0a05 | 2239 | |
8566c4df BP |
2240 | edac_printk(KERN_DEBUG, EDAC_MC, "DCT%d chip selects:\n", ctrl); |
2241 | ||
f71d0a05 DT |
2242 | /* Dump memory sizes for DIMM and its CSROWs */ |
2243 | for (dimm = 0; dimm < 4; dimm++) { | |
2244 | ||
2245 | size0 = 0; | |
11c75ead | 2246 | if (dcsb[dimm*2] & DCSB_CS_ENABLE) |
07ed82ef YG |
2247 | /* |
2248 | * For F15m60h, we need multiplier for LRDIMM cs_size | |
2249 | * calculation. We pass dimm value to the dbam_to_cs | |
a597d2a5 AG |
2250 | * mapper so we can find the multiplier from the |
2251 | * corresponding DCSM. | |
2252 | */ | |
41d8bfab | 2253 | size0 = pvt->ops->dbam_to_cs(pvt, ctrl, |
a597d2a5 AG |
2254 | DBAM_DIMM(dimm, dbam), |
2255 | dimm); | |
f71d0a05 DT |
2256 | |
2257 | size1 = 0; | |
11c75ead | 2258 | if (dcsb[dimm*2 + 1] & DCSB_CS_ENABLE) |
41d8bfab | 2259 | size1 = pvt->ops->dbam_to_cs(pvt, ctrl, |
a597d2a5 AG |
2260 | DBAM_DIMM(dimm, dbam), |
2261 | dimm); | |
f71d0a05 | 2262 | |
24f9a7fe | 2263 | amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n", |
bb89f5a0 BP |
2264 | dimm * 2, size0, |
2265 | dimm * 2 + 1, size1); | |
f71d0a05 DT |
2266 | } |
2267 | } | |
2268 | ||
d1ea71cd | 2269 | static struct amd64_family_type family_types[] = { |
4d37607a | 2270 | [K8_CPUS] = { |
0092b20d | 2271 | .ctl_name = "K8", |
8d5b5d9c | 2272 | .f1_id = PCI_DEVICE_ID_AMD_K8_NB_ADDRMAP, |
3f37a36b | 2273 | .f2_id = PCI_DEVICE_ID_AMD_K8_NB_MEMCTL, |
5e4c5527 | 2274 | .max_mcs = 2, |
4d37607a | 2275 | .ops = { |
1433eb99 | 2276 | .early_channel_count = k8_early_channel_count, |
1433eb99 BP |
2277 | .map_sysaddr_to_csrow = k8_map_sysaddr_to_csrow, |
2278 | .dbam_to_cs = k8_dbam_to_chip_select, | |
4d37607a DT |
2279 | } |
2280 | }, | |
2281 | [F10_CPUS] = { | |
0092b20d | 2282 | .ctl_name = "F10h", |
8d5b5d9c | 2283 | .f1_id = PCI_DEVICE_ID_AMD_10H_NB_MAP, |
3f37a36b | 2284 | .f2_id = PCI_DEVICE_ID_AMD_10H_NB_DRAM, |
5e4c5527 | 2285 | .max_mcs = 2, |
4d37607a | 2286 | .ops = { |
7d20d14d | 2287 | .early_channel_count = f1x_early_channel_count, |
b15f0fca | 2288 | .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow, |
1433eb99 | 2289 | .dbam_to_cs = f10_dbam_to_chip_select, |
b2b0c605 BP |
2290 | } |
2291 | }, | |
2292 | [F15_CPUS] = { | |
2293 | .ctl_name = "F15h", | |
df71a053 | 2294 | .f1_id = PCI_DEVICE_ID_AMD_15H_NB_F1, |
3f37a36b | 2295 | .f2_id = PCI_DEVICE_ID_AMD_15H_NB_F2, |
5e4c5527 | 2296 | .max_mcs = 2, |
b2b0c605 | 2297 | .ops = { |
7d20d14d | 2298 | .early_channel_count = f1x_early_channel_count, |
b15f0fca | 2299 | .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow, |
41d8bfab | 2300 | .dbam_to_cs = f15_dbam_to_chip_select, |
4d37607a DT |
2301 | } |
2302 | }, | |
18b94f66 AG |
2303 | [F15_M30H_CPUS] = { |
2304 | .ctl_name = "F15h_M30h", | |
2305 | .f1_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1, | |
3f37a36b | 2306 | .f2_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F2, |
5e4c5527 | 2307 | .max_mcs = 2, |
18b94f66 AG |
2308 | .ops = { |
2309 | .early_channel_count = f1x_early_channel_count, | |
2310 | .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow, | |
2311 | .dbam_to_cs = f16_dbam_to_chip_select, | |
18b94f66 AG |
2312 | } |
2313 | }, | |
a597d2a5 AG |
2314 | [F15_M60H_CPUS] = { |
2315 | .ctl_name = "F15h_M60h", | |
2316 | .f1_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1, | |
3f37a36b | 2317 | .f2_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F2, |
5e4c5527 | 2318 | .max_mcs = 2, |
a597d2a5 AG |
2319 | .ops = { |
2320 | .early_channel_count = f1x_early_channel_count, | |
2321 | .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow, | |
2322 | .dbam_to_cs = f15_m60h_dbam_to_chip_select, | |
2323 | } | |
2324 | }, | |
94c1acf2 AG |
2325 | [F16_CPUS] = { |
2326 | .ctl_name = "F16h", | |
2327 | .f1_id = PCI_DEVICE_ID_AMD_16H_NB_F1, | |
3f37a36b | 2328 | .f2_id = PCI_DEVICE_ID_AMD_16H_NB_F2, |
5e4c5527 | 2329 | .max_mcs = 2, |
94c1acf2 AG |
2330 | .ops = { |
2331 | .early_channel_count = f1x_early_channel_count, | |
2332 | .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow, | |
2333 | .dbam_to_cs = f16_dbam_to_chip_select, | |
94c1acf2 AG |
2334 | } |
2335 | }, | |
85a8885b AG |
2336 | [F16_M30H_CPUS] = { |
2337 | .ctl_name = "F16h_M30h", | |
2338 | .f1_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F1, | |
3f37a36b | 2339 | .f2_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F2, |
5e4c5527 | 2340 | .max_mcs = 2, |
85a8885b AG |
2341 | .ops = { |
2342 | .early_channel_count = f1x_early_channel_count, | |
2343 | .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow, | |
2344 | .dbam_to_cs = f16_dbam_to_chip_select, | |
85a8885b AG |
2345 | } |
2346 | }, | |
f1cbbec9 YG |
2347 | [F17_CPUS] = { |
2348 | .ctl_name = "F17h", | |
2349 | .f0_id = PCI_DEVICE_ID_AMD_17H_DF_F0, | |
2350 | .f6_id = PCI_DEVICE_ID_AMD_17H_DF_F6, | |
5e4c5527 | 2351 | .max_mcs = 2, |
f1cbbec9 YG |
2352 | .ops = { |
2353 | .early_channel_count = f17_early_channel_count, | |
e53a3b26 | 2354 | .dbam_to_cs = f17_addr_mask_to_cs_size, |
f1cbbec9 YG |
2355 | } |
2356 | }, | |
8960de4a MJ |
2357 | [F17_M10H_CPUS] = { |
2358 | .ctl_name = "F17h_M10h", | |
2359 | .f0_id = PCI_DEVICE_ID_AMD_17H_M10H_DF_F0, | |
2360 | .f6_id = PCI_DEVICE_ID_AMD_17H_M10H_DF_F6, | |
5e4c5527 | 2361 | .max_mcs = 2, |
8960de4a MJ |
2362 | .ops = { |
2363 | .early_channel_count = f17_early_channel_count, | |
e53a3b26 | 2364 | .dbam_to_cs = f17_addr_mask_to_cs_size, |
8960de4a MJ |
2365 | } |
2366 | }, | |
6e846239 YG |
2367 | [F17_M30H_CPUS] = { |
2368 | .ctl_name = "F17h_M30h", | |
2369 | .f0_id = PCI_DEVICE_ID_AMD_17H_M30H_DF_F0, | |
2370 | .f6_id = PCI_DEVICE_ID_AMD_17H_M30H_DF_F6, | |
5e4c5527 | 2371 | .max_mcs = 8, |
6e846239 YG |
2372 | .ops = { |
2373 | .early_channel_count = f17_early_channel_count, | |
e53a3b26 | 2374 | .dbam_to_cs = f17_addr_mask_to_cs_size, |
6e846239 YG |
2375 | } |
2376 | }, | |
b6bea24d AM |
2377 | [F17_M60H_CPUS] = { |
2378 | .ctl_name = "F17h_M60h", | |
2379 | .f0_id = PCI_DEVICE_ID_AMD_17H_M60H_DF_F0, | |
2380 | .f6_id = PCI_DEVICE_ID_AMD_17H_M60H_DF_F6, | |
2381 | .max_mcs = 2, | |
2382 | .ops = { | |
2383 | .early_channel_count = f17_early_channel_count, | |
2384 | .dbam_to_cs = f17_addr_mask_to_cs_size, | |
2385 | } | |
2386 | }, | |
3e443eb3 IV |
2387 | [F17_M70H_CPUS] = { |
2388 | .ctl_name = "F17h_M70h", | |
2389 | .f0_id = PCI_DEVICE_ID_AMD_17H_M70H_DF_F0, | |
2390 | .f6_id = PCI_DEVICE_ID_AMD_17H_M70H_DF_F6, | |
5e4c5527 | 2391 | .max_mcs = 2, |
3e443eb3 IV |
2392 | .ops = { |
2393 | .early_channel_count = f17_early_channel_count, | |
2394 | .dbam_to_cs = f17_addr_mask_to_cs_size, | |
2395 | } | |
2396 | }, | |
2eb61c91 YG |
2397 | [F19_CPUS] = { |
2398 | .ctl_name = "F19h", | |
2399 | .f0_id = PCI_DEVICE_ID_AMD_19H_DF_F0, | |
2400 | .f6_id = PCI_DEVICE_ID_AMD_19H_DF_F6, | |
2401 | .max_mcs = 8, | |
2402 | .ops = { | |
2403 | .early_channel_count = f17_early_channel_count, | |
2404 | .dbam_to_cs = f17_addr_mask_to_cs_size, | |
2405 | } | |
2406 | }, | |
4d37607a DT |
2407 | }; |
2408 | ||
b1289d6f | 2409 | /* |
bfc04aec BP |
2410 | * These are tables of eigenvectors (one per line) which can be used for the |
2411 | * construction of the syndrome tables. The modified syndrome search algorithm | |
2412 | * uses those to find the symbol in error and thus the DIMM. | |
b1289d6f | 2413 | * |
bfc04aec | 2414 | * Algorithm courtesy of Ross LaFetra from AMD. |
b1289d6f | 2415 | */ |
c7e5301a | 2416 | static const u16 x4_vectors[] = { |
bfc04aec BP |
2417 | 0x2f57, 0x1afe, 0x66cc, 0xdd88, |
2418 | 0x11eb, 0x3396, 0x7f4c, 0xeac8, | |
2419 | 0x0001, 0x0002, 0x0004, 0x0008, | |
2420 | 0x1013, 0x3032, 0x4044, 0x8088, | |
2421 | 0x106b, 0x30d6, 0x70fc, 0xe0a8, | |
2422 | 0x4857, 0xc4fe, 0x13cc, 0x3288, | |
2423 | 0x1ac5, 0x2f4a, 0x5394, 0xa1e8, | |
2424 | 0x1f39, 0x251e, 0xbd6c, 0x6bd8, | |
2425 | 0x15c1, 0x2a42, 0x89ac, 0x4758, | |
2426 | 0x2b03, 0x1602, 0x4f0c, 0xca08, | |
2427 | 0x1f07, 0x3a0e, 0x6b04, 0xbd08, | |
2428 | 0x8ba7, 0x465e, 0x244c, 0x1cc8, | |
2429 | 0x2b87, 0x164e, 0x642c, 0xdc18, | |
2430 | 0x40b9, 0x80de, 0x1094, 0x20e8, | |
2431 | 0x27db, 0x1eb6, 0x9dac, 0x7b58, | |
2432 | 0x11c1, 0x2242, 0x84ac, 0x4c58, | |
2433 | 0x1be5, 0x2d7a, 0x5e34, 0xa718, | |
2434 | 0x4b39, 0x8d1e, 0x14b4, 0x28d8, | |
2435 | 0x4c97, 0xc87e, 0x11fc, 0x33a8, | |
2436 | 0x8e97, 0x497e, 0x2ffc, 0x1aa8, | |
2437 | 0x16b3, 0x3d62, 0x4f34, 0x8518, | |
2438 | 0x1e2f, 0x391a, 0x5cac, 0xf858, | |
2439 | 0x1d9f, 0x3b7a, 0x572c, 0xfe18, | |
2440 | 0x15f5, 0x2a5a, 0x5264, 0xa3b8, | |
2441 | 0x1dbb, 0x3b66, 0x715c, 0xe3f8, | |
2442 | 0x4397, 0xc27e, 0x17fc, 0x3ea8, | |
2443 | 0x1617, 0x3d3e, 0x6464, 0xb8b8, | |
2444 | 0x23ff, 0x12aa, 0xab6c, 0x56d8, | |
2445 | 0x2dfb, 0x1ba6, 0x913c, 0x7328, | |
2446 | 0x185d, 0x2ca6, 0x7914, 0x9e28, | |
2447 | 0x171b, 0x3e36, 0x7d7c, 0xebe8, | |
2448 | 0x4199, 0x82ee, 0x19f4, 0x2e58, | |
2449 | 0x4807, 0xc40e, 0x130c, 0x3208, | |
2450 | 0x1905, 0x2e0a, 0x5804, 0xac08, | |
2451 | 0x213f, 0x132a, 0xadfc, 0x5ba8, | |
2452 | 0x19a9, 0x2efe, 0xb5cc, 0x6f88, | |
b1289d6f DT |
2453 | }; |
2454 | ||
c7e5301a | 2455 | static const u16 x8_vectors[] = { |
bfc04aec BP |
2456 | 0x0145, 0x028a, 0x2374, 0x43c8, 0xa1f0, 0x0520, 0x0a40, 0x1480, |
2457 | 0x0211, 0x0422, 0x0844, 0x1088, 0x01b0, 0x44e0, 0x23c0, 0xed80, | |
2458 | 0x1011, 0x0116, 0x022c, 0x0458, 0x08b0, 0x8c60, 0x2740, 0x4e80, | |
2459 | 0x0411, 0x0822, 0x1044, 0x0158, 0x02b0, 0x2360, 0x46c0, 0xab80, | |
2460 | 0x0811, 0x1022, 0x012c, 0x0258, 0x04b0, 0x4660, 0x8cc0, 0x2780, | |
2461 | 0x2071, 0x40e2, 0xa0c4, 0x0108, 0x0210, 0x0420, 0x0840, 0x1080, | |
2462 | 0x4071, 0x80e2, 0x0104, 0x0208, 0x0410, 0x0820, 0x1040, 0x2080, | |
2463 | 0x8071, 0x0102, 0x0204, 0x0408, 0x0810, 0x1020, 0x2040, 0x4080, | |
2464 | 0x019d, 0x03d6, 0x136c, 0x2198, 0x50b0, 0xb2e0, 0x0740, 0x0e80, | |
2465 | 0x0189, 0x03ea, 0x072c, 0x0e58, 0x1cb0, 0x56e0, 0x37c0, 0xf580, | |
2466 | 0x01fd, 0x0376, 0x06ec, 0x0bb8, 0x1110, 0x2220, 0x4440, 0x8880, | |
2467 | 0x0163, 0x02c6, 0x1104, 0x0758, 0x0eb0, 0x2be0, 0x6140, 0xc280, | |
2468 | 0x02fd, 0x01c6, 0x0b5c, 0x1108, 0x07b0, 0x25a0, 0x8840, 0x6180, | |
2469 | 0x0801, 0x012e, 0x025c, 0x04b8, 0x1370, 0x26e0, 0x57c0, 0xb580, | |
2470 | 0x0401, 0x0802, 0x015c, 0x02b8, 0x22b0, 0x13e0, 0x7140, 0xe280, | |
2471 | 0x0201, 0x0402, 0x0804, 0x01b8, 0x11b0, 0x31a0, 0x8040, 0x7180, | |
2472 | 0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080, | |
2473 | 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, | |
2474 | 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000, | |
2475 | }; | |
2476 | ||
c7e5301a | 2477 | static int decode_syndrome(u16 syndrome, const u16 *vectors, unsigned num_vecs, |
d34a6ecd | 2478 | unsigned v_dim) |
b1289d6f | 2479 | { |
bfc04aec BP |
2480 | unsigned int i, err_sym; |
2481 | ||
2482 | for (err_sym = 0; err_sym < num_vecs / v_dim; err_sym++) { | |
2483 | u16 s = syndrome; | |
d34a6ecd BP |
2484 | unsigned v_idx = err_sym * v_dim; |
2485 | unsigned v_end = (err_sym + 1) * v_dim; | |
bfc04aec BP |
2486 | |
2487 | /* walk over all 16 bits of the syndrome */ | |
2488 | for (i = 1; i < (1U << 16); i <<= 1) { | |
2489 | ||
2490 | /* if bit is set in that eigenvector... */ | |
2491 | if (v_idx < v_end && vectors[v_idx] & i) { | |
2492 | u16 ev_comp = vectors[v_idx++]; | |
2493 | ||
2494 | /* ... and bit set in the modified syndrome, */ | |
2495 | if (s & i) { | |
2496 | /* remove it. */ | |
2497 | s ^= ev_comp; | |
4d37607a | 2498 | |
bfc04aec BP |
2499 | if (!s) |
2500 | return err_sym; | |
2501 | } | |
b1289d6f | 2502 | |
bfc04aec BP |
2503 | } else if (s & i) |
2504 | /* can't get to zero, move to next symbol */ | |
2505 | break; | |
2506 | } | |
b1289d6f DT |
2507 | } |
2508 | ||
956b9ba1 | 2509 | edac_dbg(0, "syndrome(%x) not found\n", syndrome); |
b1289d6f DT |
2510 | return -1; |
2511 | } | |
d27bf6fa | 2512 | |
bfc04aec BP |
2513 | static int map_err_sym_to_channel(int err_sym, int sym_size) |
2514 | { | |
2515 | if (sym_size == 4) | |
2516 | switch (err_sym) { | |
2517 | case 0x20: | |
2518 | case 0x21: | |
2519 | return 0; | |
bfc04aec BP |
2520 | case 0x22: |
2521 | case 0x23: | |
2522 | return 1; | |
bfc04aec BP |
2523 | default: |
2524 | return err_sym >> 4; | |
bfc04aec BP |
2525 | } |
2526 | /* x8 symbols */ | |
2527 | else | |
2528 | switch (err_sym) { | |
2529 | /* imaginary bits not in a DIMM */ | |
2530 | case 0x10: | |
2531 | WARN(1, KERN_ERR "Invalid error symbol: 0x%x\n", | |
2532 | err_sym); | |
2533 | return -1; | |
bfc04aec BP |
2534 | case 0x11: |
2535 | return 0; | |
bfc04aec BP |
2536 | case 0x12: |
2537 | return 1; | |
bfc04aec BP |
2538 | default: |
2539 | return err_sym >> 3; | |
bfc04aec BP |
2540 | } |
2541 | return -1; | |
2542 | } | |
2543 | ||
2544 | static int get_channel_from_ecc_syndrome(struct mem_ctl_info *mci, u16 syndrome) | |
2545 | { | |
2546 | struct amd64_pvt *pvt = mci->pvt_info; | |
ad6a32e9 BP |
2547 | int err_sym = -1; |
2548 | ||
a3b7db09 | 2549 | if (pvt->ecc_sym_sz == 8) |
ad6a32e9 BP |
2550 | err_sym = decode_syndrome(syndrome, x8_vectors, |
2551 | ARRAY_SIZE(x8_vectors), | |
a3b7db09 BP |
2552 | pvt->ecc_sym_sz); |
2553 | else if (pvt->ecc_sym_sz == 4) | |
ad6a32e9 BP |
2554 | err_sym = decode_syndrome(syndrome, x4_vectors, |
2555 | ARRAY_SIZE(x4_vectors), | |
a3b7db09 | 2556 | pvt->ecc_sym_sz); |
ad6a32e9 | 2557 | else { |
a3b7db09 | 2558 | amd64_warn("Illegal syndrome type: %u\n", pvt->ecc_sym_sz); |
ad6a32e9 | 2559 | return err_sym; |
bfc04aec | 2560 | } |
ad6a32e9 | 2561 | |
a3b7db09 | 2562 | return map_err_sym_to_channel(err_sym, pvt->ecc_sym_sz); |
bfc04aec BP |
2563 | } |
2564 | ||
e70984d9 | 2565 | static void __log_ecc_error(struct mem_ctl_info *mci, struct err_info *err, |
33ca0643 | 2566 | u8 ecc_type) |
d27bf6fa | 2567 | { |
33ca0643 BP |
2568 | enum hw_event_mc_err_type err_type; |
2569 | const char *string; | |
d27bf6fa | 2570 | |
33ca0643 BP |
2571 | if (ecc_type == 2) |
2572 | err_type = HW_EVENT_ERR_CORRECTED; | |
2573 | else if (ecc_type == 1) | |
2574 | err_type = HW_EVENT_ERR_UNCORRECTED; | |
d12a969e YG |
2575 | else if (ecc_type == 3) |
2576 | err_type = HW_EVENT_ERR_DEFERRED; | |
33ca0643 BP |
2577 | else { |
2578 | WARN(1, "Something is rotten in the state of Denmark.\n"); | |
d27bf6fa DT |
2579 | return; |
2580 | } | |
2581 | ||
33ca0643 BP |
2582 | switch (err->err_code) { |
2583 | case DECODE_OK: | |
2584 | string = ""; | |
2585 | break; | |
2586 | case ERR_NODE: | |
2587 | string = "Failed to map error addr to a node"; | |
2588 | break; | |
2589 | case ERR_CSROW: | |
2590 | string = "Failed to map error addr to a csrow"; | |
2591 | break; | |
2592 | case ERR_CHANNEL: | |
713ad546 YG |
2593 | string = "Unknown syndrome - possible error reporting race"; |
2594 | break; | |
2595 | case ERR_SYND: | |
2596 | string = "MCA_SYND not valid - unknown syndrome and csrow"; | |
2597 | break; | |
2598 | case ERR_NORM_ADDR: | |
2599 | string = "Cannot decode normalized address"; | |
33ca0643 BP |
2600 | break; |
2601 | default: | |
2602 | string = "WTF error"; | |
2603 | break; | |
d27bf6fa | 2604 | } |
33ca0643 BP |
2605 | |
2606 | edac_mc_handle_error(err_type, mci, 1, | |
2607 | err->page, err->offset, err->syndrome, | |
2608 | err->csrow, err->channel, -1, | |
2609 | string, ""); | |
d27bf6fa DT |
2610 | } |
2611 | ||
df781d03 | 2612 | static inline void decode_bus_error(int node_id, struct mce *m) |
d27bf6fa | 2613 | { |
0c510cc8 DB |
2614 | struct mem_ctl_info *mci; |
2615 | struct amd64_pvt *pvt; | |
f192c7b1 | 2616 | u8 ecc_type = (m->status >> 45) & 0x3; |
66fed2d4 BP |
2617 | u8 xec = XEC(m->status, 0x1f); |
2618 | u16 ec = EC(m->status); | |
33ca0643 BP |
2619 | u64 sys_addr; |
2620 | struct err_info err; | |
d27bf6fa | 2621 | |
0c510cc8 DB |
2622 | mci = edac_mc_find(node_id); |
2623 | if (!mci) | |
2624 | return; | |
2625 | ||
2626 | pvt = mci->pvt_info; | |
2627 | ||
66fed2d4 | 2628 | /* Bail out early if this was an 'observed' error */ |
5980bb9c | 2629 | if (PP(ec) == NBSL_PP_OBS) |
b70ef010 | 2630 | return; |
d27bf6fa | 2631 | |
ecaf5606 BP |
2632 | /* Do only ECC errors */ |
2633 | if (xec && xec != F10_NBSL_EXT_ERR_ECC) | |
d27bf6fa | 2634 | return; |
d27bf6fa | 2635 | |
33ca0643 BP |
2636 | memset(&err, 0, sizeof(err)); |
2637 | ||
a4b4bedc | 2638 | sys_addr = get_error_address(pvt, m); |
33ca0643 | 2639 | |
ecaf5606 | 2640 | if (ecc_type == 2) |
33ca0643 BP |
2641 | err.syndrome = extract_syndrome(m->status); |
2642 | ||
2643 | pvt->ops->map_sysaddr_to_csrow(mci, sys_addr, &err); | |
2644 | ||
e70984d9 | 2645 | __log_ecc_error(mci, &err, ecc_type); |
d27bf6fa DT |
2646 | } |
2647 | ||
713ad546 YG |
2648 | /* |
2649 | * To find the UMC channel represented by this bank we need to match on its | |
2650 | * instance_id. The instance_id of a bank is held in the lower 32 bits of its | |
2651 | * IPID. | |
bdcee774 YG |
2652 | * |
2653 | * Currently, we can derive the channel number by looking at the 6th nibble in | |
2654 | * the instance_id. For example, instance_id=0xYXXXXX where Y is the channel | |
2655 | * number. | |
713ad546 | 2656 | */ |
bdcee774 | 2657 | static int find_umc_channel(struct mce *m) |
713ad546 | 2658 | { |
bdcee774 | 2659 | return (m->ipid & GENMASK(31, 0)) >> 20; |
713ad546 YG |
2660 | } |
2661 | ||
2662 | static void decode_umc_error(int node_id, struct mce *m) | |
2663 | { | |
2664 | u8 ecc_type = (m->status >> 45) & 0x3; | |
2665 | struct mem_ctl_info *mci; | |
2666 | struct amd64_pvt *pvt; | |
2667 | struct err_info err; | |
2668 | u64 sys_addr; | |
2669 | ||
2670 | mci = edac_mc_find(node_id); | |
2671 | if (!mci) | |
2672 | return; | |
2673 | ||
2674 | pvt = mci->pvt_info; | |
2675 | ||
2676 | memset(&err, 0, sizeof(err)); | |
2677 | ||
2678 | if (m->status & MCI_STATUS_DEFERRED) | |
2679 | ecc_type = 3; | |
2680 | ||
bdcee774 | 2681 | err.channel = find_umc_channel(m); |
713ad546 | 2682 | |
713ad546 YG |
2683 | if (!(m->status & MCI_STATUS_SYNDV)) { |
2684 | err.err_code = ERR_SYND; | |
2685 | goto log_error; | |
2686 | } | |
2687 | ||
2688 | if (ecc_type == 2) { | |
2689 | u8 length = (m->synd >> 18) & 0x3f; | |
2690 | ||
2691 | if (length) | |
2692 | err.syndrome = (m->synd >> 32) & GENMASK(length - 1, 0); | |
2693 | else | |
2694 | err.err_code = ERR_CHANNEL; | |
2695 | } | |
2696 | ||
2697 | err.csrow = m->synd & 0x7; | |
2698 | ||
8a2eaab7 YG |
2699 | if (umc_normaddr_to_sysaddr(m->addr, pvt->mc_node_id, err.channel, &sys_addr)) { |
2700 | err.err_code = ERR_NORM_ADDR; | |
2701 | goto log_error; | |
2702 | } | |
2703 | ||
2704 | error_address_to_page_and_offset(sys_addr, &err); | |
2705 | ||
713ad546 YG |
2706 | log_error: |
2707 | __log_ecc_error(mci, &err, ecc_type); | |
2708 | } | |
2709 | ||
0ec449ee | 2710 | /* |
3f37a36b BP |
2711 | * Use pvt->F3 which contains the F3 CPU PCI device to get the related |
2712 | * F1 (AddrMap) and F2 (Dct) devices. Return negative value on error. | |
936fc3af | 2713 | * Reserve F0 and F6 on systems with a UMC. |
0ec449ee | 2714 | */ |
936fc3af YG |
2715 | static int |
2716 | reserve_mc_sibling_devs(struct amd64_pvt *pvt, u16 pci_id1, u16 pci_id2) | |
2717 | { | |
2718 | if (pvt->umc) { | |
2719 | pvt->F0 = pci_get_related_function(pvt->F3->vendor, pci_id1, pvt->F3); | |
2720 | if (!pvt->F0) { | |
6a4afe38 | 2721 | edac_dbg(1, "F0 not found, device 0x%x\n", pci_id1); |
936fc3af YG |
2722 | return -ENODEV; |
2723 | } | |
2724 | ||
2725 | pvt->F6 = pci_get_related_function(pvt->F3->vendor, pci_id2, pvt->F3); | |
2726 | if (!pvt->F6) { | |
2727 | pci_dev_put(pvt->F0); | |
2728 | pvt->F0 = NULL; | |
2729 | ||
6a4afe38 | 2730 | edac_dbg(1, "F6 not found: device 0x%x\n", pci_id2); |
936fc3af YG |
2731 | return -ENODEV; |
2732 | } | |
5246c540 | 2733 | |
706657b1 BP |
2734 | if (!pci_ctl_dev) |
2735 | pci_ctl_dev = &pvt->F0->dev; | |
2736 | ||
936fc3af YG |
2737 | edac_dbg(1, "F0: %s\n", pci_name(pvt->F0)); |
2738 | edac_dbg(1, "F3: %s\n", pci_name(pvt->F3)); | |
2739 | edac_dbg(1, "F6: %s\n", pci_name(pvt->F6)); | |
2740 | ||
2741 | return 0; | |
2742 | } | |
2743 | ||
0ec449ee | 2744 | /* Reserve the ADDRESS MAP Device */ |
936fc3af | 2745 | pvt->F1 = pci_get_related_function(pvt->F3->vendor, pci_id1, pvt->F3); |
8d5b5d9c | 2746 | if (!pvt->F1) { |
6a4afe38 | 2747 | edac_dbg(1, "F1 not found: device 0x%x\n", pci_id1); |
bbd0c1f6 | 2748 | return -ENODEV; |
0ec449ee DT |
2749 | } |
2750 | ||
3f37a36b | 2751 | /* Reserve the DCT Device */ |
936fc3af | 2752 | pvt->F2 = pci_get_related_function(pvt->F3->vendor, pci_id2, pvt->F3); |
3f37a36b | 2753 | if (!pvt->F2) { |
8d5b5d9c BP |
2754 | pci_dev_put(pvt->F1); |
2755 | pvt->F1 = NULL; | |
0ec449ee | 2756 | |
6a4afe38 | 2757 | edac_dbg(1, "F2 not found: device 0x%x\n", pci_id2); |
5246c540 | 2758 | return -ENODEV; |
0ec449ee | 2759 | } |
936fc3af | 2760 | |
706657b1 BP |
2761 | if (!pci_ctl_dev) |
2762 | pci_ctl_dev = &pvt->F2->dev; | |
2763 | ||
956b9ba1 JP |
2764 | edac_dbg(1, "F1: %s\n", pci_name(pvt->F1)); |
2765 | edac_dbg(1, "F2: %s\n", pci_name(pvt->F2)); | |
2766 | edac_dbg(1, "F3: %s\n", pci_name(pvt->F3)); | |
0ec449ee DT |
2767 | |
2768 | return 0; | |
2769 | } | |
2770 | ||
360b7f3c | 2771 | static void free_mc_sibling_devs(struct amd64_pvt *pvt) |
0ec449ee | 2772 | { |
936fc3af YG |
2773 | if (pvt->umc) { |
2774 | pci_dev_put(pvt->F0); | |
2775 | pci_dev_put(pvt->F6); | |
2776 | } else { | |
2777 | pci_dev_put(pvt->F1); | |
2778 | pci_dev_put(pvt->F2); | |
2779 | } | |
0ec449ee DT |
2780 | } |
2781 | ||
b64ce7cd YG |
2782 | static void determine_ecc_sym_sz(struct amd64_pvt *pvt) |
2783 | { | |
2784 | pvt->ecc_sym_sz = 4; | |
2785 | ||
2786 | if (pvt->umc) { | |
2787 | u8 i; | |
2788 | ||
4d30d2bc | 2789 | for_each_umc(i) { |
b64ce7cd | 2790 | /* Check enabled channels only: */ |
7835961d YG |
2791 | if (pvt->umc[i].sdp_ctrl & UMC_SDP_INIT) { |
2792 | if (pvt->umc[i].ecc_ctrl & BIT(9)) { | |
2793 | pvt->ecc_sym_sz = 16; | |
2794 | return; | |
2795 | } else if (pvt->umc[i].ecc_ctrl & BIT(7)) { | |
2796 | pvt->ecc_sym_sz = 8; | |
2797 | return; | |
2798 | } | |
b64ce7cd YG |
2799 | } |
2800 | } | |
7835961d | 2801 | } else if (pvt->fam >= 0x10) { |
b64ce7cd YG |
2802 | u32 tmp; |
2803 | ||
2804 | amd64_read_pci_cfg(pvt->F3, EXT_NB_MCA_CFG, &tmp); | |
2805 | /* F16h has only DCT0, so no need to read dbam1. */ | |
2806 | if (pvt->fam != 0x16) | |
2807 | amd64_read_dct_pci_cfg(pvt, 1, DBAM0, &pvt->dbam1); | |
2808 | ||
2809 | /* F10h, revD and later can do x8 ECC too. */ | |
2810 | if ((pvt->fam > 0x10 || pvt->model > 7) && tmp & BIT(25)) | |
2811 | pvt->ecc_sym_sz = 8; | |
2812 | } | |
2813 | } | |
2814 | ||
2815 | /* | |
2816 | * Retrieve the hardware registers of the memory controller. | |
2817 | */ | |
2818 | static void __read_mc_regs_df(struct amd64_pvt *pvt) | |
2819 | { | |
2820 | u8 nid = pvt->mc_node_id; | |
2821 | struct amd64_umc *umc; | |
2822 | u32 i, umc_base; | |
2823 | ||
2824 | /* Read registers from each UMC */ | |
4d30d2bc | 2825 | for_each_umc(i) { |
b64ce7cd YG |
2826 | |
2827 | umc_base = get_umc_base(i); | |
2828 | umc = &pvt->umc[i]; | |
2829 | ||
07ed82ef YG |
2830 | amd_smn_read(nid, umc_base + UMCCH_DIMM_CFG, &umc->dimm_cfg); |
2831 | amd_smn_read(nid, umc_base + UMCCH_UMC_CFG, &umc->umc_cfg); | |
b64ce7cd YG |
2832 | amd_smn_read(nid, umc_base + UMCCH_SDP_CTRL, &umc->sdp_ctrl); |
2833 | amd_smn_read(nid, umc_base + UMCCH_ECC_CTRL, &umc->ecc_ctrl); | |
07ed82ef | 2834 | amd_smn_read(nid, umc_base + UMCCH_UMC_CAP_HI, &umc->umc_cap_hi); |
b64ce7cd YG |
2835 | } |
2836 | } | |
2837 | ||
0ec449ee DT |
2838 | /* |
2839 | * Retrieve the hardware registers of the memory controller (this includes the | |
2840 | * 'Address Map' and 'Misc' device regs) | |
2841 | */ | |
360b7f3c | 2842 | static void read_mc_regs(struct amd64_pvt *pvt) |
0ec449ee | 2843 | { |
b64ce7cd | 2844 | unsigned int range; |
0ec449ee | 2845 | u64 msr_val; |
0ec449ee DT |
2846 | |
2847 | /* | |
2848 | * Retrieve TOP_MEM and TOP_MEM2; no masking off of reserved bits since | |
b64ce7cd | 2849 | * those are Read-As-Zero. |
0ec449ee | 2850 | */ |
e97f8bb8 | 2851 | rdmsrl(MSR_K8_TOP_MEM1, pvt->top_mem); |
956b9ba1 | 2852 | edac_dbg(0, " TOP_MEM: 0x%016llx\n", pvt->top_mem); |
0ec449ee | 2853 | |
b64ce7cd | 2854 | /* Check first whether TOP_MEM2 is enabled: */ |
0ec449ee | 2855 | rdmsrl(MSR_K8_SYSCFG, msr_val); |
b64ce7cd | 2856 | if (msr_val & BIT(21)) { |
e97f8bb8 | 2857 | rdmsrl(MSR_K8_TOP_MEM2, pvt->top_mem2); |
956b9ba1 | 2858 | edac_dbg(0, " TOP_MEM2: 0x%016llx\n", pvt->top_mem2); |
b64ce7cd | 2859 | } else { |
956b9ba1 | 2860 | edac_dbg(0, " TOP_MEM2 disabled\n"); |
b64ce7cd YG |
2861 | } |
2862 | ||
2863 | if (pvt->umc) { | |
2864 | __read_mc_regs_df(pvt); | |
2865 | amd64_read_pci_cfg(pvt->F0, DF_DHAR, &pvt->dhar); | |
2866 | ||
2867 | goto skip; | |
2868 | } | |
0ec449ee | 2869 | |
5980bb9c | 2870 | amd64_read_pci_cfg(pvt->F3, NBCAP, &pvt->nbcap); |
0ec449ee | 2871 | |
5a5d2371 | 2872 | read_dram_ctl_register(pvt); |
0ec449ee | 2873 | |
7f19bf75 BP |
2874 | for (range = 0; range < DRAM_RANGES; range++) { |
2875 | u8 rw; | |
0ec449ee | 2876 | |
7f19bf75 BP |
2877 | /* read settings for this DRAM range */ |
2878 | read_dram_base_limit_regs(pvt, range); | |
2879 | ||
2880 | rw = dram_rw(pvt, range); | |
2881 | if (!rw) | |
2882 | continue; | |
2883 | ||
956b9ba1 JP |
2884 | edac_dbg(1, " DRAM range[%d], base: 0x%016llx; limit: 0x%016llx\n", |
2885 | range, | |
2886 | get_dram_base(pvt, range), | |
2887 | get_dram_limit(pvt, range)); | |
7f19bf75 | 2888 | |
956b9ba1 JP |
2889 | edac_dbg(1, " IntlvEn=%s; Range access: %s%s IntlvSel=%d DstNode=%d\n", |
2890 | dram_intlv_en(pvt, range) ? "Enabled" : "Disabled", | |
2891 | (rw & 0x1) ? "R" : "-", | |
2892 | (rw & 0x2) ? "W" : "-", | |
2893 | dram_intlv_sel(pvt, range), | |
2894 | dram_dst_node(pvt, range)); | |
0ec449ee DT |
2895 | } |
2896 | ||
bc21fa57 | 2897 | amd64_read_pci_cfg(pvt->F1, DHAR, &pvt->dhar); |
7981a28f | 2898 | amd64_read_dct_pci_cfg(pvt, 0, DBAM0, &pvt->dbam0); |
0ec449ee | 2899 | |
8d5b5d9c | 2900 | amd64_read_pci_cfg(pvt->F3, F10_ONLINE_SPARE, &pvt->online_spare); |
0ec449ee | 2901 | |
7981a28f AG |
2902 | amd64_read_dct_pci_cfg(pvt, 0, DCLR0, &pvt->dclr0); |
2903 | amd64_read_dct_pci_cfg(pvt, 0, DCHR0, &pvt->dchr0); | |
0ec449ee | 2904 | |
78da121e | 2905 | if (!dct_ganging_enabled(pvt)) { |
7981a28f AG |
2906 | amd64_read_dct_pci_cfg(pvt, 1, DCLR0, &pvt->dclr1); |
2907 | amd64_read_dct_pci_cfg(pvt, 1, DCHR0, &pvt->dchr1); | |
0ec449ee | 2908 | } |
ad6a32e9 | 2909 | |
b64ce7cd YG |
2910 | skip: |
2911 | read_dct_base_mask(pvt); | |
2912 | ||
a597d2a5 AG |
2913 | determine_memory_type(pvt); |
2914 | edac_dbg(1, " DIMM type: %s\n", edac_mem_types[pvt->dram_type]); | |
a3b7db09 | 2915 | |
b64ce7cd | 2916 | determine_ecc_sym_sz(pvt); |
0ec449ee DT |
2917 | } |
2918 | ||
2919 | /* | |
2920 | * NOTE: CPU Revision Dependent code | |
2921 | * | |
2922 | * Input: | |
11c75ead | 2923 | * @csrow_nr ChipSelect Row Number (0..NUM_CHIPSELECTS-1) |
0ec449ee DT |
2924 | * k8 private pointer to --> |
2925 | * DRAM Bank Address mapping register | |
2926 | * node_id | |
2927 | * DCL register where dual_channel_active is | |
2928 | * | |
2929 | * The DBAM register consists of 4 sets of 4 bits each definitions: | |
2930 | * | |
2931 | * Bits: CSROWs | |
2932 | * 0-3 CSROWs 0 and 1 | |
2933 | * 4-7 CSROWs 2 and 3 | |
2934 | * 8-11 CSROWs 4 and 5 | |
2935 | * 12-15 CSROWs 6 and 7 | |
2936 | * | |
2937 | * Values range from: 0 to 15 | |
2938 | * The meaning of the values depends on CPU revision and dual-channel state, | |
2939 | * see relevant BKDG more info. | |
2940 | * | |
2941 | * The memory controller provides for total of only 8 CSROWs in its current | |
2942 | * architecture. Each "pair" of CSROWs normally represents just one DIMM in | |
2943 | * single channel or two (2) DIMMs in dual channel mode. | |
2944 | * | |
2945 | * The following code logic collapses the various tables for CSROW based on CPU | |
2946 | * revision. | |
2947 | * | |
2948 | * Returns: | |
2949 | * The number of PAGE_SIZE pages on the specified CSROW number it | |
2950 | * encompasses | |
2951 | * | |
2952 | */ | |
eb77e6b8 | 2953 | static u32 get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr_orig) |
0ec449ee | 2954 | { |
f92cae45 | 2955 | u32 dbam = dct ? pvt->dbam1 : pvt->dbam0; |
eb77e6b8 YG |
2956 | int csrow_nr = csrow_nr_orig; |
2957 | u32 cs_mode, nr_pages; | |
0ec449ee | 2958 | |
e53a3b26 | 2959 | if (!pvt->umc) { |
eb77e6b8 | 2960 | csrow_nr >>= 1; |
e53a3b26 YG |
2961 | cs_mode = DBAM_DIMM(csrow_nr, dbam); |
2962 | } else { | |
2963 | cs_mode = f17_get_cs_mode(csrow_nr >> 1, dct, pvt); | |
2964 | } | |
0ec449ee | 2965 | |
eb77e6b8 YG |
2966 | nr_pages = pvt->ops->dbam_to_cs(pvt, dct, cs_mode, csrow_nr); |
2967 | nr_pages <<= 20 - PAGE_SHIFT; | |
0ec449ee | 2968 | |
10de6497 | 2969 | edac_dbg(0, "csrow: %d, channel: %d, DBAM idx: %d\n", |
eb77e6b8 | 2970 | csrow_nr_orig, dct, cs_mode); |
10de6497 | 2971 | edac_dbg(0, "nr_pages/channel: %u\n", nr_pages); |
0ec449ee DT |
2972 | |
2973 | return nr_pages; | |
2974 | } | |
2975 | ||
353a1fcb YG |
2976 | static int init_csrows_df(struct mem_ctl_info *mci) |
2977 | { | |
2978 | struct amd64_pvt *pvt = mci->pvt_info; | |
2979 | enum edac_type edac_mode = EDAC_NONE; | |
2980 | enum dev_type dev_type = DEV_UNKNOWN; | |
2981 | struct dimm_info *dimm; | |
2982 | int empty = 1; | |
2983 | u8 umc, cs; | |
2984 | ||
2985 | if (mci->edac_ctl_cap & EDAC_FLAG_S16ECD16ED) { | |
2986 | edac_mode = EDAC_S16ECD16ED; | |
2987 | dev_type = DEV_X16; | |
2988 | } else if (mci->edac_ctl_cap & EDAC_FLAG_S8ECD8ED) { | |
2989 | edac_mode = EDAC_S8ECD8ED; | |
2990 | dev_type = DEV_X8; | |
2991 | } else if (mci->edac_ctl_cap & EDAC_FLAG_S4ECD4ED) { | |
2992 | edac_mode = EDAC_S4ECD4ED; | |
2993 | dev_type = DEV_X4; | |
2994 | } else if (mci->edac_ctl_cap & EDAC_FLAG_SECDED) { | |
2995 | edac_mode = EDAC_SECDED; | |
2996 | } | |
2997 | ||
2998 | for_each_umc(umc) { | |
2999 | for_each_chip_select(cs, umc, pvt) { | |
3000 | if (!csrow_enabled(cs, umc, pvt)) | |
3001 | continue; | |
3002 | ||
3003 | empty = 0; | |
3004 | dimm = mci->csrows[cs]->channels[umc]->dimm; | |
3005 | ||
3006 | edac_dbg(1, "MC node: %d, csrow: %d\n", | |
3007 | pvt->mc_node_id, cs); | |
3008 | ||
3009 | dimm->nr_pages = get_csrow_nr_pages(pvt, umc, cs); | |
3010 | dimm->mtype = pvt->dram_type; | |
3011 | dimm->edac_mode = edac_mode; | |
3012 | dimm->dtype = dev_type; | |
466503d6 | 3013 | dimm->grain = 64; |
353a1fcb YG |
3014 | } |
3015 | } | |
3016 | ||
3017 | return empty; | |
3018 | } | |
3019 | ||
0ec449ee DT |
3020 | /* |
3021 | * Initialize the array of csrow attribute instances, based on the values | |
3022 | * from pci config hardware registers. | |
3023 | */ | |
360b7f3c | 3024 | static int init_csrows(struct mem_ctl_info *mci) |
0ec449ee | 3025 | { |
10de6497 | 3026 | struct amd64_pvt *pvt = mci->pvt_info; |
2d09d8f3 | 3027 | enum edac_type edac_mode = EDAC_NONE; |
0ec449ee | 3028 | struct csrow_info *csrow; |
de3910eb | 3029 | struct dimm_info *dimm; |
10de6497 | 3030 | int i, j, empty = 1; |
a895bf8b | 3031 | int nr_pages = 0; |
10de6497 | 3032 | u32 val; |
0ec449ee | 3033 | |
353a1fcb YG |
3034 | if (pvt->umc) |
3035 | return init_csrows_df(mci); | |
0ec449ee | 3036 | |
353a1fcb | 3037 | amd64_read_pci_cfg(pvt->F3, NBCFG, &val); |
0ec449ee | 3038 | |
353a1fcb YG |
3039 | pvt->nbcfg = val; |
3040 | ||
3041 | edac_dbg(0, "node %d, NBCFG=0x%08x[ChipKillEccCap: %d|DramEccEn: %d]\n", | |
3042 | pvt->mc_node_id, val, | |
3043 | !!(val & NBCFG_CHIPKILL), !!(val & NBCFG_ECC_ENABLE)); | |
0ec449ee | 3044 | |
10de6497 BP |
3045 | /* |
3046 | * We iterate over DCT0 here but we look at DCT1 in parallel, if needed. | |
3047 | */ | |
11c75ead | 3048 | for_each_chip_select(i, 0, pvt) { |
10de6497 BP |
3049 | bool row_dct0 = !!csrow_enabled(i, 0, pvt); |
3050 | bool row_dct1 = false; | |
0ec449ee | 3051 | |
a4b4bedc | 3052 | if (pvt->fam != 0xf) |
10de6497 BP |
3053 | row_dct1 = !!csrow_enabled(i, 1, pvt); |
3054 | ||
3055 | if (!row_dct0 && !row_dct1) | |
0ec449ee | 3056 | continue; |
0ec449ee | 3057 | |
10de6497 | 3058 | csrow = mci->csrows[i]; |
0ec449ee | 3059 | empty = 0; |
10de6497 BP |
3060 | |
3061 | edac_dbg(1, "MC node: %d, csrow: %d\n", | |
3062 | pvt->mc_node_id, i); | |
3063 | ||
1eef1282 | 3064 | if (row_dct0) { |
d1ea71cd | 3065 | nr_pages = get_csrow_nr_pages(pvt, 0, i); |
1eef1282 MCC |
3066 | csrow->channels[0]->dimm->nr_pages = nr_pages; |
3067 | } | |
11c75ead | 3068 | |
10de6497 | 3069 | /* K8 has only one DCT */ |
a4b4bedc | 3070 | if (pvt->fam != 0xf && row_dct1) { |
d1ea71cd | 3071 | int row_dct1_pages = get_csrow_nr_pages(pvt, 1, i); |
1eef1282 MCC |
3072 | |
3073 | csrow->channels[1]->dimm->nr_pages = row_dct1_pages; | |
3074 | nr_pages += row_dct1_pages; | |
3075 | } | |
0ec449ee | 3076 | |
10de6497 | 3077 | edac_dbg(1, "Total csrow%d pages: %u\n", i, nr_pages); |
0ec449ee | 3078 | |
2d09d8f3 | 3079 | /* Determine DIMM ECC mode: */ |
353a1fcb | 3080 | if (pvt->nbcfg & NBCFG_ECC_ENABLE) { |
2d09d8f3 YG |
3081 | edac_mode = (pvt->nbcfg & NBCFG_CHIPKILL) |
3082 | ? EDAC_S4ECD4ED | |
3083 | : EDAC_SECDED; | |
3084 | } | |
084a4fcc MCC |
3085 | |
3086 | for (j = 0; j < pvt->channel_count; j++) { | |
de3910eb | 3087 | dimm = csrow->channels[j]->dimm; |
a597d2a5 | 3088 | dimm->mtype = pvt->dram_type; |
de3910eb | 3089 | dimm->edac_mode = edac_mode; |
466503d6 | 3090 | dimm->grain = 64; |
084a4fcc | 3091 | } |
0ec449ee DT |
3092 | } |
3093 | ||
3094 | return empty; | |
3095 | } | |
d27bf6fa | 3096 | |
f6d6ae96 | 3097 | /* get all cores on this DCT */ |
8b84c8df | 3098 | static void get_cpus_on_this_dct_cpumask(struct cpumask *mask, u16 nid) |
f6d6ae96 BP |
3099 | { |
3100 | int cpu; | |
3101 | ||
3102 | for_each_online_cpu(cpu) | |
db970bd2 | 3103 | if (topology_die_id(cpu) == nid) |
f6d6ae96 BP |
3104 | cpumask_set_cpu(cpu, mask); |
3105 | } | |
3106 | ||
3107 | /* check MCG_CTL on all the cpus on this node */ | |
d1ea71cd | 3108 | static bool nb_mce_bank_enabled_on_node(u16 nid) |
f6d6ae96 BP |
3109 | { |
3110 | cpumask_var_t mask; | |
50542251 | 3111 | int cpu, nbe; |
f6d6ae96 BP |
3112 | bool ret = false; |
3113 | ||
3114 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { | |
24f9a7fe | 3115 | amd64_warn("%s: Error allocating mask\n", __func__); |
f6d6ae96 BP |
3116 | return false; |
3117 | } | |
3118 | ||
3119 | get_cpus_on_this_dct_cpumask(mask, nid); | |
3120 | ||
f6d6ae96 BP |
3121 | rdmsr_on_cpus(mask, MSR_IA32_MCG_CTL, msrs); |
3122 | ||
3123 | for_each_cpu(cpu, mask) { | |
50542251 | 3124 | struct msr *reg = per_cpu_ptr(msrs, cpu); |
5980bb9c | 3125 | nbe = reg->l & MSR_MCGCTL_NBE; |
f6d6ae96 | 3126 | |
956b9ba1 JP |
3127 | edac_dbg(0, "core: %u, MCG_CTL: 0x%llx, NB MSR is %s\n", |
3128 | cpu, reg->q, | |
3129 | (nbe ? "enabled" : "disabled")); | |
f6d6ae96 BP |
3130 | |
3131 | if (!nbe) | |
3132 | goto out; | |
f6d6ae96 BP |
3133 | } |
3134 | ret = true; | |
3135 | ||
3136 | out: | |
f6d6ae96 BP |
3137 | free_cpumask_var(mask); |
3138 | return ret; | |
3139 | } | |
3140 | ||
c7e5301a | 3141 | static int toggle_ecc_err_reporting(struct ecc_settings *s, u16 nid, bool on) |
f6d6ae96 BP |
3142 | { |
3143 | cpumask_var_t cmask; | |
50542251 | 3144 | int cpu; |
f6d6ae96 BP |
3145 | |
3146 | if (!zalloc_cpumask_var(&cmask, GFP_KERNEL)) { | |
24f9a7fe | 3147 | amd64_warn("%s: error allocating mask\n", __func__); |
0de27884 | 3148 | return -ENOMEM; |
f6d6ae96 BP |
3149 | } |
3150 | ||
ae7bb7c6 | 3151 | get_cpus_on_this_dct_cpumask(cmask, nid); |
f6d6ae96 | 3152 | |
f6d6ae96 BP |
3153 | rdmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs); |
3154 | ||
3155 | for_each_cpu(cpu, cmask) { | |
3156 | ||
50542251 BP |
3157 | struct msr *reg = per_cpu_ptr(msrs, cpu); |
3158 | ||
f6d6ae96 | 3159 | if (on) { |
5980bb9c | 3160 | if (reg->l & MSR_MCGCTL_NBE) |
ae7bb7c6 | 3161 | s->flags.nb_mce_enable = 1; |
f6d6ae96 | 3162 | |
5980bb9c | 3163 | reg->l |= MSR_MCGCTL_NBE; |
f6d6ae96 BP |
3164 | } else { |
3165 | /* | |
d95cf4de | 3166 | * Turn off NB MCE reporting only when it was off before |
f6d6ae96 | 3167 | */ |
ae7bb7c6 | 3168 | if (!s->flags.nb_mce_enable) |
5980bb9c | 3169 | reg->l &= ~MSR_MCGCTL_NBE; |
f6d6ae96 | 3170 | } |
f6d6ae96 BP |
3171 | } |
3172 | wrmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs); | |
3173 | ||
f6d6ae96 BP |
3174 | free_cpumask_var(cmask); |
3175 | ||
3176 | return 0; | |
3177 | } | |
3178 | ||
c7e5301a | 3179 | static bool enable_ecc_error_reporting(struct ecc_settings *s, u16 nid, |
2299ef71 | 3180 | struct pci_dev *F3) |
f9431992 | 3181 | { |
2299ef71 | 3182 | bool ret = true; |
c9f4f26e | 3183 | u32 value, mask = 0x3; /* UECC/CECC enable */ |
f9431992 | 3184 | |
2299ef71 BP |
3185 | if (toggle_ecc_err_reporting(s, nid, ON)) { |
3186 | amd64_warn("Error enabling ECC reporting over MCGCTL!\n"); | |
3187 | return false; | |
3188 | } | |
3189 | ||
c9f4f26e | 3190 | amd64_read_pci_cfg(F3, NBCTL, &value); |
f9431992 | 3191 | |
ae7bb7c6 BP |
3192 | s->old_nbctl = value & mask; |
3193 | s->nbctl_valid = true; | |
f9431992 DT |
3194 | |
3195 | value |= mask; | |
c9f4f26e | 3196 | amd64_write_pci_cfg(F3, NBCTL, value); |
f9431992 | 3197 | |
a97fa68e | 3198 | amd64_read_pci_cfg(F3, NBCFG, &value); |
f9431992 | 3199 | |
956b9ba1 JP |
3200 | edac_dbg(0, "1: node %d, NBCFG=0x%08x[DramEccEn: %d]\n", |
3201 | nid, value, !!(value & NBCFG_ECC_ENABLE)); | |
f9431992 | 3202 | |
a97fa68e | 3203 | if (!(value & NBCFG_ECC_ENABLE)) { |
24f9a7fe | 3204 | amd64_warn("DRAM ECC disabled on this node, enabling...\n"); |
f9431992 | 3205 | |
ae7bb7c6 | 3206 | s->flags.nb_ecc_prev = 0; |
d95cf4de | 3207 | |
f9431992 | 3208 | /* Attempt to turn on DRAM ECC Enable */ |
a97fa68e BP |
3209 | value |= NBCFG_ECC_ENABLE; |
3210 | amd64_write_pci_cfg(F3, NBCFG, value); | |
f9431992 | 3211 | |
a97fa68e | 3212 | amd64_read_pci_cfg(F3, NBCFG, &value); |
f9431992 | 3213 | |
a97fa68e | 3214 | if (!(value & NBCFG_ECC_ENABLE)) { |
24f9a7fe BP |
3215 | amd64_warn("Hardware rejected DRAM ECC enable," |
3216 | "check memory DIMM configuration.\n"); | |
2299ef71 | 3217 | ret = false; |
f9431992 | 3218 | } else { |
24f9a7fe | 3219 | amd64_info("Hardware accepted DRAM ECC Enable\n"); |
f9431992 | 3220 | } |
d95cf4de | 3221 | } else { |
ae7bb7c6 | 3222 | s->flags.nb_ecc_prev = 1; |
f9431992 | 3223 | } |
d95cf4de | 3224 | |
956b9ba1 JP |
3225 | edac_dbg(0, "2: node %d, NBCFG=0x%08x[DramEccEn: %d]\n", |
3226 | nid, value, !!(value & NBCFG_ECC_ENABLE)); | |
f9431992 | 3227 | |
2299ef71 | 3228 | return ret; |
f9431992 DT |
3229 | } |
3230 | ||
c7e5301a | 3231 | static void restore_ecc_error_reporting(struct ecc_settings *s, u16 nid, |
360b7f3c | 3232 | struct pci_dev *F3) |
f9431992 | 3233 | { |
c9f4f26e BP |
3234 | u32 value, mask = 0x3; /* UECC/CECC enable */ |
3235 | ||
ae7bb7c6 | 3236 | if (!s->nbctl_valid) |
f9431992 DT |
3237 | return; |
3238 | ||
c9f4f26e | 3239 | amd64_read_pci_cfg(F3, NBCTL, &value); |
f9431992 | 3240 | value &= ~mask; |
ae7bb7c6 | 3241 | value |= s->old_nbctl; |
f9431992 | 3242 | |
c9f4f26e | 3243 | amd64_write_pci_cfg(F3, NBCTL, value); |
f9431992 | 3244 | |
ae7bb7c6 BP |
3245 | /* restore previous BIOS DRAM ECC "off" setting we force-enabled */ |
3246 | if (!s->flags.nb_ecc_prev) { | |
a97fa68e BP |
3247 | amd64_read_pci_cfg(F3, NBCFG, &value); |
3248 | value &= ~NBCFG_ECC_ENABLE; | |
3249 | amd64_write_pci_cfg(F3, NBCFG, value); | |
d95cf4de BP |
3250 | } |
3251 | ||
3252 | /* restore the NB Enable MCGCTL bit */ | |
2299ef71 | 3253 | if (toggle_ecc_err_reporting(s, nid, OFF)) |
24f9a7fe | 3254 | amd64_warn("Error restoring NB MCGCTL settings!\n"); |
f9431992 DT |
3255 | } |
3256 | ||
1c9b08ba | 3257 | static bool ecc_enabled(struct amd64_pvt *pvt) |
f9431992 | 3258 | { |
1c9b08ba | 3259 | u16 nid = pvt->mc_node_id; |
06724535 | 3260 | bool nb_mce_en = false; |
196b79fc YG |
3261 | u8 ecc_en = 0, i; |
3262 | u32 value; | |
f9431992 | 3263 | |
196b79fc YG |
3264 | if (boot_cpu_data.x86 >= 0x17) { |
3265 | u8 umc_en_mask = 0, ecc_en_mask = 0; | |
1c9b08ba | 3266 | struct amd64_umc *umc; |
f9431992 | 3267 | |
4d30d2bc | 3268 | for_each_umc(i) { |
1c9b08ba | 3269 | umc = &pvt->umc[i]; |
196b79fc YG |
3270 | |
3271 | /* Only check enabled UMCs. */ | |
1c9b08ba | 3272 | if (!(umc->sdp_ctrl & UMC_SDP_INIT)) |
196b79fc YG |
3273 | continue; |
3274 | ||
3275 | umc_en_mask |= BIT(i); | |
3276 | ||
1c9b08ba | 3277 | if (umc->umc_cap_hi & UMC_ECC_ENABLED) |
196b79fc YG |
3278 | ecc_en_mask |= BIT(i); |
3279 | } | |
3280 | ||
3281 | /* Check whether at least one UMC is enabled: */ | |
3282 | if (umc_en_mask) | |
3283 | ecc_en = umc_en_mask == ecc_en_mask; | |
11ab1cae YG |
3284 | else |
3285 | edac_dbg(0, "Node %d: No enabled UMCs.\n", nid); | |
196b79fc YG |
3286 | |
3287 | /* Assume UMC MCA banks are enabled. */ | |
3288 | nb_mce_en = true; | |
3289 | } else { | |
1c9b08ba | 3290 | amd64_read_pci_cfg(pvt->F3, NBCFG, &value); |
f9431992 | 3291 | |
196b79fc YG |
3292 | ecc_en = !!(value & NBCFG_ECC_ENABLE); |
3293 | ||
3294 | nb_mce_en = nb_mce_bank_enabled_on_node(nid); | |
3295 | if (!nb_mce_en) | |
11ab1cae | 3296 | edac_dbg(0, "NB MCE bank disabled, set MSR 0x%08x[4] on node %d to enable.\n", |
196b79fc YG |
3297 | MSR_IA32_MCG_CTL, nid); |
3298 | } | |
3299 | ||
11ab1cae YG |
3300 | amd64_info("Node %d: DRAM ECC %s.\n", |
3301 | nid, (ecc_en ? "enabled" : "disabled")); | |
f9431992 | 3302 | |
7fdfee92 | 3303 | if (!ecc_en || !nb_mce_en) |
2299ef71 | 3304 | return false; |
7fdfee92 BP |
3305 | else |
3306 | return true; | |
f9431992 DT |
3307 | } |
3308 | ||
2d09d8f3 YG |
3309 | static inline void |
3310 | f17h_determine_edac_ctl_cap(struct mem_ctl_info *mci, struct amd64_pvt *pvt) | |
3311 | { | |
f8be8e56 | 3312 | u8 i, ecc_en = 1, cpk_en = 1, dev_x4 = 1, dev_x16 = 1; |
2d09d8f3 | 3313 | |
4d30d2bc | 3314 | for_each_umc(i) { |
2d09d8f3 YG |
3315 | if (pvt->umc[i].sdp_ctrl & UMC_SDP_INIT) { |
3316 | ecc_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_ENABLED); | |
3317 | cpk_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_CHIPKILL_CAP); | |
f8be8e56 YG |
3318 | |
3319 | dev_x4 &= !!(pvt->umc[i].dimm_cfg & BIT(6)); | |
3320 | dev_x16 &= !!(pvt->umc[i].dimm_cfg & BIT(7)); | |
2d09d8f3 YG |
3321 | } |
3322 | } | |
3323 | ||
3324 | /* Set chipkill only if ECC is enabled: */ | |
3325 | if (ecc_en) { | |
3326 | mci->edac_ctl_cap |= EDAC_FLAG_SECDED; | |
3327 | ||
f8be8e56 YG |
3328 | if (!cpk_en) |
3329 | return; | |
3330 | ||
3331 | if (dev_x4) | |
2d09d8f3 | 3332 | mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED; |
f8be8e56 YG |
3333 | else if (dev_x16) |
3334 | mci->edac_ctl_cap |= EDAC_FLAG_S16ECD16ED; | |
3335 | else | |
3336 | mci->edac_ctl_cap |= EDAC_FLAG_S8ECD8ED; | |
2d09d8f3 YG |
3337 | } |
3338 | } | |
3339 | ||
38ddd4d1 | 3340 | static void setup_mci_misc_attrs(struct mem_ctl_info *mci) |
7d6034d3 DT |
3341 | { |
3342 | struct amd64_pvt *pvt = mci->pvt_info; | |
3343 | ||
3344 | mci->mtype_cap = MEM_FLAG_DDR2 | MEM_FLAG_RDDR2; | |
3345 | mci->edac_ctl_cap = EDAC_FLAG_NONE; | |
7d6034d3 | 3346 | |
2d09d8f3 YG |
3347 | if (pvt->umc) { |
3348 | f17h_determine_edac_ctl_cap(mci, pvt); | |
3349 | } else { | |
3350 | if (pvt->nbcap & NBCAP_SECDED) | |
3351 | mci->edac_ctl_cap |= EDAC_FLAG_SECDED; | |
7d6034d3 | 3352 | |
2d09d8f3 YG |
3353 | if (pvt->nbcap & NBCAP_CHIPKILL) |
3354 | mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED; | |
3355 | } | |
7d6034d3 | 3356 | |
d1ea71cd | 3357 | mci->edac_cap = determine_edac_cap(pvt); |
7d6034d3 | 3358 | mci->mod_name = EDAC_MOD_STR; |
38ddd4d1 | 3359 | mci->ctl_name = fam_type->ctl_name; |
e7934b70 | 3360 | mci->dev_name = pci_name(pvt->F3); |
7d6034d3 DT |
3361 | mci->ctl_page_to_phys = NULL; |
3362 | ||
7d6034d3 | 3363 | /* memory scrubber interface */ |
d1ea71cd BP |
3364 | mci->set_sdram_scrub_rate = set_scrub_rate; |
3365 | mci->get_sdram_scrub_rate = get_scrub_rate; | |
7d6034d3 DT |
3366 | } |
3367 | ||
0092b20d BP |
3368 | /* |
3369 | * returns a pointer to the family descriptor on success, NULL otherwise. | |
3370 | */ | |
d1ea71cd | 3371 | static struct amd64_family_type *per_family_init(struct amd64_pvt *pvt) |
395ae783 | 3372 | { |
18b94f66 | 3373 | pvt->ext_model = boot_cpu_data.x86_model >> 4; |
b399151c | 3374 | pvt->stepping = boot_cpu_data.x86_stepping; |
18b94f66 AG |
3375 | pvt->model = boot_cpu_data.x86_model; |
3376 | pvt->fam = boot_cpu_data.x86; | |
3377 | ||
3378 | switch (pvt->fam) { | |
395ae783 | 3379 | case 0xf: |
d1ea71cd BP |
3380 | fam_type = &family_types[K8_CPUS]; |
3381 | pvt->ops = &family_types[K8_CPUS].ops; | |
395ae783 | 3382 | break; |
df71a053 | 3383 | |
395ae783 | 3384 | case 0x10: |
d1ea71cd BP |
3385 | fam_type = &family_types[F10_CPUS]; |
3386 | pvt->ops = &family_types[F10_CPUS].ops; | |
df71a053 BP |
3387 | break; |
3388 | ||
3389 | case 0x15: | |
18b94f66 | 3390 | if (pvt->model == 0x30) { |
d1ea71cd BP |
3391 | fam_type = &family_types[F15_M30H_CPUS]; |
3392 | pvt->ops = &family_types[F15_M30H_CPUS].ops; | |
18b94f66 | 3393 | break; |
a597d2a5 AG |
3394 | } else if (pvt->model == 0x60) { |
3395 | fam_type = &family_types[F15_M60H_CPUS]; | |
3396 | pvt->ops = &family_types[F15_M60H_CPUS].ops; | |
3397 | break; | |
6c13d7ff BP |
3398 | /* Richland is only client */ |
3399 | } else if (pvt->model == 0x13) { | |
3400 | return NULL; | |
3401 | } else { | |
3402 | fam_type = &family_types[F15_CPUS]; | |
3403 | pvt->ops = &family_types[F15_CPUS].ops; | |
18b94f66 | 3404 | } |
395ae783 BP |
3405 | break; |
3406 | ||
94c1acf2 | 3407 | case 0x16: |
85a8885b AG |
3408 | if (pvt->model == 0x30) { |
3409 | fam_type = &family_types[F16_M30H_CPUS]; | |
3410 | pvt->ops = &family_types[F16_M30H_CPUS].ops; | |
3411 | break; | |
3412 | } | |
d1ea71cd BP |
3413 | fam_type = &family_types[F16_CPUS]; |
3414 | pvt->ops = &family_types[F16_CPUS].ops; | |
94c1acf2 AG |
3415 | break; |
3416 | ||
f1cbbec9 | 3417 | case 0x17: |
8960de4a MJ |
3418 | if (pvt->model >= 0x10 && pvt->model <= 0x2f) { |
3419 | fam_type = &family_types[F17_M10H_CPUS]; | |
3420 | pvt->ops = &family_types[F17_M10H_CPUS].ops; | |
3421 | break; | |
6e846239 YG |
3422 | } else if (pvt->model >= 0x30 && pvt->model <= 0x3f) { |
3423 | fam_type = &family_types[F17_M30H_CPUS]; | |
3424 | pvt->ops = &family_types[F17_M30H_CPUS].ops; | |
3425 | break; | |
b6bea24d AM |
3426 | } else if (pvt->model >= 0x60 && pvt->model <= 0x6f) { |
3427 | fam_type = &family_types[F17_M60H_CPUS]; | |
3428 | pvt->ops = &family_types[F17_M60H_CPUS].ops; | |
3429 | break; | |
3e443eb3 IV |
3430 | } else if (pvt->model >= 0x70 && pvt->model <= 0x7f) { |
3431 | fam_type = &family_types[F17_M70H_CPUS]; | |
3432 | pvt->ops = &family_types[F17_M70H_CPUS].ops; | |
3433 | break; | |
8960de4a | 3434 | } |
df561f66 | 3435 | fallthrough; |
c4a3e946 | 3436 | case 0x18: |
f1cbbec9 YG |
3437 | fam_type = &family_types[F17_CPUS]; |
3438 | pvt->ops = &family_types[F17_CPUS].ops; | |
c4a3e946 PW |
3439 | |
3440 | if (pvt->fam == 0x18) | |
3441 | family_types[F17_CPUS].ctl_name = "F18h"; | |
f1cbbec9 YG |
3442 | break; |
3443 | ||
2eb61c91 | 3444 | case 0x19: |
b4210eab YG |
3445 | if (pvt->model >= 0x20 && pvt->model <= 0x2f) { |
3446 | fam_type = &family_types[F17_M70H_CPUS]; | |
3447 | pvt->ops = &family_types[F17_M70H_CPUS].ops; | |
3448 | fam_type->ctl_name = "F19h_M20h"; | |
3449 | break; | |
3450 | } | |
2eb61c91 YG |
3451 | fam_type = &family_types[F19_CPUS]; |
3452 | pvt->ops = &family_types[F19_CPUS].ops; | |
3453 | family_types[F19_CPUS].ctl_name = "F19h"; | |
3454 | break; | |
3455 | ||
395ae783 | 3456 | default: |
24f9a7fe | 3457 | amd64_err("Unsupported family!\n"); |
0092b20d | 3458 | return NULL; |
395ae783 | 3459 | } |
0092b20d | 3460 | |
df71a053 | 3461 | amd64_info("%s %sdetected (node %d).\n", fam_type->ctl_name, |
18b94f66 | 3462 | (pvt->fam == 0xf ? |
24f9a7fe BP |
3463 | (pvt->ext_model >= K8_REV_F ? "revF or later " |
3464 | : "revE or earlier ") | |
3465 | : ""), pvt->mc_node_id); | |
0092b20d | 3466 | return fam_type; |
395ae783 BP |
3467 | } |
3468 | ||
e339f1ec TI |
3469 | static const struct attribute_group *amd64_edac_attr_groups[] = { |
3470 | #ifdef CONFIG_EDAC_DEBUG | |
2a28ceef | 3471 | &dbg_group, |
e339f1ec TI |
3472 | #endif |
3473 | #ifdef CONFIG_EDAC_AMD64_ERROR_INJECTION | |
3474 | &amd64_edac_inj_group, | |
3475 | #endif | |
3476 | NULL | |
3477 | }; | |
3478 | ||
80355a3b | 3479 | static int hw_info_get(struct amd64_pvt *pvt) |
7d6034d3 | 3480 | { |
936fc3af | 3481 | u16 pci_id1, pci_id2; |
f00eb5ff | 3482 | int ret; |
395ae783 | 3483 | |
936fc3af | 3484 | if (pvt->fam >= 0x17) { |
5e4c5527 | 3485 | pvt->umc = kcalloc(fam_type->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL); |
80355a3b YG |
3486 | if (!pvt->umc) |
3487 | return -ENOMEM; | |
936fc3af YG |
3488 | |
3489 | pci_id1 = fam_type->f0_id; | |
3490 | pci_id2 = fam_type->f6_id; | |
3491 | } else { | |
3492 | pci_id1 = fam_type->f1_id; | |
3493 | pci_id2 = fam_type->f2_id; | |
3494 | } | |
3495 | ||
80355a3b YG |
3496 | ret = reserve_mc_sibling_devs(pvt, pci_id1, pci_id2); |
3497 | if (ret) | |
3498 | return ret; | |
7d6034d3 | 3499 | |
360b7f3c | 3500 | read_mc_regs(pvt); |
7d6034d3 | 3501 | |
80355a3b YG |
3502 | return 0; |
3503 | } | |
3504 | ||
3505 | static void hw_info_put(struct amd64_pvt *pvt) | |
3506 | { | |
3507 | if (pvt->F0 || pvt->F1) | |
3508 | free_mc_sibling_devs(pvt); | |
3509 | ||
3510 | kfree(pvt->umc); | |
3511 | } | |
3512 | ||
3513 | static int init_one_instance(struct amd64_pvt *pvt) | |
3514 | { | |
3515 | struct mem_ctl_info *mci = NULL; | |
3516 | struct edac_mc_layer layers[2]; | |
3517 | int ret = -EINVAL; | |
3518 | ||
7d6034d3 DT |
3519 | /* |
3520 | * We need to determine how many memory channels there are. Then use | |
3521 | * that information for calculating the size of the dynamic instance | |
360b7f3c | 3522 | * tables in the 'mci' structure. |
7d6034d3 DT |
3523 | */ |
3524 | pvt->channel_count = pvt->ops->early_channel_count(pvt); | |
3525 | if (pvt->channel_count < 0) | |
80355a3b | 3526 | return ret; |
7d6034d3 DT |
3527 | |
3528 | ret = -ENOMEM; | |
ab5a503c MCC |
3529 | layers[0].type = EDAC_MC_LAYER_CHIP_SELECT; |
3530 | layers[0].size = pvt->csels[0].b_cnt; | |
3531 | layers[0].is_virt_csrow = true; | |
3532 | layers[1].type = EDAC_MC_LAYER_CHANNEL; | |
f0a56c48 BP |
3533 | |
3534 | /* | |
3535 | * Always allocate two channels since we can have setups with DIMMs on | |
3536 | * only one channel. Also, this simplifies handling later for the price | |
3537 | * of a couple of KBs tops. | |
3538 | */ | |
5e4c5527 | 3539 | layers[1].size = fam_type->max_mcs; |
ab5a503c | 3540 | layers[1].is_virt_csrow = false; |
f0a56c48 | 3541 | |
80355a3b | 3542 | mci = edac_mc_alloc(pvt->mc_node_id, ARRAY_SIZE(layers), layers, 0); |
7d6034d3 | 3543 | if (!mci) |
80355a3b | 3544 | return ret; |
7d6034d3 DT |
3545 | |
3546 | mci->pvt_info = pvt; | |
3f37a36b | 3547 | mci->pdev = &pvt->F3->dev; |
7d6034d3 | 3548 | |
38ddd4d1 | 3549 | setup_mci_misc_attrs(mci); |
360b7f3c BP |
3550 | |
3551 | if (init_csrows(mci)) | |
7d6034d3 DT |
3552 | mci->edac_cap = EDAC_FLAG_NONE; |
3553 | ||
7d6034d3 | 3554 | ret = -ENODEV; |
e339f1ec | 3555 | if (edac_mc_add_mc_with_groups(mci, amd64_edac_attr_groups)) { |
956b9ba1 | 3556 | edac_dbg(1, "failed edac_mc_add_mc()\n"); |
80355a3b YG |
3557 | edac_mc_free(mci); |
3558 | return ret; | |
7d6034d3 DT |
3559 | } |
3560 | ||
7d6034d3 | 3561 | return 0; |
7d6034d3 DT |
3562 | } |
3563 | ||
582f94b5 YG |
3564 | static bool instance_has_memory(struct amd64_pvt *pvt) |
3565 | { | |
3566 | bool cs_enabled = false; | |
3567 | int cs = 0, dct = 0; | |
3568 | ||
3569 | for (dct = 0; dct < fam_type->max_mcs; dct++) { | |
3570 | for_each_chip_select(cs, dct, pvt) | |
3571 | cs_enabled |= csrow_enabled(cs, dct, pvt); | |
3572 | } | |
3573 | ||
3574 | return cs_enabled; | |
3575 | } | |
3576 | ||
3f37a36b | 3577 | static int probe_one_instance(unsigned int nid) |
7d6034d3 | 3578 | { |
2299ef71 | 3579 | struct pci_dev *F3 = node_to_amd_nb(nid)->misc; |
80355a3b | 3580 | struct amd64_pvt *pvt = NULL; |
ae7bb7c6 | 3581 | struct ecc_settings *s; |
3f37a36b | 3582 | int ret; |
7d6034d3 | 3583 | |
ae7bb7c6 BP |
3584 | ret = -ENOMEM; |
3585 | s = kzalloc(sizeof(struct ecc_settings), GFP_KERNEL); | |
3586 | if (!s) | |
2299ef71 | 3587 | goto err_out; |
ae7bb7c6 BP |
3588 | |
3589 | ecc_stngs[nid] = s; | |
3590 | ||
80355a3b YG |
3591 | pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL); |
3592 | if (!pvt) | |
3593 | goto err_settings; | |
3594 | ||
3595 | pvt->mc_node_id = nid; | |
3596 | pvt->F3 = F3; | |
3597 | ||
6c13d7ff | 3598 | ret = -ENODEV; |
80355a3b YG |
3599 | fam_type = per_family_init(pvt); |
3600 | if (!fam_type) | |
3601 | goto err_enable; | |
3602 | ||
3603 | ret = hw_info_get(pvt); | |
3604 | if (ret < 0) | |
3605 | goto err_enable; | |
3606 | ||
582f94b5 YG |
3607 | ret = 0; |
3608 | if (!instance_has_memory(pvt)) { | |
3609 | amd64_info("Node %d: No DIMMs detected.\n", nid); | |
3610 | goto err_enable; | |
3611 | } | |
3612 | ||
1c9b08ba | 3613 | if (!ecc_enabled(pvt)) { |
582f94b5 | 3614 | ret = -ENODEV; |
2299ef71 BP |
3615 | |
3616 | if (!ecc_enable_override) | |
3617 | goto err_enable; | |
3618 | ||
044e7a41 YG |
3619 | if (boot_cpu_data.x86 >= 0x17) { |
3620 | amd64_warn("Forcing ECC on is not recommended on newer systems. Please enable ECC in BIOS."); | |
3621 | goto err_enable; | |
3622 | } else | |
3623 | amd64_warn("Forcing ECC on!\n"); | |
2299ef71 BP |
3624 | |
3625 | if (!enable_ecc_error_reporting(s, nid, F3)) | |
3626 | goto err_enable; | |
3627 | } | |
3628 | ||
80355a3b | 3629 | ret = init_one_instance(pvt); |
360b7f3c | 3630 | if (ret < 0) { |
ae7bb7c6 | 3631 | amd64_err("Error probing instance: %d\n", nid); |
044e7a41 YG |
3632 | |
3633 | if (boot_cpu_data.x86 < 0x17) | |
3634 | restore_ecc_error_reporting(s, nid, F3); | |
2b9b2c46 YG |
3635 | |
3636 | goto err_enable; | |
360b7f3c | 3637 | } |
7d6034d3 | 3638 | |
582f94b5 YG |
3639 | dump_misc_regs(pvt); |
3640 | ||
7d6034d3 | 3641 | return ret; |
2299ef71 BP |
3642 | |
3643 | err_enable: | |
80355a3b YG |
3644 | hw_info_put(pvt); |
3645 | kfree(pvt); | |
3646 | ||
3647 | err_settings: | |
2299ef71 BP |
3648 | kfree(s); |
3649 | ecc_stngs[nid] = NULL; | |
3650 | ||
3651 | err_out: | |
3652 | return ret; | |
7d6034d3 DT |
3653 | } |
3654 | ||
3f37a36b | 3655 | static void remove_one_instance(unsigned int nid) |
7d6034d3 | 3656 | { |
360b7f3c BP |
3657 | struct pci_dev *F3 = node_to_amd_nb(nid)->misc; |
3658 | struct ecc_settings *s = ecc_stngs[nid]; | |
3f37a36b BP |
3659 | struct mem_ctl_info *mci; |
3660 | struct amd64_pvt *pvt; | |
7d6034d3 DT |
3661 | |
3662 | /* Remove from EDAC CORE tracking list */ | |
3f37a36b | 3663 | mci = edac_mc_del_mc(&F3->dev); |
7d6034d3 DT |
3664 | if (!mci) |
3665 | return; | |
3666 | ||
3667 | pvt = mci->pvt_info; | |
3668 | ||
360b7f3c | 3669 | restore_ecc_error_reporting(s, nid, F3); |
7d6034d3 | 3670 | |
360b7f3c BP |
3671 | kfree(ecc_stngs[nid]); |
3672 | ecc_stngs[nid] = NULL; | |
ae7bb7c6 | 3673 | |
7d6034d3 | 3674 | /* Free the EDAC CORE resources */ |
8f68ed97 | 3675 | mci->pvt_info = NULL; |
8f68ed97 | 3676 | |
80355a3b | 3677 | hw_info_put(pvt); |
8f68ed97 | 3678 | kfree(pvt); |
7d6034d3 DT |
3679 | edac_mc_free(mci); |
3680 | } | |
3681 | ||
360b7f3c | 3682 | static void setup_pci_device(void) |
7d6034d3 | 3683 | { |
d1ea71cd | 3684 | if (pci_ctl) |
7d6034d3 DT |
3685 | return; |
3686 | ||
706657b1 | 3687 | pci_ctl = edac_pci_create_generic_ctl(pci_ctl_dev, EDAC_MOD_STR); |
d1ea71cd BP |
3688 | if (!pci_ctl) { |
3689 | pr_warn("%s(): Unable to create PCI control\n", __func__); | |
3690 | pr_warn("%s(): PCI error report via EDAC not set\n", __func__); | |
7d6034d3 DT |
3691 | } |
3692 | } | |
3693 | ||
d6efab74 | 3694 | static const struct x86_cpu_id amd64_cpuids[] = { |
29842621 TG |
3695 | X86_MATCH_VENDOR_FAM(AMD, 0x0F, NULL), |
3696 | X86_MATCH_VENDOR_FAM(AMD, 0x10, NULL), | |
3697 | X86_MATCH_VENDOR_FAM(AMD, 0x15, NULL), | |
3698 | X86_MATCH_VENDOR_FAM(AMD, 0x16, NULL), | |
3699 | X86_MATCH_VENDOR_FAM(AMD, 0x17, NULL), | |
3700 | X86_MATCH_VENDOR_FAM(HYGON, 0x18, NULL), | |
3701 | X86_MATCH_VENDOR_FAM(AMD, 0x19, NULL), | |
d6efab74 YG |
3702 | { } |
3703 | }; | |
3704 | MODULE_DEVICE_TABLE(x86cpu, amd64_cpuids); | |
3705 | ||
7d6034d3 DT |
3706 | static int __init amd64_edac_init(void) |
3707 | { | |
301375e7 | 3708 | const char *owner; |
360b7f3c | 3709 | int err = -ENODEV; |
3f37a36b | 3710 | int i; |
7d6034d3 | 3711 | |
301375e7 TK |
3712 | owner = edac_get_owner(); |
3713 | if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR))) | |
3714 | return -EBUSY; | |
3715 | ||
1bd9900b YG |
3716 | if (!x86_match_cpu(amd64_cpuids)) |
3717 | return -ENODEV; | |
3718 | ||
9653a5c7 | 3719 | if (amd_cache_northbridges() < 0) |
1bd9900b | 3720 | return -ENODEV; |
7d6034d3 | 3721 | |
6ba92fea BP |
3722 | opstate_init(); |
3723 | ||
cc4d8860 | 3724 | err = -ENOMEM; |
6396bb22 | 3725 | ecc_stngs = kcalloc(amd_nb_num(), sizeof(ecc_stngs[0]), GFP_KERNEL); |
2ec591ac | 3726 | if (!ecc_stngs) |
a9f0fbe2 | 3727 | goto err_free; |
cc4d8860 | 3728 | |
50542251 | 3729 | msrs = msrs_alloc(); |
56b34b91 | 3730 | if (!msrs) |
360b7f3c | 3731 | goto err_free; |
50542251 | 3732 | |
2287c636 YG |
3733 | for (i = 0; i < amd_nb_num(); i++) { |
3734 | err = probe_one_instance(i); | |
3735 | if (err) { | |
3f37a36b BP |
3736 | /* unwind properly */ |
3737 | while (--i >= 0) | |
3738 | remove_one_instance(i); | |
7d6034d3 | 3739 | |
3f37a36b BP |
3740 | goto err_pci; |
3741 | } | |
2287c636 | 3742 | } |
7d6034d3 | 3743 | |
4688c9b4 YG |
3744 | if (!edac_has_mcs()) { |
3745 | err = -ENODEV; | |
3746 | goto err_pci; | |
3747 | } | |
3748 | ||
234365f5 | 3749 | /* register stuff with EDAC MCE */ |
234365f5 YG |
3750 | if (boot_cpu_data.x86 >= 0x17) |
3751 | amd_register_ecc_decoder(decode_umc_error); | |
3752 | else | |
3753 | amd_register_ecc_decoder(decode_bus_error); | |
3754 | ||
360b7f3c | 3755 | setup_pci_device(); |
f5b10c45 TP |
3756 | |
3757 | #ifdef CONFIG_X86_32 | |
3758 | amd64_err("%s on 32-bit is unsupported. USE AT YOUR OWN RISK!\n", EDAC_MOD_STR); | |
3759 | #endif | |
3760 | ||
de0336b3 BP |
3761 | printk(KERN_INFO "AMD64 EDAC driver v%s\n", EDAC_AMD64_VERSION); |
3762 | ||
360b7f3c | 3763 | return 0; |
7d6034d3 | 3764 | |
56b34b91 | 3765 | err_pci: |
706657b1 BP |
3766 | pci_ctl_dev = NULL; |
3767 | ||
56b34b91 BP |
3768 | msrs_free(msrs); |
3769 | msrs = NULL; | |
cc4d8860 | 3770 | |
360b7f3c | 3771 | err_free: |
360b7f3c BP |
3772 | kfree(ecc_stngs); |
3773 | ecc_stngs = NULL; | |
3774 | ||
7d6034d3 DT |
3775 | return err; |
3776 | } | |
3777 | ||
3778 | static void __exit amd64_edac_exit(void) | |
3779 | { | |
3f37a36b BP |
3780 | int i; |
3781 | ||
d1ea71cd BP |
3782 | if (pci_ctl) |
3783 | edac_pci_release_generic_ctl(pci_ctl); | |
7d6034d3 | 3784 | |
234365f5 | 3785 | /* unregister from EDAC MCE */ |
234365f5 YG |
3786 | if (boot_cpu_data.x86 >= 0x17) |
3787 | amd_unregister_ecc_decoder(decode_umc_error); | |
3788 | else | |
3789 | amd_unregister_ecc_decoder(decode_bus_error); | |
3790 | ||
3f37a36b BP |
3791 | for (i = 0; i < amd_nb_num(); i++) |
3792 | remove_one_instance(i); | |
50542251 | 3793 | |
ae7bb7c6 BP |
3794 | kfree(ecc_stngs); |
3795 | ecc_stngs = NULL; | |
3796 | ||
706657b1 BP |
3797 | pci_ctl_dev = NULL; |
3798 | ||
50542251 BP |
3799 | msrs_free(msrs); |
3800 | msrs = NULL; | |
7d6034d3 DT |
3801 | } |
3802 | ||
3803 | module_init(amd64_edac_init); | |
3804 | module_exit(amd64_edac_exit); | |
3805 | ||
3806 | MODULE_LICENSE("GPL"); | |
3807 | MODULE_AUTHOR("SoftwareBitMaker: Doug Thompson, " | |
3808 | "Dave Peterson, Thayne Harbaugh"); | |
3809 | MODULE_DESCRIPTION("MC support for AMD64 memory controllers - " | |
3810 | EDAC_AMD64_VERSION); | |
3811 | ||
3812 | module_param(edac_op_state, int, 0444); | |
3813 | MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI"); |