nvmem: rockchip-efuse: add optional property to get efuse size
[firefly-linux-kernel-4.4.55.git] / drivers / edac / sb_edac.c
1 /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
2  *
3  * This driver supports the memory controllers found on the Intel
4  * processor family Sandy Bridge.
5  *
6  * This file may be distributed under the terms of the
7  * GNU General Public License version 2 only.
8  *
9  * Copyright (c) 2011 by:
10  *       Mauro Carvalho Chehab
11  */
12
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 <asm/processor.h>
25 #include <asm/mce.h>
26
27 #include "edac_core.h"
28
29 /* Static vars */
30 static LIST_HEAD(sbridge_edac_list);
31 static DEFINE_MUTEX(sbridge_edac_lock);
32 static int probed;
33
34 /*
35  * Alter this version for the module when modifications are made
36  */
37 #define SBRIDGE_REVISION    " Ver: 1.1.1 "
38 #define EDAC_MOD_STR      "sbridge_edac"
39
40 /*
41  * Debug macros
42  */
43 #define sbridge_printk(level, fmt, arg...)                      \
44         edac_printk(level, "sbridge", fmt, ##arg)
45
46 #define sbridge_mc_printk(mci, level, fmt, arg...)              \
47         edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
48
49 /*
50  * Get a bit field at register value <v>, from bit <lo> to bit <hi>
51  */
52 #define GET_BITFIELD(v, lo, hi) \
53         (((v) & GENMASK_ULL(hi, lo)) >> (lo))
54
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,
59 };
60
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,
66 };
67
68 #define SAD_LIMIT(reg)          ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
69 #define DRAM_ATTR(reg)          GET_BITFIELD(reg, 2,  3)
70 #define INTERLEAVE_MODE(reg)    GET_BITFIELD(reg, 1,  1)
71 #define DRAM_RULE_ENABLE(reg)   GET_BITFIELD(reg, 0,  0)
72 #define A7MODE(reg)             GET_BITFIELD(reg, 26, 26)
73
74 static char *get_dram_attr(u32 reg)
75 {
76         switch(DRAM_ATTR(reg)) {
77                 case 0:
78                         return "DRAM";
79                 case 1:
80                         return "MMCFG";
81                 case 2:
82                         return "NXM";
83                 default:
84                         return "unknown";
85         }
86 }
87
88 static const u32 sbridge_interleave_list[] = {
89         0x84, 0x8c, 0x94, 0x9c, 0xa4,
90         0xac, 0xb4, 0xbc, 0xc4, 0xcc,
91 };
92
93 static const u32 ibridge_interleave_list[] = {
94         0x64, 0x6c, 0x74, 0x7c, 0x84,
95         0x8c, 0x94, 0x9c, 0xa4, 0xac,
96         0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
97         0xdc, 0xe4, 0xec, 0xf4, 0xfc,
98 };
99
100 struct interleave_pkg {
101         unsigned char start;
102         unsigned char end;
103 };
104
105 static const struct interleave_pkg sbridge_interleave_pkg[] = {
106         { 0, 2 },
107         { 3, 5 },
108         { 8, 10 },
109         { 11, 13 },
110         { 16, 18 },
111         { 19, 21 },
112         { 24, 26 },
113         { 27, 29 },
114 };
115
116 static const struct interleave_pkg ibridge_interleave_pkg[] = {
117         { 0, 3 },
118         { 4, 7 },
119         { 8, 11 },
120         { 12, 15 },
121         { 16, 19 },
122         { 20, 23 },
123         { 24, 27 },
124         { 28, 31 },
125 };
126
127 static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
128                           int interleave)
129 {
130         return GET_BITFIELD(reg, table[interleave].start,
131                             table[interleave].end);
132 }
133
134 /* Devices 12 Function 7 */
135
136 #define TOLM            0x80
137 #define TOHM            0x84
138 #define HASWELL_TOLM    0xd0
139 #define HASWELL_TOHM_0  0xd4
140 #define HASWELL_TOHM_1  0xd8
141
142 #define GET_TOLM(reg)           ((GET_BITFIELD(reg, 0,  3) << 28) | 0x3ffffff)
143 #define GET_TOHM(reg)           ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
144
145 /* Device 13 Function 6 */
146
147 #define SAD_TARGET      0xf0
148
149 #define SOURCE_ID(reg)          GET_BITFIELD(reg, 9, 11)
150
151 #define SAD_CONTROL     0xf4
152
153 /* Device 14 function 0 */
154
155 static const u32 tad_dram_rule[] = {
156         0x40, 0x44, 0x48, 0x4c,
157         0x50, 0x54, 0x58, 0x5c,
158         0x60, 0x64, 0x68, 0x6c,
159 };
160 #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
161
162 #define TAD_LIMIT(reg)          ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
163 #define TAD_SOCK(reg)           GET_BITFIELD(reg, 10, 11)
164 #define TAD_CH(reg)             GET_BITFIELD(reg,  8,  9)
165 #define TAD_TGT3(reg)           GET_BITFIELD(reg,  6,  7)
166 #define TAD_TGT2(reg)           GET_BITFIELD(reg,  4,  5)
167 #define TAD_TGT1(reg)           GET_BITFIELD(reg,  2,  3)
168 #define TAD_TGT0(reg)           GET_BITFIELD(reg,  0,  1)
169
170 /* Device 15, function 0 */
171
172 #define MCMTR                   0x7c
173
174 #define IS_ECC_ENABLED(mcmtr)           GET_BITFIELD(mcmtr, 2, 2)
175 #define IS_LOCKSTEP_ENABLED(mcmtr)      GET_BITFIELD(mcmtr, 1, 1)
176 #define IS_CLOSE_PG(mcmtr)              GET_BITFIELD(mcmtr, 0, 0)
177
178 /* Device 15, function 1 */
179
180 #define RASENABLES              0xac
181 #define IS_MIRROR_ENABLED(reg)          GET_BITFIELD(reg, 0, 0)
182
183 /* Device 15, functions 2-5 */
184
185 static const int mtr_regs[] = {
186         0x80, 0x84, 0x88,
187 };
188
189 #define RANK_DISABLE(mtr)               GET_BITFIELD(mtr, 16, 19)
190 #define IS_DIMM_PRESENT(mtr)            GET_BITFIELD(mtr, 14, 14)
191 #define RANK_CNT_BITS(mtr)              GET_BITFIELD(mtr, 12, 13)
192 #define RANK_WIDTH_BITS(mtr)            GET_BITFIELD(mtr, 2, 4)
193 #define COL_WIDTH_BITS(mtr)             GET_BITFIELD(mtr, 0, 1)
194
195 static const u32 tad_ch_nilv_offset[] = {
196         0x90, 0x94, 0x98, 0x9c,
197         0xa0, 0xa4, 0xa8, 0xac,
198         0xb0, 0xb4, 0xb8, 0xbc,
199 };
200 #define CHN_IDX_OFFSET(reg)             GET_BITFIELD(reg, 28, 29)
201 #define TAD_OFFSET(reg)                 (GET_BITFIELD(reg,  6, 25) << 26)
202
203 static const u32 rir_way_limit[] = {
204         0x108, 0x10c, 0x110, 0x114, 0x118,
205 };
206 #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
207
208 #define IS_RIR_VALID(reg)       GET_BITFIELD(reg, 31, 31)
209 #define RIR_WAY(reg)            GET_BITFIELD(reg, 28, 29)
210
211 #define MAX_RIR_WAY     8
212
213 static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
214         { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
215         { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
216         { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
217         { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
218         { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
219 };
220
221 #define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
222         GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
223
224 #define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
225         GET_BITFIELD(reg,  2, 15) : GET_BITFIELD(reg,  2, 14))
226
227 /* Device 16, functions 2-7 */
228
229 /*
230  * FIXME: Implement the error count reads directly
231  */
232
233 static const u32 correrrcnt[] = {
234         0x104, 0x108, 0x10c, 0x110,
235 };
236
237 #define RANK_ODD_OV(reg)                GET_BITFIELD(reg, 31, 31)
238 #define RANK_ODD_ERR_CNT(reg)           GET_BITFIELD(reg, 16, 30)
239 #define RANK_EVEN_OV(reg)               GET_BITFIELD(reg, 15, 15)
240 #define RANK_EVEN_ERR_CNT(reg)          GET_BITFIELD(reg,  0, 14)
241
242 static const u32 correrrthrsld[] = {
243         0x11c, 0x120, 0x124, 0x128,
244 };
245
246 #define RANK_ODD_ERR_THRSLD(reg)        GET_BITFIELD(reg, 16, 30)
247 #define RANK_EVEN_ERR_THRSLD(reg)       GET_BITFIELD(reg,  0, 14)
248
249
250 /* Device 17, function 0 */
251
252 #define SB_RANK_CFG_A           0x0328
253
254 #define IB_RANK_CFG_A           0x0320
255
256 /*
257  * sbridge structs
258  */
259
260 #define NUM_CHANNELS            8       /* 2MC per socket, four chan per MC */
261 #define MAX_DIMMS               3       /* Max DIMMS per channel */
262 #define CHANNEL_UNSPECIFIED     0xf     /* Intel IA32 SDM 15-14 */
263
264 enum type {
265         SANDY_BRIDGE,
266         IVY_BRIDGE,
267         HASWELL,
268         BROADWELL,
269 };
270
271 struct sbridge_pvt;
272 struct sbridge_info {
273         enum type       type;
274         u32             mcmtr;
275         u32             rankcfgr;
276         u64             (*get_tolm)(struct sbridge_pvt *pvt);
277         u64             (*get_tohm)(struct sbridge_pvt *pvt);
278         u64             (*rir_limit)(u32 reg);
279         const u32       *dram_rule;
280         const u32       *interleave_list;
281         const struct interleave_pkg *interleave_pkg;
282         u8              max_sad;
283         u8              max_interleave;
284         u8              (*get_node_id)(struct sbridge_pvt *pvt);
285         enum mem_type   (*get_memory_type)(struct sbridge_pvt *pvt);
286         enum dev_type   (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
287         struct pci_dev  *pci_vtd;
288 };
289
290 struct sbridge_channel {
291         u32             ranks;
292         u32             dimms;
293 };
294
295 struct pci_id_descr {
296         int                     dev_id;
297         int                     optional;
298 };
299
300 struct pci_id_table {
301         const struct pci_id_descr       *descr;
302         int                             n_devs;
303 };
304
305 struct sbridge_dev {
306         struct list_head        list;
307         u8                      bus, mc;
308         u8                      node_id, source_id;
309         struct pci_dev          **pdev;
310         int                     n_devs;
311         struct mem_ctl_info     *mci;
312 };
313
314 struct sbridge_pvt {
315         struct pci_dev          *pci_ta, *pci_ddrio, *pci_ras;
316         struct pci_dev          *pci_sad0, *pci_sad1;
317         struct pci_dev          *pci_ha0, *pci_ha1;
318         struct pci_dev          *pci_br0, *pci_br1;
319         struct pci_dev          *pci_ha1_ta;
320         struct pci_dev          *pci_tad[NUM_CHANNELS];
321
322         struct sbridge_dev      *sbridge_dev;
323
324         struct sbridge_info     info;
325         struct sbridge_channel  channel[NUM_CHANNELS];
326
327         /* Memory type detection */
328         bool                    is_mirrored, is_lockstep, is_close_pg;
329
330         /* Fifo double buffers */
331         struct mce              mce_entry[MCE_LOG_LEN];
332         struct mce              mce_outentry[MCE_LOG_LEN];
333
334         /* Fifo in/out counters */
335         unsigned                mce_in, mce_out;
336
337         /* Count indicator to show errors not got */
338         unsigned                mce_overrun;
339
340         /* Memory description */
341         u64                     tolm, tohm;
342 };
343
344 #define PCI_DESCR(device_id, opt)       \
345         .dev_id = (device_id),          \
346         .optional = opt
347
348 static const struct pci_id_descr pci_dev_descr_sbridge[] = {
349                 /* Processor Home Agent */
350         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0)     },
351
352                 /* Memory controller */
353         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0)      },
354         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0)     },
355         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0)    },
356         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0)    },
357         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0)    },
358         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0)    },
359         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1)   },
360
361                 /* System Address Decoder */
362         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0)        },
363         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0)        },
364
365                 /* Broadcast Registers */
366         { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0)          },
367 };
368
369 #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
370 static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
371         PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
372         {0,}                    /* 0 terminated list. */
373 };
374
375 /* This changes depending if 1HA or 2HA:
376  * 1HA:
377  *      0x0eb8 (17.0) is DDRIO0
378  * 2HA:
379  *      0x0ebc (17.4) is DDRIO0
380  */
381 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0      0x0eb8
382 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0      0x0ebc
383
384 /* pci ids */
385 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0             0x0ea0
386 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA          0x0ea8
387 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS         0x0e71
388 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0        0x0eaa
389 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1        0x0eab
390 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2        0x0eac
391 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3        0x0ead
392 #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD                 0x0ec8
393 #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0                 0x0ec9
394 #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1                 0x0eca
395 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1             0x0e60
396 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA          0x0e68
397 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS         0x0e79
398 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0        0x0e6a
399 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1        0x0e6b
400 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2        0x0e6c
401 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3        0x0e6d
402
403 static const struct pci_id_descr pci_dev_descr_ibridge[] = {
404                 /* Processor Home Agent */
405         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0)             },
406
407                 /* Memory controller */
408         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0)          },
409         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0)         },
410         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0)        },
411         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0)        },
412         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0)        },
413         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0)        },
414
415                 /* System Address Decoder */
416         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0)                 },
417
418                 /* Broadcast Registers */
419         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1)                 },
420         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0)                 },
421
422                 /* Optional, mode 2HA */
423         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1)             },
424 #if 0
425         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1)  },
426         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
427 #endif
428         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1)        },
429         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1)        },
430         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1)        },
431         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1)        },
432
433         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1)      },
434         { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1)      },
435 };
436
437 static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
438         PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
439         {0,}                    /* 0 terminated list. */
440 };
441
442 /* Haswell support */
443 /* EN processor:
444  *      - 1 IMC
445  *      - 3 DDR3 channels, 2 DPC per channel
446  * EP processor:
447  *      - 1 or 2 IMC
448  *      - 4 DDR4 channels, 3 DPC per channel
449  * EP 4S processor:
450  *      - 2 IMC
451  *      - 4 DDR4 channels, 3 DPC per channel
452  * EX processor:
453  *      - 2 IMC
454  *      - each IMC interfaces with a SMI 2 channel
455  *      - each SMI channel interfaces with a scalable memory buffer
456  *      - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
457  */
458 #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
459 #define HASWELL_HASYSDEFEATURE2 0x84
460 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
461 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0     0x2fa0
462 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1     0x2f60
463 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA  0x2fa8
464 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
465 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA  0x2f68
466 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
467 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
468 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
469 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
470 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
471 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
472 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
473 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
474 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
475 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
476 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
477 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
478 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
479 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
480 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
481 static const struct pci_id_descr pci_dev_descr_haswell[] = {
482         /* first item must be the HA */
483         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0)             },
484
485         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0)        },
486         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0)        },
487
488         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1)             },
489
490         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0)          },
491         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0)     },
492         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0)        },
493         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0)        },
494         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1)        },
495         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1)        },
496
497         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1)          },
498         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1)          },
499         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1)          },
500         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1)          },
501
502         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1)          },
503         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1)     },
504         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1)        },
505         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1)        },
506         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1)        },
507         { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1)        },
508 };
509
510 static const struct pci_id_table pci_dev_descr_haswell_table[] = {
511         PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
512         {0,}                    /* 0 terminated list. */
513 };
514
515 /*
516  * Broadwell support
517  *
518  * DE processor:
519  *      - 1 IMC
520  *      - 2 DDR3 channels, 2 DPC per channel
521  * EP processor:
522  *      - 1 or 2 IMC
523  *      - 4 DDR4 channels, 3 DPC per channel
524  * EP 4S processor:
525  *      - 2 IMC
526  *      - 4 DDR4 channels, 3 DPC per channel
527  * EX processor:
528  *      - 2 IMC
529  *      - each IMC interfaces with a SMI 2 channel
530  *      - each SMI channel interfaces with a scalable memory buffer
531  *      - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
532  */
533 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
534 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0   0x6fa0
535 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1   0x6f60
536 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA        0x6fa8
537 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
538 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA        0x6f68
539 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
540 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
541 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
542 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
543 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
544 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
545 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
546 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
547 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
548 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
549 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
550 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
551
552 static const struct pci_id_descr pci_dev_descr_broadwell[] = {
553         /* first item must be the HA */
554         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0)           },
555
556         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0)      },
557         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0)      },
558
559         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1)           },
560
561         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0)        },
562         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0)   },
563         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0)      },
564         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0)      },
565         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1)      },
566         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1)      },
567
568         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1)        },
569
570         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1)        },
571         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1)   },
572         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1)      },
573         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1)      },
574         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1)      },
575         { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1)      },
576 };
577
578 static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
579         PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
580         {0,}                    /* 0 terminated list. */
581 };
582
583 /*
584  *      pci_device_id   table for which devices we are looking for
585  */
586 static const struct pci_device_id sbridge_pci_tbl[] = {
587         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0)},
588         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
589         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0)},
590         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0)},
591         {0,}                    /* 0 terminated list. */
592 };
593
594
595 /****************************************************************************
596                         Ancillary status routines
597  ****************************************************************************/
598
599 static inline int numrank(enum type type, u32 mtr)
600 {
601         int ranks = (1 << RANK_CNT_BITS(mtr));
602         int max = 4;
603
604         if (type == HASWELL || type == BROADWELL)
605                 max = 8;
606
607         if (ranks > max) {
608                 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
609                          ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
610                 return -EINVAL;
611         }
612
613         return ranks;
614 }
615
616 static inline int numrow(u32 mtr)
617 {
618         int rows = (RANK_WIDTH_BITS(mtr) + 12);
619
620         if (rows < 13 || rows > 18) {
621                 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
622                          rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
623                 return -EINVAL;
624         }
625
626         return 1 << rows;
627 }
628
629 static inline int numcol(u32 mtr)
630 {
631         int cols = (COL_WIDTH_BITS(mtr) + 10);
632
633         if (cols > 12) {
634                 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
635                          cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
636                 return -EINVAL;
637         }
638
639         return 1 << cols;
640 }
641
642 static struct sbridge_dev *get_sbridge_dev(u8 bus)
643 {
644         struct sbridge_dev *sbridge_dev;
645
646         list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
647                 if (sbridge_dev->bus == bus)
648                         return sbridge_dev;
649         }
650
651         return NULL;
652 }
653
654 static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
655                                            const struct pci_id_table *table)
656 {
657         struct sbridge_dev *sbridge_dev;
658
659         sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
660         if (!sbridge_dev)
661                 return NULL;
662
663         sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
664                                    GFP_KERNEL);
665         if (!sbridge_dev->pdev) {
666                 kfree(sbridge_dev);
667                 return NULL;
668         }
669
670         sbridge_dev->bus = bus;
671         sbridge_dev->n_devs = table->n_devs;
672         list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
673
674         return sbridge_dev;
675 }
676
677 static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
678 {
679         list_del(&sbridge_dev->list);
680         kfree(sbridge_dev->pdev);
681         kfree(sbridge_dev);
682 }
683
684 static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
685 {
686         u32 reg;
687
688         /* Address range is 32:28 */
689         pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
690         return GET_TOLM(reg);
691 }
692
693 static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
694 {
695         u32 reg;
696
697         pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
698         return GET_TOHM(reg);
699 }
700
701 static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
702 {
703         u32 reg;
704
705         pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
706
707         return GET_TOLM(reg);
708 }
709
710 static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
711 {
712         u32 reg;
713
714         pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
715
716         return GET_TOHM(reg);
717 }
718
719 static u64 rir_limit(u32 reg)
720 {
721         return ((u64)GET_BITFIELD(reg,  1, 10) << 29) | 0x1fffffff;
722 }
723
724 static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
725 {
726         u32 reg;
727         enum mem_type mtype;
728
729         if (pvt->pci_ddrio) {
730                 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
731                                       &reg);
732                 if (GET_BITFIELD(reg, 11, 11))
733                         /* FIXME: Can also be LRDIMM */
734                         mtype = MEM_RDDR3;
735                 else
736                         mtype = MEM_DDR3;
737         } else
738                 mtype = MEM_UNKNOWN;
739
740         return mtype;
741 }
742
743 static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
744 {
745         u32 reg;
746         bool registered = false;
747         enum mem_type mtype = MEM_UNKNOWN;
748
749         if (!pvt->pci_ddrio)
750                 goto out;
751
752         pci_read_config_dword(pvt->pci_ddrio,
753                               HASWELL_DDRCRCLKCONTROLS, &reg);
754         /* Is_Rdimm */
755         if (GET_BITFIELD(reg, 16, 16))
756                 registered = true;
757
758         pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
759         if (GET_BITFIELD(reg, 14, 14)) {
760                 if (registered)
761                         mtype = MEM_RDDR4;
762                 else
763                         mtype = MEM_DDR4;
764         } else {
765                 if (registered)
766                         mtype = MEM_RDDR3;
767                 else
768                         mtype = MEM_DDR3;
769         }
770
771 out:
772         return mtype;
773 }
774
775 static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
776 {
777         /* there's no way to figure out */
778         return DEV_UNKNOWN;
779 }
780
781 static enum dev_type __ibridge_get_width(u32 mtr)
782 {
783         enum dev_type type;
784
785         switch (mtr) {
786         case 3:
787                 type = DEV_UNKNOWN;
788                 break;
789         case 2:
790                 type = DEV_X16;
791                 break;
792         case 1:
793                 type = DEV_X8;
794                 break;
795         case 0:
796                 type = DEV_X4;
797                 break;
798         }
799
800         return type;
801 }
802
803 static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
804 {
805         /*
806          * ddr3_width on the documentation but also valid for DDR4 on
807          * Haswell
808          */
809         return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
810 }
811
812 static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
813 {
814         /* ddr3_width on the documentation but also valid for DDR4 */
815         return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
816 }
817
818 static u8 get_node_id(struct sbridge_pvt *pvt)
819 {
820         u32 reg;
821         pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
822         return GET_BITFIELD(reg, 0, 2);
823 }
824
825 static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
826 {
827         u32 reg;
828
829         pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
830         return GET_BITFIELD(reg, 0, 3);
831 }
832
833 static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
834 {
835         u32 reg;
836
837         pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
838         return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
839 }
840
841 static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
842 {
843         u64 rc;
844         u32 reg;
845
846         pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
847         rc = GET_BITFIELD(reg, 26, 31);
848         pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
849         rc = ((reg << 6) | rc) << 26;
850
851         return rc | 0x1ffffff;
852 }
853
854 static u64 haswell_rir_limit(u32 reg)
855 {
856         return (((u64)GET_BITFIELD(reg,  1, 11) + 1) << 29) - 1;
857 }
858
859 static inline u8 sad_pkg_socket(u8 pkg)
860 {
861         /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
862         return ((pkg >> 3) << 2) | (pkg & 0x3);
863 }
864
865 static inline u8 sad_pkg_ha(u8 pkg)
866 {
867         return (pkg >> 2) & 0x1;
868 }
869
870 /****************************************************************************
871                         Memory check routines
872  ****************************************************************************/
873 static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
874 {
875         struct pci_dev *pdev = NULL;
876
877         do {
878                 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
879                 if (pdev && pdev->bus->number == bus)
880                         break;
881         } while (pdev);
882
883         return pdev;
884 }
885
886 /**
887  * check_if_ecc_is_active() - Checks if ECC is active
888  * @bus:        Device bus
889  * @type:       Memory controller type
890  * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
891  *          disabled
892  */
893 static int check_if_ecc_is_active(const u8 bus, enum type type)
894 {
895         struct pci_dev *pdev = NULL;
896         u32 mcmtr, id;
897
898         switch (type) {
899         case IVY_BRIDGE:
900                 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
901                 break;
902         case HASWELL:
903                 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
904                 break;
905         case SANDY_BRIDGE:
906                 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
907                 break;
908         case BROADWELL:
909                 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
910                 break;
911         default:
912                 return -ENODEV;
913         }
914
915         pdev = get_pdev_same_bus(bus, id);
916         if (!pdev) {
917                 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
918                                         "%04x:%04x! on bus %02d\n",
919                                         PCI_VENDOR_ID_INTEL, id, bus);
920                 return -ENODEV;
921         }
922
923         pci_read_config_dword(pdev, MCMTR, &mcmtr);
924         if (!IS_ECC_ENABLED(mcmtr)) {
925                 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
926                 return -ENODEV;
927         }
928         return 0;
929 }
930
931 static int get_dimm_config(struct mem_ctl_info *mci)
932 {
933         struct sbridge_pvt *pvt = mci->pvt_info;
934         struct dimm_info *dimm;
935         unsigned i, j, banks, ranks, rows, cols, npages;
936         u64 size;
937         u32 reg;
938         enum edac_type mode;
939         enum mem_type mtype;
940
941         if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL)
942                 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
943         else
944                 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
945
946         pvt->sbridge_dev->source_id = SOURCE_ID(reg);
947
948         pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
949         edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
950                  pvt->sbridge_dev->mc,
951                  pvt->sbridge_dev->node_id,
952                  pvt->sbridge_dev->source_id);
953
954         pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
955         if (IS_MIRROR_ENABLED(reg)) {
956                 edac_dbg(0, "Memory mirror is enabled\n");
957                 pvt->is_mirrored = true;
958         } else {
959                 edac_dbg(0, "Memory mirror is disabled\n");
960                 pvt->is_mirrored = false;
961         }
962
963         pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
964         if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
965                 edac_dbg(0, "Lockstep is enabled\n");
966                 mode = EDAC_S8ECD8ED;
967                 pvt->is_lockstep = true;
968         } else {
969                 edac_dbg(0, "Lockstep is disabled\n");
970                 mode = EDAC_S4ECD4ED;
971                 pvt->is_lockstep = false;
972         }
973         if (IS_CLOSE_PG(pvt->info.mcmtr)) {
974                 edac_dbg(0, "address map is on closed page mode\n");
975                 pvt->is_close_pg = true;
976         } else {
977                 edac_dbg(0, "address map is on open page mode\n");
978                 pvt->is_close_pg = false;
979         }
980
981         mtype = pvt->info.get_memory_type(pvt);
982         if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
983                 edac_dbg(0, "Memory is registered\n");
984         else if (mtype == MEM_UNKNOWN)
985                 edac_dbg(0, "Cannot determine memory type\n");
986         else
987                 edac_dbg(0, "Memory is unregistered\n");
988
989         if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
990                 banks = 16;
991         else
992                 banks = 8;
993
994         for (i = 0; i < NUM_CHANNELS; i++) {
995                 u32 mtr;
996
997                 if (!pvt->pci_tad[i])
998                         continue;
999                 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
1000                         dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1001                                        i, j, 0);
1002                         pci_read_config_dword(pvt->pci_tad[i],
1003                                               mtr_regs[j], &mtr);
1004                         edac_dbg(4, "Channel #%d  MTR%d = %x\n", i, j, mtr);
1005                         if (IS_DIMM_PRESENT(mtr)) {
1006                                 pvt->channel[i].dimms++;
1007
1008                                 ranks = numrank(pvt->info.type, mtr);
1009                                 rows = numrow(mtr);
1010                                 cols = numcol(mtr);
1011
1012                                 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
1013                                 npages = MiB_TO_PAGES(size);
1014
1015                                 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1016                                          pvt->sbridge_dev->mc, i/4, i%4, j,
1017                                          size, npages,
1018                                          banks, ranks, rows, cols);
1019
1020                                 dimm->nr_pages = npages;
1021                                 dimm->grain = 32;
1022                                 dimm->dtype = pvt->info.get_width(pvt, mtr);
1023                                 dimm->mtype = mtype;
1024                                 dimm->edac_mode = mode;
1025                                 snprintf(dimm->label, sizeof(dimm->label),
1026                                          "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1027                                          pvt->sbridge_dev->source_id, i/4, i%4, j);
1028                         }
1029                 }
1030         }
1031
1032         return 0;
1033 }
1034
1035 static void get_memory_layout(const struct mem_ctl_info *mci)
1036 {
1037         struct sbridge_pvt *pvt = mci->pvt_info;
1038         int i, j, k, n_sads, n_tads, sad_interl;
1039         u32 reg;
1040         u64 limit, prv = 0;
1041         u64 tmp_mb;
1042         u32 gb, mb;
1043         u32 rir_way;
1044
1045         /*
1046          * Step 1) Get TOLM/TOHM ranges
1047          */
1048
1049         pvt->tolm = pvt->info.get_tolm(pvt);
1050         tmp_mb = (1 + pvt->tolm) >> 20;
1051
1052         gb = div_u64_rem(tmp_mb, 1024, &mb);
1053         edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1054                 gb, (mb*1000)/1024, (u64)pvt->tolm);
1055
1056         /* Address range is already 45:25 */
1057         pvt->tohm = pvt->info.get_tohm(pvt);
1058         tmp_mb = (1 + pvt->tohm) >> 20;
1059
1060         gb = div_u64_rem(tmp_mb, 1024, &mb);
1061         edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1062                 gb, (mb*1000)/1024, (u64)pvt->tohm);
1063
1064         /*
1065          * Step 2) Get SAD range and SAD Interleave list
1066          * TAD registers contain the interleave wayness. However, it
1067          * seems simpler to just discover it indirectly, with the
1068          * algorithm bellow.
1069          */
1070         prv = 0;
1071         for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1072                 /* SAD_LIMIT Address range is 45:26 */
1073                 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1074                                       &reg);
1075                 limit = SAD_LIMIT(reg);
1076
1077                 if (!DRAM_RULE_ENABLE(reg))
1078                         continue;
1079
1080                 if (limit <= prv)
1081                         break;
1082
1083                 tmp_mb = (limit + 1) >> 20;
1084                 gb = div_u64_rem(tmp_mb, 1024, &mb);
1085                 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1086                          n_sads,
1087                          get_dram_attr(reg),
1088                          gb, (mb*1000)/1024,
1089                          ((u64)tmp_mb) << 20L,
1090                          INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
1091                          reg);
1092                 prv = limit;
1093
1094                 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1095                                       &reg);
1096                 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1097                 for (j = 0; j < 8; j++) {
1098                         u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1099                         if (j > 0 && sad_interl == pkg)
1100                                 break;
1101
1102                         edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
1103                                  n_sads, j, pkg);
1104                 }
1105         }
1106
1107         /*
1108          * Step 3) Get TAD range
1109          */
1110         prv = 0;
1111         for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1112                 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1113                                       &reg);
1114                 limit = TAD_LIMIT(reg);
1115                 if (limit <= prv)
1116                         break;
1117                 tmp_mb = (limit + 1) >> 20;
1118
1119                 gb = div_u64_rem(tmp_mb, 1024, &mb);
1120                 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",
1121                          n_tads, gb, (mb*1000)/1024,
1122                          ((u64)tmp_mb) << 20L,
1123                          (u32)(1 << TAD_SOCK(reg)),
1124                          (u32)TAD_CH(reg) + 1,
1125                          (u32)TAD_TGT0(reg),
1126                          (u32)TAD_TGT1(reg),
1127                          (u32)TAD_TGT2(reg),
1128                          (u32)TAD_TGT3(reg),
1129                          reg);
1130                 prv = limit;
1131         }
1132
1133         /*
1134          * Step 4) Get TAD offsets, per each channel
1135          */
1136         for (i = 0; i < NUM_CHANNELS; i++) {
1137                 if (!pvt->channel[i].dimms)
1138                         continue;
1139                 for (j = 0; j < n_tads; j++) {
1140                         pci_read_config_dword(pvt->pci_tad[i],
1141                                               tad_ch_nilv_offset[j],
1142                                               &reg);
1143                         tmp_mb = TAD_OFFSET(reg) >> 20;
1144                         gb = div_u64_rem(tmp_mb, 1024, &mb);
1145                         edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1146                                  i, j,
1147                                  gb, (mb*1000)/1024,
1148                                  ((u64)tmp_mb) << 20L,
1149                                  reg);
1150                 }
1151         }
1152
1153         /*
1154          * Step 6) Get RIR Wayness/Limit, per each channel
1155          */
1156         for (i = 0; i < NUM_CHANNELS; i++) {
1157                 if (!pvt->channel[i].dimms)
1158                         continue;
1159                 for (j = 0; j < MAX_RIR_RANGES; j++) {
1160                         pci_read_config_dword(pvt->pci_tad[i],
1161                                               rir_way_limit[j],
1162                                               &reg);
1163
1164                         if (!IS_RIR_VALID(reg))
1165                                 continue;
1166
1167                         tmp_mb = pvt->info.rir_limit(reg) >> 20;
1168                         rir_way = 1 << RIR_WAY(reg);
1169                         gb = div_u64_rem(tmp_mb, 1024, &mb);
1170                         edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1171                                  i, j,
1172                                  gb, (mb*1000)/1024,
1173                                  ((u64)tmp_mb) << 20L,
1174                                  rir_way,
1175                                  reg);
1176
1177                         for (k = 0; k < rir_way; k++) {
1178                                 pci_read_config_dword(pvt->pci_tad[i],
1179                                                       rir_offset[j][k],
1180                                                       &reg);
1181                                 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
1182
1183                                 gb = div_u64_rem(tmp_mb, 1024, &mb);
1184                                 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1185                                          i, j, k,
1186                                          gb, (mb*1000)/1024,
1187                                          ((u64)tmp_mb) << 20L,
1188                                          (u32)RIR_RNK_TGT(pvt->info.type, reg),
1189                                          reg);
1190                         }
1191                 }
1192         }
1193 }
1194
1195 static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
1196 {
1197         struct sbridge_dev *sbridge_dev;
1198
1199         list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1200                 if (sbridge_dev->node_id == node_id)
1201                         return sbridge_dev->mci;
1202         }
1203         return NULL;
1204 }
1205
1206 static int get_memory_error_data(struct mem_ctl_info *mci,
1207                                  u64 addr,
1208                                  u8 *socket, u8 *ha,
1209                                  long *channel_mask,
1210                                  u8 *rank,
1211                                  char **area_type, char *msg)
1212 {
1213         struct mem_ctl_info     *new_mci;
1214         struct sbridge_pvt *pvt = mci->pvt_info;
1215         struct pci_dev          *pci_ha;
1216         int                     n_rir, n_sads, n_tads, sad_way, sck_xch;
1217         int                     sad_interl, idx, base_ch;
1218         int                     interleave_mode, shiftup = 0;
1219         unsigned                sad_interleave[pvt->info.max_interleave];
1220         u32                     reg, dram_rule;
1221         u8                      ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
1222         u32                     tad_offset;
1223         u32                     rir_way;
1224         u32                     mb, gb;
1225         u64                     ch_addr, offset, limit = 0, prv = 0;
1226
1227
1228         /*
1229          * Step 0) Check if the address is at special memory ranges
1230          * The check bellow is probably enough to fill all cases where
1231          * the error is not inside a memory, except for the legacy
1232          * range (e. g. VGA addresses). It is unlikely, however, that the
1233          * memory controller would generate an error on that range.
1234          */
1235         if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
1236                 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
1237                 return -EINVAL;
1238         }
1239         if (addr >= (u64)pvt->tohm) {
1240                 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
1241                 return -EINVAL;
1242         }
1243
1244         /*
1245          * Step 1) Get socket
1246          */
1247         for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1248                 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1249                                       &reg);
1250
1251                 if (!DRAM_RULE_ENABLE(reg))
1252                         continue;
1253
1254                 limit = SAD_LIMIT(reg);
1255                 if (limit <= prv) {
1256                         sprintf(msg, "Can't discover the memory socket");
1257                         return -EINVAL;
1258                 }
1259                 if  (addr <= limit)
1260                         break;
1261                 prv = limit;
1262         }
1263         if (n_sads == pvt->info.max_sad) {
1264                 sprintf(msg, "Can't discover the memory socket");
1265                 return -EINVAL;
1266         }
1267         dram_rule = reg;
1268         *area_type = get_dram_attr(dram_rule);
1269         interleave_mode = INTERLEAVE_MODE(dram_rule);
1270
1271         pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1272                               &reg);
1273
1274         if (pvt->info.type == SANDY_BRIDGE) {
1275                 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1276                 for (sad_way = 0; sad_way < 8; sad_way++) {
1277                         u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1278                         if (sad_way > 0 && sad_interl == pkg)
1279                                 break;
1280                         sad_interleave[sad_way] = pkg;
1281                         edac_dbg(0, "SAD interleave #%d: %d\n",
1282                                  sad_way, sad_interleave[sad_way]);
1283                 }
1284                 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1285                          pvt->sbridge_dev->mc,
1286                          n_sads,
1287                          addr,
1288                          limit,
1289                          sad_way + 7,
1290                          !interleave_mode ? "" : "XOR[18:16]");
1291                 if (interleave_mode)
1292                         idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1293                 else
1294                         idx = (addr >> 6) & 7;
1295                 switch (sad_way) {
1296                 case 1:
1297                         idx = 0;
1298                         break;
1299                 case 2:
1300                         idx = idx & 1;
1301                         break;
1302                 case 4:
1303                         idx = idx & 3;
1304                         break;
1305                 case 8:
1306                         break;
1307                 default:
1308                         sprintf(msg, "Can't discover socket interleave");
1309                         return -EINVAL;
1310                 }
1311                 *socket = sad_interleave[idx];
1312                 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1313                          idx, sad_way, *socket);
1314         } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1315                 int bits, a7mode = A7MODE(dram_rule);
1316
1317                 if (a7mode) {
1318                         /* A7 mode swaps P9 with P6 */
1319                         bits = GET_BITFIELD(addr, 7, 8) << 1;
1320                         bits |= GET_BITFIELD(addr, 9, 9);
1321                 } else
1322                         bits = GET_BITFIELD(addr, 6, 8);
1323
1324                 if (interleave_mode == 0) {
1325                         /* interleave mode will XOR {8,7,6} with {18,17,16} */
1326                         idx = GET_BITFIELD(addr, 16, 18);
1327                         idx ^= bits;
1328                 } else
1329                         idx = bits;
1330
1331                 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1332                 *socket = sad_pkg_socket(pkg);
1333                 sad_ha = sad_pkg_ha(pkg);
1334                 if (sad_ha)
1335                         ch_add = 4;
1336
1337                 if (a7mode) {
1338                         /* MCChanShiftUpEnable */
1339                         pci_read_config_dword(pvt->pci_ha0,
1340                                               HASWELL_HASYSDEFEATURE2, &reg);
1341                         shiftup = GET_BITFIELD(reg, 22, 22);
1342                 }
1343
1344                 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
1345                          idx, *socket, sad_ha, shiftup);
1346         } else {
1347                 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
1348                 idx = (addr >> 6) & 7;
1349                 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1350                 *socket = sad_pkg_socket(pkg);
1351                 sad_ha = sad_pkg_ha(pkg);
1352                 if (sad_ha)
1353                         ch_add = 4;
1354                 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1355                          idx, *socket, sad_ha);
1356         }
1357
1358         *ha = sad_ha;
1359
1360         /*
1361          * Move to the proper node structure, in order to access the
1362          * right PCI registers
1363          */
1364         new_mci = get_mci_for_node_id(*socket);
1365         if (!new_mci) {
1366                 sprintf(msg, "Struct for socket #%u wasn't initialized",
1367                         *socket);
1368                 return -EINVAL;
1369         }
1370         mci = new_mci;
1371         pvt = mci->pvt_info;
1372
1373         /*
1374          * Step 2) Get memory channel
1375          */
1376         prv = 0;
1377         if (pvt->info.type == SANDY_BRIDGE)
1378                 pci_ha = pvt->pci_ha0;
1379         else {
1380                 if (sad_ha)
1381                         pci_ha = pvt->pci_ha1;
1382                 else
1383                         pci_ha = pvt->pci_ha0;
1384         }
1385         for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1386                 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
1387                 limit = TAD_LIMIT(reg);
1388                 if (limit <= prv) {
1389                         sprintf(msg, "Can't discover the memory channel");
1390                         return -EINVAL;
1391                 }
1392                 if  (addr <= limit)
1393                         break;
1394                 prv = limit;
1395         }
1396         if (n_tads == MAX_TAD) {
1397                 sprintf(msg, "Can't discover the memory channel");
1398                 return -EINVAL;
1399         }
1400
1401         ch_way = TAD_CH(reg) + 1;
1402         sck_way = TAD_SOCK(reg);
1403
1404         if (ch_way == 3)
1405                 idx = addr >> 6;
1406         else
1407                 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
1408         idx = idx % ch_way;
1409
1410         /*
1411          * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1412          */
1413         switch (idx) {
1414         case 0:
1415                 base_ch = TAD_TGT0(reg);
1416                 break;
1417         case 1:
1418                 base_ch = TAD_TGT1(reg);
1419                 break;
1420         case 2:
1421                 base_ch = TAD_TGT2(reg);
1422                 break;
1423         case 3:
1424                 base_ch = TAD_TGT3(reg);
1425                 break;
1426         default:
1427                 sprintf(msg, "Can't discover the TAD target");
1428                 return -EINVAL;
1429         }
1430         *channel_mask = 1 << base_ch;
1431
1432         pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
1433                                 tad_ch_nilv_offset[n_tads],
1434                                 &tad_offset);
1435
1436         if (pvt->is_mirrored) {
1437                 *channel_mask |= 1 << ((base_ch + 2) % 4);
1438                 switch(ch_way) {
1439                 case 2:
1440                 case 4:
1441                         sck_xch = (1 << sck_way) * (ch_way >> 1);
1442                         break;
1443                 default:
1444                         sprintf(msg, "Invalid mirror set. Can't decode addr");
1445                         return -EINVAL;
1446                 }
1447         } else
1448                 sck_xch = (1 << sck_way) * ch_way;
1449
1450         if (pvt->is_lockstep)
1451                 *channel_mask |= 1 << ((base_ch + 1) % 4);
1452
1453         offset = TAD_OFFSET(tad_offset);
1454
1455         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",
1456                  n_tads,
1457                  addr,
1458                  limit,
1459                  sck_way,
1460                  ch_way,
1461                  offset,
1462                  idx,
1463                  base_ch,
1464                  *channel_mask);
1465
1466         /* Calculate channel address */
1467         /* Remove the TAD offset */
1468
1469         if (offset > addr) {
1470                 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1471                         offset, addr);
1472                 return -EINVAL;
1473         }
1474
1475         ch_addr = addr - offset;
1476         ch_addr >>= (6 + shiftup);
1477         ch_addr /= sck_xch;
1478         ch_addr <<= (6 + shiftup);
1479         ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
1480
1481         /*
1482          * Step 3) Decode rank
1483          */
1484         for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1485                 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
1486                                       rir_way_limit[n_rir],
1487                                       &reg);
1488
1489                 if (!IS_RIR_VALID(reg))
1490                         continue;
1491
1492                 limit = pvt->info.rir_limit(reg);
1493                 gb = div_u64_rem(limit >> 20, 1024, &mb);
1494                 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1495                          n_rir,
1496                          gb, (mb*1000)/1024,
1497                          limit,
1498                          1 << RIR_WAY(reg));
1499                 if  (ch_addr <= limit)
1500                         break;
1501         }
1502         if (n_rir == MAX_RIR_RANGES) {
1503                 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1504                         ch_addr);
1505                 return -EINVAL;
1506         }
1507         rir_way = RIR_WAY(reg);
1508
1509         if (pvt->is_close_pg)
1510                 idx = (ch_addr >> 6);
1511         else
1512                 idx = (ch_addr >> 13);  /* FIXME: Datasheet says to shift by 15 */
1513         idx %= 1 << rir_way;
1514
1515         pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
1516                               rir_offset[n_rir][idx],
1517                               &reg);
1518         *rank = RIR_RNK_TGT(pvt->info.type, reg);
1519
1520         edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1521                  n_rir,
1522                  ch_addr,
1523                  limit,
1524                  rir_way,
1525                  idx);
1526
1527         return 0;
1528 }
1529
1530 /****************************************************************************
1531         Device initialization routines: put/get, init/exit
1532  ****************************************************************************/
1533
1534 /*
1535  *      sbridge_put_all_devices 'put' all the devices that we have
1536  *                              reserved via 'get'
1537  */
1538 static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1539 {
1540         int i;
1541
1542         edac_dbg(0, "\n");
1543         for (i = 0; i < sbridge_dev->n_devs; i++) {
1544                 struct pci_dev *pdev = sbridge_dev->pdev[i];
1545                 if (!pdev)
1546                         continue;
1547                 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1548                          pdev->bus->number,
1549                          PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1550                 pci_dev_put(pdev);
1551         }
1552 }
1553
1554 static void sbridge_put_all_devices(void)
1555 {
1556         struct sbridge_dev *sbridge_dev, *tmp;
1557
1558         list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1559                 sbridge_put_devices(sbridge_dev);
1560                 free_sbridge_dev(sbridge_dev);
1561         }
1562 }
1563
1564 static int sbridge_get_onedevice(struct pci_dev **prev,
1565                                  u8 *num_mc,
1566                                  const struct pci_id_table *table,
1567                                  const unsigned devno)
1568 {
1569         struct sbridge_dev *sbridge_dev;
1570         const struct pci_id_descr *dev_descr = &table->descr[devno];
1571         struct pci_dev *pdev = NULL;
1572         u8 bus = 0;
1573
1574         sbridge_printk(KERN_DEBUG,
1575                 "Seeking for: PCI ID %04x:%04x\n",
1576                 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1577
1578         pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1579                               dev_descr->dev_id, *prev);
1580
1581         if (!pdev) {
1582                 if (*prev) {
1583                         *prev = pdev;
1584                         return 0;
1585                 }
1586
1587                 if (dev_descr->optional)
1588                         return 0;
1589
1590                 /* if the HA wasn't found */
1591                 if (devno == 0)
1592                         return -ENODEV;
1593
1594                 sbridge_printk(KERN_INFO,
1595                         "Device not found: %04x:%04x\n",
1596                         PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1597
1598                 /* End of list, leave */
1599                 return -ENODEV;
1600         }
1601         bus = pdev->bus->number;
1602
1603         sbridge_dev = get_sbridge_dev(bus);
1604         if (!sbridge_dev) {
1605                 sbridge_dev = alloc_sbridge_dev(bus, table);
1606                 if (!sbridge_dev) {
1607                         pci_dev_put(pdev);
1608                         return -ENOMEM;
1609                 }
1610                 (*num_mc)++;
1611         }
1612
1613         if (sbridge_dev->pdev[devno]) {
1614                 sbridge_printk(KERN_ERR,
1615                         "Duplicated device for %04x:%04x\n",
1616                         PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1617                 pci_dev_put(pdev);
1618                 return -ENODEV;
1619         }
1620
1621         sbridge_dev->pdev[devno] = pdev;
1622
1623         /* Be sure that the device is enabled */
1624         if (unlikely(pci_enable_device(pdev) < 0)) {
1625                 sbridge_printk(KERN_ERR,
1626                         "Couldn't enable %04x:%04x\n",
1627                         PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1628                 return -ENODEV;
1629         }
1630
1631         edac_dbg(0, "Detected %04x:%04x\n",
1632                  PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1633
1634         /*
1635          * As stated on drivers/pci/search.c, the reference count for
1636          * @from is always decremented if it is not %NULL. So, as we need
1637          * to get all devices up to null, we need to do a get for the device
1638          */
1639         pci_dev_get(pdev);
1640
1641         *prev = pdev;
1642
1643         return 0;
1644 }
1645
1646 /*
1647  * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
1648  *                           devices we want to reference for this driver.
1649  * @num_mc: pointer to the memory controllers count, to be incremented in case
1650  *          of success.
1651  * @table: model specific table
1652  *
1653  * returns 0 in case of success or error code
1654  */
1655 static int sbridge_get_all_devices(u8 *num_mc,
1656                                    const struct pci_id_table *table)
1657 {
1658         int i, rc;
1659         struct pci_dev *pdev = NULL;
1660
1661         while (table && table->descr) {
1662                 for (i = 0; i < table->n_devs; i++) {
1663                         pdev = NULL;
1664                         do {
1665                                 rc = sbridge_get_onedevice(&pdev, num_mc,
1666                                                            table, i);
1667                                 if (rc < 0) {
1668                                         if (i == 0) {
1669                                                 i = table->n_devs;
1670                                                 break;
1671                                         }
1672                                         sbridge_put_all_devices();
1673                                         return -ENODEV;
1674                                 }
1675                         } while (pdev);
1676                 }
1677                 table++;
1678         }
1679
1680         return 0;
1681 }
1682
1683 static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1684                                  struct sbridge_dev *sbridge_dev)
1685 {
1686         struct sbridge_pvt *pvt = mci->pvt_info;
1687         struct pci_dev *pdev;
1688         u8 saw_chan_mask = 0;
1689         int i;
1690
1691         for (i = 0; i < sbridge_dev->n_devs; i++) {
1692                 pdev = sbridge_dev->pdev[i];
1693                 if (!pdev)
1694                         continue;
1695
1696                 switch (pdev->device) {
1697                 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
1698                         pvt->pci_sad0 = pdev;
1699                         break;
1700                 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
1701                         pvt->pci_sad1 = pdev;
1702                         break;
1703                 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
1704                         pvt->pci_br0 = pdev;
1705                         break;
1706                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
1707                         pvt->pci_ha0 = pdev;
1708                         break;
1709                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
1710                         pvt->pci_ta = pdev;
1711                         break;
1712                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
1713                         pvt->pci_ras = pdev;
1714                         break;
1715                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
1716                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
1717                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
1718                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
1719                 {
1720                         int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
1721                         pvt->pci_tad[id] = pdev;
1722                         saw_chan_mask |= 1 << id;
1723                 }
1724                         break;
1725                 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
1726                         pvt->pci_ddrio = pdev;
1727                         break;
1728                 default:
1729                         goto error;
1730                 }
1731
1732                 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
1733                          pdev->vendor, pdev->device,
1734                          sbridge_dev->bus,
1735                          pdev);
1736         }
1737
1738         /* Check if everything were registered */
1739         if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
1740             !pvt-> pci_tad || !pvt->pci_ras  || !pvt->pci_ta)
1741                 goto enodev;
1742
1743         if (saw_chan_mask != 0x0f)
1744                 goto enodev;
1745         return 0;
1746
1747 enodev:
1748         sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1749         return -ENODEV;
1750
1751 error:
1752         sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
1753                        PCI_VENDOR_ID_INTEL, pdev->device);
1754         return -EINVAL;
1755 }
1756
1757 static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1758                                  struct sbridge_dev *sbridge_dev)
1759 {
1760         struct sbridge_pvt *pvt = mci->pvt_info;
1761         struct pci_dev *pdev;
1762         u8 saw_chan_mask = 0;
1763         int i;
1764
1765         for (i = 0; i < sbridge_dev->n_devs; i++) {
1766                 pdev = sbridge_dev->pdev[i];
1767                 if (!pdev)
1768                         continue;
1769
1770                 switch (pdev->device) {
1771                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
1772                         pvt->pci_ha0 = pdev;
1773                         break;
1774                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
1775                         pvt->pci_ta = pdev;
1776                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
1777                         pvt->pci_ras = pdev;
1778                         break;
1779                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
1780                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
1781                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
1782                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
1783                 {
1784                         int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
1785                         pvt->pci_tad[id] = pdev;
1786                         saw_chan_mask |= 1 << id;
1787                 }
1788                         break;
1789                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
1790                         pvt->pci_ddrio = pdev;
1791                         break;
1792                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
1793                         pvt->pci_ddrio = pdev;
1794                         break;
1795                 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
1796                         pvt->pci_sad0 = pdev;
1797                         break;
1798                 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
1799                         pvt->pci_br0 = pdev;
1800                         break;
1801                 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
1802                         pvt->pci_br1 = pdev;
1803                         break;
1804                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
1805                         pvt->pci_ha1 = pdev;
1806                         break;
1807                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
1808                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
1809                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
1810                 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
1811                 {
1812                         int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
1813                         pvt->pci_tad[id] = pdev;
1814                         saw_chan_mask |= 1 << id;
1815                 }
1816                         break;
1817                 default:
1818                         goto error;
1819                 }
1820
1821                 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1822                          sbridge_dev->bus,
1823                          PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1824                          pdev);
1825         }
1826
1827         /* Check if everything were registered */
1828         if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1829             !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras  ||
1830             !pvt->pci_ta)
1831                 goto enodev;
1832
1833         if (saw_chan_mask != 0x0f && /* -EN */
1834             saw_chan_mask != 0x33 && /* -EP */
1835             saw_chan_mask != 0xff)   /* -EX */
1836                 goto enodev;
1837         return 0;
1838
1839 enodev:
1840         sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1841         return -ENODEV;
1842
1843 error:
1844         sbridge_printk(KERN_ERR,
1845                        "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
1846                         pdev->device);
1847         return -EINVAL;
1848 }
1849
1850 static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
1851                                  struct sbridge_dev *sbridge_dev)
1852 {
1853         struct sbridge_pvt *pvt = mci->pvt_info;
1854         struct pci_dev *pdev;
1855         u8 saw_chan_mask = 0;
1856         int i;
1857
1858         /* there's only one device per system; not tied to any bus */
1859         if (pvt->info.pci_vtd == NULL)
1860                 /* result will be checked later */
1861                 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1862                                                    PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
1863                                                    NULL);
1864
1865         for (i = 0; i < sbridge_dev->n_devs; i++) {
1866                 pdev = sbridge_dev->pdev[i];
1867                 if (!pdev)
1868                         continue;
1869
1870                 switch (pdev->device) {
1871                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
1872                         pvt->pci_sad0 = pdev;
1873                         break;
1874                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
1875                         pvt->pci_sad1 = pdev;
1876                         break;
1877                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
1878                         pvt->pci_ha0 = pdev;
1879                         break;
1880                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
1881                         pvt->pci_ta = pdev;
1882                         break;
1883                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
1884                         pvt->pci_ras = pdev;
1885                         break;
1886                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
1887                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
1888                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
1889                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
1890                 {
1891                         int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
1892
1893                         pvt->pci_tad[id] = pdev;
1894                         saw_chan_mask |= 1 << id;
1895                 }
1896                         break;
1897                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
1898                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
1899                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
1900                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
1901                 {
1902                         int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
1903
1904                         pvt->pci_tad[id] = pdev;
1905                         saw_chan_mask |= 1 << id;
1906                 }
1907                         break;
1908                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
1909                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
1910                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
1911                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
1912                         if (!pvt->pci_ddrio)
1913                                 pvt->pci_ddrio = pdev;
1914                         break;
1915                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
1916                         pvt->pci_ha1 = pdev;
1917                         break;
1918                 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
1919                         pvt->pci_ha1_ta = pdev;
1920                         break;
1921                 default:
1922                         break;
1923                 }
1924
1925                 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1926                          sbridge_dev->bus,
1927                          PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1928                          pdev);
1929         }
1930
1931         /* Check if everything were registered */
1932         if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
1933             !pvt->pci_ras  || !pvt->pci_ta || !pvt->info.pci_vtd)
1934                 goto enodev;
1935
1936         if (saw_chan_mask != 0x0f && /* -EN */
1937             saw_chan_mask != 0x33 && /* -EP */
1938             saw_chan_mask != 0xff)   /* -EX */
1939                 goto enodev;
1940         return 0;
1941
1942 enodev:
1943         sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1944         return -ENODEV;
1945 }
1946
1947 static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
1948                                  struct sbridge_dev *sbridge_dev)
1949 {
1950         struct sbridge_pvt *pvt = mci->pvt_info;
1951         struct pci_dev *pdev;
1952         u8 saw_chan_mask = 0;
1953         int i;
1954
1955         /* there's only one device per system; not tied to any bus */
1956         if (pvt->info.pci_vtd == NULL)
1957                 /* result will be checked later */
1958                 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1959                                                    PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
1960                                                    NULL);
1961
1962         for (i = 0; i < sbridge_dev->n_devs; i++) {
1963                 pdev = sbridge_dev->pdev[i];
1964                 if (!pdev)
1965                         continue;
1966
1967                 switch (pdev->device) {
1968                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
1969                         pvt->pci_sad0 = pdev;
1970                         break;
1971                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
1972                         pvt->pci_sad1 = pdev;
1973                         break;
1974                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
1975                         pvt->pci_ha0 = pdev;
1976                         break;
1977                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
1978                         pvt->pci_ta = pdev;
1979                         break;
1980                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
1981                         pvt->pci_ras = pdev;
1982                         break;
1983                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
1984                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
1985                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
1986                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
1987                 {
1988                         int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
1989                         pvt->pci_tad[id] = pdev;
1990                         saw_chan_mask |= 1 << id;
1991                 }
1992                         break;
1993                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
1994                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
1995                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
1996                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
1997                 {
1998                         int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
1999                         pvt->pci_tad[id] = pdev;
2000                         saw_chan_mask |= 1 << id;
2001                 }
2002                         break;
2003                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2004                         pvt->pci_ddrio = pdev;
2005                         break;
2006                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2007                         pvt->pci_ha1 = pdev;
2008                         break;
2009                 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2010                         pvt->pci_ha1_ta = pdev;
2011                         break;
2012                 default:
2013                         break;
2014                 }
2015
2016                 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2017                          sbridge_dev->bus,
2018                          PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2019                          pdev);
2020         }
2021
2022         /* Check if everything were registered */
2023         if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2024             !pvt->pci_ras  || !pvt->pci_ta || !pvt->info.pci_vtd)
2025                 goto enodev;
2026
2027         if (saw_chan_mask != 0x0f && /* -EN */
2028             saw_chan_mask != 0x33 && /* -EP */
2029             saw_chan_mask != 0xff)   /* -EX */
2030                 goto enodev;
2031         return 0;
2032
2033 enodev:
2034         sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2035         return -ENODEV;
2036 }
2037
2038 /****************************************************************************
2039                         Error check routines
2040  ****************************************************************************/
2041
2042 /*
2043  * While Sandy Bridge has error count registers, SMI BIOS read values from
2044  * and resets the counters. So, they are not reliable for the OS to read
2045  * from them. So, we have no option but to just trust on whatever MCE is
2046  * telling us about the errors.
2047  */
2048 static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2049                                     const struct mce *m)
2050 {
2051         struct mem_ctl_info *new_mci;
2052         struct sbridge_pvt *pvt = mci->pvt_info;
2053         enum hw_event_mc_err_type tp_event;
2054         char *type, *optype, msg[256];
2055         bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2056         bool overflow = GET_BITFIELD(m->status, 62, 62);
2057         bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
2058         bool recoverable;
2059         u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2060         u32 mscod = GET_BITFIELD(m->status, 16, 31);
2061         u32 errcode = GET_BITFIELD(m->status, 0, 15);
2062         u32 channel = GET_BITFIELD(m->status, 0, 3);
2063         u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2064         long channel_mask, first_channel;
2065         u8  rank, socket, ha;
2066         int rc, dimm;
2067         char *area_type = NULL;
2068
2069         if (pvt->info.type != SANDY_BRIDGE)
2070                 recoverable = true;
2071         else
2072                 recoverable = GET_BITFIELD(m->status, 56, 56);
2073
2074         if (uncorrected_error) {
2075                 if (ripv) {
2076                         type = "FATAL";
2077                         tp_event = HW_EVENT_ERR_FATAL;
2078                 } else {
2079                         type = "NON_FATAL";
2080                         tp_event = HW_EVENT_ERR_UNCORRECTED;
2081                 }
2082         } else {
2083                 type = "CORRECTED";
2084                 tp_event = HW_EVENT_ERR_CORRECTED;
2085         }
2086
2087         /*
2088          * According with Table 15-9 of the Intel Architecture spec vol 3A,
2089          * memory errors should fit in this mask:
2090          *      000f 0000 1mmm cccc (binary)
2091          * where:
2092          *      f = Correction Report Filtering Bit. If 1, subsequent errors
2093          *          won't be shown
2094          *      mmm = error type
2095          *      cccc = channel
2096          * If the mask doesn't match, report an error to the parsing logic
2097          */
2098         if (! ((errcode & 0xef80) == 0x80)) {
2099                 optype = "Can't parse: it is not a mem";
2100         } else {
2101                 switch (optypenum) {
2102                 case 0:
2103                         optype = "generic undef request error";
2104                         break;
2105                 case 1:
2106                         optype = "memory read error";
2107                         break;
2108                 case 2:
2109                         optype = "memory write error";
2110                         break;
2111                 case 3:
2112                         optype = "addr/cmd error";
2113                         break;
2114                 case 4:
2115                         optype = "memory scrubbing error";
2116                         break;
2117                 default:
2118                         optype = "reserved";
2119                         break;
2120                 }
2121         }
2122
2123         /* Only decode errors with an valid address (ADDRV) */
2124         if (!GET_BITFIELD(m->status, 58, 58))
2125                 return;
2126
2127         rc = get_memory_error_data(mci, m->addr, &socket, &ha,
2128                                    &channel_mask, &rank, &area_type, msg);
2129         if (rc < 0)
2130                 goto err_parsing;
2131         new_mci = get_mci_for_node_id(socket);
2132         if (!new_mci) {
2133                 strcpy(msg, "Error: socket got corrupted!");
2134                 goto err_parsing;
2135         }
2136         mci = new_mci;
2137         pvt = mci->pvt_info;
2138
2139         first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
2140
2141         if (rank < 4)
2142                 dimm = 0;
2143         else if (rank < 8)
2144                 dimm = 1;
2145         else
2146                 dimm = 2;
2147
2148
2149         /*
2150          * FIXME: On some memory configurations (mirror, lockstep), the
2151          * Memory Controller can't point the error to a single DIMM. The
2152          * EDAC core should be handling the channel mask, in order to point
2153          * to the group of dimm's where the error may be happening.
2154          */
2155         if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
2156                 channel = first_channel;
2157
2158         snprintf(msg, sizeof(msg),
2159                  "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
2160                  overflow ? " OVERFLOW" : "",
2161                  (uncorrected_error && recoverable) ? " recoverable" : "",
2162                  area_type,
2163                  mscod, errcode,
2164                  socket, ha,
2165                  channel_mask,
2166                  rank);
2167
2168         edac_dbg(0, "%s\n", msg);
2169
2170         /* FIXME: need support for channel mask */
2171
2172         if (channel == CHANNEL_UNSPECIFIED)
2173                 channel = -1;
2174
2175         /* Call the helper to output message */
2176         edac_mc_handle_error(tp_event, mci, core_err_cnt,
2177                              m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
2178                              4*ha+channel, dimm, -1,
2179                              optype, msg);
2180         return;
2181 err_parsing:
2182         edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
2183                              -1, -1, -1,
2184                              msg, "");
2185
2186 }
2187
2188 /*
2189  *      sbridge_check_error     Retrieve and process errors reported by the
2190  *                              hardware. Called by the Core module.
2191  */
2192 static void sbridge_check_error(struct mem_ctl_info *mci)
2193 {
2194         struct sbridge_pvt *pvt = mci->pvt_info;
2195         int i;
2196         unsigned count = 0;
2197         struct mce *m;
2198
2199         /*
2200          * MCE first step: Copy all mce errors into a temporary buffer
2201          * We use a double buffering here, to reduce the risk of
2202          * loosing an error.
2203          */
2204         smp_rmb();
2205         count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
2206                 % MCE_LOG_LEN;
2207         if (!count)
2208                 return;
2209
2210         m = pvt->mce_outentry;
2211         if (pvt->mce_in + count > MCE_LOG_LEN) {
2212                 unsigned l = MCE_LOG_LEN - pvt->mce_in;
2213
2214                 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
2215                 smp_wmb();
2216                 pvt->mce_in = 0;
2217                 count -= l;
2218                 m += l;
2219         }
2220         memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
2221         smp_wmb();
2222         pvt->mce_in += count;
2223
2224         smp_rmb();
2225         if (pvt->mce_overrun) {
2226                 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
2227                               pvt->mce_overrun);
2228                 smp_wmb();
2229                 pvt->mce_overrun = 0;
2230         }
2231
2232         /*
2233          * MCE second step: parse errors and display
2234          */
2235         for (i = 0; i < count; i++)
2236                 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
2237 }
2238
2239 /*
2240  * sbridge_mce_check_error      Replicates mcelog routine to get errors
2241  *                              This routine simply queues mcelog errors, and
2242  *                              return. The error itself should be handled later
2243  *                              by sbridge_check_error.
2244  * WARNING: As this routine should be called at NMI time, extra care should
2245  * be taken to avoid deadlocks, and to be as fast as possible.
2246  */
2247 static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
2248                                    void *data)
2249 {
2250         struct mce *mce = (struct mce *)data;
2251         struct mem_ctl_info *mci;
2252         struct sbridge_pvt *pvt;
2253         char *type;
2254
2255         if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2256                 return NOTIFY_DONE;
2257
2258         mci = get_mci_for_node_id(mce->socketid);
2259         if (!mci)
2260                 return NOTIFY_DONE;
2261         pvt = mci->pvt_info;
2262
2263         /*
2264          * Just let mcelog handle it if the error is
2265          * outside the memory controller. A memory error
2266          * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
2267          * bit 12 has an special meaning.
2268          */
2269         if ((mce->status & 0xefff) >> 7 != 1)
2270                 return NOTIFY_DONE;
2271
2272         if (mce->mcgstatus & MCG_STATUS_MCIP)
2273                 type = "Exception";
2274         else
2275                 type = "Event";
2276
2277         sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
2278
2279         sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
2280                           "Bank %d: %016Lx\n", mce->extcpu, type,
2281                           mce->mcgstatus, mce->bank, mce->status);
2282         sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
2283         sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
2284         sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
2285
2286         sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
2287                           "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
2288                           mce->time, mce->socketid, mce->apicid);
2289
2290         smp_rmb();
2291         if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
2292                 smp_wmb();
2293                 pvt->mce_overrun++;
2294                 return NOTIFY_DONE;
2295         }
2296
2297         /* Copy memory error at the ringbuffer */
2298         memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
2299         smp_wmb();
2300         pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
2301
2302         /* Handle fatal errors immediately */
2303         if (mce->mcgstatus & 1)
2304                 sbridge_check_error(mci);
2305
2306         /* Advice mcelog that the error were handled */
2307         return NOTIFY_STOP;
2308 }
2309
2310 static struct notifier_block sbridge_mce_dec = {
2311         .notifier_call      = sbridge_mce_check_error,
2312 };
2313
2314 /****************************************************************************
2315                         EDAC register/unregister logic
2316  ****************************************************************************/
2317
2318 static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
2319 {
2320         struct mem_ctl_info *mci = sbridge_dev->mci;
2321         struct sbridge_pvt *pvt;
2322
2323         if (unlikely(!mci || !mci->pvt_info)) {
2324                 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
2325
2326                 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
2327                 return;
2328         }
2329
2330         pvt = mci->pvt_info;
2331
2332         edac_dbg(0, "MC: mci = %p, dev = %p\n",
2333                  mci, &sbridge_dev->pdev[0]->dev);
2334
2335         /* Remove MC sysfs nodes */
2336         edac_mc_del_mc(mci->pdev);
2337
2338         edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
2339         kfree(mci->ctl_name);
2340         edac_mc_free(mci);
2341         sbridge_dev->mci = NULL;
2342 }
2343
2344 static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
2345 {
2346         struct mem_ctl_info *mci;
2347         struct edac_mc_layer layers[2];
2348         struct sbridge_pvt *pvt;
2349         struct pci_dev *pdev = sbridge_dev->pdev[0];
2350         int rc;
2351
2352         /* Check the number of active and not disabled channels */
2353         rc = check_if_ecc_is_active(sbridge_dev->bus, type);
2354         if (unlikely(rc < 0))
2355                 return rc;
2356
2357         /* allocate a new MC control structure */
2358         layers[0].type = EDAC_MC_LAYER_CHANNEL;
2359         layers[0].size = NUM_CHANNELS;
2360         layers[0].is_virt_csrow = false;
2361         layers[1].type = EDAC_MC_LAYER_SLOT;
2362         layers[1].size = MAX_DIMMS;
2363         layers[1].is_virt_csrow = true;
2364         mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
2365                             sizeof(*pvt));
2366
2367         if (unlikely(!mci))
2368                 return -ENOMEM;
2369
2370         edac_dbg(0, "MC: mci = %p, dev = %p\n",
2371                  mci, &pdev->dev);
2372
2373         pvt = mci->pvt_info;
2374         memset(pvt, 0, sizeof(*pvt));
2375
2376         /* Associate sbridge_dev and mci for future usage */
2377         pvt->sbridge_dev = sbridge_dev;
2378         sbridge_dev->mci = mci;
2379
2380         mci->mtype_cap = MEM_FLAG_DDR3;
2381         mci->edac_ctl_cap = EDAC_FLAG_NONE;
2382         mci->edac_cap = EDAC_FLAG_NONE;
2383         mci->mod_name = "sbridge_edac.c";
2384         mci->mod_ver = SBRIDGE_REVISION;
2385         mci->dev_name = pci_name(pdev);
2386         mci->ctl_page_to_phys = NULL;
2387
2388         /* Set the function pointer to an actual operation function */
2389         mci->edac_check = sbridge_check_error;
2390
2391         pvt->info.type = type;
2392         switch (type) {
2393         case IVY_BRIDGE:
2394                 pvt->info.rankcfgr = IB_RANK_CFG_A;
2395                 pvt->info.get_tolm = ibridge_get_tolm;
2396                 pvt->info.get_tohm = ibridge_get_tohm;
2397                 pvt->info.dram_rule = ibridge_dram_rule;
2398                 pvt->info.get_memory_type = get_memory_type;
2399                 pvt->info.get_node_id = get_node_id;
2400                 pvt->info.rir_limit = rir_limit;
2401                 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2402                 pvt->info.interleave_list = ibridge_interleave_list;
2403                 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2404                 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2405                 pvt->info.get_width = ibridge_get_width;
2406                 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
2407
2408                 /* Store pci devices at mci for faster access */
2409                 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
2410                 if (unlikely(rc < 0))
2411                         goto fail0;
2412                 break;
2413         case SANDY_BRIDGE:
2414                 pvt->info.rankcfgr = SB_RANK_CFG_A;
2415                 pvt->info.get_tolm = sbridge_get_tolm;
2416                 pvt->info.get_tohm = sbridge_get_tohm;
2417                 pvt->info.dram_rule = sbridge_dram_rule;
2418                 pvt->info.get_memory_type = get_memory_type;
2419                 pvt->info.get_node_id = get_node_id;
2420                 pvt->info.rir_limit = rir_limit;
2421                 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2422                 pvt->info.interleave_list = sbridge_interleave_list;
2423                 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2424                 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2425                 pvt->info.get_width = sbridge_get_width;
2426                 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2427
2428                 /* Store pci devices at mci for faster access */
2429                 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2430                 if (unlikely(rc < 0))
2431                         goto fail0;
2432                 break;
2433         case HASWELL:
2434                 /* rankcfgr isn't used */
2435                 pvt->info.get_tolm = haswell_get_tolm;
2436                 pvt->info.get_tohm = haswell_get_tohm;
2437                 pvt->info.dram_rule = ibridge_dram_rule;
2438                 pvt->info.get_memory_type = haswell_get_memory_type;
2439                 pvt->info.get_node_id = haswell_get_node_id;
2440                 pvt->info.rir_limit = haswell_rir_limit;
2441                 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2442                 pvt->info.interleave_list = ibridge_interleave_list;
2443                 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2444                 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2445                 pvt->info.get_width = ibridge_get_width;
2446                 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
2447
2448                 /* Store pci devices at mci for faster access */
2449                 rc = haswell_mci_bind_devs(mci, sbridge_dev);
2450                 if (unlikely(rc < 0))
2451                         goto fail0;
2452                 break;
2453         case BROADWELL:
2454                 /* rankcfgr isn't used */
2455                 pvt->info.get_tolm = haswell_get_tolm;
2456                 pvt->info.get_tohm = haswell_get_tohm;
2457                 pvt->info.dram_rule = ibridge_dram_rule;
2458                 pvt->info.get_memory_type = haswell_get_memory_type;
2459                 pvt->info.get_node_id = haswell_get_node_id;
2460                 pvt->info.rir_limit = haswell_rir_limit;
2461                 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2462                 pvt->info.interleave_list = ibridge_interleave_list;
2463                 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2464                 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2465                 pvt->info.get_width = broadwell_get_width;
2466                 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
2467
2468                 /* Store pci devices at mci for faster access */
2469                 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
2470                 if (unlikely(rc < 0))
2471                         goto fail0;
2472                 break;
2473         }
2474
2475         /* Get dimm basic config and the memory layout */
2476         get_dimm_config(mci);
2477         get_memory_layout(mci);
2478
2479         /* record ptr to the generic device */
2480         mci->pdev = &pdev->dev;
2481
2482         /* add this new MC control structure to EDAC's list of MCs */
2483         if (unlikely(edac_mc_add_mc(mci))) {
2484                 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
2485                 rc = -EINVAL;
2486                 goto fail0;
2487         }
2488
2489         return 0;
2490
2491 fail0:
2492         kfree(mci->ctl_name);
2493         edac_mc_free(mci);
2494         sbridge_dev->mci = NULL;
2495         return rc;
2496 }
2497
2498 /*
2499  *      sbridge_probe   Probe for ONE instance of device to see if it is
2500  *                      present.
2501  *      return:
2502  *              0 for FOUND a device
2503  *              < 0 for error code
2504  */
2505
2506 static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2507 {
2508         int rc = -ENODEV;
2509         u8 mc, num_mc = 0;
2510         struct sbridge_dev *sbridge_dev;
2511         enum type type = SANDY_BRIDGE;
2512
2513         /* get the pci devices we want to reserve for our use */
2514         mutex_lock(&sbridge_edac_lock);
2515
2516         /*
2517          * All memory controllers are allocated at the first pass.
2518          */
2519         if (unlikely(probed >= 1)) {
2520                 mutex_unlock(&sbridge_edac_lock);
2521                 return -ENODEV;
2522         }
2523         probed++;
2524
2525         switch (pdev->device) {
2526         case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2527                 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2528                 type = IVY_BRIDGE;
2529                 break;
2530         case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2531                 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2532                 type = SANDY_BRIDGE;
2533                 break;
2534         case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2535                 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_haswell_table);
2536                 type = HASWELL;
2537                 break;
2538         case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2539                 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_broadwell_table);
2540                 type = BROADWELL;
2541                 break;
2542         }
2543         if (unlikely(rc < 0)) {
2544                 edac_dbg(0, "couldn't get all devices for 0x%x\n", pdev->device);
2545                 goto fail0;
2546         }
2547
2548         mc = 0;
2549
2550         list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
2551                 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2552                          mc, mc + 1, num_mc);
2553
2554                 sbridge_dev->mc = mc++;
2555                 rc = sbridge_register_mci(sbridge_dev, type);
2556                 if (unlikely(rc < 0))
2557                         goto fail1;
2558         }
2559
2560         sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
2561
2562         mutex_unlock(&sbridge_edac_lock);
2563         return 0;
2564
2565 fail1:
2566         list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2567                 sbridge_unregister_mci(sbridge_dev);
2568
2569         sbridge_put_all_devices();
2570 fail0:
2571         mutex_unlock(&sbridge_edac_lock);
2572         return rc;
2573 }
2574
2575 /*
2576  *      sbridge_remove  destructor for one instance of device
2577  *
2578  */
2579 static void sbridge_remove(struct pci_dev *pdev)
2580 {
2581         struct sbridge_dev *sbridge_dev;
2582
2583         edac_dbg(0, "\n");
2584
2585         /*
2586          * we have a trouble here: pdev value for removal will be wrong, since
2587          * it will point to the X58 register used to detect that the machine
2588          * is a Nehalem or upper design. However, due to the way several PCI
2589          * devices are grouped together to provide MC functionality, we need
2590          * to use a different method for releasing the devices
2591          */
2592
2593         mutex_lock(&sbridge_edac_lock);
2594
2595         if (unlikely(!probed)) {
2596                 mutex_unlock(&sbridge_edac_lock);
2597                 return;
2598         }
2599
2600         list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2601                 sbridge_unregister_mci(sbridge_dev);
2602
2603         /* Release PCI resources */
2604         sbridge_put_all_devices();
2605
2606         probed--;
2607
2608         mutex_unlock(&sbridge_edac_lock);
2609 }
2610
2611 MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2612
2613 /*
2614  *      sbridge_driver  pci_driver structure for this module
2615  *
2616  */
2617 static struct pci_driver sbridge_driver = {
2618         .name     = "sbridge_edac",
2619         .probe    = sbridge_probe,
2620         .remove   = sbridge_remove,
2621         .id_table = sbridge_pci_tbl,
2622 };
2623
2624 /*
2625  *      sbridge_init            Module entry function
2626  *                      Try to initialize this module for its devices
2627  */
2628 static int __init sbridge_init(void)
2629 {
2630         int pci_rc;
2631
2632         edac_dbg(2, "\n");
2633
2634         /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2635         opstate_init();
2636
2637         pci_rc = pci_register_driver(&sbridge_driver);
2638         if (pci_rc >= 0) {
2639                 mce_register_decode_chain(&sbridge_mce_dec);
2640                 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2641                         sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
2642                 return 0;
2643         }
2644
2645         sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2646                       pci_rc);
2647
2648         return pci_rc;
2649 }
2650
2651 /*
2652  *      sbridge_exit()  Module exit function
2653  *                      Unregister the driver
2654  */
2655 static void __exit sbridge_exit(void)
2656 {
2657         edac_dbg(2, "\n");
2658         pci_unregister_driver(&sbridge_driver);
2659         mce_unregister_decode_chain(&sbridge_mce_dec);
2660 }
2661
2662 module_init(sbridge_init);
2663 module_exit(sbridge_exit);
2664
2665 module_param(edac_op_state, int, 0444);
2666 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2667
2668 MODULE_LICENSE("GPL");
2669 MODULE_AUTHOR("Mauro Carvalho Chehab");
2670 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
2671 MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
2672                    SBRIDGE_REVISION);