blob: c99c595adc2bce97696d0ad8bfb0137579218d65 [file] [log] [blame]
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001/* 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:
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -020010 * Mauro Carvalho Chehab
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020011 */
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>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020021#include <linux/smp.h>
22#include <linux/bitmap.h>
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -030023#include <linux/math64.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020024#include <asm/processor.h>
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -020025#include <asm/mce.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020026
27#include "edac_core.h"
28
29/* Static vars */
30static LIST_HEAD(sbridge_edac_list);
31static DEFINE_MUTEX(sbridge_edac_lock);
32static int probed;
33
34/*
35 * Alter this version for the module when modifications are made
36 */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030037#define SBRIDGE_REVISION " Ver: 1.1.0 "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020038#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) \
Chen, Gong10ef6b02013-10-18 14:29:07 -070053 (((v) & GENMASK_ULL(hi, lo)) >> (lo))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020054
55/*
56 * sbridge Memory Controller Registers
57 */
58
59/*
60 * FIXME: For now, let's order by device function, as it makes
David Mackey15ed1032012-04-17 11:30:52 -070061 * easier for driver's development process. This table should be
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020062 * moved to pci_id.h when submitted upstream
63 */
64#define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0 0x3cf4 /* 12.6 */
65#define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1 0x3cf6 /* 12.7 */
66#define PCI_DEVICE_ID_INTEL_SBRIDGE_BR 0x3cf5 /* 13.6 */
67#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0 0x3ca0 /* 14.0 */
68#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA 0x3ca8 /* 15.0 */
69#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS 0x3c71 /* 15.1 */
70#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0 0x3caa /* 15.2 */
71#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1 0x3cab /* 15.3 */
72#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2 0x3cac /* 15.4 */
73#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3 0x3cad /* 15.5 */
74#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO 0x3cb8 /* 17.0 */
75
76 /*
77 * Currently, unused, but will be needed in the future
78 * implementations, as they hold the error counters
79 */
80#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR0 0x3c72 /* 16.2 */
81#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR1 0x3c73 /* 16.3 */
82#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR2 0x3c76 /* 16.6 */
83#define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR3 0x3c77 /* 16.7 */
84
85/* Devices 12 Function 6, Offsets 0x80 to 0xcc */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -030086static const u32 sbridge_dram_rule[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020087 0x80, 0x88, 0x90, 0x98, 0xa0,
88 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
89};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020090
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030091static const u32 ibridge_dram_rule[] = {
92 0x60, 0x68, 0x70, 0x78, 0x80,
93 0x88, 0x90, 0x98, 0xa0, 0xa8,
94 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
95 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
96};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020097
98#define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
99#define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
100#define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
101#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
102
103static char *get_dram_attr(u32 reg)
104{
105 switch(DRAM_ATTR(reg)) {
106 case 0:
107 return "DRAM";
108 case 1:
109 return "MMCFG";
110 case 2:
111 return "NXM";
112 default:
113 return "unknown";
114 }
115}
116
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300117static const u32 sbridge_interleave_list[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200118 0x84, 0x8c, 0x94, 0x9c, 0xa4,
119 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
120};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200121
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300122static const u32 ibridge_interleave_list[] = {
123 0x64, 0x6c, 0x74, 0x7c, 0x84,
124 0x8c, 0x94, 0x9c, 0xa4, 0xac,
125 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
126 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
127};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200128
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300129struct interleave_pkg {
130 unsigned char start;
131 unsigned char end;
132};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200133
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300134static const struct interleave_pkg sbridge_interleave_pkg[] = {
135 { 0, 2 },
136 { 3, 5 },
137 { 8, 10 },
138 { 11, 13 },
139 { 16, 18 },
140 { 19, 21 },
141 { 24, 26 },
142 { 27, 29 },
143};
144
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300145static const struct interleave_pkg ibridge_interleave_pkg[] = {
146 { 0, 3 },
147 { 4, 7 },
148 { 8, 11 },
149 { 12, 15 },
150 { 16, 19 },
151 { 20, 23 },
152 { 24, 27 },
153 { 28, 31 },
154};
155
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300156static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
157 int interleave)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200158{
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300159 return GET_BITFIELD(reg, table[interleave].start,
160 table[interleave].end);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200161}
162
163/* Devices 12 Function 7 */
164
165#define TOLM 0x80
166#define TOHM 0x84
167
168#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
169#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
170
171/* Device 13 Function 6 */
172
173#define SAD_TARGET 0xf0
174
175#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
176
177#define SAD_CONTROL 0xf4
178
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200179/* Device 14 function 0 */
180
181static const u32 tad_dram_rule[] = {
182 0x40, 0x44, 0x48, 0x4c,
183 0x50, 0x54, 0x58, 0x5c,
184 0x60, 0x64, 0x68, 0x6c,
185};
186#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
187
188#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
189#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
190#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
191#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
192#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
193#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
194#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
195
196/* Device 15, function 0 */
197
198#define MCMTR 0x7c
199
200#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
201#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
202#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
203
204/* Device 15, function 1 */
205
206#define RASENABLES 0xac
207#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
208
209/* Device 15, functions 2-5 */
210
211static const int mtr_regs[] = {
212 0x80, 0x84, 0x88,
213};
214
215#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
216#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
217#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
218#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
219#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
220
221static const u32 tad_ch_nilv_offset[] = {
222 0x90, 0x94, 0x98, 0x9c,
223 0xa0, 0xa4, 0xa8, 0xac,
224 0xb0, 0xb4, 0xb8, 0xbc,
225};
226#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
227#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
228
229static const u32 rir_way_limit[] = {
230 0x108, 0x10c, 0x110, 0x114, 0x118,
231};
232#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
233
234#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
235#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200236
237#define MAX_RIR_WAY 8
238
239static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
240 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
241 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
242 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
243 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
244 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
245};
246
247#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
248#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
249
250/* Device 16, functions 2-7 */
251
252/*
253 * FIXME: Implement the error count reads directly
254 */
255
256static const u32 correrrcnt[] = {
257 0x104, 0x108, 0x10c, 0x110,
258};
259
260#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
261#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
262#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
263#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
264
265static const u32 correrrthrsld[] = {
266 0x11c, 0x120, 0x124, 0x128,
267};
268
269#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
270#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
271
272
273/* Device 17, function 0 */
274
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300275#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200276
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300277#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200278
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200279/*
280 * sbridge structs
281 */
282
283#define NUM_CHANNELS 4
284#define MAX_DIMMS 3 /* Max DIMMS per channel */
285
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300286enum type {
287 SANDY_BRIDGE,
288 IVY_BRIDGE,
289};
290
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300291struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200292struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300293 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300294 u32 mcmtr;
295 u32 rankcfgr;
296 u64 (*get_tolm)(struct sbridge_pvt *pvt);
297 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300298 u64 (*rir_limit)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300299 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300300 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300301 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300302 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300303 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300304 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300305 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200306};
307
308struct sbridge_channel {
309 u32 ranks;
310 u32 dimms;
311};
312
313struct pci_id_descr {
314 int dev;
315 int func;
316 int dev_id;
317 int optional;
318};
319
320struct pci_id_table {
321 const struct pci_id_descr *descr;
322 int n_devs;
323};
324
325struct sbridge_dev {
326 struct list_head list;
327 u8 bus, mc;
328 u8 node_id, source_id;
329 struct pci_dev **pdev;
330 int n_devs;
331 struct mem_ctl_info *mci;
332};
333
334struct sbridge_pvt {
335 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300336 struct pci_dev *pci_sad0, *pci_sad1;
337 struct pci_dev *pci_ha0, *pci_ha1;
338 struct pci_dev *pci_br0, *pci_br1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200339 struct pci_dev *pci_tad[NUM_CHANNELS];
340
341 struct sbridge_dev *sbridge_dev;
342
343 struct sbridge_info info;
344 struct sbridge_channel channel[NUM_CHANNELS];
345
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200346 /* Memory type detection */
347 bool is_mirrored, is_lockstep, is_close_pg;
348
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200349 /* Fifo double buffers */
350 struct mce mce_entry[MCE_LOG_LEN];
351 struct mce mce_outentry[MCE_LOG_LEN];
352
353 /* Fifo in/out counters */
354 unsigned mce_in, mce_out;
355
356 /* Count indicator to show errors not got */
357 unsigned mce_overrun;
358
359 /* Memory description */
360 u64 tolm, tohm;
361};
362
Luck, Tonyde4772c2013-03-28 09:59:15 -0700363#define PCI_DESCR(device, function, device_id, opt) \
364 .dev = (device), \
365 .func = (function), \
366 .dev_id = (device_id), \
367 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200368
369static const struct pci_id_descr pci_dev_descr_sbridge[] = {
370 /* Processor Home Agent */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700371 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200372
373 /* Memory controller */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700374 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
375 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
376 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
377 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
378 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
379 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
380 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200381
382 /* System Address Decoder */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700383 { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
384 { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200385
386 /* Broadcast Registers */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700387 { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200388};
389
390#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
391static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
392 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
393 {0,} /* 0 terminated list. */
394};
395
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300396/* This changes depending if 1HA or 2HA:
397 * 1HA:
398 * 0x0eb8 (17.0) is DDRIO0
399 * 2HA:
400 * 0x0ebc (17.4) is DDRIO0
401 */
402#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
403#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
404
405/* pci ids */
406#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
407#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
408#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
409#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
410#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
411#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
412#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
413#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
414#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
415#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
416#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
417#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
418#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
419#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
420#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
421
422static const struct pci_id_descr pci_dev_descr_ibridge[] = {
423 /* Processor Home Agent */
424 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
425
426 /* Memory controller */
427 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
428 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
429 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
430 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
431 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
432 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
433
434 /* System Address Decoder */
435 { PCI_DESCR(22, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
436
437 /* Broadcast Registers */
438 { PCI_DESCR(22, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
439 { PCI_DESCR(22, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
440
441 /* Optional, mode 2HA */
442 { PCI_DESCR(28, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
443#if 0
444 { PCI_DESCR(29, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
445 { PCI_DESCR(29, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
446#endif
447 { PCI_DESCR(29, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
448 { PCI_DESCR(29, 3, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
449
450 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
451 { PCI_DESCR(17, 4, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
452};
453
454static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
455 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
456 {0,} /* 0 terminated list. */
457};
458
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200459/*
460 * pci_device_id table for which devices we are looking for
461 */
Jingoo Hanba935f42013-12-06 10:23:08 +0100462static const struct pci_device_id sbridge_pci_tbl[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200463 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300464 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200465 {0,} /* 0 terminated list. */
466};
467
468
469/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700470 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200471 ****************************************************************************/
472
473static inline int numrank(u32 mtr)
474{
475 int ranks = (1 << RANK_CNT_BITS(mtr));
476
477 if (ranks > 4) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300478 edac_dbg(0, "Invalid number of ranks: %d (max = 4) raw value = %x (%04x)\n",
479 ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200480 return -EINVAL;
481 }
482
483 return ranks;
484}
485
486static inline int numrow(u32 mtr)
487{
488 int rows = (RANK_WIDTH_BITS(mtr) + 12);
489
490 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300491 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
492 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200493 return -EINVAL;
494 }
495
496 return 1 << rows;
497}
498
499static inline int numcol(u32 mtr)
500{
501 int cols = (COL_WIDTH_BITS(mtr) + 10);
502
503 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300504 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
505 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200506 return -EINVAL;
507 }
508
509 return 1 << cols;
510}
511
512static struct sbridge_dev *get_sbridge_dev(u8 bus)
513{
514 struct sbridge_dev *sbridge_dev;
515
516 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
517 if (sbridge_dev->bus == bus)
518 return sbridge_dev;
519 }
520
521 return NULL;
522}
523
524static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
525 const struct pci_id_table *table)
526{
527 struct sbridge_dev *sbridge_dev;
528
529 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
530 if (!sbridge_dev)
531 return NULL;
532
533 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
534 GFP_KERNEL);
535 if (!sbridge_dev->pdev) {
536 kfree(sbridge_dev);
537 return NULL;
538 }
539
540 sbridge_dev->bus = bus;
541 sbridge_dev->n_devs = table->n_devs;
542 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
543
544 return sbridge_dev;
545}
546
547static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
548{
549 list_del(&sbridge_dev->list);
550 kfree(sbridge_dev->pdev);
551 kfree(sbridge_dev);
552}
553
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300554static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
555{
556 u32 reg;
557
558 /* Address range is 32:28 */
559 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
560 return GET_TOLM(reg);
561}
562
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300563static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
564{
565 u32 reg;
566
567 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
568 return GET_TOHM(reg);
569}
570
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300571static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
572{
573 u32 reg;
574
575 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
576
577 return GET_TOLM(reg);
578}
579
580static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
581{
582 u32 reg;
583
584 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
585
586 return GET_TOHM(reg);
587}
588
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300589static u64 rir_limit(u32 reg)
590{
591 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
592}
593
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300594static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
595{
596 u32 reg;
597 enum mem_type mtype;
598
599 if (pvt->pci_ddrio) {
600 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
601 &reg);
602 if (GET_BITFIELD(reg, 11, 11))
603 /* FIXME: Can also be LRDIMM */
604 mtype = MEM_RDDR3;
605 else
606 mtype = MEM_DDR3;
607 } else
608 mtype = MEM_UNKNOWN;
609
610 return mtype;
611}
612
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300613static u8 get_node_id(struct sbridge_pvt *pvt)
614{
615 u32 reg;
616 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
617 return GET_BITFIELD(reg, 0, 2);
618}
619
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300620static inline u8 sad_pkg_socket(u8 pkg)
621{
622 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
623 return (pkg >> 3) | (pkg & 0x3);
624}
625
626static inline u8 sad_pkg_ha(u8 pkg)
627{
628 return (pkg >> 2) & 0x1;
629}
630
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200631/****************************************************************************
632 Memory check routines
633 ****************************************************************************/
634static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
635 unsigned func)
636{
637 struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
638 int i;
639
640 if (!sbridge_dev)
641 return NULL;
642
643 for (i = 0; i < sbridge_dev->n_devs; i++) {
644 if (!sbridge_dev->pdev[i])
645 continue;
646
647 if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
648 PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300649 edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
650 bus, slot, func, sbridge_dev->pdev[i]);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200651 return sbridge_dev->pdev[i];
652 }
653 }
654
655 return NULL;
656}
657
658/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300659 * check_if_ecc_is_active() - Checks if ECC is active
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200660 * bus: Device bus
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200661 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300662static int check_if_ecc_is_active(const u8 bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200663{
664 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200665 u32 mcmtr;
666
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200667 pdev = get_pdev_slot_func(bus, 15, 0);
668 if (!pdev) {
669 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
670 "%2x.%02d.%d!!!\n",
671 bus, 15, 0);
672 return -ENODEV;
673 }
674
675 pci_read_config_dword(pdev, MCMTR, &mcmtr);
676 if (!IS_ECC_ENABLED(mcmtr)) {
677 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
678 return -ENODEV;
679 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200680 return 0;
681}
682
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300683static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200684{
685 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300686 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300687 unsigned i, j, banks, ranks, rows, cols, npages;
688 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200689 u32 reg;
690 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200691 enum mem_type mtype;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200692
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300693 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200694 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
695
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300696 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -0300697 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
698 pvt->sbridge_dev->mc,
699 pvt->sbridge_dev->node_id,
700 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200701
702 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
703 if (IS_MIRROR_ENABLED(reg)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300704 edac_dbg(0, "Memory mirror is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200705 pvt->is_mirrored = true;
706 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300707 edac_dbg(0, "Memory mirror is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200708 pvt->is_mirrored = false;
709 }
710
711 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
712 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300713 edac_dbg(0, "Lockstep is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200714 mode = EDAC_S8ECD8ED;
715 pvt->is_lockstep = true;
716 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300717 edac_dbg(0, "Lockstep is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200718 mode = EDAC_S4ECD4ED;
719 pvt->is_lockstep = false;
720 }
721 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300722 edac_dbg(0, "address map is on closed page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200723 pvt->is_close_pg = true;
724 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300725 edac_dbg(0, "address map is on open page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200726 pvt->is_close_pg = false;
727 }
728
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300729 mtype = pvt->info.get_memory_type(pvt);
730 if (mtype == MEM_RDDR3)
731 edac_dbg(0, "Memory is registered\n");
732 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -0700733 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300734 else
735 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200736
737 /* On all supported DDR3 DIMM types, there are 8 banks available */
738 banks = 8;
739
740 for (i = 0; i < NUM_CHANNELS; i++) {
741 u32 mtr;
742
743 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300744 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
745 i, j, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200746 pci_read_config_dword(pvt->pci_tad[i],
747 mtr_regs[j], &mtr);
Joe Perches956b9ba2012-04-29 17:08:39 -0300748 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200749 if (IS_DIMM_PRESENT(mtr)) {
750 pvt->channel[i].dimms++;
751
752 ranks = numrank(mtr);
753 rows = numrow(mtr);
754 cols = numcol(mtr);
755
756 /* DDR3 has 8 I/O banks */
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300757 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200758 npages = MiB_TO_PAGES(size);
759
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300760 edac_dbg(0, "mc#%d: channel %d, dimm %d, %Ld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -0300761 pvt->sbridge_dev->mc, i, j,
762 size, npages,
763 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200764
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300765 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300766 dimm->grain = 32;
767 dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
768 dimm->mtype = mtype;
769 dimm->edac_mode = mode;
770 snprintf(dimm->label, sizeof(dimm->label),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200771 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
772 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200773 }
774 }
775 }
776
777 return 0;
778}
779
780static void get_memory_layout(const struct mem_ctl_info *mci)
781{
782 struct sbridge_pvt *pvt = mci->pvt_info;
783 int i, j, k, n_sads, n_tads, sad_interl;
784 u32 reg;
785 u64 limit, prv = 0;
786 u64 tmp_mb;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300787 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200788 u32 rir_way;
789
790 /*
791 * Step 1) Get TOLM/TOHM ranges
792 */
793
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300794 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200795 tmp_mb = (1 + pvt->tolm) >> 20;
796
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300797 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300798 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200799
800 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300801 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200802 tmp_mb = (1 + pvt->tohm) >> 20;
803
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300804 mb = div_u64_rem(tmp_mb, 1000, &kb);
Mauro Carvalho Chehabda14d932012-10-25 09:07:21 -0200805 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200806
807 /*
808 * Step 2) Get SAD range and SAD Interleave list
809 * TAD registers contain the interleave wayness. However, it
810 * seems simpler to just discover it indirectly, with the
811 * algorithm bellow.
812 */
813 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300814 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200815 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300816 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200817 &reg);
818 limit = SAD_LIMIT(reg);
819
820 if (!DRAM_RULE_ENABLE(reg))
821 continue;
822
823 if (limit <= prv)
824 break;
825
826 tmp_mb = (limit + 1) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300827 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300828 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
829 n_sads,
830 get_dram_attr(reg),
831 mb, kb,
832 ((u64)tmp_mb) << 20L,
833 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
834 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200835 prv = limit;
836
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300837 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200838 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300839 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200840 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300841 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
842 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200843 break;
844
Joe Perches956b9ba2012-04-29 17:08:39 -0300845 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300846 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200847 }
848 }
849
850 /*
851 * Step 3) Get TAD range
852 */
853 prv = 0;
854 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
855 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
856 &reg);
857 limit = TAD_LIMIT(reg);
858 if (limit <= prv)
859 break;
860 tmp_mb = (limit + 1) >> 20;
861
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300862 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300863 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",
864 n_tads, mb, kb,
865 ((u64)tmp_mb) << 20L,
866 (u32)TAD_SOCK(reg),
867 (u32)TAD_CH(reg),
868 (u32)TAD_TGT0(reg),
869 (u32)TAD_TGT1(reg),
870 (u32)TAD_TGT2(reg),
871 (u32)TAD_TGT3(reg),
872 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -0300873 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200874 }
875
876 /*
877 * Step 4) Get TAD offsets, per each channel
878 */
879 for (i = 0; i < NUM_CHANNELS; i++) {
880 if (!pvt->channel[i].dimms)
881 continue;
882 for (j = 0; j < n_tads; j++) {
883 pci_read_config_dword(pvt->pci_tad[i],
884 tad_ch_nilv_offset[j],
885 &reg);
886 tmp_mb = TAD_OFFSET(reg) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300887 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300888 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
889 i, j,
890 mb, kb,
891 ((u64)tmp_mb) << 20L,
892 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200893 }
894 }
895
896 /*
897 * Step 6) Get RIR Wayness/Limit, per each channel
898 */
899 for (i = 0; i < NUM_CHANNELS; i++) {
900 if (!pvt->channel[i].dimms)
901 continue;
902 for (j = 0; j < MAX_RIR_RANGES; j++) {
903 pci_read_config_dword(pvt->pci_tad[i],
904 rir_way_limit[j],
905 &reg);
906
907 if (!IS_RIR_VALID(reg))
908 continue;
909
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300910 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200911 rir_way = 1 << RIR_WAY(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300912 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300913 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
914 i, j,
915 mb, kb,
916 ((u64)tmp_mb) << 20L,
917 rir_way,
918 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200919
920 for (k = 0; k < rir_way; k++) {
921 pci_read_config_dword(pvt->pci_tad[i],
922 rir_offset[j][k],
923 &reg);
924 tmp_mb = RIR_OFFSET(reg) << 6;
925
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300926 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300927 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
928 i, j, k,
929 mb, kb,
930 ((u64)tmp_mb) << 20L,
931 (u32)RIR_RNK_TGT(reg),
932 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200933 }
934 }
935 }
936}
937
Rashika Kheria8112c0c2013-12-14 19:32:09 +0530938static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200939{
940 struct sbridge_dev *sbridge_dev;
941
942 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
943 if (sbridge_dev->node_id == node_id)
944 return sbridge_dev->mci;
945 }
946 return NULL;
947}
948
949static int get_memory_error_data(struct mem_ctl_info *mci,
950 u64 addr,
951 u8 *socket,
952 long *channel_mask,
953 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -0300954 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200955{
956 struct mem_ctl_info *new_mci;
957 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300958 struct pci_dev *pci_ha;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200959 int n_rir, n_sads, n_tads, sad_way, sck_xch;
960 int sad_interl, idx, base_ch;
961 int interleave_mode;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300962 unsigned sad_interleave[pvt->info.max_interleave];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200963 u32 reg;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300964 u8 ch_way, sck_way, pkg, sad_ha = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200965 u32 tad_offset;
966 u32 rir_way;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300967 u32 mb, kb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -0500968 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200969
970
971 /*
972 * Step 0) Check if the address is at special memory ranges
973 * The check bellow is probably enough to fill all cases where
974 * the error is not inside a memory, except for the legacy
975 * range (e. g. VGA addresses). It is unlikely, however, that the
976 * memory controller would generate an error on that range.
977 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300978 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200979 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200980 return -EINVAL;
981 }
982 if (addr >= (u64)pvt->tohm) {
983 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200984 return -EINVAL;
985 }
986
987 /*
988 * Step 1) Get socket
989 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300990 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
991 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200992 &reg);
993
994 if (!DRAM_RULE_ENABLE(reg))
995 continue;
996
997 limit = SAD_LIMIT(reg);
998 if (limit <= prv) {
999 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001000 return -EINVAL;
1001 }
1002 if (addr <= limit)
1003 break;
1004 prv = limit;
1005 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001006 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001007 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001008 return -EINVAL;
1009 }
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001010 *area_type = get_dram_attr(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001011 interleave_mode = INTERLEAVE_MODE(reg);
1012
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001013 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001014 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001015
1016 if (pvt->info.type == SANDY_BRIDGE) {
1017 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1018 for (sad_way = 0; sad_way < 8; sad_way++) {
1019 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1020 if (sad_way > 0 && sad_interl == pkg)
1021 break;
1022 sad_interleave[sad_way] = pkg;
1023 edac_dbg(0, "SAD interleave #%d: %d\n",
1024 sad_way, sad_interleave[sad_way]);
1025 }
1026 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1027 pvt->sbridge_dev->mc,
1028 n_sads,
1029 addr,
1030 limit,
1031 sad_way + 7,
1032 !interleave_mode ? "" : "XOR[18:16]");
1033 if (interleave_mode)
1034 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1035 else
1036 idx = (addr >> 6) & 7;
1037 switch (sad_way) {
1038 case 1:
1039 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001040 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001041 case 2:
1042 idx = idx & 1;
1043 break;
1044 case 4:
1045 idx = idx & 3;
1046 break;
1047 case 8:
1048 break;
1049 default:
1050 sprintf(msg, "Can't discover socket interleave");
1051 return -EINVAL;
1052 }
1053 *socket = sad_interleave[idx];
1054 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1055 idx, sad_way, *socket);
1056 } else {
1057 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001058 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001059 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1060 *socket = sad_pkg_socket(pkg);
1061 sad_ha = sad_pkg_ha(pkg);
1062 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1063 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001064 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001065
1066 /*
1067 * Move to the proper node structure, in order to access the
1068 * right PCI registers
1069 */
1070 new_mci = get_mci_for_node_id(*socket);
1071 if (!new_mci) {
1072 sprintf(msg, "Struct for socket #%u wasn't initialized",
1073 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001074 return -EINVAL;
1075 }
1076 mci = new_mci;
1077 pvt = mci->pvt_info;
1078
1079 /*
1080 * Step 2) Get memory channel
1081 */
1082 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001083 if (pvt->info.type == SANDY_BRIDGE)
1084 pci_ha = pvt->pci_ha0;
1085 else {
1086 if (sad_ha)
1087 pci_ha = pvt->pci_ha1;
1088 else
1089 pci_ha = pvt->pci_ha0;
1090 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001091 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001092 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001093 limit = TAD_LIMIT(reg);
1094 if (limit <= prv) {
1095 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001096 return -EINVAL;
1097 }
1098 if (addr <= limit)
1099 break;
1100 prv = limit;
1101 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001102 if (n_tads == MAX_TAD) {
1103 sprintf(msg, "Can't discover the memory channel");
1104 return -EINVAL;
1105 }
1106
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001107 ch_way = TAD_CH(reg) + 1;
1108 sck_way = TAD_SOCK(reg) + 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001109
1110 if (ch_way == 3)
1111 idx = addr >> 6;
1112 else
1113 idx = addr >> (6 + sck_way);
1114 idx = idx % ch_way;
1115
1116 /*
1117 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1118 */
1119 switch (idx) {
1120 case 0:
1121 base_ch = TAD_TGT0(reg);
1122 break;
1123 case 1:
1124 base_ch = TAD_TGT1(reg);
1125 break;
1126 case 2:
1127 base_ch = TAD_TGT2(reg);
1128 break;
1129 case 3:
1130 base_ch = TAD_TGT3(reg);
1131 break;
1132 default:
1133 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001134 return -EINVAL;
1135 }
1136 *channel_mask = 1 << base_ch;
1137
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001138 pci_read_config_dword(pvt->pci_tad[base_ch],
1139 tad_ch_nilv_offset[n_tads],
1140 &tad_offset);
1141
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001142 if (pvt->is_mirrored) {
1143 *channel_mask |= 1 << ((base_ch + 2) % 4);
1144 switch(ch_way) {
1145 case 2:
1146 case 4:
1147 sck_xch = 1 << sck_way * (ch_way >> 1);
1148 break;
1149 default:
1150 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001151 return -EINVAL;
1152 }
1153 } else
1154 sck_xch = (1 << sck_way) * ch_way;
1155
1156 if (pvt->is_lockstep)
1157 *channel_mask |= 1 << ((base_ch + 1) % 4);
1158
1159 offset = TAD_OFFSET(tad_offset);
1160
Joe Perches956b9ba2012-04-29 17:08:39 -03001161 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",
1162 n_tads,
1163 addr,
1164 limit,
1165 (u32)TAD_SOCK(reg),
1166 ch_way,
1167 offset,
1168 idx,
1169 base_ch,
1170 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001171
1172 /* Calculate channel address */
1173 /* Remove the TAD offset */
1174
1175 if (offset > addr) {
1176 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1177 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001178 return -EINVAL;
1179 }
1180 addr -= offset;
1181 /* Store the low bits [0:6] of the addr */
1182 ch_addr = addr & 0x7f;
1183 /* Remove socket wayness and remove 6 bits */
1184 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001185 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001186#if 0
1187 /* Divide by channel way */
1188 addr = addr / ch_way;
1189#endif
1190 /* Recover the last 6 bits */
1191 ch_addr |= addr << 6;
1192
1193 /*
1194 * Step 3) Decode rank
1195 */
1196 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1197 pci_read_config_dword(pvt->pci_tad[base_ch],
1198 rir_way_limit[n_rir],
1199 &reg);
1200
1201 if (!IS_RIR_VALID(reg))
1202 continue;
1203
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001204 limit = pvt->info.rir_limit(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001205 mb = div_u64_rem(limit >> 20, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001206 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1207 n_rir,
1208 mb, kb,
1209 limit,
1210 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001211 if (ch_addr <= limit)
1212 break;
1213 }
1214 if (n_rir == MAX_RIR_RANGES) {
1215 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1216 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001217 return -EINVAL;
1218 }
1219 rir_way = RIR_WAY(reg);
1220 if (pvt->is_close_pg)
1221 idx = (ch_addr >> 6);
1222 else
1223 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1224 idx %= 1 << rir_way;
1225
1226 pci_read_config_dword(pvt->pci_tad[base_ch],
1227 rir_offset[n_rir][idx],
1228 &reg);
1229 *rank = RIR_RNK_TGT(reg);
1230
Joe Perches956b9ba2012-04-29 17:08:39 -03001231 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1232 n_rir,
1233 ch_addr,
1234 limit,
1235 rir_way,
1236 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001237
1238 return 0;
1239}
1240
1241/****************************************************************************
1242 Device initialization routines: put/get, init/exit
1243 ****************************************************************************/
1244
1245/*
1246 * sbridge_put_all_devices 'put' all the devices that we have
1247 * reserved via 'get'
1248 */
1249static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1250{
1251 int i;
1252
Joe Perches956b9ba2012-04-29 17:08:39 -03001253 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001254 for (i = 0; i < sbridge_dev->n_devs; i++) {
1255 struct pci_dev *pdev = sbridge_dev->pdev[i];
1256 if (!pdev)
1257 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03001258 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1259 pdev->bus->number,
1260 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001261 pci_dev_put(pdev);
1262 }
1263}
1264
1265static void sbridge_put_all_devices(void)
1266{
1267 struct sbridge_dev *sbridge_dev, *tmp;
1268
1269 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1270 sbridge_put_devices(sbridge_dev);
1271 free_sbridge_dev(sbridge_dev);
1272 }
1273}
1274
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001275static int sbridge_get_onedevice(struct pci_dev **prev,
1276 u8 *num_mc,
1277 const struct pci_id_table *table,
1278 const unsigned devno)
1279{
1280 struct sbridge_dev *sbridge_dev;
1281 const struct pci_id_descr *dev_descr = &table->descr[devno];
1282
1283 struct pci_dev *pdev = NULL;
1284 u8 bus = 0;
1285
Jiang Liuec5a0b32014-02-17 13:10:23 +08001286 sbridge_printk(KERN_DEBUG,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001287 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1288 dev_descr->dev, dev_descr->func,
1289 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1290
1291 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1292 dev_descr->dev_id, *prev);
1293
1294 if (!pdev) {
1295 if (*prev) {
1296 *prev = pdev;
1297 return 0;
1298 }
1299
1300 if (dev_descr->optional)
1301 return 0;
1302
1303 if (devno == 0)
1304 return -ENODEV;
1305
1306 sbridge_printk(KERN_INFO,
1307 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1308 dev_descr->dev, dev_descr->func,
1309 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1310
1311 /* End of list, leave */
1312 return -ENODEV;
1313 }
1314 bus = pdev->bus->number;
1315
1316 sbridge_dev = get_sbridge_dev(bus);
1317 if (!sbridge_dev) {
1318 sbridge_dev = alloc_sbridge_dev(bus, table);
1319 if (!sbridge_dev) {
1320 pci_dev_put(pdev);
1321 return -ENOMEM;
1322 }
1323 (*num_mc)++;
1324 }
1325
1326 if (sbridge_dev->pdev[devno]) {
1327 sbridge_printk(KERN_ERR,
1328 "Duplicated device for "
1329 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1330 bus, dev_descr->dev, dev_descr->func,
1331 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1332 pci_dev_put(pdev);
1333 return -ENODEV;
1334 }
1335
1336 sbridge_dev->pdev[devno] = pdev;
1337
1338 /* Sanity check */
1339 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1340 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1341 sbridge_printk(KERN_ERR,
1342 "Device PCI ID %04x:%04x "
1343 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1344 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1345 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1346 bus, dev_descr->dev, dev_descr->func);
1347 return -ENODEV;
1348 }
1349
1350 /* Be sure that the device is enabled */
1351 if (unlikely(pci_enable_device(pdev) < 0)) {
1352 sbridge_printk(KERN_ERR,
1353 "Couldn't enable "
1354 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1355 bus, dev_descr->dev, dev_descr->func,
1356 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1357 return -ENODEV;
1358 }
1359
Joe Perches956b9ba2012-04-29 17:08:39 -03001360 edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1361 bus, dev_descr->dev, dev_descr->func,
1362 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001363
1364 /*
1365 * As stated on drivers/pci/search.c, the reference count for
1366 * @from is always decremented if it is not %NULL. So, as we need
1367 * to get all devices up to null, we need to do a get for the device
1368 */
1369 pci_dev_get(pdev);
1370
1371 *prev = pdev;
1372
1373 return 0;
1374}
1375
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001376/*
1377 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
1378 * device/functions we want to reference for this driver.
1379 * Need to 'get' device 16 func 1 and func 2.
1380 * @num_mc: pointer to the memory controllers count, to be incremented in case
1381 * of success.
1382 * @table: model specific table
1383 *
1384 * returns 0 in case of success or error code
1385 */
1386static int sbridge_get_all_devices(u8 *num_mc,
1387 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001388{
1389 int i, rc;
1390 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001391
1392 while (table && table->descr) {
1393 for (i = 0; i < table->n_devs; i++) {
1394 pdev = NULL;
1395 do {
1396 rc = sbridge_get_onedevice(&pdev, num_mc,
1397 table, i);
1398 if (rc < 0) {
1399 if (i == 0) {
1400 i = table->n_devs;
1401 break;
1402 }
1403 sbridge_put_all_devices();
1404 return -ENODEV;
1405 }
1406 } while (pdev);
1407 }
1408 table++;
1409 }
1410
1411 return 0;
1412}
1413
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001414static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1415 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001416{
1417 struct sbridge_pvt *pvt = mci->pvt_info;
1418 struct pci_dev *pdev;
1419 int i, func, slot;
1420
1421 for (i = 0; i < sbridge_dev->n_devs; i++) {
1422 pdev = sbridge_dev->pdev[i];
1423 if (!pdev)
1424 continue;
1425 slot = PCI_SLOT(pdev->devfn);
1426 func = PCI_FUNC(pdev->devfn);
1427 switch (slot) {
1428 case 12:
1429 switch (func) {
1430 case 6:
1431 pvt->pci_sad0 = pdev;
1432 break;
1433 case 7:
1434 pvt->pci_sad1 = pdev;
1435 break;
1436 default:
1437 goto error;
1438 }
1439 break;
1440 case 13:
1441 switch (func) {
1442 case 6:
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -03001443 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001444 break;
1445 default:
1446 goto error;
1447 }
1448 break;
1449 case 14:
1450 switch (func) {
1451 case 0:
1452 pvt->pci_ha0 = pdev;
1453 break;
1454 default:
1455 goto error;
1456 }
1457 break;
1458 case 15:
1459 switch (func) {
1460 case 0:
1461 pvt->pci_ta = pdev;
1462 break;
1463 case 1:
1464 pvt->pci_ras = pdev;
1465 break;
1466 case 2:
1467 case 3:
1468 case 4:
1469 case 5:
1470 pvt->pci_tad[func - 2] = pdev;
1471 break;
1472 default:
1473 goto error;
1474 }
1475 break;
1476 case 17:
1477 switch (func) {
1478 case 0:
1479 pvt->pci_ddrio = pdev;
1480 break;
1481 default:
1482 goto error;
1483 }
1484 break;
1485 default:
1486 goto error;
1487 }
1488
Joe Perches956b9ba2012-04-29 17:08:39 -03001489 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1490 sbridge_dev->bus,
1491 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1492 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001493 }
1494
1495 /* Check if everything were registered */
1496 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07001497 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001498 goto enodev;
1499
1500 for (i = 0; i < NUM_CHANNELS; i++) {
1501 if (!pvt->pci_tad[i])
1502 goto enodev;
1503 }
1504 return 0;
1505
1506enodev:
1507 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1508 return -ENODEV;
1509
1510error:
1511 sbridge_printk(KERN_ERR, "Device %d, function %d "
1512 "is out of the expected range\n",
1513 slot, func);
1514 return -EINVAL;
1515}
1516
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001517static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1518 struct sbridge_dev *sbridge_dev)
1519{
1520 struct sbridge_pvt *pvt = mci->pvt_info;
1521 struct pci_dev *pdev, *tmp;
1522 int i, func, slot;
1523 bool mode_2ha = false;
1524
1525 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1526 PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, NULL);
1527 if (tmp) {
1528 mode_2ha = true;
1529 pci_dev_put(tmp);
1530 }
1531
1532 for (i = 0; i < sbridge_dev->n_devs; i++) {
1533 pdev = sbridge_dev->pdev[i];
1534 if (!pdev)
1535 continue;
1536 slot = PCI_SLOT(pdev->devfn);
1537 func = PCI_FUNC(pdev->devfn);
1538
1539 switch (slot) {
1540 case 14:
1541 if (func == 0) {
1542 pvt->pci_ha0 = pdev;
1543 break;
1544 }
1545 goto error;
1546 case 15:
1547 switch (func) {
1548 case 0:
1549 pvt->pci_ta = pdev;
1550 break;
1551 case 1:
1552 pvt->pci_ras = pdev;
1553 break;
1554 case 4:
1555 case 5:
1556 /* if we have 2 HAs active, channels 2 and 3
1557 * are in other device */
1558 if (mode_2ha)
1559 break;
1560 /* fall through */
1561 case 2:
1562 case 3:
1563 pvt->pci_tad[func - 2] = pdev;
1564 break;
1565 default:
1566 goto error;
1567 }
1568 break;
1569 case 17:
1570 if (func == 4) {
1571 pvt->pci_ddrio = pdev;
1572 break;
1573 } else if (func == 0) {
1574 if (!mode_2ha)
1575 pvt->pci_ddrio = pdev;
1576 break;
1577 }
1578 goto error;
1579 case 22:
1580 switch (func) {
1581 case 0:
1582 pvt->pci_sad0 = pdev;
1583 break;
1584 case 1:
1585 pvt->pci_br0 = pdev;
1586 break;
1587 case 2:
1588 pvt->pci_br1 = pdev;
1589 break;
1590 default:
1591 goto error;
1592 }
1593 break;
1594 case 28:
1595 if (func == 0) {
1596 pvt->pci_ha1 = pdev;
1597 break;
1598 }
1599 goto error;
1600 case 29:
1601 /* we shouldn't have this device if we have just one
1602 * HA present */
1603 WARN_ON(!mode_2ha);
1604 if (func == 2 || func == 3) {
1605 pvt->pci_tad[func] = pdev;
1606 break;
1607 }
1608 goto error;
1609 default:
1610 goto error;
1611 }
1612
1613 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1614 sbridge_dev->bus,
1615 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1616 pdev);
1617 }
1618
1619 /* Check if everything were registered */
1620 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1621 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
1622 !pvt->pci_ta)
1623 goto enodev;
1624
1625 for (i = 0; i < NUM_CHANNELS; i++) {
1626 if (!pvt->pci_tad[i])
1627 goto enodev;
1628 }
1629 return 0;
1630
1631enodev:
1632 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1633 return -ENODEV;
1634
1635error:
1636 sbridge_printk(KERN_ERR,
1637 "Device %d, function %d is out of the expected range\n",
1638 slot, func);
1639 return -EINVAL;
1640}
1641
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001642/****************************************************************************
1643 Error check routines
1644 ****************************************************************************/
1645
1646/*
1647 * While Sandy Bridge has error count registers, SMI BIOS read values from
1648 * and resets the counters. So, they are not reliable for the OS to read
1649 * from them. So, we have no option but to just trust on whatever MCE is
1650 * telling us about the errors.
1651 */
1652static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1653 const struct mce *m)
1654{
1655 struct mem_ctl_info *new_mci;
1656 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001657 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001658 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001659 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1660 bool overflow = GET_BITFIELD(m->status, 62, 62);
1661 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001662 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001663 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1664 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1665 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1666 u32 channel = GET_BITFIELD(m->status, 0, 3);
1667 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1668 long channel_mask, first_channel;
1669 u8 rank, socket;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001670 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001671 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001672
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001673 if (pvt->info.type == IVY_BRIDGE)
1674 recoverable = true;
1675 else
1676 recoverable = GET_BITFIELD(m->status, 56, 56);
1677
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001678 if (uncorrected_error) {
1679 if (ripv) {
1680 type = "FATAL";
1681 tp_event = HW_EVENT_ERR_FATAL;
1682 } else {
1683 type = "NON_FATAL";
1684 tp_event = HW_EVENT_ERR_UNCORRECTED;
1685 }
1686 } else {
1687 type = "CORRECTED";
1688 tp_event = HW_EVENT_ERR_CORRECTED;
1689 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001690
1691 /*
David Mackey15ed1032012-04-17 11:30:52 -07001692 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001693 * memory errors should fit in this mask:
1694 * 000f 0000 1mmm cccc (binary)
1695 * where:
1696 * f = Correction Report Filtering Bit. If 1, subsequent errors
1697 * won't be shown
1698 * mmm = error type
1699 * cccc = channel
1700 * If the mask doesn't match, report an error to the parsing logic
1701 */
1702 if (! ((errcode & 0xef80) == 0x80)) {
1703 optype = "Can't parse: it is not a mem";
1704 } else {
1705 switch (optypenum) {
1706 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001707 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001708 break;
1709 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001710 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001711 break;
1712 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001713 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001714 break;
1715 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001716 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001717 break;
1718 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001719 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001720 break;
1721 default:
1722 optype = "reserved";
1723 break;
1724 }
1725 }
1726
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03001727 /* Only decode errors with an valid address (ADDRV) */
1728 if (!GET_BITFIELD(m->status, 58, 58))
1729 return;
1730
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001731 rc = get_memory_error_data(mci, m->addr, &socket,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001732 &channel_mask, &rank, &area_type, msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001733 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001734 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001735 new_mci = get_mci_for_node_id(socket);
1736 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001737 strcpy(msg, "Error: socket got corrupted!");
1738 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001739 }
1740 mci = new_mci;
1741 pvt = mci->pvt_info;
1742
1743 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1744
1745 if (rank < 4)
1746 dimm = 0;
1747 else if (rank < 8)
1748 dimm = 1;
1749 else
1750 dimm = 2;
1751
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001752
1753 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001754 * FIXME: On some memory configurations (mirror, lockstep), the
1755 * Memory Controller can't point the error to a single DIMM. The
1756 * EDAC core should be handling the channel mask, in order to point
1757 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001758 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001759 snprintf(msg, sizeof(msg),
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001760 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001761 overflow ? " OVERFLOW" : "",
1762 (uncorrected_error && recoverable) ? " recoverable" : "",
1763 area_type,
1764 mscod, errcode,
1765 socket,
1766 channel_mask,
1767 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001768
Joe Perches956b9ba2012-04-29 17:08:39 -03001769 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001770
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001771 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001772
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001773 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001774 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001775 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1776 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001777 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001778 return;
1779err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001780 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001781 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001782 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001783
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001784}
1785
1786/*
1787 * sbridge_check_error Retrieve and process errors reported by the
1788 * hardware. Called by the Core module.
1789 */
1790static void sbridge_check_error(struct mem_ctl_info *mci)
1791{
1792 struct sbridge_pvt *pvt = mci->pvt_info;
1793 int i;
1794 unsigned count = 0;
1795 struct mce *m;
1796
1797 /*
1798 * MCE first step: Copy all mce errors into a temporary buffer
1799 * We use a double buffering here, to reduce the risk of
1800 * loosing an error.
1801 */
1802 smp_rmb();
1803 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1804 % MCE_LOG_LEN;
1805 if (!count)
1806 return;
1807
1808 m = pvt->mce_outentry;
1809 if (pvt->mce_in + count > MCE_LOG_LEN) {
1810 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1811
1812 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1813 smp_wmb();
1814 pvt->mce_in = 0;
1815 count -= l;
1816 m += l;
1817 }
1818 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1819 smp_wmb();
1820 pvt->mce_in += count;
1821
1822 smp_rmb();
1823 if (pvt->mce_overrun) {
1824 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
1825 pvt->mce_overrun);
1826 smp_wmb();
1827 pvt->mce_overrun = 0;
1828 }
1829
1830 /*
1831 * MCE second step: parse errors and display
1832 */
1833 for (i = 0; i < count; i++)
1834 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
1835}
1836
1837/*
1838 * sbridge_mce_check_error Replicates mcelog routine to get errors
1839 * This routine simply queues mcelog errors, and
1840 * return. The error itself should be handled later
1841 * by sbridge_check_error.
1842 * WARNING: As this routine should be called at NMI time, extra care should
1843 * be taken to avoid deadlocks, and to be as fast as possible.
1844 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001845static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
1846 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001847{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001848 struct mce *mce = (struct mce *)data;
1849 struct mem_ctl_info *mci;
1850 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04001851 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001852
Chen, Gongfd521032013-12-06 01:17:09 -05001853 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
1854 return NOTIFY_DONE;
1855
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001856 mci = get_mci_for_node_id(mce->socketid);
1857 if (!mci)
1858 return NOTIFY_BAD;
1859 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001860
1861 /*
1862 * Just let mcelog handle it if the error is
1863 * outside the memory controller. A memory error
1864 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1865 * bit 12 has an special meaning.
1866 */
1867 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001868 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001869
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04001870 if (mce->mcgstatus & MCG_STATUS_MCIP)
1871 type = "Exception";
1872 else
1873 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001874
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001875 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001876
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001877 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
1878 "Bank %d: %016Lx\n", mce->extcpu, type,
1879 mce->mcgstatus, mce->bank, mce->status);
1880 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
1881 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
1882 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001883
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001884 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
1885 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
1886 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001887
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001888 /* Only handle if it is the right mc controller */
1889 if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001890 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001891
1892 smp_rmb();
1893 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1894 smp_wmb();
1895 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001896 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001897 }
1898
1899 /* Copy memory error at the ringbuffer */
1900 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1901 smp_wmb();
1902 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1903
1904 /* Handle fatal errors immediately */
1905 if (mce->mcgstatus & 1)
1906 sbridge_check_error(mci);
1907
1908 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001909 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001910}
1911
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001912static struct notifier_block sbridge_mce_dec = {
1913 .notifier_call = sbridge_mce_check_error,
1914};
1915
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001916/****************************************************************************
1917 EDAC register/unregister logic
1918 ****************************************************************************/
1919
1920static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
1921{
1922 struct mem_ctl_info *mci = sbridge_dev->mci;
1923 struct sbridge_pvt *pvt;
1924
1925 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001926 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001927
1928 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
1929 return;
1930 }
1931
1932 pvt = mci->pvt_info;
1933
Joe Perches956b9ba2012-04-29 17:08:39 -03001934 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1935 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001936
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001937 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03001938 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001939
Joe Perches956b9ba2012-04-29 17:08:39 -03001940 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001941 kfree(mci->ctl_name);
1942 edac_mc_free(mci);
1943 sbridge_dev->mci = NULL;
1944}
1945
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001946static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001947{
1948 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001949 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001950 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001951 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001952 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001953
1954 /* Check the number of active and not disabled channels */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001955 rc = check_if_ecc_is_active(sbridge_dev->bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001956 if (unlikely(rc < 0))
1957 return rc;
1958
1959 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001960 layers[0].type = EDAC_MC_LAYER_CHANNEL;
1961 layers[0].size = NUM_CHANNELS;
1962 layers[0].is_virt_csrow = false;
1963 layers[1].type = EDAC_MC_LAYER_SLOT;
1964 layers[1].size = MAX_DIMMS;
1965 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03001966 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001967 sizeof(*pvt));
1968
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001969 if (unlikely(!mci))
1970 return -ENOMEM;
1971
Joe Perches956b9ba2012-04-29 17:08:39 -03001972 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001973 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001974
1975 pvt = mci->pvt_info;
1976 memset(pvt, 0, sizeof(*pvt));
1977
1978 /* Associate sbridge_dev and mci for future usage */
1979 pvt->sbridge_dev = sbridge_dev;
1980 sbridge_dev->mci = mci;
1981
1982 mci->mtype_cap = MEM_FLAG_DDR3;
1983 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1984 mci->edac_cap = EDAC_FLAG_NONE;
1985 mci->mod_name = "sbridge_edac.c";
1986 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001987 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001988 mci->ctl_page_to_phys = NULL;
1989
1990 /* Set the function pointer to an actual operation function */
1991 mci->edac_check = sbridge_check_error;
1992
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001993 pvt->info.type = type;
1994 if (type == IVY_BRIDGE) {
1995 pvt->info.rankcfgr = IB_RANK_CFG_A;
1996 pvt->info.get_tolm = ibridge_get_tolm;
1997 pvt->info.get_tohm = ibridge_get_tohm;
1998 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001999 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002000 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002001 pvt->info.rir_limit = rir_limit;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002002 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2003 pvt->info.interleave_list = ibridge_interleave_list;
2004 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2005 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2006 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
2007
2008 /* Store pci devices at mci for faster access */
2009 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
2010 if (unlikely(rc < 0))
2011 goto fail0;
2012 } else {
2013 pvt->info.rankcfgr = SB_RANK_CFG_A;
2014 pvt->info.get_tolm = sbridge_get_tolm;
2015 pvt->info.get_tohm = sbridge_get_tohm;
2016 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002017 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002018 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002019 pvt->info.rir_limit = rir_limit;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002020 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2021 pvt->info.interleave_list = sbridge_interleave_list;
2022 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2023 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2024 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2025
2026 /* Store pci devices at mci for faster access */
2027 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2028 if (unlikely(rc < 0))
2029 goto fail0;
2030 }
2031
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002032
2033 /* Get dimm basic config and the memory layout */
2034 get_dimm_config(mci);
2035 get_memory_layout(mci);
2036
2037 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002038 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002039
2040 /* add this new MC control structure to EDAC's list of MCs */
2041 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002042 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002043 rc = -EINVAL;
2044 goto fail0;
2045 }
2046
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002047 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002048
2049fail0:
2050 kfree(mci->ctl_name);
2051 edac_mc_free(mci);
2052 sbridge_dev->mci = NULL;
2053 return rc;
2054}
2055
2056/*
2057 * sbridge_probe Probe for ONE instance of device to see if it is
2058 * present.
2059 * return:
2060 * 0 for FOUND a device
2061 * < 0 for error code
2062 */
2063
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002064static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002065{
2066 int rc;
2067 u8 mc, num_mc = 0;
2068 struct sbridge_dev *sbridge_dev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002069 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002070
2071 /* get the pci devices we want to reserve for our use */
2072 mutex_lock(&sbridge_edac_lock);
2073
2074 /*
2075 * All memory controllers are allocated at the first pass.
2076 */
2077 if (unlikely(probed >= 1)) {
2078 mutex_unlock(&sbridge_edac_lock);
2079 return -ENODEV;
2080 }
2081 probed++;
2082
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002083 if (pdev->device == PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA) {
2084 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2085 type = IVY_BRIDGE;
2086 } else {
2087 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2088 type = SANDY_BRIDGE;
2089 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002090 if (unlikely(rc < 0))
2091 goto fail0;
2092 mc = 0;
2093
2094 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002095 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2096 mc, mc + 1, num_mc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002097 sbridge_dev->mc = mc++;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002098 rc = sbridge_register_mci(sbridge_dev, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002099 if (unlikely(rc < 0))
2100 goto fail1;
2101 }
2102
2103 sbridge_printk(KERN_INFO, "Driver loaded.\n");
2104
2105 mutex_unlock(&sbridge_edac_lock);
2106 return 0;
2107
2108fail1:
2109 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2110 sbridge_unregister_mci(sbridge_dev);
2111
2112 sbridge_put_all_devices();
2113fail0:
2114 mutex_unlock(&sbridge_edac_lock);
2115 return rc;
2116}
2117
2118/*
2119 * sbridge_remove destructor for one instance of device
2120 *
2121 */
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002122static void sbridge_remove(struct pci_dev *pdev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002123{
2124 struct sbridge_dev *sbridge_dev;
2125
Joe Perches956b9ba2012-04-29 17:08:39 -03002126 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002127
2128 /*
2129 * we have a trouble here: pdev value for removal will be wrong, since
2130 * it will point to the X58 register used to detect that the machine
2131 * is a Nehalem or upper design. However, due to the way several PCI
2132 * devices are grouped together to provide MC functionality, we need
2133 * to use a different method for releasing the devices
2134 */
2135
2136 mutex_lock(&sbridge_edac_lock);
2137
2138 if (unlikely(!probed)) {
2139 mutex_unlock(&sbridge_edac_lock);
2140 return;
2141 }
2142
2143 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2144 sbridge_unregister_mci(sbridge_dev);
2145
2146 /* Release PCI resources */
2147 sbridge_put_all_devices();
2148
2149 probed--;
2150
2151 mutex_unlock(&sbridge_edac_lock);
2152}
2153
2154MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2155
2156/*
2157 * sbridge_driver pci_driver structure for this module
2158 *
2159 */
2160static struct pci_driver sbridge_driver = {
2161 .name = "sbridge_edac",
2162 .probe = sbridge_probe,
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002163 .remove = sbridge_remove,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002164 .id_table = sbridge_pci_tbl,
2165};
2166
2167/*
2168 * sbridge_init Module entry function
2169 * Try to initialize this module for its devices
2170 */
2171static int __init sbridge_init(void)
2172{
2173 int pci_rc;
2174
Joe Perches956b9ba2012-04-29 17:08:39 -03002175 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002176
2177 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2178 opstate_init();
2179
2180 pci_rc = pci_register_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002181 if (pci_rc >= 0) {
2182 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05002183 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2184 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002185 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03002186 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002187
2188 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2189 pci_rc);
2190
2191 return pci_rc;
2192}
2193
2194/*
2195 * sbridge_exit() Module exit function
2196 * Unregister the driver
2197 */
2198static void __exit sbridge_exit(void)
2199{
Joe Perches956b9ba2012-04-29 17:08:39 -03002200 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002201 pci_unregister_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002202 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002203}
2204
2205module_init(sbridge_init);
2206module_exit(sbridge_exit);
2207
2208module_param(edac_op_state, int, 0444);
2209MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2210
2211MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002212MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002213MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002214MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002215 SBRIDGE_REVISION);