1 /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
3 * This driver supports the memory controllers found on the Intel
4 * processor family Sandy Bridge.
6 * This file may be distributed under the terms of the
7 * GNU General Public License version 2 only.
9 * Copyright (c) 2011 by:
10 * Mauro Carvalho Chehab
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/pci.h>
16 #include <linux/pci_ids.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/edac.h>
20 #include <linux/mmzone.h>
21 #include <linux/smp.h>
22 #include <linux/bitmap.h>
23 #include <linux/math64.h>
24 #include <linux/mod_devicetable.h>
25 #include <asm/cpu_device_id.h>
26 #include <asm/processor.h>
29 #include "edac_core.h"
32 static LIST_HEAD(sbridge_edac_list);
35 * Alter this version for the module when modifications are made
37 #define SBRIDGE_REVISION " Ver: 1.1.1 "
38 #define EDAC_MOD_STR "sbridge_edac"
43 #define sbridge_printk(level, fmt, arg...) \
44 edac_printk(level, "sbridge", fmt, ##arg)
46 #define sbridge_mc_printk(mci, level, fmt, arg...) \
47 edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
50 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
52 #define GET_BITFIELD(v, lo, hi) \
53 (((v) & GENMASK_ULL(hi, lo)) >> (lo))
55 /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
56 static const u32 sbridge_dram_rule[] = {
57 0x80, 0x88, 0x90, 0x98, 0xa0,
58 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
61 static const u32 ibridge_dram_rule[] = {
62 0x60, 0x68, 0x70, 0x78, 0x80,
63 0x88, 0x90, 0x98, 0xa0, 0xa8,
64 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
65 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
68 static const u32 knl_dram_rule[] = {
69 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
70 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
71 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
72 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
73 0x100, 0x108, 0x110, 0x118, /* 20-23 */
76 #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
77 #define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
79 static char *show_dram_attr(u32 attr)
93 static const u32 sbridge_interleave_list[] = {
94 0x84, 0x8c, 0x94, 0x9c, 0xa4,
95 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
98 static const u32 ibridge_interleave_list[] = {
99 0x64, 0x6c, 0x74, 0x7c, 0x84,
100 0x8c, 0x94, 0x9c, 0xa4, 0xac,
101 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
102 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
105 static const u32 knl_interleave_list[] = {
106 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
107 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
108 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
109 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
110 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
113 struct interleave_pkg {
118 static const struct interleave_pkg sbridge_interleave_pkg[] = {
129 static const struct interleave_pkg ibridge_interleave_pkg[] = {
140 static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
143 return GET_BITFIELD(reg, table[interleave].start,
144 table[interleave].end);
147 /* Devices 12 Function 7 */
151 #define HASWELL_TOLM 0xd0
152 #define HASWELL_TOHM_0 0xd4
153 #define HASWELL_TOHM_1 0xd8
154 #define KNL_TOLM 0xd0
155 #define KNL_TOHM_0 0xd4
156 #define KNL_TOHM_1 0xd8
158 #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
159 #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
161 /* Device 13 Function 6 */
163 #define SAD_TARGET 0xf0
165 #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
167 #define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
169 #define SAD_CONTROL 0xf4
171 /* Device 14 function 0 */
173 static const u32 tad_dram_rule[] = {
174 0x40, 0x44, 0x48, 0x4c,
175 0x50, 0x54, 0x58, 0x5c,
176 0x60, 0x64, 0x68, 0x6c,
178 #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
180 #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
181 #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
182 #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
183 #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
184 #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
185 #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
186 #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
188 /* Device 15, function 0 */
191 #define KNL_MCMTR 0x624
193 #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
194 #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
195 #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
197 /* Device 15, function 1 */
199 #define RASENABLES 0xac
200 #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
202 /* Device 15, functions 2-5 */
204 static const int mtr_regs[] = {
208 static const int knl_mtr_reg = 0xb60;
210 #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
211 #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
212 #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
213 #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
214 #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
216 static const u32 tad_ch_nilv_offset[] = {
217 0x90, 0x94, 0x98, 0x9c,
218 0xa0, 0xa4, 0xa8, 0xac,
219 0xb0, 0xb4, 0xb8, 0xbc,
221 #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
222 #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
224 static const u32 rir_way_limit[] = {
225 0x108, 0x10c, 0x110, 0x114, 0x118,
227 #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
229 #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
230 #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
232 #define MAX_RIR_WAY 8
234 static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
235 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
236 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
237 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
238 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
239 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
242 #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
243 #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
245 /* Device 16, functions 2-7 */
248 * FIXME: Implement the error count reads directly
251 static const u32 correrrcnt[] = {
252 0x104, 0x108, 0x10c, 0x110,
255 #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
256 #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
257 #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
258 #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
260 static const u32 correrrthrsld[] = {
261 0x11c, 0x120, 0x124, 0x128,
264 #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
265 #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
268 /* Device 17, function 0 */
270 #define SB_RANK_CFG_A 0x0328
272 #define IB_RANK_CFG_A 0x0320
278 #define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
279 #define MAX_DIMMS 3 /* Max DIMMS per channel */
280 #define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
281 #define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
282 #define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
283 #define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
294 struct sbridge_info {
298 u64 (*get_tolm)(struct sbridge_pvt *pvt);
299 u64 (*get_tohm)(struct sbridge_pvt *pvt);
300 u64 (*rir_limit)(u32 reg);
301 u64 (*sad_limit)(u32 reg);
302 u32 (*interleave_mode)(u32 reg);
303 char* (*show_interleave_mode)(u32 reg);
304 u32 (*dram_attr)(u32 reg);
305 const u32 *dram_rule;
306 const u32 *interleave_list;
307 const struct interleave_pkg *interleave_pkg;
310 u8 (*get_node_id)(struct sbridge_pvt *pvt);
311 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
312 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
313 struct pci_dev *pci_vtd;
316 struct sbridge_channel {
321 struct pci_id_descr {
326 struct pci_id_table {
327 const struct pci_id_descr *descr;
332 struct list_head list;
334 u8 node_id, source_id;
335 struct pci_dev **pdev;
337 struct mem_ctl_info *mci;
341 struct pci_dev *pci_cha[KNL_MAX_CHAS];
342 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
343 struct pci_dev *pci_mc0;
344 struct pci_dev *pci_mc1;
345 struct pci_dev *pci_mc0_misc;
346 struct pci_dev *pci_mc1_misc;
347 struct pci_dev *pci_mc_info; /* tolm, tohm */
351 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
352 struct pci_dev *pci_sad0, *pci_sad1;
353 struct pci_dev *pci_ha0, *pci_ha1;
354 struct pci_dev *pci_br0, *pci_br1;
355 struct pci_dev *pci_ha1_ta;
356 struct pci_dev *pci_tad[NUM_CHANNELS];
358 struct sbridge_dev *sbridge_dev;
360 struct sbridge_info info;
361 struct sbridge_channel channel[NUM_CHANNELS];
363 /* Memory type detection */
364 bool is_mirrored, is_lockstep, is_close_pg;
366 /* Memory description */
371 #define PCI_DESCR(device_id, opt) \
372 .dev_id = (device_id), \
375 static const struct pci_id_descr pci_dev_descr_sbridge[] = {
376 /* Processor Home Agent */
377 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
379 /* Memory controller */
380 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
381 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
382 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
383 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
384 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
385 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
386 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
388 /* System Address Decoder */
389 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
390 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
392 /* Broadcast Registers */
393 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
396 #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
397 static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
398 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
399 {0,} /* 0 terminated list. */
402 /* This changes depending if 1HA or 2HA:
404 * 0x0eb8 (17.0) is DDRIO0
406 * 0x0ebc (17.4) is DDRIO0
408 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
409 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
412 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
413 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
414 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
415 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
416 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
417 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
418 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
419 #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
420 #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
421 #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
422 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
423 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
424 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
425 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
426 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
427 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
428 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
430 static const struct pci_id_descr pci_dev_descr_ibridge[] = {
431 /* Processor Home Agent */
432 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
434 /* Memory controller */
435 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
436 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
437 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
438 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
439 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
440 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
442 /* System Address Decoder */
443 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
445 /* Broadcast Registers */
446 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
447 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
449 /* Optional, mode 2HA */
450 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
452 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
453 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
455 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
456 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
457 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
458 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
460 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
461 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
464 static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
465 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
466 {0,} /* 0 terminated list. */
469 /* Haswell support */
472 * - 3 DDR3 channels, 2 DPC per channel
475 * - 4 DDR4 channels, 3 DPC per channel
478 * - 4 DDR4 channels, 3 DPC per channel
481 * - each IMC interfaces with a SMI 2 channel
482 * - each SMI channel interfaces with a scalable memory buffer
483 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
485 #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
486 #define HASWELL_HASYSDEFEATURE2 0x84
487 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
488 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
489 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
490 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
491 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
492 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
493 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
494 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
495 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
496 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
497 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
498 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
499 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
500 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
501 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
502 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
503 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
504 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
505 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
506 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
507 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
508 static const struct pci_id_descr pci_dev_descr_haswell[] = {
509 /* first item must be the HA */
510 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
512 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
513 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
515 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
517 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
518 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
519 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
520 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
521 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
522 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
524 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
525 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
526 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
527 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
529 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
530 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
531 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
532 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
533 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
534 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
537 static const struct pci_id_table pci_dev_descr_haswell_table[] = {
538 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
539 {0,} /* 0 terminated list. */
542 /* Knight's Landing Support */
544 * KNL's memory channels are swizzled between memory controllers.
545 * MC0 is mapped to CH3,5,6 and MC1 is mapped to CH0,1,2
547 #define knl_channel_remap(channel) ((channel + 3) % 6)
549 /* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
550 #define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
551 /* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
552 #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
553 /* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
554 #define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
555 /* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
556 #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
557 /* SAD target - 1-29-1 (1 of these) */
558 #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
559 /* Caching / Home Agent */
560 #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
561 /* Device with TOLM and TOHM, 0-5-0 (1 of these) */
562 #define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
565 * KNL differs from SB, IB, and Haswell in that it has multiple
566 * instances of the same device with the same device ID, so we handle that
567 * by creating as many copies in the table as we expect to find.
568 * (Like device ID must be grouped together.)
571 static const struct pci_id_descr pci_dev_descr_knl[] = {
572 [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
573 [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
574 [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
575 [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
576 [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
577 [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
578 [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
581 static const struct pci_id_table pci_dev_descr_knl_table[] = {
582 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl),
591 * - 2 DDR3 channels, 2 DPC per channel
594 * - 4 DDR4 channels, 3 DPC per channel
597 * - 4 DDR4 channels, 3 DPC per channel
600 * - each IMC interfaces with a SMI 2 channel
601 * - each SMI channel interfaces with a scalable memory buffer
602 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
604 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
605 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
606 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
607 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
608 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
609 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
610 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
611 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
612 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
613 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
614 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
615 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
616 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
617 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
618 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
619 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
620 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
621 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
623 static const struct pci_id_descr pci_dev_descr_broadwell[] = {
624 /* first item must be the HA */
625 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
627 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
628 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
630 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
632 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
633 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
634 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
635 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
636 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
637 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
639 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
641 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
642 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
643 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
644 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
645 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
646 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
649 static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
650 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
651 {0,} /* 0 terminated list. */
655 /****************************************************************************
656 Ancillary status routines
657 ****************************************************************************/
659 static inline int numrank(enum type type, u32 mtr)
661 int ranks = (1 << RANK_CNT_BITS(mtr));
664 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
668 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
669 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
676 static inline int numrow(u32 mtr)
678 int rows = (RANK_WIDTH_BITS(mtr) + 12);
680 if (rows < 13 || rows > 18) {
681 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
682 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
689 static inline int numcol(u32 mtr)
691 int cols = (COL_WIDTH_BITS(mtr) + 10);
694 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
695 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
702 static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
704 struct sbridge_dev *sbridge_dev;
707 * If we have devices scattered across several busses that pertain
708 * to the same memory controller, we'll lump them all together.
711 return list_first_entry_or_null(&sbridge_edac_list,
712 struct sbridge_dev, list);
715 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
716 if (sbridge_dev->bus == bus)
723 static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
724 const struct pci_id_table *table)
726 struct sbridge_dev *sbridge_dev;
728 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
732 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
734 if (!sbridge_dev->pdev) {
739 sbridge_dev->bus = bus;
740 sbridge_dev->n_devs = table->n_devs;
741 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
746 static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
748 list_del(&sbridge_dev->list);
749 kfree(sbridge_dev->pdev);
753 static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
757 /* Address range is 32:28 */
758 pci_read_config_dword(pvt->pci_sad1, TOLM, ®);
759 return GET_TOLM(reg);
762 static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
766 pci_read_config_dword(pvt->pci_sad1, TOHM, ®);
767 return GET_TOHM(reg);
770 static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
774 pci_read_config_dword(pvt->pci_br1, TOLM, ®);
776 return GET_TOLM(reg);
779 static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
783 pci_read_config_dword(pvt->pci_br1, TOHM, ®);
785 return GET_TOHM(reg);
788 static u64 rir_limit(u32 reg)
790 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
793 static u64 sad_limit(u32 reg)
795 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
798 static u32 interleave_mode(u32 reg)
800 return GET_BITFIELD(reg, 1, 1);
803 char *show_interleave_mode(u32 reg)
805 return interleave_mode(reg) ? "8:6" : "[8:6]XOR[18:16]";
808 static u32 dram_attr(u32 reg)
810 return GET_BITFIELD(reg, 2, 3);
813 static u64 knl_sad_limit(u32 reg)
815 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
818 static u32 knl_interleave_mode(u32 reg)
820 return GET_BITFIELD(reg, 1, 2);
823 static char *knl_show_interleave_mode(u32 reg)
827 switch (knl_interleave_mode(reg)) {
829 s = "use address bits [8:6]";
832 s = "use address bits [10:8]";
835 s = "use address bits [14:12]";
838 s = "use address bits [32:30]";
848 static u32 dram_attr_knl(u32 reg)
850 return GET_BITFIELD(reg, 3, 4);
854 static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
859 if (pvt->pci_ddrio) {
860 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
862 if (GET_BITFIELD(reg, 11, 11))
863 /* FIXME: Can also be LRDIMM */
873 static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
876 bool registered = false;
877 enum mem_type mtype = MEM_UNKNOWN;
882 pci_read_config_dword(pvt->pci_ddrio,
883 HASWELL_DDRCRCLKCONTROLS, ®);
885 if (GET_BITFIELD(reg, 16, 16))
888 pci_read_config_dword(pvt->pci_ta, MCMTR, ®);
889 if (GET_BITFIELD(reg, 14, 14)) {
905 static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
907 /* for KNL value is fixed */
911 static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
913 /* there's no way to figure out */
917 static enum dev_type __ibridge_get_width(u32 mtr)
939 static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
942 * ddr3_width on the documentation but also valid for DDR4 on
945 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
948 static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
950 /* ddr3_width on the documentation but also valid for DDR4 */
951 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
954 static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
956 /* DDR4 RDIMMS and LRDIMMS are supported */
960 static u8 get_node_id(struct sbridge_pvt *pvt)
963 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, ®);
964 return GET_BITFIELD(reg, 0, 2);
967 static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
971 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, ®);
972 return GET_BITFIELD(reg, 0, 3);
975 static u8 knl_get_node_id(struct sbridge_pvt *pvt)
979 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, ®);
980 return GET_BITFIELD(reg, 0, 2);
984 static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
988 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, ®);
989 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
992 static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
997 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, ®);
998 rc = GET_BITFIELD(reg, 26, 31);
999 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, ®);
1000 rc = ((reg << 6) | rc) << 26;
1002 return rc | 0x1ffffff;
1005 static u64 knl_get_tolm(struct sbridge_pvt *pvt)
1009 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, ®);
1010 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1013 static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1018 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, ®_lo);
1019 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, ®_hi);
1020 rc = ((u64)reg_hi << 32) | reg_lo;
1021 return rc | 0x3ffffff;
1025 static u64 haswell_rir_limit(u32 reg)
1027 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1030 static inline u8 sad_pkg_socket(u8 pkg)
1032 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
1033 return ((pkg >> 3) << 2) | (pkg & 0x3);
1036 static inline u8 sad_pkg_ha(u8 pkg)
1038 return (pkg >> 2) & 0x1;
1041 /****************************************************************************
1042 Memory check routines
1043 ****************************************************************************/
1044 static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
1046 struct pci_dev *pdev = NULL;
1049 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
1050 if (pdev && pdev->bus->number == bus)
1058 * check_if_ecc_is_active() - Checks if ECC is active
1060 * @type: Memory controller type
1061 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
1064 static int check_if_ecc_is_active(const u8 bus, enum type type)
1066 struct pci_dev *pdev = NULL;
1071 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
1074 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
1077 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
1080 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
1082 case KNIGHTS_LANDING:
1084 * KNL doesn't group things by bus the same way
1085 * SB/IB/Haswell does.
1087 id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
1093 if (type != KNIGHTS_LANDING)
1094 pdev = get_pdev_same_bus(bus, id);
1096 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
1099 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
1100 "%04x:%04x! on bus %02d\n",
1101 PCI_VENDOR_ID_INTEL, id, bus);
1105 pci_read_config_dword(pdev,
1106 type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
1107 if (!IS_ECC_ENABLED(mcmtr)) {
1108 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
1114 /* Low bits of TAD limit, and some metadata. */
1115 static const u32 knl_tad_dram_limit_lo[] = {
1116 0x400, 0x500, 0x600, 0x700,
1117 0x800, 0x900, 0xa00, 0xb00,
1120 /* Low bits of TAD offset. */
1121 static const u32 knl_tad_dram_offset_lo[] = {
1122 0x404, 0x504, 0x604, 0x704,
1123 0x804, 0x904, 0xa04, 0xb04,
1126 /* High 16 bits of TAD limit and offset. */
1127 static const u32 knl_tad_dram_hi[] = {
1128 0x408, 0x508, 0x608, 0x708,
1129 0x808, 0x908, 0xa08, 0xb08,
1132 /* Number of ways a tad entry is interleaved. */
1133 static const u32 knl_tad_ways[] = {
1138 * Retrieve the n'th Target Address Decode table entry
1139 * from the memory controller's TAD table.
1141 * @pvt: driver private data
1142 * @entry: which entry you want to retrieve
1143 * @mc: which memory controller (0 or 1)
1144 * @offset: output tad range offset
1145 * @limit: output address of first byte above tad range
1146 * @ways: output number of interleave ways
1148 * The offset value has curious semantics. It's a sort of running total
1149 * of the sizes of all the memory regions that aren't mapped in this
1152 static int knl_get_tad(const struct sbridge_pvt *pvt,
1159 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1160 struct pci_dev *pci_mc;
1165 pci_mc = pvt->knl.pci_mc0;
1168 pci_mc = pvt->knl.pci_mc1;
1175 pci_read_config_dword(pci_mc,
1176 knl_tad_dram_limit_lo[entry], ®_limit_lo);
1177 pci_read_config_dword(pci_mc,
1178 knl_tad_dram_offset_lo[entry], ®_offset_lo);
1179 pci_read_config_dword(pci_mc,
1180 knl_tad_dram_hi[entry], ®_hi);
1182 /* Is this TAD entry enabled? */
1183 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1186 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1188 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1189 *ways = knl_tad_ways[way_id];
1192 sbridge_printk(KERN_ERR,
1193 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1199 * The least significant 6 bits of base and limit are truncated.
1200 * For limit, we fill the missing bits with 1s.
1202 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1203 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1204 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1205 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1210 /* Determine which memory controller is responsible for a given channel. */
1211 static int knl_channel_mc(int channel)
1213 WARN_ON(channel < 0 || channel >= 6);
1215 return channel < 3 ? 1 : 0;
1219 * Get the Nth entry from EDC_ROUTE_TABLE register.
1220 * (This is the per-tile mapping of logical interleave targets to
1221 * physical EDC modules.)
1233 static u32 knl_get_edc_route(int entry, u32 reg)
1235 WARN_ON(entry >= KNL_MAX_EDCS);
1236 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1240 * Get the Nth entry from MC_ROUTE_TABLE register.
1241 * (This is the per-tile mapping of logical interleave targets to
1242 * physical DRAM channels modules.)
1244 * entry 0: mc 0:2 channel 18:19
1245 * 1: mc 3:5 channel 20:21
1246 * 2: mc 6:8 channel 22:23
1247 * 3: mc 9:11 channel 24:25
1248 * 4: mc 12:14 channel 26:27
1249 * 5: mc 15:17 channel 28:29
1252 * Though we have 3 bits to identify the MC, we should only see
1253 * the values 0 or 1.
1256 static u32 knl_get_mc_route(int entry, u32 reg)
1260 WARN_ON(entry >= KNL_MAX_CHANNELS);
1262 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1263 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1265 return knl_channel_remap(mc*3 + chan);
1269 * Render the EDC_ROUTE register in human-readable form.
1270 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1272 static void knl_show_edc_route(u32 reg, char *s)
1276 for (i = 0; i < KNL_MAX_EDCS; i++) {
1277 s[i*2] = knl_get_edc_route(i, reg) + '0';
1281 s[KNL_MAX_EDCS*2 - 1] = '\0';
1285 * Render the MC_ROUTE register in human-readable form.
1286 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1288 static void knl_show_mc_route(u32 reg, char *s)
1292 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1293 s[i*2] = knl_get_mc_route(i, reg) + '0';
1297 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1300 #define KNL_EDC_ROUTE 0xb8
1301 #define KNL_MC_ROUTE 0xb4
1303 /* Is this dram rule backed by regular DRAM in flat mode? */
1304 #define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1306 /* Is this dram rule cached? */
1307 #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1309 /* Is this rule backed by edc ? */
1310 #define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1312 /* Is this rule backed by DRAM, cacheable in EDRAM? */
1313 #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1315 /* Is this rule mod3? */
1316 #define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1319 * Figure out how big our RAM modules are.
1321 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1322 * have to figure this out from the SAD rules, interleave lists, route tables,
1325 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1326 * inspect the TAD rules to figure out how large the SAD regions really are.
1328 * When we know the real size of a SAD region and how many ways it's
1329 * interleaved, we know the individual contribution of each channel to
1332 * Finally, we have to check whether each channel participates in each SAD
1335 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1336 * much memory the channel uses, we know the DIMM is at least that large.
1337 * (The BIOS might possibly choose not to map all available memory, in which
1338 * case we will underreport the size of the DIMM.)
1340 * In theory, we could try to determine the EDC sizes as well, but that would
1341 * only work in flat mode, not in cache mode.
1343 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1346 static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1348 u64 sad_base, sad_size, sad_limit = 0;
1349 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1352 int intrlv_ways, tad_ways;
1355 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1356 u32 dram_rule, interleave_reg;
1357 u32 mc_route_reg[KNL_MAX_CHAS];
1358 u32 edc_route_reg[KNL_MAX_CHAS];
1360 char edc_route_string[KNL_MAX_EDCS*2];
1361 char mc_route_string[KNL_MAX_CHANNELS*2];
1366 int participants[KNL_MAX_CHANNELS];
1367 int participant_count = 0;
1369 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1372 /* Read the EDC route table in each CHA. */
1374 for (i = 0; i < KNL_MAX_CHAS; i++) {
1375 pci_read_config_dword(pvt->knl.pci_cha[i],
1376 KNL_EDC_ROUTE, &edc_route_reg[i]);
1378 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1379 knl_show_edc_route(edc_route_reg[i-1],
1381 if (cur_reg_start == i-1)
1382 edac_dbg(0, "edc route table for CHA %d: %s\n",
1383 cur_reg_start, edc_route_string);
1385 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1386 cur_reg_start, i-1, edc_route_string);
1390 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1391 if (cur_reg_start == i-1)
1392 edac_dbg(0, "edc route table for CHA %d: %s\n",
1393 cur_reg_start, edc_route_string);
1395 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1396 cur_reg_start, i-1, edc_route_string);
1398 /* Read the MC route table in each CHA. */
1400 for (i = 0; i < KNL_MAX_CHAS; i++) {
1401 pci_read_config_dword(pvt->knl.pci_cha[i],
1402 KNL_MC_ROUTE, &mc_route_reg[i]);
1404 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1405 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1406 if (cur_reg_start == i-1)
1407 edac_dbg(0, "mc route table for CHA %d: %s\n",
1408 cur_reg_start, mc_route_string);
1410 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1411 cur_reg_start, i-1, mc_route_string);
1415 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1416 if (cur_reg_start == i-1)
1417 edac_dbg(0, "mc route table for CHA %d: %s\n",
1418 cur_reg_start, mc_route_string);
1420 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1421 cur_reg_start, i-1, mc_route_string);
1423 /* Process DRAM rules */
1424 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1425 /* previous limit becomes the new base */
1426 sad_base = sad_limit;
1428 pci_read_config_dword(pvt->pci_sad0,
1429 pvt->info.dram_rule[sad_rule], &dram_rule);
1431 if (!DRAM_RULE_ENABLE(dram_rule))
1434 edram_only = KNL_EDRAM_ONLY(dram_rule);
1436 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1437 sad_size = sad_limit - sad_base;
1439 pci_read_config_dword(pvt->pci_sad0,
1440 pvt->info.interleave_list[sad_rule], &interleave_reg);
1443 * Find out how many ways this dram rule is interleaved.
1444 * We stop when we see the first channel again.
1446 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1448 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1449 pkg = sad_pkg(pvt->info.interleave_pkg,
1450 interleave_reg, intrlv_ways);
1452 if ((pkg & 0x8) == 0) {
1454 * 0 bit means memory is non-local,
1455 * which KNL doesn't support
1457 edac_dbg(0, "Unexpected interleave target %d\n",
1462 if (pkg == first_pkg)
1465 if (KNL_MOD3(dram_rule))
1468 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1473 edram_only ? ", EDRAM" : "");
1476 * Find out how big the SAD region really is by iterating
1477 * over TAD tables (SAD regions may contain holes).
1478 * Each memory controller might have a different TAD table, so
1479 * we have to look at both.
1481 * Livespace is the memory that's mapped in this TAD table,
1482 * deadspace is the holes (this could be the MMIO hole, or it
1483 * could be memory that's mapped by the other TAD table but
1486 for (mc = 0; mc < 2; mc++) {
1487 sad_actual_size[mc] = 0;
1490 tad_rule < ARRAY_SIZE(
1491 knl_tad_dram_limit_lo);
1493 if (knl_get_tad(pvt,
1501 tad_size = (tad_limit+1) -
1502 (tad_livespace + tad_deadspace);
1503 tad_livespace += tad_size;
1504 tad_base = (tad_limit+1) - tad_size;
1506 if (tad_base < sad_base) {
1507 if (tad_limit > sad_base)
1508 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1509 } else if (tad_base < sad_limit) {
1510 if (tad_limit+1 > sad_limit) {
1511 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1513 /* TAD region is completely inside SAD region */
1514 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1516 tad_limit, tad_size,
1518 sad_actual_size[mc] += tad_size;
1521 tad_base = tad_limit+1;
1525 for (mc = 0; mc < 2; mc++) {
1526 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1527 mc, sad_actual_size[mc], sad_actual_size[mc]);
1530 /* Ignore EDRAM rule */
1534 /* Figure out which channels participate in interleave. */
1535 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1536 participants[channel] = 0;
1538 /* For each channel, does at least one CHA have
1539 * this channel mapped to the given target?
1541 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1542 for (way = 0; way < intrlv_ways; way++) {
1546 if (KNL_MOD3(dram_rule))
1549 target = 0x7 & sad_pkg(
1550 pvt->info.interleave_pkg, interleave_reg, way);
1552 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1553 if (knl_get_mc_route(target,
1554 mc_route_reg[cha]) == channel
1555 && !participants[channel]) {
1556 participant_count++;
1557 participants[channel] = 1;
1564 if (participant_count != intrlv_ways)
1565 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1566 participant_count, intrlv_ways);
1568 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1569 mc = knl_channel_mc(channel);
1570 if (participants[channel]) {
1571 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1573 sad_actual_size[mc]/intrlv_ways,
1575 mc_sizes[channel] +=
1576 sad_actual_size[mc]/intrlv_ways;
1584 static int get_dimm_config(struct mem_ctl_info *mci)
1586 struct sbridge_pvt *pvt = mci->pvt_info;
1587 struct dimm_info *dimm;
1588 unsigned i, j, banks, ranks, rows, cols, npages;
1591 enum edac_type mode;
1592 enum mem_type mtype;
1593 int channels = pvt->info.type == KNIGHTS_LANDING ?
1594 KNL_MAX_CHANNELS : NUM_CHANNELS;
1595 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
1597 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1598 pvt->info.type == KNIGHTS_LANDING)
1599 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, ®);
1601 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, ®);
1603 if (pvt->info.type == KNIGHTS_LANDING)
1604 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1606 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
1608 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
1609 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1610 pvt->sbridge_dev->mc,
1611 pvt->sbridge_dev->node_id,
1612 pvt->sbridge_dev->source_id);
1614 /* KNL doesn't support mirroring or lockstep,
1615 * and is always closed page
1617 if (pvt->info.type == KNIGHTS_LANDING) {
1618 mode = EDAC_S4ECD4ED;
1619 pvt->is_mirrored = false;
1621 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1624 pci_read_config_dword(pvt->pci_ras, RASENABLES, ®);
1625 if (IS_MIRROR_ENABLED(reg)) {
1626 edac_dbg(0, "Memory mirror is enabled\n");
1627 pvt->is_mirrored = true;
1629 edac_dbg(0, "Memory mirror is disabled\n");
1630 pvt->is_mirrored = false;
1633 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
1634 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1635 edac_dbg(0, "Lockstep is enabled\n");
1636 mode = EDAC_S8ECD8ED;
1637 pvt->is_lockstep = true;
1639 edac_dbg(0, "Lockstep is disabled\n");
1640 mode = EDAC_S4ECD4ED;
1641 pvt->is_lockstep = false;
1643 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1644 edac_dbg(0, "address map is on closed page mode\n");
1645 pvt->is_close_pg = true;
1647 edac_dbg(0, "address map is on open page mode\n");
1648 pvt->is_close_pg = false;
1652 mtype = pvt->info.get_memory_type(pvt);
1653 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
1654 edac_dbg(0, "Memory is registered\n");
1655 else if (mtype == MEM_UNKNOWN)
1656 edac_dbg(0, "Cannot determine memory type\n");
1658 edac_dbg(0, "Memory is unregistered\n");
1660 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
1665 for (i = 0; i < channels; i++) {
1668 int max_dimms_per_channel;
1670 if (pvt->info.type == KNIGHTS_LANDING) {
1671 max_dimms_per_channel = 1;
1672 if (!pvt->knl.pci_channel[i])
1675 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1676 if (!pvt->pci_tad[i])
1680 for (j = 0; j < max_dimms_per_channel; j++) {
1681 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1683 if (pvt->info.type == KNIGHTS_LANDING) {
1684 pci_read_config_dword(pvt->knl.pci_channel[i],
1687 pci_read_config_dword(pvt->pci_tad[i],
1690 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
1691 if (IS_DIMM_PRESENT(mtr)) {
1692 pvt->channel[i].dimms++;
1694 ranks = numrank(pvt->info.type, mtr);
1696 if (pvt->info.type == KNIGHTS_LANDING) {
1697 /* For DDR4, this is fixed. */
1699 rows = knl_mc_sizes[i] /
1700 ((u64) cols * ranks * banks * 8);
1706 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
1707 npages = MiB_TO_PAGES(size);
1709 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1710 pvt->sbridge_dev->mc, i/4, i%4, j,
1712 banks, ranks, rows, cols);
1714 dimm->nr_pages = npages;
1716 dimm->dtype = pvt->info.get_width(pvt, mtr);
1717 dimm->mtype = mtype;
1718 dimm->edac_mode = mode;
1719 snprintf(dimm->label, sizeof(dimm->label),
1720 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1721 pvt->sbridge_dev->source_id, i/4, i%4, j);
1729 static void get_memory_layout(const struct mem_ctl_info *mci)
1731 struct sbridge_pvt *pvt = mci->pvt_info;
1732 int i, j, k, n_sads, n_tads, sad_interl;
1740 * Step 1) Get TOLM/TOHM ranges
1743 pvt->tolm = pvt->info.get_tolm(pvt);
1744 tmp_mb = (1 + pvt->tolm) >> 20;
1746 gb = div_u64_rem(tmp_mb, 1024, &mb);
1747 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1748 gb, (mb*1000)/1024, (u64)pvt->tolm);
1750 /* Address range is already 45:25 */
1751 pvt->tohm = pvt->info.get_tohm(pvt);
1752 tmp_mb = (1 + pvt->tohm) >> 20;
1754 gb = div_u64_rem(tmp_mb, 1024, &mb);
1755 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1756 gb, (mb*1000)/1024, (u64)pvt->tohm);
1759 * Step 2) Get SAD range and SAD Interleave list
1760 * TAD registers contain the interleave wayness. However, it
1761 * seems simpler to just discover it indirectly, with the
1765 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1766 /* SAD_LIMIT Address range is 45:26 */
1767 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1769 limit = pvt->info.sad_limit(reg);
1771 if (!DRAM_RULE_ENABLE(reg))
1777 tmp_mb = (limit + 1) >> 20;
1778 gb = div_u64_rem(tmp_mb, 1024, &mb);
1779 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1781 show_dram_attr(pvt->info.dram_attr(reg)),
1783 ((u64)tmp_mb) << 20L,
1784 pvt->info.show_interleave_mode(reg),
1788 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1790 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1791 for (j = 0; j < 8; j++) {
1792 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1793 if (j > 0 && sad_interl == pkg)
1796 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
1801 if (pvt->info.type == KNIGHTS_LANDING)
1805 * Step 3) Get TAD range
1808 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1809 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1811 limit = TAD_LIMIT(reg);
1814 tmp_mb = (limit + 1) >> 20;
1816 gb = div_u64_rem(tmp_mb, 1024, &mb);
1817 edac_dbg(0, "TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
1818 n_tads, gb, (mb*1000)/1024,
1819 ((u64)tmp_mb) << 20L,
1820 (u32)(1 << TAD_SOCK(reg)),
1821 (u32)TAD_CH(reg) + 1,
1831 * Step 4) Get TAD offsets, per each channel
1833 for (i = 0; i < NUM_CHANNELS; i++) {
1834 if (!pvt->channel[i].dimms)
1836 for (j = 0; j < n_tads; j++) {
1837 pci_read_config_dword(pvt->pci_tad[i],
1838 tad_ch_nilv_offset[j],
1840 tmp_mb = TAD_OFFSET(reg) >> 20;
1841 gb = div_u64_rem(tmp_mb, 1024, &mb);
1842 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1845 ((u64)tmp_mb) << 20L,
1851 * Step 6) Get RIR Wayness/Limit, per each channel
1853 for (i = 0; i < NUM_CHANNELS; i++) {
1854 if (!pvt->channel[i].dimms)
1856 for (j = 0; j < MAX_RIR_RANGES; j++) {
1857 pci_read_config_dword(pvt->pci_tad[i],
1861 if (!IS_RIR_VALID(reg))
1864 tmp_mb = pvt->info.rir_limit(reg) >> 20;
1865 rir_way = 1 << RIR_WAY(reg);
1866 gb = div_u64_rem(tmp_mb, 1024, &mb);
1867 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1870 ((u64)tmp_mb) << 20L,
1874 for (k = 0; k < rir_way; k++) {
1875 pci_read_config_dword(pvt->pci_tad[i],
1878 tmp_mb = RIR_OFFSET(reg) << 6;
1880 gb = div_u64_rem(tmp_mb, 1024, &mb);
1881 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1884 ((u64)tmp_mb) << 20L,
1885 (u32)RIR_RNK_TGT(reg),
1892 static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
1894 struct sbridge_dev *sbridge_dev;
1896 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1897 if (sbridge_dev->node_id == node_id)
1898 return sbridge_dev->mci;
1903 static int get_memory_error_data(struct mem_ctl_info *mci,
1908 char **area_type, char *msg)
1910 struct mem_ctl_info *new_mci;
1911 struct sbridge_pvt *pvt = mci->pvt_info;
1912 struct pci_dev *pci_ha;
1913 int n_rir, n_sads, n_tads, sad_way, sck_xch;
1914 int sad_interl, idx, base_ch;
1915 int interleave_mode, shiftup = 0;
1916 unsigned sad_interleave[pvt->info.max_interleave];
1918 u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
1922 u64 ch_addr, offset, limit = 0, prv = 0;
1926 * Step 0) Check if the address is at special memory ranges
1927 * The check bellow is probably enough to fill all cases where
1928 * the error is not inside a memory, except for the legacy
1929 * range (e. g. VGA addresses). It is unlikely, however, that the
1930 * memory controller would generate an error on that range.
1932 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
1933 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
1936 if (addr >= (u64)pvt->tohm) {
1937 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
1942 * Step 1) Get socket
1944 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1945 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1948 if (!DRAM_RULE_ENABLE(reg))
1951 limit = pvt->info.sad_limit(reg);
1953 sprintf(msg, "Can't discover the memory socket");
1960 if (n_sads == pvt->info.max_sad) {
1961 sprintf(msg, "Can't discover the memory socket");
1965 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1966 interleave_mode = pvt->info.interleave_mode(dram_rule);
1968 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1971 if (pvt->info.type == SANDY_BRIDGE) {
1972 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1973 for (sad_way = 0; sad_way < 8; sad_way++) {
1974 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1975 if (sad_way > 0 && sad_interl == pkg)
1977 sad_interleave[sad_way] = pkg;
1978 edac_dbg(0, "SAD interleave #%d: %d\n",
1979 sad_way, sad_interleave[sad_way]);
1981 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1982 pvt->sbridge_dev->mc,
1987 !interleave_mode ? "" : "XOR[18:16]");
1988 if (interleave_mode)
1989 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1991 idx = (addr >> 6) & 7;
2005 sprintf(msg, "Can't discover socket interleave");
2008 *socket = sad_interleave[idx];
2009 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2010 idx, sad_way, *socket);
2011 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
2012 int bits, a7mode = A7MODE(dram_rule);
2015 /* A7 mode swaps P9 with P6 */
2016 bits = GET_BITFIELD(addr, 7, 8) << 1;
2017 bits |= GET_BITFIELD(addr, 9, 9);
2019 bits = GET_BITFIELD(addr, 6, 8);
2021 if (interleave_mode == 0) {
2022 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2023 idx = GET_BITFIELD(addr, 16, 18);
2028 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2029 *socket = sad_pkg_socket(pkg);
2030 sad_ha = sad_pkg_ha(pkg);
2035 /* MCChanShiftUpEnable */
2036 pci_read_config_dword(pvt->pci_ha0,
2037 HASWELL_HASYSDEFEATURE2, ®);
2038 shiftup = GET_BITFIELD(reg, 22, 22);
2041 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2042 idx, *socket, sad_ha, shiftup);
2044 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
2045 idx = (addr >> 6) & 7;
2046 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2047 *socket = sad_pkg_socket(pkg);
2048 sad_ha = sad_pkg_ha(pkg);
2051 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2052 idx, *socket, sad_ha);
2058 * Move to the proper node structure, in order to access the
2059 * right PCI registers
2061 new_mci = get_mci_for_node_id(*socket);
2063 sprintf(msg, "Struct for socket #%u wasn't initialized",
2068 pvt = mci->pvt_info;
2071 * Step 2) Get memory channel
2074 if (pvt->info.type == SANDY_BRIDGE)
2075 pci_ha = pvt->pci_ha0;
2078 pci_ha = pvt->pci_ha1;
2080 pci_ha = pvt->pci_ha0;
2082 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
2083 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], ®);
2084 limit = TAD_LIMIT(reg);
2086 sprintf(msg, "Can't discover the memory channel");
2093 if (n_tads == MAX_TAD) {
2094 sprintf(msg, "Can't discover the memory channel");
2098 ch_way = TAD_CH(reg) + 1;
2099 sck_way = 1 << TAD_SOCK(reg);
2104 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
2108 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2112 base_ch = TAD_TGT0(reg);
2115 base_ch = TAD_TGT1(reg);
2118 base_ch = TAD_TGT2(reg);
2121 base_ch = TAD_TGT3(reg);
2124 sprintf(msg, "Can't discover the TAD target");
2127 *channel_mask = 1 << base_ch;
2129 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
2130 tad_ch_nilv_offset[n_tads],
2133 if (pvt->is_mirrored) {
2134 *channel_mask |= 1 << ((base_ch + 2) % 4);
2138 sck_xch = 1 << sck_way * (ch_way >> 1);
2141 sprintf(msg, "Invalid mirror set. Can't decode addr");
2145 sck_xch = (1 << sck_way) * ch_way;
2147 if (pvt->is_lockstep)
2148 *channel_mask |= 1 << ((base_ch + 1) % 4);
2150 offset = TAD_OFFSET(tad_offset);
2152 edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
2163 /* Calculate channel address */
2164 /* Remove the TAD offset */
2166 if (offset > addr) {
2167 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2172 ch_addr = addr - offset;
2173 ch_addr >>= (6 + shiftup);
2174 ch_addr /= ch_way * sck_way;
2175 ch_addr <<= (6 + shiftup);
2176 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
2179 * Step 3) Decode rank
2181 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
2182 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
2183 rir_way_limit[n_rir],
2186 if (!IS_RIR_VALID(reg))
2189 limit = pvt->info.rir_limit(reg);
2190 gb = div_u64_rem(limit >> 20, 1024, &mb);
2191 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2196 if (ch_addr <= limit)
2199 if (n_rir == MAX_RIR_RANGES) {
2200 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2204 rir_way = RIR_WAY(reg);
2206 if (pvt->is_close_pg)
2207 idx = (ch_addr >> 6);
2209 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2210 idx %= 1 << rir_way;
2212 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
2213 rir_offset[n_rir][idx],
2215 *rank = RIR_RNK_TGT(reg);
2217 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2227 /****************************************************************************
2228 Device initialization routines: put/get, init/exit
2229 ****************************************************************************/
2232 * sbridge_put_all_devices 'put' all the devices that we have
2233 * reserved via 'get'
2235 static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2240 for (i = 0; i < sbridge_dev->n_devs; i++) {
2241 struct pci_dev *pdev = sbridge_dev->pdev[i];
2244 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2246 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2251 static void sbridge_put_all_devices(void)
2253 struct sbridge_dev *sbridge_dev, *tmp;
2255 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2256 sbridge_put_devices(sbridge_dev);
2257 free_sbridge_dev(sbridge_dev);
2261 static int sbridge_get_onedevice(struct pci_dev **prev,
2263 const struct pci_id_table *table,
2264 const unsigned devno,
2265 const int multi_bus)
2267 struct sbridge_dev *sbridge_dev;
2268 const struct pci_id_descr *dev_descr = &table->descr[devno];
2269 struct pci_dev *pdev = NULL;
2272 sbridge_printk(KERN_DEBUG,
2273 "Seeking for: PCI ID %04x:%04x\n",
2274 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2276 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2277 dev_descr->dev_id, *prev);
2285 if (dev_descr->optional)
2288 /* if the HA wasn't found */
2292 sbridge_printk(KERN_INFO,
2293 "Device not found: %04x:%04x\n",
2294 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2296 /* End of list, leave */
2299 bus = pdev->bus->number;
2301 sbridge_dev = get_sbridge_dev(bus, multi_bus);
2303 sbridge_dev = alloc_sbridge_dev(bus, table);
2311 if (sbridge_dev->pdev[devno]) {
2312 sbridge_printk(KERN_ERR,
2313 "Duplicated device for %04x:%04x\n",
2314 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2319 sbridge_dev->pdev[devno] = pdev;
2321 /* Be sure that the device is enabled */
2322 if (unlikely(pci_enable_device(pdev) < 0)) {
2323 sbridge_printk(KERN_ERR,
2324 "Couldn't enable %04x:%04x\n",
2325 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2329 edac_dbg(0, "Detected %04x:%04x\n",
2330 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2333 * As stated on drivers/pci/search.c, the reference count for
2334 * @from is always decremented if it is not %NULL. So, as we need
2335 * to get all devices up to null, we need to do a get for the device
2345 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
2346 * devices we want to reference for this driver.
2347 * @num_mc: pointer to the memory controllers count, to be incremented in case
2349 * @table: model specific table
2350 * @allow_dups: allow for multiple devices to exist with the same device id
2351 * (as implemented, this isn't expected to work correctly in the
2352 * multi-socket case).
2353 * @multi_bus: don't assume devices on different buses belong to different
2354 * memory controllers.
2356 * returns 0 in case of success or error code
2358 static int sbridge_get_all_devices_full(u8 *num_mc,
2359 const struct pci_id_table *table,
2364 struct pci_dev *pdev = NULL;
2366 while (table && table->descr) {
2367 for (i = 0; i < table->n_devs; i++) {
2368 if (!allow_dups || i == 0 ||
2369 table->descr[i].dev_id !=
2370 table->descr[i-1].dev_id) {
2374 rc = sbridge_get_onedevice(&pdev, num_mc,
2375 table, i, multi_bus);
2381 sbridge_put_all_devices();
2384 } while (pdev && !allow_dups);
2392 #define sbridge_get_all_devices(num_mc, table) \
2393 sbridge_get_all_devices_full(num_mc, table, 0, 0)
2394 #define sbridge_get_all_devices_knl(num_mc, table) \
2395 sbridge_get_all_devices_full(num_mc, table, 1, 1)
2397 static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2398 struct sbridge_dev *sbridge_dev)
2400 struct sbridge_pvt *pvt = mci->pvt_info;
2401 struct pci_dev *pdev;
2402 u8 saw_chan_mask = 0;
2405 for (i = 0; i < sbridge_dev->n_devs; i++) {
2406 pdev = sbridge_dev->pdev[i];
2410 switch (pdev->device) {
2411 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2412 pvt->pci_sad0 = pdev;
2414 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2415 pvt->pci_sad1 = pdev;
2417 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2418 pvt->pci_br0 = pdev;
2420 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2421 pvt->pci_ha0 = pdev;
2423 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2426 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2427 pvt->pci_ras = pdev;
2429 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2430 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2431 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2432 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2434 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
2435 pvt->pci_tad[id] = pdev;
2436 saw_chan_mask |= 1 << id;
2439 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2440 pvt->pci_ddrio = pdev;
2446 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2447 pdev->vendor, pdev->device,
2452 /* Check if everything were registered */
2453 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
2454 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
2457 if (saw_chan_mask != 0x0f)
2462 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2466 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2467 PCI_VENDOR_ID_INTEL, pdev->device);
2471 static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2472 struct sbridge_dev *sbridge_dev)
2474 struct sbridge_pvt *pvt = mci->pvt_info;
2475 struct pci_dev *pdev;
2476 u8 saw_chan_mask = 0;
2479 for (i = 0; i < sbridge_dev->n_devs; i++) {
2480 pdev = sbridge_dev->pdev[i];
2484 switch (pdev->device) {
2485 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
2486 pvt->pci_ha0 = pdev;
2488 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2490 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
2491 pvt->pci_ras = pdev;
2493 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2494 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
2495 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2496 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
2498 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
2499 pvt->pci_tad[id] = pdev;
2500 saw_chan_mask |= 1 << id;
2503 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2504 pvt->pci_ddrio = pdev;
2506 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
2507 pvt->pci_ddrio = pdev;
2509 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2510 pvt->pci_sad0 = pdev;
2512 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2513 pvt->pci_br0 = pdev;
2515 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2516 pvt->pci_br1 = pdev;
2518 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
2519 pvt->pci_ha1 = pdev;
2521 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2522 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
2523 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2524 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
2526 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
2527 pvt->pci_tad[id] = pdev;
2528 saw_chan_mask |= 1 << id;
2535 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2537 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2541 /* Check if everything were registered */
2542 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
2543 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
2547 if (saw_chan_mask != 0x0f && /* -EN */
2548 saw_chan_mask != 0x33 && /* -EP */
2549 saw_chan_mask != 0xff) /* -EX */
2554 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2558 sbridge_printk(KERN_ERR,
2559 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2564 static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2565 struct sbridge_dev *sbridge_dev)
2567 struct sbridge_pvt *pvt = mci->pvt_info;
2568 struct pci_dev *pdev;
2569 u8 saw_chan_mask = 0;
2572 /* there's only one device per system; not tied to any bus */
2573 if (pvt->info.pci_vtd == NULL)
2574 /* result will be checked later */
2575 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2576 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2579 for (i = 0; i < sbridge_dev->n_devs; i++) {
2580 pdev = sbridge_dev->pdev[i];
2584 switch (pdev->device) {
2585 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2586 pvt->pci_sad0 = pdev;
2588 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2589 pvt->pci_sad1 = pdev;
2591 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2592 pvt->pci_ha0 = pdev;
2594 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2597 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
2598 pvt->pci_ras = pdev;
2600 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
2601 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
2602 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
2603 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
2605 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
2607 pvt->pci_tad[id] = pdev;
2608 saw_chan_mask |= 1 << id;
2611 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2612 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2613 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2614 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2616 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
2618 pvt->pci_tad[id] = pdev;
2619 saw_chan_mask |= 1 << id;
2622 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
2623 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2624 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2625 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2626 if (!pvt->pci_ddrio)
2627 pvt->pci_ddrio = pdev;
2629 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
2630 pvt->pci_ha1 = pdev;
2632 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
2633 pvt->pci_ha1_ta = pdev;
2639 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2641 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2645 /* Check if everything were registered */
2646 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2647 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2650 if (saw_chan_mask != 0x0f && /* -EN */
2651 saw_chan_mask != 0x33 && /* -EP */
2652 saw_chan_mask != 0xff) /* -EX */
2657 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2661 static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2662 struct sbridge_dev *sbridge_dev)
2664 struct sbridge_pvt *pvt = mci->pvt_info;
2665 struct pci_dev *pdev;
2666 u8 saw_chan_mask = 0;
2669 /* there's only one device per system; not tied to any bus */
2670 if (pvt->info.pci_vtd == NULL)
2671 /* result will be checked later */
2672 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2673 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2676 for (i = 0; i < sbridge_dev->n_devs; i++) {
2677 pdev = sbridge_dev->pdev[i];
2681 switch (pdev->device) {
2682 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2683 pvt->pci_sad0 = pdev;
2685 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2686 pvt->pci_sad1 = pdev;
2688 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2689 pvt->pci_ha0 = pdev;
2691 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2694 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
2695 pvt->pci_ras = pdev;
2697 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
2698 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
2699 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
2700 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
2702 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
2703 pvt->pci_tad[id] = pdev;
2704 saw_chan_mask |= 1 << id;
2707 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2708 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2709 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2710 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2712 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
2713 pvt->pci_tad[id] = pdev;
2714 saw_chan_mask |= 1 << id;
2717 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2718 pvt->pci_ddrio = pdev;
2720 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2721 pvt->pci_ha1 = pdev;
2723 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2724 pvt->pci_ha1_ta = pdev;
2730 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2732 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2736 /* Check if everything were registered */
2737 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2738 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2741 if (saw_chan_mask != 0x0f && /* -EN */
2742 saw_chan_mask != 0x33 && /* -EP */
2743 saw_chan_mask != 0xff) /* -EX */
2748 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2752 static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2753 struct sbridge_dev *sbridge_dev)
2755 struct sbridge_pvt *pvt = mci->pvt_info;
2756 struct pci_dev *pdev;
2762 for (i = 0; i < sbridge_dev->n_devs; i++) {
2763 pdev = sbridge_dev->pdev[i];
2767 /* Extract PCI device and function. */
2768 dev = (pdev->devfn >> 3) & 0x1f;
2769 func = pdev->devfn & 0x7;
2771 switch (pdev->device) {
2772 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2774 pvt->knl.pci_mc0 = pdev;
2776 pvt->knl.pci_mc1 = pdev;
2778 sbridge_printk(KERN_ERR,
2779 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2785 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2786 pvt->pci_sad0 = pdev;
2789 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2790 pvt->pci_sad1 = pdev;
2793 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2794 /* There are one of these per tile, and range from
2797 devidx = ((dev-14)*8)+func;
2799 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2800 sbridge_printk(KERN_ERR,
2801 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2806 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2808 pvt->knl.pci_cha[devidx] = pdev;
2811 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL:
2815 * MC0 channels 0-2 are device 9 function 2-4,
2816 * MC1 channels 3-5 are device 8 function 2-4.
2822 devidx = 3 + (func-2);
2824 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2825 sbridge_printk(KERN_ERR,
2826 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2831 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2832 pvt->knl.pci_channel[devidx] = pdev;
2835 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2836 pvt->knl.pci_mc_info = pdev;
2839 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2844 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2850 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2851 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2856 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2857 if (!pvt->knl.pci_channel[i]) {
2858 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2863 for (i = 0; i < KNL_MAX_CHAS; i++) {
2864 if (!pvt->knl.pci_cha[i]) {
2865 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2873 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2877 /****************************************************************************
2878 Error check routines
2879 ****************************************************************************/
2882 * While Sandy Bridge has error count registers, SMI BIOS read values from
2883 * and resets the counters. So, they are not reliable for the OS to read
2884 * from them. So, we have no option but to just trust on whatever MCE is
2885 * telling us about the errors.
2887 static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2888 const struct mce *m)
2890 struct mem_ctl_info *new_mci;
2891 struct sbridge_pvt *pvt = mci->pvt_info;
2892 enum hw_event_mc_err_type tp_event;
2893 char *type, *optype, msg[256];
2894 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2895 bool overflow = GET_BITFIELD(m->status, 62, 62);
2896 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
2898 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2899 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2900 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2901 u32 channel = GET_BITFIELD(m->status, 0, 3);
2902 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2903 long channel_mask, first_channel;
2904 u8 rank, socket, ha;
2906 char *area_type = NULL;
2908 if (pvt->info.type != SANDY_BRIDGE)
2911 recoverable = GET_BITFIELD(m->status, 56, 56);
2913 if (uncorrected_error) {
2916 tp_event = HW_EVENT_ERR_FATAL;
2919 tp_event = HW_EVENT_ERR_UNCORRECTED;
2923 tp_event = HW_EVENT_ERR_CORRECTED;
2927 * According with Table 15-9 of the Intel Architecture spec vol 3A,
2928 * memory errors should fit in this mask:
2929 * 000f 0000 1mmm cccc (binary)
2931 * f = Correction Report Filtering Bit. If 1, subsequent errors
2935 * If the mask doesn't match, report an error to the parsing logic
2937 if (! ((errcode & 0xef80) == 0x80)) {
2938 optype = "Can't parse: it is not a mem";
2940 switch (optypenum) {
2942 optype = "generic undef request error";
2945 optype = "memory read error";
2948 optype = "memory write error";
2951 optype = "addr/cmd error";
2954 optype = "memory scrubbing error";
2957 optype = "reserved";
2962 /* Only decode errors with an valid address (ADDRV) */
2963 if (!GET_BITFIELD(m->status, 58, 58))
2966 if (pvt->info.type == KNIGHTS_LANDING) {
2967 if (channel == 14) {
2968 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2969 overflow ? " OVERFLOW" : "",
2970 (uncorrected_error && recoverable)
2971 ? " recoverable" : "",
2977 channel = knl_channel_remap(channel);
2978 channel_mask = 1 << channel;
2979 snprintf(msg, sizeof(msg),
2980 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
2981 overflow ? " OVERFLOW" : "",
2982 (uncorrected_error && recoverable)
2983 ? " recoverable" : " ",
2984 mscod, errcode, channel, A + channel);
2985 edac_mc_handle_error(tp_event, mci, core_err_cnt,
2986 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
2992 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
2993 &channel_mask, &rank, &area_type, msg);
2998 new_mci = get_mci_for_node_id(socket);
3000 strcpy(msg, "Error: socket got corrupted!");
3004 pvt = mci->pvt_info;
3006 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
3017 * FIXME: On some memory configurations (mirror, lockstep), the
3018 * Memory Controller can't point the error to a single DIMM. The
3019 * EDAC core should be handling the channel mask, in order to point
3020 * to the group of dimm's where the error may be happening.
3022 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
3023 channel = first_channel;
3025 snprintf(msg, sizeof(msg),
3026 "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
3027 overflow ? " OVERFLOW" : "",
3028 (uncorrected_error && recoverable) ? " recoverable" : "",
3035 edac_dbg(0, "%s\n", msg);
3037 /* FIXME: need support for channel mask */
3039 if (channel == CHANNEL_UNSPECIFIED)
3042 /* Call the helper to output message */
3043 edac_mc_handle_error(tp_event, mci, core_err_cnt,
3044 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
3045 4*ha+channel, dimm, -1,
3049 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
3056 * Check that logging is enabled and that this is the right type
3057 * of error for us to handle.
3059 static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3062 struct mce *mce = (struct mce *)data;
3063 struct mem_ctl_info *mci;
3064 struct sbridge_pvt *pvt;
3067 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3070 mci = get_mci_for_node_id(mce->socketid);
3073 pvt = mci->pvt_info;
3076 * Just let mcelog handle it if the error is
3077 * outside the memory controller. A memory error
3078 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3079 * bit 12 has an special meaning.
3081 if ((mce->status & 0xefff) >> 7 != 1)
3084 if (mce->mcgstatus & MCG_STATUS_MCIP)
3089 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
3091 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3092 "Bank %d: %016Lx\n", mce->extcpu, type,
3093 mce->mcgstatus, mce->bank, mce->status);
3094 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3095 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3096 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
3098 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3099 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3100 mce->time, mce->socketid, mce->apicid);
3102 sbridge_mce_output_error(mci, mce);
3104 /* Advice mcelog that the error were handled */
3108 static struct notifier_block sbridge_mce_dec = {
3109 .notifier_call = sbridge_mce_check_error,
3112 /****************************************************************************
3113 EDAC register/unregister logic
3114 ****************************************************************************/
3116 static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3118 struct mem_ctl_info *mci = sbridge_dev->mci;
3119 struct sbridge_pvt *pvt;
3121 if (unlikely(!mci || !mci->pvt_info)) {
3122 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
3124 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3128 pvt = mci->pvt_info;
3130 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3131 mci, &sbridge_dev->pdev[0]->dev);
3133 /* Remove MC sysfs nodes */
3134 edac_mc_del_mc(mci->pdev);
3136 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
3137 kfree(mci->ctl_name);
3139 sbridge_dev->mci = NULL;
3142 static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
3144 struct mem_ctl_info *mci;
3145 struct edac_mc_layer layers[2];
3146 struct sbridge_pvt *pvt;
3147 struct pci_dev *pdev = sbridge_dev->pdev[0];
3150 /* Check the number of active and not disabled channels */
3151 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
3152 if (unlikely(rc < 0))
3155 /* allocate a new MC control structure */
3156 layers[0].type = EDAC_MC_LAYER_CHANNEL;
3157 layers[0].size = type == KNIGHTS_LANDING ?
3158 KNL_MAX_CHANNELS : NUM_CHANNELS;
3159 layers[0].is_virt_csrow = false;
3160 layers[1].type = EDAC_MC_LAYER_SLOT;
3161 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
3162 layers[1].is_virt_csrow = true;
3163 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
3169 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3172 pvt = mci->pvt_info;
3173 memset(pvt, 0, sizeof(*pvt));
3175 /* Associate sbridge_dev and mci for future usage */
3176 pvt->sbridge_dev = sbridge_dev;
3177 sbridge_dev->mci = mci;
3179 mci->mtype_cap = type == KNIGHTS_LANDING ?
3180 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
3181 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3182 mci->edac_cap = EDAC_FLAG_NONE;
3183 mci->mod_name = "sbridge_edac.c";
3184 mci->mod_ver = SBRIDGE_REVISION;
3185 mci->dev_name = pci_name(pdev);
3186 mci->ctl_page_to_phys = NULL;
3188 pvt->info.type = type;
3191 pvt->info.rankcfgr = IB_RANK_CFG_A;
3192 pvt->info.get_tolm = ibridge_get_tolm;
3193 pvt->info.get_tohm = ibridge_get_tohm;
3194 pvt->info.dram_rule = ibridge_dram_rule;
3195 pvt->info.get_memory_type = get_memory_type;
3196 pvt->info.get_node_id = get_node_id;
3197 pvt->info.rir_limit = rir_limit;
3198 pvt->info.sad_limit = sad_limit;
3199 pvt->info.interleave_mode = interleave_mode;
3200 pvt->info.show_interleave_mode = show_interleave_mode;
3201 pvt->info.dram_attr = dram_attr;
3202 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3203 pvt->info.interleave_list = ibridge_interleave_list;
3204 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3205 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3206 pvt->info.get_width = ibridge_get_width;
3207 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
3209 /* Store pci devices at mci for faster access */
3210 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3211 if (unlikely(rc < 0))
3215 pvt->info.rankcfgr = SB_RANK_CFG_A;
3216 pvt->info.get_tolm = sbridge_get_tolm;
3217 pvt->info.get_tohm = sbridge_get_tohm;
3218 pvt->info.dram_rule = sbridge_dram_rule;
3219 pvt->info.get_memory_type = get_memory_type;
3220 pvt->info.get_node_id = get_node_id;
3221 pvt->info.rir_limit = rir_limit;
3222 pvt->info.sad_limit = sad_limit;
3223 pvt->info.interleave_mode = interleave_mode;
3224 pvt->info.show_interleave_mode = show_interleave_mode;
3225 pvt->info.dram_attr = dram_attr;
3226 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3227 pvt->info.interleave_list = sbridge_interleave_list;
3228 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3229 pvt->info.interleave_pkg = sbridge_interleave_pkg;
3230 pvt->info.get_width = sbridge_get_width;
3231 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
3233 /* Store pci devices at mci for faster access */
3234 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3235 if (unlikely(rc < 0))
3239 /* rankcfgr isn't used */
3240 pvt->info.get_tolm = haswell_get_tolm;
3241 pvt->info.get_tohm = haswell_get_tohm;
3242 pvt->info.dram_rule = ibridge_dram_rule;
3243 pvt->info.get_memory_type = haswell_get_memory_type;
3244 pvt->info.get_node_id = haswell_get_node_id;
3245 pvt->info.rir_limit = haswell_rir_limit;
3246 pvt->info.sad_limit = sad_limit;
3247 pvt->info.interleave_mode = interleave_mode;
3248 pvt->info.show_interleave_mode = show_interleave_mode;
3249 pvt->info.dram_attr = dram_attr;
3250 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3251 pvt->info.interleave_list = ibridge_interleave_list;
3252 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3253 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3254 pvt->info.get_width = ibridge_get_width;
3255 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
3257 /* Store pci devices at mci for faster access */
3258 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3259 if (unlikely(rc < 0))
3263 /* rankcfgr isn't used */
3264 pvt->info.get_tolm = haswell_get_tolm;
3265 pvt->info.get_tohm = haswell_get_tohm;
3266 pvt->info.dram_rule = ibridge_dram_rule;
3267 pvt->info.get_memory_type = haswell_get_memory_type;
3268 pvt->info.get_node_id = haswell_get_node_id;
3269 pvt->info.rir_limit = haswell_rir_limit;
3270 pvt->info.sad_limit = sad_limit;
3271 pvt->info.interleave_mode = interleave_mode;
3272 pvt->info.show_interleave_mode = show_interleave_mode;
3273 pvt->info.dram_attr = dram_attr;
3274 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3275 pvt->info.interleave_list = ibridge_interleave_list;
3276 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3277 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3278 pvt->info.get_width = broadwell_get_width;
3279 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
3281 /* Store pci devices at mci for faster access */
3282 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3283 if (unlikely(rc < 0))
3286 case KNIGHTS_LANDING:
3287 /* pvt->info.rankcfgr == ??? */
3288 pvt->info.get_tolm = knl_get_tolm;
3289 pvt->info.get_tohm = knl_get_tohm;
3290 pvt->info.dram_rule = knl_dram_rule;
3291 pvt->info.get_memory_type = knl_get_memory_type;
3292 pvt->info.get_node_id = knl_get_node_id;
3293 pvt->info.rir_limit = NULL;
3294 pvt->info.sad_limit = knl_sad_limit;
3295 pvt->info.interleave_mode = knl_interleave_mode;
3296 pvt->info.show_interleave_mode = knl_show_interleave_mode;
3297 pvt->info.dram_attr = dram_attr_knl;
3298 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3299 pvt->info.interleave_list = knl_interleave_list;
3300 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3301 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3302 pvt->info.get_width = knl_get_width;
3303 mci->ctl_name = kasprintf(GFP_KERNEL,
3304 "Knights Landing Socket#%d", mci->mc_idx);
3306 rc = knl_mci_bind_devs(mci, sbridge_dev);
3307 if (unlikely(rc < 0))
3312 /* Get dimm basic config and the memory layout */
3313 get_dimm_config(mci);
3314 get_memory_layout(mci);
3316 /* record ptr to the generic device */
3317 mci->pdev = &pdev->dev;
3319 /* add this new MC control structure to EDAC's list of MCs */
3320 if (unlikely(edac_mc_add_mc(mci))) {
3321 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
3329 kfree(mci->ctl_name);
3331 sbridge_dev->mci = NULL;
3335 #define ICPU(model, table) \
3336 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3338 /* Order here must match "enum type" */
3339 static const struct x86_cpu_id sbridge_cpuids[] = {
3340 ICPU(0x2d, pci_dev_descr_sbridge_table), /* SANDY_BRIDGE */
3341 ICPU(0x3e, pci_dev_descr_ibridge_table), /* IVY_BRIDGE */
3342 ICPU(0x3f, pci_dev_descr_haswell_table), /* HASWELL */
3343 ICPU(0x4f, pci_dev_descr_broadwell_table), /* BROADWELL */
3344 ICPU(0x57, pci_dev_descr_knl_table), /* KNIGHTS_LANDING */
3347 MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3350 * sbridge_probe Get all devices and register memory controllers
3353 * 0 for FOUND a device
3354 * < 0 for error code
3357 static int sbridge_probe(const struct x86_cpu_id *id)
3361 struct sbridge_dev *sbridge_dev;
3362 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
3364 /* get the pci devices we want to reserve for our use */
3365 rc = sbridge_get_all_devices(&num_mc, ptable);
3367 if (unlikely(rc < 0)) {
3368 edac_dbg(0, "couldn't get all devices\n");
3374 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
3375 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3376 mc, mc + 1, num_mc);
3378 sbridge_dev->mc = mc++;
3379 rc = sbridge_register_mci(sbridge_dev, id - sbridge_cpuids);
3380 if (unlikely(rc < 0))
3384 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
3389 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3390 sbridge_unregister_mci(sbridge_dev);
3392 sbridge_put_all_devices();
3398 * sbridge_remove cleanup
3401 static void sbridge_remove(void)
3403 struct sbridge_dev *sbridge_dev;
3407 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3408 sbridge_unregister_mci(sbridge_dev);
3410 /* Release PCI resources */
3411 sbridge_put_all_devices();
3415 * sbridge_init Module entry function
3416 * Try to initialize this module for its devices
3418 static int __init sbridge_init(void)
3420 const struct x86_cpu_id *id;
3425 id = x86_match_cpu(sbridge_cpuids);
3429 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3432 rc = sbridge_probe(id);
3435 mce_register_decode_chain(&sbridge_mce_dec);
3436 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3437 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
3441 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
3448 * sbridge_exit() Module exit function
3449 * Unregister the driver
3451 static void __exit sbridge_exit(void)
3455 mce_unregister_decode_chain(&sbridge_mce_dec);
3458 module_init(sbridge_init);
3459 module_exit(sbridge_exit);
3461 module_param(edac_op_state, int, 0444);
3462 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3464 MODULE_LICENSE("GPL");
3465 MODULE_AUTHOR("Mauro Carvalho Chehab");
3466 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
3467 MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "