blob: 3335281982dc46c52c90c8b99ce79e97a95d3854 [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)
236#define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
237
238#define MAX_RIR_WAY 8
239
240static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
241 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
242 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
243 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
244 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
245 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
246};
247
248#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
249#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
250
251/* Device 16, functions 2-7 */
252
253/*
254 * FIXME: Implement the error count reads directly
255 */
256
257static const u32 correrrcnt[] = {
258 0x104, 0x108, 0x10c, 0x110,
259};
260
261#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
262#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
263#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
264#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
265
266static const u32 correrrthrsld[] = {
267 0x11c, 0x120, 0x124, 0x128,
268};
269
270#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
271#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
272
273
274/* Device 17, function 0 */
275
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300276#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200277
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300278#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200279
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200280/*
281 * sbridge structs
282 */
283
284#define NUM_CHANNELS 4
285#define MAX_DIMMS 3 /* Max DIMMS per channel */
286
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300287enum type {
288 SANDY_BRIDGE,
289 IVY_BRIDGE,
290};
291
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300292struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200293struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300294 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300295 u32 mcmtr;
296 u32 rankcfgr;
297 u64 (*get_tolm)(struct sbridge_pvt *pvt);
298 u64 (*get_tohm)(struct sbridge_pvt *pvt);
299 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 Rozanski9e375442014-06-02 15:15:22 -0300589static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
590{
591 u32 reg;
592 enum mem_type mtype;
593
594 if (pvt->pci_ddrio) {
595 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
596 &reg);
597 if (GET_BITFIELD(reg, 11, 11))
598 /* FIXME: Can also be LRDIMM */
599 mtype = MEM_RDDR3;
600 else
601 mtype = MEM_DDR3;
602 } else
603 mtype = MEM_UNKNOWN;
604
605 return mtype;
606}
607
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300608static u8 get_node_id(struct sbridge_pvt *pvt)
609{
610 u32 reg;
611 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
612 return GET_BITFIELD(reg, 0, 2);
613}
614
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300615static inline u8 sad_pkg_socket(u8 pkg)
616{
617 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
618 return (pkg >> 3) | (pkg & 0x3);
619}
620
621static inline u8 sad_pkg_ha(u8 pkg)
622{
623 return (pkg >> 2) & 0x1;
624}
625
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200626/****************************************************************************
627 Memory check routines
628 ****************************************************************************/
629static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
630 unsigned func)
631{
632 struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
633 int i;
634
635 if (!sbridge_dev)
636 return NULL;
637
638 for (i = 0; i < sbridge_dev->n_devs; i++) {
639 if (!sbridge_dev->pdev[i])
640 continue;
641
642 if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
643 PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300644 edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
645 bus, slot, func, sbridge_dev->pdev[i]);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200646 return sbridge_dev->pdev[i];
647 }
648 }
649
650 return NULL;
651}
652
653/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300654 * check_if_ecc_is_active() - Checks if ECC is active
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200655 * bus: Device bus
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200656 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300657static int check_if_ecc_is_active(const u8 bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200658{
659 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200660 u32 mcmtr;
661
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200662 pdev = get_pdev_slot_func(bus, 15, 0);
663 if (!pdev) {
664 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
665 "%2x.%02d.%d!!!\n",
666 bus, 15, 0);
667 return -ENODEV;
668 }
669
670 pci_read_config_dword(pdev, MCMTR, &mcmtr);
671 if (!IS_ECC_ENABLED(mcmtr)) {
672 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
673 return -ENODEV;
674 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200675 return 0;
676}
677
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300678static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200679{
680 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300681 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300682 unsigned i, j, banks, ranks, rows, cols, npages;
683 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200684 u32 reg;
685 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200686 enum mem_type mtype;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200687
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300688 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200689 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
690
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300691 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -0300692 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
693 pvt->sbridge_dev->mc,
694 pvt->sbridge_dev->node_id,
695 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200696
697 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
698 if (IS_MIRROR_ENABLED(reg)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300699 edac_dbg(0, "Memory mirror is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200700 pvt->is_mirrored = true;
701 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300702 edac_dbg(0, "Memory mirror is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200703 pvt->is_mirrored = false;
704 }
705
706 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
707 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300708 edac_dbg(0, "Lockstep is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200709 mode = EDAC_S8ECD8ED;
710 pvt->is_lockstep = true;
711 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300712 edac_dbg(0, "Lockstep is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200713 mode = EDAC_S4ECD4ED;
714 pvt->is_lockstep = false;
715 }
716 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300717 edac_dbg(0, "address map is on closed page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200718 pvt->is_close_pg = true;
719 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300720 edac_dbg(0, "address map is on open page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200721 pvt->is_close_pg = false;
722 }
723
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300724 mtype = pvt->info.get_memory_type(pvt);
725 if (mtype == MEM_RDDR3)
726 edac_dbg(0, "Memory is registered\n");
727 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -0700728 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300729 else
730 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200731
732 /* On all supported DDR3 DIMM types, there are 8 banks available */
733 banks = 8;
734
735 for (i = 0; i < NUM_CHANNELS; i++) {
736 u32 mtr;
737
738 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300739 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
740 i, j, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200741 pci_read_config_dword(pvt->pci_tad[i],
742 mtr_regs[j], &mtr);
Joe Perches956b9ba2012-04-29 17:08:39 -0300743 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200744 if (IS_DIMM_PRESENT(mtr)) {
745 pvt->channel[i].dimms++;
746
747 ranks = numrank(mtr);
748 rows = numrow(mtr);
749 cols = numcol(mtr);
750
751 /* DDR3 has 8 I/O banks */
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300752 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200753 npages = MiB_TO_PAGES(size);
754
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300755 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 -0300756 pvt->sbridge_dev->mc, i, j,
757 size, npages,
758 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200759
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300760 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300761 dimm->grain = 32;
762 dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
763 dimm->mtype = mtype;
764 dimm->edac_mode = mode;
765 snprintf(dimm->label, sizeof(dimm->label),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200766 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
767 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200768 }
769 }
770 }
771
772 return 0;
773}
774
775static void get_memory_layout(const struct mem_ctl_info *mci)
776{
777 struct sbridge_pvt *pvt = mci->pvt_info;
778 int i, j, k, n_sads, n_tads, sad_interl;
779 u32 reg;
780 u64 limit, prv = 0;
781 u64 tmp_mb;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300782 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200783 u32 rir_way;
784
785 /*
786 * Step 1) Get TOLM/TOHM ranges
787 */
788
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300789 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200790 tmp_mb = (1 + pvt->tolm) >> 20;
791
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300792 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300793 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200794
795 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300796 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200797 tmp_mb = (1 + pvt->tohm) >> 20;
798
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300799 mb = div_u64_rem(tmp_mb, 1000, &kb);
Mauro Carvalho Chehabda14d932012-10-25 09:07:21 -0200800 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200801
802 /*
803 * Step 2) Get SAD range and SAD Interleave list
804 * TAD registers contain the interleave wayness. However, it
805 * seems simpler to just discover it indirectly, with the
806 * algorithm bellow.
807 */
808 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300809 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200810 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300811 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200812 &reg);
813 limit = SAD_LIMIT(reg);
814
815 if (!DRAM_RULE_ENABLE(reg))
816 continue;
817
818 if (limit <= prv)
819 break;
820
821 tmp_mb = (limit + 1) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300822 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300823 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
824 n_sads,
825 get_dram_attr(reg),
826 mb, kb,
827 ((u64)tmp_mb) << 20L,
828 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
829 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200830 prv = limit;
831
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300832 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200833 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300834 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200835 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300836 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
837 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200838 break;
839
Joe Perches956b9ba2012-04-29 17:08:39 -0300840 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300841 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200842 }
843 }
844
845 /*
846 * Step 3) Get TAD range
847 */
848 prv = 0;
849 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
850 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
851 &reg);
852 limit = TAD_LIMIT(reg);
853 if (limit <= prv)
854 break;
855 tmp_mb = (limit + 1) >> 20;
856
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300857 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300858 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",
859 n_tads, mb, kb,
860 ((u64)tmp_mb) << 20L,
861 (u32)TAD_SOCK(reg),
862 (u32)TAD_CH(reg),
863 (u32)TAD_TGT0(reg),
864 (u32)TAD_TGT1(reg),
865 (u32)TAD_TGT2(reg),
866 (u32)TAD_TGT3(reg),
867 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -0300868 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200869 }
870
871 /*
872 * Step 4) Get TAD offsets, per each channel
873 */
874 for (i = 0; i < NUM_CHANNELS; i++) {
875 if (!pvt->channel[i].dimms)
876 continue;
877 for (j = 0; j < n_tads; j++) {
878 pci_read_config_dword(pvt->pci_tad[i],
879 tad_ch_nilv_offset[j],
880 &reg);
881 tmp_mb = TAD_OFFSET(reg) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300882 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300883 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
884 i, j,
885 mb, kb,
886 ((u64)tmp_mb) << 20L,
887 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200888 }
889 }
890
891 /*
892 * Step 6) Get RIR Wayness/Limit, per each channel
893 */
894 for (i = 0; i < NUM_CHANNELS; i++) {
895 if (!pvt->channel[i].dimms)
896 continue;
897 for (j = 0; j < MAX_RIR_RANGES; j++) {
898 pci_read_config_dword(pvt->pci_tad[i],
899 rir_way_limit[j],
900 &reg);
901
902 if (!IS_RIR_VALID(reg))
903 continue;
904
905 tmp_mb = RIR_LIMIT(reg) >> 20;
906 rir_way = 1 << RIR_WAY(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300907 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300908 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
909 i, j,
910 mb, kb,
911 ((u64)tmp_mb) << 20L,
912 rir_way,
913 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200914
915 for (k = 0; k < rir_way; k++) {
916 pci_read_config_dword(pvt->pci_tad[i],
917 rir_offset[j][k],
918 &reg);
919 tmp_mb = RIR_OFFSET(reg) << 6;
920
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300921 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300922 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
923 i, j, k,
924 mb, kb,
925 ((u64)tmp_mb) << 20L,
926 (u32)RIR_RNK_TGT(reg),
927 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200928 }
929 }
930 }
931}
932
Rashika Kheria8112c0c2013-12-14 19:32:09 +0530933static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200934{
935 struct sbridge_dev *sbridge_dev;
936
937 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
938 if (sbridge_dev->node_id == node_id)
939 return sbridge_dev->mci;
940 }
941 return NULL;
942}
943
944static int get_memory_error_data(struct mem_ctl_info *mci,
945 u64 addr,
946 u8 *socket,
947 long *channel_mask,
948 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -0300949 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200950{
951 struct mem_ctl_info *new_mci;
952 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300953 struct pci_dev *pci_ha;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200954 int n_rir, n_sads, n_tads, sad_way, sck_xch;
955 int sad_interl, idx, base_ch;
956 int interleave_mode;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300957 unsigned sad_interleave[pvt->info.max_interleave];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200958 u32 reg;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300959 u8 ch_way, sck_way, pkg, sad_ha = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200960 u32 tad_offset;
961 u32 rir_way;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300962 u32 mb, kb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -0500963 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200964
965
966 /*
967 * Step 0) Check if the address is at special memory ranges
968 * The check bellow is probably enough to fill all cases where
969 * the error is not inside a memory, except for the legacy
970 * range (e. g. VGA addresses). It is unlikely, however, that the
971 * memory controller would generate an error on that range.
972 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300973 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200974 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200975 return -EINVAL;
976 }
977 if (addr >= (u64)pvt->tohm) {
978 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200979 return -EINVAL;
980 }
981
982 /*
983 * Step 1) Get socket
984 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300985 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
986 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200987 &reg);
988
989 if (!DRAM_RULE_ENABLE(reg))
990 continue;
991
992 limit = SAD_LIMIT(reg);
993 if (limit <= prv) {
994 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200995 return -EINVAL;
996 }
997 if (addr <= limit)
998 break;
999 prv = limit;
1000 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001001 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001002 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001003 return -EINVAL;
1004 }
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001005 *area_type = get_dram_attr(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001006 interleave_mode = INTERLEAVE_MODE(reg);
1007
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001008 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001009 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001010
1011 if (pvt->info.type == SANDY_BRIDGE) {
1012 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1013 for (sad_way = 0; sad_way < 8; sad_way++) {
1014 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1015 if (sad_way > 0 && sad_interl == pkg)
1016 break;
1017 sad_interleave[sad_way] = pkg;
1018 edac_dbg(0, "SAD interleave #%d: %d\n",
1019 sad_way, sad_interleave[sad_way]);
1020 }
1021 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1022 pvt->sbridge_dev->mc,
1023 n_sads,
1024 addr,
1025 limit,
1026 sad_way + 7,
1027 !interleave_mode ? "" : "XOR[18:16]");
1028 if (interleave_mode)
1029 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1030 else
1031 idx = (addr >> 6) & 7;
1032 switch (sad_way) {
1033 case 1:
1034 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001035 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001036 case 2:
1037 idx = idx & 1;
1038 break;
1039 case 4:
1040 idx = idx & 3;
1041 break;
1042 case 8:
1043 break;
1044 default:
1045 sprintf(msg, "Can't discover socket interleave");
1046 return -EINVAL;
1047 }
1048 *socket = sad_interleave[idx];
1049 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1050 idx, sad_way, *socket);
1051 } else {
1052 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001053 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001054 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1055 *socket = sad_pkg_socket(pkg);
1056 sad_ha = sad_pkg_ha(pkg);
1057 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1058 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001059 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001060
1061 /*
1062 * Move to the proper node structure, in order to access the
1063 * right PCI registers
1064 */
1065 new_mci = get_mci_for_node_id(*socket);
1066 if (!new_mci) {
1067 sprintf(msg, "Struct for socket #%u wasn't initialized",
1068 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001069 return -EINVAL;
1070 }
1071 mci = new_mci;
1072 pvt = mci->pvt_info;
1073
1074 /*
1075 * Step 2) Get memory channel
1076 */
1077 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001078 if (pvt->info.type == SANDY_BRIDGE)
1079 pci_ha = pvt->pci_ha0;
1080 else {
1081 if (sad_ha)
1082 pci_ha = pvt->pci_ha1;
1083 else
1084 pci_ha = pvt->pci_ha0;
1085 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001086 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001087 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001088 limit = TAD_LIMIT(reg);
1089 if (limit <= prv) {
1090 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001091 return -EINVAL;
1092 }
1093 if (addr <= limit)
1094 break;
1095 prv = limit;
1096 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001097 if (n_tads == MAX_TAD) {
1098 sprintf(msg, "Can't discover the memory channel");
1099 return -EINVAL;
1100 }
1101
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001102 ch_way = TAD_CH(reg) + 1;
1103 sck_way = TAD_SOCK(reg) + 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001104
1105 if (ch_way == 3)
1106 idx = addr >> 6;
1107 else
1108 idx = addr >> (6 + sck_way);
1109 idx = idx % ch_way;
1110
1111 /*
1112 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1113 */
1114 switch (idx) {
1115 case 0:
1116 base_ch = TAD_TGT0(reg);
1117 break;
1118 case 1:
1119 base_ch = TAD_TGT1(reg);
1120 break;
1121 case 2:
1122 base_ch = TAD_TGT2(reg);
1123 break;
1124 case 3:
1125 base_ch = TAD_TGT3(reg);
1126 break;
1127 default:
1128 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001129 return -EINVAL;
1130 }
1131 *channel_mask = 1 << base_ch;
1132
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001133 pci_read_config_dword(pvt->pci_tad[base_ch],
1134 tad_ch_nilv_offset[n_tads],
1135 &tad_offset);
1136
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001137 if (pvt->is_mirrored) {
1138 *channel_mask |= 1 << ((base_ch + 2) % 4);
1139 switch(ch_way) {
1140 case 2:
1141 case 4:
1142 sck_xch = 1 << sck_way * (ch_way >> 1);
1143 break;
1144 default:
1145 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001146 return -EINVAL;
1147 }
1148 } else
1149 sck_xch = (1 << sck_way) * ch_way;
1150
1151 if (pvt->is_lockstep)
1152 *channel_mask |= 1 << ((base_ch + 1) % 4);
1153
1154 offset = TAD_OFFSET(tad_offset);
1155
Joe Perches956b9ba2012-04-29 17:08:39 -03001156 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",
1157 n_tads,
1158 addr,
1159 limit,
1160 (u32)TAD_SOCK(reg),
1161 ch_way,
1162 offset,
1163 idx,
1164 base_ch,
1165 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001166
1167 /* Calculate channel address */
1168 /* Remove the TAD offset */
1169
1170 if (offset > addr) {
1171 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1172 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001173 return -EINVAL;
1174 }
1175 addr -= offset;
1176 /* Store the low bits [0:6] of the addr */
1177 ch_addr = addr & 0x7f;
1178 /* Remove socket wayness and remove 6 bits */
1179 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001180 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001181#if 0
1182 /* Divide by channel way */
1183 addr = addr / ch_way;
1184#endif
1185 /* Recover the last 6 bits */
1186 ch_addr |= addr << 6;
1187
1188 /*
1189 * Step 3) Decode rank
1190 */
1191 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1192 pci_read_config_dword(pvt->pci_tad[base_ch],
1193 rir_way_limit[n_rir],
1194 &reg);
1195
1196 if (!IS_RIR_VALID(reg))
1197 continue;
1198
1199 limit = RIR_LIMIT(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001200 mb = div_u64_rem(limit >> 20, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001201 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1202 n_rir,
1203 mb, kb,
1204 limit,
1205 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001206 if (ch_addr <= limit)
1207 break;
1208 }
1209 if (n_rir == MAX_RIR_RANGES) {
1210 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1211 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001212 return -EINVAL;
1213 }
1214 rir_way = RIR_WAY(reg);
1215 if (pvt->is_close_pg)
1216 idx = (ch_addr >> 6);
1217 else
1218 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1219 idx %= 1 << rir_way;
1220
1221 pci_read_config_dword(pvt->pci_tad[base_ch],
1222 rir_offset[n_rir][idx],
1223 &reg);
1224 *rank = RIR_RNK_TGT(reg);
1225
Joe Perches956b9ba2012-04-29 17:08:39 -03001226 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1227 n_rir,
1228 ch_addr,
1229 limit,
1230 rir_way,
1231 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001232
1233 return 0;
1234}
1235
1236/****************************************************************************
1237 Device initialization routines: put/get, init/exit
1238 ****************************************************************************/
1239
1240/*
1241 * sbridge_put_all_devices 'put' all the devices that we have
1242 * reserved via 'get'
1243 */
1244static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1245{
1246 int i;
1247
Joe Perches956b9ba2012-04-29 17:08:39 -03001248 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001249 for (i = 0; i < sbridge_dev->n_devs; i++) {
1250 struct pci_dev *pdev = sbridge_dev->pdev[i];
1251 if (!pdev)
1252 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03001253 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1254 pdev->bus->number,
1255 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001256 pci_dev_put(pdev);
1257 }
1258}
1259
1260static void sbridge_put_all_devices(void)
1261{
1262 struct sbridge_dev *sbridge_dev, *tmp;
1263
1264 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1265 sbridge_put_devices(sbridge_dev);
1266 free_sbridge_dev(sbridge_dev);
1267 }
1268}
1269
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001270static int sbridge_get_onedevice(struct pci_dev **prev,
1271 u8 *num_mc,
1272 const struct pci_id_table *table,
1273 const unsigned devno)
1274{
1275 struct sbridge_dev *sbridge_dev;
1276 const struct pci_id_descr *dev_descr = &table->descr[devno];
1277
1278 struct pci_dev *pdev = NULL;
1279 u8 bus = 0;
1280
Jiang Liuec5a0b32014-02-17 13:10:23 +08001281 sbridge_printk(KERN_DEBUG,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001282 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1283 dev_descr->dev, dev_descr->func,
1284 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1285
1286 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1287 dev_descr->dev_id, *prev);
1288
1289 if (!pdev) {
1290 if (*prev) {
1291 *prev = pdev;
1292 return 0;
1293 }
1294
1295 if (dev_descr->optional)
1296 return 0;
1297
1298 if (devno == 0)
1299 return -ENODEV;
1300
1301 sbridge_printk(KERN_INFO,
1302 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1303 dev_descr->dev, dev_descr->func,
1304 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1305
1306 /* End of list, leave */
1307 return -ENODEV;
1308 }
1309 bus = pdev->bus->number;
1310
1311 sbridge_dev = get_sbridge_dev(bus);
1312 if (!sbridge_dev) {
1313 sbridge_dev = alloc_sbridge_dev(bus, table);
1314 if (!sbridge_dev) {
1315 pci_dev_put(pdev);
1316 return -ENOMEM;
1317 }
1318 (*num_mc)++;
1319 }
1320
1321 if (sbridge_dev->pdev[devno]) {
1322 sbridge_printk(KERN_ERR,
1323 "Duplicated device for "
1324 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1325 bus, dev_descr->dev, dev_descr->func,
1326 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1327 pci_dev_put(pdev);
1328 return -ENODEV;
1329 }
1330
1331 sbridge_dev->pdev[devno] = pdev;
1332
1333 /* Sanity check */
1334 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1335 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1336 sbridge_printk(KERN_ERR,
1337 "Device PCI ID %04x:%04x "
1338 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1339 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1340 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1341 bus, dev_descr->dev, dev_descr->func);
1342 return -ENODEV;
1343 }
1344
1345 /* Be sure that the device is enabled */
1346 if (unlikely(pci_enable_device(pdev) < 0)) {
1347 sbridge_printk(KERN_ERR,
1348 "Couldn't enable "
1349 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1350 bus, dev_descr->dev, dev_descr->func,
1351 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1352 return -ENODEV;
1353 }
1354
Joe Perches956b9ba2012-04-29 17:08:39 -03001355 edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1356 bus, dev_descr->dev, dev_descr->func,
1357 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001358
1359 /*
1360 * As stated on drivers/pci/search.c, the reference count for
1361 * @from is always decremented if it is not %NULL. So, as we need
1362 * to get all devices up to null, we need to do a get for the device
1363 */
1364 pci_dev_get(pdev);
1365
1366 *prev = pdev;
1367
1368 return 0;
1369}
1370
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001371/*
1372 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
1373 * device/functions we want to reference for this driver.
1374 * Need to 'get' device 16 func 1 and func 2.
1375 * @num_mc: pointer to the memory controllers count, to be incremented in case
1376 * of success.
1377 * @table: model specific table
1378 *
1379 * returns 0 in case of success or error code
1380 */
1381static int sbridge_get_all_devices(u8 *num_mc,
1382 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001383{
1384 int i, rc;
1385 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001386
1387 while (table && table->descr) {
1388 for (i = 0; i < table->n_devs; i++) {
1389 pdev = NULL;
1390 do {
1391 rc = sbridge_get_onedevice(&pdev, num_mc,
1392 table, i);
1393 if (rc < 0) {
1394 if (i == 0) {
1395 i = table->n_devs;
1396 break;
1397 }
1398 sbridge_put_all_devices();
1399 return -ENODEV;
1400 }
1401 } while (pdev);
1402 }
1403 table++;
1404 }
1405
1406 return 0;
1407}
1408
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001409static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1410 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001411{
1412 struct sbridge_pvt *pvt = mci->pvt_info;
1413 struct pci_dev *pdev;
1414 int i, func, slot;
1415
1416 for (i = 0; i < sbridge_dev->n_devs; i++) {
1417 pdev = sbridge_dev->pdev[i];
1418 if (!pdev)
1419 continue;
1420 slot = PCI_SLOT(pdev->devfn);
1421 func = PCI_FUNC(pdev->devfn);
1422 switch (slot) {
1423 case 12:
1424 switch (func) {
1425 case 6:
1426 pvt->pci_sad0 = pdev;
1427 break;
1428 case 7:
1429 pvt->pci_sad1 = pdev;
1430 break;
1431 default:
1432 goto error;
1433 }
1434 break;
1435 case 13:
1436 switch (func) {
1437 case 6:
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -03001438 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001439 break;
1440 default:
1441 goto error;
1442 }
1443 break;
1444 case 14:
1445 switch (func) {
1446 case 0:
1447 pvt->pci_ha0 = pdev;
1448 break;
1449 default:
1450 goto error;
1451 }
1452 break;
1453 case 15:
1454 switch (func) {
1455 case 0:
1456 pvt->pci_ta = pdev;
1457 break;
1458 case 1:
1459 pvt->pci_ras = pdev;
1460 break;
1461 case 2:
1462 case 3:
1463 case 4:
1464 case 5:
1465 pvt->pci_tad[func - 2] = pdev;
1466 break;
1467 default:
1468 goto error;
1469 }
1470 break;
1471 case 17:
1472 switch (func) {
1473 case 0:
1474 pvt->pci_ddrio = pdev;
1475 break;
1476 default:
1477 goto error;
1478 }
1479 break;
1480 default:
1481 goto error;
1482 }
1483
Joe Perches956b9ba2012-04-29 17:08:39 -03001484 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1485 sbridge_dev->bus,
1486 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1487 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001488 }
1489
1490 /* Check if everything were registered */
1491 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07001492 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001493 goto enodev;
1494
1495 for (i = 0; i < NUM_CHANNELS; i++) {
1496 if (!pvt->pci_tad[i])
1497 goto enodev;
1498 }
1499 return 0;
1500
1501enodev:
1502 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1503 return -ENODEV;
1504
1505error:
1506 sbridge_printk(KERN_ERR, "Device %d, function %d "
1507 "is out of the expected range\n",
1508 slot, func);
1509 return -EINVAL;
1510}
1511
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001512static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1513 struct sbridge_dev *sbridge_dev)
1514{
1515 struct sbridge_pvt *pvt = mci->pvt_info;
1516 struct pci_dev *pdev, *tmp;
1517 int i, func, slot;
1518 bool mode_2ha = false;
1519
1520 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1521 PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, NULL);
1522 if (tmp) {
1523 mode_2ha = true;
1524 pci_dev_put(tmp);
1525 }
1526
1527 for (i = 0; i < sbridge_dev->n_devs; i++) {
1528 pdev = sbridge_dev->pdev[i];
1529 if (!pdev)
1530 continue;
1531 slot = PCI_SLOT(pdev->devfn);
1532 func = PCI_FUNC(pdev->devfn);
1533
1534 switch (slot) {
1535 case 14:
1536 if (func == 0) {
1537 pvt->pci_ha0 = pdev;
1538 break;
1539 }
1540 goto error;
1541 case 15:
1542 switch (func) {
1543 case 0:
1544 pvt->pci_ta = pdev;
1545 break;
1546 case 1:
1547 pvt->pci_ras = pdev;
1548 break;
1549 case 4:
1550 case 5:
1551 /* if we have 2 HAs active, channels 2 and 3
1552 * are in other device */
1553 if (mode_2ha)
1554 break;
1555 /* fall through */
1556 case 2:
1557 case 3:
1558 pvt->pci_tad[func - 2] = pdev;
1559 break;
1560 default:
1561 goto error;
1562 }
1563 break;
1564 case 17:
1565 if (func == 4) {
1566 pvt->pci_ddrio = pdev;
1567 break;
1568 } else if (func == 0) {
1569 if (!mode_2ha)
1570 pvt->pci_ddrio = pdev;
1571 break;
1572 }
1573 goto error;
1574 case 22:
1575 switch (func) {
1576 case 0:
1577 pvt->pci_sad0 = pdev;
1578 break;
1579 case 1:
1580 pvt->pci_br0 = pdev;
1581 break;
1582 case 2:
1583 pvt->pci_br1 = pdev;
1584 break;
1585 default:
1586 goto error;
1587 }
1588 break;
1589 case 28:
1590 if (func == 0) {
1591 pvt->pci_ha1 = pdev;
1592 break;
1593 }
1594 goto error;
1595 case 29:
1596 /* we shouldn't have this device if we have just one
1597 * HA present */
1598 WARN_ON(!mode_2ha);
1599 if (func == 2 || func == 3) {
1600 pvt->pci_tad[func] = pdev;
1601 break;
1602 }
1603 goto error;
1604 default:
1605 goto error;
1606 }
1607
1608 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1609 sbridge_dev->bus,
1610 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1611 pdev);
1612 }
1613
1614 /* Check if everything were registered */
1615 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1616 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
1617 !pvt->pci_ta)
1618 goto enodev;
1619
1620 for (i = 0; i < NUM_CHANNELS; i++) {
1621 if (!pvt->pci_tad[i])
1622 goto enodev;
1623 }
1624 return 0;
1625
1626enodev:
1627 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1628 return -ENODEV;
1629
1630error:
1631 sbridge_printk(KERN_ERR,
1632 "Device %d, function %d is out of the expected range\n",
1633 slot, func);
1634 return -EINVAL;
1635}
1636
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001637/****************************************************************************
1638 Error check routines
1639 ****************************************************************************/
1640
1641/*
1642 * While Sandy Bridge has error count registers, SMI BIOS read values from
1643 * and resets the counters. So, they are not reliable for the OS to read
1644 * from them. So, we have no option but to just trust on whatever MCE is
1645 * telling us about the errors.
1646 */
1647static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1648 const struct mce *m)
1649{
1650 struct mem_ctl_info *new_mci;
1651 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001652 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001653 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001654 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1655 bool overflow = GET_BITFIELD(m->status, 62, 62);
1656 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001657 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001658 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1659 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1660 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1661 u32 channel = GET_BITFIELD(m->status, 0, 3);
1662 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1663 long channel_mask, first_channel;
1664 u8 rank, socket;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001665 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001666 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001667
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001668 if (pvt->info.type == IVY_BRIDGE)
1669 recoverable = true;
1670 else
1671 recoverable = GET_BITFIELD(m->status, 56, 56);
1672
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001673 if (uncorrected_error) {
1674 if (ripv) {
1675 type = "FATAL";
1676 tp_event = HW_EVENT_ERR_FATAL;
1677 } else {
1678 type = "NON_FATAL";
1679 tp_event = HW_EVENT_ERR_UNCORRECTED;
1680 }
1681 } else {
1682 type = "CORRECTED";
1683 tp_event = HW_EVENT_ERR_CORRECTED;
1684 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001685
1686 /*
David Mackey15ed1032012-04-17 11:30:52 -07001687 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001688 * memory errors should fit in this mask:
1689 * 000f 0000 1mmm cccc (binary)
1690 * where:
1691 * f = Correction Report Filtering Bit. If 1, subsequent errors
1692 * won't be shown
1693 * mmm = error type
1694 * cccc = channel
1695 * If the mask doesn't match, report an error to the parsing logic
1696 */
1697 if (! ((errcode & 0xef80) == 0x80)) {
1698 optype = "Can't parse: it is not a mem";
1699 } else {
1700 switch (optypenum) {
1701 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001702 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001703 break;
1704 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001705 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001706 break;
1707 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001708 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001709 break;
1710 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001711 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001712 break;
1713 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001714 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001715 break;
1716 default:
1717 optype = "reserved";
1718 break;
1719 }
1720 }
1721
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03001722 /* Only decode errors with an valid address (ADDRV) */
1723 if (!GET_BITFIELD(m->status, 58, 58))
1724 return;
1725
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001726 rc = get_memory_error_data(mci, m->addr, &socket,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001727 &channel_mask, &rank, &area_type, msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001728 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001729 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001730 new_mci = get_mci_for_node_id(socket);
1731 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001732 strcpy(msg, "Error: socket got corrupted!");
1733 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001734 }
1735 mci = new_mci;
1736 pvt = mci->pvt_info;
1737
1738 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1739
1740 if (rank < 4)
1741 dimm = 0;
1742 else if (rank < 8)
1743 dimm = 1;
1744 else
1745 dimm = 2;
1746
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001747
1748 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001749 * FIXME: On some memory configurations (mirror, lockstep), the
1750 * Memory Controller can't point the error to a single DIMM. The
1751 * EDAC core should be handling the channel mask, in order to point
1752 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001753 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001754 snprintf(msg, sizeof(msg),
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001755 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001756 overflow ? " OVERFLOW" : "",
1757 (uncorrected_error && recoverable) ? " recoverable" : "",
1758 area_type,
1759 mscod, errcode,
1760 socket,
1761 channel_mask,
1762 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001763
Joe Perches956b9ba2012-04-29 17:08:39 -03001764 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001765
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001766 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001767
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001768 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001769 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001770 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1771 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001772 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001773 return;
1774err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001775 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001776 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001777 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001778
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001779}
1780
1781/*
1782 * sbridge_check_error Retrieve and process errors reported by the
1783 * hardware. Called by the Core module.
1784 */
1785static void sbridge_check_error(struct mem_ctl_info *mci)
1786{
1787 struct sbridge_pvt *pvt = mci->pvt_info;
1788 int i;
1789 unsigned count = 0;
1790 struct mce *m;
1791
1792 /*
1793 * MCE first step: Copy all mce errors into a temporary buffer
1794 * We use a double buffering here, to reduce the risk of
1795 * loosing an error.
1796 */
1797 smp_rmb();
1798 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1799 % MCE_LOG_LEN;
1800 if (!count)
1801 return;
1802
1803 m = pvt->mce_outentry;
1804 if (pvt->mce_in + count > MCE_LOG_LEN) {
1805 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1806
1807 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1808 smp_wmb();
1809 pvt->mce_in = 0;
1810 count -= l;
1811 m += l;
1812 }
1813 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1814 smp_wmb();
1815 pvt->mce_in += count;
1816
1817 smp_rmb();
1818 if (pvt->mce_overrun) {
1819 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
1820 pvt->mce_overrun);
1821 smp_wmb();
1822 pvt->mce_overrun = 0;
1823 }
1824
1825 /*
1826 * MCE second step: parse errors and display
1827 */
1828 for (i = 0; i < count; i++)
1829 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
1830}
1831
1832/*
1833 * sbridge_mce_check_error Replicates mcelog routine to get errors
1834 * This routine simply queues mcelog errors, and
1835 * return. The error itself should be handled later
1836 * by sbridge_check_error.
1837 * WARNING: As this routine should be called at NMI time, extra care should
1838 * be taken to avoid deadlocks, and to be as fast as possible.
1839 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001840static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
1841 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001842{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001843 struct mce *mce = (struct mce *)data;
1844 struct mem_ctl_info *mci;
1845 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04001846 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001847
Chen, Gongfd521032013-12-06 01:17:09 -05001848 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
1849 return NOTIFY_DONE;
1850
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001851 mci = get_mci_for_node_id(mce->socketid);
1852 if (!mci)
1853 return NOTIFY_BAD;
1854 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001855
1856 /*
1857 * Just let mcelog handle it if the error is
1858 * outside the memory controller. A memory error
1859 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1860 * bit 12 has an special meaning.
1861 */
1862 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001863 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001864
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04001865 if (mce->mcgstatus & MCG_STATUS_MCIP)
1866 type = "Exception";
1867 else
1868 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001869
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001870 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001871
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001872 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
1873 "Bank %d: %016Lx\n", mce->extcpu, type,
1874 mce->mcgstatus, mce->bank, mce->status);
1875 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
1876 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
1877 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001878
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001879 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
1880 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
1881 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001882
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001883 /* Only handle if it is the right mc controller */
1884 if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001885 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001886
1887 smp_rmb();
1888 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1889 smp_wmb();
1890 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001891 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001892 }
1893
1894 /* Copy memory error at the ringbuffer */
1895 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1896 smp_wmb();
1897 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1898
1899 /* Handle fatal errors immediately */
1900 if (mce->mcgstatus & 1)
1901 sbridge_check_error(mci);
1902
1903 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001904 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001905}
1906
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001907static struct notifier_block sbridge_mce_dec = {
1908 .notifier_call = sbridge_mce_check_error,
1909};
1910
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001911/****************************************************************************
1912 EDAC register/unregister logic
1913 ****************************************************************************/
1914
1915static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
1916{
1917 struct mem_ctl_info *mci = sbridge_dev->mci;
1918 struct sbridge_pvt *pvt;
1919
1920 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001921 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001922
1923 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
1924 return;
1925 }
1926
1927 pvt = mci->pvt_info;
1928
Joe Perches956b9ba2012-04-29 17:08:39 -03001929 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1930 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001931
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001932 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03001933 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001934
Joe Perches956b9ba2012-04-29 17:08:39 -03001935 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001936 kfree(mci->ctl_name);
1937 edac_mc_free(mci);
1938 sbridge_dev->mci = NULL;
1939}
1940
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001941static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001942{
1943 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001944 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001945 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001946 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001947 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001948
1949 /* Check the number of active and not disabled channels */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001950 rc = check_if_ecc_is_active(sbridge_dev->bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001951 if (unlikely(rc < 0))
1952 return rc;
1953
1954 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001955 layers[0].type = EDAC_MC_LAYER_CHANNEL;
1956 layers[0].size = NUM_CHANNELS;
1957 layers[0].is_virt_csrow = false;
1958 layers[1].type = EDAC_MC_LAYER_SLOT;
1959 layers[1].size = MAX_DIMMS;
1960 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03001961 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001962 sizeof(*pvt));
1963
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001964 if (unlikely(!mci))
1965 return -ENOMEM;
1966
Joe Perches956b9ba2012-04-29 17:08:39 -03001967 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001968 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001969
1970 pvt = mci->pvt_info;
1971 memset(pvt, 0, sizeof(*pvt));
1972
1973 /* Associate sbridge_dev and mci for future usage */
1974 pvt->sbridge_dev = sbridge_dev;
1975 sbridge_dev->mci = mci;
1976
1977 mci->mtype_cap = MEM_FLAG_DDR3;
1978 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1979 mci->edac_cap = EDAC_FLAG_NONE;
1980 mci->mod_name = "sbridge_edac.c";
1981 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001982 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001983 mci->ctl_page_to_phys = NULL;
1984
1985 /* Set the function pointer to an actual operation function */
1986 mci->edac_check = sbridge_check_error;
1987
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001988 pvt->info.type = type;
1989 if (type == IVY_BRIDGE) {
1990 pvt->info.rankcfgr = IB_RANK_CFG_A;
1991 pvt->info.get_tolm = ibridge_get_tolm;
1992 pvt->info.get_tohm = ibridge_get_tohm;
1993 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001994 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03001995 pvt->info.get_node_id = get_node_id;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001996 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
1997 pvt->info.interleave_list = ibridge_interleave_list;
1998 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
1999 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2000 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
2001
2002 /* Store pci devices at mci for faster access */
2003 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
2004 if (unlikely(rc < 0))
2005 goto fail0;
2006 } else {
2007 pvt->info.rankcfgr = SB_RANK_CFG_A;
2008 pvt->info.get_tolm = sbridge_get_tolm;
2009 pvt->info.get_tohm = sbridge_get_tohm;
2010 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002011 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002012 pvt->info.get_node_id = get_node_id;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002013 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2014 pvt->info.interleave_list = sbridge_interleave_list;
2015 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2016 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2017 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2018
2019 /* Store pci devices at mci for faster access */
2020 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2021 if (unlikely(rc < 0))
2022 goto fail0;
2023 }
2024
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002025
2026 /* Get dimm basic config and the memory layout */
2027 get_dimm_config(mci);
2028 get_memory_layout(mci);
2029
2030 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002031 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002032
2033 /* add this new MC control structure to EDAC's list of MCs */
2034 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002035 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002036 rc = -EINVAL;
2037 goto fail0;
2038 }
2039
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002040 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002041
2042fail0:
2043 kfree(mci->ctl_name);
2044 edac_mc_free(mci);
2045 sbridge_dev->mci = NULL;
2046 return rc;
2047}
2048
2049/*
2050 * sbridge_probe Probe for ONE instance of device to see if it is
2051 * present.
2052 * return:
2053 * 0 for FOUND a device
2054 * < 0 for error code
2055 */
2056
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002057static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002058{
2059 int rc;
2060 u8 mc, num_mc = 0;
2061 struct sbridge_dev *sbridge_dev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002062 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002063
2064 /* get the pci devices we want to reserve for our use */
2065 mutex_lock(&sbridge_edac_lock);
2066
2067 /*
2068 * All memory controllers are allocated at the first pass.
2069 */
2070 if (unlikely(probed >= 1)) {
2071 mutex_unlock(&sbridge_edac_lock);
2072 return -ENODEV;
2073 }
2074 probed++;
2075
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002076 if (pdev->device == PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA) {
2077 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2078 type = IVY_BRIDGE;
2079 } else {
2080 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2081 type = SANDY_BRIDGE;
2082 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002083 if (unlikely(rc < 0))
2084 goto fail0;
2085 mc = 0;
2086
2087 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002088 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2089 mc, mc + 1, num_mc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002090 sbridge_dev->mc = mc++;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002091 rc = sbridge_register_mci(sbridge_dev, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002092 if (unlikely(rc < 0))
2093 goto fail1;
2094 }
2095
2096 sbridge_printk(KERN_INFO, "Driver loaded.\n");
2097
2098 mutex_unlock(&sbridge_edac_lock);
2099 return 0;
2100
2101fail1:
2102 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2103 sbridge_unregister_mci(sbridge_dev);
2104
2105 sbridge_put_all_devices();
2106fail0:
2107 mutex_unlock(&sbridge_edac_lock);
2108 return rc;
2109}
2110
2111/*
2112 * sbridge_remove destructor for one instance of device
2113 *
2114 */
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002115static void sbridge_remove(struct pci_dev *pdev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002116{
2117 struct sbridge_dev *sbridge_dev;
2118
Joe Perches956b9ba2012-04-29 17:08:39 -03002119 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002120
2121 /*
2122 * we have a trouble here: pdev value for removal will be wrong, since
2123 * it will point to the X58 register used to detect that the machine
2124 * is a Nehalem or upper design. However, due to the way several PCI
2125 * devices are grouped together to provide MC functionality, we need
2126 * to use a different method for releasing the devices
2127 */
2128
2129 mutex_lock(&sbridge_edac_lock);
2130
2131 if (unlikely(!probed)) {
2132 mutex_unlock(&sbridge_edac_lock);
2133 return;
2134 }
2135
2136 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2137 sbridge_unregister_mci(sbridge_dev);
2138
2139 /* Release PCI resources */
2140 sbridge_put_all_devices();
2141
2142 probed--;
2143
2144 mutex_unlock(&sbridge_edac_lock);
2145}
2146
2147MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2148
2149/*
2150 * sbridge_driver pci_driver structure for this module
2151 *
2152 */
2153static struct pci_driver sbridge_driver = {
2154 .name = "sbridge_edac",
2155 .probe = sbridge_probe,
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002156 .remove = sbridge_remove,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002157 .id_table = sbridge_pci_tbl,
2158};
2159
2160/*
2161 * sbridge_init Module entry function
2162 * Try to initialize this module for its devices
2163 */
2164static int __init sbridge_init(void)
2165{
2166 int pci_rc;
2167
Joe Perches956b9ba2012-04-29 17:08:39 -03002168 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002169
2170 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2171 opstate_init();
2172
2173 pci_rc = pci_register_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002174 if (pci_rc >= 0) {
2175 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05002176 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2177 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002178 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03002179 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002180
2181 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2182 pci_rc);
2183
2184 return pci_rc;
2185}
2186
2187/*
2188 * sbridge_exit() Module exit function
2189 * Unregister the driver
2190 */
2191static void __exit sbridge_exit(void)
2192{
Joe Perches956b9ba2012-04-29 17:08:39 -03002193 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002194 pci_unregister_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002195 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002196}
2197
2198module_init(sbridge_init);
2199module_exit(sbridge_exit);
2200
2201module_param(edac_op_state, int, 0444);
2202MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2203
2204MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002205MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002206MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002207MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002208 SBRIDGE_REVISION);