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