blob: 21147ac38c4df6a157eebc73ef6fb9f484ea9208 [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
61 * easier for driver's development proccess. This table should be
62 * 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 */
86static const u32 dram_rule[] = {
87 0x80, 0x88, 0x90, 0x98, 0xa0,
88 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
89};
90#define MAX_SAD ARRAY_SIZE(dram_rule)
91
92#define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
93#define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
94#define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
95#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
96
97static char *get_dram_attr(u32 reg)
98{
99 switch(DRAM_ATTR(reg)) {
100 case 0:
101 return "DRAM";
102 case 1:
103 return "MMCFG";
104 case 2:
105 return "NXM";
106 default:
107 return "unknown";
108 }
109}
110
111static const u32 interleave_list[] = {
112 0x84, 0x8c, 0x94, 0x9c, 0xa4,
113 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
114};
115#define MAX_INTERLEAVE ARRAY_SIZE(interleave_list)
116
117#define SAD_PKG0(reg) GET_BITFIELD(reg, 0, 2)
118#define SAD_PKG1(reg) GET_BITFIELD(reg, 3, 5)
119#define SAD_PKG2(reg) GET_BITFIELD(reg, 8, 10)
120#define SAD_PKG3(reg) GET_BITFIELD(reg, 11, 13)
121#define SAD_PKG4(reg) GET_BITFIELD(reg, 16, 18)
122#define SAD_PKG5(reg) GET_BITFIELD(reg, 19, 21)
123#define SAD_PKG6(reg) GET_BITFIELD(reg, 24, 26)
124#define SAD_PKG7(reg) GET_BITFIELD(reg, 27, 29)
125
126static inline int sad_pkg(u32 reg, int interleave)
127{
128 switch (interleave) {
129 case 0:
130 return SAD_PKG0(reg);
131 case 1:
132 return SAD_PKG1(reg);
133 case 2:
134 return SAD_PKG2(reg);
135 case 3:
136 return SAD_PKG3(reg);
137 case 4:
138 return SAD_PKG4(reg);
139 case 5:
140 return SAD_PKG5(reg);
141 case 6:
142 return SAD_PKG6(reg);
143 case 7:
144 return SAD_PKG7(reg);
145 default:
146 return -EINVAL;
147 }
148}
149
150/* Devices 12 Function 7 */
151
152#define TOLM 0x80
153#define TOHM 0x84
154
155#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
156#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
157
158/* Device 13 Function 6 */
159
160#define SAD_TARGET 0xf0
161
162#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
163
164#define SAD_CONTROL 0xf4
165
166#define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
167
168/* Device 14 function 0 */
169
170static const u32 tad_dram_rule[] = {
171 0x40, 0x44, 0x48, 0x4c,
172 0x50, 0x54, 0x58, 0x5c,
173 0x60, 0x64, 0x68, 0x6c,
174};
175#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
176
177#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
178#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
179#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
180#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
181#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
182#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
183#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
184
185/* Device 15, function 0 */
186
187#define MCMTR 0x7c
188
189#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
190#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
191#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
192
193/* Device 15, function 1 */
194
195#define RASENABLES 0xac
196#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
197
198/* Device 15, functions 2-5 */
199
200static const int mtr_regs[] = {
201 0x80, 0x84, 0x88,
202};
203
204#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
205#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
206#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
207#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
208#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
209
210static const u32 tad_ch_nilv_offset[] = {
211 0x90, 0x94, 0x98, 0x9c,
212 0xa0, 0xa4, 0xa8, 0xac,
213 0xb0, 0xb4, 0xb8, 0xbc,
214};
215#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
216#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
217
218static const u32 rir_way_limit[] = {
219 0x108, 0x10c, 0x110, 0x114, 0x118,
220};
221#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
222
223#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
224#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
225#define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
226
227#define MAX_RIR_WAY 8
228
229static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
230 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
231 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
232 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
233 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
234 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
235};
236
237#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
238#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
239
240/* Device 16, functions 2-7 */
241
242/*
243 * FIXME: Implement the error count reads directly
244 */
245
246static const u32 correrrcnt[] = {
247 0x104, 0x108, 0x10c, 0x110,
248};
249
250#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
251#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
252#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
253#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
254
255static const u32 correrrthrsld[] = {
256 0x11c, 0x120, 0x124, 0x128,
257};
258
259#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
260#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
261
262
263/* Device 17, function 0 */
264
265#define RANK_CFG_A 0x0328
266
267#define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
268
269/*
270 * sbridge structs
271 */
272
273#define NUM_CHANNELS 4
274#define MAX_DIMMS 3 /* Max DIMMS per channel */
275
276struct sbridge_info {
277 u32 mcmtr;
278};
279
280struct sbridge_channel {
281 u32 ranks;
282 u32 dimms;
283};
284
285struct pci_id_descr {
286 int dev;
287 int func;
288 int dev_id;
289 int optional;
290};
291
292struct pci_id_table {
293 const struct pci_id_descr *descr;
294 int n_devs;
295};
296
297struct sbridge_dev {
298 struct list_head list;
299 u8 bus, mc;
300 u8 node_id, source_id;
301 struct pci_dev **pdev;
302 int n_devs;
303 struct mem_ctl_info *mci;
304};
305
306struct sbridge_pvt {
307 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
308 struct pci_dev *pci_sad0, *pci_sad1, *pci_ha0;
309 struct pci_dev *pci_br;
310 struct pci_dev *pci_tad[NUM_CHANNELS];
311
312 struct sbridge_dev *sbridge_dev;
313
314 struct sbridge_info info;
315 struct sbridge_channel channel[NUM_CHANNELS];
316
317 int csrow_map[NUM_CHANNELS][MAX_DIMMS];
318
319 /* Memory type detection */
320 bool is_mirrored, is_lockstep, is_close_pg;
321
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200322 /* Fifo double buffers */
323 struct mce mce_entry[MCE_LOG_LEN];
324 struct mce mce_outentry[MCE_LOG_LEN];
325
326 /* Fifo in/out counters */
327 unsigned mce_in, mce_out;
328
329 /* Count indicator to show errors not got */
330 unsigned mce_overrun;
331
332 /* Memory description */
333 u64 tolm, tohm;
334};
335
336#define PCI_DESCR(device, function, device_id) \
337 .dev = (device), \
338 .func = (function), \
339 .dev_id = (device_id)
340
341static const struct pci_id_descr pci_dev_descr_sbridge[] = {
342 /* Processor Home Agent */
343 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0) },
344
345 /* Memory controller */
346 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA) },
347 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS) },
348 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0) },
349 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1) },
350 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2) },
351 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3) },
352 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO) },
353
354 /* System Address Decoder */
355 { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0) },
356 { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1) },
357
358 /* Broadcast Registers */
359 { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR) },
360};
361
362#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
363static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
364 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
365 {0,} /* 0 terminated list. */
366};
367
368/*
369 * pci_device_id table for which devices we are looking for
370 */
Lionel Debroux36c46f32012-02-27 07:41:47 +0100371static DEFINE_PCI_DEVICE_TABLE(sbridge_pci_tbl) = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200372 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
373 {0,} /* 0 terminated list. */
374};
375
376
377/****************************************************************************
378 Anciliary status routines
379 ****************************************************************************/
380
381static inline int numrank(u32 mtr)
382{
383 int ranks = (1 << RANK_CNT_BITS(mtr));
384
385 if (ranks > 4) {
386 debugf0("Invalid number of ranks: %d (max = 4) raw value = %x (%04x)",
387 ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
388 return -EINVAL;
389 }
390
391 return ranks;
392}
393
394static inline int numrow(u32 mtr)
395{
396 int rows = (RANK_WIDTH_BITS(mtr) + 12);
397
398 if (rows < 13 || rows > 18) {
399 debugf0("Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)",
400 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
401 return -EINVAL;
402 }
403
404 return 1 << rows;
405}
406
407static inline int numcol(u32 mtr)
408{
409 int cols = (COL_WIDTH_BITS(mtr) + 10);
410
411 if (cols > 12) {
412 debugf0("Invalid number of cols: %d (max = 4) raw value = %x (%04x)",
413 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
414 return -EINVAL;
415 }
416
417 return 1 << cols;
418}
419
420static struct sbridge_dev *get_sbridge_dev(u8 bus)
421{
422 struct sbridge_dev *sbridge_dev;
423
424 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
425 if (sbridge_dev->bus == bus)
426 return sbridge_dev;
427 }
428
429 return NULL;
430}
431
432static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
433 const struct pci_id_table *table)
434{
435 struct sbridge_dev *sbridge_dev;
436
437 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
438 if (!sbridge_dev)
439 return NULL;
440
441 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
442 GFP_KERNEL);
443 if (!sbridge_dev->pdev) {
444 kfree(sbridge_dev);
445 return NULL;
446 }
447
448 sbridge_dev->bus = bus;
449 sbridge_dev->n_devs = table->n_devs;
450 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
451
452 return sbridge_dev;
453}
454
455static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
456{
457 list_del(&sbridge_dev->list);
458 kfree(sbridge_dev->pdev);
459 kfree(sbridge_dev);
460}
461
462/****************************************************************************
463 Memory check routines
464 ****************************************************************************/
465static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
466 unsigned func)
467{
468 struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
469 int i;
470
471 if (!sbridge_dev)
472 return NULL;
473
474 for (i = 0; i < sbridge_dev->n_devs; i++) {
475 if (!sbridge_dev->pdev[i])
476 continue;
477
478 if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
479 PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
480 debugf1("Associated %02x.%02x.%d with %p\n",
481 bus, slot, func, sbridge_dev->pdev[i]);
482 return sbridge_dev->pdev[i];
483 }
484 }
485
486 return NULL;
487}
488
489/**
490 * sbridge_get_active_channels() - gets the number of channels and csrows
491 * bus: Device bus
492 * @channels: Number of channels that will be returned
493 * @csrows: Number of csrows found
494 *
495 * Since EDAC core needs to know in advance the number of available channels
496 * and csrows, in order to allocate memory for csrows/channels, it is needed
497 * to run two similar steps. At the first step, implemented on this function,
498 * it checks the number of csrows/channels present at one socket, identified
499 * by the associated PCI bus.
500 * this is used in order to properly allocate the size of mci components.
501 * Note: one csrow is one dimm.
502 */
503static int sbridge_get_active_channels(const u8 bus, unsigned *channels,
504 unsigned *csrows)
505{
506 struct pci_dev *pdev = NULL;
507 int i, j;
508 u32 mcmtr;
509
510 *channels = 0;
511 *csrows = 0;
512
513 pdev = get_pdev_slot_func(bus, 15, 0);
514 if (!pdev) {
515 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
516 "%2x.%02d.%d!!!\n",
517 bus, 15, 0);
518 return -ENODEV;
519 }
520
521 pci_read_config_dword(pdev, MCMTR, &mcmtr);
522 if (!IS_ECC_ENABLED(mcmtr)) {
523 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
524 return -ENODEV;
525 }
526
527 for (i = 0; i < NUM_CHANNELS; i++) {
528 u32 mtr;
529
530 /* Device 15 functions 2 - 5 */
531 pdev = get_pdev_slot_func(bus, 15, 2 + i);
532 if (!pdev) {
533 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
534 "%2x.%02d.%d!!!\n",
535 bus, 15, 2 + i);
536 return -ENODEV;
537 }
538 (*channels)++;
539
540 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
541 pci_read_config_dword(pdev, mtr_regs[j], &mtr);
542 debugf1("Bus#%02x channel #%d MTR%d = %x\n", bus, i, j, mtr);
543 if (IS_DIMM_PRESENT(mtr))
544 (*csrows)++;
545 }
546 }
547
548 debugf0("Number of active channels: %d, number of active dimms: %d\n",
549 *channels, *csrows);
550
551 return 0;
552}
553
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300554static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200555{
556 struct sbridge_pvt *pvt = mci->pvt_info;
557 struct csrow_info *csr;
558 int i, j, banks, ranks, rows, cols, size, npages;
559 int csrow = 0;
560 unsigned long last_page = 0;
561 u32 reg;
562 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200563 enum mem_type mtype;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300564 struct dimm_info *dimm;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200565
566 pci_read_config_dword(pvt->pci_br, SAD_TARGET, &reg);
567 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
568
569 pci_read_config_dword(pvt->pci_br, SAD_CONTROL, &reg);
570 pvt->sbridge_dev->node_id = NODE_ID(reg);
571 debugf0("mc#%d: Node ID: %d, source ID: %d\n",
572 pvt->sbridge_dev->mc,
573 pvt->sbridge_dev->node_id,
574 pvt->sbridge_dev->source_id);
575
576 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
577 if (IS_MIRROR_ENABLED(reg)) {
578 debugf0("Memory mirror is enabled\n");
579 pvt->is_mirrored = true;
580 } else {
581 debugf0("Memory mirror is disabled\n");
582 pvt->is_mirrored = false;
583 }
584
585 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
586 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
587 debugf0("Lockstep is enabled\n");
588 mode = EDAC_S8ECD8ED;
589 pvt->is_lockstep = true;
590 } else {
591 debugf0("Lockstep is disabled\n");
592 mode = EDAC_S4ECD4ED;
593 pvt->is_lockstep = false;
594 }
595 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
596 debugf0("address map is on closed page mode\n");
597 pvt->is_close_pg = true;
598 } else {
599 debugf0("address map is on open page mode\n");
600 pvt->is_close_pg = false;
601 }
602
603 pci_read_config_dword(pvt->pci_ta, RANK_CFG_A, &reg);
604 if (IS_RDIMM_ENABLED(reg)) {
605 /* FIXME: Can also be LRDIMM */
606 debugf0("Memory is registered\n");
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200607 mtype = MEM_RDDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200608 } else {
609 debugf0("Memory is unregistered\n");
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200610 mtype = MEM_DDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200611 }
612
613 /* On all supported DDR3 DIMM types, there are 8 banks available */
614 banks = 8;
615
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300616 dimm = mci->dimms;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200617 for (i = 0; i < NUM_CHANNELS; i++) {
618 u32 mtr;
619
620 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
621 pci_read_config_dword(pvt->pci_tad[i],
622 mtr_regs[j], &mtr);
623 debugf4("Channel #%d MTR%d = %x\n", i, j, mtr);
624 if (IS_DIMM_PRESENT(mtr)) {
625 pvt->channel[i].dimms++;
626
627 ranks = numrank(mtr);
628 rows = numrow(mtr);
629 cols = numcol(mtr);
630
631 /* DDR3 has 8 I/O banks */
632 size = (rows * cols * banks * ranks) >> (20 - 3);
633 npages = MiB_TO_PAGES(size);
634
635 debugf0("mc#%d: channel %d, dimm %d, %d Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
636 pvt->sbridge_dev->mc, i, j,
637 size, npages,
638 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200639
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300640 /*
641 * Fake stuff. This controller doesn't see
642 * csrows.
643 */
644 csr = &mci->csrows[csrow];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200645 csr->first_page = last_page;
646 csr->last_page = last_page + npages - 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200647 csr->nr_pages = npages;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200648 csr->csrow_idx = csrow;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200649 csr->nr_channels = 1;
650 csr->channels[0].chan_idx = i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200651 pvt->csrow_map[i][j] = csrow;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200652 last_page += npages;
653 csrow++;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300654
655 csr->channels[0].dimm = dimm;
656 dimm->grain = 32;
657 dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
658 dimm->mtype = mtype;
659 dimm->edac_mode = mode;
660 snprintf(dimm->label, sizeof(dimm->label),
661 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
662 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200663 }
664 }
665 }
666
667 return 0;
668}
669
670static void get_memory_layout(const struct mem_ctl_info *mci)
671{
672 struct sbridge_pvt *pvt = mci->pvt_info;
673 int i, j, k, n_sads, n_tads, sad_interl;
674 u32 reg;
675 u64 limit, prv = 0;
676 u64 tmp_mb;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300677 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200678 u32 rir_way;
679
680 /*
681 * Step 1) Get TOLM/TOHM ranges
682 */
683
684 /* Address range is 32:28 */
685 pci_read_config_dword(pvt->pci_sad1, TOLM,
686 &reg);
687 pvt->tolm = GET_TOLM(reg);
688 tmp_mb = (1 + pvt->tolm) >> 20;
689
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300690 mb = div_u64_rem(tmp_mb, 1000, &kb);
691 debugf0("TOLM: %u.%03u GB (0x%016Lx)\n",
692 mb, kb, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200693
694 /* Address range is already 45:25 */
695 pci_read_config_dword(pvt->pci_sad1, TOHM,
696 &reg);
697 pvt->tohm = GET_TOHM(reg);
698 tmp_mb = (1 + pvt->tohm) >> 20;
699
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300700 mb = div_u64_rem(tmp_mb, 1000, &kb);
701 debugf0("TOHM: %u.%03u GB (0x%016Lx)",
702 mb, kb, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200703
704 /*
705 * Step 2) Get SAD range and SAD Interleave list
706 * TAD registers contain the interleave wayness. However, it
707 * seems simpler to just discover it indirectly, with the
708 * algorithm bellow.
709 */
710 prv = 0;
711 for (n_sads = 0; n_sads < MAX_SAD; n_sads++) {
712 /* SAD_LIMIT Address range is 45:26 */
713 pci_read_config_dword(pvt->pci_sad0, dram_rule[n_sads],
714 &reg);
715 limit = SAD_LIMIT(reg);
716
717 if (!DRAM_RULE_ENABLE(reg))
718 continue;
719
720 if (limit <= prv)
721 break;
722
723 tmp_mb = (limit + 1) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300724 mb = div_u64_rem(tmp_mb, 1000, &kb);
725 debugf0("SAD#%d %s up to %u.%03u GB (0x%016Lx) %s reg=0x%08x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200726 n_sads,
727 get_dram_attr(reg),
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300728 mb, kb,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200729 ((u64)tmp_mb) << 20L,
730 INTERLEAVE_MODE(reg) ? "Interleave: 8:6" : "Interleave: [8:6]XOR[18:16]",
731 reg);
732 prv = limit;
733
734 pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
735 &reg);
736 sad_interl = sad_pkg(reg, 0);
737 for (j = 0; j < 8; j++) {
738 if (j > 0 && sad_interl == sad_pkg(reg, j))
739 break;
740
741 debugf0("SAD#%d, interleave #%d: %d\n",
742 n_sads, j, sad_pkg(reg, j));
743 }
744 }
745
746 /*
747 * Step 3) Get TAD range
748 */
749 prv = 0;
750 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
751 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
752 &reg);
753 limit = TAD_LIMIT(reg);
754 if (limit <= prv)
755 break;
756 tmp_mb = (limit + 1) >> 20;
757
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300758 mb = div_u64_rem(tmp_mb, 1000, &kb);
759 debugf0("TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
760 n_tads, mb, kb,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200761 ((u64)tmp_mb) << 20L,
762 (u32)TAD_SOCK(reg),
763 (u32)TAD_CH(reg),
764 (u32)TAD_TGT0(reg),
765 (u32)TAD_TGT1(reg),
766 (u32)TAD_TGT2(reg),
767 (u32)TAD_TGT3(reg),
768 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -0300769 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200770 }
771
772 /*
773 * Step 4) Get TAD offsets, per each channel
774 */
775 for (i = 0; i < NUM_CHANNELS; i++) {
776 if (!pvt->channel[i].dimms)
777 continue;
778 for (j = 0; j < n_tads; j++) {
779 pci_read_config_dword(pvt->pci_tad[i],
780 tad_ch_nilv_offset[j],
781 &reg);
782 tmp_mb = TAD_OFFSET(reg) >> 20;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300783 mb = div_u64_rem(tmp_mb, 1000, &kb);
784 debugf0("TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200785 i, j,
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300786 mb, kb,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200787 ((u64)tmp_mb) << 20L,
788 reg);
789 }
790 }
791
792 /*
793 * Step 6) Get RIR Wayness/Limit, per each channel
794 */
795 for (i = 0; i < NUM_CHANNELS; i++) {
796 if (!pvt->channel[i].dimms)
797 continue;
798 for (j = 0; j < MAX_RIR_RANGES; j++) {
799 pci_read_config_dword(pvt->pci_tad[i],
800 rir_way_limit[j],
801 &reg);
802
803 if (!IS_RIR_VALID(reg))
804 continue;
805
806 tmp_mb = RIR_LIMIT(reg) >> 20;
807 rir_way = 1 << RIR_WAY(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300808 mb = div_u64_rem(tmp_mb, 1000, &kb);
809 debugf0("CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200810 i, j,
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300811 mb, kb,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200812 ((u64)tmp_mb) << 20L,
813 rir_way,
814 reg);
815
816 for (k = 0; k < rir_way; k++) {
817 pci_read_config_dword(pvt->pci_tad[i],
818 rir_offset[j][k],
819 &reg);
820 tmp_mb = RIR_OFFSET(reg) << 6;
821
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300822 mb = div_u64_rem(tmp_mb, 1000, &kb);
823 debugf0("CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200824 i, j, k,
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300825 mb, kb,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200826 ((u64)tmp_mb) << 20L,
827 (u32)RIR_RNK_TGT(reg),
828 reg);
829 }
830 }
831 }
832}
833
834struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
835{
836 struct sbridge_dev *sbridge_dev;
837
838 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
839 if (sbridge_dev->node_id == node_id)
840 return sbridge_dev->mci;
841 }
842 return NULL;
843}
844
845static int get_memory_error_data(struct mem_ctl_info *mci,
846 u64 addr,
847 u8 *socket,
848 long *channel_mask,
849 u8 *rank,
850 char *area_type)
851{
852 struct mem_ctl_info *new_mci;
853 struct sbridge_pvt *pvt = mci->pvt_info;
854 char msg[256];
855 int n_rir, n_sads, n_tads, sad_way, sck_xch;
856 int sad_interl, idx, base_ch;
857 int interleave_mode;
858 unsigned sad_interleave[MAX_INTERLEAVE];
859 u32 reg;
860 u8 ch_way,sck_way;
861 u32 tad_offset;
862 u32 rir_way;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300863 u32 mb, kb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200864 u64 ch_addr, offset, limit, prv = 0;
865
866
867 /*
868 * Step 0) Check if the address is at special memory ranges
869 * The check bellow is probably enough to fill all cases where
870 * the error is not inside a memory, except for the legacy
871 * range (e. g. VGA addresses). It is unlikely, however, that the
872 * memory controller would generate an error on that range.
873 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -0300874 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200875 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
876 edac_mc_handle_ce_no_info(mci, msg);
877 return -EINVAL;
878 }
879 if (addr >= (u64)pvt->tohm) {
880 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
881 edac_mc_handle_ce_no_info(mci, msg);
882 return -EINVAL;
883 }
884
885 /*
886 * Step 1) Get socket
887 */
888 for (n_sads = 0; n_sads < MAX_SAD; n_sads++) {
889 pci_read_config_dword(pvt->pci_sad0, dram_rule[n_sads],
890 &reg);
891
892 if (!DRAM_RULE_ENABLE(reg))
893 continue;
894
895 limit = SAD_LIMIT(reg);
896 if (limit <= prv) {
897 sprintf(msg, "Can't discover the memory socket");
898 edac_mc_handle_ce_no_info(mci, msg);
899 return -EINVAL;
900 }
901 if (addr <= limit)
902 break;
903 prv = limit;
904 }
905 if (n_sads == MAX_SAD) {
906 sprintf(msg, "Can't discover the memory socket");
907 edac_mc_handle_ce_no_info(mci, msg);
908 return -EINVAL;
909 }
910 area_type = get_dram_attr(reg);
911 interleave_mode = INTERLEAVE_MODE(reg);
912
913 pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
914 &reg);
915 sad_interl = sad_pkg(reg, 0);
916 for (sad_way = 0; sad_way < 8; sad_way++) {
917 if (sad_way > 0 && sad_interl == sad_pkg(reg, sad_way))
918 break;
919 sad_interleave[sad_way] = sad_pkg(reg, sad_way);
920 debugf0("SAD interleave #%d: %d\n",
921 sad_way, sad_interleave[sad_way]);
922 }
923 debugf0("mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
924 pvt->sbridge_dev->mc,
925 n_sads,
926 addr,
927 limit,
928 sad_way + 7,
Hui Wangad9c40b2012-02-06 04:11:00 -0300929 interleave_mode ? "" : "XOR[18:16]");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200930 if (interleave_mode)
931 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
932 else
933 idx = (addr >> 6) & 7;
934 switch (sad_way) {
935 case 1:
936 idx = 0;
937 break;
938 case 2:
939 idx = idx & 1;
940 break;
941 case 4:
942 idx = idx & 3;
943 break;
944 case 8:
945 break;
946 default:
947 sprintf(msg, "Can't discover socket interleave");
948 edac_mc_handle_ce_no_info(mci, msg);
949 return -EINVAL;
950 }
951 *socket = sad_interleave[idx];
952 debugf0("SAD interleave index: %d (wayness %d) = CPU socket %d\n",
953 idx, sad_way, *socket);
954
955 /*
956 * Move to the proper node structure, in order to access the
957 * right PCI registers
958 */
959 new_mci = get_mci_for_node_id(*socket);
960 if (!new_mci) {
961 sprintf(msg, "Struct for socket #%u wasn't initialized",
962 *socket);
963 edac_mc_handle_ce_no_info(mci, msg);
964 return -EINVAL;
965 }
966 mci = new_mci;
967 pvt = mci->pvt_info;
968
969 /*
970 * Step 2) Get memory channel
971 */
972 prv = 0;
973 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
974 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
975 &reg);
976 limit = TAD_LIMIT(reg);
977 if (limit <= prv) {
978 sprintf(msg, "Can't discover the memory channel");
979 edac_mc_handle_ce_no_info(mci, msg);
980 return -EINVAL;
981 }
982 if (addr <= limit)
983 break;
984 prv = limit;
985 }
986 ch_way = TAD_CH(reg) + 1;
987 sck_way = TAD_SOCK(reg) + 1;
988 /*
989 * FIXME: Is it right to always use channel 0 for offsets?
990 */
991 pci_read_config_dword(pvt->pci_tad[0],
992 tad_ch_nilv_offset[n_tads],
993 &tad_offset);
994
995 if (ch_way == 3)
996 idx = addr >> 6;
997 else
998 idx = addr >> (6 + sck_way);
999 idx = idx % ch_way;
1000
1001 /*
1002 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1003 */
1004 switch (idx) {
1005 case 0:
1006 base_ch = TAD_TGT0(reg);
1007 break;
1008 case 1:
1009 base_ch = TAD_TGT1(reg);
1010 break;
1011 case 2:
1012 base_ch = TAD_TGT2(reg);
1013 break;
1014 case 3:
1015 base_ch = TAD_TGT3(reg);
1016 break;
1017 default:
1018 sprintf(msg, "Can't discover the TAD target");
1019 edac_mc_handle_ce_no_info(mci, msg);
1020 return -EINVAL;
1021 }
1022 *channel_mask = 1 << base_ch;
1023
1024 if (pvt->is_mirrored) {
1025 *channel_mask |= 1 << ((base_ch + 2) % 4);
1026 switch(ch_way) {
1027 case 2:
1028 case 4:
1029 sck_xch = 1 << sck_way * (ch_way >> 1);
1030 break;
1031 default:
1032 sprintf(msg, "Invalid mirror set. Can't decode addr");
1033 edac_mc_handle_ce_no_info(mci, msg);
1034 return -EINVAL;
1035 }
1036 } else
1037 sck_xch = (1 << sck_way) * ch_way;
1038
1039 if (pvt->is_lockstep)
1040 *channel_mask |= 1 << ((base_ch + 1) % 4);
1041
1042 offset = TAD_OFFSET(tad_offset);
1043
1044 debugf0("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",
1045 n_tads,
1046 addr,
1047 limit,
1048 (u32)TAD_SOCK(reg),
1049 ch_way,
1050 offset,
1051 idx,
1052 base_ch,
1053 *channel_mask);
1054
1055 /* Calculate channel address */
1056 /* Remove the TAD offset */
1057
1058 if (offset > addr) {
1059 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1060 offset, addr);
1061 edac_mc_handle_ce_no_info(mci, msg);
1062 return -EINVAL;
1063 }
1064 addr -= offset;
1065 /* Store the low bits [0:6] of the addr */
1066 ch_addr = addr & 0x7f;
1067 /* Remove socket wayness and remove 6 bits */
1068 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001069 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001070#if 0
1071 /* Divide by channel way */
1072 addr = addr / ch_way;
1073#endif
1074 /* Recover the last 6 bits */
1075 ch_addr |= addr << 6;
1076
1077 /*
1078 * Step 3) Decode rank
1079 */
1080 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1081 pci_read_config_dword(pvt->pci_tad[base_ch],
1082 rir_way_limit[n_rir],
1083 &reg);
1084
1085 if (!IS_RIR_VALID(reg))
1086 continue;
1087
1088 limit = RIR_LIMIT(reg);
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001089 mb = div_u64_rem(limit >> 20, 1000, &kb);
1090 debugf0("RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001091 n_rir,
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001092 mb, kb,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001093 limit,
1094 1 << RIR_WAY(reg));
1095 if (ch_addr <= limit)
1096 break;
1097 }
1098 if (n_rir == MAX_RIR_RANGES) {
1099 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1100 ch_addr);
1101 edac_mc_handle_ce_no_info(mci, msg);
1102 return -EINVAL;
1103 }
1104 rir_way = RIR_WAY(reg);
1105 if (pvt->is_close_pg)
1106 idx = (ch_addr >> 6);
1107 else
1108 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1109 idx %= 1 << rir_way;
1110
1111 pci_read_config_dword(pvt->pci_tad[base_ch],
1112 rir_offset[n_rir][idx],
1113 &reg);
1114 *rank = RIR_RNK_TGT(reg);
1115
1116 debugf0("RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1117 n_rir,
1118 ch_addr,
1119 limit,
1120 rir_way,
1121 idx);
1122
1123 return 0;
1124}
1125
1126/****************************************************************************
1127 Device initialization routines: put/get, init/exit
1128 ****************************************************************************/
1129
1130/*
1131 * sbridge_put_all_devices 'put' all the devices that we have
1132 * reserved via 'get'
1133 */
1134static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1135{
1136 int i;
1137
1138 debugf0(__FILE__ ": %s()\n", __func__);
1139 for (i = 0; i < sbridge_dev->n_devs; i++) {
1140 struct pci_dev *pdev = sbridge_dev->pdev[i];
1141 if (!pdev)
1142 continue;
1143 debugf0("Removing dev %02x:%02x.%d\n",
1144 pdev->bus->number,
1145 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1146 pci_dev_put(pdev);
1147 }
1148}
1149
1150static void sbridge_put_all_devices(void)
1151{
1152 struct sbridge_dev *sbridge_dev, *tmp;
1153
1154 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1155 sbridge_put_devices(sbridge_dev);
1156 free_sbridge_dev(sbridge_dev);
1157 }
1158}
1159
1160/*
1161 * sbridge_get_all_devices Find and perform 'get' operation on the MCH's
1162 * device/functions we want to reference for this driver
1163 *
1164 * Need to 'get' device 16 func 1 and func 2
1165 */
1166static int sbridge_get_onedevice(struct pci_dev **prev,
1167 u8 *num_mc,
1168 const struct pci_id_table *table,
1169 const unsigned devno)
1170{
1171 struct sbridge_dev *sbridge_dev;
1172 const struct pci_id_descr *dev_descr = &table->descr[devno];
1173
1174 struct pci_dev *pdev = NULL;
1175 u8 bus = 0;
1176
1177 sbridge_printk(KERN_INFO,
1178 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1179 dev_descr->dev, dev_descr->func,
1180 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1181
1182 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1183 dev_descr->dev_id, *prev);
1184
1185 if (!pdev) {
1186 if (*prev) {
1187 *prev = pdev;
1188 return 0;
1189 }
1190
1191 if (dev_descr->optional)
1192 return 0;
1193
1194 if (devno == 0)
1195 return -ENODEV;
1196
1197 sbridge_printk(KERN_INFO,
1198 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1199 dev_descr->dev, dev_descr->func,
1200 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1201
1202 /* End of list, leave */
1203 return -ENODEV;
1204 }
1205 bus = pdev->bus->number;
1206
1207 sbridge_dev = get_sbridge_dev(bus);
1208 if (!sbridge_dev) {
1209 sbridge_dev = alloc_sbridge_dev(bus, table);
1210 if (!sbridge_dev) {
1211 pci_dev_put(pdev);
1212 return -ENOMEM;
1213 }
1214 (*num_mc)++;
1215 }
1216
1217 if (sbridge_dev->pdev[devno]) {
1218 sbridge_printk(KERN_ERR,
1219 "Duplicated device for "
1220 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1221 bus, dev_descr->dev, dev_descr->func,
1222 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1223 pci_dev_put(pdev);
1224 return -ENODEV;
1225 }
1226
1227 sbridge_dev->pdev[devno] = pdev;
1228
1229 /* Sanity check */
1230 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1231 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1232 sbridge_printk(KERN_ERR,
1233 "Device PCI ID %04x:%04x "
1234 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1235 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1236 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1237 bus, dev_descr->dev, dev_descr->func);
1238 return -ENODEV;
1239 }
1240
1241 /* Be sure that the device is enabled */
1242 if (unlikely(pci_enable_device(pdev) < 0)) {
1243 sbridge_printk(KERN_ERR,
1244 "Couldn't enable "
1245 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1246 bus, dev_descr->dev, dev_descr->func,
1247 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1248 return -ENODEV;
1249 }
1250
1251 debugf0("Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1252 bus, dev_descr->dev,
1253 dev_descr->func,
1254 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1255
1256 /*
1257 * As stated on drivers/pci/search.c, the reference count for
1258 * @from is always decremented if it is not %NULL. So, as we need
1259 * to get all devices up to null, we need to do a get for the device
1260 */
1261 pci_dev_get(pdev);
1262
1263 *prev = pdev;
1264
1265 return 0;
1266}
1267
1268static int sbridge_get_all_devices(u8 *num_mc)
1269{
1270 int i, rc;
1271 struct pci_dev *pdev = NULL;
1272 const struct pci_id_table *table = pci_dev_descr_sbridge_table;
1273
1274 while (table && table->descr) {
1275 for (i = 0; i < table->n_devs; i++) {
1276 pdev = NULL;
1277 do {
1278 rc = sbridge_get_onedevice(&pdev, num_mc,
1279 table, i);
1280 if (rc < 0) {
1281 if (i == 0) {
1282 i = table->n_devs;
1283 break;
1284 }
1285 sbridge_put_all_devices();
1286 return -ENODEV;
1287 }
1288 } while (pdev);
1289 }
1290 table++;
1291 }
1292
1293 return 0;
1294}
1295
1296static int mci_bind_devs(struct mem_ctl_info *mci,
1297 struct sbridge_dev *sbridge_dev)
1298{
1299 struct sbridge_pvt *pvt = mci->pvt_info;
1300 struct pci_dev *pdev;
1301 int i, func, slot;
1302
1303 for (i = 0; i < sbridge_dev->n_devs; i++) {
1304 pdev = sbridge_dev->pdev[i];
1305 if (!pdev)
1306 continue;
1307 slot = PCI_SLOT(pdev->devfn);
1308 func = PCI_FUNC(pdev->devfn);
1309 switch (slot) {
1310 case 12:
1311 switch (func) {
1312 case 6:
1313 pvt->pci_sad0 = pdev;
1314 break;
1315 case 7:
1316 pvt->pci_sad1 = pdev;
1317 break;
1318 default:
1319 goto error;
1320 }
1321 break;
1322 case 13:
1323 switch (func) {
1324 case 6:
1325 pvt->pci_br = pdev;
1326 break;
1327 default:
1328 goto error;
1329 }
1330 break;
1331 case 14:
1332 switch (func) {
1333 case 0:
1334 pvt->pci_ha0 = pdev;
1335 break;
1336 default:
1337 goto error;
1338 }
1339 break;
1340 case 15:
1341 switch (func) {
1342 case 0:
1343 pvt->pci_ta = pdev;
1344 break;
1345 case 1:
1346 pvt->pci_ras = pdev;
1347 break;
1348 case 2:
1349 case 3:
1350 case 4:
1351 case 5:
1352 pvt->pci_tad[func - 2] = pdev;
1353 break;
1354 default:
1355 goto error;
1356 }
1357 break;
1358 case 17:
1359 switch (func) {
1360 case 0:
1361 pvt->pci_ddrio = pdev;
1362 break;
1363 default:
1364 goto error;
1365 }
1366 break;
1367 default:
1368 goto error;
1369 }
1370
1371 debugf0("Associated PCI %02x.%02d.%d with dev = %p\n",
1372 sbridge_dev->bus,
1373 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1374 pdev);
1375 }
1376
1377 /* Check if everything were registered */
1378 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
1379 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta ||
1380 !pvt->pci_ddrio)
1381 goto enodev;
1382
1383 for (i = 0; i < NUM_CHANNELS; i++) {
1384 if (!pvt->pci_tad[i])
1385 goto enodev;
1386 }
1387 return 0;
1388
1389enodev:
1390 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1391 return -ENODEV;
1392
1393error:
1394 sbridge_printk(KERN_ERR, "Device %d, function %d "
1395 "is out of the expected range\n",
1396 slot, func);
1397 return -EINVAL;
1398}
1399
1400/****************************************************************************
1401 Error check routines
1402 ****************************************************************************/
1403
1404/*
1405 * While Sandy Bridge has error count registers, SMI BIOS read values from
1406 * and resets the counters. So, they are not reliable for the OS to read
1407 * from them. So, we have no option but to just trust on whatever MCE is
1408 * telling us about the errors.
1409 */
1410static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1411 const struct mce *m)
1412{
1413 struct mem_ctl_info *new_mci;
1414 struct sbridge_pvt *pvt = mci->pvt_info;
1415 char *type, *optype, *msg, *recoverable_msg;
1416 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1417 bool overflow = GET_BITFIELD(m->status, 62, 62);
1418 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
1419 bool recoverable = GET_BITFIELD(m->status, 56, 56);
1420 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1421 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1422 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1423 u32 channel = GET_BITFIELD(m->status, 0, 3);
1424 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1425 long channel_mask, first_channel;
1426 u8 rank, socket;
1427 int csrow, rc, dimm;
1428 char *area_type = "Unknown";
1429
1430 if (ripv)
1431 type = "NON_FATAL";
1432 else
1433 type = "FATAL";
1434
1435 /*
1436 * According with Table 15-9 of the Intel Archictecture spec vol 3A,
1437 * memory errors should fit in this mask:
1438 * 000f 0000 1mmm cccc (binary)
1439 * where:
1440 * f = Correction Report Filtering Bit. If 1, subsequent errors
1441 * won't be shown
1442 * mmm = error type
1443 * cccc = channel
1444 * If the mask doesn't match, report an error to the parsing logic
1445 */
1446 if (! ((errcode & 0xef80) == 0x80)) {
1447 optype = "Can't parse: it is not a mem";
1448 } else {
1449 switch (optypenum) {
1450 case 0:
1451 optype = "generic undef request";
1452 break;
1453 case 1:
1454 optype = "memory read";
1455 break;
1456 case 2:
1457 optype = "memory write";
1458 break;
1459 case 3:
1460 optype = "addr/cmd";
1461 break;
1462 case 4:
1463 optype = "memory scrubbing";
1464 break;
1465 default:
1466 optype = "reserved";
1467 break;
1468 }
1469 }
1470
1471 rc = get_memory_error_data(mci, m->addr, &socket,
1472 &channel_mask, &rank, area_type);
1473 if (rc < 0)
1474 return;
1475 new_mci = get_mci_for_node_id(socket);
1476 if (!new_mci) {
1477 edac_mc_handle_ce_no_info(mci, "Error: socket got corrupted!");
1478 return;
1479 }
1480 mci = new_mci;
1481 pvt = mci->pvt_info;
1482
1483 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1484
1485 if (rank < 4)
1486 dimm = 0;
1487 else if (rank < 8)
1488 dimm = 1;
1489 else
1490 dimm = 2;
1491
1492 csrow = pvt->csrow_map[first_channel][dimm];
1493
1494 if (uncorrected_error && recoverable)
1495 recoverable_msg = " recoverable";
1496 else
1497 recoverable_msg = "";
1498
1499 /*
1500 * FIXME: What should we do with "channel" information on mcelog?
1501 * Probably, we can just discard it, as the channel information
1502 * comes from the get_memory_error_data() address decoding
1503 */
1504 msg = kasprintf(GFP_ATOMIC,
1505 "%d %s error(s): %s on %s area %s%s: cpu=%d Err=%04x:%04x (ch=%d), "
1506 "addr = 0x%08llx => socket=%d, Channel=%ld(mask=%ld), rank=%d\n",
1507 core_err_cnt,
1508 area_type,
1509 optype,
1510 type,
1511 recoverable_msg,
1512 overflow ? "OVERFLOW" : "",
1513 m->cpu,
1514 mscod, errcode,
1515 channel, /* 1111b means not specified */
1516 (long long) m->addr,
1517 socket,
1518 first_channel, /* This is the real channel on SB */
1519 channel_mask,
1520 rank);
1521
1522 debugf0("%s", msg);
1523
1524 /* Call the helper to output message */
1525 if (uncorrected_error)
1526 edac_mc_handle_fbd_ue(mci, csrow, 0, 0, msg);
1527 else
1528 edac_mc_handle_fbd_ce(mci, csrow, 0, msg);
1529
1530 kfree(msg);
1531}
1532
1533/*
1534 * sbridge_check_error Retrieve and process errors reported by the
1535 * hardware. Called by the Core module.
1536 */
1537static void sbridge_check_error(struct mem_ctl_info *mci)
1538{
1539 struct sbridge_pvt *pvt = mci->pvt_info;
1540 int i;
1541 unsigned count = 0;
1542 struct mce *m;
1543
1544 /*
1545 * MCE first step: Copy all mce errors into a temporary buffer
1546 * We use a double buffering here, to reduce the risk of
1547 * loosing an error.
1548 */
1549 smp_rmb();
1550 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1551 % MCE_LOG_LEN;
1552 if (!count)
1553 return;
1554
1555 m = pvt->mce_outentry;
1556 if (pvt->mce_in + count > MCE_LOG_LEN) {
1557 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1558
1559 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1560 smp_wmb();
1561 pvt->mce_in = 0;
1562 count -= l;
1563 m += l;
1564 }
1565 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1566 smp_wmb();
1567 pvt->mce_in += count;
1568
1569 smp_rmb();
1570 if (pvt->mce_overrun) {
1571 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
1572 pvt->mce_overrun);
1573 smp_wmb();
1574 pvt->mce_overrun = 0;
1575 }
1576
1577 /*
1578 * MCE second step: parse errors and display
1579 */
1580 for (i = 0; i < count; i++)
1581 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
1582}
1583
1584/*
1585 * sbridge_mce_check_error Replicates mcelog routine to get errors
1586 * This routine simply queues mcelog errors, and
1587 * return. The error itself should be handled later
1588 * by sbridge_check_error.
1589 * WARNING: As this routine should be called at NMI time, extra care should
1590 * be taken to avoid deadlocks, and to be as fast as possible.
1591 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001592static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
1593 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001594{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001595 struct mce *mce = (struct mce *)data;
1596 struct mem_ctl_info *mci;
1597 struct sbridge_pvt *pvt;
1598
1599 mci = get_mci_for_node_id(mce->socketid);
1600 if (!mci)
1601 return NOTIFY_BAD;
1602 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001603
1604 /*
1605 * Just let mcelog handle it if the error is
1606 * outside the memory controller. A memory error
1607 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1608 * bit 12 has an special meaning.
1609 */
1610 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001611 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001612
1613 printk("sbridge: HANDLING MCE MEMORY ERROR\n");
1614
1615 printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
1616 mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
1617 printk("TSC %llx ", mce->tsc);
1618 printk("ADDR %llx ", mce->addr);
1619 printk("MISC %llx ", mce->misc);
1620
1621 printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
1622 mce->cpuvendor, mce->cpuid, mce->time,
1623 mce->socketid, mce->apicid);
1624
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001625 /* Only handle if it is the right mc controller */
1626 if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001627 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001628
1629 smp_rmb();
1630 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1631 smp_wmb();
1632 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001633 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001634 }
1635
1636 /* Copy memory error at the ringbuffer */
1637 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1638 smp_wmb();
1639 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1640
1641 /* Handle fatal errors immediately */
1642 if (mce->mcgstatus & 1)
1643 sbridge_check_error(mci);
1644
1645 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001646 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001647}
1648
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02001649static struct notifier_block sbridge_mce_dec = {
1650 .notifier_call = sbridge_mce_check_error,
1651};
1652
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001653/****************************************************************************
1654 EDAC register/unregister logic
1655 ****************************************************************************/
1656
1657static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
1658{
1659 struct mem_ctl_info *mci = sbridge_dev->mci;
1660 struct sbridge_pvt *pvt;
1661
1662 if (unlikely(!mci || !mci->pvt_info)) {
1663 debugf0("MC: " __FILE__ ": %s(): dev = %p\n",
1664 __func__, &sbridge_dev->pdev[0]->dev);
1665
1666 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
1667 return;
1668 }
1669
1670 pvt = mci->pvt_info;
1671
1672 debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
1673 __func__, mci, &sbridge_dev->pdev[0]->dev);
1674
Borislav Petkov3653ada2011-12-04 15:12:09 +01001675 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001676
1677 /* Remove MC sysfs nodes */
1678 edac_mc_del_mc(mci->dev);
1679
1680 debugf1("%s: free mci struct\n", mci->ctl_name);
1681 kfree(mci->ctl_name);
1682 edac_mc_free(mci);
1683 sbridge_dev->mci = NULL;
1684}
1685
1686static int sbridge_register_mci(struct sbridge_dev *sbridge_dev)
1687{
1688 struct mem_ctl_info *mci;
1689 struct sbridge_pvt *pvt;
1690 int rc, channels, csrows;
1691
1692 /* Check the number of active and not disabled channels */
1693 rc = sbridge_get_active_channels(sbridge_dev->bus, &channels, &csrows);
1694 if (unlikely(rc < 0))
1695 return rc;
1696
1697 /* allocate a new MC control structure */
1698 mci = edac_mc_alloc(sizeof(*pvt), csrows, channels, sbridge_dev->mc);
1699 if (unlikely(!mci))
1700 return -ENOMEM;
1701
1702 debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
1703 __func__, mci, &sbridge_dev->pdev[0]->dev);
1704
1705 pvt = mci->pvt_info;
1706 memset(pvt, 0, sizeof(*pvt));
1707
1708 /* Associate sbridge_dev and mci for future usage */
1709 pvt->sbridge_dev = sbridge_dev;
1710 sbridge_dev->mci = mci;
1711
1712 mci->mtype_cap = MEM_FLAG_DDR3;
1713 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1714 mci->edac_cap = EDAC_FLAG_NONE;
1715 mci->mod_name = "sbridge_edac.c";
1716 mci->mod_ver = SBRIDGE_REVISION;
1717 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
1718 mci->dev_name = pci_name(sbridge_dev->pdev[0]);
1719 mci->ctl_page_to_phys = NULL;
1720
1721 /* Set the function pointer to an actual operation function */
1722 mci->edac_check = sbridge_check_error;
1723
1724 /* Store pci devices at mci for faster access */
1725 rc = mci_bind_devs(mci, sbridge_dev);
1726 if (unlikely(rc < 0))
1727 goto fail0;
1728
1729 /* Get dimm basic config and the memory layout */
1730 get_dimm_config(mci);
1731 get_memory_layout(mci);
1732
1733 /* record ptr to the generic device */
1734 mci->dev = &sbridge_dev->pdev[0]->dev;
1735
1736 /* add this new MC control structure to EDAC's list of MCs */
1737 if (unlikely(edac_mc_add_mc(mci))) {
1738 debugf0("MC: " __FILE__
1739 ": %s(): failed edac_mc_add_mc()\n", __func__);
1740 rc = -EINVAL;
1741 goto fail0;
1742 }
1743
Borislav Petkov3653ada2011-12-04 15:12:09 +01001744 mce_register_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001745 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001746
1747fail0:
1748 kfree(mci->ctl_name);
1749 edac_mc_free(mci);
1750 sbridge_dev->mci = NULL;
1751 return rc;
1752}
1753
1754/*
1755 * sbridge_probe Probe for ONE instance of device to see if it is
1756 * present.
1757 * return:
1758 * 0 for FOUND a device
1759 * < 0 for error code
1760 */
1761
1762static int __devinit sbridge_probe(struct pci_dev *pdev,
1763 const struct pci_device_id *id)
1764{
1765 int rc;
1766 u8 mc, num_mc = 0;
1767 struct sbridge_dev *sbridge_dev;
1768
1769 /* get the pci devices we want to reserve for our use */
1770 mutex_lock(&sbridge_edac_lock);
1771
1772 /*
1773 * All memory controllers are allocated at the first pass.
1774 */
1775 if (unlikely(probed >= 1)) {
1776 mutex_unlock(&sbridge_edac_lock);
1777 return -ENODEV;
1778 }
1779 probed++;
1780
1781 rc = sbridge_get_all_devices(&num_mc);
1782 if (unlikely(rc < 0))
1783 goto fail0;
1784 mc = 0;
1785
1786 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1787 debugf0("Registering MC#%d (%d of %d)\n", mc, mc + 1, num_mc);
1788 sbridge_dev->mc = mc++;
1789 rc = sbridge_register_mci(sbridge_dev);
1790 if (unlikely(rc < 0))
1791 goto fail1;
1792 }
1793
1794 sbridge_printk(KERN_INFO, "Driver loaded.\n");
1795
1796 mutex_unlock(&sbridge_edac_lock);
1797 return 0;
1798
1799fail1:
1800 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
1801 sbridge_unregister_mci(sbridge_dev);
1802
1803 sbridge_put_all_devices();
1804fail0:
1805 mutex_unlock(&sbridge_edac_lock);
1806 return rc;
1807}
1808
1809/*
1810 * sbridge_remove destructor for one instance of device
1811 *
1812 */
1813static void __devexit sbridge_remove(struct pci_dev *pdev)
1814{
1815 struct sbridge_dev *sbridge_dev;
1816
1817 debugf0(__FILE__ ": %s()\n", __func__);
1818
1819 /*
1820 * we have a trouble here: pdev value for removal will be wrong, since
1821 * it will point to the X58 register used to detect that the machine
1822 * is a Nehalem or upper design. However, due to the way several PCI
1823 * devices are grouped together to provide MC functionality, we need
1824 * to use a different method for releasing the devices
1825 */
1826
1827 mutex_lock(&sbridge_edac_lock);
1828
1829 if (unlikely(!probed)) {
1830 mutex_unlock(&sbridge_edac_lock);
1831 return;
1832 }
1833
1834 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
1835 sbridge_unregister_mci(sbridge_dev);
1836
1837 /* Release PCI resources */
1838 sbridge_put_all_devices();
1839
1840 probed--;
1841
1842 mutex_unlock(&sbridge_edac_lock);
1843}
1844
1845MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
1846
1847/*
1848 * sbridge_driver pci_driver structure for this module
1849 *
1850 */
1851static struct pci_driver sbridge_driver = {
1852 .name = "sbridge_edac",
1853 .probe = sbridge_probe,
1854 .remove = __devexit_p(sbridge_remove),
1855 .id_table = sbridge_pci_tbl,
1856};
1857
1858/*
1859 * sbridge_init Module entry function
1860 * Try to initialize this module for its devices
1861 */
1862static int __init sbridge_init(void)
1863{
1864 int pci_rc;
1865
1866 debugf2("MC: " __FILE__ ": %s()\n", __func__);
1867
1868 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
1869 opstate_init();
1870
1871 pci_rc = pci_register_driver(&sbridge_driver);
1872
1873 if (pci_rc >= 0)
1874 return 0;
1875
1876 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
1877 pci_rc);
1878
1879 return pci_rc;
1880}
1881
1882/*
1883 * sbridge_exit() Module exit function
1884 * Unregister the driver
1885 */
1886static void __exit sbridge_exit(void)
1887{
1888 debugf2("MC: " __FILE__ ": %s()\n", __func__);
1889 pci_unregister_driver(&sbridge_driver);
1890}
1891
1892module_init(sbridge_init);
1893module_exit(sbridge_exit);
1894
1895module_param(edac_op_state, int, 0444);
1896MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
1897
1898MODULE_LICENSE("GPL");
1899MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1900MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
1901MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "
1902 SBRIDGE_REVISION);