blob: fe4ffeaa623b02c602bcd6556c1450eac6d488e7 [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>
Dave Hansen20f4d692016-09-29 13:43:21 -070026#include <asm/intel-family.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020027#include <asm/processor.h>
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -020028#include <asm/mce.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020029
Mauro Carvalho Chehab78d88e82016-10-29 15:16:34 -020030#include "edac_module.h"
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020031
32/* Static vars */
33static LIST_HEAD(sbridge_edac_list);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020034
35/*
36 * Alter this version for the module when modifications are made
37 */
Tony Luck7d375bf2015-05-18 17:50:42 -030038#define SBRIDGE_REVISION " Ver: 1.1.1 "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020039#define EDAC_MOD_STR "sbridge_edac"
40
41/*
42 * Debug macros
43 */
44#define sbridge_printk(level, fmt, arg...) \
45 edac_printk(level, "sbridge", fmt, ##arg)
46
47#define sbridge_mc_printk(mci, level, fmt, arg...) \
48 edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
49
50/*
51 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
52 */
53#define GET_BITFIELD(v, lo, hi) \
Chen, Gong10ef6b02013-10-18 14:29:07 -070054 (((v) & GENMASK_ULL(hi, lo)) >> (lo))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020055
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020056/* Devices 12 Function 6, Offsets 0x80 to 0xcc */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -030057static const u32 sbridge_dram_rule[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020058 0x80, 0x88, 0x90, 0x98, 0xa0,
59 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
60};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020061
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030062static const u32 ibridge_dram_rule[] = {
63 0x60, 0x68, 0x70, 0x78, 0x80,
64 0x88, 0x90, 0x98, 0xa0, 0xa8,
65 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
66 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
67};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020068
Jim Snowd0cdf902015-12-03 10:48:54 +010069static const u32 knl_dram_rule[] = {
70 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
71 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
72 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
73 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
74 0x100, 0x108, 0x110, 0x118, /* 20-23 */
75};
76
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020077#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -030078#define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020079
Jim Snowc59f9c02015-12-03 10:48:52 +010080static char *show_dram_attr(u32 attr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020081{
Jim Snowc59f9c02015-12-03 10:48:52 +010082 switch (attr) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020083 case 0:
84 return "DRAM";
85 case 1:
86 return "MMCFG";
87 case 2:
88 return "NXM";
89 default:
90 return "unknown";
91 }
92}
93
Aristeu Rozanskief1ce512013-10-30 13:27:01 -030094static const u32 sbridge_interleave_list[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020095 0x84, 0x8c, 0x94, 0x9c, 0xa4,
96 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
97};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020098
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030099static const u32 ibridge_interleave_list[] = {
100 0x64, 0x6c, 0x74, 0x7c, 0x84,
101 0x8c, 0x94, 0x9c, 0xa4, 0xac,
102 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
103 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
104};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200105
Jim Snowd0cdf902015-12-03 10:48:54 +0100106static const u32 knl_interleave_list[] = {
107 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
108 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
109 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
110 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
111 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
112};
113
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300114struct interleave_pkg {
115 unsigned char start;
116 unsigned char end;
117};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200118
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300119static const struct interleave_pkg sbridge_interleave_pkg[] = {
120 { 0, 2 },
121 { 3, 5 },
122 { 8, 10 },
123 { 11, 13 },
124 { 16, 18 },
125 { 19, 21 },
126 { 24, 26 },
127 { 27, 29 },
128};
129
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300130static const struct interleave_pkg ibridge_interleave_pkg[] = {
131 { 0, 3 },
132 { 4, 7 },
133 { 8, 11 },
134 { 12, 15 },
135 { 16, 19 },
136 { 20, 23 },
137 { 24, 27 },
138 { 28, 31 },
139};
140
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300141static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
142 int interleave)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200143{
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300144 return GET_BITFIELD(reg, table[interleave].start,
145 table[interleave].end);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200146}
147
148/* Devices 12 Function 7 */
149
150#define TOLM 0x80
Jim Snowd0cdf902015-12-03 10:48:54 +0100151#define TOHM 0x84
Tony Luckf7cf2a22014-10-29 10:36:50 -0700152#define HASWELL_TOLM 0xd0
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300153#define HASWELL_TOHM_0 0xd4
154#define HASWELL_TOHM_1 0xd8
Jim Snowd0cdf902015-12-03 10:48:54 +0100155#define KNL_TOLM 0xd0
156#define KNL_TOHM_0 0xd4
157#define KNL_TOHM_1 0xd8
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200158
159#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
160#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
161
162/* Device 13 Function 6 */
163
164#define SAD_TARGET 0xf0
165
166#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
167
Jim Snowd0cdf902015-12-03 10:48:54 +0100168#define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
169
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200170#define SAD_CONTROL 0xf4
171
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200172/* Device 14 function 0 */
173
174static const u32 tad_dram_rule[] = {
175 0x40, 0x44, 0x48, 0x4c,
176 0x50, 0x54, 0x58, 0x5c,
177 0x60, 0x64, 0x68, 0x6c,
178};
179#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
180
181#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
182#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
183#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
184#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
185#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
186#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
187#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
188
189/* Device 15, function 0 */
190
191#define MCMTR 0x7c
Jim Snowd0cdf902015-12-03 10:48:54 +0100192#define KNL_MCMTR 0x624
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200193
194#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
195#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
196#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
197
198/* Device 15, function 1 */
199
200#define RASENABLES 0xac
201#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
202
203/* Device 15, functions 2-5 */
204
205static const int mtr_regs[] = {
206 0x80, 0x84, 0x88,
207};
208
Jim Snowd0cdf902015-12-03 10:48:54 +0100209static const int knl_mtr_reg = 0xb60;
210
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200211#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
212#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
213#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
214#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
215#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
216
217static const u32 tad_ch_nilv_offset[] = {
218 0x90, 0x94, 0x98, 0x9c,
219 0xa0, 0xa4, 0xa8, 0xac,
220 0xb0, 0xb4, 0xb8, 0xbc,
221};
222#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
223#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
224
225static const u32 rir_way_limit[] = {
226 0x108, 0x10c, 0x110, 0x114, 0x118,
227};
228#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
229
230#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
231#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200232
233#define MAX_RIR_WAY 8
234
235static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
236 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
237 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
238 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
239 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
240 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
241};
242
Tony Luckc7103f62016-05-31 11:50:28 -0700243#define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
244 GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
245
246#define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
247 GET_BITFIELD(reg, 2, 15) : GET_BITFIELD(reg, 2, 14))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200248
249/* Device 16, functions 2-7 */
250
251/*
252 * FIXME: Implement the error count reads directly
253 */
254
255static const u32 correrrcnt[] = {
256 0x104, 0x108, 0x10c, 0x110,
257};
258
259#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
260#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
261#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
262#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
263
264static const u32 correrrthrsld[] = {
265 0x11c, 0x120, 0x124, 0x128,
266};
267
268#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
269#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
270
271
272/* Device 17, function 0 */
273
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300274#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200275
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300276#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200277
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200278/*
279 * sbridge structs
280 */
281
Tony Luck7d375bf2015-05-18 17:50:42 -0300282#define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
Seth Jennings351fc4a2014-09-05 14:28:47 -0500283#define MAX_DIMMS 3 /* Max DIMMS per channel */
Jim Snowd0cdf902015-12-03 10:48:54 +0100284#define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
285#define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
286#define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
Seth Jennings351fc4a2014-09-05 14:28:47 -0500287#define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200288
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300289enum type {
290 SANDY_BRIDGE,
291 IVY_BRIDGE,
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300292 HASWELL,
Tony Luck1f395812014-12-02 09:27:30 -0800293 BROADWELL,
Jim Snowd0cdf902015-12-03 10:48:54 +0100294 KNIGHTS_LANDING,
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300295};
296
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800297enum domain {
298 IMC0 = 0,
299 IMC1,
300 SOCK,
301};
302
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300303struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200304struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300305 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300306 u32 mcmtr;
307 u32 rankcfgr;
308 u64 (*get_tolm)(struct sbridge_pvt *pvt);
309 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300310 u64 (*rir_limit)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100311 u64 (*sad_limit)(u32 reg);
312 u32 (*interleave_mode)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100313 u32 (*dram_attr)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300314 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300315 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300316 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300317 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300318 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300319 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300320 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400321 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300322 struct pci_dev *pci_vtd;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200323};
324
325struct sbridge_channel {
326 u32 ranks;
327 u32 dimms;
328};
329
330struct pci_id_descr {
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -0300331 int dev_id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200332 int optional;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800333 enum domain dom;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200334};
335
336struct pci_id_table {
337 const struct pci_id_descr *descr;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800338 int n_devs_per_imc;
339 int n_devs_per_sock;
340 int n_imcs_per_sock;
Tony Luck665f05e02016-06-02 10:58:08 -0700341 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200342};
343
344struct sbridge_dev {
345 struct list_head list;
346 u8 bus, mc;
347 u8 node_id, source_id;
348 struct pci_dev **pdev;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800349 enum domain dom;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200350 int n_devs;
351 struct mem_ctl_info *mci;
352};
353
Jim Snowd0cdf902015-12-03 10:48:54 +0100354struct knl_pvt {
355 struct pci_dev *pci_cha[KNL_MAX_CHAS];
356 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
357 struct pci_dev *pci_mc0;
358 struct pci_dev *pci_mc1;
359 struct pci_dev *pci_mc0_misc;
360 struct pci_dev *pci_mc1_misc;
361 struct pci_dev *pci_mc_info; /* tolm, tohm */
362};
363
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200364struct sbridge_pvt {
365 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300366 struct pci_dev *pci_sad0, *pci_sad1;
367 struct pci_dev *pci_ha0, *pci_ha1;
368 struct pci_dev *pci_br0, *pci_br1;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300369 struct pci_dev *pci_ha1_ta;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200370 struct pci_dev *pci_tad[NUM_CHANNELS];
371
372 struct sbridge_dev *sbridge_dev;
373
374 struct sbridge_info info;
375 struct sbridge_channel channel[NUM_CHANNELS];
376
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200377 /* Memory type detection */
378 bool is_mirrored, is_lockstep, is_close_pg;
Tony Luckea5dfb52016-04-14 10:22:02 -0700379 bool is_chan_hash;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200380
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200381 /* Memory description */
382 u64 tolm, tohm;
Jim Snowd0cdf902015-12-03 10:48:54 +0100383 struct knl_pvt knl;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200384};
385
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800386#define PCI_DESCR(device_id, opt, domain) \
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300387 .dev_id = (device_id), \
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800388 .optional = opt, \
389 .dom = domain
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200390
391static const struct pci_id_descr pci_dev_descr_sbridge[] = {
392 /* Processor Home Agent */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800393 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0, IMC0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200394
395 /* Memory controller */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800396 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0, IMC0) },
397 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0, IMC0) },
398 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0, IMC0) },
399 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0, IMC0) },
400 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0, IMC0) },
401 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0, IMC0) },
402 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1, SOCK) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200403
404 /* System Address Decoder */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800405 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0, SOCK) },
406 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0, SOCK) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200407
408 /* Broadcast Registers */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800409 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0, SOCK) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200410};
411
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800412#define PCI_ID_TABLE_ENTRY(A, N, M, T) { \
Tony Luck665f05e02016-06-02 10:58:08 -0700413 .descr = A, \
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800414 .n_devs_per_imc = N, \
415 .n_devs_per_sock = ARRAY_SIZE(A), \
416 .n_imcs_per_sock = M, \
Tony Luck665f05e02016-06-02 10:58:08 -0700417 .type = T \
418}
419
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200420static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800421 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, ARRAY_SIZE(pci_dev_descr_sbridge), 1, SANDY_BRIDGE),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200422 {0,} /* 0 terminated list. */
423};
424
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300425/* This changes depending if 1HA or 2HA:
426 * 1HA:
427 * 0x0eb8 (17.0) is DDRIO0
428 * 2HA:
429 * 0x0ebc (17.4) is DDRIO0
430 */
431#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
432#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
433
434/* pci ids */
435#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
436#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
437#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
438#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
439#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
440#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
441#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
442#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
443#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
444#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
445#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
446#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
447#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
448#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
449#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
Tony Luck7d375bf2015-05-18 17:50:42 -0300450#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
451#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300452
453static const struct pci_id_descr pci_dev_descr_ibridge[] = {
454 /* Processor Home Agent */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800455 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0, IMC0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300456
457 /* Memory controller */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800458 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0, IMC0) },
459 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0, IMC0) },
460 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0, IMC0) },
461 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0, IMC0) },
462 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0, IMC0) },
463 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0, IMC0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300464
465 /* Optional, mode 2HA */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800466 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1, IMC1) },
467 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1, IMC1) },
468 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1, IMC1) },
469 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1, IMC1) },
470 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1, IMC1) },
471 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1, IMC1) },
472 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1, IMC1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300473
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800474 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1, SOCK) },
475 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1, SOCK) },
476
477 /* System Address Decoder */
478 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0, SOCK) },
479
480 /* Broadcast Registers */
481 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1, SOCK) },
482 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0, SOCK) },
483
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300484};
485
486static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800487 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, 12, 2, IVY_BRIDGE),
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300488 {0,} /* 0 terminated list. */
489};
490
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300491/* Haswell support */
492/* EN processor:
493 * - 1 IMC
494 * - 3 DDR3 channels, 2 DPC per channel
495 * EP processor:
496 * - 1 or 2 IMC
497 * - 4 DDR4 channels, 3 DPC per channel
498 * EP 4S processor:
499 * - 2 IMC
500 * - 4 DDR4 channels, 3 DPC per channel
501 * EX processor:
502 * - 2 IMC
503 * - each IMC interfaces with a SMI 2 channel
504 * - each SMI channel interfaces with a scalable memory buffer
505 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
506 */
Tony Luck1f395812014-12-02 09:27:30 -0800507#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300508#define HASWELL_HASYSDEFEATURE2 0x84
509#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
510#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
511#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
512#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800513#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM 0x2f71
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300514#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800515#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM 0x2f79
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300516#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
517#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
518#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
519#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
520#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
521#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
522#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
523#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
524#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
525#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
526#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
Aristeu Rozanski71793852015-06-12 09:44:52 -0400527#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
528#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
529#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300530static const struct pci_id_descr pci_dev_descr_haswell[] = {
531 /* first item must be the HA */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800532 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0, IMC0) },
533 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1, IMC1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300534
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800535 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0, IMC0) },
536 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM, 0, IMC0) },
537 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0, IMC0) },
538 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0, IMC0) },
539 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1, IMC0) },
540 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1, IMC0) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300541
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800542 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1, IMC1) },
543 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM, 1, IMC1) },
544 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1, IMC1) },
545 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1, IMC1) },
546 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1, IMC1) },
547 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1, IMC1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300548
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800549 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0, SOCK) },
550 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0, SOCK) },
551 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1, SOCK) },
552 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1, SOCK) },
553 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1, SOCK) },
554 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1, SOCK) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300555};
556
557static const struct pci_id_table pci_dev_descr_haswell_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800558 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, 13, 2, HASWELL),
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300559 {0,} /* 0 terminated list. */
560};
561
Jim Snowd0cdf902015-12-03 10:48:54 +0100562/* Knight's Landing Support */
563/*
564 * KNL's memory channels are swizzled between memory controllers.
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200565 * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
Jim Snowd0cdf902015-12-03 10:48:54 +0100566 */
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200567#define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
Jim Snowd0cdf902015-12-03 10:48:54 +0100568
569/* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
570#define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
571/* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800572#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN 0x7843
Jim Snowd0cdf902015-12-03 10:48:54 +0100573/* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
574#define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
575/* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
576#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
577/* SAD target - 1-29-1 (1 of these) */
578#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
579/* Caching / Home Agent */
580#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
581/* Device with TOLM and TOHM, 0-5-0 (1 of these) */
582#define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
583
584/*
585 * KNL differs from SB, IB, and Haswell in that it has multiple
586 * instances of the same device with the same device ID, so we handle that
587 * by creating as many copies in the table as we expect to find.
588 * (Like device ID must be grouped together.)
589 */
590
591static const struct pci_id_descr pci_dev_descr_knl[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800592 [0 ... 1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0, IMC0)},
593 [2 ... 7] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN, 0, IMC0) },
594 [8] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0, IMC0) },
595 [9] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0, IMC0) },
596 [10] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0, SOCK) },
597 [11] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0, SOCK) },
598 [12 ... 49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0, SOCK) },
Jim Snowd0cdf902015-12-03 10:48:54 +0100599};
600
601static const struct pci_id_table pci_dev_descr_knl_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800602 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, ARRAY_SIZE(pci_dev_descr_knl), 1, KNIGHTS_LANDING),
Jim Snowd0cdf902015-12-03 10:48:54 +0100603 {0,}
604};
605
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200606/*
Tony Luck1f395812014-12-02 09:27:30 -0800607 * Broadwell support
608 *
609 * DE processor:
610 * - 1 IMC
611 * - 2 DDR3 channels, 2 DPC per channel
Tony Luckfa2ce642015-05-20 19:10:35 -0300612 * EP processor:
613 * - 1 or 2 IMC
614 * - 4 DDR4 channels, 3 DPC per channel
615 * EP 4S processor:
616 * - 2 IMC
617 * - 4 DDR4 channels, 3 DPC per channel
618 * EX processor:
619 * - 2 IMC
620 * - each IMC interfaces with a SMI 2 channel
621 * - each SMI channel interfaces with a scalable memory buffer
622 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
Tony Luck1f395812014-12-02 09:27:30 -0800623 */
624#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
625#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
Tony Luckfa2ce642015-05-20 19:10:35 -0300626#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
Tony Luck1f395812014-12-02 09:27:30 -0800627#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800628#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM 0x6f71
Tony Luckfa2ce642015-05-20 19:10:35 -0300629#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800630#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM 0x6f79
Tony Luck1f395812014-12-02 09:27:30 -0800631#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
632#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
633#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
634#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
635#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
636#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
Tony Luckfa2ce642015-05-20 19:10:35 -0300637#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
638#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
639#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
640#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
Tony Luck1f395812014-12-02 09:27:30 -0800641#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
642
643static const struct pci_id_descr pci_dev_descr_broadwell[] = {
644 /* first item must be the HA */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800645 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0, IMC0) },
646 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1, IMC1) },
Tony Luck1f395812014-12-02 09:27:30 -0800647
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800648 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0, IMC0) },
649 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM, 0, IMC0) },
650 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0, IMC0) },
651 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0, IMC0) },
652 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1, IMC0) },
653 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1, IMC0) },
Tony Luck1f395812014-12-02 09:27:30 -0800654
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800655 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1, IMC1) },
656 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM, 1, IMC1) },
657 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1, IMC1) },
658 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1, IMC1) },
659 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1, IMC1) },
660 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1, IMC1) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300661
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800662 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0, SOCK) },
663 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0, SOCK) },
664 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1, SOCK) },
Tony Luck1f395812014-12-02 09:27:30 -0800665};
666
667static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800668 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, 10, 2, BROADWELL),
Tony Luck1f395812014-12-02 09:27:30 -0800669 {0,} /* 0 terminated list. */
670};
671
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200672
673/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700674 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200675 ****************************************************************************/
676
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300677static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200678{
679 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300680 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200681
Jim Snowd0cdf902015-12-03 10:48:54 +0100682 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300683 max = 8;
684
685 if (ranks > max) {
686 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
687 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200688 return -EINVAL;
689 }
690
691 return ranks;
692}
693
694static inline int numrow(u32 mtr)
695{
696 int rows = (RANK_WIDTH_BITS(mtr) + 12);
697
698 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300699 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
700 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200701 return -EINVAL;
702 }
703
704 return 1 << rows;
705}
706
707static inline int numcol(u32 mtr)
708{
709 int cols = (COL_WIDTH_BITS(mtr) + 10);
710
711 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300712 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
713 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200714 return -EINVAL;
715 }
716
717 return 1 << cols;
718}
719
Jim Snowc1979ba2015-12-03 10:48:53 +0100720static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200721{
722 struct sbridge_dev *sbridge_dev;
723
Jim Snowc1979ba2015-12-03 10:48:53 +0100724 /*
725 * If we have devices scattered across several busses that pertain
726 * to the same memory controller, we'll lump them all together.
727 */
728 if (multi_bus) {
729 return list_first_entry_or_null(&sbridge_edac_list,
730 struct sbridge_dev, list);
731 }
732
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200733 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
734 if (sbridge_dev->bus == bus)
735 return sbridge_dev;
736 }
737
738 return NULL;
739}
740
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800741static struct sbridge_dev *alloc_sbridge_dev(u8 bus, enum domain dom, const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200742{
743 struct sbridge_dev *sbridge_dev;
744
745 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
746 if (!sbridge_dev)
747 return NULL;
748
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800749 sbridge_dev->pdev = kcalloc(table->n_devs_per_sock, sizeof(*sbridge_dev->pdev), GFP_KERNEL);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200750 if (!sbridge_dev->pdev) {
751 kfree(sbridge_dev);
752 return NULL;
753 }
754
755 sbridge_dev->bus = bus;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800756 sbridge_dev->dom = dom;
757 sbridge_dev->n_devs = table->n_devs_per_sock;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200758 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
759
760 return sbridge_dev;
761}
762
763static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
764{
765 list_del(&sbridge_dev->list);
766 kfree(sbridge_dev->pdev);
767 kfree(sbridge_dev);
768}
769
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300770static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
771{
772 u32 reg;
773
774 /* Address range is 32:28 */
775 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
776 return GET_TOLM(reg);
777}
778
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300779static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
780{
781 u32 reg;
782
783 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
784 return GET_TOHM(reg);
785}
786
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300787static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
788{
789 u32 reg;
790
791 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
792
793 return GET_TOLM(reg);
794}
795
796static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
797{
798 u32 reg;
799
800 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
801
802 return GET_TOHM(reg);
803}
804
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300805static u64 rir_limit(u32 reg)
806{
807 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
808}
809
Jim Snowc59f9c02015-12-03 10:48:52 +0100810static u64 sad_limit(u32 reg)
811{
812 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
813}
814
815static u32 interleave_mode(u32 reg)
816{
817 return GET_BITFIELD(reg, 1, 1);
818}
819
Jim Snowc59f9c02015-12-03 10:48:52 +0100820static u32 dram_attr(u32 reg)
821{
822 return GET_BITFIELD(reg, 2, 3);
823}
824
Jim Snowd0cdf902015-12-03 10:48:54 +0100825static u64 knl_sad_limit(u32 reg)
826{
827 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
828}
829
830static u32 knl_interleave_mode(u32 reg)
831{
832 return GET_BITFIELD(reg, 1, 2);
833}
834
Nicolas Iooss127c1222017-01-22 18:28:06 +0100835static const char * const knl_intlv_mode[] = {
836 "[8:6]", "[10:8]", "[14:12]", "[32:30]"
837};
838
839static const char *get_intlv_mode_str(u32 reg, enum type t)
Jim Snowd0cdf902015-12-03 10:48:54 +0100840{
Nicolas Iooss127c1222017-01-22 18:28:06 +0100841 if (t == KNIGHTS_LANDING)
842 return knl_intlv_mode[knl_interleave_mode(reg)];
843 else
844 return interleave_mode(reg) ? "[8:6]" : "[8:6]XOR[18:16]";
Jim Snowd0cdf902015-12-03 10:48:54 +0100845}
846
847static u32 dram_attr_knl(u32 reg)
848{
849 return GET_BITFIELD(reg, 3, 4);
850}
851
852
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300853static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
854{
855 u32 reg;
856 enum mem_type mtype;
857
858 if (pvt->pci_ddrio) {
859 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
860 &reg);
861 if (GET_BITFIELD(reg, 11, 11))
862 /* FIXME: Can also be LRDIMM */
863 mtype = MEM_RDDR3;
864 else
865 mtype = MEM_DDR3;
866 } else
867 mtype = MEM_UNKNOWN;
868
869 return mtype;
870}
871
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300872static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
873{
874 u32 reg;
875 bool registered = false;
876 enum mem_type mtype = MEM_UNKNOWN;
877
878 if (!pvt->pci_ddrio)
879 goto out;
880
881 pci_read_config_dword(pvt->pci_ddrio,
882 HASWELL_DDRCRCLKCONTROLS, &reg);
883 /* Is_Rdimm */
884 if (GET_BITFIELD(reg, 16, 16))
885 registered = true;
886
887 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
888 if (GET_BITFIELD(reg, 14, 14)) {
889 if (registered)
890 mtype = MEM_RDDR4;
891 else
892 mtype = MEM_DDR4;
893 } else {
894 if (registered)
895 mtype = MEM_RDDR3;
896 else
897 mtype = MEM_DDR3;
898 }
899
900out:
901 return mtype;
902}
903
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +0100904static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
905{
906 /* for KNL value is fixed */
907 return DEV_X16;
908}
909
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400910static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
911{
912 /* there's no way to figure out */
913 return DEV_UNKNOWN;
914}
915
916static enum dev_type __ibridge_get_width(u32 mtr)
917{
918 enum dev_type type;
919
920 switch (mtr) {
921 case 3:
922 type = DEV_UNKNOWN;
923 break;
924 case 2:
925 type = DEV_X16;
926 break;
927 case 1:
928 type = DEV_X8;
929 break;
930 case 0:
931 type = DEV_X4;
932 break;
933 }
934
935 return type;
936}
937
938static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
939{
940 /*
941 * ddr3_width on the documentation but also valid for DDR4 on
942 * Haswell
943 */
944 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
945}
946
947static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
948{
949 /* ddr3_width on the documentation but also valid for DDR4 */
950 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
951}
952
Jim Snowd0cdf902015-12-03 10:48:54 +0100953static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
954{
955 /* DDR4 RDIMMS and LRDIMMS are supported */
956 return MEM_RDDR4;
957}
958
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300959static u8 get_node_id(struct sbridge_pvt *pvt)
960{
961 u32 reg;
962 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
963 return GET_BITFIELD(reg, 0, 2);
964}
965
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300966static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
967{
968 u32 reg;
969
970 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
971 return GET_BITFIELD(reg, 0, 3);
972}
973
Jim Snowd0cdf902015-12-03 10:48:54 +0100974static u8 knl_get_node_id(struct sbridge_pvt *pvt)
975{
976 u32 reg;
977
978 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
979 return GET_BITFIELD(reg, 0, 2);
980}
981
982
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300983static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
984{
985 u32 reg;
986
Tony Luckf7cf2a22014-10-29 10:36:50 -0700987 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
988 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300989}
990
991static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
992{
993 u64 rc;
994 u32 reg;
995
996 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
997 rc = GET_BITFIELD(reg, 26, 31);
998 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
999 rc = ((reg << 6) | rc) << 26;
1000
1001 return rc | 0x1ffffff;
1002}
1003
Jim Snowd0cdf902015-12-03 10:48:54 +01001004static u64 knl_get_tolm(struct sbridge_pvt *pvt)
1005{
1006 u32 reg;
1007
1008 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
1009 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1010}
1011
1012static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1013{
1014 u64 rc;
1015 u32 reg_lo, reg_hi;
1016
1017 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
1018 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
1019 rc = ((u64)reg_hi << 32) | reg_lo;
1020 return rc | 0x3ffffff;
1021}
1022
1023
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001024static u64 haswell_rir_limit(u32 reg)
1025{
1026 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1027}
1028
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001029static inline u8 sad_pkg_socket(u8 pkg)
1030{
1031 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -03001032 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001033}
1034
1035static inline u8 sad_pkg_ha(u8 pkg)
1036{
1037 return (pkg >> 2) & 0x1;
1038}
1039
Tony Luckea5dfb52016-04-14 10:22:02 -07001040static int haswell_chan_hash(int idx, u64 addr)
1041{
1042 int i;
1043
1044 /*
1045 * XOR even bits from 12:26 to bit0 of idx,
1046 * odd bits from 13:27 to bit1
1047 */
1048 for (i = 12; i < 28; i += 2)
1049 idx ^= (addr >> i) & 3;
1050
1051 return idx;
1052}
1053
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001054/****************************************************************************
1055 Memory check routines
1056 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001057static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001058{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001059 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001060
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001061 do {
1062 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
1063 if (pdev && pdev->bus->number == bus)
1064 break;
1065 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001066
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001067 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001068}
1069
1070/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001071 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001072 * @bus: Device bus
1073 * @type: Memory controller type
1074 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
1075 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001076 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001077static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001078{
1079 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001080 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001081
Tony Luck1f395812014-12-02 09:27:30 -08001082 switch (type) {
1083 case IVY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001084 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001085 break;
1086 case HASWELL:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001087 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001088 break;
1089 case SANDY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001090 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001091 break;
1092 case BROADWELL:
1093 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
1094 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01001095 case KNIGHTS_LANDING:
1096 /*
1097 * KNL doesn't group things by bus the same way
1098 * SB/IB/Haswell does.
1099 */
1100 id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
1101 break;
Tony Luck1f395812014-12-02 09:27:30 -08001102 default:
1103 return -ENODEV;
1104 }
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001105
Jim Snowd0cdf902015-12-03 10:48:54 +01001106 if (type != KNIGHTS_LANDING)
1107 pdev = get_pdev_same_bus(bus, id);
1108 else
1109 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
1110
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001111 if (!pdev) {
1112 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001113 "%04x:%04x! on bus %02d\n",
1114 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001115 return -ENODEV;
1116 }
1117
Jim Snowd0cdf902015-12-03 10:48:54 +01001118 pci_read_config_dword(pdev,
1119 type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001120 if (!IS_ECC_ENABLED(mcmtr)) {
1121 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
1122 return -ENODEV;
1123 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001124 return 0;
1125}
1126
Jim Snowd0cdf902015-12-03 10:48:54 +01001127/* Low bits of TAD limit, and some metadata. */
1128static const u32 knl_tad_dram_limit_lo[] = {
1129 0x400, 0x500, 0x600, 0x700,
1130 0x800, 0x900, 0xa00, 0xb00,
1131};
1132
1133/* Low bits of TAD offset. */
1134static const u32 knl_tad_dram_offset_lo[] = {
1135 0x404, 0x504, 0x604, 0x704,
1136 0x804, 0x904, 0xa04, 0xb04,
1137};
1138
1139/* High 16 bits of TAD limit and offset. */
1140static const u32 knl_tad_dram_hi[] = {
1141 0x408, 0x508, 0x608, 0x708,
1142 0x808, 0x908, 0xa08, 0xb08,
1143};
1144
1145/* Number of ways a tad entry is interleaved. */
1146static const u32 knl_tad_ways[] = {
1147 8, 6, 4, 3, 2, 1,
1148};
1149
1150/*
1151 * Retrieve the n'th Target Address Decode table entry
1152 * from the memory controller's TAD table.
1153 *
1154 * @pvt: driver private data
1155 * @entry: which entry you want to retrieve
1156 * @mc: which memory controller (0 or 1)
1157 * @offset: output tad range offset
1158 * @limit: output address of first byte above tad range
1159 * @ways: output number of interleave ways
1160 *
1161 * The offset value has curious semantics. It's a sort of running total
1162 * of the sizes of all the memory regions that aren't mapped in this
1163 * tad table.
1164 */
1165static int knl_get_tad(const struct sbridge_pvt *pvt,
1166 const int entry,
1167 const int mc,
1168 u64 *offset,
1169 u64 *limit,
1170 int *ways)
1171{
1172 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1173 struct pci_dev *pci_mc;
1174 int way_id;
1175
1176 switch (mc) {
1177 case 0:
1178 pci_mc = pvt->knl.pci_mc0;
1179 break;
1180 case 1:
1181 pci_mc = pvt->knl.pci_mc1;
1182 break;
1183 default:
1184 WARN_ON(1);
1185 return -EINVAL;
1186 }
1187
1188 pci_read_config_dword(pci_mc,
1189 knl_tad_dram_limit_lo[entry], &reg_limit_lo);
1190 pci_read_config_dword(pci_mc,
1191 knl_tad_dram_offset_lo[entry], &reg_offset_lo);
1192 pci_read_config_dword(pci_mc,
1193 knl_tad_dram_hi[entry], &reg_hi);
1194
1195 /* Is this TAD entry enabled? */
1196 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1197 return -ENODEV;
1198
1199 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1200
1201 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1202 *ways = knl_tad_ways[way_id];
1203 } else {
1204 *ways = 0;
1205 sbridge_printk(KERN_ERR,
1206 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1207 way_id);
1208 return -ENODEV;
1209 }
1210
1211 /*
1212 * The least significant 6 bits of base and limit are truncated.
1213 * For limit, we fill the missing bits with 1s.
1214 */
1215 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1216 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1217 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1218 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1219
1220 return 0;
1221}
1222
1223/* Determine which memory controller is responsible for a given channel. */
1224static int knl_channel_mc(int channel)
1225{
1226 WARN_ON(channel < 0 || channel >= 6);
1227
1228 return channel < 3 ? 1 : 0;
1229}
1230
1231/*
1232 * Get the Nth entry from EDC_ROUTE_TABLE register.
1233 * (This is the per-tile mapping of logical interleave targets to
1234 * physical EDC modules.)
1235 *
1236 * entry 0: 0:2
1237 * 1: 3:5
1238 * 2: 6:8
1239 * 3: 9:11
1240 * 4: 12:14
1241 * 5: 15:17
1242 * 6: 18:20
1243 * 7: 21:23
1244 * reserved: 24:31
1245 */
1246static u32 knl_get_edc_route(int entry, u32 reg)
1247{
1248 WARN_ON(entry >= KNL_MAX_EDCS);
1249 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1250}
1251
1252/*
1253 * Get the Nth entry from MC_ROUTE_TABLE register.
1254 * (This is the per-tile mapping of logical interleave targets to
1255 * physical DRAM channels modules.)
1256 *
1257 * entry 0: mc 0:2 channel 18:19
1258 * 1: mc 3:5 channel 20:21
1259 * 2: mc 6:8 channel 22:23
1260 * 3: mc 9:11 channel 24:25
1261 * 4: mc 12:14 channel 26:27
1262 * 5: mc 15:17 channel 28:29
1263 * reserved: 30:31
1264 *
1265 * Though we have 3 bits to identify the MC, we should only see
1266 * the values 0 or 1.
1267 */
1268
1269static u32 knl_get_mc_route(int entry, u32 reg)
1270{
1271 int mc, chan;
1272
1273 WARN_ON(entry >= KNL_MAX_CHANNELS);
1274
1275 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1276 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1277
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02001278 return knl_channel_remap(mc, chan);
Jim Snowd0cdf902015-12-03 10:48:54 +01001279}
1280
1281/*
1282 * Render the EDC_ROUTE register in human-readable form.
1283 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1284 */
1285static void knl_show_edc_route(u32 reg, char *s)
1286{
1287 int i;
1288
1289 for (i = 0; i < KNL_MAX_EDCS; i++) {
1290 s[i*2] = knl_get_edc_route(i, reg) + '0';
1291 s[i*2+1] = '-';
1292 }
1293
1294 s[KNL_MAX_EDCS*2 - 1] = '\0';
1295}
1296
1297/*
1298 * Render the MC_ROUTE register in human-readable form.
1299 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1300 */
1301static void knl_show_mc_route(u32 reg, char *s)
1302{
1303 int i;
1304
1305 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1306 s[i*2] = knl_get_mc_route(i, reg) + '0';
1307 s[i*2+1] = '-';
1308 }
1309
1310 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1311}
1312
1313#define KNL_EDC_ROUTE 0xb8
1314#define KNL_MC_ROUTE 0xb4
1315
1316/* Is this dram rule backed by regular DRAM in flat mode? */
1317#define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1318
1319/* Is this dram rule cached? */
1320#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1321
1322/* Is this rule backed by edc ? */
1323#define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1324
1325/* Is this rule backed by DRAM, cacheable in EDRAM? */
1326#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1327
1328/* Is this rule mod3? */
1329#define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1330
1331/*
1332 * Figure out how big our RAM modules are.
1333 *
1334 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1335 * have to figure this out from the SAD rules, interleave lists, route tables,
1336 * and TAD rules.
1337 *
1338 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1339 * inspect the TAD rules to figure out how large the SAD regions really are.
1340 *
1341 * When we know the real size of a SAD region and how many ways it's
1342 * interleaved, we know the individual contribution of each channel to
1343 * TAD is size/ways.
1344 *
1345 * Finally, we have to check whether each channel participates in each SAD
1346 * region.
1347 *
1348 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1349 * much memory the channel uses, we know the DIMM is at least that large.
1350 * (The BIOS might possibly choose not to map all available memory, in which
1351 * case we will underreport the size of the DIMM.)
1352 *
1353 * In theory, we could try to determine the EDC sizes as well, but that would
1354 * only work in flat mode, not in cache mode.
1355 *
1356 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1357 * elements)
1358 */
1359static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1360{
1361 u64 sad_base, sad_size, sad_limit = 0;
1362 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1363 int sad_rule = 0;
1364 int tad_rule = 0;
1365 int intrlv_ways, tad_ways;
1366 u32 first_pkg, pkg;
1367 int i;
1368 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1369 u32 dram_rule, interleave_reg;
1370 u32 mc_route_reg[KNL_MAX_CHAS];
1371 u32 edc_route_reg[KNL_MAX_CHAS];
1372 int edram_only;
1373 char edc_route_string[KNL_MAX_EDCS*2];
1374 char mc_route_string[KNL_MAX_CHANNELS*2];
1375 int cur_reg_start;
1376 int mc;
1377 int channel;
1378 int way;
1379 int participants[KNL_MAX_CHANNELS];
1380 int participant_count = 0;
1381
1382 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1383 mc_sizes[i] = 0;
1384
1385 /* Read the EDC route table in each CHA. */
1386 cur_reg_start = 0;
1387 for (i = 0; i < KNL_MAX_CHAS; i++) {
1388 pci_read_config_dword(pvt->knl.pci_cha[i],
1389 KNL_EDC_ROUTE, &edc_route_reg[i]);
1390
1391 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1392 knl_show_edc_route(edc_route_reg[i-1],
1393 edc_route_string);
1394 if (cur_reg_start == i-1)
1395 edac_dbg(0, "edc route table for CHA %d: %s\n",
1396 cur_reg_start, edc_route_string);
1397 else
1398 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1399 cur_reg_start, i-1, edc_route_string);
1400 cur_reg_start = i;
1401 }
1402 }
1403 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1404 if (cur_reg_start == i-1)
1405 edac_dbg(0, "edc route table for CHA %d: %s\n",
1406 cur_reg_start, edc_route_string);
1407 else
1408 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1409 cur_reg_start, i-1, edc_route_string);
1410
1411 /* Read the MC route table in each CHA. */
1412 cur_reg_start = 0;
1413 for (i = 0; i < KNL_MAX_CHAS; i++) {
1414 pci_read_config_dword(pvt->knl.pci_cha[i],
1415 KNL_MC_ROUTE, &mc_route_reg[i]);
1416
1417 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1418 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1419 if (cur_reg_start == i-1)
1420 edac_dbg(0, "mc route table for CHA %d: %s\n",
1421 cur_reg_start, mc_route_string);
1422 else
1423 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1424 cur_reg_start, i-1, mc_route_string);
1425 cur_reg_start = i;
1426 }
1427 }
1428 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1429 if (cur_reg_start == i-1)
1430 edac_dbg(0, "mc route table for CHA %d: %s\n",
1431 cur_reg_start, mc_route_string);
1432 else
1433 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1434 cur_reg_start, i-1, mc_route_string);
1435
1436 /* Process DRAM rules */
1437 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1438 /* previous limit becomes the new base */
1439 sad_base = sad_limit;
1440
1441 pci_read_config_dword(pvt->pci_sad0,
1442 pvt->info.dram_rule[sad_rule], &dram_rule);
1443
1444 if (!DRAM_RULE_ENABLE(dram_rule))
1445 break;
1446
1447 edram_only = KNL_EDRAM_ONLY(dram_rule);
1448
1449 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1450 sad_size = sad_limit - sad_base;
1451
1452 pci_read_config_dword(pvt->pci_sad0,
1453 pvt->info.interleave_list[sad_rule], &interleave_reg);
1454
1455 /*
1456 * Find out how many ways this dram rule is interleaved.
1457 * We stop when we see the first channel again.
1458 */
1459 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1460 interleave_reg, 0);
1461 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1462 pkg = sad_pkg(pvt->info.interleave_pkg,
1463 interleave_reg, intrlv_ways);
1464
1465 if ((pkg & 0x8) == 0) {
1466 /*
1467 * 0 bit means memory is non-local,
1468 * which KNL doesn't support
1469 */
1470 edac_dbg(0, "Unexpected interleave target %d\n",
1471 pkg);
1472 return -1;
1473 }
1474
1475 if (pkg == first_pkg)
1476 break;
1477 }
1478 if (KNL_MOD3(dram_rule))
1479 intrlv_ways *= 3;
1480
1481 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1482 sad_rule,
1483 sad_base,
1484 sad_limit,
1485 intrlv_ways,
1486 edram_only ? ", EDRAM" : "");
1487
1488 /*
1489 * Find out how big the SAD region really is by iterating
1490 * over TAD tables (SAD regions may contain holes).
1491 * Each memory controller might have a different TAD table, so
1492 * we have to look at both.
1493 *
1494 * Livespace is the memory that's mapped in this TAD table,
1495 * deadspace is the holes (this could be the MMIO hole, or it
1496 * could be memory that's mapped by the other TAD table but
1497 * not this one).
1498 */
1499 for (mc = 0; mc < 2; mc++) {
1500 sad_actual_size[mc] = 0;
1501 tad_livespace = 0;
1502 for (tad_rule = 0;
1503 tad_rule < ARRAY_SIZE(
1504 knl_tad_dram_limit_lo);
1505 tad_rule++) {
1506 if (knl_get_tad(pvt,
1507 tad_rule,
1508 mc,
1509 &tad_deadspace,
1510 &tad_limit,
1511 &tad_ways))
1512 break;
1513
1514 tad_size = (tad_limit+1) -
1515 (tad_livespace + tad_deadspace);
1516 tad_livespace += tad_size;
1517 tad_base = (tad_limit+1) - tad_size;
1518
1519 if (tad_base < sad_base) {
1520 if (tad_limit > sad_base)
1521 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1522 } else if (tad_base < sad_limit) {
1523 if (tad_limit+1 > sad_limit) {
1524 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1525 } else {
1526 /* TAD region is completely inside SAD region */
1527 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1528 tad_rule, tad_base,
1529 tad_limit, tad_size,
1530 mc);
1531 sad_actual_size[mc] += tad_size;
1532 }
1533 }
1534 tad_base = tad_limit+1;
1535 }
1536 }
1537
1538 for (mc = 0; mc < 2; mc++) {
1539 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1540 mc, sad_actual_size[mc], sad_actual_size[mc]);
1541 }
1542
1543 /* Ignore EDRAM rule */
1544 if (edram_only)
1545 continue;
1546
1547 /* Figure out which channels participate in interleave. */
1548 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1549 participants[channel] = 0;
1550
1551 /* For each channel, does at least one CHA have
1552 * this channel mapped to the given target?
1553 */
1554 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1555 for (way = 0; way < intrlv_ways; way++) {
1556 int target;
1557 int cha;
1558
1559 if (KNL_MOD3(dram_rule))
1560 target = way;
1561 else
1562 target = 0x7 & sad_pkg(
1563 pvt->info.interleave_pkg, interleave_reg, way);
1564
1565 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1566 if (knl_get_mc_route(target,
1567 mc_route_reg[cha]) == channel
Hubert Chrzaniuk83bdaad2016-03-07 15:30:45 +01001568 && !participants[channel]) {
Jim Snowd0cdf902015-12-03 10:48:54 +01001569 participant_count++;
1570 participants[channel] = 1;
1571 break;
1572 }
1573 }
1574 }
1575 }
1576
1577 if (participant_count != intrlv_ways)
1578 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1579 participant_count, intrlv_ways);
1580
1581 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1582 mc = knl_channel_mc(channel);
1583 if (participants[channel]) {
1584 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1585 channel,
1586 sad_actual_size[mc]/intrlv_ways,
1587 sad_rule);
1588 mc_sizes[channel] +=
1589 sad_actual_size[mc]/intrlv_ways;
1590 }
1591 }
1592 }
1593
1594 return 0;
1595}
1596
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001597static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001598{
1599 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001600 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001601 unsigned i, j, banks, ranks, rows, cols, npages;
1602 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001603 u32 reg;
1604 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -02001605 enum mem_type mtype;
Jim Snowd0cdf902015-12-03 10:48:54 +01001606 int channels = pvt->info.type == KNIGHTS_LANDING ?
1607 KNL_MAX_CHANNELS : NUM_CHANNELS;
1608 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001609
Tony Luckea5dfb52016-04-14 10:22:02 -07001610 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1611 pci_read_config_dword(pvt->pci_ha0, HASWELL_HASYSDEFEATURE2, &reg);
1612 pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
1613 }
Jim Snowd0cdf902015-12-03 10:48:54 +01001614 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1615 pvt->info.type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001616 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
1617 else
1618 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
1619
Jim Snowd0cdf902015-12-03 10:48:54 +01001620 if (pvt->info.type == KNIGHTS_LANDING)
1621 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1622 else
1623 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001624
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03001625 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -03001626 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1627 pvt->sbridge_dev->mc,
1628 pvt->sbridge_dev->node_id,
1629 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001630
Jim Snowd0cdf902015-12-03 10:48:54 +01001631 /* KNL doesn't support mirroring or lockstep,
1632 * and is always closed page
1633 */
1634 if (pvt->info.type == KNIGHTS_LANDING) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001635 mode = EDAC_S4ECD4ED;
Jim Snowd0cdf902015-12-03 10:48:54 +01001636 pvt->is_mirrored = false;
1637
1638 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1639 return -1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001640 } else {
Jim Snowd0cdf902015-12-03 10:48:54 +01001641 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
1642 if (IS_MIRROR_ENABLED(reg)) {
1643 edac_dbg(0, "Memory mirror is enabled\n");
1644 pvt->is_mirrored = true;
1645 } else {
1646 edac_dbg(0, "Memory mirror is disabled\n");
1647 pvt->is_mirrored = false;
1648 }
1649
1650 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
1651 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1652 edac_dbg(0, "Lockstep is enabled\n");
1653 mode = EDAC_S8ECD8ED;
1654 pvt->is_lockstep = true;
1655 } else {
1656 edac_dbg(0, "Lockstep is disabled\n");
1657 mode = EDAC_S4ECD4ED;
1658 pvt->is_lockstep = false;
1659 }
1660 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1661 edac_dbg(0, "address map is on closed page mode\n");
1662 pvt->is_close_pg = true;
1663 } else {
1664 edac_dbg(0, "address map is on open page mode\n");
1665 pvt->is_close_pg = false;
1666 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001667 }
1668
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001669 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001670 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001671 edac_dbg(0, "Memory is registered\n");
1672 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -07001673 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001674 else
1675 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001676
Tony Luckfec53af2014-12-02 09:41:58 -08001677 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001678 banks = 16;
1679 else
1680 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001681
Jim Snowd0cdf902015-12-03 10:48:54 +01001682 for (i = 0; i < channels; i++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001683 u32 mtr;
1684
Jim Snowd0cdf902015-12-03 10:48:54 +01001685 int max_dimms_per_channel;
1686
1687 if (pvt->info.type == KNIGHTS_LANDING) {
1688 max_dimms_per_channel = 1;
1689 if (!pvt->knl.pci_channel[i])
1690 continue;
1691 } else {
1692 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1693 if (!pvt->pci_tad[i])
1694 continue;
1695 }
1696
1697 for (j = 0; j < max_dimms_per_channel; j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001698 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1699 i, j, 0);
Jim Snowd0cdf902015-12-03 10:48:54 +01001700 if (pvt->info.type == KNIGHTS_LANDING) {
1701 pci_read_config_dword(pvt->knl.pci_channel[i],
1702 knl_mtr_reg, &mtr);
1703 } else {
1704 pci_read_config_dword(pvt->pci_tad[i],
1705 mtr_regs[j], &mtr);
1706 }
Joe Perches956b9ba2012-04-29 17:08:39 -03001707 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001708 if (IS_DIMM_PRESENT(mtr)) {
1709 pvt->channel[i].dimms++;
1710
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001711 ranks = numrank(pvt->info.type, mtr);
Jim Snowd0cdf902015-12-03 10:48:54 +01001712
1713 if (pvt->info.type == KNIGHTS_LANDING) {
1714 /* For DDR4, this is fixed. */
1715 cols = 1 << 10;
1716 rows = knl_mc_sizes[i] /
1717 ((u64) cols * ranks * banks * 8);
1718 } else {
1719 rows = numrow(mtr);
1720 cols = numcol(mtr);
1721 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001722
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001723 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001724 npages = MiB_TO_PAGES(size);
1725
Tony Luck7d375bf2015-05-18 17:50:42 -03001726 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1727 pvt->sbridge_dev->mc, i/4, i%4, j,
Joe Perches956b9ba2012-04-29 17:08:39 -03001728 size, npages,
1729 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001730
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -03001731 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001732 dimm->grain = 32;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04001733 dimm->dtype = pvt->info.get_width(pvt, mtr);
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001734 dimm->mtype = mtype;
1735 dimm->edac_mode = mode;
1736 snprintf(dimm->label, sizeof(dimm->label),
Tony Luck7d375bf2015-05-18 17:50:42 -03001737 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1738 pvt->sbridge_dev->source_id, i/4, i%4, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001739 }
1740 }
1741 }
1742
1743 return 0;
1744}
1745
1746static void get_memory_layout(const struct mem_ctl_info *mci)
1747{
1748 struct sbridge_pvt *pvt = mci->pvt_info;
1749 int i, j, k, n_sads, n_tads, sad_interl;
1750 u32 reg;
1751 u64 limit, prv = 0;
1752 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +01001753 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001754 u32 rir_way;
1755
1756 /*
1757 * Step 1) Get TOLM/TOHM ranges
1758 */
1759
Aristeu Rozanskifb79a502013-10-30 13:26:57 -03001760 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001761 tmp_mb = (1 + pvt->tolm) >> 20;
1762
Jim Snow8c009102014-11-18 14:51:09 +01001763 gb = div_u64_rem(tmp_mb, 1024, &mb);
1764 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1765 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001766
1767 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -03001768 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001769 tmp_mb = (1 + pvt->tohm) >> 20;
1770
Jim Snow8c009102014-11-18 14:51:09 +01001771 gb = div_u64_rem(tmp_mb, 1024, &mb);
1772 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1773 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001774
1775 /*
1776 * Step 2) Get SAD range and SAD Interleave list
1777 * TAD registers contain the interleave wayness. However, it
1778 * seems simpler to just discover it indirectly, with the
1779 * algorithm bellow.
1780 */
1781 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001782 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001783 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001784 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001785 &reg);
Jim Snowc59f9c02015-12-03 10:48:52 +01001786 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001787
1788 if (!DRAM_RULE_ENABLE(reg))
1789 continue;
1790
1791 if (limit <= prv)
1792 break;
1793
1794 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001795 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001796 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1797 n_sads,
Jim Snowc59f9c02015-12-03 10:48:52 +01001798 show_dram_attr(pvt->info.dram_attr(reg)),
Jim Snow8c009102014-11-18 14:51:09 +01001799 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001800 ((u64)tmp_mb) << 20L,
Nicolas Iooss127c1222017-01-22 18:28:06 +01001801 get_intlv_mode_str(reg, pvt->info.type),
Joe Perches956b9ba2012-04-29 17:08:39 -03001802 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001803 prv = limit;
1804
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001805 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001806 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001807 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001808 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001809 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1810 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001811 break;
1812
Joe Perches956b9ba2012-04-29 17:08:39 -03001813 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001814 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001815 }
1816 }
1817
Jim Snowd0cdf902015-12-03 10:48:54 +01001818 if (pvt->info.type == KNIGHTS_LANDING)
1819 return;
1820
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001821 /*
1822 * Step 3) Get TAD range
1823 */
1824 prv = 0;
1825 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1826 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1827 &reg);
1828 limit = TAD_LIMIT(reg);
1829 if (limit <= prv)
1830 break;
1831 tmp_mb = (limit + 1) >> 20;
1832
Jim Snow8c009102014-11-18 14:51:09 +01001833 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001834 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 +01001835 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001836 ((u64)tmp_mb) << 20L,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08001837 (u32)(1 << TAD_SOCK(reg)),
1838 (u32)TAD_CH(reg) + 1,
Joe Perches956b9ba2012-04-29 17:08:39 -03001839 (u32)TAD_TGT0(reg),
1840 (u32)TAD_TGT1(reg),
1841 (u32)TAD_TGT2(reg),
1842 (u32)TAD_TGT3(reg),
1843 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001844 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001845 }
1846
1847 /*
1848 * Step 4) Get TAD offsets, per each channel
1849 */
1850 for (i = 0; i < NUM_CHANNELS; i++) {
1851 if (!pvt->channel[i].dimms)
1852 continue;
1853 for (j = 0; j < n_tads; j++) {
1854 pci_read_config_dword(pvt->pci_tad[i],
1855 tad_ch_nilv_offset[j],
1856 &reg);
1857 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001858 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001859 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1860 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001861 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001862 ((u64)tmp_mb) << 20L,
1863 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001864 }
1865 }
1866
1867 /*
1868 * Step 6) Get RIR Wayness/Limit, per each channel
1869 */
1870 for (i = 0; i < NUM_CHANNELS; i++) {
1871 if (!pvt->channel[i].dimms)
1872 continue;
1873 for (j = 0; j < MAX_RIR_RANGES; j++) {
1874 pci_read_config_dword(pvt->pci_tad[i],
1875 rir_way_limit[j],
1876 &reg);
1877
1878 if (!IS_RIR_VALID(reg))
1879 continue;
1880
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001881 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001882 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001883 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001884 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1885 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001886 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001887 ((u64)tmp_mb) << 20L,
1888 rir_way,
1889 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001890
1891 for (k = 0; k < rir_way; k++) {
1892 pci_read_config_dword(pvt->pci_tad[i],
1893 rir_offset[j][k],
1894 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07001895 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001896
Jim Snow8c009102014-11-18 14:51:09 +01001897 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001898 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1899 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001900 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001901 ((u64)tmp_mb) << 20L,
Tony Luckc7103f62016-05-31 11:50:28 -07001902 (u32)RIR_RNK_TGT(pvt->info.type, reg),
Joe Perches956b9ba2012-04-29 17:08:39 -03001903 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001904 }
1905 }
1906 }
1907}
1908
Rashika Kheria8112c0c2013-12-14 19:32:09 +05301909static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001910{
1911 struct sbridge_dev *sbridge_dev;
1912
1913 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1914 if (sbridge_dev->node_id == node_id)
1915 return sbridge_dev->mci;
1916 }
1917 return NULL;
1918}
1919
1920static int get_memory_error_data(struct mem_ctl_info *mci,
1921 u64 addr,
Tony Luck7d375bf2015-05-18 17:50:42 -03001922 u8 *socket, u8 *ha,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001923 long *channel_mask,
1924 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001925 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001926{
1927 struct mem_ctl_info *new_mci;
1928 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001929 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001930 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001931 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001932 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001933 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001934 u32 reg, dram_rule;
Tony Luck7d375bf2015-05-18 17:50:42 -03001935 u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001936 u32 tad_offset;
1937 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001938 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001939 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001940
1941
1942 /*
1943 * Step 0) Check if the address is at special memory ranges
1944 * The check bellow is probably enough to fill all cases where
1945 * the error is not inside a memory, except for the legacy
1946 * range (e. g. VGA addresses). It is unlikely, however, that the
1947 * memory controller would generate an error on that range.
1948 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001949 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001950 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001951 return -EINVAL;
1952 }
1953 if (addr >= (u64)pvt->tohm) {
1954 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001955 return -EINVAL;
1956 }
1957
1958 /*
1959 * Step 1) Get socket
1960 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001961 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1962 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001963 &reg);
1964
1965 if (!DRAM_RULE_ENABLE(reg))
1966 continue;
1967
Jim Snowc59f9c02015-12-03 10:48:52 +01001968 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001969 if (limit <= prv) {
1970 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001971 return -EINVAL;
1972 }
1973 if (addr <= limit)
1974 break;
1975 prv = limit;
1976 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001977 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001978 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001979 return -EINVAL;
1980 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001981 dram_rule = reg;
Jim Snowc59f9c02015-12-03 10:48:52 +01001982 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1983 interleave_mode = pvt->info.interleave_mode(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001984
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001985 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001986 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001987
1988 if (pvt->info.type == SANDY_BRIDGE) {
1989 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1990 for (sad_way = 0; sad_way < 8; sad_way++) {
1991 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1992 if (sad_way > 0 && sad_interl == pkg)
1993 break;
1994 sad_interleave[sad_way] = pkg;
1995 edac_dbg(0, "SAD interleave #%d: %d\n",
1996 sad_way, sad_interleave[sad_way]);
1997 }
1998 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1999 pvt->sbridge_dev->mc,
2000 n_sads,
2001 addr,
2002 limit,
2003 sad_way + 7,
2004 !interleave_mode ? "" : "XOR[18:16]");
2005 if (interleave_mode)
2006 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
2007 else
2008 idx = (addr >> 6) & 7;
2009 switch (sad_way) {
2010 case 1:
2011 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002012 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002013 case 2:
2014 idx = idx & 1;
2015 break;
2016 case 4:
2017 idx = idx & 3;
2018 break;
2019 case 8:
2020 break;
2021 default:
2022 sprintf(msg, "Can't discover socket interleave");
2023 return -EINVAL;
2024 }
2025 *socket = sad_interleave[idx];
2026 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2027 idx, sad_way, *socket);
Tony Luck1f395812014-12-02 09:27:30 -08002028 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002029 int bits, a7mode = A7MODE(dram_rule);
2030
2031 if (a7mode) {
2032 /* A7 mode swaps P9 with P6 */
2033 bits = GET_BITFIELD(addr, 7, 8) << 1;
2034 bits |= GET_BITFIELD(addr, 9, 9);
2035 } else
Tony Luckbb89e712015-05-18 17:39:06 -03002036 bits = GET_BITFIELD(addr, 6, 8);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002037
Tony Luckbb89e712015-05-18 17:39:06 -03002038 if (interleave_mode == 0) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002039 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2040 idx = GET_BITFIELD(addr, 16, 18);
2041 idx ^= bits;
2042 } else
2043 idx = bits;
2044
2045 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2046 *socket = sad_pkg_socket(pkg);
2047 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002048 if (sad_ha)
2049 ch_add = 4;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002050
2051 if (a7mode) {
2052 /* MCChanShiftUpEnable */
2053 pci_read_config_dword(pvt->pci_ha0,
2054 HASWELL_HASYSDEFEATURE2, &reg);
2055 shiftup = GET_BITFIELD(reg, 22, 22);
2056 }
2057
2058 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2059 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002060 } else {
2061 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002062 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002063 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2064 *socket = sad_pkg_socket(pkg);
2065 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002066 if (sad_ha)
2067 ch_add = 4;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002068 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2069 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002070 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002071
Tony Luck7d375bf2015-05-18 17:50:42 -03002072 *ha = sad_ha;
2073
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002074 /*
2075 * Move to the proper node structure, in order to access the
2076 * right PCI registers
2077 */
2078 new_mci = get_mci_for_node_id(*socket);
2079 if (!new_mci) {
2080 sprintf(msg, "Struct for socket #%u wasn't initialized",
2081 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002082 return -EINVAL;
2083 }
2084 mci = new_mci;
2085 pvt = mci->pvt_info;
2086
2087 /*
2088 * Step 2) Get memory channel
2089 */
2090 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002091 if (pvt->info.type == SANDY_BRIDGE)
2092 pci_ha = pvt->pci_ha0;
2093 else {
2094 if (sad_ha)
2095 pci_ha = pvt->pci_ha1;
2096 else
2097 pci_ha = pvt->pci_ha0;
2098 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002099 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002100 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002101 limit = TAD_LIMIT(reg);
2102 if (limit <= prv) {
2103 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002104 return -EINVAL;
2105 }
2106 if (addr <= limit)
2107 break;
2108 prv = limit;
2109 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002110 if (n_tads == MAX_TAD) {
2111 sprintf(msg, "Can't discover the memory channel");
2112 return -EINVAL;
2113 }
2114
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002115 ch_way = TAD_CH(reg) + 1;
Tony Luckff15e952016-04-14 10:21:52 -07002116 sck_way = TAD_SOCK(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002117
2118 if (ch_way == 3)
2119 idx = addr >> 6;
Tony Luckea5dfb52016-04-14 10:22:02 -07002120 else {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002121 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Tony Luckea5dfb52016-04-14 10:22:02 -07002122 if (pvt->is_chan_hash)
2123 idx = haswell_chan_hash(idx, addr);
2124 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002125 idx = idx % ch_way;
2126
2127 /*
2128 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2129 */
2130 switch (idx) {
2131 case 0:
2132 base_ch = TAD_TGT0(reg);
2133 break;
2134 case 1:
2135 base_ch = TAD_TGT1(reg);
2136 break;
2137 case 2:
2138 base_ch = TAD_TGT2(reg);
2139 break;
2140 case 3:
2141 base_ch = TAD_TGT3(reg);
2142 break;
2143 default:
2144 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002145 return -EINVAL;
2146 }
2147 *channel_mask = 1 << base_ch;
2148
Tony Luck7d375bf2015-05-18 17:50:42 -03002149 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002150 tad_ch_nilv_offset[n_tads],
2151 &tad_offset);
2152
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002153 if (pvt->is_mirrored) {
2154 *channel_mask |= 1 << ((base_ch + 2) % 4);
2155 switch(ch_way) {
2156 case 2:
2157 case 4:
Tony Luckff15e952016-04-14 10:21:52 -07002158 sck_xch = (1 << sck_way) * (ch_way >> 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002159 break;
2160 default:
2161 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002162 return -EINVAL;
2163 }
2164 } else
2165 sck_xch = (1 << sck_way) * ch_way;
2166
2167 if (pvt->is_lockstep)
2168 *channel_mask |= 1 << ((base_ch + 1) % 4);
2169
2170 offset = TAD_OFFSET(tad_offset);
2171
Joe Perches956b9ba2012-04-29 17:08:39 -03002172 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",
2173 n_tads,
2174 addr,
2175 limit,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002176 sck_way,
Joe Perches956b9ba2012-04-29 17:08:39 -03002177 ch_way,
2178 offset,
2179 idx,
2180 base_ch,
2181 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002182
2183 /* Calculate channel address */
2184 /* Remove the TAD offset */
2185
2186 if (offset > addr) {
2187 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2188 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002189 return -EINVAL;
2190 }
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002191
2192 ch_addr = addr - offset;
2193 ch_addr >>= (6 + shiftup);
Tony Luckff15e952016-04-14 10:21:52 -07002194 ch_addr /= sck_xch;
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002195 ch_addr <<= (6 + shiftup);
2196 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002197
2198 /*
2199 * Step 3) Decode rank
2200 */
2201 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
Tony Luck7d375bf2015-05-18 17:50:42 -03002202 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002203 rir_way_limit[n_rir],
2204 &reg);
2205
2206 if (!IS_RIR_VALID(reg))
2207 continue;
2208
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002209 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01002210 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03002211 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2212 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01002213 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03002214 limit,
2215 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002216 if (ch_addr <= limit)
2217 break;
2218 }
2219 if (n_rir == MAX_RIR_RANGES) {
2220 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2221 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002222 return -EINVAL;
2223 }
2224 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002225
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002226 if (pvt->is_close_pg)
2227 idx = (ch_addr >> 6);
2228 else
2229 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2230 idx %= 1 << rir_way;
2231
Tony Luck7d375bf2015-05-18 17:50:42 -03002232 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002233 rir_offset[n_rir][idx],
2234 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07002235 *rank = RIR_RNK_TGT(pvt->info.type, reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002236
Joe Perches956b9ba2012-04-29 17:08:39 -03002237 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2238 n_rir,
2239 ch_addr,
2240 limit,
2241 rir_way,
2242 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002243
2244 return 0;
2245}
2246
2247/****************************************************************************
2248 Device initialization routines: put/get, init/exit
2249 ****************************************************************************/
2250
2251/*
2252 * sbridge_put_all_devices 'put' all the devices that we have
2253 * reserved via 'get'
2254 */
2255static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2256{
2257 int i;
2258
Joe Perches956b9ba2012-04-29 17:08:39 -03002259 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002260 for (i = 0; i < sbridge_dev->n_devs; i++) {
2261 struct pci_dev *pdev = sbridge_dev->pdev[i];
2262 if (!pdev)
2263 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03002264 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2265 pdev->bus->number,
2266 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002267 pci_dev_put(pdev);
2268 }
2269}
2270
2271static void sbridge_put_all_devices(void)
2272{
2273 struct sbridge_dev *sbridge_dev, *tmp;
2274
2275 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2276 sbridge_put_devices(sbridge_dev);
2277 free_sbridge_dev(sbridge_dev);
2278 }
2279}
2280
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002281static int sbridge_get_onedevice(struct pci_dev **prev,
2282 u8 *num_mc,
2283 const struct pci_id_table *table,
Jim Snowc1979ba2015-12-03 10:48:53 +01002284 const unsigned devno,
2285 const int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002286{
2287 struct sbridge_dev *sbridge_dev;
2288 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002289 struct pci_dev *pdev = NULL;
2290 u8 bus = 0;
2291
Jiang Liuec5a0b32014-02-17 13:10:23 +08002292 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002293 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002294 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2295
2296 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2297 dev_descr->dev_id, *prev);
2298
2299 if (!pdev) {
2300 if (*prev) {
2301 *prev = pdev;
2302 return 0;
2303 }
2304
2305 if (dev_descr->optional)
2306 return 0;
2307
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002308 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002309 if (devno == 0)
2310 return -ENODEV;
2311
2312 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002313 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002314 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2315
2316 /* End of list, leave */
2317 return -ENODEV;
2318 }
2319 bus = pdev->bus->number;
2320
Jim Snowc1979ba2015-12-03 10:48:53 +01002321 sbridge_dev = get_sbridge_dev(bus, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002322 if (!sbridge_dev) {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002323 sbridge_dev = alloc_sbridge_dev(bus, dev_descr->dom, table);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002324 if (!sbridge_dev) {
2325 pci_dev_put(pdev);
2326 return -ENOMEM;
2327 }
2328 (*num_mc)++;
2329 }
2330
2331 if (sbridge_dev->pdev[devno]) {
2332 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002333 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002334 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2335 pci_dev_put(pdev);
2336 return -ENODEV;
2337 }
2338
2339 sbridge_dev->pdev[devno] = pdev;
2340
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002341 /* Be sure that the device is enabled */
2342 if (unlikely(pci_enable_device(pdev) < 0)) {
2343 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002344 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002345 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2346 return -ENODEV;
2347 }
2348
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002349 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -03002350 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002351
2352 /*
2353 * As stated on drivers/pci/search.c, the reference count for
2354 * @from is always decremented if it is not %NULL. So, as we need
2355 * to get all devices up to null, we need to do a get for the device
2356 */
2357 pci_dev_get(pdev);
2358
2359 *prev = pdev;
2360
2361 return 0;
2362}
2363
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002364/*
2365 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002366 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002367 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03002368 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002369 * @table: model specific table
2370 *
2371 * returns 0 in case of success or error code
2372 */
Tony Luck0ba169ac2016-07-14 15:38:43 -07002373static int sbridge_get_all_devices(u8 *num_mc,
2374 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002375{
2376 int i, rc;
2377 struct pci_dev *pdev = NULL;
Tony Luck0ba169ac2016-07-14 15:38:43 -07002378 int allow_dups = 0;
2379 int multi_bus = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002380
Tony Luck0ba169ac2016-07-14 15:38:43 -07002381 if (table->type == KNIGHTS_LANDING)
2382 allow_dups = multi_bus = 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002383 while (table && table->descr) {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002384 for (i = 0; i < table->n_devs_per_sock; i++) {
Jim Snowc1979ba2015-12-03 10:48:53 +01002385 if (!allow_dups || i == 0 ||
2386 table->descr[i].dev_id !=
2387 table->descr[i-1].dev_id) {
2388 pdev = NULL;
2389 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002390 do {
2391 rc = sbridge_get_onedevice(&pdev, num_mc,
Jim Snowc1979ba2015-12-03 10:48:53 +01002392 table, i, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002393 if (rc < 0) {
2394 if (i == 0) {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002395 i = table->n_devs_per_sock;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002396 break;
2397 }
2398 sbridge_put_all_devices();
2399 return -ENODEV;
2400 }
Jim Snowc1979ba2015-12-03 10:48:53 +01002401 } while (pdev && !allow_dups);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002402 }
2403 table++;
2404 }
2405
2406 return 0;
2407}
2408
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03002409static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2410 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002411{
2412 struct sbridge_pvt *pvt = mci->pvt_info;
2413 struct pci_dev *pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002414 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002415 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002416
2417 for (i = 0; i < sbridge_dev->n_devs; i++) {
2418 pdev = sbridge_dev->pdev[i];
2419 if (!pdev)
2420 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002421
2422 switch (pdev->device) {
2423 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2424 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002425 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002426 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2427 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002428 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002429 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2430 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002431 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002432 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2433 pvt->pci_ha0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002434 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002435 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2436 pvt->pci_ta = pdev;
2437 break;
2438 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2439 pvt->pci_ras = pdev;
2440 break;
2441 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2442 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2443 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2444 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2445 {
2446 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
2447 pvt->pci_tad[id] = pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002448 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002449 }
2450 break;
2451 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2452 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002453 break;
2454 default:
2455 goto error;
2456 }
2457
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002458 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2459 pdev->vendor, pdev->device,
Joe Perches956b9ba2012-04-29 17:08:39 -03002460 sbridge_dev->bus,
Joe Perches956b9ba2012-04-29 17:08:39 -03002461 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002462 }
2463
2464 /* Check if everything were registered */
2465 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002466 !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002467 goto enodev;
2468
Seth Jennings2900ea62015-08-05 13:16:01 -05002469 if (saw_chan_mask != 0x0f)
2470 goto enodev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002471 return 0;
2472
2473enodev:
2474 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2475 return -ENODEV;
2476
2477error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002478 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2479 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002480 return -EINVAL;
2481}
2482
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002483static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2484 struct sbridge_dev *sbridge_dev)
2485{
2486 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002487 struct pci_dev *pdev;
2488 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002489 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002490
2491 for (i = 0; i < sbridge_dev->n_devs; i++) {
2492 pdev = sbridge_dev->pdev[i];
2493 if (!pdev)
2494 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002495
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002496 switch (pdev->device) {
2497 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
2498 pvt->pci_ha0 = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002499 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002500 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2501 pvt->pci_ta = pdev;
2502 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
2503 pvt->pci_ras = pdev;
2504 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002505 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2506 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002507 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2508 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002509 {
2510 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
2511 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002512 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002513 }
2514 break;
2515 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2516 pvt->pci_ddrio = pdev;
2517 break;
2518 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
Tony Luck7d375bf2015-05-18 17:50:42 -03002519 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002520 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002521 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2522 pvt->pci_sad0 = pdev;
2523 break;
2524 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2525 pvt->pci_br0 = pdev;
2526 break;
2527 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2528 pvt->pci_br1 = pdev;
2529 break;
2530 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
2531 pvt->pci_ha1 = pdev;
2532 break;
2533 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2534 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002535 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2536 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002537 {
Tony Luck7d375bf2015-05-18 17:50:42 -03002538 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002539 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002540 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002541 }
2542 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002543 default:
2544 goto error;
2545 }
2546
2547 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2548 sbridge_dev->bus,
2549 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2550 pdev);
2551 }
2552
2553 /* Check if everything were registered */
2554 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002555 !pvt->pci_br1 || !pvt->pci_ras || !pvt->pci_ta)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002556 goto enodev;
2557
Tony Luck7d375bf2015-05-18 17:50:42 -03002558 if (saw_chan_mask != 0x0f && /* -EN */
2559 saw_chan_mask != 0x33 && /* -EP */
2560 saw_chan_mask != 0xff) /* -EX */
2561 goto enodev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002562 return 0;
2563
2564enodev:
2565 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2566 return -ENODEV;
2567
2568error:
2569 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002570 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2571 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002572 return -EINVAL;
2573}
2574
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002575static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2576 struct sbridge_dev *sbridge_dev)
2577{
2578 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002579 struct pci_dev *pdev;
2580 u8 saw_chan_mask = 0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002581 int i;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002582
2583 /* there's only one device per system; not tied to any bus */
2584 if (pvt->info.pci_vtd == NULL)
2585 /* result will be checked later */
2586 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2587 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2588 NULL);
2589
2590 for (i = 0; i < sbridge_dev->n_devs; i++) {
2591 pdev = sbridge_dev->pdev[i];
2592 if (!pdev)
2593 continue;
2594
2595 switch (pdev->device) {
2596 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2597 pvt->pci_sad0 = pdev;
2598 break;
2599 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2600 pvt->pci_sad1 = pdev;
2601 break;
2602 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2603 pvt->pci_ha0 = pdev;
2604 break;
2605 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2606 pvt->pci_ta = pdev;
2607 break;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002608 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002609 pvt->pci_ras = pdev;
2610 break;
2611 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002612 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002613 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002614 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
Tony Luck7d375bf2015-05-18 17:50:42 -03002615 {
2616 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
2617
2618 pvt->pci_tad[id] = pdev;
2619 saw_chan_mask |= 1 << id;
2620 }
2621 break;
2622 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2623 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2624 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2625 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2626 {
2627 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
2628
2629 pvt->pci_tad[id] = pdev;
2630 saw_chan_mask |= 1 << id;
2631 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002632 break;
2633 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
Aristeu Rozanski71793852015-06-12 09:44:52 -04002634 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2635 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2636 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2637 if (!pvt->pci_ddrio)
2638 pvt->pci_ddrio = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002639 break;
2640 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
2641 pvt->pci_ha1 = pdev;
2642 break;
2643 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
2644 pvt->pci_ha1_ta = pdev;
2645 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002646 default:
2647 break;
2648 }
2649
2650 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2651 sbridge_dev->bus,
2652 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2653 pdev);
2654 }
2655
2656 /* Check if everything were registered */
2657 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2658 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2659 goto enodev;
2660
Tony Luck7d375bf2015-05-18 17:50:42 -03002661 if (saw_chan_mask != 0x0f && /* -EN */
2662 saw_chan_mask != 0x33 && /* -EP */
2663 saw_chan_mask != 0xff) /* -EX */
2664 goto enodev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002665 return 0;
2666
2667enodev:
2668 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2669 return -ENODEV;
2670}
2671
Tony Luck1f395812014-12-02 09:27:30 -08002672static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2673 struct sbridge_dev *sbridge_dev)
2674{
2675 struct sbridge_pvt *pvt = mci->pvt_info;
2676 struct pci_dev *pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002677 u8 saw_chan_mask = 0;
Tony Luck1f395812014-12-02 09:27:30 -08002678 int i;
2679
2680 /* there's only one device per system; not tied to any bus */
2681 if (pvt->info.pci_vtd == NULL)
2682 /* result will be checked later */
2683 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2684 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2685 NULL);
2686
2687 for (i = 0; i < sbridge_dev->n_devs; i++) {
2688 pdev = sbridge_dev->pdev[i];
2689 if (!pdev)
2690 continue;
2691
2692 switch (pdev->device) {
2693 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2694 pvt->pci_sad0 = pdev;
2695 break;
2696 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2697 pvt->pci_sad1 = pdev;
2698 break;
2699 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2700 pvt->pci_ha0 = pdev;
2701 break;
2702 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2703 pvt->pci_ta = pdev;
2704 break;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002705 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM:
Tony Luck1f395812014-12-02 09:27:30 -08002706 pvt->pci_ras = pdev;
2707 break;
2708 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
Tony Luck1f395812014-12-02 09:27:30 -08002709 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
Tony Luck1f395812014-12-02 09:27:30 -08002710 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
Tony Luck1f395812014-12-02 09:27:30 -08002711 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
Tony Luckfa2ce642015-05-20 19:10:35 -03002712 {
2713 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
2714 pvt->pci_tad[id] = pdev;
2715 saw_chan_mask |= 1 << id;
2716 }
2717 break;
2718 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2719 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2720 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2721 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2722 {
2723 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
2724 pvt->pci_tad[id] = pdev;
2725 saw_chan_mask |= 1 << id;
2726 }
Tony Luck1f395812014-12-02 09:27:30 -08002727 break;
2728 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2729 pvt->pci_ddrio = pdev;
2730 break;
Tony Luckfa2ce642015-05-20 19:10:35 -03002731 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2732 pvt->pci_ha1 = pdev;
2733 break;
2734 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2735 pvt->pci_ha1_ta = pdev;
2736 break;
Tony Luck1f395812014-12-02 09:27:30 -08002737 default:
2738 break;
2739 }
2740
2741 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2742 sbridge_dev->bus,
2743 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2744 pdev);
2745 }
2746
2747 /* Check if everything were registered */
2748 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2749 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2750 goto enodev;
2751
Tony Luckfa2ce642015-05-20 19:10:35 -03002752 if (saw_chan_mask != 0x0f && /* -EN */
2753 saw_chan_mask != 0x33 && /* -EP */
2754 saw_chan_mask != 0xff) /* -EX */
2755 goto enodev;
Tony Luck1f395812014-12-02 09:27:30 -08002756 return 0;
2757
2758enodev:
2759 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2760 return -ENODEV;
2761}
2762
Jim Snowd0cdf902015-12-03 10:48:54 +01002763static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2764 struct sbridge_dev *sbridge_dev)
2765{
2766 struct sbridge_pvt *pvt = mci->pvt_info;
2767 struct pci_dev *pdev;
2768 int dev, func;
2769
2770 int i;
2771 int devidx;
2772
2773 for (i = 0; i < sbridge_dev->n_devs; i++) {
2774 pdev = sbridge_dev->pdev[i];
2775 if (!pdev)
2776 continue;
2777
2778 /* Extract PCI device and function. */
2779 dev = (pdev->devfn >> 3) & 0x1f;
2780 func = pdev->devfn & 0x7;
2781
2782 switch (pdev->device) {
2783 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2784 if (dev == 8)
2785 pvt->knl.pci_mc0 = pdev;
2786 else if (dev == 9)
2787 pvt->knl.pci_mc1 = pdev;
2788 else {
2789 sbridge_printk(KERN_ERR,
2790 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2791 dev, func);
2792 continue;
2793 }
2794 break;
2795
2796 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2797 pvt->pci_sad0 = pdev;
2798 break;
2799
2800 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2801 pvt->pci_sad1 = pdev;
2802 break;
2803
2804 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2805 /* There are one of these per tile, and range from
2806 * 1.14.0 to 1.18.5.
2807 */
2808 devidx = ((dev-14)*8)+func;
2809
2810 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2811 sbridge_printk(KERN_ERR,
2812 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2813 dev, func);
2814 continue;
2815 }
2816
2817 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2818
2819 pvt->knl.pci_cha[devidx] = pdev;
2820 break;
2821
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002822 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN:
Jim Snowd0cdf902015-12-03 10:48:54 +01002823 devidx = -1;
2824
2825 /*
2826 * MC0 channels 0-2 are device 9 function 2-4,
2827 * MC1 channels 3-5 are device 8 function 2-4.
2828 */
2829
2830 if (dev == 9)
2831 devidx = func-2;
2832 else if (dev == 8)
2833 devidx = 3 + (func-2);
2834
2835 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2836 sbridge_printk(KERN_ERR,
2837 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2838 dev, func);
2839 continue;
2840 }
2841
2842 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2843 pvt->knl.pci_channel[devidx] = pdev;
2844 break;
2845
2846 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2847 pvt->knl.pci_mc_info = pdev;
2848 break;
2849
2850 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2851 pvt->pci_ta = pdev;
2852 break;
2853
2854 default:
2855 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2856 pdev->device);
2857 break;
2858 }
2859 }
2860
2861 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2862 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2863 !pvt->pci_ta) {
2864 goto enodev;
2865 }
2866
2867 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2868 if (!pvt->knl.pci_channel[i]) {
2869 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2870 goto enodev;
2871 }
2872 }
2873
2874 for (i = 0; i < KNL_MAX_CHAS; i++) {
2875 if (!pvt->knl.pci_cha[i]) {
2876 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2877 goto enodev;
2878 }
2879 }
2880
2881 return 0;
2882
2883enodev:
2884 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2885 return -ENODEV;
2886}
2887
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002888/****************************************************************************
2889 Error check routines
2890 ****************************************************************************/
2891
2892/*
2893 * While Sandy Bridge has error count registers, SMI BIOS read values from
2894 * and resets the counters. So, they are not reliable for the OS to read
2895 * from them. So, we have no option but to just trust on whatever MCE is
2896 * telling us about the errors.
2897 */
2898static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2899 const struct mce *m)
2900{
2901 struct mem_ctl_info *new_mci;
2902 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002903 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002904 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002905 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2906 bool overflow = GET_BITFIELD(m->status, 62, 62);
2907 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002908 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002909 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2910 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2911 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2912 u32 channel = GET_BITFIELD(m->status, 0, 3);
2913 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2914 long channel_mask, first_channel;
Tony Luck7d375bf2015-05-18 17:50:42 -03002915 u8 rank, socket, ha;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002916 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002917 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002918
Tony Luckfa2ce642015-05-20 19:10:35 -03002919 if (pvt->info.type != SANDY_BRIDGE)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002920 recoverable = true;
2921 else
2922 recoverable = GET_BITFIELD(m->status, 56, 56);
2923
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002924 if (uncorrected_error) {
2925 if (ripv) {
2926 type = "FATAL";
2927 tp_event = HW_EVENT_ERR_FATAL;
2928 } else {
2929 type = "NON_FATAL";
2930 tp_event = HW_EVENT_ERR_UNCORRECTED;
2931 }
2932 } else {
2933 type = "CORRECTED";
2934 tp_event = HW_EVENT_ERR_CORRECTED;
2935 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002936
2937 /*
David Mackey15ed1032012-04-17 11:30:52 -07002938 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002939 * memory errors should fit in this mask:
2940 * 000f 0000 1mmm cccc (binary)
2941 * where:
2942 * f = Correction Report Filtering Bit. If 1, subsequent errors
2943 * won't be shown
2944 * mmm = error type
2945 * cccc = channel
2946 * If the mask doesn't match, report an error to the parsing logic
2947 */
2948 if (! ((errcode & 0xef80) == 0x80)) {
2949 optype = "Can't parse: it is not a mem";
2950 } else {
2951 switch (optypenum) {
2952 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002953 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002954 break;
2955 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002956 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002957 break;
2958 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002959 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002960 break;
2961 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002962 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002963 break;
2964 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002965 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002966 break;
2967 default:
2968 optype = "reserved";
2969 break;
2970 }
2971 }
2972
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03002973 /* Only decode errors with an valid address (ADDRV) */
2974 if (!GET_BITFIELD(m->status, 58, 58))
2975 return;
2976
Jim Snowd0cdf902015-12-03 10:48:54 +01002977 if (pvt->info.type == KNIGHTS_LANDING) {
2978 if (channel == 14) {
2979 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2980 overflow ? " OVERFLOW" : "",
2981 (uncorrected_error && recoverable)
2982 ? " recoverable" : "",
2983 mscod, errcode,
2984 m->bank);
2985 } else {
2986 char A = *("A");
2987
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02002988 /*
2989 * Reported channel is in range 0-2, so we can't map it
2990 * back to mc. To figure out mc we check machine check
2991 * bank register that reported this error.
2992 * bank15 means mc0 and bank16 means mc1.
2993 */
2994 channel = knl_channel_remap(m->bank == 16, channel);
Jim Snowd0cdf902015-12-03 10:48:54 +01002995 channel_mask = 1 << channel;
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02002996
Jim Snowd0cdf902015-12-03 10:48:54 +01002997 snprintf(msg, sizeof(msg),
2998 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
2999 overflow ? " OVERFLOW" : "",
3000 (uncorrected_error && recoverable)
3001 ? " recoverable" : " ",
3002 mscod, errcode, channel, A + channel);
3003 edac_mc_handle_error(tp_event, mci, core_err_cnt,
3004 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
3005 channel, 0, -1,
3006 optype, msg);
3007 }
3008 return;
3009 } else {
3010 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
3011 &channel_mask, &rank, &area_type, msg);
3012 }
3013
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003014 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003015 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003016 new_mci = get_mci_for_node_id(socket);
3017 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003018 strcpy(msg, "Error: socket got corrupted!");
3019 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003020 }
3021 mci = new_mci;
3022 pvt = mci->pvt_info;
3023
3024 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
3025
3026 if (rank < 4)
3027 dimm = 0;
3028 else if (rank < 8)
3029 dimm = 1;
3030 else
3031 dimm = 2;
3032
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003033
3034 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003035 * FIXME: On some memory configurations (mirror, lockstep), the
3036 * Memory Controller can't point the error to a single DIMM. The
3037 * EDAC core should be handling the channel mask, in order to point
3038 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003039 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03003040 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
3041 channel = first_channel;
3042
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003043 snprintf(msg, sizeof(msg),
Tony Luck7d375bf2015-05-18 17:50:42 -03003044 "%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 -03003045 overflow ? " OVERFLOW" : "",
3046 (uncorrected_error && recoverable) ? " recoverable" : "",
3047 area_type,
3048 mscod, errcode,
Tony Luck7d375bf2015-05-18 17:50:42 -03003049 socket, ha,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003050 channel_mask,
3051 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003052
Joe Perches956b9ba2012-04-29 17:08:39 -03003053 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003054
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003055 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003056
Seth Jennings351fc4a2014-09-05 14:28:47 -05003057 if (channel == CHANNEL_UNSPECIFIED)
3058 channel = -1;
3059
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003060 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003061 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003062 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
Tony Luck7d375bf2015-05-18 17:50:42 -03003063 4*ha+channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003064 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003065 return;
3066err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003067 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003068 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003069 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003070
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003071}
3072
3073/*
Tony Luckad08c4e2016-04-15 14:50:32 -07003074 * Check that logging is enabled and that this is the right type
3075 * of error for us to handle.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003076 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003077static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3078 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003079{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003080 struct mce *mce = (struct mce *)data;
3081 struct mem_ctl_info *mci;
3082 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003083 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003084
Borislav Petkovbffc7de2017-02-04 18:10:14 +01003085 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
Chen, Gongfd521032013-12-06 01:17:09 -05003086 return NOTIFY_DONE;
3087
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003088 mci = get_mci_for_node_id(mce->socketid);
3089 if (!mci)
Tony Luckc4fc1952016-04-29 15:42:25 +02003090 return NOTIFY_DONE;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003091 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003092
3093 /*
3094 * Just let mcelog handle it if the error is
3095 * outside the memory controller. A memory error
3096 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3097 * bit 12 has an special meaning.
3098 */
3099 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003100 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003101
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003102 if (mce->mcgstatus & MCG_STATUS_MCIP)
3103 type = "Exception";
3104 else
3105 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003106
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003107 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003108
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003109 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3110 "Bank %d: %016Lx\n", mce->extcpu, type,
3111 mce->mcgstatus, mce->bank, mce->status);
3112 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3113 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3114 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003115
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003116 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3117 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3118 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003119
Tony Luckad08c4e2016-04-15 14:50:32 -07003120 sbridge_mce_output_error(mci, mce);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003121
3122 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003123 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003124}
3125
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003126static struct notifier_block sbridge_mce_dec = {
Borislav Petkov9026cc82017-01-23 19:35:14 +01003127 .notifier_call = sbridge_mce_check_error,
3128 .priority = MCE_PRIO_EDAC,
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003129};
3130
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003131/****************************************************************************
3132 EDAC register/unregister logic
3133 ****************************************************************************/
3134
3135static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3136{
3137 struct mem_ctl_info *mci = sbridge_dev->mci;
3138 struct sbridge_pvt *pvt;
3139
3140 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003141 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003142
3143 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3144 return;
3145 }
3146
3147 pvt = mci->pvt_info;
3148
Joe Perches956b9ba2012-04-29 17:08:39 -03003149 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3150 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003151
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003152 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03003153 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003154
Joe Perches956b9ba2012-04-29 17:08:39 -03003155 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003156 kfree(mci->ctl_name);
3157 edac_mc_free(mci);
3158 sbridge_dev->mci = NULL;
3159}
3160
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003161static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003162{
3163 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003164 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003165 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003166 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003167 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003168
3169 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03003170 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003171 if (unlikely(rc < 0))
3172 return rc;
3173
3174 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003175 layers[0].type = EDAC_MC_LAYER_CHANNEL;
Jim Snowd0cdf902015-12-03 10:48:54 +01003176 layers[0].size = type == KNIGHTS_LANDING ?
3177 KNL_MAX_CHANNELS : NUM_CHANNELS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003178 layers[0].is_virt_csrow = false;
3179 layers[1].type = EDAC_MC_LAYER_SLOT;
Jim Snowd0cdf902015-12-03 10:48:54 +01003180 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003181 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03003182 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003183 sizeof(*pvt));
3184
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003185 if (unlikely(!mci))
3186 return -ENOMEM;
3187
Joe Perches956b9ba2012-04-29 17:08:39 -03003188 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003189 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003190
3191 pvt = mci->pvt_info;
3192 memset(pvt, 0, sizeof(*pvt));
3193
3194 /* Associate sbridge_dev and mci for future usage */
3195 pvt->sbridge_dev = sbridge_dev;
3196 sbridge_dev->mci = mci;
3197
Jim Snowd0cdf902015-12-03 10:48:54 +01003198 mci->mtype_cap = type == KNIGHTS_LANDING ?
3199 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003200 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3201 mci->edac_cap = EDAC_FLAG_NONE;
3202 mci->mod_name = "sbridge_edac.c";
3203 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003204 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003205 mci->ctl_page_to_phys = NULL;
3206
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003207 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003208 switch (type) {
3209 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003210 pvt->info.rankcfgr = IB_RANK_CFG_A;
3211 pvt->info.get_tolm = ibridge_get_tolm;
3212 pvt->info.get_tohm = ibridge_get_tohm;
3213 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003214 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003215 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003216 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003217 pvt->info.sad_limit = sad_limit;
3218 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003219 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003220 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3221 pvt->info.interleave_list = ibridge_interleave_list;
3222 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3223 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003224 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003225 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
3226
3227 /* Store pci devices at mci for faster access */
3228 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3229 if (unlikely(rc < 0))
3230 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003231 break;
3232 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003233 pvt->info.rankcfgr = SB_RANK_CFG_A;
3234 pvt->info.get_tolm = sbridge_get_tolm;
3235 pvt->info.get_tohm = sbridge_get_tohm;
3236 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003237 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003238 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003239 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003240 pvt->info.sad_limit = sad_limit;
3241 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003242 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003243 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3244 pvt->info.interleave_list = sbridge_interleave_list;
3245 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3246 pvt->info.interleave_pkg = sbridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003247 pvt->info.get_width = sbridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003248 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
3249
3250 /* Store pci devices at mci for faster access */
3251 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3252 if (unlikely(rc < 0))
3253 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003254 break;
3255 case HASWELL:
3256 /* rankcfgr isn't used */
3257 pvt->info.get_tolm = haswell_get_tolm;
3258 pvt->info.get_tohm = haswell_get_tohm;
3259 pvt->info.dram_rule = ibridge_dram_rule;
3260 pvt->info.get_memory_type = haswell_get_memory_type;
3261 pvt->info.get_node_id = haswell_get_node_id;
3262 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003263 pvt->info.sad_limit = sad_limit;
3264 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003265 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003266 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3267 pvt->info.interleave_list = ibridge_interleave_list;
3268 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3269 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003270 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003271 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003272
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003273 /* Store pci devices at mci for faster access */
3274 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3275 if (unlikely(rc < 0))
3276 goto fail0;
3277 break;
Tony Luck1f395812014-12-02 09:27:30 -08003278 case BROADWELL:
3279 /* rankcfgr isn't used */
3280 pvt->info.get_tolm = haswell_get_tolm;
3281 pvt->info.get_tohm = haswell_get_tohm;
3282 pvt->info.dram_rule = ibridge_dram_rule;
3283 pvt->info.get_memory_type = haswell_get_memory_type;
3284 pvt->info.get_node_id = haswell_get_node_id;
3285 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003286 pvt->info.sad_limit = sad_limit;
3287 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003288 pvt->info.dram_attr = dram_attr;
Tony Luck1f395812014-12-02 09:27:30 -08003289 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3290 pvt->info.interleave_list = ibridge_interleave_list;
3291 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3292 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003293 pvt->info.get_width = broadwell_get_width;
Tony Luck1f395812014-12-02 09:27:30 -08003294 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
3295
3296 /* Store pci devices at mci for faster access */
3297 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3298 if (unlikely(rc < 0))
3299 goto fail0;
3300 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01003301 case KNIGHTS_LANDING:
3302 /* pvt->info.rankcfgr == ??? */
3303 pvt->info.get_tolm = knl_get_tolm;
3304 pvt->info.get_tohm = knl_get_tohm;
3305 pvt->info.dram_rule = knl_dram_rule;
3306 pvt->info.get_memory_type = knl_get_memory_type;
3307 pvt->info.get_node_id = knl_get_node_id;
3308 pvt->info.rir_limit = NULL;
3309 pvt->info.sad_limit = knl_sad_limit;
3310 pvt->info.interleave_mode = knl_interleave_mode;
Jim Snowd0cdf902015-12-03 10:48:54 +01003311 pvt->info.dram_attr = dram_attr_knl;
3312 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3313 pvt->info.interleave_list = knl_interleave_list;
3314 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3315 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +01003316 pvt->info.get_width = knl_get_width;
Jim Snowd0cdf902015-12-03 10:48:54 +01003317 mci->ctl_name = kasprintf(GFP_KERNEL,
3318 "Knights Landing Socket#%d", mci->mc_idx);
3319
3320 rc = knl_mci_bind_devs(mci, sbridge_dev);
3321 if (unlikely(rc < 0))
3322 goto fail0;
3323 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003324 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003325
3326 /* Get dimm basic config and the memory layout */
3327 get_dimm_config(mci);
3328 get_memory_layout(mci);
3329
3330 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003331 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003332
3333 /* add this new MC control structure to EDAC's list of MCs */
3334 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003335 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003336 rc = -EINVAL;
3337 goto fail0;
3338 }
3339
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003340 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003341
3342fail0:
3343 kfree(mci->ctl_name);
3344 edac_mc_free(mci);
3345 sbridge_dev->mci = NULL;
3346 return rc;
3347}
3348
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003349#define ICPU(model, table) \
3350 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3351
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003352static const struct x86_cpu_id sbridge_cpuids[] = {
Dave Hansen20f4d692016-09-29 13:43:21 -07003353 ICPU(INTEL_FAM6_SANDYBRIDGE_X, pci_dev_descr_sbridge_table),
3354 ICPU(INTEL_FAM6_IVYBRIDGE_X, pci_dev_descr_ibridge_table),
3355 ICPU(INTEL_FAM6_HASWELL_X, pci_dev_descr_haswell_table),
3356 ICPU(INTEL_FAM6_BROADWELL_X, pci_dev_descr_broadwell_table),
3357 ICPU(INTEL_FAM6_BROADWELL_XEON_D, pci_dev_descr_broadwell_table),
3358 ICPU(INTEL_FAM6_XEON_PHI_KNL, pci_dev_descr_knl_table),
Piotr Luc9a9260ca2016-10-13 17:30:59 +02003359 ICPU(INTEL_FAM6_XEON_PHI_KNM, pci_dev_descr_knl_table),
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003360 { }
3361};
3362MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3363
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003364/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003365 * sbridge_probe Get all devices and register memory controllers
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003366 * present.
3367 * return:
3368 * 0 for FOUND a device
3369 * < 0 for error code
3370 */
3371
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003372static int sbridge_probe(const struct x86_cpu_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003373{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003374 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003375 u8 mc, num_mc = 0;
3376 struct sbridge_dev *sbridge_dev;
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003377 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003378
3379 /* get the pci devices we want to reserve for our use */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003380 rc = sbridge_get_all_devices(&num_mc, ptable);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003381
Borislav Petkov11249e72015-02-05 12:39:36 +01003382 if (unlikely(rc < 0)) {
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003383 edac_dbg(0, "couldn't get all devices\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003384 goto fail0;
Borislav Petkov11249e72015-02-05 12:39:36 +01003385 }
3386
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003387 mc = 0;
3388
3389 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003390 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3391 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003392
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003393 sbridge_dev->mc = mc++;
Tony Luck665f05e02016-06-02 10:58:08 -07003394 rc = sbridge_register_mci(sbridge_dev, ptable->type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003395 if (unlikely(rc < 0))
3396 goto fail1;
3397 }
3398
Borislav Petkov11249e72015-02-05 12:39:36 +01003399 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003400
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003401 return 0;
3402
3403fail1:
3404 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3405 sbridge_unregister_mci(sbridge_dev);
3406
3407 sbridge_put_all_devices();
3408fail0:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003409 return rc;
3410}
3411
3412/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003413 * sbridge_remove cleanup
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003414 *
3415 */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003416static void sbridge_remove(void)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003417{
3418 struct sbridge_dev *sbridge_dev;
3419
Joe Perches956b9ba2012-04-29 17:08:39 -03003420 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003421
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003422 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3423 sbridge_unregister_mci(sbridge_dev);
3424
3425 /* Release PCI resources */
3426 sbridge_put_all_devices();
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003427}
3428
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003429/*
3430 * sbridge_init Module entry function
3431 * Try to initialize this module for its devices
3432 */
3433static int __init sbridge_init(void)
3434{
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003435 const struct x86_cpu_id *id;
3436 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003437
Joe Perches956b9ba2012-04-29 17:08:39 -03003438 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003439
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003440 id = x86_match_cpu(sbridge_cpuids);
3441 if (!id)
3442 return -ENODEV;
3443
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003444 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3445 opstate_init();
3446
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003447 rc = sbridge_probe(id);
3448
3449 if (rc >= 0) {
Chen Gonge35fca42012-05-08 20:40:12 -03003450 mce_register_decode_chain(&sbridge_mce_dec);
Borislav Petkovbffc7de2017-02-04 18:10:14 +01003451 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
Chen, Gongfd521032013-12-06 01:17:09 -05003452 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003453 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03003454 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003455
3456 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003457 rc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003458
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003459 return rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003460}
3461
3462/*
3463 * sbridge_exit() Module exit function
3464 * Unregister the driver
3465 */
3466static void __exit sbridge_exit(void)
3467{
Joe Perches956b9ba2012-04-29 17:08:39 -03003468 edac_dbg(2, "\n");
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003469 sbridge_remove();
Chen Gonge35fca42012-05-08 20:40:12 -03003470 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003471}
3472
3473module_init(sbridge_init);
3474module_exit(sbridge_exit);
3475
3476module_param(edac_op_state, int, 0444);
3477MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3478
3479MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003480MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003481MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003482MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003483 SBRIDGE_REVISION);