blob: 44336cb0a5613612380aecb23a8a272a17008c67 [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
179#define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
180
181/* Device 14 function 0 */
182
183static const u32 tad_dram_rule[] = {
184 0x40, 0x44, 0x48, 0x4c,
185 0x50, 0x54, 0x58, 0x5c,
186 0x60, 0x64, 0x68, 0x6c,
187};
188#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
189
190#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
191#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
192#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
193#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
194#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
195#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
196#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
197
198/* Device 15, function 0 */
199
200#define MCMTR 0x7c
201
202#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
203#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
204#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
205
206/* Device 15, function 1 */
207
208#define RASENABLES 0xac
209#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
210
211/* Device 15, functions 2-5 */
212
213static const int mtr_regs[] = {
214 0x80, 0x84, 0x88,
215};
216
217#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
218#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
219#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
220#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
221#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
222
223static const u32 tad_ch_nilv_offset[] = {
224 0x90, 0x94, 0x98, 0x9c,
225 0xa0, 0xa4, 0xa8, 0xac,
226 0xb0, 0xb4, 0xb8, 0xbc,
227};
228#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
229#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
230
231static const u32 rir_way_limit[] = {
232 0x108, 0x10c, 0x110, 0x114, 0x118,
233};
234#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
235
236#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
237#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
238#define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
239
240#define MAX_RIR_WAY 8
241
242static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
243 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
244 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
245 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
246 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
247 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
248};
249
250#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
251#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
252
253/* Device 16, functions 2-7 */
254
255/*
256 * FIXME: Implement the error count reads directly
257 */
258
259static const u32 correrrcnt[] = {
260 0x104, 0x108, 0x10c, 0x110,
261};
262
263#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
264#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
265#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
266#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
267
268static const u32 correrrthrsld[] = {
269 0x11c, 0x120, 0x124, 0x128,
270};
271
272#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
273#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
274
275
276/* Device 17, function 0 */
277
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300278#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200279
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300280#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200281
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200282/*
283 * sbridge structs
284 */
285
286#define NUM_CHANNELS 4
287#define MAX_DIMMS 3 /* Max DIMMS per channel */
288
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300289enum type {
290 SANDY_BRIDGE,
291 IVY_BRIDGE,
292};
293
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300294struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200295struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300296 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300297 u32 mcmtr;
298 u32 rankcfgr;
299 u64 (*get_tolm)(struct sbridge_pvt *pvt);
300 u64 (*get_tohm)(struct sbridge_pvt *pvt);
301 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300302 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300303 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300304 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300305 u8 max_interleave;
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300306 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200307};
308
309struct sbridge_channel {
310 u32 ranks;
311 u32 dimms;
312};
313
314struct pci_id_descr {
315 int dev;
316 int func;
317 int dev_id;
318 int optional;
319};
320
321struct pci_id_table {
322 const struct pci_id_descr *descr;
323 int n_devs;
324};
325
326struct sbridge_dev {
327 struct list_head list;
328 u8 bus, mc;
329 u8 node_id, source_id;
330 struct pci_dev **pdev;
331 int n_devs;
332 struct mem_ctl_info *mci;
333};
334
335struct sbridge_pvt {
336 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300337 struct pci_dev *pci_sad0, *pci_sad1;
338 struct pci_dev *pci_ha0, *pci_ha1;
339 struct pci_dev *pci_br0, *pci_br1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200340 struct pci_dev *pci_tad[NUM_CHANNELS];
341
342 struct sbridge_dev *sbridge_dev;
343
344 struct sbridge_info info;
345 struct sbridge_channel channel[NUM_CHANNELS];
346
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200347 /* Memory type detection */
348 bool is_mirrored, is_lockstep, is_close_pg;
349
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200350 /* Fifo double buffers */
351 struct mce mce_entry[MCE_LOG_LEN];
352 struct mce mce_outentry[MCE_LOG_LEN];
353
354 /* Fifo in/out counters */
355 unsigned mce_in, mce_out;
356
357 /* Count indicator to show errors not got */
358 unsigned mce_overrun;
359
360 /* Memory description */
361 u64 tolm, tohm;
362};
363
Luck, Tonyde4772c2013-03-28 09:59:15 -0700364#define PCI_DESCR(device, function, device_id, opt) \
365 .dev = (device), \
366 .func = (function), \
367 .dev_id = (device_id), \
368 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200369
370static const struct pci_id_descr pci_dev_descr_sbridge[] = {
371 /* Processor Home Agent */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700372 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200373
374 /* Memory controller */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700375 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
376 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
377 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
378 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
379 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
380 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
381 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200382
383 /* System Address Decoder */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700384 { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
385 { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200386
387 /* Broadcast Registers */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700388 { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200389};
390
391#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
392static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
393 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
394 {0,} /* 0 terminated list. */
395};
396
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300397/* This changes depending if 1HA or 2HA:
398 * 1HA:
399 * 0x0eb8 (17.0) is DDRIO0
400 * 2HA:
401 * 0x0ebc (17.4) is DDRIO0
402 */
403#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
404#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
405
406/* pci ids */
407#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
408#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
409#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
410#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
411#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
412#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
413#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
414#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
415#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
416#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
417#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
418#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
419#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
420#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
421#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
422
423static const struct pci_id_descr pci_dev_descr_ibridge[] = {
424 /* Processor Home Agent */
425 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
426
427 /* Memory controller */
428 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
429 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
430 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
431 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
432 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
433 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
434
435 /* System Address Decoder */
436 { PCI_DESCR(22, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
437
438 /* Broadcast Registers */
439 { PCI_DESCR(22, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
440 { PCI_DESCR(22, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
441
442 /* Optional, mode 2HA */
443 { PCI_DESCR(28, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
444#if 0
445 { PCI_DESCR(29, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
446 { PCI_DESCR(29, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
447#endif
448 { PCI_DESCR(29, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
449 { PCI_DESCR(29, 3, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
450
451 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
452 { PCI_DESCR(17, 4, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
453};
454
455static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
456 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
457 {0,} /* 0 terminated list. */
458};
459
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200460/*
461 * pci_device_id table for which devices we are looking for
462 */
Jingoo Hanba935f42013-12-06 10:23:08 +0100463static const struct pci_device_id sbridge_pci_tbl[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200464 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300465 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200466 {0,} /* 0 terminated list. */
467};
468
469
470/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700471 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200472 ****************************************************************************/
473
474static inline int numrank(u32 mtr)
475{
476 int ranks = (1 << RANK_CNT_BITS(mtr));
477
478 if (ranks > 4) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300479 edac_dbg(0, "Invalid number of ranks: %d (max = 4) raw value = %x (%04x)\n",
480 ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200481 return -EINVAL;
482 }
483
484 return ranks;
485}
486
487static inline int numrow(u32 mtr)
488{
489 int rows = (RANK_WIDTH_BITS(mtr) + 12);
490
491 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300492 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
493 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200494 return -EINVAL;
495 }
496
497 return 1 << rows;
498}
499
500static inline int numcol(u32 mtr)
501{
502 int cols = (COL_WIDTH_BITS(mtr) + 10);
503
504 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300505 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
506 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200507 return -EINVAL;
508 }
509
510 return 1 << cols;
511}
512
513static struct sbridge_dev *get_sbridge_dev(u8 bus)
514{
515 struct sbridge_dev *sbridge_dev;
516
517 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
518 if (sbridge_dev->bus == bus)
519 return sbridge_dev;
520 }
521
522 return NULL;
523}
524
525static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
526 const struct pci_id_table *table)
527{
528 struct sbridge_dev *sbridge_dev;
529
530 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
531 if (!sbridge_dev)
532 return NULL;
533
534 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
535 GFP_KERNEL);
536 if (!sbridge_dev->pdev) {
537 kfree(sbridge_dev);
538 return NULL;
539 }
540
541 sbridge_dev->bus = bus;
542 sbridge_dev->n_devs = table->n_devs;
543 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
544
545 return sbridge_dev;
546}
547
548static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
549{
550 list_del(&sbridge_dev->list);
551 kfree(sbridge_dev->pdev);
552 kfree(sbridge_dev);
553}
554
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300555static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
556{
557 u32 reg;
558
559 /* Address range is 32:28 */
560 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
561 return GET_TOLM(reg);
562}
563
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300564static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
565{
566 u32 reg;
567
568 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
569 return GET_TOHM(reg);
570}
571
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300572static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
573{
574 u32 reg;
575
576 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
577
578 return GET_TOLM(reg);
579}
580
581static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
582{
583 u32 reg;
584
585 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
586
587 return GET_TOHM(reg);
588}
589
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300590static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
591{
592 u32 reg;
593 enum mem_type mtype;
594
595 if (pvt->pci_ddrio) {
596 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
597 &reg);
598 if (GET_BITFIELD(reg, 11, 11))
599 /* FIXME: Can also be LRDIMM */
600 mtype = MEM_RDDR3;
601 else
602 mtype = MEM_DDR3;
603 } else
604 mtype = MEM_UNKNOWN;
605
606 return mtype;
607}
608
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300609static inline u8 sad_pkg_socket(u8 pkg)
610{
611 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
612 return (pkg >> 3) | (pkg & 0x3);
613}
614
615static inline u8 sad_pkg_ha(u8 pkg)
616{
617 return (pkg >> 2) & 0x1;
618}
619
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200620/****************************************************************************
621 Memory check routines
622 ****************************************************************************/
623static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
624 unsigned func)
625{
626 struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
627 int i;
628
629 if (!sbridge_dev)
630 return NULL;
631
632 for (i = 0; i < sbridge_dev->n_devs; i++) {
633 if (!sbridge_dev->pdev[i])
634 continue;
635
636 if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
637 PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300638 edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
639 bus, slot, func, sbridge_dev->pdev[i]);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200640 return sbridge_dev->pdev[i];
641 }
642 }
643
644 return NULL;
645}
646
647/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300648 * check_if_ecc_is_active() - Checks if ECC is active
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200649 * bus: Device bus
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200650 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300651static int check_if_ecc_is_active(const u8 bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200652{
653 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200654 u32 mcmtr;
655
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200656 pdev = get_pdev_slot_func(bus, 15, 0);
657 if (!pdev) {
658 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
659 "%2x.%02d.%d!!!\n",
660 bus, 15, 0);
661 return -ENODEV;
662 }
663
664 pci_read_config_dword(pdev, MCMTR, &mcmtr);
665 if (!IS_ECC_ENABLED(mcmtr)) {
666 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
667 return -ENODEV;
668 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200669 return 0;
670}
671
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300672static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200673{
674 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300675 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300676 unsigned i, j, banks, ranks, rows, cols, npages;
677 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200678 u32 reg;
679 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200680 enum mem_type mtype;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200681
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300682 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200683 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
684
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300685 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200686 pvt->sbridge_dev->node_id = NODE_ID(reg);
Joe Perches956b9ba2012-04-29 17:08:39 -0300687 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
688 pvt->sbridge_dev->mc,
689 pvt->sbridge_dev->node_id,
690 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200691
692 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
693 if (IS_MIRROR_ENABLED(reg)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300694 edac_dbg(0, "Memory mirror is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200695 pvt->is_mirrored = true;
696 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300697 edac_dbg(0, "Memory mirror is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200698 pvt->is_mirrored = false;
699 }
700
701 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
702 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300703 edac_dbg(0, "Lockstep is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200704 mode = EDAC_S8ECD8ED;
705 pvt->is_lockstep = true;
706 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300707 edac_dbg(0, "Lockstep is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200708 mode = EDAC_S4ECD4ED;
709 pvt->is_lockstep = false;
710 }
711 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300712 edac_dbg(0, "address map is on closed page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200713 pvt->is_close_pg = true;
714 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300715 edac_dbg(0, "address map is on open page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200716 pvt->is_close_pg = false;
717 }
718
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300719 mtype = pvt->info.get_memory_type(pvt);
720 if (mtype == MEM_RDDR3)
721 edac_dbg(0, "Memory is registered\n");
722 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -0700723 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300724 else
725 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200726
727 /* On all supported DDR3 DIMM types, there are 8 banks available */
728 banks = 8;
729
730 for (i = 0; i < NUM_CHANNELS; i++) {
731 u32 mtr;
732
733 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300734 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
735 i, j, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200736 pci_read_config_dword(pvt->pci_tad[i],
737 mtr_regs[j], &mtr);
Joe Perches956b9ba2012-04-29 17:08:39 -0300738 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200739 if (IS_DIMM_PRESENT(mtr)) {
740 pvt->channel[i].dimms++;
741
742 ranks = numrank(mtr);
743 rows = numrow(mtr);
744 cols = numcol(mtr);
745
746 /* DDR3 has 8 I/O banks */
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300747 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200748 npages = MiB_TO_PAGES(size);
749
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300750 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 -0300751 pvt->sbridge_dev->mc, i, j,
752 size, npages,
753 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200754
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300755 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300756 dimm->grain = 32;
757 dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
758 dimm->mtype = mtype;
759 dimm->edac_mode = mode;
760 snprintf(dimm->label, sizeof(dimm->label),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200761 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
762 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200763 }
764 }
765 }
766
767 return 0;
768}
769
770static void get_memory_layout(const struct mem_ctl_info *mci)
771{
772 struct sbridge_pvt *pvt = mci->pvt_info;
773 int i, j, k, n_sads, n_tads, sad_interl;
774 u32 reg;
775 u64 limit, prv = 0;
776 u64 tmp_mb;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300777 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200778 u32 rir_way;
779
780 /*
781 * Step 1) Get TOLM/TOHM ranges
782 */
783
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300784 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200785 tmp_mb = (1 + pvt->tolm) >> 20;
786
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300787 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300788 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200789
790 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300791 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200792 tmp_mb = (1 + pvt->tohm) >> 20;
793
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300794 mb = div_u64_rem(tmp_mb, 1000, &kb);
Mauro Carvalho Chehabda14d932012-10-25 09:07:21 -0200795 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200796
797 /*
798 * Step 2) Get SAD range and SAD Interleave list
799 * TAD registers contain the interleave wayness. However, it
800 * seems simpler to just discover it indirectly, with the
801 * algorithm bellow.
802 */
803 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300804 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200805 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300806 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200807 &reg);
808 limit = SAD_LIMIT(reg);
809
810 if (!DRAM_RULE_ENABLE(reg))
811 continue;
812
813 if (limit <= prv)
814 break;
815
816 tmp_mb = (limit + 1) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300817 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300818 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
819 n_sads,
820 get_dram_attr(reg),
821 mb, kb,
822 ((u64)tmp_mb) << 20L,
823 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
824 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200825 prv = limit;
826
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300827 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200828 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300829 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200830 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300831 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
832 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200833 break;
834
Joe Perches956b9ba2012-04-29 17:08:39 -0300835 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300836 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200837 }
838 }
839
840 /*
841 * Step 3) Get TAD range
842 */
843 prv = 0;
844 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
845 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
846 &reg);
847 limit = TAD_LIMIT(reg);
848 if (limit <= prv)
849 break;
850 tmp_mb = (limit + 1) >> 20;
851
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300852 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300853 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",
854 n_tads, mb, kb,
855 ((u64)tmp_mb) << 20L,
856 (u32)TAD_SOCK(reg),
857 (u32)TAD_CH(reg),
858 (u32)TAD_TGT0(reg),
859 (u32)TAD_TGT1(reg),
860 (u32)TAD_TGT2(reg),
861 (u32)TAD_TGT3(reg),
862 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -0300863 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200864 }
865
866 /*
867 * Step 4) Get TAD offsets, per each channel
868 */
869 for (i = 0; i < NUM_CHANNELS; i++) {
870 if (!pvt->channel[i].dimms)
871 continue;
872 for (j = 0; j < n_tads; j++) {
873 pci_read_config_dword(pvt->pci_tad[i],
874 tad_ch_nilv_offset[j],
875 &reg);
876 tmp_mb = TAD_OFFSET(reg) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300877 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300878 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
879 i, j,
880 mb, kb,
881 ((u64)tmp_mb) << 20L,
882 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200883 }
884 }
885
886 /*
887 * Step 6) Get RIR Wayness/Limit, per each channel
888 */
889 for (i = 0; i < NUM_CHANNELS; i++) {
890 if (!pvt->channel[i].dimms)
891 continue;
892 for (j = 0; j < MAX_RIR_RANGES; j++) {
893 pci_read_config_dword(pvt->pci_tad[i],
894 rir_way_limit[j],
895 &reg);
896
897 if (!IS_RIR_VALID(reg))
898 continue;
899
900 tmp_mb = RIR_LIMIT(reg) >> 20;
901 rir_way = 1 << RIR_WAY(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300902 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300903 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
904 i, j,
905 mb, kb,
906 ((u64)tmp_mb) << 20L,
907 rir_way,
908 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200909
910 for (k = 0; k < rir_way; k++) {
911 pci_read_config_dword(pvt->pci_tad[i],
912 rir_offset[j][k],
913 &reg);
914 tmp_mb = RIR_OFFSET(reg) << 6;
915
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300916 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300917 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
918 i, j, k,
919 mb, kb,
920 ((u64)tmp_mb) << 20L,
921 (u32)RIR_RNK_TGT(reg),
922 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200923 }
924 }
925 }
926}
927
Rashika Kheria8112c0c2013-12-14 19:32:09 +0530928static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200929{
930 struct sbridge_dev *sbridge_dev;
931
932 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
933 if (sbridge_dev->node_id == node_id)
934 return sbridge_dev->mci;
935 }
936 return NULL;
937}
938
939static int get_memory_error_data(struct mem_ctl_info *mci,
940 u64 addr,
941 u8 *socket,
942 long *channel_mask,
943 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -0300944 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200945{
946 struct mem_ctl_info *new_mci;
947 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300948 struct pci_dev *pci_ha;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200949 int n_rir, n_sads, n_tads, sad_way, sck_xch;
950 int sad_interl, idx, base_ch;
951 int interleave_mode;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300952 unsigned sad_interleave[pvt->info.max_interleave];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200953 u32 reg;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300954 u8 ch_way, sck_way, pkg, sad_ha = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200955 u32 tad_offset;
956 u32 rir_way;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300957 u32 mb, kb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -0500958 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200959
960
961 /*
962 * Step 0) Check if the address is at special memory ranges
963 * The check bellow is probably enough to fill all cases where
964 * the error is not inside a memory, except for the legacy
965 * range (e. g. VGA addresses). It is unlikely, however, that the
966 * memory controller would generate an error on that range.
967 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300968 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200969 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200970 return -EINVAL;
971 }
972 if (addr >= (u64)pvt->tohm) {
973 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200974 return -EINVAL;
975 }
976
977 /*
978 * Step 1) Get socket
979 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300980 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
981 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200982 &reg);
983
984 if (!DRAM_RULE_ENABLE(reg))
985 continue;
986
987 limit = SAD_LIMIT(reg);
988 if (limit <= prv) {
989 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200990 return -EINVAL;
991 }
992 if (addr <= limit)
993 break;
994 prv = limit;
995 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300996 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200997 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200998 return -EINVAL;
999 }
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001000 *area_type = get_dram_attr(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001001 interleave_mode = INTERLEAVE_MODE(reg);
1002
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001003 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001004 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001005
1006 if (pvt->info.type == SANDY_BRIDGE) {
1007 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1008 for (sad_way = 0; sad_way < 8; sad_way++) {
1009 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1010 if (sad_way > 0 && sad_interl == pkg)
1011 break;
1012 sad_interleave[sad_way] = pkg;
1013 edac_dbg(0, "SAD interleave #%d: %d\n",
1014 sad_way, sad_interleave[sad_way]);
1015 }
1016 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1017 pvt->sbridge_dev->mc,
1018 n_sads,
1019 addr,
1020 limit,
1021 sad_way + 7,
1022 !interleave_mode ? "" : "XOR[18:16]");
1023 if (interleave_mode)
1024 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1025 else
1026 idx = (addr >> 6) & 7;
1027 switch (sad_way) {
1028 case 1:
1029 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001030 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001031 case 2:
1032 idx = idx & 1;
1033 break;
1034 case 4:
1035 idx = idx & 3;
1036 break;
1037 case 8:
1038 break;
1039 default:
1040 sprintf(msg, "Can't discover socket interleave");
1041 return -EINVAL;
1042 }
1043 *socket = sad_interleave[idx];
1044 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1045 idx, sad_way, *socket);
1046 } else {
1047 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001048 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001049 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1050 *socket = sad_pkg_socket(pkg);
1051 sad_ha = sad_pkg_ha(pkg);
1052 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1053 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001054 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001055
1056 /*
1057 * Move to the proper node structure, in order to access the
1058 * right PCI registers
1059 */
1060 new_mci = get_mci_for_node_id(*socket);
1061 if (!new_mci) {
1062 sprintf(msg, "Struct for socket #%u wasn't initialized",
1063 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001064 return -EINVAL;
1065 }
1066 mci = new_mci;
1067 pvt = mci->pvt_info;
1068
1069 /*
1070 * Step 2) Get memory channel
1071 */
1072 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001073 if (pvt->info.type == SANDY_BRIDGE)
1074 pci_ha = pvt->pci_ha0;
1075 else {
1076 if (sad_ha)
1077 pci_ha = pvt->pci_ha1;
1078 else
1079 pci_ha = pvt->pci_ha0;
1080 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001081 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001082 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001083 limit = TAD_LIMIT(reg);
1084 if (limit <= prv) {
1085 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001086 return -EINVAL;
1087 }
1088 if (addr <= limit)
1089 break;
1090 prv = limit;
1091 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001092 if (n_tads == MAX_TAD) {
1093 sprintf(msg, "Can't discover the memory channel");
1094 return -EINVAL;
1095 }
1096
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001097 ch_way = TAD_CH(reg) + 1;
1098 sck_way = TAD_SOCK(reg) + 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001099
1100 if (ch_way == 3)
1101 idx = addr >> 6;
1102 else
1103 idx = addr >> (6 + sck_way);
1104 idx = idx % ch_way;
1105
1106 /*
1107 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1108 */
1109 switch (idx) {
1110 case 0:
1111 base_ch = TAD_TGT0(reg);
1112 break;
1113 case 1:
1114 base_ch = TAD_TGT1(reg);
1115 break;
1116 case 2:
1117 base_ch = TAD_TGT2(reg);
1118 break;
1119 case 3:
1120 base_ch = TAD_TGT3(reg);
1121 break;
1122 default:
1123 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001124 return -EINVAL;
1125 }
1126 *channel_mask = 1 << base_ch;
1127
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001128 pci_read_config_dword(pvt->pci_tad[base_ch],
1129 tad_ch_nilv_offset[n_tads],
1130 &tad_offset);
1131
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001132 if (pvt->is_mirrored) {
1133 *channel_mask |= 1 << ((base_ch + 2) % 4);
1134 switch(ch_way) {
1135 case 2:
1136 case 4:
1137 sck_xch = 1 << sck_way * (ch_way >> 1);
1138 break;
1139 default:
1140 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001141 return -EINVAL;
1142 }
1143 } else
1144 sck_xch = (1 << sck_way) * ch_way;
1145
1146 if (pvt->is_lockstep)
1147 *channel_mask |= 1 << ((base_ch + 1) % 4);
1148
1149 offset = TAD_OFFSET(tad_offset);
1150
Joe Perches956b9ba2012-04-29 17:08:39 -03001151 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",
1152 n_tads,
1153 addr,
1154 limit,
1155 (u32)TAD_SOCK(reg),
1156 ch_way,
1157 offset,
1158 idx,
1159 base_ch,
1160 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001161
1162 /* Calculate channel address */
1163 /* Remove the TAD offset */
1164
1165 if (offset > addr) {
1166 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1167 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001168 return -EINVAL;
1169 }
1170 addr -= offset;
1171 /* Store the low bits [0:6] of the addr */
1172 ch_addr = addr & 0x7f;
1173 /* Remove socket wayness and remove 6 bits */
1174 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001175 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001176#if 0
1177 /* Divide by channel way */
1178 addr = addr / ch_way;
1179#endif
1180 /* Recover the last 6 bits */
1181 ch_addr |= addr << 6;
1182
1183 /*
1184 * Step 3) Decode rank
1185 */
1186 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1187 pci_read_config_dword(pvt->pci_tad[base_ch],
1188 rir_way_limit[n_rir],
1189 &reg);
1190
1191 if (!IS_RIR_VALID(reg))
1192 continue;
1193
1194 limit = RIR_LIMIT(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001195 mb = div_u64_rem(limit >> 20, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001196 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1197 n_rir,
1198 mb, kb,
1199 limit,
1200 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001201 if (ch_addr <= limit)
1202 break;
1203 }
1204 if (n_rir == MAX_RIR_RANGES) {
1205 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1206 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001207 return -EINVAL;
1208 }
1209 rir_way = RIR_WAY(reg);
1210 if (pvt->is_close_pg)
1211 idx = (ch_addr >> 6);
1212 else
1213 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1214 idx %= 1 << rir_way;
1215
1216 pci_read_config_dword(pvt->pci_tad[base_ch],
1217 rir_offset[n_rir][idx],
1218 &reg);
1219 *rank = RIR_RNK_TGT(reg);
1220
Joe Perches956b9ba2012-04-29 17:08:39 -03001221 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1222 n_rir,
1223 ch_addr,
1224 limit,
1225 rir_way,
1226 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001227
1228 return 0;
1229}
1230
1231/****************************************************************************
1232 Device initialization routines: put/get, init/exit
1233 ****************************************************************************/
1234
1235/*
1236 * sbridge_put_all_devices 'put' all the devices that we have
1237 * reserved via 'get'
1238 */
1239static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1240{
1241 int i;
1242
Joe Perches956b9ba2012-04-29 17:08:39 -03001243 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001244 for (i = 0; i < sbridge_dev->n_devs; i++) {
1245 struct pci_dev *pdev = sbridge_dev->pdev[i];
1246 if (!pdev)
1247 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03001248 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1249 pdev->bus->number,
1250 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001251 pci_dev_put(pdev);
1252 }
1253}
1254
1255static void sbridge_put_all_devices(void)
1256{
1257 struct sbridge_dev *sbridge_dev, *tmp;
1258
1259 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1260 sbridge_put_devices(sbridge_dev);
1261 free_sbridge_dev(sbridge_dev);
1262 }
1263}
1264
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001265static int sbridge_get_onedevice(struct pci_dev **prev,
1266 u8 *num_mc,
1267 const struct pci_id_table *table,
1268 const unsigned devno)
1269{
1270 struct sbridge_dev *sbridge_dev;
1271 const struct pci_id_descr *dev_descr = &table->descr[devno];
1272
1273 struct pci_dev *pdev = NULL;
1274 u8 bus = 0;
1275
Jiang Liuec5a0b32014-02-17 13:10:23 +08001276 sbridge_printk(KERN_DEBUG,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001277 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1278 dev_descr->dev, dev_descr->func,
1279 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1280
1281 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1282 dev_descr->dev_id, *prev);
1283
1284 if (!pdev) {
1285 if (*prev) {
1286 *prev = pdev;
1287 return 0;
1288 }
1289
1290 if (dev_descr->optional)
1291 return 0;
1292
1293 if (devno == 0)
1294 return -ENODEV;
1295
1296 sbridge_printk(KERN_INFO,
1297 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1298 dev_descr->dev, dev_descr->func,
1299 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1300
1301 /* End of list, leave */
1302 return -ENODEV;
1303 }
1304 bus = pdev->bus->number;
1305
1306 sbridge_dev = get_sbridge_dev(bus);
1307 if (!sbridge_dev) {
1308 sbridge_dev = alloc_sbridge_dev(bus, table);
1309 if (!sbridge_dev) {
1310 pci_dev_put(pdev);
1311 return -ENOMEM;
1312 }
1313 (*num_mc)++;
1314 }
1315
1316 if (sbridge_dev->pdev[devno]) {
1317 sbridge_printk(KERN_ERR,
1318 "Duplicated device for "
1319 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1320 bus, dev_descr->dev, dev_descr->func,
1321 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1322 pci_dev_put(pdev);
1323 return -ENODEV;
1324 }
1325
1326 sbridge_dev->pdev[devno] = pdev;
1327
1328 /* Sanity check */
1329 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1330 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1331 sbridge_printk(KERN_ERR,
1332 "Device PCI ID %04x:%04x "
1333 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1334 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1335 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1336 bus, dev_descr->dev, dev_descr->func);
1337 return -ENODEV;
1338 }
1339
1340 /* Be sure that the device is enabled */
1341 if (unlikely(pci_enable_device(pdev) < 0)) {
1342 sbridge_printk(KERN_ERR,
1343 "Couldn't enable "
1344 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1345 bus, dev_descr->dev, dev_descr->func,
1346 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1347 return -ENODEV;
1348 }
1349
Joe Perches956b9ba2012-04-29 17:08:39 -03001350 edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1351 bus, dev_descr->dev, dev_descr->func,
1352 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001353
1354 /*
1355 * As stated on drivers/pci/search.c, the reference count for
1356 * @from is always decremented if it is not %NULL. So, as we need
1357 * to get all devices up to null, we need to do a get for the device
1358 */
1359 pci_dev_get(pdev);
1360
1361 *prev = pdev;
1362
1363 return 0;
1364}
1365
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001366/*
1367 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
1368 * device/functions we want to reference for this driver.
1369 * Need to 'get' device 16 func 1 and func 2.
1370 * @num_mc: pointer to the memory controllers count, to be incremented in case
1371 * of success.
1372 * @table: model specific table
1373 *
1374 * returns 0 in case of success or error code
1375 */
1376static int sbridge_get_all_devices(u8 *num_mc,
1377 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001378{
1379 int i, rc;
1380 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001381
1382 while (table && table->descr) {
1383 for (i = 0; i < table->n_devs; i++) {
1384 pdev = NULL;
1385 do {
1386 rc = sbridge_get_onedevice(&pdev, num_mc,
1387 table, i);
1388 if (rc < 0) {
1389 if (i == 0) {
1390 i = table->n_devs;
1391 break;
1392 }
1393 sbridge_put_all_devices();
1394 return -ENODEV;
1395 }
1396 } while (pdev);
1397 }
1398 table++;
1399 }
1400
1401 return 0;
1402}
1403
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001404static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1405 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001406{
1407 struct sbridge_pvt *pvt = mci->pvt_info;
1408 struct pci_dev *pdev;
1409 int i, func, slot;
1410
1411 for (i = 0; i < sbridge_dev->n_devs; i++) {
1412 pdev = sbridge_dev->pdev[i];
1413 if (!pdev)
1414 continue;
1415 slot = PCI_SLOT(pdev->devfn);
1416 func = PCI_FUNC(pdev->devfn);
1417 switch (slot) {
1418 case 12:
1419 switch (func) {
1420 case 6:
1421 pvt->pci_sad0 = pdev;
1422 break;
1423 case 7:
1424 pvt->pci_sad1 = pdev;
1425 break;
1426 default:
1427 goto error;
1428 }
1429 break;
1430 case 13:
1431 switch (func) {
1432 case 6:
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -03001433 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001434 break;
1435 default:
1436 goto error;
1437 }
1438 break;
1439 case 14:
1440 switch (func) {
1441 case 0:
1442 pvt->pci_ha0 = pdev;
1443 break;
1444 default:
1445 goto error;
1446 }
1447 break;
1448 case 15:
1449 switch (func) {
1450 case 0:
1451 pvt->pci_ta = pdev;
1452 break;
1453 case 1:
1454 pvt->pci_ras = pdev;
1455 break;
1456 case 2:
1457 case 3:
1458 case 4:
1459 case 5:
1460 pvt->pci_tad[func - 2] = pdev;
1461 break;
1462 default:
1463 goto error;
1464 }
1465 break;
1466 case 17:
1467 switch (func) {
1468 case 0:
1469 pvt->pci_ddrio = pdev;
1470 break;
1471 default:
1472 goto error;
1473 }
1474 break;
1475 default:
1476 goto error;
1477 }
1478
Joe Perches956b9ba2012-04-29 17:08:39 -03001479 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1480 sbridge_dev->bus,
1481 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1482 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001483 }
1484
1485 /* Check if everything were registered */
1486 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07001487 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001488 goto enodev;
1489
1490 for (i = 0; i < NUM_CHANNELS; i++) {
1491 if (!pvt->pci_tad[i])
1492 goto enodev;
1493 }
1494 return 0;
1495
1496enodev:
1497 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1498 return -ENODEV;
1499
1500error:
1501 sbridge_printk(KERN_ERR, "Device %d, function %d "
1502 "is out of the expected range\n",
1503 slot, func);
1504 return -EINVAL;
1505}
1506
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001507static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1508 struct sbridge_dev *sbridge_dev)
1509{
1510 struct sbridge_pvt *pvt = mci->pvt_info;
1511 struct pci_dev *pdev, *tmp;
1512 int i, func, slot;
1513 bool mode_2ha = false;
1514
1515 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1516 PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, NULL);
1517 if (tmp) {
1518 mode_2ha = true;
1519 pci_dev_put(tmp);
1520 }
1521
1522 for (i = 0; i < sbridge_dev->n_devs; i++) {
1523 pdev = sbridge_dev->pdev[i];
1524 if (!pdev)
1525 continue;
1526 slot = PCI_SLOT(pdev->devfn);
1527 func = PCI_FUNC(pdev->devfn);
1528
1529 switch (slot) {
1530 case 14:
1531 if (func == 0) {
1532 pvt->pci_ha0 = pdev;
1533 break;
1534 }
1535 goto error;
1536 case 15:
1537 switch (func) {
1538 case 0:
1539 pvt->pci_ta = pdev;
1540 break;
1541 case 1:
1542 pvt->pci_ras = pdev;
1543 break;
1544 case 4:
1545 case 5:
1546 /* if we have 2 HAs active, channels 2 and 3
1547 * are in other device */
1548 if (mode_2ha)
1549 break;
1550 /* fall through */
1551 case 2:
1552 case 3:
1553 pvt->pci_tad[func - 2] = pdev;
1554 break;
1555 default:
1556 goto error;
1557 }
1558 break;
1559 case 17:
1560 if (func == 4) {
1561 pvt->pci_ddrio = pdev;
1562 break;
1563 } else if (func == 0) {
1564 if (!mode_2ha)
1565 pvt->pci_ddrio = pdev;
1566 break;
1567 }
1568 goto error;
1569 case 22:
1570 switch (func) {
1571 case 0:
1572 pvt->pci_sad0 = pdev;
1573 break;
1574 case 1:
1575 pvt->pci_br0 = pdev;
1576 break;
1577 case 2:
1578 pvt->pci_br1 = pdev;
1579 break;
1580 default:
1581 goto error;
1582 }
1583 break;
1584 case 28:
1585 if (func == 0) {
1586 pvt->pci_ha1 = pdev;
1587 break;
1588 }
1589 goto error;
1590 case 29:
1591 /* we shouldn't have this device if we have just one
1592 * HA present */
1593 WARN_ON(!mode_2ha);
1594 if (func == 2 || func == 3) {
1595 pvt->pci_tad[func] = pdev;
1596 break;
1597 }
1598 goto error;
1599 default:
1600 goto error;
1601 }
1602
1603 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1604 sbridge_dev->bus,
1605 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1606 pdev);
1607 }
1608
1609 /* Check if everything were registered */
1610 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1611 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
1612 !pvt->pci_ta)
1613 goto enodev;
1614
1615 for (i = 0; i < NUM_CHANNELS; i++) {
1616 if (!pvt->pci_tad[i])
1617 goto enodev;
1618 }
1619 return 0;
1620
1621enodev:
1622 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1623 return -ENODEV;
1624
1625error:
1626 sbridge_printk(KERN_ERR,
1627 "Device %d, function %d is out of the expected range\n",
1628 slot, func);
1629 return -EINVAL;
1630}
1631
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001632/****************************************************************************
1633 Error check routines
1634 ****************************************************************************/
1635
1636/*
1637 * While Sandy Bridge has error count registers, SMI BIOS read values from
1638 * and resets the counters. So, they are not reliable for the OS to read
1639 * from them. So, we have no option but to just trust on whatever MCE is
1640 * telling us about the errors.
1641 */
1642static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1643 const struct mce *m)
1644{
1645 struct mem_ctl_info *new_mci;
1646 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001647 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001648 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001649 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1650 bool overflow = GET_BITFIELD(m->status, 62, 62);
1651 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001652 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001653 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1654 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1655 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1656 u32 channel = GET_BITFIELD(m->status, 0, 3);
1657 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1658 long channel_mask, first_channel;
1659 u8 rank, socket;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001660 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001661 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001662
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001663 if (pvt->info.type == IVY_BRIDGE)
1664 recoverable = true;
1665 else
1666 recoverable = GET_BITFIELD(m->status, 56, 56);
1667
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001668 if (uncorrected_error) {
1669 if (ripv) {
1670 type = "FATAL";
1671 tp_event = HW_EVENT_ERR_FATAL;
1672 } else {
1673 type = "NON_FATAL";
1674 tp_event = HW_EVENT_ERR_UNCORRECTED;
1675 }
1676 } else {
1677 type = "CORRECTED";
1678 tp_event = HW_EVENT_ERR_CORRECTED;
1679 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001680
1681 /*
David Mackey15ed1032012-04-17 11:30:52 -07001682 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001683 * memory errors should fit in this mask:
1684 * 000f 0000 1mmm cccc (binary)
1685 * where:
1686 * f = Correction Report Filtering Bit. If 1, subsequent errors
1687 * won't be shown
1688 * mmm = error type
1689 * cccc = channel
1690 * If the mask doesn't match, report an error to the parsing logic
1691 */
1692 if (! ((errcode & 0xef80) == 0x80)) {
1693 optype = "Can't parse: it is not a mem";
1694 } else {
1695 switch (optypenum) {
1696 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001697 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001698 break;
1699 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001700 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001701 break;
1702 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001703 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001704 break;
1705 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001706 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001707 break;
1708 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001709 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001710 break;
1711 default:
1712 optype = "reserved";
1713 break;
1714 }
1715 }
1716
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03001717 /* Only decode errors with an valid address (ADDRV) */
1718 if (!GET_BITFIELD(m->status, 58, 58))
1719 return;
1720
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001721 rc = get_memory_error_data(mci, m->addr, &socket,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001722 &channel_mask, &rank, &area_type, msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001723 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001724 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001725 new_mci = get_mci_for_node_id(socket);
1726 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001727 strcpy(msg, "Error: socket got corrupted!");
1728 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001729 }
1730 mci = new_mci;
1731 pvt = mci->pvt_info;
1732
1733 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1734
1735 if (rank < 4)
1736 dimm = 0;
1737 else if (rank < 8)
1738 dimm = 1;
1739 else
1740 dimm = 2;
1741
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001742
1743 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001744 * FIXME: On some memory configurations (mirror, lockstep), the
1745 * Memory Controller can't point the error to a single DIMM. The
1746 * EDAC core should be handling the channel mask, in order to point
1747 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001748 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001749 snprintf(msg, sizeof(msg),
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001750 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001751 overflow ? " OVERFLOW" : "",
1752 (uncorrected_error && recoverable) ? " recoverable" : "",
1753 area_type,
1754 mscod, errcode,
1755 socket,
1756 channel_mask,
1757 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001758
Joe Perches956b9ba2012-04-29 17:08:39 -03001759 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001760
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001761 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001762
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001763 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001764 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001765 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1766 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001767 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001768 return;
1769err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001770 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001771 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001772 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001773
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001774}
1775
1776/*
1777 * sbridge_check_error Retrieve and process errors reported by the
1778 * hardware. Called by the Core module.
1779 */
1780static void sbridge_check_error(struct mem_ctl_info *mci)
1781{
1782 struct sbridge_pvt *pvt = mci->pvt_info;
1783 int i;
1784 unsigned count = 0;
1785 struct mce *m;
1786
1787 /*
1788 * MCE first step: Copy all mce errors into a temporary buffer
1789 * We use a double buffering here, to reduce the risk of
1790 * loosing an error.
1791 */
1792 smp_rmb();
1793 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1794 % MCE_LOG_LEN;
1795 if (!count)
1796 return;
1797
1798 m = pvt->mce_outentry;
1799 if (pvt->mce_in + count > MCE_LOG_LEN) {
1800 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1801
1802 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1803 smp_wmb();
1804 pvt->mce_in = 0;
1805 count -= l;
1806 m += l;
1807 }
1808 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1809 smp_wmb();
1810 pvt->mce_in += count;
1811
1812 smp_rmb();
1813 if (pvt->mce_overrun) {
1814 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
1815 pvt->mce_overrun);
1816 smp_wmb();
1817 pvt->mce_overrun = 0;
1818 }
1819
1820 /*
1821 * MCE second step: parse errors and display
1822 */
1823 for (i = 0; i < count; i++)
1824 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
1825}
1826
1827/*
1828 * sbridge_mce_check_error Replicates mcelog routine to get errors
1829 * This routine simply queues mcelog errors, and
1830 * return. The error itself should be handled later
1831 * by sbridge_check_error.
1832 * WARNING: As this routine should be called at NMI time, extra care should
1833 * be taken to avoid deadlocks, and to be as fast as possible.
1834 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001835static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
1836 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001837{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001838 struct mce *mce = (struct mce *)data;
1839 struct mem_ctl_info *mci;
1840 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04001841 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001842
Chen, Gongfd521032013-12-06 01:17:09 -05001843 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
1844 return NOTIFY_DONE;
1845
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001846 mci = get_mci_for_node_id(mce->socketid);
1847 if (!mci)
1848 return NOTIFY_BAD;
1849 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001850
1851 /*
1852 * Just let mcelog handle it if the error is
1853 * outside the memory controller. A memory error
1854 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1855 * bit 12 has an special meaning.
1856 */
1857 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001858 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001859
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04001860 if (mce->mcgstatus & MCG_STATUS_MCIP)
1861 type = "Exception";
1862 else
1863 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001864
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001865 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001866
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001867 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
1868 "Bank %d: %016Lx\n", mce->extcpu, type,
1869 mce->mcgstatus, mce->bank, mce->status);
1870 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
1871 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
1872 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001873
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04001874 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
1875 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
1876 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001877
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001878 /* Only handle if it is the right mc controller */
1879 if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001880 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001881
1882 smp_rmb();
1883 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1884 smp_wmb();
1885 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001886 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001887 }
1888
1889 /* Copy memory error at the ringbuffer */
1890 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1891 smp_wmb();
1892 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1893
1894 /* Handle fatal errors immediately */
1895 if (mce->mcgstatus & 1)
1896 sbridge_check_error(mci);
1897
1898 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001899 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001900}
1901
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001902static struct notifier_block sbridge_mce_dec = {
1903 .notifier_call = sbridge_mce_check_error,
1904};
1905
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001906/****************************************************************************
1907 EDAC register/unregister logic
1908 ****************************************************************************/
1909
1910static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
1911{
1912 struct mem_ctl_info *mci = sbridge_dev->mci;
1913 struct sbridge_pvt *pvt;
1914
1915 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001916 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001917
1918 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
1919 return;
1920 }
1921
1922 pvt = mci->pvt_info;
1923
Joe Perches956b9ba2012-04-29 17:08:39 -03001924 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1925 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001926
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001927 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03001928 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001929
Joe Perches956b9ba2012-04-29 17:08:39 -03001930 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001931 kfree(mci->ctl_name);
1932 edac_mc_free(mci);
1933 sbridge_dev->mci = NULL;
1934}
1935
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001936static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001937{
1938 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001939 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001940 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001941 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001942 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001943
1944 /* Check the number of active and not disabled channels */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001945 rc = check_if_ecc_is_active(sbridge_dev->bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001946 if (unlikely(rc < 0))
1947 return rc;
1948
1949 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001950 layers[0].type = EDAC_MC_LAYER_CHANNEL;
1951 layers[0].size = NUM_CHANNELS;
1952 layers[0].is_virt_csrow = false;
1953 layers[1].type = EDAC_MC_LAYER_SLOT;
1954 layers[1].size = MAX_DIMMS;
1955 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03001956 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001957 sizeof(*pvt));
1958
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001959 if (unlikely(!mci))
1960 return -ENOMEM;
1961
Joe Perches956b9ba2012-04-29 17:08:39 -03001962 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001963 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001964
1965 pvt = mci->pvt_info;
1966 memset(pvt, 0, sizeof(*pvt));
1967
1968 /* Associate sbridge_dev and mci for future usage */
1969 pvt->sbridge_dev = sbridge_dev;
1970 sbridge_dev->mci = mci;
1971
1972 mci->mtype_cap = MEM_FLAG_DDR3;
1973 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1974 mci->edac_cap = EDAC_FLAG_NONE;
1975 mci->mod_name = "sbridge_edac.c";
1976 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001977 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001978 mci->ctl_page_to_phys = NULL;
1979
1980 /* Set the function pointer to an actual operation function */
1981 mci->edac_check = sbridge_check_error;
1982
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001983 pvt->info.type = type;
1984 if (type == IVY_BRIDGE) {
1985 pvt->info.rankcfgr = IB_RANK_CFG_A;
1986 pvt->info.get_tolm = ibridge_get_tolm;
1987 pvt->info.get_tohm = ibridge_get_tohm;
1988 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001989 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001990 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
1991 pvt->info.interleave_list = ibridge_interleave_list;
1992 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
1993 pvt->info.interleave_pkg = ibridge_interleave_pkg;
1994 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
1995
1996 /* Store pci devices at mci for faster access */
1997 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
1998 if (unlikely(rc < 0))
1999 goto fail0;
2000 } else {
2001 pvt->info.rankcfgr = SB_RANK_CFG_A;
2002 pvt->info.get_tolm = sbridge_get_tolm;
2003 pvt->info.get_tohm = sbridge_get_tohm;
2004 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002005 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002006 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2007 pvt->info.interleave_list = sbridge_interleave_list;
2008 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2009 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2010 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2011
2012 /* Store pci devices at mci for faster access */
2013 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2014 if (unlikely(rc < 0))
2015 goto fail0;
2016 }
2017
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002018
2019 /* Get dimm basic config and the memory layout */
2020 get_dimm_config(mci);
2021 get_memory_layout(mci);
2022
2023 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002024 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002025
2026 /* add this new MC control structure to EDAC's list of MCs */
2027 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002028 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002029 rc = -EINVAL;
2030 goto fail0;
2031 }
2032
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002033 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002034
2035fail0:
2036 kfree(mci->ctl_name);
2037 edac_mc_free(mci);
2038 sbridge_dev->mci = NULL;
2039 return rc;
2040}
2041
2042/*
2043 * sbridge_probe Probe for ONE instance of device to see if it is
2044 * present.
2045 * return:
2046 * 0 for FOUND a device
2047 * < 0 for error code
2048 */
2049
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002050static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002051{
2052 int rc;
2053 u8 mc, num_mc = 0;
2054 struct sbridge_dev *sbridge_dev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002055 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002056
2057 /* get the pci devices we want to reserve for our use */
2058 mutex_lock(&sbridge_edac_lock);
2059
2060 /*
2061 * All memory controllers are allocated at the first pass.
2062 */
2063 if (unlikely(probed >= 1)) {
2064 mutex_unlock(&sbridge_edac_lock);
2065 return -ENODEV;
2066 }
2067 probed++;
2068
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002069 if (pdev->device == PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA) {
2070 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2071 type = IVY_BRIDGE;
2072 } else {
2073 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2074 type = SANDY_BRIDGE;
2075 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002076 if (unlikely(rc < 0))
2077 goto fail0;
2078 mc = 0;
2079
2080 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002081 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2082 mc, mc + 1, num_mc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002083 sbridge_dev->mc = mc++;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002084 rc = sbridge_register_mci(sbridge_dev, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002085 if (unlikely(rc < 0))
2086 goto fail1;
2087 }
2088
2089 sbridge_printk(KERN_INFO, "Driver loaded.\n");
2090
2091 mutex_unlock(&sbridge_edac_lock);
2092 return 0;
2093
2094fail1:
2095 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2096 sbridge_unregister_mci(sbridge_dev);
2097
2098 sbridge_put_all_devices();
2099fail0:
2100 mutex_unlock(&sbridge_edac_lock);
2101 return rc;
2102}
2103
2104/*
2105 * sbridge_remove destructor for one instance of device
2106 *
2107 */
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002108static void sbridge_remove(struct pci_dev *pdev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002109{
2110 struct sbridge_dev *sbridge_dev;
2111
Joe Perches956b9ba2012-04-29 17:08:39 -03002112 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002113
2114 /*
2115 * we have a trouble here: pdev value for removal will be wrong, since
2116 * it will point to the X58 register used to detect that the machine
2117 * is a Nehalem or upper design. However, due to the way several PCI
2118 * devices are grouped together to provide MC functionality, we need
2119 * to use a different method for releasing the devices
2120 */
2121
2122 mutex_lock(&sbridge_edac_lock);
2123
2124 if (unlikely(!probed)) {
2125 mutex_unlock(&sbridge_edac_lock);
2126 return;
2127 }
2128
2129 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2130 sbridge_unregister_mci(sbridge_dev);
2131
2132 /* Release PCI resources */
2133 sbridge_put_all_devices();
2134
2135 probed--;
2136
2137 mutex_unlock(&sbridge_edac_lock);
2138}
2139
2140MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2141
2142/*
2143 * sbridge_driver pci_driver structure for this module
2144 *
2145 */
2146static struct pci_driver sbridge_driver = {
2147 .name = "sbridge_edac",
2148 .probe = sbridge_probe,
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002149 .remove = sbridge_remove,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002150 .id_table = sbridge_pci_tbl,
2151};
2152
2153/*
2154 * sbridge_init Module entry function
2155 * Try to initialize this module for its devices
2156 */
2157static int __init sbridge_init(void)
2158{
2159 int pci_rc;
2160
Joe Perches956b9ba2012-04-29 17:08:39 -03002161 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002162
2163 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2164 opstate_init();
2165
2166 pci_rc = pci_register_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002167 if (pci_rc >= 0) {
2168 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05002169 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2170 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002171 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03002172 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002173
2174 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2175 pci_rc);
2176
2177 return pci_rc;
2178}
2179
2180/*
2181 * sbridge_exit() Module exit function
2182 * Unregister the driver
2183 */
2184static void __exit sbridge_exit(void)
2185{
Joe Perches956b9ba2012-04-29 17:08:39 -03002186 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002187 pci_unregister_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002188 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002189}
2190
2191module_init(sbridge_init);
2192module_exit(sbridge_exit);
2193
2194module_param(edac_op_state, int, 0444);
2195MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2196
2197MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002198MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002199MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002200MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002201 SBRIDGE_REVISION);