blob: 1acf57ba4c86bdc95b2e6d4d8281295f5568f6fc [file] [log] [blame]
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001/* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
2 *
3 * This driver supports the memory controllers found on the Intel
4 * processor family Sandy Bridge.
5 *
6 * This file may be distributed under the terms of the
7 * GNU General Public License version 2 only.
8 *
9 * Copyright (c) 2011 by:
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -020010 * Mauro Carvalho Chehab
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020011 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/pci.h>
16#include <linux/pci_ids.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/edac.h>
20#include <linux/mmzone.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020021#include <linux/smp.h>
22#include <linux/bitmap.h>
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -030023#include <linux/math64.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020024#include <asm/processor.h>
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -020025#include <asm/mce.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020026
27#include "edac_core.h"
28
29/* Static vars */
30static LIST_HEAD(sbridge_edac_list);
31static DEFINE_MUTEX(sbridge_edac_lock);
32static int probed;
33
34/*
35 * Alter this version for the module when modifications are made
36 */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030037#define SBRIDGE_REVISION " Ver: 1.1.0 "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020038#define EDAC_MOD_STR "sbridge_edac"
39
40/*
41 * Debug macros
42 */
43#define sbridge_printk(level, fmt, arg...) \
44 edac_printk(level, "sbridge", fmt, ##arg)
45
46#define sbridge_mc_printk(mci, level, fmt, arg...) \
47 edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
48
49/*
50 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
51 */
52#define GET_BITFIELD(v, lo, hi) \
Chen, Gong10ef6b02013-10-18 14:29:07 -070053 (((v) & GENMASK_ULL(hi, lo)) >> (lo))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020054
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020055/* Devices 12 Function 6, Offsets 0x80 to 0xcc */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -030056static const u32 sbridge_dram_rule[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020057 0x80, 0x88, 0x90, 0x98, 0xa0,
58 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
59};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020060
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030061static const u32 ibridge_dram_rule[] = {
62 0x60, 0x68, 0x70, 0x78, 0x80,
63 0x88, 0x90, 0x98, 0xa0, 0xa8,
64 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
65 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
66};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020067
68#define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
69#define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
70#define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
71#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -030072#define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020073
74static char *get_dram_attr(u32 reg)
75{
76 switch(DRAM_ATTR(reg)) {
77 case 0:
78 return "DRAM";
79 case 1:
80 return "MMCFG";
81 case 2:
82 return "NXM";
83 default:
84 return "unknown";
85 }
86}
87
Aristeu Rozanskief1ce512013-10-30 13:27:01 -030088static const u32 sbridge_interleave_list[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020089 0x84, 0x8c, 0x94, 0x9c, 0xa4,
90 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
91};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020092
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030093static const u32 ibridge_interleave_list[] = {
94 0x64, 0x6c, 0x74, 0x7c, 0x84,
95 0x8c, 0x94, 0x9c, 0xa4, 0xac,
96 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
97 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
98};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020099
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300100struct interleave_pkg {
101 unsigned char start;
102 unsigned char end;
103};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200104
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300105static const struct interleave_pkg sbridge_interleave_pkg[] = {
106 { 0, 2 },
107 { 3, 5 },
108 { 8, 10 },
109 { 11, 13 },
110 { 16, 18 },
111 { 19, 21 },
112 { 24, 26 },
113 { 27, 29 },
114};
115
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300116static const struct interleave_pkg ibridge_interleave_pkg[] = {
117 { 0, 3 },
118 { 4, 7 },
119 { 8, 11 },
120 { 12, 15 },
121 { 16, 19 },
122 { 20, 23 },
123 { 24, 27 },
124 { 28, 31 },
125};
126
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300127static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
128 int interleave)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200129{
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300130 return GET_BITFIELD(reg, table[interleave].start,
131 table[interleave].end);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200132}
133
134/* Devices 12 Function 7 */
135
136#define TOLM 0x80
137#define TOHM 0x84
Tony Luckf7cf2a22014-10-29 10:36:50 -0700138#define HASWELL_TOLM 0xd0
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300139#define HASWELL_TOHM_0 0xd4
140#define HASWELL_TOHM_1 0xd8
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200141
142#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
143#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
144
145/* Device 13 Function 6 */
146
147#define SAD_TARGET 0xf0
148
149#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
150
151#define SAD_CONTROL 0xf4
152
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200153/* Device 14 function 0 */
154
155static const u32 tad_dram_rule[] = {
156 0x40, 0x44, 0x48, 0x4c,
157 0x50, 0x54, 0x58, 0x5c,
158 0x60, 0x64, 0x68, 0x6c,
159};
160#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
161
162#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
163#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
164#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
165#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
166#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
167#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
168#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
169
170/* Device 15, function 0 */
171
172#define MCMTR 0x7c
173
174#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
175#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
176#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
177
178/* Device 15, function 1 */
179
180#define RASENABLES 0xac
181#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
182
183/* Device 15, functions 2-5 */
184
185static const int mtr_regs[] = {
186 0x80, 0x84, 0x88,
187};
188
189#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
190#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
191#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
192#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
193#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
194
195static const u32 tad_ch_nilv_offset[] = {
196 0x90, 0x94, 0x98, 0x9c,
197 0xa0, 0xa4, 0xa8, 0xac,
198 0xb0, 0xb4, 0xb8, 0xbc,
199};
200#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
201#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
202
203static const u32 rir_way_limit[] = {
204 0x108, 0x10c, 0x110, 0x114, 0x118,
205};
206#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
207
208#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
209#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200210
211#define MAX_RIR_WAY 8
212
213static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
214 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
215 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
216 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
217 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
218 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
219};
220
221#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
222#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
223
224/* Device 16, functions 2-7 */
225
226/*
227 * FIXME: Implement the error count reads directly
228 */
229
230static const u32 correrrcnt[] = {
231 0x104, 0x108, 0x10c, 0x110,
232};
233
234#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
235#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
236#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
237#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
238
239static const u32 correrrthrsld[] = {
240 0x11c, 0x120, 0x124, 0x128,
241};
242
243#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
244#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
245
246
247/* Device 17, function 0 */
248
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300249#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200250
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300251#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200252
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200253/*
254 * sbridge structs
255 */
256
Seth Jennings351fc4a2014-09-05 14:28:47 -0500257#define NUM_CHANNELS 4
258#define MAX_DIMMS 3 /* Max DIMMS per channel */
259#define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200260
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300261enum type {
262 SANDY_BRIDGE,
263 IVY_BRIDGE,
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300264 HASWELL,
Tony Luck1f395812014-12-02 09:27:30 -0800265 BROADWELL,
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300266};
267
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300268struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200269struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300270 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300271 u32 mcmtr;
272 u32 rankcfgr;
273 u64 (*get_tolm)(struct sbridge_pvt *pvt);
274 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300275 u64 (*rir_limit)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300276 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300277 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300278 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300279 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300280 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300281 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300282 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300283 struct pci_dev *pci_vtd;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200284};
285
286struct sbridge_channel {
287 u32 ranks;
288 u32 dimms;
289};
290
291struct pci_id_descr {
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -0300292 int dev_id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200293 int optional;
294};
295
296struct pci_id_table {
297 const struct pci_id_descr *descr;
298 int n_devs;
299};
300
301struct sbridge_dev {
302 struct list_head list;
303 u8 bus, mc;
304 u8 node_id, source_id;
305 struct pci_dev **pdev;
306 int n_devs;
307 struct mem_ctl_info *mci;
308};
309
310struct sbridge_pvt {
311 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300312 struct pci_dev *pci_sad0, *pci_sad1;
313 struct pci_dev *pci_ha0, *pci_ha1;
314 struct pci_dev *pci_br0, *pci_br1;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300315 struct pci_dev *pci_ha1_ta;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200316 struct pci_dev *pci_tad[NUM_CHANNELS];
317
318 struct sbridge_dev *sbridge_dev;
319
320 struct sbridge_info info;
321 struct sbridge_channel channel[NUM_CHANNELS];
322
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200323 /* Memory type detection */
324 bool is_mirrored, is_lockstep, is_close_pg;
325
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200326 /* Fifo double buffers */
327 struct mce mce_entry[MCE_LOG_LEN];
328 struct mce mce_outentry[MCE_LOG_LEN];
329
330 /* Fifo in/out counters */
331 unsigned mce_in, mce_out;
332
333 /* Count indicator to show errors not got */
334 unsigned mce_overrun;
335
336 /* Memory description */
337 u64 tolm, tohm;
338};
339
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300340#define PCI_DESCR(device_id, opt) \
341 .dev_id = (device_id), \
Luck, Tonyde4772c2013-03-28 09:59:15 -0700342 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200343
344static const struct pci_id_descr pci_dev_descr_sbridge[] = {
345 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300346 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200347
348 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300349 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
350 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
351 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
352 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
353 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
354 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
355 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200356
357 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300358 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
359 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200360
361 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300362 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200363};
364
365#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
366static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
367 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
368 {0,} /* 0 terminated list. */
369};
370
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300371/* This changes depending if 1HA or 2HA:
372 * 1HA:
373 * 0x0eb8 (17.0) is DDRIO0
374 * 2HA:
375 * 0x0ebc (17.4) is DDRIO0
376 */
377#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
378#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
379
380/* pci ids */
381#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
382#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
383#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
384#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
385#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
386#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
387#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
388#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
389#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
390#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
391#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
392#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
393#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
394#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
395#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
396
397static const struct pci_id_descr pci_dev_descr_ibridge[] = {
398 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300399 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300400
401 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300402 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
403 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
404 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
405 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
406 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
407 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300408
409 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300410 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300411
412 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300413 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
414 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300415
416 /* Optional, mode 2HA */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300417 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300418#if 0
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300419 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
420 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300421#endif
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300422 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
423 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300424
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300425 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
426 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300427};
428
429static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
430 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
431 {0,} /* 0 terminated list. */
432};
433
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300434/* Haswell support */
435/* EN processor:
436 * - 1 IMC
437 * - 3 DDR3 channels, 2 DPC per channel
438 * EP processor:
439 * - 1 or 2 IMC
440 * - 4 DDR4 channels, 3 DPC per channel
441 * EP 4S processor:
442 * - 2 IMC
443 * - 4 DDR4 channels, 3 DPC per channel
444 * EX processor:
445 * - 2 IMC
446 * - each IMC interfaces with a SMI 2 channel
447 * - each SMI channel interfaces with a scalable memory buffer
448 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
449 */
Tony Luck1f395812014-12-02 09:27:30 -0800450#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300451#define HASWELL_HASYSDEFEATURE2 0x84
452#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
453#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
454#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
455#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
456#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
457#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
458#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
459#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
460#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
461#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
462#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
463#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
464#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
465#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
466#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
467#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
468#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
469#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
470static const struct pci_id_descr pci_dev_descr_haswell[] = {
471 /* first item must be the HA */
472 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
473
474 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
475 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
476
477 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
478
479 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
480 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
481 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
482 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
483 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
484 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
485
486 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
487
488 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
489 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
490 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
491 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
492 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
493 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
494};
495
496static const struct pci_id_table pci_dev_descr_haswell_table[] = {
497 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
498 {0,} /* 0 terminated list. */
499};
500
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200501/*
Tony Luck1f395812014-12-02 09:27:30 -0800502 * Broadwell support
503 *
504 * DE processor:
505 * - 1 IMC
506 * - 2 DDR3 channels, 2 DPC per channel
507 */
508#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
509#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
510#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
511#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
512#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
513#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
514#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
515#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
516#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
517#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
518#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
519
520static const struct pci_id_descr pci_dev_descr_broadwell[] = {
521 /* first item must be the HA */
522 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
523
524 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
525 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
526
527 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
528 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
529 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
530 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
531 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 0) },
532 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 0) },
533 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
534};
535
536static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
537 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
538 {0,} /* 0 terminated list. */
539};
540
541/*
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200542 * pci_device_id table for which devices we are looking for
543 */
Jingoo Hanba935f42013-12-06 10:23:08 +0100544static const struct pci_device_id sbridge_pci_tbl[] = {
Andy Lutomirskid0585cd2014-08-14 14:45:41 -0700545 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0)},
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300546 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300547 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0)},
Tony Luck1f395812014-12-02 09:27:30 -0800548 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0)},
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200549 {0,} /* 0 terminated list. */
550};
551
552
553/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700554 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200555 ****************************************************************************/
556
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300557static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200558{
559 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300560 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200561
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300562 if (type == HASWELL)
563 max = 8;
564
565 if (ranks > max) {
566 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
567 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200568 return -EINVAL;
569 }
570
571 return ranks;
572}
573
574static inline int numrow(u32 mtr)
575{
576 int rows = (RANK_WIDTH_BITS(mtr) + 12);
577
578 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300579 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
580 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200581 return -EINVAL;
582 }
583
584 return 1 << rows;
585}
586
587static inline int numcol(u32 mtr)
588{
589 int cols = (COL_WIDTH_BITS(mtr) + 10);
590
591 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300592 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
593 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200594 return -EINVAL;
595 }
596
597 return 1 << cols;
598}
599
600static struct sbridge_dev *get_sbridge_dev(u8 bus)
601{
602 struct sbridge_dev *sbridge_dev;
603
604 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
605 if (sbridge_dev->bus == bus)
606 return sbridge_dev;
607 }
608
609 return NULL;
610}
611
612static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
613 const struct pci_id_table *table)
614{
615 struct sbridge_dev *sbridge_dev;
616
617 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
618 if (!sbridge_dev)
619 return NULL;
620
621 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
622 GFP_KERNEL);
623 if (!sbridge_dev->pdev) {
624 kfree(sbridge_dev);
625 return NULL;
626 }
627
628 sbridge_dev->bus = bus;
629 sbridge_dev->n_devs = table->n_devs;
630 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
631
632 return sbridge_dev;
633}
634
635static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
636{
637 list_del(&sbridge_dev->list);
638 kfree(sbridge_dev->pdev);
639 kfree(sbridge_dev);
640}
641
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300642static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
643{
644 u32 reg;
645
646 /* Address range is 32:28 */
647 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
648 return GET_TOLM(reg);
649}
650
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300651static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
652{
653 u32 reg;
654
655 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
656 return GET_TOHM(reg);
657}
658
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300659static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
660{
661 u32 reg;
662
663 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
664
665 return GET_TOLM(reg);
666}
667
668static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
669{
670 u32 reg;
671
672 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
673
674 return GET_TOHM(reg);
675}
676
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300677static u64 rir_limit(u32 reg)
678{
679 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
680}
681
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300682static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
683{
684 u32 reg;
685 enum mem_type mtype;
686
687 if (pvt->pci_ddrio) {
688 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
689 &reg);
690 if (GET_BITFIELD(reg, 11, 11))
691 /* FIXME: Can also be LRDIMM */
692 mtype = MEM_RDDR3;
693 else
694 mtype = MEM_DDR3;
695 } else
696 mtype = MEM_UNKNOWN;
697
698 return mtype;
699}
700
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300701static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
702{
703 u32 reg;
704 bool registered = false;
705 enum mem_type mtype = MEM_UNKNOWN;
706
707 if (!pvt->pci_ddrio)
708 goto out;
709
710 pci_read_config_dword(pvt->pci_ddrio,
711 HASWELL_DDRCRCLKCONTROLS, &reg);
712 /* Is_Rdimm */
713 if (GET_BITFIELD(reg, 16, 16))
714 registered = true;
715
716 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
717 if (GET_BITFIELD(reg, 14, 14)) {
718 if (registered)
719 mtype = MEM_RDDR4;
720 else
721 mtype = MEM_DDR4;
722 } else {
723 if (registered)
724 mtype = MEM_RDDR3;
725 else
726 mtype = MEM_DDR3;
727 }
728
729out:
730 return mtype;
731}
732
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300733static u8 get_node_id(struct sbridge_pvt *pvt)
734{
735 u32 reg;
736 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
737 return GET_BITFIELD(reg, 0, 2);
738}
739
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300740static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
741{
742 u32 reg;
743
744 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
745 return GET_BITFIELD(reg, 0, 3);
746}
747
748static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
749{
750 u32 reg;
751
Tony Luckf7cf2a22014-10-29 10:36:50 -0700752 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
753 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300754}
755
756static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
757{
758 u64 rc;
759 u32 reg;
760
761 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
762 rc = GET_BITFIELD(reg, 26, 31);
763 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
764 rc = ((reg << 6) | rc) << 26;
765
766 return rc | 0x1ffffff;
767}
768
769static u64 haswell_rir_limit(u32 reg)
770{
771 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
772}
773
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300774static inline u8 sad_pkg_socket(u8 pkg)
775{
776 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -0300777 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300778}
779
780static inline u8 sad_pkg_ha(u8 pkg)
781{
782 return (pkg >> 2) & 0x1;
783}
784
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200785/****************************************************************************
786 Memory check routines
787 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300788static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200789{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300790 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200791
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300792 do {
793 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
794 if (pdev && pdev->bus->number == bus)
795 break;
796 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200797
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300798 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200799}
800
801/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300802 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300803 * @bus: Device bus
804 * @type: Memory controller type
805 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
806 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200807 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300808static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200809{
810 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300811 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200812
Tony Luck1f395812014-12-02 09:27:30 -0800813 switch (type) {
814 case IVY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300815 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -0800816 break;
817 case HASWELL:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300818 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -0800819 break;
820 case SANDY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300821 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
Tony Luck1f395812014-12-02 09:27:30 -0800822 break;
823 case BROADWELL:
824 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
825 break;
826 default:
827 return -ENODEV;
828 }
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300829
830 pdev = get_pdev_same_bus(bus, id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200831 if (!pdev) {
832 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300833 "%04x:%04x! on bus %02d\n",
834 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200835 return -ENODEV;
836 }
837
838 pci_read_config_dword(pdev, MCMTR, &mcmtr);
839 if (!IS_ECC_ENABLED(mcmtr)) {
840 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
841 return -ENODEV;
842 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200843 return 0;
844}
845
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300846static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200847{
848 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300849 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300850 unsigned i, j, banks, ranks, rows, cols, npages;
851 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200852 u32 reg;
853 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200854 enum mem_type mtype;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200855
Tony Luck1f395812014-12-02 09:27:30 -0800856 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300857 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
858 else
859 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
860
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200861 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
862
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300863 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -0300864 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
865 pvt->sbridge_dev->mc,
866 pvt->sbridge_dev->node_id,
867 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200868
869 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
870 if (IS_MIRROR_ENABLED(reg)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300871 edac_dbg(0, "Memory mirror is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200872 pvt->is_mirrored = true;
873 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300874 edac_dbg(0, "Memory mirror is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200875 pvt->is_mirrored = false;
876 }
877
878 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
879 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300880 edac_dbg(0, "Lockstep is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200881 mode = EDAC_S8ECD8ED;
882 pvt->is_lockstep = true;
883 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300884 edac_dbg(0, "Lockstep is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200885 mode = EDAC_S4ECD4ED;
886 pvt->is_lockstep = false;
887 }
888 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300889 edac_dbg(0, "address map is on closed page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200890 pvt->is_close_pg = true;
891 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300892 edac_dbg(0, "address map is on open page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200893 pvt->is_close_pg = false;
894 }
895
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300896 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300897 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300898 edac_dbg(0, "Memory is registered\n");
899 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -0700900 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300901 else
902 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200903
Tony Luckfec53af2014-12-02 09:41:58 -0800904 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300905 banks = 16;
906 else
907 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200908
909 for (i = 0; i < NUM_CHANNELS; i++) {
910 u32 mtr;
911
912 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300913 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
914 i, j, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200915 pci_read_config_dword(pvt->pci_tad[i],
916 mtr_regs[j], &mtr);
Joe Perches956b9ba2012-04-29 17:08:39 -0300917 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200918 if (IS_DIMM_PRESENT(mtr)) {
919 pvt->channel[i].dimms++;
920
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300921 ranks = numrank(pvt->info.type, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200922 rows = numrow(mtr);
923 cols = numcol(mtr);
924
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300925 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200926 npages = MiB_TO_PAGES(size);
927
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300928 edac_dbg(0, "mc#%d: channel %d, dimm %d, %Ld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -0300929 pvt->sbridge_dev->mc, i, j,
930 size, npages,
931 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200932
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300933 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300934 dimm->grain = 32;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300935 switch (banks) {
936 case 16:
937 dimm->dtype = DEV_X16;
938 break;
939 case 8:
940 dimm->dtype = DEV_X8;
941 break;
942 case 4:
943 dimm->dtype = DEV_X4;
944 break;
945 }
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300946 dimm->mtype = mtype;
947 dimm->edac_mode = mode;
948 snprintf(dimm->label, sizeof(dimm->label),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200949 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
950 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200951 }
952 }
953 }
954
955 return 0;
956}
957
958static void get_memory_layout(const struct mem_ctl_info *mci)
959{
960 struct sbridge_pvt *pvt = mci->pvt_info;
961 int i, j, k, n_sads, n_tads, sad_interl;
962 u32 reg;
963 u64 limit, prv = 0;
964 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +0100965 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200966 u32 rir_way;
967
968 /*
969 * Step 1) Get TOLM/TOHM ranges
970 */
971
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300972 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200973 tmp_mb = (1 + pvt->tolm) >> 20;
974
Jim Snow8c009102014-11-18 14:51:09 +0100975 gb = div_u64_rem(tmp_mb, 1024, &mb);
976 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
977 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200978
979 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300980 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200981 tmp_mb = (1 + pvt->tohm) >> 20;
982
Jim Snow8c009102014-11-18 14:51:09 +0100983 gb = div_u64_rem(tmp_mb, 1024, &mb);
984 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
985 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200986
987 /*
988 * Step 2) Get SAD range and SAD Interleave list
989 * TAD registers contain the interleave wayness. However, it
990 * seems simpler to just discover it indirectly, with the
991 * algorithm bellow.
992 */
993 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300994 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200995 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300996 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200997 &reg);
998 limit = SAD_LIMIT(reg);
999
1000 if (!DRAM_RULE_ENABLE(reg))
1001 continue;
1002
1003 if (limit <= prv)
1004 break;
1005
1006 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001007 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001008 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1009 n_sads,
1010 get_dram_attr(reg),
Jim Snow8c009102014-11-18 14:51:09 +01001011 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001012 ((u64)tmp_mb) << 20L,
1013 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
1014 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001015 prv = limit;
1016
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001017 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001018 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001019 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001020 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001021 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1022 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001023 break;
1024
Joe Perches956b9ba2012-04-29 17:08:39 -03001025 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001026 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001027 }
1028 }
1029
1030 /*
1031 * Step 3) Get TAD range
1032 */
1033 prv = 0;
1034 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1035 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1036 &reg);
1037 limit = TAD_LIMIT(reg);
1038 if (limit <= prv)
1039 break;
1040 tmp_mb = (limit + 1) >> 20;
1041
Jim Snow8c009102014-11-18 14:51:09 +01001042 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001043 edac_dbg(0, "TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
Jim Snow8c009102014-11-18 14:51:09 +01001044 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001045 ((u64)tmp_mb) << 20L,
1046 (u32)TAD_SOCK(reg),
1047 (u32)TAD_CH(reg),
1048 (u32)TAD_TGT0(reg),
1049 (u32)TAD_TGT1(reg),
1050 (u32)TAD_TGT2(reg),
1051 (u32)TAD_TGT3(reg),
1052 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001053 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001054 }
1055
1056 /*
1057 * Step 4) Get TAD offsets, per each channel
1058 */
1059 for (i = 0; i < NUM_CHANNELS; i++) {
1060 if (!pvt->channel[i].dimms)
1061 continue;
1062 for (j = 0; j < n_tads; j++) {
1063 pci_read_config_dword(pvt->pci_tad[i],
1064 tad_ch_nilv_offset[j],
1065 &reg);
1066 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001067 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001068 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1069 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001070 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001071 ((u64)tmp_mb) << 20L,
1072 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001073 }
1074 }
1075
1076 /*
1077 * Step 6) Get RIR Wayness/Limit, per each channel
1078 */
1079 for (i = 0; i < NUM_CHANNELS; i++) {
1080 if (!pvt->channel[i].dimms)
1081 continue;
1082 for (j = 0; j < MAX_RIR_RANGES; j++) {
1083 pci_read_config_dword(pvt->pci_tad[i],
1084 rir_way_limit[j],
1085 &reg);
1086
1087 if (!IS_RIR_VALID(reg))
1088 continue;
1089
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001090 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001091 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001092 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001093 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1094 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001095 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001096 ((u64)tmp_mb) << 20L,
1097 rir_way,
1098 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001099
1100 for (k = 0; k < rir_way; k++) {
1101 pci_read_config_dword(pvt->pci_tad[i],
1102 rir_offset[j][k],
1103 &reg);
1104 tmp_mb = RIR_OFFSET(reg) << 6;
1105
Jim Snow8c009102014-11-18 14:51:09 +01001106 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001107 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1108 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001109 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001110 ((u64)tmp_mb) << 20L,
1111 (u32)RIR_RNK_TGT(reg),
1112 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001113 }
1114 }
1115 }
1116}
1117
Rashika Kheria8112c0c2013-12-14 19:32:09 +05301118static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001119{
1120 struct sbridge_dev *sbridge_dev;
1121
1122 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1123 if (sbridge_dev->node_id == node_id)
1124 return sbridge_dev->mci;
1125 }
1126 return NULL;
1127}
1128
1129static int get_memory_error_data(struct mem_ctl_info *mci,
1130 u64 addr,
1131 u8 *socket,
1132 long *channel_mask,
1133 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001134 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001135{
1136 struct mem_ctl_info *new_mci;
1137 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001138 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001139 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001140 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001141 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001142 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001143 u32 reg, dram_rule;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001144 u8 ch_way, sck_way, pkg, sad_ha = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001145 u32 tad_offset;
1146 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001147 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001148 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001149
1150
1151 /*
1152 * Step 0) Check if the address is at special memory ranges
1153 * The check bellow is probably enough to fill all cases where
1154 * the error is not inside a memory, except for the legacy
1155 * range (e. g. VGA addresses). It is unlikely, however, that the
1156 * memory controller would generate an error on that range.
1157 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001158 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001159 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001160 return -EINVAL;
1161 }
1162 if (addr >= (u64)pvt->tohm) {
1163 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001164 return -EINVAL;
1165 }
1166
1167 /*
1168 * Step 1) Get socket
1169 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001170 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1171 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001172 &reg);
1173
1174 if (!DRAM_RULE_ENABLE(reg))
1175 continue;
1176
1177 limit = SAD_LIMIT(reg);
1178 if (limit <= prv) {
1179 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001180 return -EINVAL;
1181 }
1182 if (addr <= limit)
1183 break;
1184 prv = limit;
1185 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001186 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001187 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001188 return -EINVAL;
1189 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001190 dram_rule = reg;
1191 *area_type = get_dram_attr(dram_rule);
1192 interleave_mode = INTERLEAVE_MODE(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001193
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001194 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001195 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001196
1197 if (pvt->info.type == SANDY_BRIDGE) {
1198 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1199 for (sad_way = 0; sad_way < 8; sad_way++) {
1200 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1201 if (sad_way > 0 && sad_interl == pkg)
1202 break;
1203 sad_interleave[sad_way] = pkg;
1204 edac_dbg(0, "SAD interleave #%d: %d\n",
1205 sad_way, sad_interleave[sad_way]);
1206 }
1207 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1208 pvt->sbridge_dev->mc,
1209 n_sads,
1210 addr,
1211 limit,
1212 sad_way + 7,
1213 !interleave_mode ? "" : "XOR[18:16]");
1214 if (interleave_mode)
1215 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1216 else
1217 idx = (addr >> 6) & 7;
1218 switch (sad_way) {
1219 case 1:
1220 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001221 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001222 case 2:
1223 idx = idx & 1;
1224 break;
1225 case 4:
1226 idx = idx & 3;
1227 break;
1228 case 8:
1229 break;
1230 default:
1231 sprintf(msg, "Can't discover socket interleave");
1232 return -EINVAL;
1233 }
1234 *socket = sad_interleave[idx];
1235 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1236 idx, sad_way, *socket);
Tony Luck1f395812014-12-02 09:27:30 -08001237 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001238 int bits, a7mode = A7MODE(dram_rule);
1239
1240 if (a7mode) {
1241 /* A7 mode swaps P9 with P6 */
1242 bits = GET_BITFIELD(addr, 7, 8) << 1;
1243 bits |= GET_BITFIELD(addr, 9, 9);
1244 } else
1245 bits = GET_BITFIELD(addr, 7, 9);
1246
1247 if (interleave_mode) {
1248 /* interleave mode will XOR {8,7,6} with {18,17,16} */
1249 idx = GET_BITFIELD(addr, 16, 18);
1250 idx ^= bits;
1251 } else
1252 idx = bits;
1253
1254 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1255 *socket = sad_pkg_socket(pkg);
1256 sad_ha = sad_pkg_ha(pkg);
1257
1258 if (a7mode) {
1259 /* MCChanShiftUpEnable */
1260 pci_read_config_dword(pvt->pci_ha0,
1261 HASWELL_HASYSDEFEATURE2, &reg);
1262 shiftup = GET_BITFIELD(reg, 22, 22);
1263 }
1264
1265 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
1266 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001267 } else {
1268 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001269 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001270 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1271 *socket = sad_pkg_socket(pkg);
1272 sad_ha = sad_pkg_ha(pkg);
1273 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1274 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001275 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001276
1277 /*
1278 * Move to the proper node structure, in order to access the
1279 * right PCI registers
1280 */
1281 new_mci = get_mci_for_node_id(*socket);
1282 if (!new_mci) {
1283 sprintf(msg, "Struct for socket #%u wasn't initialized",
1284 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001285 return -EINVAL;
1286 }
1287 mci = new_mci;
1288 pvt = mci->pvt_info;
1289
1290 /*
1291 * Step 2) Get memory channel
1292 */
1293 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001294 if (pvt->info.type == SANDY_BRIDGE)
1295 pci_ha = pvt->pci_ha0;
1296 else {
1297 if (sad_ha)
1298 pci_ha = pvt->pci_ha1;
1299 else
1300 pci_ha = pvt->pci_ha0;
1301 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001302 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001303 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001304 limit = TAD_LIMIT(reg);
1305 if (limit <= prv) {
1306 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001307 return -EINVAL;
1308 }
1309 if (addr <= limit)
1310 break;
1311 prv = limit;
1312 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001313 if (n_tads == MAX_TAD) {
1314 sprintf(msg, "Can't discover the memory channel");
1315 return -EINVAL;
1316 }
1317
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001318 ch_way = TAD_CH(reg) + 1;
1319 sck_way = TAD_SOCK(reg) + 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001320
1321 if (ch_way == 3)
1322 idx = addr >> 6;
1323 else
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001324 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001325 idx = idx % ch_way;
1326
1327 /*
1328 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1329 */
1330 switch (idx) {
1331 case 0:
1332 base_ch = TAD_TGT0(reg);
1333 break;
1334 case 1:
1335 base_ch = TAD_TGT1(reg);
1336 break;
1337 case 2:
1338 base_ch = TAD_TGT2(reg);
1339 break;
1340 case 3:
1341 base_ch = TAD_TGT3(reg);
1342 break;
1343 default:
1344 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001345 return -EINVAL;
1346 }
1347 *channel_mask = 1 << base_ch;
1348
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001349 pci_read_config_dword(pvt->pci_tad[base_ch],
1350 tad_ch_nilv_offset[n_tads],
1351 &tad_offset);
1352
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001353 if (pvt->is_mirrored) {
1354 *channel_mask |= 1 << ((base_ch + 2) % 4);
1355 switch(ch_way) {
1356 case 2:
1357 case 4:
1358 sck_xch = 1 << sck_way * (ch_way >> 1);
1359 break;
1360 default:
1361 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001362 return -EINVAL;
1363 }
1364 } else
1365 sck_xch = (1 << sck_way) * ch_way;
1366
1367 if (pvt->is_lockstep)
1368 *channel_mask |= 1 << ((base_ch + 1) % 4);
1369
1370 offset = TAD_OFFSET(tad_offset);
1371
Joe Perches956b9ba2012-04-29 17:08:39 -03001372 edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
1373 n_tads,
1374 addr,
1375 limit,
1376 (u32)TAD_SOCK(reg),
1377 ch_way,
1378 offset,
1379 idx,
1380 base_ch,
1381 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001382
1383 /* Calculate channel address */
1384 /* Remove the TAD offset */
1385
1386 if (offset > addr) {
1387 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1388 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001389 return -EINVAL;
1390 }
1391 addr -= offset;
1392 /* Store the low bits [0:6] of the addr */
1393 ch_addr = addr & 0x7f;
1394 /* Remove socket wayness and remove 6 bits */
1395 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001396 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001397#if 0
1398 /* Divide by channel way */
1399 addr = addr / ch_way;
1400#endif
1401 /* Recover the last 6 bits */
1402 ch_addr |= addr << 6;
1403
1404 /*
1405 * Step 3) Decode rank
1406 */
1407 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1408 pci_read_config_dword(pvt->pci_tad[base_ch],
1409 rir_way_limit[n_rir],
1410 &reg);
1411
1412 if (!IS_RIR_VALID(reg))
1413 continue;
1414
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001415 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001416 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001417 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1418 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01001419 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001420 limit,
1421 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001422 if (ch_addr <= limit)
1423 break;
1424 }
1425 if (n_rir == MAX_RIR_RANGES) {
1426 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1427 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001428 return -EINVAL;
1429 }
1430 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001431
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001432 if (pvt->is_close_pg)
1433 idx = (ch_addr >> 6);
1434 else
1435 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1436 idx %= 1 << rir_way;
1437
1438 pci_read_config_dword(pvt->pci_tad[base_ch],
1439 rir_offset[n_rir][idx],
1440 &reg);
1441 *rank = RIR_RNK_TGT(reg);
1442
Joe Perches956b9ba2012-04-29 17:08:39 -03001443 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1444 n_rir,
1445 ch_addr,
1446 limit,
1447 rir_way,
1448 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001449
1450 return 0;
1451}
1452
1453/****************************************************************************
1454 Device initialization routines: put/get, init/exit
1455 ****************************************************************************/
1456
1457/*
1458 * sbridge_put_all_devices 'put' all the devices that we have
1459 * reserved via 'get'
1460 */
1461static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1462{
1463 int i;
1464
Joe Perches956b9ba2012-04-29 17:08:39 -03001465 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001466 for (i = 0; i < sbridge_dev->n_devs; i++) {
1467 struct pci_dev *pdev = sbridge_dev->pdev[i];
1468 if (!pdev)
1469 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03001470 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1471 pdev->bus->number,
1472 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001473 pci_dev_put(pdev);
1474 }
1475}
1476
1477static void sbridge_put_all_devices(void)
1478{
1479 struct sbridge_dev *sbridge_dev, *tmp;
1480
1481 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1482 sbridge_put_devices(sbridge_dev);
1483 free_sbridge_dev(sbridge_dev);
1484 }
1485}
1486
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001487static int sbridge_get_onedevice(struct pci_dev **prev,
1488 u8 *num_mc,
1489 const struct pci_id_table *table,
1490 const unsigned devno)
1491{
1492 struct sbridge_dev *sbridge_dev;
1493 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001494 struct pci_dev *pdev = NULL;
1495 u8 bus = 0;
1496
Jiang Liuec5a0b32014-02-17 13:10:23 +08001497 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001498 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001499 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1500
1501 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1502 dev_descr->dev_id, *prev);
1503
1504 if (!pdev) {
1505 if (*prev) {
1506 *prev = pdev;
1507 return 0;
1508 }
1509
1510 if (dev_descr->optional)
1511 return 0;
1512
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001513 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001514 if (devno == 0)
1515 return -ENODEV;
1516
1517 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001518 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001519 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1520
1521 /* End of list, leave */
1522 return -ENODEV;
1523 }
1524 bus = pdev->bus->number;
1525
1526 sbridge_dev = get_sbridge_dev(bus);
1527 if (!sbridge_dev) {
1528 sbridge_dev = alloc_sbridge_dev(bus, table);
1529 if (!sbridge_dev) {
1530 pci_dev_put(pdev);
1531 return -ENOMEM;
1532 }
1533 (*num_mc)++;
1534 }
1535
1536 if (sbridge_dev->pdev[devno]) {
1537 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001538 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001539 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1540 pci_dev_put(pdev);
1541 return -ENODEV;
1542 }
1543
1544 sbridge_dev->pdev[devno] = pdev;
1545
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001546 /* Be sure that the device is enabled */
1547 if (unlikely(pci_enable_device(pdev) < 0)) {
1548 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001549 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001550 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1551 return -ENODEV;
1552 }
1553
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001554 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -03001555 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001556
1557 /*
1558 * As stated on drivers/pci/search.c, the reference count for
1559 * @from is always decremented if it is not %NULL. So, as we need
1560 * to get all devices up to null, we need to do a get for the device
1561 */
1562 pci_dev_get(pdev);
1563
1564 *prev = pdev;
1565
1566 return 0;
1567}
1568
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001569/*
1570 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001571 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001572 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001573 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001574 * @table: model specific table
1575 *
1576 * returns 0 in case of success or error code
1577 */
1578static int sbridge_get_all_devices(u8 *num_mc,
1579 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001580{
1581 int i, rc;
1582 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001583
1584 while (table && table->descr) {
1585 for (i = 0; i < table->n_devs; i++) {
1586 pdev = NULL;
1587 do {
1588 rc = sbridge_get_onedevice(&pdev, num_mc,
1589 table, i);
1590 if (rc < 0) {
1591 if (i == 0) {
1592 i = table->n_devs;
1593 break;
1594 }
1595 sbridge_put_all_devices();
1596 return -ENODEV;
1597 }
1598 } while (pdev);
1599 }
1600 table++;
1601 }
1602
1603 return 0;
1604}
1605
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001606static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1607 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001608{
1609 struct sbridge_pvt *pvt = mci->pvt_info;
1610 struct pci_dev *pdev;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001611 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001612
1613 for (i = 0; i < sbridge_dev->n_devs; i++) {
1614 pdev = sbridge_dev->pdev[i];
1615 if (!pdev)
1616 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001617
1618 switch (pdev->device) {
1619 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
1620 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001621 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001622 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
1623 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001624 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001625 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
1626 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001627 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001628 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
1629 pvt->pci_ha0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001630 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001631 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
1632 pvt->pci_ta = pdev;
1633 break;
1634 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
1635 pvt->pci_ras = pdev;
1636 break;
1637 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
1638 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
1639 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
1640 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
1641 {
1642 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
1643 pvt->pci_tad[id] = pdev;
1644 }
1645 break;
1646 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
1647 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001648 break;
1649 default:
1650 goto error;
1651 }
1652
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001653 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
1654 pdev->vendor, pdev->device,
Joe Perches956b9ba2012-04-29 17:08:39 -03001655 sbridge_dev->bus,
Joe Perches956b9ba2012-04-29 17:08:39 -03001656 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001657 }
1658
1659 /* Check if everything were registered */
1660 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07001661 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001662 goto enodev;
1663
1664 for (i = 0; i < NUM_CHANNELS; i++) {
1665 if (!pvt->pci_tad[i])
1666 goto enodev;
1667 }
1668 return 0;
1669
1670enodev:
1671 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1672 return -ENODEV;
1673
1674error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001675 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
1676 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001677 return -EINVAL;
1678}
1679
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001680static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1681 struct sbridge_dev *sbridge_dev)
1682{
1683 struct sbridge_pvt *pvt = mci->pvt_info;
1684 struct pci_dev *pdev, *tmp;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001685 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001686 bool mode_2ha = false;
1687
1688 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1689 PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, NULL);
1690 if (tmp) {
1691 mode_2ha = true;
1692 pci_dev_put(tmp);
1693 }
1694
1695 for (i = 0; i < sbridge_dev->n_devs; i++) {
1696 pdev = sbridge_dev->pdev[i];
1697 if (!pdev)
1698 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001699
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001700 switch (pdev->device) {
1701 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
1702 pvt->pci_ha0 = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001703 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001704 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
1705 pvt->pci_ta = pdev;
1706 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
1707 pvt->pci_ras = pdev;
1708 break;
1709 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
1710 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
1711 /* if we have 2 HAs active, channels 2 and 3
1712 * are in other device */
1713 if (mode_2ha)
1714 break;
1715 /* fall through */
1716 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
1717 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
1718 {
1719 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
1720 pvt->pci_tad[id] = pdev;
1721 }
1722 break;
1723 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
1724 pvt->pci_ddrio = pdev;
1725 break;
1726 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
1727 if (!mode_2ha)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001728 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001729 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001730 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
1731 pvt->pci_sad0 = pdev;
1732 break;
1733 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
1734 pvt->pci_br0 = pdev;
1735 break;
1736 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
1737 pvt->pci_br1 = pdev;
1738 break;
1739 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
1740 pvt->pci_ha1 = pdev;
1741 break;
1742 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
1743 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
1744 {
1745 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 2;
1746
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001747 /* we shouldn't have this device if we have just one
1748 * HA present */
1749 WARN_ON(!mode_2ha);
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001750 pvt->pci_tad[id] = pdev;
1751 }
1752 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001753 default:
1754 goto error;
1755 }
1756
1757 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1758 sbridge_dev->bus,
1759 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1760 pdev);
1761 }
1762
1763 /* Check if everything were registered */
1764 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1765 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
1766 !pvt->pci_ta)
1767 goto enodev;
1768
1769 for (i = 0; i < NUM_CHANNELS; i++) {
1770 if (!pvt->pci_tad[i])
1771 goto enodev;
1772 }
1773 return 0;
1774
1775enodev:
1776 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1777 return -ENODEV;
1778
1779error:
1780 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001781 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
1782 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001783 return -EINVAL;
1784}
1785
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001786static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
1787 struct sbridge_dev *sbridge_dev)
1788{
1789 struct sbridge_pvt *pvt = mci->pvt_info;
1790 struct pci_dev *pdev, *tmp;
1791 int i;
1792 bool mode_2ha = false;
1793
1794 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1795 PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, NULL);
1796 if (tmp) {
1797 mode_2ha = true;
1798 pci_dev_put(tmp);
1799 }
1800
1801 /* there's only one device per system; not tied to any bus */
1802 if (pvt->info.pci_vtd == NULL)
1803 /* result will be checked later */
1804 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1805 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
1806 NULL);
1807
1808 for (i = 0; i < sbridge_dev->n_devs; i++) {
1809 pdev = sbridge_dev->pdev[i];
1810 if (!pdev)
1811 continue;
1812
1813 switch (pdev->device) {
1814 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
1815 pvt->pci_sad0 = pdev;
1816 break;
1817 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
1818 pvt->pci_sad1 = pdev;
1819 break;
1820 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
1821 pvt->pci_ha0 = pdev;
1822 break;
1823 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
1824 pvt->pci_ta = pdev;
1825 break;
1826 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
1827 pvt->pci_ras = pdev;
1828 break;
1829 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
1830 pvt->pci_tad[0] = pdev;
1831 break;
1832 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
1833 pvt->pci_tad[1] = pdev;
1834 break;
1835 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
1836 if (!mode_2ha)
1837 pvt->pci_tad[2] = pdev;
1838 break;
1839 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
1840 if (!mode_2ha)
1841 pvt->pci_tad[3] = pdev;
1842 break;
1843 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
1844 pvt->pci_ddrio = pdev;
1845 break;
1846 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
1847 pvt->pci_ha1 = pdev;
1848 break;
1849 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
1850 pvt->pci_ha1_ta = pdev;
1851 break;
1852 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
1853 if (mode_2ha)
1854 pvt->pci_tad[2] = pdev;
1855 break;
1856 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
1857 if (mode_2ha)
1858 pvt->pci_tad[3] = pdev;
1859 break;
1860 default:
1861 break;
1862 }
1863
1864 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1865 sbridge_dev->bus,
1866 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1867 pdev);
1868 }
1869
1870 /* Check if everything were registered */
1871 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
1872 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
1873 goto enodev;
1874
1875 for (i = 0; i < NUM_CHANNELS; i++) {
1876 if (!pvt->pci_tad[i])
1877 goto enodev;
1878 }
1879 return 0;
1880
1881enodev:
1882 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1883 return -ENODEV;
1884}
1885
Tony Luck1f395812014-12-02 09:27:30 -08001886static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
1887 struct sbridge_dev *sbridge_dev)
1888{
1889 struct sbridge_pvt *pvt = mci->pvt_info;
1890 struct pci_dev *pdev;
1891 int i;
1892
1893 /* there's only one device per system; not tied to any bus */
1894 if (pvt->info.pci_vtd == NULL)
1895 /* result will be checked later */
1896 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1897 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
1898 NULL);
1899
1900 for (i = 0; i < sbridge_dev->n_devs; i++) {
1901 pdev = sbridge_dev->pdev[i];
1902 if (!pdev)
1903 continue;
1904
1905 switch (pdev->device) {
1906 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
1907 pvt->pci_sad0 = pdev;
1908 break;
1909 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
1910 pvt->pci_sad1 = pdev;
1911 break;
1912 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
1913 pvt->pci_ha0 = pdev;
1914 break;
1915 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
1916 pvt->pci_ta = pdev;
1917 break;
1918 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
1919 pvt->pci_ras = pdev;
1920 break;
1921 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
1922 pvt->pci_tad[0] = pdev;
1923 break;
1924 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
1925 pvt->pci_tad[1] = pdev;
1926 break;
1927 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
1928 pvt->pci_tad[2] = pdev;
1929 break;
1930 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
1931 pvt->pci_tad[3] = pdev;
1932 break;
1933 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
1934 pvt->pci_ddrio = pdev;
1935 break;
1936 default:
1937 break;
1938 }
1939
1940 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1941 sbridge_dev->bus,
1942 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1943 pdev);
1944 }
1945
1946 /* Check if everything were registered */
1947 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
1948 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
1949 goto enodev;
1950
1951 for (i = 0; i < NUM_CHANNELS; i++) {
1952 if (!pvt->pci_tad[i])
1953 goto enodev;
1954 }
1955 return 0;
1956
1957enodev:
1958 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1959 return -ENODEV;
1960}
1961
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001962/****************************************************************************
1963 Error check routines
1964 ****************************************************************************/
1965
1966/*
1967 * While Sandy Bridge has error count registers, SMI BIOS read values from
1968 * and resets the counters. So, they are not reliable for the OS to read
1969 * from them. So, we have no option but to just trust on whatever MCE is
1970 * telling us about the errors.
1971 */
1972static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1973 const struct mce *m)
1974{
1975 struct mem_ctl_info *new_mci;
1976 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001977 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001978 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001979 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1980 bool overflow = GET_BITFIELD(m->status, 62, 62);
1981 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001982 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001983 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1984 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1985 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1986 u32 channel = GET_BITFIELD(m->status, 0, 3);
1987 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1988 long channel_mask, first_channel;
1989 u8 rank, socket;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001990 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001991 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001992
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001993 if (pvt->info.type == IVY_BRIDGE)
1994 recoverable = true;
1995 else
1996 recoverable = GET_BITFIELD(m->status, 56, 56);
1997
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001998 if (uncorrected_error) {
1999 if (ripv) {
2000 type = "FATAL";
2001 tp_event = HW_EVENT_ERR_FATAL;
2002 } else {
2003 type = "NON_FATAL";
2004 tp_event = HW_EVENT_ERR_UNCORRECTED;
2005 }
2006 } else {
2007 type = "CORRECTED";
2008 tp_event = HW_EVENT_ERR_CORRECTED;
2009 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002010
2011 /*
David Mackey15ed1032012-04-17 11:30:52 -07002012 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002013 * memory errors should fit in this mask:
2014 * 000f 0000 1mmm cccc (binary)
2015 * where:
2016 * f = Correction Report Filtering Bit. If 1, subsequent errors
2017 * won't be shown
2018 * mmm = error type
2019 * cccc = channel
2020 * If the mask doesn't match, report an error to the parsing logic
2021 */
2022 if (! ((errcode & 0xef80) == 0x80)) {
2023 optype = "Can't parse: it is not a mem";
2024 } else {
2025 switch (optypenum) {
2026 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002027 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002028 break;
2029 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002030 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002031 break;
2032 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002033 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002034 break;
2035 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002036 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002037 break;
2038 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002039 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002040 break;
2041 default:
2042 optype = "reserved";
2043 break;
2044 }
2045 }
2046
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03002047 /* Only decode errors with an valid address (ADDRV) */
2048 if (!GET_BITFIELD(m->status, 58, 58))
2049 return;
2050
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002051 rc = get_memory_error_data(mci, m->addr, &socket,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002052 &channel_mask, &rank, &area_type, msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002053 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002054 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002055 new_mci = get_mci_for_node_id(socket);
2056 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002057 strcpy(msg, "Error: socket got corrupted!");
2058 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002059 }
2060 mci = new_mci;
2061 pvt = mci->pvt_info;
2062
2063 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
2064
2065 if (rank < 4)
2066 dimm = 0;
2067 else if (rank < 8)
2068 dimm = 1;
2069 else
2070 dimm = 2;
2071
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002072
2073 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002074 * FIXME: On some memory configurations (mirror, lockstep), the
2075 * Memory Controller can't point the error to a single DIMM. The
2076 * EDAC core should be handling the channel mask, in order to point
2077 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002078 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03002079 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
2080 channel = first_channel;
2081
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002082 snprintf(msg, sizeof(msg),
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03002083 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002084 overflow ? " OVERFLOW" : "",
2085 (uncorrected_error && recoverable) ? " recoverable" : "",
2086 area_type,
2087 mscod, errcode,
2088 socket,
2089 channel_mask,
2090 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002091
Joe Perches956b9ba2012-04-29 17:08:39 -03002092 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002093
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002094 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002095
Seth Jennings351fc4a2014-09-05 14:28:47 -05002096 if (channel == CHANNEL_UNSPECIFIED)
2097 channel = -1;
2098
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002099 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03002100 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002101 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
2102 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03002103 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002104 return;
2105err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03002106 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002107 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03002108 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002109
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002110}
2111
2112/*
2113 * sbridge_check_error Retrieve and process errors reported by the
2114 * hardware. Called by the Core module.
2115 */
2116static void sbridge_check_error(struct mem_ctl_info *mci)
2117{
2118 struct sbridge_pvt *pvt = mci->pvt_info;
2119 int i;
2120 unsigned count = 0;
2121 struct mce *m;
2122
2123 /*
2124 * MCE first step: Copy all mce errors into a temporary buffer
2125 * We use a double buffering here, to reduce the risk of
2126 * loosing an error.
2127 */
2128 smp_rmb();
2129 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
2130 % MCE_LOG_LEN;
2131 if (!count)
2132 return;
2133
2134 m = pvt->mce_outentry;
2135 if (pvt->mce_in + count > MCE_LOG_LEN) {
2136 unsigned l = MCE_LOG_LEN - pvt->mce_in;
2137
2138 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
2139 smp_wmb();
2140 pvt->mce_in = 0;
2141 count -= l;
2142 m += l;
2143 }
2144 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
2145 smp_wmb();
2146 pvt->mce_in += count;
2147
2148 smp_rmb();
2149 if (pvt->mce_overrun) {
2150 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
2151 pvt->mce_overrun);
2152 smp_wmb();
2153 pvt->mce_overrun = 0;
2154 }
2155
2156 /*
2157 * MCE second step: parse errors and display
2158 */
2159 for (i = 0; i < count; i++)
2160 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
2161}
2162
2163/*
2164 * sbridge_mce_check_error Replicates mcelog routine to get errors
2165 * This routine simply queues mcelog errors, and
2166 * return. The error itself should be handled later
2167 * by sbridge_check_error.
2168 * WARNING: As this routine should be called at NMI time, extra care should
2169 * be taken to avoid deadlocks, and to be as fast as possible.
2170 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002171static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
2172 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002173{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002174 struct mce *mce = (struct mce *)data;
2175 struct mem_ctl_info *mci;
2176 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04002177 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002178
Chen, Gongfd521032013-12-06 01:17:09 -05002179 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2180 return NOTIFY_DONE;
2181
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002182 mci = get_mci_for_node_id(mce->socketid);
2183 if (!mci)
2184 return NOTIFY_BAD;
2185 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002186
2187 /*
2188 * Just let mcelog handle it if the error is
2189 * outside the memory controller. A memory error
2190 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
2191 * bit 12 has an special meaning.
2192 */
2193 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002194 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002195
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04002196 if (mce->mcgstatus & MCG_STATUS_MCIP)
2197 type = "Exception";
2198 else
2199 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002200
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04002201 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002202
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04002203 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
2204 "Bank %d: %016Lx\n", mce->extcpu, type,
2205 mce->mcgstatus, mce->bank, mce->status);
2206 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
2207 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
2208 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002209
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04002210 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
2211 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
2212 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002213
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002214 smp_rmb();
2215 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
2216 smp_wmb();
2217 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002218 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002219 }
2220
2221 /* Copy memory error at the ringbuffer */
2222 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
2223 smp_wmb();
2224 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
2225
2226 /* Handle fatal errors immediately */
2227 if (mce->mcgstatus & 1)
2228 sbridge_check_error(mci);
2229
2230 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002231 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002232}
2233
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002234static struct notifier_block sbridge_mce_dec = {
2235 .notifier_call = sbridge_mce_check_error,
2236};
2237
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002238/****************************************************************************
2239 EDAC register/unregister logic
2240 ****************************************************************************/
2241
2242static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
2243{
2244 struct mem_ctl_info *mci = sbridge_dev->mci;
2245 struct sbridge_pvt *pvt;
2246
2247 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002248 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002249
2250 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
2251 return;
2252 }
2253
2254 pvt = mci->pvt_info;
2255
Joe Perches956b9ba2012-04-29 17:08:39 -03002256 edac_dbg(0, "MC: mci = %p, dev = %p\n",
2257 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002258
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002259 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03002260 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002261
Joe Perches956b9ba2012-04-29 17:08:39 -03002262 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002263 kfree(mci->ctl_name);
2264 edac_mc_free(mci);
2265 sbridge_dev->mci = NULL;
2266}
2267
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002268static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002269{
2270 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002271 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002272 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002273 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002274 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002275
2276 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002277 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002278 if (unlikely(rc < 0))
2279 return rc;
2280
2281 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002282 layers[0].type = EDAC_MC_LAYER_CHANNEL;
2283 layers[0].size = NUM_CHANNELS;
2284 layers[0].is_virt_csrow = false;
2285 layers[1].type = EDAC_MC_LAYER_SLOT;
2286 layers[1].size = MAX_DIMMS;
2287 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03002288 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002289 sizeof(*pvt));
2290
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002291 if (unlikely(!mci))
2292 return -ENOMEM;
2293
Joe Perches956b9ba2012-04-29 17:08:39 -03002294 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002295 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002296
2297 pvt = mci->pvt_info;
2298 memset(pvt, 0, sizeof(*pvt));
2299
2300 /* Associate sbridge_dev and mci for future usage */
2301 pvt->sbridge_dev = sbridge_dev;
2302 sbridge_dev->mci = mci;
2303
2304 mci->mtype_cap = MEM_FLAG_DDR3;
2305 mci->edac_ctl_cap = EDAC_FLAG_NONE;
2306 mci->edac_cap = EDAC_FLAG_NONE;
2307 mci->mod_name = "sbridge_edac.c";
2308 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002309 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002310 mci->ctl_page_to_phys = NULL;
2311
2312 /* Set the function pointer to an actual operation function */
2313 mci->edac_check = sbridge_check_error;
2314
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002315 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002316 switch (type) {
2317 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002318 pvt->info.rankcfgr = IB_RANK_CFG_A;
2319 pvt->info.get_tolm = ibridge_get_tolm;
2320 pvt->info.get_tohm = ibridge_get_tohm;
2321 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002322 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002323 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002324 pvt->info.rir_limit = rir_limit;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002325 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2326 pvt->info.interleave_list = ibridge_interleave_list;
2327 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2328 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2329 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
2330
2331 /* Store pci devices at mci for faster access */
2332 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
2333 if (unlikely(rc < 0))
2334 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002335 break;
2336 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002337 pvt->info.rankcfgr = SB_RANK_CFG_A;
2338 pvt->info.get_tolm = sbridge_get_tolm;
2339 pvt->info.get_tohm = sbridge_get_tohm;
2340 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002341 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002342 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002343 pvt->info.rir_limit = rir_limit;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002344 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2345 pvt->info.interleave_list = sbridge_interleave_list;
2346 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2347 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2348 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2349
2350 /* Store pci devices at mci for faster access */
2351 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2352 if (unlikely(rc < 0))
2353 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002354 break;
2355 case HASWELL:
2356 /* rankcfgr isn't used */
2357 pvt->info.get_tolm = haswell_get_tolm;
2358 pvt->info.get_tohm = haswell_get_tohm;
2359 pvt->info.dram_rule = ibridge_dram_rule;
2360 pvt->info.get_memory_type = haswell_get_memory_type;
2361 pvt->info.get_node_id = haswell_get_node_id;
2362 pvt->info.rir_limit = haswell_rir_limit;
2363 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2364 pvt->info.interleave_list = ibridge_interleave_list;
2365 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2366 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2367 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002368
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002369 /* Store pci devices at mci for faster access */
2370 rc = haswell_mci_bind_devs(mci, sbridge_dev);
2371 if (unlikely(rc < 0))
2372 goto fail0;
2373 break;
Tony Luck1f395812014-12-02 09:27:30 -08002374 case BROADWELL:
2375 /* rankcfgr isn't used */
2376 pvt->info.get_tolm = haswell_get_tolm;
2377 pvt->info.get_tohm = haswell_get_tohm;
2378 pvt->info.dram_rule = ibridge_dram_rule;
2379 pvt->info.get_memory_type = haswell_get_memory_type;
2380 pvt->info.get_node_id = haswell_get_node_id;
2381 pvt->info.rir_limit = haswell_rir_limit;
2382 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2383 pvt->info.interleave_list = ibridge_interleave_list;
2384 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2385 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2386 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
2387
2388 /* Store pci devices at mci for faster access */
2389 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
2390 if (unlikely(rc < 0))
2391 goto fail0;
2392 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002393 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002394
2395 /* Get dimm basic config and the memory layout */
2396 get_dimm_config(mci);
2397 get_memory_layout(mci);
2398
2399 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002400 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002401
2402 /* add this new MC control structure to EDAC's list of MCs */
2403 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002404 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002405 rc = -EINVAL;
2406 goto fail0;
2407 }
2408
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002409 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002410
2411fail0:
2412 kfree(mci->ctl_name);
2413 edac_mc_free(mci);
2414 sbridge_dev->mci = NULL;
2415 return rc;
2416}
2417
2418/*
2419 * sbridge_probe Probe for ONE instance of device to see if it is
2420 * present.
2421 * return:
2422 * 0 for FOUND a device
2423 * < 0 for error code
2424 */
2425
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002426static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002427{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002428 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002429 u8 mc, num_mc = 0;
2430 struct sbridge_dev *sbridge_dev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002431 enum type type = SANDY_BRIDGE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002432
2433 /* get the pci devices we want to reserve for our use */
2434 mutex_lock(&sbridge_edac_lock);
2435
2436 /*
2437 * All memory controllers are allocated at the first pass.
2438 */
2439 if (unlikely(probed >= 1)) {
2440 mutex_unlock(&sbridge_edac_lock);
2441 return -ENODEV;
2442 }
2443 probed++;
2444
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002445 switch (pdev->device) {
2446 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002447 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2448 type = IVY_BRIDGE;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002449 break;
Borislav Petkov11249e72015-02-05 12:39:36 +01002450 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002451 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2452 type = SANDY_BRIDGE;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002453 break;
2454 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2455 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_haswell_table);
2456 type = HASWELL;
2457 break;
Tony Luck1f395812014-12-02 09:27:30 -08002458 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2459 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_broadwell_table);
2460 type = BROADWELL;
2461 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002462 }
Borislav Petkov11249e72015-02-05 12:39:36 +01002463 if (unlikely(rc < 0)) {
2464 edac_dbg(0, "couldn't get all devices for 0x%x\n", pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002465 goto fail0;
Borislav Petkov11249e72015-02-05 12:39:36 +01002466 }
2467
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002468 mc = 0;
2469
2470 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002471 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2472 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002473
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002474 sbridge_dev->mc = mc++;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002475 rc = sbridge_register_mci(sbridge_dev, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002476 if (unlikely(rc < 0))
2477 goto fail1;
2478 }
2479
Borislav Petkov11249e72015-02-05 12:39:36 +01002480 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002481
2482 mutex_unlock(&sbridge_edac_lock);
2483 return 0;
2484
2485fail1:
2486 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2487 sbridge_unregister_mci(sbridge_dev);
2488
2489 sbridge_put_all_devices();
2490fail0:
2491 mutex_unlock(&sbridge_edac_lock);
2492 return rc;
2493}
2494
2495/*
2496 * sbridge_remove destructor for one instance of device
2497 *
2498 */
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002499static void sbridge_remove(struct pci_dev *pdev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002500{
2501 struct sbridge_dev *sbridge_dev;
2502
Joe Perches956b9ba2012-04-29 17:08:39 -03002503 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002504
2505 /*
2506 * we have a trouble here: pdev value for removal will be wrong, since
2507 * it will point to the X58 register used to detect that the machine
2508 * is a Nehalem or upper design. However, due to the way several PCI
2509 * devices are grouped together to provide MC functionality, we need
2510 * to use a different method for releasing the devices
2511 */
2512
2513 mutex_lock(&sbridge_edac_lock);
2514
2515 if (unlikely(!probed)) {
2516 mutex_unlock(&sbridge_edac_lock);
2517 return;
2518 }
2519
2520 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2521 sbridge_unregister_mci(sbridge_dev);
2522
2523 /* Release PCI resources */
2524 sbridge_put_all_devices();
2525
2526 probed--;
2527
2528 mutex_unlock(&sbridge_edac_lock);
2529}
2530
2531MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2532
2533/*
2534 * sbridge_driver pci_driver structure for this module
2535 *
2536 */
2537static struct pci_driver sbridge_driver = {
2538 .name = "sbridge_edac",
2539 .probe = sbridge_probe,
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002540 .remove = sbridge_remove,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002541 .id_table = sbridge_pci_tbl,
2542};
2543
2544/*
2545 * sbridge_init Module entry function
2546 * Try to initialize this module for its devices
2547 */
2548static int __init sbridge_init(void)
2549{
2550 int pci_rc;
2551
Joe Perches956b9ba2012-04-29 17:08:39 -03002552 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002553
2554 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2555 opstate_init();
2556
2557 pci_rc = pci_register_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002558 if (pci_rc >= 0) {
2559 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05002560 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2561 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002562 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03002563 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002564
2565 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2566 pci_rc);
2567
2568 return pci_rc;
2569}
2570
2571/*
2572 * sbridge_exit() Module exit function
2573 * Unregister the driver
2574 */
2575static void __exit sbridge_exit(void)
2576{
Joe Perches956b9ba2012-04-29 17:08:39 -03002577 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002578 pci_unregister_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002579 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002580}
2581
2582module_init(sbridge_init);
2583module_exit(sbridge_exit);
2584
2585module_param(edac_op_state, int, 0444);
2586MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2587
2588MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002589MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002590MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002591MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002592 SBRIDGE_REVISION);