]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/edac/i7core_edac.c
i7core_edac: Add more status functions to EDAC driver
[net-next-2.6.git] / drivers / edac / i7core_edac.c
CommitLineData
a0c36a1f
MCC
1/* Intel 7 core Memory Controller kernel module (Nehalem)
2 *
3 * This file may be distributed under the terms of the
4 * GNU General Public License version 2 only.
5 *
6 * Copyright (c) 2009 by:
7 * Mauro Carvalho Chehab <mchehab@redhat.com>
8 *
9 * Red Hat Inc. http://www.redhat.com
10 *
11 * Forked and adapted from the i5400_edac driver
12 *
13 * Based on the following public Intel datasheets:
14 * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
15 * Datasheet, Volume 2:
16 * http://download.intel.com/design/processor/datashts/320835.pdf
17 * Intel Xeon Processor 5500 Series Datasheet Volume 2
18 * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
19 * also available at:
20 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
21 */
22
a0c36a1f
MCC
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/pci.h>
26#include <linux/pci_ids.h>
27#include <linux/slab.h>
28#include <linux/edac.h>
29#include <linux/mmzone.h>
30
31#include "edac_core.h"
32
33
34/*
35 * Alter this version for the module when modifications are made
36 */
37#define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
38#define EDAC_MOD_STR "i7core_edac"
39
40/* HACK: temporary, just to enable all logs, for now */
41#undef debugf0
42#define debugf0(fmt, arg...) edac_printk(KERN_INFO, "i7core", fmt, ##arg)
43
44/*
45 * Debug macros
46 */
47#define i7core_printk(level, fmt, arg...) \
48 edac_printk(level, "i7core", fmt, ##arg)
49
50#define i7core_mc_printk(mci, level, fmt, arg...) \
51 edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
52
53/*
54 * i7core Memory Controller Registers
55 */
56
57 /* OFFSETS for Device 3 Function 0 */
58
59#define MC_CONTROL 0x48
60#define MC_STATUS 0x4c
61#define MC_MAX_DOD 0x64
62
63 /* OFFSETS for Devices 4,5 and 6 Function 0 */
64
0b2b7b7e
MCC
65#define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
66 #define THREE_DIMMS_PRESENT (1 << 24)
67 #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
68 #define QUAD_RANK_PRESENT (1 << 22)
69 #define REGISTERED_DIMM (1 << 15)
70
71#define MC_CHANNEL_RANK_PRESENT 0x7c
72 #define RANK_PRESENT_MASK 0xffff
73
a0c36a1f 74#define MC_CHANNEL_ADDR_MATCH 0xf0
194a40fe
MCC
75#define MC_CHANNEL_ERROR_MASK 0xf8
76#define MC_CHANNEL_ERROR_INJECT 0xfc
77 #define INJECT_ADDR_PARITY 0x10
78 #define INJECT_ECC 0x08
79 #define MASK_CACHELINE 0x06
80 #define MASK_FULL_CACHELINE 0x06
81 #define MASK_MSB32_CACHELINE 0x04
82 #define MASK_LSB32_CACHELINE 0x02
83 #define NO_MASK_CACHELINE 0x00
84 #define REPEAT_EN 0x01
a0c36a1f 85
0b2b7b7e
MCC
86 /* OFFSETS for Devices 4,5 and 6 Function 1 */
87#define MC_DOD_CH_DIMM0 0x48
88#define MC_DOD_CH_DIMM1 0x4c
89#define MC_DOD_CH_DIMM2 0x50
90 #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
91 #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
92 #define DIMM_PRESENT_MASK (1 << 9)
93 #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
94 #define NUMBANK_MASK ((1 << 8) | (1 << 7))
95 #define NUMBANK(x) (((x) & NUMBANK_MASK) >> 7)
96 #define NUMRANK_MASK ((1 << 6) | (1 << 5))
97 #define NUMRANK(x) (((x) & NUMRANK_MASK) >> 5)
98 #define NUMROW_MASK ((1 << 4) | (1 << 3))
99 #define NUMROW(x) (((x) & NUMROW_MASK) >> 3)
100 #define NUMCOL_MASK 3
101 #define NUMCOL(x) ((x) & NUMCOL_MASK)
102
103#define MC_SAG_CH_0 0x80
104#define MC_SAG_CH_1 0x84
105#define MC_SAG_CH_2 0x88
106#define MC_SAG_CH_3 0x8c
107#define MC_SAG_CH_4 0x90
108#define MC_SAG_CH_5 0x94
109#define MC_SAG_CH_6 0x98
110#define MC_SAG_CH_7 0x9c
111
112#define MC_RIR_LIMIT_CH_0 0x40
113#define MC_RIR_LIMIT_CH_1 0x44
114#define MC_RIR_LIMIT_CH_2 0x48
115#define MC_RIR_LIMIT_CH_3 0x4C
116#define MC_RIR_LIMIT_CH_4 0x50
117#define MC_RIR_LIMIT_CH_5 0x54
118#define MC_RIR_LIMIT_CH_6 0x58
119#define MC_RIR_LIMIT_CH_7 0x5C
120#define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
121
122#define MC_RIR_WAY_CH 0x80
123 #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
124 #define MC_RIR_WAY_RANK_MASK 0x7
125
a0c36a1f
MCC
126/*
127 * i7core structs
128 */
129
130#define NUM_CHANS 3
131#define NUM_FUNCS 1
132
133struct i7core_info {
134 u32 mc_control;
135 u32 mc_status;
136 u32 max_dod;
137};
138
194a40fe
MCC
139
140struct i7core_inject {
141 int enable;
142
143 u32 section;
144 u32 type;
145 u32 eccmask;
146
147 /* Error address mask */
148 int channel, dimm, rank, bank, page, col;
149};
150
0b2b7b7e
MCC
151struct i7core_channel {
152 u32 ranks;
153 u32 dimms;
154};
155
a0c36a1f
MCC
156struct i7core_pvt {
157 struct pci_dev *pci_mcr; /* Dev 3:0 */
158 struct pci_dev *pci_ch[NUM_CHANS][NUM_FUNCS];
159 struct i7core_info info;
194a40fe 160 struct i7core_inject inject;
0b2b7b7e 161 struct i7core_channel channel[NUM_CHANS];
a0c36a1f
MCC
162};
163
164/* Device name and register DID (Device ID) */
165struct i7core_dev_info {
166 const char *ctl_name; /* name for this device */
167 u16 fsb_mapping_errors; /* DID for the branchmap,control */
168};
169
170static int chan_pci_ids[NUM_CHANS] = {
171 PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL, /* Dev 4 */
172 PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL, /* Dev 5 */
173 PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL, /* Dev 6 */
174};
175
176/* Table of devices attributes supported by this driver */
177static const struct i7core_dev_info i7core_devs[] = {
178 {
179 .ctl_name = "i7 Core",
180 .fsb_mapping_errors = PCI_DEVICE_ID_INTEL_I7_MCR,
181 },
182};
183
184static struct edac_pci_ctl_info *i7core_pci;
185
186/****************************************************************************
187 Anciliary status routines
188 ****************************************************************************/
189
190 /* MC_CONTROL bits */
0b2b7b7e 191#define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & 1 << (8 + ch))
a0c36a1f
MCC
192#define ECCx8(pvt) ((pvt)->info.mc_control & 1 << 1)
193
194 /* MC_STATUS bits */
195#define ECC_ENABLED(pvt) ((pvt)->info.mc_status & 1 << 3)
0b2b7b7e 196#define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & 1 << ch)
a0c36a1f
MCC
197
198 /* MC_MAX_DOD read functions */
199static inline int maxnumdimms(struct i7core_pvt *pvt)
200{
201 return (pvt->info.max_dod & 0x3) + 1;
202}
203
204static inline int maxnumrank(struct i7core_pvt *pvt)
205{
206 static int ranks[4] = { 1, 2, 4, -EINVAL };
207
208 return ranks[(pvt->info.max_dod >> 2) & 0x3];
209}
210
211static inline int maxnumbank(struct i7core_pvt *pvt)
212{
213 static int banks[4] = { 4, 8, 16, -EINVAL };
214
215 return banks[(pvt->info.max_dod >> 4) & 0x3];
216}
217
218static inline int maxnumrow(struct i7core_pvt *pvt)
219{
220 static int rows[8] = {
221 1 << 12, 1 << 13, 1 << 14, 1 << 15,
222 1 << 16, -EINVAL, -EINVAL, -EINVAL,
223 };
224
225 return rows[((pvt->info.max_dod >> 6) & 0x7)];
226}
227
228static inline int maxnumcol(struct i7core_pvt *pvt)
229{
230 static int cols[8] = {
231 1 << 10, 1 << 11, 1 << 12, -EINVAL,
232 };
233 return cols[((pvt->info.max_dod >> 9) & 0x3) << 12];
234}
235
194a40fe 236
a0c36a1f
MCC
237/****************************************************************************
238 Memory check routines
239 ****************************************************************************/
240static int get_dimm_config(struct mem_ctl_info *mci)
241{
242 struct i7core_pvt *pvt = mci->pvt_info;
0b2b7b7e 243 int i;
a0c36a1f
MCC
244
245 pci_read_config_dword(pvt->pci_mcr, MC_CONTROL, &pvt->info.mc_control);
246 pci_read_config_dword(pvt->pci_mcr, MC_STATUS, &pvt->info.mc_status);
247 pci_read_config_dword(pvt->pci_mcr, MC_MAX_DOD, &pvt->info.max_dod);
248
a0c36a1f
MCC
249 if (ECC_ENABLED(pvt))
250 debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt)?8:4);
251 else
252 debugf0("ECC disabled\n");
253
254 /* FIXME: need to handle the error codes */
255 debugf0("DOD Maximum limits: DIMMS: %d, %d-ranked, %d-banked\n",
256 maxnumdimms(pvt), maxnumrank(pvt), maxnumbank(pvt));
257 debugf0("DOD Maximum rows x colums = 0x%x x 0x%x\n",
258 maxnumrow(pvt), maxnumcol(pvt));
259
0b2b7b7e
MCC
260 debugf0("Memory channel configuration:\n");
261
262 for (i = 0; i < NUM_CHANS; i++) {
263 u32 data;
264
265 if (!CH_ACTIVE(pvt, i)) {
266 debugf0("Channel %i is not active\n", i);
267 continue;
268 }
269 if (CH_DISABLED(pvt, i)) {
270 debugf0("Channel %i is disabled\n", i);
271 continue;
272 }
273
274 pci_read_config_dword(pvt->pci_ch[i][0],
275 MC_CHANNEL_DIMM_INIT_PARAMS, &data);
276
277 pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT)? 4 : 2;
278
279 if (data & THREE_DIMMS_PRESENT)
280 pvt->channel[i].dimms = 3;
281 else if (data & SINGLE_QUAD_RANK_PRESENT)
282 pvt->channel[i].dimms = 1;
283 else
284 pvt->channel[i].dimms = 2;
285
286 debugf0("Channel %d (0x%08x): %d ranks, %d dimms "
287 "(%sregistered)\n", i, data,
288 pvt->channel[i].ranks, pvt->channel[i].dimms,
289 (data & REGISTERED_DIMM)? "" : "un" );
290 }
291
a0c36a1f
MCC
292 return 0;
293}
294
194a40fe
MCC
295/****************************************************************************
296 Error insertion routines
297 ****************************************************************************/
298
299/* The i7core has independent error injection features per channel.
300 However, to have a simpler code, we don't allow enabling error injection
301 on more than one channel.
302 Also, since a change at an inject parameter will be applied only at enable,
303 we're disabling error injection on all write calls to the sysfs nodes that
304 controls the error code injection.
305 */
306static void disable_inject(struct mem_ctl_info *mci)
307{
308 struct i7core_pvt *pvt = mci->pvt_info;
309
310 pvt->inject.enable = 0;
311
312 pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
313 MC_CHANNEL_ERROR_MASK, 0);
314}
315
316/*
317 * i7core inject inject.section
318 *
319 * accept and store error injection inject.section value
320 * bit 0 - refers to the lower 32-byte half cacheline
321 * bit 1 - refers to the upper 32-byte half cacheline
322 */
323static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
324 const char *data, size_t count)
325{
326 struct i7core_pvt *pvt = mci->pvt_info;
327 unsigned long value;
328 int rc;
329
330 if (pvt->inject.enable)
331 disable_inject(mci);
332
333 rc = strict_strtoul(data, 10, &value);
334 if ((rc < 0) || (value > 3))
335 return 0;
336
337 pvt->inject.section = (u32) value;
338 return count;
339}
340
341static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
342 char *data)
343{
344 struct i7core_pvt *pvt = mci->pvt_info;
345 return sprintf(data, "0x%08x\n", pvt->inject.section);
346}
347
348/*
349 * i7core inject.type
350 *
351 * accept and store error injection inject.section value
352 * bit 0 - repeat enable - Enable error repetition
353 * bit 1 - inject ECC error
354 * bit 2 - inject parity error
355 */
356static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
357 const char *data, size_t count)
358{
359 struct i7core_pvt *pvt = mci->pvt_info;
360 unsigned long value;
361 int rc;
362
363 if (pvt->inject.enable)
364 disable_inject(mci);
365
366 rc = strict_strtoul(data, 10, &value);
367 if ((rc < 0) || (value > 7))
368 return 0;
369
370 pvt->inject.type = (u32) value;
371 return count;
372}
373
374static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
375 char *data)
376{
377 struct i7core_pvt *pvt = mci->pvt_info;
378 return sprintf(data, "0x%08x\n", pvt->inject.type);
379}
380
381/*
382 * i7core_inject_inject.eccmask_store
383 *
384 * The type of error (UE/CE) will depend on the inject.eccmask value:
385 * Any bits set to a 1 will flip the corresponding ECC bit
386 * Correctable errors can be injected by flipping 1 bit or the bits within
387 * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
388 * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
389 * uncorrectable error to be injected.
390 */
391static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
392 const char *data, size_t count)
393{
394 struct i7core_pvt *pvt = mci->pvt_info;
395 unsigned long value;
396 int rc;
397
398 if (pvt->inject.enable)
399 disable_inject(mci);
400
401 rc = strict_strtoul(data, 10, &value);
402 if (rc < 0)
403 return 0;
404
405 pvt->inject.eccmask = (u32) value;
406 return count;
407}
408
409static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
410 char *data)
411{
412 struct i7core_pvt *pvt = mci->pvt_info;
413 return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
414}
415
416/*
417 * i7core_addrmatch
418 *
419 * The type of error (UE/CE) will depend on the inject.eccmask value:
420 * Any bits set to a 1 will flip the corresponding ECC bit
421 * Correctable errors can be injected by flipping 1 bit or the bits within
422 * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
423 * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
424 * uncorrectable error to be injected.
425 */
426static ssize_t i7core_inject_addrmatch_store(struct mem_ctl_info *mci,
427 const char *data, size_t count)
428{
429 struct i7core_pvt *pvt = mci->pvt_info;
430 char *cmd, *val;
431 long value;
432 int rc;
433
434 if (pvt->inject.enable)
435 disable_inject(mci);
436
437 do {
438 cmd = strsep((char **) &data, ":");
439 if (!cmd)
440 break;
441 val = strsep((char **) &data, " \n\t");
442 if (!val)
443 return cmd - data;
444
445 if (!strcasecmp(val,"any"))
446 value = -1;
447 else {
448 rc = strict_strtol(val, 10, &value);
449 if ((rc < 0) || (value < 0))
450 return cmd - data;
451 }
452
453 if (!strcasecmp(cmd,"channel")) {
454 if (value < 3)
455 pvt->inject.channel = value;
456 else
457 return cmd - data;
458 } else if (!strcasecmp(cmd,"dimm")) {
459 if (value < 4)
460 pvt->inject.dimm = value;
461 else
462 return cmd - data;
463 } else if (!strcasecmp(cmd,"rank")) {
464 if (value < 4)
465 pvt->inject.rank = value;
466 else
467 return cmd - data;
468 } else if (!strcasecmp(cmd,"bank")) {
469 if (value < 4)
470 pvt->inject.bank = value;
471 else
472 return cmd - data;
473 } else if (!strcasecmp(cmd,"page")) {
474 if (value <= 0xffff)
475 pvt->inject.page = value;
476 else
477 return cmd - data;
478 } else if (!strcasecmp(cmd,"col") ||
479 !strcasecmp(cmd,"column")) {
480 if (value <= 0x3fff)
481 pvt->inject.col = value;
482 else
483 return cmd - data;
484 }
485 } while (1);
486
487 return count;
488}
489
490static ssize_t i7core_inject_addrmatch_show(struct mem_ctl_info *mci,
491 char *data)
492{
493 struct i7core_pvt *pvt = mci->pvt_info;
494 char channel[4], dimm[4], bank[4], rank[4], page[7], col[7];
495
496 if (pvt->inject.channel < 0)
497 sprintf(channel, "any");
498 else
499 sprintf(channel, "%d", pvt->inject.channel);
500 if (pvt->inject.dimm < 0)
501 sprintf(dimm, "any");
502 else
503 sprintf(dimm, "%d", pvt->inject.dimm);
504 if (pvt->inject.bank < 0)
505 sprintf(bank, "any");
506 else
507 sprintf(bank, "%d", pvt->inject.bank);
508 if (pvt->inject.rank < 0)
509 sprintf(rank, "any");
510 else
511 sprintf(rank, "%d", pvt->inject.rank);
512 if (pvt->inject.page < 0)
513 sprintf(page, "any");
514 else
515 sprintf(page, "0x%04x", pvt->inject.page);
516 if (pvt->inject.col < 0)
517 sprintf(col, "any");
518 else
519 sprintf(col, "0x%04x", pvt->inject.col);
520
521 return sprintf(data, "channel: %s\ndimm: %s\nbank: %s\n"
522 "rank: %s\npage: %s\ncolumn: %s\n",
523 channel, dimm, bank, rank, page, col);
524}
525
526/*
527 * This routine prepares the Memory Controller for error injection.
528 * The error will be injected when some process tries to write to the
529 * memory that matches the given criteria.
530 * The criteria can be set in terms of a mask where dimm, rank, bank, page
531 * and col can be specified.
532 * A -1 value for any of the mask items will make the MCU to ignore
533 * that matching criteria for error injection.
534 *
535 * It should be noticed that the error will only happen after a write operation
536 * on a memory that matches the condition. if REPEAT_EN is not enabled at
537 * inject mask, then it will produce just one error. Otherwise, it will repeat
538 * until the injectmask would be cleaned.
539 *
540 * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
541 * is reliable enough to check if the MC is using the
542 * three channels. However, this is not clear at the datasheet.
543 */
544static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
545 const char *data, size_t count)
546{
547 struct i7core_pvt *pvt = mci->pvt_info;
548 u32 injectmask;
549 u64 mask = 0;
550 int rc;
551 long enable;
552
553 rc = strict_strtoul(data, 10, &enable);
554 if ((rc < 0))
555 return 0;
556
557 if (enable) {
558 pvt->inject.enable = 1;
559 } else {
560 disable_inject(mci);
561 return count;
562 }
563
564 /* Sets pvt->inject.dimm mask */
565 if (pvt->inject.dimm < 0)
566 mask |= 1l << 41;
567 else {
0b2b7b7e 568 if (pvt->channel[pvt->inject.channel].dimms > 2)
194a40fe
MCC
569 mask |= (pvt->inject.dimm & 0x3l) << 35;
570 else
571 mask |= (pvt->inject.dimm & 0x1l) << 36;
572 }
573
574 /* Sets pvt->inject.rank mask */
575 if (pvt->inject.rank < 0)
576 mask |= 1l << 40;
577 else {
0b2b7b7e 578 if (pvt->channel[pvt->inject.channel].dimms > 2)
194a40fe
MCC
579 mask |= (pvt->inject.rank & 0x1l) << 34;
580 else
581 mask |= (pvt->inject.rank & 0x3l) << 34;
582 }
583
584 /* Sets pvt->inject.bank mask */
585 if (pvt->inject.bank < 0)
586 mask |= 1l << 39;
587 else
588 mask |= (pvt->inject.bank & 0x15l) << 30;
589
590 /* Sets pvt->inject.page mask */
591 if (pvt->inject.page < 0)
592 mask |= 1l << 38;
593 else
594 mask |= (pvt->inject.page & 0xffffl) << 14;
595
596 /* Sets pvt->inject.column mask */
597 if (pvt->inject.col < 0)
598 mask |= 1l << 37;
599 else
600 mask |= (pvt->inject.col & 0x3fffl);
601
602 pci_write_config_qword(pvt->pci_ch[pvt->inject.channel][0],
603 MC_CHANNEL_ADDR_MATCH, mask);
604
605 pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
606 MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
607
608 /*
609 * bit 0: REPEAT_EN
610 * bits 1-2: MASK_HALF_CACHELINE
611 * bit 3: INJECT_ECC
612 * bit 4: INJECT_ADDR_PARITY
613 */
614
615 injectmask = (pvt->inject.type & 1) &&
616 (pvt->inject.section & 0x3) << 1 &&
617 (pvt->inject.type & 0x6) << (3 - 1);
618
619 pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
620 MC_CHANNEL_ERROR_MASK, injectmask);
621
622
623 debugf0("Error inject addr match 0x%016llx, ecc 0x%08x, inject 0x%08x\n",
624 mask, pvt->inject.eccmask, injectmask);
625
626 return count;
627}
628
629static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
630 char *data)
631{
632 struct i7core_pvt *pvt = mci->pvt_info;
633 return sprintf(data, "%d\n", pvt->inject.enable);
634}
635
636/*
637 * Sysfs struct
638 */
639static struct mcidev_sysfs_attribute i7core_inj_attrs[] = {
640
641 {
642 .attr = {
643 .name = "inject_section",
644 .mode = (S_IRUGO | S_IWUSR)
645 },
646 .show = i7core_inject_section_show,
647 .store = i7core_inject_section_store,
648 }, {
649 .attr = {
650 .name = "inject_type",
651 .mode = (S_IRUGO | S_IWUSR)
652 },
653 .show = i7core_inject_type_show,
654 .store = i7core_inject_type_store,
655 }, {
656 .attr = {
657 .name = "inject_eccmask",
658 .mode = (S_IRUGO | S_IWUSR)
659 },
660 .show = i7core_inject_eccmask_show,
661 .store = i7core_inject_eccmask_store,
662 }, {
663 .attr = {
664 .name = "inject_addrmatch",
665 .mode = (S_IRUGO | S_IWUSR)
666 },
667 .show = i7core_inject_addrmatch_show,
668 .store = i7core_inject_addrmatch_store,
669 }, {
670 .attr = {
671 .name = "inject_enable",
672 .mode = (S_IRUGO | S_IWUSR)
673 },
674 .show = i7core_inject_enable_show,
675 .store = i7core_inject_enable_store,
676 },
677};
678
a0c36a1f
MCC
679/****************************************************************************
680 Device initialization routines: put/get, init/exit
681 ****************************************************************************/
682
683/*
684 * i7core_put_devices 'put' all the devices that we have
685 * reserved via 'get'
686 */
687static void i7core_put_devices(struct mem_ctl_info *mci)
688{
689 struct i7core_pvt *pvt = mci->pvt_info;
690 int i, n;
691
692 pci_dev_put(pvt->pci_mcr);
693
694 /* Release all PCI device functions at MTR channel controllers */
695 for (i = 0; i < NUM_CHANS; i++)
696 for (n = 0; n < NUM_FUNCS; n++)
697 pci_dev_put(pvt->pci_ch[i][n]);
698}
699
700/*
701 * i7core_get_devices Find and perform 'get' operation on the MCH's
702 * device/functions we want to reference for this driver
703 *
704 * Need to 'get' device 16 func 1 and func 2
705 */
706static int i7core_get_devices(struct mem_ctl_info *mci, int dev_idx)
707{
708 struct i7core_pvt *pvt;
709 struct pci_dev *pdev;
710 int i, n, func;
711
712 pvt = mci->pvt_info;
713 memset(pvt, 0, sizeof(*pvt));
714
715 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7_MCR,
716 NULL);
717 if (!pdev) {
718 i7core_printk(KERN_ERR,
719 "Couldn't get PCI ID %04x:%04x function 0\n",
720 PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7_MCR);
721 return -ENODEV;
722 }
723 pvt->pci_mcr=pdev;
724
a0c36a1f
MCC
725 /* Retrieve all needed functions at MTR channel controllers */
726 for (i = 0; i < NUM_CHANS; i++) {
727 pdev = NULL;
728 for (n = 0; n < NUM_FUNCS; n++) {
729 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
730 chan_pci_ids[i], pdev);
731 if (!pdev) {
732 /* End of list, leave */
733 i7core_printk(KERN_ERR,
734 "Device not found: PCI ID %04x:%04x "
735 "found only %d functions "
736 "(broken BIOS?)\n",
737 PCI_VENDOR_ID_INTEL,
738 chan_pci_ids[i], n);
739 i7core_put_devices(mci);
740 return -ENODEV;
741 }
742 func = PCI_FUNC(pdev->devfn);
743 pvt->pci_ch[i][func] = pdev;
744 }
745 }
746 i7core_printk(KERN_INFO, "Driver loaded.\n");
747
0b2b7b7e
MCC
748 /* Get dimm basic config */
749 get_dimm_config(mci);
750
a0c36a1f
MCC
751 return 0;
752}
753
754/*
755 * i7core_probe Probe for ONE instance of device to see if it is
756 * present.
757 * return:
758 * 0 for FOUND a device
759 * < 0 for error code
760 */
761static int __devinit i7core_probe(struct pci_dev *pdev,
762 const struct pci_device_id *id)
763{
764 struct mem_ctl_info *mci;
765 struct i7core_pvt *pvt;
766 int rc;
767 int num_channels;
768 int num_csrows;
769 int num_dimms_per_channel;
770 int dev_idx = id->driver_data;
771
772 if (dev_idx >= ARRAY_SIZE(i7core_devs))
773 return -EINVAL;
774
775 /* wake up device */
776 rc = pci_enable_device(pdev);
777 if (rc == -EIO)
778 return rc;
779
780 debugf0("MC: " __FILE__ ": %s(), pdev bus %u dev=0x%x fn=0x%x\n",
781 __func__,
782 pdev->bus->number,
783 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
784
785 /* We only are looking for func 0 of the set */
786 if (PCI_FUNC(pdev->devfn) != 0)
787 return -ENODEV;
788
789 num_channels = NUM_CHANS;
790
791 /* FIXME: FAKE data, since we currently don't now how to get this */
792 num_dimms_per_channel = 4;
793 num_csrows = num_dimms_per_channel;
794
795 /* allocate a new MC control structure */
796 mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels, 0);
797 if (mci == NULL)
798 return -ENOMEM;
799
800 debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
801
194a40fe 802 mci->dev = &pdev->dev; /* record ptr to the generic device */
a0c36a1f
MCC
803 dev_set_drvdata(mci->dev, mci);
804
805 pvt = mci->pvt_info;
194a40fe 806
a0c36a1f
MCC
807// pvt->system_address = pdev; /* Record this device in our private */
808// pvt->maxch = num_channels;
809// pvt->maxdimmperch = num_dimms_per_channel;
810
811 /* 'get' the pci devices we want to reserve for our use */
812 if (i7core_get_devices(mci, dev_idx))
813 goto fail0;
814
815 mci->mc_idx = 0;
816 mci->mtype_cap = MEM_FLAG_FB_DDR2; /* FIXME: it uses DDR3 */
817 mci->edac_ctl_cap = EDAC_FLAG_NONE;
818 mci->edac_cap = EDAC_FLAG_NONE;
819 mci->mod_name = "i7core_edac.c";
820 mci->mod_ver = I7CORE_REVISION;
821 mci->ctl_name = i7core_devs[dev_idx].ctl_name;
822 mci->dev_name = pci_name(pdev);
823 mci->ctl_page_to_phys = NULL;
194a40fe 824 mci->mc_driver_sysfs_attributes = i7core_inj_attrs;
a0c36a1f
MCC
825
826 /* add this new MC control structure to EDAC's list of MCs */
827 if (edac_mc_add_mc(mci)) {
828 debugf0("MC: " __FILE__
829 ": %s(): failed edac_mc_add_mc()\n", __func__);
830 /* FIXME: perhaps some code should go here that disables error
831 * reporting if we just enabled it
832 */
833 goto fail1;
834 }
835
836 /* allocating generic PCI control info */
837 i7core_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
838 if (!i7core_pci) {
839 printk(KERN_WARNING
840 "%s(): Unable to create PCI control\n",
841 __func__);
842 printk(KERN_WARNING
843 "%s(): PCI error report via EDAC not setup\n",
844 __func__);
845 }
846
194a40fe
MCC
847 /* Default error mask is any memory */
848 pvt->inject.channel = -1;
849 pvt->inject.dimm = -1;
850 pvt->inject.rank = -1;
851 pvt->inject.bank = -1;
852 pvt->inject.page = -1;
853 pvt->inject.col = -1;
854
a0c36a1f
MCC
855 return 0;
856
857fail1:
858 i7core_put_devices(mci);
859
860fail0:
861 edac_mc_free(mci);
862 return -ENODEV;
863}
864
865/*
866 * i7core_remove destructor for one instance of device
867 *
868 */
869static void __devexit i7core_remove(struct pci_dev *pdev)
870{
871 struct mem_ctl_info *mci;
872
873 debugf0(__FILE__ ": %s()\n", __func__);
874
875 if (i7core_pci)
876 edac_pci_release_generic_ctl(i7core_pci);
877
878 mci = edac_mc_del_mc(&pdev->dev);
879 if (!mci)
880 return;
881
882 /* retrieve references to resources, and free those resources */
883 i7core_put_devices(mci);
884
885 edac_mc_free(mci);
886}
887
888/*
889 * pci_device_id table for which devices we are looking for
890 *
891 * The "E500P" device is the first device supported.
892 */
893static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
894 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7_MCR)},
895 {0,} /* 0 terminated list. */
896};
897
898MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
899
900/*
901 * i7core_driver pci_driver structure for this module
902 *
903 */
904static struct pci_driver i7core_driver = {
905 .name = "i7core_edac",
906 .probe = i7core_probe,
907 .remove = __devexit_p(i7core_remove),
908 .id_table = i7core_pci_tbl,
909};
910
911/*
912 * i7core_init Module entry function
913 * Try to initialize this module for its devices
914 */
915static int __init i7core_init(void)
916{
917 int pci_rc;
918
919 debugf2("MC: " __FILE__ ": %s()\n", __func__);
920
921 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
922 opstate_init();
923
924 pci_rc = pci_register_driver(&i7core_driver);
925
926 return (pci_rc < 0) ? pci_rc : 0;
927}
928
929/*
930 * i7core_exit() Module exit function
931 * Unregister the driver
932 */
933static void __exit i7core_exit(void)
934{
935 debugf2("MC: " __FILE__ ": %s()\n", __func__);
936 pci_unregister_driver(&i7core_driver);
937}
938
939module_init(i7core_init);
940module_exit(i7core_exit);
941
942MODULE_LICENSE("GPL");
943MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
944MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
945MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
946 I7CORE_REVISION);
947
948module_param(edac_op_state, int, 0444);
949MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");