blob: f37d01f3bb173e549563cfb079e9dc1134cb5eed [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
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300138#define HASWELL_TOHM_0 0xd4
139#define HASWELL_TOHM_1 0xd8
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200140
141#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
142#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
143
144/* Device 13 Function 6 */
145
146#define SAD_TARGET 0xf0
147
148#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
149
150#define SAD_CONTROL 0xf4
151
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200152/* Device 14 function 0 */
153
154static const u32 tad_dram_rule[] = {
155 0x40, 0x44, 0x48, 0x4c,
156 0x50, 0x54, 0x58, 0x5c,
157 0x60, 0x64, 0x68, 0x6c,
158};
159#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
160
161#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
162#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
163#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
164#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
165#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
166#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
167#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
168
169/* Device 15, function 0 */
170
171#define MCMTR 0x7c
172
173#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
174#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
175#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
176
177/* Device 15, function 1 */
178
179#define RASENABLES 0xac
180#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
181
182/* Device 15, functions 2-5 */
183
184static const int mtr_regs[] = {
185 0x80, 0x84, 0x88,
186};
187
188#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
189#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
190#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
191#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
192#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
193
194static const u32 tad_ch_nilv_offset[] = {
195 0x90, 0x94, 0x98, 0x9c,
196 0xa0, 0xa4, 0xa8, 0xac,
197 0xb0, 0xb4, 0xb8, 0xbc,
198};
199#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
200#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
201
202static const u32 rir_way_limit[] = {
203 0x108, 0x10c, 0x110, 0x114, 0x118,
204};
205#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
206
207#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
208#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200209
210#define MAX_RIR_WAY 8
211
212static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
213 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
214 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
215 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
216 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
217 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
218};
219
220#define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
221#define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
222
223/* Device 16, functions 2-7 */
224
225/*
226 * FIXME: Implement the error count reads directly
227 */
228
229static const u32 correrrcnt[] = {
230 0x104, 0x108, 0x10c, 0x110,
231};
232
233#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
234#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
235#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
236#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
237
238static const u32 correrrthrsld[] = {
239 0x11c, 0x120, 0x124, 0x128,
240};
241
242#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
243#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
244
245
246/* Device 17, function 0 */
247
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300248#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200249
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300250#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200251
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200252/*
253 * sbridge structs
254 */
255
Seth Jennings351fc4a2014-09-05 14:28:47 -0500256#define NUM_CHANNELS 4
257#define MAX_DIMMS 3 /* Max DIMMS per channel */
258#define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200259
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300260enum type {
261 SANDY_BRIDGE,
262 IVY_BRIDGE,
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300263 HASWELL,
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300264};
265
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300266struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200267struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300268 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300269 u32 mcmtr;
270 u32 rankcfgr;
271 u64 (*get_tolm)(struct sbridge_pvt *pvt);
272 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300273 u64 (*rir_limit)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300274 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300275 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300276 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300277 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300278 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300279 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300280 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300281 struct pci_dev *pci_vtd;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200282};
283
284struct sbridge_channel {
285 u32 ranks;
286 u32 dimms;
287};
288
289struct pci_id_descr {
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -0300290 int dev_id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200291 int optional;
292};
293
294struct pci_id_table {
295 const struct pci_id_descr *descr;
296 int n_devs;
297};
298
299struct sbridge_dev {
300 struct list_head list;
301 u8 bus, mc;
302 u8 node_id, source_id;
303 struct pci_dev **pdev;
304 int n_devs;
305 struct mem_ctl_info *mci;
306};
307
308struct sbridge_pvt {
309 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300310 struct pci_dev *pci_sad0, *pci_sad1;
311 struct pci_dev *pci_ha0, *pci_ha1;
312 struct pci_dev *pci_br0, *pci_br1;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300313 struct pci_dev *pci_ha1_ta;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200314 struct pci_dev *pci_tad[NUM_CHANNELS];
315
316 struct sbridge_dev *sbridge_dev;
317
318 struct sbridge_info info;
319 struct sbridge_channel channel[NUM_CHANNELS];
320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200321 /* Memory type detection */
322 bool is_mirrored, is_lockstep, is_close_pg;
323
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200324 /* Fifo double buffers */
325 struct mce mce_entry[MCE_LOG_LEN];
326 struct mce mce_outentry[MCE_LOG_LEN];
327
328 /* Fifo in/out counters */
329 unsigned mce_in, mce_out;
330
331 /* Count indicator to show errors not got */
332 unsigned mce_overrun;
333
334 /* Memory description */
335 u64 tolm, tohm;
336};
337
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300338#define PCI_DESCR(device_id, opt) \
339 .dev_id = (device_id), \
Luck, Tonyde4772c2013-03-28 09:59:15 -0700340 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200341
342static const struct pci_id_descr pci_dev_descr_sbridge[] = {
343 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300344 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200345
346 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300347 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
348 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
349 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
350 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
351 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
352 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
353 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200354
355 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300356 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
357 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200358
359 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300360 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200361};
362
363#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
364static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
365 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
366 {0,} /* 0 terminated list. */
367};
368
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300369/* This changes depending if 1HA or 2HA:
370 * 1HA:
371 * 0x0eb8 (17.0) is DDRIO0
372 * 2HA:
373 * 0x0ebc (17.4) is DDRIO0
374 */
375#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
376#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
377
378/* pci ids */
379#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
380#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
381#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
382#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
383#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
384#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
385#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
386#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
387#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
388#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
389#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
390#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
391#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
392#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
393#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
394
395static const struct pci_id_descr pci_dev_descr_ibridge[] = {
396 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300397 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300398
399 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300400 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
401 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
402 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
403 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
404 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
405 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300406
407 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300408 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300409
410 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300411 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
412 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300413
414 /* Optional, mode 2HA */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300415 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300416#if 0
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300417 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
418 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300419#endif
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300420 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
421 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300422
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300423 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
424 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300425};
426
427static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
428 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
429 {0,} /* 0 terminated list. */
430};
431
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300432/* Haswell support */
433/* EN processor:
434 * - 1 IMC
435 * - 3 DDR3 channels, 2 DPC per channel
436 * EP processor:
437 * - 1 or 2 IMC
438 * - 4 DDR4 channels, 3 DPC per channel
439 * EP 4S processor:
440 * - 2 IMC
441 * - 4 DDR4 channels, 3 DPC per channel
442 * EX processor:
443 * - 2 IMC
444 * - each IMC interfaces with a SMI 2 channel
445 * - each SMI channel interfaces with a scalable memory buffer
446 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
447 */
448#define HASWELL_DDRCRCLKCONTROLS 0xa10
449#define HASWELL_HASYSDEFEATURE2 0x84
450#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
451#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
452#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
453#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
454#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
455#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
456#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
457#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
458#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
459#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
460#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
461#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
462#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
463#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
464#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
465#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
466#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
467#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
468static const struct pci_id_descr pci_dev_descr_haswell[] = {
469 /* first item must be the HA */
470 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
471
472 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
473 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
474
475 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
476
477 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
478 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
479 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
480 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
481 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
482 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
483
484 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
485
486 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
487 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
488 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
489 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
490 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
491 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
492};
493
494static const struct pci_id_table pci_dev_descr_haswell_table[] = {
495 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
496 {0,} /* 0 terminated list. */
497};
498
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200499/*
500 * pci_device_id table for which devices we are looking for
501 */
Jingoo Hanba935f42013-12-06 10:23:08 +0100502static const struct pci_device_id sbridge_pci_tbl[] = {
Andy Lutomirskid0585cd2014-08-14 14:45:41 -0700503 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0)},
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300504 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300505 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0)},
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200506 {0,} /* 0 terminated list. */
507};
508
509
510/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700511 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200512 ****************************************************************************/
513
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300514static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200515{
516 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300517 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200518
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300519 if (type == HASWELL)
520 max = 8;
521
522 if (ranks > max) {
523 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
524 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200525 return -EINVAL;
526 }
527
528 return ranks;
529}
530
531static inline int numrow(u32 mtr)
532{
533 int rows = (RANK_WIDTH_BITS(mtr) + 12);
534
535 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300536 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
537 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200538 return -EINVAL;
539 }
540
541 return 1 << rows;
542}
543
544static inline int numcol(u32 mtr)
545{
546 int cols = (COL_WIDTH_BITS(mtr) + 10);
547
548 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300549 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
550 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200551 return -EINVAL;
552 }
553
554 return 1 << cols;
555}
556
557static struct sbridge_dev *get_sbridge_dev(u8 bus)
558{
559 struct sbridge_dev *sbridge_dev;
560
561 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
562 if (sbridge_dev->bus == bus)
563 return sbridge_dev;
564 }
565
566 return NULL;
567}
568
569static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
570 const struct pci_id_table *table)
571{
572 struct sbridge_dev *sbridge_dev;
573
574 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
575 if (!sbridge_dev)
576 return NULL;
577
578 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
579 GFP_KERNEL);
580 if (!sbridge_dev->pdev) {
581 kfree(sbridge_dev);
582 return NULL;
583 }
584
585 sbridge_dev->bus = bus;
586 sbridge_dev->n_devs = table->n_devs;
587 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
588
589 return sbridge_dev;
590}
591
592static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
593{
594 list_del(&sbridge_dev->list);
595 kfree(sbridge_dev->pdev);
596 kfree(sbridge_dev);
597}
598
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300599static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
600{
601 u32 reg;
602
603 /* Address range is 32:28 */
604 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
605 return GET_TOLM(reg);
606}
607
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300608static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
609{
610 u32 reg;
611
612 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
613 return GET_TOHM(reg);
614}
615
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300616static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
617{
618 u32 reg;
619
620 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
621
622 return GET_TOLM(reg);
623}
624
625static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
626{
627 u32 reg;
628
629 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
630
631 return GET_TOHM(reg);
632}
633
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300634static u64 rir_limit(u32 reg)
635{
636 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
637}
638
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300639static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
640{
641 u32 reg;
642 enum mem_type mtype;
643
644 if (pvt->pci_ddrio) {
645 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
646 &reg);
647 if (GET_BITFIELD(reg, 11, 11))
648 /* FIXME: Can also be LRDIMM */
649 mtype = MEM_RDDR3;
650 else
651 mtype = MEM_DDR3;
652 } else
653 mtype = MEM_UNKNOWN;
654
655 return mtype;
656}
657
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300658static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
659{
660 u32 reg;
661 bool registered = false;
662 enum mem_type mtype = MEM_UNKNOWN;
663
664 if (!pvt->pci_ddrio)
665 goto out;
666
667 pci_read_config_dword(pvt->pci_ddrio,
668 HASWELL_DDRCRCLKCONTROLS, &reg);
669 /* Is_Rdimm */
670 if (GET_BITFIELD(reg, 16, 16))
671 registered = true;
672
673 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
674 if (GET_BITFIELD(reg, 14, 14)) {
675 if (registered)
676 mtype = MEM_RDDR4;
677 else
678 mtype = MEM_DDR4;
679 } else {
680 if (registered)
681 mtype = MEM_RDDR3;
682 else
683 mtype = MEM_DDR3;
684 }
685
686out:
687 return mtype;
688}
689
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300690static u8 get_node_id(struct sbridge_pvt *pvt)
691{
692 u32 reg;
693 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
694 return GET_BITFIELD(reg, 0, 2);
695}
696
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300697static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
698{
699 u32 reg;
700
701 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
702 return GET_BITFIELD(reg, 0, 3);
703}
704
705static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
706{
707 u32 reg;
708
709 pci_read_config_dword(pvt->info.pci_vtd, TOLM, &reg);
710 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x1ffffff;
711}
712
713static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
714{
715 u64 rc;
716 u32 reg;
717
718 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
719 rc = GET_BITFIELD(reg, 26, 31);
720 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
721 rc = ((reg << 6) | rc) << 26;
722
723 return rc | 0x1ffffff;
724}
725
726static u64 haswell_rir_limit(u32 reg)
727{
728 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
729}
730
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300731static inline u8 sad_pkg_socket(u8 pkg)
732{
733 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -0300734 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300735}
736
737static inline u8 sad_pkg_ha(u8 pkg)
738{
739 return (pkg >> 2) & 0x1;
740}
741
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200742/****************************************************************************
743 Memory check routines
744 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300745static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200746{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300747 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200748
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300749 do {
750 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
751 if (pdev && pdev->bus->number == bus)
752 break;
753 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200754
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300755 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200756}
757
758/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300759 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300760 * @bus: Device bus
761 * @type: Memory controller type
762 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
763 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200764 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300765static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200766{
767 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300768 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200769
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300770 if (type == IVY_BRIDGE)
771 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300772 else if (type == HASWELL)
773 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300774 else
775 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
776
777 pdev = get_pdev_same_bus(bus, id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200778 if (!pdev) {
779 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300780 "%04x:%04x! on bus %02d\n",
781 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200782 return -ENODEV;
783 }
784
785 pci_read_config_dword(pdev, MCMTR, &mcmtr);
786 if (!IS_ECC_ENABLED(mcmtr)) {
787 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
788 return -ENODEV;
789 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200790 return 0;
791}
792
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300793static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200794{
795 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300796 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300797 unsigned i, j, banks, ranks, rows, cols, npages;
798 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200799 u32 reg;
800 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -0200801 enum mem_type mtype;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200802
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300803 if (pvt->info.type == HASWELL)
804 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
805 else
806 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
807
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200808 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
809
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300810 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -0300811 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
812 pvt->sbridge_dev->mc,
813 pvt->sbridge_dev->node_id,
814 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200815
816 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
817 if (IS_MIRROR_ENABLED(reg)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300818 edac_dbg(0, "Memory mirror is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200819 pvt->is_mirrored = true;
820 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300821 edac_dbg(0, "Memory mirror is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200822 pvt->is_mirrored = false;
823 }
824
825 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
826 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300827 edac_dbg(0, "Lockstep is enabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200828 mode = EDAC_S8ECD8ED;
829 pvt->is_lockstep = true;
830 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300831 edac_dbg(0, "Lockstep is disabled\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200832 mode = EDAC_S4ECD4ED;
833 pvt->is_lockstep = false;
834 }
835 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300836 edac_dbg(0, "address map is on closed page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200837 pvt->is_close_pg = true;
838 } else {
Joe Perches956b9ba2012-04-29 17:08:39 -0300839 edac_dbg(0, "address map is on open page mode\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200840 pvt->is_close_pg = false;
841 }
842
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300843 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300844 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300845 edac_dbg(0, "Memory is registered\n");
846 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -0700847 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300848 else
849 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200850
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300851 if (mtype == MEM_DDR4 || MEM_RDDR4)
852 banks = 16;
853 else
854 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200855
856 for (i = 0; i < NUM_CHANNELS; i++) {
857 u32 mtr;
858
859 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -0300860 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
861 i, j, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200862 pci_read_config_dword(pvt->pci_tad[i],
863 mtr_regs[j], &mtr);
Joe Perches956b9ba2012-04-29 17:08:39 -0300864 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200865 if (IS_DIMM_PRESENT(mtr)) {
866 pvt->channel[i].dimms++;
867
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300868 ranks = numrank(pvt->info.type, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200869 rows = numrow(mtr);
870 cols = numcol(mtr);
871
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300872 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200873 npages = MiB_TO_PAGES(size);
874
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -0300875 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 -0300876 pvt->sbridge_dev->mc, i, j,
877 size, npages,
878 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200879
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300880 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300881 dimm->grain = 32;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300882 switch (banks) {
883 case 16:
884 dimm->dtype = DEV_X16;
885 break;
886 case 8:
887 dimm->dtype = DEV_X8;
888 break;
889 case 4:
890 dimm->dtype = DEV_X4;
891 break;
892 }
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300893 dimm->mtype = mtype;
894 dimm->edac_mode = mode;
895 snprintf(dimm->label, sizeof(dimm->label),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200896 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
897 pvt->sbridge_dev->source_id, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200898 }
899 }
900 }
901
902 return 0;
903}
904
905static void get_memory_layout(const struct mem_ctl_info *mci)
906{
907 struct sbridge_pvt *pvt = mci->pvt_info;
908 int i, j, k, n_sads, n_tads, sad_interl;
909 u32 reg;
910 u64 limit, prv = 0;
911 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +0100912 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200913 u32 rir_way;
914
915 /*
916 * Step 1) Get TOLM/TOHM ranges
917 */
918
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300919 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200920 tmp_mb = (1 + pvt->tolm) >> 20;
921
Jim Snow8c009102014-11-18 14:51:09 +0100922 gb = div_u64_rem(tmp_mb, 1024, &mb);
923 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
924 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200925
926 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300927 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200928 tmp_mb = (1 + pvt->tohm) >> 20;
929
Jim Snow8c009102014-11-18 14:51:09 +0100930 gb = div_u64_rem(tmp_mb, 1024, &mb);
931 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
932 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200933
934 /*
935 * Step 2) Get SAD range and SAD Interleave list
936 * TAD registers contain the interleave wayness. However, it
937 * seems simpler to just discover it indirectly, with the
938 * algorithm bellow.
939 */
940 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300941 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200942 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300943 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200944 &reg);
945 limit = SAD_LIMIT(reg);
946
947 if (!DRAM_RULE_ENABLE(reg))
948 continue;
949
950 if (limit <= prv)
951 break;
952
953 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +0100954 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300955 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
956 n_sads,
957 get_dram_attr(reg),
Jim Snow8c009102014-11-18 14:51:09 +0100958 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -0300959 ((u64)tmp_mb) << 20L,
960 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
961 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200962 prv = limit;
963
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300964 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200965 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300966 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200967 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300968 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
969 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200970 break;
971
Joe Perches956b9ba2012-04-29 17:08:39 -0300972 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300973 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200974 }
975 }
976
977 /*
978 * Step 3) Get TAD range
979 */
980 prv = 0;
981 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
982 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
983 &reg);
984 limit = TAD_LIMIT(reg);
985 if (limit <= prv)
986 break;
987 tmp_mb = (limit + 1) >> 20;
988
Jim Snow8c009102014-11-18 14:51:09 +0100989 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -0300990 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 +0100991 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -0300992 ((u64)tmp_mb) << 20L,
993 (u32)TAD_SOCK(reg),
994 (u32)TAD_CH(reg),
995 (u32)TAD_TGT0(reg),
996 (u32)TAD_TGT1(reg),
997 (u32)TAD_TGT2(reg),
998 (u32)TAD_TGT3(reg),
999 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001000 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001001 }
1002
1003 /*
1004 * Step 4) Get TAD offsets, per each channel
1005 */
1006 for (i = 0; i < NUM_CHANNELS; i++) {
1007 if (!pvt->channel[i].dimms)
1008 continue;
1009 for (j = 0; j < n_tads; j++) {
1010 pci_read_config_dword(pvt->pci_tad[i],
1011 tad_ch_nilv_offset[j],
1012 &reg);
1013 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001014 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001015 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1016 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001017 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001018 ((u64)tmp_mb) << 20L,
1019 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001020 }
1021 }
1022
1023 /*
1024 * Step 6) Get RIR Wayness/Limit, per each channel
1025 */
1026 for (i = 0; i < NUM_CHANNELS; i++) {
1027 if (!pvt->channel[i].dimms)
1028 continue;
1029 for (j = 0; j < MAX_RIR_RANGES; j++) {
1030 pci_read_config_dword(pvt->pci_tad[i],
1031 rir_way_limit[j],
1032 &reg);
1033
1034 if (!IS_RIR_VALID(reg))
1035 continue;
1036
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001037 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001038 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001039 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001040 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1041 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001042 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001043 ((u64)tmp_mb) << 20L,
1044 rir_way,
1045 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001046
1047 for (k = 0; k < rir_way; k++) {
1048 pci_read_config_dword(pvt->pci_tad[i],
1049 rir_offset[j][k],
1050 &reg);
1051 tmp_mb = RIR_OFFSET(reg) << 6;
1052
Jim Snow8c009102014-11-18 14:51:09 +01001053 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001054 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1055 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001056 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001057 ((u64)tmp_mb) << 20L,
1058 (u32)RIR_RNK_TGT(reg),
1059 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001060 }
1061 }
1062 }
1063}
1064
Rashika Kheria8112c0c2013-12-14 19:32:09 +05301065static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001066{
1067 struct sbridge_dev *sbridge_dev;
1068
1069 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1070 if (sbridge_dev->node_id == node_id)
1071 return sbridge_dev->mci;
1072 }
1073 return NULL;
1074}
1075
1076static int get_memory_error_data(struct mem_ctl_info *mci,
1077 u64 addr,
1078 u8 *socket,
1079 long *channel_mask,
1080 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001081 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001082{
1083 struct mem_ctl_info *new_mci;
1084 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001085 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001086 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001087 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001088 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001089 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001090 u32 reg, dram_rule;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001091 u8 ch_way, sck_way, pkg, sad_ha = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001092 u32 tad_offset;
1093 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001094 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001095 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001096
1097
1098 /*
1099 * Step 0) Check if the address is at special memory ranges
1100 * The check bellow is probably enough to fill all cases where
1101 * the error is not inside a memory, except for the legacy
1102 * range (e. g. VGA addresses). It is unlikely, however, that the
1103 * memory controller would generate an error on that range.
1104 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001105 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001106 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001107 return -EINVAL;
1108 }
1109 if (addr >= (u64)pvt->tohm) {
1110 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001111 return -EINVAL;
1112 }
1113
1114 /*
1115 * Step 1) Get socket
1116 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001117 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1118 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001119 &reg);
1120
1121 if (!DRAM_RULE_ENABLE(reg))
1122 continue;
1123
1124 limit = SAD_LIMIT(reg);
1125 if (limit <= prv) {
1126 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001127 return -EINVAL;
1128 }
1129 if (addr <= limit)
1130 break;
1131 prv = limit;
1132 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001133 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001134 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001135 return -EINVAL;
1136 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001137 dram_rule = reg;
1138 *area_type = get_dram_attr(dram_rule);
1139 interleave_mode = INTERLEAVE_MODE(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001140
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001141 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001142 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001143
1144 if (pvt->info.type == SANDY_BRIDGE) {
1145 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1146 for (sad_way = 0; sad_way < 8; sad_way++) {
1147 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1148 if (sad_way > 0 && sad_interl == pkg)
1149 break;
1150 sad_interleave[sad_way] = pkg;
1151 edac_dbg(0, "SAD interleave #%d: %d\n",
1152 sad_way, sad_interleave[sad_way]);
1153 }
1154 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1155 pvt->sbridge_dev->mc,
1156 n_sads,
1157 addr,
1158 limit,
1159 sad_way + 7,
1160 !interleave_mode ? "" : "XOR[18:16]");
1161 if (interleave_mode)
1162 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1163 else
1164 idx = (addr >> 6) & 7;
1165 switch (sad_way) {
1166 case 1:
1167 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001168 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001169 case 2:
1170 idx = idx & 1;
1171 break;
1172 case 4:
1173 idx = idx & 3;
1174 break;
1175 case 8:
1176 break;
1177 default:
1178 sprintf(msg, "Can't discover socket interleave");
1179 return -EINVAL;
1180 }
1181 *socket = sad_interleave[idx];
1182 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1183 idx, sad_way, *socket);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001184 } else if (pvt->info.type == HASWELL) {
1185 int bits, a7mode = A7MODE(dram_rule);
1186
1187 if (a7mode) {
1188 /* A7 mode swaps P9 with P6 */
1189 bits = GET_BITFIELD(addr, 7, 8) << 1;
1190 bits |= GET_BITFIELD(addr, 9, 9);
1191 } else
1192 bits = GET_BITFIELD(addr, 7, 9);
1193
1194 if (interleave_mode) {
1195 /* interleave mode will XOR {8,7,6} with {18,17,16} */
1196 idx = GET_BITFIELD(addr, 16, 18);
1197 idx ^= bits;
1198 } else
1199 idx = bits;
1200
1201 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1202 *socket = sad_pkg_socket(pkg);
1203 sad_ha = sad_pkg_ha(pkg);
1204
1205 if (a7mode) {
1206 /* MCChanShiftUpEnable */
1207 pci_read_config_dword(pvt->pci_ha0,
1208 HASWELL_HASYSDEFEATURE2, &reg);
1209 shiftup = GET_BITFIELD(reg, 22, 22);
1210 }
1211
1212 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
1213 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001214 } else {
1215 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001216 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001217 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1218 *socket = sad_pkg_socket(pkg);
1219 sad_ha = sad_pkg_ha(pkg);
1220 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1221 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001222 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001223
1224 /*
1225 * Move to the proper node structure, in order to access the
1226 * right PCI registers
1227 */
1228 new_mci = get_mci_for_node_id(*socket);
1229 if (!new_mci) {
1230 sprintf(msg, "Struct for socket #%u wasn't initialized",
1231 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001232 return -EINVAL;
1233 }
1234 mci = new_mci;
1235 pvt = mci->pvt_info;
1236
1237 /*
1238 * Step 2) Get memory channel
1239 */
1240 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001241 if (pvt->info.type == SANDY_BRIDGE)
1242 pci_ha = pvt->pci_ha0;
1243 else {
1244 if (sad_ha)
1245 pci_ha = pvt->pci_ha1;
1246 else
1247 pci_ha = pvt->pci_ha0;
1248 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001249 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001250 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001251 limit = TAD_LIMIT(reg);
1252 if (limit <= prv) {
1253 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001254 return -EINVAL;
1255 }
1256 if (addr <= limit)
1257 break;
1258 prv = limit;
1259 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001260 if (n_tads == MAX_TAD) {
1261 sprintf(msg, "Can't discover the memory channel");
1262 return -EINVAL;
1263 }
1264
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001265 ch_way = TAD_CH(reg) + 1;
1266 sck_way = TAD_SOCK(reg) + 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001267
1268 if (ch_way == 3)
1269 idx = addr >> 6;
1270 else
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001271 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001272 idx = idx % ch_way;
1273
1274 /*
1275 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1276 */
1277 switch (idx) {
1278 case 0:
1279 base_ch = TAD_TGT0(reg);
1280 break;
1281 case 1:
1282 base_ch = TAD_TGT1(reg);
1283 break;
1284 case 2:
1285 base_ch = TAD_TGT2(reg);
1286 break;
1287 case 3:
1288 base_ch = TAD_TGT3(reg);
1289 break;
1290 default:
1291 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001292 return -EINVAL;
1293 }
1294 *channel_mask = 1 << base_ch;
1295
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001296 pci_read_config_dword(pvt->pci_tad[base_ch],
1297 tad_ch_nilv_offset[n_tads],
1298 &tad_offset);
1299
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001300 if (pvt->is_mirrored) {
1301 *channel_mask |= 1 << ((base_ch + 2) % 4);
1302 switch(ch_way) {
1303 case 2:
1304 case 4:
1305 sck_xch = 1 << sck_way * (ch_way >> 1);
1306 break;
1307 default:
1308 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001309 return -EINVAL;
1310 }
1311 } else
1312 sck_xch = (1 << sck_way) * ch_way;
1313
1314 if (pvt->is_lockstep)
1315 *channel_mask |= 1 << ((base_ch + 1) % 4);
1316
1317 offset = TAD_OFFSET(tad_offset);
1318
Joe Perches956b9ba2012-04-29 17:08:39 -03001319 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",
1320 n_tads,
1321 addr,
1322 limit,
1323 (u32)TAD_SOCK(reg),
1324 ch_way,
1325 offset,
1326 idx,
1327 base_ch,
1328 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001329
1330 /* Calculate channel address */
1331 /* Remove the TAD offset */
1332
1333 if (offset > addr) {
1334 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1335 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001336 return -EINVAL;
1337 }
1338 addr -= offset;
1339 /* Store the low bits [0:6] of the addr */
1340 ch_addr = addr & 0x7f;
1341 /* Remove socket wayness and remove 6 bits */
1342 addr >>= 6;
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001343 addr = div_u64(addr, sck_xch);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001344#if 0
1345 /* Divide by channel way */
1346 addr = addr / ch_way;
1347#endif
1348 /* Recover the last 6 bits */
1349 ch_addr |= addr << 6;
1350
1351 /*
1352 * Step 3) Decode rank
1353 */
1354 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1355 pci_read_config_dword(pvt->pci_tad[base_ch],
1356 rir_way_limit[n_rir],
1357 &reg);
1358
1359 if (!IS_RIR_VALID(reg))
1360 continue;
1361
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001362 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001363 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001364 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1365 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01001366 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001367 limit,
1368 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001369 if (ch_addr <= limit)
1370 break;
1371 }
1372 if (n_rir == MAX_RIR_RANGES) {
1373 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1374 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001375 return -EINVAL;
1376 }
1377 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001378
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001379 if (pvt->is_close_pg)
1380 idx = (ch_addr >> 6);
1381 else
1382 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1383 idx %= 1 << rir_way;
1384
1385 pci_read_config_dword(pvt->pci_tad[base_ch],
1386 rir_offset[n_rir][idx],
1387 &reg);
1388 *rank = RIR_RNK_TGT(reg);
1389
Joe Perches956b9ba2012-04-29 17:08:39 -03001390 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1391 n_rir,
1392 ch_addr,
1393 limit,
1394 rir_way,
1395 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001396
1397 return 0;
1398}
1399
1400/****************************************************************************
1401 Device initialization routines: put/get, init/exit
1402 ****************************************************************************/
1403
1404/*
1405 * sbridge_put_all_devices 'put' all the devices that we have
1406 * reserved via 'get'
1407 */
1408static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1409{
1410 int i;
1411
Joe Perches956b9ba2012-04-29 17:08:39 -03001412 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001413 for (i = 0; i < sbridge_dev->n_devs; i++) {
1414 struct pci_dev *pdev = sbridge_dev->pdev[i];
1415 if (!pdev)
1416 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03001417 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1418 pdev->bus->number,
1419 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001420 pci_dev_put(pdev);
1421 }
1422}
1423
1424static void sbridge_put_all_devices(void)
1425{
1426 struct sbridge_dev *sbridge_dev, *tmp;
1427
1428 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1429 sbridge_put_devices(sbridge_dev);
1430 free_sbridge_dev(sbridge_dev);
1431 }
1432}
1433
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001434static int sbridge_get_onedevice(struct pci_dev **prev,
1435 u8 *num_mc,
1436 const struct pci_id_table *table,
1437 const unsigned devno)
1438{
1439 struct sbridge_dev *sbridge_dev;
1440 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001441 struct pci_dev *pdev = NULL;
1442 u8 bus = 0;
1443
Jiang Liuec5a0b32014-02-17 13:10:23 +08001444 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001445 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001446 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1447
1448 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1449 dev_descr->dev_id, *prev);
1450
1451 if (!pdev) {
1452 if (*prev) {
1453 *prev = pdev;
1454 return 0;
1455 }
1456
1457 if (dev_descr->optional)
1458 return 0;
1459
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001460 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001461 if (devno == 0)
1462 return -ENODEV;
1463
1464 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001465 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001466 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1467
1468 /* End of list, leave */
1469 return -ENODEV;
1470 }
1471 bus = pdev->bus->number;
1472
1473 sbridge_dev = get_sbridge_dev(bus);
1474 if (!sbridge_dev) {
1475 sbridge_dev = alloc_sbridge_dev(bus, table);
1476 if (!sbridge_dev) {
1477 pci_dev_put(pdev);
1478 return -ENOMEM;
1479 }
1480 (*num_mc)++;
1481 }
1482
1483 if (sbridge_dev->pdev[devno]) {
1484 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001485 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001486 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1487 pci_dev_put(pdev);
1488 return -ENODEV;
1489 }
1490
1491 sbridge_dev->pdev[devno] = pdev;
1492
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001493 /* Be sure that the device is enabled */
1494 if (unlikely(pci_enable_device(pdev) < 0)) {
1495 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001496 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001497 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1498 return -ENODEV;
1499 }
1500
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001501 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -03001502 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001503
1504 /*
1505 * As stated on drivers/pci/search.c, the reference count for
1506 * @from is always decremented if it is not %NULL. So, as we need
1507 * to get all devices up to null, we need to do a get for the device
1508 */
1509 pci_dev_get(pdev);
1510
1511 *prev = pdev;
1512
1513 return 0;
1514}
1515
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001516/*
1517 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001518 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001519 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001520 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03001521 * @table: model specific table
1522 *
1523 * returns 0 in case of success or error code
1524 */
1525static int sbridge_get_all_devices(u8 *num_mc,
1526 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001527{
1528 int i, rc;
1529 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001530
1531 while (table && table->descr) {
1532 for (i = 0; i < table->n_devs; i++) {
1533 pdev = NULL;
1534 do {
1535 rc = sbridge_get_onedevice(&pdev, num_mc,
1536 table, i);
1537 if (rc < 0) {
1538 if (i == 0) {
1539 i = table->n_devs;
1540 break;
1541 }
1542 sbridge_put_all_devices();
1543 return -ENODEV;
1544 }
1545 } while (pdev);
1546 }
1547 table++;
1548 }
1549
1550 return 0;
1551}
1552
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03001553static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1554 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001555{
1556 struct sbridge_pvt *pvt = mci->pvt_info;
1557 struct pci_dev *pdev;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001558 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001559
1560 for (i = 0; i < sbridge_dev->n_devs; i++) {
1561 pdev = sbridge_dev->pdev[i];
1562 if (!pdev)
1563 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001564
1565 switch (pdev->device) {
1566 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
1567 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001568 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001569 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
1570 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001571 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001572 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
1573 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001574 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001575 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
1576 pvt->pci_ha0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001577 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001578 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
1579 pvt->pci_ta = pdev;
1580 break;
1581 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
1582 pvt->pci_ras = pdev;
1583 break;
1584 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
1585 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
1586 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
1587 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
1588 {
1589 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
1590 pvt->pci_tad[id] = pdev;
1591 }
1592 break;
1593 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
1594 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001595 break;
1596 default:
1597 goto error;
1598 }
1599
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001600 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
1601 pdev->vendor, pdev->device,
Joe Perches956b9ba2012-04-29 17:08:39 -03001602 sbridge_dev->bus,
Joe Perches956b9ba2012-04-29 17:08:39 -03001603 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001604 }
1605
1606 /* Check if everything were registered */
1607 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07001608 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001609 goto enodev;
1610
1611 for (i = 0; i < NUM_CHANNELS; i++) {
1612 if (!pvt->pci_tad[i])
1613 goto enodev;
1614 }
1615 return 0;
1616
1617enodev:
1618 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1619 return -ENODEV;
1620
1621error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001622 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
1623 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001624 return -EINVAL;
1625}
1626
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001627static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1628 struct sbridge_dev *sbridge_dev)
1629{
1630 struct sbridge_pvt *pvt = mci->pvt_info;
1631 struct pci_dev *pdev, *tmp;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001632 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001633 bool mode_2ha = false;
1634
1635 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1636 PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, NULL);
1637 if (tmp) {
1638 mode_2ha = true;
1639 pci_dev_put(tmp);
1640 }
1641
1642 for (i = 0; i < sbridge_dev->n_devs; i++) {
1643 pdev = sbridge_dev->pdev[i];
1644 if (!pdev)
1645 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001646
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001647 switch (pdev->device) {
1648 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
1649 pvt->pci_ha0 = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001650 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001651 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
1652 pvt->pci_ta = pdev;
1653 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
1654 pvt->pci_ras = pdev;
1655 break;
1656 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
1657 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
1658 /* if we have 2 HAs active, channels 2 and 3
1659 * are in other device */
1660 if (mode_2ha)
1661 break;
1662 /* fall through */
1663 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
1664 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
1665 {
1666 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
1667 pvt->pci_tad[id] = pdev;
1668 }
1669 break;
1670 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
1671 pvt->pci_ddrio = pdev;
1672 break;
1673 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
1674 if (!mode_2ha)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001675 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001676 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001677 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
1678 pvt->pci_sad0 = pdev;
1679 break;
1680 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
1681 pvt->pci_br0 = pdev;
1682 break;
1683 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
1684 pvt->pci_br1 = pdev;
1685 break;
1686 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
1687 pvt->pci_ha1 = pdev;
1688 break;
1689 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
1690 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
1691 {
1692 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 2;
1693
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001694 /* we shouldn't have this device if we have just one
1695 * HA present */
1696 WARN_ON(!mode_2ha);
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001697 pvt->pci_tad[id] = pdev;
1698 }
1699 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001700 default:
1701 goto error;
1702 }
1703
1704 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1705 sbridge_dev->bus,
1706 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1707 pdev);
1708 }
1709
1710 /* Check if everything were registered */
1711 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1712 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
1713 !pvt->pci_ta)
1714 goto enodev;
1715
1716 for (i = 0; i < NUM_CHANNELS; i++) {
1717 if (!pvt->pci_tad[i])
1718 goto enodev;
1719 }
1720 return 0;
1721
1722enodev:
1723 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1724 return -ENODEV;
1725
1726error:
1727 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001728 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
1729 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001730 return -EINVAL;
1731}
1732
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001733static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
1734 struct sbridge_dev *sbridge_dev)
1735{
1736 struct sbridge_pvt *pvt = mci->pvt_info;
1737 struct pci_dev *pdev, *tmp;
1738 int i;
1739 bool mode_2ha = false;
1740
1741 tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
1742 PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, NULL);
1743 if (tmp) {
1744 mode_2ha = true;
1745 pci_dev_put(tmp);
1746 }
1747
1748 /* there's only one device per system; not tied to any bus */
1749 if (pvt->info.pci_vtd == NULL)
1750 /* result will be checked later */
1751 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1752 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
1753 NULL);
1754
1755 for (i = 0; i < sbridge_dev->n_devs; i++) {
1756 pdev = sbridge_dev->pdev[i];
1757 if (!pdev)
1758 continue;
1759
1760 switch (pdev->device) {
1761 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
1762 pvt->pci_sad0 = pdev;
1763 break;
1764 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
1765 pvt->pci_sad1 = pdev;
1766 break;
1767 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
1768 pvt->pci_ha0 = pdev;
1769 break;
1770 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
1771 pvt->pci_ta = pdev;
1772 break;
1773 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
1774 pvt->pci_ras = pdev;
1775 break;
1776 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
1777 pvt->pci_tad[0] = pdev;
1778 break;
1779 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
1780 pvt->pci_tad[1] = pdev;
1781 break;
1782 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
1783 if (!mode_2ha)
1784 pvt->pci_tad[2] = pdev;
1785 break;
1786 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
1787 if (!mode_2ha)
1788 pvt->pci_tad[3] = pdev;
1789 break;
1790 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
1791 pvt->pci_ddrio = pdev;
1792 break;
1793 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
1794 pvt->pci_ha1 = pdev;
1795 break;
1796 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
1797 pvt->pci_ha1_ta = pdev;
1798 break;
1799 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
1800 if (mode_2ha)
1801 pvt->pci_tad[2] = pdev;
1802 break;
1803 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
1804 if (mode_2ha)
1805 pvt->pci_tad[3] = pdev;
1806 break;
1807 default:
1808 break;
1809 }
1810
1811 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1812 sbridge_dev->bus,
1813 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1814 pdev);
1815 }
1816
1817 /* Check if everything were registered */
1818 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
1819 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
1820 goto enodev;
1821
1822 for (i = 0; i < NUM_CHANNELS; i++) {
1823 if (!pvt->pci_tad[i])
1824 goto enodev;
1825 }
1826 return 0;
1827
1828enodev:
1829 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1830 return -ENODEV;
1831}
1832
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001833/****************************************************************************
1834 Error check routines
1835 ****************************************************************************/
1836
1837/*
1838 * While Sandy Bridge has error count registers, SMI BIOS read values from
1839 * and resets the counters. So, they are not reliable for the OS to read
1840 * from them. So, we have no option but to just trust on whatever MCE is
1841 * telling us about the errors.
1842 */
1843static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1844 const struct mce *m)
1845{
1846 struct mem_ctl_info *new_mci;
1847 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001848 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001849 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001850 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1851 bool overflow = GET_BITFIELD(m->status, 62, 62);
1852 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001853 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001854 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1855 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1856 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1857 u32 channel = GET_BITFIELD(m->status, 0, 3);
1858 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1859 long channel_mask, first_channel;
1860 u8 rank, socket;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001861 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001862 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001863
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001864 if (pvt->info.type == IVY_BRIDGE)
1865 recoverable = true;
1866 else
1867 recoverable = GET_BITFIELD(m->status, 56, 56);
1868
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001869 if (uncorrected_error) {
1870 if (ripv) {
1871 type = "FATAL";
1872 tp_event = HW_EVENT_ERR_FATAL;
1873 } else {
1874 type = "NON_FATAL";
1875 tp_event = HW_EVENT_ERR_UNCORRECTED;
1876 }
1877 } else {
1878 type = "CORRECTED";
1879 tp_event = HW_EVENT_ERR_CORRECTED;
1880 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001881
1882 /*
David Mackey15ed1032012-04-17 11:30:52 -07001883 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001884 * memory errors should fit in this mask:
1885 * 000f 0000 1mmm cccc (binary)
1886 * where:
1887 * f = Correction Report Filtering Bit. If 1, subsequent errors
1888 * won't be shown
1889 * mmm = error type
1890 * cccc = channel
1891 * If the mask doesn't match, report an error to the parsing logic
1892 */
1893 if (! ((errcode & 0xef80) == 0x80)) {
1894 optype = "Can't parse: it is not a mem";
1895 } else {
1896 switch (optypenum) {
1897 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001898 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001899 break;
1900 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001901 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001902 break;
1903 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001904 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001905 break;
1906 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001907 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001908 break;
1909 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001910 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001911 break;
1912 default:
1913 optype = "reserved";
1914 break;
1915 }
1916 }
1917
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03001918 /* Only decode errors with an valid address (ADDRV) */
1919 if (!GET_BITFIELD(m->status, 58, 58))
1920 return;
1921
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001922 rc = get_memory_error_data(mci, m->addr, &socket,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001923 &channel_mask, &rank, &area_type, msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001924 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001925 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001926 new_mci = get_mci_for_node_id(socket);
1927 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001928 strcpy(msg, "Error: socket got corrupted!");
1929 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001930 }
1931 mci = new_mci;
1932 pvt = mci->pvt_info;
1933
1934 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1935
1936 if (rank < 4)
1937 dimm = 0;
1938 else if (rank < 8)
1939 dimm = 1;
1940 else
1941 dimm = 2;
1942
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001943
1944 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001945 * FIXME: On some memory configurations (mirror, lockstep), the
1946 * Memory Controller can't point the error to a single DIMM. The
1947 * EDAC core should be handling the channel mask, in order to point
1948 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001949 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03001950 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
1951 channel = first_channel;
1952
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001953 snprintf(msg, sizeof(msg),
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001954 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001955 overflow ? " OVERFLOW" : "",
1956 (uncorrected_error && recoverable) ? " recoverable" : "",
1957 area_type,
1958 mscod, errcode,
1959 socket,
1960 channel_mask,
1961 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001962
Joe Perches956b9ba2012-04-29 17:08:39 -03001963 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001964
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001965 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001966
Seth Jennings351fc4a2014-09-05 14:28:47 -05001967 if (channel == CHANNEL_UNSPECIFIED)
1968 channel = -1;
1969
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001970 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001971 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001972 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1973 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001974 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001975 return;
1976err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03001977 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001978 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03001979 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001980
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001981}
1982
1983/*
1984 * sbridge_check_error Retrieve and process errors reported by the
1985 * hardware. Called by the Core module.
1986 */
1987static void sbridge_check_error(struct mem_ctl_info *mci)
1988{
1989 struct sbridge_pvt *pvt = mci->pvt_info;
1990 int i;
1991 unsigned count = 0;
1992 struct mce *m;
1993
1994 /*
1995 * MCE first step: Copy all mce errors into a temporary buffer
1996 * We use a double buffering here, to reduce the risk of
1997 * loosing an error.
1998 */
1999 smp_rmb();
2000 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
2001 % MCE_LOG_LEN;
2002 if (!count)
2003 return;
2004
2005 m = pvt->mce_outentry;
2006 if (pvt->mce_in + count > MCE_LOG_LEN) {
2007 unsigned l = MCE_LOG_LEN - pvt->mce_in;
2008
2009 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
2010 smp_wmb();
2011 pvt->mce_in = 0;
2012 count -= l;
2013 m += l;
2014 }
2015 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
2016 smp_wmb();
2017 pvt->mce_in += count;
2018
2019 smp_rmb();
2020 if (pvt->mce_overrun) {
2021 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
2022 pvt->mce_overrun);
2023 smp_wmb();
2024 pvt->mce_overrun = 0;
2025 }
2026
2027 /*
2028 * MCE second step: parse errors and display
2029 */
2030 for (i = 0; i < count; i++)
2031 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
2032}
2033
2034/*
2035 * sbridge_mce_check_error Replicates mcelog routine to get errors
2036 * This routine simply queues mcelog errors, and
2037 * return. The error itself should be handled later
2038 * by sbridge_check_error.
2039 * WARNING: As this routine should be called at NMI time, extra care should
2040 * be taken to avoid deadlocks, and to be as fast as possible.
2041 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002042static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
2043 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002044{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002045 struct mce *mce = (struct mce *)data;
2046 struct mem_ctl_info *mci;
2047 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04002048 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002049
Chen, Gongfd521032013-12-06 01:17:09 -05002050 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2051 return NOTIFY_DONE;
2052
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002053 mci = get_mci_for_node_id(mce->socketid);
2054 if (!mci)
2055 return NOTIFY_BAD;
2056 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002057
2058 /*
2059 * Just let mcelog handle it if the error is
2060 * outside the memory controller. A memory error
2061 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
2062 * bit 12 has an special meaning.
2063 */
2064 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002065 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002066
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04002067 if (mce->mcgstatus & MCG_STATUS_MCIP)
2068 type = "Exception";
2069 else
2070 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002071
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04002072 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002073
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04002074 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
2075 "Bank %d: %016Lx\n", mce->extcpu, type,
2076 mce->mcgstatus, mce->bank, mce->status);
2077 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
2078 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
2079 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002080
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04002081 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
2082 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
2083 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002084
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002085 smp_rmb();
2086 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
2087 smp_wmb();
2088 pvt->mce_overrun++;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002089 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002090 }
2091
2092 /* Copy memory error at the ringbuffer */
2093 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
2094 smp_wmb();
2095 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
2096
2097 /* Handle fatal errors immediately */
2098 if (mce->mcgstatus & 1)
2099 sbridge_check_error(mci);
2100
2101 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002102 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002103}
2104
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02002105static struct notifier_block sbridge_mce_dec = {
2106 .notifier_call = sbridge_mce_check_error,
2107};
2108
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002109/****************************************************************************
2110 EDAC register/unregister logic
2111 ****************************************************************************/
2112
2113static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
2114{
2115 struct mem_ctl_info *mci = sbridge_dev->mci;
2116 struct sbridge_pvt *pvt;
2117
2118 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002119 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002120
2121 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
2122 return;
2123 }
2124
2125 pvt = mci->pvt_info;
2126
Joe Perches956b9ba2012-04-29 17:08:39 -03002127 edac_dbg(0, "MC: mci = %p, dev = %p\n",
2128 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002129
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002130 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03002131 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002132
Joe Perches956b9ba2012-04-29 17:08:39 -03002133 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002134 kfree(mci->ctl_name);
2135 edac_mc_free(mci);
2136 sbridge_dev->mci = NULL;
2137}
2138
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002139static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002140{
2141 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002142 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002143 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002144 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002145 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002146
2147 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002148 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002149 if (unlikely(rc < 0))
2150 return rc;
2151
2152 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002153 layers[0].type = EDAC_MC_LAYER_CHANNEL;
2154 layers[0].size = NUM_CHANNELS;
2155 layers[0].is_virt_csrow = false;
2156 layers[1].type = EDAC_MC_LAYER_SLOT;
2157 layers[1].size = MAX_DIMMS;
2158 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03002159 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002160 sizeof(*pvt));
2161
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002162 if (unlikely(!mci))
2163 return -ENOMEM;
2164
Joe Perches956b9ba2012-04-29 17:08:39 -03002165 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002166 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002167
2168 pvt = mci->pvt_info;
2169 memset(pvt, 0, sizeof(*pvt));
2170
2171 /* Associate sbridge_dev and mci for future usage */
2172 pvt->sbridge_dev = sbridge_dev;
2173 sbridge_dev->mci = mci;
2174
2175 mci->mtype_cap = MEM_FLAG_DDR3;
2176 mci->edac_ctl_cap = EDAC_FLAG_NONE;
2177 mci->edac_cap = EDAC_FLAG_NONE;
2178 mci->mod_name = "sbridge_edac.c";
2179 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002180 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002181 mci->ctl_page_to_phys = NULL;
2182
2183 /* Set the function pointer to an actual operation function */
2184 mci->edac_check = sbridge_check_error;
2185
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002186 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002187 switch (type) {
2188 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002189 pvt->info.rankcfgr = IB_RANK_CFG_A;
2190 pvt->info.get_tolm = ibridge_get_tolm;
2191 pvt->info.get_tohm = ibridge_get_tohm;
2192 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002193 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002194 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002195 pvt->info.rir_limit = rir_limit;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002196 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2197 pvt->info.interleave_list = ibridge_interleave_list;
2198 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2199 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2200 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
2201
2202 /* Store pci devices at mci for faster access */
2203 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
2204 if (unlikely(rc < 0))
2205 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002206 break;
2207 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002208 pvt->info.rankcfgr = SB_RANK_CFG_A;
2209 pvt->info.get_tolm = sbridge_get_tolm;
2210 pvt->info.get_tohm = sbridge_get_tohm;
2211 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03002212 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03002213 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002214 pvt->info.rir_limit = rir_limit;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002215 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2216 pvt->info.interleave_list = sbridge_interleave_list;
2217 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2218 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2219 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2220
2221 /* Store pci devices at mci for faster access */
2222 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2223 if (unlikely(rc < 0))
2224 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002225 break;
2226 case HASWELL:
2227 /* rankcfgr isn't used */
2228 pvt->info.get_tolm = haswell_get_tolm;
2229 pvt->info.get_tohm = haswell_get_tohm;
2230 pvt->info.dram_rule = ibridge_dram_rule;
2231 pvt->info.get_memory_type = haswell_get_memory_type;
2232 pvt->info.get_node_id = haswell_get_node_id;
2233 pvt->info.rir_limit = haswell_rir_limit;
2234 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2235 pvt->info.interleave_list = ibridge_interleave_list;
2236 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2237 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2238 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002239
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002240 /* Store pci devices at mci for faster access */
2241 rc = haswell_mci_bind_devs(mci, sbridge_dev);
2242 if (unlikely(rc < 0))
2243 goto fail0;
2244 break;
2245 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002246
2247 /* Get dimm basic config and the memory layout */
2248 get_dimm_config(mci);
2249 get_memory_layout(mci);
2250
2251 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002252 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002253
2254 /* add this new MC control structure to EDAC's list of MCs */
2255 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002256 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002257 rc = -EINVAL;
2258 goto fail0;
2259 }
2260
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002261 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002262
2263fail0:
2264 kfree(mci->ctl_name);
2265 edac_mc_free(mci);
2266 sbridge_dev->mci = NULL;
2267 return rc;
2268}
2269
2270/*
2271 * sbridge_probe Probe for ONE instance of device to see if it is
2272 * present.
2273 * return:
2274 * 0 for FOUND a device
2275 * < 0 for error code
2276 */
2277
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002278static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002279{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002280 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002281 u8 mc, num_mc = 0;
2282 struct sbridge_dev *sbridge_dev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002283 enum type type = SANDY_BRIDGE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002284
2285 /* get the pci devices we want to reserve for our use */
2286 mutex_lock(&sbridge_edac_lock);
2287
2288 /*
2289 * All memory controllers are allocated at the first pass.
2290 */
2291 if (unlikely(probed >= 1)) {
2292 mutex_unlock(&sbridge_edac_lock);
2293 return -ENODEV;
2294 }
2295 probed++;
2296
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002297 switch (pdev->device) {
2298 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002299 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2300 type = IVY_BRIDGE;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002301 break;
2302 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002303 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2304 type = SANDY_BRIDGE;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002305 break;
2306 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2307 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_haswell_table);
2308 type = HASWELL;
2309 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002310 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002311 if (unlikely(rc < 0))
2312 goto fail0;
2313 mc = 0;
2314
2315 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03002316 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2317 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002318
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002319 sbridge_dev->mc = mc++;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002320 rc = sbridge_register_mci(sbridge_dev, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002321 if (unlikely(rc < 0))
2322 goto fail1;
2323 }
2324
2325 sbridge_printk(KERN_INFO, "Driver loaded.\n");
2326
2327 mutex_unlock(&sbridge_edac_lock);
2328 return 0;
2329
2330fail1:
2331 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2332 sbridge_unregister_mci(sbridge_dev);
2333
2334 sbridge_put_all_devices();
2335fail0:
2336 mutex_unlock(&sbridge_edac_lock);
2337 return rc;
2338}
2339
2340/*
2341 * sbridge_remove destructor for one instance of device
2342 *
2343 */
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002344static void sbridge_remove(struct pci_dev *pdev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002345{
2346 struct sbridge_dev *sbridge_dev;
2347
Joe Perches956b9ba2012-04-29 17:08:39 -03002348 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002349
2350 /*
2351 * we have a trouble here: pdev value for removal will be wrong, since
2352 * it will point to the X58 register used to detect that the machine
2353 * is a Nehalem or upper design. However, due to the way several PCI
2354 * devices are grouped together to provide MC functionality, we need
2355 * to use a different method for releasing the devices
2356 */
2357
2358 mutex_lock(&sbridge_edac_lock);
2359
2360 if (unlikely(!probed)) {
2361 mutex_unlock(&sbridge_edac_lock);
2362 return;
2363 }
2364
2365 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2366 sbridge_unregister_mci(sbridge_dev);
2367
2368 /* Release PCI resources */
2369 sbridge_put_all_devices();
2370
2371 probed--;
2372
2373 mutex_unlock(&sbridge_edac_lock);
2374}
2375
2376MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2377
2378/*
2379 * sbridge_driver pci_driver structure for this module
2380 *
2381 */
2382static struct pci_driver sbridge_driver = {
2383 .name = "sbridge_edac",
2384 .probe = sbridge_probe,
Greg Kroah-Hartman9b3c6e82012-12-21 13:23:51 -08002385 .remove = sbridge_remove,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002386 .id_table = sbridge_pci_tbl,
2387};
2388
2389/*
2390 * sbridge_init Module entry function
2391 * Try to initialize this module for its devices
2392 */
2393static int __init sbridge_init(void)
2394{
2395 int pci_rc;
2396
Joe Perches956b9ba2012-04-29 17:08:39 -03002397 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002398
2399 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2400 opstate_init();
2401
2402 pci_rc = pci_register_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002403 if (pci_rc >= 0) {
2404 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05002405 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2406 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002407 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03002408 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002409
2410 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2411 pci_rc);
2412
2413 return pci_rc;
2414}
2415
2416/*
2417 * sbridge_exit() Module exit function
2418 * Unregister the driver
2419 */
2420static void __exit sbridge_exit(void)
2421{
Joe Perches956b9ba2012-04-29 17:08:39 -03002422 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002423 pci_unregister_driver(&sbridge_driver);
Chen Gonge35fca42012-05-08 20:40:12 -03002424 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002425}
2426
2427module_init(sbridge_init);
2428module_exit(sbridge_exit);
2429
2430module_param(edac_op_state, int, 0444);
2431MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2432
2433MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002434MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002435MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002436MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002437 SBRIDGE_REVISION);