blob: a65ea44e3b0bf66294c0c32effb774c9d3c23622 [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
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300297struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200298struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300299 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300300 u32 mcmtr;
301 u32 rankcfgr;
302 u64 (*get_tolm)(struct sbridge_pvt *pvt);
303 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300304 u64 (*rir_limit)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100305 u64 (*sad_limit)(u32 reg);
306 u32 (*interleave_mode)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100307 u32 (*dram_attr)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300308 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300309 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300310 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300311 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300312 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300313 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300314 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400315 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300316 struct pci_dev *pci_vtd;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200317};
318
319struct sbridge_channel {
320 u32 ranks;
321 u32 dimms;
322};
323
324struct pci_id_descr {
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -0300325 int dev_id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200326 int optional;
327};
328
329struct pci_id_table {
330 const struct pci_id_descr *descr;
331 int n_devs;
Tony Luck665f05e02016-06-02 10:58:08 -0700332 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200333};
334
335struct sbridge_dev {
336 struct list_head list;
337 u8 bus, mc;
338 u8 node_id, source_id;
339 struct pci_dev **pdev;
340 int n_devs;
341 struct mem_ctl_info *mci;
342};
343
Jim Snowd0cdf902015-12-03 10:48:54 +0100344struct knl_pvt {
345 struct pci_dev *pci_cha[KNL_MAX_CHAS];
346 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
347 struct pci_dev *pci_mc0;
348 struct pci_dev *pci_mc1;
349 struct pci_dev *pci_mc0_misc;
350 struct pci_dev *pci_mc1_misc;
351 struct pci_dev *pci_mc_info; /* tolm, tohm */
352};
353
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200354struct sbridge_pvt {
355 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300356 struct pci_dev *pci_sad0, *pci_sad1;
357 struct pci_dev *pci_ha0, *pci_ha1;
358 struct pci_dev *pci_br0, *pci_br1;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300359 struct pci_dev *pci_ha1_ta;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200360 struct pci_dev *pci_tad[NUM_CHANNELS];
361
362 struct sbridge_dev *sbridge_dev;
363
364 struct sbridge_info info;
365 struct sbridge_channel channel[NUM_CHANNELS];
366
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200367 /* Memory type detection */
368 bool is_mirrored, is_lockstep, is_close_pg;
Tony Luckea5dfb52016-04-14 10:22:02 -0700369 bool is_chan_hash;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200370
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200371 /* Memory description */
372 u64 tolm, tohm;
Jim Snowd0cdf902015-12-03 10:48:54 +0100373 struct knl_pvt knl;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200374};
375
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300376#define PCI_DESCR(device_id, opt) \
377 .dev_id = (device_id), \
Luck, Tonyde4772c2013-03-28 09:59:15 -0700378 .optional = opt
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200379
380static const struct pci_id_descr pci_dev_descr_sbridge[] = {
381 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300382 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200383
384 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300385 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
386 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
387 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
388 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
389 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
390 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
391 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200392
393 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300394 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
395 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200396
397 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300398 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200399};
400
Tony Luck665f05e02016-06-02 10:58:08 -0700401#define PCI_ID_TABLE_ENTRY(A, T) { \
402 .descr = A, \
403 .n_devs = ARRAY_SIZE(A), \
404 .type = T \
405}
406
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200407static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700408 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, SANDY_BRIDGE),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200409 {0,} /* 0 terminated list. */
410};
411
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300412/* This changes depending if 1HA or 2HA:
413 * 1HA:
414 * 0x0eb8 (17.0) is DDRIO0
415 * 2HA:
416 * 0x0ebc (17.4) is DDRIO0
417 */
418#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
419#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
420
421/* pci ids */
422#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
423#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
424#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
425#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
426#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
427#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
428#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
429#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
430#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
431#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
432#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
433#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
434#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
435#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
436#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
Tony Luck7d375bf2015-05-18 17:50:42 -0300437#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
438#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300439
440static const struct pci_id_descr pci_dev_descr_ibridge[] = {
441 /* Processor Home Agent */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300442 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300443
444 /* Memory controller */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300445 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
446 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
447 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
448 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
449 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
450 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300451
452 /* System Address Decoder */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300453 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300454
455 /* Broadcast Registers */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300456 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
457 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300458
459 /* Optional, mode 2HA */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300460 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300461#if 0
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300462 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
463 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300464#endif
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300465 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
466 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
Tony Luck7d375bf2015-05-18 17:50:42 -0300467 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
468 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300469
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300470 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
471 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300472};
473
474static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700475 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, IVY_BRIDGE),
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300476 {0,} /* 0 terminated list. */
477};
478
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300479/* Haswell support */
480/* EN processor:
481 * - 1 IMC
482 * - 3 DDR3 channels, 2 DPC per channel
483 * EP processor:
484 * - 1 or 2 IMC
485 * - 4 DDR4 channels, 3 DPC per channel
486 * EP 4S processor:
487 * - 2 IMC
488 * - 4 DDR4 channels, 3 DPC per channel
489 * EX processor:
490 * - 2 IMC
491 * - each IMC interfaces with a SMI 2 channel
492 * - each SMI channel interfaces with a scalable memory buffer
493 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
494 */
Tony Luck1f395812014-12-02 09:27:30 -0800495#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300496#define HASWELL_HASYSDEFEATURE2 0x84
497#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
498#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
499#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
500#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
501#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
502#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
503#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
504#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
505#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
506#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
507#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
508#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
509#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
510#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
511#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
512#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
513#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
514#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
Aristeu Rozanski71793852015-06-12 09:44:52 -0400515#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
516#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
517#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300518static const struct pci_id_descr pci_dev_descr_haswell[] = {
519 /* first item must be the HA */
520 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
521
522 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
523 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
524
525 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
526
527 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
528 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
529 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
530 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
531 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
532 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
533
534 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
Aristeu Rozanski71793852015-06-12 09:44:52 -0400535 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
536 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
537 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300538
539 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
540 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
541 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
542 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
543 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
544 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
545};
546
547static const struct pci_id_table pci_dev_descr_haswell_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700548 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, HASWELL),
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300549 {0,} /* 0 terminated list. */
550};
551
Jim Snowd0cdf902015-12-03 10:48:54 +0100552/* Knight's Landing Support */
553/*
554 * KNL's memory channels are swizzled between memory controllers.
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200555 * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
Jim Snowd0cdf902015-12-03 10:48:54 +0100556 */
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200557#define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
Jim Snowd0cdf902015-12-03 10:48:54 +0100558
559/* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
560#define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
561/* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
562#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
563/* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
564#define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
565/* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
566#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
567/* SAD target - 1-29-1 (1 of these) */
568#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
569/* Caching / Home Agent */
570#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
571/* Device with TOLM and TOHM, 0-5-0 (1 of these) */
572#define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
573
574/*
575 * KNL differs from SB, IB, and Haswell in that it has multiple
576 * instances of the same device with the same device ID, so we handle that
577 * by creating as many copies in the table as we expect to find.
578 * (Like device ID must be grouped together.)
579 */
580
581static const struct pci_id_descr pci_dev_descr_knl[] = {
582 [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
583 [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
584 [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
585 [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
586 [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
587 [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
588 [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
589};
590
591static const struct pci_id_table pci_dev_descr_knl_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700592 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, KNIGHTS_LANDING),
Jim Snowd0cdf902015-12-03 10:48:54 +0100593 {0,}
594};
595
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200596/*
Tony Luck1f395812014-12-02 09:27:30 -0800597 * Broadwell support
598 *
599 * DE processor:
600 * - 1 IMC
601 * - 2 DDR3 channels, 2 DPC per channel
Tony Luckfa2ce642015-05-20 19:10:35 -0300602 * EP processor:
603 * - 1 or 2 IMC
604 * - 4 DDR4 channels, 3 DPC per channel
605 * EP 4S processor:
606 * - 2 IMC
607 * - 4 DDR4 channels, 3 DPC per channel
608 * EX processor:
609 * - 2 IMC
610 * - each IMC interfaces with a SMI 2 channel
611 * - each SMI channel interfaces with a scalable memory buffer
612 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
Tony Luck1f395812014-12-02 09:27:30 -0800613 */
614#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
615#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
Tony Luckfa2ce642015-05-20 19:10:35 -0300616#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
Tony Luck1f395812014-12-02 09:27:30 -0800617#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
618#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
Tony Luckfa2ce642015-05-20 19:10:35 -0300619#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
620#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
Tony Luck1f395812014-12-02 09:27:30 -0800621#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
622#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
623#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
624#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
625#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
626#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
Tony Luckfa2ce642015-05-20 19:10:35 -0300627#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
628#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
629#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
630#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
Tony Luck1f395812014-12-02 09:27:30 -0800631#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
632
633static const struct pci_id_descr pci_dev_descr_broadwell[] = {
634 /* first item must be the HA */
635 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
636
637 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
638 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
639
Tony Luckfa2ce642015-05-20 19:10:35 -0300640 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
641
Tony Luck1f395812014-12-02 09:27:30 -0800642 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
643 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
644 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
645 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300646 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
647 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
648
Tony Luck1f395812014-12-02 09:27:30 -0800649 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300650
651 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
652 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
653 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
654 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
655 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
656 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
Tony Luck1f395812014-12-02 09:27:30 -0800657};
658
659static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
Tony Luck665f05e02016-06-02 10:58:08 -0700660 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, BROADWELL),
Tony Luck1f395812014-12-02 09:27:30 -0800661 {0,} /* 0 terminated list. */
662};
663
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200664
665/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700666 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200667 ****************************************************************************/
668
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300669static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200670{
671 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300672 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200673
Jim Snowd0cdf902015-12-03 10:48:54 +0100674 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300675 max = 8;
676
677 if (ranks > max) {
678 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
679 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200680 return -EINVAL;
681 }
682
683 return ranks;
684}
685
686static inline int numrow(u32 mtr)
687{
688 int rows = (RANK_WIDTH_BITS(mtr) + 12);
689
690 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300691 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
692 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200693 return -EINVAL;
694 }
695
696 return 1 << rows;
697}
698
699static inline int numcol(u32 mtr)
700{
701 int cols = (COL_WIDTH_BITS(mtr) + 10);
702
703 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300704 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
705 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200706 return -EINVAL;
707 }
708
709 return 1 << cols;
710}
711
Jim Snowc1979ba2015-12-03 10:48:53 +0100712static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200713{
714 struct sbridge_dev *sbridge_dev;
715
Jim Snowc1979ba2015-12-03 10:48:53 +0100716 /*
717 * If we have devices scattered across several busses that pertain
718 * to the same memory controller, we'll lump them all together.
719 */
720 if (multi_bus) {
721 return list_first_entry_or_null(&sbridge_edac_list,
722 struct sbridge_dev, list);
723 }
724
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200725 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
726 if (sbridge_dev->bus == bus)
727 return sbridge_dev;
728 }
729
730 return NULL;
731}
732
733static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
734 const struct pci_id_table *table)
735{
736 struct sbridge_dev *sbridge_dev;
737
738 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
739 if (!sbridge_dev)
740 return NULL;
741
742 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
743 GFP_KERNEL);
744 if (!sbridge_dev->pdev) {
745 kfree(sbridge_dev);
746 return NULL;
747 }
748
749 sbridge_dev->bus = bus;
750 sbridge_dev->n_devs = table->n_devs;
751 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
752
753 return sbridge_dev;
754}
755
756static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
757{
758 list_del(&sbridge_dev->list);
759 kfree(sbridge_dev->pdev);
760 kfree(sbridge_dev);
761}
762
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300763static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
764{
765 u32 reg;
766
767 /* Address range is 32:28 */
768 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
769 return GET_TOLM(reg);
770}
771
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300772static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
773{
774 u32 reg;
775
776 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
777 return GET_TOHM(reg);
778}
779
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300780static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
781{
782 u32 reg;
783
784 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
785
786 return GET_TOLM(reg);
787}
788
789static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
790{
791 u32 reg;
792
793 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
794
795 return GET_TOHM(reg);
796}
797
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300798static u64 rir_limit(u32 reg)
799{
800 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
801}
802
Jim Snowc59f9c02015-12-03 10:48:52 +0100803static u64 sad_limit(u32 reg)
804{
805 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
806}
807
808static u32 interleave_mode(u32 reg)
809{
810 return GET_BITFIELD(reg, 1, 1);
811}
812
Jim Snowc59f9c02015-12-03 10:48:52 +0100813static u32 dram_attr(u32 reg)
814{
815 return GET_BITFIELD(reg, 2, 3);
816}
817
Jim Snowd0cdf902015-12-03 10:48:54 +0100818static u64 knl_sad_limit(u32 reg)
819{
820 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
821}
822
823static u32 knl_interleave_mode(u32 reg)
824{
825 return GET_BITFIELD(reg, 1, 2);
826}
827
Nicolas Iooss127c1222017-01-22 18:28:06 +0100828static const char * const knl_intlv_mode[] = {
829 "[8:6]", "[10:8]", "[14:12]", "[32:30]"
830};
831
832static const char *get_intlv_mode_str(u32 reg, enum type t)
Jim Snowd0cdf902015-12-03 10:48:54 +0100833{
Nicolas Iooss127c1222017-01-22 18:28:06 +0100834 if (t == KNIGHTS_LANDING)
835 return knl_intlv_mode[knl_interleave_mode(reg)];
836 else
837 return interleave_mode(reg) ? "[8:6]" : "[8:6]XOR[18:16]";
Jim Snowd0cdf902015-12-03 10:48:54 +0100838}
839
840static u32 dram_attr_knl(u32 reg)
841{
842 return GET_BITFIELD(reg, 3, 4);
843}
844
845
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300846static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
847{
848 u32 reg;
849 enum mem_type mtype;
850
851 if (pvt->pci_ddrio) {
852 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
853 &reg);
854 if (GET_BITFIELD(reg, 11, 11))
855 /* FIXME: Can also be LRDIMM */
856 mtype = MEM_RDDR3;
857 else
858 mtype = MEM_DDR3;
859 } else
860 mtype = MEM_UNKNOWN;
861
862 return mtype;
863}
864
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300865static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
866{
867 u32 reg;
868 bool registered = false;
869 enum mem_type mtype = MEM_UNKNOWN;
870
871 if (!pvt->pci_ddrio)
872 goto out;
873
874 pci_read_config_dword(pvt->pci_ddrio,
875 HASWELL_DDRCRCLKCONTROLS, &reg);
876 /* Is_Rdimm */
877 if (GET_BITFIELD(reg, 16, 16))
878 registered = true;
879
880 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
881 if (GET_BITFIELD(reg, 14, 14)) {
882 if (registered)
883 mtype = MEM_RDDR4;
884 else
885 mtype = MEM_DDR4;
886 } else {
887 if (registered)
888 mtype = MEM_RDDR3;
889 else
890 mtype = MEM_DDR3;
891 }
892
893out:
894 return mtype;
895}
896
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +0100897static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
898{
899 /* for KNL value is fixed */
900 return DEV_X16;
901}
902
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400903static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
904{
905 /* there's no way to figure out */
906 return DEV_UNKNOWN;
907}
908
909static enum dev_type __ibridge_get_width(u32 mtr)
910{
911 enum dev_type type;
912
913 switch (mtr) {
914 case 3:
915 type = DEV_UNKNOWN;
916 break;
917 case 2:
918 type = DEV_X16;
919 break;
920 case 1:
921 type = DEV_X8;
922 break;
923 case 0:
924 type = DEV_X4;
925 break;
926 }
927
928 return type;
929}
930
931static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
932{
933 /*
934 * ddr3_width on the documentation but also valid for DDR4 on
935 * Haswell
936 */
937 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
938}
939
940static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
941{
942 /* ddr3_width on the documentation but also valid for DDR4 */
943 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
944}
945
Jim Snowd0cdf902015-12-03 10:48:54 +0100946static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
947{
948 /* DDR4 RDIMMS and LRDIMMS are supported */
949 return MEM_RDDR4;
950}
951
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300952static u8 get_node_id(struct sbridge_pvt *pvt)
953{
954 u32 reg;
955 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
956 return GET_BITFIELD(reg, 0, 2);
957}
958
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300959static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
960{
961 u32 reg;
962
963 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
964 return GET_BITFIELD(reg, 0, 3);
965}
966
Jim Snowd0cdf902015-12-03 10:48:54 +0100967static u8 knl_get_node_id(struct sbridge_pvt *pvt)
968{
969 u32 reg;
970
971 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
972 return GET_BITFIELD(reg, 0, 2);
973}
974
975
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300976static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
977{
978 u32 reg;
979
Tony Luckf7cf2a22014-10-29 10:36:50 -0700980 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
981 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300982}
983
984static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
985{
986 u64 rc;
987 u32 reg;
988
989 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
990 rc = GET_BITFIELD(reg, 26, 31);
991 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
992 rc = ((reg << 6) | rc) << 26;
993
994 return rc | 0x1ffffff;
995}
996
Jim Snowd0cdf902015-12-03 10:48:54 +0100997static u64 knl_get_tolm(struct sbridge_pvt *pvt)
998{
999 u32 reg;
1000
1001 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
1002 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1003}
1004
1005static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1006{
1007 u64 rc;
1008 u32 reg_lo, reg_hi;
1009
1010 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
1011 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
1012 rc = ((u64)reg_hi << 32) | reg_lo;
1013 return rc | 0x3ffffff;
1014}
1015
1016
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001017static u64 haswell_rir_limit(u32 reg)
1018{
1019 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1020}
1021
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001022static inline u8 sad_pkg_socket(u8 pkg)
1023{
1024 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -03001025 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001026}
1027
1028static inline u8 sad_pkg_ha(u8 pkg)
1029{
1030 return (pkg >> 2) & 0x1;
1031}
1032
Tony Luckea5dfb52016-04-14 10:22:02 -07001033static int haswell_chan_hash(int idx, u64 addr)
1034{
1035 int i;
1036
1037 /*
1038 * XOR even bits from 12:26 to bit0 of idx,
1039 * odd bits from 13:27 to bit1
1040 */
1041 for (i = 12; i < 28; i += 2)
1042 idx ^= (addr >> i) & 3;
1043
1044 return idx;
1045}
1046
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001047/****************************************************************************
1048 Memory check routines
1049 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001050static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001051{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001052 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001053
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001054 do {
1055 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
1056 if (pdev && pdev->bus->number == bus)
1057 break;
1058 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001059
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001060 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001061}
1062
1063/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001064 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001065 * @bus: Device bus
1066 * @type: Memory controller type
1067 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
1068 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001069 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001070static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001071{
1072 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001073 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001074
Tony Luck1f395812014-12-02 09:27:30 -08001075 switch (type) {
1076 case IVY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001077 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001078 break;
1079 case HASWELL:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001080 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001081 break;
1082 case SANDY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001083 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001084 break;
1085 case BROADWELL:
1086 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
1087 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01001088 case KNIGHTS_LANDING:
1089 /*
1090 * KNL doesn't group things by bus the same way
1091 * SB/IB/Haswell does.
1092 */
1093 id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
1094 break;
Tony Luck1f395812014-12-02 09:27:30 -08001095 default:
1096 return -ENODEV;
1097 }
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001098
Jim Snowd0cdf902015-12-03 10:48:54 +01001099 if (type != KNIGHTS_LANDING)
1100 pdev = get_pdev_same_bus(bus, id);
1101 else
1102 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
1103
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001104 if (!pdev) {
1105 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001106 "%04x:%04x! on bus %02d\n",
1107 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001108 return -ENODEV;
1109 }
1110
Jim Snowd0cdf902015-12-03 10:48:54 +01001111 pci_read_config_dword(pdev,
1112 type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001113 if (!IS_ECC_ENABLED(mcmtr)) {
1114 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
1115 return -ENODEV;
1116 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001117 return 0;
1118}
1119
Jim Snowd0cdf902015-12-03 10:48:54 +01001120/* Low bits of TAD limit, and some metadata. */
1121static const u32 knl_tad_dram_limit_lo[] = {
1122 0x400, 0x500, 0x600, 0x700,
1123 0x800, 0x900, 0xa00, 0xb00,
1124};
1125
1126/* Low bits of TAD offset. */
1127static const u32 knl_tad_dram_offset_lo[] = {
1128 0x404, 0x504, 0x604, 0x704,
1129 0x804, 0x904, 0xa04, 0xb04,
1130};
1131
1132/* High 16 bits of TAD limit and offset. */
1133static const u32 knl_tad_dram_hi[] = {
1134 0x408, 0x508, 0x608, 0x708,
1135 0x808, 0x908, 0xa08, 0xb08,
1136};
1137
1138/* Number of ways a tad entry is interleaved. */
1139static const u32 knl_tad_ways[] = {
1140 8, 6, 4, 3, 2, 1,
1141};
1142
1143/*
1144 * Retrieve the n'th Target Address Decode table entry
1145 * from the memory controller's TAD table.
1146 *
1147 * @pvt: driver private data
1148 * @entry: which entry you want to retrieve
1149 * @mc: which memory controller (0 or 1)
1150 * @offset: output tad range offset
1151 * @limit: output address of first byte above tad range
1152 * @ways: output number of interleave ways
1153 *
1154 * The offset value has curious semantics. It's a sort of running total
1155 * of the sizes of all the memory regions that aren't mapped in this
1156 * tad table.
1157 */
1158static int knl_get_tad(const struct sbridge_pvt *pvt,
1159 const int entry,
1160 const int mc,
1161 u64 *offset,
1162 u64 *limit,
1163 int *ways)
1164{
1165 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1166 struct pci_dev *pci_mc;
1167 int way_id;
1168
1169 switch (mc) {
1170 case 0:
1171 pci_mc = pvt->knl.pci_mc0;
1172 break;
1173 case 1:
1174 pci_mc = pvt->knl.pci_mc1;
1175 break;
1176 default:
1177 WARN_ON(1);
1178 return -EINVAL;
1179 }
1180
1181 pci_read_config_dword(pci_mc,
1182 knl_tad_dram_limit_lo[entry], &reg_limit_lo);
1183 pci_read_config_dword(pci_mc,
1184 knl_tad_dram_offset_lo[entry], &reg_offset_lo);
1185 pci_read_config_dword(pci_mc,
1186 knl_tad_dram_hi[entry], &reg_hi);
1187
1188 /* Is this TAD entry enabled? */
1189 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1190 return -ENODEV;
1191
1192 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1193
1194 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1195 *ways = knl_tad_ways[way_id];
1196 } else {
1197 *ways = 0;
1198 sbridge_printk(KERN_ERR,
1199 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1200 way_id);
1201 return -ENODEV;
1202 }
1203
1204 /*
1205 * The least significant 6 bits of base and limit are truncated.
1206 * For limit, we fill the missing bits with 1s.
1207 */
1208 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1209 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1210 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1211 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1212
1213 return 0;
1214}
1215
1216/* Determine which memory controller is responsible for a given channel. */
1217static int knl_channel_mc(int channel)
1218{
1219 WARN_ON(channel < 0 || channel >= 6);
1220
1221 return channel < 3 ? 1 : 0;
1222}
1223
1224/*
1225 * Get the Nth entry from EDC_ROUTE_TABLE register.
1226 * (This is the per-tile mapping of logical interleave targets to
1227 * physical EDC modules.)
1228 *
1229 * entry 0: 0:2
1230 * 1: 3:5
1231 * 2: 6:8
1232 * 3: 9:11
1233 * 4: 12:14
1234 * 5: 15:17
1235 * 6: 18:20
1236 * 7: 21:23
1237 * reserved: 24:31
1238 */
1239static u32 knl_get_edc_route(int entry, u32 reg)
1240{
1241 WARN_ON(entry >= KNL_MAX_EDCS);
1242 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1243}
1244
1245/*
1246 * Get the Nth entry from MC_ROUTE_TABLE register.
1247 * (This is the per-tile mapping of logical interleave targets to
1248 * physical DRAM channels modules.)
1249 *
1250 * entry 0: mc 0:2 channel 18:19
1251 * 1: mc 3:5 channel 20:21
1252 * 2: mc 6:8 channel 22:23
1253 * 3: mc 9:11 channel 24:25
1254 * 4: mc 12:14 channel 26:27
1255 * 5: mc 15:17 channel 28:29
1256 * reserved: 30:31
1257 *
1258 * Though we have 3 bits to identify the MC, we should only see
1259 * the values 0 or 1.
1260 */
1261
1262static u32 knl_get_mc_route(int entry, u32 reg)
1263{
1264 int mc, chan;
1265
1266 WARN_ON(entry >= KNL_MAX_CHANNELS);
1267
1268 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1269 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1270
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02001271 return knl_channel_remap(mc, chan);
Jim Snowd0cdf902015-12-03 10:48:54 +01001272}
1273
1274/*
1275 * Render the EDC_ROUTE register in human-readable form.
1276 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1277 */
1278static void knl_show_edc_route(u32 reg, char *s)
1279{
1280 int i;
1281
1282 for (i = 0; i < KNL_MAX_EDCS; i++) {
1283 s[i*2] = knl_get_edc_route(i, reg) + '0';
1284 s[i*2+1] = '-';
1285 }
1286
1287 s[KNL_MAX_EDCS*2 - 1] = '\0';
1288}
1289
1290/*
1291 * Render the MC_ROUTE register in human-readable form.
1292 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1293 */
1294static void knl_show_mc_route(u32 reg, char *s)
1295{
1296 int i;
1297
1298 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1299 s[i*2] = knl_get_mc_route(i, reg) + '0';
1300 s[i*2+1] = '-';
1301 }
1302
1303 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1304}
1305
1306#define KNL_EDC_ROUTE 0xb8
1307#define KNL_MC_ROUTE 0xb4
1308
1309/* Is this dram rule backed by regular DRAM in flat mode? */
1310#define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1311
1312/* Is this dram rule cached? */
1313#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1314
1315/* Is this rule backed by edc ? */
1316#define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1317
1318/* Is this rule backed by DRAM, cacheable in EDRAM? */
1319#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1320
1321/* Is this rule mod3? */
1322#define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1323
1324/*
1325 * Figure out how big our RAM modules are.
1326 *
1327 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1328 * have to figure this out from the SAD rules, interleave lists, route tables,
1329 * and TAD rules.
1330 *
1331 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1332 * inspect the TAD rules to figure out how large the SAD regions really are.
1333 *
1334 * When we know the real size of a SAD region and how many ways it's
1335 * interleaved, we know the individual contribution of each channel to
1336 * TAD is size/ways.
1337 *
1338 * Finally, we have to check whether each channel participates in each SAD
1339 * region.
1340 *
1341 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1342 * much memory the channel uses, we know the DIMM is at least that large.
1343 * (The BIOS might possibly choose not to map all available memory, in which
1344 * case we will underreport the size of the DIMM.)
1345 *
1346 * In theory, we could try to determine the EDC sizes as well, but that would
1347 * only work in flat mode, not in cache mode.
1348 *
1349 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1350 * elements)
1351 */
1352static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1353{
1354 u64 sad_base, sad_size, sad_limit = 0;
1355 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1356 int sad_rule = 0;
1357 int tad_rule = 0;
1358 int intrlv_ways, tad_ways;
1359 u32 first_pkg, pkg;
1360 int i;
1361 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1362 u32 dram_rule, interleave_reg;
1363 u32 mc_route_reg[KNL_MAX_CHAS];
1364 u32 edc_route_reg[KNL_MAX_CHAS];
1365 int edram_only;
1366 char edc_route_string[KNL_MAX_EDCS*2];
1367 char mc_route_string[KNL_MAX_CHANNELS*2];
1368 int cur_reg_start;
1369 int mc;
1370 int channel;
1371 int way;
1372 int participants[KNL_MAX_CHANNELS];
1373 int participant_count = 0;
1374
1375 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1376 mc_sizes[i] = 0;
1377
1378 /* Read the EDC route table in each CHA. */
1379 cur_reg_start = 0;
1380 for (i = 0; i < KNL_MAX_CHAS; i++) {
1381 pci_read_config_dword(pvt->knl.pci_cha[i],
1382 KNL_EDC_ROUTE, &edc_route_reg[i]);
1383
1384 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1385 knl_show_edc_route(edc_route_reg[i-1],
1386 edc_route_string);
1387 if (cur_reg_start == i-1)
1388 edac_dbg(0, "edc route table for CHA %d: %s\n",
1389 cur_reg_start, edc_route_string);
1390 else
1391 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1392 cur_reg_start, i-1, edc_route_string);
1393 cur_reg_start = i;
1394 }
1395 }
1396 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1397 if (cur_reg_start == i-1)
1398 edac_dbg(0, "edc route table for CHA %d: %s\n",
1399 cur_reg_start, edc_route_string);
1400 else
1401 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1402 cur_reg_start, i-1, edc_route_string);
1403
1404 /* Read the MC route table in each CHA. */
1405 cur_reg_start = 0;
1406 for (i = 0; i < KNL_MAX_CHAS; i++) {
1407 pci_read_config_dword(pvt->knl.pci_cha[i],
1408 KNL_MC_ROUTE, &mc_route_reg[i]);
1409
1410 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1411 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1412 if (cur_reg_start == i-1)
1413 edac_dbg(0, "mc route table for CHA %d: %s\n",
1414 cur_reg_start, mc_route_string);
1415 else
1416 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1417 cur_reg_start, i-1, mc_route_string);
1418 cur_reg_start = i;
1419 }
1420 }
1421 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1422 if (cur_reg_start == i-1)
1423 edac_dbg(0, "mc route table for CHA %d: %s\n",
1424 cur_reg_start, mc_route_string);
1425 else
1426 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1427 cur_reg_start, i-1, mc_route_string);
1428
1429 /* Process DRAM rules */
1430 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1431 /* previous limit becomes the new base */
1432 sad_base = sad_limit;
1433
1434 pci_read_config_dword(pvt->pci_sad0,
1435 pvt->info.dram_rule[sad_rule], &dram_rule);
1436
1437 if (!DRAM_RULE_ENABLE(dram_rule))
1438 break;
1439
1440 edram_only = KNL_EDRAM_ONLY(dram_rule);
1441
1442 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1443 sad_size = sad_limit - sad_base;
1444
1445 pci_read_config_dword(pvt->pci_sad0,
1446 pvt->info.interleave_list[sad_rule], &interleave_reg);
1447
1448 /*
1449 * Find out how many ways this dram rule is interleaved.
1450 * We stop when we see the first channel again.
1451 */
1452 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1453 interleave_reg, 0);
1454 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1455 pkg = sad_pkg(pvt->info.interleave_pkg,
1456 interleave_reg, intrlv_ways);
1457
1458 if ((pkg & 0x8) == 0) {
1459 /*
1460 * 0 bit means memory is non-local,
1461 * which KNL doesn't support
1462 */
1463 edac_dbg(0, "Unexpected interleave target %d\n",
1464 pkg);
1465 return -1;
1466 }
1467
1468 if (pkg == first_pkg)
1469 break;
1470 }
1471 if (KNL_MOD3(dram_rule))
1472 intrlv_ways *= 3;
1473
1474 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1475 sad_rule,
1476 sad_base,
1477 sad_limit,
1478 intrlv_ways,
1479 edram_only ? ", EDRAM" : "");
1480
1481 /*
1482 * Find out how big the SAD region really is by iterating
1483 * over TAD tables (SAD regions may contain holes).
1484 * Each memory controller might have a different TAD table, so
1485 * we have to look at both.
1486 *
1487 * Livespace is the memory that's mapped in this TAD table,
1488 * deadspace is the holes (this could be the MMIO hole, or it
1489 * could be memory that's mapped by the other TAD table but
1490 * not this one).
1491 */
1492 for (mc = 0; mc < 2; mc++) {
1493 sad_actual_size[mc] = 0;
1494 tad_livespace = 0;
1495 for (tad_rule = 0;
1496 tad_rule < ARRAY_SIZE(
1497 knl_tad_dram_limit_lo);
1498 tad_rule++) {
1499 if (knl_get_tad(pvt,
1500 tad_rule,
1501 mc,
1502 &tad_deadspace,
1503 &tad_limit,
1504 &tad_ways))
1505 break;
1506
1507 tad_size = (tad_limit+1) -
1508 (tad_livespace + tad_deadspace);
1509 tad_livespace += tad_size;
1510 tad_base = (tad_limit+1) - tad_size;
1511
1512 if (tad_base < sad_base) {
1513 if (tad_limit > sad_base)
1514 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1515 } else if (tad_base < sad_limit) {
1516 if (tad_limit+1 > sad_limit) {
1517 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1518 } else {
1519 /* TAD region is completely inside SAD region */
1520 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1521 tad_rule, tad_base,
1522 tad_limit, tad_size,
1523 mc);
1524 sad_actual_size[mc] += tad_size;
1525 }
1526 }
1527 tad_base = tad_limit+1;
1528 }
1529 }
1530
1531 for (mc = 0; mc < 2; mc++) {
1532 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1533 mc, sad_actual_size[mc], sad_actual_size[mc]);
1534 }
1535
1536 /* Ignore EDRAM rule */
1537 if (edram_only)
1538 continue;
1539
1540 /* Figure out which channels participate in interleave. */
1541 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1542 participants[channel] = 0;
1543
1544 /* For each channel, does at least one CHA have
1545 * this channel mapped to the given target?
1546 */
1547 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1548 for (way = 0; way < intrlv_ways; way++) {
1549 int target;
1550 int cha;
1551
1552 if (KNL_MOD3(dram_rule))
1553 target = way;
1554 else
1555 target = 0x7 & sad_pkg(
1556 pvt->info.interleave_pkg, interleave_reg, way);
1557
1558 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1559 if (knl_get_mc_route(target,
1560 mc_route_reg[cha]) == channel
Hubert Chrzaniuk83bdaad2016-03-07 15:30:45 +01001561 && !participants[channel]) {
Jim Snowd0cdf902015-12-03 10:48:54 +01001562 participant_count++;
1563 participants[channel] = 1;
1564 break;
1565 }
1566 }
1567 }
1568 }
1569
1570 if (participant_count != intrlv_ways)
1571 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1572 participant_count, intrlv_ways);
1573
1574 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1575 mc = knl_channel_mc(channel);
1576 if (participants[channel]) {
1577 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1578 channel,
1579 sad_actual_size[mc]/intrlv_ways,
1580 sad_rule);
1581 mc_sizes[channel] +=
1582 sad_actual_size[mc]/intrlv_ways;
1583 }
1584 }
1585 }
1586
1587 return 0;
1588}
1589
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001590static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001591{
1592 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001593 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001594 unsigned i, j, banks, ranks, rows, cols, npages;
1595 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001596 u32 reg;
1597 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -02001598 enum mem_type mtype;
Jim Snowd0cdf902015-12-03 10:48:54 +01001599 int channels = pvt->info.type == KNIGHTS_LANDING ?
1600 KNL_MAX_CHANNELS : NUM_CHANNELS;
1601 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001602
Tony Luckea5dfb52016-04-14 10:22:02 -07001603 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1604 pci_read_config_dword(pvt->pci_ha0, HASWELL_HASYSDEFEATURE2, &reg);
1605 pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
1606 }
Jim Snowd0cdf902015-12-03 10:48:54 +01001607 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1608 pvt->info.type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001609 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
1610 else
1611 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
1612
Jim Snowd0cdf902015-12-03 10:48:54 +01001613 if (pvt->info.type == KNIGHTS_LANDING)
1614 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1615 else
1616 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001617
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03001618 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -03001619 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1620 pvt->sbridge_dev->mc,
1621 pvt->sbridge_dev->node_id,
1622 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001623
Jim Snowd0cdf902015-12-03 10:48:54 +01001624 /* KNL doesn't support mirroring or lockstep,
1625 * and is always closed page
1626 */
1627 if (pvt->info.type == KNIGHTS_LANDING) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001628 mode = EDAC_S4ECD4ED;
Jim Snowd0cdf902015-12-03 10:48:54 +01001629 pvt->is_mirrored = false;
1630
1631 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1632 return -1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001633 } else {
Jim Snowd0cdf902015-12-03 10:48:54 +01001634 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
1635 if (IS_MIRROR_ENABLED(reg)) {
1636 edac_dbg(0, "Memory mirror is enabled\n");
1637 pvt->is_mirrored = true;
1638 } else {
1639 edac_dbg(0, "Memory mirror is disabled\n");
1640 pvt->is_mirrored = false;
1641 }
1642
1643 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
1644 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1645 edac_dbg(0, "Lockstep is enabled\n");
1646 mode = EDAC_S8ECD8ED;
1647 pvt->is_lockstep = true;
1648 } else {
1649 edac_dbg(0, "Lockstep is disabled\n");
1650 mode = EDAC_S4ECD4ED;
1651 pvt->is_lockstep = false;
1652 }
1653 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1654 edac_dbg(0, "address map is on closed page mode\n");
1655 pvt->is_close_pg = true;
1656 } else {
1657 edac_dbg(0, "address map is on open page mode\n");
1658 pvt->is_close_pg = false;
1659 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001660 }
1661
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001662 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001663 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001664 edac_dbg(0, "Memory is registered\n");
1665 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -07001666 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001667 else
1668 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001669
Tony Luckfec53af2014-12-02 09:41:58 -08001670 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001671 banks = 16;
1672 else
1673 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001674
Jim Snowd0cdf902015-12-03 10:48:54 +01001675 for (i = 0; i < channels; i++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001676 u32 mtr;
1677
Jim Snowd0cdf902015-12-03 10:48:54 +01001678 int max_dimms_per_channel;
1679
1680 if (pvt->info.type == KNIGHTS_LANDING) {
1681 max_dimms_per_channel = 1;
1682 if (!pvt->knl.pci_channel[i])
1683 continue;
1684 } else {
1685 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1686 if (!pvt->pci_tad[i])
1687 continue;
1688 }
1689
1690 for (j = 0; j < max_dimms_per_channel; j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001691 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1692 i, j, 0);
Jim Snowd0cdf902015-12-03 10:48:54 +01001693 if (pvt->info.type == KNIGHTS_LANDING) {
1694 pci_read_config_dword(pvt->knl.pci_channel[i],
1695 knl_mtr_reg, &mtr);
1696 } else {
1697 pci_read_config_dword(pvt->pci_tad[i],
1698 mtr_regs[j], &mtr);
1699 }
Joe Perches956b9ba2012-04-29 17:08:39 -03001700 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001701 if (IS_DIMM_PRESENT(mtr)) {
1702 pvt->channel[i].dimms++;
1703
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001704 ranks = numrank(pvt->info.type, mtr);
Jim Snowd0cdf902015-12-03 10:48:54 +01001705
1706 if (pvt->info.type == KNIGHTS_LANDING) {
1707 /* For DDR4, this is fixed. */
1708 cols = 1 << 10;
1709 rows = knl_mc_sizes[i] /
1710 ((u64) cols * ranks * banks * 8);
1711 } else {
1712 rows = numrow(mtr);
1713 cols = numcol(mtr);
1714 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001715
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001716 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001717 npages = MiB_TO_PAGES(size);
1718
Tony Luck7d375bf2015-05-18 17:50:42 -03001719 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1720 pvt->sbridge_dev->mc, i/4, i%4, j,
Joe Perches956b9ba2012-04-29 17:08:39 -03001721 size, npages,
1722 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001723
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -03001724 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001725 dimm->grain = 32;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04001726 dimm->dtype = pvt->info.get_width(pvt, mtr);
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001727 dimm->mtype = mtype;
1728 dimm->edac_mode = mode;
1729 snprintf(dimm->label, sizeof(dimm->label),
Tony Luck7d375bf2015-05-18 17:50:42 -03001730 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1731 pvt->sbridge_dev->source_id, i/4, i%4, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001732 }
1733 }
1734 }
1735
1736 return 0;
1737}
1738
1739static void get_memory_layout(const struct mem_ctl_info *mci)
1740{
1741 struct sbridge_pvt *pvt = mci->pvt_info;
1742 int i, j, k, n_sads, n_tads, sad_interl;
1743 u32 reg;
1744 u64 limit, prv = 0;
1745 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +01001746 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001747 u32 rir_way;
1748
1749 /*
1750 * Step 1) Get TOLM/TOHM ranges
1751 */
1752
Aristeu Rozanskifb79a502013-10-30 13:26:57 -03001753 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001754 tmp_mb = (1 + pvt->tolm) >> 20;
1755
Jim Snow8c009102014-11-18 14:51:09 +01001756 gb = div_u64_rem(tmp_mb, 1024, &mb);
1757 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1758 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001759
1760 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -03001761 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001762 tmp_mb = (1 + pvt->tohm) >> 20;
1763
Jim Snow8c009102014-11-18 14:51:09 +01001764 gb = div_u64_rem(tmp_mb, 1024, &mb);
1765 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1766 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001767
1768 /*
1769 * Step 2) Get SAD range and SAD Interleave list
1770 * TAD registers contain the interleave wayness. However, it
1771 * seems simpler to just discover it indirectly, with the
1772 * algorithm bellow.
1773 */
1774 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001775 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001776 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001777 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001778 &reg);
Jim Snowc59f9c02015-12-03 10:48:52 +01001779 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001780
1781 if (!DRAM_RULE_ENABLE(reg))
1782 continue;
1783
1784 if (limit <= prv)
1785 break;
1786
1787 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001788 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001789 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1790 n_sads,
Jim Snowc59f9c02015-12-03 10:48:52 +01001791 show_dram_attr(pvt->info.dram_attr(reg)),
Jim Snow8c009102014-11-18 14:51:09 +01001792 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001793 ((u64)tmp_mb) << 20L,
Nicolas Iooss127c1222017-01-22 18:28:06 +01001794 get_intlv_mode_str(reg, pvt->info.type),
Joe Perches956b9ba2012-04-29 17:08:39 -03001795 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001796 prv = limit;
1797
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001798 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001799 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001800 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001801 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001802 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1803 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001804 break;
1805
Joe Perches956b9ba2012-04-29 17:08:39 -03001806 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001807 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001808 }
1809 }
1810
Jim Snowd0cdf902015-12-03 10:48:54 +01001811 if (pvt->info.type == KNIGHTS_LANDING)
1812 return;
1813
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001814 /*
1815 * Step 3) Get TAD range
1816 */
1817 prv = 0;
1818 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1819 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1820 &reg);
1821 limit = TAD_LIMIT(reg);
1822 if (limit <= prv)
1823 break;
1824 tmp_mb = (limit + 1) >> 20;
1825
Jim Snow8c009102014-11-18 14:51:09 +01001826 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001827 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 +01001828 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001829 ((u64)tmp_mb) << 20L,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08001830 (u32)(1 << TAD_SOCK(reg)),
1831 (u32)TAD_CH(reg) + 1,
Joe Perches956b9ba2012-04-29 17:08:39 -03001832 (u32)TAD_TGT0(reg),
1833 (u32)TAD_TGT1(reg),
1834 (u32)TAD_TGT2(reg),
1835 (u32)TAD_TGT3(reg),
1836 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001837 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001838 }
1839
1840 /*
1841 * Step 4) Get TAD offsets, per each channel
1842 */
1843 for (i = 0; i < NUM_CHANNELS; i++) {
1844 if (!pvt->channel[i].dimms)
1845 continue;
1846 for (j = 0; j < n_tads; j++) {
1847 pci_read_config_dword(pvt->pci_tad[i],
1848 tad_ch_nilv_offset[j],
1849 &reg);
1850 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001851 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001852 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1853 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001854 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001855 ((u64)tmp_mb) << 20L,
1856 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001857 }
1858 }
1859
1860 /*
1861 * Step 6) Get RIR Wayness/Limit, per each channel
1862 */
1863 for (i = 0; i < NUM_CHANNELS; i++) {
1864 if (!pvt->channel[i].dimms)
1865 continue;
1866 for (j = 0; j < MAX_RIR_RANGES; j++) {
1867 pci_read_config_dword(pvt->pci_tad[i],
1868 rir_way_limit[j],
1869 &reg);
1870
1871 if (!IS_RIR_VALID(reg))
1872 continue;
1873
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001874 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001875 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001876 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001877 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1878 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001879 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001880 ((u64)tmp_mb) << 20L,
1881 rir_way,
1882 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001883
1884 for (k = 0; k < rir_way; k++) {
1885 pci_read_config_dword(pvt->pci_tad[i],
1886 rir_offset[j][k],
1887 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07001888 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001889
Jim Snow8c009102014-11-18 14:51:09 +01001890 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001891 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1892 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001893 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001894 ((u64)tmp_mb) << 20L,
Tony Luckc7103f62016-05-31 11:50:28 -07001895 (u32)RIR_RNK_TGT(pvt->info.type, reg),
Joe Perches956b9ba2012-04-29 17:08:39 -03001896 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001897 }
1898 }
1899 }
1900}
1901
Rashika Kheria8112c0c2013-12-14 19:32:09 +05301902static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001903{
1904 struct sbridge_dev *sbridge_dev;
1905
1906 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1907 if (sbridge_dev->node_id == node_id)
1908 return sbridge_dev->mci;
1909 }
1910 return NULL;
1911}
1912
1913static int get_memory_error_data(struct mem_ctl_info *mci,
1914 u64 addr,
Tony Luck7d375bf2015-05-18 17:50:42 -03001915 u8 *socket, u8 *ha,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001916 long *channel_mask,
1917 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001918 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001919{
1920 struct mem_ctl_info *new_mci;
1921 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001922 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001923 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001924 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001925 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001926 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001927 u32 reg, dram_rule;
Tony Luck7d375bf2015-05-18 17:50:42 -03001928 u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001929 u32 tad_offset;
1930 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001931 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001932 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001933
1934
1935 /*
1936 * Step 0) Check if the address is at special memory ranges
1937 * The check bellow is probably enough to fill all cases where
1938 * the error is not inside a memory, except for the legacy
1939 * range (e. g. VGA addresses). It is unlikely, however, that the
1940 * memory controller would generate an error on that range.
1941 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001942 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001943 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001944 return -EINVAL;
1945 }
1946 if (addr >= (u64)pvt->tohm) {
1947 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001948 return -EINVAL;
1949 }
1950
1951 /*
1952 * Step 1) Get socket
1953 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001954 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1955 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001956 &reg);
1957
1958 if (!DRAM_RULE_ENABLE(reg))
1959 continue;
1960
Jim Snowc59f9c02015-12-03 10:48:52 +01001961 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001962 if (limit <= prv) {
1963 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001964 return -EINVAL;
1965 }
1966 if (addr <= limit)
1967 break;
1968 prv = limit;
1969 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001970 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001971 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001972 return -EINVAL;
1973 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001974 dram_rule = reg;
Jim Snowc59f9c02015-12-03 10:48:52 +01001975 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1976 interleave_mode = pvt->info.interleave_mode(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001977
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001978 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001979 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001980
1981 if (pvt->info.type == SANDY_BRIDGE) {
1982 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1983 for (sad_way = 0; sad_way < 8; sad_way++) {
1984 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1985 if (sad_way > 0 && sad_interl == pkg)
1986 break;
1987 sad_interleave[sad_way] = pkg;
1988 edac_dbg(0, "SAD interleave #%d: %d\n",
1989 sad_way, sad_interleave[sad_way]);
1990 }
1991 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1992 pvt->sbridge_dev->mc,
1993 n_sads,
1994 addr,
1995 limit,
1996 sad_way + 7,
1997 !interleave_mode ? "" : "XOR[18:16]");
1998 if (interleave_mode)
1999 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
2000 else
2001 idx = (addr >> 6) & 7;
2002 switch (sad_way) {
2003 case 1:
2004 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002005 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002006 case 2:
2007 idx = idx & 1;
2008 break;
2009 case 4:
2010 idx = idx & 3;
2011 break;
2012 case 8:
2013 break;
2014 default:
2015 sprintf(msg, "Can't discover socket interleave");
2016 return -EINVAL;
2017 }
2018 *socket = sad_interleave[idx];
2019 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2020 idx, sad_way, *socket);
Tony Luck1f395812014-12-02 09:27:30 -08002021 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002022 int bits, a7mode = A7MODE(dram_rule);
2023
2024 if (a7mode) {
2025 /* A7 mode swaps P9 with P6 */
2026 bits = GET_BITFIELD(addr, 7, 8) << 1;
2027 bits |= GET_BITFIELD(addr, 9, 9);
2028 } else
Tony Luckbb89e712015-05-18 17:39:06 -03002029 bits = GET_BITFIELD(addr, 6, 8);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002030
Tony Luckbb89e712015-05-18 17:39:06 -03002031 if (interleave_mode == 0) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002032 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2033 idx = GET_BITFIELD(addr, 16, 18);
2034 idx ^= bits;
2035 } else
2036 idx = bits;
2037
2038 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2039 *socket = sad_pkg_socket(pkg);
2040 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002041 if (sad_ha)
2042 ch_add = 4;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002043
2044 if (a7mode) {
2045 /* MCChanShiftUpEnable */
2046 pci_read_config_dword(pvt->pci_ha0,
2047 HASWELL_HASYSDEFEATURE2, &reg);
2048 shiftup = GET_BITFIELD(reg, 22, 22);
2049 }
2050
2051 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2052 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002053 } else {
2054 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002055 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002056 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2057 *socket = sad_pkg_socket(pkg);
2058 sad_ha = sad_pkg_ha(pkg);
Tony Luck7d375bf2015-05-18 17:50:42 -03002059 if (sad_ha)
2060 ch_add = 4;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002061 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2062 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002063 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002064
Tony Luck7d375bf2015-05-18 17:50:42 -03002065 *ha = sad_ha;
2066
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002067 /*
2068 * Move to the proper node structure, in order to access the
2069 * right PCI registers
2070 */
2071 new_mci = get_mci_for_node_id(*socket);
2072 if (!new_mci) {
2073 sprintf(msg, "Struct for socket #%u wasn't initialized",
2074 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002075 return -EINVAL;
2076 }
2077 mci = new_mci;
2078 pvt = mci->pvt_info;
2079
2080 /*
2081 * Step 2) Get memory channel
2082 */
2083 prv = 0;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002084 if (pvt->info.type == SANDY_BRIDGE)
2085 pci_ha = pvt->pci_ha0;
2086 else {
2087 if (sad_ha)
2088 pci_ha = pvt->pci_ha1;
2089 else
2090 pci_ha = pvt->pci_ha0;
2091 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002092 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002093 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002094 limit = TAD_LIMIT(reg);
2095 if (limit <= prv) {
2096 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002097 return -EINVAL;
2098 }
2099 if (addr <= limit)
2100 break;
2101 prv = limit;
2102 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002103 if (n_tads == MAX_TAD) {
2104 sprintf(msg, "Can't discover the memory channel");
2105 return -EINVAL;
2106 }
2107
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002108 ch_way = TAD_CH(reg) + 1;
Tony Luckff15e952016-04-14 10:21:52 -07002109 sck_way = TAD_SOCK(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002110
2111 if (ch_way == 3)
2112 idx = addr >> 6;
Tony Luckea5dfb52016-04-14 10:22:02 -07002113 else {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002114 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Tony Luckea5dfb52016-04-14 10:22:02 -07002115 if (pvt->is_chan_hash)
2116 idx = haswell_chan_hash(idx, addr);
2117 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002118 idx = idx % ch_way;
2119
2120 /*
2121 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2122 */
2123 switch (idx) {
2124 case 0:
2125 base_ch = TAD_TGT0(reg);
2126 break;
2127 case 1:
2128 base_ch = TAD_TGT1(reg);
2129 break;
2130 case 2:
2131 base_ch = TAD_TGT2(reg);
2132 break;
2133 case 3:
2134 base_ch = TAD_TGT3(reg);
2135 break;
2136 default:
2137 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002138 return -EINVAL;
2139 }
2140 *channel_mask = 1 << base_ch;
2141
Tony Luck7d375bf2015-05-18 17:50:42 -03002142 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002143 tad_ch_nilv_offset[n_tads],
2144 &tad_offset);
2145
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002146 if (pvt->is_mirrored) {
2147 *channel_mask |= 1 << ((base_ch + 2) % 4);
2148 switch(ch_way) {
2149 case 2:
2150 case 4:
Tony Luckff15e952016-04-14 10:21:52 -07002151 sck_xch = (1 << sck_way) * (ch_way >> 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002152 break;
2153 default:
2154 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002155 return -EINVAL;
2156 }
2157 } else
2158 sck_xch = (1 << sck_way) * ch_way;
2159
2160 if (pvt->is_lockstep)
2161 *channel_mask |= 1 << ((base_ch + 1) % 4);
2162
2163 offset = TAD_OFFSET(tad_offset);
2164
Joe Perches956b9ba2012-04-29 17:08:39 -03002165 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",
2166 n_tads,
2167 addr,
2168 limit,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002169 sck_way,
Joe Perches956b9ba2012-04-29 17:08:39 -03002170 ch_way,
2171 offset,
2172 idx,
2173 base_ch,
2174 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002175
2176 /* Calculate channel address */
2177 /* Remove the TAD offset */
2178
2179 if (offset > addr) {
2180 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2181 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002182 return -EINVAL;
2183 }
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002184
2185 ch_addr = addr - offset;
2186 ch_addr >>= (6 + shiftup);
Tony Luckff15e952016-04-14 10:21:52 -07002187 ch_addr /= sck_xch;
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002188 ch_addr <<= (6 + shiftup);
2189 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002190
2191 /*
2192 * Step 3) Decode rank
2193 */
2194 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
Tony Luck7d375bf2015-05-18 17:50:42 -03002195 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002196 rir_way_limit[n_rir],
2197 &reg);
2198
2199 if (!IS_RIR_VALID(reg))
2200 continue;
2201
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002202 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01002203 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03002204 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2205 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01002206 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03002207 limit,
2208 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002209 if (ch_addr <= limit)
2210 break;
2211 }
2212 if (n_rir == MAX_RIR_RANGES) {
2213 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2214 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002215 return -EINVAL;
2216 }
2217 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002218
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002219 if (pvt->is_close_pg)
2220 idx = (ch_addr >> 6);
2221 else
2222 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2223 idx %= 1 << rir_way;
2224
Tony Luck7d375bf2015-05-18 17:50:42 -03002225 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002226 rir_offset[n_rir][idx],
2227 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07002228 *rank = RIR_RNK_TGT(pvt->info.type, reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002229
Joe Perches956b9ba2012-04-29 17:08:39 -03002230 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2231 n_rir,
2232 ch_addr,
2233 limit,
2234 rir_way,
2235 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002236
2237 return 0;
2238}
2239
2240/****************************************************************************
2241 Device initialization routines: put/get, init/exit
2242 ****************************************************************************/
2243
2244/*
2245 * sbridge_put_all_devices 'put' all the devices that we have
2246 * reserved via 'get'
2247 */
2248static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2249{
2250 int i;
2251
Joe Perches956b9ba2012-04-29 17:08:39 -03002252 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002253 for (i = 0; i < sbridge_dev->n_devs; i++) {
2254 struct pci_dev *pdev = sbridge_dev->pdev[i];
2255 if (!pdev)
2256 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03002257 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2258 pdev->bus->number,
2259 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002260 pci_dev_put(pdev);
2261 }
2262}
2263
2264static void sbridge_put_all_devices(void)
2265{
2266 struct sbridge_dev *sbridge_dev, *tmp;
2267
2268 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2269 sbridge_put_devices(sbridge_dev);
2270 free_sbridge_dev(sbridge_dev);
2271 }
2272}
2273
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002274static int sbridge_get_onedevice(struct pci_dev **prev,
2275 u8 *num_mc,
2276 const struct pci_id_table *table,
Jim Snowc1979ba2015-12-03 10:48:53 +01002277 const unsigned devno,
2278 const int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002279{
2280 struct sbridge_dev *sbridge_dev;
2281 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002282 struct pci_dev *pdev = NULL;
2283 u8 bus = 0;
2284
Jiang Liuec5a0b32014-02-17 13:10:23 +08002285 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002286 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002287 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2288
2289 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2290 dev_descr->dev_id, *prev);
2291
2292 if (!pdev) {
2293 if (*prev) {
2294 *prev = pdev;
2295 return 0;
2296 }
2297
2298 if (dev_descr->optional)
2299 return 0;
2300
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002301 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002302 if (devno == 0)
2303 return -ENODEV;
2304
2305 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002306 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002307 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2308
2309 /* End of list, leave */
2310 return -ENODEV;
2311 }
2312 bus = pdev->bus->number;
2313
Jim Snowc1979ba2015-12-03 10:48:53 +01002314 sbridge_dev = get_sbridge_dev(bus, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002315 if (!sbridge_dev) {
2316 sbridge_dev = alloc_sbridge_dev(bus, table);
2317 if (!sbridge_dev) {
2318 pci_dev_put(pdev);
2319 return -ENOMEM;
2320 }
2321 (*num_mc)++;
2322 }
2323
2324 if (sbridge_dev->pdev[devno]) {
2325 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002326 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002327 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2328 pci_dev_put(pdev);
2329 return -ENODEV;
2330 }
2331
2332 sbridge_dev->pdev[devno] = pdev;
2333
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002334 /* Be sure that the device is enabled */
2335 if (unlikely(pci_enable_device(pdev) < 0)) {
2336 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002337 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002338 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2339 return -ENODEV;
2340 }
2341
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002342 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -03002343 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002344
2345 /*
2346 * As stated on drivers/pci/search.c, the reference count for
2347 * @from is always decremented if it is not %NULL. So, as we need
2348 * to get all devices up to null, we need to do a get for the device
2349 */
2350 pci_dev_get(pdev);
2351
2352 *prev = pdev;
2353
2354 return 0;
2355}
2356
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002357/*
2358 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002359 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002360 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03002361 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002362 * @table: model specific table
2363 *
2364 * returns 0 in case of success or error code
2365 */
Tony Luck0ba169ac2016-07-14 15:38:43 -07002366static int sbridge_get_all_devices(u8 *num_mc,
2367 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002368{
2369 int i, rc;
2370 struct pci_dev *pdev = NULL;
Tony Luck0ba169ac2016-07-14 15:38:43 -07002371 int allow_dups = 0;
2372 int multi_bus = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002373
Tony Luck0ba169ac2016-07-14 15:38:43 -07002374 if (table->type == KNIGHTS_LANDING)
2375 allow_dups = multi_bus = 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002376 while (table && table->descr) {
2377 for (i = 0; i < table->n_devs; i++) {
Jim Snowc1979ba2015-12-03 10:48:53 +01002378 if (!allow_dups || i == 0 ||
2379 table->descr[i].dev_id !=
2380 table->descr[i-1].dev_id) {
2381 pdev = NULL;
2382 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002383 do {
2384 rc = sbridge_get_onedevice(&pdev, num_mc,
Jim Snowc1979ba2015-12-03 10:48:53 +01002385 table, i, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002386 if (rc < 0) {
2387 if (i == 0) {
2388 i = table->n_devs;
2389 break;
2390 }
2391 sbridge_put_all_devices();
2392 return -ENODEV;
2393 }
Jim Snowc1979ba2015-12-03 10:48:53 +01002394 } while (pdev && !allow_dups);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002395 }
2396 table++;
2397 }
2398
2399 return 0;
2400}
2401
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03002402static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2403 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002404{
2405 struct sbridge_pvt *pvt = mci->pvt_info;
2406 struct pci_dev *pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002407 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002408 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002409
2410 for (i = 0; i < sbridge_dev->n_devs; i++) {
2411 pdev = sbridge_dev->pdev[i];
2412 if (!pdev)
2413 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002414
2415 switch (pdev->device) {
2416 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2417 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002418 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002419 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2420 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002421 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002422 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2423 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002424 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002425 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2426 pvt->pci_ha0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002427 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002428 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2429 pvt->pci_ta = pdev;
2430 break;
2431 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2432 pvt->pci_ras = pdev;
2433 break;
2434 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2435 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2436 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2437 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2438 {
2439 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
2440 pvt->pci_tad[id] = pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002441 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002442 }
2443 break;
2444 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2445 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002446 break;
2447 default:
2448 goto error;
2449 }
2450
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002451 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2452 pdev->vendor, pdev->device,
Joe Perches956b9ba2012-04-29 17:08:39 -03002453 sbridge_dev->bus,
Joe Perches956b9ba2012-04-29 17:08:39 -03002454 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002455 }
2456
2457 /* Check if everything were registered */
2458 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002459 !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002460 goto enodev;
2461
Seth Jennings2900ea62015-08-05 13:16:01 -05002462 if (saw_chan_mask != 0x0f)
2463 goto enodev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002464 return 0;
2465
2466enodev:
2467 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2468 return -ENODEV;
2469
2470error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002471 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2472 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002473 return -EINVAL;
2474}
2475
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002476static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2477 struct sbridge_dev *sbridge_dev)
2478{
2479 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002480 struct pci_dev *pdev;
2481 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002482 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002483
2484 for (i = 0; i < sbridge_dev->n_devs; i++) {
2485 pdev = sbridge_dev->pdev[i];
2486 if (!pdev)
2487 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002488
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002489 switch (pdev->device) {
2490 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
2491 pvt->pci_ha0 = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002492 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002493 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2494 pvt->pci_ta = pdev;
2495 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
2496 pvt->pci_ras = pdev;
2497 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002498 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2499 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002500 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2501 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002502 {
2503 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
2504 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002505 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002506 }
2507 break;
2508 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2509 pvt->pci_ddrio = pdev;
2510 break;
2511 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
Tony Luck7d375bf2015-05-18 17:50:42 -03002512 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002513 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002514 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2515 pvt->pci_sad0 = pdev;
2516 break;
2517 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2518 pvt->pci_br0 = pdev;
2519 break;
2520 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2521 pvt->pci_br1 = pdev;
2522 break;
2523 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
2524 pvt->pci_ha1 = pdev;
2525 break;
2526 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2527 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002528 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2529 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002530 {
Tony Luck7d375bf2015-05-18 17:50:42 -03002531 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002532 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002533 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002534 }
2535 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002536 default:
2537 goto error;
2538 }
2539
2540 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2541 sbridge_dev->bus,
2542 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2543 pdev);
2544 }
2545
2546 /* Check if everything were registered */
2547 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002548 !pvt->pci_br1 || !pvt->pci_ras || !pvt->pci_ta)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002549 goto enodev;
2550
Tony Luck7d375bf2015-05-18 17:50:42 -03002551 if (saw_chan_mask != 0x0f && /* -EN */
2552 saw_chan_mask != 0x33 && /* -EP */
2553 saw_chan_mask != 0xff) /* -EX */
2554 goto enodev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002555 return 0;
2556
2557enodev:
2558 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2559 return -ENODEV;
2560
2561error:
2562 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002563 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2564 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002565 return -EINVAL;
2566}
2567
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002568static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2569 struct sbridge_dev *sbridge_dev)
2570{
2571 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002572 struct pci_dev *pdev;
2573 u8 saw_chan_mask = 0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002574 int i;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002575
2576 /* there's only one device per system; not tied to any bus */
2577 if (pvt->info.pci_vtd == NULL)
2578 /* result will be checked later */
2579 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2580 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2581 NULL);
2582
2583 for (i = 0; i < sbridge_dev->n_devs; i++) {
2584 pdev = sbridge_dev->pdev[i];
2585 if (!pdev)
2586 continue;
2587
2588 switch (pdev->device) {
2589 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2590 pvt->pci_sad0 = pdev;
2591 break;
2592 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2593 pvt->pci_sad1 = pdev;
2594 break;
2595 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2596 pvt->pci_ha0 = pdev;
2597 break;
2598 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2599 pvt->pci_ta = pdev;
2600 break;
2601 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
2602 pvt->pci_ras = pdev;
2603 break;
2604 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002605 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002606 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002607 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
Tony Luck7d375bf2015-05-18 17:50:42 -03002608 {
2609 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
2610
2611 pvt->pci_tad[id] = pdev;
2612 saw_chan_mask |= 1 << id;
2613 }
2614 break;
2615 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2616 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2617 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2618 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2619 {
2620 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
2621
2622 pvt->pci_tad[id] = pdev;
2623 saw_chan_mask |= 1 << id;
2624 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002625 break;
2626 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
Aristeu Rozanski71793852015-06-12 09:44:52 -04002627 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2628 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2629 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2630 if (!pvt->pci_ddrio)
2631 pvt->pci_ddrio = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002632 break;
2633 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
2634 pvt->pci_ha1 = pdev;
2635 break;
2636 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
2637 pvt->pci_ha1_ta = pdev;
2638 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002639 default:
2640 break;
2641 }
2642
2643 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2644 sbridge_dev->bus,
2645 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2646 pdev);
2647 }
2648
2649 /* Check if everything were registered */
2650 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2651 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2652 goto enodev;
2653
Tony Luck7d375bf2015-05-18 17:50:42 -03002654 if (saw_chan_mask != 0x0f && /* -EN */
2655 saw_chan_mask != 0x33 && /* -EP */
2656 saw_chan_mask != 0xff) /* -EX */
2657 goto enodev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002658 return 0;
2659
2660enodev:
2661 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2662 return -ENODEV;
2663}
2664
Tony Luck1f395812014-12-02 09:27:30 -08002665static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2666 struct sbridge_dev *sbridge_dev)
2667{
2668 struct sbridge_pvt *pvt = mci->pvt_info;
2669 struct pci_dev *pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002670 u8 saw_chan_mask = 0;
Tony Luck1f395812014-12-02 09:27:30 -08002671 int i;
2672
2673 /* there's only one device per system; not tied to any bus */
2674 if (pvt->info.pci_vtd == NULL)
2675 /* result will be checked later */
2676 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2677 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2678 NULL);
2679
2680 for (i = 0; i < sbridge_dev->n_devs; i++) {
2681 pdev = sbridge_dev->pdev[i];
2682 if (!pdev)
2683 continue;
2684
2685 switch (pdev->device) {
2686 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2687 pvt->pci_sad0 = pdev;
2688 break;
2689 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2690 pvt->pci_sad1 = pdev;
2691 break;
2692 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2693 pvt->pci_ha0 = pdev;
2694 break;
2695 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2696 pvt->pci_ta = pdev;
2697 break;
2698 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
2699 pvt->pci_ras = pdev;
2700 break;
2701 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
Tony Luck1f395812014-12-02 09:27:30 -08002702 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
Tony Luck1f395812014-12-02 09:27:30 -08002703 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
Tony Luck1f395812014-12-02 09:27:30 -08002704 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
Tony Luckfa2ce642015-05-20 19:10:35 -03002705 {
2706 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
2707 pvt->pci_tad[id] = pdev;
2708 saw_chan_mask |= 1 << id;
2709 }
2710 break;
2711 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2712 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2713 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2714 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2715 {
2716 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
2717 pvt->pci_tad[id] = pdev;
2718 saw_chan_mask |= 1 << id;
2719 }
Tony Luck1f395812014-12-02 09:27:30 -08002720 break;
2721 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2722 pvt->pci_ddrio = pdev;
2723 break;
Tony Luckfa2ce642015-05-20 19:10:35 -03002724 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2725 pvt->pci_ha1 = pdev;
2726 break;
2727 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2728 pvt->pci_ha1_ta = pdev;
2729 break;
Tony Luck1f395812014-12-02 09:27:30 -08002730 default:
2731 break;
2732 }
2733
2734 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2735 sbridge_dev->bus,
2736 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2737 pdev);
2738 }
2739
2740 /* Check if everything were registered */
2741 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2742 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2743 goto enodev;
2744
Tony Luckfa2ce642015-05-20 19:10:35 -03002745 if (saw_chan_mask != 0x0f && /* -EN */
2746 saw_chan_mask != 0x33 && /* -EP */
2747 saw_chan_mask != 0xff) /* -EX */
2748 goto enodev;
Tony Luck1f395812014-12-02 09:27:30 -08002749 return 0;
2750
2751enodev:
2752 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2753 return -ENODEV;
2754}
2755
Jim Snowd0cdf902015-12-03 10:48:54 +01002756static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2757 struct sbridge_dev *sbridge_dev)
2758{
2759 struct sbridge_pvt *pvt = mci->pvt_info;
2760 struct pci_dev *pdev;
2761 int dev, func;
2762
2763 int i;
2764 int devidx;
2765
2766 for (i = 0; i < sbridge_dev->n_devs; i++) {
2767 pdev = sbridge_dev->pdev[i];
2768 if (!pdev)
2769 continue;
2770
2771 /* Extract PCI device and function. */
2772 dev = (pdev->devfn >> 3) & 0x1f;
2773 func = pdev->devfn & 0x7;
2774
2775 switch (pdev->device) {
2776 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2777 if (dev == 8)
2778 pvt->knl.pci_mc0 = pdev;
2779 else if (dev == 9)
2780 pvt->knl.pci_mc1 = pdev;
2781 else {
2782 sbridge_printk(KERN_ERR,
2783 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2784 dev, func);
2785 continue;
2786 }
2787 break;
2788
2789 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2790 pvt->pci_sad0 = pdev;
2791 break;
2792
2793 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2794 pvt->pci_sad1 = pdev;
2795 break;
2796
2797 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2798 /* There are one of these per tile, and range from
2799 * 1.14.0 to 1.18.5.
2800 */
2801 devidx = ((dev-14)*8)+func;
2802
2803 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2804 sbridge_printk(KERN_ERR,
2805 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2806 dev, func);
2807 continue;
2808 }
2809
2810 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2811
2812 pvt->knl.pci_cha[devidx] = pdev;
2813 break;
2814
2815 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL:
2816 devidx = -1;
2817
2818 /*
2819 * MC0 channels 0-2 are device 9 function 2-4,
2820 * MC1 channels 3-5 are device 8 function 2-4.
2821 */
2822
2823 if (dev == 9)
2824 devidx = func-2;
2825 else if (dev == 8)
2826 devidx = 3 + (func-2);
2827
2828 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2829 sbridge_printk(KERN_ERR,
2830 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2831 dev, func);
2832 continue;
2833 }
2834
2835 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2836 pvt->knl.pci_channel[devidx] = pdev;
2837 break;
2838
2839 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2840 pvt->knl.pci_mc_info = pdev;
2841 break;
2842
2843 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2844 pvt->pci_ta = pdev;
2845 break;
2846
2847 default:
2848 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2849 pdev->device);
2850 break;
2851 }
2852 }
2853
2854 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2855 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2856 !pvt->pci_ta) {
2857 goto enodev;
2858 }
2859
2860 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2861 if (!pvt->knl.pci_channel[i]) {
2862 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2863 goto enodev;
2864 }
2865 }
2866
2867 for (i = 0; i < KNL_MAX_CHAS; i++) {
2868 if (!pvt->knl.pci_cha[i]) {
2869 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2870 goto enodev;
2871 }
2872 }
2873
2874 return 0;
2875
2876enodev:
2877 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2878 return -ENODEV;
2879}
2880
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002881/****************************************************************************
2882 Error check routines
2883 ****************************************************************************/
2884
2885/*
2886 * While Sandy Bridge has error count registers, SMI BIOS read values from
2887 * and resets the counters. So, they are not reliable for the OS to read
2888 * from them. So, we have no option but to just trust on whatever MCE is
2889 * telling us about the errors.
2890 */
2891static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2892 const struct mce *m)
2893{
2894 struct mem_ctl_info *new_mci;
2895 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002896 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002897 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002898 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2899 bool overflow = GET_BITFIELD(m->status, 62, 62);
2900 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002901 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002902 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2903 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2904 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2905 u32 channel = GET_BITFIELD(m->status, 0, 3);
2906 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2907 long channel_mask, first_channel;
Tony Luck7d375bf2015-05-18 17:50:42 -03002908 u8 rank, socket, ha;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002909 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002910 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002911
Tony Luckfa2ce642015-05-20 19:10:35 -03002912 if (pvt->info.type != SANDY_BRIDGE)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002913 recoverable = true;
2914 else
2915 recoverable = GET_BITFIELD(m->status, 56, 56);
2916
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002917 if (uncorrected_error) {
2918 if (ripv) {
2919 type = "FATAL";
2920 tp_event = HW_EVENT_ERR_FATAL;
2921 } else {
2922 type = "NON_FATAL";
2923 tp_event = HW_EVENT_ERR_UNCORRECTED;
2924 }
2925 } else {
2926 type = "CORRECTED";
2927 tp_event = HW_EVENT_ERR_CORRECTED;
2928 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002929
2930 /*
David Mackey15ed1032012-04-17 11:30:52 -07002931 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002932 * memory errors should fit in this mask:
2933 * 000f 0000 1mmm cccc (binary)
2934 * where:
2935 * f = Correction Report Filtering Bit. If 1, subsequent errors
2936 * won't be shown
2937 * mmm = error type
2938 * cccc = channel
2939 * If the mask doesn't match, report an error to the parsing logic
2940 */
2941 if (! ((errcode & 0xef80) == 0x80)) {
2942 optype = "Can't parse: it is not a mem";
2943 } else {
2944 switch (optypenum) {
2945 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002946 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002947 break;
2948 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002949 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002950 break;
2951 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002952 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002953 break;
2954 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002955 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002956 break;
2957 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002958 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002959 break;
2960 default:
2961 optype = "reserved";
2962 break;
2963 }
2964 }
2965
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03002966 /* Only decode errors with an valid address (ADDRV) */
2967 if (!GET_BITFIELD(m->status, 58, 58))
2968 return;
2969
Jim Snowd0cdf902015-12-03 10:48:54 +01002970 if (pvt->info.type == KNIGHTS_LANDING) {
2971 if (channel == 14) {
2972 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2973 overflow ? " OVERFLOW" : "",
2974 (uncorrected_error && recoverable)
2975 ? " recoverable" : "",
2976 mscod, errcode,
2977 m->bank);
2978 } else {
2979 char A = *("A");
2980
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02002981 /*
2982 * Reported channel is in range 0-2, so we can't map it
2983 * back to mc. To figure out mc we check machine check
2984 * bank register that reported this error.
2985 * bank15 means mc0 and bank16 means mc1.
2986 */
2987 channel = knl_channel_remap(m->bank == 16, channel);
Jim Snowd0cdf902015-12-03 10:48:54 +01002988 channel_mask = 1 << channel;
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02002989
Jim Snowd0cdf902015-12-03 10:48:54 +01002990 snprintf(msg, sizeof(msg),
2991 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
2992 overflow ? " OVERFLOW" : "",
2993 (uncorrected_error && recoverable)
2994 ? " recoverable" : " ",
2995 mscod, errcode, channel, A + channel);
2996 edac_mc_handle_error(tp_event, mci, core_err_cnt,
2997 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
2998 channel, 0, -1,
2999 optype, msg);
3000 }
3001 return;
3002 } else {
3003 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
3004 &channel_mask, &rank, &area_type, msg);
3005 }
3006
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003007 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003008 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003009 new_mci = get_mci_for_node_id(socket);
3010 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003011 strcpy(msg, "Error: socket got corrupted!");
3012 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003013 }
3014 mci = new_mci;
3015 pvt = mci->pvt_info;
3016
3017 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
3018
3019 if (rank < 4)
3020 dimm = 0;
3021 else if (rank < 8)
3022 dimm = 1;
3023 else
3024 dimm = 2;
3025
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003026
3027 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003028 * FIXME: On some memory configurations (mirror, lockstep), the
3029 * Memory Controller can't point the error to a single DIMM. The
3030 * EDAC core should be handling the channel mask, in order to point
3031 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003032 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03003033 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
3034 channel = first_channel;
3035
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003036 snprintf(msg, sizeof(msg),
Tony Luck7d375bf2015-05-18 17:50:42 -03003037 "%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 -03003038 overflow ? " OVERFLOW" : "",
3039 (uncorrected_error && recoverable) ? " recoverable" : "",
3040 area_type,
3041 mscod, errcode,
Tony Luck7d375bf2015-05-18 17:50:42 -03003042 socket, ha,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003043 channel_mask,
3044 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003045
Joe Perches956b9ba2012-04-29 17:08:39 -03003046 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003047
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003048 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003049
Seth Jennings351fc4a2014-09-05 14:28:47 -05003050 if (channel == CHANNEL_UNSPECIFIED)
3051 channel = -1;
3052
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003053 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003054 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003055 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
Tony Luck7d375bf2015-05-18 17:50:42 -03003056 4*ha+channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003057 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003058 return;
3059err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003060 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003061 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003062 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003063
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003064}
3065
3066/*
Tony Luckad08c4e2016-04-15 14:50:32 -07003067 * Check that logging is enabled and that this is the right type
3068 * of error for us to handle.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003069 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003070static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3071 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003072{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003073 struct mce *mce = (struct mce *)data;
3074 struct mem_ctl_info *mci;
3075 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003076 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003077
Chen, Gongfd521032013-12-06 01:17:09 -05003078 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3079 return NOTIFY_DONE;
3080
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003081 mci = get_mci_for_node_id(mce->socketid);
3082 if (!mci)
Tony Luckc4fc1952016-04-29 15:42:25 +02003083 return NOTIFY_DONE;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003084 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003085
3086 /*
3087 * Just let mcelog handle it if the error is
3088 * outside the memory controller. A memory error
3089 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3090 * bit 12 has an special meaning.
3091 */
3092 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003093 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003094
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003095 if (mce->mcgstatus & MCG_STATUS_MCIP)
3096 type = "Exception";
3097 else
3098 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003099
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003100 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003101
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003102 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3103 "Bank %d: %016Lx\n", mce->extcpu, type,
3104 mce->mcgstatus, mce->bank, mce->status);
3105 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3106 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3107 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003108
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003109 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3110 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3111 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003112
Tony Luckad08c4e2016-04-15 14:50:32 -07003113 sbridge_mce_output_error(mci, mce);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003114
3115 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003116 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003117}
3118
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003119static struct notifier_block sbridge_mce_dec = {
Borislav Petkov9026cc82017-01-23 19:35:14 +01003120 .notifier_call = sbridge_mce_check_error,
3121 .priority = MCE_PRIO_EDAC,
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003122};
3123
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003124/****************************************************************************
3125 EDAC register/unregister logic
3126 ****************************************************************************/
3127
3128static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3129{
3130 struct mem_ctl_info *mci = sbridge_dev->mci;
3131 struct sbridge_pvt *pvt;
3132
3133 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003134 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003135
3136 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3137 return;
3138 }
3139
3140 pvt = mci->pvt_info;
3141
Joe Perches956b9ba2012-04-29 17:08:39 -03003142 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3143 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003144
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003145 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03003146 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003147
Joe Perches956b9ba2012-04-29 17:08:39 -03003148 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003149 kfree(mci->ctl_name);
3150 edac_mc_free(mci);
3151 sbridge_dev->mci = NULL;
3152}
3153
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003154static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003155{
3156 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003157 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003158 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003159 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003160 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003161
3162 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03003163 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003164 if (unlikely(rc < 0))
3165 return rc;
3166
3167 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003168 layers[0].type = EDAC_MC_LAYER_CHANNEL;
Jim Snowd0cdf902015-12-03 10:48:54 +01003169 layers[0].size = type == KNIGHTS_LANDING ?
3170 KNL_MAX_CHANNELS : NUM_CHANNELS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003171 layers[0].is_virt_csrow = false;
3172 layers[1].type = EDAC_MC_LAYER_SLOT;
Jim Snowd0cdf902015-12-03 10:48:54 +01003173 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003174 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03003175 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003176 sizeof(*pvt));
3177
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003178 if (unlikely(!mci))
3179 return -ENOMEM;
3180
Joe Perches956b9ba2012-04-29 17:08:39 -03003181 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003182 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003183
3184 pvt = mci->pvt_info;
3185 memset(pvt, 0, sizeof(*pvt));
3186
3187 /* Associate sbridge_dev and mci for future usage */
3188 pvt->sbridge_dev = sbridge_dev;
3189 sbridge_dev->mci = mci;
3190
Jim Snowd0cdf902015-12-03 10:48:54 +01003191 mci->mtype_cap = type == KNIGHTS_LANDING ?
3192 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003193 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3194 mci->edac_cap = EDAC_FLAG_NONE;
3195 mci->mod_name = "sbridge_edac.c";
3196 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003197 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003198 mci->ctl_page_to_phys = NULL;
3199
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003200 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003201 switch (type) {
3202 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003203 pvt->info.rankcfgr = IB_RANK_CFG_A;
3204 pvt->info.get_tolm = ibridge_get_tolm;
3205 pvt->info.get_tohm = ibridge_get_tohm;
3206 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003207 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003208 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003209 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003210 pvt->info.sad_limit = sad_limit;
3211 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003212 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003213 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3214 pvt->info.interleave_list = ibridge_interleave_list;
3215 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3216 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003217 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003218 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
3219
3220 /* Store pci devices at mci for faster access */
3221 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3222 if (unlikely(rc < 0))
3223 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003224 break;
3225 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003226 pvt->info.rankcfgr = SB_RANK_CFG_A;
3227 pvt->info.get_tolm = sbridge_get_tolm;
3228 pvt->info.get_tohm = sbridge_get_tohm;
3229 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003230 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003231 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003232 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003233 pvt->info.sad_limit = sad_limit;
3234 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003235 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003236 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3237 pvt->info.interleave_list = sbridge_interleave_list;
3238 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3239 pvt->info.interleave_pkg = sbridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003240 pvt->info.get_width = sbridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003241 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
3242
3243 /* Store pci devices at mci for faster access */
3244 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3245 if (unlikely(rc < 0))
3246 goto fail0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003247 break;
3248 case HASWELL:
3249 /* rankcfgr isn't used */
3250 pvt->info.get_tolm = haswell_get_tolm;
3251 pvt->info.get_tohm = haswell_get_tohm;
3252 pvt->info.dram_rule = ibridge_dram_rule;
3253 pvt->info.get_memory_type = haswell_get_memory_type;
3254 pvt->info.get_node_id = haswell_get_node_id;
3255 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003256 pvt->info.sad_limit = sad_limit;
3257 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003258 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003259 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3260 pvt->info.interleave_list = ibridge_interleave_list;
3261 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3262 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003263 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003264 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003265
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003266 /* Store pci devices at mci for faster access */
3267 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3268 if (unlikely(rc < 0))
3269 goto fail0;
3270 break;
Tony Luck1f395812014-12-02 09:27:30 -08003271 case BROADWELL:
3272 /* rankcfgr isn't used */
3273 pvt->info.get_tolm = haswell_get_tolm;
3274 pvt->info.get_tohm = haswell_get_tohm;
3275 pvt->info.dram_rule = ibridge_dram_rule;
3276 pvt->info.get_memory_type = haswell_get_memory_type;
3277 pvt->info.get_node_id = haswell_get_node_id;
3278 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003279 pvt->info.sad_limit = sad_limit;
3280 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003281 pvt->info.dram_attr = dram_attr;
Tony Luck1f395812014-12-02 09:27:30 -08003282 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3283 pvt->info.interleave_list = ibridge_interleave_list;
3284 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3285 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003286 pvt->info.get_width = broadwell_get_width;
Tony Luck1f395812014-12-02 09:27:30 -08003287 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
3288
3289 /* Store pci devices at mci for faster access */
3290 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3291 if (unlikely(rc < 0))
3292 goto fail0;
3293 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01003294 case KNIGHTS_LANDING:
3295 /* pvt->info.rankcfgr == ??? */
3296 pvt->info.get_tolm = knl_get_tolm;
3297 pvt->info.get_tohm = knl_get_tohm;
3298 pvt->info.dram_rule = knl_dram_rule;
3299 pvt->info.get_memory_type = knl_get_memory_type;
3300 pvt->info.get_node_id = knl_get_node_id;
3301 pvt->info.rir_limit = NULL;
3302 pvt->info.sad_limit = knl_sad_limit;
3303 pvt->info.interleave_mode = knl_interleave_mode;
Jim Snowd0cdf902015-12-03 10:48:54 +01003304 pvt->info.dram_attr = dram_attr_knl;
3305 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3306 pvt->info.interleave_list = knl_interleave_list;
3307 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3308 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +01003309 pvt->info.get_width = knl_get_width;
Jim Snowd0cdf902015-12-03 10:48:54 +01003310 mci->ctl_name = kasprintf(GFP_KERNEL,
3311 "Knights Landing Socket#%d", mci->mc_idx);
3312
3313 rc = knl_mci_bind_devs(mci, sbridge_dev);
3314 if (unlikely(rc < 0))
3315 goto fail0;
3316 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003317 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003318
3319 /* Get dimm basic config and the memory layout */
3320 get_dimm_config(mci);
3321 get_memory_layout(mci);
3322
3323 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003324 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003325
3326 /* add this new MC control structure to EDAC's list of MCs */
3327 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003328 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003329 rc = -EINVAL;
3330 goto fail0;
3331 }
3332
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003333 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003334
3335fail0:
3336 kfree(mci->ctl_name);
3337 edac_mc_free(mci);
3338 sbridge_dev->mci = NULL;
3339 return rc;
3340}
3341
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003342#define ICPU(model, table) \
3343 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3344
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003345static const struct x86_cpu_id sbridge_cpuids[] = {
Dave Hansen20f4d692016-09-29 13:43:21 -07003346 ICPU(INTEL_FAM6_SANDYBRIDGE_X, pci_dev_descr_sbridge_table),
3347 ICPU(INTEL_FAM6_IVYBRIDGE_X, pci_dev_descr_ibridge_table),
3348 ICPU(INTEL_FAM6_HASWELL_X, pci_dev_descr_haswell_table),
3349 ICPU(INTEL_FAM6_BROADWELL_X, pci_dev_descr_broadwell_table),
3350 ICPU(INTEL_FAM6_BROADWELL_XEON_D, pci_dev_descr_broadwell_table),
3351 ICPU(INTEL_FAM6_XEON_PHI_KNL, pci_dev_descr_knl_table),
Piotr Luc9a9260ca2016-10-13 17:30:59 +02003352 ICPU(INTEL_FAM6_XEON_PHI_KNM, pci_dev_descr_knl_table),
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003353 { }
3354};
3355MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3356
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003357/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003358 * sbridge_probe Get all devices and register memory controllers
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003359 * present.
3360 * return:
3361 * 0 for FOUND a device
3362 * < 0 for error code
3363 */
3364
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003365static int sbridge_probe(const struct x86_cpu_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003366{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003367 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003368 u8 mc, num_mc = 0;
3369 struct sbridge_dev *sbridge_dev;
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003370 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003371
3372 /* get the pci devices we want to reserve for our use */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003373 rc = sbridge_get_all_devices(&num_mc, ptable);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003374
Borislav Petkov11249e72015-02-05 12:39:36 +01003375 if (unlikely(rc < 0)) {
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003376 edac_dbg(0, "couldn't get all devices\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003377 goto fail0;
Borislav Petkov11249e72015-02-05 12:39:36 +01003378 }
3379
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003380 mc = 0;
3381
3382 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003383 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3384 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003385
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003386 sbridge_dev->mc = mc++;
Tony Luck665f05e02016-06-02 10:58:08 -07003387 rc = sbridge_register_mci(sbridge_dev, ptable->type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003388 if (unlikely(rc < 0))
3389 goto fail1;
3390 }
3391
Borislav Petkov11249e72015-02-05 12:39:36 +01003392 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003393
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003394 return 0;
3395
3396fail1:
3397 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3398 sbridge_unregister_mci(sbridge_dev);
3399
3400 sbridge_put_all_devices();
3401fail0:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003402 return rc;
3403}
3404
3405/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003406 * sbridge_remove cleanup
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003407 *
3408 */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003409static void sbridge_remove(void)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003410{
3411 struct sbridge_dev *sbridge_dev;
3412
Joe Perches956b9ba2012-04-29 17:08:39 -03003413 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003414
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003415 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3416 sbridge_unregister_mci(sbridge_dev);
3417
3418 /* Release PCI resources */
3419 sbridge_put_all_devices();
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003420}
3421
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003422/*
3423 * sbridge_init Module entry function
3424 * Try to initialize this module for its devices
3425 */
3426static int __init sbridge_init(void)
3427{
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003428 const struct x86_cpu_id *id;
3429 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003430
Joe Perches956b9ba2012-04-29 17:08:39 -03003431 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003432
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003433 id = x86_match_cpu(sbridge_cpuids);
3434 if (!id)
3435 return -ENODEV;
3436
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003437 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3438 opstate_init();
3439
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003440 rc = sbridge_probe(id);
3441
3442 if (rc >= 0) {
Chen Gonge35fca42012-05-08 20:40:12 -03003443 mce_register_decode_chain(&sbridge_mce_dec);
Chen, Gongfd521032013-12-06 01:17:09 -05003444 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
3445 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003446 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03003447 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003448
3449 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003450 rc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003451
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003452 return rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003453}
3454
3455/*
3456 * sbridge_exit() Module exit function
3457 * Unregister the driver
3458 */
3459static void __exit sbridge_exit(void)
3460{
Joe Perches956b9ba2012-04-29 17:08:39 -03003461 edac_dbg(2, "\n");
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003462 sbridge_remove();
Chen Gonge35fca42012-05-08 20:40:12 -03003463 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003464}
3465
3466module_init(sbridge_init);
3467module_exit(sbridge_exit);
3468
3469module_param(edac_op_state, int, 0444);
3470MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3471
3472MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003473MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003474MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003475MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003476 SBRIDGE_REVISION);