blob: ace662e8a7a846a0ccd1d53b17e06ac0f1f74e2a [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>
Tony Luck2c1ea4c2016-04-28 15:40:00 -070024#include <linux/mod_devicetable.h>
25#include <asm/cpu_device_id.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020026#include <asm/processor.h>
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -020027#include <asm/mce.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020028
29#include "edac_core.h"
30
31/* Static vars */
32static LIST_HEAD(sbridge_edac_list);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020033
34/*
35 * Alter this version for the module when modifications are made
36 */
Tony Luck7d375bf2015-05-18 17:50:42 -030037#define SBRIDGE_REVISION " Ver: 1.1.1 "
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
Jim Snowd0cdf902015-12-03 10:48:54 +010068static const u32 knl_dram_rule[] = {
69 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
70 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
71 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
72 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
73 0x100, 0x108, 0x110, 0x118, /* 20-23 */
74};
75
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020076#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -030077#define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020078
Jim Snowc59f9c02015-12-03 10:48:52 +010079static char *show_dram_attr(u32 attr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020080{
Jim Snowc59f9c02015-12-03 10:48:52 +010081 switch (attr) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020082 case 0:
83 return "DRAM";
84 case 1:
85 return "MMCFG";
86 case 2:
87 return "NXM";
88 default:
89 return "unknown";
90 }
91}
92
Aristeu Rozanskief1ce512013-10-30 13:27:01 -030093static const u32 sbridge_interleave_list[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020094 0x84, 0x8c, 0x94, 0x9c, 0xa4,
95 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
96};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020097
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030098static const u32 ibridge_interleave_list[] = {
99 0x64, 0x6c, 0x74, 0x7c, 0x84,
100 0x8c, 0x94, 0x9c, 0xa4, 0xac,
101 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
102 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
103};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200104
Jim Snowd0cdf902015-12-03 10:48:54 +0100105static const u32 knl_interleave_list[] = {
106 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
107 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
108 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
109 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
110 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
111};
112
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300113struct interleave_pkg {
114 unsigned char start;
115 unsigned char end;
116};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200117
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300118static const struct interleave_pkg sbridge_interleave_pkg[] = {
119 { 0, 2 },
120 { 3, 5 },
121 { 8, 10 },
122 { 11, 13 },
123 { 16, 18 },
124 { 19, 21 },
125 { 24, 26 },
126 { 27, 29 },
127};
128
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300129static const struct interleave_pkg ibridge_interleave_pkg[] = {
130 { 0, 3 },
131 { 4, 7 },
132 { 8, 11 },
133 { 12, 15 },
134 { 16, 19 },
135 { 20, 23 },
136 { 24, 27 },
137 { 28, 31 },
138};
139
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300140static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
141 int interleave)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200142{
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300143 return GET_BITFIELD(reg, table[interleave].start,
144 table[interleave].end);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200145}
146
147/* Devices 12 Function 7 */
148
149#define TOLM 0x80
Jim Snowd0cdf902015-12-03 10:48:54 +0100150#define TOHM 0x84
Tony Luckf7cf2a22014-10-29 10:36:50 -0700151#define HASWELL_TOLM 0xd0
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300152#define HASWELL_TOHM_0 0xd4
153#define HASWELL_TOHM_1 0xd8
Jim Snowd0cdf902015-12-03 10:48:54 +0100154#define KNL_TOLM 0xd0
155#define KNL_TOHM_0 0xd4
156#define KNL_TOHM_1 0xd8
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200157
158#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
159#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
160
161/* Device 13 Function 6 */
162
163#define SAD_TARGET 0xf0
164
165#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
166
Jim Snowd0cdf902015-12-03 10:48:54 +0100167#define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
168
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200169#define SAD_CONTROL 0xf4
170
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200171/* Device 14 function 0 */
172
173static const u32 tad_dram_rule[] = {
174 0x40, 0x44, 0x48, 0x4c,
175 0x50, 0x54, 0x58, 0x5c,
176 0x60, 0x64, 0x68, 0x6c,
177};
178#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
179
180#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
181#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
182#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
183#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
184#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
185#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
186#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
187
188/* Device 15, function 0 */
189
190#define MCMTR 0x7c
Jim Snowd0cdf902015-12-03 10:48:54 +0100191#define KNL_MCMTR 0x624
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200192
193#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
194#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
195#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
196
197/* Device 15, function 1 */
198
199#define RASENABLES 0xac
200#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
201
202/* Device 15, functions 2-5 */
203
204static const int mtr_regs[] = {
205 0x80, 0x84, 0x88,
206};
207
Jim Snowd0cdf902015-12-03 10:48:54 +0100208static const int knl_mtr_reg = 0xb60;
209
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200210#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
211#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
212#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
213#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
214#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
215
216static const u32 tad_ch_nilv_offset[] = {
217 0x90, 0x94, 0x98, 0x9c,
218 0xa0, 0xa4, 0xa8, 0xac,
219 0xb0, 0xb4, 0xb8, 0xbc,
220};
221#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
222#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
223
224static const u32 rir_way_limit[] = {
225 0x108, 0x10c, 0x110, 0x114, 0x118,
226};
227#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
228
229#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
230#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200231
232#define MAX_RIR_WAY 8
233
234static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
235 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
236 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
237 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
238 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
239 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
240};
241
Tony Luckc7103f62016-05-31 11:50:28 -0700242#define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
243 GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
244
245#define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
246 GET_BITFIELD(reg, 2, 15) : GET_BITFIELD(reg, 2, 14))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200247
248/* Device 16, functions 2-7 */
249
250/*
251 * FIXME: Implement the error count reads directly
252 */
253
254static const u32 correrrcnt[] = {
255 0x104, 0x108, 0x10c, 0x110,
256};
257
258#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
259#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
260#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
261#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
262
263static const u32 correrrthrsld[] = {
264 0x11c, 0x120, 0x124, 0x128,
265};
266
267#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
268#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
269
270
271/* Device 17, function 0 */
272
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300273#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200274
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300275#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200276
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200277/*
278 * sbridge structs
279 */
280
Tony Luck7d375bf2015-05-18 17:50:42 -0300281#define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
Seth Jennings351fc4a2014-09-05 14:28:47 -0500282#define MAX_DIMMS 3 /* Max DIMMS per channel */
Jim Snowd0cdf902015-12-03 10:48:54 +0100283#define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
284#define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
285#define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
Seth Jennings351fc4a2014-09-05 14:28:47 -0500286#define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200287
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300288enum type {
289 SANDY_BRIDGE,
290 IVY_BRIDGE,
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300291 HASWELL,
Tony Luck1f395812014-12-02 09:27:30 -0800292 BROADWELL,
Jim Snowd0cdf902015-12-03 10:48:54 +0100293 KNIGHTS_LANDING,
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300294};
295
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300296struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200297struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300298 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300299 u32 mcmtr;
300 u32 rankcfgr;
301 u64 (*get_tolm)(struct sbridge_pvt *pvt);
302 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300303 u64 (*rir_limit)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100304 u64 (*sad_limit)(u32 reg);
305 u32 (*interleave_mode)(u32 reg);
306 char* (*show_interleave_mode)(u32 reg);
307 u32 (*dram_attr)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300308 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300309 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300310 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300311 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300312 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300313 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300314 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400315 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300316 struct pci_dev *pci_vtd;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200317};
318
319struct sbridge_channel {
320 u32 ranks;
321 u32 dimms;
322};
323
324struct pci_id_descr {
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -0300325 int dev_id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200326 int optional;
327};
328
329struct pci_id_table {
330 const struct pci_id_descr *descr;
331 int n_devs;
332};
333
334struct sbridge_dev {
335 struct list_head list;
336 u8 bus, mc;
337 u8 node_id, source_id;
338 struct pci_dev **pdev;
339 int n_devs;
340 struct mem_ctl_info *mci;
341};
342
Jim Snowd0cdf902015-12-03 10:48:54 +0100343struct knl_pvt {
344 struct pci_dev *pci_cha[KNL_MAX_CHAS];
345 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
346 struct pci_dev *pci_mc0;
347 struct pci_dev *pci_mc1;
348 struct pci_dev *pci_mc0_misc;
349 struct pci_dev *pci_mc1_misc;
350 struct pci_dev *pci_mc_info; /* tolm, tohm */
351};
352
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200353struct sbridge_pvt {
354 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300355 struct pci_dev *pci_sad0, *pci_sad1;
356 struct pci_dev *pci_ha0, *pci_ha1;
357 struct pci_dev *pci_br0, *pci_br1;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300358 struct pci_dev *pci_ha1_ta;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200359 struct pci_dev *pci_tad[NUM_CHANNELS];
360
361 struct sbridge_dev *sbridge_dev;
362
363 struct sbridge_info info;
364 struct sbridge_channel channel[NUM_CHANNELS];
365
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200366 /* Memory type detection */
367 bool is_mirrored, is_lockstep, is_close_pg;
Tony Luckea5dfb52016-04-14 10:22:02 -0700368 bool is_chan_hash;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200369
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200370 /* Memory description */
371 u64 tolm, tohm;
Jim Snowd0cdf902015-12-03 10:48:54 +0100372 struct knl_pvt knl;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200373};
374
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300375#define PCI_DESCR(device_id, opt) \
376 .dev_id = (device_id), \
Luck, Tonyde4772c2013-03-28 09:59:15 -0700377 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200378
379static const struct pci_id_descr pci_dev_descr_sbridge[] = {
380 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300381 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200382
383 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300384 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
385 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
386 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
387 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
388 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
389 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
390 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200391
392 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300393 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
394 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200395
396 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300397 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200398};
399
400#define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
401static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
402 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
403 {0,} /* 0 terminated list. */
404};
405
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300406/* This changes depending if 1HA or 2HA:
407 * 1HA:
408 * 0x0eb8 (17.0) is DDRIO0
409 * 2HA:
410 * 0x0ebc (17.4) is DDRIO0
411 */
412#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
413#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
414
415/* pci ids */
416#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
417#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
418#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
419#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
420#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
421#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
422#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
423#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
424#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
425#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
426#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
427#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
428#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
429#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
430#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
Tony Luck7d375bf2015-05-18 17:50:42 -0300431#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
432#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300433
434static const struct pci_id_descr pci_dev_descr_ibridge[] = {
435 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300436 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300437
438 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300439 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
440 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
441 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
442 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
443 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
444 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300445
446 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300447 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300448
449 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300450 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
451 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300452
453 /* Optional, mode 2HA */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300454 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300455#if 0
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300456 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
457 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300458#endif
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300459 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
460 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
Tony Luck7d375bf2015-05-18 17:50:42 -0300461 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
462 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300463
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300464 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
465 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300466};
467
468static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
469 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
470 {0,} /* 0 terminated list. */
471};
472
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300473/* Haswell support */
474/* EN processor:
475 * - 1 IMC
476 * - 3 DDR3 channels, 2 DPC per channel
477 * EP processor:
478 * - 1 or 2 IMC
479 * - 4 DDR4 channels, 3 DPC per channel
480 * EP 4S processor:
481 * - 2 IMC
482 * - 4 DDR4 channels, 3 DPC per channel
483 * EX processor:
484 * - 2 IMC
485 * - each IMC interfaces with a SMI 2 channel
486 * - each SMI channel interfaces with a scalable memory buffer
487 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
488 */
Tony Luck1f395812014-12-02 09:27:30 -0800489#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300490#define HASWELL_HASYSDEFEATURE2 0x84
491#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
492#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
493#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
494#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
495#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
496#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
497#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
498#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
499#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
500#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
501#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
502#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
503#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
504#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
505#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
506#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
507#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
508#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
Aristeu Rozanski71793852015-06-12 09:44:52 -0400509#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
510#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
511#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300512static const struct pci_id_descr pci_dev_descr_haswell[] = {
513 /* first item must be the HA */
514 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
515
516 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
517 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
518
519 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
520
521 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
522 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
523 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
524 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
525 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
526 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
527
528 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
Aristeu Rozanski71793852015-06-12 09:44:52 -0400529 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
530 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
531 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300532
533 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
534 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
535 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
536 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
537 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
538 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
539};
540
541static const struct pci_id_table pci_dev_descr_haswell_table[] = {
542 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
543 {0,} /* 0 terminated list. */
544};
545
Jim Snowd0cdf902015-12-03 10:48:54 +0100546/* Knight's Landing Support */
547/*
548 * KNL's memory channels are swizzled between memory controllers.
549 * MC0 is mapped to CH3,5,6 and MC1 is mapped to CH0,1,2
550 */
551#define knl_channel_remap(channel) ((channel + 3) % 6)
552
553/* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
554#define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
555/* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
556#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
557/* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
558#define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
559/* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
560#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
561/* SAD target - 1-29-1 (1 of these) */
562#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
563/* Caching / Home Agent */
564#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
565/* Device with TOLM and TOHM, 0-5-0 (1 of these) */
566#define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
567
568/*
569 * KNL differs from SB, IB, and Haswell in that it has multiple
570 * instances of the same device with the same device ID, so we handle that
571 * by creating as many copies in the table as we expect to find.
572 * (Like device ID must be grouped together.)
573 */
574
575static const struct pci_id_descr pci_dev_descr_knl[] = {
576 [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
577 [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
578 [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
579 [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
580 [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
581 [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
582 [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
583};
584
585static const struct pci_id_table pci_dev_descr_knl_table[] = {
586 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl),
587 {0,}
588};
589
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200590/*
Tony Luck1f395812014-12-02 09:27:30 -0800591 * Broadwell support
592 *
593 * DE processor:
594 * - 1 IMC
595 * - 2 DDR3 channels, 2 DPC per channel
Tony Luckfa2ce642015-05-20 19:10:35 -0300596 * EP processor:
597 * - 1 or 2 IMC
598 * - 4 DDR4 channels, 3 DPC per channel
599 * EP 4S processor:
600 * - 2 IMC
601 * - 4 DDR4 channels, 3 DPC per channel
602 * EX processor:
603 * - 2 IMC
604 * - each IMC interfaces with a SMI 2 channel
605 * - each SMI channel interfaces with a scalable memory buffer
606 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
Tony Luck1f395812014-12-02 09:27:30 -0800607 */
608#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
609#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
Tony Luckfa2ce642015-05-20 19:10:35 -0300610#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
Tony Luck1f395812014-12-02 09:27:30 -0800611#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
612#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
Tony Luckfa2ce642015-05-20 19:10:35 -0300613#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
614#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
Tony Luck1f395812014-12-02 09:27:30 -0800615#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
616#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
617#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
618#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
619#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
620#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
Tony Luckfa2ce642015-05-20 19:10:35 -0300621#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
622#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
623#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
624#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
Tony Luck1f395812014-12-02 09:27:30 -0800625#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
626
627static const struct pci_id_descr pci_dev_descr_broadwell[] = {
628 /* first item must be the HA */
629 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
630
631 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
632 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
633
Tony Luckfa2ce642015-05-20 19:10:35 -0300634 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
635
Tony Luck1f395812014-12-02 09:27:30 -0800636 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
637 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
638 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
639 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300640 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
641 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
642
Tony Luck1f395812014-12-02 09:27:30 -0800643 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300644
645 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
646 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
647 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
648 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
649 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
650 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
Tony Luck1f395812014-12-02 09:27:30 -0800651};
652
653static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
654 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
655 {0,} /* 0 terminated list. */
656};
657
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200658
659/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700660 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200661 ****************************************************************************/
662
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300663static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200664{
665 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300666 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200667
Jim Snowd0cdf902015-12-03 10:48:54 +0100668 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300669 max = 8;
670
671 if (ranks > max) {
672 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
673 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200674 return -EINVAL;
675 }
676
677 return ranks;
678}
679
680static inline int numrow(u32 mtr)
681{
682 int rows = (RANK_WIDTH_BITS(mtr) + 12);
683
684 if (rows < 13 || rows > 18) {
Joe Perches956b9ba12012-04-29 17:08:39 -0300685 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
686 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200687 return -EINVAL;
688 }
689
690 return 1 << rows;
691}
692
693static inline int numcol(u32 mtr)
694{
695 int cols = (COL_WIDTH_BITS(mtr) + 10);
696
697 if (cols > 12) {
Joe Perches956b9ba12012-04-29 17:08:39 -0300698 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
699 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200700 return -EINVAL;
701 }
702
703 return 1 << cols;
704}
705
Jim Snowc1979ba2015-12-03 10:48:53 +0100706static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200707{
708 struct sbridge_dev *sbridge_dev;
709
Jim Snowc1979ba2015-12-03 10:48:53 +0100710 /*
711 * If we have devices scattered across several busses that pertain
712 * to the same memory controller, we'll lump them all together.
713 */
714 if (multi_bus) {
715 return list_first_entry_or_null(&sbridge_edac_list,
716 struct sbridge_dev, list);
717 }
718
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200719 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
720 if (sbridge_dev->bus == bus)
721 return sbridge_dev;
722 }
723
724 return NULL;
725}
726
727static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
728 const struct pci_id_table *table)
729{
730 struct sbridge_dev *sbridge_dev;
731
732 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
733 if (!sbridge_dev)
734 return NULL;
735
736 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
737 GFP_KERNEL);
738 if (!sbridge_dev->pdev) {
739 kfree(sbridge_dev);
740 return NULL;
741 }
742
743 sbridge_dev->bus = bus;
744 sbridge_dev->n_devs = table->n_devs;
745 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
746
747 return sbridge_dev;
748}
749
750static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
751{
752 list_del(&sbridge_dev->list);
753 kfree(sbridge_dev->pdev);
754 kfree(sbridge_dev);
755}
756
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300757static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
758{
759 u32 reg;
760
761 /* Address range is 32:28 */
762 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
763 return GET_TOLM(reg);
764}
765
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300766static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
767{
768 u32 reg;
769
770 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
771 return GET_TOHM(reg);
772}
773
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300774static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
775{
776 u32 reg;
777
778 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
779
780 return GET_TOLM(reg);
781}
782
783static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
784{
785 u32 reg;
786
787 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
788
789 return GET_TOHM(reg);
790}
791
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300792static u64 rir_limit(u32 reg)
793{
794 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
795}
796
Jim Snowc59f9c02015-12-03 10:48:52 +0100797static u64 sad_limit(u32 reg)
798{
799 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
800}
801
802static u32 interleave_mode(u32 reg)
803{
804 return GET_BITFIELD(reg, 1, 1);
805}
806
807char *show_interleave_mode(u32 reg)
808{
809 return interleave_mode(reg) ? "8:6" : "[8:6]XOR[18:16]";
810}
811
812static u32 dram_attr(u32 reg)
813{
814 return GET_BITFIELD(reg, 2, 3);
815}
816
Jim Snowd0cdf902015-12-03 10:48:54 +0100817static u64 knl_sad_limit(u32 reg)
818{
819 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
820}
821
822static u32 knl_interleave_mode(u32 reg)
823{
824 return GET_BITFIELD(reg, 1, 2);
825}
826
827static char *knl_show_interleave_mode(u32 reg)
828{
829 char *s;
830
831 switch (knl_interleave_mode(reg)) {
832 case 0:
833 s = "use address bits [8:6]";
834 break;
835 case 1:
836 s = "use address bits [10:8]";
837 break;
838 case 2:
839 s = "use address bits [14:12]";
840 break;
841 case 3:
842 s = "use address bits [32:30]";
843 break;
844 default:
845 WARN_ON(1);
846 break;
847 }
848
849 return s;
850}
851
852static u32 dram_attr_knl(u32 reg)
853{
854 return GET_BITFIELD(reg, 3, 4);
855}
856
857
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300858static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
859{
860 u32 reg;
861 enum mem_type mtype;
862
863 if (pvt->pci_ddrio) {
864 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
865 &reg);
866 if (GET_BITFIELD(reg, 11, 11))
867 /* FIXME: Can also be LRDIMM */
868 mtype = MEM_RDDR3;
869 else
870 mtype = MEM_DDR3;
871 } else
872 mtype = MEM_UNKNOWN;
873
874 return mtype;
875}
876
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300877static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
878{
879 u32 reg;
880 bool registered = false;
881 enum mem_type mtype = MEM_UNKNOWN;
882
883 if (!pvt->pci_ddrio)
884 goto out;
885
886 pci_read_config_dword(pvt->pci_ddrio,
887 HASWELL_DDRCRCLKCONTROLS, &reg);
888 /* Is_Rdimm */
889 if (GET_BITFIELD(reg, 16, 16))
890 registered = true;
891
892 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
893 if (GET_BITFIELD(reg, 14, 14)) {
894 if (registered)
895 mtype = MEM_RDDR4;
896 else
897 mtype = MEM_DDR4;
898 } else {
899 if (registered)
900 mtype = MEM_RDDR3;
901 else
902 mtype = MEM_DDR3;
903 }
904
905out:
906 return mtype;
907}
908
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +0100909static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
910{
911 /* for KNL value is fixed */
912 return DEV_X16;
913}
914
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400915static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
916{
917 /* there's no way to figure out */
918 return DEV_UNKNOWN;
919}
920
921static enum dev_type __ibridge_get_width(u32 mtr)
922{
923 enum dev_type type;
924
925 switch (mtr) {
926 case 3:
927 type = DEV_UNKNOWN;
928 break;
929 case 2:
930 type = DEV_X16;
931 break;
932 case 1:
933 type = DEV_X8;
934 break;
935 case 0:
936 type = DEV_X4;
937 break;
938 }
939
940 return type;
941}
942
943static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
944{
945 /*
946 * ddr3_width on the documentation but also valid for DDR4 on
947 * Haswell
948 */
949 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
950}
951
952static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
953{
954 /* ddr3_width on the documentation but also valid for DDR4 */
955 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
956}
957
Jim Snowd0cdf902015-12-03 10:48:54 +0100958static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
959{
960 /* DDR4 RDIMMS and LRDIMMS are supported */
961 return MEM_RDDR4;
962}
963
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300964static u8 get_node_id(struct sbridge_pvt *pvt)
965{
966 u32 reg;
967 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
968 return GET_BITFIELD(reg, 0, 2);
969}
970
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300971static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
972{
973 u32 reg;
974
975 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
976 return GET_BITFIELD(reg, 0, 3);
977}
978
Jim Snowd0cdf902015-12-03 10:48:54 +0100979static u8 knl_get_node_id(struct sbridge_pvt *pvt)
980{
981 u32 reg;
982
983 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
984 return GET_BITFIELD(reg, 0, 2);
985}
986
987
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300988static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
989{
990 u32 reg;
991
Tony Luckf7cf2a22014-10-29 10:36:50 -0700992 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
993 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300994}
995
996static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
997{
998 u64 rc;
999 u32 reg;
1000
1001 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
1002 rc = GET_BITFIELD(reg, 26, 31);
1003 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
1004 rc = ((reg << 6) | rc) << 26;
1005
1006 return rc | 0x1ffffff;
1007}
1008
Jim Snowd0cdf902015-12-03 10:48:54 +01001009static u64 knl_get_tolm(struct sbridge_pvt *pvt)
1010{
1011 u32 reg;
1012
1013 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
1014 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1015}
1016
1017static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1018{
1019 u64 rc;
1020 u32 reg_lo, reg_hi;
1021
1022 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
1023 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
1024 rc = ((u64)reg_hi << 32) | reg_lo;
1025 return rc | 0x3ffffff;
1026}
1027
1028
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001029static u64 haswell_rir_limit(u32 reg)
1030{
1031 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1032}
1033
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001034static inline u8 sad_pkg_socket(u8 pkg)
1035{
1036 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -03001037 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001038}
1039
1040static inline u8 sad_pkg_ha(u8 pkg)
1041{
1042 return (pkg >> 2) & 0x1;
1043}
1044
Tony Luckea5dfb52016-04-14 10:22:02 -07001045static int haswell_chan_hash(int idx, u64 addr)
1046{
1047 int i;
1048
1049 /*
1050 * XOR even bits from 12:26 to bit0 of idx,
1051 * odd bits from 13:27 to bit1
1052 */
1053 for (i = 12; i < 28; i += 2)
1054 idx ^= (addr >> i) & 3;
1055
1056 return idx;
1057}
1058
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001059/****************************************************************************
1060 Memory check routines
1061 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001062static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001063{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001064 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001065
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001066 do {
1067 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
1068 if (pdev && pdev->bus->number == bus)
1069 break;
1070 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001071
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001072 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001073}
1074
1075/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001076 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001077 * @bus: Device bus
1078 * @type: Memory controller type
1079 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
1080 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001081 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001082static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001083{
1084 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001085 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001086
Tony Luck1f395812014-12-02 09:27:30 -08001087 switch (type) {
1088 case IVY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001089 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001090 break;
1091 case HASWELL:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001092 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001093 break;
1094 case SANDY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001095 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001096 break;
1097 case BROADWELL:
1098 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
1099 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01001100 case KNIGHTS_LANDING:
1101 /*
1102 * KNL doesn't group things by bus the same way
1103 * SB/IB/Haswell does.
1104 */
1105 id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
1106 break;
Tony Luck1f395812014-12-02 09:27:30 -08001107 default:
1108 return -ENODEV;
1109 }
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001110
Jim Snowd0cdf902015-12-03 10:48:54 +01001111 if (type != KNIGHTS_LANDING)
1112 pdev = get_pdev_same_bus(bus, id);
1113 else
1114 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
1115
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001116 if (!pdev) {
1117 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001118 "%04x:%04x! on bus %02d\n",
1119 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001120 return -ENODEV;
1121 }
1122
Jim Snowd0cdf902015-12-03 10:48:54 +01001123 pci_read_config_dword(pdev,
1124 type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001125 if (!IS_ECC_ENABLED(mcmtr)) {
1126 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
1127 return -ENODEV;
1128 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001129 return 0;
1130}
1131
Jim Snowd0cdf902015-12-03 10:48:54 +01001132/* Low bits of TAD limit, and some metadata. */
1133static const u32 knl_tad_dram_limit_lo[] = {
1134 0x400, 0x500, 0x600, 0x700,
1135 0x800, 0x900, 0xa00, 0xb00,
1136};
1137
1138/* Low bits of TAD offset. */
1139static const u32 knl_tad_dram_offset_lo[] = {
1140 0x404, 0x504, 0x604, 0x704,
1141 0x804, 0x904, 0xa04, 0xb04,
1142};
1143
1144/* High 16 bits of TAD limit and offset. */
1145static const u32 knl_tad_dram_hi[] = {
1146 0x408, 0x508, 0x608, 0x708,
1147 0x808, 0x908, 0xa08, 0xb08,
1148};
1149
1150/* Number of ways a tad entry is interleaved. */
1151static const u32 knl_tad_ways[] = {
1152 8, 6, 4, 3, 2, 1,
1153};
1154
1155/*
1156 * Retrieve the n'th Target Address Decode table entry
1157 * from the memory controller's TAD table.
1158 *
1159 * @pvt: driver private data
1160 * @entry: which entry you want to retrieve
1161 * @mc: which memory controller (0 or 1)
1162 * @offset: output tad range offset
1163 * @limit: output address of first byte above tad range
1164 * @ways: output number of interleave ways
1165 *
1166 * The offset value has curious semantics. It's a sort of running total
1167 * of the sizes of all the memory regions that aren't mapped in this
1168 * tad table.
1169 */
1170static int knl_get_tad(const struct sbridge_pvt *pvt,
1171 const int entry,
1172 const int mc,
1173 u64 *offset,
1174 u64 *limit,
1175 int *ways)
1176{
1177 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1178 struct pci_dev *pci_mc;
1179 int way_id;
1180
1181 switch (mc) {
1182 case 0:
1183 pci_mc = pvt->knl.pci_mc0;
1184 break;
1185 case 1:
1186 pci_mc = pvt->knl.pci_mc1;
1187 break;
1188 default:
1189 WARN_ON(1);
1190 return -EINVAL;
1191 }
1192
1193 pci_read_config_dword(pci_mc,
1194 knl_tad_dram_limit_lo[entry], &reg_limit_lo);
1195 pci_read_config_dword(pci_mc,
1196 knl_tad_dram_offset_lo[entry], &reg_offset_lo);
1197 pci_read_config_dword(pci_mc,
1198 knl_tad_dram_hi[entry], &reg_hi);
1199
1200 /* Is this TAD entry enabled? */
1201 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1202 return -ENODEV;
1203
1204 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1205
1206 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1207 *ways = knl_tad_ways[way_id];
1208 } else {
1209 *ways = 0;
1210 sbridge_printk(KERN_ERR,
1211 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1212 way_id);
1213 return -ENODEV;
1214 }
1215
1216 /*
1217 * The least significant 6 bits of base and limit are truncated.
1218 * For limit, we fill the missing bits with 1s.
1219 */
1220 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1221 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1222 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1223 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1224
1225 return 0;
1226}
1227
1228/* Determine which memory controller is responsible for a given channel. */
1229static int knl_channel_mc(int channel)
1230{
1231 WARN_ON(channel < 0 || channel >= 6);
1232
1233 return channel < 3 ? 1 : 0;
1234}
1235
1236/*
1237 * Get the Nth entry from EDC_ROUTE_TABLE register.
1238 * (This is the per-tile mapping of logical interleave targets to
1239 * physical EDC modules.)
1240 *
1241 * entry 0: 0:2
1242 * 1: 3:5
1243 * 2: 6:8
1244 * 3: 9:11
1245 * 4: 12:14
1246 * 5: 15:17
1247 * 6: 18:20
1248 * 7: 21:23
1249 * reserved: 24:31
1250 */
1251static u32 knl_get_edc_route(int entry, u32 reg)
1252{
1253 WARN_ON(entry >= KNL_MAX_EDCS);
1254 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1255}
1256
1257/*
1258 * Get the Nth entry from MC_ROUTE_TABLE register.
1259 * (This is the per-tile mapping of logical interleave targets to
1260 * physical DRAM channels modules.)
1261 *
1262 * entry 0: mc 0:2 channel 18:19
1263 * 1: mc 3:5 channel 20:21
1264 * 2: mc 6:8 channel 22:23
1265 * 3: mc 9:11 channel 24:25
1266 * 4: mc 12:14 channel 26:27
1267 * 5: mc 15:17 channel 28:29
1268 * reserved: 30:31
1269 *
1270 * Though we have 3 bits to identify the MC, we should only see
1271 * the values 0 or 1.
1272 */
1273
1274static u32 knl_get_mc_route(int entry, u32 reg)
1275{
1276 int mc, chan;
1277
1278 WARN_ON(entry >= KNL_MAX_CHANNELS);
1279
1280 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1281 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1282
1283 return knl_channel_remap(mc*3 + chan);
1284}
1285
1286/*
1287 * Render the EDC_ROUTE register in human-readable form.
1288 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1289 */
1290static void knl_show_edc_route(u32 reg, char *s)
1291{
1292 int i;
1293
1294 for (i = 0; i < KNL_MAX_EDCS; i++) {
1295 s[i*2] = knl_get_edc_route(i, reg) + '0';
1296 s[i*2+1] = '-';
1297 }
1298
1299 s[KNL_MAX_EDCS*2 - 1] = '\0';
1300}
1301
1302/*
1303 * Render the MC_ROUTE register in human-readable form.
1304 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1305 */
1306static void knl_show_mc_route(u32 reg, char *s)
1307{
1308 int i;
1309
1310 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1311 s[i*2] = knl_get_mc_route(i, reg) + '0';
1312 s[i*2+1] = '-';
1313 }
1314
1315 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1316}
1317
1318#define KNL_EDC_ROUTE 0xb8
1319#define KNL_MC_ROUTE 0xb4
1320
1321/* Is this dram rule backed by regular DRAM in flat mode? */
1322#define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1323
1324/* Is this dram rule cached? */
1325#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1326
1327/* Is this rule backed by edc ? */
1328#define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1329
1330/* Is this rule backed by DRAM, cacheable in EDRAM? */
1331#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1332
1333/* Is this rule mod3? */
1334#define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1335
1336/*
1337 * Figure out how big our RAM modules are.
1338 *
1339 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1340 * have to figure this out from the SAD rules, interleave lists, route tables,
1341 * and TAD rules.
1342 *
1343 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1344 * inspect the TAD rules to figure out how large the SAD regions really are.
1345 *
1346 * When we know the real size of a SAD region and how many ways it's
1347 * interleaved, we know the individual contribution of each channel to
1348 * TAD is size/ways.
1349 *
1350 * Finally, we have to check whether each channel participates in each SAD
1351 * region.
1352 *
1353 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1354 * much memory the channel uses, we know the DIMM is at least that large.
1355 * (The BIOS might possibly choose not to map all available memory, in which
1356 * case we will underreport the size of the DIMM.)
1357 *
1358 * In theory, we could try to determine the EDC sizes as well, but that would
1359 * only work in flat mode, not in cache mode.
1360 *
1361 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1362 * elements)
1363 */
1364static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1365{
1366 u64 sad_base, sad_size, sad_limit = 0;
1367 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1368 int sad_rule = 0;
1369 int tad_rule = 0;
1370 int intrlv_ways, tad_ways;
1371 u32 first_pkg, pkg;
1372 int i;
1373 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1374 u32 dram_rule, interleave_reg;
1375 u32 mc_route_reg[KNL_MAX_CHAS];
1376 u32 edc_route_reg[KNL_MAX_CHAS];
1377 int edram_only;
1378 char edc_route_string[KNL_MAX_EDCS*2];
1379 char mc_route_string[KNL_MAX_CHANNELS*2];
1380 int cur_reg_start;
1381 int mc;
1382 int channel;
1383 int way;
1384 int participants[KNL_MAX_CHANNELS];
1385 int participant_count = 0;
1386
1387 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1388 mc_sizes[i] = 0;
1389
1390 /* Read the EDC route table in each CHA. */
1391 cur_reg_start = 0;
1392 for (i = 0; i < KNL_MAX_CHAS; i++) {
1393 pci_read_config_dword(pvt->knl.pci_cha[i],
1394 KNL_EDC_ROUTE, &edc_route_reg[i]);
1395
1396 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1397 knl_show_edc_route(edc_route_reg[i-1],
1398 edc_route_string);
1399 if (cur_reg_start == i-1)
1400 edac_dbg(0, "edc route table for CHA %d: %s\n",
1401 cur_reg_start, edc_route_string);
1402 else
1403 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1404 cur_reg_start, i-1, edc_route_string);
1405 cur_reg_start = i;
1406 }
1407 }
1408 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1409 if (cur_reg_start == i-1)
1410 edac_dbg(0, "edc route table for CHA %d: %s\n",
1411 cur_reg_start, edc_route_string);
1412 else
1413 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1414 cur_reg_start, i-1, edc_route_string);
1415
1416 /* Read the MC route table in each CHA. */
1417 cur_reg_start = 0;
1418 for (i = 0; i < KNL_MAX_CHAS; i++) {
1419 pci_read_config_dword(pvt->knl.pci_cha[i],
1420 KNL_MC_ROUTE, &mc_route_reg[i]);
1421
1422 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1423 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1424 if (cur_reg_start == i-1)
1425 edac_dbg(0, "mc route table for CHA %d: %s\n",
1426 cur_reg_start, mc_route_string);
1427 else
1428 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1429 cur_reg_start, i-1, mc_route_string);
1430 cur_reg_start = i;
1431 }
1432 }
1433 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1434 if (cur_reg_start == i-1)
1435 edac_dbg(0, "mc route table for CHA %d: %s\n",
1436 cur_reg_start, mc_route_string);
1437 else
1438 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1439 cur_reg_start, i-1, mc_route_string);
1440
1441 /* Process DRAM rules */
1442 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1443 /* previous limit becomes the new base */
1444 sad_base = sad_limit;
1445
1446 pci_read_config_dword(pvt->pci_sad0,
1447 pvt->info.dram_rule[sad_rule], &dram_rule);
1448
1449 if (!DRAM_RULE_ENABLE(dram_rule))
1450 break;
1451
1452 edram_only = KNL_EDRAM_ONLY(dram_rule);
1453
1454 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1455 sad_size = sad_limit - sad_base;
1456
1457 pci_read_config_dword(pvt->pci_sad0,
1458 pvt->info.interleave_list[sad_rule], &interleave_reg);
1459
1460 /*
1461 * Find out how many ways this dram rule is interleaved.
1462 * We stop when we see the first channel again.
1463 */
1464 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1465 interleave_reg, 0);
1466 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1467 pkg = sad_pkg(pvt->info.interleave_pkg,
1468 interleave_reg, intrlv_ways);
1469
1470 if ((pkg & 0x8) == 0) {
1471 /*
1472 * 0 bit means memory is non-local,
1473 * which KNL doesn't support
1474 */
1475 edac_dbg(0, "Unexpected interleave target %d\n",
1476 pkg);
1477 return -1;
1478 }
1479
1480 if (pkg == first_pkg)
1481 break;
1482 }
1483 if (KNL_MOD3(dram_rule))
1484 intrlv_ways *= 3;
1485
1486 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1487 sad_rule,
1488 sad_base,
1489 sad_limit,
1490 intrlv_ways,
1491 edram_only ? ", EDRAM" : "");
1492
1493 /*
1494 * Find out how big the SAD region really is by iterating
1495 * over TAD tables (SAD regions may contain holes).
1496 * Each memory controller might have a different TAD table, so
1497 * we have to look at both.
1498 *
1499 * Livespace is the memory that's mapped in this TAD table,
1500 * deadspace is the holes (this could be the MMIO hole, or it
1501 * could be memory that's mapped by the other TAD table but
1502 * not this one).
1503 */
1504 for (mc = 0; mc < 2; mc++) {
1505 sad_actual_size[mc] = 0;
1506 tad_livespace = 0;
1507 for (tad_rule = 0;
1508 tad_rule < ARRAY_SIZE(
1509 knl_tad_dram_limit_lo);
1510 tad_rule++) {
1511 if (knl_get_tad(pvt,
1512 tad_rule,
1513 mc,
1514 &tad_deadspace,
1515 &tad_limit,
1516 &tad_ways))
1517 break;
1518
1519 tad_size = (tad_limit+1) -
1520 (tad_livespace + tad_deadspace);
1521 tad_livespace += tad_size;
1522 tad_base = (tad_limit+1) - tad_size;
1523
1524 if (tad_base < sad_base) {
1525 if (tad_limit > sad_base)
1526 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1527 } else if (tad_base < sad_limit) {
1528 if (tad_limit+1 > sad_limit) {
1529 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1530 } else {
1531 /* TAD region is completely inside SAD region */
1532 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1533 tad_rule, tad_base,
1534 tad_limit, tad_size,
1535 mc);
1536 sad_actual_size[mc] += tad_size;
1537 }
1538 }
1539 tad_base = tad_limit+1;
1540 }
1541 }
1542
1543 for (mc = 0; mc < 2; mc++) {
1544 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1545 mc, sad_actual_size[mc], sad_actual_size[mc]);
1546 }
1547
1548 /* Ignore EDRAM rule */
1549 if (edram_only)
1550 continue;
1551
1552 /* Figure out which channels participate in interleave. */
1553 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1554 participants[channel] = 0;
1555
1556 /* For each channel, does at least one CHA have
1557 * this channel mapped to the given target?
1558 */
1559 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1560 for (way = 0; way < intrlv_ways; way++) {
1561 int target;
1562 int cha;
1563
1564 if (KNL_MOD3(dram_rule))
1565 target = way;
1566 else
1567 target = 0x7 & sad_pkg(
1568 pvt->info.interleave_pkg, interleave_reg, way);
1569
1570 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1571 if (knl_get_mc_route(target,
1572 mc_route_reg[cha]) == channel
Hubert Chrzaniuk83bdaad2016-03-07 15:30:45 +01001573 && !participants[channel]) {
Jim Snowd0cdf902015-12-03 10:48:54 +01001574 participant_count++;
1575 participants[channel] = 1;
1576 break;
1577 }
1578 }
1579 }
1580 }
1581
1582 if (participant_count != intrlv_ways)
1583 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1584 participant_count, intrlv_ways);
1585
1586 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1587 mc = knl_channel_mc(channel);
1588 if (participants[channel]) {
1589 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1590 channel,
1591 sad_actual_size[mc]/intrlv_ways,
1592 sad_rule);
1593 mc_sizes[channel] +=
1594 sad_actual_size[mc]/intrlv_ways;
1595 }
1596 }
1597 }
1598
1599 return 0;
1600}
1601
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001602static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001603{
1604 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001605 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001606 unsigned i, j, banks, ranks, rows, cols, npages;
1607 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001608 u32 reg;
1609 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -02001610 enum mem_type mtype;
Jim Snowd0cdf902015-12-03 10:48:54 +01001611 int channels = pvt->info.type == KNIGHTS_LANDING ?
1612 KNL_MAX_CHANNELS : NUM_CHANNELS;
1613 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001614
Tony Luckea5dfb52016-04-14 10:22:02 -07001615 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1616 pci_read_config_dword(pvt->pci_ha0, HASWELL_HASYSDEFEATURE2, &reg);
1617 pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
1618 }
Jim Snowd0cdf902015-12-03 10:48:54 +01001619 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1620 pvt->info.type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001621 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
1622 else
1623 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
1624
Jim Snowd0cdf902015-12-03 10:48:54 +01001625 if (pvt->info.type == KNIGHTS_LANDING)
1626 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1627 else
1628 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001629
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03001630 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba12012-04-29 17:08:39 -03001631 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1632 pvt->sbridge_dev->mc,
1633 pvt->sbridge_dev->node_id,
1634 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001635
Jim Snowd0cdf902015-12-03 10:48:54 +01001636 /* KNL doesn't support mirroring or lockstep,
1637 * and is always closed page
1638 */
1639 if (pvt->info.type == KNIGHTS_LANDING) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001640 mode = EDAC_S4ECD4ED;
Jim Snowd0cdf902015-12-03 10:48:54 +01001641 pvt->is_mirrored = false;
1642
1643 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1644 return -1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001645 } else {
Jim Snowd0cdf902015-12-03 10:48:54 +01001646 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
1647 if (IS_MIRROR_ENABLED(reg)) {
1648 edac_dbg(0, "Memory mirror is enabled\n");
1649 pvt->is_mirrored = true;
1650 } else {
1651 edac_dbg(0, "Memory mirror is disabled\n");
1652 pvt->is_mirrored = false;
1653 }
1654
1655 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
1656 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1657 edac_dbg(0, "Lockstep is enabled\n");
1658 mode = EDAC_S8ECD8ED;
1659 pvt->is_lockstep = true;
1660 } else {
1661 edac_dbg(0, "Lockstep is disabled\n");
1662 mode = EDAC_S4ECD4ED;
1663 pvt->is_lockstep = false;
1664 }
1665 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1666 edac_dbg(0, "address map is on closed page mode\n");
1667 pvt->is_close_pg = true;
1668 } else {
1669 edac_dbg(0, "address map is on open page mode\n");
1670 pvt->is_close_pg = false;
1671 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001672 }
1673
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001674 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001675 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001676 edac_dbg(0, "Memory is registered\n");
1677 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -07001678 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001679 else
1680 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001681
Tony Luckfec53af2014-12-02 09:41:58 -08001682 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001683 banks = 16;
1684 else
1685 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001686
Jim Snowd0cdf902015-12-03 10:48:54 +01001687 for (i = 0; i < channels; i++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001688 u32 mtr;
1689
Jim Snowd0cdf902015-12-03 10:48:54 +01001690 int max_dimms_per_channel;
1691
1692 if (pvt->info.type == KNIGHTS_LANDING) {
1693 max_dimms_per_channel = 1;
1694 if (!pvt->knl.pci_channel[i])
1695 continue;
1696 } else {
1697 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1698 if (!pvt->pci_tad[i])
1699 continue;
1700 }
1701
1702 for (j = 0; j < max_dimms_per_channel; j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001703 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1704 i, j, 0);
Jim Snowd0cdf902015-12-03 10:48:54 +01001705 if (pvt->info.type == KNIGHTS_LANDING) {
1706 pci_read_config_dword(pvt->knl.pci_channel[i],
1707 knl_mtr_reg, &mtr);
1708 } else {
1709 pci_read_config_dword(pvt->pci_tad[i],
1710 mtr_regs[j], &mtr);
1711 }
Joe Perches956b9ba12012-04-29 17:08:39 -03001712 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001713 if (IS_DIMM_PRESENT(mtr)) {
1714 pvt->channel[i].dimms++;
1715
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001716 ranks = numrank(pvt->info.type, mtr);
Jim Snowd0cdf902015-12-03 10:48:54 +01001717
1718 if (pvt->info.type == KNIGHTS_LANDING) {
1719 /* For DDR4, this is fixed. */
1720 cols = 1 << 10;
1721 rows = knl_mc_sizes[i] /
1722 ((u64) cols * ranks * banks * 8);
1723 } else {
1724 rows = numrow(mtr);
1725 cols = numcol(mtr);
1726 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001727
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001728 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001729 npages = MiB_TO_PAGES(size);
1730
Tony Luck7d375bf2015-05-18 17:50:42 -03001731 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1732 pvt->sbridge_dev->mc, i/4, i%4, j,
Joe Perches956b9ba12012-04-29 17:08:39 -03001733 size, npages,
1734 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001735
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -03001736 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001737 dimm->grain = 32;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04001738 dimm->dtype = pvt->info.get_width(pvt, mtr);
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001739 dimm->mtype = mtype;
1740 dimm->edac_mode = mode;
1741 snprintf(dimm->label, sizeof(dimm->label),
Tony Luck7d375bf2015-05-18 17:50:42 -03001742 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1743 pvt->sbridge_dev->source_id, i/4, i%4, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001744 }
1745 }
1746 }
1747
1748 return 0;
1749}
1750
1751static void get_memory_layout(const struct mem_ctl_info *mci)
1752{
1753 struct sbridge_pvt *pvt = mci->pvt_info;
1754 int i, j, k, n_sads, n_tads, sad_interl;
1755 u32 reg;
1756 u64 limit, prv = 0;
1757 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +01001758 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001759 u32 rir_way;
1760
1761 /*
1762 * Step 1) Get TOLM/TOHM ranges
1763 */
1764
Aristeu Rozanskifb79a502013-10-30 13:26:57 -03001765 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001766 tmp_mb = (1 + pvt->tolm) >> 20;
1767
Jim Snow8c009102014-11-18 14:51:09 +01001768 gb = div_u64_rem(tmp_mb, 1024, &mb);
1769 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1770 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001771
1772 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -03001773 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001774 tmp_mb = (1 + pvt->tohm) >> 20;
1775
Jim Snow8c009102014-11-18 14:51:09 +01001776 gb = div_u64_rem(tmp_mb, 1024, &mb);
1777 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1778 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001779
1780 /*
1781 * Step 2) Get SAD range and SAD Interleave list
1782 * TAD registers contain the interleave wayness. However, it
1783 * seems simpler to just discover it indirectly, with the
1784 * algorithm bellow.
1785 */
1786 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001787 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001788 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001789 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001790 &reg);
Jim Snowc59f9c02015-12-03 10:48:52 +01001791 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001792
1793 if (!DRAM_RULE_ENABLE(reg))
1794 continue;
1795
1796 if (limit <= prv)
1797 break;
1798
1799 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001800 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001801 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1802 n_sads,
Jim Snowc59f9c02015-12-03 10:48:52 +01001803 show_dram_attr(pvt->info.dram_attr(reg)),
Jim Snow8c009102014-11-18 14:51:09 +01001804 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001805 ((u64)tmp_mb) << 20L,
Jim Snowc59f9c02015-12-03 10:48:52 +01001806 pvt->info.show_interleave_mode(reg),
Joe Perches956b9ba12012-04-29 17:08:39 -03001807 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001808 prv = limit;
1809
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001810 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001811 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001812 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001813 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001814 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1815 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001816 break;
1817
Joe Perches956b9ba12012-04-29 17:08:39 -03001818 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001819 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001820 }
1821 }
1822
Jim Snowd0cdf902015-12-03 10:48:54 +01001823 if (pvt->info.type == KNIGHTS_LANDING)
1824 return;
1825
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001826 /*
1827 * Step 3) Get TAD range
1828 */
1829 prv = 0;
1830 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1831 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1832 &reg);
1833 limit = TAD_LIMIT(reg);
1834 if (limit <= prv)
1835 break;
1836 tmp_mb = (limit + 1) >> 20;
1837
Jim Snow8c009102014-11-18 14:51:09 +01001838 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001839 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 +01001840 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001841 ((u64)tmp_mb) << 20L,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08001842 (u32)(1 << TAD_SOCK(reg)),
1843 (u32)TAD_CH(reg) + 1,
Joe Perches956b9ba12012-04-29 17:08:39 -03001844 (u32)TAD_TGT0(reg),
1845 (u32)TAD_TGT1(reg),
1846 (u32)TAD_TGT2(reg),
1847 (u32)TAD_TGT3(reg),
1848 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001849 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001850 }
1851
1852 /*
1853 * Step 4) Get TAD offsets, per each channel
1854 */
1855 for (i = 0; i < NUM_CHANNELS; i++) {
1856 if (!pvt->channel[i].dimms)
1857 continue;
1858 for (j = 0; j < n_tads; j++) {
1859 pci_read_config_dword(pvt->pci_tad[i],
1860 tad_ch_nilv_offset[j],
1861 &reg);
1862 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001863 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001864 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1865 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001866 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001867 ((u64)tmp_mb) << 20L,
1868 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001869 }
1870 }
1871
1872 /*
1873 * Step 6) Get RIR Wayness/Limit, per each channel
1874 */
1875 for (i = 0; i < NUM_CHANNELS; i++) {
1876 if (!pvt->channel[i].dimms)
1877 continue;
1878 for (j = 0; j < MAX_RIR_RANGES; j++) {
1879 pci_read_config_dword(pvt->pci_tad[i],
1880 rir_way_limit[j],
1881 &reg);
1882
1883 if (!IS_RIR_VALID(reg))
1884 continue;
1885
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001886 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001887 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001888 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001889 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1890 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001891 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001892 ((u64)tmp_mb) << 20L,
1893 rir_way,
1894 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001895
1896 for (k = 0; k < rir_way; k++) {
1897 pci_read_config_dword(pvt->pci_tad[i],
1898 rir_offset[j][k],
1899 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07001900 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001901
Jim Snow8c009102014-11-18 14:51:09 +01001902 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001903 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1904 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001905 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001906 ((u64)tmp_mb) << 20L,
Tony Luckc7103f62016-05-31 11:50:28 -07001907 (u32)RIR_RNK_TGT(pvt->info.type, reg),
Joe Perches956b9ba12012-04-29 17:08:39 -03001908 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001909 }
1910 }
1911 }
1912}
1913
Rashika Kheria8112c0c2013-12-14 19:32:09 +05301914static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001915{
1916 struct sbridge_dev *sbridge_dev;
1917
1918 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1919 if (sbridge_dev->node_id == node_id)
1920 return sbridge_dev->mci;
1921 }
1922 return NULL;
1923}
1924
1925static int get_memory_error_data(struct mem_ctl_info *mci,
1926 u64 addr,
Tony Luck7d375bf2015-05-18 17:50:42 -03001927 u8 *socket, u8 *ha,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001928 long *channel_mask,
1929 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001930 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001931{
1932 struct mem_ctl_info *new_mci;
1933 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001934 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001935 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001936 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001937 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001938 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001939 u32 reg, dram_rule;
Tony Luck7d375bf2015-05-18 17:50:42 -03001940 u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001941 u32 tad_offset;
1942 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001943 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001944 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001945
1946
1947 /*
1948 * Step 0) Check if the address is at special memory ranges
1949 * The check bellow is probably enough to fill all cases where
1950 * the error is not inside a memory, except for the legacy
1951 * range (e. g. VGA addresses). It is unlikely, however, that the
1952 * memory controller would generate an error on that range.
1953 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001954 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001955 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001956 return -EINVAL;
1957 }
1958 if (addr >= (u64)pvt->tohm) {
1959 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001960 return -EINVAL;
1961 }
1962
1963 /*
1964 * Step 1) Get socket
1965 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001966 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1967 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001968 &reg);
1969
1970 if (!DRAM_RULE_ENABLE(reg))
1971 continue;
1972
Jim Snowc59f9c02015-12-03 10:48:52 +01001973 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001974 if (limit <= prv) {
1975 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001976 return -EINVAL;
1977 }
1978 if (addr <= limit)
1979 break;
1980 prv = limit;
1981 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001982 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001983 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001984 return -EINVAL;
1985 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001986 dram_rule = reg;
Jim Snowc59f9c02015-12-03 10:48:52 +01001987 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1988 interleave_mode = pvt->info.interleave_mode(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001989
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001990 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001991 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001992
1993 if (pvt->info.type == SANDY_BRIDGE) {
1994 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1995 for (sad_way = 0; sad_way < 8; sad_way++) {
1996 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1997 if (sad_way > 0 && sad_interl == pkg)
1998 break;
1999 sad_interleave[sad_way] = pkg;
2000 edac_dbg(0, "SAD interleave #%d: %d\n",
2001 sad_way, sad_interleave[sad_way]);
2002 }
2003 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
2004 pvt->sbridge_dev->mc,
2005 n_sads,
2006 addr,
2007 limit,
2008 sad_way + 7,
2009 !interleave_mode ? "" : "XOR[18:16]");
2010 if (interleave_mode)
2011 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
2012 else
2013 idx = (addr >> 6) & 7;
2014 switch (sad_way) {
2015 case 1:
2016 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002017 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002018 case 2:
2019 idx = idx & 1;
2020 break;
2021 case 4:
2022 idx = idx & 3;
2023 break;
2024 case 8:
2025 break;
2026 default:
2027 sprintf(msg, "Can't discover socket interleave");
2028 return -EINVAL;
2029 }
2030 *socket = sad_interleave[idx];
2031 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2032 idx, sad_way, *socket);
Tony Luck1f395812014-12-02 09:27:30 -08002033 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002034 int bits, a7mode = A7MODE(dram_rule);
2035
2036 if (a7mode) {
2037 /* A7 mode swaps P9 with P6 */
2038 bits = GET_BITFIELD(addr, 7, 8) << 1;
2039 bits |= GET_BITFIELD(addr, 9, 9);
2040 } else
Tony Luckbb89e712015-05-18 17:39:06 -03002041 bits = GET_BITFIELD(addr, 6, 8);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002042
Tony Luckbb89e712015-05-18 17:39:06 -03002043 if (interleave_mode == 0) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002044 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2045 idx = GET_BITFIELD(addr, 16, 18);
2046 idx ^= bits;
2047 } else
2048 idx = bits;
2049
2050 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2051 *socket = sad_pkg_socket(pkg);
2052 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002053 if (sad_ha)
2054 ch_add = 4;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002055
2056 if (a7mode) {
2057 /* MCChanShiftUpEnable */
2058 pci_read_config_dword(pvt->pci_ha0,
2059 HASWELL_HASYSDEFEATURE2, &reg);
2060 shiftup = GET_BITFIELD(reg, 22, 22);
2061 }
2062
2063 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2064 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002065 } else {
2066 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002067 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002068 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2069 *socket = sad_pkg_socket(pkg);
2070 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002071 if (sad_ha)
2072 ch_add = 4;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002073 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2074 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002075 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002076
Tony Luck7d375bf2015-05-18 17:50:42 -03002077 *ha = sad_ha;
2078
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002079 /*
2080 * Move to the proper node structure, in order to access the
2081 * right PCI registers
2082 */
2083 new_mci = get_mci_for_node_id(*socket);
2084 if (!new_mci) {
2085 sprintf(msg, "Struct for socket #%u wasn't initialized",
2086 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002087 return -EINVAL;
2088 }
2089 mci = new_mci;
2090 pvt = mci->pvt_info;
2091
2092 /*
2093 * Step 2) Get memory channel
2094 */
2095 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002096 if (pvt->info.type == SANDY_BRIDGE)
2097 pci_ha = pvt->pci_ha0;
2098 else {
2099 if (sad_ha)
2100 pci_ha = pvt->pci_ha1;
2101 else
2102 pci_ha = pvt->pci_ha0;
2103 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002104 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002105 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002106 limit = TAD_LIMIT(reg);
2107 if (limit <= prv) {
2108 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002109 return -EINVAL;
2110 }
2111 if (addr <= limit)
2112 break;
2113 prv = limit;
2114 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002115 if (n_tads == MAX_TAD) {
2116 sprintf(msg, "Can't discover the memory channel");
2117 return -EINVAL;
2118 }
2119
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002120 ch_way = TAD_CH(reg) + 1;
Tony Luckff15e952016-04-14 10:21:52 -07002121 sck_way = TAD_SOCK(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002122
2123 if (ch_way == 3)
2124 idx = addr >> 6;
Tony Luckea5dfb52016-04-14 10:22:02 -07002125 else {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002126 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Tony Luckea5dfb52016-04-14 10:22:02 -07002127 if (pvt->is_chan_hash)
2128 idx = haswell_chan_hash(idx, addr);
2129 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002130 idx = idx % ch_way;
2131
2132 /*
2133 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2134 */
2135 switch (idx) {
2136 case 0:
2137 base_ch = TAD_TGT0(reg);
2138 break;
2139 case 1:
2140 base_ch = TAD_TGT1(reg);
2141 break;
2142 case 2:
2143 base_ch = TAD_TGT2(reg);
2144 break;
2145 case 3:
2146 base_ch = TAD_TGT3(reg);
2147 break;
2148 default:
2149 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002150 return -EINVAL;
2151 }
2152 *channel_mask = 1 << base_ch;
2153
Tony Luck7d375bf2015-05-18 17:50:42 -03002154 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002155 tad_ch_nilv_offset[n_tads],
2156 &tad_offset);
2157
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002158 if (pvt->is_mirrored) {
2159 *channel_mask |= 1 << ((base_ch + 2) % 4);
2160 switch(ch_way) {
2161 case 2:
2162 case 4:
Tony Luckff15e952016-04-14 10:21:52 -07002163 sck_xch = (1 << sck_way) * (ch_way >> 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002164 break;
2165 default:
2166 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002167 return -EINVAL;
2168 }
2169 } else
2170 sck_xch = (1 << sck_way) * ch_way;
2171
2172 if (pvt->is_lockstep)
2173 *channel_mask |= 1 << ((base_ch + 1) % 4);
2174
2175 offset = TAD_OFFSET(tad_offset);
2176
Joe Perches956b9ba12012-04-29 17:08:39 -03002177 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",
2178 n_tads,
2179 addr,
2180 limit,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002181 sck_way,
Joe Perches956b9ba12012-04-29 17:08:39 -03002182 ch_way,
2183 offset,
2184 idx,
2185 base_ch,
2186 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002187
2188 /* Calculate channel address */
2189 /* Remove the TAD offset */
2190
2191 if (offset > addr) {
2192 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2193 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002194 return -EINVAL;
2195 }
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002196
2197 ch_addr = addr - offset;
2198 ch_addr >>= (6 + shiftup);
Tony Luckff15e952016-04-14 10:21:52 -07002199 ch_addr /= sck_xch;
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002200 ch_addr <<= (6 + shiftup);
2201 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002202
2203 /*
2204 * Step 3) Decode rank
2205 */
2206 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
Tony Luck7d375bf2015-05-18 17:50:42 -03002207 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002208 rir_way_limit[n_rir],
2209 &reg);
2210
2211 if (!IS_RIR_VALID(reg))
2212 continue;
2213
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002214 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01002215 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03002216 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2217 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01002218 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03002219 limit,
2220 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002221 if (ch_addr <= limit)
2222 break;
2223 }
2224 if (n_rir == MAX_RIR_RANGES) {
2225 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2226 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002227 return -EINVAL;
2228 }
2229 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002230
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002231 if (pvt->is_close_pg)
2232 idx = (ch_addr >> 6);
2233 else
2234 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2235 idx %= 1 << rir_way;
2236
Tony Luck7d375bf2015-05-18 17:50:42 -03002237 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002238 rir_offset[n_rir][idx],
2239 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07002240 *rank = RIR_RNK_TGT(pvt->info.type, reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002241
Joe Perches956b9ba12012-04-29 17:08:39 -03002242 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2243 n_rir,
2244 ch_addr,
2245 limit,
2246 rir_way,
2247 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002248
2249 return 0;
2250}
2251
2252/****************************************************************************
2253 Device initialization routines: put/get, init/exit
2254 ****************************************************************************/
2255
2256/*
2257 * sbridge_put_all_devices 'put' all the devices that we have
2258 * reserved via 'get'
2259 */
2260static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2261{
2262 int i;
2263
Joe Perches956b9ba12012-04-29 17:08:39 -03002264 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002265 for (i = 0; i < sbridge_dev->n_devs; i++) {
2266 struct pci_dev *pdev = sbridge_dev->pdev[i];
2267 if (!pdev)
2268 continue;
Joe Perches956b9ba12012-04-29 17:08:39 -03002269 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2270 pdev->bus->number,
2271 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002272 pci_dev_put(pdev);
2273 }
2274}
2275
2276static void sbridge_put_all_devices(void)
2277{
2278 struct sbridge_dev *sbridge_dev, *tmp;
2279
2280 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2281 sbridge_put_devices(sbridge_dev);
2282 free_sbridge_dev(sbridge_dev);
2283 }
2284}
2285
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002286static int sbridge_get_onedevice(struct pci_dev **prev,
2287 u8 *num_mc,
2288 const struct pci_id_table *table,
Jim Snowc1979ba2015-12-03 10:48:53 +01002289 const unsigned devno,
2290 const int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002291{
2292 struct sbridge_dev *sbridge_dev;
2293 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002294 struct pci_dev *pdev = NULL;
2295 u8 bus = 0;
2296
Jiang Liuec5a0b32014-02-17 13:10:23 +08002297 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002298 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002299 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2300
2301 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2302 dev_descr->dev_id, *prev);
2303
2304 if (!pdev) {
2305 if (*prev) {
2306 *prev = pdev;
2307 return 0;
2308 }
2309
2310 if (dev_descr->optional)
2311 return 0;
2312
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002313 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002314 if (devno == 0)
2315 return -ENODEV;
2316
2317 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002318 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002319 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2320
2321 /* End of list, leave */
2322 return -ENODEV;
2323 }
2324 bus = pdev->bus->number;
2325
Jim Snowc1979ba2015-12-03 10:48:53 +01002326 sbridge_dev = get_sbridge_dev(bus, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002327 if (!sbridge_dev) {
2328 sbridge_dev = alloc_sbridge_dev(bus, table);
2329 if (!sbridge_dev) {
2330 pci_dev_put(pdev);
2331 return -ENOMEM;
2332 }
2333 (*num_mc)++;
2334 }
2335
2336 if (sbridge_dev->pdev[devno]) {
2337 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002338 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002339 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2340 pci_dev_put(pdev);
2341 return -ENODEV;
2342 }
2343
2344 sbridge_dev->pdev[devno] = pdev;
2345
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002346 /* Be sure that the device is enabled */
2347 if (unlikely(pci_enable_device(pdev) < 0)) {
2348 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002349 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002350 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2351 return -ENODEV;
2352 }
2353
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002354 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba12012-04-29 17:08:39 -03002355 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002356
2357 /*
2358 * As stated on drivers/pci/search.c, the reference count for
2359 * @from is always decremented if it is not %NULL. So, as we need
2360 * to get all devices up to null, we need to do a get for the device
2361 */
2362 pci_dev_get(pdev);
2363
2364 *prev = pdev;
2365
2366 return 0;
2367}
2368
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002369/*
2370 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002371 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002372 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03002373 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002374 * @table: model specific table
Jim Snowc1979ba2015-12-03 10:48:53 +01002375 * @allow_dups: allow for multiple devices to exist with the same device id
2376 * (as implemented, this isn't expected to work correctly in the
2377 * multi-socket case).
2378 * @multi_bus: don't assume devices on different buses belong to different
2379 * memory controllers.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002380 *
2381 * returns 0 in case of success or error code
2382 */
Jim Snowc1979ba2015-12-03 10:48:53 +01002383static int sbridge_get_all_devices_full(u8 *num_mc,
2384 const struct pci_id_table *table,
2385 int allow_dups,
2386 int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002387{
2388 int i, rc;
2389 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002390
2391 while (table && table->descr) {
2392 for (i = 0; i < table->n_devs; i++) {
Jim Snowc1979ba2015-12-03 10:48:53 +01002393 if (!allow_dups || i == 0 ||
2394 table->descr[i].dev_id !=
2395 table->descr[i-1].dev_id) {
2396 pdev = NULL;
2397 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002398 do {
2399 rc = sbridge_get_onedevice(&pdev, num_mc,
Jim Snowc1979ba2015-12-03 10:48:53 +01002400 table, i, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002401 if (rc < 0) {
2402 if (i == 0) {
2403 i = table->n_devs;
2404 break;
2405 }
2406 sbridge_put_all_devices();
2407 return -ENODEV;
2408 }
Jim Snowc1979ba2015-12-03 10:48:53 +01002409 } while (pdev && !allow_dups);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002410 }
2411 table++;
2412 }
2413
2414 return 0;
2415}
2416
Jim Snowc1979ba2015-12-03 10:48:53 +01002417#define sbridge_get_all_devices(num_mc, table) \
2418 sbridge_get_all_devices_full(num_mc, table, 0, 0)
Jim Snowd0cdf902015-12-03 10:48:54 +01002419#define sbridge_get_all_devices_knl(num_mc, table) \
2420 sbridge_get_all_devices_full(num_mc, table, 1, 1)
Jim Snowc1979ba2015-12-03 10:48:53 +01002421
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03002422static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2423 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002424{
2425 struct sbridge_pvt *pvt = mci->pvt_info;
2426 struct pci_dev *pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002427 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002428 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002429
2430 for (i = 0; i < sbridge_dev->n_devs; i++) {
2431 pdev = sbridge_dev->pdev[i];
2432 if (!pdev)
2433 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002434
2435 switch (pdev->device) {
2436 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2437 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002438 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002439 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2440 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002441 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002442 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2443 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002444 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002445 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2446 pvt->pci_ha0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002447 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002448 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2449 pvt->pci_ta = pdev;
2450 break;
2451 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2452 pvt->pci_ras = pdev;
2453 break;
2454 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2455 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2456 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2457 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2458 {
2459 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
2460 pvt->pci_tad[id] = pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002461 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002462 }
2463 break;
2464 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2465 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002466 break;
2467 default:
2468 goto error;
2469 }
2470
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002471 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2472 pdev->vendor, pdev->device,
Joe Perches956b9ba12012-04-29 17:08:39 -03002473 sbridge_dev->bus,
Joe Perches956b9ba12012-04-29 17:08:39 -03002474 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002475 }
2476
2477 /* Check if everything were registered */
2478 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Luck, Tonyde4772c2013-03-28 09:59:15 -07002479 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002480 goto enodev;
2481
Seth Jennings2900ea62015-08-05 13:16:01 -05002482 if (saw_chan_mask != 0x0f)
2483 goto enodev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002484 return 0;
2485
2486enodev:
2487 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2488 return -ENODEV;
2489
2490error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002491 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2492 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002493 return -EINVAL;
2494}
2495
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002496static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2497 struct sbridge_dev *sbridge_dev)
2498{
2499 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002500 struct pci_dev *pdev;
2501 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002502 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002503
2504 for (i = 0; i < sbridge_dev->n_devs; i++) {
2505 pdev = sbridge_dev->pdev[i];
2506 if (!pdev)
2507 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002508
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002509 switch (pdev->device) {
2510 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
2511 pvt->pci_ha0 = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002512 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002513 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2514 pvt->pci_ta = pdev;
2515 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
2516 pvt->pci_ras = pdev;
2517 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002518 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2519 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002520 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2521 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002522 {
2523 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
2524 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002525 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002526 }
2527 break;
2528 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2529 pvt->pci_ddrio = pdev;
2530 break;
2531 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
Tony Luck7d375bf2015-05-18 17:50:42 -03002532 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002533 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002534 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2535 pvt->pci_sad0 = pdev;
2536 break;
2537 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2538 pvt->pci_br0 = pdev;
2539 break;
2540 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2541 pvt->pci_br1 = pdev;
2542 break;
2543 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
2544 pvt->pci_ha1 = pdev;
2545 break;
2546 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2547 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002548 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2549 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002550 {
Tony Luck7d375bf2015-05-18 17:50:42 -03002551 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002552 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002553 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002554 }
2555 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002556 default:
2557 goto error;
2558 }
2559
2560 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2561 sbridge_dev->bus,
2562 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2563 pdev);
2564 }
2565
2566 /* Check if everything were registered */
2567 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
2568 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
2569 !pvt->pci_ta)
2570 goto enodev;
2571
Tony Luck7d375bf2015-05-18 17:50:42 -03002572 if (saw_chan_mask != 0x0f && /* -EN */
2573 saw_chan_mask != 0x33 && /* -EP */
2574 saw_chan_mask != 0xff) /* -EX */
2575 goto enodev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002576 return 0;
2577
2578enodev:
2579 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2580 return -ENODEV;
2581
2582error:
2583 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002584 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2585 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002586 return -EINVAL;
2587}
2588
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002589static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2590 struct sbridge_dev *sbridge_dev)
2591{
2592 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002593 struct pci_dev *pdev;
2594 u8 saw_chan_mask = 0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002595 int i;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002596
2597 /* there's only one device per system; not tied to any bus */
2598 if (pvt->info.pci_vtd == NULL)
2599 /* result will be checked later */
2600 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2601 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2602 NULL);
2603
2604 for (i = 0; i < sbridge_dev->n_devs; i++) {
2605 pdev = sbridge_dev->pdev[i];
2606 if (!pdev)
2607 continue;
2608
2609 switch (pdev->device) {
2610 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2611 pvt->pci_sad0 = pdev;
2612 break;
2613 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2614 pvt->pci_sad1 = pdev;
2615 break;
2616 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2617 pvt->pci_ha0 = pdev;
2618 break;
2619 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2620 pvt->pci_ta = pdev;
2621 break;
2622 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
2623 pvt->pci_ras = pdev;
2624 break;
2625 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002626 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002627 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002628 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
Tony Luck7d375bf2015-05-18 17:50:42 -03002629 {
2630 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
2631
2632 pvt->pci_tad[id] = pdev;
2633 saw_chan_mask |= 1 << id;
2634 }
2635 break;
2636 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2637 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2638 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2639 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2640 {
2641 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
2642
2643 pvt->pci_tad[id] = pdev;
2644 saw_chan_mask |= 1 << id;
2645 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002646 break;
2647 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
Aristeu Rozanski71793852015-06-12 09:44:52 -04002648 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2649 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2650 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2651 if (!pvt->pci_ddrio)
2652 pvt->pci_ddrio = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002653 break;
2654 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
2655 pvt->pci_ha1 = pdev;
2656 break;
2657 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
2658 pvt->pci_ha1_ta = pdev;
2659 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002660 default:
2661 break;
2662 }
2663
2664 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2665 sbridge_dev->bus,
2666 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2667 pdev);
2668 }
2669
2670 /* Check if everything were registered */
2671 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2672 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2673 goto enodev;
2674
Tony Luck7d375bf2015-05-18 17:50:42 -03002675 if (saw_chan_mask != 0x0f && /* -EN */
2676 saw_chan_mask != 0x33 && /* -EP */
2677 saw_chan_mask != 0xff) /* -EX */
2678 goto enodev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002679 return 0;
2680
2681enodev:
2682 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2683 return -ENODEV;
2684}
2685
Tony Luck1f395812014-12-02 09:27:30 -08002686static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2687 struct sbridge_dev *sbridge_dev)
2688{
2689 struct sbridge_pvt *pvt = mci->pvt_info;
2690 struct pci_dev *pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002691 u8 saw_chan_mask = 0;
Tony Luck1f395812014-12-02 09:27:30 -08002692 int i;
2693
2694 /* there's only one device per system; not tied to any bus */
2695 if (pvt->info.pci_vtd == NULL)
2696 /* result will be checked later */
2697 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2698 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2699 NULL);
2700
2701 for (i = 0; i < sbridge_dev->n_devs; i++) {
2702 pdev = sbridge_dev->pdev[i];
2703 if (!pdev)
2704 continue;
2705
2706 switch (pdev->device) {
2707 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2708 pvt->pci_sad0 = pdev;
2709 break;
2710 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2711 pvt->pci_sad1 = pdev;
2712 break;
2713 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2714 pvt->pci_ha0 = pdev;
2715 break;
2716 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2717 pvt->pci_ta = pdev;
2718 break;
2719 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
2720 pvt->pci_ras = pdev;
2721 break;
2722 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
Tony Luck1f395812014-12-02 09:27:30 -08002723 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
Tony Luck1f395812014-12-02 09:27:30 -08002724 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
Tony Luck1f395812014-12-02 09:27:30 -08002725 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
Tony Luckfa2ce642015-05-20 19:10:35 -03002726 {
2727 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
2728 pvt->pci_tad[id] = pdev;
2729 saw_chan_mask |= 1 << id;
2730 }
2731 break;
2732 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2733 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2734 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2735 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2736 {
2737 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
2738 pvt->pci_tad[id] = pdev;
2739 saw_chan_mask |= 1 << id;
2740 }
Tony Luck1f395812014-12-02 09:27:30 -08002741 break;
2742 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2743 pvt->pci_ddrio = pdev;
2744 break;
Tony Luckfa2ce642015-05-20 19:10:35 -03002745 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2746 pvt->pci_ha1 = pdev;
2747 break;
2748 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2749 pvt->pci_ha1_ta = pdev;
2750 break;
Tony Luck1f395812014-12-02 09:27:30 -08002751 default:
2752 break;
2753 }
2754
2755 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2756 sbridge_dev->bus,
2757 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2758 pdev);
2759 }
2760
2761 /* Check if everything were registered */
2762 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2763 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2764 goto enodev;
2765
Tony Luckfa2ce642015-05-20 19:10:35 -03002766 if (saw_chan_mask != 0x0f && /* -EN */
2767 saw_chan_mask != 0x33 && /* -EP */
2768 saw_chan_mask != 0xff) /* -EX */
2769 goto enodev;
Tony Luck1f395812014-12-02 09:27:30 -08002770 return 0;
2771
2772enodev:
2773 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2774 return -ENODEV;
2775}
2776
Jim Snowd0cdf902015-12-03 10:48:54 +01002777static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2778 struct sbridge_dev *sbridge_dev)
2779{
2780 struct sbridge_pvt *pvt = mci->pvt_info;
2781 struct pci_dev *pdev;
2782 int dev, func;
2783
2784 int i;
2785 int devidx;
2786
2787 for (i = 0; i < sbridge_dev->n_devs; i++) {
2788 pdev = sbridge_dev->pdev[i];
2789 if (!pdev)
2790 continue;
2791
2792 /* Extract PCI device and function. */
2793 dev = (pdev->devfn >> 3) & 0x1f;
2794 func = pdev->devfn & 0x7;
2795
2796 switch (pdev->device) {
2797 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2798 if (dev == 8)
2799 pvt->knl.pci_mc0 = pdev;
2800 else if (dev == 9)
2801 pvt->knl.pci_mc1 = pdev;
2802 else {
2803 sbridge_printk(KERN_ERR,
2804 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2805 dev, func);
2806 continue;
2807 }
2808 break;
2809
2810 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2811 pvt->pci_sad0 = pdev;
2812 break;
2813
2814 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2815 pvt->pci_sad1 = pdev;
2816 break;
2817
2818 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2819 /* There are one of these per tile, and range from
2820 * 1.14.0 to 1.18.5.
2821 */
2822 devidx = ((dev-14)*8)+func;
2823
2824 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2825 sbridge_printk(KERN_ERR,
2826 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2827 dev, func);
2828 continue;
2829 }
2830
2831 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2832
2833 pvt->knl.pci_cha[devidx] = pdev;
2834 break;
2835
2836 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL:
2837 devidx = -1;
2838
2839 /*
2840 * MC0 channels 0-2 are device 9 function 2-4,
2841 * MC1 channels 3-5 are device 8 function 2-4.
2842 */
2843
2844 if (dev == 9)
2845 devidx = func-2;
2846 else if (dev == 8)
2847 devidx = 3 + (func-2);
2848
2849 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2850 sbridge_printk(KERN_ERR,
2851 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2852 dev, func);
2853 continue;
2854 }
2855
2856 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2857 pvt->knl.pci_channel[devidx] = pdev;
2858 break;
2859
2860 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2861 pvt->knl.pci_mc_info = pdev;
2862 break;
2863
2864 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2865 pvt->pci_ta = pdev;
2866 break;
2867
2868 default:
2869 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2870 pdev->device);
2871 break;
2872 }
2873 }
2874
2875 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2876 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2877 !pvt->pci_ta) {
2878 goto enodev;
2879 }
2880
2881 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2882 if (!pvt->knl.pci_channel[i]) {
2883 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2884 goto enodev;
2885 }
2886 }
2887
2888 for (i = 0; i < KNL_MAX_CHAS; i++) {
2889 if (!pvt->knl.pci_cha[i]) {
2890 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2891 goto enodev;
2892 }
2893 }
2894
2895 return 0;
2896
2897enodev:
2898 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2899 return -ENODEV;
2900}
2901
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002902/****************************************************************************
2903 Error check routines
2904 ****************************************************************************/
2905
2906/*
2907 * While Sandy Bridge has error count registers, SMI BIOS read values from
2908 * and resets the counters. So, they are not reliable for the OS to read
2909 * from them. So, we have no option but to just trust on whatever MCE is
2910 * telling us about the errors.
2911 */
2912static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2913 const struct mce *m)
2914{
2915 struct mem_ctl_info *new_mci;
2916 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002917 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002918 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002919 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2920 bool overflow = GET_BITFIELD(m->status, 62, 62);
2921 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002922 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002923 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2924 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2925 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2926 u32 channel = GET_BITFIELD(m->status, 0, 3);
2927 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2928 long channel_mask, first_channel;
Tony Luck7d375bf2015-05-18 17:50:42 -03002929 u8 rank, socket, ha;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002930 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002931 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002932
Tony Luckfa2ce642015-05-20 19:10:35 -03002933 if (pvt->info.type != SANDY_BRIDGE)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002934 recoverable = true;
2935 else
2936 recoverable = GET_BITFIELD(m->status, 56, 56);
2937
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002938 if (uncorrected_error) {
2939 if (ripv) {
2940 type = "FATAL";
2941 tp_event = HW_EVENT_ERR_FATAL;
2942 } else {
2943 type = "NON_FATAL";
2944 tp_event = HW_EVENT_ERR_UNCORRECTED;
2945 }
2946 } else {
2947 type = "CORRECTED";
2948 tp_event = HW_EVENT_ERR_CORRECTED;
2949 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002950
2951 /*
David Mackey15ed1032012-04-17 11:30:52 -07002952 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002953 * memory errors should fit in this mask:
2954 * 000f 0000 1mmm cccc (binary)
2955 * where:
2956 * f = Correction Report Filtering Bit. If 1, subsequent errors
2957 * won't be shown
2958 * mmm = error type
2959 * cccc = channel
2960 * If the mask doesn't match, report an error to the parsing logic
2961 */
2962 if (! ((errcode & 0xef80) == 0x80)) {
2963 optype = "Can't parse: it is not a mem";
2964 } else {
2965 switch (optypenum) {
2966 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002967 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002968 break;
2969 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002970 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002971 break;
2972 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002973 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002974 break;
2975 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002976 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002977 break;
2978 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002979 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002980 break;
2981 default:
2982 optype = "reserved";
2983 break;
2984 }
2985 }
2986
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03002987 /* Only decode errors with an valid address (ADDRV) */
2988 if (!GET_BITFIELD(m->status, 58, 58))
2989 return;
2990
Jim Snowd0cdf902015-12-03 10:48:54 +01002991 if (pvt->info.type == KNIGHTS_LANDING) {
2992 if (channel == 14) {
2993 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2994 overflow ? " OVERFLOW" : "",
2995 (uncorrected_error && recoverable)
2996 ? " recoverable" : "",
2997 mscod, errcode,
2998 m->bank);
2999 } else {
3000 char A = *("A");
3001
3002 channel = knl_channel_remap(channel);
3003 channel_mask = 1 << channel;
3004 snprintf(msg, sizeof(msg),
3005 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
3006 overflow ? " OVERFLOW" : "",
3007 (uncorrected_error && recoverable)
3008 ? " recoverable" : " ",
3009 mscod, errcode, channel, A + channel);
3010 edac_mc_handle_error(tp_event, mci, core_err_cnt,
3011 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
3012 channel, 0, -1,
3013 optype, msg);
3014 }
3015 return;
3016 } else {
3017 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
3018 &channel_mask, &rank, &area_type, msg);
3019 }
3020
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003021 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003022 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003023 new_mci = get_mci_for_node_id(socket);
3024 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003025 strcpy(msg, "Error: socket got corrupted!");
3026 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003027 }
3028 mci = new_mci;
3029 pvt = mci->pvt_info;
3030
3031 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
3032
3033 if (rank < 4)
3034 dimm = 0;
3035 else if (rank < 8)
3036 dimm = 1;
3037 else
3038 dimm = 2;
3039
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003040
3041 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003042 * FIXME: On some memory configurations (mirror, lockstep), the
3043 * Memory Controller can't point the error to a single DIMM. The
3044 * EDAC core should be handling the channel mask, in order to point
3045 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003046 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03003047 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
3048 channel = first_channel;
3049
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003050 snprintf(msg, sizeof(msg),
Tony Luck7d375bf2015-05-18 17:50:42 -03003051 "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003052 overflow ? " OVERFLOW" : "",
3053 (uncorrected_error && recoverable) ? " recoverable" : "",
3054 area_type,
3055 mscod, errcode,
Tony Luck7d375bf2015-05-18 17:50:42 -03003056 socket, ha,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003057 channel_mask,
3058 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003059
Joe Perches956b9ba12012-04-29 17:08:39 -03003060 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003061
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003062 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003063
Seth Jennings351fc4a2014-09-05 14:28:47 -05003064 if (channel == CHANNEL_UNSPECIFIED)
3065 channel = -1;
3066
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003067 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003068 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003069 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
Tony Luck7d375bf2015-05-18 17:50:42 -03003070 4*ha+channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003071 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003072 return;
3073err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003074 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003075 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003076 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003077
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003078}
3079
3080/*
Tony Luckad08c4e2016-04-15 14:50:32 -07003081 * Check that logging is enabled and that this is the right type
3082 * of error for us to handle.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003083 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003084static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3085 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003086{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003087 struct mce *mce = (struct mce *)data;
3088 struct mem_ctl_info *mci;
3089 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003090 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003091
Chen, Gongfd521032013-12-06 01:17:09 -05003092 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3093 return NOTIFY_DONE;
3094
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003095 mci = get_mci_for_node_id(mce->socketid);
3096 if (!mci)
Tony Luckc4fc1952016-04-29 15:42:25 +02003097 return NOTIFY_DONE;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003098 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003099
3100 /*
3101 * Just let mcelog handle it if the error is
3102 * outside the memory controller. A memory error
3103 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3104 * bit 12 has an special meaning.
3105 */
3106 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003107 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003108
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003109 if (mce->mcgstatus & MCG_STATUS_MCIP)
3110 type = "Exception";
3111 else
3112 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003113
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003114 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003115
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003116 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3117 "Bank %d: %016Lx\n", mce->extcpu, type,
3118 mce->mcgstatus, mce->bank, mce->status);
3119 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3120 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3121 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003122
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003123 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3124 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3125 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003126
Tony Luckad08c4e2016-04-15 14:50:32 -07003127 sbridge_mce_output_error(mci, mce);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003128
3129 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003130 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003131}
3132
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003133static struct notifier_block sbridge_mce_dec = {
3134 .notifier_call = sbridge_mce_check_error,
3135};
3136
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003137/****************************************************************************
3138 EDAC register/unregister logic
3139 ****************************************************************************/
3140
3141static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3142{
3143 struct mem_ctl_info *mci = sbridge_dev->mci;
3144 struct sbridge_pvt *pvt;
3145
3146 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba12012-04-29 17:08:39 -03003147 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003148
3149 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3150 return;
3151 }
3152
3153 pvt = mci->pvt_info;
3154
Joe Perches956b9ba12012-04-29 17:08:39 -03003155 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3156 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003157
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003158 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03003159 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003160
Joe Perches956b9ba12012-04-29 17:08:39 -03003161 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003162 kfree(mci->ctl_name);
3163 edac_mc_free(mci);
3164 sbridge_dev->mci = NULL;
3165}
3166
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003167static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003168{
3169 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003170 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003171 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003172 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003173 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003174
3175 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03003176 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003177 if (unlikely(rc < 0))
3178 return rc;
3179
3180 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003181 layers[0].type = EDAC_MC_LAYER_CHANNEL;
Jim Snowd0cdf902015-12-03 10:48:54 +01003182 layers[0].size = type == KNIGHTS_LANDING ?
3183 KNL_MAX_CHANNELS : NUM_CHANNELS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003184 layers[0].is_virt_csrow = false;
3185 layers[1].type = EDAC_MC_LAYER_SLOT;
Jim Snowd0cdf902015-12-03 10:48:54 +01003186 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003187 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03003188 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003189 sizeof(*pvt));
3190
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003191 if (unlikely(!mci))
3192 return -ENOMEM;
3193
Joe Perches956b9ba12012-04-29 17:08:39 -03003194 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003195 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003196
3197 pvt = mci->pvt_info;
3198 memset(pvt, 0, sizeof(*pvt));
3199
3200 /* Associate sbridge_dev and mci for future usage */
3201 pvt->sbridge_dev = sbridge_dev;
3202 sbridge_dev->mci = mci;
3203
Jim Snowd0cdf902015-12-03 10:48:54 +01003204 mci->mtype_cap = type == KNIGHTS_LANDING ?
3205 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003206 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3207 mci->edac_cap = EDAC_FLAG_NONE;
3208 mci->mod_name = "sbridge_edac.c";
3209 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003210 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003211 mci->ctl_page_to_phys = NULL;
3212
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003213 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003214 switch (type) {
3215 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003216 pvt->info.rankcfgr = IB_RANK_CFG_A;
3217 pvt->info.get_tolm = ibridge_get_tolm;
3218 pvt->info.get_tohm = ibridge_get_tohm;
3219 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003220 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003221 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003222 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003223 pvt->info.sad_limit = sad_limit;
3224 pvt->info.interleave_mode = interleave_mode;
3225 pvt->info.show_interleave_mode = show_interleave_mode;
3226 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003227 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3228 pvt->info.interleave_list = ibridge_interleave_list;
3229 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3230 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003231 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003232 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
3233
3234 /* Store pci devices at mci for faster access */
3235 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3236 if (unlikely(rc < 0))
3237 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003238 break;
3239 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003240 pvt->info.rankcfgr = SB_RANK_CFG_A;
3241 pvt->info.get_tolm = sbridge_get_tolm;
3242 pvt->info.get_tohm = sbridge_get_tohm;
3243 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003244 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003245 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003246 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003247 pvt->info.sad_limit = sad_limit;
3248 pvt->info.interleave_mode = interleave_mode;
3249 pvt->info.show_interleave_mode = show_interleave_mode;
3250 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003251 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3252 pvt->info.interleave_list = sbridge_interleave_list;
3253 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3254 pvt->info.interleave_pkg = sbridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003255 pvt->info.get_width = sbridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003256 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
3257
3258 /* Store pci devices at mci for faster access */
3259 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3260 if (unlikely(rc < 0))
3261 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003262 break;
3263 case HASWELL:
3264 /* rankcfgr isn't used */
3265 pvt->info.get_tolm = haswell_get_tolm;
3266 pvt->info.get_tohm = haswell_get_tohm;
3267 pvt->info.dram_rule = ibridge_dram_rule;
3268 pvt->info.get_memory_type = haswell_get_memory_type;
3269 pvt->info.get_node_id = haswell_get_node_id;
3270 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003271 pvt->info.sad_limit = sad_limit;
3272 pvt->info.interleave_mode = interleave_mode;
3273 pvt->info.show_interleave_mode = show_interleave_mode;
3274 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003275 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3276 pvt->info.interleave_list = ibridge_interleave_list;
3277 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3278 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003279 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003280 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003281
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003282 /* Store pci devices at mci for faster access */
3283 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3284 if (unlikely(rc < 0))
3285 goto fail0;
3286 break;
Tony Luck1f395812014-12-02 09:27:30 -08003287 case BROADWELL:
3288 /* rankcfgr isn't used */
3289 pvt->info.get_tolm = haswell_get_tolm;
3290 pvt->info.get_tohm = haswell_get_tohm;
3291 pvt->info.dram_rule = ibridge_dram_rule;
3292 pvt->info.get_memory_type = haswell_get_memory_type;
3293 pvt->info.get_node_id = haswell_get_node_id;
3294 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003295 pvt->info.sad_limit = sad_limit;
3296 pvt->info.interleave_mode = interleave_mode;
3297 pvt->info.show_interleave_mode = show_interleave_mode;
3298 pvt->info.dram_attr = dram_attr;
Tony Luck1f395812014-12-02 09:27:30 -08003299 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3300 pvt->info.interleave_list = ibridge_interleave_list;
3301 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3302 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003303 pvt->info.get_width = broadwell_get_width;
Tony Luck1f395812014-12-02 09:27:30 -08003304 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
3305
3306 /* Store pci devices at mci for faster access */
3307 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3308 if (unlikely(rc < 0))
3309 goto fail0;
3310 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01003311 case KNIGHTS_LANDING:
3312 /* pvt->info.rankcfgr == ??? */
3313 pvt->info.get_tolm = knl_get_tolm;
3314 pvt->info.get_tohm = knl_get_tohm;
3315 pvt->info.dram_rule = knl_dram_rule;
3316 pvt->info.get_memory_type = knl_get_memory_type;
3317 pvt->info.get_node_id = knl_get_node_id;
3318 pvt->info.rir_limit = NULL;
3319 pvt->info.sad_limit = knl_sad_limit;
3320 pvt->info.interleave_mode = knl_interleave_mode;
3321 pvt->info.show_interleave_mode = knl_show_interleave_mode;
3322 pvt->info.dram_attr = dram_attr_knl;
3323 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3324 pvt->info.interleave_list = knl_interleave_list;
3325 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3326 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +01003327 pvt->info.get_width = knl_get_width;
Jim Snowd0cdf902015-12-03 10:48:54 +01003328 mci->ctl_name = kasprintf(GFP_KERNEL,
3329 "Knights Landing Socket#%d", mci->mc_idx);
3330
3331 rc = knl_mci_bind_devs(mci, sbridge_dev);
3332 if (unlikely(rc < 0))
3333 goto fail0;
3334 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003335 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003336
3337 /* Get dimm basic config and the memory layout */
3338 get_dimm_config(mci);
3339 get_memory_layout(mci);
3340
3341 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003342 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003343
3344 /* add this new MC control structure to EDAC's list of MCs */
3345 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba12012-04-29 17:08:39 -03003346 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003347 rc = -EINVAL;
3348 goto fail0;
3349 }
3350
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003351 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003352
3353fail0:
3354 kfree(mci->ctl_name);
3355 edac_mc_free(mci);
3356 sbridge_dev->mci = NULL;
3357 return rc;
3358}
3359
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003360#define ICPU(model, table) \
3361 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3362
3363/* Order here must match "enum type" */
3364static const struct x86_cpu_id sbridge_cpuids[] = {
3365 ICPU(0x2d, pci_dev_descr_sbridge_table), /* SANDY_BRIDGE */
3366 ICPU(0x3e, pci_dev_descr_ibridge_table), /* IVY_BRIDGE */
3367 ICPU(0x3f, pci_dev_descr_haswell_table), /* HASWELL */
3368 ICPU(0x4f, pci_dev_descr_broadwell_table), /* BROADWELL */
3369 ICPU(0x57, pci_dev_descr_knl_table), /* KNIGHTS_LANDING */
3370 { }
3371};
3372MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3373
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003374/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003375 * sbridge_probe Get all devices and register memory controllers
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003376 * present.
3377 * return:
3378 * 0 for FOUND a device
3379 * < 0 for error code
3380 */
3381
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003382static int sbridge_probe(const struct x86_cpu_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003383{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003384 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003385 u8 mc, num_mc = 0;
3386 struct sbridge_dev *sbridge_dev;
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003387 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003388
3389 /* get the pci devices we want to reserve for our use */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003390 rc = sbridge_get_all_devices(&num_mc, ptable);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003391
Borislav Petkov11249e72015-02-05 12:39:36 +01003392 if (unlikely(rc < 0)) {
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003393 edac_dbg(0, "couldn't get all devices\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003394 goto fail0;
Borislav Petkov11249e72015-02-05 12:39:36 +01003395 }
3396
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003397 mc = 0;
3398
3399 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba12012-04-29 17:08:39 -03003400 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3401 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003402
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003403 sbridge_dev->mc = mc++;
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003404 rc = sbridge_register_mci(sbridge_dev, id - sbridge_cpuids);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003405 if (unlikely(rc < 0))
3406 goto fail1;
3407 }
3408
Borislav Petkov11249e72015-02-05 12:39:36 +01003409 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003410
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003411 return 0;
3412
3413fail1:
3414 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3415 sbridge_unregister_mci(sbridge_dev);
3416
3417 sbridge_put_all_devices();
3418fail0:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003419 return rc;
3420}
3421
3422/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003423 * sbridge_remove cleanup
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003424 *
3425 */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003426static void sbridge_remove(void)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003427{
3428 struct sbridge_dev *sbridge_dev;
3429
Joe Perches956b9ba12012-04-29 17:08:39 -03003430 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003431
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003432 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3433 sbridge_unregister_mci(sbridge_dev);
3434
3435 /* Release PCI resources */
3436 sbridge_put_all_devices();
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003437}
3438
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003439/*
3440 * sbridge_init Module entry function
3441 * Try to initialize this module for its devices
3442 */
3443static int __init sbridge_init(void)
3444{
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003445 const struct x86_cpu_id *id;
3446 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003447
Joe Perches956b9ba12012-04-29 17:08:39 -03003448 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003449
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003450 id = x86_match_cpu(sbridge_cpuids);
3451 if (!id)
3452 return -ENODEV;
3453
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003454 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3455 opstate_init();
3456
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003457 rc = sbridge_probe(id);
3458
3459 if (rc >= 0) {
Chen Gonge35fca42012-05-08 20:40:12 -03003460 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05003461 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3462 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003463 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03003464 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003465
3466 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003467 rc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003468
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003469 return rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003470}
3471
3472/*
3473 * sbridge_exit() Module exit function
3474 * Unregister the driver
3475 */
3476static void __exit sbridge_exit(void)
3477{
Joe Perches956b9ba12012-04-29 17:08:39 -03003478 edac_dbg(2, "\n");
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003479 sbridge_remove();
Chen Gonge35fca42012-05-08 20:40:12 -03003480 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003481}
3482
3483module_init(sbridge_init);
3484module_exit(sbridge_exit);
3485
3486module_param(edac_op_state, int, 0444);
3487MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3488
3489MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003490MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003491MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003492MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003493 SBRIDGE_REVISION);