1 /* Intel i7 core/Nehalem Memory Controller kernel module
3 * This driver supports yhe memory controllers found on the Intel
4 * processor families i7core, i7core 7xx/8xx, i5core, Xeon 35xx,
5 * Xeon 55xx and Xeon 56xx also known as Nehalem, Nehalem-EP, Lynnfield
8 * This file may be distributed under the terms of the
9 * GNU General Public License version 2 only.
11 * Copyright (c) 2009-2010 by:
12 * Mauro Carvalho Chehab <mchehab@redhat.com>
14 * Red Hat Inc. http://www.redhat.com
16 * Forked and adapted from the i5400_edac driver
18 * Based on the following public Intel datasheets:
19 * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
20 * Datasheet, Volume 2:
21 * http://download.intel.com/design/processor/datashts/320835.pdf
22 * Intel Xeon Processor 5500 Series Datasheet Volume 2
23 * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
25 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/pci_ids.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/edac.h>
35 #include <linux/mmzone.h>
36 #include <linux/edac_mce.h>
37 #include <linux/smp.h>
38 #include <asm/processor.h>
40 #include "edac_core.h"
43 static LIST_HEAD(i7core_edac_list);
44 static DEFINE_MUTEX(i7core_edac_lock);
48 * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
49 * registers start at bus 255, and are not reported by BIOS.
50 * We currently find devices with only 2 sockets. In order to support more QPI
51 * Quick Path Interconnect, just increment this number.
53 #define MAX_SOCKET_BUSES 2
57 * Alter this version for the module when modifications are made
59 #define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
60 #define EDAC_MOD_STR "i7core_edac"
65 #define i7core_printk(level, fmt, arg...) \
66 edac_printk(level, "i7core", fmt, ##arg)
68 #define i7core_mc_printk(mci, level, fmt, arg...) \
69 edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
72 * i7core Memory Controller Registers
75 /* OFFSETS for Device 0 Function 0 */
77 #define MC_CFG_CONTROL 0x90
79 /* OFFSETS for Device 3 Function 0 */
81 #define MC_CONTROL 0x48
82 #define MC_STATUS 0x4c
83 #define MC_MAX_DOD 0x64
86 * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
87 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
90 #define MC_TEST_ERR_RCV1 0x60
91 #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
93 #define MC_TEST_ERR_RCV0 0x64
94 #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
95 #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
97 /* OFFSETS for Device 3 Function 2, as inicated on Xeon 5500 datasheet */
98 #define MC_COR_ECC_CNT_0 0x80
99 #define MC_COR_ECC_CNT_1 0x84
100 #define MC_COR_ECC_CNT_2 0x88
101 #define MC_COR_ECC_CNT_3 0x8c
102 #define MC_COR_ECC_CNT_4 0x90
103 #define MC_COR_ECC_CNT_5 0x94
105 #define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
106 #define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
109 /* OFFSETS for Devices 4,5 and 6 Function 0 */
111 #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
112 #define THREE_DIMMS_PRESENT (1 << 24)
113 #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
114 #define QUAD_RANK_PRESENT (1 << 22)
115 #define REGISTERED_DIMM (1 << 15)
117 #define MC_CHANNEL_MAPPER 0x60
118 #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
119 #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
121 #define MC_CHANNEL_RANK_PRESENT 0x7c
122 #define RANK_PRESENT_MASK 0xffff
124 #define MC_CHANNEL_ADDR_MATCH 0xf0
125 #define MC_CHANNEL_ERROR_MASK 0xf8
126 #define MC_CHANNEL_ERROR_INJECT 0xfc
127 #define INJECT_ADDR_PARITY 0x10
128 #define INJECT_ECC 0x08
129 #define MASK_CACHELINE 0x06
130 #define MASK_FULL_CACHELINE 0x06
131 #define MASK_MSB32_CACHELINE 0x04
132 #define MASK_LSB32_CACHELINE 0x02
133 #define NO_MASK_CACHELINE 0x00
134 #define REPEAT_EN 0x01
136 /* OFFSETS for Devices 4,5 and 6 Function 1 */
138 #define MC_DOD_CH_DIMM0 0x48
139 #define MC_DOD_CH_DIMM1 0x4c
140 #define MC_DOD_CH_DIMM2 0x50
141 #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
142 #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
143 #define DIMM_PRESENT_MASK (1 << 9)
144 #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
145 #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
146 #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
147 #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
148 #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
149 #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
150 #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
151 #define MC_DOD_NUMCOL_MASK 3
152 #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
154 #define MC_RANK_PRESENT 0x7c
156 #define MC_SAG_CH_0 0x80
157 #define MC_SAG_CH_1 0x84
158 #define MC_SAG_CH_2 0x88
159 #define MC_SAG_CH_3 0x8c
160 #define MC_SAG_CH_4 0x90
161 #define MC_SAG_CH_5 0x94
162 #define MC_SAG_CH_6 0x98
163 #define MC_SAG_CH_7 0x9c
165 #define MC_RIR_LIMIT_CH_0 0x40
166 #define MC_RIR_LIMIT_CH_1 0x44
167 #define MC_RIR_LIMIT_CH_2 0x48
168 #define MC_RIR_LIMIT_CH_3 0x4C
169 #define MC_RIR_LIMIT_CH_4 0x50
170 #define MC_RIR_LIMIT_CH_5 0x54
171 #define MC_RIR_LIMIT_CH_6 0x58
172 #define MC_RIR_LIMIT_CH_7 0x5C
173 #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
175 #define MC_RIR_WAY_CH 0x80
176 #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
177 #define MC_RIR_WAY_RANK_MASK 0x7
184 #define MAX_DIMMS 3 /* Max DIMMS per channel */
185 #define MAX_MCR_FUNC 4
186 #define MAX_CHAN_FUNC 3
196 struct i7core_inject {
203 /* Error address mask */
204 int channel, dimm, rank, bank, page, col;
207 struct i7core_channel {
212 struct pci_id_descr {
219 struct pci_id_table {
220 const struct pci_id_descr *descr;
225 struct list_head list;
227 struct pci_dev **pdev;
229 struct mem_ctl_info *mci;
233 struct pci_dev *pci_noncore;
234 struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
235 struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
237 struct i7core_dev *i7core_dev;
239 struct i7core_info info;
240 struct i7core_inject inject;
241 struct i7core_channel channel[NUM_CHANS];
243 int channels; /* Number of active channels */
245 int ce_count_available;
246 int csrow_map[NUM_CHANS][MAX_DIMMS];
248 /* ECC corrected errors counts per udimm */
249 unsigned long udimm_ce_count[MAX_DIMMS];
250 int udimm_last_ce_count[MAX_DIMMS];
251 /* ECC corrected errors counts per rdimm */
252 unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
253 int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
255 unsigned int is_registered;
258 struct edac_mce edac_mce;
260 /* Fifo double buffers */
261 struct mce mce_entry[MCE_LOG_LEN];
262 struct mce mce_outentry[MCE_LOG_LEN];
264 /* Fifo in/out counters */
265 unsigned mce_in, mce_out;
267 /* Count indicator to show errors not got */
268 unsigned mce_overrun;
270 /* Struct to control EDAC polling */
271 struct edac_pci_ctl_info *i7core_pci;
274 #define PCI_DESCR(device, function, device_id) \
276 .func = (function), \
277 .dev_id = (device_id)
279 static const struct pci_id_descr pci_dev_descr_i7core_nehalem[] = {
280 /* Memory controller */
281 { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
282 { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
283 /* Exists only for RDIMM */
284 { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
285 { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
288 { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
289 { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
290 { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
291 { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
294 { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
295 { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
296 { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
297 { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
300 { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
301 { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
302 { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
303 { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
305 /* Generic Non-core registers */
307 * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
308 * On Xeon 55xx, however, it has a different id (8086:2c40). So,
309 * the probing code needs to test for the other address in case of
310 * failure of this one
312 { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NONCORE) },
316 static const struct pci_id_descr pci_dev_descr_lynnfield[] = {
317 { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
318 { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
319 { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
321 { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
322 { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
323 { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
324 { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
326 { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
327 { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
328 { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
329 { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
332 * This is the PCI device has an alternate address on some
333 * processors like Core i7 860
335 { PCI_DESCR( 0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE) },
338 static const struct pci_id_descr pci_dev_descr_i7core_westmere[] = {
339 /* Memory controller */
340 { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR_REV2) },
341 { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD_REV2) },
342 /* Exists only for RDIMM */
343 { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_RAS_REV2), .optional = 1 },
344 { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST_REV2) },
347 { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL_REV2) },
348 { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR_REV2) },
349 { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK_REV2) },
350 { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC_REV2) },
353 { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL_REV2) },
354 { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR_REV2) },
355 { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK_REV2) },
356 { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC_REV2) },
359 { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_CTRL_REV2) },
360 { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_ADDR_REV2) },
361 { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_RANK_REV2) },
362 { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_TC_REV2) },
364 /* Generic Non-core registers */
365 { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2) },
369 #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
370 static const struct pci_id_table pci_dev_table[] = {
371 PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_nehalem),
372 PCI_ID_TABLE_ENTRY(pci_dev_descr_lynnfield),
373 PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_westmere),
377 * pci_device_id table for which devices we are looking for
379 static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
380 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
381 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
382 {0,} /* 0 terminated list. */
385 /****************************************************************************
386 Anciliary status routines
387 ****************************************************************************/
389 /* MC_CONTROL bits */
390 #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
391 #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
394 #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
395 #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
397 /* MC_MAX_DOD read functions */
398 static inline int numdimms(u32 dimms)
400 return (dimms & 0x3) + 1;
403 static inline int numrank(u32 rank)
405 static int ranks[4] = { 1, 2, 4, -EINVAL };
407 return ranks[rank & 0x3];
410 static inline int numbank(u32 bank)
412 static int banks[4] = { 4, 8, 16, -EINVAL };
414 return banks[bank & 0x3];
417 static inline int numrow(u32 row)
419 static int rows[8] = {
420 1 << 12, 1 << 13, 1 << 14, 1 << 15,
421 1 << 16, -EINVAL, -EINVAL, -EINVAL,
424 return rows[row & 0x7];
427 static inline int numcol(u32 col)
429 static int cols[8] = {
430 1 << 10, 1 << 11, 1 << 12, -EINVAL,
432 return cols[col & 0x3];
435 static struct i7core_dev *get_i7core_dev(u8 socket)
437 struct i7core_dev *i7core_dev;
439 list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
440 if (i7core_dev->socket == socket)
447 /****************************************************************************
448 Memory check routines
449 ****************************************************************************/
450 static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
453 struct i7core_dev *i7core_dev = get_i7core_dev(socket);
459 for (i = 0; i < i7core_dev->n_devs; i++) {
460 if (!i7core_dev->pdev[i])
463 if (PCI_SLOT(i7core_dev->pdev[i]->devfn) == slot &&
464 PCI_FUNC(i7core_dev->pdev[i]->devfn) == func) {
465 return i7core_dev->pdev[i];
473 * i7core_get_active_channels() - gets the number of channels and csrows
474 * @socket: Quick Path Interconnect socket
475 * @channels: Number of channels that will be returned
476 * @csrows: Number of csrows found
478 * Since EDAC core needs to know in advance the number of available channels
479 * and csrows, in order to allocate memory for csrows/channels, it is needed
480 * to run two similar steps. At the first step, implemented on this function,
481 * it checks the number of csrows/channels present at one socket.
482 * this is used in order to properly allocate the size of mci components.
484 * It should be noticed that none of the current available datasheets explain
485 * or even mention how csrows are seen by the memory controller. So, we need
486 * to add a fake description for csrows.
487 * So, this driver is attributing one DIMM memory for one csrow.
489 static int i7core_get_active_channels(const u8 socket, unsigned *channels,
492 struct pci_dev *pdev = NULL;
499 pdev = get_pdev_slot_func(socket, 3, 0);
501 i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
506 /* Device 3 function 0 reads */
507 pci_read_config_dword(pdev, MC_STATUS, &status);
508 pci_read_config_dword(pdev, MC_CONTROL, &control);
510 for (i = 0; i < NUM_CHANS; i++) {
512 /* Check if the channel is active */
513 if (!(control & (1 << (8 + i))))
516 /* Check if the channel is disabled */
517 if (status & (1 << i))
520 pdev = get_pdev_slot_func(socket, i + 4, 1);
522 i7core_printk(KERN_ERR, "Couldn't find socket %d "
527 /* Devices 4-6 function 1 */
528 pci_read_config_dword(pdev,
529 MC_DOD_CH_DIMM0, &dimm_dod[0]);
530 pci_read_config_dword(pdev,
531 MC_DOD_CH_DIMM1, &dimm_dod[1]);
532 pci_read_config_dword(pdev,
533 MC_DOD_CH_DIMM2, &dimm_dod[2]);
537 for (j = 0; j < 3; j++) {
538 if (!DIMM_PRESENT(dimm_dod[j]))
544 debugf0("Number of active channels on socket %d: %d\n",
550 static int get_dimm_config(const struct mem_ctl_info *mci, int *csrow)
552 struct i7core_pvt *pvt = mci->pvt_info;
553 struct csrow_info *csr;
554 struct pci_dev *pdev;
556 unsigned long last_page = 0;
560 /* Get data from the MC register, function 0 */
561 pdev = pvt->pci_mcr[0];
565 /* Device 3 function 0 reads */
566 pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
567 pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
568 pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
569 pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
571 debugf0("QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
572 pvt->i7core_dev->socket, pvt->info.mc_control, pvt->info.mc_status,
573 pvt->info.max_dod, pvt->info.ch_map);
575 if (ECC_ENABLED(pvt)) {
576 debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
578 mode = EDAC_S8ECD8ED;
580 mode = EDAC_S4ECD4ED;
582 debugf0("ECC disabled\n");
586 /* FIXME: need to handle the error codes */
587 debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked "
589 numdimms(pvt->info.max_dod),
590 numrank(pvt->info.max_dod >> 2),
591 numbank(pvt->info.max_dod >> 4),
592 numrow(pvt->info.max_dod >> 6),
593 numcol(pvt->info.max_dod >> 9));
595 for (i = 0; i < NUM_CHANS; i++) {
596 u32 data, dimm_dod[3], value[8];
598 if (!pvt->pci_ch[i][0])
601 if (!CH_ACTIVE(pvt, i)) {
602 debugf0("Channel %i is not active\n", i);
605 if (CH_DISABLED(pvt, i)) {
606 debugf0("Channel %i is disabled\n", i);
610 /* Devices 4-6 function 0 */
611 pci_read_config_dword(pvt->pci_ch[i][0],
612 MC_CHANNEL_DIMM_INIT_PARAMS, &data);
614 pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ?
617 if (data & REGISTERED_DIMM)
622 if (data & THREE_DIMMS_PRESENT)
623 pvt->channel[i].dimms = 3;
624 else if (data & SINGLE_QUAD_RANK_PRESENT)
625 pvt->channel[i].dimms = 1;
627 pvt->channel[i].dimms = 2;
630 /* Devices 4-6 function 1 */
631 pci_read_config_dword(pvt->pci_ch[i][1],
632 MC_DOD_CH_DIMM0, &dimm_dod[0]);
633 pci_read_config_dword(pvt->pci_ch[i][1],
634 MC_DOD_CH_DIMM1, &dimm_dod[1]);
635 pci_read_config_dword(pvt->pci_ch[i][1],
636 MC_DOD_CH_DIMM2, &dimm_dod[2]);
638 debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
639 "%d ranks, %cDIMMs\n",
641 RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
643 pvt->channel[i].ranks,
644 (data & REGISTERED_DIMM) ? 'R' : 'U');
646 for (j = 0; j < 3; j++) {
647 u32 banks, ranks, rows, cols;
650 if (!DIMM_PRESENT(dimm_dod[j]))
653 banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
654 ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
655 rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
656 cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
658 /* DDR3 has 8 I/O banks */
659 size = (rows * cols * banks * ranks) >> (20 - 3);
661 pvt->channel[i].dimms++;
663 debugf0("\tdimm %d %d Mb offset: %x, "
664 "bank: %d, rank: %d, row: %#x, col: %#x\n",
666 RANKOFFSET(dimm_dod[j]),
667 banks, ranks, rows, cols);
670 npages = size >> (PAGE_SHIFT - 20);
672 npages = size << (20 - PAGE_SHIFT);
675 csr = &mci->csrows[*csrow];
676 csr->first_page = last_page + 1;
678 csr->last_page = last_page;
679 csr->nr_pages = npages;
683 csr->csrow_idx = *csrow;
684 csr->nr_channels = 1;
686 csr->channels[0].chan_idx = i;
687 csr->channels[0].ce_count = 0;
689 pvt->csrow_map[i][j] = *csrow;
699 csr->dtype = DEV_X16;
702 csr->dtype = DEV_UNKNOWN;
705 csr->edac_mode = mode;
711 pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
712 pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
713 pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
714 pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
715 pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
716 pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
717 pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
718 pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
719 debugf1("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
720 for (j = 0; j < 8; j++)
721 debugf1("\t\t%#x\t%#x\t%#x\n",
722 (value[j] >> 27) & 0x1,
723 (value[j] >> 24) & 0x7,
724 (value[j] && ((1 << 24) - 1)));
730 /****************************************************************************
731 Error insertion routines
732 ****************************************************************************/
734 /* The i7core has independent error injection features per channel.
735 However, to have a simpler code, we don't allow enabling error injection
736 on more than one channel.
737 Also, since a change at an inject parameter will be applied only at enable,
738 we're disabling error injection on all write calls to the sysfs nodes that
739 controls the error code injection.
741 static int disable_inject(const struct mem_ctl_info *mci)
743 struct i7core_pvt *pvt = mci->pvt_info;
745 pvt->inject.enable = 0;
747 if (!pvt->pci_ch[pvt->inject.channel][0])
750 pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
751 MC_CHANNEL_ERROR_INJECT, 0);
757 * i7core inject inject.section
759 * accept and store error injection inject.section value
760 * bit 0 - refers to the lower 32-byte half cacheline
761 * bit 1 - refers to the upper 32-byte half cacheline
763 static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
764 const char *data, size_t count)
766 struct i7core_pvt *pvt = mci->pvt_info;
770 if (pvt->inject.enable)
773 rc = strict_strtoul(data, 10, &value);
774 if ((rc < 0) || (value > 3))
777 pvt->inject.section = (u32) value;
781 static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
784 struct i7core_pvt *pvt = mci->pvt_info;
785 return sprintf(data, "0x%08x\n", pvt->inject.section);
791 * accept and store error injection inject.section value
792 * bit 0 - repeat enable - Enable error repetition
793 * bit 1 - inject ECC error
794 * bit 2 - inject parity error
796 static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
797 const char *data, size_t count)
799 struct i7core_pvt *pvt = mci->pvt_info;
803 if (pvt->inject.enable)
806 rc = strict_strtoul(data, 10, &value);
807 if ((rc < 0) || (value > 7))
810 pvt->inject.type = (u32) value;
814 static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
817 struct i7core_pvt *pvt = mci->pvt_info;
818 return sprintf(data, "0x%08x\n", pvt->inject.type);
822 * i7core_inject_inject.eccmask_store
824 * The type of error (UE/CE) will depend on the inject.eccmask value:
825 * Any bits set to a 1 will flip the corresponding ECC bit
826 * Correctable errors can be injected by flipping 1 bit or the bits within
827 * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
828 * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
829 * uncorrectable error to be injected.
831 static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
832 const char *data, size_t count)
834 struct i7core_pvt *pvt = mci->pvt_info;
838 if (pvt->inject.enable)
841 rc = strict_strtoul(data, 10, &value);
845 pvt->inject.eccmask = (u32) value;
849 static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
852 struct i7core_pvt *pvt = mci->pvt_info;
853 return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
859 * The type of error (UE/CE) will depend on the inject.eccmask value:
860 * Any bits set to a 1 will flip the corresponding ECC bit
861 * Correctable errors can be injected by flipping 1 bit or the bits within
862 * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
863 * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
864 * uncorrectable error to be injected.
867 #define DECLARE_ADDR_MATCH(param, limit) \
868 static ssize_t i7core_inject_store_##param( \
869 struct mem_ctl_info *mci, \
870 const char *data, size_t count) \
872 struct i7core_pvt *pvt; \
876 debugf1("%s()\n", __func__); \
877 pvt = mci->pvt_info; \
879 if (pvt->inject.enable) \
880 disable_inject(mci); \
882 if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
885 rc = strict_strtoul(data, 10, &value); \
886 if ((rc < 0) || (value >= limit)) \
890 pvt->inject.param = value; \
895 static ssize_t i7core_inject_show_##param( \
896 struct mem_ctl_info *mci, \
899 struct i7core_pvt *pvt; \
901 pvt = mci->pvt_info; \
902 debugf1("%s() pvt=%p\n", __func__, pvt); \
903 if (pvt->inject.param < 0) \
904 return sprintf(data, "any\n"); \
906 return sprintf(data, "%d\n", pvt->inject.param);\
909 #define ATTR_ADDR_MATCH(param) \
913 .mode = (S_IRUGO | S_IWUSR) \
915 .show = i7core_inject_show_##param, \
916 .store = i7core_inject_store_##param, \
919 DECLARE_ADDR_MATCH(channel, 3);
920 DECLARE_ADDR_MATCH(dimm, 3);
921 DECLARE_ADDR_MATCH(rank, 4);
922 DECLARE_ADDR_MATCH(bank, 32);
923 DECLARE_ADDR_MATCH(page, 0x10000);
924 DECLARE_ADDR_MATCH(col, 0x4000);
926 static int write_and_test(struct pci_dev *dev, const int where, const u32 val)
931 debugf0("setting pci %02x:%02x.%x reg=%02x value=%08x\n",
932 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
935 for (count = 0; count < 10; count++) {
938 pci_write_config_dword(dev, where, val);
939 pci_read_config_dword(dev, where, &read);
945 i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
946 "write=%08x. Read=%08x\n",
947 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
954 * This routine prepares the Memory Controller for error injection.
955 * The error will be injected when some process tries to write to the
956 * memory that matches the given criteria.
957 * The criteria can be set in terms of a mask where dimm, rank, bank, page
958 * and col can be specified.
959 * A -1 value for any of the mask items will make the MCU to ignore
960 * that matching criteria for error injection.
962 * It should be noticed that the error will only happen after a write operation
963 * on a memory that matches the condition. if REPEAT_EN is not enabled at
964 * inject mask, then it will produce just one error. Otherwise, it will repeat
965 * until the injectmask would be cleaned.
967 * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
968 * is reliable enough to check if the MC is using the
969 * three channels. However, this is not clear at the datasheet.
971 static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
972 const char *data, size_t count)
974 struct i7core_pvt *pvt = mci->pvt_info;
980 if (!pvt->pci_ch[pvt->inject.channel][0])
983 rc = strict_strtoul(data, 10, &enable);
988 pvt->inject.enable = 1;
994 /* Sets pvt->inject.dimm mask */
995 if (pvt->inject.dimm < 0)
998 if (pvt->channel[pvt->inject.channel].dimms > 2)
999 mask |= (pvt->inject.dimm & 0x3LL) << 35;
1001 mask |= (pvt->inject.dimm & 0x1LL) << 36;
1004 /* Sets pvt->inject.rank mask */
1005 if (pvt->inject.rank < 0)
1008 if (pvt->channel[pvt->inject.channel].dimms > 2)
1009 mask |= (pvt->inject.rank & 0x1LL) << 34;
1011 mask |= (pvt->inject.rank & 0x3LL) << 34;
1014 /* Sets pvt->inject.bank mask */
1015 if (pvt->inject.bank < 0)
1018 mask |= (pvt->inject.bank & 0x15LL) << 30;
1020 /* Sets pvt->inject.page mask */
1021 if (pvt->inject.page < 0)
1024 mask |= (pvt->inject.page & 0xffff) << 14;
1026 /* Sets pvt->inject.column mask */
1027 if (pvt->inject.col < 0)
1030 mask |= (pvt->inject.col & 0x3fff);
1034 * bits 1-2: MASK_HALF_CACHELINE
1036 * bit 4: INJECT_ADDR_PARITY
1039 injectmask = (pvt->inject.type & 1) |
1040 (pvt->inject.section & 0x3) << 1 |
1041 (pvt->inject.type & 0x6) << (3 - 1);
1043 /* Unlock writes to registers - this register is write only */
1044 pci_write_config_dword(pvt->pci_noncore,
1045 MC_CFG_CONTROL, 0x2);
1047 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
1048 MC_CHANNEL_ADDR_MATCH, mask);
1049 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
1050 MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
1052 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
1053 MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
1055 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
1056 MC_CHANNEL_ERROR_INJECT, injectmask);
1059 * This is something undocumented, based on my tests
1060 * Without writing 8 to this register, errors aren't injected. Not sure
1063 pci_write_config_dword(pvt->pci_noncore,
1066 debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
1068 mask, pvt->inject.eccmask, injectmask);
1074 static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
1077 struct i7core_pvt *pvt = mci->pvt_info;
1080 if (!pvt->pci_ch[pvt->inject.channel][0])
1083 pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
1084 MC_CHANNEL_ERROR_INJECT, &injectmask);
1086 debugf0("Inject error read: 0x%018x\n", injectmask);
1088 if (injectmask & 0x0c)
1089 pvt->inject.enable = 1;
1091 return sprintf(data, "%d\n", pvt->inject.enable);
1094 #define DECLARE_COUNTER(param) \
1095 static ssize_t i7core_show_counter_##param( \
1096 struct mem_ctl_info *mci, \
1099 struct i7core_pvt *pvt = mci->pvt_info; \
1101 debugf1("%s() \n", __func__); \
1102 if (!pvt->ce_count_available || (pvt->is_registered)) \
1103 return sprintf(data, "data unavailable\n"); \
1104 return sprintf(data, "%lu\n", \
1105 pvt->udimm_ce_count[param]); \
1108 #define ATTR_COUNTER(param) \
1111 .name = __stringify(udimm##param), \
1112 .mode = (S_IRUGO | S_IWUSR) \
1114 .show = i7core_show_counter_##param \
1125 static const struct mcidev_sysfs_attribute i7core_addrmatch_attrs[] = {
1126 ATTR_ADDR_MATCH(channel),
1127 ATTR_ADDR_MATCH(dimm),
1128 ATTR_ADDR_MATCH(rank),
1129 ATTR_ADDR_MATCH(bank),
1130 ATTR_ADDR_MATCH(page),
1131 ATTR_ADDR_MATCH(col),
1132 { } /* End of list */
1135 static const struct mcidev_sysfs_group i7core_inject_addrmatch = {
1136 .name = "inject_addrmatch",
1137 .mcidev_attr = i7core_addrmatch_attrs,
1140 static const struct mcidev_sysfs_attribute i7core_udimm_counters_attrs[] = {
1144 { .attr = { .name = NULL } }
1147 static const struct mcidev_sysfs_group i7core_udimm_counters = {
1148 .name = "all_channel_counts",
1149 .mcidev_attr = i7core_udimm_counters_attrs,
1152 static const struct mcidev_sysfs_attribute i7core_sysfs_rdimm_attrs[] = {
1155 .name = "inject_section",
1156 .mode = (S_IRUGO | S_IWUSR)
1158 .show = i7core_inject_section_show,
1159 .store = i7core_inject_section_store,
1162 .name = "inject_type",
1163 .mode = (S_IRUGO | S_IWUSR)
1165 .show = i7core_inject_type_show,
1166 .store = i7core_inject_type_store,
1169 .name = "inject_eccmask",
1170 .mode = (S_IRUGO | S_IWUSR)
1172 .show = i7core_inject_eccmask_show,
1173 .store = i7core_inject_eccmask_store,
1175 .grp = &i7core_inject_addrmatch,
1178 .name = "inject_enable",
1179 .mode = (S_IRUGO | S_IWUSR)
1181 .show = i7core_inject_enable_show,
1182 .store = i7core_inject_enable_store,
1184 { } /* End of list */
1187 static const struct mcidev_sysfs_attribute i7core_sysfs_udimm_attrs[] = {
1190 .name = "inject_section",
1191 .mode = (S_IRUGO | S_IWUSR)
1193 .show = i7core_inject_section_show,
1194 .store = i7core_inject_section_store,
1197 .name = "inject_type",
1198 .mode = (S_IRUGO | S_IWUSR)
1200 .show = i7core_inject_type_show,
1201 .store = i7core_inject_type_store,
1204 .name = "inject_eccmask",
1205 .mode = (S_IRUGO | S_IWUSR)
1207 .show = i7core_inject_eccmask_show,
1208 .store = i7core_inject_eccmask_store,
1210 .grp = &i7core_inject_addrmatch,
1213 .name = "inject_enable",
1214 .mode = (S_IRUGO | S_IWUSR)
1216 .show = i7core_inject_enable_show,
1217 .store = i7core_inject_enable_store,
1219 .grp = &i7core_udimm_counters,
1221 { } /* End of list */
1224 /****************************************************************************
1225 Device initialization routines: put/get, init/exit
1226 ****************************************************************************/
1229 * i7core_put_devices 'put' all the devices that we have
1230 * reserved via 'get'
1232 static void i7core_put_devices(struct i7core_dev *i7core_dev)
1236 debugf0(__FILE__ ": %s()\n", __func__);
1237 for (i = 0; i < i7core_dev->n_devs; i++) {
1238 struct pci_dev *pdev = i7core_dev->pdev[i];
1241 debugf0("Removing dev %02x:%02x.%d\n",
1243 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1246 kfree(i7core_dev->pdev);
1247 list_del(&i7core_dev->list);
1251 static void i7core_put_all_devices(void)
1253 struct i7core_dev *i7core_dev, *tmp;
1255 list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list)
1256 i7core_put_devices(i7core_dev);
1259 static void __init i7core_xeon_pci_fixup(const struct pci_id_table *table)
1261 struct pci_dev *pdev = NULL;
1264 * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core pci buses
1265 * aren't announced by acpi. So, we need to use a legacy scan probing
1268 while (table && table->descr) {
1269 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, table->descr[0].dev_id, NULL);
1270 if (unlikely(!pdev)) {
1271 for (i = 0; i < MAX_SOCKET_BUSES; i++)
1272 pcibios_scan_specific_bus(255-i);
1279 static unsigned i7core_pci_lastbus(void)
1281 int last_bus = 0, bus;
1282 struct pci_bus *b = NULL;
1284 while ((b = pci_find_next_bus(b)) != NULL) {
1286 debugf0("Found bus %d\n", bus);
1291 debugf0("Last bus %d\n", last_bus);
1297 * i7core_get_devices Find and perform 'get' operation on the MCH's
1298 * device/functions we want to reference for this driver
1300 * Need to 'get' device 16 func 1 and func 2
1302 int i7core_get_onedevice(struct pci_dev **prev, const int devno,
1303 const struct pci_id_descr *dev_descr,
1304 const unsigned n_devs,
1305 const unsigned last_bus)
1307 struct i7core_dev *i7core_dev;
1309 struct pci_dev *pdev = NULL;
1313 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1314 dev_descr->dev_id, *prev);
1317 * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core regs
1318 * is at addr 8086:2c40, instead of 8086:2c41. So, we need
1319 * to probe for the alternate address in case of failure
1321 if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_I7_NONCORE && !pdev)
1322 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1323 PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT, *prev);
1325 if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE && !pdev)
1326 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1327 PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT,
1336 if (dev_descr->optional)
1342 i7core_printk(KERN_INFO,
1343 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1344 dev_descr->dev, dev_descr->func,
1345 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1347 /* End of list, leave */
1350 bus = pdev->bus->number;
1352 socket = last_bus - bus;
1354 i7core_dev = get_i7core_dev(socket);
1356 i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
1359 i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * n_devs,
1361 if (!i7core_dev->pdev) {
1365 i7core_dev->socket = socket;
1366 i7core_dev->n_devs = n_devs;
1367 list_add_tail(&i7core_dev->list, &i7core_edac_list);
1370 if (i7core_dev->pdev[devno]) {
1371 i7core_printk(KERN_ERR,
1372 "Duplicated device for "
1373 "dev %02x:%02x.%d PCI ID %04x:%04x\n",
1374 bus, dev_descr->dev, dev_descr->func,
1375 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1380 i7core_dev->pdev[devno] = pdev;
1383 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1384 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1385 i7core_printk(KERN_ERR,
1386 "Device PCI ID %04x:%04x "
1387 "has dev %02x:%02x.%d instead of dev %02x:%02x.%d\n",
1388 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1389 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1390 bus, dev_descr->dev, dev_descr->func);
1394 /* Be sure that the device is enabled */
1395 if (unlikely(pci_enable_device(pdev) < 0)) {
1396 i7core_printk(KERN_ERR,
1398 "dev %02x:%02x.%d PCI ID %04x:%04x\n",
1399 bus, dev_descr->dev, dev_descr->func,
1400 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1404 debugf0("Detected socket %d dev %02x:%02x.%d PCI ID %04x:%04x\n",
1405 socket, bus, dev_descr->dev,
1407 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1414 static int i7core_get_devices(const struct pci_id_table *table)
1416 int i, rc, last_bus;
1417 struct pci_dev *pdev = NULL;
1418 const struct pci_id_descr *dev_descr;
1420 last_bus = i7core_pci_lastbus();
1422 while (table && table->descr) {
1423 dev_descr = table->descr;
1424 for (i = 0; i < table->n_devs; i++) {
1427 rc = i7core_get_onedevice(&pdev, i,
1436 i7core_put_all_devices();
1448 static int mci_bind_devs(struct mem_ctl_info *mci,
1449 struct i7core_dev *i7core_dev)
1451 struct i7core_pvt *pvt = mci->pvt_info;
1452 struct pci_dev *pdev;
1455 /* Associates i7core_dev and mci for future usage */
1456 pvt->i7core_dev = i7core_dev;
1457 i7core_dev->mci = mci;
1459 pvt->is_registered = 0;
1460 for (i = 0; i < i7core_dev->n_devs; i++) {
1461 pdev = i7core_dev->pdev[i];
1465 func = PCI_FUNC(pdev->devfn);
1466 slot = PCI_SLOT(pdev->devfn);
1468 if (unlikely(func > MAX_MCR_FUNC))
1470 pvt->pci_mcr[func] = pdev;
1471 } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
1472 if (unlikely(func > MAX_CHAN_FUNC))
1474 pvt->pci_ch[slot - 4][func] = pdev;
1475 } else if (!slot && !func)
1476 pvt->pci_noncore = pdev;
1480 debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
1481 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1482 pdev, i7core_dev->socket);
1484 if (PCI_SLOT(pdev->devfn) == 3 &&
1485 PCI_FUNC(pdev->devfn) == 2)
1486 pvt->is_registered = 1;
1492 i7core_printk(KERN_ERR, "Device %d, function %d "
1493 "is out of the expected range\n",
1498 /****************************************************************************
1499 Error check routines
1500 ****************************************************************************/
1501 static void i7core_rdimm_update_csrow(struct mem_ctl_info *mci,
1507 struct i7core_pvt *pvt = mci->pvt_info;
1508 int row = pvt->csrow_map[chan][dimm], i;
1510 for (i = 0; i < add; i++) {
1511 msg = kasprintf(GFP_KERNEL, "Corrected error "
1512 "(Socket=%d channel=%d dimm=%d)",
1513 pvt->i7core_dev->socket, chan, dimm);
1515 edac_mc_handle_fbd_ce(mci, row, 0, msg);
1520 static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
1526 struct i7core_pvt *pvt = mci->pvt_info;
1527 int add0 = 0, add1 = 0, add2 = 0;
1528 /* Updates CE counters if it is not the first time here */
1529 if (pvt->ce_count_available) {
1530 /* Updates CE counters */
1532 add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
1533 add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
1534 add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
1538 pvt->rdimm_ce_count[chan][2] += add2;
1542 pvt->rdimm_ce_count[chan][1] += add1;
1546 pvt->rdimm_ce_count[chan][0] += add0;
1548 pvt->ce_count_available = 1;
1550 /* Store the new values */
1551 pvt->rdimm_last_ce_count[chan][2] = new2;
1552 pvt->rdimm_last_ce_count[chan][1] = new1;
1553 pvt->rdimm_last_ce_count[chan][0] = new0;
1555 /*updated the edac core */
1557 i7core_rdimm_update_csrow(mci, chan, 0, add0);
1559 i7core_rdimm_update_csrow(mci, chan, 1, add1);
1561 i7core_rdimm_update_csrow(mci, chan, 2, add2);
1565 static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
1567 struct i7core_pvt *pvt = mci->pvt_info;
1569 int i, new0, new1, new2;
1571 /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
1572 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
1574 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
1576 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
1578 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
1580 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
1582 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
1584 for (i = 0 ; i < 3; i++) {
1585 debugf3("MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
1586 (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
1587 /*if the channel has 3 dimms*/
1588 if (pvt->channel[i].dimms > 2) {
1589 new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
1590 new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
1591 new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
1593 new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
1594 DIMM_BOT_COR_ERR(rcv[i][0]);
1595 new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
1596 DIMM_BOT_COR_ERR(rcv[i][1]);
1600 i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
1604 /* This function is based on the device 3 function 4 registers as described on:
1605 * Intel Xeon Processor 5500 Series Datasheet Volume 2
1606 * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
1607 * also available at:
1608 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
1610 static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
1612 struct i7core_pvt *pvt = mci->pvt_info;
1614 int new0, new1, new2;
1616 if (!pvt->pci_mcr[4]) {
1617 debugf0("%s MCR registers not found\n", __func__);
1621 /* Corrected test errors */
1622 pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
1623 pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
1625 /* Store the new values */
1626 new2 = DIMM2_COR_ERR(rcv1);
1627 new1 = DIMM1_COR_ERR(rcv0);
1628 new0 = DIMM0_COR_ERR(rcv0);
1630 /* Updates CE counters if it is not the first time here */
1631 if (pvt->ce_count_available) {
1632 /* Updates CE counters */
1633 int add0, add1, add2;
1635 add2 = new2 - pvt->udimm_last_ce_count[2];
1636 add1 = new1 - pvt->udimm_last_ce_count[1];
1637 add0 = new0 - pvt->udimm_last_ce_count[0];
1641 pvt->udimm_ce_count[2] += add2;
1645 pvt->udimm_ce_count[1] += add1;
1649 pvt->udimm_ce_count[0] += add0;
1651 if (add0 | add1 | add2)
1652 i7core_printk(KERN_ERR, "New Corrected error(s): "
1653 "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
1656 pvt->ce_count_available = 1;
1658 /* Store the new values */
1659 pvt->udimm_last_ce_count[2] = new2;
1660 pvt->udimm_last_ce_count[1] = new1;
1661 pvt->udimm_last_ce_count[0] = new0;
1665 * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
1666 * Architectures Software Developer’s Manual Volume 3B.
1667 * Nehalem are defined as family 0x06, model 0x1a
1669 * The MCA registers used here are the following ones:
1670 * struct mce field MCA Register
1671 * m->status MSR_IA32_MC8_STATUS
1672 * m->addr MSR_IA32_MC8_ADDR
1673 * m->misc MSR_IA32_MC8_MISC
1674 * In the case of Nehalem, the error information is masked at .status and .misc
1677 static void i7core_mce_output_error(struct mem_ctl_info *mci,
1678 const struct mce *m)
1680 struct i7core_pvt *pvt = mci->pvt_info;
1681 char *type, *optype, *err, *msg;
1682 unsigned long error = m->status & 0x1ff0000l;
1683 u32 optypenum = (m->status >> 4) & 0x07;
1684 u32 core_err_cnt = (m->status >> 38) && 0x7fff;
1685 u32 dimm = (m->misc >> 16) & 0x3;
1686 u32 channel = (m->misc >> 18) & 0x3;
1687 u32 syndrome = m->misc >> 32;
1688 u32 errnum = find_first_bit(&error, 32);
1691 if (m->mcgstatus & 1)
1696 switch (optypenum) {
1698 optype = "generic undef request";
1701 optype = "read error";
1704 optype = "write error";
1707 optype = "addr/cmd error";
1710 optype = "scrubbing error";
1713 optype = "reserved";
1719 err = "read ECC error";
1722 err = "RAS ECC error";
1725 err = "write parity error";
1728 err = "redundacy loss";
1734 err = "memory range error";
1737 err = "RTID out of range";
1740 err = "address parity error";
1743 err = "byte enable parity error";
1749 /* FIXME: should convert addr into bank and rank information */
1750 msg = kasprintf(GFP_ATOMIC,
1751 "%s (addr = 0x%08llx, cpu=%d, Dimm=%d, Channel=%d, "
1752 "syndrome=0x%08x, count=%d, Err=%08llx:%08llx (%s: %s))\n",
1753 type, (long long) m->addr, m->cpu, dimm, channel,
1754 syndrome, core_err_cnt, (long long)m->status,
1755 (long long)m->misc, optype, err);
1759 csrow = pvt->csrow_map[channel][dimm];
1761 /* Call the helper to output message */
1762 if (m->mcgstatus & 1)
1763 edac_mc_handle_fbd_ue(mci, csrow, 0,
1764 0 /* FIXME: should be channel here */, msg);
1765 else if (!pvt->is_registered)
1766 edac_mc_handle_fbd_ce(mci, csrow,
1767 0 /* FIXME: should be channel here */, msg);
1773 * i7core_check_error Retrieve and process errors reported by the
1774 * hardware. Called by the Core module.
1776 static void i7core_check_error(struct mem_ctl_info *mci)
1778 struct i7core_pvt *pvt = mci->pvt_info;
1784 * MCE first step: Copy all mce errors into a temporary buffer
1785 * We use a double buffering here, to reduce the risk of
1789 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1792 goto check_ce_error;
1794 m = pvt->mce_outentry;
1795 if (pvt->mce_in + count > MCE_LOG_LEN) {
1796 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1798 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1804 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1806 pvt->mce_in += count;
1809 if (pvt->mce_overrun) {
1810 i7core_printk(KERN_ERR, "Lost %d memory errors\n",
1813 pvt->mce_overrun = 0;
1817 * MCE second step: parse errors and display
1819 for (i = 0; i < count; i++)
1820 i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
1823 * Now, let's increment CE error counts
1826 if (!pvt->is_registered)
1827 i7core_udimm_check_mc_ecc_err(mci);
1829 i7core_rdimm_check_mc_ecc_err(mci);
1833 * i7core_mce_check_error Replicates mcelog routine to get errors
1834 * This routine simply queues mcelog errors, and
1835 * return. The error itself should be handled later
1836 * by i7core_check_error.
1837 * WARNING: As this routine should be called at NMI time, extra care should
1838 * be taken to avoid deadlocks, and to be as fast as possible.
1840 static int i7core_mce_check_error(void *priv, struct mce *mce)
1842 struct mem_ctl_info *mci = priv;
1843 struct i7core_pvt *pvt = mci->pvt_info;
1846 * Just let mcelog handle it if the error is
1847 * outside the memory controller
1849 if (((mce->status & 0xffff) >> 7) != 1)
1852 /* Bank 8 registers are the only ones that we know how to handle */
1857 /* Only handle if it is the right mc controller */
1858 if (cpu_data(mce->cpu).phys_proc_id != pvt->i7core_dev->socket)
1863 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1869 /* Copy memory error at the ringbuffer */
1870 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1872 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1874 /* Handle fatal errors immediately */
1875 if (mce->mcgstatus & 1)
1876 i7core_check_error(mci);
1878 /* Advice mcelog that the error were handled */
1882 static int i7core_register_mci(struct i7core_dev *i7core_dev,
1883 const int num_channels, const int num_csrows)
1885 struct mem_ctl_info *mci;
1886 struct i7core_pvt *pvt;
1890 /* allocate a new MC control structure */
1891 mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels,
1892 i7core_dev->socket);
1896 debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
1898 /* record ptr to the generic device */
1899 mci->dev = &i7core_dev->pdev[0]->dev;
1901 pvt = mci->pvt_info;
1902 memset(pvt, 0, sizeof(*pvt));
1905 * FIXME: how to handle RDDR3 at MCI level? It is possible to have
1906 * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
1909 mci->mtype_cap = MEM_FLAG_DDR3;
1910 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1911 mci->edac_cap = EDAC_FLAG_NONE;
1912 mci->mod_name = "i7core_edac.c";
1913 mci->mod_ver = I7CORE_REVISION;
1914 mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
1915 i7core_dev->socket);
1916 mci->dev_name = pci_name(i7core_dev->pdev[0]);
1917 mci->ctl_page_to_phys = NULL;
1919 if (pvt->is_registered)
1920 mci->mc_driver_sysfs_attributes = i7core_sysfs_rdimm_attrs;
1922 mci->mc_driver_sysfs_attributes = i7core_sysfs_udimm_attrs;
1924 /* Set the function pointer to an actual operation function */
1925 mci->edac_check = i7core_check_error;
1927 /* Store pci devices at mci for faster access */
1928 rc = mci_bind_devs(mci, i7core_dev);
1929 if (unlikely(rc < 0))
1932 /* Get dimm basic config */
1933 get_dimm_config(mci, &csrow);
1935 /* add this new MC control structure to EDAC's list of MCs */
1936 if (unlikely(edac_mc_add_mc(mci))) {
1937 debugf0("MC: " __FILE__
1938 ": %s(): failed edac_mc_add_mc()\n", __func__);
1939 /* FIXME: perhaps some code should go here that disables error
1940 * reporting if we just enabled it
1947 /* allocating generic PCI control info */
1948 pvt->i7core_pci = edac_pci_create_generic_ctl(&i7core_dev->pdev[0]->dev,
1950 if (unlikely(!pvt->i7core_pci)) {
1952 "%s(): Unable to create PCI control\n",
1955 "%s(): PCI error report via EDAC not setup\n",
1959 /* Default error mask is any memory */
1960 pvt->inject.channel = 0;
1961 pvt->inject.dimm = -1;
1962 pvt->inject.rank = -1;
1963 pvt->inject.bank = -1;
1964 pvt->inject.page = -1;
1965 pvt->inject.col = -1;
1967 /* Registers on edac_mce in order to receive memory errors */
1968 pvt->edac_mce.priv = mci;
1969 pvt->edac_mce.check_error = i7core_mce_check_error;
1971 rc = edac_mce_register(&pvt->edac_mce);
1972 if (unlikely(rc < 0)) {
1973 debugf0("MC: " __FILE__
1974 ": %s(): failed edac_mce_register()\n", __func__);
1984 * i7core_probe Probe for ONE instance of device to see if it is
1987 * 0 for FOUND a device
1988 * < 0 for error code
1991 static int __devinit i7core_probe(struct pci_dev *pdev,
1992 const struct pci_device_id *id)
1995 struct i7core_dev *i7core_dev;
1997 /* get the pci devices we want to reserve for our use */
1998 mutex_lock(&i7core_edac_lock);
2001 * All memory controllers are allocated at the first pass.
2003 if (unlikely(probed >= 1)) {
2004 mutex_unlock(&i7core_edac_lock);
2009 rc = i7core_get_devices(pci_dev_table);
2010 if (unlikely(rc < 0))
2013 list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
2017 /* Check the number of active and not disabled channels */
2018 rc = i7core_get_active_channels(i7core_dev->socket,
2019 &channels, &csrows);
2020 if (unlikely(rc < 0))
2023 rc = i7core_register_mci(i7core_dev, channels, csrows);
2024 if (unlikely(rc < 0))
2028 i7core_printk(KERN_INFO, "Driver loaded.\n");
2030 mutex_unlock(&i7core_edac_lock);
2034 i7core_put_all_devices();
2036 mutex_unlock(&i7core_edac_lock);
2041 * i7core_remove destructor for one instance of device
2044 static void __devexit i7core_remove(struct pci_dev *pdev)
2046 struct mem_ctl_info *mci;
2047 struct i7core_dev *i7core_dev, *tmp;
2048 struct i7core_pvt *pvt;
2050 debugf0(__FILE__ ": %s()\n", __func__);
2053 * we have a trouble here: pdev value for removal will be wrong, since
2054 * it will point to the X58 register used to detect that the machine
2055 * is a Nehalem or upper design. However, due to the way several PCI
2056 * devices are grouped together to provide MC functionality, we need
2057 * to use a different method for releasing the devices
2060 mutex_lock(&i7core_edac_lock);
2061 list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
2062 mci = find_mci_by_dev(&i7core_dev->pdev[0]->dev);
2063 if (unlikely(!mci || !mci->pvt_info)) {
2064 i7core_printk(KERN_ERR,
2065 "Couldn't find mci hanler\n");
2067 pvt = mci->pvt_info;
2068 i7core_dev = pvt->i7core_dev;
2070 if (likely(pvt->i7core_pci))
2071 edac_pci_release_generic_ctl(pvt->i7core_pci);
2073 i7core_printk(KERN_ERR,
2074 "Couldn't find mem_ctl_info for socket %d\n",
2075 i7core_dev->socket);
2076 pvt->i7core_pci = NULL;
2078 edac_mc_del_mc(&i7core_dev->pdev[0]->dev);
2080 edac_mce_unregister(&pvt->edac_mce);
2081 kfree(mci->ctl_name);
2083 i7core_put_devices(i7core_dev);
2088 mutex_unlock(&i7core_edac_lock);
2091 MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
2094 * i7core_driver pci_driver structure for this module
2097 static struct pci_driver i7core_driver = {
2098 .name = "i7core_edac",
2099 .probe = i7core_probe,
2100 .remove = __devexit_p(i7core_remove),
2101 .id_table = i7core_pci_tbl,
2105 * i7core_init Module entry function
2106 * Try to initialize this module for its devices
2108 static int __init i7core_init(void)
2112 debugf2("MC: " __FILE__ ": %s()\n", __func__);
2114 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2117 i7core_xeon_pci_fixup(pci_dev_table);
2119 pci_rc = pci_register_driver(&i7core_driver);
2124 i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
2131 * i7core_exit() Module exit function
2132 * Unregister the driver
2134 static void __exit i7core_exit(void)
2136 debugf2("MC: " __FILE__ ": %s()\n", __func__);
2137 pci_unregister_driver(&i7core_driver);
2140 module_init(i7core_init);
2141 module_exit(i7core_exit);
2143 MODULE_LICENSE("GPL");
2144 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2145 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
2146 MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
2149 module_param(edac_op_state, int, 0444);
2150 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");