blob: 3aeb5173e2007fcd1b4a555e0b334f002c13bbec [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Doug Thompson2bc65412009-05-04 20:11:14 +02002#include "amd64_edac.h"
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +02003#include <asm/amd_nb.h>
Doug Thompson2bc65412009-05-04 20:11:14 +02004
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01005static struct edac_pci_ctl_info *pci_ctl;
Doug Thompson2bc65412009-05-04 20:11:14 +02006
7static int report_gart_errors;
8module_param(report_gart_errors, int, 0644);
9
10/*
11 * Set by command line parameter. If BIOS has enabled the ECC, this override is
12 * cleared to prevent re-enabling the hardware by this driver.
13 */
14static int ecc_enable_override;
15module_param(ecc_enable_override, int, 0644);
16
Tejun Heoa29d8b82010-02-02 14:39:15 +090017static struct msr __percpu *msrs;
Borislav Petkov50542252009-12-11 18:14:40 +010018
Yazen Ghannam38ddd4d2019-10-22 20:35:09 +000019static struct amd64_family_type *fam_type;
20
Borislav Petkov2ec591a2015-02-17 10:58:34 +010021/* Per-node stuff */
Borislav Petkovae7bb7c2010-10-14 16:01:30 +020022static struct ecc_settings **ecc_stngs;
Doug Thompson2bc65412009-05-04 20:11:14 +020023
24/*
Borislav Petkovb70ef012009-06-25 19:32:38 +020025 * 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 */
Daniel J Bluemanc7e53012012-11-30 16:44:20 +080031static const struct scrubrate {
Borislav Petkov39094442010-11-24 19:52:09 +010032 u32 scrubval; /* bit pattern for scrub rate */
33 u32 bandwidth; /* bandwidth consumed (bytes/sec) */
34} scrubrates[] = {
Borislav Petkovb70ef012009-06-25 19:32:38 +020035 { 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
Borislav Petkov66fed2d2012-08-09 18:41:07 +020060int __amd64_read_pci_cfg_dword(struct pci_dev *pdev, int offset,
61 u32 *val, const char *func)
Borislav Petkovb2b0c602010-10-08 18:32:29 +020062{
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
73int __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
86/*
Borislav Petkov73ba8592011-09-19 17:34:45 +020087 * Select DCT to which PCI cfg accesses are routed
88 */
89static 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, &reg);
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -050094 reg &= (pvt->model == 0x30) ? ~3 : ~1;
Borislav Petkov73ba8592011-09-19 17:34:45 +020095 reg |= dct;
96 amd64_write_pci_cfg(pvt->F1, DCT_CFG_SEL, reg);
97}
98
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -050099/*
100 *
101 * Depending on the family, F2 DCT reads need special handling:
102 *
103 * K8: has a single DCT only and no address offsets >= 0x100
104 *
105 * F10h: each DCT has its own set of regs
106 * DCT0 -> F2x040..
107 * DCT1 -> F2x140..
108 *
109 * F16h: has only 1 DCT
110 *
111 * F15h: we select which DCT we access using F1x10C[DctCfgSel]
112 */
113static inline int amd64_read_dct_pci_cfg(struct amd64_pvt *pvt, u8 dct,
114 int offset, u32 *val)
Borislav Petkovb2b0c602010-10-08 18:32:29 +0200115{
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -0500116 switch (pvt->fam) {
117 case 0xf:
118 if (dct || offset >= 0x100)
119 return -EINVAL;
120 break;
Borislav Petkovb2b0c602010-10-08 18:32:29 +0200121
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -0500122 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;
131
132 offset += 0x100;
133 }
134 break;
135
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;
144
145 case 0x16:
146 if (dct)
147 return -EINVAL;
148 break;
149
150 default:
151 break;
Borislav Petkovb2b0c602010-10-08 18:32:29 +0200152 }
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -0500153 return amd64_read_pci_cfg(pvt->F2, offset, val);
Borislav Petkovb2b0c602010-10-08 18:32:29 +0200154}
155
Borislav Petkovb70ef012009-06-25 19:32:38 +0200156/*
Doug Thompson2bc65412009-05-04 20:11:14 +0200157 * 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
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500170static 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}
Doug Thompson2bc65412009-05-04 20:11:14 +0200185/*
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500186 * Scan the scrub rate mapping table for a close or matching bandwidth value to
Doug Thompson2bc65412009-05-04 20:11:14 +0200187 * issue. If requested is too big, then use last maximum value found.
188 */
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500189static int __set_scrub_rate(struct amd64_pvt *pvt, u32 new_bw, u32 min_rate)
Doug Thompson2bc65412009-05-04 20:11:14 +0200190{
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.
Andrew Morton168bfee2012-10-23 14:09:39 -0700199 *
200 * If no suitable bandwidth is found, turn off DRAM scrubbing entirely
201 * by falling back to the last element in scrubrates[].
Doug Thompson2bc65412009-05-04 20:11:14 +0200202 */
Andrew Morton168bfee2012-10-23 14:09:39 -0700203 for (i = 0; i < ARRAY_SIZE(scrubrates) - 1; i++) {
Doug Thompson2bc65412009-05-04 20:11:14 +0200204 /*
205 * skip scrub rates which aren't recommended
206 * (see F10 BKDG, F3x58)
207 */
Borislav Petkov395ae782010-10-01 18:38:19 +0200208 if (scrubrates[i].scrubval < min_rate)
Doug Thompson2bc65412009-05-04 20:11:14 +0200209 continue;
210
211 if (scrubrates[i].bandwidth <= new_bw)
212 break;
Doug Thompson2bc65412009-05-04 20:11:14 +0200213 }
214
215 scrubval = scrubrates[i].scrubval;
Doug Thompson2bc65412009-05-04 20:11:14 +0200216
Pu Wenc4a3e942018-09-27 16:31:28 +0200217 if (pvt->fam == 0x17 || pvt->fam == 0x18) {
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500218 __f17h_set_scrubval(pvt, scrubval);
219 } else if (pvt->fam == 0x15 && pvt->model == 0x60) {
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500220 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 }
Doug Thompson2bc65412009-05-04 20:11:14 +0200227
Borislav Petkov39094442010-11-24 19:52:09 +0100228 if (scrubval)
229 return scrubrates[i].bandwidth;
230
Doug Thompson2bc65412009-05-04 20:11:14 +0200231 return 0;
232}
233
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100234static int set_scrub_rate(struct mem_ctl_info *mci, u32 bw)
Doug Thompson2bc65412009-05-04 20:11:14 +0200235{
236 struct amd64_pvt *pvt = mci->pvt_info;
Borislav Petkov87b3e0e2011-01-19 20:02:38 +0100237 u32 min_scrubrate = 0x5;
Doug Thompson2bc65412009-05-04 20:11:14 +0200238
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200239 if (pvt->fam == 0xf)
Borislav Petkov87b3e0e2011-01-19 20:02:38 +0100240 min_scrubrate = 0x0;
241
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500242 if (pvt->fam == 0x15) {
243 /* Erratum #505 */
244 if (pvt->model < 0x10)
245 f15h_select_dct(pvt, 0);
Borislav Petkov73ba8592011-09-19 17:34:45 +0200246
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500247 if (pvt->model == 0x60)
248 min_scrubrate = 0x6;
249 }
250 return __set_scrub_rate(pvt, bw, min_scrubrate);
Doug Thompson2bc65412009-05-04 20:11:14 +0200251}
252
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100253static int get_scrub_rate(struct mem_ctl_info *mci)
Doug Thompson2bc65412009-05-04 20:11:14 +0200254{
255 struct amd64_pvt *pvt = mci->pvt_info;
Borislav Petkov39094442010-11-24 19:52:09 +0100256 int i, retval = -EINVAL;
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500257 u32 scrubval = 0;
Doug Thompson2bc65412009-05-04 20:11:14 +0200258
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500259 switch (pvt->fam) {
260 case 0x15:
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500261 /* Erratum #505 */
262 if (pvt->model < 0x10)
263 f15h_select_dct(pvt, 0);
Borislav Petkov73ba8592011-09-19 17:34:45 +0200264
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500265 if (pvt->model == 0x60)
266 amd64_read_pci_cfg(pvt->F2, F15H_M60H_SCRCTRL, &scrubval);
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500267 break;
268
269 case 0x17:
Pu Wenc4a3e942018-09-27 16:31:28 +0200270 case 0x18:
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500271 amd64_read_pci_cfg(pvt->F6, F17H_SCR_BASE_ADDR, &scrubval);
272 if (scrubval & BIT(0)) {
273 amd64_read_pci_cfg(pvt->F6, F17H_SCR_LIMIT_ADDR, &scrubval);
274 scrubval &= 0xF;
275 scrubval += 0x5;
276 } else {
277 scrubval = 0;
278 }
279 break;
280
281 default:
Aravind Gopalakrishnanda921102015-09-28 06:43:12 -0500282 amd64_read_pci_cfg(pvt->F3, SCRCTRL, &scrubval);
Yazen Ghannam8051c0a2016-11-17 17:57:42 -0500283 break;
284 }
Doug Thompson2bc65412009-05-04 20:11:14 +0200285
286 scrubval = scrubval & 0x001F;
287
Roel Kluin926311f2010-01-11 20:58:21 +0100288 for (i = 0; i < ARRAY_SIZE(scrubrates); i++) {
Doug Thompson2bc65412009-05-04 20:11:14 +0200289 if (scrubrates[i].scrubval == scrubval) {
Borislav Petkov39094442010-11-24 19:52:09 +0100290 retval = scrubrates[i].bandwidth;
Doug Thompson2bc65412009-05-04 20:11:14 +0200291 break;
292 }
293 }
Borislav Petkov39094442010-11-24 19:52:09 +0100294 return retval;
Doug Thompson2bc65412009-05-04 20:11:14 +0200295}
296
Doug Thompson67757632009-04-27 15:53:22 +0200297/*
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200298 * returns true if the SysAddr given by sys_addr matches the
299 * DRAM base/limit associated with node_id
Doug Thompson67757632009-04-27 15:53:22 +0200300 */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100301static bool base_limit_match(struct amd64_pvt *pvt, u64 sys_addr, u8 nid)
Doug Thompson67757632009-04-27 15:53:22 +0200302{
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200303 u64 addr;
Doug Thompson67757632009-04-27 15:53:22 +0200304
305 /* The K8 treats this as a 40-bit value. However, bits 63-40 will be
306 * all ones if the most significant implemented address bit is 1.
307 * Here we discard bits 63-40. See section 3.4.2 of AMD publication
308 * 24592: AMD x86-64 Architecture Programmer's Manual Volume 1
309 * Application Programming.
310 */
311 addr = sys_addr & 0x000000ffffffffffull;
312
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200313 return ((addr >= get_dram_base(pvt, nid)) &&
314 (addr <= get_dram_limit(pvt, nid)));
Doug Thompson67757632009-04-27 15:53:22 +0200315}
316
317/*
318 * Attempt to map a SysAddr to a node. On success, return a pointer to the
319 * mem_ctl_info structure for the node that the SysAddr maps to.
320 *
321 * On failure, return NULL.
322 */
323static struct mem_ctl_info *find_mc_by_sys_addr(struct mem_ctl_info *mci,
324 u64 sys_addr)
325{
326 struct amd64_pvt *pvt;
Daniel J Bluemanc7e53012012-11-30 16:44:20 +0800327 u8 node_id;
Doug Thompson67757632009-04-27 15:53:22 +0200328 u32 intlv_en, bits;
329
330 /*
331 * Here we use the DRAM Base (section 3.4.4.1) and DRAM Limit (section
332 * 3.4.4.2) registers to map the SysAddr to a node ID.
333 */
334 pvt = mci->pvt_info;
335
336 /*
337 * The value of this field should be the same for all DRAM Base
338 * registers. Therefore we arbitrarily choose to read it from the
339 * register for node 0.
340 */
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200341 intlv_en = dram_intlv_en(pvt, 0);
Doug Thompson67757632009-04-27 15:53:22 +0200342
343 if (intlv_en == 0) {
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200344 for (node_id = 0; node_id < DRAM_RANGES; node_id++) {
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100345 if (base_limit_match(pvt, sys_addr, node_id))
Borislav Petkov8edc5442009-09-18 12:39:19 +0200346 goto found;
Doug Thompson67757632009-04-27 15:53:22 +0200347 }
Borislav Petkov8edc5442009-09-18 12:39:19 +0200348 goto err_no_match;
Doug Thompson67757632009-04-27 15:53:22 +0200349 }
350
Borislav Petkov72f158f2009-09-18 12:27:27 +0200351 if (unlikely((intlv_en != 0x01) &&
352 (intlv_en != 0x03) &&
353 (intlv_en != 0x07))) {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +0200354 amd64_warn("DRAM Base[IntlvEn] junk value: 0x%x, BIOS bug?\n", intlv_en);
Doug Thompson67757632009-04-27 15:53:22 +0200355 return NULL;
356 }
357
358 bits = (((u32) sys_addr) >> 12) & intlv_en;
359
360 for (node_id = 0; ; ) {
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200361 if ((dram_intlv_sel(pvt, node_id) & intlv_en) == bits)
Doug Thompson67757632009-04-27 15:53:22 +0200362 break; /* intlv_sel field matches */
363
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200364 if (++node_id >= DRAM_RANGES)
Doug Thompson67757632009-04-27 15:53:22 +0200365 goto err_no_match;
366 }
367
368 /* sanity test for sys_addr */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100369 if (unlikely(!base_limit_match(pvt, sys_addr, node_id))) {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +0200370 amd64_warn("%s: sys_addr 0x%llx falls outside base/limit address"
371 "range for node %d with node interleaving enabled.\n",
372 __func__, sys_addr, node_id);
Doug Thompson67757632009-04-27 15:53:22 +0200373 return NULL;
374 }
375
376found:
Borislav Petkovb487c332011-02-21 18:55:00 +0100377 return edac_mc_find((int)node_id);
Doug Thompson67757632009-04-27 15:53:22 +0200378
379err_no_match:
Joe Perches956b9ba2012-04-29 17:08:39 -0300380 edac_dbg(2, "sys_addr 0x%lx doesn't match any node\n",
381 (unsigned long)sys_addr);
Doug Thompson67757632009-04-27 15:53:22 +0200382
383 return NULL;
384}
Doug Thompsone2ce7252009-04-27 15:57:12 +0200385
386/*
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100387 * compute the CS base address of the @csrow on the DRAM controller @dct.
388 * For details see F2x[5C:40] in the processor's BKDG
Doug Thompsone2ce7252009-04-27 15:57:12 +0200389 */
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100390static void get_cs_base_and_mask(struct amd64_pvt *pvt, int csrow, u8 dct,
391 u64 *base, u64 *mask)
Doug Thompsone2ce7252009-04-27 15:57:12 +0200392{
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100393 u64 csbase, csmask, base_bits, mask_bits;
394 u8 addr_shift;
395
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -0500396 if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) {
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100397 csbase = pvt->csels[dct].csbases[csrow];
398 csmask = pvt->csels[dct].csmasks[csrow];
Chen, Gong10ef6b02013-10-18 14:29:07 -0700399 base_bits = GENMASK_ULL(31, 21) | GENMASK_ULL(15, 9);
400 mask_bits = GENMASK_ULL(29, 21) | GENMASK_ULL(15, 9);
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100401 addr_shift = 4;
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -0500402
403 /*
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -0500404 * F16h and F15h, models 30h and later need two addr_shift values:
405 * 8 for high and 6 for low (cf. F16h BKDG).
406 */
407 } else if (pvt->fam == 0x16 ||
408 (pvt->fam == 0x15 && pvt->model >= 0x30)) {
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -0500409 csbase = pvt->csels[dct].csbases[csrow];
410 csmask = pvt->csels[dct].csmasks[csrow >> 1];
411
Chen, Gong10ef6b02013-10-18 14:29:07 -0700412 *base = (csbase & GENMASK_ULL(15, 5)) << 6;
413 *base |= (csbase & GENMASK_ULL(30, 19)) << 8;
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -0500414
415 *mask = ~0ULL;
416 /* poke holes for the csmask */
Chen, Gong10ef6b02013-10-18 14:29:07 -0700417 *mask &= ~((GENMASK_ULL(15, 5) << 6) |
418 (GENMASK_ULL(30, 19) << 8));
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -0500419
Chen, Gong10ef6b02013-10-18 14:29:07 -0700420 *mask |= (csmask & GENMASK_ULL(15, 5)) << 6;
421 *mask |= (csmask & GENMASK_ULL(30, 19)) << 8;
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -0500422
423 return;
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100424 } else {
425 csbase = pvt->csels[dct].csbases[csrow];
426 csmask = pvt->csels[dct].csmasks[csrow >> 1];
427 addr_shift = 8;
428
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200429 if (pvt->fam == 0x15)
Chen, Gong10ef6b02013-10-18 14:29:07 -0700430 base_bits = mask_bits =
431 GENMASK_ULL(30,19) | GENMASK_ULL(13,5);
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100432 else
Chen, Gong10ef6b02013-10-18 14:29:07 -0700433 base_bits = mask_bits =
434 GENMASK_ULL(28,19) | GENMASK_ULL(13,5);
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100435 }
436
437 *base = (csbase & base_bits) << addr_shift;
438
439 *mask = ~0ULL;
440 /* poke holes for the csmask */
441 *mask &= ~(mask_bits << addr_shift);
442 /* OR them in */
443 *mask |= (csmask & mask_bits) << addr_shift;
Doug Thompsone2ce7252009-04-27 15:57:12 +0200444}
445
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100446#define for_each_chip_select(i, dct, pvt) \
447 for (i = 0; i < pvt->csels[dct].b_cnt; i++)
Doug Thompsone2ce7252009-04-27 15:57:12 +0200448
Borislav Petkov614ec9d2011-01-13 18:02:22 +0100449#define chip_select_base(i, dct, pvt) \
450 pvt->csels[dct].csbases[i]
451
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100452#define for_each_chip_select_mask(i, dct, pvt) \
453 for (i = 0; i < pvt->csels[dct].m_cnt; i++)
Doug Thompsone2ce7252009-04-27 15:57:12 +0200454
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +0000455#define for_each_umc(i) \
Yazen Ghannam5e4c5522019-10-22 20:35:11 +0000456 for (i = 0; i < fam_type->max_mcs; i++)
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +0000457
Doug Thompsone2ce7252009-04-27 15:57:12 +0200458/*
459 * @input_addr is an InputAddr associated with the node given by mci. Return the
460 * csrow that input_addr maps to, or -1 on failure (no csrow claims input_addr).
461 */
462static int input_addr_to_csrow(struct mem_ctl_info *mci, u64 input_addr)
463{
464 struct amd64_pvt *pvt;
465 int csrow;
466 u64 base, mask;
467
468 pvt = mci->pvt_info;
469
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100470 for_each_chip_select(csrow, 0, pvt) {
471 if (!csrow_enabled(csrow, 0, pvt))
Doug Thompsone2ce7252009-04-27 15:57:12 +0200472 continue;
473
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100474 get_cs_base_and_mask(pvt, csrow, 0, &base, &mask);
475
476 mask = ~mask;
Doug Thompsone2ce7252009-04-27 15:57:12 +0200477
478 if ((input_addr & mask) == (base & mask)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300479 edac_dbg(2, "InputAddr 0x%lx matches csrow %d (node %d)\n",
480 (unsigned long)input_addr, csrow,
481 pvt->mc_node_id);
Doug Thompsone2ce7252009-04-27 15:57:12 +0200482
483 return csrow;
484 }
485 }
Joe Perches956b9ba2012-04-29 17:08:39 -0300486 edac_dbg(2, "no matching csrow for InputAddr 0x%lx (MC node %d)\n",
487 (unsigned long)input_addr, pvt->mc_node_id);
Doug Thompsone2ce7252009-04-27 15:57:12 +0200488
489 return -1;
490}
491
492/*
Doug Thompsone2ce7252009-04-27 15:57:12 +0200493 * Obtain info from the DRAM Hole Address Register (section 3.4.8, pub #26094)
494 * for the node represented by mci. Info is passed back in *hole_base,
495 * *hole_offset, and *hole_size. Function returns 0 if info is valid or 1 if
496 * info is invalid. Info may be invalid for either of the following reasons:
497 *
498 * - The revision of the node is not E or greater. In this case, the DRAM Hole
499 * Address Register does not exist.
500 *
501 * - The DramHoleValid bit is cleared in the DRAM Hole Address Register,
502 * indicating that its contents are not valid.
503 *
504 * The values passed back in *hole_base, *hole_offset, and *hole_size are
505 * complete 32-bit values despite the fact that the bitfields in the DHAR
506 * only represent bits 31-24 of the base and offset values.
507 */
508int amd64_get_dram_hole_info(struct mem_ctl_info *mci, u64 *hole_base,
509 u64 *hole_offset, u64 *hole_size)
510{
511 struct amd64_pvt *pvt = mci->pvt_info;
Doug Thompsone2ce7252009-04-27 15:57:12 +0200512
513 /* only revE and later have the DRAM Hole Address Register */
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200514 if (pvt->fam == 0xf && pvt->ext_model < K8_REV_E) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300515 edac_dbg(1, " revision %d for node %d does not support DHAR\n",
516 pvt->ext_model, pvt->mc_node_id);
Doug Thompsone2ce7252009-04-27 15:57:12 +0200517 return 1;
518 }
519
Borislav Petkovbc21fa52010-11-11 17:29:13 +0100520 /* valid for Fam10h and above */
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200521 if (pvt->fam >= 0x10 && !dhar_mem_hoist_valid(pvt)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300522 edac_dbg(1, " Dram Memory Hoisting is DISABLED on this system\n");
Doug Thompsone2ce7252009-04-27 15:57:12 +0200523 return 1;
524 }
525
Borislav Petkovc8e518d2010-12-10 19:49:19 +0100526 if (!dhar_valid(pvt)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300527 edac_dbg(1, " Dram Memory Hoisting is DISABLED on this node %d\n",
528 pvt->mc_node_id);
Doug Thompsone2ce7252009-04-27 15:57:12 +0200529 return 1;
530 }
531
532 /* This node has Memory Hoisting */
533
534 /* +------------------+--------------------+--------------------+-----
535 * | memory | DRAM hole | relocated |
536 * | [0, (x - 1)] | [x, 0xffffffff] | addresses from |
537 * | | | DRAM hole |
538 * | | | [0x100000000, |
539 * | | | (0x100000000+ |
540 * | | | (0xffffffff-x))] |
541 * +------------------+--------------------+--------------------+-----
542 *
543 * Above is a diagram of physical memory showing the DRAM hole and the
544 * relocated addresses from the DRAM hole. As shown, the DRAM hole
545 * starts at address x (the base address) and extends through address
546 * 0xffffffff. The DRAM Hole Address Register (DHAR) relocates the
547 * addresses in the hole so that they start at 0x100000000.
548 */
549
Borislav Petkov1f316772012-08-10 12:50:50 +0200550 *hole_base = dhar_base(pvt);
551 *hole_size = (1ULL << 32) - *hole_base;
Doug Thompsone2ce7252009-04-27 15:57:12 +0200552
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200553 *hole_offset = (pvt->fam > 0xf) ? f10_dhar_offset(pvt)
554 : k8_dhar_offset(pvt);
Doug Thompsone2ce7252009-04-27 15:57:12 +0200555
Joe Perches956b9ba2012-04-29 17:08:39 -0300556 edac_dbg(1, " DHAR info for node %d base 0x%lx offset 0x%lx size 0x%lx\n",
557 pvt->mc_node_id, (unsigned long)*hole_base,
558 (unsigned long)*hole_offset, (unsigned long)*hole_size);
Doug Thompsone2ce7252009-04-27 15:57:12 +0200559
560 return 0;
561}
562EXPORT_SYMBOL_GPL(amd64_get_dram_hole_info);
563
Doug Thompson93c2df52009-05-04 20:46:50 +0200564/*
565 * Return the DramAddr that the SysAddr given by @sys_addr maps to. It is
566 * assumed that sys_addr maps to the node given by mci.
567 *
568 * The first part of section 3.4.4 (p. 70) shows how the DRAM Base (section
569 * 3.4.4.1) and DRAM Limit (section 3.4.4.2) registers are used to translate a
570 * SysAddr to a DramAddr. If the DRAM Hole Address Register (DHAR) is enabled,
571 * then it is also involved in translating a SysAddr to a DramAddr. Sections
572 * 3.4.8 and 3.5.8.2 describe the DHAR and how it is used for memory hoisting.
573 * These parts of the documentation are unclear. I interpret them as follows:
574 *
575 * When node n receives a SysAddr, it processes the SysAddr as follows:
576 *
577 * 1. It extracts the DRAMBase and DRAMLimit values from the DRAM Base and DRAM
578 * Limit registers for node n. If the SysAddr is not within the range
579 * specified by the base and limit values, then node n ignores the Sysaddr
580 * (since it does not map to node n). Otherwise continue to step 2 below.
581 *
582 * 2. If the DramHoleValid bit of the DHAR for node n is clear, the DHAR is
583 * disabled so skip to step 3 below. Otherwise see if the SysAddr is within
584 * the range of relocated addresses (starting at 0x100000000) from the DRAM
585 * hole. If not, skip to step 3 below. Else get the value of the
586 * DramHoleOffset field from the DHAR. To obtain the DramAddr, subtract the
587 * offset defined by this value from the SysAddr.
588 *
589 * 3. Obtain the base address for node n from the DRAMBase field of the DRAM
590 * Base register for node n. To obtain the DramAddr, subtract the base
591 * address from the SysAddr, as shown near the start of section 3.4.4 (p.70).
592 */
593static u64 sys_addr_to_dram_addr(struct mem_ctl_info *mci, u64 sys_addr)
594{
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200595 struct amd64_pvt *pvt = mci->pvt_info;
Doug Thompson93c2df52009-05-04 20:46:50 +0200596 u64 dram_base, hole_base, hole_offset, hole_size, dram_addr;
Borislav Petkov1f316772012-08-10 12:50:50 +0200597 int ret;
Doug Thompson93c2df52009-05-04 20:46:50 +0200598
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200599 dram_base = get_dram_base(pvt, pvt->mc_node_id);
Doug Thompson93c2df52009-05-04 20:46:50 +0200600
601 ret = amd64_get_dram_hole_info(mci, &hole_base, &hole_offset,
602 &hole_size);
603 if (!ret) {
Borislav Petkov1f316772012-08-10 12:50:50 +0200604 if ((sys_addr >= (1ULL << 32)) &&
605 (sys_addr < ((1ULL << 32) + hole_size))) {
Doug Thompson93c2df52009-05-04 20:46:50 +0200606 /* use DHAR to translate SysAddr to DramAddr */
607 dram_addr = sys_addr - hole_offset;
608
Joe Perches956b9ba2012-04-29 17:08:39 -0300609 edac_dbg(2, "using DHAR to translate SysAddr 0x%lx to DramAddr 0x%lx\n",
610 (unsigned long)sys_addr,
611 (unsigned long)dram_addr);
Doug Thompson93c2df52009-05-04 20:46:50 +0200612
613 return dram_addr;
614 }
615 }
616
617 /*
618 * Translate the SysAddr to a DramAddr as shown near the start of
619 * section 3.4.4 (p. 70). Although sys_addr is a 64-bit value, the k8
620 * only deals with 40-bit values. Therefore we discard bits 63-40 of
621 * sys_addr below. If bit 39 of sys_addr is 1 then the bits we
622 * discard are all 1s. Otherwise the bits we discard are all 0s. See
623 * section 3.4.2 of AMD publication 24592: AMD x86-64 Architecture
624 * Programmer's Manual Volume 1 Application Programming.
625 */
Chen, Gong10ef6b02013-10-18 14:29:07 -0700626 dram_addr = (sys_addr & GENMASK_ULL(39, 0)) - dram_base;
Doug Thompson93c2df52009-05-04 20:46:50 +0200627
Joe Perches956b9ba2012-04-29 17:08:39 -0300628 edac_dbg(2, "using DRAM Base register to translate SysAddr 0x%lx to DramAddr 0x%lx\n",
629 (unsigned long)sys_addr, (unsigned long)dram_addr);
Doug Thompson93c2df52009-05-04 20:46:50 +0200630 return dram_addr;
631}
632
633/*
634 * @intlv_en is the value of the IntlvEn field from a DRAM Base register
635 * (section 3.4.4.1). Return the number of bits from a SysAddr that are used
636 * for node interleaving.
637 */
638static int num_node_interleave_bits(unsigned intlv_en)
639{
640 static const int intlv_shift_table[] = { 0, 1, 0, 2, 0, 0, 0, 3 };
641 int n;
642
643 BUG_ON(intlv_en > 7);
644 n = intlv_shift_table[intlv_en];
645 return n;
646}
647
648/* Translate the DramAddr given by @dram_addr to an InputAddr. */
649static u64 dram_addr_to_input_addr(struct mem_ctl_info *mci, u64 dram_addr)
650{
651 struct amd64_pvt *pvt;
652 int intlv_shift;
653 u64 input_addr;
654
655 pvt = mci->pvt_info;
656
657 /*
658 * See the start of section 3.4.4 (p. 70, BKDG #26094, K8, revA-E)
659 * concerning translating a DramAddr to an InputAddr.
660 */
Borislav Petkov7f19bf72010-10-21 18:52:53 +0200661 intlv_shift = num_node_interleave_bits(dram_intlv_en(pvt, 0));
Chen, Gong10ef6b02013-10-18 14:29:07 -0700662 input_addr = ((dram_addr >> intlv_shift) & GENMASK_ULL(35, 12)) +
Borislav Petkovf678b8c2010-12-13 19:21:07 +0100663 (dram_addr & 0xfff);
Doug Thompson93c2df52009-05-04 20:46:50 +0200664
Joe Perches956b9ba2012-04-29 17:08:39 -0300665 edac_dbg(2, " Intlv Shift=%d DramAddr=0x%lx maps to InputAddr=0x%lx\n",
666 intlv_shift, (unsigned long)dram_addr,
667 (unsigned long)input_addr);
Doug Thompson93c2df52009-05-04 20:46:50 +0200668
669 return input_addr;
670}
671
672/*
673 * Translate the SysAddr represented by @sys_addr to an InputAddr. It is
674 * assumed that @sys_addr maps to the node given by mci.
675 */
676static u64 sys_addr_to_input_addr(struct mem_ctl_info *mci, u64 sys_addr)
677{
678 u64 input_addr;
679
680 input_addr =
681 dram_addr_to_input_addr(mci, sys_addr_to_dram_addr(mci, sys_addr));
682
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900683 edac_dbg(2, "SysAddr 0x%lx translates to InputAddr 0x%lx\n",
Joe Perches956b9ba2012-04-29 17:08:39 -0300684 (unsigned long)sys_addr, (unsigned long)input_addr);
Doug Thompson93c2df52009-05-04 20:46:50 +0200685
686 return input_addr;
687}
688
Doug Thompson93c2df52009-05-04 20:46:50 +0200689/* Map the Error address to a PAGE and PAGE OFFSET. */
690static inline void error_address_to_page_and_offset(u64 error_address,
Borislav Petkov33ca0642012-08-30 18:01:36 +0200691 struct err_info *err)
Doug Thompson93c2df52009-05-04 20:46:50 +0200692{
Borislav Petkov33ca0642012-08-30 18:01:36 +0200693 err->page = (u32) (error_address >> PAGE_SHIFT);
694 err->offset = ((u32) error_address) & ~PAGE_MASK;
Doug Thompson93c2df52009-05-04 20:46:50 +0200695}
696
697/*
698 * @sys_addr is an error address (a SysAddr) extracted from the MCA NB Address
699 * Low (section 3.6.4.5) and MCA NB Address High (section 3.6.4.6) registers
700 * of a node that detected an ECC memory error. mci represents the node that
701 * the error address maps to (possibly different from the node that detected
702 * the error). Return the number of the csrow that sys_addr maps to, or -1 on
703 * error.
704 */
705static int sys_addr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr)
706{
707 int csrow;
708
709 csrow = input_addr_to_csrow(mci, sys_addr_to_input_addr(mci, sys_addr));
710
711 if (csrow == -1)
Borislav Petkov24f9a7f2010-10-07 18:29:15 +0200712 amd64_mc_err(mci, "Failed to translate InputAddr to csrow for "
713 "address 0x%lx\n", (unsigned long)sys_addr);
Doug Thompson93c2df52009-05-04 20:46:50 +0200714 return csrow;
715}
Doug Thompsone2ce7252009-04-27 15:57:12 +0200716
Borislav Petkovbfc04ae2009-11-12 19:05:07 +0100717static int get_channel_from_ecc_syndrome(struct mem_ctl_info *, u16);
Doug Thompson2da11652009-04-27 16:09:09 +0200718
Doug Thompson2da11652009-04-27 16:09:09 +0200719/*
720 * Determine if the DIMMs have ECC enabled. ECC is enabled ONLY if all the DIMMs
721 * are ECC capable.
722 */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100723static unsigned long determine_edac_cap(struct amd64_pvt *pvt)
Doug Thompson2da11652009-04-27 16:09:09 +0200724{
Dan Carpenter1f6189e2011-10-06 02:30:25 -0400725 unsigned long edac_cap = EDAC_FLAG_NONE;
Yazen Ghannamd27f3a32016-11-17 17:57:40 -0500726 u8 bit;
Doug Thompson2da11652009-04-27 16:09:09 +0200727
Yazen Ghannamd27f3a32016-11-17 17:57:40 -0500728 if (pvt->umc) {
729 u8 i, umc_en_mask = 0, dimm_ecc_en_mask = 0;
Doug Thompson2da11652009-04-27 16:09:09 +0200730
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +0000731 for_each_umc(i) {
Yazen Ghannamd27f3a32016-11-17 17:57:40 -0500732 if (!(pvt->umc[i].sdp_ctrl & UMC_SDP_INIT))
733 continue;
734
735 umc_en_mask |= BIT(i);
736
737 /* UMC Configuration bit 12 (DimmEccEn) */
738 if (pvt->umc[i].umc_cfg & BIT(12))
739 dimm_ecc_en_mask |= BIT(i);
740 }
741
742 if (umc_en_mask == dimm_ecc_en_mask)
743 edac_cap = EDAC_FLAG_SECDED;
744 } else {
745 bit = (pvt->fam > 0xf || pvt->ext_model >= K8_REV_F)
746 ? 19
747 : 17;
748
749 if (pvt->dclr0 & BIT(bit))
750 edac_cap = EDAC_FLAG_SECDED;
751 }
Doug Thompson2da11652009-04-27 16:09:09 +0200752
753 return edac_cap;
754}
755
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100756static void debug_display_dimm_sizes(struct amd64_pvt *, u8);
Doug Thompson2da11652009-04-27 16:09:09 +0200757
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100758static void debug_dump_dramcfg_low(struct amd64_pvt *pvt, u32 dclr, int chan)
Borislav Petkov68798e12009-11-03 16:18:33 +0100759{
Joe Perches956b9ba2012-04-29 17:08:39 -0300760 edac_dbg(1, "F2x%d90 (DRAM Cfg Low): 0x%08x\n", chan, dclr);
Borislav Petkov68798e12009-11-03 16:18:33 +0100761
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +0100762 if (pvt->dram_type == MEM_LRDDR3) {
763 u32 dcsm = pvt->csels[chan].csmasks[0];
764 /*
765 * It's assumed all LRDIMMs in a DCT are going to be of
766 * same 'type' until proven otherwise. So, use a cs
767 * value of '0' here to get dcsm value.
768 */
769 edac_dbg(1, " LRDIMM %dx rank multiply\n", (dcsm & 0x3));
770 }
771
772 edac_dbg(1, "All DIMMs support ECC:%s\n",
773 (dclr & BIT(19)) ? "yes" : "no");
774
Borislav Petkov68798e12009-11-03 16:18:33 +0100775
Joe Perches956b9ba2012-04-29 17:08:39 -0300776 edac_dbg(1, " PAR/ERR parity: %s\n",
777 (dclr & BIT(8)) ? "enabled" : "disabled");
Borislav Petkov68798e12009-11-03 16:18:33 +0100778
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200779 if (pvt->fam == 0x10)
Joe Perches956b9ba2012-04-29 17:08:39 -0300780 edac_dbg(1, " DCT 128bit mode width: %s\n",
781 (dclr & BIT(11)) ? "128b" : "64b");
Borislav Petkov68798e12009-11-03 16:18:33 +0100782
Joe Perches956b9ba2012-04-29 17:08:39 -0300783 edac_dbg(1, " x4 logical DIMMs present: L0: %s L1: %s L2: %s L3: %s\n",
784 (dclr & BIT(12)) ? "yes" : "no",
785 (dclr & BIT(13)) ? "yes" : "no",
786 (dclr & BIT(14)) ? "yes" : "no",
787 (dclr & BIT(15)) ? "yes" : "no");
Borislav Petkov68798e12009-11-03 16:18:33 +0100788}
789
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000790#define CS_EVEN_PRIMARY BIT(0)
791#define CS_ODD_PRIMARY BIT(1)
Yazen Ghannam81f50902019-08-22 00:00:02 +0000792#define CS_EVEN_SECONDARY BIT(2)
793#define CS_ODD_SECONDARY BIT(3)
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000794
Yazen Ghannam81f50902019-08-22 00:00:02 +0000795#define CS_EVEN (CS_EVEN_PRIMARY | CS_EVEN_SECONDARY)
796#define CS_ODD (CS_ODD_PRIMARY | CS_ODD_SECONDARY)
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000797
798static int f17_get_cs_mode(int dimm, u8 ctrl, struct amd64_pvt *pvt)
Yazen Ghannamfc00c6a2019-02-28 15:36:12 +0000799{
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000800 int cs_mode = 0;
Yazen Ghannamfc00c6a2019-02-28 15:36:12 +0000801
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000802 if (csrow_enabled(2 * dimm, ctrl, pvt))
803 cs_mode |= CS_EVEN_PRIMARY;
Yazen Ghannamfc00c6a2019-02-28 15:36:12 +0000804
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000805 if (csrow_enabled(2 * dimm + 1, ctrl, pvt))
806 cs_mode |= CS_ODD_PRIMARY;
807
Yazen Ghannam81f50902019-08-22 00:00:02 +0000808 /* Asymmetric dual-rank DIMM support. */
809 if (csrow_sec_enabled(2 * dimm + 1, ctrl, pvt))
810 cs_mode |= CS_ODD_SECONDARY;
811
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000812 return cs_mode;
Yazen Ghannamfc00c6a2019-02-28 15:36:12 +0000813}
814
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600815static void debug_display_dimm_sizes_df(struct amd64_pvt *pvt, u8 ctrl)
816{
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000817 int dimm, size0, size1, cs0, cs1, cs_mode;
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600818
819 edac_printk(KERN_DEBUG, EDAC_MC, "UMC%d chip selects:\n", ctrl);
820
Yazen Ghannamd971e282019-08-21 23:59:55 +0000821 for (dimm = 0; dimm < 2; dimm++) {
Yazen Ghannameb77e6b2017-04-27 12:11:54 -0500822 cs0 = dimm * 2;
Yazen Ghannameb77e6b2017-04-27 12:11:54 -0500823 cs1 = dimm * 2 + 1;
824
Yazen Ghanname53a3b22019-08-21 23:59:59 +0000825 cs_mode = f17_get_cs_mode(dimm, ctrl, pvt);
826
827 size0 = pvt->ops->dbam_to_cs(pvt, ctrl, cs_mode, cs0);
828 size1 = pvt->ops->dbam_to_cs(pvt, ctrl, cs_mode, cs1);
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600829
830 amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n",
Yazen Ghannameb77e6b2017-04-27 12:11:54 -0500831 cs0, size0,
832 cs1, size1);
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600833 }
834}
835
836static void __dump_misc_regs_df(struct amd64_pvt *pvt)
837{
838 struct amd64_umc *umc;
839 u32 i, tmp, umc_base;
840
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +0000841 for_each_umc(i) {
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600842 umc_base = get_umc_base(i);
843 umc = &pvt->umc[i];
844
845 edac_dbg(1, "UMC%d DIMM cfg: 0x%x\n", i, umc->dimm_cfg);
846 edac_dbg(1, "UMC%d UMC cfg: 0x%x\n", i, umc->umc_cfg);
847 edac_dbg(1, "UMC%d SDP ctrl: 0x%x\n", i, umc->sdp_ctrl);
848 edac_dbg(1, "UMC%d ECC ctrl: 0x%x\n", i, umc->ecc_ctrl);
849
850 amd_smn_read(pvt->mc_node_id, umc_base + UMCCH_ECC_BAD_SYMBOL, &tmp);
851 edac_dbg(1, "UMC%d ECC bad symbol: 0x%x\n", i, tmp);
852
853 amd_smn_read(pvt->mc_node_id, umc_base + UMCCH_UMC_CAP, &tmp);
854 edac_dbg(1, "UMC%d UMC cap: 0x%x\n", i, tmp);
855 edac_dbg(1, "UMC%d UMC cap high: 0x%x\n", i, umc->umc_cap_hi);
856
857 edac_dbg(1, "UMC%d ECC capable: %s, ChipKill ECC capable: %s\n",
858 i, (umc->umc_cap_hi & BIT(30)) ? "yes" : "no",
859 (umc->umc_cap_hi & BIT(31)) ? "yes" : "no");
860 edac_dbg(1, "UMC%d All DIMMs support ECC: %s\n",
861 i, (umc->umc_cfg & BIT(12)) ? "yes" : "no");
862 edac_dbg(1, "UMC%d x4 DIMMs present: %s\n",
863 i, (umc->dimm_cfg & BIT(6)) ? "yes" : "no");
864 edac_dbg(1, "UMC%d x16 DIMMs present: %s\n",
865 i, (umc->dimm_cfg & BIT(7)) ? "yes" : "no");
866
867 if (pvt->dram_type == MEM_LRDDR4) {
868 amd_smn_read(pvt->mc_node_id, umc_base + UMCCH_ADDR_CFG, &tmp);
869 edac_dbg(1, "UMC%d LRDIMM %dx rank multiply\n",
870 i, 1 << ((tmp >> 4) & 0x3));
871 }
872
873 debug_display_dimm_sizes_df(pvt, i);
874 }
875
876 edac_dbg(1, "F0x104 (DRAM Hole Address): 0x%08x, base: 0x%08x\n",
877 pvt->dhar, dhar_base(pvt));
878}
879
Doug Thompson2da11652009-04-27 16:09:09 +0200880/* Display and decode various NB registers for debug purposes. */
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600881static void __dump_misc_regs(struct amd64_pvt *pvt)
Doug Thompson2da11652009-04-27 16:09:09 +0200882{
Joe Perches956b9ba2012-04-29 17:08:39 -0300883 edac_dbg(1, "F3xE8 (NB Cap): 0x%08x\n", pvt->nbcap);
Doug Thompson2da11652009-04-27 16:09:09 +0200884
Joe Perches956b9ba2012-04-29 17:08:39 -0300885 edac_dbg(1, " NB two channel DRAM capable: %s\n",
886 (pvt->nbcap & NBCAP_DCT_DUAL) ? "yes" : "no");
Borislav Petkov68798e12009-11-03 16:18:33 +0100887
Joe Perches956b9ba2012-04-29 17:08:39 -0300888 edac_dbg(1, " ECC capable: %s, ChipKill ECC capable: %s\n",
889 (pvt->nbcap & NBCAP_SECDED) ? "yes" : "no",
890 (pvt->nbcap & NBCAP_CHIPKILL) ? "yes" : "no");
Borislav Petkov68798e12009-11-03 16:18:33 +0100891
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100892 debug_dump_dramcfg_low(pvt, pvt->dclr0, 0);
Doug Thompson2da11652009-04-27 16:09:09 +0200893
Joe Perches956b9ba2012-04-29 17:08:39 -0300894 edac_dbg(1, "F3xB0 (Online Spare): 0x%08x\n", pvt->online_spare);
Doug Thompson2da11652009-04-27 16:09:09 +0200895
Joe Perches956b9ba2012-04-29 17:08:39 -0300896 edac_dbg(1, "F1xF0 (DRAM Hole Address): 0x%08x, base: 0x%08x, offset: 0x%08x\n",
897 pvt->dhar, dhar_base(pvt),
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200898 (pvt->fam == 0xf) ? k8_dhar_offset(pvt)
899 : f10_dhar_offset(pvt));
Doug Thompson2da11652009-04-27 16:09:09 +0200900
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100901 debug_display_dimm_sizes(pvt, 0);
Borislav Petkov4d796362011-02-03 15:59:57 +0100902
Borislav Petkov8de1d912009-10-16 13:39:30 +0200903 /* everything below this point is Fam10h and above */
Borislav Petkova4b4bed2013-08-10 13:54:48 +0200904 if (pvt->fam == 0xf)
Doug Thompson2da11652009-04-27 16:09:09 +0200905 return;
Borislav Petkov4d796362011-02-03 15:59:57 +0100906
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100907 debug_display_dimm_sizes(pvt, 1);
Doug Thompson2da11652009-04-27 16:09:09 +0200908
Borislav Petkov8de1d912009-10-16 13:39:30 +0200909 /* Only if NOT ganged does dclr1 have valid info */
Borislav Petkov68798e12009-11-03 16:18:33 +0100910 if (!dct_ganging_enabled(pvt))
Borislav Petkovd1ea71c2013-12-15 17:54:27 +0100911 debug_dump_dramcfg_low(pvt, pvt->dclr1, 1);
Doug Thompson2da11652009-04-27 16:09:09 +0200912}
913
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600914/* Display and decode various NB registers for debug purposes. */
915static void dump_misc_regs(struct amd64_pvt *pvt)
916{
917 if (pvt->umc)
918 __dump_misc_regs_df(pvt);
919 else
920 __dump_misc_regs(pvt);
921
922 edac_dbg(1, " DramHoleValid: %s\n", dhar_valid(pvt) ? "yes" : "no");
923
Yazen Ghannam78359612019-02-28 15:36:11 +0000924 amd64_info("using x%u syndromes.\n", pvt->ecc_sym_sz);
Yazen Ghannam07ed82e2016-11-28 08:50:21 -0600925}
926
Doug Thompson94be4bf2009-04-27 16:12:00 +0200927/*
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -0500928 * See BKDG, F2x[1,0][5C:40], F2[1,0][6C:60]
Doug Thompson94be4bf2009-04-27 16:12:00 +0200929 */
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100930static void prep_chip_selects(struct amd64_pvt *pvt)
Doug Thompson94be4bf2009-04-27 16:12:00 +0200931{
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -0500932 if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) {
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100933 pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8;
934 pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 8;
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +0100935 } else if (pvt->fam == 0x15 && pvt->model == 0x30) {
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -0500936 pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 4;
937 pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 2;
Yazen Ghannamd971e282019-08-21 23:59:55 +0000938 } else if (pvt->fam >= 0x17) {
939 int umc;
940
941 for_each_umc(umc) {
942 pvt->csels[umc].b_cnt = 4;
943 pvt->csels[umc].m_cnt = 2;
944 }
945
Borislav Petkov9d858bb2009-09-21 14:35:51 +0200946 } else {
Borislav Petkov11c75ea2010-11-29 19:49:02 +0100947 pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8;
948 pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 4;
Doug Thompson94be4bf2009-04-27 16:12:00 +0200949 }
950}
951
Yazen Ghannamd971e282019-08-21 23:59:55 +0000952static void read_umc_base_mask(struct amd64_pvt *pvt)
953{
Yazen Ghannam75747292019-08-22 00:00:01 +0000954 u32 umc_base_reg, umc_base_reg_sec;
955 u32 umc_mask_reg, umc_mask_reg_sec;
956 u32 base_reg, base_reg_sec;
957 u32 mask_reg, mask_reg_sec;
958 u32 *base, *base_sec;
959 u32 *mask, *mask_sec;
Yazen Ghannamd971e282019-08-21 23:59:55 +0000960 int cs, umc;
961
962 for_each_umc(umc) {
963 umc_base_reg = get_umc_base(umc) + UMCCH_BASE_ADDR;
Yazen Ghannam75747292019-08-22 00:00:01 +0000964 umc_base_reg_sec = get_umc_base(umc) + UMCCH_BASE_ADDR_SEC;
Yazen Ghannamd971e282019-08-21 23:59:55 +0000965
966 for_each_chip_select(cs, umc, pvt) {
967 base = &pvt->csels[umc].csbases[cs];
Yazen Ghannam75747292019-08-22 00:00:01 +0000968 base_sec = &pvt->csels[umc].csbases_sec[cs];
Yazen Ghannamd971e282019-08-21 23:59:55 +0000969
970 base_reg = umc_base_reg + (cs * 4);
Yazen Ghannam75747292019-08-22 00:00:01 +0000971 base_reg_sec = umc_base_reg_sec + (cs * 4);
Yazen Ghannamd971e282019-08-21 23:59:55 +0000972
973 if (!amd_smn_read(pvt->mc_node_id, base_reg, base))
974 edac_dbg(0, " DCSB%d[%d]=0x%08x reg: 0x%x\n",
975 umc, cs, *base, base_reg);
Yazen Ghannam75747292019-08-22 00:00:01 +0000976
977 if (!amd_smn_read(pvt->mc_node_id, base_reg_sec, base_sec))
978 edac_dbg(0, " DCSB_SEC%d[%d]=0x%08x reg: 0x%x\n",
979 umc, cs, *base_sec, base_reg_sec);
Yazen Ghannamd971e282019-08-21 23:59:55 +0000980 }
981
982 umc_mask_reg = get_umc_base(umc) + UMCCH_ADDR_MASK;
Yazen Ghannam75747292019-08-22 00:00:01 +0000983 umc_mask_reg_sec = get_umc_base(umc) + UMCCH_ADDR_MASK_SEC;
Yazen Ghannamd971e282019-08-21 23:59:55 +0000984
985 for_each_chip_select_mask(cs, umc, pvt) {
986 mask = &pvt->csels[umc].csmasks[cs];
Yazen Ghannam75747292019-08-22 00:00:01 +0000987 mask_sec = &pvt->csels[umc].csmasks_sec[cs];
Yazen Ghannamd971e282019-08-21 23:59:55 +0000988
989 mask_reg = umc_mask_reg + (cs * 4);
Yazen Ghannam75747292019-08-22 00:00:01 +0000990 mask_reg_sec = umc_mask_reg_sec + (cs * 4);
Yazen Ghannamd971e282019-08-21 23:59:55 +0000991
992 if (!amd_smn_read(pvt->mc_node_id, mask_reg, mask))
993 edac_dbg(0, " DCSM%d[%d]=0x%08x reg: 0x%x\n",
994 umc, cs, *mask, mask_reg);
Yazen Ghannam75747292019-08-22 00:00:01 +0000995
996 if (!amd_smn_read(pvt->mc_node_id, mask_reg_sec, mask_sec))
997 edac_dbg(0, " DCSM_SEC%d[%d]=0x%08x reg: 0x%x\n",
998 umc, cs, *mask_sec, mask_reg_sec);
Yazen Ghannamd971e282019-08-21 23:59:55 +0000999 }
1000 }
1001}
1002
Doug Thompson94be4bf2009-04-27 16:12:00 +02001003/*
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001004 * Function 2 Offset F10_DCSB0; read in the DCS Base and DCS Mask registers
Doug Thompson94be4bf2009-04-27 16:12:00 +02001005 */
Borislav Petkovb2b0c602010-10-08 18:32:29 +02001006static void read_dct_base_mask(struct amd64_pvt *pvt)
Doug Thompson94be4bf2009-04-27 16:12:00 +02001007{
Yazen Ghannamd971e282019-08-21 23:59:55 +00001008 int cs;
Doug Thompson94be4bf2009-04-27 16:12:00 +02001009
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001010 prep_chip_selects(pvt);
Doug Thompson94be4bf2009-04-27 16:12:00 +02001011
Yazen Ghannamd971e282019-08-21 23:59:55 +00001012 if (pvt->umc)
1013 return read_umc_base_mask(pvt);
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05001014
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001015 for_each_chip_select(cs, 0, pvt) {
Yazen Ghannamd971e282019-08-21 23:59:55 +00001016 int reg0 = DCSB0 + (cs * 4);
1017 int reg1 = DCSB1 + (cs * 4);
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001018 u32 *base0 = &pvt->csels[0].csbases[cs];
1019 u32 *base1 = &pvt->csels[1].csbases[cs];
Borislav Petkovb2b0c602010-10-08 18:32:29 +02001020
Yazen Ghannamd971e282019-08-21 23:59:55 +00001021 if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, base0))
1022 edac_dbg(0, " DCSB0[%d]=0x%08x reg: F2x%x\n",
1023 cs, *base0, reg0);
Doug Thompson94be4bf2009-04-27 16:12:00 +02001024
Yazen Ghannamd971e282019-08-21 23:59:55 +00001025 if (pvt->fam == 0xf)
1026 continue;
Borislav Petkovb2b0c602010-10-08 18:32:29 +02001027
Yazen Ghannamd971e282019-08-21 23:59:55 +00001028 if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, base1))
1029 edac_dbg(0, " DCSB1[%d]=0x%08x reg: F2x%x\n",
1030 cs, *base1, (pvt->fam == 0x10) ? reg1
1031 : reg0);
Doug Thompson94be4bf2009-04-27 16:12:00 +02001032 }
1033
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001034 for_each_chip_select_mask(cs, 0, pvt) {
Yazen Ghannamd971e282019-08-21 23:59:55 +00001035 int reg0 = DCSM0 + (cs * 4);
1036 int reg1 = DCSM1 + (cs * 4);
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001037 u32 *mask0 = &pvt->csels[0].csmasks[cs];
1038 u32 *mask1 = &pvt->csels[1].csmasks[cs];
Borislav Petkovb2b0c602010-10-08 18:32:29 +02001039
Yazen Ghannamd971e282019-08-21 23:59:55 +00001040 if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, mask0))
1041 edac_dbg(0, " DCSM0[%d]=0x%08x reg: F2x%x\n",
1042 cs, *mask0, reg0);
Doug Thompson94be4bf2009-04-27 16:12:00 +02001043
Yazen Ghannamd971e282019-08-21 23:59:55 +00001044 if (pvt->fam == 0xf)
1045 continue;
Borislav Petkovb2b0c602010-10-08 18:32:29 +02001046
Yazen Ghannamd971e282019-08-21 23:59:55 +00001047 if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, mask1))
1048 edac_dbg(0, " DCSM1[%d]=0x%08x reg: F2x%x\n",
1049 cs, *mask1, (pvt->fam == 0x10) ? reg1
1050 : reg0);
Doug Thompson94be4bf2009-04-27 16:12:00 +02001051 }
1052}
1053
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001054static void determine_memory_type(struct amd64_pvt *pvt)
Doug Thompson94be4bf2009-04-27 16:12:00 +02001055{
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001056 u32 dram_ctrl, dcsm;
Doug Thompson94be4bf2009-04-27 16:12:00 +02001057
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001058 switch (pvt->fam) {
1059 case 0xf:
1060 if (pvt->ext_model >= K8_REV_F)
1061 goto ddr3;
1062
1063 pvt->dram_type = (pvt->dclr0 & BIT(18)) ? MEM_DDR : MEM_RDDR;
1064 return;
1065
1066 case 0x10:
Borislav Petkov6b4c0bd2009-11-12 15:37:57 +01001067 if (pvt->dchr0 & DDR3_MODE)
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001068 goto ddr3;
1069
1070 pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR2 : MEM_RDDR2;
1071 return;
1072
1073 case 0x15:
1074 if (pvt->model < 0x60)
1075 goto ddr3;
1076
1077 /*
1078 * Model 0x60h needs special handling:
1079 *
1080 * We use a Chip Select value of '0' to obtain dcsm.
1081 * Theoretically, it is possible to populate LRDIMMs of different
1082 * 'Rank' value on a DCT. But this is not the common case. So,
1083 * it's reasonable to assume all DIMMs are going to be of same
1084 * 'type' until proven otherwise.
1085 */
1086 amd64_read_dct_pci_cfg(pvt, 0, DRAM_CONTROL, &dram_ctrl);
1087 dcsm = pvt->csels[0].csmasks[0];
1088
1089 if (((dram_ctrl >> 8) & 0x7) == 0x2)
1090 pvt->dram_type = MEM_DDR4;
1091 else if (pvt->dclr0 & BIT(16))
1092 pvt->dram_type = MEM_DDR3;
1093 else if (dcsm & 0x3)
1094 pvt->dram_type = MEM_LRDDR3;
Borislav Petkov6b4c0bd2009-11-12 15:37:57 +01001095 else
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001096 pvt->dram_type = MEM_RDDR3;
1097
1098 return;
1099
1100 case 0x16:
1101 goto ddr3;
1102
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05001103 case 0x17:
Pu Wenc4a3e942018-09-27 16:31:28 +02001104 case 0x18:
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05001105 if ((pvt->umc[0].dimm_cfg | pvt->umc[1].dimm_cfg) & BIT(5))
1106 pvt->dram_type = MEM_LRDDR4;
1107 else if ((pvt->umc[0].dimm_cfg | pvt->umc[1].dimm_cfg) & BIT(4))
1108 pvt->dram_type = MEM_RDDR4;
1109 else
1110 pvt->dram_type = MEM_DDR4;
1111 return;
1112
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001113 default:
1114 WARN(1, KERN_ERR "%s: Family??? 0x%x\n", __func__, pvt->fam);
1115 pvt->dram_type = MEM_EMPTY;
Doug Thompson94be4bf2009-04-27 16:12:00 +02001116 }
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001117 return;
Doug Thompson94be4bf2009-04-27 16:12:00 +02001118
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001119ddr3:
1120 pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR3 : MEM_RDDR3;
Doug Thompson94be4bf2009-04-27 16:12:00 +02001121}
1122
Borislav Petkovcb328502010-12-22 14:28:24 +01001123/* Get the number of DCT channels the memory controller is using. */
Doug Thompsonddff8762009-04-27 16:14:52 +02001124static int k8_early_channel_count(struct amd64_pvt *pvt)
1125{
Borislav Petkovcb328502010-12-22 14:28:24 +01001126 int flag;
Doug Thompsonddff8762009-04-27 16:14:52 +02001127
Borislav Petkov9f56da02010-10-01 19:44:53 +02001128 if (pvt->ext_model >= K8_REV_F)
Doug Thompsonddff8762009-04-27 16:14:52 +02001129 /* RevF (NPT) and later */
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001130 flag = pvt->dclr0 & WIDTH_128;
Borislav Petkov9f56da02010-10-01 19:44:53 +02001131 else
Doug Thompsonddff8762009-04-27 16:14:52 +02001132 /* RevE and earlier */
1133 flag = pvt->dclr0 & REVE_WIDTH_128;
Doug Thompsonddff8762009-04-27 16:14:52 +02001134
1135 /* not used */
1136 pvt->dclr1 = 0;
1137
1138 return (flag) ? 2 : 1;
1139}
1140
Borislav Petkov70046622011-01-10 14:37:27 +01001141/* On F10h and later ErrAddr is MC4_ADDR[47:1] */
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001142static u64 get_error_address(struct amd64_pvt *pvt, struct mce *m)
Doug Thompsonddff8762009-04-27 16:14:52 +02001143{
Borislav Petkov2ec591a2015-02-17 10:58:34 +01001144 u16 mce_nid = amd_get_nb_id(m->extcpu);
1145 struct mem_ctl_info *mci;
Borislav Petkov70046622011-01-10 14:37:27 +01001146 u8 start_bit = 1;
1147 u8 end_bit = 47;
Borislav Petkov2ec591a2015-02-17 10:58:34 +01001148 u64 addr;
1149
1150 mci = edac_mc_find(mce_nid);
1151 if (!mci)
1152 return 0;
1153
1154 pvt = mci->pvt_info;
Borislav Petkov70046622011-01-10 14:37:27 +01001155
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001156 if (pvt->fam == 0xf) {
Borislav Petkov70046622011-01-10 14:37:27 +01001157 start_bit = 3;
1158 end_bit = 39;
1159 }
1160
Chen, Gong10ef6b02013-10-18 14:29:07 -07001161 addr = m->addr & GENMASK_ULL(end_bit, start_bit);
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001162
1163 /*
1164 * Erratum 637 workaround
1165 */
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001166 if (pvt->fam == 0x15) {
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001167 u64 cc6_base, tmp_addr;
1168 u32 tmp;
Daniel J Blueman8b84c8d2012-11-27 14:32:10 +08001169 u8 intlv_en;
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001170
Chen, Gong10ef6b02013-10-18 14:29:07 -07001171 if ((addr & GENMASK_ULL(47, 24)) >> 24 != 0x00fdf7)
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001172 return addr;
1173
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001174
1175 amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_LIM, &tmp);
1176 intlv_en = tmp >> 21 & 0x7;
1177
1178 /* add [47:27] + 3 trailing bits */
Chen, Gong10ef6b02013-10-18 14:29:07 -07001179 cc6_base = (tmp & GENMASK_ULL(20, 0)) << 3;
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001180
1181 /* reverse and add DramIntlvEn */
1182 cc6_base |= intlv_en ^ 0x7;
1183
1184 /* pin at [47:24] */
1185 cc6_base <<= 24;
1186
1187 if (!intlv_en)
Chen, Gong10ef6b02013-10-18 14:29:07 -07001188 return cc6_base | (addr & GENMASK_ULL(23, 0));
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001189
1190 amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_BASE, &tmp);
1191
1192 /* faster log2 */
Chen, Gong10ef6b02013-10-18 14:29:07 -07001193 tmp_addr = (addr & GENMASK_ULL(23, 12)) << __fls(intlv_en + 1);
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001194
1195 /* OR DramIntlvSel into bits [14:12] */
Chen, Gong10ef6b02013-10-18 14:29:07 -07001196 tmp_addr |= (tmp & GENMASK_ULL(23, 21)) >> 9;
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001197
1198 /* add remaining [11:0] bits from original MC4_ADDR */
Chen, Gong10ef6b02013-10-18 14:29:07 -07001199 tmp_addr |= addr & GENMASK_ULL(11, 0);
Borislav Petkovc1ae6832011-03-30 15:42:10 +02001200
1201 return cc6_base | tmp_addr;
1202 }
1203
1204 return addr;
Doug Thompsonddff8762009-04-27 16:14:52 +02001205}
1206
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001207static struct pci_dev *pci_get_related_function(unsigned int vendor,
1208 unsigned int device,
1209 struct pci_dev *related)
1210{
1211 struct pci_dev *dev = NULL;
1212
1213 while ((dev = pci_get_device(vendor, device, dev))) {
1214 if (pci_domain_nr(dev->bus) == pci_domain_nr(related->bus) &&
1215 (dev->bus->number == related->bus->number) &&
1216 (PCI_SLOT(dev->devfn) == PCI_SLOT(related->devfn)))
1217 break;
1218 }
1219
1220 return dev;
1221}
1222
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001223static void read_dram_base_limit_regs(struct amd64_pvt *pvt, unsigned range)
Doug Thompsonddff8762009-04-27 16:14:52 +02001224{
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001225 struct amd_northbridge *nb;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001226 struct pci_dev *f1 = NULL;
1227 unsigned int pci_func;
Borislav Petkov71d2a322011-02-21 19:37:24 +01001228 int off = range << 3;
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001229 u32 llim;
Doug Thompsonddff8762009-04-27 16:14:52 +02001230
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001231 amd64_read_pci_cfg(pvt->F1, DRAM_BASE_LO + off, &pvt->ranges[range].base.lo);
1232 amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_LO + off, &pvt->ranges[range].lim.lo);
Doug Thompsonddff8762009-04-27 16:14:52 +02001233
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001234 if (pvt->fam == 0xf)
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001235 return;
Doug Thompsonddff8762009-04-27 16:14:52 +02001236
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001237 if (!dram_rw(pvt, range))
1238 return;
Doug Thompsonddff8762009-04-27 16:14:52 +02001239
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001240 amd64_read_pci_cfg(pvt->F1, DRAM_BASE_HI + off, &pvt->ranges[range].base.hi);
1241 amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_HI + off, &pvt->ranges[range].lim.hi);
Borislav Petkovf08e4572011-03-21 20:45:06 +01001242
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001243 /* F15h: factor in CC6 save area by reading dst node's limit reg */
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001244 if (pvt->fam != 0x15)
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001245 return;
Borislav Petkovf08e4572011-03-21 20:45:06 +01001246
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001247 nb = node_to_amd_nb(dram_dst_node(pvt, range));
1248 if (WARN_ON(!nb))
1249 return;
Borislav Petkovf08e4572011-03-21 20:45:06 +01001250
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001251 if (pvt->model == 0x60)
1252 pci_func = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1;
1253 else if (pvt->model == 0x30)
1254 pci_func = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1;
1255 else
1256 pci_func = PCI_DEVICE_ID_AMD_15H_NB_F1;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001257
1258 f1 = pci_get_related_function(nb->misc->vendor, pci_func, nb->misc);
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001259 if (WARN_ON(!f1))
1260 return;
Borislav Petkovf08e4572011-03-21 20:45:06 +01001261
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001262 amd64_read_pci_cfg(f1, DRAM_LOCAL_NODE_LIM, &llim);
Borislav Petkovf08e4572011-03-21 20:45:06 +01001263
Chen, Gong10ef6b02013-10-18 14:29:07 -07001264 pvt->ranges[range].lim.lo &= GENMASK_ULL(15, 0);
Borislav Petkovf08e4572011-03-21 20:45:06 +01001265
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001266 /* {[39:27],111b} */
1267 pvt->ranges[range].lim.lo |= ((llim & 0x1fff) << 3 | 0x7) << 16;
Borislav Petkovf08e4572011-03-21 20:45:06 +01001268
Chen, Gong10ef6b02013-10-18 14:29:07 -07001269 pvt->ranges[range].lim.hi &= GENMASK_ULL(7, 0);
Borislav Petkovf08e4572011-03-21 20:45:06 +01001270
Daniel J Bluemane2c0bff2012-11-30 16:44:19 +08001271 /* [47:40] */
1272 pvt->ranges[range].lim.hi |= llim >> 13;
1273
1274 pci_dev_put(f1);
Doug Thompsonddff8762009-04-27 16:14:52 +02001275}
1276
Borislav Petkovf192c7b2011-01-10 14:24:32 +01001277static void k8_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr,
Borislav Petkov33ca0642012-08-30 18:01:36 +02001278 struct err_info *err)
Doug Thompsonddff8762009-04-27 16:14:52 +02001279{
Borislav Petkovf192c7b2011-01-10 14:24:32 +01001280 struct amd64_pvt *pvt = mci->pvt_info;
Doug Thompsonddff8762009-04-27 16:14:52 +02001281
Borislav Petkov33ca0642012-08-30 18:01:36 +02001282 error_address_to_page_and_offset(sys_addr, err);
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03001283
1284 /*
1285 * Find out which node the error address belongs to. This may be
1286 * different from the node that detected the error.
1287 */
Borislav Petkov33ca0642012-08-30 18:01:36 +02001288 err->src_mci = find_mc_by_sys_addr(mci, sys_addr);
1289 if (!err->src_mci) {
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03001290 amd64_mc_err(mci, "failed to map error addr 0x%lx to a node\n",
1291 (unsigned long)sys_addr);
Borislav Petkov33ca0642012-08-30 18:01:36 +02001292 err->err_code = ERR_NODE;
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03001293 return;
1294 }
1295
1296 /* Now map the sys_addr to a CSROW */
Borislav Petkov33ca0642012-08-30 18:01:36 +02001297 err->csrow = sys_addr_to_csrow(err->src_mci, sys_addr);
1298 if (err->csrow < 0) {
1299 err->err_code = ERR_CSROW;
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03001300 return;
1301 }
1302
Doug Thompsonddff8762009-04-27 16:14:52 +02001303 /* CHIPKILL enabled */
Borislav Petkovf192c7b2011-01-10 14:24:32 +01001304 if (pvt->nbcfg & NBCFG_CHIPKILL) {
Borislav Petkov33ca0642012-08-30 18:01:36 +02001305 err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome);
1306 if (err->channel < 0) {
Doug Thompsonddff8762009-04-27 16:14:52 +02001307 /*
1308 * Syndrome didn't map, so we don't know which of the
1309 * 2 DIMMs is in error. So we need to ID 'both' of them
1310 * as suspect.
1311 */
Borislav Petkov33ca0642012-08-30 18:01:36 +02001312 amd64_mc_warn(err->src_mci, "unknown syndrome 0x%04x - "
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03001313 "possible error reporting race\n",
Borislav Petkov33ca0642012-08-30 18:01:36 +02001314 err->syndrome);
1315 err->err_code = ERR_CHANNEL;
Doug Thompsonddff8762009-04-27 16:14:52 +02001316 return;
1317 }
1318 } else {
1319 /*
1320 * non-chipkill ecc mode
1321 *
1322 * The k8 documentation is unclear about how to determine the
1323 * channel number when using non-chipkill memory. This method
1324 * was obtained from email communication with someone at AMD.
1325 * (Wish the email was placed in this comment - norsk)
1326 */
Borislav Petkov33ca0642012-08-30 18:01:36 +02001327 err->channel = ((sys_addr & BIT(3)) != 0);
Doug Thompsonddff8762009-04-27 16:14:52 +02001328 }
Doug Thompsonddff8762009-04-27 16:14:52 +02001329}
1330
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001331static int ddr2_cs_size(unsigned i, bool dct_width)
Doug Thompsonddff8762009-04-27 16:14:52 +02001332{
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001333 unsigned shift = 0;
Doug Thompsonddff8762009-04-27 16:14:52 +02001334
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001335 if (i <= 2)
1336 shift = i;
1337 else if (!(i & 0x1))
1338 shift = i >> 1;
Borislav Petkov1433eb92009-10-21 13:44:36 +02001339 else
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001340 shift = (i + 1) >> 1;
Doug Thompsonddff8762009-04-27 16:14:52 +02001341
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001342 return 128 << (shift + !!dct_width);
1343}
1344
1345static int k8_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001346 unsigned cs_mode, int cs_mask_nr)
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001347{
1348 u32 dclr = dct ? pvt->dclr1 : pvt->dclr0;
1349
1350 if (pvt->ext_model >= K8_REV_F) {
1351 WARN_ON(cs_mode > 11);
1352 return ddr2_cs_size(cs_mode, dclr & WIDTH_128);
1353 }
1354 else if (pvt->ext_model >= K8_REV_D) {
Borislav Petkov11b0a312011-11-09 21:28:43 +01001355 unsigned diff;
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001356 WARN_ON(cs_mode > 10);
1357
Borislav Petkov11b0a312011-11-09 21:28:43 +01001358 /*
1359 * the below calculation, besides trying to win an obfuscated C
1360 * contest, maps cs_mode values to DIMM chip select sizes. The
1361 * mappings are:
1362 *
1363 * cs_mode CS size (mb)
1364 * ======= ============
1365 * 0 32
1366 * 1 64
1367 * 2 128
1368 * 3 128
1369 * 4 256
1370 * 5 512
1371 * 6 256
1372 * 7 512
1373 * 8 1024
1374 * 9 1024
1375 * 10 2048
1376 *
1377 * Basically, it calculates a value with which to shift the
1378 * smallest CS size of 32MB.
1379 *
1380 * ddr[23]_cs_size have a similar purpose.
1381 */
1382 diff = cs_mode/3 + (unsigned)(cs_mode > 5);
1383
1384 return 32 << (cs_mode - diff);
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001385 }
1386 else {
1387 WARN_ON(cs_mode > 6);
1388 return 32 << cs_mode;
1389 }
Doug Thompsonddff8762009-04-27 16:14:52 +02001390}
1391
Doug Thompson1afd3c92009-04-27 16:16:50 +02001392/*
1393 * Get the number of DCT channels in use.
1394 *
1395 * Return:
1396 * number of Memory Channels in operation
1397 * Pass back:
1398 * contents of the DCL0_LOW register
1399 */
Borislav Petkov7d20d142011-01-07 17:58:04 +01001400static int f1x_early_channel_count(struct amd64_pvt *pvt)
Doug Thompson1afd3c92009-04-27 16:16:50 +02001401{
Borislav Petkov6ba5dcd2009-10-13 19:26:55 +02001402 int i, j, channels = 0;
Doug Thompsonddff8762009-04-27 16:14:52 +02001403
Borislav Petkov7d20d142011-01-07 17:58:04 +01001404 /* On F10h, if we are in 128 bit mode, then we are using 2 channels */
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001405 if (pvt->fam == 0x10 && (pvt->dclr0 & WIDTH_128))
Borislav Petkov7d20d142011-01-07 17:58:04 +01001406 return 2;
Doug Thompson1afd3c92009-04-27 16:16:50 +02001407
1408 /*
Borislav Petkovd16149e2009-10-16 19:55:49 +02001409 * Need to check if in unganged mode: In such, there are 2 channels,
1410 * but they are not in 128 bit mode and thus the above 'dclr0' status
1411 * bit will be OFF.
Doug Thompson1afd3c92009-04-27 16:16:50 +02001412 *
1413 * Need to check DCT0[0] and DCT1[0] to see if only one of them has
1414 * their CSEnable bit on. If so, then SINGLE DIMM case.
1415 */
Joe Perches956b9ba2012-04-29 17:08:39 -03001416 edac_dbg(0, "Data width is not 128 bits - need more decoding\n");
Doug Thompson1afd3c92009-04-27 16:16:50 +02001417
1418 /*
1419 * Check DRAM Bank Address Mapping values for each DIMM to see if there
1420 * is more than just one DIMM present in unganged mode. Need to check
1421 * both controllers since DIMMs can be placed in either one.
1422 */
Borislav Petkov525a1b22010-12-21 15:53:27 +01001423 for (i = 0; i < 2; i++) {
1424 u32 dbam = (i ? pvt->dbam1 : pvt->dbam0);
Doug Thompson1afd3c92009-04-27 16:16:50 +02001425
Wan Wei57a30852009-08-07 17:04:49 +02001426 for (j = 0; j < 4; j++) {
1427 if (DBAM_DIMM(j, dbam) > 0) {
1428 channels++;
1429 break;
1430 }
1431 }
Doug Thompson1afd3c92009-04-27 16:16:50 +02001432 }
1433
Borislav Petkovd16149e2009-10-16 19:55:49 +02001434 if (channels > 2)
1435 channels = 2;
1436
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02001437 amd64_info("MCT channel count: %d\n", channels);
Doug Thompson1afd3c92009-04-27 16:16:50 +02001438
1439 return channels;
Doug Thompson1afd3c92009-04-27 16:16:50 +02001440}
1441
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001442static int f17_early_channel_count(struct amd64_pvt *pvt)
1443{
1444 int i, channels = 0;
1445
1446 /* SDP Control bit 31 (SdpInit) is clear for unused UMC channels */
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +00001447 for_each_umc(i)
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001448 channels += !!(pvt->umc[i].sdp_ctrl & UMC_SDP_INIT);
1449
1450 amd64_info("MCT channel count: %d\n", channels);
1451
1452 return channels;
1453}
1454
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001455static int ddr3_cs_size(unsigned i, bool dct_width)
Doug Thompson1afd3c92009-04-27 16:16:50 +02001456{
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001457 unsigned shift = 0;
1458 int cs_size = 0;
1459
1460 if (i == 0 || i == 3 || i == 4)
1461 cs_size = -1;
1462 else if (i <= 2)
1463 shift = i;
1464 else if (i == 12)
1465 shift = 7;
1466 else if (!(i & 0x1))
1467 shift = i >> 1;
1468 else
1469 shift = (i + 1) >> 1;
1470
1471 if (cs_size != -1)
1472 cs_size = (128 * (1 << !!dct_width)) << shift;
1473
1474 return cs_size;
1475}
1476
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001477static int ddr3_lrdimm_cs_size(unsigned i, unsigned rank_multiply)
1478{
1479 unsigned shift = 0;
1480 int cs_size = 0;
1481
1482 if (i < 4 || i == 6)
1483 cs_size = -1;
1484 else if (i == 12)
1485 shift = 7;
1486 else if (!(i & 0x1))
1487 shift = i >> 1;
1488 else
1489 shift = (i + 1) >> 1;
1490
1491 if (cs_size != -1)
1492 cs_size = rank_multiply * (128 << shift);
1493
1494 return cs_size;
1495}
1496
1497static int ddr4_cs_size(unsigned i)
1498{
1499 int cs_size = 0;
1500
1501 if (i == 0)
1502 cs_size = -1;
1503 else if (i == 1)
1504 cs_size = 1024;
1505 else
1506 /* Min cs_size = 1G */
1507 cs_size = 1024 * (1 << (i >> 1));
1508
1509 return cs_size;
1510}
1511
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001512static int f10_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001513 unsigned cs_mode, int cs_mask_nr)
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001514{
1515 u32 dclr = dct ? pvt->dclr1 : pvt->dclr0;
1516
1517 WARN_ON(cs_mode > 11);
Borislav Petkov1433eb92009-10-21 13:44:36 +02001518
1519 if (pvt->dchr0 & DDR3_MODE || pvt->dchr1 & DDR3_MODE)
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001520 return ddr3_cs_size(cs_mode, dclr & WIDTH_128);
Borislav Petkov1433eb92009-10-21 13:44:36 +02001521 else
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001522 return ddr2_cs_size(cs_mode, dclr & WIDTH_128);
1523}
Borislav Petkov1433eb92009-10-21 13:44:36 +02001524
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001525/*
1526 * F15h supports only 64bit DCT interfaces
1527 */
1528static int f15_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001529 unsigned cs_mode, int cs_mask_nr)
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01001530{
1531 WARN_ON(cs_mode > 12);
1532
1533 return ddr3_cs_size(cs_mode, false);
Doug Thompson1afd3c92009-04-27 16:16:50 +02001534}
1535
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001536/* F15h M60h supports DDR4 mapping as well.. */
1537static int f15_m60h_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
1538 unsigned cs_mode, int cs_mask_nr)
1539{
1540 int cs_size;
1541 u32 dcsm = pvt->csels[dct].csmasks[cs_mask_nr];
1542
1543 WARN_ON(cs_mode > 12);
1544
1545 if (pvt->dram_type == MEM_DDR4) {
1546 if (cs_mode > 9)
1547 return -1;
1548
1549 cs_size = ddr4_cs_size(cs_mode);
1550 } else if (pvt->dram_type == MEM_LRDDR3) {
1551 unsigned rank_multiply = dcsm & 0xf;
1552
1553 if (rank_multiply == 3)
1554 rank_multiply = 4;
1555 cs_size = ddr3_lrdimm_cs_size(cs_mode, rank_multiply);
1556 } else {
1557 /* Minimum cs size is 512mb for F15hM60h*/
1558 if (cs_mode == 0x1)
1559 return -1;
1560
1561 cs_size = ddr3_cs_size(cs_mode, false);
1562 }
1563
1564 return cs_size;
1565}
1566
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05001567/*
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001568 * F16h and F15h model 30h have only limited cs_modes.
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05001569 */
1570static int f16_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01001571 unsigned cs_mode, int cs_mask_nr)
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05001572{
1573 WARN_ON(cs_mode > 12);
1574
1575 if (cs_mode == 6 || cs_mode == 8 ||
1576 cs_mode == 9 || cs_mode == 12)
1577 return -1;
1578 else
1579 return ddr3_cs_size(cs_mode, false);
1580}
1581
Yazen Ghanname53a3b22019-08-21 23:59:59 +00001582static int f17_addr_mask_to_cs_size(struct amd64_pvt *pvt, u8 umc,
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001583 unsigned int cs_mode, int csrow_nr)
1584{
Yazen Ghanname53a3b22019-08-21 23:59:59 +00001585 u32 addr_mask_orig, addr_mask_deinterleaved;
1586 u32 msb, weight, num_zero_bits;
1587 int dimm, size = 0;
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001588
Yazen Ghanname53a3b22019-08-21 23:59:59 +00001589 /* No Chip Selects are enabled. */
1590 if (!cs_mode)
1591 return size;
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001592
Yazen Ghanname53a3b22019-08-21 23:59:59 +00001593 /* Requested size of an even CS but none are enabled. */
1594 if (!(cs_mode & CS_EVEN) && !(csrow_nr & 1))
1595 return size;
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001596
Yazen Ghanname53a3b22019-08-21 23:59:59 +00001597 /* Requested size of an odd CS but none are enabled. */
1598 if (!(cs_mode & CS_ODD) && (csrow_nr & 1))
1599 return size;
1600
1601 /*
1602 * There is one mask per DIMM, and two Chip Selects per DIMM.
1603 * CS0 and CS1 -> DIMM0
1604 * CS2 and CS3 -> DIMM1
1605 */
1606 dimm = csrow_nr >> 1;
1607
Yazen Ghannam81f50902019-08-22 00:00:02 +00001608 /* Asymmetric dual-rank DIMM support. */
1609 if ((csrow_nr & 1) && (cs_mode & CS_ODD_SECONDARY))
1610 addr_mask_orig = pvt->csels[umc].csmasks_sec[dimm];
1611 else
1612 addr_mask_orig = pvt->csels[umc].csmasks[dimm];
Yazen Ghanname53a3b22019-08-21 23:59:59 +00001613
1614 /*
1615 * The number of zero bits in the mask is equal to the number of bits
1616 * in a full mask minus the number of bits in the current mask.
1617 *
1618 * The MSB is the number of bits in the full mask because BIT[0] is
1619 * always 0.
1620 */
1621 msb = fls(addr_mask_orig) - 1;
1622 weight = hweight_long(addr_mask_orig);
1623 num_zero_bits = msb - weight;
1624
1625 /* Take the number of zero bits off from the top of the mask. */
1626 addr_mask_deinterleaved = GENMASK_ULL(msb - num_zero_bits, 1);
1627
1628 edac_dbg(1, "CS%d DIMM%d AddrMasks:\n", csrow_nr, dimm);
1629 edac_dbg(1, " Original AddrMask: 0x%x\n", addr_mask_orig);
1630 edac_dbg(1, " Deinterleaved AddrMask: 0x%x\n", addr_mask_deinterleaved);
1631
1632 /* Register [31:1] = Address [39:9]. Size is in kBs here. */
1633 size = (addr_mask_deinterleaved >> 2) + 1;
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05001634
1635 /* Return size in MBs. */
1636 return size >> 10;
1637}
1638
Borislav Petkov5a5d2372011-01-17 17:52:57 +01001639static void read_dram_ctl_register(struct amd64_pvt *pvt)
Doug Thompson6163b5d2009-04-27 16:20:17 +02001640{
Doug Thompson6163b5d2009-04-27 16:20:17 +02001641
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001642 if (pvt->fam == 0xf)
Borislav Petkov5a5d2372011-01-17 17:52:57 +01001643 return;
1644
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05001645 if (!amd64_read_pci_cfg(pvt->F2, DCT_SEL_LO, &pvt->dct_sel_lo)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001646 edac_dbg(0, "F2x110 (DCTSelLow): 0x%08x, High range addrs at: 0x%x\n",
1647 pvt->dct_sel_lo, dct_sel_baseaddr(pvt));
Doug Thompson6163b5d2009-04-27 16:20:17 +02001648
Joe Perches956b9ba2012-04-29 17:08:39 -03001649 edac_dbg(0, " DCTs operate in %s mode\n",
1650 (dct_ganging_enabled(pvt) ? "ganged" : "unganged"));
Doug Thompson6163b5d2009-04-27 16:20:17 +02001651
Borislav Petkov72381bd2009-10-09 19:14:43 +02001652 if (!dct_ganging_enabled(pvt))
Joe Perches956b9ba2012-04-29 17:08:39 -03001653 edac_dbg(0, " Address range split per DCT: %s\n",
1654 (dct_high_range_enabled(pvt) ? "yes" : "no"));
Borislav Petkov72381bd2009-10-09 19:14:43 +02001655
Joe Perches956b9ba2012-04-29 17:08:39 -03001656 edac_dbg(0, " data interleave for ECC: %s, DRAM cleared since last warm reset: %s\n",
1657 (dct_data_intlv_enabled(pvt) ? "enabled" : "disabled"),
1658 (dct_memory_cleared(pvt) ? "yes" : "no"));
Borislav Petkov72381bd2009-10-09 19:14:43 +02001659
Joe Perches956b9ba2012-04-29 17:08:39 -03001660 edac_dbg(0, " channel interleave: %s, "
1661 "interleave bits selector: 0x%x\n",
1662 (dct_interleave_enabled(pvt) ? "enabled" : "disabled"),
1663 dct_sel_interleave_addr(pvt));
Doug Thompson6163b5d2009-04-27 16:20:17 +02001664 }
1665
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05001666 amd64_read_pci_cfg(pvt->F2, DCT_SEL_HI, &pvt->dct_sel_hi);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001667}
1668
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001669/*
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001670 * Determine channel (DCT) based on the interleaving mode (see F15h M30h BKDG,
1671 * 2.10.12 Memory Interleaving Modes).
1672 */
1673static u8 f15_m30h_determine_channel(struct amd64_pvt *pvt, u64 sys_addr,
1674 u8 intlv_en, int num_dcts_intlv,
1675 u32 dct_sel)
1676{
1677 u8 channel = 0;
1678 u8 select;
1679
1680 if (!(intlv_en))
1681 return (u8)(dct_sel);
1682
1683 if (num_dcts_intlv == 2) {
1684 select = (sys_addr >> 8) & 0x3;
1685 channel = select ? 0x3 : 0;
Aravind Gopalakrishnan9d0e8d82014-01-21 15:03:36 -06001686 } else if (num_dcts_intlv == 4) {
1687 u8 intlv_addr = dct_sel_interleave_addr(pvt);
1688 switch (intlv_addr) {
1689 case 0x4:
1690 channel = (sys_addr >> 8) & 0x3;
1691 break;
1692 case 0x5:
1693 channel = (sys_addr >> 9) & 0x3;
1694 break;
1695 }
1696 }
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001697 return channel;
1698}
1699
1700/*
Borislav Petkov229a7a12010-12-09 18:57:54 +01001701 * Determine channel (DCT) based on the interleaving mode: F10h BKDG, 2.8.9 Memory
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001702 * Interleaving Modes.
1703 */
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01001704static u8 f1x_determine_channel(struct amd64_pvt *pvt, u64 sys_addr,
Borislav Petkov229a7a12010-12-09 18:57:54 +01001705 bool hi_range_sel, u8 intlv_en)
Doug Thompson6163b5d2009-04-27 16:20:17 +02001706{
Borislav Petkov151fa712011-02-21 19:33:10 +01001707 u8 dct_sel_high = (pvt->dct_sel_lo >> 1) & 1;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001708
1709 if (dct_ganging_enabled(pvt))
Borislav Petkov229a7a12010-12-09 18:57:54 +01001710 return 0;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001711
Borislav Petkov229a7a12010-12-09 18:57:54 +01001712 if (hi_range_sel)
1713 return dct_sel_high;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001714
Borislav Petkov229a7a12010-12-09 18:57:54 +01001715 /*
1716 * see F2x110[DctSelIntLvAddr] - channel interleave mode
1717 */
1718 if (dct_interleave_enabled(pvt)) {
1719 u8 intlv_addr = dct_sel_interleave_addr(pvt);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001720
Borislav Petkov229a7a12010-12-09 18:57:54 +01001721 /* return DCT select function: 0=DCT0, 1=DCT1 */
1722 if (!intlv_addr)
1723 return sys_addr >> 6 & 1;
1724
1725 if (intlv_addr & 0x2) {
1726 u8 shift = intlv_addr & 0x1 ? 9 : 6;
Yazen Ghannamdc0a50a82016-08-03 10:59:15 -04001727 u32 temp = hweight_long((u32) ((sys_addr >> 16) & 0x1F)) & 1;
Borislav Petkov229a7a12010-12-09 18:57:54 +01001728
1729 return ((sys_addr >> shift) & 1) ^ temp;
1730 }
1731
Yazen Ghannamdc0a50a82016-08-03 10:59:15 -04001732 if (intlv_addr & 0x4) {
1733 u8 shift = intlv_addr & 0x1 ? 9 : 8;
1734
1735 return (sys_addr >> shift) & 1;
1736 }
1737
Borislav Petkov229a7a12010-12-09 18:57:54 +01001738 return (sys_addr >> (12 + hweight8(intlv_en))) & 1;
1739 }
1740
1741 if (dct_high_range_enabled(pvt))
1742 return ~dct_sel_high & 1;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001743
1744 return 0;
1745}
1746
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001747/* Convert the sys_addr to the normalized DCT address */
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08001748static u64 f1x_get_norm_dct_addr(struct amd64_pvt *pvt, u8 range,
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001749 u64 sys_addr, bool hi_rng,
1750 u32 dct_sel_base_addr)
Doug Thompson6163b5d2009-04-27 16:20:17 +02001751{
1752 u64 chan_off;
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001753 u64 dram_base = get_dram_base(pvt, range);
1754 u64 hole_off = f10_dhar_offset(pvt);
Dan Carpenter6f3508f2016-01-20 12:54:51 +03001755 u64 dct_sel_base_off = (u64)(pvt->dct_sel_hi & 0xFFFFFC00) << 16;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001756
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001757 if (hi_rng) {
1758 /*
1759 * if
1760 * base address of high range is below 4Gb
1761 * (bits [47:27] at [31:11])
1762 * DRAM address space on this DCT is hoisted above 4Gb &&
1763 * sys_addr > 4Gb
1764 *
1765 * remove hole offset from sys_addr
1766 * else
1767 * remove high range offset from sys_addr
1768 */
1769 if ((!(dct_sel_base_addr >> 16) ||
1770 dct_sel_base_addr < dhar_base(pvt)) &&
Borislav Petkov972ea172011-02-21 19:43:02 +01001771 dhar_valid(pvt) &&
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001772 (sys_addr >= BIT_64(32)))
Borislav Petkovbc21fa52010-11-11 17:29:13 +01001773 chan_off = hole_off;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001774 else
1775 chan_off = dct_sel_base_off;
1776 } else {
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001777 /*
1778 * if
1779 * we have a valid hole &&
1780 * sys_addr > 4Gb
1781 *
1782 * remove hole
1783 * else
1784 * remove dram base to normalize to DCT address
1785 */
Borislav Petkov972ea172011-02-21 19:43:02 +01001786 if (dhar_valid(pvt) && (sys_addr >= BIT_64(32)))
Borislav Petkovbc21fa52010-11-11 17:29:13 +01001787 chan_off = hole_off;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001788 else
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001789 chan_off = dram_base;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001790 }
1791
Chen, Gong10ef6b02013-10-18 14:29:07 -07001792 return (sys_addr & GENMASK_ULL(47,6)) - (chan_off & GENMASK_ULL(47,23));
Doug Thompson6163b5d2009-04-27 16:20:17 +02001793}
1794
Doug Thompson6163b5d2009-04-27 16:20:17 +02001795/*
1796 * checks if the csrow passed in is marked as SPARED, if so returns the new
1797 * spare row
1798 */
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001799static int f10_process_possible_spare(struct amd64_pvt *pvt, u8 dct, int csrow)
Doug Thompson6163b5d2009-04-27 16:20:17 +02001800{
Borislav Petkov614ec9d2011-01-13 18:02:22 +01001801 int tmp_cs;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001802
Borislav Petkov614ec9d2011-01-13 18:02:22 +01001803 if (online_spare_swap_done(pvt, dct) &&
1804 csrow == online_spare_bad_dramcs(pvt, dct)) {
1805
1806 for_each_chip_select(tmp_cs, dct, pvt) {
1807 if (chip_select_base(tmp_cs, dct, pvt) & 0x2) {
1808 csrow = tmp_cs;
1809 break;
1810 }
1811 }
Doug Thompson6163b5d2009-04-27 16:20:17 +02001812 }
1813 return csrow;
1814}
1815
1816/*
1817 * Iterate over the DRAM DCT "base" and "mask" registers looking for a
1818 * SystemAddr match on the specified 'ChannelSelect' and 'NodeID'
1819 *
1820 * Return:
1821 * -EINVAL: NOT FOUND
1822 * 0..csrow = Chip-Select Row
1823 */
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08001824static int f1x_lookup_addr_in_dct(u64 in_addr, u8 nid, u8 dct)
Doug Thompson6163b5d2009-04-27 16:20:17 +02001825{
1826 struct mem_ctl_info *mci;
1827 struct amd64_pvt *pvt;
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001828 u64 cs_base, cs_mask;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001829 int cs_found = -EINVAL;
1830 int csrow;
1831
Borislav Petkov2ec591a2015-02-17 10:58:34 +01001832 mci = edac_mc_find(nid);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001833 if (!mci)
1834 return cs_found;
1835
1836 pvt = mci->pvt_info;
1837
Joe Perches956b9ba2012-04-29 17:08:39 -03001838 edac_dbg(1, "input addr: 0x%llx, DCT: %d\n", in_addr, dct);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001839
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001840 for_each_chip_select(csrow, dct, pvt) {
1841 if (!csrow_enabled(csrow, dct, pvt))
Doug Thompson6163b5d2009-04-27 16:20:17 +02001842 continue;
1843
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001844 get_cs_base_and_mask(pvt, csrow, dct, &cs_base, &cs_mask);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001845
Joe Perches956b9ba2012-04-29 17:08:39 -03001846 edac_dbg(1, " CSROW=%d CSBase=0x%llx CSMask=0x%llx\n",
1847 csrow, cs_base, cs_mask);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001848
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001849 cs_mask = ~cs_mask;
Doug Thompson6163b5d2009-04-27 16:20:17 +02001850
Joe Perches956b9ba2012-04-29 17:08:39 -03001851 edac_dbg(1, " (InputAddr & ~CSMask)=0x%llx (CSBase & ~CSMask)=0x%llx\n",
1852 (in_addr & cs_mask), (cs_base & cs_mask));
Doug Thompson6163b5d2009-04-27 16:20:17 +02001853
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001854 if ((in_addr & cs_mask) == (cs_base & cs_mask)) {
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001855 if (pvt->fam == 0x15 && pvt->model >= 0x30) {
1856 cs_found = csrow;
1857 break;
1858 }
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001859 cs_found = f10_process_possible_spare(pvt, dct, csrow);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001860
Joe Perches956b9ba2012-04-29 17:08:39 -03001861 edac_dbg(1, " MATCH csrow=%d\n", cs_found);
Doug Thompson6163b5d2009-04-27 16:20:17 +02001862 break;
1863 }
1864 }
1865 return cs_found;
1866}
1867
Borislav Petkov95b0ef52011-01-11 22:08:07 +01001868/*
1869 * See F2x10C. Non-interleaved graphics framebuffer memory under the 16G is
1870 * swapped with a region located at the bottom of memory so that the GPU can use
1871 * the interleaved region and thus two channels.
1872 */
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01001873static u64 f1x_swap_interleaved_region(struct amd64_pvt *pvt, u64 sys_addr)
Borislav Petkov95b0ef52011-01-11 22:08:07 +01001874{
1875 u32 swap_reg, swap_base, swap_limit, rgn_size, tmp_addr;
1876
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001877 if (pvt->fam == 0x10) {
Borislav Petkov95b0ef52011-01-11 22:08:07 +01001878 /* only revC3 and revE have that feature */
Borislav Petkova4b4bed2013-08-10 13:54:48 +02001879 if (pvt->model < 4 || (pvt->model < 0xa && pvt->stepping < 3))
Borislav Petkov95b0ef52011-01-11 22:08:07 +01001880 return sys_addr;
1881 }
1882
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05001883 amd64_read_pci_cfg(pvt->F2, SWAP_INTLV_REG, &swap_reg);
Borislav Petkov95b0ef52011-01-11 22:08:07 +01001884
1885 if (!(swap_reg & 0x1))
1886 return sys_addr;
1887
1888 swap_base = (swap_reg >> 3) & 0x7f;
1889 swap_limit = (swap_reg >> 11) & 0x7f;
1890 rgn_size = (swap_reg >> 20) & 0x7f;
1891 tmp_addr = sys_addr >> 27;
1892
1893 if (!(sys_addr >> 34) &&
1894 (((tmp_addr >= swap_base) &&
1895 (tmp_addr <= swap_limit)) ||
1896 (tmp_addr < rgn_size)))
1897 return sys_addr ^ (u64)swap_base << 27;
1898
1899 return sys_addr;
1900}
1901
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001902/* For a given @dram_range, check if @sys_addr falls within it. */
Borislav Petkove761359a2011-02-21 19:49:01 +01001903static int f1x_match_to_this_node(struct amd64_pvt *pvt, unsigned range,
Borislav Petkov33ca0642012-08-30 18:01:36 +02001904 u64 sys_addr, int *chan_sel)
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001905{
Borislav Petkov229a7a12010-12-09 18:57:54 +01001906 int cs_found = -EINVAL;
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001907 u64 chan_addr;
Borislav Petkov5d4b58e2011-01-13 16:01:13 +01001908 u32 dct_sel_base;
Borislav Petkov11c75ea2010-11-29 19:49:02 +01001909 u8 channel;
Borislav Petkov229a7a12010-12-09 18:57:54 +01001910 bool high_range = false;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001911
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001912 u8 node_id = dram_dst_node(pvt, range);
Borislav Petkov229a7a12010-12-09 18:57:54 +01001913 u8 intlv_en = dram_intlv_en(pvt, range);
Borislav Petkov7f19bf72010-10-21 18:52:53 +02001914 u32 intlv_sel = dram_intlv_sel(pvt, range);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001915
Joe Perches956b9ba2012-04-29 17:08:39 -03001916 edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n",
1917 range, sys_addr, get_dram_limit(pvt, range));
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001918
Borislav Petkov355fba62011-01-17 13:03:26 +01001919 if (dhar_valid(pvt) &&
1920 dhar_base(pvt) <= sys_addr &&
1921 sys_addr < BIT_64(32)) {
1922 amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n",
1923 sys_addr);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001924 return -EINVAL;
Borislav Petkov355fba62011-01-17 13:03:26 +01001925 }
1926
Borislav Petkovf030ddf2011-04-08 15:05:21 +02001927 if (intlv_en && (intlv_sel != ((sys_addr >> 12) & intlv_en)))
Borislav Petkov355fba62011-01-17 13:03:26 +01001928 return -EINVAL;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001929
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01001930 sys_addr = f1x_swap_interleaved_region(pvt, sys_addr);
Borislav Petkov95b0ef52011-01-11 22:08:07 +01001931
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001932 dct_sel_base = dct_sel_baseaddr(pvt);
1933
1934 /*
1935 * check whether addresses >= DctSelBaseAddr[47:27] are to be used to
1936 * select between DCT0 and DCT1.
1937 */
1938 if (dct_high_range_enabled(pvt) &&
1939 !dct_ganging_enabled(pvt) &&
1940 ((sys_addr >> 27) >= (dct_sel_base >> 11)))
Borislav Petkov229a7a12010-12-09 18:57:54 +01001941 high_range = true;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001942
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01001943 channel = f1x_determine_channel(pvt, sys_addr, high_range, intlv_en);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001944
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01001945 chan_addr = f1x_get_norm_dct_addr(pvt, range, sys_addr,
Borislav Petkovc8e518d2010-12-10 19:49:19 +01001946 high_range, dct_sel_base);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001947
Borislav Petkove2f79db2011-01-13 14:57:34 +01001948 /* Remove node interleaving, see F1x120 */
1949 if (intlv_en)
1950 chan_addr = ((chan_addr >> (12 + hweight8(intlv_en))) << 12) |
1951 (chan_addr & 0xfff);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001952
Borislav Petkov5d4b58e2011-01-13 16:01:13 +01001953 /* remove channel interleave */
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001954 if (dct_interleave_enabled(pvt) &&
1955 !dct_high_range_enabled(pvt) &&
1956 !dct_ganging_enabled(pvt)) {
Borislav Petkov5d4b58e2011-01-13 16:01:13 +01001957
1958 if (dct_sel_interleave_addr(pvt) != 1) {
1959 if (dct_sel_interleave_addr(pvt) == 0x3)
1960 /* hash 9 */
1961 chan_addr = ((chan_addr >> 10) << 9) |
1962 (chan_addr & 0x1ff);
1963 else
1964 /* A[6] or hash 6 */
1965 chan_addr = ((chan_addr >> 7) << 6) |
1966 (chan_addr & 0x3f);
1967 } else
1968 /* A[12] */
1969 chan_addr = ((chan_addr >> 13) << 12) |
1970 (chan_addr & 0xfff);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001971 }
1972
Joe Perches956b9ba2012-04-29 17:08:39 -03001973 edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001974
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01001975 cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, channel);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001976
Borislav Petkov33ca0642012-08-30 18:01:36 +02001977 if (cs_found >= 0)
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001978 *chan_sel = channel;
Borislav Petkov33ca0642012-08-30 18:01:36 +02001979
Doug Thompsonf71d0a02009-04-27 16:22:43 +02001980 return cs_found;
1981}
1982
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05001983static int f15_m30h_match_to_this_node(struct amd64_pvt *pvt, unsigned range,
1984 u64 sys_addr, int *chan_sel)
1985{
1986 int cs_found = -EINVAL;
1987 int num_dcts_intlv = 0;
1988 u64 chan_addr, chan_offset;
1989 u64 dct_base, dct_limit;
1990 u32 dct_cont_base_reg, dct_cont_limit_reg, tmp;
1991 u8 channel, alias_channel, leg_mmio_hole, dct_sel, dct_offset_en;
1992
1993 u64 dhar_offset = f10_dhar_offset(pvt);
1994 u8 intlv_addr = dct_sel_interleave_addr(pvt);
1995 u8 node_id = dram_dst_node(pvt, range);
1996 u8 intlv_en = dram_intlv_en(pvt, range);
1997
1998 amd64_read_pci_cfg(pvt->F1, DRAM_CONT_BASE, &dct_cont_base_reg);
1999 amd64_read_pci_cfg(pvt->F1, DRAM_CONT_LIMIT, &dct_cont_limit_reg);
2000
2001 dct_offset_en = (u8) ((dct_cont_base_reg >> 3) & BIT(0));
2002 dct_sel = (u8) ((dct_cont_base_reg >> 4) & 0x7);
2003
2004 edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n",
2005 range, sys_addr, get_dram_limit(pvt, range));
2006
2007 if (!(get_dram_base(pvt, range) <= sys_addr) &&
2008 !(get_dram_limit(pvt, range) >= sys_addr))
2009 return -EINVAL;
2010
2011 if (dhar_valid(pvt) &&
2012 dhar_base(pvt) <= sys_addr &&
2013 sys_addr < BIT_64(32)) {
2014 amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n",
2015 sys_addr);
2016 return -EINVAL;
2017 }
2018
2019 /* Verify sys_addr is within DCT Range. */
Aravind Gopalakrishnan4fc06b32013-08-24 10:47:48 -05002020 dct_base = (u64) dct_sel_baseaddr(pvt);
2021 dct_limit = (dct_cont_limit_reg >> 11) & 0x1FFF;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002022
2023 if (!(dct_cont_base_reg & BIT(0)) &&
Aravind Gopalakrishnan4fc06b32013-08-24 10:47:48 -05002024 !(dct_base <= (sys_addr >> 27) &&
2025 dct_limit >= (sys_addr >> 27)))
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002026 return -EINVAL;
2027
2028 /* Verify number of dct's that participate in channel interleaving. */
2029 num_dcts_intlv = (int) hweight8(intlv_en);
2030
2031 if (!(num_dcts_intlv % 2 == 0) || (num_dcts_intlv > 4))
2032 return -EINVAL;
2033
Yazen Ghannamdc0a50a82016-08-03 10:59:15 -04002034 if (pvt->model >= 0x60)
2035 channel = f1x_determine_channel(pvt, sys_addr, false, intlv_en);
2036 else
2037 channel = f15_m30h_determine_channel(pvt, sys_addr, intlv_en,
2038 num_dcts_intlv, dct_sel);
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002039
2040 /* Verify we stay within the MAX number of channels allowed */
Aravind Gopalakrishnan7f3f5242013-12-04 11:40:11 -06002041 if (channel > 3)
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002042 return -EINVAL;
2043
2044 leg_mmio_hole = (u8) (dct_cont_base_reg >> 1 & BIT(0));
2045
2046 /* Get normalized DCT addr */
2047 if (leg_mmio_hole && (sys_addr >= BIT_64(32)))
2048 chan_offset = dhar_offset;
2049 else
Aravind Gopalakrishnan4fc06b32013-08-24 10:47:48 -05002050 chan_offset = dct_base << 27;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002051
2052 chan_addr = sys_addr - chan_offset;
2053
2054 /* remove channel interleave */
2055 if (num_dcts_intlv == 2) {
2056 if (intlv_addr == 0x4)
2057 chan_addr = ((chan_addr >> 9) << 8) |
2058 (chan_addr & 0xff);
2059 else if (intlv_addr == 0x5)
2060 chan_addr = ((chan_addr >> 10) << 9) |
2061 (chan_addr & 0x1ff);
2062 else
2063 return -EINVAL;
2064
2065 } else if (num_dcts_intlv == 4) {
2066 if (intlv_addr == 0x4)
2067 chan_addr = ((chan_addr >> 10) << 8) |
2068 (chan_addr & 0xff);
2069 else if (intlv_addr == 0x5)
2070 chan_addr = ((chan_addr >> 11) << 9) |
2071 (chan_addr & 0x1ff);
2072 else
2073 return -EINVAL;
2074 }
2075
2076 if (dct_offset_en) {
2077 amd64_read_pci_cfg(pvt->F1,
2078 DRAM_CONT_HIGH_OFF + (int) channel * 4,
2079 &tmp);
Aravind Gopalakrishnan4fc06b32013-08-24 10:47:48 -05002080 chan_addr += (u64) ((tmp >> 11) & 0xfff) << 27;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002081 }
2082
2083 f15h_select_dct(pvt, channel);
2084
2085 edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr);
2086
2087 /*
2088 * Find Chip select:
2089 * if channel = 3, then alias it to 1. This is because, in F15 M30h,
2090 * there is support for 4 DCT's, but only 2 are currently functional.
2091 * They are DCT0 and DCT3. But we have read all registers of DCT3 into
2092 * pvt->csels[1]. So we need to use '1' here to get correct info.
2093 * Refer F15 M30h BKDG Section 2.10 and 2.10.3 for clarifications.
2094 */
2095 alias_channel = (channel == 3) ? 1 : channel;
2096
2097 cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, alias_channel);
2098
2099 if (cs_found >= 0)
2100 *chan_sel = alias_channel;
2101
2102 return cs_found;
2103}
2104
2105static int f1x_translate_sysaddr_to_cs(struct amd64_pvt *pvt,
2106 u64 sys_addr,
2107 int *chan_sel)
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002108{
Borislav Petkove761359a2011-02-21 19:49:01 +01002109 int cs_found = -EINVAL;
2110 unsigned range;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002111
Borislav Petkov7f19bf72010-10-21 18:52:53 +02002112 for (range = 0; range < DRAM_RANGES; range++) {
Borislav Petkov7f19bf72010-10-21 18:52:53 +02002113 if (!dram_rw(pvt, range))
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002114 continue;
2115
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002116 if (pvt->fam == 0x15 && pvt->model >= 0x30)
2117 cs_found = f15_m30h_match_to_this_node(pvt, range,
2118 sys_addr,
2119 chan_sel);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002120
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002121 else if ((get_dram_base(pvt, range) <= sys_addr) &&
2122 (get_dram_limit(pvt, range) >= sys_addr)) {
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01002123 cs_found = f1x_match_to_this_node(pvt, range,
Borislav Petkov33ca0642012-08-30 18:01:36 +02002124 sys_addr, chan_sel);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002125 if (cs_found >= 0)
2126 break;
2127 }
2128 }
2129 return cs_found;
2130}
2131
2132/*
Borislav Petkovbdc30a02009-11-13 15:10:43 +01002133 * For reference see "2.8.5 Routing DRAM Requests" in F10 BKDG. This code maps
2134 * a @sys_addr to NodeID, DCT (channel) and chip select (CSROW).
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002135 *
Borislav Petkovbdc30a02009-11-13 15:10:43 +01002136 * The @sys_addr is usually an error address received from the hardware
2137 * (MCX_ADDR).
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002138 */
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01002139static void f1x_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr,
Borislav Petkov33ca0642012-08-30 18:01:36 +02002140 struct err_info *err)
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002141{
2142 struct amd64_pvt *pvt = mci->pvt_info;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002143
Borislav Petkov33ca0642012-08-30 18:01:36 +02002144 error_address_to_page_and_offset(sys_addr, err);
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03002145
Borislav Petkov33ca0642012-08-30 18:01:36 +02002146 err->csrow = f1x_translate_sysaddr_to_cs(pvt, sys_addr, &err->channel);
2147 if (err->csrow < 0) {
2148 err->err_code = ERR_CSROW;
Borislav Petkovbdc30a02009-11-13 15:10:43 +01002149 return;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002150 }
Borislav Petkovbdc30a02009-11-13 15:10:43 +01002151
Borislav Petkovbdc30a02009-11-13 15:10:43 +01002152 /*
2153 * We need the syndromes for channel detection only when we're
2154 * ganged. Otherwise @chan should already contain the channel at
2155 * this point.
2156 */
Borislav Petkova97fa682010-12-23 14:07:18 +01002157 if (dct_ganging_enabled(pvt))
Borislav Petkov33ca0642012-08-30 18:01:36 +02002158 err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002159}
2160
2161/*
Borislav Petkov8566c4d2009-10-16 13:48:28 +02002162 * debug routine to display the memory sizes of all logical DIMMs and its
Borislav Petkovcb328502010-12-22 14:28:24 +01002163 * CSROWs
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002164 */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01002165static void debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl)
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002166{
Borislav Petkovbb89f5a2012-09-12 18:06:00 +02002167 int dimm, size0, size1;
Borislav Petkov525a1b22010-12-21 15:53:27 +01002168 u32 *dcsb = ctrl ? pvt->csels[1].csbases : pvt->csels[0].csbases;
2169 u32 dbam = ctrl ? pvt->dbam1 : pvt->dbam0;
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002170
Borislav Petkova4b4bed2013-08-10 13:54:48 +02002171 if (pvt->fam == 0xf) {
Borislav Petkov8566c4d2009-10-16 13:48:28 +02002172 /* K8 families < revF not supported yet */
Borislav Petkov1433eb92009-10-21 13:44:36 +02002173 if (pvt->ext_model < K8_REV_F)
Borislav Petkov8566c4d2009-10-16 13:48:28 +02002174 return;
2175 else
2176 WARN_ON(ctrl != 0);
2177 }
2178
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05002179 if (pvt->fam == 0x10) {
2180 dbam = (ctrl && !dct_ganging_enabled(pvt)) ? pvt->dbam1
2181 : pvt->dbam0;
2182 dcsb = (ctrl && !dct_ganging_enabled(pvt)) ?
2183 pvt->csels[1].csbases :
2184 pvt->csels[0].csbases;
2185 } else if (ctrl) {
2186 dbam = pvt->dbam0;
2187 dcsb = pvt->csels[1].csbases;
2188 }
Joe Perches956b9ba2012-04-29 17:08:39 -03002189 edac_dbg(1, "F2x%d80 (DRAM Bank Address Mapping): 0x%08x\n",
2190 ctrl, dbam);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002191
Borislav Petkov8566c4d2009-10-16 13:48:28 +02002192 edac_printk(KERN_DEBUG, EDAC_MC, "DCT%d chip selects:\n", ctrl);
2193
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002194 /* Dump memory sizes for DIMM and its CSROWs */
2195 for (dimm = 0; dimm < 4; dimm++) {
2196
2197 size0 = 0;
Borislav Petkov11c75ea2010-11-29 19:49:02 +01002198 if (dcsb[dimm*2] & DCSB_CS_ENABLE)
Yazen Ghannam07ed82e2016-11-28 08:50:21 -06002199 /*
2200 * For F15m60h, we need multiplier for LRDIMM cs_size
2201 * calculation. We pass dimm value to the dbam_to_cs
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01002202 * mapper so we can find the multiplier from the
2203 * corresponding DCSM.
2204 */
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01002205 size0 = pvt->ops->dbam_to_cs(pvt, ctrl,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01002206 DBAM_DIMM(dimm, dbam),
2207 dimm);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002208
2209 size1 = 0;
Borislav Petkov11c75ea2010-11-29 19:49:02 +01002210 if (dcsb[dimm*2 + 1] & DCSB_CS_ENABLE)
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01002211 size1 = pvt->ops->dbam_to_cs(pvt, ctrl,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01002212 DBAM_DIMM(dimm, dbam),
2213 dimm);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002214
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02002215 amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n",
Borislav Petkovbb89f5a2012-09-12 18:06:00 +02002216 dimm * 2, size0,
2217 dimm * 2 + 1, size1);
Doug Thompsonf71d0a02009-04-27 16:22:43 +02002218 }
2219}
2220
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01002221static struct amd64_family_type family_types[] = {
Doug Thompson4d376072009-04-27 16:25:05 +02002222 [K8_CPUS] = {
Borislav Petkov0092b202010-10-01 19:20:05 +02002223 .ctl_name = "K8",
Borislav Petkov8d5b5d92010-10-01 20:11:07 +02002224 .f1_id = PCI_DEVICE_ID_AMD_K8_NB_ADDRMAP,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002225 .f2_id = PCI_DEVICE_ID_AMD_K8_NB_MEMCTL,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002226 .max_mcs = 2,
Doug Thompson4d376072009-04-27 16:25:05 +02002227 .ops = {
Borislav Petkov1433eb92009-10-21 13:44:36 +02002228 .early_channel_count = k8_early_channel_count,
Borislav Petkov1433eb92009-10-21 13:44:36 +02002229 .map_sysaddr_to_csrow = k8_map_sysaddr_to_csrow,
2230 .dbam_to_cs = k8_dbam_to_chip_select,
Doug Thompson4d376072009-04-27 16:25:05 +02002231 }
2232 },
2233 [F10_CPUS] = {
Borislav Petkov0092b202010-10-01 19:20:05 +02002234 .ctl_name = "F10h",
Borislav Petkov8d5b5d92010-10-01 20:11:07 +02002235 .f1_id = PCI_DEVICE_ID_AMD_10H_NB_MAP,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002236 .f2_id = PCI_DEVICE_ID_AMD_10H_NB_DRAM,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002237 .max_mcs = 2,
Doug Thompson4d376072009-04-27 16:25:05 +02002238 .ops = {
Borislav Petkov7d20d142011-01-07 17:58:04 +01002239 .early_channel_count = f1x_early_channel_count,
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01002240 .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
Borislav Petkov1433eb92009-10-21 13:44:36 +02002241 .dbam_to_cs = f10_dbam_to_chip_select,
Borislav Petkovb2b0c602010-10-08 18:32:29 +02002242 }
2243 },
2244 [F15_CPUS] = {
2245 .ctl_name = "F15h",
Borislav Petkovdf71a052011-01-19 18:15:10 +01002246 .f1_id = PCI_DEVICE_ID_AMD_15H_NB_F1,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002247 .f2_id = PCI_DEVICE_ID_AMD_15H_NB_F2,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002248 .max_mcs = 2,
Borislav Petkovb2b0c602010-10-08 18:32:29 +02002249 .ops = {
Borislav Petkov7d20d142011-01-07 17:58:04 +01002250 .early_channel_count = f1x_early_channel_count,
Borislav Petkovb15f0fc2011-01-17 15:59:58 +01002251 .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
Borislav Petkov41d8bfa2011-01-18 19:16:08 +01002252 .dbam_to_cs = f15_dbam_to_chip_select,
Doug Thompson4d376072009-04-27 16:25:05 +02002253 }
2254 },
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002255 [F15_M30H_CPUS] = {
2256 .ctl_name = "F15h_M30h",
2257 .f1_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002258 .f2_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F2,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002259 .max_mcs = 2,
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002260 .ops = {
2261 .early_channel_count = f1x_early_channel_count,
2262 .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
2263 .dbam_to_cs = f16_dbam_to_chip_select,
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05002264 }
2265 },
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01002266 [F15_M60H_CPUS] = {
2267 .ctl_name = "F15h_M60h",
2268 .f1_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002269 .f2_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F2,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002270 .max_mcs = 2,
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01002271 .ops = {
2272 .early_channel_count = f1x_early_channel_count,
2273 .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
2274 .dbam_to_cs = f15_m60h_dbam_to_chip_select,
2275 }
2276 },
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05002277 [F16_CPUS] = {
2278 .ctl_name = "F16h",
2279 .f1_id = PCI_DEVICE_ID_AMD_16H_NB_F1,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002280 .f2_id = PCI_DEVICE_ID_AMD_16H_NB_F2,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002281 .max_mcs = 2,
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05002282 .ops = {
2283 .early_channel_count = f1x_early_channel_count,
2284 .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
2285 .dbam_to_cs = f16_dbam_to_chip_select,
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05002286 }
2287 },
Aravind Gopalakrishnan85a88852014-02-20 10:28:46 -06002288 [F16_M30H_CPUS] = {
2289 .ctl_name = "F16h_M30h",
2290 .f1_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F1,
Borislav Petkov3f37a362016-05-06 19:44:27 +02002291 .f2_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F2,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002292 .max_mcs = 2,
Aravind Gopalakrishnan85a88852014-02-20 10:28:46 -06002293 .ops = {
2294 .early_channel_count = f1x_early_channel_count,
2295 .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
2296 .dbam_to_cs = f16_dbam_to_chip_select,
Aravind Gopalakrishnan85a88852014-02-20 10:28:46 -06002297 }
2298 },
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05002299 [F17_CPUS] = {
2300 .ctl_name = "F17h",
2301 .f0_id = PCI_DEVICE_ID_AMD_17H_DF_F0,
2302 .f6_id = PCI_DEVICE_ID_AMD_17H_DF_F6,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002303 .max_mcs = 2,
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05002304 .ops = {
2305 .early_channel_count = f17_early_channel_count,
Yazen Ghanname53a3b22019-08-21 23:59:59 +00002306 .dbam_to_cs = f17_addr_mask_to_cs_size,
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05002307 }
2308 },
Michael Jin8960de42018-08-16 15:28:40 -04002309 [F17_M10H_CPUS] = {
2310 .ctl_name = "F17h_M10h",
2311 .f0_id = PCI_DEVICE_ID_AMD_17H_M10H_DF_F0,
2312 .f6_id = PCI_DEVICE_ID_AMD_17H_M10H_DF_F6,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002313 .max_mcs = 2,
Michael Jin8960de42018-08-16 15:28:40 -04002314 .ops = {
2315 .early_channel_count = f17_early_channel_count,
Yazen Ghanname53a3b22019-08-21 23:59:59 +00002316 .dbam_to_cs = f17_addr_mask_to_cs_size,
Michael Jin8960de42018-08-16 15:28:40 -04002317 }
2318 },
Yazen Ghannam6e8462392019-02-28 15:36:09 +00002319 [F17_M30H_CPUS] = {
2320 .ctl_name = "F17h_M30h",
2321 .f0_id = PCI_DEVICE_ID_AMD_17H_M30H_DF_F0,
2322 .f6_id = PCI_DEVICE_ID_AMD_17H_M30H_DF_F6,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002323 .max_mcs = 8,
Yazen Ghannam6e8462392019-02-28 15:36:09 +00002324 .ops = {
2325 .early_channel_count = f17_early_channel_count,
Yazen Ghanname53a3b22019-08-21 23:59:59 +00002326 .dbam_to_cs = f17_addr_mask_to_cs_size,
Yazen Ghannam6e8462392019-02-28 15:36:09 +00002327 }
2328 },
Isaac Vaughn3e443eb2019-09-06 23:21:38 +00002329 [F17_M70H_CPUS] = {
2330 .ctl_name = "F17h_M70h",
2331 .f0_id = PCI_DEVICE_ID_AMD_17H_M70H_DF_F0,
2332 .f6_id = PCI_DEVICE_ID_AMD_17H_M70H_DF_F6,
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00002333 .max_mcs = 2,
Isaac Vaughn3e443eb2019-09-06 23:21:38 +00002334 .ops = {
2335 .early_channel_count = f17_early_channel_count,
2336 .dbam_to_cs = f17_addr_mask_to_cs_size,
2337 }
2338 },
Doug Thompson4d376072009-04-27 16:25:05 +02002339};
2340
Doug Thompsonb1289d62009-04-27 16:37:05 +02002341/*
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002342 * These are tables of eigenvectors (one per line) which can be used for the
2343 * construction of the syndrome tables. The modified syndrome search algorithm
2344 * uses those to find the symbol in error and thus the DIMM.
Doug Thompsonb1289d62009-04-27 16:37:05 +02002345 *
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002346 * Algorithm courtesy of Ross LaFetra from AMD.
Doug Thompsonb1289d62009-04-27 16:37:05 +02002347 */
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08002348static const u16 x4_vectors[] = {
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002349 0x2f57, 0x1afe, 0x66cc, 0xdd88,
2350 0x11eb, 0x3396, 0x7f4c, 0xeac8,
2351 0x0001, 0x0002, 0x0004, 0x0008,
2352 0x1013, 0x3032, 0x4044, 0x8088,
2353 0x106b, 0x30d6, 0x70fc, 0xe0a8,
2354 0x4857, 0xc4fe, 0x13cc, 0x3288,
2355 0x1ac5, 0x2f4a, 0x5394, 0xa1e8,
2356 0x1f39, 0x251e, 0xbd6c, 0x6bd8,
2357 0x15c1, 0x2a42, 0x89ac, 0x4758,
2358 0x2b03, 0x1602, 0x4f0c, 0xca08,
2359 0x1f07, 0x3a0e, 0x6b04, 0xbd08,
2360 0x8ba7, 0x465e, 0x244c, 0x1cc8,
2361 0x2b87, 0x164e, 0x642c, 0xdc18,
2362 0x40b9, 0x80de, 0x1094, 0x20e8,
2363 0x27db, 0x1eb6, 0x9dac, 0x7b58,
2364 0x11c1, 0x2242, 0x84ac, 0x4c58,
2365 0x1be5, 0x2d7a, 0x5e34, 0xa718,
2366 0x4b39, 0x8d1e, 0x14b4, 0x28d8,
2367 0x4c97, 0xc87e, 0x11fc, 0x33a8,
2368 0x8e97, 0x497e, 0x2ffc, 0x1aa8,
2369 0x16b3, 0x3d62, 0x4f34, 0x8518,
2370 0x1e2f, 0x391a, 0x5cac, 0xf858,
2371 0x1d9f, 0x3b7a, 0x572c, 0xfe18,
2372 0x15f5, 0x2a5a, 0x5264, 0xa3b8,
2373 0x1dbb, 0x3b66, 0x715c, 0xe3f8,
2374 0x4397, 0xc27e, 0x17fc, 0x3ea8,
2375 0x1617, 0x3d3e, 0x6464, 0xb8b8,
2376 0x23ff, 0x12aa, 0xab6c, 0x56d8,
2377 0x2dfb, 0x1ba6, 0x913c, 0x7328,
2378 0x185d, 0x2ca6, 0x7914, 0x9e28,
2379 0x171b, 0x3e36, 0x7d7c, 0xebe8,
2380 0x4199, 0x82ee, 0x19f4, 0x2e58,
2381 0x4807, 0xc40e, 0x130c, 0x3208,
2382 0x1905, 0x2e0a, 0x5804, 0xac08,
2383 0x213f, 0x132a, 0xadfc, 0x5ba8,
2384 0x19a9, 0x2efe, 0xb5cc, 0x6f88,
Doug Thompsonb1289d62009-04-27 16:37:05 +02002385};
2386
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08002387static const u16 x8_vectors[] = {
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002388 0x0145, 0x028a, 0x2374, 0x43c8, 0xa1f0, 0x0520, 0x0a40, 0x1480,
2389 0x0211, 0x0422, 0x0844, 0x1088, 0x01b0, 0x44e0, 0x23c0, 0xed80,
2390 0x1011, 0x0116, 0x022c, 0x0458, 0x08b0, 0x8c60, 0x2740, 0x4e80,
2391 0x0411, 0x0822, 0x1044, 0x0158, 0x02b0, 0x2360, 0x46c0, 0xab80,
2392 0x0811, 0x1022, 0x012c, 0x0258, 0x04b0, 0x4660, 0x8cc0, 0x2780,
2393 0x2071, 0x40e2, 0xa0c4, 0x0108, 0x0210, 0x0420, 0x0840, 0x1080,
2394 0x4071, 0x80e2, 0x0104, 0x0208, 0x0410, 0x0820, 0x1040, 0x2080,
2395 0x8071, 0x0102, 0x0204, 0x0408, 0x0810, 0x1020, 0x2040, 0x4080,
2396 0x019d, 0x03d6, 0x136c, 0x2198, 0x50b0, 0xb2e0, 0x0740, 0x0e80,
2397 0x0189, 0x03ea, 0x072c, 0x0e58, 0x1cb0, 0x56e0, 0x37c0, 0xf580,
2398 0x01fd, 0x0376, 0x06ec, 0x0bb8, 0x1110, 0x2220, 0x4440, 0x8880,
2399 0x0163, 0x02c6, 0x1104, 0x0758, 0x0eb0, 0x2be0, 0x6140, 0xc280,
2400 0x02fd, 0x01c6, 0x0b5c, 0x1108, 0x07b0, 0x25a0, 0x8840, 0x6180,
2401 0x0801, 0x012e, 0x025c, 0x04b8, 0x1370, 0x26e0, 0x57c0, 0xb580,
2402 0x0401, 0x0802, 0x015c, 0x02b8, 0x22b0, 0x13e0, 0x7140, 0xe280,
2403 0x0201, 0x0402, 0x0804, 0x01b8, 0x11b0, 0x31a0, 0x8040, 0x7180,
2404 0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080,
2405 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2406 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000,
2407};
2408
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08002409static int decode_syndrome(u16 syndrome, const u16 *vectors, unsigned num_vecs,
Borislav Petkovd34a6ec2011-02-23 17:41:50 +01002410 unsigned v_dim)
Doug Thompsonb1289d62009-04-27 16:37:05 +02002411{
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002412 unsigned int i, err_sym;
Doug Thompsonb1289d62009-04-27 16:37:05 +02002413
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002414 for (err_sym = 0; err_sym < num_vecs / v_dim; err_sym++) {
2415 u16 s = syndrome;
Borislav Petkovd34a6ec2011-02-23 17:41:50 +01002416 unsigned v_idx = err_sym * v_dim;
2417 unsigned v_end = (err_sym + 1) * v_dim;
Doug Thompsonb1289d62009-04-27 16:37:05 +02002418
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002419 /* walk over all 16 bits of the syndrome */
2420 for (i = 1; i < (1U << 16); i <<= 1) {
2421
2422 /* if bit is set in that eigenvector... */
2423 if (v_idx < v_end && vectors[v_idx] & i) {
2424 u16 ev_comp = vectors[v_idx++];
2425
2426 /* ... and bit set in the modified syndrome, */
2427 if (s & i) {
2428 /* remove it. */
2429 s ^= ev_comp;
2430
2431 if (!s)
2432 return err_sym;
2433 }
2434
2435 } else if (s & i)
2436 /* can't get to zero, move to next symbol */
2437 break;
2438 }
Doug Thompsonb1289d62009-04-27 16:37:05 +02002439 }
2440
Joe Perches956b9ba2012-04-29 17:08:39 -03002441 edac_dbg(0, "syndrome(%x) not found\n", syndrome);
Doug Thompsonb1289d62009-04-27 16:37:05 +02002442 return -1;
2443}
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002444
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002445static int map_err_sym_to_channel(int err_sym, int sym_size)
2446{
2447 if (sym_size == 4)
2448 switch (err_sym) {
2449 case 0x20:
2450 case 0x21:
2451 return 0;
2452 break;
2453 case 0x22:
2454 case 0x23:
2455 return 1;
2456 break;
2457 default:
2458 return err_sym >> 4;
2459 break;
2460 }
2461 /* x8 symbols */
2462 else
2463 switch (err_sym) {
2464 /* imaginary bits not in a DIMM */
2465 case 0x10:
2466 WARN(1, KERN_ERR "Invalid error symbol: 0x%x\n",
2467 err_sym);
2468 return -1;
2469 break;
2470
2471 case 0x11:
2472 return 0;
2473 break;
2474 case 0x12:
2475 return 1;
2476 break;
2477 default:
2478 return err_sym >> 3;
2479 break;
2480 }
2481 return -1;
2482}
2483
2484static int get_channel_from_ecc_syndrome(struct mem_ctl_info *mci, u16 syndrome)
2485{
2486 struct amd64_pvt *pvt = mci->pvt_info;
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002487 int err_sym = -1;
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002488
Borislav Petkova3b7db02011-01-19 20:35:12 +01002489 if (pvt->ecc_sym_sz == 8)
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002490 err_sym = decode_syndrome(syndrome, x8_vectors,
2491 ARRAY_SIZE(x8_vectors),
Borislav Petkova3b7db02011-01-19 20:35:12 +01002492 pvt->ecc_sym_sz);
2493 else if (pvt->ecc_sym_sz == 4)
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002494 err_sym = decode_syndrome(syndrome, x4_vectors,
2495 ARRAY_SIZE(x4_vectors),
Borislav Petkova3b7db02011-01-19 20:35:12 +01002496 pvt->ecc_sym_sz);
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002497 else {
Borislav Petkova3b7db02011-01-19 20:35:12 +01002498 amd64_warn("Illegal syndrome type: %u\n", pvt->ecc_sym_sz);
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002499 return err_sym;
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002500 }
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002501
Borislav Petkova3b7db02011-01-19 20:35:12 +01002502 return map_err_sym_to_channel(err_sym, pvt->ecc_sym_sz);
Borislav Petkovbfc04ae2009-11-12 19:05:07 +01002503}
2504
Yazen Ghanname70984d2016-11-17 17:57:31 -05002505static void __log_ecc_error(struct mem_ctl_info *mci, struct err_info *err,
Borislav Petkov33ca0642012-08-30 18:01:36 +02002506 u8 ecc_type)
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002507{
Borislav Petkov33ca0642012-08-30 18:01:36 +02002508 enum hw_event_mc_err_type err_type;
2509 const char *string;
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002510
Borislav Petkov33ca0642012-08-30 18:01:36 +02002511 if (ecc_type == 2)
2512 err_type = HW_EVENT_ERR_CORRECTED;
2513 else if (ecc_type == 1)
2514 err_type = HW_EVENT_ERR_UNCORRECTED;
Yazen Ghannamd12a9692016-11-17 17:57:32 -05002515 else if (ecc_type == 3)
2516 err_type = HW_EVENT_ERR_DEFERRED;
Borislav Petkov33ca0642012-08-30 18:01:36 +02002517 else {
2518 WARN(1, "Something is rotten in the state of Denmark.\n");
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002519 return;
2520 }
2521
Borislav Petkov33ca0642012-08-30 18:01:36 +02002522 switch (err->err_code) {
2523 case DECODE_OK:
2524 string = "";
2525 break;
2526 case ERR_NODE:
2527 string = "Failed to map error addr to a node";
2528 break;
2529 case ERR_CSROW:
2530 string = "Failed to map error addr to a csrow";
2531 break;
2532 case ERR_CHANNEL:
Yazen Ghannam713ad542016-11-28 12:59:53 -06002533 string = "Unknown syndrome - possible error reporting race";
2534 break;
2535 case ERR_SYND:
2536 string = "MCA_SYND not valid - unknown syndrome and csrow";
2537 break;
2538 case ERR_NORM_ADDR:
2539 string = "Cannot decode normalized address";
Borislav Petkov33ca0642012-08-30 18:01:36 +02002540 break;
2541 default:
2542 string = "WTF error";
2543 break;
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002544 }
Borislav Petkov33ca0642012-08-30 18:01:36 +02002545
2546 edac_mc_handle_error(err_type, mci, 1,
2547 err->page, err->offset, err->syndrome,
2548 err->csrow, err->channel, -1,
2549 string, "");
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002550}
2551
Borislav Petkovdf781d02013-12-15 17:29:44 +01002552static inline void decode_bus_error(int node_id, struct mce *m)
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002553{
Daniel J Blueman0c510cc2015-02-17 11:34:38 +08002554 struct mem_ctl_info *mci;
2555 struct amd64_pvt *pvt;
Borislav Petkovf192c7b2011-01-10 14:24:32 +01002556 u8 ecc_type = (m->status >> 45) & 0x3;
Borislav Petkov66fed2d2012-08-09 18:41:07 +02002557 u8 xec = XEC(m->status, 0x1f);
2558 u16 ec = EC(m->status);
Borislav Petkov33ca0642012-08-30 18:01:36 +02002559 u64 sys_addr;
2560 struct err_info err;
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002561
Daniel J Blueman0c510cc2015-02-17 11:34:38 +08002562 mci = edac_mc_find(node_id);
2563 if (!mci)
2564 return;
2565
2566 pvt = mci->pvt_info;
2567
Borislav Petkov66fed2d2012-08-09 18:41:07 +02002568 /* Bail out early if this was an 'observed' error */
Borislav Petkov5980bb92011-01-07 16:26:49 +01002569 if (PP(ec) == NBSL_PP_OBS)
Borislav Petkovb70ef012009-06-25 19:32:38 +02002570 return;
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002571
Borislav Petkovecaf5602009-07-23 16:32:01 +02002572 /* Do only ECC errors */
2573 if (xec && xec != F10_NBSL_EXT_ERR_ECC)
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002574 return;
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002575
Borislav Petkov33ca0642012-08-30 18:01:36 +02002576 memset(&err, 0, sizeof(err));
2577
Borislav Petkova4b4bed2013-08-10 13:54:48 +02002578 sys_addr = get_error_address(pvt, m);
Borislav Petkov33ca0642012-08-30 18:01:36 +02002579
Borislav Petkovecaf5602009-07-23 16:32:01 +02002580 if (ecc_type == 2)
Borislav Petkov33ca0642012-08-30 18:01:36 +02002581 err.syndrome = extract_syndrome(m->status);
2582
2583 pvt->ops->map_sysaddr_to_csrow(mci, sys_addr, &err);
2584
Yazen Ghanname70984d2016-11-17 17:57:31 -05002585 __log_ecc_error(mci, &err, ecc_type);
Doug Thompsond27bf6f2009-05-06 17:55:27 +02002586}
2587
Doug Thompson0ec449e2009-04-27 19:41:25 +02002588/*
Yazen Ghannam713ad542016-11-28 12:59:53 -06002589 * To find the UMC channel represented by this bank we need to match on its
2590 * instance_id. The instance_id of a bank is held in the lower 32 bits of its
2591 * IPID.
Yazen Ghannambdcee772019-02-28 15:36:10 +00002592 *
2593 * Currently, we can derive the channel number by looking at the 6th nibble in
2594 * the instance_id. For example, instance_id=0xYXXXXX where Y is the channel
2595 * number.
Yazen Ghannam713ad542016-11-28 12:59:53 -06002596 */
Yazen Ghannambdcee772019-02-28 15:36:10 +00002597static int find_umc_channel(struct mce *m)
Yazen Ghannam713ad542016-11-28 12:59:53 -06002598{
Yazen Ghannambdcee772019-02-28 15:36:10 +00002599 return (m->ipid & GENMASK(31, 0)) >> 20;
Yazen Ghannam713ad542016-11-28 12:59:53 -06002600}
2601
2602static void decode_umc_error(int node_id, struct mce *m)
2603{
2604 u8 ecc_type = (m->status >> 45) & 0x3;
2605 struct mem_ctl_info *mci;
2606 struct amd64_pvt *pvt;
2607 struct err_info err;
2608 u64 sys_addr;
2609
2610 mci = edac_mc_find(node_id);
2611 if (!mci)
2612 return;
2613
2614 pvt = mci->pvt_info;
2615
2616 memset(&err, 0, sizeof(err));
2617
2618 if (m->status & MCI_STATUS_DEFERRED)
2619 ecc_type = 3;
2620
Yazen Ghannambdcee772019-02-28 15:36:10 +00002621 err.channel = find_umc_channel(m);
Yazen Ghannam713ad542016-11-28 12:59:53 -06002622
Yazen Ghannam713ad542016-11-28 12:59:53 -06002623 if (!(m->status & MCI_STATUS_SYNDV)) {
2624 err.err_code = ERR_SYND;
2625 goto log_error;
2626 }
2627
2628 if (ecc_type == 2) {
2629 u8 length = (m->synd >> 18) & 0x3f;
2630
2631 if (length)
2632 err.syndrome = (m->synd >> 32) & GENMASK(length - 1, 0);
2633 else
2634 err.err_code = ERR_CHANNEL;
2635 }
2636
2637 err.csrow = m->synd & 0x7;
2638
Yazen Ghannam8a2eaab2019-08-22 00:00:00 +00002639 if (umc_normaddr_to_sysaddr(m->addr, pvt->mc_node_id, err.channel, &sys_addr)) {
2640 err.err_code = ERR_NORM_ADDR;
2641 goto log_error;
2642 }
2643
2644 error_address_to_page_and_offset(sys_addr, &err);
2645
Yazen Ghannam713ad542016-11-28 12:59:53 -06002646log_error:
2647 __log_ecc_error(mci, &err, ecc_type);
2648}
2649
2650/*
Borislav Petkov3f37a362016-05-06 19:44:27 +02002651 * Use pvt->F3 which contains the F3 CPU PCI device to get the related
2652 * F1 (AddrMap) and F2 (Dct) devices. Return negative value on error.
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002653 * Reserve F0 and F6 on systems with a UMC.
Doug Thompson0ec449e2009-04-27 19:41:25 +02002654 */
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002655static int
2656reserve_mc_sibling_devs(struct amd64_pvt *pvt, u16 pci_id1, u16 pci_id2)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002657{
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002658 if (pvt->umc) {
2659 pvt->F0 = pci_get_related_function(pvt->F3->vendor, pci_id1, pvt->F3);
2660 if (!pvt->F0) {
Borislav Petkov5246c542016-12-01 11:35:07 +01002661 amd64_err("F0 not found, device 0x%x (broken BIOS?)\n", pci_id1);
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002662 return -ENODEV;
2663 }
2664
2665 pvt->F6 = pci_get_related_function(pvt->F3->vendor, pci_id2, pvt->F3);
2666 if (!pvt->F6) {
2667 pci_dev_put(pvt->F0);
2668 pvt->F0 = NULL;
2669
Borislav Petkov5246c542016-12-01 11:35:07 +01002670 amd64_err("F6 not found: device 0x%x (broken BIOS?)\n", pci_id2);
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002671 return -ENODEV;
2672 }
Borislav Petkov5246c542016-12-01 11:35:07 +01002673
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002674 edac_dbg(1, "F0: %s\n", pci_name(pvt->F0));
2675 edac_dbg(1, "F3: %s\n", pci_name(pvt->F3));
2676 edac_dbg(1, "F6: %s\n", pci_name(pvt->F6));
2677
2678 return 0;
2679 }
2680
Doug Thompson0ec449e2009-04-27 19:41:25 +02002681 /* Reserve the ADDRESS MAP Device */
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002682 pvt->F1 = pci_get_related_function(pvt->F3->vendor, pci_id1, pvt->F3);
Borislav Petkov8d5b5d92010-10-01 20:11:07 +02002683 if (!pvt->F1) {
Borislav Petkov5246c542016-12-01 11:35:07 +01002684 amd64_err("F1 not found: device 0x%x (broken BIOS?)\n", pci_id1);
Borislav Petkovbbd0c1f2010-10-01 19:27:58 +02002685 return -ENODEV;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002686 }
2687
Borislav Petkov3f37a362016-05-06 19:44:27 +02002688 /* Reserve the DCT Device */
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002689 pvt->F2 = pci_get_related_function(pvt->F3->vendor, pci_id2, pvt->F3);
Borislav Petkov3f37a362016-05-06 19:44:27 +02002690 if (!pvt->F2) {
Borislav Petkov8d5b5d92010-10-01 20:11:07 +02002691 pci_dev_put(pvt->F1);
2692 pvt->F1 = NULL;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002693
Borislav Petkov5246c542016-12-01 11:35:07 +01002694 amd64_err("F2 not found: device 0x%x (broken BIOS?)\n", pci_id2);
2695 return -ENODEV;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002696 }
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002697
Joe Perches956b9ba2012-04-29 17:08:39 -03002698 edac_dbg(1, "F1: %s\n", pci_name(pvt->F1));
2699 edac_dbg(1, "F2: %s\n", pci_name(pvt->F2));
2700 edac_dbg(1, "F3: %s\n", pci_name(pvt->F3));
Doug Thompson0ec449e2009-04-27 19:41:25 +02002701
2702 return 0;
2703}
2704
Borislav Petkov360b7f32010-10-15 19:25:38 +02002705static void free_mc_sibling_devs(struct amd64_pvt *pvt)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002706{
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05002707 if (pvt->umc) {
2708 pci_dev_put(pvt->F0);
2709 pci_dev_put(pvt->F6);
2710 } else {
2711 pci_dev_put(pvt->F1);
2712 pci_dev_put(pvt->F2);
2713 }
Doug Thompson0ec449e2009-04-27 19:41:25 +02002714}
2715
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002716static void determine_ecc_sym_sz(struct amd64_pvt *pvt)
2717{
2718 pvt->ecc_sym_sz = 4;
2719
2720 if (pvt->umc) {
2721 u8 i;
2722
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +00002723 for_each_umc(i) {
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002724 /* Check enabled channels only: */
Yazen Ghannam78359612019-02-28 15:36:11 +00002725 if (pvt->umc[i].sdp_ctrl & UMC_SDP_INIT) {
2726 if (pvt->umc[i].ecc_ctrl & BIT(9)) {
2727 pvt->ecc_sym_sz = 16;
2728 return;
2729 } else if (pvt->umc[i].ecc_ctrl & BIT(7)) {
2730 pvt->ecc_sym_sz = 8;
2731 return;
2732 }
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002733 }
2734 }
Yazen Ghannam78359612019-02-28 15:36:11 +00002735 } else if (pvt->fam >= 0x10) {
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002736 u32 tmp;
2737
2738 amd64_read_pci_cfg(pvt->F3, EXT_NB_MCA_CFG, &tmp);
2739 /* F16h has only DCT0, so no need to read dbam1. */
2740 if (pvt->fam != 0x16)
2741 amd64_read_dct_pci_cfg(pvt, 1, DBAM0, &pvt->dbam1);
2742
2743 /* F10h, revD and later can do x8 ECC too. */
2744 if ((pvt->fam > 0x10 || pvt->model > 7) && tmp & BIT(25))
2745 pvt->ecc_sym_sz = 8;
2746 }
2747}
2748
2749/*
2750 * Retrieve the hardware registers of the memory controller.
2751 */
2752static void __read_mc_regs_df(struct amd64_pvt *pvt)
2753{
2754 u8 nid = pvt->mc_node_id;
2755 struct amd64_umc *umc;
2756 u32 i, umc_base;
2757
2758 /* Read registers from each UMC */
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +00002759 for_each_umc(i) {
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002760
2761 umc_base = get_umc_base(i);
2762 umc = &pvt->umc[i];
2763
Yazen Ghannam07ed82e2016-11-28 08:50:21 -06002764 amd_smn_read(nid, umc_base + UMCCH_DIMM_CFG, &umc->dimm_cfg);
2765 amd_smn_read(nid, umc_base + UMCCH_UMC_CFG, &umc->umc_cfg);
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002766 amd_smn_read(nid, umc_base + UMCCH_SDP_CTRL, &umc->sdp_ctrl);
2767 amd_smn_read(nid, umc_base + UMCCH_ECC_CTRL, &umc->ecc_ctrl);
Yazen Ghannam07ed82e2016-11-28 08:50:21 -06002768 amd_smn_read(nid, umc_base + UMCCH_UMC_CAP_HI, &umc->umc_cap_hi);
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002769 }
2770}
2771
Doug Thompson0ec449e2009-04-27 19:41:25 +02002772/*
2773 * Retrieve the hardware registers of the memory controller (this includes the
2774 * 'Address Map' and 'Misc' device regs)
2775 */
Borislav Petkov360b7f32010-10-15 19:25:38 +02002776static void read_mc_regs(struct amd64_pvt *pvt)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002777{
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002778 unsigned int range;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002779 u64 msr_val;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002780
2781 /*
2782 * Retrieve TOP_MEM and TOP_MEM2; no masking off of reserved bits since
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002783 * those are Read-As-Zero.
Doug Thompson0ec449e2009-04-27 19:41:25 +02002784 */
Borislav Petkove97f8bb2009-10-12 15:27:45 +02002785 rdmsrl(MSR_K8_TOP_MEM1, pvt->top_mem);
Joe Perches956b9ba2012-04-29 17:08:39 -03002786 edac_dbg(0, " TOP_MEM: 0x%016llx\n", pvt->top_mem);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002787
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002788 /* Check first whether TOP_MEM2 is enabled: */
Doug Thompson0ec449e2009-04-27 19:41:25 +02002789 rdmsrl(MSR_K8_SYSCFG, msr_val);
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002790 if (msr_val & BIT(21)) {
Borislav Petkove97f8bb2009-10-12 15:27:45 +02002791 rdmsrl(MSR_K8_TOP_MEM2, pvt->top_mem2);
Joe Perches956b9ba2012-04-29 17:08:39 -03002792 edac_dbg(0, " TOP_MEM2: 0x%016llx\n", pvt->top_mem2);
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002793 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -03002794 edac_dbg(0, " TOP_MEM2 disabled\n");
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002795 }
2796
2797 if (pvt->umc) {
2798 __read_mc_regs_df(pvt);
2799 amd64_read_pci_cfg(pvt->F0, DF_DHAR, &pvt->dhar);
2800
2801 goto skip;
2802 }
Doug Thompson0ec449e2009-04-27 19:41:25 +02002803
Borislav Petkov5980bb92011-01-07 16:26:49 +01002804 amd64_read_pci_cfg(pvt->F3, NBCAP, &pvt->nbcap);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002805
Borislav Petkov5a5d2372011-01-17 17:52:57 +01002806 read_dram_ctl_register(pvt);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002807
Borislav Petkov7f19bf72010-10-21 18:52:53 +02002808 for (range = 0; range < DRAM_RANGES; range++) {
2809 u8 rw;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002810
Borislav Petkov7f19bf72010-10-21 18:52:53 +02002811 /* read settings for this DRAM range */
2812 read_dram_base_limit_regs(pvt, range);
Borislav Petkove97f8bb2009-10-12 15:27:45 +02002813
Borislav Petkov7f19bf72010-10-21 18:52:53 +02002814 rw = dram_rw(pvt, range);
2815 if (!rw)
2816 continue;
2817
Joe Perches956b9ba2012-04-29 17:08:39 -03002818 edac_dbg(1, " DRAM range[%d], base: 0x%016llx; limit: 0x%016llx\n",
2819 range,
2820 get_dram_base(pvt, range),
2821 get_dram_limit(pvt, range));
Borislav Petkov7f19bf72010-10-21 18:52:53 +02002822
Joe Perches956b9ba2012-04-29 17:08:39 -03002823 edac_dbg(1, " IntlvEn=%s; Range access: %s%s IntlvSel=%d DstNode=%d\n",
2824 dram_intlv_en(pvt, range) ? "Enabled" : "Disabled",
2825 (rw & 0x1) ? "R" : "-",
2826 (rw & 0x2) ? "W" : "-",
2827 dram_intlv_sel(pvt, range),
2828 dram_dst_node(pvt, range));
Doug Thompson0ec449e2009-04-27 19:41:25 +02002829 }
2830
Borislav Petkovbc21fa52010-11-11 17:29:13 +01002831 amd64_read_pci_cfg(pvt->F1, DHAR, &pvt->dhar);
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05002832 amd64_read_dct_pci_cfg(pvt, 0, DBAM0, &pvt->dbam0);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002833
Borislav Petkov8d5b5d92010-10-01 20:11:07 +02002834 amd64_read_pci_cfg(pvt->F3, F10_ONLINE_SPARE, &pvt->online_spare);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002835
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05002836 amd64_read_dct_pci_cfg(pvt, 0, DCLR0, &pvt->dclr0);
2837 amd64_read_dct_pci_cfg(pvt, 0, DCHR0, &pvt->dchr0);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002838
Borislav Petkov78da1212010-12-22 19:31:45 +01002839 if (!dct_ganging_enabled(pvt)) {
Aravind Gopalakrishnan7981a282014-09-15 11:37:38 -05002840 amd64_read_dct_pci_cfg(pvt, 1, DCLR0, &pvt->dclr1);
2841 amd64_read_dct_pci_cfg(pvt, 1, DCHR0, &pvt->dchr1);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002842 }
Borislav Petkovad6a32e2010-03-09 12:46:00 +01002843
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002844skip:
2845 read_dct_base_mask(pvt);
2846
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01002847 determine_memory_type(pvt);
2848 edac_dbg(1, " DIMM type: %s\n", edac_mem_types[pvt->dram_type]);
Borislav Petkova3b7db02011-01-19 20:35:12 +01002849
Yazen Ghannamb64ce7c2016-11-17 17:57:37 -05002850 determine_ecc_sym_sz(pvt);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002851}
2852
2853/*
2854 * NOTE: CPU Revision Dependent code
2855 *
2856 * Input:
Borislav Petkov11c75ea2010-11-29 19:49:02 +01002857 * @csrow_nr ChipSelect Row Number (0..NUM_CHIPSELECTS-1)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002858 * k8 private pointer to -->
2859 * DRAM Bank Address mapping register
2860 * node_id
2861 * DCL register where dual_channel_active is
2862 *
2863 * The DBAM register consists of 4 sets of 4 bits each definitions:
2864 *
2865 * Bits: CSROWs
2866 * 0-3 CSROWs 0 and 1
2867 * 4-7 CSROWs 2 and 3
2868 * 8-11 CSROWs 4 and 5
2869 * 12-15 CSROWs 6 and 7
2870 *
2871 * Values range from: 0 to 15
2872 * The meaning of the values depends on CPU revision and dual-channel state,
2873 * see relevant BKDG more info.
2874 *
2875 * The memory controller provides for total of only 8 CSROWs in its current
2876 * architecture. Each "pair" of CSROWs normally represents just one DIMM in
2877 * single channel or two (2) DIMMs in dual channel mode.
2878 *
2879 * The following code logic collapses the various tables for CSROW based on CPU
2880 * revision.
2881 *
2882 * Returns:
2883 * The number of PAGE_SIZE pages on the specified CSROW number it
2884 * encompasses
2885 *
2886 */
Yazen Ghannameb77e6b2017-04-27 12:11:54 -05002887static u32 get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr_orig)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002888{
Ashish Shenoyf92cae42012-02-22 17:20:38 -08002889 u32 dbam = dct ? pvt->dbam1 : pvt->dbam0;
Yazen Ghannameb77e6b2017-04-27 12:11:54 -05002890 int csrow_nr = csrow_nr_orig;
2891 u32 cs_mode, nr_pages;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002892
Yazen Ghanname53a3b22019-08-21 23:59:59 +00002893 if (!pvt->umc) {
Yazen Ghannameb77e6b2017-04-27 12:11:54 -05002894 csrow_nr >>= 1;
Yazen Ghanname53a3b22019-08-21 23:59:59 +00002895 cs_mode = DBAM_DIMM(csrow_nr, dbam);
2896 } else {
2897 cs_mode = f17_get_cs_mode(csrow_nr >> 1, dct, pvt);
2898 }
Doug Thompson0ec449e2009-04-27 19:41:25 +02002899
Yazen Ghannameb77e6b2017-04-27 12:11:54 -05002900 nr_pages = pvt->ops->dbam_to_cs(pvt, dct, cs_mode, csrow_nr);
2901 nr_pages <<= 20 - PAGE_SHIFT;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002902
Borislav Petkov10de6492012-09-12 19:00:38 +02002903 edac_dbg(0, "csrow: %d, channel: %d, DBAM idx: %d\n",
Yazen Ghannameb77e6b2017-04-27 12:11:54 -05002904 csrow_nr_orig, dct, cs_mode);
Borislav Petkov10de6492012-09-12 19:00:38 +02002905 edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002906
2907 return nr_pages;
2908}
2909
Yazen Ghannam353a1fc2019-08-21 23:59:57 +00002910static int init_csrows_df(struct mem_ctl_info *mci)
2911{
2912 struct amd64_pvt *pvt = mci->pvt_info;
2913 enum edac_type edac_mode = EDAC_NONE;
2914 enum dev_type dev_type = DEV_UNKNOWN;
2915 struct dimm_info *dimm;
2916 int empty = 1;
2917 u8 umc, cs;
2918
2919 if (mci->edac_ctl_cap & EDAC_FLAG_S16ECD16ED) {
2920 edac_mode = EDAC_S16ECD16ED;
2921 dev_type = DEV_X16;
2922 } else if (mci->edac_ctl_cap & EDAC_FLAG_S8ECD8ED) {
2923 edac_mode = EDAC_S8ECD8ED;
2924 dev_type = DEV_X8;
2925 } else if (mci->edac_ctl_cap & EDAC_FLAG_S4ECD4ED) {
2926 edac_mode = EDAC_S4ECD4ED;
2927 dev_type = DEV_X4;
2928 } else if (mci->edac_ctl_cap & EDAC_FLAG_SECDED) {
2929 edac_mode = EDAC_SECDED;
2930 }
2931
2932 for_each_umc(umc) {
2933 for_each_chip_select(cs, umc, pvt) {
2934 if (!csrow_enabled(cs, umc, pvt))
2935 continue;
2936
2937 empty = 0;
2938 dimm = mci->csrows[cs]->channels[umc]->dimm;
2939
2940 edac_dbg(1, "MC node: %d, csrow: %d\n",
2941 pvt->mc_node_id, cs);
2942
2943 dimm->nr_pages = get_csrow_nr_pages(pvt, umc, cs);
2944 dimm->mtype = pvt->dram_type;
2945 dimm->edac_mode = edac_mode;
2946 dimm->dtype = dev_type;
Yazen Ghannam466503d2019-10-22 20:35:14 +00002947 dimm->grain = 64;
Yazen Ghannam353a1fc2019-08-21 23:59:57 +00002948 }
2949 }
2950
2951 return empty;
2952}
2953
Doug Thompson0ec449e2009-04-27 19:41:25 +02002954/*
2955 * Initialize the array of csrow attribute instances, based on the values
2956 * from pci config hardware registers.
2957 */
Borislav Petkov360b7f32010-10-15 19:25:38 +02002958static int init_csrows(struct mem_ctl_info *mci)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002959{
Borislav Petkov10de6492012-09-12 19:00:38 +02002960 struct amd64_pvt *pvt = mci->pvt_info;
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06002961 enum edac_type edac_mode = EDAC_NONE;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002962 struct csrow_info *csrow;
Mauro Carvalho Chehabde3910eb2012-04-24 15:05:43 -03002963 struct dimm_info *dimm;
Borislav Petkov10de6492012-09-12 19:00:38 +02002964 int i, j, empty = 1;
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -03002965 int nr_pages = 0;
Borislav Petkov10de6492012-09-12 19:00:38 +02002966 u32 val;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002967
Yazen Ghannam353a1fc2019-08-21 23:59:57 +00002968 if (pvt->umc)
2969 return init_csrows_df(mci);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002970
Yazen Ghannam353a1fc2019-08-21 23:59:57 +00002971 amd64_read_pci_cfg(pvt->F3, NBCFG, &val);
Doug Thompson0ec449e2009-04-27 19:41:25 +02002972
Yazen Ghannam353a1fc2019-08-21 23:59:57 +00002973 pvt->nbcfg = val;
2974
2975 edac_dbg(0, "node %d, NBCFG=0x%08x[ChipKillEccCap: %d|DramEccEn: %d]\n",
2976 pvt->mc_node_id, val,
2977 !!(val & NBCFG_CHIPKILL), !!(val & NBCFG_ECC_ENABLE));
Doug Thompson0ec449e2009-04-27 19:41:25 +02002978
Borislav Petkov10de6492012-09-12 19:00:38 +02002979 /*
2980 * We iterate over DCT0 here but we look at DCT1 in parallel, if needed.
2981 */
Borislav Petkov11c75ea2010-11-29 19:49:02 +01002982 for_each_chip_select(i, 0, pvt) {
Borislav Petkov10de6492012-09-12 19:00:38 +02002983 bool row_dct0 = !!csrow_enabled(i, 0, pvt);
2984 bool row_dct1 = false;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002985
Borislav Petkova4b4bed2013-08-10 13:54:48 +02002986 if (pvt->fam != 0xf)
Borislav Petkov10de6492012-09-12 19:00:38 +02002987 row_dct1 = !!csrow_enabled(i, 1, pvt);
2988
2989 if (!row_dct0 && !row_dct1)
Doug Thompson0ec449e2009-04-27 19:41:25 +02002990 continue;
Doug Thompson0ec449e2009-04-27 19:41:25 +02002991
Borislav Petkov10de6492012-09-12 19:00:38 +02002992 csrow = mci->csrows[i];
Doug Thompson0ec449e2009-04-27 19:41:25 +02002993 empty = 0;
Borislav Petkov11c75ea2010-11-29 19:49:02 +01002994
Borislav Petkov10de6492012-09-12 19:00:38 +02002995 edac_dbg(1, "MC node: %d, csrow: %d\n",
2996 pvt->mc_node_id, i);
2997
Mauro Carvalho Chehab1eef1282013-03-11 09:07:46 -03002998 if (row_dct0) {
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01002999 nr_pages = get_csrow_nr_pages(pvt, 0, i);
Mauro Carvalho Chehab1eef1282013-03-11 09:07:46 -03003000 csrow->channels[0]->dimm->nr_pages = nr_pages;
3001 }
Borislav Petkov10de6492012-09-12 19:00:38 +02003002
3003 /* K8 has only one DCT */
Borislav Petkova4b4bed2013-08-10 13:54:48 +02003004 if (pvt->fam != 0xf && row_dct1) {
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003005 int row_dct1_pages = get_csrow_nr_pages(pvt, 1, i);
Mauro Carvalho Chehab1eef1282013-03-11 09:07:46 -03003006
3007 csrow->channels[1]->dimm->nr_pages = row_dct1_pages;
3008 nr_pages += row_dct1_pages;
3009 }
Doug Thompson0ec449e2009-04-27 19:41:25 +02003010
Borislav Petkov10de6492012-09-12 19:00:38 +02003011 edac_dbg(1, "Total csrow%d pages: %u\n", i, nr_pages);
Doug Thompson0ec449e2009-04-27 19:41:25 +02003012
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003013 /* Determine DIMM ECC mode: */
Yazen Ghannam353a1fc2019-08-21 23:59:57 +00003014 if (pvt->nbcfg & NBCFG_ECC_ENABLE) {
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003015 edac_mode = (pvt->nbcfg & NBCFG_CHIPKILL)
3016 ? EDAC_S4ECD4ED
3017 : EDAC_SECDED;
3018 }
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03003019
3020 for (j = 0; j < pvt->channel_count; j++) {
Mauro Carvalho Chehabde3910eb2012-04-24 15:05:43 -03003021 dimm = csrow->channels[j]->dimm;
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01003022 dimm->mtype = pvt->dram_type;
Mauro Carvalho Chehabde3910eb2012-04-24 15:05:43 -03003023 dimm->edac_mode = edac_mode;
Yazen Ghannam466503d2019-10-22 20:35:14 +00003024 dimm->grain = 64;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03003025 }
Doug Thompson0ec449e2009-04-27 19:41:25 +02003026 }
3027
3028 return empty;
3029}
Doug Thompsond27bf6f2009-05-06 17:55:27 +02003030
Borislav Petkov06724532009-09-16 13:05:46 +02003031/* get all cores on this DCT */
Daniel J Blueman8b84c8d2012-11-27 14:32:10 +08003032static void get_cpus_on_this_dct_cpumask(struct cpumask *mask, u16 nid)
Doug Thompsonf9431992009-04-27 19:46:08 +02003033{
Borislav Petkov06724532009-09-16 13:05:46 +02003034 int cpu;
Doug Thompsonf9431992009-04-27 19:46:08 +02003035
Borislav Petkov06724532009-09-16 13:05:46 +02003036 for_each_online_cpu(cpu)
3037 if (amd_get_nb_id(cpu) == nid)
3038 cpumask_set_cpu(cpu, mask);
Doug Thompsonf9431992009-04-27 19:46:08 +02003039}
3040
3041/* check MCG_CTL on all the cpus on this node */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003042static bool nb_mce_bank_enabled_on_node(u16 nid)
Doug Thompsonf9431992009-04-27 19:46:08 +02003043{
Rusty Russellba578cb2009-11-03 14:56:35 +10303044 cpumask_var_t mask;
Borislav Petkov50542252009-12-11 18:14:40 +01003045 int cpu, nbe;
Borislav Petkov06724532009-09-16 13:05:46 +02003046 bool ret = false;
Doug Thompsonf9431992009-04-27 19:46:08 +02003047
Rusty Russellba578cb2009-11-03 14:56:35 +10303048 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003049 amd64_warn("%s: Error allocating mask\n", __func__);
Rusty Russellba578cb2009-11-03 14:56:35 +10303050 return false;
3051 }
Borislav Petkov06724532009-09-16 13:05:46 +02003052
Rusty Russellba578cb2009-11-03 14:56:35 +10303053 get_cpus_on_this_dct_cpumask(mask, nid);
Borislav Petkov06724532009-09-16 13:05:46 +02003054
Rusty Russellba578cb2009-11-03 14:56:35 +10303055 rdmsr_on_cpus(mask, MSR_IA32_MCG_CTL, msrs);
Borislav Petkov06724532009-09-16 13:05:46 +02003056
Rusty Russellba578cb2009-11-03 14:56:35 +10303057 for_each_cpu(cpu, mask) {
Borislav Petkov50542252009-12-11 18:14:40 +01003058 struct msr *reg = per_cpu_ptr(msrs, cpu);
Borislav Petkov5980bb92011-01-07 16:26:49 +01003059 nbe = reg->l & MSR_MCGCTL_NBE;
Borislav Petkov06724532009-09-16 13:05:46 +02003060
Joe Perches956b9ba2012-04-29 17:08:39 -03003061 edac_dbg(0, "core: %u, MCG_CTL: 0x%llx, NB MSR is %s\n",
3062 cpu, reg->q,
3063 (nbe ? "enabled" : "disabled"));
Borislav Petkov06724532009-09-16 13:05:46 +02003064
3065 if (!nbe)
3066 goto out;
Borislav Petkov06724532009-09-16 13:05:46 +02003067 }
3068 ret = true;
3069
3070out:
Rusty Russellba578cb2009-11-03 14:56:35 +10303071 free_cpumask_var(mask);
Doug Thompsonf9431992009-04-27 19:46:08 +02003072 return ret;
3073}
3074
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08003075static int toggle_ecc_err_reporting(struct ecc_settings *s, u16 nid, bool on)
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003076{
3077 cpumask_var_t cmask;
Borislav Petkov50542252009-12-11 18:14:40 +01003078 int cpu;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003079
3080 if (!zalloc_cpumask_var(&cmask, GFP_KERNEL)) {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003081 amd64_warn("%s: error allocating mask\n", __func__);
Pan Bian0de27882016-12-04 14:07:18 +08003082 return -ENOMEM;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003083 }
3084
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003085 get_cpus_on_this_dct_cpumask(cmask, nid);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003086
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003087 rdmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs);
3088
3089 for_each_cpu(cpu, cmask) {
3090
Borislav Petkov50542252009-12-11 18:14:40 +01003091 struct msr *reg = per_cpu_ptr(msrs, cpu);
3092
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003093 if (on) {
Borislav Petkov5980bb92011-01-07 16:26:49 +01003094 if (reg->l & MSR_MCGCTL_NBE)
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003095 s->flags.nb_mce_enable = 1;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003096
Borislav Petkov5980bb92011-01-07 16:26:49 +01003097 reg->l |= MSR_MCGCTL_NBE;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003098 } else {
3099 /*
Borislav Petkovd95cf4d2010-02-24 14:49:47 +01003100 * Turn off NB MCE reporting only when it was off before
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003101 */
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003102 if (!s->flags.nb_mce_enable)
Borislav Petkov5980bb92011-01-07 16:26:49 +01003103 reg->l &= ~MSR_MCGCTL_NBE;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003104 }
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003105 }
3106 wrmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs);
3107
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003108 free_cpumask_var(cmask);
3109
3110 return 0;
3111}
3112
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08003113static bool enable_ecc_error_reporting(struct ecc_settings *s, u16 nid,
Borislav Petkov2299ef72010-10-15 17:44:04 +02003114 struct pci_dev *F3)
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003115{
Borislav Petkov2299ef72010-10-15 17:44:04 +02003116 bool ret = true;
Borislav Petkovc9f4f262010-12-22 19:48:20 +01003117 u32 value, mask = 0x3; /* UECC/CECC enable */
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003118
Borislav Petkov2299ef72010-10-15 17:44:04 +02003119 if (toggle_ecc_err_reporting(s, nid, ON)) {
3120 amd64_warn("Error enabling ECC reporting over MCGCTL!\n");
3121 return false;
3122 }
3123
Borislav Petkovc9f4f262010-12-22 19:48:20 +01003124 amd64_read_pci_cfg(F3, NBCTL, &value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003125
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003126 s->old_nbctl = value & mask;
3127 s->nbctl_valid = true;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003128
3129 value |= mask;
Borislav Petkovc9f4f262010-12-22 19:48:20 +01003130 amd64_write_pci_cfg(F3, NBCTL, value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003131
Borislav Petkova97fa682010-12-23 14:07:18 +01003132 amd64_read_pci_cfg(F3, NBCFG, &value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003133
Joe Perches956b9ba2012-04-29 17:08:39 -03003134 edac_dbg(0, "1: node %d, NBCFG=0x%08x[DramEccEn: %d]\n",
3135 nid, value, !!(value & NBCFG_ECC_ENABLE));
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003136
Borislav Petkova97fa682010-12-23 14:07:18 +01003137 if (!(value & NBCFG_ECC_ENABLE)) {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003138 amd64_warn("DRAM ECC disabled on this node, enabling...\n");
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003139
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003140 s->flags.nb_ecc_prev = 0;
Borislav Petkovd95cf4d2010-02-24 14:49:47 +01003141
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003142 /* Attempt to turn on DRAM ECC Enable */
Borislav Petkova97fa682010-12-23 14:07:18 +01003143 value |= NBCFG_ECC_ENABLE;
3144 amd64_write_pci_cfg(F3, NBCFG, value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003145
Borislav Petkova97fa682010-12-23 14:07:18 +01003146 amd64_read_pci_cfg(F3, NBCFG, &value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003147
Borislav Petkova97fa682010-12-23 14:07:18 +01003148 if (!(value & NBCFG_ECC_ENABLE)) {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003149 amd64_warn("Hardware rejected DRAM ECC enable,"
3150 "check memory DIMM configuration.\n");
Borislav Petkov2299ef72010-10-15 17:44:04 +02003151 ret = false;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003152 } else {
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003153 amd64_info("Hardware accepted DRAM ECC Enable\n");
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003154 }
Borislav Petkovd95cf4d2010-02-24 14:49:47 +01003155 } else {
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003156 s->flags.nb_ecc_prev = 1;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003157 }
Borislav Petkovd95cf4d2010-02-24 14:49:47 +01003158
Joe Perches956b9ba2012-04-29 17:08:39 -03003159 edac_dbg(0, "2: node %d, NBCFG=0x%08x[DramEccEn: %d]\n",
3160 nid, value, !!(value & NBCFG_ECC_ENABLE));
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003161
Borislav Petkov2299ef72010-10-15 17:44:04 +02003162 return ret;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003163}
3164
Daniel J Bluemanc7e53012012-11-30 16:44:20 +08003165static void restore_ecc_error_reporting(struct ecc_settings *s, u16 nid,
Borislav Petkov360b7f32010-10-15 19:25:38 +02003166 struct pci_dev *F3)
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003167{
Borislav Petkovc9f4f262010-12-22 19:48:20 +01003168 u32 value, mask = 0x3; /* UECC/CECC enable */
3169
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003170 if (!s->nbctl_valid)
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003171 return;
3172
Borislav Petkovc9f4f262010-12-22 19:48:20 +01003173 amd64_read_pci_cfg(F3, NBCTL, &value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003174 value &= ~mask;
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003175 value |= s->old_nbctl;
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003176
Borislav Petkovc9f4f262010-12-22 19:48:20 +01003177 amd64_write_pci_cfg(F3, NBCTL, value);
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003178
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003179 /* restore previous BIOS DRAM ECC "off" setting we force-enabled */
3180 if (!s->flags.nb_ecc_prev) {
Borislav Petkova97fa682010-12-23 14:07:18 +01003181 amd64_read_pci_cfg(F3, NBCFG, &value);
3182 value &= ~NBCFG_ECC_ENABLE;
3183 amd64_write_pci_cfg(F3, NBCFG, value);
Borislav Petkovd95cf4d2010-02-24 14:49:47 +01003184 }
3185
3186 /* restore the NB Enable MCGCTL bit */
Borislav Petkov2299ef72010-10-15 17:44:04 +02003187 if (toggle_ecc_err_reporting(s, nid, OFF))
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003188 amd64_warn("Error restoring NB MCGCTL settings!\n");
Borislav Petkovf6d6ae92009-11-03 15:29:26 +01003189}
3190
Doug Thompsonf9431992009-04-27 19:46:08 +02003191/*
Borislav Petkov2299ef72010-10-15 17:44:04 +02003192 * EDAC requires that the BIOS have ECC enabled before
3193 * taking over the processing of ECC errors. A command line
3194 * option allows to force-enable hardware ECC later in
3195 * enable_ecc_error_reporting().
Doug Thompsonf9431992009-04-27 19:46:08 +02003196 */
Borislav Petkovcab4d272010-02-11 17:15:57 +01003197static const char *ecc_msg =
3198 "ECC disabled in the BIOS or no ECC capability, module will not load.\n"
3199 " Either enable ECC checking or force module loading by setting "
3200 "'ecc_enable_override'.\n"
3201 " (Note that use of the override may cause unknown side effects.)\n";
Borislav Petkovbe3468e2009-08-05 15:47:22 +02003202
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003203static bool ecc_enabled(struct amd64_pvt *pvt)
Doug Thompsonf9431992009-04-27 19:46:08 +02003204{
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003205 u16 nid = pvt->mc_node_id;
Borislav Petkov06724532009-09-16 13:05:46 +02003206 bool nb_mce_en = false;
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003207 u8 ecc_en = 0, i;
3208 u32 value;
Doug Thompsonf9431992009-04-27 19:46:08 +02003209
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003210 if (boot_cpu_data.x86 >= 0x17) {
3211 u8 umc_en_mask = 0, ecc_en_mask = 0;
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003212 struct amd64_umc *umc;
Doug Thompsonf9431992009-04-27 19:46:08 +02003213
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +00003214 for_each_umc(i) {
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003215 umc = &pvt->umc[i];
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003216
3217 /* Only check enabled UMCs. */
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003218 if (!(umc->sdp_ctrl & UMC_SDP_INIT))
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003219 continue;
3220
3221 umc_en_mask |= BIT(i);
3222
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003223 if (umc->umc_cap_hi & UMC_ECC_ENABLED)
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003224 ecc_en_mask |= BIT(i);
3225 }
3226
3227 /* Check whether at least one UMC is enabled: */
3228 if (umc_en_mask)
3229 ecc_en = umc_en_mask == ecc_en_mask;
Yazen Ghannam11ab1ca2017-01-27 11:24:19 -06003230 else
3231 edac_dbg(0, "Node %d: No enabled UMCs.\n", nid);
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003232
3233 /* Assume UMC MCA banks are enabled. */
3234 nb_mce_en = true;
3235 } else {
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003236 amd64_read_pci_cfg(pvt->F3, NBCFG, &value);
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003237
3238 ecc_en = !!(value & NBCFG_ECC_ENABLE);
3239
3240 nb_mce_en = nb_mce_bank_enabled_on_node(nid);
3241 if (!nb_mce_en)
Yazen Ghannam11ab1ca2017-01-27 11:24:19 -06003242 edac_dbg(0, "NB MCE bank disabled, set MSR 0x%08x[4] on node %d to enable.\n",
Yazen Ghannam196b79f2016-11-17 17:57:34 -05003243 MSR_IA32_MCG_CTL, nid);
3244 }
3245
Yazen Ghannam11ab1ca2017-01-27 11:24:19 -06003246 amd64_info("Node %d: DRAM ECC %s.\n",
3247 nid, (ecc_en ? "enabled" : "disabled"));
Doug Thompsonf9431992009-04-27 19:46:08 +02003248
Borislav Petkov2299ef72010-10-15 17:44:04 +02003249 if (!ecc_en || !nb_mce_en) {
Yazen Ghannam11ab1ca2017-01-27 11:24:19 -06003250 amd64_info("%s", ecc_msg);
Borislav Petkov2299ef72010-10-15 17:44:04 +02003251 return false;
Borislav Petkov43f5e682009-12-21 18:55:18 +01003252 }
Borislav Petkov2299ef72010-10-15 17:44:04 +02003253 return true;
Doug Thompsonf9431992009-04-27 19:46:08 +02003254}
3255
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003256static inline void
3257f17h_determine_edac_ctl_cap(struct mem_ctl_info *mci, struct amd64_pvt *pvt)
3258{
Yazen Ghannamf8be8e52019-08-21 23:59:56 +00003259 u8 i, ecc_en = 1, cpk_en = 1, dev_x4 = 1, dev_x16 = 1;
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003260
Yazen Ghannam4d30d2b2019-02-28 15:36:10 +00003261 for_each_umc(i) {
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003262 if (pvt->umc[i].sdp_ctrl & UMC_SDP_INIT) {
3263 ecc_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_ENABLED);
3264 cpk_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_CHIPKILL_CAP);
Yazen Ghannamf8be8e52019-08-21 23:59:56 +00003265
3266 dev_x4 &= !!(pvt->umc[i].dimm_cfg & BIT(6));
3267 dev_x16 &= !!(pvt->umc[i].dimm_cfg & BIT(7));
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003268 }
3269 }
3270
3271 /* Set chipkill only if ECC is enabled: */
3272 if (ecc_en) {
3273 mci->edac_ctl_cap |= EDAC_FLAG_SECDED;
3274
Yazen Ghannamf8be8e52019-08-21 23:59:56 +00003275 if (!cpk_en)
3276 return;
3277
3278 if (dev_x4)
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003279 mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED;
Yazen Ghannamf8be8e52019-08-21 23:59:56 +00003280 else if (dev_x16)
3281 mci->edac_ctl_cap |= EDAC_FLAG_S16ECD16ED;
3282 else
3283 mci->edac_ctl_cap |= EDAC_FLAG_S8ECD8ED;
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003284 }
3285}
3286
Yazen Ghannam38ddd4d2019-10-22 20:35:09 +00003287static void setup_mci_misc_attrs(struct mem_ctl_info *mci)
Doug Thompson7d6034d2009-04-27 20:01:01 +02003288{
3289 struct amd64_pvt *pvt = mci->pvt_info;
3290
3291 mci->mtype_cap = MEM_FLAG_DDR2 | MEM_FLAG_RDDR2;
3292 mci->edac_ctl_cap = EDAC_FLAG_NONE;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003293
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003294 if (pvt->umc) {
3295 f17h_determine_edac_ctl_cap(mci, pvt);
3296 } else {
3297 if (pvt->nbcap & NBCAP_SECDED)
3298 mci->edac_ctl_cap |= EDAC_FLAG_SECDED;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003299
Yazen Ghannam2d09d8f2016-11-29 08:51:56 -06003300 if (pvt->nbcap & NBCAP_CHIPKILL)
3301 mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED;
3302 }
Doug Thompson7d6034d2009-04-27 20:01:01 +02003303
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003304 mci->edac_cap = determine_edac_cap(pvt);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003305 mci->mod_name = EDAC_MOD_STR;
Yazen Ghannam38ddd4d2019-10-22 20:35:09 +00003306 mci->ctl_name = fam_type->ctl_name;
Yazen Ghanname7934b72016-11-17 17:57:30 -05003307 mci->dev_name = pci_name(pvt->F3);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003308 mci->ctl_page_to_phys = NULL;
3309
Doug Thompson7d6034d2009-04-27 20:01:01 +02003310 /* memory scrubber interface */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003311 mci->set_sdram_scrub_rate = set_scrub_rate;
3312 mci->get_sdram_scrub_rate = get_scrub_rate;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003313}
3314
Borislav Petkov0092b202010-10-01 19:20:05 +02003315/*
3316 * returns a pointer to the family descriptor on success, NULL otherwise.
3317 */
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003318static struct amd64_family_type *per_family_init(struct amd64_pvt *pvt)
Borislav Petkov395ae782010-10-01 18:38:19 +02003319{
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05003320 pvt->ext_model = boot_cpu_data.x86_model >> 4;
Jia Zhangb3991512018-01-01 09:52:10 +08003321 pvt->stepping = boot_cpu_data.x86_stepping;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05003322 pvt->model = boot_cpu_data.x86_model;
3323 pvt->fam = boot_cpu_data.x86;
3324
3325 switch (pvt->fam) {
Borislav Petkov395ae782010-10-01 18:38:19 +02003326 case 0xf:
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003327 fam_type = &family_types[K8_CPUS];
3328 pvt->ops = &family_types[K8_CPUS].ops;
Borislav Petkov395ae782010-10-01 18:38:19 +02003329 break;
Borislav Petkovdf71a052011-01-19 18:15:10 +01003330
Borislav Petkov395ae782010-10-01 18:38:19 +02003331 case 0x10:
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003332 fam_type = &family_types[F10_CPUS];
3333 pvt->ops = &family_types[F10_CPUS].ops;
Borislav Petkovdf71a052011-01-19 18:15:10 +01003334 break;
3335
3336 case 0x15:
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05003337 if (pvt->model == 0x30) {
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003338 fam_type = &family_types[F15_M30H_CPUS];
3339 pvt->ops = &family_types[F15_M30H_CPUS].ops;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05003340 break;
Aravind Gopalakrishnana597d2a2014-10-30 12:16:09 +01003341 } else if (pvt->model == 0x60) {
3342 fam_type = &family_types[F15_M60H_CPUS];
3343 pvt->ops = &family_types[F15_M60H_CPUS].ops;
3344 break;
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05003345 }
3346
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003347 fam_type = &family_types[F15_CPUS];
3348 pvt->ops = &family_types[F15_CPUS].ops;
Borislav Petkov395ae782010-10-01 18:38:19 +02003349 break;
3350
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05003351 case 0x16:
Aravind Gopalakrishnan85a88852014-02-20 10:28:46 -06003352 if (pvt->model == 0x30) {
3353 fam_type = &family_types[F16_M30H_CPUS];
3354 pvt->ops = &family_types[F16_M30H_CPUS].ops;
3355 break;
3356 }
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003357 fam_type = &family_types[F16_CPUS];
3358 pvt->ops = &family_types[F16_CPUS].ops;
Aravind Gopalakrishnan94c1acf2013-04-17 14:57:13 -05003359 break;
3360
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05003361 case 0x17:
Michael Jin8960de42018-08-16 15:28:40 -04003362 if (pvt->model >= 0x10 && pvt->model <= 0x2f) {
3363 fam_type = &family_types[F17_M10H_CPUS];
3364 pvt->ops = &family_types[F17_M10H_CPUS].ops;
3365 break;
Yazen Ghannam6e8462392019-02-28 15:36:09 +00003366 } else if (pvt->model >= 0x30 && pvt->model <= 0x3f) {
3367 fam_type = &family_types[F17_M30H_CPUS];
3368 pvt->ops = &family_types[F17_M30H_CPUS].ops;
3369 break;
Isaac Vaughn3e443eb2019-09-06 23:21:38 +00003370 } else if (pvt->model >= 0x70 && pvt->model <= 0x7f) {
3371 fam_type = &family_types[F17_M70H_CPUS];
3372 pvt->ops = &family_types[F17_M70H_CPUS].ops;
3373 break;
Michael Jin8960de42018-08-16 15:28:40 -04003374 }
Pu Wenc4a3e942018-09-27 16:31:28 +02003375 /* fall through */
3376 case 0x18:
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05003377 fam_type = &family_types[F17_CPUS];
3378 pvt->ops = &family_types[F17_CPUS].ops;
Pu Wenc4a3e942018-09-27 16:31:28 +02003379
3380 if (pvt->fam == 0x18)
3381 family_types[F17_CPUS].ctl_name = "F18h";
Yazen Ghannamf1cbbec2016-11-17 17:57:35 -05003382 break;
3383
Borislav Petkov395ae782010-10-01 18:38:19 +02003384 default:
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003385 amd64_err("Unsupported family!\n");
Borislav Petkov0092b202010-10-01 19:20:05 +02003386 return NULL;
Borislav Petkov395ae782010-10-01 18:38:19 +02003387 }
Borislav Petkov0092b202010-10-01 19:20:05 +02003388
Borislav Petkovdf71a052011-01-19 18:15:10 +01003389 amd64_info("%s %sdetected (node %d).\n", fam_type->ctl_name,
Aravind Gopalakrishnan18b94f62013-08-09 11:54:49 -05003390 (pvt->fam == 0xf ?
Borislav Petkov24f9a7f2010-10-07 18:29:15 +02003391 (pvt->ext_model >= K8_REV_F ? "revF or later "
3392 : "revE or earlier ")
3393 : ""), pvt->mc_node_id);
Borislav Petkov0092b202010-10-01 19:20:05 +02003394 return fam_type;
Borislav Petkov395ae782010-10-01 18:38:19 +02003395}
3396
Takashi Iwaie339f1e2015-02-04 11:48:53 +01003397static const struct attribute_group *amd64_edac_attr_groups[] = {
3398#ifdef CONFIG_EDAC_DEBUG
3399 &amd64_edac_dbg_group,
3400#endif
3401#ifdef CONFIG_EDAC_AMD64_ERROR_INJECTION
3402 &amd64_edac_inj_group,
3403#endif
3404 NULL
3405};
3406
Yazen Ghannam80355a32019-10-22 20:35:10 +00003407static int hw_info_get(struct amd64_pvt *pvt)
Doug Thompson7d6034d2009-04-27 20:01:01 +02003408{
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05003409 u16 pci_id1, pci_id2;
Yazen Ghannam80355a32019-10-22 20:35:10 +00003410 int ret = -EINVAL;
Borislav Petkov395ae782010-10-01 18:38:19 +02003411
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05003412 if (pvt->fam >= 0x17) {
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00003413 pvt->umc = kcalloc(fam_type->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL);
Yazen Ghannam80355a32019-10-22 20:35:10 +00003414 if (!pvt->umc)
3415 return -ENOMEM;
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05003416
3417 pci_id1 = fam_type->f0_id;
3418 pci_id2 = fam_type->f6_id;
3419 } else {
3420 pci_id1 = fam_type->f1_id;
3421 pci_id2 = fam_type->f2_id;
3422 }
3423
Yazen Ghannam80355a32019-10-22 20:35:10 +00003424 ret = reserve_mc_sibling_devs(pvt, pci_id1, pci_id2);
3425 if (ret)
3426 return ret;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003427
Borislav Petkov360b7f32010-10-15 19:25:38 +02003428 read_mc_regs(pvt);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003429
Yazen Ghannam80355a32019-10-22 20:35:10 +00003430 return 0;
3431}
3432
3433static void hw_info_put(struct amd64_pvt *pvt)
3434{
3435 if (pvt->F0 || pvt->F1)
3436 free_mc_sibling_devs(pvt);
3437
3438 kfree(pvt->umc);
3439}
3440
3441static int init_one_instance(struct amd64_pvt *pvt)
3442{
3443 struct mem_ctl_info *mci = NULL;
3444 struct edac_mc_layer layers[2];
3445 int ret = -EINVAL;
3446
Doug Thompson7d6034d2009-04-27 20:01:01 +02003447 /*
3448 * We need to determine how many memory channels there are. Then use
3449 * that information for calculating the size of the dynamic instance
Borislav Petkov360b7f32010-10-15 19:25:38 +02003450 * tables in the 'mci' structure.
Doug Thompson7d6034d2009-04-27 20:01:01 +02003451 */
3452 pvt->channel_count = pvt->ops->early_channel_count(pvt);
3453 if (pvt->channel_count < 0)
Yazen Ghannam80355a32019-10-22 20:35:10 +00003454 return ret;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003455
3456 ret = -ENOMEM;
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03003457 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
3458 layers[0].size = pvt->csels[0].b_cnt;
3459 layers[0].is_virt_csrow = true;
3460 layers[1].type = EDAC_MC_LAYER_CHANNEL;
Borislav Petkovf0a56c42013-07-23 20:01:23 +02003461
3462 /*
3463 * Always allocate two channels since we can have setups with DIMMs on
3464 * only one channel. Also, this simplifies handling later for the price
3465 * of a couple of KBs tops.
3466 */
Yazen Ghannam5e4c5522019-10-22 20:35:11 +00003467 layers[1].size = fam_type->max_mcs;
Mauro Carvalho Chehabab5a5032012-04-16 15:03:50 -03003468 layers[1].is_virt_csrow = false;
Borislav Petkovf0a56c42013-07-23 20:01:23 +02003469
Yazen Ghannam80355a32019-10-22 20:35:10 +00003470 mci = edac_mc_alloc(pvt->mc_node_id, ARRAY_SIZE(layers), layers, 0);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003471 if (!mci)
Yazen Ghannam80355a32019-10-22 20:35:10 +00003472 return ret;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003473
3474 mci->pvt_info = pvt;
Borislav Petkov3f37a362016-05-06 19:44:27 +02003475 mci->pdev = &pvt->F3->dev;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003476
Yazen Ghannam38ddd4d2019-10-22 20:35:09 +00003477 setup_mci_misc_attrs(mci);
Borislav Petkov360b7f32010-10-15 19:25:38 +02003478
3479 if (init_csrows(mci))
Doug Thompson7d6034d2009-04-27 20:01:01 +02003480 mci->edac_cap = EDAC_FLAG_NONE;
3481
Doug Thompson7d6034d2009-04-27 20:01:01 +02003482 ret = -ENODEV;
Takashi Iwaie339f1e2015-02-04 11:48:53 +01003483 if (edac_mc_add_mc_with_groups(mci, amd64_edac_attr_groups)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003484 edac_dbg(1, "failed edac_mc_add_mc()\n");
Yazen Ghannam80355a32019-10-22 20:35:10 +00003485 edac_mc_free(mci);
3486 return ret;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003487 }
3488
Doug Thompson7d6034d2009-04-27 20:01:01 +02003489 return 0;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003490}
3491
Yazen Ghannam582f94b2019-11-06 01:25:01 +00003492static bool instance_has_memory(struct amd64_pvt *pvt)
3493{
3494 bool cs_enabled = false;
3495 int cs = 0, dct = 0;
3496
3497 for (dct = 0; dct < fam_type->max_mcs; dct++) {
3498 for_each_chip_select(cs, dct, pvt)
3499 cs_enabled |= csrow_enabled(cs, dct, pvt);
3500 }
3501
3502 return cs_enabled;
3503}
3504
Borislav Petkov3f37a362016-05-06 19:44:27 +02003505static int probe_one_instance(unsigned int nid)
Doug Thompson7d6034d2009-04-27 20:01:01 +02003506{
Borislav Petkov2299ef72010-10-15 17:44:04 +02003507 struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
Yazen Ghannam80355a32019-10-22 20:35:10 +00003508 struct amd64_pvt *pvt = NULL;
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003509 struct ecc_settings *s;
Borislav Petkov3f37a362016-05-06 19:44:27 +02003510 int ret;
Borislav Petkovb8cfa022010-10-01 19:35:38 +02003511
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003512 ret = -ENOMEM;
3513 s = kzalloc(sizeof(struct ecc_settings), GFP_KERNEL);
3514 if (!s)
Borislav Petkov2299ef72010-10-15 17:44:04 +02003515 goto err_out;
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003516
3517 ecc_stngs[nid] = s;
3518
Yazen Ghannam80355a32019-10-22 20:35:10 +00003519 pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL);
3520 if (!pvt)
3521 goto err_settings;
3522
3523 pvt->mc_node_id = nid;
3524 pvt->F3 = F3;
3525
3526 fam_type = per_family_init(pvt);
3527 if (!fam_type)
3528 goto err_enable;
3529
3530 ret = hw_info_get(pvt);
3531 if (ret < 0)
3532 goto err_enable;
3533
Yazen Ghannam582f94b2019-11-06 01:25:01 +00003534 ret = 0;
3535 if (!instance_has_memory(pvt)) {
3536 amd64_info("Node %d: No DIMMs detected.\n", nid);
3537 goto err_enable;
3538 }
3539
Yazen Ghannam1c9b08b2019-10-22 20:35:12 +00003540 if (!ecc_enabled(pvt)) {
Yazen Ghannam582f94b2019-11-06 01:25:01 +00003541 ret = -ENODEV;
Borislav Petkov2299ef72010-10-15 17:44:04 +02003542
3543 if (!ecc_enable_override)
3544 goto err_enable;
3545
Yazen Ghannam044e7a42016-11-22 15:40:16 -06003546 if (boot_cpu_data.x86 >= 0x17) {
3547 amd64_warn("Forcing ECC on is not recommended on newer systems. Please enable ECC in BIOS.");
3548 goto err_enable;
3549 } else
3550 amd64_warn("Forcing ECC on!\n");
Borislav Petkov2299ef72010-10-15 17:44:04 +02003551
3552 if (!enable_ecc_error_reporting(s, nid, F3))
3553 goto err_enable;
3554 }
3555
Yazen Ghannam80355a32019-10-22 20:35:10 +00003556 ret = init_one_instance(pvt);
Borislav Petkov360b7f32010-10-15 19:25:38 +02003557 if (ret < 0) {
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003558 amd64_err("Error probing instance: %d\n", nid);
Yazen Ghannam044e7a42016-11-22 15:40:16 -06003559
3560 if (boot_cpu_data.x86 < 0x17)
3561 restore_ecc_error_reporting(s, nid, F3);
Yazen Ghannam2b9b2c42017-01-24 16:32:24 -06003562
3563 goto err_enable;
Borislav Petkov360b7f32010-10-15 19:25:38 +02003564 }
Doug Thompson7d6034d2009-04-27 20:01:01 +02003565
Yazen Ghannam582f94b2019-11-06 01:25:01 +00003566 dump_misc_regs(pvt);
3567
Doug Thompson7d6034d2009-04-27 20:01:01 +02003568 return ret;
Borislav Petkov2299ef72010-10-15 17:44:04 +02003569
3570err_enable:
Yazen Ghannam80355a32019-10-22 20:35:10 +00003571 hw_info_put(pvt);
3572 kfree(pvt);
3573
3574err_settings:
Borislav Petkov2299ef72010-10-15 17:44:04 +02003575 kfree(s);
3576 ecc_stngs[nid] = NULL;
3577
3578err_out:
3579 return ret;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003580}
3581
Borislav Petkov3f37a362016-05-06 19:44:27 +02003582static void remove_one_instance(unsigned int nid)
Doug Thompson7d6034d2009-04-27 20:01:01 +02003583{
Borislav Petkov360b7f32010-10-15 19:25:38 +02003584 struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
3585 struct ecc_settings *s = ecc_stngs[nid];
Borislav Petkov3f37a362016-05-06 19:44:27 +02003586 struct mem_ctl_info *mci;
3587 struct amd64_pvt *pvt;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003588
Borislav Petkov3f37a362016-05-06 19:44:27 +02003589 mci = find_mci_by_dev(&F3->dev);
Borislav Petkova4b4bed2013-08-10 13:54:48 +02003590 WARN_ON(!mci);
3591
Doug Thompson7d6034d2009-04-27 20:01:01 +02003592 /* Remove from EDAC CORE tracking list */
Borislav Petkov3f37a362016-05-06 19:44:27 +02003593 mci = edac_mc_del_mc(&F3->dev);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003594 if (!mci)
3595 return;
3596
3597 pvt = mci->pvt_info;
3598
Borislav Petkov360b7f32010-10-15 19:25:38 +02003599 restore_ecc_error_reporting(s, nid, F3);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003600
Borislav Petkov360b7f32010-10-15 19:25:38 +02003601 kfree(ecc_stngs[nid]);
3602 ecc_stngs[nid] = NULL;
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003603
Doug Thompson7d6034d2009-04-27 20:01:01 +02003604 /* Free the EDAC CORE resources */
Borislav Petkov8f68ed92009-12-21 15:15:59 +01003605 mci->pvt_info = NULL;
Borislav Petkov8f68ed92009-12-21 15:15:59 +01003606
Yazen Ghannam80355a32019-10-22 20:35:10 +00003607 hw_info_put(pvt);
Borislav Petkov8f68ed92009-12-21 15:15:59 +01003608 kfree(pvt);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003609 edac_mc_free(mci);
3610}
3611
Borislav Petkov360b7f32010-10-15 19:25:38 +02003612static void setup_pci_device(void)
Doug Thompson7d6034d2009-04-27 20:01:01 +02003613{
3614 struct mem_ctl_info *mci;
3615 struct amd64_pvt *pvt;
3616
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003617 if (pci_ctl)
Doug Thompson7d6034d2009-04-27 20:01:01 +02003618 return;
3619
Borislav Petkov2ec591a2015-02-17 10:58:34 +01003620 mci = edac_mc_find(0);
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003621 if (!mci)
3622 return;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003623
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003624 pvt = mci->pvt_info;
Yazen Ghannam936fc3a2016-11-17 17:57:36 -05003625 if (pvt->umc)
3626 pci_ctl = edac_pci_create_generic_ctl(&pvt->F0->dev, EDAC_MOD_STR);
3627 else
3628 pci_ctl = edac_pci_create_generic_ctl(&pvt->F2->dev, EDAC_MOD_STR);
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003629 if (!pci_ctl) {
3630 pr_warn("%s(): Unable to create PCI control\n", __func__);
3631 pr_warn("%s(): PCI error report via EDAC not set\n", __func__);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003632 }
3633}
3634
Yazen Ghannamd6efab72016-09-15 19:07:17 -05003635static const struct x86_cpu_id amd64_cpuids[] = {
3636 { X86_VENDOR_AMD, 0xF, X86_MODEL_ANY, X86_FEATURE_ANY, 0 },
3637 { X86_VENDOR_AMD, 0x10, X86_MODEL_ANY, X86_FEATURE_ANY, 0 },
3638 { X86_VENDOR_AMD, 0x15, X86_MODEL_ANY, X86_FEATURE_ANY, 0 },
3639 { X86_VENDOR_AMD, 0x16, X86_MODEL_ANY, X86_FEATURE_ANY, 0 },
Yazen Ghannam95d3af62016-11-17 17:57:43 -05003640 { X86_VENDOR_AMD, 0x17, X86_MODEL_ANY, X86_FEATURE_ANY, 0 },
Pu Wenc4a3e942018-09-27 16:31:28 +02003641 { X86_VENDOR_HYGON, 0x18, X86_MODEL_ANY, X86_FEATURE_ANY, 0 },
Yazen Ghannamd6efab72016-09-15 19:07:17 -05003642 { }
3643};
3644MODULE_DEVICE_TABLE(x86cpu, amd64_cpuids);
3645
Doug Thompson7d6034d2009-04-27 20:01:01 +02003646static int __init amd64_edac_init(void)
3647{
Toshi Kani301375e2017-08-23 16:54:47 -06003648 const char *owner;
Borislav Petkov360b7f32010-10-15 19:25:38 +02003649 int err = -ENODEV;
Borislav Petkov3f37a362016-05-06 19:44:27 +02003650 int i;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003651
Toshi Kani301375e2017-08-23 16:54:47 -06003652 owner = edac_get_owner();
3653 if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
3654 return -EBUSY;
3655
Yazen Ghannam1bd99002017-01-27 11:24:23 -06003656 if (!x86_match_cpu(amd64_cpuids))
3657 return -ENODEV;
3658
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +02003659 if (amd_cache_northbridges() < 0)
Yazen Ghannam1bd99002017-01-27 11:24:23 -06003660 return -ENODEV;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003661
Borislav Petkov6ba92fe2016-06-16 01:13:18 +02003662 opstate_init();
3663
Borislav Petkovcc4d8862010-10-13 16:11:59 +02003664 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07003665 ecc_stngs = kcalloc(amd_nb_num(), sizeof(ecc_stngs[0]), GFP_KERNEL);
Borislav Petkov2ec591a2015-02-17 10:58:34 +01003666 if (!ecc_stngs)
Borislav Petkova9f0fbe2011-03-29 18:10:53 +02003667 goto err_free;
Borislav Petkovcc4d8862010-10-13 16:11:59 +02003668
Borislav Petkov50542252009-12-11 18:14:40 +01003669 msrs = msrs_alloc();
Borislav Petkov56b34b92009-12-21 18:13:01 +01003670 if (!msrs)
Borislav Petkov360b7f32010-10-15 19:25:38 +02003671 goto err_free;
Borislav Petkov50542252009-12-11 18:14:40 +01003672
Yazen Ghannam2287c632017-01-13 09:52:19 -06003673 for (i = 0; i < amd_nb_num(); i++) {
3674 err = probe_one_instance(i);
3675 if (err) {
Borislav Petkov3f37a362016-05-06 19:44:27 +02003676 /* unwind properly */
3677 while (--i >= 0)
3678 remove_one_instance(i);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003679
Borislav Petkov3f37a362016-05-06 19:44:27 +02003680 goto err_pci;
3681 }
Yazen Ghannam2287c632017-01-13 09:52:19 -06003682 }
Doug Thompson7d6034d2009-04-27 20:01:01 +02003683
Yazen Ghannam4688c9b2017-01-27 11:24:22 -06003684 if (!edac_has_mcs()) {
3685 err = -ENODEV;
3686 goto err_pci;
3687 }
3688
Yazen Ghannam234365f2017-01-24 16:32:25 -06003689 /* register stuff with EDAC MCE */
3690 if (report_gart_errors)
3691 amd_report_gart_errors(true);
3692
3693 if (boot_cpu_data.x86 >= 0x17)
3694 amd_register_ecc_decoder(decode_umc_error);
3695 else
3696 amd_register_ecc_decoder(decode_bus_error);
3697
Borislav Petkov360b7f32010-10-15 19:25:38 +02003698 setup_pci_device();
Tomasz Palaf5b10c42014-11-02 11:22:12 +01003699
3700#ifdef CONFIG_X86_32
3701 amd64_err("%s on 32-bit is unsupported. USE AT YOUR OWN RISK!\n", EDAC_MOD_STR);
3702#endif
3703
Borislav Petkovde0336b2016-04-27 12:21:21 +02003704 printk(KERN_INFO "AMD64 EDAC driver v%s\n", EDAC_AMD64_VERSION);
3705
Borislav Petkov360b7f32010-10-15 19:25:38 +02003706 return 0;
Borislav Petkov56b34b92009-12-21 18:13:01 +01003707
Borislav Petkov56b34b92009-12-21 18:13:01 +01003708err_pci:
3709 msrs_free(msrs);
3710 msrs = NULL;
Borislav Petkovcc4d8862010-10-13 16:11:59 +02003711
Borislav Petkov360b7f32010-10-15 19:25:38 +02003712err_free:
Borislav Petkov360b7f32010-10-15 19:25:38 +02003713 kfree(ecc_stngs);
3714 ecc_stngs = NULL;
3715
Doug Thompson7d6034d2009-04-27 20:01:01 +02003716 return err;
3717}
3718
3719static void __exit amd64_edac_exit(void)
3720{
Borislav Petkov3f37a362016-05-06 19:44:27 +02003721 int i;
3722
Borislav Petkovd1ea71c2013-12-15 17:54:27 +01003723 if (pci_ctl)
3724 edac_pci_release_generic_ctl(pci_ctl);
Doug Thompson7d6034d2009-04-27 20:01:01 +02003725
Yazen Ghannam234365f2017-01-24 16:32:25 -06003726 /* unregister from EDAC MCE */
3727 amd_report_gart_errors(false);
3728
3729 if (boot_cpu_data.x86 >= 0x17)
3730 amd_unregister_ecc_decoder(decode_umc_error);
3731 else
3732 amd_unregister_ecc_decoder(decode_bus_error);
3733
Borislav Petkov3f37a362016-05-06 19:44:27 +02003734 for (i = 0; i < amd_nb_num(); i++)
3735 remove_one_instance(i);
Borislav Petkov50542252009-12-11 18:14:40 +01003736
Borislav Petkovae7bb7c2010-10-14 16:01:30 +02003737 kfree(ecc_stngs);
3738 ecc_stngs = NULL;
3739
Borislav Petkov50542252009-12-11 18:14:40 +01003740 msrs_free(msrs);
3741 msrs = NULL;
Doug Thompson7d6034d2009-04-27 20:01:01 +02003742}
3743
3744module_init(amd64_edac_init);
3745module_exit(amd64_edac_exit);
3746
3747MODULE_LICENSE("GPL");
3748MODULE_AUTHOR("SoftwareBitMaker: Doug Thompson, "
3749 "Dave Peterson, Thayne Harbaugh");
3750MODULE_DESCRIPTION("MC support for AMD64 memory controllers - "
3751 EDAC_AMD64_VERSION);
3752
3753module_param(edac_op_state, int, 0444);
3754MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");