blob: 54775221a01fbe73d6a184ac903ccafc7e76c50c [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;
Tony Luck665f05e02016-06-02 10:58:08 -0700332 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200333};
334
335struct sbridge_dev {
336 struct list_head list;
337 u8 bus, mc;
338 u8 node_id, source_id;
339 struct pci_dev **pdev;
340 int n_devs;
341 struct mem_ctl_info *mci;
342};
343
Jim Snowd0cdf902015-12-03 10:48:54 +0100344struct knl_pvt {
345 struct pci_dev *pci_cha[KNL_MAX_CHAS];
346 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
347 struct pci_dev *pci_mc0;
348 struct pci_dev *pci_mc1;
349 struct pci_dev *pci_mc0_misc;
350 struct pci_dev *pci_mc1_misc;
351 struct pci_dev *pci_mc_info; /* tolm, tohm */
352};
353
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200354struct sbridge_pvt {
355 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300356 struct pci_dev *pci_sad0, *pci_sad1;
357 struct pci_dev *pci_ha0, *pci_ha1;
358 struct pci_dev *pci_br0, *pci_br1;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300359 struct pci_dev *pci_ha1_ta;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200360 struct pci_dev *pci_tad[NUM_CHANNELS];
361
362 struct sbridge_dev *sbridge_dev;
363
364 struct sbridge_info info;
365 struct sbridge_channel channel[NUM_CHANNELS];
366
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200367 /* Memory type detection */
368 bool is_mirrored, is_lockstep, is_close_pg;
Tony Luckea5dfb52016-04-14 10:22:02 -0700369 bool is_chan_hash;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200370
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200371 /* Memory description */
372 u64 tolm, tohm;
Jim Snowd0cdf902015-12-03 10:48:54 +0100373 struct knl_pvt knl;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200374};
375
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300376#define PCI_DESCR(device_id, opt) \
377 .dev_id = (device_id), \
Luck, Tonyde4772c2013-03-28 09:59:15 -0700378 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200379
380static const struct pci_id_descr pci_dev_descr_sbridge[] = {
381 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300382 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200383
384 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300385 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
386 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
387 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
388 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
389 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
390 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
391 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200392
393 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300394 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
395 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200396
397 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300398 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200399};
400
Tony Luck665f05e02016-06-02 10:58:08 -0700401#define PCI_ID_TABLE_ENTRY(A, T) { \
402 .descr = A, \
403 .n_devs = ARRAY_SIZE(A), \
404 .type = T \
405}
406
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200407static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700408 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, SANDY_BRIDGE),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200409 {0,} /* 0 terminated list. */
410};
411
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300412/* This changes depending if 1HA or 2HA:
413 * 1HA:
414 * 0x0eb8 (17.0) is DDRIO0
415 * 2HA:
416 * 0x0ebc (17.4) is DDRIO0
417 */
418#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
419#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
420
421/* pci ids */
422#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
423#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
424#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
425#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
426#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
427#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
428#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
429#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
430#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
431#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
432#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
433#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
434#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
435#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
436#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
Tony Luck7d375bf2015-05-18 17:50:42 -0300437#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
438#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300439
440static const struct pci_id_descr pci_dev_descr_ibridge[] = {
441 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300442 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300443
444 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300445 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
446 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
447 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
448 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
449 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
450 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300451
452 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300453 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300454
455 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300456 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
457 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300458
459 /* Optional, mode 2HA */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300460 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300461#if 0
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300462 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
463 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300464#endif
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300465 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
466 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
Tony Luck7d375bf2015-05-18 17:50:42 -0300467 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
468 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300469
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300470 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
471 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300472};
473
474static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700475 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, IVY_BRIDGE),
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300476 {0,} /* 0 terminated list. */
477};
478
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300479/* Haswell support */
480/* EN processor:
481 * - 1 IMC
482 * - 3 DDR3 channels, 2 DPC per channel
483 * EP processor:
484 * - 1 or 2 IMC
485 * - 4 DDR4 channels, 3 DPC per channel
486 * EP 4S processor:
487 * - 2 IMC
488 * - 4 DDR4 channels, 3 DPC per channel
489 * EX processor:
490 * - 2 IMC
491 * - each IMC interfaces with a SMI 2 channel
492 * - each SMI channel interfaces with a scalable memory buffer
493 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
494 */
Tony Luck1f395812014-12-02 09:27:30 -0800495#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300496#define HASWELL_HASYSDEFEATURE2 0x84
497#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
498#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
499#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
500#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
501#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
502#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
503#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
504#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
505#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
506#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
507#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
508#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
509#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
510#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
511#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
512#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
513#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
514#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
Aristeu Rozanski71793852015-06-12 09:44:52 -0400515#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
516#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
517#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300518static const struct pci_id_descr pci_dev_descr_haswell[] = {
519 /* first item must be the HA */
520 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
521
522 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
523 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
524
525 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
526
527 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
528 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
529 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
530 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
531 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
532 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
533
534 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
Aristeu Rozanski71793852015-06-12 09:44:52 -0400535 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
536 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
537 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300538
539 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
540 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
541 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
542 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
543 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
544 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
545};
546
547static const struct pci_id_table pci_dev_descr_haswell_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700548 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, HASWELL),
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300549 {0,} /* 0 terminated list. */
550};
551
Jim Snowd0cdf902015-12-03 10:48:54 +0100552/* Knight's Landing Support */
553/*
554 * KNL's memory channels are swizzled between memory controllers.
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200555 * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
Jim Snowd0cdf902015-12-03 10:48:54 +0100556 */
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200557#define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
Jim Snowd0cdf902015-12-03 10:48:54 +0100558
559/* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
560#define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
561/* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
562#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
563/* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
564#define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
565/* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
566#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
567/* SAD target - 1-29-1 (1 of these) */
568#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
569/* Caching / Home Agent */
570#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
571/* Device with TOLM and TOHM, 0-5-0 (1 of these) */
572#define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
573
574/*
575 * KNL differs from SB, IB, and Haswell in that it has multiple
576 * instances of the same device with the same device ID, so we handle that
577 * by creating as many copies in the table as we expect to find.
578 * (Like device ID must be grouped together.)
579 */
580
581static const struct pci_id_descr pci_dev_descr_knl[] = {
582 [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
583 [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
584 [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
585 [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
586 [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
587 [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
588 [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
589};
590
591static const struct pci_id_table pci_dev_descr_knl_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700592 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, KNIGHTS_LANDING),
Jim Snowd0cdf902015-12-03 10:48:54 +0100593 {0,}
594};
595
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200596/*
Tony Luck1f395812014-12-02 09:27:30 -0800597 * Broadwell support
598 *
599 * DE processor:
600 * - 1 IMC
601 * - 2 DDR3 channels, 2 DPC per channel
Tony Luckfa2ce642015-05-20 19:10:35 -0300602 * EP processor:
603 * - 1 or 2 IMC
604 * - 4 DDR4 channels, 3 DPC per channel
605 * EP 4S processor:
606 * - 2 IMC
607 * - 4 DDR4 channels, 3 DPC per channel
608 * EX processor:
609 * - 2 IMC
610 * - each IMC interfaces with a SMI 2 channel
611 * - each SMI channel interfaces with a scalable memory buffer
612 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
Tony Luck1f395812014-12-02 09:27:30 -0800613 */
614#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
615#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
Tony Luckfa2ce642015-05-20 19:10:35 -0300616#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
Tony Luck1f395812014-12-02 09:27:30 -0800617#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
618#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
Tony Luckfa2ce642015-05-20 19:10:35 -0300619#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
620#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
Tony Luck1f395812014-12-02 09:27:30 -0800621#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
622#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
623#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
624#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
625#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
626#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
Tony Luckfa2ce642015-05-20 19:10:35 -0300627#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
628#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
629#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
630#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
Tony Luck1f395812014-12-02 09:27:30 -0800631#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
632
633static const struct pci_id_descr pci_dev_descr_broadwell[] = {
634 /* first item must be the HA */
635 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
636
637 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
638 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
639
Tony Luckfa2ce642015-05-20 19:10:35 -0300640 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
641
Tony Luck1f395812014-12-02 09:27:30 -0800642 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
643 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
644 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
645 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300646 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
647 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
648
Tony Luck1f395812014-12-02 09:27:30 -0800649 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300650
651 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
652 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
653 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
654 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
655 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
656 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
Tony Luck1f395812014-12-02 09:27:30 -0800657};
658
659static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700660 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, BROADWELL),
Tony Luck1f395812014-12-02 09:27:30 -0800661 {0,} /* 0 terminated list. */
662};
663
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200664
665/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700666 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200667 ****************************************************************************/
668
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300669static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200670{
671 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300672 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200673
Jim Snowd0cdf902015-12-03 10:48:54 +0100674 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300675 max = 8;
676
677 if (ranks > max) {
678 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
679 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200680 return -EINVAL;
681 }
682
683 return ranks;
684}
685
686static inline int numrow(u32 mtr)
687{
688 int rows = (RANK_WIDTH_BITS(mtr) + 12);
689
690 if (rows < 13 || rows > 18) {
Joe Perches956b9ba12012-04-29 17:08:39 -0300691 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
692 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200693 return -EINVAL;
694 }
695
696 return 1 << rows;
697}
698
699static inline int numcol(u32 mtr)
700{
701 int cols = (COL_WIDTH_BITS(mtr) + 10);
702
703 if (cols > 12) {
Joe Perches956b9ba12012-04-29 17:08:39 -0300704 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
705 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200706 return -EINVAL;
707 }
708
709 return 1 << cols;
710}
711
Jim Snowc1979ba2015-12-03 10:48:53 +0100712static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200713{
714 struct sbridge_dev *sbridge_dev;
715
Jim Snowc1979ba2015-12-03 10:48:53 +0100716 /*
717 * If we have devices scattered across several busses that pertain
718 * to the same memory controller, we'll lump them all together.
719 */
720 if (multi_bus) {
721 return list_first_entry_or_null(&sbridge_edac_list,
722 struct sbridge_dev, list);
723 }
724
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200725 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
726 if (sbridge_dev->bus == bus)
727 return sbridge_dev;
728 }
729
730 return NULL;
731}
732
733static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
734 const struct pci_id_table *table)
735{
736 struct sbridge_dev *sbridge_dev;
737
738 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
739 if (!sbridge_dev)
740 return NULL;
741
742 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
743 GFP_KERNEL);
744 if (!sbridge_dev->pdev) {
745 kfree(sbridge_dev);
746 return NULL;
747 }
748
749 sbridge_dev->bus = bus;
750 sbridge_dev->n_devs = table->n_devs;
751 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
752
753 return sbridge_dev;
754}
755
756static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
757{
758 list_del(&sbridge_dev->list);
759 kfree(sbridge_dev->pdev);
760 kfree(sbridge_dev);
761}
762
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300763static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
764{
765 u32 reg;
766
767 /* Address range is 32:28 */
768 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
769 return GET_TOLM(reg);
770}
771
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300772static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
773{
774 u32 reg;
775
776 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
777 return GET_TOHM(reg);
778}
779
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300780static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
781{
782 u32 reg;
783
784 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
785
786 return GET_TOLM(reg);
787}
788
789static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
790{
791 u32 reg;
792
793 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
794
795 return GET_TOHM(reg);
796}
797
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300798static u64 rir_limit(u32 reg)
799{
800 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
801}
802
Jim Snowc59f9c02015-12-03 10:48:52 +0100803static u64 sad_limit(u32 reg)
804{
805 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
806}
807
808static u32 interleave_mode(u32 reg)
809{
810 return GET_BITFIELD(reg, 1, 1);
811}
812
813char *show_interleave_mode(u32 reg)
814{
815 return interleave_mode(reg) ? "8:6" : "[8:6]XOR[18:16]";
816}
817
818static u32 dram_attr(u32 reg)
819{
820 return GET_BITFIELD(reg, 2, 3);
821}
822
Jim Snowd0cdf902015-12-03 10:48:54 +0100823static u64 knl_sad_limit(u32 reg)
824{
825 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
826}
827
828static u32 knl_interleave_mode(u32 reg)
829{
830 return GET_BITFIELD(reg, 1, 2);
831}
832
833static char *knl_show_interleave_mode(u32 reg)
834{
835 char *s;
836
837 switch (knl_interleave_mode(reg)) {
838 case 0:
839 s = "use address bits [8:6]";
840 break;
841 case 1:
842 s = "use address bits [10:8]";
843 break;
844 case 2:
845 s = "use address bits [14:12]";
846 break;
847 case 3:
848 s = "use address bits [32:30]";
849 break;
850 default:
851 WARN_ON(1);
852 break;
853 }
854
855 return s;
856}
857
858static u32 dram_attr_knl(u32 reg)
859{
860 return GET_BITFIELD(reg, 3, 4);
861}
862
863
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300864static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
865{
866 u32 reg;
867 enum mem_type mtype;
868
869 if (pvt->pci_ddrio) {
870 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
871 &reg);
872 if (GET_BITFIELD(reg, 11, 11))
873 /* FIXME: Can also be LRDIMM */
874 mtype = MEM_RDDR3;
875 else
876 mtype = MEM_DDR3;
877 } else
878 mtype = MEM_UNKNOWN;
879
880 return mtype;
881}
882
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300883static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
884{
885 u32 reg;
886 bool registered = false;
887 enum mem_type mtype = MEM_UNKNOWN;
888
889 if (!pvt->pci_ddrio)
890 goto out;
891
892 pci_read_config_dword(pvt->pci_ddrio,
893 HASWELL_DDRCRCLKCONTROLS, &reg);
894 /* Is_Rdimm */
895 if (GET_BITFIELD(reg, 16, 16))
896 registered = true;
897
898 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
899 if (GET_BITFIELD(reg, 14, 14)) {
900 if (registered)
901 mtype = MEM_RDDR4;
902 else
903 mtype = MEM_DDR4;
904 } else {
905 if (registered)
906 mtype = MEM_RDDR3;
907 else
908 mtype = MEM_DDR3;
909 }
910
911out:
912 return mtype;
913}
914
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +0100915static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
916{
917 /* for KNL value is fixed */
918 return DEV_X16;
919}
920
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400921static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
922{
923 /* there's no way to figure out */
924 return DEV_UNKNOWN;
925}
926
927static enum dev_type __ibridge_get_width(u32 mtr)
928{
929 enum dev_type type;
930
931 switch (mtr) {
932 case 3:
933 type = DEV_UNKNOWN;
934 break;
935 case 2:
936 type = DEV_X16;
937 break;
938 case 1:
939 type = DEV_X8;
940 break;
941 case 0:
942 type = DEV_X4;
943 break;
944 }
945
946 return type;
947}
948
949static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
950{
951 /*
952 * ddr3_width on the documentation but also valid for DDR4 on
953 * Haswell
954 */
955 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
956}
957
958static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
959{
960 /* ddr3_width on the documentation but also valid for DDR4 */
961 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
962}
963
Jim Snowd0cdf902015-12-03 10:48:54 +0100964static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
965{
966 /* DDR4 RDIMMS and LRDIMMS are supported */
967 return MEM_RDDR4;
968}
969
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300970static u8 get_node_id(struct sbridge_pvt *pvt)
971{
972 u32 reg;
973 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
974 return GET_BITFIELD(reg, 0, 2);
975}
976
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300977static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
978{
979 u32 reg;
980
981 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
982 return GET_BITFIELD(reg, 0, 3);
983}
984
Jim Snowd0cdf902015-12-03 10:48:54 +0100985static u8 knl_get_node_id(struct sbridge_pvt *pvt)
986{
987 u32 reg;
988
989 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
990 return GET_BITFIELD(reg, 0, 2);
991}
992
993
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300994static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
995{
996 u32 reg;
997
Tony Luckf7cf2a22014-10-29 10:36:50 -0700998 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
999 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001000}
1001
1002static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
1003{
1004 u64 rc;
1005 u32 reg;
1006
1007 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
1008 rc = GET_BITFIELD(reg, 26, 31);
1009 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
1010 rc = ((reg << 6) | rc) << 26;
1011
1012 return rc | 0x1ffffff;
1013}
1014
Jim Snowd0cdf902015-12-03 10:48:54 +01001015static u64 knl_get_tolm(struct sbridge_pvt *pvt)
1016{
1017 u32 reg;
1018
1019 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
1020 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1021}
1022
1023static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1024{
1025 u64 rc;
1026 u32 reg_lo, reg_hi;
1027
1028 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
1029 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
1030 rc = ((u64)reg_hi << 32) | reg_lo;
1031 return rc | 0x3ffffff;
1032}
1033
1034
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001035static u64 haswell_rir_limit(u32 reg)
1036{
1037 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1038}
1039
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001040static inline u8 sad_pkg_socket(u8 pkg)
1041{
1042 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -03001043 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001044}
1045
1046static inline u8 sad_pkg_ha(u8 pkg)
1047{
1048 return (pkg >> 2) & 0x1;
1049}
1050
Tony Luckea5dfb52016-04-14 10:22:02 -07001051static int haswell_chan_hash(int idx, u64 addr)
1052{
1053 int i;
1054
1055 /*
1056 * XOR even bits from 12:26 to bit0 of idx,
1057 * odd bits from 13:27 to bit1
1058 */
1059 for (i = 12; i < 28; i += 2)
1060 idx ^= (addr >> i) & 3;
1061
1062 return idx;
1063}
1064
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001065/****************************************************************************
1066 Memory check routines
1067 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001068static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001069{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001070 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001071
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001072 do {
1073 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
1074 if (pdev && pdev->bus->number == bus)
1075 break;
1076 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001077
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001078 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001079}
1080
1081/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001082 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001083 * @bus: Device bus
1084 * @type: Memory controller type
1085 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
1086 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001087 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001088static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001089{
1090 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001091 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001092
Tony Luck1f395812014-12-02 09:27:30 -08001093 switch (type) {
1094 case IVY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001095 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001096 break;
1097 case HASWELL:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001098 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001099 break;
1100 case SANDY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001101 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001102 break;
1103 case BROADWELL:
1104 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
1105 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01001106 case KNIGHTS_LANDING:
1107 /*
1108 * KNL doesn't group things by bus the same way
1109 * SB/IB/Haswell does.
1110 */
1111 id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
1112 break;
Tony Luck1f395812014-12-02 09:27:30 -08001113 default:
1114 return -ENODEV;
1115 }
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001116
Jim Snowd0cdf902015-12-03 10:48:54 +01001117 if (type != KNIGHTS_LANDING)
1118 pdev = get_pdev_same_bus(bus, id);
1119 else
1120 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
1121
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001122 if (!pdev) {
1123 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001124 "%04x:%04x! on bus %02d\n",
1125 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001126 return -ENODEV;
1127 }
1128
Jim Snowd0cdf902015-12-03 10:48:54 +01001129 pci_read_config_dword(pdev,
1130 type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001131 if (!IS_ECC_ENABLED(mcmtr)) {
1132 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
1133 return -ENODEV;
1134 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001135 return 0;
1136}
1137
Jim Snowd0cdf902015-12-03 10:48:54 +01001138/* Low bits of TAD limit, and some metadata. */
1139static const u32 knl_tad_dram_limit_lo[] = {
1140 0x400, 0x500, 0x600, 0x700,
1141 0x800, 0x900, 0xa00, 0xb00,
1142};
1143
1144/* Low bits of TAD offset. */
1145static const u32 knl_tad_dram_offset_lo[] = {
1146 0x404, 0x504, 0x604, 0x704,
1147 0x804, 0x904, 0xa04, 0xb04,
1148};
1149
1150/* High 16 bits of TAD limit and offset. */
1151static const u32 knl_tad_dram_hi[] = {
1152 0x408, 0x508, 0x608, 0x708,
1153 0x808, 0x908, 0xa08, 0xb08,
1154};
1155
1156/* Number of ways a tad entry is interleaved. */
1157static const u32 knl_tad_ways[] = {
1158 8, 6, 4, 3, 2, 1,
1159};
1160
1161/*
1162 * Retrieve the n'th Target Address Decode table entry
1163 * from the memory controller's TAD table.
1164 *
1165 * @pvt: driver private data
1166 * @entry: which entry you want to retrieve
1167 * @mc: which memory controller (0 or 1)
1168 * @offset: output tad range offset
1169 * @limit: output address of first byte above tad range
1170 * @ways: output number of interleave ways
1171 *
1172 * The offset value has curious semantics. It's a sort of running total
1173 * of the sizes of all the memory regions that aren't mapped in this
1174 * tad table.
1175 */
1176static int knl_get_tad(const struct sbridge_pvt *pvt,
1177 const int entry,
1178 const int mc,
1179 u64 *offset,
1180 u64 *limit,
1181 int *ways)
1182{
1183 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1184 struct pci_dev *pci_mc;
1185 int way_id;
1186
1187 switch (mc) {
1188 case 0:
1189 pci_mc = pvt->knl.pci_mc0;
1190 break;
1191 case 1:
1192 pci_mc = pvt->knl.pci_mc1;
1193 break;
1194 default:
1195 WARN_ON(1);
1196 return -EINVAL;
1197 }
1198
1199 pci_read_config_dword(pci_mc,
1200 knl_tad_dram_limit_lo[entry], &reg_limit_lo);
1201 pci_read_config_dword(pci_mc,
1202 knl_tad_dram_offset_lo[entry], &reg_offset_lo);
1203 pci_read_config_dword(pci_mc,
1204 knl_tad_dram_hi[entry], &reg_hi);
1205
1206 /* Is this TAD entry enabled? */
1207 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1208 return -ENODEV;
1209
1210 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1211
1212 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1213 *ways = knl_tad_ways[way_id];
1214 } else {
1215 *ways = 0;
1216 sbridge_printk(KERN_ERR,
1217 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1218 way_id);
1219 return -ENODEV;
1220 }
1221
1222 /*
1223 * The least significant 6 bits of base and limit are truncated.
1224 * For limit, we fill the missing bits with 1s.
1225 */
1226 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1227 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1228 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1229 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1230
1231 return 0;
1232}
1233
1234/* Determine which memory controller is responsible for a given channel. */
1235static int knl_channel_mc(int channel)
1236{
1237 WARN_ON(channel < 0 || channel >= 6);
1238
1239 return channel < 3 ? 1 : 0;
1240}
1241
1242/*
1243 * Get the Nth entry from EDC_ROUTE_TABLE register.
1244 * (This is the per-tile mapping of logical interleave targets to
1245 * physical EDC modules.)
1246 *
1247 * entry 0: 0:2
1248 * 1: 3:5
1249 * 2: 6:8
1250 * 3: 9:11
1251 * 4: 12:14
1252 * 5: 15:17
1253 * 6: 18:20
1254 * 7: 21:23
1255 * reserved: 24:31
1256 */
1257static u32 knl_get_edc_route(int entry, u32 reg)
1258{
1259 WARN_ON(entry >= KNL_MAX_EDCS);
1260 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1261}
1262
1263/*
1264 * Get the Nth entry from MC_ROUTE_TABLE register.
1265 * (This is the per-tile mapping of logical interleave targets to
1266 * physical DRAM channels modules.)
1267 *
1268 * entry 0: mc 0:2 channel 18:19
1269 * 1: mc 3:5 channel 20:21
1270 * 2: mc 6:8 channel 22:23
1271 * 3: mc 9:11 channel 24:25
1272 * 4: mc 12:14 channel 26:27
1273 * 5: mc 15:17 channel 28:29
1274 * reserved: 30:31
1275 *
1276 * Though we have 3 bits to identify the MC, we should only see
1277 * the values 0 or 1.
1278 */
1279
1280static u32 knl_get_mc_route(int entry, u32 reg)
1281{
1282 int mc, chan;
1283
1284 WARN_ON(entry >= KNL_MAX_CHANNELS);
1285
1286 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1287 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1288
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02001289 return knl_channel_remap(mc, chan);
Jim Snowd0cdf902015-12-03 10:48:54 +01001290}
1291
1292/*
1293 * Render the EDC_ROUTE register in human-readable form.
1294 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1295 */
1296static void knl_show_edc_route(u32 reg, char *s)
1297{
1298 int i;
1299
1300 for (i = 0; i < KNL_MAX_EDCS; i++) {
1301 s[i*2] = knl_get_edc_route(i, reg) + '0';
1302 s[i*2+1] = '-';
1303 }
1304
1305 s[KNL_MAX_EDCS*2 - 1] = '\0';
1306}
1307
1308/*
1309 * Render the MC_ROUTE register in human-readable form.
1310 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1311 */
1312static void knl_show_mc_route(u32 reg, char *s)
1313{
1314 int i;
1315
1316 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1317 s[i*2] = knl_get_mc_route(i, reg) + '0';
1318 s[i*2+1] = '-';
1319 }
1320
1321 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1322}
1323
1324#define KNL_EDC_ROUTE 0xb8
1325#define KNL_MC_ROUTE 0xb4
1326
1327/* Is this dram rule backed by regular DRAM in flat mode? */
1328#define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1329
1330/* Is this dram rule cached? */
1331#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1332
1333/* Is this rule backed by edc ? */
1334#define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1335
1336/* Is this rule backed by DRAM, cacheable in EDRAM? */
1337#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1338
1339/* Is this rule mod3? */
1340#define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1341
1342/*
1343 * Figure out how big our RAM modules are.
1344 *
1345 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1346 * have to figure this out from the SAD rules, interleave lists, route tables,
1347 * and TAD rules.
1348 *
1349 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1350 * inspect the TAD rules to figure out how large the SAD regions really are.
1351 *
1352 * When we know the real size of a SAD region and how many ways it's
1353 * interleaved, we know the individual contribution of each channel to
1354 * TAD is size/ways.
1355 *
1356 * Finally, we have to check whether each channel participates in each SAD
1357 * region.
1358 *
1359 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1360 * much memory the channel uses, we know the DIMM is at least that large.
1361 * (The BIOS might possibly choose not to map all available memory, in which
1362 * case we will underreport the size of the DIMM.)
1363 *
1364 * In theory, we could try to determine the EDC sizes as well, but that would
1365 * only work in flat mode, not in cache mode.
1366 *
1367 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1368 * elements)
1369 */
1370static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1371{
1372 u64 sad_base, sad_size, sad_limit = 0;
1373 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1374 int sad_rule = 0;
1375 int tad_rule = 0;
1376 int intrlv_ways, tad_ways;
1377 u32 first_pkg, pkg;
1378 int i;
1379 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1380 u32 dram_rule, interleave_reg;
1381 u32 mc_route_reg[KNL_MAX_CHAS];
1382 u32 edc_route_reg[KNL_MAX_CHAS];
1383 int edram_only;
1384 char edc_route_string[KNL_MAX_EDCS*2];
1385 char mc_route_string[KNL_MAX_CHANNELS*2];
1386 int cur_reg_start;
1387 int mc;
1388 int channel;
1389 int way;
1390 int participants[KNL_MAX_CHANNELS];
1391 int participant_count = 0;
1392
1393 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1394 mc_sizes[i] = 0;
1395
1396 /* Read the EDC route table in each CHA. */
1397 cur_reg_start = 0;
1398 for (i = 0; i < KNL_MAX_CHAS; i++) {
1399 pci_read_config_dword(pvt->knl.pci_cha[i],
1400 KNL_EDC_ROUTE, &edc_route_reg[i]);
1401
1402 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1403 knl_show_edc_route(edc_route_reg[i-1],
1404 edc_route_string);
1405 if (cur_reg_start == i-1)
1406 edac_dbg(0, "edc route table for CHA %d: %s\n",
1407 cur_reg_start, edc_route_string);
1408 else
1409 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1410 cur_reg_start, i-1, edc_route_string);
1411 cur_reg_start = i;
1412 }
1413 }
1414 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1415 if (cur_reg_start == i-1)
1416 edac_dbg(0, "edc route table for CHA %d: %s\n",
1417 cur_reg_start, edc_route_string);
1418 else
1419 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1420 cur_reg_start, i-1, edc_route_string);
1421
1422 /* Read the MC route table in each CHA. */
1423 cur_reg_start = 0;
1424 for (i = 0; i < KNL_MAX_CHAS; i++) {
1425 pci_read_config_dword(pvt->knl.pci_cha[i],
1426 KNL_MC_ROUTE, &mc_route_reg[i]);
1427
1428 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1429 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1430 if (cur_reg_start == i-1)
1431 edac_dbg(0, "mc route table for CHA %d: %s\n",
1432 cur_reg_start, mc_route_string);
1433 else
1434 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1435 cur_reg_start, i-1, mc_route_string);
1436 cur_reg_start = i;
1437 }
1438 }
1439 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1440 if (cur_reg_start == i-1)
1441 edac_dbg(0, "mc route table for CHA %d: %s\n",
1442 cur_reg_start, mc_route_string);
1443 else
1444 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1445 cur_reg_start, i-1, mc_route_string);
1446
1447 /* Process DRAM rules */
1448 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1449 /* previous limit becomes the new base */
1450 sad_base = sad_limit;
1451
1452 pci_read_config_dword(pvt->pci_sad0,
1453 pvt->info.dram_rule[sad_rule], &dram_rule);
1454
1455 if (!DRAM_RULE_ENABLE(dram_rule))
1456 break;
1457
1458 edram_only = KNL_EDRAM_ONLY(dram_rule);
1459
1460 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1461 sad_size = sad_limit - sad_base;
1462
1463 pci_read_config_dword(pvt->pci_sad0,
1464 pvt->info.interleave_list[sad_rule], &interleave_reg);
1465
1466 /*
1467 * Find out how many ways this dram rule is interleaved.
1468 * We stop when we see the first channel again.
1469 */
1470 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1471 interleave_reg, 0);
1472 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1473 pkg = sad_pkg(pvt->info.interleave_pkg,
1474 interleave_reg, intrlv_ways);
1475
1476 if ((pkg & 0x8) == 0) {
1477 /*
1478 * 0 bit means memory is non-local,
1479 * which KNL doesn't support
1480 */
1481 edac_dbg(0, "Unexpected interleave target %d\n",
1482 pkg);
1483 return -1;
1484 }
1485
1486 if (pkg == first_pkg)
1487 break;
1488 }
1489 if (KNL_MOD3(dram_rule))
1490 intrlv_ways *= 3;
1491
1492 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1493 sad_rule,
1494 sad_base,
1495 sad_limit,
1496 intrlv_ways,
1497 edram_only ? ", EDRAM" : "");
1498
1499 /*
1500 * Find out how big the SAD region really is by iterating
1501 * over TAD tables (SAD regions may contain holes).
1502 * Each memory controller might have a different TAD table, so
1503 * we have to look at both.
1504 *
1505 * Livespace is the memory that's mapped in this TAD table,
1506 * deadspace is the holes (this could be the MMIO hole, or it
1507 * could be memory that's mapped by the other TAD table but
1508 * not this one).
1509 */
1510 for (mc = 0; mc < 2; mc++) {
1511 sad_actual_size[mc] = 0;
1512 tad_livespace = 0;
1513 for (tad_rule = 0;
1514 tad_rule < ARRAY_SIZE(
1515 knl_tad_dram_limit_lo);
1516 tad_rule++) {
1517 if (knl_get_tad(pvt,
1518 tad_rule,
1519 mc,
1520 &tad_deadspace,
1521 &tad_limit,
1522 &tad_ways))
1523 break;
1524
1525 tad_size = (tad_limit+1) -
1526 (tad_livespace + tad_deadspace);
1527 tad_livespace += tad_size;
1528 tad_base = (tad_limit+1) - tad_size;
1529
1530 if (tad_base < sad_base) {
1531 if (tad_limit > sad_base)
1532 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1533 } else if (tad_base < sad_limit) {
1534 if (tad_limit+1 > sad_limit) {
1535 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1536 } else {
1537 /* TAD region is completely inside SAD region */
1538 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1539 tad_rule, tad_base,
1540 tad_limit, tad_size,
1541 mc);
1542 sad_actual_size[mc] += tad_size;
1543 }
1544 }
1545 tad_base = tad_limit+1;
1546 }
1547 }
1548
1549 for (mc = 0; mc < 2; mc++) {
1550 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1551 mc, sad_actual_size[mc], sad_actual_size[mc]);
1552 }
1553
1554 /* Ignore EDRAM rule */
1555 if (edram_only)
1556 continue;
1557
1558 /* Figure out which channels participate in interleave. */
1559 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1560 participants[channel] = 0;
1561
1562 /* For each channel, does at least one CHA have
1563 * this channel mapped to the given target?
1564 */
1565 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1566 for (way = 0; way < intrlv_ways; way++) {
1567 int target;
1568 int cha;
1569
1570 if (KNL_MOD3(dram_rule))
1571 target = way;
1572 else
1573 target = 0x7 & sad_pkg(
1574 pvt->info.interleave_pkg, interleave_reg, way);
1575
1576 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1577 if (knl_get_mc_route(target,
1578 mc_route_reg[cha]) == channel
Hubert Chrzaniuk83bdaad2016-03-07 15:30:45 +01001579 && !participants[channel]) {
Jim Snowd0cdf902015-12-03 10:48:54 +01001580 participant_count++;
1581 participants[channel] = 1;
1582 break;
1583 }
1584 }
1585 }
1586 }
1587
1588 if (participant_count != intrlv_ways)
1589 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1590 participant_count, intrlv_ways);
1591
1592 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1593 mc = knl_channel_mc(channel);
1594 if (participants[channel]) {
1595 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1596 channel,
1597 sad_actual_size[mc]/intrlv_ways,
1598 sad_rule);
1599 mc_sizes[channel] +=
1600 sad_actual_size[mc]/intrlv_ways;
1601 }
1602 }
1603 }
1604
1605 return 0;
1606}
1607
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001608static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001609{
1610 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001611 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001612 unsigned i, j, banks, ranks, rows, cols, npages;
1613 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001614 u32 reg;
1615 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -02001616 enum mem_type mtype;
Jim Snowd0cdf902015-12-03 10:48:54 +01001617 int channels = pvt->info.type == KNIGHTS_LANDING ?
1618 KNL_MAX_CHANNELS : NUM_CHANNELS;
1619 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001620
Tony Luckea5dfb52016-04-14 10:22:02 -07001621 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1622 pci_read_config_dword(pvt->pci_ha0, HASWELL_HASYSDEFEATURE2, &reg);
1623 pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
1624 }
Jim Snowd0cdf902015-12-03 10:48:54 +01001625 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1626 pvt->info.type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001627 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
1628 else
1629 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
1630
Jim Snowd0cdf902015-12-03 10:48:54 +01001631 if (pvt->info.type == KNIGHTS_LANDING)
1632 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1633 else
1634 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001635
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03001636 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba12012-04-29 17:08:39 -03001637 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1638 pvt->sbridge_dev->mc,
1639 pvt->sbridge_dev->node_id,
1640 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001641
Jim Snowd0cdf902015-12-03 10:48:54 +01001642 /* KNL doesn't support mirroring or lockstep,
1643 * and is always closed page
1644 */
1645 if (pvt->info.type == KNIGHTS_LANDING) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001646 mode = EDAC_S4ECD4ED;
Jim Snowd0cdf902015-12-03 10:48:54 +01001647 pvt->is_mirrored = false;
1648
1649 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1650 return -1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001651 } else {
Jim Snowd0cdf902015-12-03 10:48:54 +01001652 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
1653 if (IS_MIRROR_ENABLED(reg)) {
1654 edac_dbg(0, "Memory mirror is enabled\n");
1655 pvt->is_mirrored = true;
1656 } else {
1657 edac_dbg(0, "Memory mirror is disabled\n");
1658 pvt->is_mirrored = false;
1659 }
1660
1661 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
1662 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1663 edac_dbg(0, "Lockstep is enabled\n");
1664 mode = EDAC_S8ECD8ED;
1665 pvt->is_lockstep = true;
1666 } else {
1667 edac_dbg(0, "Lockstep is disabled\n");
1668 mode = EDAC_S4ECD4ED;
1669 pvt->is_lockstep = false;
1670 }
1671 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1672 edac_dbg(0, "address map is on closed page mode\n");
1673 pvt->is_close_pg = true;
1674 } else {
1675 edac_dbg(0, "address map is on open page mode\n");
1676 pvt->is_close_pg = false;
1677 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001678 }
1679
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001680 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001681 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001682 edac_dbg(0, "Memory is registered\n");
1683 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -07001684 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001685 else
1686 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001687
Tony Luckfec53af2014-12-02 09:41:58 -08001688 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001689 banks = 16;
1690 else
1691 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001692
Jim Snowd0cdf902015-12-03 10:48:54 +01001693 for (i = 0; i < channels; i++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001694 u32 mtr;
1695
Jim Snowd0cdf902015-12-03 10:48:54 +01001696 int max_dimms_per_channel;
1697
1698 if (pvt->info.type == KNIGHTS_LANDING) {
1699 max_dimms_per_channel = 1;
1700 if (!pvt->knl.pci_channel[i])
1701 continue;
1702 } else {
1703 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1704 if (!pvt->pci_tad[i])
1705 continue;
1706 }
1707
1708 for (j = 0; j < max_dimms_per_channel; j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001709 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1710 i, j, 0);
Jim Snowd0cdf902015-12-03 10:48:54 +01001711 if (pvt->info.type == KNIGHTS_LANDING) {
1712 pci_read_config_dword(pvt->knl.pci_channel[i],
1713 knl_mtr_reg, &mtr);
1714 } else {
1715 pci_read_config_dword(pvt->pci_tad[i],
1716 mtr_regs[j], &mtr);
1717 }
Joe Perches956b9ba12012-04-29 17:08:39 -03001718 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001719 if (IS_DIMM_PRESENT(mtr)) {
1720 pvt->channel[i].dimms++;
1721
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001722 ranks = numrank(pvt->info.type, mtr);
Jim Snowd0cdf902015-12-03 10:48:54 +01001723
1724 if (pvt->info.type == KNIGHTS_LANDING) {
1725 /* For DDR4, this is fixed. */
1726 cols = 1 << 10;
1727 rows = knl_mc_sizes[i] /
1728 ((u64) cols * ranks * banks * 8);
1729 } else {
1730 rows = numrow(mtr);
1731 cols = numcol(mtr);
1732 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001733
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001734 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001735 npages = MiB_TO_PAGES(size);
1736
Tony Luck7d375bf2015-05-18 17:50:42 -03001737 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1738 pvt->sbridge_dev->mc, i/4, i%4, j,
Joe Perches956b9ba12012-04-29 17:08:39 -03001739 size, npages,
1740 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001741
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -03001742 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001743 dimm->grain = 32;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04001744 dimm->dtype = pvt->info.get_width(pvt, mtr);
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001745 dimm->mtype = mtype;
1746 dimm->edac_mode = mode;
1747 snprintf(dimm->label, sizeof(dimm->label),
Tony Luck7d375bf2015-05-18 17:50:42 -03001748 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1749 pvt->sbridge_dev->source_id, i/4, i%4, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001750 }
1751 }
1752 }
1753
1754 return 0;
1755}
1756
1757static void get_memory_layout(const struct mem_ctl_info *mci)
1758{
1759 struct sbridge_pvt *pvt = mci->pvt_info;
1760 int i, j, k, n_sads, n_tads, sad_interl;
1761 u32 reg;
1762 u64 limit, prv = 0;
1763 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +01001764 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001765 u32 rir_way;
1766
1767 /*
1768 * Step 1) Get TOLM/TOHM ranges
1769 */
1770
Aristeu Rozanskifb79a502013-10-30 13:26:57 -03001771 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001772 tmp_mb = (1 + pvt->tolm) >> 20;
1773
Jim Snow8c009102014-11-18 14:51:09 +01001774 gb = div_u64_rem(tmp_mb, 1024, &mb);
1775 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1776 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001777
1778 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -03001779 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001780 tmp_mb = (1 + pvt->tohm) >> 20;
1781
Jim Snow8c009102014-11-18 14:51:09 +01001782 gb = div_u64_rem(tmp_mb, 1024, &mb);
1783 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1784 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001785
1786 /*
1787 * Step 2) Get SAD range and SAD Interleave list
1788 * TAD registers contain the interleave wayness. However, it
1789 * seems simpler to just discover it indirectly, with the
1790 * algorithm bellow.
1791 */
1792 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001793 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001794 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001795 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001796 &reg);
Jim Snowc59f9c02015-12-03 10:48:52 +01001797 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001798
1799 if (!DRAM_RULE_ENABLE(reg))
1800 continue;
1801
1802 if (limit <= prv)
1803 break;
1804
1805 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001806 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001807 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1808 n_sads,
Jim Snowc59f9c02015-12-03 10:48:52 +01001809 show_dram_attr(pvt->info.dram_attr(reg)),
Jim Snow8c009102014-11-18 14:51:09 +01001810 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001811 ((u64)tmp_mb) << 20L,
Jim Snowc59f9c02015-12-03 10:48:52 +01001812 pvt->info.show_interleave_mode(reg),
Joe Perches956b9ba12012-04-29 17:08:39 -03001813 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001814 prv = limit;
1815
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001816 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001817 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001818 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001819 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001820 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1821 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001822 break;
1823
Joe Perches956b9ba12012-04-29 17:08:39 -03001824 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001825 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001826 }
1827 }
1828
Jim Snowd0cdf902015-12-03 10:48:54 +01001829 if (pvt->info.type == KNIGHTS_LANDING)
1830 return;
1831
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001832 /*
1833 * Step 3) Get TAD range
1834 */
1835 prv = 0;
1836 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1837 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1838 &reg);
1839 limit = TAD_LIMIT(reg);
1840 if (limit <= prv)
1841 break;
1842 tmp_mb = (limit + 1) >> 20;
1843
Jim Snow8c009102014-11-18 14:51:09 +01001844 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001845 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 +01001846 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001847 ((u64)tmp_mb) << 20L,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08001848 (u32)(1 << TAD_SOCK(reg)),
1849 (u32)TAD_CH(reg) + 1,
Joe Perches956b9ba12012-04-29 17:08:39 -03001850 (u32)TAD_TGT0(reg),
1851 (u32)TAD_TGT1(reg),
1852 (u32)TAD_TGT2(reg),
1853 (u32)TAD_TGT3(reg),
1854 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001855 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001856 }
1857
1858 /*
1859 * Step 4) Get TAD offsets, per each channel
1860 */
1861 for (i = 0; i < NUM_CHANNELS; i++) {
1862 if (!pvt->channel[i].dimms)
1863 continue;
1864 for (j = 0; j < n_tads; j++) {
1865 pci_read_config_dword(pvt->pci_tad[i],
1866 tad_ch_nilv_offset[j],
1867 &reg);
1868 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001869 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001870 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1871 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001872 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001873 ((u64)tmp_mb) << 20L,
1874 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001875 }
1876 }
1877
1878 /*
1879 * Step 6) Get RIR Wayness/Limit, per each channel
1880 */
1881 for (i = 0; i < NUM_CHANNELS; i++) {
1882 if (!pvt->channel[i].dimms)
1883 continue;
1884 for (j = 0; j < MAX_RIR_RANGES; j++) {
1885 pci_read_config_dword(pvt->pci_tad[i],
1886 rir_way_limit[j],
1887 &reg);
1888
1889 if (!IS_RIR_VALID(reg))
1890 continue;
1891
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001892 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001893 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001894 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001895 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1896 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001897 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001898 ((u64)tmp_mb) << 20L,
1899 rir_way,
1900 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001901
1902 for (k = 0; k < rir_way; k++) {
1903 pci_read_config_dword(pvt->pci_tad[i],
1904 rir_offset[j][k],
1905 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07001906 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001907
Jim Snow8c009102014-11-18 14:51:09 +01001908 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03001909 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1910 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001911 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03001912 ((u64)tmp_mb) << 20L,
Tony Luckc7103f62016-05-31 11:50:28 -07001913 (u32)RIR_RNK_TGT(pvt->info.type, reg),
Joe Perches956b9ba12012-04-29 17:08:39 -03001914 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001915 }
1916 }
1917 }
1918}
1919
Rashika Kheria8112c0c2013-12-14 19:32:09 +05301920static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001921{
1922 struct sbridge_dev *sbridge_dev;
1923
1924 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1925 if (sbridge_dev->node_id == node_id)
1926 return sbridge_dev->mci;
1927 }
1928 return NULL;
1929}
1930
1931static int get_memory_error_data(struct mem_ctl_info *mci,
1932 u64 addr,
Tony Luck7d375bf2015-05-18 17:50:42 -03001933 u8 *socket, u8 *ha,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001934 long *channel_mask,
1935 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001936 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001937{
1938 struct mem_ctl_info *new_mci;
1939 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001940 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001941 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001942 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001943 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001944 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001945 u32 reg, dram_rule;
Tony Luck7d375bf2015-05-18 17:50:42 -03001946 u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001947 u32 tad_offset;
1948 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001949 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001950 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001951
1952
1953 /*
1954 * Step 0) Check if the address is at special memory ranges
1955 * The check bellow is probably enough to fill all cases where
1956 * the error is not inside a memory, except for the legacy
1957 * range (e. g. VGA addresses). It is unlikely, however, that the
1958 * memory controller would generate an error on that range.
1959 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001960 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001961 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001962 return -EINVAL;
1963 }
1964 if (addr >= (u64)pvt->tohm) {
1965 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001966 return -EINVAL;
1967 }
1968
1969 /*
1970 * Step 1) Get socket
1971 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001972 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1973 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001974 &reg);
1975
1976 if (!DRAM_RULE_ENABLE(reg))
1977 continue;
1978
Jim Snowc59f9c02015-12-03 10:48:52 +01001979 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001980 if (limit <= prv) {
1981 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001982 return -EINVAL;
1983 }
1984 if (addr <= limit)
1985 break;
1986 prv = limit;
1987 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001988 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001989 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001990 return -EINVAL;
1991 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001992 dram_rule = reg;
Jim Snowc59f9c02015-12-03 10:48:52 +01001993 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1994 interleave_mode = pvt->info.interleave_mode(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001995
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001996 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001997 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001998
1999 if (pvt->info.type == SANDY_BRIDGE) {
2000 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
2001 for (sad_way = 0; sad_way < 8; sad_way++) {
2002 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
2003 if (sad_way > 0 && sad_interl == pkg)
2004 break;
2005 sad_interleave[sad_way] = pkg;
2006 edac_dbg(0, "SAD interleave #%d: %d\n",
2007 sad_way, sad_interleave[sad_way]);
2008 }
2009 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
2010 pvt->sbridge_dev->mc,
2011 n_sads,
2012 addr,
2013 limit,
2014 sad_way + 7,
2015 !interleave_mode ? "" : "XOR[18:16]");
2016 if (interleave_mode)
2017 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
2018 else
2019 idx = (addr >> 6) & 7;
2020 switch (sad_way) {
2021 case 1:
2022 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002023 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002024 case 2:
2025 idx = idx & 1;
2026 break;
2027 case 4:
2028 idx = idx & 3;
2029 break;
2030 case 8:
2031 break;
2032 default:
2033 sprintf(msg, "Can't discover socket interleave");
2034 return -EINVAL;
2035 }
2036 *socket = sad_interleave[idx];
2037 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2038 idx, sad_way, *socket);
Tony Luck1f395812014-12-02 09:27:30 -08002039 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002040 int bits, a7mode = A7MODE(dram_rule);
2041
2042 if (a7mode) {
2043 /* A7 mode swaps P9 with P6 */
2044 bits = GET_BITFIELD(addr, 7, 8) << 1;
2045 bits |= GET_BITFIELD(addr, 9, 9);
2046 } else
Tony Luckbb89e712015-05-18 17:39:06 -03002047 bits = GET_BITFIELD(addr, 6, 8);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002048
Tony Luckbb89e712015-05-18 17:39:06 -03002049 if (interleave_mode == 0) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002050 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2051 idx = GET_BITFIELD(addr, 16, 18);
2052 idx ^= bits;
2053 } else
2054 idx = bits;
2055
2056 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2057 *socket = sad_pkg_socket(pkg);
2058 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002059 if (sad_ha)
2060 ch_add = 4;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002061
2062 if (a7mode) {
2063 /* MCChanShiftUpEnable */
2064 pci_read_config_dword(pvt->pci_ha0,
2065 HASWELL_HASYSDEFEATURE2, &reg);
2066 shiftup = GET_BITFIELD(reg, 22, 22);
2067 }
2068
2069 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2070 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002071 } else {
2072 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002073 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002074 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2075 *socket = sad_pkg_socket(pkg);
2076 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002077 if (sad_ha)
2078 ch_add = 4;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002079 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2080 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002081 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002082
Tony Luck7d375bf2015-05-18 17:50:42 -03002083 *ha = sad_ha;
2084
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002085 /*
2086 * Move to the proper node structure, in order to access the
2087 * right PCI registers
2088 */
2089 new_mci = get_mci_for_node_id(*socket);
2090 if (!new_mci) {
2091 sprintf(msg, "Struct for socket #%u wasn't initialized",
2092 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002093 return -EINVAL;
2094 }
2095 mci = new_mci;
2096 pvt = mci->pvt_info;
2097
2098 /*
2099 * Step 2) Get memory channel
2100 */
2101 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002102 if (pvt->info.type == SANDY_BRIDGE)
2103 pci_ha = pvt->pci_ha0;
2104 else {
2105 if (sad_ha)
2106 pci_ha = pvt->pci_ha1;
2107 else
2108 pci_ha = pvt->pci_ha0;
2109 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002110 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002111 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002112 limit = TAD_LIMIT(reg);
2113 if (limit <= prv) {
2114 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002115 return -EINVAL;
2116 }
2117 if (addr <= limit)
2118 break;
2119 prv = limit;
2120 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002121 if (n_tads == MAX_TAD) {
2122 sprintf(msg, "Can't discover the memory channel");
2123 return -EINVAL;
2124 }
2125
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002126 ch_way = TAD_CH(reg) + 1;
Tony Luckff15e952016-04-14 10:21:52 -07002127 sck_way = TAD_SOCK(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002128
2129 if (ch_way == 3)
2130 idx = addr >> 6;
Tony Luckea5dfb52016-04-14 10:22:02 -07002131 else {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002132 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Tony Luckea5dfb52016-04-14 10:22:02 -07002133 if (pvt->is_chan_hash)
2134 idx = haswell_chan_hash(idx, addr);
2135 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002136 idx = idx % ch_way;
2137
2138 /*
2139 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2140 */
2141 switch (idx) {
2142 case 0:
2143 base_ch = TAD_TGT0(reg);
2144 break;
2145 case 1:
2146 base_ch = TAD_TGT1(reg);
2147 break;
2148 case 2:
2149 base_ch = TAD_TGT2(reg);
2150 break;
2151 case 3:
2152 base_ch = TAD_TGT3(reg);
2153 break;
2154 default:
2155 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002156 return -EINVAL;
2157 }
2158 *channel_mask = 1 << base_ch;
2159
Tony Luck7d375bf2015-05-18 17:50:42 -03002160 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002161 tad_ch_nilv_offset[n_tads],
2162 &tad_offset);
2163
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002164 if (pvt->is_mirrored) {
2165 *channel_mask |= 1 << ((base_ch + 2) % 4);
2166 switch(ch_way) {
2167 case 2:
2168 case 4:
Tony Luckff15e952016-04-14 10:21:52 -07002169 sck_xch = (1 << sck_way) * (ch_way >> 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002170 break;
2171 default:
2172 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002173 return -EINVAL;
2174 }
2175 } else
2176 sck_xch = (1 << sck_way) * ch_way;
2177
2178 if (pvt->is_lockstep)
2179 *channel_mask |= 1 << ((base_ch + 1) % 4);
2180
2181 offset = TAD_OFFSET(tad_offset);
2182
Joe Perches956b9ba12012-04-29 17:08:39 -03002183 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",
2184 n_tads,
2185 addr,
2186 limit,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002187 sck_way,
Joe Perches956b9ba12012-04-29 17:08:39 -03002188 ch_way,
2189 offset,
2190 idx,
2191 base_ch,
2192 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002193
2194 /* Calculate channel address */
2195 /* Remove the TAD offset */
2196
2197 if (offset > addr) {
2198 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2199 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002200 return -EINVAL;
2201 }
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002202
2203 ch_addr = addr - offset;
2204 ch_addr >>= (6 + shiftup);
Tony Luckff15e952016-04-14 10:21:52 -07002205 ch_addr /= sck_xch;
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002206 ch_addr <<= (6 + shiftup);
2207 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002208
2209 /*
2210 * Step 3) Decode rank
2211 */
2212 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
Tony Luck7d375bf2015-05-18 17:50:42 -03002213 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002214 rir_way_limit[n_rir],
2215 &reg);
2216
2217 if (!IS_RIR_VALID(reg))
2218 continue;
2219
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002220 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01002221 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba12012-04-29 17:08:39 -03002222 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2223 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01002224 gb, (mb*1000)/1024,
Joe Perches956b9ba12012-04-29 17:08:39 -03002225 limit,
2226 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002227 if (ch_addr <= limit)
2228 break;
2229 }
2230 if (n_rir == MAX_RIR_RANGES) {
2231 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2232 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002233 return -EINVAL;
2234 }
2235 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002236
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002237 if (pvt->is_close_pg)
2238 idx = (ch_addr >> 6);
2239 else
2240 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2241 idx %= 1 << rir_way;
2242
Tony Luck7d375bf2015-05-18 17:50:42 -03002243 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002244 rir_offset[n_rir][idx],
2245 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07002246 *rank = RIR_RNK_TGT(pvt->info.type, reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002247
Joe Perches956b9ba12012-04-29 17:08:39 -03002248 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2249 n_rir,
2250 ch_addr,
2251 limit,
2252 rir_way,
2253 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002254
2255 return 0;
2256}
2257
2258/****************************************************************************
2259 Device initialization routines: put/get, init/exit
2260 ****************************************************************************/
2261
2262/*
2263 * sbridge_put_all_devices 'put' all the devices that we have
2264 * reserved via 'get'
2265 */
2266static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2267{
2268 int i;
2269
Joe Perches956b9ba12012-04-29 17:08:39 -03002270 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002271 for (i = 0; i < sbridge_dev->n_devs; i++) {
2272 struct pci_dev *pdev = sbridge_dev->pdev[i];
2273 if (!pdev)
2274 continue;
Joe Perches956b9ba12012-04-29 17:08:39 -03002275 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2276 pdev->bus->number,
2277 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002278 pci_dev_put(pdev);
2279 }
2280}
2281
2282static void sbridge_put_all_devices(void)
2283{
2284 struct sbridge_dev *sbridge_dev, *tmp;
2285
2286 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2287 sbridge_put_devices(sbridge_dev);
2288 free_sbridge_dev(sbridge_dev);
2289 }
2290}
2291
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002292static int sbridge_get_onedevice(struct pci_dev **prev,
2293 u8 *num_mc,
2294 const struct pci_id_table *table,
Jim Snowc1979ba2015-12-03 10:48:53 +01002295 const unsigned devno,
2296 const int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002297{
2298 struct sbridge_dev *sbridge_dev;
2299 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002300 struct pci_dev *pdev = NULL;
2301 u8 bus = 0;
2302
Jiang Liuec5a0b32014-02-17 13:10:23 +08002303 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002304 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002305 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2306
2307 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2308 dev_descr->dev_id, *prev);
2309
2310 if (!pdev) {
2311 if (*prev) {
2312 *prev = pdev;
2313 return 0;
2314 }
2315
2316 if (dev_descr->optional)
2317 return 0;
2318
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002319 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002320 if (devno == 0)
2321 return -ENODEV;
2322
2323 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002324 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002325 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2326
2327 /* End of list, leave */
2328 return -ENODEV;
2329 }
2330 bus = pdev->bus->number;
2331
Jim Snowc1979ba2015-12-03 10:48:53 +01002332 sbridge_dev = get_sbridge_dev(bus, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002333 if (!sbridge_dev) {
2334 sbridge_dev = alloc_sbridge_dev(bus, table);
2335 if (!sbridge_dev) {
2336 pci_dev_put(pdev);
2337 return -ENOMEM;
2338 }
2339 (*num_mc)++;
2340 }
2341
2342 if (sbridge_dev->pdev[devno]) {
2343 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002344 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002345 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2346 pci_dev_put(pdev);
2347 return -ENODEV;
2348 }
2349
2350 sbridge_dev->pdev[devno] = pdev;
2351
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002352 /* Be sure that the device is enabled */
2353 if (unlikely(pci_enable_device(pdev) < 0)) {
2354 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002355 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002356 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2357 return -ENODEV;
2358 }
2359
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002360 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba12012-04-29 17:08:39 -03002361 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002362
2363 /*
2364 * As stated on drivers/pci/search.c, the reference count for
2365 * @from is always decremented if it is not %NULL. So, as we need
2366 * to get all devices up to null, we need to do a get for the device
2367 */
2368 pci_dev_get(pdev);
2369
2370 *prev = pdev;
2371
2372 return 0;
2373}
2374
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002375/*
2376 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002377 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002378 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03002379 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002380 * @table: model specific table
2381 *
2382 * returns 0 in case of success or error code
2383 */
Tony Luck0ba169ac2016-07-14 15:38:43 -07002384static int sbridge_get_all_devices(u8 *num_mc,
2385 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002386{
2387 int i, rc;
2388 struct pci_dev *pdev = NULL;
Tony Luck0ba169ac2016-07-14 15:38:43 -07002389 int allow_dups = 0;
2390 int multi_bus = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002391
Tony Luck0ba169ac2016-07-14 15:38:43 -07002392 if (table->type == KNIGHTS_LANDING)
2393 allow_dups = multi_bus = 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002394 while (table && table->descr) {
2395 for (i = 0; i < table->n_devs; i++) {
Jim Snowc1979ba2015-12-03 10:48:53 +01002396 if (!allow_dups || i == 0 ||
2397 table->descr[i].dev_id !=
2398 table->descr[i-1].dev_id) {
2399 pdev = NULL;
2400 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002401 do {
2402 rc = sbridge_get_onedevice(&pdev, num_mc,
Jim Snowc1979ba2015-12-03 10:48:53 +01002403 table, i, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002404 if (rc < 0) {
2405 if (i == 0) {
2406 i = table->n_devs;
2407 break;
2408 }
2409 sbridge_put_all_devices();
2410 return -ENODEV;
2411 }
Jim Snowc1979ba2015-12-03 10:48:53 +01002412 } while (pdev && !allow_dups);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002413 }
2414 table++;
2415 }
2416
2417 return 0;
2418}
2419
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03002420static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2421 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002422{
2423 struct sbridge_pvt *pvt = mci->pvt_info;
2424 struct pci_dev *pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002425 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002426 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002427
2428 for (i = 0; i < sbridge_dev->n_devs; i++) {
2429 pdev = sbridge_dev->pdev[i];
2430 if (!pdev)
2431 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002432
2433 switch (pdev->device) {
2434 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2435 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002436 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002437 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2438 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002439 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002440 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2441 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002442 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002443 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2444 pvt->pci_ha0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002445 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002446 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2447 pvt->pci_ta = pdev;
2448 break;
2449 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2450 pvt->pci_ras = pdev;
2451 break;
2452 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2453 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2454 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2455 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2456 {
2457 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
2458 pvt->pci_tad[id] = pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002459 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002460 }
2461 break;
2462 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2463 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002464 break;
2465 default:
2466 goto error;
2467 }
2468
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002469 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2470 pdev->vendor, pdev->device,
Joe Perches956b9ba12012-04-29 17:08:39 -03002471 sbridge_dev->bus,
Joe Perches956b9ba12012-04-29 17:08:39 -03002472 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002473 }
2474
2475 /* Check if everything were registered */
2476 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002477 !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002478 goto enodev;
2479
Seth Jennings2900ea62015-08-05 13:16:01 -05002480 if (saw_chan_mask != 0x0f)
2481 goto enodev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002482 return 0;
2483
2484enodev:
2485 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2486 return -ENODEV;
2487
2488error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002489 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2490 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002491 return -EINVAL;
2492}
2493
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002494static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2495 struct sbridge_dev *sbridge_dev)
2496{
2497 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002498 struct pci_dev *pdev;
2499 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002500 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002501
2502 for (i = 0; i < sbridge_dev->n_devs; i++) {
2503 pdev = sbridge_dev->pdev[i];
2504 if (!pdev)
2505 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002506
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002507 switch (pdev->device) {
2508 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
2509 pvt->pci_ha0 = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002510 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002511 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2512 pvt->pci_ta = pdev;
2513 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
2514 pvt->pci_ras = pdev;
2515 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002516 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2517 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002518 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2519 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002520 {
2521 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
2522 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002523 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002524 }
2525 break;
2526 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2527 pvt->pci_ddrio = pdev;
2528 break;
2529 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
Tony Luck7d375bf2015-05-18 17:50:42 -03002530 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002531 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002532 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2533 pvt->pci_sad0 = pdev;
2534 break;
2535 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2536 pvt->pci_br0 = pdev;
2537 break;
2538 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2539 pvt->pci_br1 = pdev;
2540 break;
2541 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
2542 pvt->pci_ha1 = pdev;
2543 break;
2544 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2545 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002546 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2547 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002548 {
Tony Luck7d375bf2015-05-18 17:50:42 -03002549 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002550 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002551 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002552 }
2553 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002554 default:
2555 goto error;
2556 }
2557
2558 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2559 sbridge_dev->bus,
2560 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2561 pdev);
2562 }
2563
2564 /* Check if everything were registered */
2565 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002566 !pvt->pci_br1 || !pvt->pci_ras || !pvt->pci_ta)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002567 goto enodev;
2568
Tony Luck7d375bf2015-05-18 17:50:42 -03002569 if (saw_chan_mask != 0x0f && /* -EN */
2570 saw_chan_mask != 0x33 && /* -EP */
2571 saw_chan_mask != 0xff) /* -EX */
2572 goto enodev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002573 return 0;
2574
2575enodev:
2576 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2577 return -ENODEV;
2578
2579error:
2580 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002581 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2582 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002583 return -EINVAL;
2584}
2585
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002586static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2587 struct sbridge_dev *sbridge_dev)
2588{
2589 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002590 struct pci_dev *pdev;
2591 u8 saw_chan_mask = 0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002592 int i;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002593
2594 /* there's only one device per system; not tied to any bus */
2595 if (pvt->info.pci_vtd == NULL)
2596 /* result will be checked later */
2597 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2598 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2599 NULL);
2600
2601 for (i = 0; i < sbridge_dev->n_devs; i++) {
2602 pdev = sbridge_dev->pdev[i];
2603 if (!pdev)
2604 continue;
2605
2606 switch (pdev->device) {
2607 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2608 pvt->pci_sad0 = pdev;
2609 break;
2610 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2611 pvt->pci_sad1 = pdev;
2612 break;
2613 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2614 pvt->pci_ha0 = pdev;
2615 break;
2616 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2617 pvt->pci_ta = pdev;
2618 break;
2619 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
2620 pvt->pci_ras = pdev;
2621 break;
2622 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002623 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002624 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002625 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
Tony Luck7d375bf2015-05-18 17:50:42 -03002626 {
2627 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
2628
2629 pvt->pci_tad[id] = pdev;
2630 saw_chan_mask |= 1 << id;
2631 }
2632 break;
2633 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2634 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2635 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2636 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2637 {
2638 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
2639
2640 pvt->pci_tad[id] = pdev;
2641 saw_chan_mask |= 1 << id;
2642 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002643 break;
2644 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
Aristeu Rozanski71793852015-06-12 09:44:52 -04002645 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2646 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2647 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2648 if (!pvt->pci_ddrio)
2649 pvt->pci_ddrio = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002650 break;
2651 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
2652 pvt->pci_ha1 = pdev;
2653 break;
2654 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
2655 pvt->pci_ha1_ta = pdev;
2656 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002657 default:
2658 break;
2659 }
2660
2661 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2662 sbridge_dev->bus,
2663 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2664 pdev);
2665 }
2666
2667 /* Check if everything were registered */
2668 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2669 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2670 goto enodev;
2671
Tony Luck7d375bf2015-05-18 17:50:42 -03002672 if (saw_chan_mask != 0x0f && /* -EN */
2673 saw_chan_mask != 0x33 && /* -EP */
2674 saw_chan_mask != 0xff) /* -EX */
2675 goto enodev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002676 return 0;
2677
2678enodev:
2679 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2680 return -ENODEV;
2681}
2682
Tony Luck1f395812014-12-02 09:27:30 -08002683static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2684 struct sbridge_dev *sbridge_dev)
2685{
2686 struct sbridge_pvt *pvt = mci->pvt_info;
2687 struct pci_dev *pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002688 u8 saw_chan_mask = 0;
Tony Luck1f395812014-12-02 09:27:30 -08002689 int i;
2690
2691 /* there's only one device per system; not tied to any bus */
2692 if (pvt->info.pci_vtd == NULL)
2693 /* result will be checked later */
2694 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2695 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2696 NULL);
2697
2698 for (i = 0; i < sbridge_dev->n_devs; i++) {
2699 pdev = sbridge_dev->pdev[i];
2700 if (!pdev)
2701 continue;
2702
2703 switch (pdev->device) {
2704 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2705 pvt->pci_sad0 = pdev;
2706 break;
2707 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2708 pvt->pci_sad1 = pdev;
2709 break;
2710 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2711 pvt->pci_ha0 = pdev;
2712 break;
2713 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2714 pvt->pci_ta = pdev;
2715 break;
2716 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
2717 pvt->pci_ras = pdev;
2718 break;
2719 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
Tony Luck1f395812014-12-02 09:27:30 -08002720 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
Tony Luck1f395812014-12-02 09:27:30 -08002721 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
Tony Luck1f395812014-12-02 09:27:30 -08002722 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
Tony Luckfa2ce642015-05-20 19:10:35 -03002723 {
2724 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
2725 pvt->pci_tad[id] = pdev;
2726 saw_chan_mask |= 1 << id;
2727 }
2728 break;
2729 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2730 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2731 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2732 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2733 {
2734 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
2735 pvt->pci_tad[id] = pdev;
2736 saw_chan_mask |= 1 << id;
2737 }
Tony Luck1f395812014-12-02 09:27:30 -08002738 break;
2739 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2740 pvt->pci_ddrio = pdev;
2741 break;
Tony Luckfa2ce642015-05-20 19:10:35 -03002742 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2743 pvt->pci_ha1 = pdev;
2744 break;
2745 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2746 pvt->pci_ha1_ta = pdev;
2747 break;
Tony Luck1f395812014-12-02 09:27:30 -08002748 default:
2749 break;
2750 }
2751
2752 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2753 sbridge_dev->bus,
2754 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2755 pdev);
2756 }
2757
2758 /* Check if everything were registered */
2759 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2760 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2761 goto enodev;
2762
Tony Luckfa2ce642015-05-20 19:10:35 -03002763 if (saw_chan_mask != 0x0f && /* -EN */
2764 saw_chan_mask != 0x33 && /* -EP */
2765 saw_chan_mask != 0xff) /* -EX */
2766 goto enodev;
Tony Luck1f395812014-12-02 09:27:30 -08002767 return 0;
2768
2769enodev:
2770 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2771 return -ENODEV;
2772}
2773
Jim Snowd0cdf902015-12-03 10:48:54 +01002774static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2775 struct sbridge_dev *sbridge_dev)
2776{
2777 struct sbridge_pvt *pvt = mci->pvt_info;
2778 struct pci_dev *pdev;
2779 int dev, func;
2780
2781 int i;
2782 int devidx;
2783
2784 for (i = 0; i < sbridge_dev->n_devs; i++) {
2785 pdev = sbridge_dev->pdev[i];
2786 if (!pdev)
2787 continue;
2788
2789 /* Extract PCI device and function. */
2790 dev = (pdev->devfn >> 3) & 0x1f;
2791 func = pdev->devfn & 0x7;
2792
2793 switch (pdev->device) {
2794 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2795 if (dev == 8)
2796 pvt->knl.pci_mc0 = pdev;
2797 else if (dev == 9)
2798 pvt->knl.pci_mc1 = pdev;
2799 else {
2800 sbridge_printk(KERN_ERR,
2801 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2802 dev, func);
2803 continue;
2804 }
2805 break;
2806
2807 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2808 pvt->pci_sad0 = pdev;
2809 break;
2810
2811 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2812 pvt->pci_sad1 = pdev;
2813 break;
2814
2815 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2816 /* There are one of these per tile, and range from
2817 * 1.14.0 to 1.18.5.
2818 */
2819 devidx = ((dev-14)*8)+func;
2820
2821 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2822 sbridge_printk(KERN_ERR,
2823 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2824 dev, func);
2825 continue;
2826 }
2827
2828 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2829
2830 pvt->knl.pci_cha[devidx] = pdev;
2831 break;
2832
2833 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL:
2834 devidx = -1;
2835
2836 /*
2837 * MC0 channels 0-2 are device 9 function 2-4,
2838 * MC1 channels 3-5 are device 8 function 2-4.
2839 */
2840
2841 if (dev == 9)
2842 devidx = func-2;
2843 else if (dev == 8)
2844 devidx = 3 + (func-2);
2845
2846 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2847 sbridge_printk(KERN_ERR,
2848 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2849 dev, func);
2850 continue;
2851 }
2852
2853 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2854 pvt->knl.pci_channel[devidx] = pdev;
2855 break;
2856
2857 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2858 pvt->knl.pci_mc_info = pdev;
2859 break;
2860
2861 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2862 pvt->pci_ta = pdev;
2863 break;
2864
2865 default:
2866 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2867 pdev->device);
2868 break;
2869 }
2870 }
2871
2872 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2873 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2874 !pvt->pci_ta) {
2875 goto enodev;
2876 }
2877
2878 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2879 if (!pvt->knl.pci_channel[i]) {
2880 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2881 goto enodev;
2882 }
2883 }
2884
2885 for (i = 0; i < KNL_MAX_CHAS; i++) {
2886 if (!pvt->knl.pci_cha[i]) {
2887 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2888 goto enodev;
2889 }
2890 }
2891
2892 return 0;
2893
2894enodev:
2895 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2896 return -ENODEV;
2897}
2898
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002899/****************************************************************************
2900 Error check routines
2901 ****************************************************************************/
2902
2903/*
2904 * While Sandy Bridge has error count registers, SMI BIOS read values from
2905 * and resets the counters. So, they are not reliable for the OS to read
2906 * from them. So, we have no option but to just trust on whatever MCE is
2907 * telling us about the errors.
2908 */
2909static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2910 const struct mce *m)
2911{
2912 struct mem_ctl_info *new_mci;
2913 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002914 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002915 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002916 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2917 bool overflow = GET_BITFIELD(m->status, 62, 62);
2918 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002919 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002920 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2921 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2922 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2923 u32 channel = GET_BITFIELD(m->status, 0, 3);
2924 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2925 long channel_mask, first_channel;
Tony Luck7d375bf2015-05-18 17:50:42 -03002926 u8 rank, socket, ha;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002927 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002928 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002929
Tony Luckfa2ce642015-05-20 19:10:35 -03002930 if (pvt->info.type != SANDY_BRIDGE)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002931 recoverable = true;
2932 else
2933 recoverable = GET_BITFIELD(m->status, 56, 56);
2934
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002935 if (uncorrected_error) {
2936 if (ripv) {
2937 type = "FATAL";
2938 tp_event = HW_EVENT_ERR_FATAL;
2939 } else {
2940 type = "NON_FATAL";
2941 tp_event = HW_EVENT_ERR_UNCORRECTED;
2942 }
2943 } else {
2944 type = "CORRECTED";
2945 tp_event = HW_EVENT_ERR_CORRECTED;
2946 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002947
2948 /*
David Mackey15ed1032012-04-17 11:30:52 -07002949 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002950 * memory errors should fit in this mask:
2951 * 000f 0000 1mmm cccc (binary)
2952 * where:
2953 * f = Correction Report Filtering Bit. If 1, subsequent errors
2954 * won't be shown
2955 * mmm = error type
2956 * cccc = channel
2957 * If the mask doesn't match, report an error to the parsing logic
2958 */
2959 if (! ((errcode & 0xef80) == 0x80)) {
2960 optype = "Can't parse: it is not a mem";
2961 } else {
2962 switch (optypenum) {
2963 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002964 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002965 break;
2966 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002967 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002968 break;
2969 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002970 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002971 break;
2972 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002973 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002974 break;
2975 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002976 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002977 break;
2978 default:
2979 optype = "reserved";
2980 break;
2981 }
2982 }
2983
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03002984 /* Only decode errors with an valid address (ADDRV) */
2985 if (!GET_BITFIELD(m->status, 58, 58))
2986 return;
2987
Jim Snowd0cdf902015-12-03 10:48:54 +01002988 if (pvt->info.type == KNIGHTS_LANDING) {
2989 if (channel == 14) {
2990 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2991 overflow ? " OVERFLOW" : "",
2992 (uncorrected_error && recoverable)
2993 ? " recoverable" : "",
2994 mscod, errcode,
2995 m->bank);
2996 } else {
2997 char A = *("A");
2998
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02002999 /*
3000 * Reported channel is in range 0-2, so we can't map it
3001 * back to mc. To figure out mc we check machine check
3002 * bank register that reported this error.
3003 * bank15 means mc0 and bank16 means mc1.
3004 */
3005 channel = knl_channel_remap(m->bank == 16, channel);
Jim Snowd0cdf902015-12-03 10:48:54 +01003006 channel_mask = 1 << channel;
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02003007
Jim Snowd0cdf902015-12-03 10:48:54 +01003008 snprintf(msg, sizeof(msg),
3009 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
3010 overflow ? " OVERFLOW" : "",
3011 (uncorrected_error && recoverable)
3012 ? " recoverable" : " ",
3013 mscod, errcode, channel, A + channel);
3014 edac_mc_handle_error(tp_event, mci, core_err_cnt,
3015 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
3016 channel, 0, -1,
3017 optype, msg);
3018 }
3019 return;
3020 } else {
3021 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
3022 &channel_mask, &rank, &area_type, msg);
3023 }
3024
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003025 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003026 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003027 new_mci = get_mci_for_node_id(socket);
3028 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003029 strcpy(msg, "Error: socket got corrupted!");
3030 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003031 }
3032 mci = new_mci;
3033 pvt = mci->pvt_info;
3034
3035 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
3036
3037 if (rank < 4)
3038 dimm = 0;
3039 else if (rank < 8)
3040 dimm = 1;
3041 else
3042 dimm = 2;
3043
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003044
3045 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003046 * FIXME: On some memory configurations (mirror, lockstep), the
3047 * Memory Controller can't point the error to a single DIMM. The
3048 * EDAC core should be handling the channel mask, in order to point
3049 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003050 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03003051 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
3052 channel = first_channel;
3053
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003054 snprintf(msg, sizeof(msg),
Tony Luck7d375bf2015-05-18 17:50:42 -03003055 "%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 -03003056 overflow ? " OVERFLOW" : "",
3057 (uncorrected_error && recoverable) ? " recoverable" : "",
3058 area_type,
3059 mscod, errcode,
Tony Luck7d375bf2015-05-18 17:50:42 -03003060 socket, ha,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003061 channel_mask,
3062 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003063
Joe Perches956b9ba12012-04-29 17:08:39 -03003064 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003065
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003066 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003067
Seth Jennings351fc4a2014-09-05 14:28:47 -05003068 if (channel == CHANNEL_UNSPECIFIED)
3069 channel = -1;
3070
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003071 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003072 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003073 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
Tony Luck7d375bf2015-05-18 17:50:42 -03003074 4*ha+channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003075 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003076 return;
3077err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003078 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003079 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003080 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003081
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003082}
3083
3084/*
Tony Luckad08c4e2016-04-15 14:50:32 -07003085 * Check that logging is enabled and that this is the right type
3086 * of error for us to handle.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003087 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003088static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3089 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003090{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003091 struct mce *mce = (struct mce *)data;
3092 struct mem_ctl_info *mci;
3093 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003094 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003095
Chen, Gongfd521032013-12-06 01:17:09 -05003096 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3097 return NOTIFY_DONE;
3098
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003099 mci = get_mci_for_node_id(mce->socketid);
3100 if (!mci)
Tony Luckc4fc1952016-04-29 15:42:25 +02003101 return NOTIFY_DONE;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003102 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003103
3104 /*
3105 * Just let mcelog handle it if the error is
3106 * outside the memory controller. A memory error
3107 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3108 * bit 12 has an special meaning.
3109 */
3110 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003111 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003112
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003113 if (mce->mcgstatus & MCG_STATUS_MCIP)
3114 type = "Exception";
3115 else
3116 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003117
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003118 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003119
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003120 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3121 "Bank %d: %016Lx\n", mce->extcpu, type,
3122 mce->mcgstatus, mce->bank, mce->status);
3123 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3124 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3125 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003126
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003127 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3128 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3129 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003130
Tony Luckad08c4e2016-04-15 14:50:32 -07003131 sbridge_mce_output_error(mci, mce);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003132
3133 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003134 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003135}
3136
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003137static struct notifier_block sbridge_mce_dec = {
3138 .notifier_call = sbridge_mce_check_error,
3139};
3140
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003141/****************************************************************************
3142 EDAC register/unregister logic
3143 ****************************************************************************/
3144
3145static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3146{
3147 struct mem_ctl_info *mci = sbridge_dev->mci;
3148 struct sbridge_pvt *pvt;
3149
3150 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba12012-04-29 17:08:39 -03003151 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003152
3153 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3154 return;
3155 }
3156
3157 pvt = mci->pvt_info;
3158
Joe Perches956b9ba12012-04-29 17:08:39 -03003159 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3160 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003161
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003162 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03003163 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003164
Joe Perches956b9ba12012-04-29 17:08:39 -03003165 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003166 kfree(mci->ctl_name);
3167 edac_mc_free(mci);
3168 sbridge_dev->mci = NULL;
3169}
3170
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003171static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003172{
3173 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003174 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003175 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003176 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003177 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003178
3179 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03003180 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003181 if (unlikely(rc < 0))
3182 return rc;
3183
3184 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003185 layers[0].type = EDAC_MC_LAYER_CHANNEL;
Jim Snowd0cdf902015-12-03 10:48:54 +01003186 layers[0].size = type == KNIGHTS_LANDING ?
3187 KNL_MAX_CHANNELS : NUM_CHANNELS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003188 layers[0].is_virt_csrow = false;
3189 layers[1].type = EDAC_MC_LAYER_SLOT;
Jim Snowd0cdf902015-12-03 10:48:54 +01003190 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003191 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03003192 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003193 sizeof(*pvt));
3194
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003195 if (unlikely(!mci))
3196 return -ENOMEM;
3197
Joe Perches956b9ba12012-04-29 17:08:39 -03003198 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003199 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003200
3201 pvt = mci->pvt_info;
3202 memset(pvt, 0, sizeof(*pvt));
3203
3204 /* Associate sbridge_dev and mci for future usage */
3205 pvt->sbridge_dev = sbridge_dev;
3206 sbridge_dev->mci = mci;
3207
Jim Snowd0cdf902015-12-03 10:48:54 +01003208 mci->mtype_cap = type == KNIGHTS_LANDING ?
3209 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003210 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3211 mci->edac_cap = EDAC_FLAG_NONE;
3212 mci->mod_name = "sbridge_edac.c";
3213 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003214 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003215 mci->ctl_page_to_phys = NULL;
3216
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003217 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003218 switch (type) {
3219 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003220 pvt->info.rankcfgr = IB_RANK_CFG_A;
3221 pvt->info.get_tolm = ibridge_get_tolm;
3222 pvt->info.get_tohm = ibridge_get_tohm;
3223 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003224 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003225 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003226 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003227 pvt->info.sad_limit = sad_limit;
3228 pvt->info.interleave_mode = interleave_mode;
3229 pvt->info.show_interleave_mode = show_interleave_mode;
3230 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003231 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3232 pvt->info.interleave_list = ibridge_interleave_list;
3233 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3234 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003235 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003236 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
3237
3238 /* Store pci devices at mci for faster access */
3239 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3240 if (unlikely(rc < 0))
3241 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003242 break;
3243 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003244 pvt->info.rankcfgr = SB_RANK_CFG_A;
3245 pvt->info.get_tolm = sbridge_get_tolm;
3246 pvt->info.get_tohm = sbridge_get_tohm;
3247 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003248 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003249 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003250 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003251 pvt->info.sad_limit = sad_limit;
3252 pvt->info.interleave_mode = interleave_mode;
3253 pvt->info.show_interleave_mode = show_interleave_mode;
3254 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003255 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3256 pvt->info.interleave_list = sbridge_interleave_list;
3257 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3258 pvt->info.interleave_pkg = sbridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003259 pvt->info.get_width = sbridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003260 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
3261
3262 /* Store pci devices at mci for faster access */
3263 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3264 if (unlikely(rc < 0))
3265 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003266 break;
3267 case HASWELL:
3268 /* rankcfgr isn't used */
3269 pvt->info.get_tolm = haswell_get_tolm;
3270 pvt->info.get_tohm = haswell_get_tohm;
3271 pvt->info.dram_rule = ibridge_dram_rule;
3272 pvt->info.get_memory_type = haswell_get_memory_type;
3273 pvt->info.get_node_id = haswell_get_node_id;
3274 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003275 pvt->info.sad_limit = sad_limit;
3276 pvt->info.interleave_mode = interleave_mode;
3277 pvt->info.show_interleave_mode = show_interleave_mode;
3278 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003279 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3280 pvt->info.interleave_list = ibridge_interleave_list;
3281 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3282 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003283 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003284 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003285
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003286 /* Store pci devices at mci for faster access */
3287 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3288 if (unlikely(rc < 0))
3289 goto fail0;
3290 break;
Tony Luck1f395812014-12-02 09:27:30 -08003291 case BROADWELL:
3292 /* rankcfgr isn't used */
3293 pvt->info.get_tolm = haswell_get_tolm;
3294 pvt->info.get_tohm = haswell_get_tohm;
3295 pvt->info.dram_rule = ibridge_dram_rule;
3296 pvt->info.get_memory_type = haswell_get_memory_type;
3297 pvt->info.get_node_id = haswell_get_node_id;
3298 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003299 pvt->info.sad_limit = sad_limit;
3300 pvt->info.interleave_mode = interleave_mode;
3301 pvt->info.show_interleave_mode = show_interleave_mode;
3302 pvt->info.dram_attr = dram_attr;
Tony Luck1f395812014-12-02 09:27:30 -08003303 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3304 pvt->info.interleave_list = ibridge_interleave_list;
3305 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3306 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003307 pvt->info.get_width = broadwell_get_width;
Tony Luck1f395812014-12-02 09:27:30 -08003308 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
3309
3310 /* Store pci devices at mci for faster access */
3311 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3312 if (unlikely(rc < 0))
3313 goto fail0;
3314 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01003315 case KNIGHTS_LANDING:
3316 /* pvt->info.rankcfgr == ??? */
3317 pvt->info.get_tolm = knl_get_tolm;
3318 pvt->info.get_tohm = knl_get_tohm;
3319 pvt->info.dram_rule = knl_dram_rule;
3320 pvt->info.get_memory_type = knl_get_memory_type;
3321 pvt->info.get_node_id = knl_get_node_id;
3322 pvt->info.rir_limit = NULL;
3323 pvt->info.sad_limit = knl_sad_limit;
3324 pvt->info.interleave_mode = knl_interleave_mode;
3325 pvt->info.show_interleave_mode = knl_show_interleave_mode;
3326 pvt->info.dram_attr = dram_attr_knl;
3327 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3328 pvt->info.interleave_list = knl_interleave_list;
3329 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3330 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +01003331 pvt->info.get_width = knl_get_width;
Jim Snowd0cdf902015-12-03 10:48:54 +01003332 mci->ctl_name = kasprintf(GFP_KERNEL,
3333 "Knights Landing Socket#%d", mci->mc_idx);
3334
3335 rc = knl_mci_bind_devs(mci, sbridge_dev);
3336 if (unlikely(rc < 0))
3337 goto fail0;
3338 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003339 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003340
3341 /* Get dimm basic config and the memory layout */
3342 get_dimm_config(mci);
3343 get_memory_layout(mci);
3344
3345 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003346 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003347
3348 /* add this new MC control structure to EDAC's list of MCs */
3349 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba12012-04-29 17:08:39 -03003350 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003351 rc = -EINVAL;
3352 goto fail0;
3353 }
3354
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003355 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003356
3357fail0:
3358 kfree(mci->ctl_name);
3359 edac_mc_free(mci);
3360 sbridge_dev->mci = NULL;
3361 return rc;
3362}
3363
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003364#define ICPU(model, table) \
3365 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3366
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003367static const struct x86_cpu_id sbridge_cpuids[] = {
3368 ICPU(0x2d, pci_dev_descr_sbridge_table), /* SANDY_BRIDGE */
3369 ICPU(0x3e, pci_dev_descr_ibridge_table), /* IVY_BRIDGE */
3370 ICPU(0x3f, pci_dev_descr_haswell_table), /* HASWELL */
3371 ICPU(0x4f, pci_dev_descr_broadwell_table), /* BROADWELL */
Tony Luck665f05e02016-06-02 10:58:08 -07003372 ICPU(0x56, pci_dev_descr_broadwell_table), /* BROADWELL-DE */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003373 ICPU(0x57, pci_dev_descr_knl_table), /* KNIGHTS_LANDING */
3374 { }
3375};
3376MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3377
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003378/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003379 * sbridge_probe Get all devices and register memory controllers
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003380 * present.
3381 * return:
3382 * 0 for FOUND a device
3383 * < 0 for error code
3384 */
3385
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003386static int sbridge_probe(const struct x86_cpu_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003387{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003388 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003389 u8 mc, num_mc = 0;
3390 struct sbridge_dev *sbridge_dev;
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003391 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003392
3393 /* get the pci devices we want to reserve for our use */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003394 rc = sbridge_get_all_devices(&num_mc, ptable);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003395
Borislav Petkov11249e72015-02-05 12:39:36 +01003396 if (unlikely(rc < 0)) {
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003397 edac_dbg(0, "couldn't get all devices\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003398 goto fail0;
Borislav Petkov11249e72015-02-05 12:39:36 +01003399 }
3400
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003401 mc = 0;
3402
3403 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba12012-04-29 17:08:39 -03003404 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3405 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003406
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003407 sbridge_dev->mc = mc++;
Tony Luck665f05e02016-06-02 10:58:08 -07003408 rc = sbridge_register_mci(sbridge_dev, ptable->type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003409 if (unlikely(rc < 0))
3410 goto fail1;
3411 }
3412
Borislav Petkov11249e72015-02-05 12:39:36 +01003413 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003414
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003415 return 0;
3416
3417fail1:
3418 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3419 sbridge_unregister_mci(sbridge_dev);
3420
3421 sbridge_put_all_devices();
3422fail0:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003423 return rc;
3424}
3425
3426/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003427 * sbridge_remove cleanup
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003428 *
3429 */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003430static void sbridge_remove(void)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003431{
3432 struct sbridge_dev *sbridge_dev;
3433
Joe Perches956b9ba12012-04-29 17:08:39 -03003434 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003435
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003436 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3437 sbridge_unregister_mci(sbridge_dev);
3438
3439 /* Release PCI resources */
3440 sbridge_put_all_devices();
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003441}
3442
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003443/*
3444 * sbridge_init Module entry function
3445 * Try to initialize this module for its devices
3446 */
3447static int __init sbridge_init(void)
3448{
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003449 const struct x86_cpu_id *id;
3450 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003451
Joe Perches956b9ba12012-04-29 17:08:39 -03003452 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003453
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003454 id = x86_match_cpu(sbridge_cpuids);
3455 if (!id)
3456 return -ENODEV;
3457
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003458 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3459 opstate_init();
3460
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003461 rc = sbridge_probe(id);
3462
3463 if (rc >= 0) {
Chen Gonge35fca42012-05-08 20:40:12 -03003464 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05003465 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3466 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003467 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03003468 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003469
3470 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003471 rc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003472
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003473 return rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003474}
3475
3476/*
3477 * sbridge_exit() Module exit function
3478 * Unregister the driver
3479 */
3480static void __exit sbridge_exit(void)
3481{
Joe Perches956b9ba12012-04-29 17:08:39 -03003482 edac_dbg(2, "\n");
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003483 sbridge_remove();
Chen Gonge35fca42012-05-08 20:40:12 -03003484 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003485}
3486
3487module_init(sbridge_init);
3488module_exit(sbridge_exit);
3489
3490module_param(edac_op_state, int, 0444);
3491MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3492
3493MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003494MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003495MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003496MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003497 SBRIDGE_REVISION);