blob: f2f171d0356a6f54d3faf64af25af56f520992a9 [file] [log] [blame]
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -03001/*
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
Mauro Carvalho Chehabc3af2ea2010-08-26 19:54:51 -030053 * filled together for the same SLOT#
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -030054 * 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
Mauro Carvalho Chehabc3af2ea2010-08-26 19:54:51 -030070/*
71 * I7300 devices
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -030072 * 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).
76 */
77
Mauro Carvalho Chehabc3af2ea2010-08-26 19:54:51 -030078/****************************************************
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
86 */
87
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -030088 /* OFFSETS for Function 0 */
Mauro Carvalho Chehabaf3d8832010-08-26 20:58:45 -030089#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 */
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -030092
93 /* OFFSETS for Function 1 */
Mauro Carvalho Chehabaf3d8832010-08-26 20:58:45 -030094#define MC_SETTINGS 0x40
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -030095
Mauro Carvalho Chehabaf3d8832010-08-26 20:58:45 -030096#define TOLM 0x6C
97#define REDMEMB 0x7C
98
99#define MIR0 0x80
100#define MIR1 0x84
101#define MIR2 0x88
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300102
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300103/*
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
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300136#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
Mauro Carvalho Chehabc3af2ea2010-08-26 19:54:51 -0300154/************************************************
155 * i7300 Register definitions for error detection
156 ************************************************/
157/*
158 * Device 16.2: Global Error Registers
159 */
160
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300161#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
Mauro Carvalho Chehabc3af2ea2010-08-26 19:54:51 -0300170#define FERR_GLOBAL_LO 0x40
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300171static const char *ferr_global_lo_name[] = {
Mauro Carvalho Chehabc3af2ea2010-08-26 19:54:51 -0300172 [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};
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300205#define ferr_global_lo_is_fatal(errno) ((errno < 16) ? 0 : 1)
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300206
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 {
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300227 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 */
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300231
232 u16 tolm; /* top of low memory */
233 u64 ambase; /* AMB BAR */
Mauro Carvalho Chehabaf3d8832010-08-26 20:58:45 -0300234 u32 mc_settings;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300235
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
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300245/* FIXME: Why do we need to have this static? */
246static struct edac_pci_ctl_info *i7300_pci;
247
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300248/********************************************
249 * i7300 Functions related to error detection
250 ********************************************/
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300251
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300252struct i7300_error_info {
253 int dummy; /* FIXME */
254};
255
256const char *get_err_from_table(const char *table[], int size, int pos)
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300257{
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300258 if (pos >= size)
259 return "Reserved";
260
261 return table[pos];
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300262}
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300263
264#define GET_ERR_FROM_TABLE(table, pos) \
265 get_err_from_table(table, ARRAY_SIZE(table), pos)
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300266
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{
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300275}
276
277/*
278 * i7300_process_error_global Retrieve the hardware error information from
279 * the hardware and cache it in the 'info'
280 * structure
281 */
282static void i7300_process_error_global(struct mem_ctl_info *mci,
283 struct i7300_error_info *info)
284{
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300285 struct i7300_pvt *pvt;
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300286 u32 errnum, value;
287 unsigned long errors;
288 const char *specific;
289 bool is_fatal;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300290
291 pvt = mci->pvt_info;
292
293 /* read in the 1st FATAL error register */
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300294 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);
Mauro Carvalho Chehab86002322010-08-27 00:46:57 -0300302
303 /* Clear the error bit */
304 pci_write_config_dword(pvt->pci_dev_16_2_fsb_err_regs,
305 FERR_GLOBAL_HI, value);
306
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300307 goto error_global;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300308 }
309
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300310 pci_read_config_dword(pvt->pci_dev_16_2_fsb_err_regs,
311 FERR_GLOBAL_LO, &value);
312 if (unlikely(value)) {
313 errors = value;
314 errnum = find_first_bit(&errors,
315 ARRAY_SIZE(ferr_global_lo_name));
316 specific = GET_ERR_FROM_TABLE(ferr_global_lo_name, errnum);
317 is_fatal = ferr_global_lo_is_fatal(errnum);
Mauro Carvalho Chehab86002322010-08-27 00:46:57 -0300318
319 /* Clear the error bit */
320 pci_write_config_dword(pvt->pci_dev_16_2_fsb_err_regs,
321 FERR_GLOBAL_LO, value);
322
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300323 goto error_global;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300324 }
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300325 return;
326
327error_global:
328 i7300_mc_printk(mci, KERN_EMERG, "%s misc error: %s\n",
329 is_fatal ? "Fatal" : "NOT fatal", specific);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300330}
331
332/*
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300333 * i7300_process_error_info Retrieve the hardware error information from
334 * the hardware and cache it in the 'info'
335 * structure
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300336 */
337static void i7300_process_error_info(struct mem_ctl_info *mci,
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300338 struct i7300_error_info *info)
339{
340 i7300_process_error_global(mci, info);
341};
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300342
343/*
344 * i7300_clear_error Retrieve any error from the hardware
345 * but do NOT process that error.
346 * Used for 'clearing' out of previous errors
347 * Called by the Core module.
348 */
349static void i7300_clear_error(struct mem_ctl_info *mci)
350{
351 struct i7300_error_info info;
352
353 i7300_get_error_info(mci, &info);
354}
355
356/*
357 * i7300_check_error Retrieve and process errors reported by the
358 * hardware. Called by the Core module.
359 */
360static void i7300_check_error(struct mem_ctl_info *mci)
361{
362 struct i7300_error_info info;
363 debugf4("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300364
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300365 i7300_get_error_info(mci, &info);
366 i7300_process_error_info(mci, &info);
367}
368
369/*
370 * i7300_enable_error_reporting
371 * Turn on the memory reporting features of the hardware
372 */
373static void i7300_enable_error_reporting(struct mem_ctl_info *mci)
374{
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300375}
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300376
377/************************************************
378 * i7300 Functions related to memory enumberation
379 ************************************************/
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300380
381/*
382 * determine_mtr(pvt, csrow, channel)
383 *
384 * return the proper MTR register as determine by the csrow and desired channel
385 */
386static int decode_mtr(struct i7300_pvt *pvt,
387 int slot, int ch, int branch,
388 struct i7300_dimm_info *dinfo,
389 struct csrow_info *p_csrow)
390{
391 int mtr, ans, addrBits, channel;
392
393 channel = to_channel(ch, branch);
394
395 mtr = pvt->mtr[slot][branch];
396 ans = MTR_DIMMS_PRESENT(mtr) ? 1 : 0;
397
398 debugf2("\tMTR%d CH%d: DIMMs are %s (mtr)\n",
399 slot, channel,
400 ans ? "Present" : "NOT Present");
401
402 /* Determine if there is a DIMM present in this DIMM slot */
403
404#if 0
405 if (!amb_present || !ans)
406 return 0;
407#else
408 if (!ans)
409 return 0;
410#endif
411
412 /* Start with the number of bits for a Bank
413 * on the DRAM */
414 addrBits = MTR_DRAM_BANKS_ADDR_BITS;
415 /* Add thenumber of ROW bits */
416 addrBits += MTR_DIMM_ROWS_ADDR_BITS(mtr);
417 /* add the number of COLUMN bits */
418 addrBits += MTR_DIMM_COLS_ADDR_BITS(mtr);
419 /* add the number of RANK bits */
420 addrBits += MTR_DIMM_RANKS(mtr);
421
422 addrBits += 6; /* add 64 bits per DIMM */
423 addrBits -= 20; /* divide by 2^^20 */
424 addrBits -= 3; /* 8 bits per bytes */
425
426 dinfo->megabytes = 1 << addrBits;
427
428 debugf2("\t\tWIDTH: x%d\n", MTR_DRAM_WIDTH(mtr));
429
430 debugf2("\t\tELECTRICAL THROTTLING is %s\n",
431 MTR_DIMMS_ETHROTTLE(mtr) ? "enabled" : "disabled");
432
433 debugf2("\t\tNUMBANK: %d bank(s)\n", MTR_DRAM_BANKS(mtr));
434 debugf2("\t\tNUMRANK: %s\n", MTR_DIMM_RANKS(mtr) ? "double" : "single");
435 debugf2("\t\tNUMROW: %s\n", numrow_toString[MTR_DIMM_ROWS(mtr)]);
436 debugf2("\t\tNUMCOL: %s\n", numcol_toString[MTR_DIMM_COLS(mtr)]);
437 debugf2("\t\tSIZE: %d MB\n", dinfo->megabytes);
438
439 p_csrow->grain = 8;
440 p_csrow->nr_pages = dinfo->megabytes << 8;
441 p_csrow->mtype = MEM_FB_DDR2;
Mauro Carvalho Chehab116389e2010-08-26 23:19:54 -0300442
443 /*
444 * FIXME: the type of error detection actually depends of the
445 * mode of operation. When it is just one single memory chip, at
446 * socket 0, channel 0, it uses 8-byte-over-32-byte SECDED+ code.
447 * In normal or mirrored mode, it uses Single Device Data correction,
448 * with the possibility of using an extended algorithm for x8 memories
449 * See datasheet Sections 7.3.6 to 7.3.8
450 */
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300451 p_csrow->edac_mode = EDAC_S8ECD8ED;
452
453 /* ask what device type on this row */
454 if (MTR_DRAM_WIDTH(mtr))
455 p_csrow->dtype = DEV_X8;
456 else
457 p_csrow->dtype = DEV_X4;
458
459 return mtr;
460}
461
462/*
463 * print_dimm_size
464 *
465 * also will output a DIMM matrix map, if debug is enabled, for viewing
466 * how the DIMMs are populated
467 */
468static void print_dimm_size(struct i7300_pvt *pvt)
469{
470 struct i7300_dimm_info *dinfo;
471 char *p, *mem_buffer;
472 int space, n;
473 int channel, slot;
474
475 space = PAGE_SIZE;
476 mem_buffer = p = kmalloc(space, GFP_KERNEL);
477 if (p == NULL) {
478 i7300_printk(KERN_ERR, "MC: %s:%s() kmalloc() failed\n",
479 __FILE__, __func__);
480 return;
481 }
482
483 n = snprintf(p, space, " ");
484 p += n;
485 space -= n;
486 for (channel = 0; channel < MAX_CHANNELS; channel++) {
487 n = snprintf(p, space, "channel %d | ", channel);
488 p += n;
489 space -= n;
490 }
491 debugf2("%s\n", mem_buffer);
492 p = mem_buffer;
493 space = PAGE_SIZE;
494 n = snprintf(p, space, "-------------------------------"
495 "------------------------------");
496 p += n;
497 space -= n;
498 debugf2("%s\n", mem_buffer);
499 p = mem_buffer;
500 space = PAGE_SIZE;
501
502 for (slot = 0; slot < MAX_SLOTS; slot++) {
503 n = snprintf(p, space, "csrow/SLOT %d ", slot);
504 p += n;
505 space -= n;
506
507 for (channel = 0; channel < MAX_CHANNELS; channel++) {
508 dinfo = &pvt->dimm_info[slot][channel];
509 n = snprintf(p, space, "%4d MB | ", dinfo->megabytes);
510 p += n;
511 space -= n;
512 }
513
514 debugf2("%s\n", mem_buffer);
515 p = mem_buffer;
516 space = PAGE_SIZE;
517 }
518
519 n = snprintf(p, space, "-------------------------------"
520 "------------------------------");
521 p += n;
522 space -= n;
523 debugf2("%s\n", mem_buffer);
524 p = mem_buffer;
525 space = PAGE_SIZE;
526
527 kfree(mem_buffer);
528}
529
530/*
531 * i7300_init_csrows Initialize the 'csrows' table within
532 * the mci control structure with the
533 * addressing of memory.
534 *
535 * return:
536 * 0 success
537 * 1 no actual memory found on this MC
538 */
539static int i7300_init_csrows(struct mem_ctl_info *mci)
540{
541 struct i7300_pvt *pvt;
542 struct i7300_dimm_info *dinfo;
543 struct csrow_info *p_csrow;
544 int empty;
545 int mtr;
546 int ch, branch, slot, channel;
547
548 pvt = mci->pvt_info;
549
550 empty = 1; /* Assume NO memory */
551
552 debugf2("Memory Technology Registers:\n");
553
554 /* Get the AMB present registers for the four channels */
555 for (branch = 0; branch < MAX_BRANCHES; branch++) {
556 /* Read and dump branch 0's MTRs */
557 channel = to_channel(0, branch);
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300558 pci_read_config_word(pvt->pci_dev_2x_0_fbd_branch[branch], AMBPRESENT_0,
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300559 &pvt->ambpresent[channel]);
560 debugf2("\t\tAMB-present CH%d = 0x%x:\n",
561 channel, pvt->ambpresent[channel]);
562
563 channel = to_channel(1, branch);
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300564 pci_read_config_word(pvt->pci_dev_2x_0_fbd_branch[branch], AMBPRESENT_1,
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300565 &pvt->ambpresent[channel]);
566 debugf2("\t\tAMB-present CH%d = 0x%x:\n",
567 channel, pvt->ambpresent[channel]);
568 }
569
570 /* Get the set of MTR[0-7] regs by each branch */
571 for (slot = 0; slot < MAX_SLOTS; slot++) {
572 int where = mtr_regs[slot];
573 for (branch = 0; branch < MAX_BRANCHES; branch++) {
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300574 pci_read_config_word(pvt->pci_dev_2x_0_fbd_branch[branch],
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300575 where,
576 &pvt->mtr[slot][branch]);
577 for (ch = 0; ch < MAX_BRANCHES; ch++) {
578 int channel = to_channel(ch, branch);
579
580 dinfo = &pvt->dimm_info[slot][channel];
581 p_csrow = &mci->csrows[slot];
582
583 mtr = decode_mtr(pvt, slot, ch, branch,
584 dinfo, p_csrow);
585 /* if no DIMMS on this row, continue */
586 if (!MTR_DIMMS_PRESENT(mtr))
587 continue;
588
589 p_csrow->csrow_idx = slot;
590
591 /* FAKE OUT VALUES, FIXME */
592 p_csrow->first_page = 0 + slot * 20;
593 p_csrow->last_page = 9 + slot * 20;
594 p_csrow->page_mask = 0xfff;
595
596 empty = 0;
597 }
598 }
599 }
600
601 return empty;
602}
603
604static void decode_mir(int mir_no, u16 mir[MAX_MIR])
605{
606 if (mir[mir_no] & 3)
607 debugf2("MIR%d: limit= 0x%x Branch(es) that participate: %s %s\n",
608 mir_no,
609 (mir[mir_no] >> 4) & 0xfff,
610 (mir[mir_no] & 1) ? "B0" : "",
611 (mir[mir_no] & 2) ? "B1": "");
612}
613
614/*
615 * i7300_get_mc_regs read in the necessary registers and
616 * cache locally
617 *
618 * Fills in the private data members
619 */
620static int i7300_get_mc_regs(struct mem_ctl_info *mci)
621{
622 struct i7300_pvt *pvt;
623 u32 actual_tolm;
624 int i, rc;
625
626 pvt = mci->pvt_info;
627
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300628 pci_read_config_dword(pvt->pci_dev_16_0_fsb_ctlr, AMBASE,
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300629 (u32 *) &pvt->ambase);
630
631 debugf2("AMBASE= 0x%lx\n", (long unsigned int)pvt->ambase);
632
633 /* Get the Branch Map regs */
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300634 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, TOLM, &pvt->tolm);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300635 pvt->tolm >>= 12;
636 debugf2("TOLM (number of 256M regions) =%u (0x%x)\n", pvt->tolm,
637 pvt->tolm);
638
639 actual_tolm = (u32) ((1000l * pvt->tolm) >> (30 - 28));
640 debugf2("Actual TOLM byte addr=%u.%03u GB (0x%x)\n",
641 actual_tolm/1000, actual_tolm % 1000, pvt->tolm << 28);
642
Mauro Carvalho Chehabaf3d8832010-08-26 20:58:45 -0300643 /* Get memory controller settings */
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300644 pci_read_config_dword(pvt->pci_dev_16_1_fsb_addr_map, MC_SETTINGS,
Mauro Carvalho Chehabaf3d8832010-08-26 20:58:45 -0300645 &pvt->mc_settings);
646 debugf0("Memory controller operating on %s mode\n",
647 pvt->mc_settings & (1 << 16)? "mirrored" : "non-mirrored");
648 debugf0("Error detection is %s\n",
649 pvt->mc_settings & (1 << 5)? "enabled" : "disabled");
650
651 /* Get Memory Interleave Range registers */
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300652 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, MIR0, &pvt->mir[0]);
653 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, MIR1, &pvt->mir[1]);
654 pci_read_config_word(pvt->pci_dev_16_1_fsb_addr_map, MIR2, &pvt->mir[2]);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300655
656 /* Decode the MIR regs */
657 for (i = 0; i < MAX_MIR; i++)
658 decode_mir(i, pvt->mir);
659
660 rc = i7300_init_csrows(mci);
661 if (rc < 0)
662 return rc;
663
664 /* Go and determine the size of each DIMM and place in an
665 * orderly matrix */
666 print_dimm_size(pvt);
667
668 return 0;
669}
670
Mauro Carvalho Chehab5de6e072010-08-27 00:16:12 -0300671/*************************************************
672 * i7300 Functions related to device probe/release
673 *************************************************/
674
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300675/*
676 * i7300_put_devices 'put' all the devices that we have
677 * reserved via 'get'
678 */
679static void i7300_put_devices(struct mem_ctl_info *mci)
680{
681 struct i7300_pvt *pvt;
682 int branch;
683
684 pvt = mci->pvt_info;
685
686 /* Decrement usage count for devices */
687 for (branch = 0; branch < MAX_CH_PER_BRANCH; branch++)
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300688 pci_dev_put(pvt->pci_dev_2x_0_fbd_branch[branch]);
689 pci_dev_put(pvt->pci_dev_16_2_fsb_err_regs);
690 pci_dev_put(pvt->pci_dev_16_1_fsb_addr_map);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300691}
692
693/*
694 * i7300_get_devices Find and perform 'get' operation on the MCH's
695 * device/functions we want to reference for this driver
696 *
697 * Need to 'get' device 16 func 1 and func 2
698 */
699static int i7300_get_devices(struct mem_ctl_info *mci, int dev_idx)
700{
701 struct i7300_pvt *pvt;
702 struct pci_dev *pdev;
703
704 pvt = mci->pvt_info;
705
706 /* Attempt to 'get' the MCH register we want */
707 pdev = NULL;
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300708 while (!pvt->pci_dev_16_1_fsb_addr_map || !pvt->pci_dev_16_2_fsb_err_regs) {
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300709 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
710 PCI_DEVICE_ID_INTEL_I7300_MCH_ERR, pdev);
711 if (!pdev) {
712 /* End of list, leave */
713 i7300_printk(KERN_ERR,
714 "'system address,Process Bus' "
715 "device not found:"
716 "vendor 0x%x device 0x%x ERR funcs "
717 "(broken BIOS?)\n",
718 PCI_VENDOR_ID_INTEL,
719 PCI_DEVICE_ID_INTEL_I7300_MCH_ERR);
720 goto error;
721 }
722
723 /* Store device 16 funcs 1 and 2 */
724 switch (PCI_FUNC(pdev->devfn)) {
725 case 1:
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300726 pvt->pci_dev_16_1_fsb_addr_map = pdev;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300727 break;
728 case 2:
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300729 pvt->pci_dev_16_2_fsb_err_regs = pdev;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300730 break;
731 }
732 }
733
734 debugf1("System Address, processor bus- PCI Bus ID: %s %x:%x\n",
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300735 pci_name(pvt->pci_dev_16_0_fsb_ctlr),
736 pvt->pci_dev_16_0_fsb_ctlr->vendor, pvt->pci_dev_16_0_fsb_ctlr->device);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300737 debugf1("Branchmap, control and errors - PCI Bus ID: %s %x:%x\n",
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300738 pci_name(pvt->pci_dev_16_1_fsb_addr_map),
739 pvt->pci_dev_16_1_fsb_addr_map->vendor, pvt->pci_dev_16_1_fsb_addr_map->device);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300740 debugf1("FSB Error Regs - PCI Bus ID: %s %x:%x\n",
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300741 pci_name(pvt->pci_dev_16_2_fsb_err_regs),
742 pvt->pci_dev_16_2_fsb_err_regs->vendor, pvt->pci_dev_16_2_fsb_err_regs->device);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300743
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300744 pvt->pci_dev_2x_0_fbd_branch[0] = pci_get_device(PCI_VENDOR_ID_INTEL,
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300745 PCI_DEVICE_ID_INTEL_I7300_MCH_FB0,
746 NULL);
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300747 if (!pvt->pci_dev_2x_0_fbd_branch[0]) {
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300748 i7300_printk(KERN_ERR,
749 "MC: 'BRANCH 0' device not found:"
750 "vendor 0x%x device 0x%x Func 0 (broken BIOS?)\n",
751 PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7300_MCH_FB0);
752 goto error;
753 }
754
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300755 pvt->pci_dev_2x_0_fbd_branch[1] = pci_get_device(PCI_VENDOR_ID_INTEL,
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300756 PCI_DEVICE_ID_INTEL_I7300_MCH_FB1,
757 NULL);
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300758 if (!pvt->pci_dev_2x_0_fbd_branch[1]) {
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300759 i7300_printk(KERN_ERR,
760 "MC: 'BRANCH 1' device not found:"
761 "vendor 0x%x device 0x%x Func 0 "
762 "(broken BIOS?)\n",
763 PCI_VENDOR_ID_INTEL,
764 PCI_DEVICE_ID_INTEL_I7300_MCH_FB1);
765 goto error;
766 }
767
768 return 0;
769
770error:
771 i7300_put_devices(mci);
772 return -ENODEV;
773}
774
775/*
776 * i7300_probe1 Probe for ONE instance of device to see if it is
777 * present.
778 * return:
779 * 0 for FOUND a device
780 * < 0 for error code
781 */
782static int i7300_probe1(struct pci_dev *pdev, int dev_idx)
783{
784 struct mem_ctl_info *mci;
785 struct i7300_pvt *pvt;
786 int num_channels;
787 int num_dimms_per_channel;
788 int num_csrows;
789
790 if (dev_idx >= ARRAY_SIZE(i7300_devs))
791 return -EINVAL;
792
793 debugf0("MC: " __FILE__ ": %s(), pdev bus %u dev=0x%x fn=0x%x\n",
794 __func__,
795 pdev->bus->number,
796 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
797
798 /* We only are looking for func 0 of the set */
799 if (PCI_FUNC(pdev->devfn) != 0)
800 return -ENODEV;
801
802 /* As we don't have a motherboard identification routine to determine
803 * actual number of slots/dimms per channel, we thus utilize the
804 * resource as specified by the chipset. Thus, we might have
805 * have more DIMMs per channel than actually on the mobo, but this
806 * allows the driver to support upto the chipset max, without
807 * some fancy mobo determination.
808 */
809 num_dimms_per_channel = MAX_SLOTS;
810 num_channels = MAX_CHANNELS;
811 num_csrows = MAX_SLOTS * MAX_CHANNELS;
812
813 debugf0("MC: %s(): Number of - Channels= %d DIMMS= %d CSROWS= %d\n",
814 __func__, num_channels, num_dimms_per_channel, num_csrows);
815
816 /* allocate a new MC control structure */
817 mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels, 0);
818
819 if (mci == NULL)
820 return -ENOMEM;
821
822 debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
823
824 mci->dev = &pdev->dev; /* record ptr to the generic device */
825
826 pvt = mci->pvt_info;
Mauro Carvalho Chehab3e57eef2010-08-26 23:38:11 -0300827 pvt->pci_dev_16_0_fsb_ctlr = pdev; /* Record this device in our private */
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300828
829 /* 'get' the pci devices we want to reserve for our use */
830 if (i7300_get_devices(mci, dev_idx))
831 goto fail0;
832
833 mci->mc_idx = 0;
834 mci->mtype_cap = MEM_FLAG_FB_DDR2;
835 mci->edac_ctl_cap = EDAC_FLAG_NONE;
836 mci->edac_cap = EDAC_FLAG_NONE;
837 mci->mod_name = "i7300_edac.c";
838 mci->mod_ver = I7300_REVISION;
839 mci->ctl_name = i7300_devs[dev_idx].ctl_name;
840 mci->dev_name = pci_name(pdev);
841 mci->ctl_page_to_phys = NULL;
842
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300843 /* Set the function pointer to an actual operation function */
844 mci->edac_check = i7300_check_error;
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300845
846 /* initialize the MC control structure 'csrows' table
847 * with the mapping and control information */
848 if (i7300_get_mc_regs(mci)) {
849 debugf0("MC: Setting mci->edac_cap to EDAC_FLAG_NONE\n"
850 " because i7300_init_csrows() returned nonzero "
851 "value\n");
852 mci->edac_cap = EDAC_FLAG_NONE; /* no csrows found */
853 } else {
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300854 debugf1("MC: Enable error reporting now\n");
855 i7300_enable_error_reporting(mci);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300856 }
857
858 /* add this new MC control structure to EDAC's list of MCs */
859 if (edac_mc_add_mc(mci)) {
860 debugf0("MC: " __FILE__
861 ": %s(): failed edac_mc_add_mc()\n", __func__);
862 /* FIXME: perhaps some code should go here that disables error
863 * reporting if we just enabled it
864 */
865 goto fail1;
866 }
867
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300868 i7300_clear_error(mci);
Mauro Carvalho Chehabfcaf7802010-08-24 23:22:57 -0300869
870 /* allocating generic PCI control info */
871 i7300_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
872 if (!i7300_pci) {
873 printk(KERN_WARNING
874 "%s(): Unable to create PCI control\n",
875 __func__);
876 printk(KERN_WARNING
877 "%s(): PCI error report via EDAC not setup\n",
878 __func__);
879 }
880
881 return 0;
882
883 /* Error exit unwinding stack */
884fail1:
885
886 i7300_put_devices(mci);
887
888fail0:
889 edac_mc_free(mci);
890 return -ENODEV;
891}
892
893/*
894 * i7300_init_one constructor for one instance of device
895 *
896 * returns:
897 * negative on error
898 * count (>= 0)
899 */
900static int __devinit i7300_init_one(struct pci_dev *pdev,
901 const struct pci_device_id *id)
902{
903 int rc;
904
905 debugf0("MC: " __FILE__ ": %s()\n", __func__);
906
907 /* wake up device */
908 rc = pci_enable_device(pdev);
909 if (rc == -EIO)
910 return rc;
911
912 /* now probe and enable the device */
913 return i7300_probe1(pdev, id->driver_data);
914}
915
916/*
917 * i7300_remove_one destructor for one instance of device
918 *
919 */
920static void __devexit i7300_remove_one(struct pci_dev *pdev)
921{
922 struct mem_ctl_info *mci;
923
924 debugf0(__FILE__ ": %s()\n", __func__);
925
926 if (i7300_pci)
927 edac_pci_release_generic_ctl(i7300_pci);
928
929 mci = edac_mc_del_mc(&pdev->dev);
930 if (!mci)
931 return;
932
933 /* retrieve references to resources, and free those resources */
934 i7300_put_devices(mci);
935
936 edac_mc_free(mci);
937}
938
939/*
940 * pci_device_id table for which devices we are looking for
941 *
942 * The "E500P" device is the first device supported.
943 */
944static const struct pci_device_id i7300_pci_tbl[] __devinitdata = {
945 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7300_MCH_ERR)},
946 {0,} /* 0 terminated list. */
947};
948
949MODULE_DEVICE_TABLE(pci, i7300_pci_tbl);
950
951/*
952 * i7300_driver pci_driver structure for this module
953 *
954 */
955static struct pci_driver i7300_driver = {
956 .name = "i7300_edac",
957 .probe = i7300_init_one,
958 .remove = __devexit_p(i7300_remove_one),
959 .id_table = i7300_pci_tbl,
960};
961
962/*
963 * i7300_init Module entry function
964 * Try to initialize this module for its devices
965 */
966static int __init i7300_init(void)
967{
968 int pci_rc;
969
970 debugf2("MC: " __FILE__ ": %s()\n", __func__);
971
972 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
973 opstate_init();
974
975 pci_rc = pci_register_driver(&i7300_driver);
976
977 return (pci_rc < 0) ? pci_rc : 0;
978}
979
980/*
981 * i7300_exit() Module exit function
982 * Unregister the driver
983 */
984static void __exit i7300_exit(void)
985{
986 debugf2("MC: " __FILE__ ": %s()\n", __func__);
987 pci_unregister_driver(&i7300_driver);
988}
989
990module_init(i7300_init);
991module_exit(i7300_exit);
992
993MODULE_LICENSE("GPL");
994MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
995MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
996MODULE_DESCRIPTION("MC Driver for Intel I7300 memory controllers - "
997 I7300_REVISION);
998
999module_param(edac_op_state, int, 0444);
1000MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");