blob: 903ca1f98361d85ceef65aba991953c1c8d253cd [file] [log] [blame]
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001/* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
2 *
3 * This driver supports the memory controllers found on the Intel
4 * processor family Sandy Bridge.
5 *
6 * This file may be distributed under the terms of the
7 * GNU General Public License version 2 only.
8 *
9 * Copyright (c) 2011 by:
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -020010 * Mauro Carvalho Chehab
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020011 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/pci.h>
16#include <linux/pci_ids.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/edac.h>
20#include <linux/mmzone.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020021#include <linux/smp.h>
22#include <linux/bitmap.h>
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -030023#include <linux/math64.h>
Tony Luck2c1ea4c2016-04-28 15:40:00 -070024#include <linux/mod_devicetable.h>
25#include <asm/cpu_device_id.h>
Dave Hansen20f4d692016-09-29 13:43:21 -070026#include <asm/intel-family.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020027#include <asm/processor.h>
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -020028#include <asm/mce.h>
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020029
Mauro Carvalho Chehab78d88e82016-10-29 15:16:34 -020030#include "edac_module.h"
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020031
32/* Static vars */
33static LIST_HEAD(sbridge_edac_list);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020034
35/*
36 * Alter this version for the module when modifications are made
37 */
Tony Luck7d375bf2015-05-18 17:50:42 -030038#define SBRIDGE_REVISION " Ver: 1.1.1 "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020039#define EDAC_MOD_STR "sbridge_edac"
40
41/*
42 * Debug macros
43 */
44#define sbridge_printk(level, fmt, arg...) \
45 edac_printk(level, "sbridge", fmt, ##arg)
46
47#define sbridge_mc_printk(mci, level, fmt, arg...) \
48 edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
49
50/*
51 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
52 */
53#define GET_BITFIELD(v, lo, hi) \
Chen, Gong10ef6b02013-10-18 14:29:07 -070054 (((v) & GENMASK_ULL(hi, lo)) >> (lo))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020055
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020056/* Devices 12 Function 6, Offsets 0x80 to 0xcc */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -030057static const u32 sbridge_dram_rule[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020058 0x80, 0x88, 0x90, 0x98, 0xa0,
59 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
60};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020061
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030062static const u32 ibridge_dram_rule[] = {
63 0x60, 0x68, 0x70, 0x78, 0x80,
64 0x88, 0x90, 0x98, 0xa0, 0xa8,
65 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
66 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
67};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020068
Jim Snowd0cdf902015-12-03 10:48:54 +010069static const u32 knl_dram_rule[] = {
70 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
71 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
72 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
73 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
74 0x100, 0x108, 0x110, 0x118, /* 20-23 */
75};
76
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020077#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -030078#define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020079
Jim Snowc59f9c02015-12-03 10:48:52 +010080static char *show_dram_attr(u32 attr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020081{
Jim Snowc59f9c02015-12-03 10:48:52 +010082 switch (attr) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020083 case 0:
84 return "DRAM";
85 case 1:
86 return "MMCFG";
87 case 2:
88 return "NXM";
89 default:
90 return "unknown";
91 }
92}
93
Aristeu Rozanskief1ce512013-10-30 13:27:01 -030094static const u32 sbridge_interleave_list[] = {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020095 0x84, 0x8c, 0x94, 0x9c, 0xa4,
96 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
97};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -020098
Aristeu Rozanski4d715a82013-10-30 13:27:06 -030099static const u32 ibridge_interleave_list[] = {
100 0x64, 0x6c, 0x74, 0x7c, 0x84,
101 0x8c, 0x94, 0x9c, 0xa4, 0xac,
102 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
103 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
104};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200105
Jim Snowd0cdf902015-12-03 10:48:54 +0100106static const u32 knl_interleave_list[] = {
107 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
108 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
109 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
110 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
111 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
112};
113
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300114struct interleave_pkg {
115 unsigned char start;
116 unsigned char end;
117};
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200118
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300119static const struct interleave_pkg sbridge_interleave_pkg[] = {
120 { 0, 2 },
121 { 3, 5 },
122 { 8, 10 },
123 { 11, 13 },
124 { 16, 18 },
125 { 19, 21 },
126 { 24, 26 },
127 { 27, 29 },
128};
129
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300130static const struct interleave_pkg ibridge_interleave_pkg[] = {
131 { 0, 3 },
132 { 4, 7 },
133 { 8, 11 },
134 { 12, 15 },
135 { 16, 19 },
136 { 20, 23 },
137 { 24, 27 },
138 { 28, 31 },
139};
140
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300141static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
142 int interleave)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200143{
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300144 return GET_BITFIELD(reg, table[interleave].start,
145 table[interleave].end);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200146}
147
148/* Devices 12 Function 7 */
149
150#define TOLM 0x80
Jim Snowd0cdf902015-12-03 10:48:54 +0100151#define TOHM 0x84
Tony Luckf7cf2a22014-10-29 10:36:50 -0700152#define HASWELL_TOLM 0xd0
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300153#define HASWELL_TOHM_0 0xd4
154#define HASWELL_TOHM_1 0xd8
Jim Snowd0cdf902015-12-03 10:48:54 +0100155#define KNL_TOLM 0xd0
156#define KNL_TOHM_0 0xd4
157#define KNL_TOHM_1 0xd8
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200158
159#define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
160#define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
161
162/* Device 13 Function 6 */
163
164#define SAD_TARGET 0xf0
165
166#define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
167
Jim Snowd0cdf902015-12-03 10:48:54 +0100168#define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
169
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200170#define SAD_CONTROL 0xf4
171
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200172/* Device 14 function 0 */
173
174static const u32 tad_dram_rule[] = {
175 0x40, 0x44, 0x48, 0x4c,
176 0x50, 0x54, 0x58, 0x5c,
177 0x60, 0x64, 0x68, 0x6c,
178};
179#define MAX_TAD ARRAY_SIZE(tad_dram_rule)
180
181#define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
182#define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
183#define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
184#define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
185#define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
186#define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
187#define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
188
189/* Device 15, function 0 */
190
191#define MCMTR 0x7c
Jim Snowd0cdf902015-12-03 10:48:54 +0100192#define KNL_MCMTR 0x624
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200193
194#define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
195#define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
196#define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
197
198/* Device 15, function 1 */
199
200#define RASENABLES 0xac
201#define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
202
203/* Device 15, functions 2-5 */
204
205static const int mtr_regs[] = {
206 0x80, 0x84, 0x88,
207};
208
Jim Snowd0cdf902015-12-03 10:48:54 +0100209static const int knl_mtr_reg = 0xb60;
210
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200211#define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
212#define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
213#define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
214#define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
215#define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
216
217static const u32 tad_ch_nilv_offset[] = {
218 0x90, 0x94, 0x98, 0x9c,
219 0xa0, 0xa4, 0xa8, 0xac,
220 0xb0, 0xb4, 0xb8, 0xbc,
221};
222#define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
223#define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
224
225static const u32 rir_way_limit[] = {
226 0x108, 0x10c, 0x110, 0x114, 0x118,
227};
228#define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
229
230#define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
231#define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200232
233#define MAX_RIR_WAY 8
234
235static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
236 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
237 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
238 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
239 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
240 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
241};
242
Tony Luckc7103f62016-05-31 11:50:28 -0700243#define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
244 GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
245
246#define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
247 GET_BITFIELD(reg, 2, 15) : GET_BITFIELD(reg, 2, 14))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200248
249/* Device 16, functions 2-7 */
250
251/*
252 * FIXME: Implement the error count reads directly
253 */
254
255static const u32 correrrcnt[] = {
256 0x104, 0x108, 0x10c, 0x110,
257};
258
259#define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
260#define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
261#define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
262#define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
263
264static const u32 correrrthrsld[] = {
265 0x11c, 0x120, 0x124, 0x128,
266};
267
268#define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
269#define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
270
271
272/* Device 17, function 0 */
273
Aristeu Rozanskief1e8d02013-10-30 13:26:56 -0300274#define SB_RANK_CFG_A 0x0328
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200275
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300276#define IB_RANK_CFG_A 0x0320
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200277
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200278/*
279 * sbridge structs
280 */
281
Tony Luck7d375bf2015-05-18 17:50:42 -0300282#define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
Seth Jennings351fc4a2014-09-05 14:28:47 -0500283#define MAX_DIMMS 3 /* Max DIMMS per channel */
Jim Snowd0cdf902015-12-03 10:48:54 +0100284#define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
285#define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
286#define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
Seth Jennings351fc4a2014-09-05 14:28:47 -0500287#define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200288
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300289enum type {
290 SANDY_BRIDGE,
291 IVY_BRIDGE,
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300292 HASWELL,
Tony Luck1f395812014-12-02 09:27:30 -0800293 BROADWELL,
Jim Snowd0cdf902015-12-03 10:48:54 +0100294 KNIGHTS_LANDING,
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300295};
296
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800297enum domain {
298 IMC0 = 0,
299 IMC1,
300 SOCK,
301};
302
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300303struct sbridge_pvt;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200304struct sbridge_info {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300305 enum type type;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300306 u32 mcmtr;
307 u32 rankcfgr;
308 u64 (*get_tolm)(struct sbridge_pvt *pvt);
309 u64 (*get_tohm)(struct sbridge_pvt *pvt);
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300310 u64 (*rir_limit)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100311 u64 (*sad_limit)(u32 reg);
312 u32 (*interleave_mode)(u32 reg);
Jim Snowc59f9c02015-12-03 10:48:52 +0100313 u32 (*dram_attr)(u32 reg);
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300314 const u32 *dram_rule;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300315 const u32 *interleave_list;
Aristeu Rozanskicc311992013-10-30 13:27:02 -0300316 const struct interleave_pkg *interleave_pkg;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -0300317 u8 max_sad;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -0300318 u8 max_interleave;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300319 u8 (*get_node_id)(struct sbridge_pvt *pvt);
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300320 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400321 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300322 struct pci_dev *pci_vtd;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200323};
324
325struct sbridge_channel {
326 u32 ranks;
327 u32 dimms;
328};
329
330struct pci_id_descr {
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -0300331 int dev_id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200332 int optional;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800333 enum domain dom;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200334};
335
336struct pci_id_table {
337 const struct pci_id_descr *descr;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800338 int n_devs_per_imc;
339 int n_devs_per_sock;
340 int n_imcs_per_sock;
Tony Luck665f05e02016-06-02 10:58:08 -0700341 enum type type;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200342};
343
344struct sbridge_dev {
345 struct list_head list;
346 u8 bus, mc;
347 u8 node_id, source_id;
348 struct pci_dev **pdev;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800349 enum domain dom;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200350 int n_devs;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800351 int i_devs;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200352 struct mem_ctl_info *mci;
353};
354
Jim Snowd0cdf902015-12-03 10:48:54 +0100355struct knl_pvt {
356 struct pci_dev *pci_cha[KNL_MAX_CHAS];
357 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
358 struct pci_dev *pci_mc0;
359 struct pci_dev *pci_mc1;
360 struct pci_dev *pci_mc0_misc;
361 struct pci_dev *pci_mc1_misc;
362 struct pci_dev *pci_mc_info; /* tolm, tohm */
363};
364
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200365struct sbridge_pvt {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800366 /* Devices per socket */
367 struct pci_dev *pci_ddrio;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300368 struct pci_dev *pci_sad0, *pci_sad1;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300369 struct pci_dev *pci_br0, *pci_br1;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800370 /* Devices per memory controller */
371 struct pci_dev *pci_ha, *pci_ta, *pci_ras;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200372 struct pci_dev *pci_tad[NUM_CHANNELS];
373
374 struct sbridge_dev *sbridge_dev;
375
376 struct sbridge_info info;
377 struct sbridge_channel channel[NUM_CHANNELS];
378
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200379 /* Memory type detection */
380 bool is_mirrored, is_lockstep, is_close_pg;
Tony Luckea5dfb52016-04-14 10:22:02 -0700381 bool is_chan_hash;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200382
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200383 /* Memory description */
384 u64 tolm, tohm;
Jim Snowd0cdf902015-12-03 10:48:54 +0100385 struct knl_pvt knl;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200386};
387
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800388#define PCI_DESCR(device_id, opt, domain) \
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -0300389 .dev_id = (device_id), \
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800390 .optional = opt, \
391 .dom = domain
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200392
393static const struct pci_id_descr pci_dev_descr_sbridge[] = {
394 /* Processor Home Agent */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800395 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0, IMC0) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200396
397 /* Memory controller */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800398 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0, IMC0) },
399 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0, IMC0) },
400 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0, IMC0) },
401 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0, IMC0) },
402 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0, IMC0) },
403 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0, IMC0) },
404 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1, SOCK) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200405
406 /* System Address Decoder */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800407 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0, SOCK) },
408 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0, SOCK) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200409
410 /* Broadcast Registers */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800411 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0, SOCK) },
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200412};
413
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800414#define PCI_ID_TABLE_ENTRY(A, N, M, T) { \
Tony Luck665f05e02016-06-02 10:58:08 -0700415 .descr = A, \
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800416 .n_devs_per_imc = N, \
417 .n_devs_per_sock = ARRAY_SIZE(A), \
418 .n_imcs_per_sock = M, \
Tony Luck665f05e02016-06-02 10:58:08 -0700419 .type = T \
420}
421
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200422static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800423 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, ARRAY_SIZE(pci_dev_descr_sbridge), 1, SANDY_BRIDGE),
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200424 {0,} /* 0 terminated list. */
425};
426
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300427/* This changes depending if 1HA or 2HA:
428 * 1HA:
429 * 0x0eb8 (17.0) is DDRIO0
430 * 2HA:
431 * 0x0ebc (17.4) is DDRIO0
432 */
433#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
434#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
435
436/* pci ids */
437#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
438#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
439#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
440#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
441#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
442#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
443#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
444#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
445#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
446#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
447#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
448#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
449#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
450#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
451#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
Tony Luck7d375bf2015-05-18 17:50:42 -0300452#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
453#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300454
455static const struct pci_id_descr pci_dev_descr_ibridge[] = {
456 /* Processor Home Agent */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800457 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0, IMC0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300458
459 /* Memory controller */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800460 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0, IMC0) },
461 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0, IMC0) },
462 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0, IMC0) },
463 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0, IMC0) },
464 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0, IMC0) },
465 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0, IMC0) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300466
467 /* Optional, mode 2HA */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800468 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1, IMC1) },
469 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1, IMC1) },
470 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1, IMC1) },
471 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1, IMC1) },
472 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1, IMC1) },
473 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1, IMC1) },
474 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1, IMC1) },
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300475
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800476 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1, SOCK) },
477 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1, SOCK) },
478
479 /* System Address Decoder */
480 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0, SOCK) },
481
482 /* Broadcast Registers */
483 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1, SOCK) },
484 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0, SOCK) },
485
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300486};
487
488static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800489 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, 12, 2, IVY_BRIDGE),
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300490 {0,} /* 0 terminated list. */
491};
492
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300493/* Haswell support */
494/* EN processor:
495 * - 1 IMC
496 * - 3 DDR3 channels, 2 DPC per channel
497 * EP processor:
498 * - 1 or 2 IMC
499 * - 4 DDR4 channels, 3 DPC per channel
500 * EP 4S processor:
501 * - 2 IMC
502 * - 4 DDR4 channels, 3 DPC per channel
503 * EX processor:
504 * - 2 IMC
505 * - each IMC interfaces with a SMI 2 channel
506 * - each SMI channel interfaces with a scalable memory buffer
507 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
508 */
Tony Luck1f395812014-12-02 09:27:30 -0800509#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300510#define HASWELL_HASYSDEFEATURE2 0x84
511#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
512#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
513#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
514#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800515#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM 0x2f71
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300516#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800517#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM 0x2f79
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300518#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
519#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
520#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
521#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
522#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
523#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
524#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
525#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
526#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
527#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
528#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
Aristeu Rozanski71793852015-06-12 09:44:52 -0400529#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
530#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
531#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300532static const struct pci_id_descr pci_dev_descr_haswell[] = {
533 /* first item must be the HA */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800534 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0, IMC0) },
535 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1, IMC1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300536
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800537 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0, IMC0) },
538 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM, 0, IMC0) },
539 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0, IMC0) },
540 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0, IMC0) },
541 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1, IMC0) },
542 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1, IMC0) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300543
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800544 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1, IMC1) },
545 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM, 1, IMC1) },
546 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1, IMC1) },
547 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1, IMC1) },
548 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1, IMC1) },
549 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1, IMC1) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300550
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800551 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0, SOCK) },
552 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0, SOCK) },
553 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1, SOCK) },
554 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1, SOCK) },
555 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1, SOCK) },
556 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1, SOCK) },
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300557};
558
559static const struct pci_id_table pci_dev_descr_haswell_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800560 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, 13, 2, HASWELL),
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300561 {0,} /* 0 terminated list. */
562};
563
Jim Snowd0cdf902015-12-03 10:48:54 +0100564/* Knight's Landing Support */
565/*
566 * KNL's memory channels are swizzled between memory controllers.
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200567 * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
Jim Snowd0cdf902015-12-03 10:48:54 +0100568 */
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +0200569#define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
Jim Snowd0cdf902015-12-03 10:48:54 +0100570
571/* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
572#define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
573/* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800574#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN 0x7843
Jim Snowd0cdf902015-12-03 10:48:54 +0100575/* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
576#define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
577/* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
578#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
579/* SAD target - 1-29-1 (1 of these) */
580#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
581/* Caching / Home Agent */
582#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
583/* Device with TOLM and TOHM, 0-5-0 (1 of these) */
584#define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
585
586/*
587 * KNL differs from SB, IB, and Haswell in that it has multiple
588 * instances of the same device with the same device ID, so we handle that
589 * by creating as many copies in the table as we expect to find.
590 * (Like device ID must be grouped together.)
591 */
592
593static const struct pci_id_descr pci_dev_descr_knl[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800594 [0 ... 1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0, IMC0)},
595 [2 ... 7] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN, 0, IMC0) },
596 [8] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0, IMC0) },
597 [9] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0, IMC0) },
598 [10] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0, SOCK) },
599 [11] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0, SOCK) },
600 [12 ... 49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0, SOCK) },
Jim Snowd0cdf902015-12-03 10:48:54 +0100601};
602
603static const struct pci_id_table pci_dev_descr_knl_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800604 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, ARRAY_SIZE(pci_dev_descr_knl), 1, KNIGHTS_LANDING),
Jim Snowd0cdf902015-12-03 10:48:54 +0100605 {0,}
606};
607
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200608/*
Tony Luck1f395812014-12-02 09:27:30 -0800609 * Broadwell support
610 *
611 * DE processor:
612 * - 1 IMC
613 * - 2 DDR3 channels, 2 DPC per channel
Tony Luckfa2ce642015-05-20 19:10:35 -0300614 * EP processor:
615 * - 1 or 2 IMC
616 * - 4 DDR4 channels, 3 DPC per channel
617 * EP 4S processor:
618 * - 2 IMC
619 * - 4 DDR4 channels, 3 DPC per channel
620 * EX processor:
621 * - 2 IMC
622 * - each IMC interfaces with a SMI 2 channel
623 * - each SMI channel interfaces with a scalable memory buffer
624 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
Tony Luck1f395812014-12-02 09:27:30 -0800625 */
626#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
627#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
Tony Luckfa2ce642015-05-20 19:10:35 -0300628#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
Tony Luck1f395812014-12-02 09:27:30 -0800629#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800630#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM 0x6f71
Tony Luckfa2ce642015-05-20 19:10:35 -0300631#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800632#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM 0x6f79
Tony Luck1f395812014-12-02 09:27:30 -0800633#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
634#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
635#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
636#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
637#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
638#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
Tony Luckfa2ce642015-05-20 19:10:35 -0300639#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
640#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
641#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
642#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
Tony Luck1f395812014-12-02 09:27:30 -0800643#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
644
645static const struct pci_id_descr pci_dev_descr_broadwell[] = {
646 /* first item must be the HA */
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800647 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0, IMC0) },
648 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1, IMC1) },
Tony Luck1f395812014-12-02 09:27:30 -0800649
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800650 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0, IMC0) },
651 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM, 0, IMC0) },
652 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0, IMC0) },
653 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0, IMC0) },
654 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1, IMC0) },
655 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1, IMC0) },
Tony Luck1f395812014-12-02 09:27:30 -0800656
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800657 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1, IMC1) },
658 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM, 1, IMC1) },
659 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1, IMC1) },
660 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1, IMC1) },
661 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1, IMC1) },
662 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1, IMC1) },
Tony Luckfa2ce642015-05-20 19:10:35 -0300663
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800664 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0, SOCK) },
665 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0, SOCK) },
666 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1, SOCK) },
Tony Luck1f395812014-12-02 09:27:30 -0800667};
668
669static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800670 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, 10, 2, BROADWELL),
Tony Luck1f395812014-12-02 09:27:30 -0800671 {0,} /* 0 terminated list. */
672};
673
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200674
675/****************************************************************************
David Mackey15ed1032012-04-17 11:30:52 -0700676 Ancillary status routines
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200677 ****************************************************************************/
678
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300679static inline int numrank(enum type type, u32 mtr)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200680{
681 int ranks = (1 << RANK_CNT_BITS(mtr));
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300682 int max = 4;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200683
Jim Snowd0cdf902015-12-03 10:48:54 +0100684 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300685 max = 8;
686
687 if (ranks > max) {
688 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
689 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200690 return -EINVAL;
691 }
692
693 return ranks;
694}
695
696static inline int numrow(u32 mtr)
697{
698 int rows = (RANK_WIDTH_BITS(mtr) + 12);
699
700 if (rows < 13 || rows > 18) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300701 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
702 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200703 return -EINVAL;
704 }
705
706 return 1 << rows;
707}
708
709static inline int numcol(u32 mtr)
710{
711 int cols = (COL_WIDTH_BITS(mtr) + 10);
712
713 if (cols > 12) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300714 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
715 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200716 return -EINVAL;
717 }
718
719 return 1 << cols;
720}
721
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800722static struct sbridge_dev *get_sbridge_dev(u8 bus, enum domain dom, int multi_bus,
723 struct sbridge_dev *prev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200724{
725 struct sbridge_dev *sbridge_dev;
726
Jim Snowc1979ba2015-12-03 10:48:53 +0100727 /*
728 * If we have devices scattered across several busses that pertain
729 * to the same memory controller, we'll lump them all together.
730 */
731 if (multi_bus) {
732 return list_first_entry_or_null(&sbridge_edac_list,
733 struct sbridge_dev, list);
734 }
735
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800736 sbridge_dev = list_entry(prev ? prev->list.next
737 : sbridge_edac_list.next, struct sbridge_dev, list);
738
739 list_for_each_entry_from(sbridge_dev, &sbridge_edac_list, list) {
740 if (sbridge_dev->bus == bus && (dom == SOCK || dom == sbridge_dev->dom))
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200741 return sbridge_dev;
742 }
743
744 return NULL;
745}
746
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800747static struct sbridge_dev *alloc_sbridge_dev(u8 bus, enum domain dom,
748 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200749{
750 struct sbridge_dev *sbridge_dev;
751
752 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
753 if (!sbridge_dev)
754 return NULL;
755
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800756 sbridge_dev->pdev = kcalloc(table->n_devs_per_imc,
757 sizeof(*sbridge_dev->pdev),
758 GFP_KERNEL);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200759 if (!sbridge_dev->pdev) {
760 kfree(sbridge_dev);
761 return NULL;
762 }
763
764 sbridge_dev->bus = bus;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +0800765 sbridge_dev->dom = dom;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +0800766 sbridge_dev->n_devs = table->n_devs_per_imc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -0200767 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
768
769 return sbridge_dev;
770}
771
772static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
773{
774 list_del(&sbridge_dev->list);
775 kfree(sbridge_dev->pdev);
776 kfree(sbridge_dev);
777}
778
Aristeu Rozanskifb79a502013-10-30 13:26:57 -0300779static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
780{
781 u32 reg;
782
783 /* Address range is 32:28 */
784 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
785 return GET_TOLM(reg);
786}
787
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -0300788static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
789{
790 u32 reg;
791
792 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
793 return GET_TOHM(reg);
794}
795
Aristeu Rozanski4d715a82013-10-30 13:27:06 -0300796static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
797{
798 u32 reg;
799
800 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
801
802 return GET_TOLM(reg);
803}
804
805static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
806{
807 u32 reg;
808
809 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
810
811 return GET_TOHM(reg);
812}
813
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -0300814static u64 rir_limit(u32 reg)
815{
816 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
817}
818
Jim Snowc59f9c02015-12-03 10:48:52 +0100819static u64 sad_limit(u32 reg)
820{
821 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
822}
823
824static u32 interleave_mode(u32 reg)
825{
826 return GET_BITFIELD(reg, 1, 1);
827}
828
Jim Snowc59f9c02015-12-03 10:48:52 +0100829static u32 dram_attr(u32 reg)
830{
831 return GET_BITFIELD(reg, 2, 3);
832}
833
Jim Snowd0cdf902015-12-03 10:48:54 +0100834static u64 knl_sad_limit(u32 reg)
835{
836 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
837}
838
839static u32 knl_interleave_mode(u32 reg)
840{
841 return GET_BITFIELD(reg, 1, 2);
842}
843
Nicolas Iooss127c1222017-01-22 18:28:06 +0100844static const char * const knl_intlv_mode[] = {
845 "[8:6]", "[10:8]", "[14:12]", "[32:30]"
846};
847
848static const char *get_intlv_mode_str(u32 reg, enum type t)
Jim Snowd0cdf902015-12-03 10:48:54 +0100849{
Nicolas Iooss127c1222017-01-22 18:28:06 +0100850 if (t == KNIGHTS_LANDING)
851 return knl_intlv_mode[knl_interleave_mode(reg)];
852 else
853 return interleave_mode(reg) ? "[8:6]" : "[8:6]XOR[18:16]";
Jim Snowd0cdf902015-12-03 10:48:54 +0100854}
855
856static u32 dram_attr_knl(u32 reg)
857{
858 return GET_BITFIELD(reg, 3, 4);
859}
860
861
Aristeu Rozanski9e375442014-06-02 15:15:22 -0300862static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
863{
864 u32 reg;
865 enum mem_type mtype;
866
867 if (pvt->pci_ddrio) {
868 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
869 &reg);
870 if (GET_BITFIELD(reg, 11, 11))
871 /* FIXME: Can also be LRDIMM */
872 mtype = MEM_RDDR3;
873 else
874 mtype = MEM_DDR3;
875 } else
876 mtype = MEM_UNKNOWN;
877
878 return mtype;
879}
880
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300881static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
882{
883 u32 reg;
884 bool registered = false;
885 enum mem_type mtype = MEM_UNKNOWN;
886
887 if (!pvt->pci_ddrio)
888 goto out;
889
890 pci_read_config_dword(pvt->pci_ddrio,
891 HASWELL_DDRCRCLKCONTROLS, &reg);
892 /* Is_Rdimm */
893 if (GET_BITFIELD(reg, 16, 16))
894 registered = true;
895
896 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
897 if (GET_BITFIELD(reg, 14, 14)) {
898 if (registered)
899 mtype = MEM_RDDR4;
900 else
901 mtype = MEM_DDR4;
902 } else {
903 if (registered)
904 mtype = MEM_RDDR3;
905 else
906 mtype = MEM_DDR3;
907 }
908
909out:
910 return mtype;
911}
912
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +0100913static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
914{
915 /* for KNL value is fixed */
916 return DEV_X16;
917}
918
Aristeu Rozanski12f07212015-06-12 15:08:17 -0400919static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
920{
921 /* there's no way to figure out */
922 return DEV_UNKNOWN;
923}
924
925static enum dev_type __ibridge_get_width(u32 mtr)
926{
927 enum dev_type type;
928
929 switch (mtr) {
930 case 3:
931 type = DEV_UNKNOWN;
932 break;
933 case 2:
934 type = DEV_X16;
935 break;
936 case 1:
937 type = DEV_X8;
938 break;
939 case 0:
940 type = DEV_X4;
941 break;
942 }
943
944 return type;
945}
946
947static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
948{
949 /*
950 * ddr3_width on the documentation but also valid for DDR4 on
951 * Haswell
952 */
953 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
954}
955
956static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
957{
958 /* ddr3_width on the documentation but also valid for DDR4 */
959 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
960}
961
Jim Snowd0cdf902015-12-03 10:48:54 +0100962static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
963{
964 /* DDR4 RDIMMS and LRDIMMS are supported */
965 return MEM_RDDR4;
966}
967
Aristeu Rozanskif14d6892014-06-02 15:15:23 -0300968static u8 get_node_id(struct sbridge_pvt *pvt)
969{
970 u32 reg;
971 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
972 return GET_BITFIELD(reg, 0, 2);
973}
974
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300975static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
976{
977 u32 reg;
978
979 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
980 return GET_BITFIELD(reg, 0, 3);
981}
982
Jim Snowd0cdf902015-12-03 10:48:54 +0100983static u8 knl_get_node_id(struct sbridge_pvt *pvt)
984{
985 u32 reg;
986
987 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
988 return GET_BITFIELD(reg, 0, 2);
989}
990
991
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300992static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
993{
994 u32 reg;
995
Tony Luckf7cf2a22014-10-29 10:36:50 -0700996 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
997 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -0300998}
999
1000static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
1001{
1002 u64 rc;
1003 u32 reg;
1004
1005 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
1006 rc = GET_BITFIELD(reg, 26, 31);
1007 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
1008 rc = ((reg << 6) | rc) << 26;
1009
1010 return rc | 0x1ffffff;
1011}
1012
Jim Snowd0cdf902015-12-03 10:48:54 +01001013static u64 knl_get_tolm(struct sbridge_pvt *pvt)
1014{
1015 u32 reg;
1016
1017 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
1018 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1019}
1020
1021static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1022{
1023 u64 rc;
1024 u32 reg_lo, reg_hi;
1025
1026 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
1027 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
1028 rc = ((u64)reg_hi << 32) | reg_lo;
1029 return rc | 0x3ffffff;
1030}
1031
1032
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001033static u64 haswell_rir_limit(u32 reg)
1034{
1035 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1036}
1037
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001038static inline u8 sad_pkg_socket(u8 pkg)
1039{
1040 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
Aristeu Rozanski2ff3a302014-06-02 15:15:27 -03001041 return ((pkg >> 3) << 2) | (pkg & 0x3);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001042}
1043
1044static inline u8 sad_pkg_ha(u8 pkg)
1045{
1046 return (pkg >> 2) & 0x1;
1047}
1048
Tony Luckea5dfb52016-04-14 10:22:02 -07001049static int haswell_chan_hash(int idx, u64 addr)
1050{
1051 int i;
1052
1053 /*
1054 * XOR even bits from 12:26 to bit0 of idx,
1055 * odd bits from 13:27 to bit1
1056 */
1057 for (i = 12; i < 28; i += 2)
1058 idx ^= (addr >> i) & 3;
1059
1060 return idx;
1061}
1062
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001063/****************************************************************************
1064 Memory check routines
1065 ****************************************************************************/
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001066static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001067{
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001068 struct pci_dev *pdev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001069
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001070 do {
1071 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
1072 if (pdev && pdev->bus->number == bus)
1073 break;
1074 } while (pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001075
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001076 return pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001077}
1078
1079/**
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001080 * check_if_ecc_is_active() - Checks if ECC is active
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001081 * @bus: Device bus
1082 * @type: Memory controller type
1083 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
1084 * disabled
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001085 */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001086static int check_if_ecc_is_active(const u8 bus, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001087{
1088 struct pci_dev *pdev = NULL;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001089 u32 mcmtr, id;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001090
Tony Luck1f395812014-12-02 09:27:30 -08001091 switch (type) {
1092 case IVY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001093 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001094 break;
1095 case HASWELL:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001096 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001097 break;
1098 case SANDY_BRIDGE:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001099 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
Tony Luck1f395812014-12-02 09:27:30 -08001100 break;
1101 case BROADWELL:
1102 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
1103 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01001104 case KNIGHTS_LANDING:
1105 /*
1106 * KNL doesn't group things by bus the same way
1107 * SB/IB/Haswell does.
1108 */
1109 id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
1110 break;
Tony Luck1f395812014-12-02 09:27:30 -08001111 default:
1112 return -ENODEV;
1113 }
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001114
Jim Snowd0cdf902015-12-03 10:48:54 +01001115 if (type != KNIGHTS_LANDING)
1116 pdev = get_pdev_same_bus(bus, id);
1117 else
1118 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
1119
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001120 if (!pdev) {
1121 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03001122 "%04x:%04x! on bus %02d\n",
1123 PCI_VENDOR_ID_INTEL, id, bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001124 return -ENODEV;
1125 }
1126
Jim Snowd0cdf902015-12-03 10:48:54 +01001127 pci_read_config_dword(pdev,
1128 type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001129 if (!IS_ECC_ENABLED(mcmtr)) {
1130 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
1131 return -ENODEV;
1132 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001133 return 0;
1134}
1135
Jim Snowd0cdf902015-12-03 10:48:54 +01001136/* Low bits of TAD limit, and some metadata. */
1137static const u32 knl_tad_dram_limit_lo[] = {
1138 0x400, 0x500, 0x600, 0x700,
1139 0x800, 0x900, 0xa00, 0xb00,
1140};
1141
1142/* Low bits of TAD offset. */
1143static const u32 knl_tad_dram_offset_lo[] = {
1144 0x404, 0x504, 0x604, 0x704,
1145 0x804, 0x904, 0xa04, 0xb04,
1146};
1147
1148/* High 16 bits of TAD limit and offset. */
1149static const u32 knl_tad_dram_hi[] = {
1150 0x408, 0x508, 0x608, 0x708,
1151 0x808, 0x908, 0xa08, 0xb08,
1152};
1153
1154/* Number of ways a tad entry is interleaved. */
1155static const u32 knl_tad_ways[] = {
1156 8, 6, 4, 3, 2, 1,
1157};
1158
1159/*
1160 * Retrieve the n'th Target Address Decode table entry
1161 * from the memory controller's TAD table.
1162 *
1163 * @pvt: driver private data
1164 * @entry: which entry you want to retrieve
1165 * @mc: which memory controller (0 or 1)
1166 * @offset: output tad range offset
1167 * @limit: output address of first byte above tad range
1168 * @ways: output number of interleave ways
1169 *
1170 * The offset value has curious semantics. It's a sort of running total
1171 * of the sizes of all the memory regions that aren't mapped in this
1172 * tad table.
1173 */
1174static int knl_get_tad(const struct sbridge_pvt *pvt,
1175 const int entry,
1176 const int mc,
1177 u64 *offset,
1178 u64 *limit,
1179 int *ways)
1180{
1181 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1182 struct pci_dev *pci_mc;
1183 int way_id;
1184
1185 switch (mc) {
1186 case 0:
1187 pci_mc = pvt->knl.pci_mc0;
1188 break;
1189 case 1:
1190 pci_mc = pvt->knl.pci_mc1;
1191 break;
1192 default:
1193 WARN_ON(1);
1194 return -EINVAL;
1195 }
1196
1197 pci_read_config_dword(pci_mc,
1198 knl_tad_dram_limit_lo[entry], &reg_limit_lo);
1199 pci_read_config_dword(pci_mc,
1200 knl_tad_dram_offset_lo[entry], &reg_offset_lo);
1201 pci_read_config_dword(pci_mc,
1202 knl_tad_dram_hi[entry], &reg_hi);
1203
1204 /* Is this TAD entry enabled? */
1205 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1206 return -ENODEV;
1207
1208 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1209
1210 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1211 *ways = knl_tad_ways[way_id];
1212 } else {
1213 *ways = 0;
1214 sbridge_printk(KERN_ERR,
1215 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1216 way_id);
1217 return -ENODEV;
1218 }
1219
1220 /*
1221 * The least significant 6 bits of base and limit are truncated.
1222 * For limit, we fill the missing bits with 1s.
1223 */
1224 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1225 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1226 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1227 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1228
1229 return 0;
1230}
1231
1232/* Determine which memory controller is responsible for a given channel. */
1233static int knl_channel_mc(int channel)
1234{
1235 WARN_ON(channel < 0 || channel >= 6);
1236
1237 return channel < 3 ? 1 : 0;
1238}
1239
1240/*
1241 * Get the Nth entry from EDC_ROUTE_TABLE register.
1242 * (This is the per-tile mapping of logical interleave targets to
1243 * physical EDC modules.)
1244 *
1245 * entry 0: 0:2
1246 * 1: 3:5
1247 * 2: 6:8
1248 * 3: 9:11
1249 * 4: 12:14
1250 * 5: 15:17
1251 * 6: 18:20
1252 * 7: 21:23
1253 * reserved: 24:31
1254 */
1255static u32 knl_get_edc_route(int entry, u32 reg)
1256{
1257 WARN_ON(entry >= KNL_MAX_EDCS);
1258 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1259}
1260
1261/*
1262 * Get the Nth entry from MC_ROUTE_TABLE register.
1263 * (This is the per-tile mapping of logical interleave targets to
1264 * physical DRAM channels modules.)
1265 *
1266 * entry 0: mc 0:2 channel 18:19
1267 * 1: mc 3:5 channel 20:21
1268 * 2: mc 6:8 channel 22:23
1269 * 3: mc 9:11 channel 24:25
1270 * 4: mc 12:14 channel 26:27
1271 * 5: mc 15:17 channel 28:29
1272 * reserved: 30:31
1273 *
1274 * Though we have 3 bits to identify the MC, we should only see
1275 * the values 0 or 1.
1276 */
1277
1278static u32 knl_get_mc_route(int entry, u32 reg)
1279{
1280 int mc, chan;
1281
1282 WARN_ON(entry >= KNL_MAX_CHANNELS);
1283
1284 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1285 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1286
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02001287 return knl_channel_remap(mc, chan);
Jim Snowd0cdf902015-12-03 10:48:54 +01001288}
1289
1290/*
1291 * Render the EDC_ROUTE register in human-readable form.
1292 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1293 */
1294static void knl_show_edc_route(u32 reg, char *s)
1295{
1296 int i;
1297
1298 for (i = 0; i < KNL_MAX_EDCS; i++) {
1299 s[i*2] = knl_get_edc_route(i, reg) + '0';
1300 s[i*2+1] = '-';
1301 }
1302
1303 s[KNL_MAX_EDCS*2 - 1] = '\0';
1304}
1305
1306/*
1307 * Render the MC_ROUTE register in human-readable form.
1308 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1309 */
1310static void knl_show_mc_route(u32 reg, char *s)
1311{
1312 int i;
1313
1314 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1315 s[i*2] = knl_get_mc_route(i, reg) + '0';
1316 s[i*2+1] = '-';
1317 }
1318
1319 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1320}
1321
1322#define KNL_EDC_ROUTE 0xb8
1323#define KNL_MC_ROUTE 0xb4
1324
1325/* Is this dram rule backed by regular DRAM in flat mode? */
1326#define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1327
1328/* Is this dram rule cached? */
1329#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1330
1331/* Is this rule backed by edc ? */
1332#define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1333
1334/* Is this rule backed by DRAM, cacheable in EDRAM? */
1335#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1336
1337/* Is this rule mod3? */
1338#define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1339
1340/*
1341 * Figure out how big our RAM modules are.
1342 *
1343 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1344 * have to figure this out from the SAD rules, interleave lists, route tables,
1345 * and TAD rules.
1346 *
1347 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1348 * inspect the TAD rules to figure out how large the SAD regions really are.
1349 *
1350 * When we know the real size of a SAD region and how many ways it's
1351 * interleaved, we know the individual contribution of each channel to
1352 * TAD is size/ways.
1353 *
1354 * Finally, we have to check whether each channel participates in each SAD
1355 * region.
1356 *
1357 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1358 * much memory the channel uses, we know the DIMM is at least that large.
1359 * (The BIOS might possibly choose not to map all available memory, in which
1360 * case we will underreport the size of the DIMM.)
1361 *
1362 * In theory, we could try to determine the EDC sizes as well, but that would
1363 * only work in flat mode, not in cache mode.
1364 *
1365 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1366 * elements)
1367 */
1368static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1369{
1370 u64 sad_base, sad_size, sad_limit = 0;
1371 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1372 int sad_rule = 0;
1373 int tad_rule = 0;
1374 int intrlv_ways, tad_ways;
1375 u32 first_pkg, pkg;
1376 int i;
1377 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1378 u32 dram_rule, interleave_reg;
1379 u32 mc_route_reg[KNL_MAX_CHAS];
1380 u32 edc_route_reg[KNL_MAX_CHAS];
1381 int edram_only;
1382 char edc_route_string[KNL_MAX_EDCS*2];
1383 char mc_route_string[KNL_MAX_CHANNELS*2];
1384 int cur_reg_start;
1385 int mc;
1386 int channel;
1387 int way;
1388 int participants[KNL_MAX_CHANNELS];
1389 int participant_count = 0;
1390
1391 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1392 mc_sizes[i] = 0;
1393
1394 /* Read the EDC route table in each CHA. */
1395 cur_reg_start = 0;
1396 for (i = 0; i < KNL_MAX_CHAS; i++) {
1397 pci_read_config_dword(pvt->knl.pci_cha[i],
1398 KNL_EDC_ROUTE, &edc_route_reg[i]);
1399
1400 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1401 knl_show_edc_route(edc_route_reg[i-1],
1402 edc_route_string);
1403 if (cur_reg_start == i-1)
1404 edac_dbg(0, "edc route table for CHA %d: %s\n",
1405 cur_reg_start, edc_route_string);
1406 else
1407 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1408 cur_reg_start, i-1, edc_route_string);
1409 cur_reg_start = i;
1410 }
1411 }
1412 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1413 if (cur_reg_start == i-1)
1414 edac_dbg(0, "edc route table for CHA %d: %s\n",
1415 cur_reg_start, edc_route_string);
1416 else
1417 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1418 cur_reg_start, i-1, edc_route_string);
1419
1420 /* Read the MC route table in each CHA. */
1421 cur_reg_start = 0;
1422 for (i = 0; i < KNL_MAX_CHAS; i++) {
1423 pci_read_config_dword(pvt->knl.pci_cha[i],
1424 KNL_MC_ROUTE, &mc_route_reg[i]);
1425
1426 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1427 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1428 if (cur_reg_start == i-1)
1429 edac_dbg(0, "mc route table for CHA %d: %s\n",
1430 cur_reg_start, mc_route_string);
1431 else
1432 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1433 cur_reg_start, i-1, mc_route_string);
1434 cur_reg_start = i;
1435 }
1436 }
1437 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1438 if (cur_reg_start == i-1)
1439 edac_dbg(0, "mc route table for CHA %d: %s\n",
1440 cur_reg_start, mc_route_string);
1441 else
1442 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1443 cur_reg_start, i-1, mc_route_string);
1444
1445 /* Process DRAM rules */
1446 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1447 /* previous limit becomes the new base */
1448 sad_base = sad_limit;
1449
1450 pci_read_config_dword(pvt->pci_sad0,
1451 pvt->info.dram_rule[sad_rule], &dram_rule);
1452
1453 if (!DRAM_RULE_ENABLE(dram_rule))
1454 break;
1455
1456 edram_only = KNL_EDRAM_ONLY(dram_rule);
1457
1458 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1459 sad_size = sad_limit - sad_base;
1460
1461 pci_read_config_dword(pvt->pci_sad0,
1462 pvt->info.interleave_list[sad_rule], &interleave_reg);
1463
1464 /*
1465 * Find out how many ways this dram rule is interleaved.
1466 * We stop when we see the first channel again.
1467 */
1468 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1469 interleave_reg, 0);
1470 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1471 pkg = sad_pkg(pvt->info.interleave_pkg,
1472 interleave_reg, intrlv_ways);
1473
1474 if ((pkg & 0x8) == 0) {
1475 /*
1476 * 0 bit means memory is non-local,
1477 * which KNL doesn't support
1478 */
1479 edac_dbg(0, "Unexpected interleave target %d\n",
1480 pkg);
1481 return -1;
1482 }
1483
1484 if (pkg == first_pkg)
1485 break;
1486 }
1487 if (KNL_MOD3(dram_rule))
1488 intrlv_ways *= 3;
1489
1490 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1491 sad_rule,
1492 sad_base,
1493 sad_limit,
1494 intrlv_ways,
1495 edram_only ? ", EDRAM" : "");
1496
1497 /*
1498 * Find out how big the SAD region really is by iterating
1499 * over TAD tables (SAD regions may contain holes).
1500 * Each memory controller might have a different TAD table, so
1501 * we have to look at both.
1502 *
1503 * Livespace is the memory that's mapped in this TAD table,
1504 * deadspace is the holes (this could be the MMIO hole, or it
1505 * could be memory that's mapped by the other TAD table but
1506 * not this one).
1507 */
1508 for (mc = 0; mc < 2; mc++) {
1509 sad_actual_size[mc] = 0;
1510 tad_livespace = 0;
1511 for (tad_rule = 0;
1512 tad_rule < ARRAY_SIZE(
1513 knl_tad_dram_limit_lo);
1514 tad_rule++) {
1515 if (knl_get_tad(pvt,
1516 tad_rule,
1517 mc,
1518 &tad_deadspace,
1519 &tad_limit,
1520 &tad_ways))
1521 break;
1522
1523 tad_size = (tad_limit+1) -
1524 (tad_livespace + tad_deadspace);
1525 tad_livespace += tad_size;
1526 tad_base = (tad_limit+1) - tad_size;
1527
1528 if (tad_base < sad_base) {
1529 if (tad_limit > sad_base)
1530 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1531 } else if (tad_base < sad_limit) {
1532 if (tad_limit+1 > sad_limit) {
1533 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1534 } else {
1535 /* TAD region is completely inside SAD region */
1536 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1537 tad_rule, tad_base,
1538 tad_limit, tad_size,
1539 mc);
1540 sad_actual_size[mc] += tad_size;
1541 }
1542 }
1543 tad_base = tad_limit+1;
1544 }
1545 }
1546
1547 for (mc = 0; mc < 2; mc++) {
1548 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1549 mc, sad_actual_size[mc], sad_actual_size[mc]);
1550 }
1551
1552 /* Ignore EDRAM rule */
1553 if (edram_only)
1554 continue;
1555
1556 /* Figure out which channels participate in interleave. */
1557 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1558 participants[channel] = 0;
1559
1560 /* For each channel, does at least one CHA have
1561 * this channel mapped to the given target?
1562 */
1563 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1564 for (way = 0; way < intrlv_ways; way++) {
1565 int target;
1566 int cha;
1567
1568 if (KNL_MOD3(dram_rule))
1569 target = way;
1570 else
1571 target = 0x7 & sad_pkg(
1572 pvt->info.interleave_pkg, interleave_reg, way);
1573
1574 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1575 if (knl_get_mc_route(target,
1576 mc_route_reg[cha]) == channel
Hubert Chrzaniuk83bdaad2016-03-07 15:30:45 +01001577 && !participants[channel]) {
Jim Snowd0cdf902015-12-03 10:48:54 +01001578 participant_count++;
1579 participants[channel] = 1;
1580 break;
1581 }
1582 }
1583 }
1584 }
1585
1586 if (participant_count != intrlv_ways)
1587 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1588 participant_count, intrlv_ways);
1589
1590 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1591 mc = knl_channel_mc(channel);
1592 if (participants[channel]) {
1593 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1594 channel,
1595 sad_actual_size[mc]/intrlv_ways,
1596 sad_rule);
1597 mc_sizes[channel] +=
1598 sad_actual_size[mc]/intrlv_ways;
1599 }
1600 }
1601 }
1602
1603 return 0;
1604}
1605
Tony Luck7fd562b2017-05-23 08:06:03 +08001606static void get_source_id(struct mem_ctl_info *mci)
1607{
1608 struct sbridge_pvt *pvt = mci->pvt_info;
1609 u32 reg;
1610
1611 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1612 pvt->info.type == KNIGHTS_LANDING)
1613 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
1614 else
1615 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
1616
1617 if (pvt->info.type == KNIGHTS_LANDING)
1618 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1619 else
1620 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
1621}
1622
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001623static int get_dimm_config(struct mem_ctl_info *mci)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001624{
1625 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001626 struct dimm_info *dimm;
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001627 unsigned i, j, banks, ranks, rows, cols, npages;
1628 u64 size;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001629 u32 reg;
1630 enum edac_type mode;
Mark A. Grondonac6e13b52011-10-18 11:02:58 -02001631 enum mem_type mtype;
Jim Snowd0cdf902015-12-03 10:48:54 +01001632 int channels = pvt->info.type == KNIGHTS_LANDING ?
1633 KNL_MAX_CHANNELS : NUM_CHANNELS;
1634 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001635
Tony Luckea5dfb52016-04-14 10:22:02 -07001636 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001637 pci_read_config_dword(pvt->pci_ha, HASWELL_HASYSDEFEATURE2, &reg);
Tony Luckea5dfb52016-04-14 10:22:02 -07001638 pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
1639 }
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03001640 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
Joe Perches956b9ba2012-04-29 17:08:39 -03001641 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1642 pvt->sbridge_dev->mc,
1643 pvt->sbridge_dev->node_id,
1644 pvt->sbridge_dev->source_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001645
Jim Snowd0cdf902015-12-03 10:48:54 +01001646 /* KNL doesn't support mirroring or lockstep,
1647 * and is always closed page
1648 */
1649 if (pvt->info.type == KNIGHTS_LANDING) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001650 mode = EDAC_S4ECD4ED;
Jim Snowd0cdf902015-12-03 10:48:54 +01001651 pvt->is_mirrored = false;
1652
1653 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1654 return -1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001655 } else {
Jim Snowd0cdf902015-12-03 10:48:54 +01001656 pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
1657 if (IS_MIRROR_ENABLED(reg)) {
1658 edac_dbg(0, "Memory mirror is enabled\n");
1659 pvt->is_mirrored = true;
1660 } else {
1661 edac_dbg(0, "Memory mirror is disabled\n");
1662 pvt->is_mirrored = false;
1663 }
1664
1665 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
1666 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1667 edac_dbg(0, "Lockstep is enabled\n");
1668 mode = EDAC_S8ECD8ED;
1669 pvt->is_lockstep = true;
1670 } else {
1671 edac_dbg(0, "Lockstep is disabled\n");
1672 mode = EDAC_S4ECD4ED;
1673 pvt->is_lockstep = false;
1674 }
1675 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1676 edac_dbg(0, "address map is on closed page mode\n");
1677 pvt->is_close_pg = true;
1678 } else {
1679 edac_dbg(0, "address map is on open page mode\n");
1680 pvt->is_close_pg = false;
1681 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001682 }
1683
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001684 mtype = pvt->info.get_memory_type(pvt);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001685 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001686 edac_dbg(0, "Memory is registered\n");
1687 else if (mtype == MEM_UNKNOWN)
Luck, Tonyde4772c2013-03-28 09:59:15 -07001688 edac_dbg(0, "Cannot determine memory type\n");
Aristeu Rozanski9e375442014-06-02 15:15:22 -03001689 else
1690 edac_dbg(0, "Memory is unregistered\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001691
Tony Luckfec53af2014-12-02 09:41:58 -08001692 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001693 banks = 16;
1694 else
1695 banks = 8;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001696
Jim Snowd0cdf902015-12-03 10:48:54 +01001697 for (i = 0; i < channels; i++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001698 u32 mtr;
1699
Jim Snowd0cdf902015-12-03 10:48:54 +01001700 int max_dimms_per_channel;
1701
1702 if (pvt->info.type == KNIGHTS_LANDING) {
1703 max_dimms_per_channel = 1;
1704 if (!pvt->knl.pci_channel[i])
1705 continue;
1706 } else {
1707 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1708 if (!pvt->pci_tad[i])
1709 continue;
1710 }
1711
1712 for (j = 0; j < max_dimms_per_channel; j++) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03001713 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1714 i, j, 0);
Jim Snowd0cdf902015-12-03 10:48:54 +01001715 if (pvt->info.type == KNIGHTS_LANDING) {
1716 pci_read_config_dword(pvt->knl.pci_channel[i],
1717 knl_mtr_reg, &mtr);
1718 } else {
1719 pci_read_config_dword(pvt->pci_tad[i],
1720 mtr_regs[j], &mtr);
1721 }
Joe Perches956b9ba2012-04-29 17:08:39 -03001722 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001723 if (IS_DIMM_PRESENT(mtr)) {
1724 pvt->channel[i].dimms++;
1725
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001726 ranks = numrank(pvt->info.type, mtr);
Jim Snowd0cdf902015-12-03 10:48:54 +01001727
1728 if (pvt->info.type == KNIGHTS_LANDING) {
1729 /* For DDR4, this is fixed. */
1730 cols = 1 << 10;
1731 rows = knl_mc_sizes[i] /
1732 ((u64) cols * ranks * banks * 8);
1733 } else {
1734 rows = numrow(mtr);
1735 cols = numcol(mtr);
1736 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001737
Mauro Carvalho Chehabdeb09dd2012-09-20 12:09:30 -03001738 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001739 npages = MiB_TO_PAGES(size);
1740
Tony Luck7d375bf2015-05-18 17:50:42 -03001741 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001742 pvt->sbridge_dev->mc, pvt->sbridge_dev->dom, i, j,
Joe Perches956b9ba2012-04-29 17:08:39 -03001743 size, npages,
1744 banks, ranks, rows, cols);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001745
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -03001746 dimm->nr_pages = npages;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001747 dimm->grain = 32;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04001748 dimm->dtype = pvt->info.get_width(pvt, mtr);
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -03001749 dimm->mtype = mtype;
1750 dimm->edac_mode = mode;
1751 snprintf(dimm->label, sizeof(dimm->label),
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001752 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1753 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom, i, j);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001754 }
1755 }
1756 }
1757
1758 return 0;
1759}
1760
1761static void get_memory_layout(const struct mem_ctl_info *mci)
1762{
1763 struct sbridge_pvt *pvt = mci->pvt_info;
1764 int i, j, k, n_sads, n_tads, sad_interl;
1765 u32 reg;
1766 u64 limit, prv = 0;
1767 u64 tmp_mb;
Jim Snow8c009102014-11-18 14:51:09 +01001768 u32 gb, mb;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001769 u32 rir_way;
1770
1771 /*
1772 * Step 1) Get TOLM/TOHM ranges
1773 */
1774
Aristeu Rozanskifb79a502013-10-30 13:26:57 -03001775 pvt->tolm = pvt->info.get_tolm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001776 tmp_mb = (1 + pvt->tolm) >> 20;
1777
Jim Snow8c009102014-11-18 14:51:09 +01001778 gb = div_u64_rem(tmp_mb, 1024, &mb);
1779 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1780 gb, (mb*1000)/1024, (u64)pvt->tolm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001781
1782 /* Address range is already 45:25 */
Aristeu Rozanski8fd6a432013-10-30 13:26:59 -03001783 pvt->tohm = pvt->info.get_tohm(pvt);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001784 tmp_mb = (1 + pvt->tohm) >> 20;
1785
Jim Snow8c009102014-11-18 14:51:09 +01001786 gb = div_u64_rem(tmp_mb, 1024, &mb);
1787 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1788 gb, (mb*1000)/1024, (u64)pvt->tohm);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001789
1790 /*
1791 * Step 2) Get SAD range and SAD Interleave list
1792 * TAD registers contain the interleave wayness. However, it
1793 * seems simpler to just discover it indirectly, with the
1794 * algorithm bellow.
1795 */
1796 prv = 0;
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001797 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001798 /* SAD_LIMIT Address range is 45:26 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001799 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001800 &reg);
Jim Snowc59f9c02015-12-03 10:48:52 +01001801 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001802
1803 if (!DRAM_RULE_ENABLE(reg))
1804 continue;
1805
1806 if (limit <= prv)
1807 break;
1808
1809 tmp_mb = (limit + 1) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001810 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001811 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1812 n_sads,
Jim Snowc59f9c02015-12-03 10:48:52 +01001813 show_dram_attr(pvt->info.dram_attr(reg)),
Jim Snow8c009102014-11-18 14:51:09 +01001814 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001815 ((u64)tmp_mb) << 20L,
Nicolas Iooss127c1222017-01-22 18:28:06 +01001816 get_intlv_mode_str(reg, pvt->info.type),
Joe Perches956b9ba2012-04-29 17:08:39 -03001817 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001818 prv = limit;
1819
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001820 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001821 &reg);
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001822 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001823 for (j = 0; j < 8; j++) {
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001824 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1825 if (j > 0 && sad_interl == pkg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001826 break;
1827
Joe Perches956b9ba2012-04-29 17:08:39 -03001828 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
Aristeu Rozanskicc311992013-10-30 13:27:02 -03001829 n_sads, j, pkg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001830 }
1831 }
1832
Jim Snowd0cdf902015-12-03 10:48:54 +01001833 if (pvt->info.type == KNIGHTS_LANDING)
1834 return;
1835
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001836 /*
1837 * Step 3) Get TAD range
1838 */
1839 prv = 0;
1840 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001841 pci_read_config_dword(pvt->pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001842 limit = TAD_LIMIT(reg);
1843 if (limit <= prv)
1844 break;
1845 tmp_mb = (limit + 1) >> 20;
1846
Jim Snow8c009102014-11-18 14:51:09 +01001847 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001848 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 +01001849 n_tads, gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001850 ((u64)tmp_mb) << 20L,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08001851 (u32)(1 << TAD_SOCK(reg)),
1852 (u32)TAD_CH(reg) + 1,
Joe Perches956b9ba2012-04-29 17:08:39 -03001853 (u32)TAD_TGT0(reg),
1854 (u32)TAD_TGT1(reg),
1855 (u32)TAD_TGT2(reg),
1856 (u32)TAD_TGT3(reg),
1857 reg);
Hui Wang7fae0db2012-02-06 04:11:01 -03001858 prv = limit;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001859 }
1860
1861 /*
1862 * Step 4) Get TAD offsets, per each channel
1863 */
1864 for (i = 0; i < NUM_CHANNELS; i++) {
1865 if (!pvt->channel[i].dimms)
1866 continue;
1867 for (j = 0; j < n_tads; j++) {
1868 pci_read_config_dword(pvt->pci_tad[i],
1869 tad_ch_nilv_offset[j],
1870 &reg);
1871 tmp_mb = TAD_OFFSET(reg) >> 20;
Jim Snow8c009102014-11-18 14:51:09 +01001872 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001873 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1874 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001875 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001876 ((u64)tmp_mb) << 20L,
1877 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001878 }
1879 }
1880
1881 /*
1882 * Step 6) Get RIR Wayness/Limit, per each channel
1883 */
1884 for (i = 0; i < NUM_CHANNELS; i++) {
1885 if (!pvt->channel[i].dimms)
1886 continue;
1887 for (j = 0; j < MAX_RIR_RANGES; j++) {
1888 pci_read_config_dword(pvt->pci_tad[i],
1889 rir_way_limit[j],
1890 &reg);
1891
1892 if (!IS_RIR_VALID(reg))
1893 continue;
1894
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03001895 tmp_mb = pvt->info.rir_limit(reg) >> 20;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001896 rir_way = 1 << RIR_WAY(reg);
Jim Snow8c009102014-11-18 14:51:09 +01001897 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001898 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1899 i, j,
Jim Snow8c009102014-11-18 14:51:09 +01001900 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001901 ((u64)tmp_mb) << 20L,
1902 rir_way,
1903 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001904
1905 for (k = 0; k < rir_way; k++) {
1906 pci_read_config_dword(pvt->pci_tad[i],
1907 rir_offset[j][k],
1908 &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07001909 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001910
Jim Snow8c009102014-11-18 14:51:09 +01001911 gb = div_u64_rem(tmp_mb, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03001912 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1913 i, j, k,
Jim Snow8c009102014-11-18 14:51:09 +01001914 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03001915 ((u64)tmp_mb) << 20L,
Tony Luckc7103f62016-05-31 11:50:28 -07001916 (u32)RIR_RNK_TGT(pvt->info.type, reg),
Joe Perches956b9ba2012-04-29 17:08:39 -03001917 reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001918 }
1919 }
1920 }
1921}
1922
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001923static struct mem_ctl_info *get_mci_for_node_id(u8 node_id, u8 ha)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001924{
1925 struct sbridge_dev *sbridge_dev;
1926
1927 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001928 if (sbridge_dev->node_id == node_id && sbridge_dev->dom == ha)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001929 return sbridge_dev->mci;
1930 }
1931 return NULL;
1932}
1933
1934static int get_memory_error_data(struct mem_ctl_info *mci,
1935 u64 addr,
Tony Luck7d375bf2015-05-18 17:50:42 -03001936 u8 *socket, u8 *ha,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001937 long *channel_mask,
1938 u8 *rank,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03001939 char **area_type, char *msg)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001940{
1941 struct mem_ctl_info *new_mci;
1942 struct sbridge_pvt *pvt = mci->pvt_info;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03001943 struct pci_dev *pci_ha;
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03001944 int n_rir, n_sads, n_tads, sad_way, sck_xch;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001945 int sad_interl, idx, base_ch;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001946 int interleave_mode, shiftup = 0;
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001947 unsigned sad_interleave[pvt->info.max_interleave];
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001948 u32 reg, dram_rule;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08001949 u8 ch_way, sck_way, pkg, sad_ha = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001950 u32 tad_offset;
1951 u32 rir_way;
Jim Snow8c009102014-11-18 14:51:09 +01001952 u32 mb, gb;
Aristeu Rozanskibd4b9682013-11-21 09:08:03 -05001953 u64 ch_addr, offset, limit = 0, prv = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001954
1955
1956 /*
1957 * Step 0) Check if the address is at special memory ranges
1958 * The check bellow is probably enough to fill all cases where
1959 * the error is not inside a memory, except for the legacy
1960 * range (e. g. VGA addresses). It is unlikely, however, that the
1961 * memory controller would generate an error on that range.
1962 */
Mauro Carvalho Chehab5b889e32011-11-07 18:26:53 -03001963 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001964 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001965 return -EINVAL;
1966 }
1967 if (addr >= (u64)pvt->tohm) {
1968 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001969 return -EINVAL;
1970 }
1971
1972 /*
1973 * Step 1) Get socket
1974 */
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001975 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1976 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001977 &reg);
1978
1979 if (!DRAM_RULE_ENABLE(reg))
1980 continue;
1981
Jim Snowc59f9c02015-12-03 10:48:52 +01001982 limit = pvt->info.sad_limit(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001983 if (limit <= prv) {
1984 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001985 return -EINVAL;
1986 }
1987 if (addr <= limit)
1988 break;
1989 prv = limit;
1990 }
Aristeu Rozanski464f1d82013-10-30 13:27:00 -03001991 if (n_sads == pvt->info.max_sad) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001992 sprintf(msg, "Can't discover the memory socket");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001993 return -EINVAL;
1994 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03001995 dram_rule = reg;
Jim Snowc59f9c02015-12-03 10:48:52 +01001996 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1997 interleave_mode = pvt->info.interleave_mode(dram_rule);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02001998
Aristeu Rozanskief1ce512013-10-30 13:27:01 -03001999 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002000 &reg);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002001
2002 if (pvt->info.type == SANDY_BRIDGE) {
2003 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
2004 for (sad_way = 0; sad_way < 8; sad_way++) {
2005 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
2006 if (sad_way > 0 && sad_interl == pkg)
2007 break;
2008 sad_interleave[sad_way] = pkg;
2009 edac_dbg(0, "SAD interleave #%d: %d\n",
2010 sad_way, sad_interleave[sad_way]);
2011 }
2012 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
2013 pvt->sbridge_dev->mc,
2014 n_sads,
2015 addr,
2016 limit,
2017 sad_way + 7,
2018 !interleave_mode ? "" : "XOR[18:16]");
2019 if (interleave_mode)
2020 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
2021 else
2022 idx = (addr >> 6) & 7;
2023 switch (sad_way) {
2024 case 1:
2025 idx = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002026 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002027 case 2:
2028 idx = idx & 1;
2029 break;
2030 case 4:
2031 idx = idx & 3;
2032 break;
2033 case 8:
2034 break;
2035 default:
2036 sprintf(msg, "Can't discover socket interleave");
2037 return -EINVAL;
2038 }
2039 *socket = sad_interleave[idx];
2040 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2041 idx, sad_way, *socket);
Tony Luck1f395812014-12-02 09:27:30 -08002042 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002043 int bits, a7mode = A7MODE(dram_rule);
2044
2045 if (a7mode) {
2046 /* A7 mode swaps P9 with P6 */
2047 bits = GET_BITFIELD(addr, 7, 8) << 1;
2048 bits |= GET_BITFIELD(addr, 9, 9);
2049 } else
Tony Luckbb89e712015-05-18 17:39:06 -03002050 bits = GET_BITFIELD(addr, 6, 8);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002051
Tony Luckbb89e712015-05-18 17:39:06 -03002052 if (interleave_mode == 0) {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002053 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2054 idx = GET_BITFIELD(addr, 16, 18);
2055 idx ^= bits;
2056 } else
2057 idx = bits;
2058
2059 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2060 *socket = sad_pkg_socket(pkg);
2061 sad_ha = sad_pkg_ha(pkg);
2062
2063 if (a7mode) {
2064 /* MCChanShiftUpEnable */
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002065 pci_read_config_dword(pvt->pci_ha, HASWELL_HASYSDEFEATURE2, &reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002066 shiftup = GET_BITFIELD(reg, 22, 22);
2067 }
2068
2069 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2070 idx, *socket, sad_ha, shiftup);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002071 } else {
2072 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002073 idx = (addr >> 6) & 7;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002074 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2075 *socket = sad_pkg_socket(pkg);
2076 sad_ha = sad_pkg_ha(pkg);
2077 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2078 idx, *socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002079 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002080
Tony Luck7d375bf2015-05-18 17:50:42 -03002081 *ha = sad_ha;
2082
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002083 /*
2084 * Move to the proper node structure, in order to access the
2085 * right PCI registers
2086 */
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002087 new_mci = get_mci_for_node_id(*socket, sad_ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002088 if (!new_mci) {
2089 sprintf(msg, "Struct for socket #%u wasn't initialized",
2090 *socket);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002091 return -EINVAL;
2092 }
2093 mci = new_mci;
2094 pvt = mci->pvt_info;
2095
2096 /*
2097 * Step 2) Get memory channel
2098 */
2099 prv = 0;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002100 pci_ha = pvt->pci_ha;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002101 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002102 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002103 limit = TAD_LIMIT(reg);
2104 if (limit <= prv) {
2105 sprintf(msg, "Can't discover the memory channel");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002106 return -EINVAL;
2107 }
2108 if (addr <= limit)
2109 break;
2110 prv = limit;
2111 }
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002112 if (n_tads == MAX_TAD) {
2113 sprintf(msg, "Can't discover the memory channel");
2114 return -EINVAL;
2115 }
2116
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002117 ch_way = TAD_CH(reg) + 1;
Tony Luckff15e952016-04-14 10:21:52 -07002118 sck_way = TAD_SOCK(reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002119
2120 if (ch_way == 3)
2121 idx = addr >> 6;
Tony Luckea5dfb52016-04-14 10:22:02 -07002122 else {
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002123 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
Tony Luckea5dfb52016-04-14 10:22:02 -07002124 if (pvt->is_chan_hash)
2125 idx = haswell_chan_hash(idx, addr);
2126 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002127 idx = idx % ch_way;
2128
2129 /*
2130 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2131 */
2132 switch (idx) {
2133 case 0:
2134 base_ch = TAD_TGT0(reg);
2135 break;
2136 case 1:
2137 base_ch = TAD_TGT1(reg);
2138 break;
2139 case 2:
2140 base_ch = TAD_TGT2(reg);
2141 break;
2142 case 3:
2143 base_ch = TAD_TGT3(reg);
2144 break;
2145 default:
2146 sprintf(msg, "Can't discover the TAD target");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002147 return -EINVAL;
2148 }
2149 *channel_mask = 1 << base_ch;
2150
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002151 pci_read_config_dword(pvt->pci_tad[base_ch], tad_ch_nilv_offset[n_tads], &tad_offset);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002152
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002153 if (pvt->is_mirrored) {
2154 *channel_mask |= 1 << ((base_ch + 2) % 4);
2155 switch(ch_way) {
2156 case 2:
2157 case 4:
Tony Luckff15e952016-04-14 10:21:52 -07002158 sck_xch = (1 << sck_way) * (ch_way >> 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002159 break;
2160 default:
2161 sprintf(msg, "Invalid mirror set. Can't decode addr");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002162 return -EINVAL;
2163 }
2164 } else
2165 sck_xch = (1 << sck_way) * ch_way;
2166
2167 if (pvt->is_lockstep)
2168 *channel_mask |= 1 << ((base_ch + 1) % 4);
2169
2170 offset = TAD_OFFSET(tad_offset);
2171
Joe Perches956b9ba2012-04-29 17:08:39 -03002172 edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
2173 n_tads,
2174 addr,
2175 limit,
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002176 sck_way,
Joe Perches956b9ba2012-04-29 17:08:39 -03002177 ch_way,
2178 offset,
2179 idx,
2180 base_ch,
2181 *channel_mask);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002182
2183 /* Calculate channel address */
2184 /* Remove the TAD offset */
2185
2186 if (offset > addr) {
2187 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2188 offset, addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002189 return -EINVAL;
2190 }
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002191
2192 ch_addr = addr - offset;
2193 ch_addr >>= (6 + shiftup);
Tony Luckff15e952016-04-14 10:21:52 -07002194 ch_addr /= sck_xch;
Luck, Tonyeb1af3b2016-03-09 16:40:48 -08002195 ch_addr <<= (6 + shiftup);
2196 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002197
2198 /*
2199 * Step 3) Decode rank
2200 */
2201 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002202 pci_read_config_dword(pvt->pci_tad[base_ch], rir_way_limit[n_rir], &reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002203
2204 if (!IS_RIR_VALID(reg))
2205 continue;
2206
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03002207 limit = pvt->info.rir_limit(reg);
Jim Snow8c009102014-11-18 14:51:09 +01002208 gb = div_u64_rem(limit >> 20, 1024, &mb);
Joe Perches956b9ba2012-04-29 17:08:39 -03002209 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2210 n_rir,
Jim Snow8c009102014-11-18 14:51:09 +01002211 gb, (mb*1000)/1024,
Joe Perches956b9ba2012-04-29 17:08:39 -03002212 limit,
2213 1 << RIR_WAY(reg));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002214 if (ch_addr <= limit)
2215 break;
2216 }
2217 if (n_rir == MAX_RIR_RANGES) {
2218 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2219 ch_addr);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002220 return -EINVAL;
2221 }
2222 rir_way = RIR_WAY(reg);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002223
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002224 if (pvt->is_close_pg)
2225 idx = (ch_addr >> 6);
2226 else
2227 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2228 idx %= 1 << rir_way;
2229
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002230 pci_read_config_dword(pvt->pci_tad[base_ch], rir_offset[n_rir][idx], &reg);
Tony Luckc7103f62016-05-31 11:50:28 -07002231 *rank = RIR_RNK_TGT(pvt->info.type, reg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002232
Joe Perches956b9ba2012-04-29 17:08:39 -03002233 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2234 n_rir,
2235 ch_addr,
2236 limit,
2237 rir_way,
2238 idx);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002239
2240 return 0;
2241}
2242
2243/****************************************************************************
2244 Device initialization routines: put/get, init/exit
2245 ****************************************************************************/
2246
2247/*
2248 * sbridge_put_all_devices 'put' all the devices that we have
2249 * reserved via 'get'
2250 */
2251static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2252{
2253 int i;
2254
Joe Perches956b9ba2012-04-29 17:08:39 -03002255 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002256 for (i = 0; i < sbridge_dev->n_devs; i++) {
2257 struct pci_dev *pdev = sbridge_dev->pdev[i];
2258 if (!pdev)
2259 continue;
Joe Perches956b9ba2012-04-29 17:08:39 -03002260 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2261 pdev->bus->number,
2262 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002263 pci_dev_put(pdev);
2264 }
2265}
2266
2267static void sbridge_put_all_devices(void)
2268{
2269 struct sbridge_dev *sbridge_dev, *tmp;
2270
2271 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2272 sbridge_put_devices(sbridge_dev);
2273 free_sbridge_dev(sbridge_dev);
2274 }
2275}
2276
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002277static int sbridge_get_onedevice(struct pci_dev **prev,
2278 u8 *num_mc,
2279 const struct pci_id_table *table,
Jim Snowc1979ba2015-12-03 10:48:53 +01002280 const unsigned devno,
2281 const int multi_bus)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002282{
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002283 struct sbridge_dev *sbridge_dev = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002284 const struct pci_id_descr *dev_descr = &table->descr[devno];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002285 struct pci_dev *pdev = NULL;
2286 u8 bus = 0;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002287 int i = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002288
Jiang Liuec5a0b32014-02-17 13:10:23 +08002289 sbridge_printk(KERN_DEBUG,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002290 "Seeking for: PCI ID %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002291 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2292
2293 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2294 dev_descr->dev_id, *prev);
2295
2296 if (!pdev) {
2297 if (*prev) {
2298 *prev = pdev;
2299 return 0;
2300 }
2301
2302 if (dev_descr->optional)
2303 return 0;
2304
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002305 /* if the HA wasn't found */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002306 if (devno == 0)
2307 return -ENODEV;
2308
2309 sbridge_printk(KERN_INFO,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002310 "Device not found: %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002311 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2312
2313 /* End of list, leave */
2314 return -ENODEV;
2315 }
2316 bus = pdev->bus->number;
2317
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002318next_imc:
2319 sbridge_dev = get_sbridge_dev(bus, dev_descr->dom, multi_bus, sbridge_dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002320 if (!sbridge_dev) {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002321 sbridge_dev = alloc_sbridge_dev(bus, dev_descr->dom, table);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002322 if (!sbridge_dev) {
2323 pci_dev_put(pdev);
2324 return -ENOMEM;
2325 }
2326 (*num_mc)++;
2327 }
2328
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002329 if (sbridge_dev->pdev[sbridge_dev->i_devs]) {
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002330 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002331 "Duplicated device for %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002332 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2333 pci_dev_put(pdev);
2334 return -ENODEV;
2335 }
2336
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002337 sbridge_dev->pdev[sbridge_dev->i_devs++] = pdev;
2338
2339 /* pdev belongs to more than one IMC, do extra gets */
2340 if (++i > 1)
2341 pci_dev_get(pdev);
2342
2343 if (dev_descr->dom == SOCK && i < table->n_imcs_per_sock)
2344 goto next_imc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002345
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002346 /* Be sure that the device is enabled */
2347 if (unlikely(pci_enable_device(pdev) < 0)) {
2348 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002349 "Couldn't enable %04x:%04x\n",
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002350 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2351 return -ENODEV;
2352 }
2353
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002354 edac_dbg(0, "Detected %04x:%04x\n",
Joe Perches956b9ba2012-04-29 17:08:39 -03002355 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002356
2357 /*
2358 * As stated on drivers/pci/search.c, the reference count for
2359 * @from is always decremented if it is not %NULL. So, as we need
2360 * to get all devices up to null, we need to do a get for the device
2361 */
2362 pci_dev_get(pdev);
2363
2364 *prev = pdev;
2365
2366 return 0;
2367}
2368
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002369/*
2370 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002371 * devices we want to reference for this driver.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002372 * @num_mc: pointer to the memory controllers count, to be incremented in case
Mauro Carvalho Chehabc41afdc2014-06-26 15:35:14 -03002373 * of success.
Aristeu Rozanski5153a0f2013-10-30 13:27:03 -03002374 * @table: model specific table
2375 *
2376 * returns 0 in case of success or error code
2377 */
Tony Luck0ba169ac2016-07-14 15:38:43 -07002378static int sbridge_get_all_devices(u8 *num_mc,
2379 const struct pci_id_table *table)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002380{
2381 int i, rc;
2382 struct pci_dev *pdev = NULL;
Tony Luck0ba169ac2016-07-14 15:38:43 -07002383 int allow_dups = 0;
2384 int multi_bus = 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002385
Tony Luck0ba169ac2016-07-14 15:38:43 -07002386 if (table->type == KNIGHTS_LANDING)
2387 allow_dups = multi_bus = 1;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002388 while (table && table->descr) {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002389 for (i = 0; i < table->n_devs_per_sock; i++) {
Jim Snowc1979ba2015-12-03 10:48:53 +01002390 if (!allow_dups || i == 0 ||
2391 table->descr[i].dev_id !=
2392 table->descr[i-1].dev_id) {
2393 pdev = NULL;
2394 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002395 do {
2396 rc = sbridge_get_onedevice(&pdev, num_mc,
Jim Snowc1979ba2015-12-03 10:48:53 +01002397 table, i, multi_bus);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002398 if (rc < 0) {
2399 if (i == 0) {
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002400 i = table->n_devs_per_sock;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002401 break;
2402 }
2403 sbridge_put_all_devices();
2404 return -ENODEV;
2405 }
Jim Snowc1979ba2015-12-03 10:48:53 +01002406 } while (pdev && !allow_dups);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002407 }
2408 table++;
2409 }
2410
2411 return 0;
2412}
2413
Aristeu Rozanskiea779b52013-10-30 13:27:04 -03002414static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2415 struct sbridge_dev *sbridge_dev)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002416{
2417 struct sbridge_pvt *pvt = mci->pvt_info;
2418 struct pci_dev *pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002419 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002420 int i;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002421
2422 for (i = 0; i < sbridge_dev->n_devs; i++) {
2423 pdev = sbridge_dev->pdev[i];
2424 if (!pdev)
2425 continue;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002426
2427 switch (pdev->device) {
2428 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2429 pvt->pci_sad0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002430 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002431 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2432 pvt->pci_sad1 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002433 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002434 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2435 pvt->pci_br0 = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002436 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002437 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002438 pvt->pci_ha = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002439 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002440 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2441 pvt->pci_ta = pdev;
2442 break;
2443 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2444 pvt->pci_ras = pdev;
2445 break;
2446 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2447 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2448 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2449 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2450 {
2451 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
2452 pvt->pci_tad[id] = pdev;
Seth Jennings2900ea62015-08-05 13:16:01 -05002453 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002454 }
2455 break;
2456 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2457 pvt->pci_ddrio = pdev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002458 break;
2459 default:
2460 goto error;
2461 }
2462
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002463 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2464 pdev->vendor, pdev->device,
Joe Perches956b9ba2012-04-29 17:08:39 -03002465 sbridge_dev->bus,
Joe Perches956b9ba2012-04-29 17:08:39 -03002466 pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002467 }
2468
2469 /* Check if everything were registered */
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002470 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002471 !pvt->pci_ras || !pvt->pci_ta)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002472 goto enodev;
2473
Seth Jennings2900ea62015-08-05 13:16:01 -05002474 if (saw_chan_mask != 0x0f)
2475 goto enodev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002476 return 0;
2477
2478enodev:
2479 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2480 return -ENODEV;
2481
2482error:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002483 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2484 PCI_VENDOR_ID_INTEL, pdev->device);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002485 return -EINVAL;
2486}
2487
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002488static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2489 struct sbridge_dev *sbridge_dev)
2490{
2491 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002492 struct pci_dev *pdev;
2493 u8 saw_chan_mask = 0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002494 int i;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002495
2496 for (i = 0; i < sbridge_dev->n_devs; i++) {
2497 pdev = sbridge_dev->pdev[i];
2498 if (!pdev)
2499 continue;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002500
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002501 switch (pdev->device) {
2502 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002503 pvt->pci_ha = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002504 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002505 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002506 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002507 pvt->pci_ta = pdev;
2508 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002509 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002510 pvt->pci_ras = pdev;
2511 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002512 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2513 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002514 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2515 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002516 {
2517 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
2518 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002519 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002520 }
2521 break;
2522 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2523 pvt->pci_ddrio = pdev;
2524 break;
2525 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
Tony Luck7d375bf2015-05-18 17:50:42 -03002526 pvt->pci_ddrio = pdev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002527 break;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002528 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2529 pvt->pci_sad0 = pdev;
2530 break;
2531 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2532 pvt->pci_br0 = pdev;
2533 break;
2534 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2535 pvt->pci_br1 = pdev;
2536 break;
2537 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002538 pvt->pci_ha = pdev;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002539 break;
2540 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2541 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
Tony Luck7d375bf2015-05-18 17:50:42 -03002542 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2543 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002544 {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002545 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002546 pvt->pci_tad[id] = pdev;
Tony Luck7d375bf2015-05-18 17:50:42 -03002547 saw_chan_mask |= 1 << id;
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002548 }
2549 break;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002550 default:
2551 goto error;
2552 }
2553
2554 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2555 sbridge_dev->bus,
2556 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2557 pdev);
2558 }
2559
2560 /* Check if everything were registered */
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002561 if (!pvt->pci_sad0 || !pvt->pci_ha || !pvt->pci_br0 ||
Colin Ian Kingc7c35402016-09-08 09:38:01 +01002562 !pvt->pci_br1 || !pvt->pci_ras || !pvt->pci_ta)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002563 goto enodev;
2564
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002565 if (saw_chan_mask != 0x0f && /* -EN/-EX */
2566 saw_chan_mask != 0x03) /* -EP */
Tony Luck7d375bf2015-05-18 17:50:42 -03002567 goto enodev;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002568 return 0;
2569
2570enodev:
2571 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2572 return -ENODEV;
2573
2574error:
2575 sbridge_printk(KERN_ERR,
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03002576 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2577 pdev->device);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002578 return -EINVAL;
2579}
2580
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002581static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2582 struct sbridge_dev *sbridge_dev)
2583{
2584 struct sbridge_pvt *pvt = mci->pvt_info;
Tony Luck7d375bf2015-05-18 17:50:42 -03002585 struct pci_dev *pdev;
2586 u8 saw_chan_mask = 0;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002587 int i;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002588
2589 /* there's only one device per system; not tied to any bus */
2590 if (pvt->info.pci_vtd == NULL)
2591 /* result will be checked later */
2592 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2593 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2594 NULL);
2595
2596 for (i = 0; i < sbridge_dev->n_devs; i++) {
2597 pdev = sbridge_dev->pdev[i];
2598 if (!pdev)
2599 continue;
2600
2601 switch (pdev->device) {
2602 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2603 pvt->pci_sad0 = pdev;
2604 break;
2605 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2606 pvt->pci_sad1 = pdev;
2607 break;
2608 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002609 pvt->pci_ha = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002610 break;
2611 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2612 pvt->pci_ta = pdev;
2613 break;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002614 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002615 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002616 pvt->pci_ras = pdev;
2617 break;
2618 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002619 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002620 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002621 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
Tony Luck7d375bf2015-05-18 17:50:42 -03002622 {
2623 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
2624
2625 pvt->pci_tad[id] = pdev;
2626 saw_chan_mask |= 1 << id;
2627 }
2628 break;
2629 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2630 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2631 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2632 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2633 {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002634 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0;
Tony Luck7d375bf2015-05-18 17:50:42 -03002635
2636 pvt->pci_tad[id] = pdev;
2637 saw_chan_mask |= 1 << id;
2638 }
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002639 break;
2640 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
Aristeu Rozanski71793852015-06-12 09:44:52 -04002641 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2642 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2643 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2644 if (!pvt->pci_ddrio)
2645 pvt->pci_ddrio = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002646 break;
2647 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002648 pvt->pci_ha = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002649 break;
2650 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002651 pvt->pci_ta = pdev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002652 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002653 default:
2654 break;
2655 }
2656
2657 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2658 sbridge_dev->bus,
2659 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2660 pdev);
2661 }
2662
2663 /* Check if everything were registered */
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002664 if (!pvt->pci_sad0 || !pvt->pci_ha || !pvt->pci_sad1 ||
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002665 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2666 goto enodev;
2667
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002668 if (saw_chan_mask != 0x0f && /* -EN/-EX */
2669 saw_chan_mask != 0x03) /* -EP */
Tony Luck7d375bf2015-05-18 17:50:42 -03002670 goto enodev;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03002671 return 0;
2672
2673enodev:
2674 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2675 return -ENODEV;
2676}
2677
Tony Luck1f395812014-12-02 09:27:30 -08002678static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2679 struct sbridge_dev *sbridge_dev)
2680{
2681 struct sbridge_pvt *pvt = mci->pvt_info;
2682 struct pci_dev *pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002683 u8 saw_chan_mask = 0;
Tony Luck1f395812014-12-02 09:27:30 -08002684 int i;
2685
2686 /* there's only one device per system; not tied to any bus */
2687 if (pvt->info.pci_vtd == NULL)
2688 /* result will be checked later */
2689 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2690 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2691 NULL);
2692
2693 for (i = 0; i < sbridge_dev->n_devs; i++) {
2694 pdev = sbridge_dev->pdev[i];
2695 if (!pdev)
2696 continue;
2697
2698 switch (pdev->device) {
2699 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2700 pvt->pci_sad0 = pdev;
2701 break;
2702 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2703 pvt->pci_sad1 = pdev;
2704 break;
2705 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002706 pvt->pci_ha = pdev;
Tony Luck1f395812014-12-02 09:27:30 -08002707 break;
2708 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2709 pvt->pci_ta = pdev;
2710 break;
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002711 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002712 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM:
Tony Luck1f395812014-12-02 09:27:30 -08002713 pvt->pci_ras = pdev;
2714 break;
2715 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
Tony Luck1f395812014-12-02 09:27:30 -08002716 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
Tony Luck1f395812014-12-02 09:27:30 -08002717 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
Tony Luck1f395812014-12-02 09:27:30 -08002718 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
Tony Luckfa2ce642015-05-20 19:10:35 -03002719 {
2720 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
2721 pvt->pci_tad[id] = pdev;
2722 saw_chan_mask |= 1 << id;
2723 }
2724 break;
2725 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2726 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2727 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2728 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2729 {
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002730 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0;
Tony Luckfa2ce642015-05-20 19:10:35 -03002731 pvt->pci_tad[id] = pdev;
2732 saw_chan_mask |= 1 << id;
2733 }
Tony Luck1f395812014-12-02 09:27:30 -08002734 break;
2735 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2736 pvt->pci_ddrio = pdev;
2737 break;
Tony Luckfa2ce642015-05-20 19:10:35 -03002738 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002739 pvt->pci_ha = pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002740 break;
2741 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002742 pvt->pci_ta = pdev;
Tony Luckfa2ce642015-05-20 19:10:35 -03002743 break;
Tony Luck1f395812014-12-02 09:27:30 -08002744 default:
2745 break;
2746 }
2747
2748 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2749 sbridge_dev->bus,
2750 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2751 pdev);
2752 }
2753
2754 /* Check if everything were registered */
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002755 if (!pvt->pci_sad0 || !pvt->pci_ha || !pvt->pci_sad1 ||
Tony Luck1f395812014-12-02 09:27:30 -08002756 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2757 goto enodev;
2758
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08002759 if (saw_chan_mask != 0x0f && /* -EN/-EX */
2760 saw_chan_mask != 0x03) /* -EP */
Tony Luckfa2ce642015-05-20 19:10:35 -03002761 goto enodev;
Tony Luck1f395812014-12-02 09:27:30 -08002762 return 0;
2763
2764enodev:
2765 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2766 return -ENODEV;
2767}
2768
Jim Snowd0cdf902015-12-03 10:48:54 +01002769static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2770 struct sbridge_dev *sbridge_dev)
2771{
2772 struct sbridge_pvt *pvt = mci->pvt_info;
2773 struct pci_dev *pdev;
2774 int dev, func;
2775
2776 int i;
2777 int devidx;
2778
2779 for (i = 0; i < sbridge_dev->n_devs; i++) {
2780 pdev = sbridge_dev->pdev[i];
2781 if (!pdev)
2782 continue;
2783
2784 /* Extract PCI device and function. */
2785 dev = (pdev->devfn >> 3) & 0x1f;
2786 func = pdev->devfn & 0x7;
2787
2788 switch (pdev->device) {
2789 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2790 if (dev == 8)
2791 pvt->knl.pci_mc0 = pdev;
2792 else if (dev == 9)
2793 pvt->knl.pci_mc1 = pdev;
2794 else {
2795 sbridge_printk(KERN_ERR,
2796 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2797 dev, func);
2798 continue;
2799 }
2800 break;
2801
2802 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2803 pvt->pci_sad0 = pdev;
2804 break;
2805
2806 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2807 pvt->pci_sad1 = pdev;
2808 break;
2809
2810 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2811 /* There are one of these per tile, and range from
2812 * 1.14.0 to 1.18.5.
2813 */
2814 devidx = ((dev-14)*8)+func;
2815
2816 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2817 sbridge_printk(KERN_ERR,
2818 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2819 dev, func);
2820 continue;
2821 }
2822
2823 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2824
2825 pvt->knl.pci_cha[devidx] = pdev;
2826 break;
2827
Qiuxu Zhuo00cf50d2017-05-23 08:05:33 +08002828 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN:
Jim Snowd0cdf902015-12-03 10:48:54 +01002829 devidx = -1;
2830
2831 /*
2832 * MC0 channels 0-2 are device 9 function 2-4,
2833 * MC1 channels 3-5 are device 8 function 2-4.
2834 */
2835
2836 if (dev == 9)
2837 devidx = func-2;
2838 else if (dev == 8)
2839 devidx = 3 + (func-2);
2840
2841 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2842 sbridge_printk(KERN_ERR,
2843 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2844 dev, func);
2845 continue;
2846 }
2847
2848 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2849 pvt->knl.pci_channel[devidx] = pdev;
2850 break;
2851
2852 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2853 pvt->knl.pci_mc_info = pdev;
2854 break;
2855
2856 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2857 pvt->pci_ta = pdev;
2858 break;
2859
2860 default:
2861 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2862 pdev->device);
2863 break;
2864 }
2865 }
2866
2867 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2868 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2869 !pvt->pci_ta) {
2870 goto enodev;
2871 }
2872
2873 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2874 if (!pvt->knl.pci_channel[i]) {
2875 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2876 goto enodev;
2877 }
2878 }
2879
2880 for (i = 0; i < KNL_MAX_CHAS; i++) {
2881 if (!pvt->knl.pci_cha[i]) {
2882 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2883 goto enodev;
2884 }
2885 }
2886
2887 return 0;
2888
2889enodev:
2890 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2891 return -ENODEV;
2892}
2893
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002894/****************************************************************************
2895 Error check routines
2896 ****************************************************************************/
2897
2898/*
2899 * While Sandy Bridge has error count registers, SMI BIOS read values from
2900 * and resets the counters. So, they are not reliable for the OS to read
2901 * from them. So, we have no option but to just trust on whatever MCE is
2902 * telling us about the errors.
2903 */
2904static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2905 const struct mce *m)
2906{
2907 struct mem_ctl_info *new_mci;
2908 struct sbridge_pvt *pvt = mci->pvt_info;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002909 enum hw_event_mc_err_type tp_event;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002910 char *type, *optype, msg[256];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002911 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2912 bool overflow = GET_BITFIELD(m->status, 62, 62);
2913 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002914 bool recoverable;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002915 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2916 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2917 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2918 u32 channel = GET_BITFIELD(m->status, 0, 3);
2919 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2920 long channel_mask, first_channel;
Tony Luck7d375bf2015-05-18 17:50:42 -03002921 u8 rank, socket, ha;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002922 int rc, dimm;
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03002923 char *area_type = NULL;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002924
Tony Luckfa2ce642015-05-20 19:10:35 -03002925 if (pvt->info.type != SANDY_BRIDGE)
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03002926 recoverable = true;
2927 else
2928 recoverable = GET_BITFIELD(m->status, 56, 56);
2929
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002930 if (uncorrected_error) {
2931 if (ripv) {
2932 type = "FATAL";
2933 tp_event = HW_EVENT_ERR_FATAL;
2934 } else {
2935 type = "NON_FATAL";
2936 tp_event = HW_EVENT_ERR_UNCORRECTED;
2937 }
2938 } else {
2939 type = "CORRECTED";
2940 tp_event = HW_EVENT_ERR_CORRECTED;
2941 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002942
2943 /*
David Mackey15ed1032012-04-17 11:30:52 -07002944 * According with Table 15-9 of the Intel Architecture spec vol 3A,
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002945 * memory errors should fit in this mask:
2946 * 000f 0000 1mmm cccc (binary)
2947 * where:
2948 * f = Correction Report Filtering Bit. If 1, subsequent errors
2949 * won't be shown
2950 * mmm = error type
2951 * cccc = channel
2952 * If the mask doesn't match, report an error to the parsing logic
2953 */
2954 if (! ((errcode & 0xef80) == 0x80)) {
2955 optype = "Can't parse: it is not a mem";
2956 } else {
2957 switch (optypenum) {
2958 case 0:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002959 optype = "generic undef request error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002960 break;
2961 case 1:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002962 optype = "memory read error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002963 break;
2964 case 2:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002965 optype = "memory write error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002966 break;
2967 case 3:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002968 optype = "addr/cmd error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002969 break;
2970 case 4:
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03002971 optype = "memory scrubbing error";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02002972 break;
2973 default:
2974 optype = "reserved";
2975 break;
2976 }
2977 }
2978
Aristeu Rozanskibe3036d2013-10-30 13:27:05 -03002979 /* Only decode errors with an valid address (ADDRV) */
2980 if (!GET_BITFIELD(m->status, 58, 58))
2981 return;
2982
Jim Snowd0cdf902015-12-03 10:48:54 +01002983 if (pvt->info.type == KNIGHTS_LANDING) {
2984 if (channel == 14) {
2985 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2986 overflow ? " OVERFLOW" : "",
2987 (uncorrected_error && recoverable)
2988 ? " recoverable" : "",
2989 mscod, errcode,
2990 m->bank);
2991 } else {
2992 char A = *("A");
2993
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02002994 /*
2995 * Reported channel is in range 0-2, so we can't map it
2996 * back to mc. To figure out mc we check machine check
2997 * bank register that reported this error.
2998 * bank15 means mc0 and bank16 means mc1.
2999 */
3000 channel = knl_channel_remap(m->bank == 16, channel);
Jim Snowd0cdf902015-12-03 10:48:54 +01003001 channel_mask = 1 << channel;
Lukasz Odziobac5b48fa2016-07-23 01:44:49 +02003002
Jim Snowd0cdf902015-12-03 10:48:54 +01003003 snprintf(msg, sizeof(msg),
3004 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
3005 overflow ? " OVERFLOW" : "",
3006 (uncorrected_error && recoverable)
3007 ? " recoverable" : " ",
3008 mscod, errcode, channel, A + channel);
3009 edac_mc_handle_error(tp_event, mci, core_err_cnt,
3010 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
3011 channel, 0, -1,
3012 optype, msg);
3013 }
3014 return;
3015 } else {
3016 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
3017 &channel_mask, &rank, &area_type, msg);
3018 }
3019
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003020 if (rc < 0)
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003021 goto err_parsing;
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003022 new_mci = get_mci_for_node_id(socket, ha);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003023 if (!new_mci) {
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003024 strcpy(msg, "Error: socket got corrupted!");
3025 goto err_parsing;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003026 }
3027 mci = new_mci;
3028 pvt = mci->pvt_info;
3029
3030 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
3031
3032 if (rank < 4)
3033 dimm = 0;
3034 else if (rank < 8)
3035 dimm = 1;
3036 else
3037 dimm = 2;
3038
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003039
3040 /*
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003041 * FIXME: On some memory configurations (mirror, lockstep), the
3042 * Memory Controller can't point the error to a single DIMM. The
3043 * EDAC core should be handling the channel mask, in order to point
3044 * to the group of dimm's where the error may be happening.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003045 */
Aristeu Rozanskid7c660b2014-06-02 15:15:28 -03003046 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
3047 channel = first_channel;
3048
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003049 snprintf(msg, sizeof(msg),
Tony Luck7d375bf2015-05-18 17:50:42 -03003050 "%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 -03003051 overflow ? " OVERFLOW" : "",
3052 (uncorrected_error && recoverable) ? " recoverable" : "",
3053 area_type,
3054 mscod, errcode,
Tony Luck7d375bf2015-05-18 17:50:42 -03003055 socket, ha,
Mauro Carvalho Chehabe17a2f42a2012-05-11 11:41:45 -03003056 channel_mask,
3057 rank);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003058
Joe Perches956b9ba2012-04-29 17:08:39 -03003059 edac_dbg(0, "%s\n", msg);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003060
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003061 /* FIXME: need support for channel mask */
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003062
Seth Jennings351fc4a2014-09-05 14:28:47 -05003063 if (channel == CHANNEL_UNSPECIFIED)
3064 channel = -1;
3065
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003066 /* Call the helper to output message */
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003067 edac_mc_handle_error(tp_event, mci, core_err_cnt,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003068 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003069 channel, dimm, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003070 optype, msg);
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003071 return;
3072err_parsing:
Mauro Carvalho Chehabc1053832012-06-04 13:40:05 -03003073 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003074 -1, -1, -1,
Mauro Carvalho Chehab03f7eae2012-06-04 11:29:25 -03003075 msg, "");
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003076
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003077}
3078
3079/*
Tony Luckad08c4e2016-04-15 14:50:32 -07003080 * Check that logging is enabled and that this is the right type
3081 * of error for us to handle.
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003082 */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003083static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3084 void *data)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003085{
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003086 struct mce *mce = (struct mce *)data;
3087 struct mem_ctl_info *mci;
3088 struct sbridge_pvt *pvt;
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003089 char *type;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003090
Borislav Petkovbffc7de2017-02-04 18:10:14 +01003091 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
Chen, Gongfd521032013-12-06 01:17:09 -05003092 return NOTIFY_DONE;
3093
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003094 mci = get_mci_for_node_id(mce->socketid, IMC0);
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003095 if (!mci)
Tony Luckc4fc1952016-04-29 15:42:25 +02003096 return NOTIFY_DONE;
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003097 pvt = mci->pvt_info;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003098
3099 /*
3100 * Just let mcelog handle it if the error is
3101 * outside the memory controller. A memory error
3102 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3103 * bit 12 has an special meaning.
3104 */
3105 if ((mce->status & 0xefff) >> 7 != 1)
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003106 return NOTIFY_DONE;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003107
Aristeu Rozanskicf40f802014-03-11 15:45:41 -04003108 if (mce->mcgstatus & MCG_STATUS_MCIP)
3109 type = "Exception";
3110 else
3111 type = "Event";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003112
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003113 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003114
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003115 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3116 "Bank %d: %016Lx\n", mce->extcpu, type,
3117 mce->mcgstatus, mce->bank, mce->status);
3118 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3119 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3120 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003121
Aristeu Rozanski49856dc2014-03-11 15:45:42 -04003122 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3123 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3124 mce->time, mce->socketid, mce->apicid);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003125
Tony Luckad08c4e2016-04-15 14:50:32 -07003126 sbridge_mce_output_error(mci, mce);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003127
3128 /* Advice mcelog that the error were handled */
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003129 return NOTIFY_STOP;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003130}
3131
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003132static struct notifier_block sbridge_mce_dec = {
Borislav Petkov9026cc82017-01-23 19:35:14 +01003133 .notifier_call = sbridge_mce_check_error,
3134 .priority = MCE_PRIO_EDAC,
Mauro Carvalho Chehab3d78c9a2011-10-20 19:33:46 -02003135};
3136
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003137/****************************************************************************
3138 EDAC register/unregister logic
3139 ****************************************************************************/
3140
3141static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3142{
3143 struct mem_ctl_info *mci = sbridge_dev->mci;
3144 struct sbridge_pvt *pvt;
3145
3146 if (unlikely(!mci || !mci->pvt_info)) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003147 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003148
3149 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3150 return;
3151 }
3152
3153 pvt = mci->pvt_info;
3154
Joe Perches956b9ba2012-04-29 17:08:39 -03003155 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3156 mci, &sbridge_dev->pdev[0]->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003157
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003158 /* Remove MC sysfs nodes */
Mauro Carvalho Chehabfd687502012-03-16 07:44:18 -03003159 edac_mc_del_mc(mci->pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003160
Joe Perches956b9ba2012-04-29 17:08:39 -03003161 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003162 kfree(mci->ctl_name);
3163 edac_mc_free(mci);
3164 sbridge_dev->mci = NULL;
3165}
3166
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003167static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003168{
3169 struct mem_ctl_info *mci;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003170 struct edac_mc_layer layers[2];
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003171 struct sbridge_pvt *pvt;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003172 struct pci_dev *pdev = sbridge_dev->pdev[0];
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003173 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003174
3175 /* Check the number of active and not disabled channels */
Aristeu Rozanskidbc954d2014-06-02 15:15:25 -03003176 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003177 if (unlikely(rc < 0))
3178 return rc;
3179
3180 /* allocate a new MC control structure */
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003181 layers[0].type = EDAC_MC_LAYER_CHANNEL;
Jim Snowd0cdf902015-12-03 10:48:54 +01003182 layers[0].size = type == KNIGHTS_LANDING ?
3183 KNL_MAX_CHANNELS : NUM_CHANNELS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003184 layers[0].is_virt_csrow = false;
3185 layers[1].type = EDAC_MC_LAYER_SLOT;
Jim Snowd0cdf902015-12-03 10:48:54 +01003186 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003187 layers[1].is_virt_csrow = true;
Mauro Carvalho Chehabca0907b2012-05-02 14:37:00 -03003188 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
Mauro Carvalho Chehabc36e3e72012-04-16 15:12:22 -03003189 sizeof(*pvt));
3190
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003191 if (unlikely(!mci))
3192 return -ENOMEM;
3193
Joe Perches956b9ba2012-04-29 17:08:39 -03003194 edac_dbg(0, "MC: mci = %p, dev = %p\n",
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003195 mci, &pdev->dev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003196
3197 pvt = mci->pvt_info;
3198 memset(pvt, 0, sizeof(*pvt));
3199
3200 /* Associate sbridge_dev and mci for future usage */
3201 pvt->sbridge_dev = sbridge_dev;
3202 sbridge_dev->mci = mci;
3203
Jim Snowd0cdf902015-12-03 10:48:54 +01003204 mci->mtype_cap = type == KNIGHTS_LANDING ?
3205 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003206 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3207 mci->edac_cap = EDAC_FLAG_NONE;
Borislav Petkov199389a2017-05-25 13:00:05 +02003208 mci->mod_name = "sb_edac.c";
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003209 mci->mod_ver = SBRIDGE_REVISION;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003210 mci->dev_name = pci_name(pdev);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003211 mci->ctl_page_to_phys = NULL;
3212
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003213 pvt->info.type = type;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003214 switch (type) {
3215 case IVY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003216 pvt->info.rankcfgr = IB_RANK_CFG_A;
3217 pvt->info.get_tolm = ibridge_get_tolm;
3218 pvt->info.get_tohm = ibridge_get_tohm;
3219 pvt->info.dram_rule = ibridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003220 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003221 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003222 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003223 pvt->info.sad_limit = sad_limit;
3224 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003225 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003226 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3227 pvt->info.interleave_list = ibridge_interleave_list;
3228 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3229 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003230 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003231
3232 /* Store pci devices at mci for faster access */
3233 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3234 if (unlikely(rc < 0))
3235 goto fail0;
Tony Luck7fd562b2017-05-23 08:06:03 +08003236 get_source_id(mci);
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003237 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge SrcID#%d_Ha#%d",
3238 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003239 break;
3240 case SANDY_BRIDGE:
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003241 pvt->info.rankcfgr = SB_RANK_CFG_A;
3242 pvt->info.get_tolm = sbridge_get_tolm;
3243 pvt->info.get_tohm = sbridge_get_tohm;
3244 pvt->info.dram_rule = sbridge_dram_rule;
Aristeu Rozanski9e375442014-06-02 15:15:22 -03003245 pvt->info.get_memory_type = get_memory_type;
Aristeu Rozanskif14d6892014-06-02 15:15:23 -03003246 pvt->info.get_node_id = get_node_id;
Aristeu Rozanskib976bcf2014-06-02 15:15:24 -03003247 pvt->info.rir_limit = rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003248 pvt->info.sad_limit = sad_limit;
3249 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003250 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003251 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3252 pvt->info.interleave_list = sbridge_interleave_list;
3253 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3254 pvt->info.interleave_pkg = sbridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003255 pvt->info.get_width = sbridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003256
3257 /* Store pci devices at mci for faster access */
3258 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3259 if (unlikely(rc < 0))
3260 goto fail0;
Tony Luck7fd562b2017-05-23 08:06:03 +08003261 get_source_id(mci);
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003262 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge SrcID#%d_Ha#%d",
3263 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003264 break;
3265 case HASWELL:
3266 /* rankcfgr isn't used */
3267 pvt->info.get_tolm = haswell_get_tolm;
3268 pvt->info.get_tohm = haswell_get_tohm;
3269 pvt->info.dram_rule = ibridge_dram_rule;
3270 pvt->info.get_memory_type = haswell_get_memory_type;
3271 pvt->info.get_node_id = haswell_get_node_id;
3272 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003273 pvt->info.sad_limit = sad_limit;
3274 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003275 pvt->info.dram_attr = dram_attr;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003276 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3277 pvt->info.interleave_list = ibridge_interleave_list;
3278 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3279 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003280 pvt->info.get_width = ibridge_get_width;
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003281
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003282 /* Store pci devices at mci for faster access */
3283 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3284 if (unlikely(rc < 0))
3285 goto fail0;
Tony Luck7fd562b2017-05-23 08:06:03 +08003286 get_source_id(mci);
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003287 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell SrcID#%d_Ha#%d",
3288 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003289 break;
Tony Luck1f395812014-12-02 09:27:30 -08003290 case BROADWELL:
3291 /* rankcfgr isn't used */
3292 pvt->info.get_tolm = haswell_get_tolm;
3293 pvt->info.get_tohm = haswell_get_tohm;
3294 pvt->info.dram_rule = ibridge_dram_rule;
3295 pvt->info.get_memory_type = haswell_get_memory_type;
3296 pvt->info.get_node_id = haswell_get_node_id;
3297 pvt->info.rir_limit = haswell_rir_limit;
Jim Snowc59f9c02015-12-03 10:48:52 +01003298 pvt->info.sad_limit = sad_limit;
3299 pvt->info.interleave_mode = interleave_mode;
Jim Snowc59f9c02015-12-03 10:48:52 +01003300 pvt->info.dram_attr = dram_attr;
Tony Luck1f395812014-12-02 09:27:30 -08003301 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3302 pvt->info.interleave_list = ibridge_interleave_list;
3303 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3304 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Aristeu Rozanski12f07212015-06-12 15:08:17 -04003305 pvt->info.get_width = broadwell_get_width;
Tony Luck1f395812014-12-02 09:27:30 -08003306
3307 /* Store pci devices at mci for faster access */
3308 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3309 if (unlikely(rc < 0))
3310 goto fail0;
Tony Luck7fd562b2017-05-23 08:06:03 +08003311 get_source_id(mci);
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003312 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell SrcID#%d_Ha#%d",
3313 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
Tony Luck1f395812014-12-02 09:27:30 -08003314 break;
Jim Snowd0cdf902015-12-03 10:48:54 +01003315 case KNIGHTS_LANDING:
3316 /* pvt->info.rankcfgr == ??? */
3317 pvt->info.get_tolm = knl_get_tolm;
3318 pvt->info.get_tohm = knl_get_tohm;
3319 pvt->info.dram_rule = knl_dram_rule;
3320 pvt->info.get_memory_type = knl_get_memory_type;
3321 pvt->info.get_node_id = knl_get_node_id;
3322 pvt->info.rir_limit = NULL;
3323 pvt->info.sad_limit = knl_sad_limit;
3324 pvt->info.interleave_mode = knl_interleave_mode;
Jim Snowd0cdf902015-12-03 10:48:54 +01003325 pvt->info.dram_attr = dram_attr_knl;
3326 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3327 pvt->info.interleave_list = knl_interleave_list;
3328 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3329 pvt->info.interleave_pkg = ibridge_interleave_pkg;
Hubert Chrzaniuk45f4d3a2015-12-11 14:21:22 +01003330 pvt->info.get_width = knl_get_width;
Jim Snowd0cdf902015-12-03 10:48:54 +01003331
3332 rc = knl_mci_bind_devs(mci, sbridge_dev);
3333 if (unlikely(rc < 0))
3334 goto fail0;
Tony Luck7fd562b2017-05-23 08:06:03 +08003335 get_source_id(mci);
Qiuxu Zhuoe2f747b2017-05-23 08:07:31 +08003336 mci->ctl_name = kasprintf(GFP_KERNEL, "Knights Landing SrcID#%d_Ha#%d",
3337 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
Jim Snowd0cdf902015-12-03 10:48:54 +01003338 break;
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003339 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003340
3341 /* Get dimm basic config and the memory layout */
3342 get_dimm_config(mci);
3343 get_memory_layout(mci);
3344
3345 /* record ptr to the generic device */
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003346 mci->pdev = &pdev->dev;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003347
3348 /* add this new MC control structure to EDAC's list of MCs */
3349 if (unlikely(edac_mc_add_mc(mci))) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003350 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003351 rc = -EINVAL;
Tony Luck7fd562b2017-05-23 08:06:03 +08003352 goto fail;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003353 }
3354
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003355 return 0;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003356
Tony Luck7fd562b2017-05-23 08:06:03 +08003357fail:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003358 kfree(mci->ctl_name);
Tony Luck7fd562b2017-05-23 08:06:03 +08003359fail0:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003360 edac_mc_free(mci);
3361 sbridge_dev->mci = NULL;
3362 return rc;
3363}
3364
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003365#define ICPU(model, table) \
3366 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3367
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003368static const struct x86_cpu_id sbridge_cpuids[] = {
Dave Hansen20f4d692016-09-29 13:43:21 -07003369 ICPU(INTEL_FAM6_SANDYBRIDGE_X, pci_dev_descr_sbridge_table),
3370 ICPU(INTEL_FAM6_IVYBRIDGE_X, pci_dev_descr_ibridge_table),
3371 ICPU(INTEL_FAM6_HASWELL_X, pci_dev_descr_haswell_table),
3372 ICPU(INTEL_FAM6_BROADWELL_X, pci_dev_descr_broadwell_table),
3373 ICPU(INTEL_FAM6_BROADWELL_XEON_D, pci_dev_descr_broadwell_table),
3374 ICPU(INTEL_FAM6_XEON_PHI_KNL, pci_dev_descr_knl_table),
Piotr Luc9a9260ca2016-10-13 17:30:59 +02003375 ICPU(INTEL_FAM6_XEON_PHI_KNM, pci_dev_descr_knl_table),
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003376 { }
3377};
3378MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3379
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003380/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003381 * sbridge_probe Get all devices and register memory controllers
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003382 * present.
3383 * return:
3384 * 0 for FOUND a device
3385 * < 0 for error code
3386 */
3387
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003388static int sbridge_probe(const struct x86_cpu_id *id)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003389{
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003390 int rc = -ENODEV;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003391 u8 mc, num_mc = 0;
3392 struct sbridge_dev *sbridge_dev;
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003393 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003394
3395 /* get the pci devices we want to reserve for our use */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003396 rc = sbridge_get_all_devices(&num_mc, ptable);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003397
Borislav Petkov11249e72015-02-05 12:39:36 +01003398 if (unlikely(rc < 0)) {
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003399 edac_dbg(0, "couldn't get all devices\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003400 goto fail0;
Borislav Petkov11249e72015-02-05 12:39:36 +01003401 }
3402
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003403 mc = 0;
3404
3405 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
Joe Perches956b9ba2012-04-29 17:08:39 -03003406 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3407 mc, mc + 1, num_mc);
Aristeu Rozanski50d1bb92014-06-20 10:27:54 -03003408
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003409 sbridge_dev->mc = mc++;
Tony Luck665f05e02016-06-02 10:58:08 -07003410 rc = sbridge_register_mci(sbridge_dev, ptable->type);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003411 if (unlikely(rc < 0))
3412 goto fail1;
3413 }
3414
Borislav Petkov11249e72015-02-05 12:39:36 +01003415 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003416
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003417 return 0;
3418
3419fail1:
3420 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3421 sbridge_unregister_mci(sbridge_dev);
3422
3423 sbridge_put_all_devices();
3424fail0:
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003425 return rc;
3426}
3427
3428/*
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003429 * sbridge_remove cleanup
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003430 *
3431 */
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003432static void sbridge_remove(void)
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003433{
3434 struct sbridge_dev *sbridge_dev;
3435
Joe Perches956b9ba2012-04-29 17:08:39 -03003436 edac_dbg(0, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003437
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003438 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3439 sbridge_unregister_mci(sbridge_dev);
3440
3441 /* Release PCI resources */
3442 sbridge_put_all_devices();
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003443}
3444
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003445/*
3446 * sbridge_init Module entry function
3447 * Try to initialize this module for its devices
3448 */
3449static int __init sbridge_init(void)
3450{
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003451 const struct x86_cpu_id *id;
3452 int rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003453
Joe Perches956b9ba2012-04-29 17:08:39 -03003454 edac_dbg(2, "\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003455
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003456 id = x86_match_cpu(sbridge_cpuids);
3457 if (!id)
3458 return -ENODEV;
3459
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003460 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3461 opstate_init();
3462
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003463 rc = sbridge_probe(id);
3464
3465 if (rc >= 0) {
Chen Gonge35fca42012-05-08 20:40:12 -03003466 mce_register_decode_chain(&sbridge_mce_dec);
Borislav Petkovbffc7de2017-02-04 18:10:14 +01003467 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
Chen, Gongfd521032013-12-06 01:17:09 -05003468 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003469 return 0;
Chen Gonge35fca42012-05-08 20:40:12 -03003470 }
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003471
3472 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003473 rc);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003474
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003475 return rc;
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003476}
3477
3478/*
3479 * sbridge_exit() Module exit function
3480 * Unregister the driver
3481 */
3482static void __exit sbridge_exit(void)
3483{
Joe Perches956b9ba2012-04-29 17:08:39 -03003484 edac_dbg(2, "\n");
Tony Luck2c1ea4c2016-04-28 15:40:00 -07003485 sbridge_remove();
Chen Gonge35fca42012-05-08 20:40:12 -03003486 mce_unregister_decode_chain(&sbridge_mce_dec);
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003487}
3488
3489module_init(sbridge_init);
3490module_exit(sbridge_exit);
3491
3492module_param(edac_op_state, int, 0444);
3493MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3494
3495MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003496MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003497MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
Aristeu Rozanski4d715a82013-10-30 13:27:06 -03003498MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
Mauro Carvalho Chehabeebf11a2011-10-20 19:18:01 -02003499 SBRIDGE_REVISION);