i7300_edac: Add error detection code for global errors
[linux-block.git] / drivers / edac / i7300_edac.c
CommitLineData
fcaf780b
MCC
1/*
2 * Intel 7300 class Memory Controllers kernel module (Clarksboro)
3 *
4 * This file may be distributed under the terms of the
5 * GNU General Public License version 2 only.
6 *
7 * Copyright (c) 2010 by:
8 * Mauro Carvalho Chehab <mchehab@redhat.com>
9 *
10 * Red Hat Inc. http://www.redhat.com
11 *
12 * Intel 7300 Chipset Memory Controller Hub (MCH) - Datasheet
13 * http://www.intel.com/Assets/PDF/datasheet/318082.pdf
14 *
15 * TODO: The chipset allow checking for PCI Express errors also. Currently,
16 * the driver covers only memory error errors
17 *
18 * This driver uses "csrows" EDAC attribute to represent DIMM slot#
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/pci.h>
24#include <linux/pci_ids.h>
25#include <linux/slab.h>
26#include <linux/edac.h>
27#include <linux/mmzone.h>
28
29#include "edac_core.h"
30
31/*
32 * Alter this version for the I7300 module when modifications are made
33 */
34#define I7300_REVISION " Ver: 1.0.0 " __DATE__
35
36#define EDAC_MOD_STR "i7300_edac"
37
38#define i7300_printk(level, fmt, arg...) \
39 edac_printk(level, "i7300", fmt, ##arg)
40
41#define i7300_mc_printk(mci, level, fmt, arg...) \
42 edac_mc_chipset_printk(mci, level, "i7300", fmt, ##arg)
43
44/*
45 * Memory topology is organized as:
46 * Branch 0 - 2 channels: channels 0 and 1 (FDB0 PCI dev 21.0)
47 * Branch 1 - 2 channels: channels 2 and 3 (FDB1 PCI dev 22.0)
48 * Each channel can have to 8 DIMM sets (called as SLOTS)
49 * Slots should generally be filled in pairs
50 * Except on Single Channel mode of operation
51 * just slot 0/channel0 filled on this mode
52 * On normal operation mode, the two channels on a branch should be
c3af2eaf 53 * filled together for the same SLOT#
fcaf780b
MCC
54 * When in mirrored mode, Branch 1 replicate memory at Branch 0, so, the four
55 * channels on both branches should be filled
56 */
57
58/* Limits for i7300 */
59#define MAX_SLOTS 8
60#define MAX_BRANCHES 2
61#define MAX_CH_PER_BRANCH 2
62#define MAX_CHANNELS (MAX_CH_PER_BRANCH * MAX_BRANCHES)
63#define MAX_MIR 3
64
65#define to_channel(ch, branch) ((((branch)) << 1) | (ch))
66
67#define to_csrow(slot, ch, branch) \
68 (to_channel(ch, branch) | ((slot) << 2))
69
c3af2eaf
MCC
70/*
71 * I7300 devices
fcaf780b
MCC
72 * All 3 functions of Device 16 (0,1,2) share the SAME DID and
73 * uses PCI_DEVICE_ID_INTEL_I7300_MCH_ERR for device 16 (0,1,2),
74 * PCI_DEVICE_ID_INTEL_I7300_MCH_FB0 and PCI_DEVICE_ID_INTEL_I7300_MCH_FB1
75 * for device 21 (0,1).
c3af2eaf
MCC
76 */
77
78/****************************************************
79 * i7300 Register definitions for memory enumberation
80 ****************************************************/
81
82/*
83 * Device 16,
84 * Function 0: System Address (not documented)
85 * Function 1: Memory Branch Map, Control, Errors Register
fcaf780b
MCC
86 */
87
88 /* OFFSETS for Function 0 */
af3d8831
MCC
89#define AMBASE 0x48 /* AMB Mem Mapped Reg Region Base */
90#define MAXCH 0x56 /* Max Channel Number */
91#define MAXDIMMPERCH 0x57 /* Max DIMM PER Channel Number */
fcaf780b
MCC
92
93 /* OFFSETS for Function 1 */
af3d8831 94#define MC_SETTINGS 0x40
fcaf780b 95
af3d8831
MCC
96#define TOLM 0x6C
97#define REDMEMB 0x7C
98
99#define MIR0 0x80
100#define MIR1 0x84
101#define MIR2 0x88
fcaf780b 102
fcaf780b
MCC
103/*
104 * Note: Other Intel EDAC drivers use AMBPRESENT to identify if the available
105 * memory. From datasheet item 7.3.1 (FB-DIMM technology & organization), it
106 * seems that we cannot use this information directly for the same usage.
107 * Each memory slot may have up to 2 AMB interfaces, one for income and another
108 * for outcome interface to the next slot.
109 * For now, the driver just stores the AMB present registers, but rely only at
110 * the MTR info to detect memory.
111 * Datasheet is also not clear about how to map each AMBPRESENT registers to
112 * one of the 4 available channels.
113 */
114#define AMBPRESENT_0 0x64
115#define AMBPRESENT_1 0x66
116
117const static u16 mtr_regs [MAX_SLOTS] = {
118 0x80, 0x84, 0x88, 0x8c,
119 0x82, 0x86, 0x8a, 0x8e
120};
121
122/* Defines to extract the vaious fields from the
123 * MTRx - Memory Technology Registers
124 */
125#define MTR_DIMMS_PRESENT(mtr) ((mtr) & (1 << 8))
126#define MTR_DIMMS_ETHROTTLE(mtr) ((mtr) & (1 << 7))
127#define MTR_DRAM_WIDTH(mtr) (((mtr) & (1 << 6)) ? 8 : 4)
128#define MTR_DRAM_BANKS(mtr) (((mtr) & (1 << 5)) ? 8 : 4)
129#define MTR_DIMM_RANKS(mtr) (((mtr) & (1 << 4)) ? 1 : 0)
130#define MTR_DIMM_ROWS(mtr) (((mtr) >> 2) & 0x3)
131#define MTR_DRAM_BANKS_ADDR_BITS 2
132#define MTR_DIMM_ROWS_ADDR_BITS(mtr) (MTR_DIMM_ROWS(mtr) + 13)
133#define MTR_DIMM_COLS(mtr) ((mtr) & 0x3)
134#define MTR_DIMM_COLS_ADDR_BITS(mtr) (MTR_DIMM_COLS(mtr) + 10)
135
fcaf780b
MCC
136#ifdef CONFIG_EDAC_DEBUG
137/* MTR NUMROW */
138static const char *numrow_toString[] = {
139 "8,192 - 13 rows",
140 "16,384 - 14 rows",
141 "32,768 - 15 rows",
142 "65,536 - 16 rows"
143};
144
145/* MTR NUMCOL */
146static const char *numcol_toString[] = {
147 "1,024 - 10 columns",
148 "2,048 - 11 columns",
149 "4,096 - 12 columns",
150 "reserved"
151};
152#endif
153
c3af2eaf
MCC
154/************************************************
155 * i7300 Register definitions for error detection
156 ************************************************/
157/*
158 * Device 16.2: Global Error Registers
159 */
160
5de6e07e
MCC
161#define FERR_GLOBAL_HI 0x48
162static const char *ferr_global_hi_name[] = {
163 [3] = "FSB 3 Fatal Error",
164 [2] = "FSB 2 Fatal Error",
165 [1] = "FSB 1 Fatal Error",
166 [0] = "FSB 0 Fatal Error",
167};
168#define ferr_global_hi_is_fatal(errno) 1
169
c3af2eaf 170#define FERR_GLOBAL_LO 0x40
5de6e07e 171static const char *ferr_global_lo_name[] = {
c3af2eaf
MCC
172 [31] = "Internal MCH Fatal Error",
173 [30] = "Intel QuickData Technology Device Fatal Error",
174 [29] = "FSB1 Fatal Error",
175 [28] = "FSB0 Fatal Error",
176 [27] = "FBD Channel 3 Fatal Error",
177 [26] = "FBD Channel 2 Fatal Error",
178 [25] = "FBD Channel 1 Fatal Error",
179 [24] = "FBD Channel 0 Fatal Error",
180 [23] = "PCI Express Device 7Fatal Error",
181 [22] = "PCI Express Device 6 Fatal Error",
182 [21] = "PCI Express Device 5 Fatal Error",
183 [20] = "PCI Express Device 4 Fatal Error",
184 [19] = "PCI Express Device 3 Fatal Error",
185 [18] = "PCI Express Device 2 Fatal Error",
186 [17] = "PCI Express Device 1 Fatal Error",
187 [16] = "ESI Fatal Error",
188 [15] = "Internal MCH Non-Fatal Error",
189 [14] = "Intel QuickData Technology Device Non Fatal Error",
190 [13] = "FSB1 Non-Fatal Error",
191 [12] = "FSB 0 Non-Fatal Error",
192 [11] = "FBD Channel 3 Non-Fatal Error",
193 [10] = "FBD Channel 2 Non-Fatal Error",
194 [9] = "FBD Channel 1 Non-Fatal Error",
195 [8] = "FBD Channel 0 Non-Fatal Error",
196 [7] = "PCI Express Device 7 Non-Fatal Error",
197 [6] = "PCI Express Device 6 Non-Fatal Error",
198 [5] = "PCI Express Device 5 Non-Fatal Error",
199 [4] = "PCI Express Device 4 Non-Fatal Error",
200 [3] = "PCI Express Device 3 Non-Fatal Error",
201 [2] = "PCI Express Device 2 Non-Fatal Error",
202 [1] = "PCI Express Device 1 Non-Fatal Error",
203 [0] = "ESI Non-Fatal Error",
204};
5de6e07e 205#define ferr_global_lo_is_fatal(errno) ((errno < 16) ? 0 : 1)
fcaf780b
MCC
206
207/* Device name and register DID (Device ID) */
208struct i7300_dev_info {
209 const char *ctl_name; /* name for this device */
210 u16 fsb_mapping_errors; /* DID for the branchmap,control */
211};
212
213/* Table of devices attributes supported by this driver */
214static const struct i7300_dev_info i7300_devs[] = {
215 {
216 .ctl_name = "I7300",
217 .fsb_mapping_errors = PCI_DEVICE_ID_INTEL_I7300_MCH_ERR,
218 },
219};
220
221struct i7300_dimm_info {
222 int megabytes; /* size, 0 means not present */
223};
224
225/* driver private data structure */
226struct i7300_pvt {
3e57eef6
MCC
227 struct pci_dev *pci_dev_16_0_fsb_ctlr; /* 16.0 */
228 struct pci_dev *pci_dev_16_1_fsb_addr_map; /* 16.1 */
229 struct pci_dev *pci_dev_16_2_fsb_err_regs; /* 16.2 */
230 struct pci_dev *pci_dev_2x_0_fbd_branch[MAX_BRANCHES]; /* 21.0 and 22.0 */
fcaf780b
MCC
231
232 u16 tolm; /* top of low memory */
233 u64 ambase; /* AMB BAR */
af3d8831 234 u32 mc_settings;
fcaf780b
MCC
235
236 u16 mir[MAX_MIR];
237
238 u16 mtr[MAX_SLOTS][MAX_BRANCHES]; /* Memory Technlogy Reg */
239 u16 ambpresent[MAX_CHANNELS]; /* AMB present regs */
240
241 /* DIMM information matrix, allocating architecture maximums */
242 struct i7300_dimm_info dimm_info[MAX_SLOTS][MAX_CHANNELS];
243};
244
fcaf780b
MCC
245/* FIXME: Why do we need to have this static? */
246static struct edac_pci_ctl_info *i7300_pci;
247
5de6e07e
MCC
248/********************************************
249 * i7300 Functions related to error detection
250 ********************************************/
fcaf780b 251
5de6e07e
MCC
252struct i7300_error_info {
253 int dummy; /* FIXME */
254};
255
256const char *get_err_from_table(const char *table[], int size, int pos)
fcaf780b 257{
5de6e07e
MCC
258 if (pos >= size)
259 return "Reserved";
260
261 return table[pos];
fcaf780b
MCC
262}
263
5de6e07e
MCC
264#define GET_ERR_FROM_TABLE(table, pos) \
265 get_err_from_table(table, ARRAY_SIZE(table), pos)
266
fcaf780b
MCC
267/*
268 * i7300_get_error_info Retrieve the hardware error information from
269 * the hardware and cache it in the 'info'
270 * structure
271 */
272static void i7300_get_error_info(struct mem_ctl_info *mci,
273 struct i7300_error_info *info)
274{
fcaf780b
MCC
275}
276
277/*
5de6e07e
MCC
278 * i7300_process_error_global Retrieve the hardware error information from
279 * the hardware and cache it in the 'info'
280 * structure
fcaf780b 281 */
5de6e07e
MCC
282static void i7300_process_error_global(struct mem_ctl_info *mci,
283 struct i7300_error_info *info)
fcaf780b 284{
5de6e07e
MCC
285 struct i7300_pvt *pvt;
286 u32 errnum, value;
287 unsigned long errors;
288 const char *specific;
289 bool is_fatal;
fcaf780b 290
5de6e07e 291 pvt = mci->pvt_info;
fcaf780b 292
5de6e07e
MCC
293 /* read in the 1st FATAL error register */
294 pci_read_config_dword(pvt->pci_dev_16_2_fsb_err_regs,
295 FERR_GLOBAL_HI, &value);
296 if (unlikely(value)) {
297 errors = value;
298 errnum = find_first_bit(&errors,
299 ARRAY_SIZE(ferr_global_hi_name));
300 specific = GET_ERR_FROM_TABLE(ferr_global_hi_name, errnum);
301 is_fatal = ferr_global_hi_is_fatal(errnum);
302 goto error_global;
fcaf780b
MCC
303 }
304
5de6e07e
MCC
305 pci_read_config_dword(pvt->pci_dev_16_2_fsb_err_regs,
306 FERR_GLOBAL_LO, &value);
307 if (unlikely(value)) {
308 errors = value;
309 errnum = find_first_bit(&errors,
310 ARRAY_SIZE(ferr_global_lo_name));
311 specific = GET_ERR_FROM_TABLE(ferr_global_lo_name, errnum);
312 is_fatal = ferr_global_lo_is_fatal(errnum);
313 goto error_global;
314 }
315 return;
fcaf780b 316
5de6e07e
MCC
317error_global:
318 i7300_mc_printk(mci, KERN_EMERG, "%s misc error: %s\n",
319 is_fatal ? "Fatal" : "NOT fatal", specific);
fcaf780b
MCC
320}
321
322/*
5de6e07e
MCC
323 * i7300_process_error_info Retrieve the hardware error information from
324 * the hardware and cache it in the 'info'
325 * structure
fcaf780b
MCC
326 */
327static void i7300_process_error_info(struct mem_ctl_info *mci,
5de6e07e
MCC
328 struct i7300_error_info *info)
329{
330 i7300_process_error_global(mci, info);
331};
fcaf780b
MCC
332
333/*
334 * i7300_clear_error Retrieve any error from the hardware
335 * but do NOT process that error.
336 * Used for 'clearing' out of previous errors
337 * Called by the Core module.
338 */
339static void i7300_clear_error(struct mem_ctl_info *mci)
340{
341 struct i7300_error_info info;
342
343 i7300_get_error_info(mci, &info);
344}
345
346/*
347 * i7300_check_error Retrieve and process errors reported by the
348 * hardware. Called by the Core module.
349 */
350static void i7300_check_error(struct mem_ctl_info *mci)
351{
352 struct i7300_error_info info;
353 debugf4("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
5de6e07e 354
fcaf780b
MCC
355 i7300_get_error_info(mci, &info);
356 i7300_process_error_info(mci, &info);
357}
358
359/*
360 * i7300_enable_error_reporting
361 * Turn on the memory reporting features of the hardware
362 */
363static void i7300_enable_error_reporting(struct mem_ctl_info *mci)
364{
fcaf780b 365}
5de6e07e
MCC
366
367/************************************************
368 * i7300 Functions related to memory enumberation
369 ************************************************/
fcaf780b
MCC
370
371/*
372 * determine_mtr(pvt, csrow, channel)
373 *
374 * return the proper MTR register as determine by the csrow and desired channel
375 */
376static int decode_mtr(struct i7300_pvt *pvt,
377 int slot, int ch, int branch,
378 struct i7300_dimm_info *dinfo,
379 struct csrow_info *p_csrow)
380{
381 int mtr, ans, addrBits, channel;
382
383 channel = to_channel(ch, branch);
384
385 mtr = pvt->mtr[slot][branch];
386 ans = MTR_DIMMS_PRESENT(mtr) ? 1 : 0;
387
388 debugf2("\tMTR%d CH%d: DIMMs are %s (mtr)\n",
389 slot, channel,
390 ans ? "Present" : "NOT Present");
391
392 /* Determine if there is a DIMM present in this DIMM slot */
393
394#if 0
395 if (!amb_present || !ans)
396 return 0;
397#else
398 if (!ans)
399 return 0;
400#endif
401
402 /* Start with the number of bits for a Bank
403 * on the DRAM */
404 addrBits = MTR_DRAM_BANKS_ADDR_BITS;
405 /* Add thenumber of ROW bits */
406 addrBits += MTR_DIMM_ROWS_ADDR_BITS(mtr);
407 /* add the number of COLUMN bits */
408 addrBits += MTR_DIMM_COLS_ADDR_BITS(mtr);
409 /* add the number of RANK bits */
410 addrBits += MTR_DIMM_RANKS(mtr);
411
412 addrBits += 6; /* add 64 bits per DIMM */
413 addrBits -= 20; /* divide by 2^^20 */
414 addrBits -= 3; /* 8 bits per bytes */
415
416 dinfo->megabytes = 1 << addrBits;
417
418 debugf2("\t\tWIDTH: x%d\n", MTR_DRAM_WIDTH(mtr));
419
420 debugf2("\t\tELECTRICAL THROTTLING is %s\n",
421 MTR_DIMMS_ETHROTTLE(mtr) ? "enabled" : "disabled");
422
423 debugf2("\t\tNUMBANK: %d bank(s)\n", MTR_DRAM_BANKS(mtr));
424 debugf2("\t\tNUMRANK: %s\n", MTR_DIMM_RANKS(mtr) ? "double" : "single");
425 debugf2("\t\tNUMROW: %s\n", numrow_toString[MTR_DIMM_ROWS(mtr)]);
426 debugf2("\t\tNUMCOL: %s\n", numcol_toString[MTR_DIMM_COLS(mtr)]);
427 debugf2("\t\tSIZE: %d MB\n", dinfo->megabytes);
428
429 p_csrow->grain = 8;
430 p_csrow->nr_pages = dinfo->megabytes << 8;
431 p_csrow->mtype = MEM_FB_DDR2;
116389ed
MCC
432
433 /*
434 * FIXME: the type of error detection actually depends of the
435 * mode of operation. When it is just one single memory chip, at
436 * socket 0, channel 0, it uses 8-byte-over-32-byte SECDED+ code.
437 * In normal or mirrored mode, it uses Single Device Data correction,
438 * with the possibility of using an extended algorithm for x8 memories
439 * See datasheet Sections 7.3.6 to 7.3.8
440 */
fcaf780b
MCC
441 p_csrow->edac_mode = EDAC_S8ECD8ED;
442
443 /* ask what device type on this row */
444 if (MTR_DRAM_WIDTH(mtr))
445 p_csrow->dtype = DEV_X8;
446 else
447 p_csrow->dtype = DEV_X4;
448
449 return mtr;
450}
451
452/*
453 * print_dimm_size
454 *
455 * also will output a DIMM matrix map, if debug is enabled, for viewing
456 * how the DIMMs are populated
457 */
458static void print_dimm_size(struct i7300_pvt *pvt)
459{
460 struct i7300_dimm_info *dinfo;
461 char *p, *mem_buffer;
462 int space, n;
463 int channel, slot;
464
465 space = PAGE_SIZE;
466 mem_buffer = p = kmalloc(space, GFP_KERNEL);
467 if (p == NULL) {
468 i7300_printk(KERN_ERR, "MC: %s:%s() kmalloc() failed\n",
469 __FILE__, __func__);
470 return;
471 }
472
473 n = snprintf(p, space, " ");
474 p += n;
475 space -= n;
476 for (channel = 0; channel < MAX_CHANNELS; channel++) {
477 n = snprintf(p, space, "channel %d | ", channel);
478 p += n;
479 space -= n;
480 }
481 debugf2("%s\n", mem_buffer);
482 p = mem_buffer;
483 space = PAGE_SIZE;
484 n = snprintf(p, space, "-------------------------------"
485 "------------------------------");
486 p += n;
487 space -= n;
488 debugf2("%s\n", mem_buffer);
489 p = mem_buffer;
490 space = PAGE_SIZE;
491
492 for (slot = 0; slot < MAX_SLOTS; slot++) {
493 n = snprintf(p, space, "csrow/SLOT %d ", slot);
494 p += n;
495 space -= n;
496
497 for (channel = 0; channel < MAX_CHANNELS; channel++) {
498 dinfo = &pvt->dimm_info[slot][channel];
499 n = snprintf(p, space, "%4d MB | ", dinfo->megabytes);
500 p += n;
501 space -= n;
502 }
503
504 debugf2("%s\n", mem_buffer);
505 p = mem_buffer;
506 space = PAGE_SIZE;
507 }
508
509 n = snprintf(p, space, "-------------------------------"
510 "------------------------------");
511 p += n;
512 space -= n;
513 debugf2("%s\n", mem_buffer);
514 p = mem_buffer;
515 space = PAGE_SIZE;
516
517 kfree(mem_buffer);
518}
519
520/*
521 * i7300_init_csrows Initialize the 'csrows' table within
522 * the mci control structure with the
523 * addressing of memory.
524 *
525 * return:
526 * 0 success
527 * 1 no actual memory found on this MC
528 */
529static int i7300_init_csrows(struct mem_ctl_info *mci)
530{
531 struct i7300_pvt *pvt;
532 struct i7300_dimm_info *dinfo;
533 struct csrow_info *p_csrow;
534 int empty;
535 int mtr;
536 int ch, branch, slot, channel;
537
538 pvt = mci->pvt_info;
539
540 empty = 1; /* Assume NO memory */
541
542 debugf2("Memory Technology Registers:\n");
543
544 /* Get the AMB present registers for the four channels */
545 for (branch = 0; branch < MAX_BRANCHES; branch++) {
546 /* Read and dump branch 0's MTRs */
547 channel = to_channel(0, branch);
3e57eef6 548 pci_read_config_word(pvt->pci_dev_2x_0_fbd_branch[branch], AMBPRESENT_0,
fcaf780b
MCC
549 &pvt->ambpresent[channel]);
550 debugf2("\t\tAMB-present CH%d = 0x%x:\n",
551 channel, pvt->ambpresent[channel]);
552
553 channel = to_channel(1, branch);
3e57eef6 554 pci_read_config_word(pvt->pci_dev_2x_0_fbd_branch[branch], AMBPRESENT_1,
fcaf780b
MCC
555 &pvt->ambpresent[channel]);
556 debugf2("\t\tAMB-present CH%d = 0x%x:\n",
557 channel, pvt->ambpresent[channel]);
558 }
559
560 /* Get the set of MTR[0-7] regs by each branch */
561 for (slot = 0; slot < MAX_SLOTS; slot++) {
562 int where = mtr_regs[slot];
563 for (branch = 0; branch < MAX_BRANCHES; branch++) {
3e57eef6 564 pci_read_config_word(pvt->pci_dev_2x_0_fbd_branch[branch],
fcaf780b
MCC
565 where,
566 &pvt->mtr[slot][branch]);
567 for (ch = 0; ch < MAX_BRANCHES; ch++) {
568 int channel = to_channel(ch, branch);
569
570 dinfo = &pvt->dimm_info[slot][channel];
571 p_csrow = &mci->csrows[slot];
572
573 mtr = decode_mtr(pvt, slot, ch, branch,
574 dinfo, p_csrow);
575 /* if no DIMMS on this row, continue */
576 if (!MTR_DIMMS_PRESENT(mtr))
577 continue;
578
579 p_csrow->csrow_idx = slot;
580
581 /* FAKE OUT VALUES, FIXME */
582 p_csrow->first_page = 0 + slot * 20;
583 p_csrow->last_page = 9 + slot * 20;
584 p_csrow->page_mask = 0xfff;
585
586 empty = 0;
587 }
588 }
589 }
590
591 return empty;
592}
593
594static void decode_mir(int mir_no, u16 mir[MAX_MIR])
595{
596 if (mir[mir_no] & 3)
597 debugf2("MIR%d: limit= 0x%x Branch(es) that participate: %s %s\n",
598 mir_no,
599 (mir[mir_no] >> 4) & 0xfff,
600 (mir[mir_no] & 1) ? "B0" : "",
601 (mir[mir_no] & 2) ? "B1": "");
602}
603
604/*
605 * i7300_get_mc_regs read in the necessary registers and
606 * cache locally
607 *
608 * Fills in the private data members
609 */
610static int i7300_get_mc_regs(struct mem_ctl_info *mci)
611{
612 struct i7300_pvt *pvt;
613 u32 actual_tolm;
614 int i, rc;
615
616 pvt = mci->pvt_info;
617
3e57eef6 618 pci_read_config_dword(pvt->pci_dev_16_0_fsb_ctlr, AMBASE,
fcaf780b
MCC
619 (u32 *) &pvt->ambase);
620
621 debugf2("AMBASE= 0x%lx\n", (long unsigned int)pvt->ambase);
622
623 /* Get the Branch Map regs */
3e57eef6 624 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, TOLM, &pvt->tolm);
fcaf780b
MCC
625 pvt->tolm >>= 12;
626 debugf2("TOLM (number of 256M regions) =%u (0x%x)\n", pvt->tolm,
627 pvt->tolm);
628
629 actual_tolm = (u32) ((1000l * pvt->tolm) >> (30 - 28));
630 debugf2("Actual TOLM byte addr=%u.%03u GB (0x%x)\n",
631 actual_tolm/1000, actual_tolm % 1000, pvt->tolm << 28);
632
af3d8831 633 /* Get memory controller settings */
3e57eef6 634 pci_read_config_dword(pvt->pci_dev_16_1_fsb_addr_map, MC_SETTINGS,
af3d8831
MCC
635 &pvt->mc_settings);
636 debugf0("Memory controller operating on %s mode\n",
637 pvt->mc_settings & (1 << 16)? "mirrored" : "non-mirrored");
638 debugf0("Error detection is %s\n",
639 pvt->mc_settings & (1 << 5)? "enabled" : "disabled");
640
641 /* Get Memory Interleave Range registers */
3e57eef6
MCC
642 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, MIR0, &pvt->mir[0]);
643 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, MIR1, &pvt->mir[1]);
644 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, MIR2, &pvt->mir[2]);
fcaf780b
MCC
645
646 /* Decode the MIR regs */
647 for (i = 0; i < MAX_MIR; i++)
648 decode_mir(i, pvt->mir);
649
650 rc = i7300_init_csrows(mci);
651 if (rc < 0)
652 return rc;
653
654 /* Go and determine the size of each DIMM and place in an
655 * orderly matrix */
656 print_dimm_size(pvt);
657
658 return 0;
659}
660
5de6e07e
MCC
661/*************************************************
662 * i7300 Functions related to device probe/release
663 *************************************************/
664
fcaf780b
MCC
665/*
666 * i7300_put_devices 'put' all the devices that we have
667 * reserved via 'get'
668 */
669static void i7300_put_devices(struct mem_ctl_info *mci)
670{
671 struct i7300_pvt *pvt;
672 int branch;
673
674 pvt = mci->pvt_info;
675
676 /* Decrement usage count for devices */
677 for (branch = 0; branch < MAX_CH_PER_BRANCH; branch++)
3e57eef6
MCC
678 pci_dev_put(pvt->pci_dev_2x_0_fbd_branch[branch]);
679 pci_dev_put(pvt->pci_dev_16_2_fsb_err_regs);
680 pci_dev_put(pvt->pci_dev_16_1_fsb_addr_map);
fcaf780b
MCC
681}
682
683/*
684 * i7300_get_devices Find and perform 'get' operation on the MCH's
685 * device/functions we want to reference for this driver
686 *
687 * Need to 'get' device 16 func 1 and func 2
688 */
689static int i7300_get_devices(struct mem_ctl_info *mci, int dev_idx)
690{
691 struct i7300_pvt *pvt;
692 struct pci_dev *pdev;
693
694 pvt = mci->pvt_info;
695
696 /* Attempt to 'get' the MCH register we want */
697 pdev = NULL;
3e57eef6 698 while (!pvt->pci_dev_16_1_fsb_addr_map || !pvt->pci_dev_16_2_fsb_err_regs) {
fcaf780b
MCC
699 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
700 PCI_DEVICE_ID_INTEL_I7300_MCH_ERR, pdev);
701 if (!pdev) {
702 /* End of list, leave */
703 i7300_printk(KERN_ERR,
704 "'system address,Process Bus' "
705 "device not found:"
706 "vendor 0x%x device 0x%x ERR funcs "
707 "(broken BIOS?)\n",
708 PCI_VENDOR_ID_INTEL,
709 PCI_DEVICE_ID_INTEL_I7300_MCH_ERR);
710 goto error;
711 }
712
713 /* Store device 16 funcs 1 and 2 */
714 switch (PCI_FUNC(pdev->devfn)) {
715 case 1:
3e57eef6 716 pvt->pci_dev_16_1_fsb_addr_map = pdev;
fcaf780b
MCC
717 break;
718 case 2:
3e57eef6 719 pvt->pci_dev_16_2_fsb_err_regs = pdev;
fcaf780b
MCC
720 break;
721 }
722 }
723
724 debugf1("System Address, processor bus- PCI Bus ID: %s %x:%x\n",
3e57eef6
MCC
725 pci_name(pvt->pci_dev_16_0_fsb_ctlr),
726 pvt->pci_dev_16_0_fsb_ctlr->vendor, pvt->pci_dev_16_0_fsb_ctlr->device);
fcaf780b 727 debugf1("Branchmap, control and errors - PCI Bus ID: %s %x:%x\n",
3e57eef6
MCC
728 pci_name(pvt->pci_dev_16_1_fsb_addr_map),
729 pvt->pci_dev_16_1_fsb_addr_map->vendor, pvt->pci_dev_16_1_fsb_addr_map->device);
fcaf780b 730 debugf1("FSB Error Regs - PCI Bus ID: %s %x:%x\n",
3e57eef6
MCC
731 pci_name(pvt->pci_dev_16_2_fsb_err_regs),
732 pvt->pci_dev_16_2_fsb_err_regs->vendor, pvt->pci_dev_16_2_fsb_err_regs->device);
fcaf780b 733
3e57eef6 734 pvt->pci_dev_2x_0_fbd_branch[0] = pci_get_device(PCI_VENDOR_ID_INTEL,
fcaf780b
MCC
735 PCI_DEVICE_ID_INTEL_I7300_MCH_FB0,
736 NULL);
3e57eef6 737 if (!pvt->pci_dev_2x_0_fbd_branch[0]) {
fcaf780b
MCC
738 i7300_printk(KERN_ERR,
739 "MC: 'BRANCH 0' device not found:"
740 "vendor 0x%x device 0x%x Func 0 (broken BIOS?)\n",
741 PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7300_MCH_FB0);
742 goto error;
743 }
744
3e57eef6 745 pvt->pci_dev_2x_0_fbd_branch[1] = pci_get_device(PCI_VENDOR_ID_INTEL,
fcaf780b
MCC
746 PCI_DEVICE_ID_INTEL_I7300_MCH_FB1,
747 NULL);
3e57eef6 748 if (!pvt->pci_dev_2x_0_fbd_branch[1]) {
fcaf780b
MCC
749 i7300_printk(KERN_ERR,
750 "MC: 'BRANCH 1' device not found:"
751 "vendor 0x%x device 0x%x Func 0 "
752 "(broken BIOS?)\n",
753 PCI_VENDOR_ID_INTEL,
754 PCI_DEVICE_ID_INTEL_I7300_MCH_FB1);
755 goto error;
756 }
757
758 return 0;
759
760error:
761 i7300_put_devices(mci);
762 return -ENODEV;
763}
764
765/*
766 * i7300_probe1 Probe for ONE instance of device to see if it is
767 * present.
768 * return:
769 * 0 for FOUND a device
770 * < 0 for error code
771 */
772static int i7300_probe1(struct pci_dev *pdev, int dev_idx)
773{
774 struct mem_ctl_info *mci;
775 struct i7300_pvt *pvt;
776 int num_channels;
777 int num_dimms_per_channel;
778 int num_csrows;
779
780 if (dev_idx >= ARRAY_SIZE(i7300_devs))
781 return -EINVAL;
782
783 debugf0("MC: " __FILE__ ": %s(), pdev bus %u dev=0x%x fn=0x%x\n",
784 __func__,
785 pdev->bus->number,
786 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
787
788 /* We only are looking for func 0 of the set */
789 if (PCI_FUNC(pdev->devfn) != 0)
790 return -ENODEV;
791
792 /* As we don't have a motherboard identification routine to determine
793 * actual number of slots/dimms per channel, we thus utilize the
794 * resource as specified by the chipset. Thus, we might have
795 * have more DIMMs per channel than actually on the mobo, but this
796 * allows the driver to support upto the chipset max, without
797 * some fancy mobo determination.
798 */
799 num_dimms_per_channel = MAX_SLOTS;
800 num_channels = MAX_CHANNELS;
801 num_csrows = MAX_SLOTS * MAX_CHANNELS;
802
803 debugf0("MC: %s(): Number of - Channels= %d DIMMS= %d CSROWS= %d\n",
804 __func__, num_channels, num_dimms_per_channel, num_csrows);
805
806 /* allocate a new MC control structure */
807 mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels, 0);
808
809 if (mci == NULL)
810 return -ENOMEM;
811
812 debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
813
814 mci->dev = &pdev->dev; /* record ptr to the generic device */
815
816 pvt = mci->pvt_info;
3e57eef6 817 pvt->pci_dev_16_0_fsb_ctlr = pdev; /* Record this device in our private */
fcaf780b
MCC
818
819 /* 'get' the pci devices we want to reserve for our use */
820 if (i7300_get_devices(mci, dev_idx))
821 goto fail0;
822
823 mci->mc_idx = 0;
824 mci->mtype_cap = MEM_FLAG_FB_DDR2;
825 mci->edac_ctl_cap = EDAC_FLAG_NONE;
826 mci->edac_cap = EDAC_FLAG_NONE;
827 mci->mod_name = "i7300_edac.c";
828 mci->mod_ver = I7300_REVISION;
829 mci->ctl_name = i7300_devs[dev_idx].ctl_name;
830 mci->dev_name = pci_name(pdev);
831 mci->ctl_page_to_phys = NULL;
832
fcaf780b
MCC
833 /* Set the function pointer to an actual operation function */
834 mci->edac_check = i7300_check_error;
fcaf780b
MCC
835
836 /* initialize the MC control structure 'csrows' table
837 * with the mapping and control information */
838 if (i7300_get_mc_regs(mci)) {
839 debugf0("MC: Setting mci->edac_cap to EDAC_FLAG_NONE\n"
840 " because i7300_init_csrows() returned nonzero "
841 "value\n");
842 mci->edac_cap = EDAC_FLAG_NONE; /* no csrows found */
843 } else {
fcaf780b
MCC
844 debugf1("MC: Enable error reporting now\n");
845 i7300_enable_error_reporting(mci);
fcaf780b
MCC
846 }
847
848 /* add this new MC control structure to EDAC's list of MCs */
849 if (edac_mc_add_mc(mci)) {
850 debugf0("MC: " __FILE__
851 ": %s(): failed edac_mc_add_mc()\n", __func__);
852 /* FIXME: perhaps some code should go here that disables error
853 * reporting if we just enabled it
854 */
855 goto fail1;
856 }
857
fcaf780b 858 i7300_clear_error(mci);
fcaf780b
MCC
859
860 /* allocating generic PCI control info */
861 i7300_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
862 if (!i7300_pci) {
863 printk(KERN_WARNING
864 "%s(): Unable to create PCI control\n",
865 __func__);
866 printk(KERN_WARNING
867 "%s(): PCI error report via EDAC not setup\n",
868 __func__);
869 }
870
871 return 0;
872
873 /* Error exit unwinding stack */
874fail1:
875
876 i7300_put_devices(mci);
877
878fail0:
879 edac_mc_free(mci);
880 return -ENODEV;
881}
882
883/*
884 * i7300_init_one constructor for one instance of device
885 *
886 * returns:
887 * negative on error
888 * count (>= 0)
889 */
890static int __devinit i7300_init_one(struct pci_dev *pdev,
891 const struct pci_device_id *id)
892{
893 int rc;
894
895 debugf0("MC: " __FILE__ ": %s()\n", __func__);
896
897 /* wake up device */
898 rc = pci_enable_device(pdev);
899 if (rc == -EIO)
900 return rc;
901
902 /* now probe and enable the device */
903 return i7300_probe1(pdev, id->driver_data);
904}
905
906/*
907 * i7300_remove_one destructor for one instance of device
908 *
909 */
910static void __devexit i7300_remove_one(struct pci_dev *pdev)
911{
912 struct mem_ctl_info *mci;
913
914 debugf0(__FILE__ ": %s()\n", __func__);
915
916 if (i7300_pci)
917 edac_pci_release_generic_ctl(i7300_pci);
918
919 mci = edac_mc_del_mc(&pdev->dev);
920 if (!mci)
921 return;
922
923 /* retrieve references to resources, and free those resources */
924 i7300_put_devices(mci);
925
926 edac_mc_free(mci);
927}
928
929/*
930 * pci_device_id table for which devices we are looking for
931 *
932 * The "E500P" device is the first device supported.
933 */
934static const struct pci_device_id i7300_pci_tbl[] __devinitdata = {
935 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7300_MCH_ERR)},
936 {0,} /* 0 terminated list. */
937};
938
939MODULE_DEVICE_TABLE(pci, i7300_pci_tbl);
940
941/*
942 * i7300_driver pci_driver structure for this module
943 *
944 */
945static struct pci_driver i7300_driver = {
946 .name = "i7300_edac",
947 .probe = i7300_init_one,
948 .remove = __devexit_p(i7300_remove_one),
949 .id_table = i7300_pci_tbl,
950};
951
952/*
953 * i7300_init Module entry function
954 * Try to initialize this module for its devices
955 */
956static int __init i7300_init(void)
957{
958 int pci_rc;
959
960 debugf2("MC: " __FILE__ ": %s()\n", __func__);
961
962 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
963 opstate_init();
964
965 pci_rc = pci_register_driver(&i7300_driver);
966
967 return (pci_rc < 0) ? pci_rc : 0;
968}
969
970/*
971 * i7300_exit() Module exit function
972 * Unregister the driver
973 */
974static void __exit i7300_exit(void)
975{
976 debugf2("MC: " __FILE__ ": %s()\n", __func__);
977 pci_unregister_driver(&i7300_driver);
978}
979
980module_init(i7300_init);
981module_exit(i7300_exit);
982
983MODULE_LICENSE("GPL");
984MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
985MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
986MODULE_DESCRIPTION("MC Driver for Intel I7300 memory controllers - "
987 I7300_REVISION);
988
989module_param(edac_op_state, int, 0444);
990MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");