blob: 7cb68decf57db335d486920313e57f305d7e03e0 [file] [log] [blame]
Mauro Carvalho Chehab52707f92010-05-18 20:43:52 -03001/* Intel i7 core/Nehalem Memory Controller kernel module
2 *
David Sterbae7bf0682010-12-27 16:51:15 +01003 * This driver supports the memory controllers found on the Intel
Mauro Carvalho Chehab52707f92010-05-18 20:43:52 -03004 * processor families i7core, i7core 7xx/8xx, i5core, Xeon 35xx,
5 * Xeon 55xx and Xeon 56xx also known as Nehalem, Nehalem-EP, Lynnfield
6 * and Westmere-EP.
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03007 *
8 * This file may be distributed under the terms of the
9 * GNU General Public License version 2 only.
10 *
Mauro Carvalho Chehab52707f92010-05-18 20:43:52 -030011 * Copyright (c) 2009-2010 by:
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030012 * Mauro Carvalho Chehab <mchehab@redhat.com>
13 *
14 * Red Hat Inc. http://www.redhat.com
15 *
16 * Forked and adapted from the i5400_edac driver
17 *
18 * Based on the following public Intel datasheets:
19 * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
20 * Datasheet, Volume 2:
21 * http://download.intel.com/design/processor/datashts/320835.pdf
22 * Intel Xeon Processor 5500 Series Datasheet Volume 2
23 * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
24 * also available at:
25 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
26 */
27
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030028#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/pci.h>
31#include <linux/pci_ids.h>
32#include <linux/slab.h>
Randy Dunlap3b918c12009-11-08 01:36:40 -020033#include <linux/delay.h>
Nils Carlson535e9c72011-08-08 06:21:26 -030034#include <linux/dmi.h>
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030035#include <linux/edac.h>
36#include <linux/mmzone.h>
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -030037#include <linux/smp.h>
Borislav Petkov4140c542011-07-18 11:24:46 -030038#include <asm/mce.h>
Mauro Carvalho Chehab14d2c082009-09-02 23:52:36 -030039#include <asm/processor.h>
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030040
41#include "edac_core.h"
42
Mauro Carvalho Chehab18c29002010-08-10 18:33:27 -030043/* Static vars */
44static LIST_HEAD(i7core_edac_list);
45static DEFINE_MUTEX(i7core_edac_lock);
46static int probed;
47
Mauro Carvalho Chehab54a08ab2010-08-19 15:51:00 -030048static int use_pci_fixup;
49module_param(use_pci_fixup, int, 0444);
50MODULE_PARM_DESC(use_pci_fixup, "Enable PCI fixup to seek for hidden devices");
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030051/*
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -030052 * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
53 * registers start at bus 255, and are not reported by BIOS.
54 * We currently find devices with only 2 sockets. In order to support more QPI
55 * Quick Path Interconnect, just increment this number.
56 */
57#define MAX_SOCKET_BUSES 2
58
59
60/*
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030061 * Alter this version for the module when modifications are made
62 */
Michal Marek152ba392011-04-01 12:41:20 +020063#define I7CORE_REVISION " Ver: 1.0.0"
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030064#define EDAC_MOD_STR "i7core_edac"
65
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030066/*
67 * Debug macros
68 */
69#define i7core_printk(level, fmt, arg...) \
70 edac_printk(level, "i7core", fmt, ##arg)
71
72#define i7core_mc_printk(mci, level, fmt, arg...) \
73 edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
74
75/*
76 * i7core Memory Controller Registers
77 */
78
Mauro Carvalho Chehabe9bd2e72009-07-09 22:14:35 -030079 /* OFFSETS for Device 0 Function 0 */
80
81#define MC_CFG_CONTROL 0x90
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -030082 #define MC_CFG_UNLOCK 0x02
83 #define MC_CFG_LOCK 0x00
Mauro Carvalho Chehabe9bd2e72009-07-09 22:14:35 -030084
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -030085 /* OFFSETS for Device 3 Function 0 */
86
87#define MC_CONTROL 0x48
88#define MC_STATUS 0x4c
89#define MC_MAX_DOD 0x64
90
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -030091/*
92 * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
93 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
94 */
95
96#define MC_TEST_ERR_RCV1 0x60
97 #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
98
99#define MC_TEST_ERR_RCV0 0x64
100 #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
101 #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
102
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -0300103/* OFFSETS for Device 3 Function 2, as inicated on Xeon 5500 datasheet */
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -0300104#define MC_SSRCONTROL 0x48
105 #define SSR_MODE_DISABLE 0x00
106 #define SSR_MODE_ENABLE 0x01
107 #define SSR_MODE_MASK 0x03
108
109#define MC_SCRUB_CONTROL 0x4c
110 #define STARTSCRUB (1 << 24)
Nils Carlson535e9c72011-08-08 06:21:26 -0300111 #define SCRUBINTERVAL_MASK 0xffffff
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -0300112
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -0300113#define MC_COR_ECC_CNT_0 0x80
114#define MC_COR_ECC_CNT_1 0x84
115#define MC_COR_ECC_CNT_2 0x88
116#define MC_COR_ECC_CNT_3 0x8c
117#define MC_COR_ECC_CNT_4 0x90
118#define MC_COR_ECC_CNT_5 0x94
119
120#define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
121#define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
122
123
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300124 /* OFFSETS for Devices 4,5 and 6 Function 0 */
125
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300126#define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
127 #define THREE_DIMMS_PRESENT (1 << 24)
128 #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
129 #define QUAD_RANK_PRESENT (1 << 22)
130 #define REGISTERED_DIMM (1 << 15)
131
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300132#define MC_CHANNEL_MAPPER 0x60
133 #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
134 #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
135
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300136#define MC_CHANNEL_RANK_PRESENT 0x7c
137 #define RANK_PRESENT_MASK 0xffff
138
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300139#define MC_CHANNEL_ADDR_MATCH 0xf0
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300140#define MC_CHANNEL_ERROR_MASK 0xf8
141#define MC_CHANNEL_ERROR_INJECT 0xfc
142 #define INJECT_ADDR_PARITY 0x10
143 #define INJECT_ECC 0x08
144 #define MASK_CACHELINE 0x06
145 #define MASK_FULL_CACHELINE 0x06
146 #define MASK_MSB32_CACHELINE 0x04
147 #define MASK_LSB32_CACHELINE 0x02
148 #define NO_MASK_CACHELINE 0x00
149 #define REPEAT_EN 0x01
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300150
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300151 /* OFFSETS for Devices 4,5 and 6 Function 1 */
Mauro Carvalho Chehabb9905382009-08-05 21:36:35 -0300152
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300153#define MC_DOD_CH_DIMM0 0x48
154#define MC_DOD_CH_DIMM1 0x4c
155#define MC_DOD_CH_DIMM2 0x50
156 #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
157 #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
158 #define DIMM_PRESENT_MASK (1 << 9)
159 #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300160 #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
161 #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
162 #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
163 #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300164 #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
Mauro Carvalho Chehab5566cb72009-06-22 22:48:31 -0300165 #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300166 #define MC_DOD_NUMCOL_MASK 3
167 #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300168
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300169#define MC_RANK_PRESENT 0x7c
170
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300171#define MC_SAG_CH_0 0x80
172#define MC_SAG_CH_1 0x84
173#define MC_SAG_CH_2 0x88
174#define MC_SAG_CH_3 0x8c
175#define MC_SAG_CH_4 0x90
176#define MC_SAG_CH_5 0x94
177#define MC_SAG_CH_6 0x98
178#define MC_SAG_CH_7 0x9c
179
180#define MC_RIR_LIMIT_CH_0 0x40
181#define MC_RIR_LIMIT_CH_1 0x44
182#define MC_RIR_LIMIT_CH_2 0x48
183#define MC_RIR_LIMIT_CH_3 0x4C
184#define MC_RIR_LIMIT_CH_4 0x50
185#define MC_RIR_LIMIT_CH_5 0x54
186#define MC_RIR_LIMIT_CH_6 0x58
187#define MC_RIR_LIMIT_CH_7 0x5C
188#define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
189
190#define MC_RIR_WAY_CH 0x80
191 #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
192 #define MC_RIR_WAY_RANK_MASK 0x7
193
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300194/*
195 * i7core structs
196 */
197
198#define NUM_CHANS 3
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -0300199#define MAX_DIMMS 3 /* Max DIMMS per channel */
200#define MAX_MCR_FUNC 4
201#define MAX_CHAN_FUNC 3
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300202
203struct i7core_info {
204 u32 mc_control;
205 u32 mc_status;
206 u32 max_dod;
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300207 u32 ch_map;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300208};
209
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300210
211struct i7core_inject {
212 int enable;
213
214 u32 section;
215 u32 type;
216 u32 eccmask;
217
218 /* Error address mask */
219 int channel, dimm, rank, bank, page, col;
220};
221
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300222struct i7core_channel {
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -0300223 u32 ranks;
224 u32 dimms;
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300225};
226
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300227struct pci_id_descr {
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -0300228 int dev;
229 int func;
230 int dev_id;
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -0300231 int optional;
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300232};
233
Vernon Mauerybd9e19c2010-05-18 19:02:50 -0300234struct pci_id_table {
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300235 const struct pci_id_descr *descr;
236 int n_devs;
Vernon Mauerybd9e19c2010-05-18 19:02:50 -0300237};
238
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300239struct i7core_dev {
240 struct list_head list;
241 u8 socket;
242 struct pci_dev **pdev;
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -0300243 int n_devs;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300244 struct mem_ctl_info *mci;
245};
246
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300247struct i7core_pvt {
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300248 struct pci_dev *pci_noncore;
249 struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
250 struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
251
252 struct i7core_dev *i7core_dev;
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300253
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300254 struct i7core_info info;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300255 struct i7core_inject inject;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300256 struct i7core_channel channel[NUM_CHANS];
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300257
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300258 int ce_count_available;
259 int csrow_map[NUM_CHANS][MAX_DIMMS];
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -0300260
261 /* ECC corrected errors counts per udimm */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300262 unsigned long udimm_ce_count[MAX_DIMMS];
263 int udimm_last_ce_count[MAX_DIMMS];
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -0300264 /* ECC corrected errors counts per rdimm */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300265 unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
266 int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -0300267
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -0300268 bool is_registered, enable_scrub;
Mauro Carvalho Chehab14d2c082009-09-02 23:52:36 -0300269
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -0300270 /* Fifo double buffers */
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -0300271 struct mce mce_entry[MCE_LOG_LEN];
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -0300272 struct mce mce_outentry[MCE_LOG_LEN];
273
274 /* Fifo in/out counters */
275 unsigned mce_in, mce_out;
276
277 /* Count indicator to show errors not got */
278 unsigned mce_overrun;
Mauro Carvalho Chehab939747bd2010-08-10 11:22:01 -0300279
Nils Carlson535e9c72011-08-08 06:21:26 -0300280 /* DCLK Frequency used for computing scrub rate */
281 int dclk_freq;
282
Mauro Carvalho Chehab939747bd2010-08-10 11:22:01 -0300283 /* Struct to control EDAC polling */
284 struct edac_pci_ctl_info *i7core_pci;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300285};
286
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300287#define PCI_DESCR(device, function, device_id) \
288 .dev = (device), \
289 .func = (function), \
290 .dev_id = (device_id)
291
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300292static const struct pci_id_descr pci_dev_descr_i7core_nehalem[] = {
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300293 /* Memory controller */
294 { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
295 { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
Mauro Carvalho Chehab224e8712011-03-17 17:02:59 -0300296 /* Exists only for RDIMM */
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -0300297 { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300298 { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
299
300 /* Channel 0 */
301 { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
302 { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
303 { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
304 { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
305
306 /* Channel 1 */
307 { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
308 { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
309 { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
310 { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
311
312 /* Channel 2 */
313 { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
314 { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
315 { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
316 { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
Mauro Carvalho Chehab224e8712011-03-17 17:02:59 -0300317
318 /* Generic Non-core registers */
319 /*
320 * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
321 * On Xeon 55xx, however, it has a different id (8086:2c40). So,
322 * the probing code needs to test for the other address in case of
323 * failure of this one
324 */
325 { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NONCORE) },
326
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300327};
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300328
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300329static const struct pci_id_descr pci_dev_descr_lynnfield[] = {
Mauro Carvalho Chehab52a2e4fc2009-10-14 11:21:58 -0300330 { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
331 { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
332 { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
333
334 { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
335 { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
336 { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
337 { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
338
Mauro Carvalho Chehab508fa172009-10-14 13:44:37 -0300339 { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
340 { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
341 { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
342 { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
Mauro Carvalho Chehab224e8712011-03-17 17:02:59 -0300343
344 /*
345 * This is the PCI device has an alternate address on some
346 * processors like Core i7 860
347 */
348 { PCI_DESCR( 0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE) },
Mauro Carvalho Chehab52a2e4fc2009-10-14 11:21:58 -0300349};
350
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300351static const struct pci_id_descr pci_dev_descr_i7core_westmere[] = {
Vernon Mauerybd9e19c2010-05-18 19:02:50 -0300352 /* Memory controller */
353 { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR_REV2) },
354 { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD_REV2) },
355 /* Exists only for RDIMM */
356 { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_RAS_REV2), .optional = 1 },
357 { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST_REV2) },
358
359 /* Channel 0 */
360 { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL_REV2) },
361 { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR_REV2) },
362 { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK_REV2) },
363 { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC_REV2) },
364
365 /* Channel 1 */
366 { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL_REV2) },
367 { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR_REV2) },
368 { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK_REV2) },
369 { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC_REV2) },
370
371 /* Channel 2 */
372 { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_CTRL_REV2) },
373 { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_ADDR_REV2) },
374 { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_RANK_REV2) },
375 { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_TC_REV2) },
Mauro Carvalho Chehab224e8712011-03-17 17:02:59 -0300376
377 /* Generic Non-core registers */
378 { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2) },
379
Vernon Mauerybd9e19c2010-05-18 19:02:50 -0300380};
381
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300382#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
383static const struct pci_id_table pci_dev_table[] = {
Vernon Mauerybd9e19c2010-05-18 19:02:50 -0300384 PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_nehalem),
385 PCI_ID_TABLE_ENTRY(pci_dev_descr_lynnfield),
386 PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_westmere),
Mauro Carvalho Chehab3c52cc52010-10-24 11:12:28 -0200387 {0,} /* 0 terminated list. */
Vernon Mauerybd9e19c2010-05-18 19:02:50 -0300388};
389
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300390/*
391 * pci_device_id table for which devices we are looking for
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300392 */
393static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
Mauro Carvalho Chehabd1fd4fb2009-07-10 18:39:53 -0300394 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
Mauro Carvalho Chehabf05da2f2009-10-14 13:31:06 -0300395 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300396 {0,} /* 0 terminated list. */
397};
398
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300399/****************************************************************************
400 Anciliary status routines
401 ****************************************************************************/
402
403 /* MC_CONTROL bits */
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300404#define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
405#define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300406
407 /* MC_STATUS bits */
Keith Mannthey61053fd2009-09-02 23:46:59 -0300408#define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300409#define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300410
411 /* MC_MAX_DOD read functions */
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300412static inline int numdimms(u32 dimms)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300413{
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300414 return (dimms & 0x3) + 1;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300415}
416
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300417static inline int numrank(u32 rank)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300418{
419 static int ranks[4] = { 1, 2, 4, -EINVAL };
420
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300421 return ranks[rank & 0x3];
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300422}
423
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300424static inline int numbank(u32 bank)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300425{
426 static int banks[4] = { 4, 8, 16, -EINVAL };
427
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300428 return banks[bank & 0x3];
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300429}
430
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300431static inline int numrow(u32 row)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300432{
433 static int rows[8] = {
434 1 << 12, 1 << 13, 1 << 14, 1 << 15,
435 1 << 16, -EINVAL, -EINVAL, -EINVAL,
436 };
437
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300438 return rows[row & 0x7];
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300439}
440
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300441static inline int numcol(u32 col)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300442{
443 static int cols[8] = {
444 1 << 10, 1 << 11, 1 << 12, -EINVAL,
445 };
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300446 return cols[col & 0x3];
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300447}
448
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300449static struct i7core_dev *get_i7core_dev(u8 socket)
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -0300450{
451 struct i7core_dev *i7core_dev;
452
453 list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
454 if (i7core_dev->socket == socket)
455 return i7core_dev;
456 }
457
458 return NULL;
459}
460
Hidetoshi Seto848b2f72010-08-20 04:24:44 -0300461static struct i7core_dev *alloc_i7core_dev(u8 socket,
462 const struct pci_id_table *table)
463{
464 struct i7core_dev *i7core_dev;
465
466 i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
467 if (!i7core_dev)
468 return NULL;
469
470 i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * table->n_devs,
471 GFP_KERNEL);
472 if (!i7core_dev->pdev) {
473 kfree(i7core_dev);
474 return NULL;
475 }
476
477 i7core_dev->socket = socket;
478 i7core_dev->n_devs = table->n_devs;
479 list_add_tail(&i7core_dev->list, &i7core_edac_list);
480
481 return i7core_dev;
482}
483
Hidetoshi Seto2aa9be42010-08-20 04:25:00 -0300484static void free_i7core_dev(struct i7core_dev *i7core_dev)
485{
486 list_del(&i7core_dev->list);
487 kfree(i7core_dev->pdev);
488 kfree(i7core_dev);
489}
490
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300491/****************************************************************************
492 Memory check routines
493 ****************************************************************************/
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300494static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
495 unsigned func)
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300496{
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -0300497 struct i7core_dev *i7core_dev = get_i7core_dev(socket);
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300498 int i;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300499
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -0300500 if (!i7core_dev)
501 return NULL;
502
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -0300503 for (i = 0; i < i7core_dev->n_devs; i++) {
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -0300504 if (!i7core_dev->pdev[i])
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300505 continue;
506
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -0300507 if (PCI_SLOT(i7core_dev->pdev[i]->devfn) == slot &&
508 PCI_FUNC(i7core_dev->pdev[i]->devfn) == func) {
509 return i7core_dev->pdev[i];
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300510 }
511 }
512
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300513 return NULL;
514}
515
Mauro Carvalho Chehabec6df242009-07-18 10:44:30 -0300516/**
517 * i7core_get_active_channels() - gets the number of channels and csrows
518 * @socket: Quick Path Interconnect socket
519 * @channels: Number of channels that will be returned
520 * @csrows: Number of csrows found
521 *
522 * Since EDAC core needs to know in advance the number of available channels
523 * and csrows, in order to allocate memory for csrows/channels, it is needed
524 * to run two similar steps. At the first step, implemented on this function,
525 * it checks the number of csrows/channels present at one socket.
526 * this is used in order to properly allocate the size of mci components.
527 *
528 * It should be noticed that none of the current available datasheets explain
529 * or even mention how csrows are seen by the memory controller. So, we need
530 * to add a fake description for csrows.
531 * So, this driver is attributing one DIMM memory for one csrow.
532 */
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300533static int i7core_get_active_channels(const u8 socket, unsigned *channels,
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300534 unsigned *csrows)
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300535{
536 struct pci_dev *pdev = NULL;
537 int i, j;
538 u32 status, control;
539
540 *channels = 0;
541 *csrows = 0;
542
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300543 pdev = get_pdev_slot_func(socket, 3, 0);
Mauro Carvalho Chehabb7c76152009-06-22 22:48:30 -0300544 if (!pdev) {
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300545 i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
546 socket);
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300547 return -ENODEV;
Mauro Carvalho Chehabb7c76152009-06-22 22:48:30 -0300548 }
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300549
550 /* Device 3 function 0 reads */
551 pci_read_config_dword(pdev, MC_STATUS, &status);
552 pci_read_config_dword(pdev, MC_CONTROL, &control);
553
554 for (i = 0; i < NUM_CHANS; i++) {
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300555 u32 dimm_dod[3];
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300556 /* Check if the channel is active */
557 if (!(control & (1 << (8 + i))))
558 continue;
559
560 /* Check if the channel is disabled */
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300561 if (status & (1 << i))
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300562 continue;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300563
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300564 pdev = get_pdev_slot_func(socket, i + 4, 1);
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300565 if (!pdev) {
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300566 i7core_printk(KERN_ERR, "Couldn't find socket %d "
567 "fn %d.%d!!!\n",
568 socket, i + 4, 1);
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300569 return -ENODEV;
570 }
571 /* Devices 4-6 function 1 */
572 pci_read_config_dword(pdev,
573 MC_DOD_CH_DIMM0, &dimm_dod[0]);
574 pci_read_config_dword(pdev,
575 MC_DOD_CH_DIMM1, &dimm_dod[1]);
576 pci_read_config_dword(pdev,
577 MC_DOD_CH_DIMM2, &dimm_dod[2]);
578
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300579 (*channels)++;
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300580
581 for (j = 0; j < 3; j++) {
582 if (!DIMM_PRESENT(dimm_dod[j]))
583 continue;
584 (*csrows)++;
585 }
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300586 }
587
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -0300588 debugf0("Number of active channels on socket %d: %d\n",
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300589 socket, *channels);
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300590
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -0300591 return 0;
592}
593
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -0300594static int get_dimm_config(const struct mem_ctl_info *mci)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300595{
596 struct i7core_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300597 struct csrow_info *csr;
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300598 struct pci_dev *pdev;
Mauro Carvalho Chehabba6c5c62009-07-15 09:02:32 -0300599 int i, j;
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -0300600 int csrow = 0;
Mauro Carvalho Chehab5566cb72009-06-22 22:48:31 -0300601 unsigned long last_page = 0;
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300602 enum edac_type mode;
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300603 enum mem_type mtype;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300604
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300605 /* Get data from the MC register, function 0 */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300606 pdev = pvt->pci_mcr[0];
Mauro Carvalho Chehab7dd69532009-06-22 22:48:30 -0300607 if (!pdev)
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300608 return -ENODEV;
609
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300610 /* Device 3 function 0 reads */
Mauro Carvalho Chehab7dd69532009-06-22 22:48:30 -0300611 pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
612 pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
613 pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
614 pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300615
Mauro Carvalho Chehab17cb7b02009-07-20 18:48:18 -0300616 debugf0("QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
Mauro Carvalho Chehab4af91882009-09-24 09:58:26 -0300617 pvt->i7core_dev->socket, pvt->info.mc_control, pvt->info.mc_status,
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300618 pvt->info.max_dod, pvt->info.ch_map);
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300619
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300620 if (ECC_ENABLED(pvt)) {
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300621 debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300622 if (ECCx8(pvt))
623 mode = EDAC_S8ECD8ED;
624 else
625 mode = EDAC_S4ECD4ED;
626 } else {
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300627 debugf0("ECC disabled\n");
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300628 mode = EDAC_NONE;
629 }
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300630
631 /* FIXME: need to handle the error codes */
Mauro Carvalho Chehab17cb7b02009-07-20 18:48:18 -0300632 debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked "
633 "x%x x 0x%x\n",
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300634 numdimms(pvt->info.max_dod),
635 numrank(pvt->info.max_dod >> 2),
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -0300636 numbank(pvt->info.max_dod >> 4),
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300637 numrow(pvt->info.max_dod >> 6),
638 numcol(pvt->info.max_dod >> 9));
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300639
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300640 for (i = 0; i < NUM_CHANS; i++) {
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300641 u32 data, dimm_dod[3], value[8];
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300642
Mauro Carvalho Chehab52a2e4fc2009-10-14 11:21:58 -0300643 if (!pvt->pci_ch[i][0])
644 continue;
645
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300646 if (!CH_ACTIVE(pvt, i)) {
647 debugf0("Channel %i is not active\n", i);
648 continue;
649 }
650 if (CH_DISABLED(pvt, i)) {
651 debugf0("Channel %i is disabled\n", i);
652 continue;
653 }
654
Mauro Carvalho Chehabf122a892009-06-22 22:48:29 -0300655 /* Devices 4-6 function 0 */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300656 pci_read_config_dword(pvt->pci_ch[i][0],
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300657 MC_CHANNEL_DIMM_INIT_PARAMS, &data);
658
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300659 pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ?
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -0300660 4 : 2;
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300661
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300662 if (data & REGISTERED_DIMM)
663 mtype = MEM_RDDR3;
Mauro Carvalho Chehab14d2c082009-09-02 23:52:36 -0300664 else
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300665 mtype = MEM_DDR3;
666#if 0
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300667 if (data & THREE_DIMMS_PRESENT)
668 pvt->channel[i].dimms = 3;
669 else if (data & SINGLE_QUAD_RANK_PRESENT)
670 pvt->channel[i].dimms = 1;
671 else
672 pvt->channel[i].dimms = 2;
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300673#endif
674
675 /* Devices 4-6 function 1 */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300676 pci_read_config_dword(pvt->pci_ch[i][1],
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300677 MC_DOD_CH_DIMM0, &dimm_dod[0]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300678 pci_read_config_dword(pvt->pci_ch[i][1],
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300679 MC_DOD_CH_DIMM1, &dimm_dod[1]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300680 pci_read_config_dword(pvt->pci_ch[i][1],
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300681 MC_DOD_CH_DIMM2, &dimm_dod[2]);
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300682
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300683 debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300684 "%d ranks, %cDIMMs\n",
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300685 i,
686 RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
687 data,
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300688 pvt->channel[i].ranks,
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300689 (data & REGISTERED_DIMM) ? 'R' : 'U');
Mauro Carvalho Chehab7dd69532009-06-22 22:48:30 -0300690
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300691 for (j = 0; j < 3; j++) {
692 u32 banks, ranks, rows, cols;
Mauro Carvalho Chehab5566cb72009-06-22 22:48:31 -0300693 u32 size, npages;
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300694
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300695 if (!DIMM_PRESENT(dimm_dod[j]))
696 continue;
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300697
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300698 banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
699 ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
700 rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
701 cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
Mauro Carvalho Chehab1c6fed82009-06-22 22:48:30 -0300702
Mauro Carvalho Chehab5566cb72009-06-22 22:48:31 -0300703 /* DDR3 has 8 I/O banks */
704 size = (rows * cols * banks * ranks) >> (20 - 3);
705
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300706 pvt->channel[i].dimms++;
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300707
Mauro Carvalho Chehab17cb7b02009-07-20 18:48:18 -0300708 debugf0("\tdimm %d %d Mb offset: %x, "
709 "bank: %d, rank: %d, row: %#x, col: %#x\n",
710 j, size,
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300711 RANKOFFSET(dimm_dod[j]),
712 banks, ranks, rows, cols);
713
Mauro Carvalho Chehabe9144602010-08-10 20:26:35 -0300714 npages = MiB_TO_PAGES(size);
Mauro Carvalho Chehab5566cb72009-06-22 22:48:31 -0300715
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -0300716 csr = &mci->csrows[csrow];
Mauro Carvalho Chehab5566cb72009-06-22 22:48:31 -0300717 csr->first_page = last_page + 1;
718 last_page += npages;
719 csr->last_page = last_page;
720 csr->nr_pages = npages;
721
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300722 csr->page_mask = 0;
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300723 csr->grain = 8;
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -0300724 csr->csrow_idx = csrow;
Mauro Carvalho Chehabeb94fc42009-06-22 22:48:31 -0300725 csr->nr_channels = 1;
726
727 csr->channels[0].chan_idx = i;
728 csr->channels[0].ce_count = 0;
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300729
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -0300730 pvt->csrow_map[i][j] = csrow;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -0300731
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300732 switch (banks) {
733 case 4:
734 csr->dtype = DEV_X4;
735 break;
736 case 8:
737 csr->dtype = DEV_X8;
738 break;
739 case 16:
740 csr->dtype = DEV_X16;
741 break;
742 default:
743 csr->dtype = DEV_UNKNOWN;
744 }
745
746 csr->edac_mode = mode;
747 csr->mtype = mtype;
748
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -0300749 csrow++;
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300750 }
751
752 pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
753 pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
754 pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
755 pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
756 pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
757 pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
758 pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
759 pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
Mauro Carvalho Chehab17cb7b02009-07-20 18:48:18 -0300760 debugf1("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300761 for (j = 0; j < 8; j++)
Mauro Carvalho Chehab17cb7b02009-07-20 18:48:18 -0300762 debugf1("\t\t%#x\t%#x\t%#x\n",
Mauro Carvalho Chehab854d3342009-06-22 22:48:30 -0300763 (value[j] >> 27) & 0x1,
764 (value[j] >> 24) & 0x7,
David Sterba80b8ce82010-12-27 15:39:12 +0000765 (value[j] & ((1 << 24) - 1)));
Mauro Carvalho Chehab0b2b7b72009-06-22 22:48:29 -0300766 }
767
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -0300768 return 0;
769}
770
771/****************************************************************************
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300772 Error insertion routines
773 ****************************************************************************/
774
775/* The i7core has independent error injection features per channel.
776 However, to have a simpler code, we don't allow enabling error injection
777 on more than one channel.
778 Also, since a change at an inject parameter will be applied only at enable,
779 we're disabling error injection on all write calls to the sysfs nodes that
780 controls the error code injection.
781 */
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300782static int disable_inject(const struct mem_ctl_info *mci)
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300783{
784 struct i7core_pvt *pvt = mci->pvt_info;
785
786 pvt->inject.enable = 0;
787
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300788 if (!pvt->pci_ch[pvt->inject.channel][0])
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300789 return -ENODEV;
790
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -0300791 pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
Mauro Carvalho Chehab4157d9f2009-08-05 20:27:15 -0300792 MC_CHANNEL_ERROR_INJECT, 0);
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -0300793
794 return 0;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300795}
796
797/*
798 * i7core inject inject.section
799 *
800 * accept and store error injection inject.section value
801 * bit 0 - refers to the lower 32-byte half cacheline
802 * bit 1 - refers to the upper 32-byte half cacheline
803 */
804static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
805 const char *data, size_t count)
806{
807 struct i7core_pvt *pvt = mci->pvt_info;
808 unsigned long value;
809 int rc;
810
811 if (pvt->inject.enable)
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300812 disable_inject(mci);
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300813
814 rc = strict_strtoul(data, 10, &value);
815 if ((rc < 0) || (value > 3))
Mauro Carvalho Chehab2068def2009-08-05 19:28:27 -0300816 return -EIO;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300817
818 pvt->inject.section = (u32) value;
819 return count;
820}
821
822static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
823 char *data)
824{
825 struct i7core_pvt *pvt = mci->pvt_info;
826 return sprintf(data, "0x%08x\n", pvt->inject.section);
827}
828
829/*
830 * i7core inject.type
831 *
832 * accept and store error injection inject.section value
833 * bit 0 - repeat enable - Enable error repetition
834 * bit 1 - inject ECC error
835 * bit 2 - inject parity error
836 */
837static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
838 const char *data, size_t count)
839{
840 struct i7core_pvt *pvt = mci->pvt_info;
841 unsigned long value;
842 int rc;
843
844 if (pvt->inject.enable)
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300845 disable_inject(mci);
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300846
847 rc = strict_strtoul(data, 10, &value);
848 if ((rc < 0) || (value > 7))
Mauro Carvalho Chehab2068def2009-08-05 19:28:27 -0300849 return -EIO;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300850
851 pvt->inject.type = (u32) value;
852 return count;
853}
854
855static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
856 char *data)
857{
858 struct i7core_pvt *pvt = mci->pvt_info;
859 return sprintf(data, "0x%08x\n", pvt->inject.type);
860}
861
862/*
863 * i7core_inject_inject.eccmask_store
864 *
865 * The type of error (UE/CE) will depend on the inject.eccmask value:
866 * Any bits set to a 1 will flip the corresponding ECC bit
867 * Correctable errors can be injected by flipping 1 bit or the bits within
868 * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
869 * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
870 * uncorrectable error to be injected.
871 */
872static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
873 const char *data, size_t count)
874{
875 struct i7core_pvt *pvt = mci->pvt_info;
876 unsigned long value;
877 int rc;
878
879 if (pvt->inject.enable)
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -0300880 disable_inject(mci);
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300881
882 rc = strict_strtoul(data, 10, &value);
883 if (rc < 0)
Mauro Carvalho Chehab2068def2009-08-05 19:28:27 -0300884 return -EIO;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300885
886 pvt->inject.eccmask = (u32) value;
887 return count;
888}
889
890static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
891 char *data)
892{
893 struct i7core_pvt *pvt = mci->pvt_info;
894 return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
895}
896
897/*
898 * i7core_addrmatch
899 *
900 * The type of error (UE/CE) will depend on the inject.eccmask value:
901 * Any bits set to a 1 will flip the corresponding ECC bit
902 * Correctable errors can be injected by flipping 1 bit or the bits within
903 * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
904 * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
905 * uncorrectable error to be injected.
906 */
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300907
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300908#define DECLARE_ADDR_MATCH(param, limit) \
909static ssize_t i7core_inject_store_##param( \
910 struct mem_ctl_info *mci, \
911 const char *data, size_t count) \
912{ \
Mauro Carvalho Chehabcc301b32009-09-24 16:23:42 -0300913 struct i7core_pvt *pvt; \
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300914 long value; \
915 int rc; \
916 \
Mauro Carvalho Chehabcc301b32009-09-24 16:23:42 -0300917 debugf1("%s()\n", __func__); \
918 pvt = mci->pvt_info; \
919 \
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300920 if (pvt->inject.enable) \
921 disable_inject(mci); \
922 \
Mauro Carvalho Chehab4f87fad2009-10-04 11:54:56 -0300923 if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300924 value = -1; \
925 else { \
926 rc = strict_strtoul(data, 10, &value); \
927 if ((rc < 0) || (value >= limit)) \
928 return -EIO; \
929 } \
930 \
931 pvt->inject.param = value; \
932 \
933 return count; \
934} \
935 \
936static ssize_t i7core_inject_show_##param( \
937 struct mem_ctl_info *mci, \
938 char *data) \
939{ \
Mauro Carvalho Chehabcc301b32009-09-24 16:23:42 -0300940 struct i7core_pvt *pvt; \
941 \
942 pvt = mci->pvt_info; \
943 debugf1("%s() pvt=%p\n", __func__, pvt); \
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300944 if (pvt->inject.param < 0) \
945 return sprintf(data, "any\n"); \
946 else \
947 return sprintf(data, "%d\n", pvt->inject.param);\
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300948}
949
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300950#define ATTR_ADDR_MATCH(param) \
951 { \
952 .attr = { \
953 .name = #param, \
954 .mode = (S_IRUGO | S_IWUSR) \
955 }, \
956 .show = i7core_inject_show_##param, \
957 .store = i7core_inject_store_##param, \
958 }
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300959
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -0300960DECLARE_ADDR_MATCH(channel, 3);
961DECLARE_ADDR_MATCH(dimm, 3);
962DECLARE_ADDR_MATCH(rank, 4);
963DECLARE_ADDR_MATCH(bank, 32);
964DECLARE_ADDR_MATCH(page, 0x10000);
965DECLARE_ADDR_MATCH(col, 0x4000);
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300966
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -0300967static int write_and_test(struct pci_dev *dev, const int where, const u32 val)
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -0300968{
969 u32 read;
970 int count;
971
Mauro Carvalho Chehab4157d9f2009-08-05 20:27:15 -0300972 debugf0("setting pci %02x:%02x.%x reg=%02x value=%08x\n",
973 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
974 where, val);
975
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -0300976 for (count = 0; count < 10; count++) {
977 if (count)
Mauro Carvalho Chehabb9905382009-08-05 21:36:35 -0300978 msleep(100);
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -0300979 pci_write_config_dword(dev, where, val);
980 pci_read_config_dword(dev, where, &read);
981
982 if (read == val)
983 return 0;
984 }
985
Mauro Carvalho Chehab4157d9f2009-08-05 20:27:15 -0300986 i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
987 "write=%08x. Read=%08x\n",
988 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
989 where, val, read);
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -0300990
991 return -EINVAL;
992}
993
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -0300994/*
995 * This routine prepares the Memory Controller for error injection.
996 * The error will be injected when some process tries to write to the
997 * memory that matches the given criteria.
998 * The criteria can be set in terms of a mask where dimm, rank, bank, page
999 * and col can be specified.
1000 * A -1 value for any of the mask items will make the MCU to ignore
1001 * that matching criteria for error injection.
1002 *
1003 * It should be noticed that the error will only happen after a write operation
1004 * on a memory that matches the condition. if REPEAT_EN is not enabled at
1005 * inject mask, then it will produce just one error. Otherwise, it will repeat
1006 * until the injectmask would be cleaned.
1007 *
1008 * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
1009 * is reliable enough to check if the MC is using the
1010 * three channels. However, this is not clear at the datasheet.
1011 */
1012static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
1013 const char *data, size_t count)
1014{
1015 struct i7core_pvt *pvt = mci->pvt_info;
1016 u32 injectmask;
1017 u64 mask = 0;
1018 int rc;
1019 long enable;
1020
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001021 if (!pvt->pci_ch[pvt->inject.channel][0])
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -03001022 return 0;
1023
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001024 rc = strict_strtoul(data, 10, &enable);
1025 if ((rc < 0))
1026 return 0;
1027
1028 if (enable) {
1029 pvt->inject.enable = 1;
1030 } else {
1031 disable_inject(mci);
1032 return count;
1033 }
1034
1035 /* Sets pvt->inject.dimm mask */
1036 if (pvt->inject.dimm < 0)
Alan Cox486dd092009-11-08 01:34:27 -02001037 mask |= 1LL << 41;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001038 else {
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001039 if (pvt->channel[pvt->inject.channel].dimms > 2)
Alan Cox486dd092009-11-08 01:34:27 -02001040 mask |= (pvt->inject.dimm & 0x3LL) << 35;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001041 else
Alan Cox486dd092009-11-08 01:34:27 -02001042 mask |= (pvt->inject.dimm & 0x1LL) << 36;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001043 }
1044
1045 /* Sets pvt->inject.rank mask */
1046 if (pvt->inject.rank < 0)
Alan Cox486dd092009-11-08 01:34:27 -02001047 mask |= 1LL << 40;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001048 else {
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001049 if (pvt->channel[pvt->inject.channel].dimms > 2)
Alan Cox486dd092009-11-08 01:34:27 -02001050 mask |= (pvt->inject.rank & 0x1LL) << 34;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001051 else
Alan Cox486dd092009-11-08 01:34:27 -02001052 mask |= (pvt->inject.rank & 0x3LL) << 34;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001053 }
1054
1055 /* Sets pvt->inject.bank mask */
1056 if (pvt->inject.bank < 0)
Alan Cox486dd092009-11-08 01:34:27 -02001057 mask |= 1LL << 39;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001058 else
Alan Cox486dd092009-11-08 01:34:27 -02001059 mask |= (pvt->inject.bank & 0x15LL) << 30;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001060
1061 /* Sets pvt->inject.page mask */
1062 if (pvt->inject.page < 0)
Alan Cox486dd092009-11-08 01:34:27 -02001063 mask |= 1LL << 38;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001064 else
Alan Cox486dd092009-11-08 01:34:27 -02001065 mask |= (pvt->inject.page & 0xffff) << 14;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001066
1067 /* Sets pvt->inject.column mask */
1068 if (pvt->inject.col < 0)
Alan Cox486dd092009-11-08 01:34:27 -02001069 mask |= 1LL << 37;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001070 else
Alan Cox486dd092009-11-08 01:34:27 -02001071 mask |= (pvt->inject.col & 0x3fff);
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001072
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001073 /*
1074 * bit 0: REPEAT_EN
1075 * bits 1-2: MASK_HALF_CACHELINE
1076 * bit 3: INJECT_ECC
1077 * bit 4: INJECT_ADDR_PARITY
1078 */
1079
Mauro Carvalho Chehab7b029d02009-06-22 22:48:29 -03001080 injectmask = (pvt->inject.type & 1) |
1081 (pvt->inject.section & 0x3) << 1 |
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001082 (pvt->inject.type & 0x6) << (3 - 1);
1083
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001084 /* Unlock writes to registers - this register is write only */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001085 pci_write_config_dword(pvt->pci_noncore,
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001086 MC_CFG_CONTROL, 0x2);
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001087
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001088 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001089 MC_CHANNEL_ADDR_MATCH, mask);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001090 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001091 MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
1092
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001093 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001094 MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
1095
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001096 write_and_test(pvt->pci_ch[pvt->inject.channel][0],
Mauro Carvalho Chehab4157d9f2009-08-05 20:27:15 -03001097 MC_CHANNEL_ERROR_INJECT, injectmask);
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001098
1099 /*
1100 * This is something undocumented, based on my tests
1101 * Without writing 8 to this register, errors aren't injected. Not sure
1102 * why.
1103 */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001104 pci_write_config_dword(pvt->pci_noncore,
Mauro Carvalho Chehab276b8242009-07-22 21:45:50 -03001105 MC_CFG_CONTROL, 8);
1106
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -03001107 debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
1108 " inject 0x%08x\n",
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001109 mask, pvt->inject.eccmask, injectmask);
1110
Mauro Carvalho Chehab7b029d02009-06-22 22:48:29 -03001111
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001112 return count;
1113}
1114
1115static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
1116 char *data)
1117{
1118 struct i7core_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehab7b029d02009-06-22 22:48:29 -03001119 u32 injectmask;
1120
Mauro Carvalho Chehab52a2e4fc2009-10-14 11:21:58 -03001121 if (!pvt->pci_ch[pvt->inject.channel][0])
1122 return 0;
1123
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001124 pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
Mauro Carvalho Chehab4157d9f2009-08-05 20:27:15 -03001125 MC_CHANNEL_ERROR_INJECT, &injectmask);
Mauro Carvalho Chehab7b029d02009-06-22 22:48:29 -03001126
1127 debugf0("Inject error read: 0x%018x\n", injectmask);
1128
1129 if (injectmask & 0x0c)
1130 pvt->inject.enable = 1;
1131
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001132 return sprintf(data, "%d\n", pvt->inject.enable);
1133}
1134
Mauro Carvalho Chehabf338d732009-09-24 17:25:43 -03001135#define DECLARE_COUNTER(param) \
1136static ssize_t i7core_show_counter_##param( \
1137 struct mem_ctl_info *mci, \
1138 char *data) \
1139{ \
1140 struct i7core_pvt *pvt = mci->pvt_info; \
1141 \
1142 debugf1("%s() \n", __func__); \
1143 if (!pvt->ce_count_available || (pvt->is_registered)) \
1144 return sprintf(data, "data unavailable\n"); \
1145 return sprintf(data, "%lu\n", \
1146 pvt->udimm_ce_count[param]); \
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001147}
1148
Mauro Carvalho Chehabf338d732009-09-24 17:25:43 -03001149#define ATTR_COUNTER(param) \
1150 { \
1151 .attr = { \
1152 .name = __stringify(udimm##param), \
1153 .mode = (S_IRUGO | S_IWUSR) \
1154 }, \
1155 .show = i7core_show_counter_##param \
1156 }
1157
1158DECLARE_COUNTER(0);
1159DECLARE_COUNTER(1);
1160DECLARE_COUNTER(2);
1161
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001162/*
1163 * Sysfs struct
1164 */
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -03001165
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001166static const struct mcidev_sysfs_attribute i7core_addrmatch_attrs[] = {
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -03001167 ATTR_ADDR_MATCH(channel),
1168 ATTR_ADDR_MATCH(dimm),
1169 ATTR_ADDR_MATCH(rank),
1170 ATTR_ADDR_MATCH(bank),
1171 ATTR_ADDR_MATCH(page),
1172 ATTR_ADDR_MATCH(col),
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001173 { } /* End of list */
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -03001174};
1175
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001176static const struct mcidev_sysfs_group i7core_inject_addrmatch = {
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -03001177 .name = "inject_addrmatch",
1178 .mcidev_attr = i7core_addrmatch_attrs,
1179};
1180
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001181static const struct mcidev_sysfs_attribute i7core_udimm_counters_attrs[] = {
Mauro Carvalho Chehabf338d732009-09-24 17:25:43 -03001182 ATTR_COUNTER(0),
1183 ATTR_COUNTER(1),
1184 ATTR_COUNTER(2),
Marcin Slusarz64aab722010-09-30 15:15:30 -07001185 { .attr = { .name = NULL } }
Mauro Carvalho Chehabf338d732009-09-24 17:25:43 -03001186};
1187
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001188static const struct mcidev_sysfs_group i7core_udimm_counters = {
Mauro Carvalho Chehabf338d732009-09-24 17:25:43 -03001189 .name = "all_channel_counts",
1190 .mcidev_attr = i7core_udimm_counters_attrs,
1191};
1192
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001193static const struct mcidev_sysfs_attribute i7core_sysfs_rdimm_attrs[] = {
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001194 {
1195 .attr = {
1196 .name = "inject_section",
1197 .mode = (S_IRUGO | S_IWUSR)
1198 },
1199 .show = i7core_inject_section_show,
1200 .store = i7core_inject_section_store,
1201 }, {
1202 .attr = {
1203 .name = "inject_type",
1204 .mode = (S_IRUGO | S_IWUSR)
1205 },
1206 .show = i7core_inject_type_show,
1207 .store = i7core_inject_type_store,
1208 }, {
1209 .attr = {
1210 .name = "inject_eccmask",
1211 .mode = (S_IRUGO | S_IWUSR)
1212 },
1213 .show = i7core_inject_eccmask_show,
1214 .store = i7core_inject_eccmask_store,
1215 }, {
Mauro Carvalho Chehaba5538e52009-09-23 18:56:47 -03001216 .grp = &i7core_inject_addrmatch,
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001217 }, {
1218 .attr = {
1219 .name = "inject_enable",
1220 .mode = (S_IRUGO | S_IWUSR)
1221 },
1222 .show = i7core_inject_enable_show,
1223 .store = i7core_inject_enable_store,
1224 },
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001225 { } /* End of list */
1226};
1227
1228static const struct mcidev_sysfs_attribute i7core_sysfs_udimm_attrs[] = {
1229 {
1230 .attr = {
1231 .name = "inject_section",
1232 .mode = (S_IRUGO | S_IWUSR)
1233 },
1234 .show = i7core_inject_section_show,
1235 .store = i7core_inject_section_store,
1236 }, {
1237 .attr = {
1238 .name = "inject_type",
1239 .mode = (S_IRUGO | S_IWUSR)
1240 },
1241 .show = i7core_inject_type_show,
1242 .store = i7core_inject_type_store,
1243 }, {
1244 .attr = {
1245 .name = "inject_eccmask",
1246 .mode = (S_IRUGO | S_IWUSR)
1247 },
1248 .show = i7core_inject_eccmask_show,
1249 .store = i7core_inject_eccmask_store,
1250 }, {
1251 .grp = &i7core_inject_addrmatch,
1252 }, {
1253 .attr = {
1254 .name = "inject_enable",
1255 .mode = (S_IRUGO | S_IWUSR)
1256 },
1257 .show = i7core_inject_enable_show,
1258 .store = i7core_inject_enable_store,
1259 }, {
1260 .grp = &i7core_udimm_counters,
1261 },
1262 { } /* End of list */
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03001263};
1264
1265/****************************************************************************
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001266 Device initialization routines: put/get, init/exit
1267 ****************************************************************************/
1268
1269/*
Hidetoshi Seto64c10f62010-08-20 04:28:14 -03001270 * i7core_put_all_devices 'put' all the devices that we have
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001271 * reserved via 'get'
1272 */
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03001273static void i7core_put_devices(struct i7core_dev *i7core_dev)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001274{
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03001275 int i;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001276
Mauro Carvalho Chehab22e6bcb2009-09-05 23:06:50 -03001277 debugf0(__FILE__ ": %s()\n", __func__);
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001278 for (i = 0; i < i7core_dev->n_devs; i++) {
Mauro Carvalho Chehab22e6bcb2009-09-05 23:06:50 -03001279 struct pci_dev *pdev = i7core_dev->pdev[i];
1280 if (!pdev)
1281 continue;
1282 debugf0("Removing dev %02x:%02x.%d\n",
1283 pdev->bus->number,
1284 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1285 pci_dev_put(pdev);
1286 }
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03001287}
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001288
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03001289static void i7core_put_all_devices(void)
1290{
Mauro Carvalho Chehab42538682009-09-24 09:59:13 -03001291 struct i7core_dev *i7core_dev, *tmp;
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03001292
Mauro Carvalho Chehab39300e72010-08-11 23:40:15 -03001293 list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03001294 i7core_put_devices(i7core_dev);
Hidetoshi Seto2aa9be42010-08-20 04:25:00 -03001295 free_i7core_dev(i7core_dev);
Mauro Carvalho Chehab39300e72010-08-11 23:40:15 -03001296 }
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001297}
1298
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001299static void __init i7core_xeon_pci_fixup(const struct pci_id_table *table)
Keith Manntheybc2d7242009-09-03 00:05:05 -03001300{
1301 struct pci_dev *pdev = NULL;
1302 int i;
Mauro Carvalho Chehab54a08ab2010-08-19 15:51:00 -03001303
Keith Manntheybc2d7242009-09-03 00:05:05 -03001304 /*
David Sterbae7bf0682010-12-27 16:51:15 +01001305 * On Xeon 55xx, the Intel Quick Path Arch Generic Non-core pci buses
Keith Manntheybc2d7242009-09-03 00:05:05 -03001306 * aren't announced by acpi. So, we need to use a legacy scan probing
1307 * to detect them
1308 */
Vernon Mauerybd9e19c2010-05-18 19:02:50 -03001309 while (table && table->descr) {
1310 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, table->descr[0].dev_id, NULL);
1311 if (unlikely(!pdev)) {
1312 for (i = 0; i < MAX_SOCKET_BUSES; i++)
1313 pcibios_scan_specific_bus(255-i);
1314 }
Mauro Carvalho Chehabbda14282010-06-30 01:41:35 -03001315 pci_dev_put(pdev);
Vernon Mauerybd9e19c2010-05-18 19:02:50 -03001316 table++;
Keith Manntheybc2d7242009-09-03 00:05:05 -03001317 }
1318}
1319
Mauro Carvalho Chehabbda14282010-06-30 01:41:35 -03001320static unsigned i7core_pci_lastbus(void)
1321{
1322 int last_bus = 0, bus;
1323 struct pci_bus *b = NULL;
1324
1325 while ((b = pci_find_next_bus(b)) != NULL) {
1326 bus = b->number;
1327 debugf0("Found bus %d\n", bus);
1328 if (bus > last_bus)
1329 last_bus = bus;
1330 }
1331
1332 debugf0("Last bus %d\n", last_bus);
1333
1334 return last_bus;
1335}
1336
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001337/*
Hidetoshi Seto64c10f62010-08-20 04:28:14 -03001338 * i7core_get_all_devices Find and perform 'get' operation on the MCH's
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001339 * device/functions we want to reference for this driver
1340 *
1341 * Need to 'get' device 16 func 1 and func 2
1342 */
Hidetoshi Setob197cba2010-08-20 04:24:31 -03001343static int i7core_get_onedevice(struct pci_dev **prev,
1344 const struct pci_id_table *table,
1345 const unsigned devno,
1346 const unsigned last_bus)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001347{
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001348 struct i7core_dev *i7core_dev;
Hidetoshi Setob197cba2010-08-20 04:24:31 -03001349 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001350
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -03001351 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -03001352 u8 bus = 0;
1353 u8 socket = 0;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001354
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001355 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001356 dev_descr->dev_id, *prev);
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001357
Mauro Carvalho Chehab224e8712011-03-17 17:02:59 -03001358 /*
1359 * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core regs
1360 * is at addr 8086:2c40, instead of 8086:2c41. So, we need
1361 * to probe for the alternate address in case of failure
1362 */
1363 if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_I7_NONCORE && !pdev)
1364 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1365 PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT, *prev);
1366
1367 if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE && !pdev)
1368 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1369 PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT,
1370 *prev);
1371
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001372 if (!pdev) {
1373 if (*prev) {
1374 *prev = pdev;
1375 return 0;
Mauro Carvalho Chehabd1fd4fb2009-07-10 18:39:53 -03001376 }
1377
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001378 if (dev_descr->optional)
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001379 return 0;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001380
Vernon Mauerybd9e19c2010-05-18 19:02:50 -03001381 if (devno == 0)
1382 return -ENODEV;
1383
Daniel J Bluemanab089372010-07-23 23:16:52 +01001384 i7core_printk(KERN_INFO,
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001385 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001386 dev_descr->dev, dev_descr->func,
1387 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001388
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001389 /* End of list, leave */
1390 return -ENODEV;
1391 }
1392 bus = pdev->bus->number;
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -03001393
Mauro Carvalho Chehabbda14282010-06-30 01:41:35 -03001394 socket = last_bus - bus;
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001395
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001396 i7core_dev = get_i7core_dev(socket);
1397 if (!i7core_dev) {
Hidetoshi Seto848b2f72010-08-20 04:24:44 -03001398 i7core_dev = alloc_i7core_dev(socket, table);
Hidetoshi Seto28966372010-08-20 04:28:51 -03001399 if (!i7core_dev) {
1400 pci_dev_put(pdev);
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001401 return -ENOMEM;
Hidetoshi Seto28966372010-08-20 04:28:51 -03001402 }
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001403 }
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001404
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001405 if (i7core_dev->pdev[devno]) {
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001406 i7core_printk(KERN_ERR,
1407 "Duplicated device for "
1408 "dev %02x:%02x.%d PCI ID %04x:%04x\n",
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001409 bus, dev_descr->dev, dev_descr->func,
1410 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001411 pci_dev_put(pdev);
1412 return -ENODEV;
1413 }
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001414
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001415 i7core_dev->pdev[devno] = pdev;
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001416
1417 /* Sanity check */
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001418 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1419 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001420 i7core_printk(KERN_ERR,
1421 "Device PCI ID %04x:%04x "
1422 "has dev %02x:%02x.%d instead of dev %02x:%02x.%d\n",
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001423 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001424 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001425 bus, dev_descr->dev, dev_descr->func);
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001426 return -ENODEV;
1427 }
1428
1429 /* Be sure that the device is enabled */
1430 if (unlikely(pci_enable_device(pdev) < 0)) {
1431 i7core_printk(KERN_ERR,
1432 "Couldn't enable "
1433 "dev %02x:%02x.%d PCI ID %04x:%04x\n",
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001434 bus, dev_descr->dev, dev_descr->func,
1435 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001436 return -ENODEV;
1437 }
1438
Mauro Carvalho Chehabd4c27792009-09-05 04:12:02 -03001439 debugf0("Detected socket %d dev %02x:%02x.%d PCI ID %04x:%04x\n",
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001440 socket, bus, dev_descr->dev,
1441 dev_descr->func,
1442 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001443
Mauro Carvalho Chehaba3e15412010-08-21 08:52:41 -03001444 /*
1445 * As stated on drivers/pci/search.c, the reference count for
1446 * @from is always decremented if it is not %NULL. So, as we need
1447 * to get all devices up to null, we need to do a get for the device
1448 */
1449 pci_dev_get(pdev);
1450
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001451 *prev = pdev;
1452
1453 return 0;
1454}
1455
Hidetoshi Seto64c10f62010-08-20 04:28:14 -03001456static int i7core_get_all_devices(void)
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001457{
Mauro Carvalho Chehab3c52cc52010-10-24 11:12:28 -02001458 int i, rc, last_bus;
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001459 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehab3c52cc52010-10-24 11:12:28 -02001460 const struct pci_id_table *table = pci_dev_table;
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001461
Mauro Carvalho Chehabbda14282010-06-30 01:41:35 -03001462 last_bus = i7core_pci_lastbus();
1463
Mauro Carvalho Chehab3c52cc52010-10-24 11:12:28 -02001464 while (table && table->descr) {
Vernon Mauerybd9e19c2010-05-18 19:02:50 -03001465 for (i = 0; i < table->n_devs; i++) {
1466 pdev = NULL;
1467 do {
Hidetoshi Setob197cba2010-08-20 04:24:31 -03001468 rc = i7core_get_onedevice(&pdev, table, i,
Mauro Carvalho Chehabbda14282010-06-30 01:41:35 -03001469 last_bus);
Vernon Mauerybd9e19c2010-05-18 19:02:50 -03001470 if (rc < 0) {
1471 if (i == 0) {
1472 i = table->n_devs;
1473 break;
1474 }
1475 i7core_put_all_devices();
1476 return -ENODEV;
1477 }
1478 } while (pdev);
1479 }
Mauro Carvalho Chehab3c52cc52010-10-24 11:12:28 -02001480 table++;
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001481 }
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001482
Mauro Carvalho Chehabc77720b2009-07-18 10:43:08 -03001483 return 0;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001484}
1485
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001486static int mci_bind_devs(struct mem_ctl_info *mci,
1487 struct i7core_dev *i7core_dev)
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001488{
1489 struct i7core_pvt *pvt = mci->pvt_info;
1490 struct pci_dev *pdev;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001491 int i, func, slot;
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03001492 char *family;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001493
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03001494 pvt->is_registered = false;
1495 pvt->enable_scrub = false;
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03001496 for (i = 0; i < i7core_dev->n_devs; i++) {
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001497 pdev = i7core_dev->pdev[i];
1498 if (!pdev)
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03001499 continue;
1500
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001501 func = PCI_FUNC(pdev->devfn);
1502 slot = PCI_SLOT(pdev->devfn);
1503 if (slot == 3) {
1504 if (unlikely(func > MAX_MCR_FUNC))
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001505 goto error;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001506 pvt->pci_mcr[func] = pdev;
1507 } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
1508 if (unlikely(func > MAX_CHAN_FUNC))
1509 goto error;
1510 pvt->pci_ch[slot - 4][func] = pdev;
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03001511 } else if (!slot && !func) {
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001512 pvt->pci_noncore = pdev;
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03001513
1514 /* Detect the processor family */
1515 switch (pdev->device) {
1516 case PCI_DEVICE_ID_INTEL_I7_NONCORE:
1517 family = "Xeon 35xx/ i7core";
1518 pvt->enable_scrub = false;
1519 break;
1520 case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT:
1521 family = "i7-800/i5-700";
1522 pvt->enable_scrub = false;
1523 break;
1524 case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE:
1525 family = "Xeon 34xx";
1526 pvt->enable_scrub = false;
1527 break;
1528 case PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT:
1529 family = "Xeon 55xx";
1530 pvt->enable_scrub = true;
1531 break;
1532 case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2:
1533 family = "Xeon 56xx / i7-900";
1534 pvt->enable_scrub = true;
1535 break;
1536 default:
1537 family = "unknown";
1538 pvt->enable_scrub = false;
1539 }
1540 debugf0("Detected a processor type %s\n", family);
1541 } else
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001542 goto error;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001543
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001544 debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
1545 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1546 pdev, i7core_dev->socket);
Mauro Carvalho Chehab14d2c082009-09-02 23:52:36 -03001547
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001548 if (PCI_SLOT(pdev->devfn) == 3 &&
1549 PCI_FUNC(pdev->devfn) == 2)
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03001550 pvt->is_registered = true;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001551 }
Mauro Carvalho Chehabe9bd2e72009-07-09 22:14:35 -03001552
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03001553 return 0;
1554
1555error:
1556 i7core_printk(KERN_ERR, "Device %d, function %d "
1557 "is out of the expected range\n",
1558 slot, func);
1559 return -EINVAL;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001560}
1561
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001562/****************************************************************************
1563 Error check routines
1564 ****************************************************************************/
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001565static void i7core_rdimm_update_csrow(struct mem_ctl_info *mci,
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001566 const int chan,
1567 const int dimm,
1568 const int add)
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001569{
1570 char *msg;
1571 struct i7core_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001572 int row = pvt->csrow_map[chan][dimm], i;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001573
1574 for (i = 0; i < add; i++) {
1575 msg = kasprintf(GFP_KERNEL, "Corrected error "
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001576 "(Socket=%d channel=%d dimm=%d)",
1577 pvt->i7core_dev->socket, chan, dimm);
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001578
1579 edac_mc_handle_fbd_ce(mci, row, 0, msg);
1580 kfree (msg);
1581 }
1582}
1583
1584static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001585 const int chan,
1586 const int new0,
1587 const int new1,
1588 const int new2)
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001589{
1590 struct i7core_pvt *pvt = mci->pvt_info;
1591 int add0 = 0, add1 = 0, add2 = 0;
1592 /* Updates CE counters if it is not the first time here */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001593 if (pvt->ce_count_available) {
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001594 /* Updates CE counters */
1595
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001596 add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
1597 add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
1598 add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001599
1600 if (add2 < 0)
1601 add2 += 0x7fff;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001602 pvt->rdimm_ce_count[chan][2] += add2;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001603
1604 if (add1 < 0)
1605 add1 += 0x7fff;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001606 pvt->rdimm_ce_count[chan][1] += add1;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001607
1608 if (add0 < 0)
1609 add0 += 0x7fff;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001610 pvt->rdimm_ce_count[chan][0] += add0;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001611 } else
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001612 pvt->ce_count_available = 1;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001613
1614 /* Store the new values */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001615 pvt->rdimm_last_ce_count[chan][2] = new2;
1616 pvt->rdimm_last_ce_count[chan][1] = new1;
1617 pvt->rdimm_last_ce_count[chan][0] = new0;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001618
1619 /*updated the edac core */
1620 if (add0 != 0)
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001621 i7core_rdimm_update_csrow(mci, chan, 0, add0);
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001622 if (add1 != 0)
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001623 i7core_rdimm_update_csrow(mci, chan, 1, add1);
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001624 if (add2 != 0)
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001625 i7core_rdimm_update_csrow(mci, chan, 2, add2);
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001626
1627}
1628
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001629static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001630{
1631 struct i7core_pvt *pvt = mci->pvt_info;
1632 u32 rcv[3][2];
1633 int i, new0, new1, new2;
1634
1635 /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001636 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001637 &rcv[0][0]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001638 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001639 &rcv[0][1]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001640 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001641 &rcv[1][0]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001642 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001643 &rcv[1][1]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001644 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001645 &rcv[2][0]);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001646 pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001647 &rcv[2][1]);
1648 for (i = 0 ; i < 3; i++) {
1649 debugf3("MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
1650 (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
1651 /*if the channel has 3 dimms*/
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001652 if (pvt->channel[i].dimms > 2) {
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001653 new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
1654 new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
1655 new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
1656 } else {
1657 new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
1658 DIMM_BOT_COR_ERR(rcv[i][0]);
1659 new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
1660 DIMM_BOT_COR_ERR(rcv[i][1]);
1661 new2 = 0;
1662 }
1663
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001664 i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001665 }
1666}
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001667
1668/* This function is based on the device 3 function 4 registers as described on:
1669 * Intel Xeon Processor 5500 Series Datasheet Volume 2
1670 * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
1671 * also available at:
1672 * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
1673 */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001674static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001675{
1676 struct i7core_pvt *pvt = mci->pvt_info;
1677 u32 rcv1, rcv0;
1678 int new0, new1, new2;
1679
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001680 if (!pvt->pci_mcr[4]) {
Mauro Carvalho Chehabb9905382009-08-05 21:36:35 -03001681 debugf0("%s MCR registers not found\n", __func__);
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001682 return;
1683 }
1684
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001685 /* Corrected test errors */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001686 pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
1687 pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001688
1689 /* Store the new values */
1690 new2 = DIMM2_COR_ERR(rcv1);
1691 new1 = DIMM1_COR_ERR(rcv0);
1692 new0 = DIMM0_COR_ERR(rcv0);
1693
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001694 /* Updates CE counters if it is not the first time here */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001695 if (pvt->ce_count_available) {
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001696 /* Updates CE counters */
1697 int add0, add1, add2;
1698
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001699 add2 = new2 - pvt->udimm_last_ce_count[2];
1700 add1 = new1 - pvt->udimm_last_ce_count[1];
1701 add0 = new0 - pvt->udimm_last_ce_count[0];
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001702
1703 if (add2 < 0)
1704 add2 += 0x7fff;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001705 pvt->udimm_ce_count[2] += add2;
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001706
1707 if (add1 < 0)
1708 add1 += 0x7fff;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001709 pvt->udimm_ce_count[1] += add1;
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001710
1711 if (add0 < 0)
1712 add0 += 0x7fff;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001713 pvt->udimm_ce_count[0] += add0;
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001714
1715 if (add0 | add1 | add2)
1716 i7core_printk(KERN_ERR, "New Corrected error(s): "
1717 "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
1718 add0, add1, add2);
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001719 } else
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001720 pvt->ce_count_available = 1;
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001721
1722 /* Store the new values */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001723 pvt->udimm_last_ce_count[2] = new2;
1724 pvt->udimm_last_ce_count[1] = new1;
1725 pvt->udimm_last_ce_count[0] = new0;
Mauro Carvalho Chehab442305b2009-06-22 22:48:29 -03001726}
1727
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001728/*
1729 * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
1730 * Architectures Software Developer’s Manual Volume 3B.
Mauro Carvalho Chehabf237fcf2009-07-15 19:53:24 -03001731 * Nehalem are defined as family 0x06, model 0x1a
1732 *
1733 * The MCA registers used here are the following ones:
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001734 * struct mce field MCA Register
Mauro Carvalho Chehabf237fcf2009-07-15 19:53:24 -03001735 * m->status MSR_IA32_MC8_STATUS
1736 * m->addr MSR_IA32_MC8_ADDR
1737 * m->misc MSR_IA32_MC8_MISC
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001738 * In the case of Nehalem, the error information is masked at .status and .misc
1739 * fields
1740 */
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001741static void i7core_mce_output_error(struct mem_ctl_info *mci,
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03001742 const struct mce *m)
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001743{
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001744 struct i7core_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehaba6395392009-07-17 10:54:23 -03001745 char *type, *optype, *err, *msg;
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001746 unsigned long error = m->status & 0x1ff0000l;
Mauro Carvalho Chehaba6395392009-07-17 10:54:23 -03001747 u32 optypenum = (m->status >> 4) & 0x07;
Mathias Krause8cf2d232011-08-18 09:17:00 +02001748 u32 core_err_cnt = (m->status >> 38) & 0x7fff;
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001749 u32 dimm = (m->misc >> 16) & 0x3;
1750 u32 channel = (m->misc >> 18) & 0x3;
1751 u32 syndrome = m->misc >> 32;
1752 u32 errnum = find_first_bit(&error, 32);
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001753 int csrow;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001754
Mauro Carvalho Chehabc5d34522009-07-17 10:28:15 -03001755 if (m->mcgstatus & 1)
1756 type = "FATAL";
1757 else
1758 type = "NON_FATAL";
1759
Mauro Carvalho Chehaba6395392009-07-17 10:54:23 -03001760 switch (optypenum) {
Mauro Carvalho Chehabb9905382009-08-05 21:36:35 -03001761 case 0:
1762 optype = "generic undef request";
1763 break;
1764 case 1:
1765 optype = "read error";
1766 break;
1767 case 2:
1768 optype = "write error";
1769 break;
1770 case 3:
1771 optype = "addr/cmd error";
1772 break;
1773 case 4:
1774 optype = "scrubbing error";
1775 break;
1776 default:
1777 optype = "reserved";
1778 break;
Mauro Carvalho Chehaba6395392009-07-17 10:54:23 -03001779 }
1780
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001781 switch (errnum) {
1782 case 16:
1783 err = "read ECC error";
1784 break;
1785 case 17:
1786 err = "RAS ECC error";
1787 break;
1788 case 18:
1789 err = "write parity error";
1790 break;
1791 case 19:
1792 err = "redundacy loss";
1793 break;
1794 case 20:
1795 err = "reserved";
1796 break;
1797 case 21:
1798 err = "memory range error";
1799 break;
1800 case 22:
1801 err = "RTID out of range";
1802 break;
1803 case 23:
1804 err = "address parity error";
1805 break;
1806 case 24:
1807 err = "byte enable parity error";
1808 break;
1809 default:
1810 err = "unknown";
1811 }
1812
Mauro Carvalho Chehabf237fcf2009-07-15 19:53:24 -03001813 /* FIXME: should convert addr into bank and rank information */
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001814 msg = kasprintf(GFP_ATOMIC,
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001815 "%s (addr = 0x%08llx, cpu=%d, Dimm=%d, Channel=%d, "
Mauro Carvalho Chehaba6395392009-07-17 10:54:23 -03001816 "syndrome=0x%08x, count=%d, Err=%08llx:%08llx (%s: %s))\n",
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001817 type, (long long) m->addr, m->cpu, dimm, channel,
Mauro Carvalho Chehaba6395392009-07-17 10:54:23 -03001818 syndrome, core_err_cnt, (long long)m->status,
1819 (long long)m->misc, optype, err);
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001820
1821 debugf0("%s", msg);
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001822
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001823 csrow = pvt->csrow_map[channel][dimm];
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001824
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001825 /* Call the helper to output message */
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001826 if (m->mcgstatus & 1)
1827 edac_mc_handle_fbd_ue(mci, csrow, 0,
1828 0 /* FIXME: should be channel here */, msg);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001829 else if (!pvt->is_registered)
Mauro Carvalho Chehabb4e8f0b2009-09-02 23:49:59 -03001830 edac_mc_handle_fbd_ce(mci, csrow,
1831 0 /* FIXME: should be channel here */, msg);
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001832
1833 kfree(msg);
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001834}
1835
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03001836/*
Mauro Carvalho Chehab87d1d272009-06-22 22:48:29 -03001837 * i7core_check_error Retrieve and process errors reported by the
1838 * hardware. Called by the Core module.
1839 */
1840static void i7core_check_error(struct mem_ctl_info *mci)
1841{
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001842 struct i7core_pvt *pvt = mci->pvt_info;
1843 int i;
1844 unsigned count = 0;
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001845 struct mce *m;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001846
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001847 /*
1848 * MCE first step: Copy all mce errors into a temporary buffer
1849 * We use a double buffering here, to reduce the risk of
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001850 * losing an error.
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001851 */
1852 smp_rmb();
Mauro Carvalho Chehab321ece42009-10-08 13:11:08 -03001853 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1854 % MCE_LOG_LEN;
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001855 if (!count)
Vernon Mauery8a311e12010-04-16 19:40:19 -03001856 goto check_ce_error;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001857
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001858 m = pvt->mce_outentry;
Mauro Carvalho Chehab321ece42009-10-08 13:11:08 -03001859 if (pvt->mce_in + count > MCE_LOG_LEN) {
1860 unsigned l = MCE_LOG_LEN - pvt->mce_in;
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001861
1862 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1863 smp_wmb();
1864 pvt->mce_in = 0;
1865 count -= l;
1866 m += l;
1867 }
1868 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1869 smp_wmb();
1870 pvt->mce_in += count;
1871
1872 smp_rmb();
1873 if (pvt->mce_overrun) {
1874 i7core_printk(KERN_ERR, "Lost %d memory errors\n",
1875 pvt->mce_overrun);
1876 smp_wmb();
1877 pvt->mce_overrun = 0;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001878 }
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001879
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001880 /*
1881 * MCE second step: parse errors and display
1882 */
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001883 for (i = 0; i < count; i++)
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001884 i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001885
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001886 /*
1887 * Now, let's increment CE error counts
1888 */
Vernon Mauery8a311e12010-04-16 19:40:19 -03001889check_ce_error:
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001890 if (!pvt->is_registered)
1891 i7core_udimm_check_mc_ecc_err(mci);
1892 else
1893 i7core_rdimm_check_mc_ecc_err(mci);
Mauro Carvalho Chehab87d1d272009-06-22 22:48:29 -03001894}
1895
1896/*
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001897 * i7core_mce_check_error Replicates mcelog routine to get errors
1898 * This routine simply queues mcelog errors, and
1899 * return. The error itself should be handled later
1900 * by i7core_check_error.
Mauro Carvalho Chehab6e103be2009-10-05 09:40:09 -03001901 * WARNING: As this routine should be called at NMI time, extra care should
1902 * be taken to avoid deadlocks, and to be as fast as possible.
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001903 */
Borislav Petkov4140c542011-07-18 11:24:46 -03001904static int i7core_mce_check_error(struct notifier_block *nb, unsigned long val,
1905 void *data)
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001906{
Borislav Petkov4140c542011-07-18 11:24:46 -03001907 struct mce *mce = (struct mce *)data;
1908 struct i7core_dev *i7_dev;
1909 struct mem_ctl_info *mci;
1910 struct i7core_pvt *pvt;
1911
1912 i7_dev = get_i7core_dev(mce->socketid);
1913 if (!i7_dev)
1914 return NOTIFY_BAD;
1915
1916 mci = i7_dev->mci;
1917 pvt = mci->pvt_info;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001918
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001919 /*
1920 * Just let mcelog handle it if the error is
1921 * outside the memory controller
1922 */
1923 if (((mce->status & 0xffff) >> 7) != 1)
Borislav Petkov4140c542011-07-18 11:24:46 -03001924 return NOTIFY_DONE;
Mauro Carvalho Chehab8a2f1182009-07-15 19:01:08 -03001925
Mauro Carvalho Chehabf237fcf2009-07-15 19:53:24 -03001926 /* Bank 8 registers are the only ones that we know how to handle */
1927 if (mce->bank != 8)
Borislav Petkov4140c542011-07-18 11:24:46 -03001928 return NOTIFY_DONE;
Mauro Carvalho Chehabf237fcf2009-07-15 19:53:24 -03001929
Randy Dunlap3b918c12009-11-08 01:36:40 -02001930#ifdef CONFIG_SMP
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001931 /* Only handle if it is the right mc controller */
Thomas Renninger50340862011-06-22 05:40:06 -03001932 if (mce->socketid != pvt->i7core_dev->socket)
Borislav Petkov4140c542011-07-18 11:24:46 -03001933 return NOTIFY_DONE;
Randy Dunlap3b918c12009-11-08 01:36:40 -02001934#endif
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03001935
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001936 smp_rmb();
Mauro Carvalho Chehab321ece42009-10-08 13:11:08 -03001937 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001938 smp_wmb();
1939 pvt->mce_overrun++;
Borislav Petkov4140c542011-07-18 11:24:46 -03001940 return NOTIFY_DONE;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001941 }
Mauro Carvalho Chehab6e103be2009-10-05 09:40:09 -03001942
1943 /* Copy memory error at the ringbuffer */
1944 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
Mauro Carvalho Chehabca9c90b2009-10-04 10:15:40 -03001945 smp_wmb();
Mauro Carvalho Chehab321ece42009-10-08 13:11:08 -03001946 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001947
Mauro Carvalho Chehabc5d34522009-07-17 10:28:15 -03001948 /* Handle fatal errors immediately */
1949 if (mce->mcgstatus & 1)
1950 i7core_check_error(mci);
1951
David Sterbae7bf0682010-12-27 16:51:15 +01001952 /* Advise mcelog that the errors were handled */
Borislav Petkov4140c542011-07-18 11:24:46 -03001953 return NOTIFY_STOP;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03001954}
1955
Borislav Petkov4140c542011-07-18 11:24:46 -03001956static struct notifier_block i7_mce_dec = {
1957 .notifier_call = i7core_mce_check_error,
1958};
1959
Nils Carlson535e9c72011-08-08 06:21:26 -03001960struct memdev_dmi_entry {
1961 u8 type;
1962 u8 length;
1963 u16 handle;
1964 u16 phys_mem_array_handle;
1965 u16 mem_err_info_handle;
1966 u16 total_width;
1967 u16 data_width;
1968 u16 size;
1969 u8 form;
1970 u8 device_set;
1971 u8 device_locator;
1972 u8 bank_locator;
1973 u8 memory_type;
1974 u16 type_detail;
1975 u16 speed;
1976 u8 manufacturer;
1977 u8 serial_number;
1978 u8 asset_tag;
1979 u8 part_number;
1980 u8 attributes;
1981 u32 extended_size;
1982 u16 conf_mem_clk_speed;
1983} __attribute__((__packed__));
1984
1985
1986/*
1987 * Decode the DRAM Clock Frequency, be paranoid, make sure that all
1988 * memory devices show the same speed, and if they don't then consider
1989 * all speeds to be invalid.
1990 */
1991static void decode_dclk(const struct dmi_header *dh, void *_dclk_freq)
1992{
1993 int *dclk_freq = _dclk_freq;
1994 u16 dmi_mem_clk_speed;
1995
1996 if (*dclk_freq == -1)
1997 return;
1998
1999 if (dh->type == DMI_ENTRY_MEM_DEVICE) {
2000 struct memdev_dmi_entry *memdev_dmi_entry =
2001 (struct memdev_dmi_entry *)dh;
2002 unsigned long conf_mem_clk_speed_offset =
2003 (unsigned long)&memdev_dmi_entry->conf_mem_clk_speed -
2004 (unsigned long)&memdev_dmi_entry->type;
2005 unsigned long speed_offset =
2006 (unsigned long)&memdev_dmi_entry->speed -
2007 (unsigned long)&memdev_dmi_entry->type;
2008
2009 /* Check that a DIMM is present */
2010 if (memdev_dmi_entry->size == 0)
2011 return;
2012
2013 /*
2014 * Pick the configured speed if it's available, otherwise
2015 * pick the DIMM speed, or we don't have a speed.
2016 */
2017 if (memdev_dmi_entry->length > conf_mem_clk_speed_offset) {
2018 dmi_mem_clk_speed =
2019 memdev_dmi_entry->conf_mem_clk_speed;
2020 } else if (memdev_dmi_entry->length > speed_offset) {
2021 dmi_mem_clk_speed = memdev_dmi_entry->speed;
2022 } else {
2023 *dclk_freq = -1;
2024 return;
2025 }
2026
2027 if (*dclk_freq == 0) {
2028 /* First pass, speed was 0 */
2029 if (dmi_mem_clk_speed > 0) {
2030 /* Set speed if a valid speed is read */
2031 *dclk_freq = dmi_mem_clk_speed;
2032 } else {
2033 /* Otherwise we don't have a valid speed */
2034 *dclk_freq = -1;
2035 }
2036 } else if (*dclk_freq > 0 &&
2037 *dclk_freq != dmi_mem_clk_speed) {
2038 /*
2039 * If we have a speed, check that all DIMMS are the same
2040 * speed, otherwise set the speed as invalid.
2041 */
2042 *dclk_freq = -1;
2043 }
2044 }
2045}
2046
2047/*
2048 * The default DCLK frequency is used as a fallback if we
2049 * fail to find anything reliable in the DMI. The value
2050 * is taken straight from the datasheet.
2051 */
2052#define DEFAULT_DCLK_FREQ 800
2053
2054static int get_dclk_freq(void)
2055{
2056 int dclk_freq = 0;
2057
2058 dmi_walk(decode_dclk, (void *)&dclk_freq);
2059
2060 if (dclk_freq < 1)
2061 return DEFAULT_DCLK_FREQ;
2062
2063 return dclk_freq;
2064}
2065
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002066/*
2067 * set_sdram_scrub_rate This routine sets byte/sec bandwidth scrub rate
2068 * to hardware according to SCRUBINTERVAL formula
2069 * found in datasheet.
2070 */
2071static int set_sdram_scrub_rate(struct mem_ctl_info *mci, u32 new_bw)
2072{
2073 struct i7core_pvt *pvt = mci->pvt_info;
2074 struct pci_dev *pdev;
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002075 u32 dw_scrub;
2076 u32 dw_ssr;
2077
2078 /* Get data from the MC register, function 2 */
2079 pdev = pvt->pci_mcr[2];
2080 if (!pdev)
2081 return -ENODEV;
2082
2083 pci_read_config_dword(pdev, MC_SCRUB_CONTROL, &dw_scrub);
2084
2085 if (new_bw == 0) {
2086 /* Prepare to disable petrol scrub */
2087 dw_scrub &= ~STARTSCRUB;
2088 /* Stop the patrol scrub engine */
Nils Carlson535e9c72011-08-08 06:21:26 -03002089 write_and_test(pdev, MC_SCRUB_CONTROL,
2090 dw_scrub & ~SCRUBINTERVAL_MASK);
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002091
2092 /* Get current status of scrub rate and set bit to disable */
2093 pci_read_config_dword(pdev, MC_SSRCONTROL, &dw_ssr);
2094 dw_ssr &= ~SSR_MODE_MASK;
2095 dw_ssr |= SSR_MODE_DISABLE;
2096 } else {
Nils Carlson535e9c72011-08-08 06:21:26 -03002097 const int cache_line_size = 64;
2098 const u32 freq_dclk_mhz = pvt->dclk_freq;
2099 unsigned long long scrub_interval;
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002100 /*
2101 * Translate the desired scrub rate to a register value and
Nils Carlson535e9c72011-08-08 06:21:26 -03002102 * program the corresponding register value.
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002103 */
Nils Carlson535e9c72011-08-08 06:21:26 -03002104 scrub_interval = (unsigned long long)freq_dclk_mhz *
2105 cache_line_size * 1000000 / new_bw;
2106
2107 if (!scrub_interval || scrub_interval > SCRUBINTERVAL_MASK)
2108 return -EINVAL;
2109
2110 dw_scrub = SCRUBINTERVAL_MASK & scrub_interval;
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002111
2112 /* Start the patrol scrub engine */
2113 pci_write_config_dword(pdev, MC_SCRUB_CONTROL,
2114 STARTSCRUB | dw_scrub);
2115
2116 /* Get current status of scrub rate and set bit to enable */
2117 pci_read_config_dword(pdev, MC_SSRCONTROL, &dw_ssr);
2118 dw_ssr &= ~SSR_MODE_MASK;
2119 dw_ssr |= SSR_MODE_ENABLE;
2120 }
2121 /* Disable or enable scrubbing */
2122 pci_write_config_dword(pdev, MC_SSRCONTROL, dw_ssr);
2123
2124 return new_bw;
2125}
2126
2127/*
2128 * get_sdram_scrub_rate This routine convert current scrub rate value
2129 * into byte/sec bandwidth accourding to
2130 * SCRUBINTERVAL formula found in datasheet.
2131 */
2132static int get_sdram_scrub_rate(struct mem_ctl_info *mci)
2133{
2134 struct i7core_pvt *pvt = mci->pvt_info;
2135 struct pci_dev *pdev;
2136 const u32 cache_line_size = 64;
Nils Carlson535e9c72011-08-08 06:21:26 -03002137 const u32 freq_dclk_mhz = pvt->dclk_freq;
2138 unsigned long long scrub_rate;
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002139 u32 scrubval;
2140
2141 /* Get data from the MC register, function 2 */
2142 pdev = pvt->pci_mcr[2];
2143 if (!pdev)
2144 return -ENODEV;
2145
2146 /* Get current scrub control data */
2147 pci_read_config_dword(pdev, MC_SCRUB_CONTROL, &scrubval);
2148
2149 /* Mask highest 8-bits to 0 */
Nils Carlson535e9c72011-08-08 06:21:26 -03002150 scrubval &= SCRUBINTERVAL_MASK;
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002151 if (!scrubval)
2152 return 0;
2153
2154 /* Calculate scrub rate value into byte/sec bandwidth */
Nils Carlson535e9c72011-08-08 06:21:26 -03002155 scrub_rate = (unsigned long long)freq_dclk_mhz *
2156 1000000 * cache_line_size / scrubval;
2157 return (int)scrub_rate;
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002158}
2159
2160static void enable_sdram_scrub_setting(struct mem_ctl_info *mci)
2161{
2162 struct i7core_pvt *pvt = mci->pvt_info;
2163 u32 pci_lock;
2164
2165 /* Unlock writes to pci registers */
2166 pci_read_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, &pci_lock);
2167 pci_lock &= ~0x3;
2168 pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL,
2169 pci_lock | MC_CFG_UNLOCK);
2170
2171 mci->set_sdram_scrub_rate = set_sdram_scrub_rate;
2172 mci->get_sdram_scrub_rate = get_sdram_scrub_rate;
2173}
2174
2175static void disable_sdram_scrub_setting(struct mem_ctl_info *mci)
2176{
2177 struct i7core_pvt *pvt = mci->pvt_info;
2178 u32 pci_lock;
2179
2180 /* Lock writes to pci registers */
2181 pci_read_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, &pci_lock);
2182 pci_lock &= ~0x3;
2183 pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL,
2184 pci_lock | MC_CFG_LOCK);
2185}
2186
Hidetoshi Setoa3aa0a42010-08-20 04:25:18 -03002187static void i7core_pci_ctl_create(struct i7core_pvt *pvt)
2188{
2189 pvt->i7core_pci = edac_pci_create_generic_ctl(
2190 &pvt->i7core_dev->pdev[0]->dev,
2191 EDAC_MOD_STR);
2192 if (unlikely(!pvt->i7core_pci))
Mauro Carvalho Chehabf9902f22010-08-21 09:42:05 -03002193 i7core_printk(KERN_WARNING,
2194 "Unable to setup PCI error report via EDAC\n");
Hidetoshi Setoa3aa0a42010-08-20 04:25:18 -03002195}
2196
2197static void i7core_pci_ctl_release(struct i7core_pvt *pvt)
2198{
2199 if (likely(pvt->i7core_pci))
2200 edac_pci_release_generic_ctl(pvt->i7core_pci);
2201 else
2202 i7core_printk(KERN_ERR,
2203 "Couldn't find mem_ctl_info for socket %d\n",
2204 pvt->i7core_dev->socket);
2205 pvt->i7core_pci = NULL;
2206}
2207
Hidetoshi Seto1c6edbb2010-08-20 04:32:33 -03002208static void i7core_unregister_mci(struct i7core_dev *i7core_dev)
2209{
2210 struct mem_ctl_info *mci = i7core_dev->mci;
2211 struct i7core_pvt *pvt;
2212
2213 if (unlikely(!mci || !mci->pvt_info)) {
2214 debugf0("MC: " __FILE__ ": %s(): dev = %p\n",
2215 __func__, &i7core_dev->pdev[0]->dev);
2216
2217 i7core_printk(KERN_ERR, "Couldn't find mci handler\n");
2218 return;
2219 }
2220
2221 pvt = mci->pvt_info;
2222
2223 debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
2224 __func__, mci, &i7core_dev->pdev[0]->dev);
2225
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002226 /* Disable scrubrate setting */
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03002227 if (pvt->enable_scrub)
2228 disable_sdram_scrub_setting(mci);
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002229
Borislav Petkov4140c542011-07-18 11:24:46 -03002230 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, &i7_mce_dec);
Hidetoshi Seto1c6edbb2010-08-20 04:32:33 -03002231
2232 /* Disable EDAC polling */
2233 i7core_pci_ctl_release(pvt);
2234
2235 /* Remove MC sysfs nodes */
2236 edac_mc_del_mc(mci->dev);
2237
2238 debugf1("%s: free mci struct\n", mci->ctl_name);
2239 kfree(mci->ctl_name);
2240 edac_mc_free(mci);
2241 i7core_dev->mci = NULL;
2242}
2243
Hidetoshi Setoaace4282010-08-20 04:32:45 -03002244static int i7core_register_mci(struct i7core_dev *i7core_dev)
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002245{
2246 struct mem_ctl_info *mci;
2247 struct i7core_pvt *pvt;
Hidetoshi Setoaace4282010-08-20 04:32:45 -03002248 int rc, channels, csrows;
2249
2250 /* Check the number of active and not disabled channels */
2251 rc = i7core_get_active_channels(i7core_dev->socket, &channels, &csrows);
2252 if (unlikely(rc < 0))
2253 return rc;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002254
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002255 /* allocate a new MC control structure */
Hidetoshi Setoaace4282010-08-20 04:32:45 -03002256 mci = edac_mc_alloc(sizeof(*pvt), csrows, channels, i7core_dev->socket);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002257 if (unlikely(!mci))
2258 return -ENOMEM;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002259
Mauro Carvalho Chehab3cfd0142010-08-10 23:23:46 -03002260 debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
2261 __func__, mci, &i7core_dev->pdev[0]->dev);
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002262
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002263 pvt = mci->pvt_info;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002264 memset(pvt, 0, sizeof(*pvt));
Mauro Carvalho Chehab67166af2009-07-15 06:56:23 -03002265
Mauro Carvalho Chehab6d37d242010-08-20 12:48:26 -03002266 /* Associates i7core_dev and mci for future usage */
2267 pvt->i7core_dev = i7core_dev;
2268 i7core_dev->mci = mci;
2269
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -03002270 /*
2271 * FIXME: how to handle RDDR3 at MCI level? It is possible to have
2272 * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
2273 * memory channels
2274 */
2275 mci->mtype_cap = MEM_FLAG_DDR3;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002276 mci->edac_ctl_cap = EDAC_FLAG_NONE;
2277 mci->edac_cap = EDAC_FLAG_NONE;
2278 mci->mod_name = "i7core_edac.c";
2279 mci->mod_ver = I7CORE_REVISION;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002280 mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
2281 i7core_dev->socket);
2282 mci->dev_name = pci_name(i7core_dev->pdev[0]);
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002283 mci->ctl_page_to_phys = NULL;
Mauro Carvalho Chehab1288c182010-08-10 18:57:01 -03002284
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002285 /* Store pci devices at mci for faster access */
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002286 rc = mci_bind_devs(mci, i7core_dev);
Mauro Carvalho Chehab41fcb7f2009-06-22 22:48:31 -03002287 if (unlikely(rc < 0))
Hidetoshi Seto628c5dd2010-08-20 04:28:40 -03002288 goto fail0;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002289
Hidetoshi Seto59398132010-08-20 04:28:25 -03002290 if (pvt->is_registered)
2291 mci->mc_driver_sysfs_attributes = i7core_sysfs_rdimm_attrs;
2292 else
2293 mci->mc_driver_sysfs_attributes = i7core_sysfs_udimm_attrs;
2294
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002295 /* Get dimm basic config */
Hidetoshi Seto2e5185f2010-08-20 04:32:56 -03002296 get_dimm_config(mci);
Hidetoshi Seto59398132010-08-20 04:28:25 -03002297 /* record ptr to the generic device */
2298 mci->dev = &i7core_dev->pdev[0]->dev;
2299 /* Set the function pointer to an actual operation function */
2300 mci->edac_check = i7core_check_error;
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002301
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002302 /* Enable scrubrate setting */
Mauro Carvalho Chehab27100db2011-08-04 21:35:27 -03002303 if (pvt->enable_scrub)
2304 enable_sdram_scrub_setting(mci);
Samuel Gabrielssone8b6a122011-03-30 10:21:23 -03002305
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002306 /* add this new MC control structure to EDAC's list of MCs */
Mauro Carvalho Chehabb7c76152009-06-22 22:48:30 -03002307 if (unlikely(edac_mc_add_mc(mci))) {
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002308 debugf0("MC: " __FILE__
2309 ": %s(): failed edac_mc_add_mc()\n", __func__);
2310 /* FIXME: perhaps some code should go here that disables error
2311 * reporting if we just enabled it
2312 */
Mauro Carvalho Chehabb7c76152009-06-22 22:48:30 -03002313
2314 rc = -EINVAL;
Hidetoshi Seto628c5dd2010-08-20 04:28:40 -03002315 goto fail0;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002316 }
2317
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03002318 /* Default error mask is any memory */
Mauro Carvalho Chehabef708b52009-06-22 22:48:30 -03002319 pvt->inject.channel = 0;
Mauro Carvalho Chehab194a40f2009-06-22 22:48:28 -03002320 pvt->inject.dimm = -1;
2321 pvt->inject.rank = -1;
2322 pvt->inject.bank = -1;
2323 pvt->inject.page = -1;
2324 pvt->inject.col = -1;
2325
Hidetoshi Setoa3aa0a42010-08-20 04:25:18 -03002326 /* allocating generic PCI control info */
2327 i7core_pci_ctl_create(pvt);
2328
Nils Carlson535e9c72011-08-08 06:21:26 -03002329 /* DCLK for scrub rate setting */
2330 pvt->dclk_freq = get_dclk_freq();
2331
Borislav Petkov4140c542011-07-18 11:24:46 -03002332 atomic_notifier_chain_register(&x86_mce_decoder_chain, &i7_mce_dec);
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002333
Hidetoshi Seto628c5dd2010-08-20 04:28:40 -03002334 return 0;
2335
Hidetoshi Seto628c5dd2010-08-20 04:28:40 -03002336fail0:
2337 kfree(mci->ctl_name);
2338 edac_mc_free(mci);
Hidetoshi Seto1c6edbb2010-08-20 04:32:33 -03002339 i7core_dev->mci = NULL;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002340 return rc;
2341}
2342
2343/*
2344 * i7core_probe Probe for ONE instance of device to see if it is
2345 * present.
2346 * return:
2347 * 0 for FOUND a device
2348 * < 0 for error code
2349 */
Mauro Carvalho Chehab2d95d812010-06-30 01:42:21 -03002350
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002351static int __devinit i7core_probe(struct pci_dev *pdev,
2352 const struct pci_device_id *id)
2353{
Mauro Carvalho Chehab40557592010-11-30 08:14:30 -02002354 int rc, count = 0;
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002355 struct i7core_dev *i7core_dev;
2356
Mauro Carvalho Chehab2d95d812010-06-30 01:42:21 -03002357 /* get the pci devices we want to reserve for our use */
2358 mutex_lock(&i7core_edac_lock);
2359
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002360 /*
Mauro Carvalho Chehabd4c27792009-09-05 04:12:02 -03002361 * All memory controllers are allocated at the first pass.
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002362 */
Mauro Carvalho Chehab2d95d812010-06-30 01:42:21 -03002363 if (unlikely(probed >= 1)) {
2364 mutex_unlock(&i7core_edac_lock);
Mauro Carvalho Chehab76a7bd82010-10-24 11:36:19 -02002365 return -ENODEV;
Mauro Carvalho Chehab2d95d812010-06-30 01:42:21 -03002366 }
2367 probed++;
Mauro Carvalho Chehabde06eee2009-10-14 08:02:40 -03002368
Hidetoshi Seto64c10f62010-08-20 04:28:14 -03002369 rc = i7core_get_all_devices();
Mauro Carvalho Chehabf4742942009-09-05 02:35:08 -03002370 if (unlikely(rc < 0))
2371 goto fail0;
2372
2373 list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
Mauro Carvalho Chehab40557592010-11-30 08:14:30 -02002374 count++;
Hidetoshi Setoaace4282010-08-20 04:32:45 -03002375 rc = i7core_register_mci(i7core_dev);
Mauro Carvalho Chehabd4c27792009-09-05 04:12:02 -03002376 if (unlikely(rc < 0))
2377 goto fail1;
Mauro Carvalho Chehabd5381642009-07-09 22:06:41 -03002378 }
2379
Mauro Carvalho Chehab40557592010-11-30 08:14:30 -02002380 /*
2381 * Nehalem-EX uses a different memory controller. However, as the
2382 * memory controller is not visible on some Nehalem/Nehalem-EP, we
2383 * need to indirectly probe via a X58 PCI device. The same devices
2384 * are found on (some) Nehalem-EX. So, on those machines, the
2385 * probe routine needs to return -ENODEV, as the actual Memory
2386 * Controller registers won't be detected.
2387 */
2388 if (!count) {
2389 rc = -ENODEV;
2390 goto fail1;
2391 }
2392
2393 i7core_printk(KERN_INFO,
2394 "Driver loaded, %d memory controller(s) found.\n",
2395 count);
Mauro Carvalho Chehab8f331902009-06-22 22:48:29 -03002396
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03002397 mutex_unlock(&i7core_edac_lock);
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002398 return 0;
2399
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03002400fail1:
Mauro Carvalho Chehab88ef5ea2010-08-20 15:39:38 -03002401 list_for_each_entry(i7core_dev, &i7core_edac_list, list)
2402 i7core_unregister_mci(i7core_dev);
2403
Mauro Carvalho Chehab13d6e9b2009-09-05 12:15:20 -03002404 i7core_put_all_devices();
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03002405fail0:
2406 mutex_unlock(&i7core_edac_lock);
Mauro Carvalho Chehabb7c76152009-06-22 22:48:30 -03002407 return rc;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002408}
2409
2410/*
2411 * i7core_remove destructor for one instance of device
2412 *
2413 */
2414static void __devexit i7core_remove(struct pci_dev *pdev)
2415{
Hidetoshi Seto64c10f62010-08-20 04:28:14 -03002416 struct i7core_dev *i7core_dev;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002417
2418 debugf0(__FILE__ ": %s()\n", __func__);
2419
Mauro Carvalho Chehab22e6bcb2009-09-05 23:06:50 -03002420 /*
2421 * we have a trouble here: pdev value for removal will be wrong, since
2422 * it will point to the X58 register used to detect that the machine
2423 * is a Nehalem or upper design. However, due to the way several PCI
2424 * devices are grouped together to provide MC functionality, we need
2425 * to use a different method for releasing the devices
2426 */
Mauro Carvalho Chehab87d1d272009-06-22 22:48:29 -03002427
Mauro Carvalho Chehab66607702009-09-05 00:52:11 -03002428 mutex_lock(&i7core_edac_lock);
Hidetoshi Seto71fe0172010-08-20 04:29:47 -03002429
2430 if (unlikely(!probed)) {
2431 mutex_unlock(&i7core_edac_lock);
2432 return;
2433 }
2434
Mauro Carvalho Chehab88ef5ea2010-08-20 15:39:38 -03002435 list_for_each_entry(i7core_dev, &i7core_edac_list, list)
2436 i7core_unregister_mci(i7core_dev);
Hidetoshi Seto64c10f62010-08-20 04:28:14 -03002437
2438 /* Release PCI resources */
2439 i7core_put_all_devices();
2440
Mauro Carvalho Chehab2d95d812010-06-30 01:42:21 -03002441 probed--;
2442
Mauro Carvalho Chehab22e6bcb2009-09-05 23:06:50 -03002443 mutex_unlock(&i7core_edac_lock);
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002444}
2445
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002446MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
2447
2448/*
2449 * i7core_driver pci_driver structure for this module
2450 *
2451 */
2452static struct pci_driver i7core_driver = {
2453 .name = "i7core_edac",
2454 .probe = i7core_probe,
2455 .remove = __devexit_p(i7core_remove),
2456 .id_table = i7core_pci_tbl,
2457};
2458
2459/*
2460 * i7core_init Module entry function
2461 * Try to initialize this module for its devices
2462 */
2463static int __init i7core_init(void)
2464{
2465 int pci_rc;
2466
2467 debugf2("MC: " __FILE__ ": %s()\n", __func__);
2468
2469 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2470 opstate_init();
2471
Mauro Carvalho Chehab54a08ab2010-08-19 15:51:00 -03002472 if (use_pci_fixup)
2473 i7core_xeon_pci_fixup(pci_dev_table);
Keith Manntheybc2d7242009-09-03 00:05:05 -03002474
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002475 pci_rc = pci_register_driver(&i7core_driver);
2476
Mauro Carvalho Chehab3ef288a2009-09-02 23:43:33 -03002477 if (pci_rc >= 0)
2478 return 0;
2479
2480 i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
2481 pci_rc);
2482
2483 return pci_rc;
Mauro Carvalho Chehaba0c36a12009-06-22 22:41:15 -03002484}
2485
2486/*
2487 * i7core_exit() Module exit function
2488 * Unregister the driver
2489 */
2490static void __exit i7core_exit(void)
2491{
2492 debugf2("MC: " __FILE__ ": %s()\n", __func__);
2493 pci_unregister_driver(&i7core_driver);
2494}
2495
2496module_init(i7core_init);
2497module_exit(i7core_exit);
2498
2499MODULE_LICENSE("GPL");
2500MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2501MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
2502MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
2503 I7CORE_REVISION);
2504
2505module_param(edac_op_state, int, 0444);
2506MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");