blob: 7f795119b5729002aa4b3dcc72f900660347b14e [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:
10 * Mauro Carvalho Chehab <mchehab@redhat.com>
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/pci.h>
16#include <linux/pci_ids.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/edac.h>
20#include <linux/mmzone.h>
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 */
37#define SBRIDGE_REVISION " Ver: 1.0.0 "
38#define EDAC_MOD_STR "sbridge_edac"
39
40/*
41 * Debug macros
42 */
43#define sbridge_printk(level, fmt, arg...) \
44 edac_printk(level, "sbridge", fmt, ##arg)
45
46#define sbridge_mc_printk(mci, level, fmt, arg...) \
47 edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
48
49/*
50 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
51 */
52#define GET_BITFIELD(v, lo, hi) \
53 (((v) & ((1ULL << ((hi) - (lo) + 1)) - 1) << (lo)) >> (lo))
54
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
91#define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
92#define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
93#define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
94#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
95
96static char *get_dram_attr(u32 reg)
97{
98 switch(DRAM_ATTR(reg)) {
99 case 0:
100 return "DRAM";
101 case 1:
102 return "MMCFG";
103 case 2:
104 return "NXM";
105 default:
106 return "unknown";
107 }
108}
109
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300110static const u32 sbridge_interleave_list[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200111 0x84, 0x8c, 0x94, 0x9c, 0xa4,
112 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
113};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200114
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300115struct interleave_pkg {
116 unsigned char start;
117 unsigned char end;
118};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200119
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300120static const struct interleave_pkg sbridge_interleave_pkg[] = {
121 { 0, 2 },
122 { 3, 5 },
123 { 8, 10 },
124 { 11, 13 },
125 { 16, 18 },
126 { 19, 21 },
127 { 24, 26 },
128 { 27, 29 },
129};
130
131static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
132 int interleave)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200133{
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300134 return GET_BITFIELD(reg, table[interleave].start,
135 table[interleave].end);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200136}
137
138/* Devices 12 Function 7 */
139
140#define TOLM 0x80
141#define TOHM 0x84
142
143#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
144#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
145
146/* Device 13 Function 6 */
147
148#define SAD_TARGET 0xf0
149
150#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
151
152#define SAD_CONTROL 0xf4
153
154#define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
155
156/* Device 14 function 0 */
157
158static const u32 tad_dram_rule[] = {
159 0x40, 0x44, 0x48, 0x4c,
160 0x50, 0x54, 0x58, 0x5c,
161 0x60, 0x64, 0x68, 0x6c,
162};
163#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
164
165#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
166#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
167#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
168#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
169#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
170#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
171#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
172
173/* Device 15, function 0 */
174
175#define MCMTR 0x7c
176
177#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
178#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
179#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
180
181/* Device 15, function 1 */
182
183#define RASENABLES 0xac
184#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
185
186/* Device 15, functions 2-5 */
187
188static const int mtr_regs[] = {
189 0x80, 0x84, 0x88,
190};
191
192#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
193#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
194#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
195#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
196#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
197
198static const u32 tad_ch_nilv_offset[] = {
199 0x90, 0x94, 0x98, 0x9c,
200 0xa0, 0xa4, 0xa8, 0xac,
201 0xb0, 0xb4, 0xb8, 0xbc,
202};
203#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
204#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
205
206static const u32 rir_way_limit[] = {
207 0x108, 0x10c, 0x110, 0x114, 0x118,
208};
209#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
210
211#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
212#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
213#define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
214
215#define MAX_RIR_WAY 8
216
217static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
218 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
219 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
220 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
221 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
222 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
223};
224
225#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
226#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
227
228/* Device 16, functions 2-7 */
229
230/*
231 * FIXME: Implement the error count reads directly
232 */
233
234static const u32 correrrcnt[] = {
235 0x104, 0x108, 0x10c, 0x110,
236};
237
238#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
239#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
240#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
241#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
242
243static const u32 correrrthrsld[] = {
244 0x11c, 0x120, 0x124, 0x128,
245};
246
247#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
248#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
249
250
251/* Device 17, function 0 */
252
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300253#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200254
255#define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
256
257/*
258 * sbridge structs
259 */
260
261#define NUM_CHANNELS 4
262#define MAX_DIMMS 3 /* Max DIMMS per channel */
263
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300264struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200265struct sbridge_info {
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300266 u32 mcmtr;
267 u32 rankcfgr;
268 u64 (*get_tolm)(struct sbridge_pvt *pvt);
269 u64 (*get_tohm)(struct sbridge_pvt *pvt);
270 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300271 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300272 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300273 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300274 u8 max_interleave;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200275};
276
277struct sbridge_channel {
278 u32 ranks;
279 u32 dimms;
280};
281
282struct pci_id_descr {
283 int dev;
284 int func;
285 int dev_id;
286 int optional;
287};
288
289struct pci_id_table {
290 const struct pci_id_descr *descr;
291 int n_devs;
292};
293
294struct sbridge_dev {
295 struct list_head list;
296 u8 bus, mc;
297 u8 node_id, source_id;
298 struct pci_dev **pdev;
299 int n_devs;
300 struct mem_ctl_info *mci;
301};
302
303struct sbridge_pvt {
304 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
305 struct pci_dev *pci_sad0, *pci_sad1, *pci_ha0;
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300306 struct pci_dev *pci_br0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200307 struct pci_dev *pci_tad[NUM_CHANNELS];
308
309 struct sbridge_dev *sbridge_dev;
310
311 struct sbridge_info info;
312 struct sbridge_channel channel[NUM_CHANNELS];
313
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200314 /* Memory type detection */
315 bool is_mirrored, is_lockstep, is_close_pg;
316
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200317 /* Fifo double buffers */
318 struct mce mce_entry[MCE_LOG_LEN];
319 struct mce mce_outentry[MCE_LOG_LEN];
320
321 /* Fifo in/out counters */
322 unsigned mce_in, mce_out;
323
324 /* Count indicator to show errors not got */
325 unsigned mce_overrun;
326
327 /* Memory description */
328 u64 tolm, tohm;
329};
330
Luck, Tonyde4772c2013-03-28 09:59:15 -0700331#define PCI_DESCR(device, function, device_id, opt) \
332 .dev = (device), \
333 .func = (function), \
334 .dev_id = (device_id), \
335 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200336
337static const struct pci_id_descr pci_dev_descr_sbridge[] = {
338 /* Processor Home Agent */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700339 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200340
341 /* Memory controller */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700342 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
343 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
344 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
345 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
346 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
347 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
348 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200349
350 /* System Address Decoder */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700351 { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
352 { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200353
354 /* Broadcast Registers */
Luck, Tonyde4772c2013-03-28 09:59:15 -0700355 { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200356};
357
358#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
359static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
360 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
361 {0,} /* 0 terminated list. */
362};
363
364/*
365 * pci_device_id table for which devices we are looking for
366 */
Lionel Debroux36c46f32012-02-27 07:41:47 +0100367static DEFINE_PCI_DEVICE_TABLE(sbridge_pci_tbl) = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200368 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
369 {0,} /* 0 terminated list. */
370};
371
372
373/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700374 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200375 ****************************************************************************/
376
377static inline int numrank(u32 mtr)
378{
379 int ranks = (1 << RANK_CNT_BITS(mtr));
380
381 if (ranks > 4) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300382 edac_dbg(0, "Invalid number of ranks: %d (max = 4) raw value = %x (%04x)\n",
383 ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200384 return -EINVAL;
385 }
386
387 return ranks;
388}
389
390static inline int numrow(u32 mtr)
391{
392 int rows = (RANK_WIDTH_BITS(mtr) + 12);
393
394 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300395 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
396 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200397 return -EINVAL;
398 }
399
400 return 1 << rows;
401}
402
403static inline int numcol(u32 mtr)
404{
405 int cols = (COL_WIDTH_BITS(mtr) + 10);
406
407 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300408 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
409 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200410 return -EINVAL;
411 }
412
413 return 1 << cols;
414}
415
416static struct sbridge_dev *get_sbridge_dev(u8 bus)
417{
418 struct sbridge_dev *sbridge_dev;
419
420 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
421 if (sbridge_dev->bus == bus)
422 return sbridge_dev;
423 }
424
425 return NULL;
426}
427
428static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
429 const struct pci_id_table *table)
430{
431 struct sbridge_dev *sbridge_dev;
432
433 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
434 if (!sbridge_dev)
435 return NULL;
436
437 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
438 GFP_KERNEL);
439 if (!sbridge_dev->pdev) {
440 kfree(sbridge_dev);
441 return NULL;
442 }
443
444 sbridge_dev->bus = bus;
445 sbridge_dev->n_devs = table->n_devs;
446 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
447
448 return sbridge_dev;
449}
450
451static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
452{
453 list_del(&sbridge_dev->list);
454 kfree(sbridge_dev->pdev);
455 kfree(sbridge_dev);
456}
457
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300458static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
459{
460 u32 reg;
461
462 /* Address range is 32:28 */
463 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
464 return GET_TOLM(reg);
465}
466
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300467static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
468{
469 u32 reg;
470
471 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
472 return GET_TOHM(reg);
473}
474
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200475/****************************************************************************
476 Memory check routines
477 ****************************************************************************/
478static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
479 unsigned func)
480{
481 struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
482 int i;
483
484 if (!sbridge_dev)
485 return NULL;
486
487 for (i = 0; i < sbridge_dev->n_devs; i++) {
488 if (!sbridge_dev->pdev[i])
489 continue;
490
491 if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
492 PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300493 edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
494 bus, slot, func, sbridge_dev->pdev[i]);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200495 return sbridge_dev->pdev[i];
496 }
497 }
498
499 return NULL;
500}
501
502/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300503 * check_if_ecc_is_active() - Checks if ECC is active
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200504 * bus: Device bus
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200505 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300506static int check_if_ecc_is_active(const u8 bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200507{
508 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200509 u32 mcmtr;
510
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200511 pdev = get_pdev_slot_func(bus, 15, 0);
512 if (!pdev) {
513 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
514 "%2x.%02d.%d!!!\n",
515 bus, 15, 0);
516 return -ENODEV;
517 }
518
519 pci_read_config_dword(pdev, MCMTR, &mcmtr);
520 if (!IS_ECC_ENABLED(mcmtr)) {
521 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
522 return -ENODEV;
523 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200524 return 0;
525}
526
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300527static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200528{
529 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300530 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300531 unsigned i, j, banks, ranks, rows, cols, npages;
532 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200533 u32 reg;
534 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200535 enum mem_type mtype;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200536
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300537 pvt->info.rankcfgr = SB_RANK_CFG_A;
538
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300539 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200540 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
541
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -0300542 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200543 pvt->sbridge_dev->node_id = NODE_ID(reg);
Joe Perches956b9ba2012-04-29 17:08:39 -0300544 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
545 pvt->sbridge_dev->mc,
546 pvt->sbridge_dev->node_id,
547 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200548
549 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
550 if (IS_MIRROR_ENABLED(reg)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300551 edac_dbg(0, "Memory mirror is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200552 pvt->is_mirrored = true;
553 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300554 edac_dbg(0, "Memory mirror is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200555 pvt->is_mirrored = false;
556 }
557
558 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
559 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300560 edac_dbg(0, "Lockstep is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200561 mode = EDAC_S8ECD8ED;
562 pvt->is_lockstep = true;
563 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300564 edac_dbg(0, "Lockstep is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200565 mode = EDAC_S4ECD4ED;
566 pvt->is_lockstep = false;
567 }
568 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300569 edac_dbg(0, "address map is on closed page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200570 pvt->is_close_pg = true;
571 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300572 edac_dbg(0, "address map is on open page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200573 pvt->is_close_pg = false;
574 }
575
Luck, Tonyde4772c2013-03-28 09:59:15 -0700576 if (pvt->pci_ddrio) {
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300577 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
578 &reg);
Luck, Tonyde4772c2013-03-28 09:59:15 -0700579 if (IS_RDIMM_ENABLED(reg)) {
580 /* FIXME: Can also be LRDIMM */
581 edac_dbg(0, "Memory is registered\n");
582 mtype = MEM_RDDR3;
583 } else {
584 edac_dbg(0, "Memory is unregistered\n");
585 mtype = MEM_DDR3;
586 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200587 } else {
Luck, Tonyde4772c2013-03-28 09:59:15 -0700588 edac_dbg(0, "Cannot determine memory type\n");
589 mtype = MEM_UNKNOWN;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200590 }
591
592 /* On all supported DDR3 DIMM types, there are 8 banks available */
593 banks = 8;
594
595 for (i = 0; i < NUM_CHANNELS; i++) {
596 u32 mtr;
597
598 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300599 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
600 i, j, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200601 pci_read_config_dword(pvt->pci_tad[i],
602 mtr_regs[j], &mtr);
Joe Perches956b9ba2012-04-29 17:08:39 -0300603 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200604 if (IS_DIMM_PRESENT(mtr)) {
605 pvt->channel[i].dimms++;
606
607 ranks = numrank(mtr);
608 rows = numrow(mtr);
609 cols = numcol(mtr);
610
611 /* DDR3 has 8 I/O banks */
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300612 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200613 npages = MiB_TO_PAGES(size);
614
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300615 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 -0300616 pvt->sbridge_dev->mc, i, j,
617 size, npages,
618 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200619
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300620 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300621 dimm->grain = 32;
622 dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
623 dimm->mtype = mtype;
624 dimm->edac_mode = mode;
625 snprintf(dimm->label, sizeof(dimm->label),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200626 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
627 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200628 }
629 }
630 }
631
632 return 0;
633}
634
635static void get_memory_layout(const struct mem_ctl_info *mci)
636{
637 struct sbridge_pvt *pvt = mci->pvt_info;
638 int i, j, k, n_sads, n_tads, sad_interl;
639 u32 reg;
640 u64 limit, prv = 0;
641 u64 tmp_mb;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300642 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200643 u32 rir_way;
644
645 /*
646 * Step 1) Get TOLM/TOHM ranges
647 */
648
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300649 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200650 tmp_mb = (1 + pvt->tolm) >> 20;
651
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300652 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300653 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200654
655 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300656 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200657 tmp_mb = (1 + pvt->tohm) >> 20;
658
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300659 mb = div_u64_rem(tmp_mb, 1000, &kb);
Mauro Carvalho Chehabda14d932012-10-25 09:07:21 -0200660 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200661
662 /*
663 * Step 2) Get SAD range and SAD Interleave list
664 * TAD registers contain the interleave wayness. However, it
665 * seems simpler to just discover it indirectly, with the
666 * algorithm bellow.
667 */
668 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300669 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200670 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300671 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200672 &reg);
673 limit = SAD_LIMIT(reg);
674
675 if (!DRAM_RULE_ENABLE(reg))
676 continue;
677
678 if (limit <= prv)
679 break;
680
681 tmp_mb = (limit + 1) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300682 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300683 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
684 n_sads,
685 get_dram_attr(reg),
686 mb, kb,
687 ((u64)tmp_mb) << 20L,
688 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
689 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200690 prv = limit;
691
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300692 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200693 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300694 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200695 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300696 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
697 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200698 break;
699
Joe Perches956b9ba2012-04-29 17:08:39 -0300700 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300701 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200702 }
703 }
704
705 /*
706 * Step 3) Get TAD range
707 */
708 prv = 0;
709 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
710 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
711 &reg);
712 limit = TAD_LIMIT(reg);
713 if (limit <= prv)
714 break;
715 tmp_mb = (limit + 1) >> 20;
716
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300717 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300718 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",
719 n_tads, mb, kb,
720 ((u64)tmp_mb) << 20L,
721 (u32)TAD_SOCK(reg),
722 (u32)TAD_CH(reg),
723 (u32)TAD_TGT0(reg),
724 (u32)TAD_TGT1(reg),
725 (u32)TAD_TGT2(reg),
726 (u32)TAD_TGT3(reg),
727 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -0300728 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200729 }
730
731 /*
732 * Step 4) Get TAD offsets, per each channel
733 */
734 for (i = 0; i < NUM_CHANNELS; i++) {
735 if (!pvt->channel[i].dimms)
736 continue;
737 for (j = 0; j < n_tads; j++) {
738 pci_read_config_dword(pvt->pci_tad[i],
739 tad_ch_nilv_offset[j],
740 &reg);
741 tmp_mb = TAD_OFFSET(reg) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300742 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300743 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
744 i, j,
745 mb, kb,
746 ((u64)tmp_mb) << 20L,
747 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200748 }
749 }
750
751 /*
752 * Step 6) Get RIR Wayness/Limit, per each channel
753 */
754 for (i = 0; i < NUM_CHANNELS; i++) {
755 if (!pvt->channel[i].dimms)
756 continue;
757 for (j = 0; j < MAX_RIR_RANGES; j++) {
758 pci_read_config_dword(pvt->pci_tad[i],
759 rir_way_limit[j],
760 &reg);
761
762 if (!IS_RIR_VALID(reg))
763 continue;
764
765 tmp_mb = RIR_LIMIT(reg) >> 20;
766 rir_way = 1 << RIR_WAY(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300767 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300768 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
769 i, j,
770 mb, kb,
771 ((u64)tmp_mb) << 20L,
772 rir_way,
773 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200774
775 for (k = 0; k < rir_way; k++) {
776 pci_read_config_dword(pvt->pci_tad[i],
777 rir_offset[j][k],
778 &reg);
779 tmp_mb = RIR_OFFSET(reg) << 6;
780
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300781 mb = div_u64_rem(tmp_mb, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300782 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
783 i, j, k,
784 mb, kb,
785 ((u64)tmp_mb) << 20L,
786 (u32)RIR_RNK_TGT(reg),
787 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200788 }
789 }
790 }
791}
792
793struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
794{
795 struct sbridge_dev *sbridge_dev;
796
797 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
798 if (sbridge_dev->node_id == node_id)
799 return sbridge_dev->mci;
800 }
801 return NULL;
802}
803
804static int get_memory_error_data(struct mem_ctl_info *mci,
805 u64 addr,
806 u8 *socket,
807 long *channel_mask,
808 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -0300809 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200810{
811 struct mem_ctl_info *new_mci;
812 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200813 int n_rir, n_sads, n_tads, sad_way, sck_xch;
814 int sad_interl, idx, base_ch;
815 int interleave_mode;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300816 unsigned sad_interleave[pvt->info.max_interleave];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200817 u32 reg;
818 u8 ch_way,sck_way;
819 u32 tad_offset;
820 u32 rir_way;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300821 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200822 u64 ch_addr, offset, limit, prv = 0;
823
824
825 /*
826 * Step 0) Check if the address is at special memory ranges
827 * The check bellow is probably enough to fill all cases where
828 * the error is not inside a memory, except for the legacy
829 * range (e. g. VGA addresses). It is unlikely, however, that the
830 * memory controller would generate an error on that range.
831 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300832 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200833 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200834 return -EINVAL;
835 }
836 if (addr >= (u64)pvt->tohm) {
837 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200838 return -EINVAL;
839 }
840
841 /*
842 * Step 1) Get socket
843 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300844 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
845 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200846 &reg);
847
848 if (!DRAM_RULE_ENABLE(reg))
849 continue;
850
851 limit = SAD_LIMIT(reg);
852 if (limit <= prv) {
853 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200854 return -EINVAL;
855 }
856 if (addr <= limit)
857 break;
858 prv = limit;
859 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300860 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200861 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200862 return -EINVAL;
863 }
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -0300864 *area_type = get_dram_attr(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200865 interleave_mode = INTERLEAVE_MODE(reg);
866
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300867 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200868 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300869 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200870 for (sad_way = 0; sad_way < 8; sad_way++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300871 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
872 if (sad_way > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200873 break;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300874 sad_interleave[sad_way] = pkg;
Joe Perches956b9ba2012-04-29 17:08:39 -0300875 edac_dbg(0, "SAD interleave #%d: %d\n",
876 sad_way, sad_interleave[sad_way]);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200877 }
Joe Perches956b9ba2012-04-29 17:08:39 -0300878 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
879 pvt->sbridge_dev->mc,
880 n_sads,
881 addr,
882 limit,
883 sad_way + 7,
884 interleave_mode ? "" : "XOR[18:16]");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200885 if (interleave_mode)
886 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
887 else
888 idx = (addr >> 6) & 7;
889 switch (sad_way) {
890 case 1:
891 idx = 0;
892 break;
893 case 2:
894 idx = idx & 1;
895 break;
896 case 4:
897 idx = idx & 3;
898 break;
899 case 8:
900 break;
901 default:
902 sprintf(msg, "Can't discover socket interleave");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200903 return -EINVAL;
904 }
905 *socket = sad_interleave[idx];
Joe Perches956b9ba2012-04-29 17:08:39 -0300906 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
907 idx, sad_way, *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200908
909 /*
910 * Move to the proper node structure, in order to access the
911 * right PCI registers
912 */
913 new_mci = get_mci_for_node_id(*socket);
914 if (!new_mci) {
915 sprintf(msg, "Struct for socket #%u wasn't initialized",
916 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200917 return -EINVAL;
918 }
919 mci = new_mci;
920 pvt = mci->pvt_info;
921
922 /*
923 * Step 2) Get memory channel
924 */
925 prv = 0;
926 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
927 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
928 &reg);
929 limit = TAD_LIMIT(reg);
930 if (limit <= prv) {
931 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200932 return -EINVAL;
933 }
934 if (addr <= limit)
935 break;
936 prv = limit;
937 }
938 ch_way = TAD_CH(reg) + 1;
939 sck_way = TAD_SOCK(reg) + 1;
940 /*
941 * FIXME: Is it right to always use channel 0 for offsets?
942 */
943 pci_read_config_dword(pvt->pci_tad[0],
944 tad_ch_nilv_offset[n_tads],
945 &tad_offset);
946
947 if (ch_way == 3)
948 idx = addr >> 6;
949 else
950 idx = addr >> (6 + sck_way);
951 idx = idx % ch_way;
952
953 /*
954 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
955 */
956 switch (idx) {
957 case 0:
958 base_ch = TAD_TGT0(reg);
959 break;
960 case 1:
961 base_ch = TAD_TGT1(reg);
962 break;
963 case 2:
964 base_ch = TAD_TGT2(reg);
965 break;
966 case 3:
967 base_ch = TAD_TGT3(reg);
968 break;
969 default:
970 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200971 return -EINVAL;
972 }
973 *channel_mask = 1 << base_ch;
974
975 if (pvt->is_mirrored) {
976 *channel_mask |= 1 << ((base_ch + 2) % 4);
977 switch(ch_way) {
978 case 2:
979 case 4:
980 sck_xch = 1 << sck_way * (ch_way >> 1);
981 break;
982 default:
983 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200984 return -EINVAL;
985 }
986 } else
987 sck_xch = (1 << sck_way) * ch_way;
988
989 if (pvt->is_lockstep)
990 *channel_mask |= 1 << ((base_ch + 1) % 4);
991
992 offset = TAD_OFFSET(tad_offset);
993
Joe Perches956b9ba2012-04-29 17:08:39 -0300994 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",
995 n_tads,
996 addr,
997 limit,
998 (u32)TAD_SOCK(reg),
999 ch_way,
1000 offset,
1001 idx,
1002 base_ch,
1003 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001004
1005 /* Calculate channel address */
1006 /* Remove the TAD offset */
1007
1008 if (offset > addr) {
1009 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1010 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001011 return -EINVAL;
1012 }
1013 addr -= offset;
1014 /* Store the low bits [0:6] of the addr */
1015 ch_addr = addr & 0x7f;
1016 /* Remove socket wayness and remove 6 bits */
1017 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001018 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001019#if 0
1020 /* Divide by channel way */
1021 addr = addr / ch_way;
1022#endif
1023 /* Recover the last 6 bits */
1024 ch_addr |= addr << 6;
1025
1026 /*
1027 * Step 3) Decode rank
1028 */
1029 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1030 pci_read_config_dword(pvt->pci_tad[base_ch],
1031 rir_way_limit[n_rir],
1032 &reg);
1033
1034 if (!IS_RIR_VALID(reg))
1035 continue;
1036
1037 limit = RIR_LIMIT(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001038 mb = div_u64_rem(limit >> 20, 1000, &kb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001039 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1040 n_rir,
1041 mb, kb,
1042 limit,
1043 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001044 if (ch_addr <= limit)
1045 break;
1046 }
1047 if (n_rir == MAX_RIR_RANGES) {
1048 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1049 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001050 return -EINVAL;
1051 }
1052 rir_way = RIR_WAY(reg);
1053 if (pvt->is_close_pg)
1054 idx = (ch_addr >> 6);
1055 else
1056 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1057 idx %= 1 << rir_way;
1058
1059 pci_read_config_dword(pvt->pci_tad[base_ch],
1060 rir_offset[n_rir][idx],
1061 &reg);
1062 *rank = RIR_RNK_TGT(reg);
1063
Joe Perches956b9ba2012-04-29 17:08:39 -03001064 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1065 n_rir,
1066 ch_addr,
1067 limit,
1068 rir_way,
1069 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001070
1071 return 0;
1072}
1073
1074/****************************************************************************
1075 Device initialization routines: put/get, init/exit
1076 ****************************************************************************/
1077
1078/*
1079 * sbridge_put_all_devices 'put' all the devices that we have
1080 * reserved via 'get'
1081 */
1082static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1083{
1084 int i;
1085
Joe Perches956b9ba2012-04-29 17:08:39 -03001086 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001087 for (i = 0; i < sbridge_dev->n_devs; i++) {
1088 struct pci_dev *pdev = sbridge_dev->pdev[i];
1089 if (!pdev)
1090 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03001091 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1092 pdev->bus->number,
1093 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001094 pci_dev_put(pdev);
1095 }
1096}
1097
1098static void sbridge_put_all_devices(void)
1099{
1100 struct sbridge_dev *sbridge_dev, *tmp;
1101
1102 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1103 sbridge_put_devices(sbridge_dev);
1104 free_sbridge_dev(sbridge_dev);
1105 }
1106}
1107
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001108static int sbridge_get_onedevice(struct pci_dev **prev,
1109 u8 *num_mc,
1110 const struct pci_id_table *table,
1111 const unsigned devno)
1112{
1113 struct sbridge_dev *sbridge_dev;
1114 const struct pci_id_descr *dev_descr = &table->descr[devno];
1115
1116 struct pci_dev *pdev = NULL;
1117 u8 bus = 0;
1118
1119 sbridge_printk(KERN_INFO,
1120 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1121 dev_descr->dev, dev_descr->func,
1122 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1123
1124 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1125 dev_descr->dev_id, *prev);
1126
1127 if (!pdev) {
1128 if (*prev) {
1129 *prev = pdev;
1130 return 0;
1131 }
1132
1133 if (dev_descr->optional)
1134 return 0;
1135
1136 if (devno == 0)
1137 return -ENODEV;
1138
1139 sbridge_printk(KERN_INFO,
1140 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1141 dev_descr->dev, dev_descr->func,
1142 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1143
1144 /* End of list, leave */
1145 return -ENODEV;
1146 }
1147 bus = pdev->bus->number;
1148
1149 sbridge_dev = get_sbridge_dev(bus);
1150 if (!sbridge_dev) {
1151 sbridge_dev = alloc_sbridge_dev(bus, table);
1152 if (!sbridge_dev) {
1153 pci_dev_put(pdev);
1154 return -ENOMEM;
1155 }
1156 (*num_mc)++;
1157 }
1158
1159 if (sbridge_dev->pdev[devno]) {
1160 sbridge_printk(KERN_ERR,
1161 "Duplicated device for "
1162 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1163 bus, dev_descr->dev, dev_descr->func,
1164 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1165 pci_dev_put(pdev);
1166 return -ENODEV;
1167 }
1168
1169 sbridge_dev->pdev[devno] = pdev;
1170
1171 /* Sanity check */
1172 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1173 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1174 sbridge_printk(KERN_ERR,
1175 "Device PCI ID %04x:%04x "
1176 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1177 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1178 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1179 bus, dev_descr->dev, dev_descr->func);
1180 return -ENODEV;
1181 }
1182
1183 /* Be sure that the device is enabled */
1184 if (unlikely(pci_enable_device(pdev) < 0)) {
1185 sbridge_printk(KERN_ERR,
1186 "Couldn't enable "
1187 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1188 bus, dev_descr->dev, dev_descr->func,
1189 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1190 return -ENODEV;
1191 }
1192
Joe Perches956b9ba2012-04-29 17:08:39 -03001193 edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1194 bus, dev_descr->dev, dev_descr->func,
1195 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001196
1197 /*
1198 * As stated on drivers/pci/search.c, the reference count for
1199 * @from is always decremented if it is not %NULL. So, as we need
1200 * to get all devices up to null, we need to do a get for the device
1201 */
1202 pci_dev_get(pdev);
1203
1204 *prev = pdev;
1205
1206 return 0;
1207}
1208
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001209/*
1210 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
1211 * device/functions we want to reference for this driver.
1212 * Need to 'get' device 16 func 1 and func 2.
1213 * @num_mc: pointer to the memory controllers count, to be incremented in case
1214 * of success.
1215 * @table: model specific table
1216 *
1217 * returns 0 in case of success or error code
1218 */
1219static int sbridge_get_all_devices(u8 *num_mc,
1220 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001221{
1222 int i, rc;
1223 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001224
1225 while (table && table->descr) {
1226 for (i = 0; i < table->n_devs; i++) {
1227 pdev = NULL;
1228 do {
1229 rc = sbridge_get_onedevice(&pdev, num_mc,
1230 table, i);
1231 if (rc < 0) {
1232 if (i == 0) {
1233 i = table->n_devs;
1234 break;
1235 }
1236 sbridge_put_all_devices();
1237 return -ENODEV;
1238 }
1239 } while (pdev);
1240 }
1241 table++;
1242 }
1243
1244 return 0;
1245}
1246
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001247static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1248 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001249{
1250 struct sbridge_pvt *pvt = mci->pvt_info;
1251 struct pci_dev *pdev;
1252 int i, func, slot;
1253
1254 for (i = 0; i < sbridge_dev->n_devs; i++) {
1255 pdev = sbridge_dev->pdev[i];
1256 if (!pdev)
1257 continue;
1258 slot = PCI_SLOT(pdev->devfn);
1259 func = PCI_FUNC(pdev->devfn);
1260 switch (slot) {
1261 case 12:
1262 switch (func) {
1263 case 6:
1264 pvt->pci_sad0 = pdev;
1265 break;
1266 case 7:
1267 pvt->pci_sad1 = pdev;
1268 break;
1269 default:
1270 goto error;
1271 }
1272 break;
1273 case 13:
1274 switch (func) {
1275 case 6:
Aristeu Rozanski5f8a1b82013-10-30 13:26:58 -03001276 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001277 break;
1278 default:
1279 goto error;
1280 }
1281 break;
1282 case 14:
1283 switch (func) {
1284 case 0:
1285 pvt->pci_ha0 = pdev;
1286 break;
1287 default:
1288 goto error;
1289 }
1290 break;
1291 case 15:
1292 switch (func) {
1293 case 0:
1294 pvt->pci_ta = pdev;
1295 break;
1296 case 1:
1297 pvt->pci_ras = pdev;
1298 break;
1299 case 2:
1300 case 3:
1301 case 4:
1302 case 5:
1303 pvt->pci_tad[func - 2] = pdev;
1304 break;
1305 default:
1306 goto error;
1307 }
1308 break;
1309 case 17:
1310 switch (func) {
1311 case 0:
1312 pvt->pci_ddrio = pdev;
1313 break;
1314 default:
1315 goto error;
1316 }
1317 break;
1318 default:
1319 goto error;
1320 }
1321
Joe Perches956b9ba2012-04-29 17:08:39 -03001322 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1323 sbridge_dev->bus,
1324 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1325 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001326 }
1327
1328 /* Check if everything were registered */
1329 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07001330 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001331 goto enodev;
1332
1333 for (i = 0; i < NUM_CHANNELS; i++) {
1334 if (!pvt->pci_tad[i])
1335 goto enodev;
1336 }
1337 return 0;
1338
1339enodev:
1340 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1341 return -ENODEV;
1342
1343error:
1344 sbridge_printk(KERN_ERR, "Device %d, function %d "
1345 "is out of the expected range\n",
1346 slot, func);
1347 return -EINVAL;
1348}
1349
1350/****************************************************************************
1351 Error check routines
1352 ****************************************************************************/
1353
1354/*
1355 * While Sandy Bridge has error count registers, SMI BIOS read values from
1356 * and resets the counters. So, they are not reliable for the OS to read
1357 * from them. So, we have no option but to just trust on whatever MCE is
1358 * telling us about the errors.
1359 */
1360static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1361 const struct mce *m)
1362{
1363 struct mem_ctl_info *new_mci;
1364 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001365 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001366 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001367 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1368 bool overflow = GET_BITFIELD(m->status, 62, 62);
1369 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
1370 bool recoverable = GET_BITFIELD(m->status, 56, 56);
1371 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1372 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1373 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1374 u32 channel = GET_BITFIELD(m->status, 0, 3);
1375 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1376 long channel_mask, first_channel;
1377 u8 rank, socket;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001378 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001379 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001380
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001381 if (uncorrected_error) {
1382 if (ripv) {
1383 type = "FATAL";
1384 tp_event = HW_EVENT_ERR_FATAL;
1385 } else {
1386 type = "NON_FATAL";
1387 tp_event = HW_EVENT_ERR_UNCORRECTED;
1388 }
1389 } else {
1390 type = "CORRECTED";
1391 tp_event = HW_EVENT_ERR_CORRECTED;
1392 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001393
1394 /*
David Mackey15ed1032012-04-17 11:30:52 -07001395 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001396 * memory errors should fit in this mask:
1397 * 000f 0000 1mmm cccc (binary)
1398 * where:
1399 * f = Correction Report Filtering Bit. If 1, subsequent errors
1400 * won't be shown
1401 * mmm = error type
1402 * cccc = channel
1403 * If the mask doesn't match, report an error to the parsing logic
1404 */
1405 if (! ((errcode & 0xef80) == 0x80)) {
1406 optype = "Can't parse: it is not a mem";
1407 } else {
1408 switch (optypenum) {
1409 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001410 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001411 break;
1412 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001413 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001414 break;
1415 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001416 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001417 break;
1418 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001419 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001420 break;
1421 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001422 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001423 break;
1424 default:
1425 optype = "reserved";
1426 break;
1427 }
1428 }
1429
1430 rc = get_memory_error_data(mci, m->addr, &socket,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001431 &channel_mask, &rank, &area_type, msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001432 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001433 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001434 new_mci = get_mci_for_node_id(socket);
1435 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001436 strcpy(msg, "Error: socket got corrupted!");
1437 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001438 }
1439 mci = new_mci;
1440 pvt = mci->pvt_info;
1441
1442 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1443
1444 if (rank < 4)
1445 dimm = 0;
1446 else if (rank < 8)
1447 dimm = 1;
1448 else
1449 dimm = 2;
1450
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001451
1452 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001453 * FIXME: On some memory configurations (mirror, lockstep), the
1454 * Memory Controller can't point the error to a single DIMM. The
1455 * EDAC core should be handling the channel mask, in order to point
1456 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001457 */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001458 snprintf(msg, sizeof(msg),
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001459 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001460 overflow ? " OVERFLOW" : "",
1461 (uncorrected_error && recoverable) ? " recoverable" : "",
1462 area_type,
1463 mscod, errcode,
1464 socket,
1465 channel_mask,
1466 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001467
Joe Perches956b9ba2012-04-29 17:08:39 -03001468 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001469
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001470 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001471
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001472 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001473 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001474 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1475 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001476 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001477 return;
1478err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001479 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001480 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001481 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001482
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001483}
1484
1485/*
1486 * sbridge_check_error Retrieve and process errors reported by the
1487 * hardware. Called by the Core module.
1488 */
1489static void sbridge_check_error(struct mem_ctl_info *mci)
1490{
1491 struct sbridge_pvt *pvt = mci->pvt_info;
1492 int i;
1493 unsigned count = 0;
1494 struct mce *m;
1495
1496 /*
1497 * MCE first step: Copy all mce errors into a temporary buffer
1498 * We use a double buffering here, to reduce the risk of
1499 * loosing an error.
1500 */
1501 smp_rmb();
1502 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1503 % MCE_LOG_LEN;
1504 if (!count)
1505 return;
1506
1507 m = pvt->mce_outentry;
1508 if (pvt->mce_in + count > MCE_LOG_LEN) {
1509 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1510
1511 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1512 smp_wmb();
1513 pvt->mce_in = 0;
1514 count -= l;
1515 m += l;
1516 }
1517 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1518 smp_wmb();
1519 pvt->mce_in += count;
1520
1521 smp_rmb();
1522 if (pvt->mce_overrun) {
1523 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
1524 pvt->mce_overrun);
1525 smp_wmb();
1526 pvt->mce_overrun = 0;
1527 }
1528
1529 /*
1530 * MCE second step: parse errors and display
1531 */
1532 for (i = 0; i < count; i++)
1533 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
1534}
1535
1536/*
1537 * sbridge_mce_check_error Replicates mcelog routine to get errors
1538 * This routine simply queues mcelog errors, and
1539 * return. The error itself should be handled later
1540 * by sbridge_check_error.
1541 * WARNING: As this routine should be called at NMI time, extra care should
1542 * be taken to avoid deadlocks, and to be as fast as possible.
1543 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001544static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
1545 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001546{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001547 struct mce *mce = (struct mce *)data;
1548 struct mem_ctl_info *mci;
1549 struct sbridge_pvt *pvt;
1550
1551 mci = get_mci_for_node_id(mce->socketid);
1552 if (!mci)
1553 return NOTIFY_BAD;
1554 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001555
1556 /*
1557 * Just let mcelog handle it if the error is
1558 * outside the memory controller. A memory error
1559 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1560 * bit 12 has an special meaning.
1561 */
1562 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001563 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001564
1565 printk("sbridge: HANDLING MCE MEMORY ERROR\n");
1566
1567 printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
1568 mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
1569 printk("TSC %llx ", mce->tsc);
1570 printk("ADDR %llx ", mce->addr);
1571 printk("MISC %llx ", mce->misc);
1572
1573 printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
1574 mce->cpuvendor, mce->cpuid, mce->time,
1575 mce->socketid, mce->apicid);
1576
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001577 /* Only handle if it is the right mc controller */
1578 if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001579 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001580
1581 smp_rmb();
1582 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1583 smp_wmb();
1584 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001585 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001586 }
1587
1588 /* Copy memory error at the ringbuffer */
1589 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1590 smp_wmb();
1591 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1592
1593 /* Handle fatal errors immediately */
1594 if (mce->mcgstatus & 1)
1595 sbridge_check_error(mci);
1596
1597 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001598 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001599}
1600
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001601static struct notifier_block sbridge_mce_dec = {
1602 .notifier_call = sbridge_mce_check_error,
1603};
1604
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001605/****************************************************************************
1606 EDAC register/unregister logic
1607 ****************************************************************************/
1608
1609static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
1610{
1611 struct mem_ctl_info *mci = sbridge_dev->mci;
1612 struct sbridge_pvt *pvt;
1613
1614 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001615 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001616
1617 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
1618 return;
1619 }
1620
1621 pvt = mci->pvt_info;
1622
Joe Perches956b9ba2012-04-29 17:08:39 -03001623 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1624 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001625
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001626 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03001627 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001628
Joe Perches956b9ba2012-04-29 17:08:39 -03001629 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001630 kfree(mci->ctl_name);
1631 edac_mc_free(mci);
1632 sbridge_dev->mci = NULL;
1633}
1634
1635static int sbridge_register_mci(struct sbridge_dev *sbridge_dev)
1636{
1637 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001638 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001639 struct sbridge_pvt *pvt;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001640 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001641
1642 /* Check the number of active and not disabled channels */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001643 rc = check_if_ecc_is_active(sbridge_dev->bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001644 if (unlikely(rc < 0))
1645 return rc;
1646
1647 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001648 layers[0].type = EDAC_MC_LAYER_CHANNEL;
1649 layers[0].size = NUM_CHANNELS;
1650 layers[0].is_virt_csrow = false;
1651 layers[1].type = EDAC_MC_LAYER_SLOT;
1652 layers[1].size = MAX_DIMMS;
1653 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03001654 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001655 sizeof(*pvt));
1656
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001657 if (unlikely(!mci))
1658 return -ENOMEM;
1659
Joe Perches956b9ba2012-04-29 17:08:39 -03001660 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1661 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001662
1663 pvt = mci->pvt_info;
1664 memset(pvt, 0, sizeof(*pvt));
1665
1666 /* Associate sbridge_dev and mci for future usage */
1667 pvt->sbridge_dev = sbridge_dev;
1668 sbridge_dev->mci = mci;
1669
1670 mci->mtype_cap = MEM_FLAG_DDR3;
1671 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1672 mci->edac_cap = EDAC_FLAG_NONE;
1673 mci->mod_name = "sbridge_edac.c";
1674 mci->mod_ver = SBRIDGE_REVISION;
1675 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
1676 mci->dev_name = pci_name(sbridge_dev->pdev[0]);
1677 mci->ctl_page_to_phys = NULL;
Aristeu Rozanskifb79a502013-10-30 13:26:57 -03001678 pvt->info.get_tolm = sbridge_get_tolm;
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -03001679 pvt->info.get_tohm = sbridge_get_tohm;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001680 pvt->info.dram_rule = sbridge_dram_rule;
1681 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001682 pvt->info.interleave_list = sbridge_interleave_list;
1683 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001684 pvt->info.interleave_pkg = sbridge_interleave_pkg;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001685
1686 /* Set the function pointer to an actual operation function */
1687 mci->edac_check = sbridge_check_error;
1688
1689 /* Store pci devices at mci for faster access */
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001690 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001691 if (unlikely(rc < 0))
1692 goto fail0;
1693
1694 /* Get dimm basic config and the memory layout */
1695 get_dimm_config(mci);
1696 get_memory_layout(mci);
1697
1698 /* record ptr to the generic device */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03001699 mci->pdev = &sbridge_dev->pdev[0]->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001700
1701 /* add this new MC control structure to EDAC's list of MCs */
1702 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001703 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001704 rc = -EINVAL;
1705 goto fail0;
1706 }
1707
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001708 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001709
1710fail0:
1711 kfree(mci->ctl_name);
1712 edac_mc_free(mci);
1713 sbridge_dev->mci = NULL;
1714 return rc;
1715}
1716
1717/*
1718 * sbridge_probe Probe for ONE instance of device to see if it is
1719 * present.
1720 * return:
1721 * 0 for FOUND a device
1722 * < 0 for error code
1723 */
1724
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08001725static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001726{
1727 int rc;
1728 u8 mc, num_mc = 0;
1729 struct sbridge_dev *sbridge_dev;
1730
1731 /* get the pci devices we want to reserve for our use */
1732 mutex_lock(&sbridge_edac_lock);
1733
1734 /*
1735 * All memory controllers are allocated at the first pass.
1736 */
1737 if (unlikely(probed >= 1)) {
1738 mutex_unlock(&sbridge_edac_lock);
1739 return -ENODEV;
1740 }
1741 probed++;
1742
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001743 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001744 if (unlikely(rc < 0))
1745 goto fail0;
1746 mc = 0;
1747
1748 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03001749 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
1750 mc, mc + 1, num_mc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001751 sbridge_dev->mc = mc++;
1752 rc = sbridge_register_mci(sbridge_dev);
1753 if (unlikely(rc < 0))
1754 goto fail1;
1755 }
1756
1757 sbridge_printk(KERN_INFO, "Driver loaded.\n");
1758
1759 mutex_unlock(&sbridge_edac_lock);
1760 return 0;
1761
1762fail1:
1763 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
1764 sbridge_unregister_mci(sbridge_dev);
1765
1766 sbridge_put_all_devices();
1767fail0:
1768 mutex_unlock(&sbridge_edac_lock);
1769 return rc;
1770}
1771
1772/*
1773 * sbridge_remove destructor for one instance of device
1774 *
1775 */
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08001776static void sbridge_remove(struct pci_dev *pdev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001777{
1778 struct sbridge_dev *sbridge_dev;
1779
Joe Perches956b9ba2012-04-29 17:08:39 -03001780 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001781
1782 /*
1783 * we have a trouble here: pdev value for removal will be wrong, since
1784 * it will point to the X58 register used to detect that the machine
1785 * is a Nehalem or upper design. However, due to the way several PCI
1786 * devices are grouped together to provide MC functionality, we need
1787 * to use a different method for releasing the devices
1788 */
1789
1790 mutex_lock(&sbridge_edac_lock);
1791
1792 if (unlikely(!probed)) {
1793 mutex_unlock(&sbridge_edac_lock);
1794 return;
1795 }
1796
1797 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
1798 sbridge_unregister_mci(sbridge_dev);
1799
1800 /* Release PCI resources */
1801 sbridge_put_all_devices();
1802
1803 probed--;
1804
1805 mutex_unlock(&sbridge_edac_lock);
1806}
1807
1808MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
1809
1810/*
1811 * sbridge_driver pci_driver structure for this module
1812 *
1813 */
1814static struct pci_driver sbridge_driver = {
1815 .name = "sbridge_edac",
1816 .probe = sbridge_probe,
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08001817 .remove = sbridge_remove,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001818 .id_table = sbridge_pci_tbl,
1819};
1820
1821/*
1822 * sbridge_init Module entry function
1823 * Try to initialize this module for its devices
1824 */
1825static int __init sbridge_init(void)
1826{
1827 int pci_rc;
1828
Joe Perches956b9ba2012-04-29 17:08:39 -03001829 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001830
1831 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
1832 opstate_init();
1833
1834 pci_rc = pci_register_driver(&sbridge_driver);
1835
Chen Gonge35fca42012-05-08 20:40:12 -03001836 if (pci_rc >= 0) {
1837 mce_register_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001838 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03001839 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001840
1841 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
1842 pci_rc);
1843
1844 return pci_rc;
1845}
1846
1847/*
1848 * sbridge_exit() Module exit function
1849 * Unregister the driver
1850 */
1851static void __exit sbridge_exit(void)
1852{
Joe Perches956b9ba2012-04-29 17:08:39 -03001853 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001854 pci_unregister_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03001855 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001856}
1857
1858module_init(sbridge_init);
1859module_exit(sbridge_exit);
1860
1861module_param(edac_op_state, int, 0444);
1862MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
1863
1864MODULE_LICENSE("GPL");
1865MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1866MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
1867MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "
1868 SBRIDGE_REVISION);