blob: ddd5990211f8abbce8ddeea330e6ca8977f350e1 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* 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:
10 * Mauro Carvalho Chehab
11 */
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>
21#include <linux/smp.h>
22#include <linux/bitmap.h>
23#include <linux/math64.h>
24#include <linux/mod_devicetable.h>
25#include <asm/cpu_device_id.h>
26#include <asm/intel-family.h>
27#include <asm/processor.h>
28#include <asm/mce.h>
29
30#include "edac_module.h"
31
32/* Static vars */
33static LIST_HEAD(sbridge_edac_list);
34
35/*
36 * Alter this version for the module when modifications are made
37 */
38#define SBRIDGE_REVISION " Ver: 1.1.2 "
39#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) \
54 (((v) & GENMASK_ULL(hi, lo)) >> (lo))
55
56/* Devices 12 Function 6, Offsets 0x80 to 0xcc */
57static const u32 sbridge_dram_rule[] = {
58 0x80, 0x88, 0x90, 0x98, 0xa0,
59 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
60};
61
62static 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};
68
69static 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
77#define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
78#define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
79
80static char *show_dram_attr(u32 attr)
81{
82 switch (attr) {
83 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
94static const u32 sbridge_interleave_list[] = {
95 0x84, 0x8c, 0x94, 0x9c, 0xa4,
96 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
97};
98
99static 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};
105
106static 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
114struct interleave_pkg {
115 unsigned char start;
116 unsigned char end;
117};
118
119static 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
130static 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
141static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
142 int interleave)
143{
144 return GET_BITFIELD(reg, table[interleave].start,
145 table[interleave].end);
146}
147
148/* Devices 12 Function 7 */
149
150#define TOLM 0x80
151#define TOHM 0x84
152#define HASWELL_TOLM 0xd0
153#define HASWELL_TOHM_0 0xd4
154#define HASWELL_TOHM_1 0xd8
155#define KNL_TOLM 0xd0
156#define KNL_TOHM_0 0xd4
157#define KNL_TOHM_1 0xd8
158
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
168#define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
169
170#define SAD_CONTROL 0xf4
171
172/* 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
192#define KNL_MCMTR 0x624
193
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
209static const int knl_mtr_reg = 0xb60;
210
211#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)
232
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
243#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))
248
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
274#define SB_RANK_CFG_A 0x0328
275
276#define IB_RANK_CFG_A 0x0320
277
278/*
279 * sbridge structs
280 */
281
282#define NUM_CHANNELS 6 /* Max channels per MC */
283#define MAX_DIMMS 3 /* Max DIMMS per channel */
284#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 */
287#define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
288
289enum type {
290 SANDY_BRIDGE,
291 IVY_BRIDGE,
292 HASWELL,
293 BROADWELL,
294 KNIGHTS_LANDING,
295};
296
297enum domain {
298 IMC0 = 0,
299 IMC1,
300 SOCK,
301};
302
303enum mirroring_mode {
304 NON_MIRRORING,
305 ADDR_RANGE_MIRRORING,
306 FULL_MIRRORING,
307};
308
309struct sbridge_pvt;
310struct sbridge_info {
311 enum type type;
312 u32 mcmtr;
313 u32 rankcfgr;
314 u64 (*get_tolm)(struct sbridge_pvt *pvt);
315 u64 (*get_tohm)(struct sbridge_pvt *pvt);
316 u64 (*rir_limit)(u32 reg);
317 u64 (*sad_limit)(u32 reg);
318 u32 (*interleave_mode)(u32 reg);
319 u32 (*dram_attr)(u32 reg);
320 const u32 *dram_rule;
321 const u32 *interleave_list;
322 const struct interleave_pkg *interleave_pkg;
323 u8 max_sad;
324 u8 max_interleave;
325 u8 (*get_node_id)(struct sbridge_pvt *pvt);
326 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
327 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
328 struct pci_dev *pci_vtd;
329};
330
331struct sbridge_channel {
332 u32 ranks;
333 u32 dimms;
334};
335
336struct pci_id_descr {
337 int dev_id;
338 int optional;
339 enum domain dom;
340};
341
342struct pci_id_table {
343 const struct pci_id_descr *descr;
344 int n_devs_per_imc;
345 int n_devs_per_sock;
346 int n_imcs_per_sock;
347 enum type type;
348};
349
350struct sbridge_dev {
351 struct list_head list;
352 u8 bus, mc;
353 u8 node_id, source_id;
354 struct pci_dev **pdev;
355 enum domain dom;
356 int n_devs;
357 int i_devs;
358 struct mem_ctl_info *mci;
359};
360
361struct knl_pvt {
362 struct pci_dev *pci_cha[KNL_MAX_CHAS];
363 struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
364 struct pci_dev *pci_mc0;
365 struct pci_dev *pci_mc1;
366 struct pci_dev *pci_mc0_misc;
367 struct pci_dev *pci_mc1_misc;
368 struct pci_dev *pci_mc_info; /* tolm, tohm */
369};
370
371struct sbridge_pvt {
372 /* Devices per socket */
373 struct pci_dev *pci_ddrio;
374 struct pci_dev *pci_sad0, *pci_sad1;
375 struct pci_dev *pci_br0, *pci_br1;
376 /* Devices per memory controller */
377 struct pci_dev *pci_ha, *pci_ta, *pci_ras;
378 struct pci_dev *pci_tad[NUM_CHANNELS];
379
380 struct sbridge_dev *sbridge_dev;
381
382 struct sbridge_info info;
383 struct sbridge_channel channel[NUM_CHANNELS];
384
385 /* Memory type detection */
386 bool is_cur_addr_mirrored, is_lockstep, is_close_pg;
387 bool is_chan_hash;
388 enum mirroring_mode mirror_mode;
389
390 /* Memory description */
391 u64 tolm, tohm;
392 struct knl_pvt knl;
393};
394
395#define PCI_DESCR(device_id, opt, domain) \
396 .dev_id = (device_id), \
397 .optional = opt, \
398 .dom = domain
399
400static const struct pci_id_descr pci_dev_descr_sbridge[] = {
401 /* Processor Home Agent */
402 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0, IMC0) },
403
404 /* Memory controller */
405 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0, IMC0) },
406 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0, IMC0) },
407 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0, IMC0) },
408 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0, IMC0) },
409 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0, IMC0) },
410 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0, IMC0) },
411 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1, SOCK) },
412
413 /* System Address Decoder */
414 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0, SOCK) },
415 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0, SOCK) },
416
417 /* Broadcast Registers */
418 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0, SOCK) },
419};
420
421#define PCI_ID_TABLE_ENTRY(A, N, M, T) { \
422 .descr = A, \
423 .n_devs_per_imc = N, \
424 .n_devs_per_sock = ARRAY_SIZE(A), \
425 .n_imcs_per_sock = M, \
426 .type = T \
427}
428
429static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
430 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge, ARRAY_SIZE(pci_dev_descr_sbridge), 1, SANDY_BRIDGE),
431 {0,} /* 0 terminated list. */
432};
433
434/* This changes depending if 1HA or 2HA:
435 * 1HA:
436 * 0x0eb8 (17.0) is DDRIO0
437 * 2HA:
438 * 0x0ebc (17.4) is DDRIO0
439 */
440#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
441#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
442
443/* pci ids */
444#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
445#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
446#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
447#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
448#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
449#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
450#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
451#define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
452#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
453#define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
454#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
455#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
456#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
457#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
458#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
459#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
460#define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
461
462static const struct pci_id_descr pci_dev_descr_ibridge[] = {
463 /* Processor Home Agent */
464 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0, IMC0) },
465 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1, IMC1) },
466
467 /* Memory controller */
468 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0, IMC0) },
469 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0, IMC0) },
470 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0, IMC0) },
471 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0, IMC0) },
472 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0, IMC0) },
473 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0, IMC0) },
474
475 /* Optional, mode 2HA */
476 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1, IMC1) },
477 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1, IMC1) },
478 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1, IMC1) },
479 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1, IMC1) },
480 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1, IMC1) },
481 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1, IMC1) },
482
483 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1, SOCK) },
484 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1, SOCK) },
485
486 /* System Address Decoder */
487 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0, SOCK) },
488
489 /* Broadcast Registers */
490 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1, SOCK) },
491 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0, SOCK) },
492
493};
494
495static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
496 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge, 12, 2, IVY_BRIDGE),
497 {0,} /* 0 terminated list. */
498};
499
500/* Haswell support */
501/* EN processor:
502 * - 1 IMC
503 * - 3 DDR3 channels, 2 DPC per channel
504 * EP processor:
505 * - 1 or 2 IMC
506 * - 4 DDR4 channels, 3 DPC per channel
507 * EP 4S processor:
508 * - 2 IMC
509 * - 4 DDR4 channels, 3 DPC per channel
510 * EX processor:
511 * - 2 IMC
512 * - each IMC interfaces with a SMI 2 channel
513 * - each SMI channel interfaces with a scalable memory buffer
514 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
515 */
516#define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
517#define HASWELL_HASYSDEFEATURE2 0x84
518#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
519#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
520#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
521#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
522#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM 0x2f71
523#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
524#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM 0x2f79
525#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
526#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
527#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
528#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
529#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
530#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
531#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
532#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
533#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
534#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
535#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
536#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
537#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
538#define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
539static const struct pci_id_descr pci_dev_descr_haswell[] = {
540 /* first item must be the HA */
541 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0, IMC0) },
542 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1, IMC1) },
543
544 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0, IMC0) },
545 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM, 0, IMC0) },
546 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0, IMC0) },
547 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0, IMC0) },
548 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1, IMC0) },
549 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1, IMC0) },
550
551 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1, IMC1) },
552 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM, 1, IMC1) },
553 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1, IMC1) },
554 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1, IMC1) },
555 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1, IMC1) },
556 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1, IMC1) },
557
558 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0, SOCK) },
559 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0, SOCK) },
560 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1, SOCK) },
561 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1, SOCK) },
562 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1, SOCK) },
563 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1, SOCK) },
564};
565
566static const struct pci_id_table pci_dev_descr_haswell_table[] = {
567 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell, 13, 2, HASWELL),
568 {0,} /* 0 terminated list. */
569};
570
571/* Knight's Landing Support */
572/*
573 * KNL's memory channels are swizzled between memory controllers.
574 * MC0 is mapped to CH3,4,5 and MC1 is mapped to CH0,1,2
575 */
576#define knl_channel_remap(mc, chan) ((mc) ? (chan) : (chan) + 3)
577
578/* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
579#define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
580/* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
581#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN 0x7843
582/* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
583#define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
584/* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
585#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
586/* SAD target - 1-29-1 (1 of these) */
587#define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
588/* Caching / Home Agent */
589#define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
590/* Device with TOLM and TOHM, 0-5-0 (1 of these) */
591#define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
592
593/*
594 * KNL differs from SB, IB, and Haswell in that it has multiple
595 * instances of the same device with the same device ID, so we handle that
596 * by creating as many copies in the table as we expect to find.
597 * (Like device ID must be grouped together.)
598 */
599
600static const struct pci_id_descr pci_dev_descr_knl[] = {
601 [0 ... 1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0, IMC0)},
602 [2 ... 7] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN, 0, IMC0) },
603 [8] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0, IMC0) },
604 [9] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0, IMC0) },
605 [10] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0, SOCK) },
606 [11] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0, SOCK) },
607 [12 ... 49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0, SOCK) },
608};
609
610static const struct pci_id_table pci_dev_descr_knl_table[] = {
611 PCI_ID_TABLE_ENTRY(pci_dev_descr_knl, ARRAY_SIZE(pci_dev_descr_knl), 1, KNIGHTS_LANDING),
612 {0,}
613};
614
615/*
616 * Broadwell support
617 *
618 * DE processor:
619 * - 1 IMC
620 * - 2 DDR3 channels, 2 DPC per channel
621 * EP processor:
622 * - 1 or 2 IMC
623 * - 4 DDR4 channels, 3 DPC per channel
624 * EP 4S processor:
625 * - 2 IMC
626 * - 4 DDR4 channels, 3 DPC per channel
627 * EX processor:
628 * - 2 IMC
629 * - each IMC interfaces with a SMI 2 channel
630 * - each SMI channel interfaces with a scalable memory buffer
631 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
632 */
633#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
634#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
635#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
636#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
637#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM 0x6f71
638#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
639#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM 0x6f79
640#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
641#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
642#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
643#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
644#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
645#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
646#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
647#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
648#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
649#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
650#define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
651
652static const struct pci_id_descr pci_dev_descr_broadwell[] = {
653 /* first item must be the HA */
654 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0, IMC0) },
655 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1, IMC1) },
656
657 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0, IMC0) },
658 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM, 0, IMC0) },
659 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0, IMC0) },
660 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0, IMC0) },
661 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1, IMC0) },
662 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1, IMC0) },
663
664 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1, IMC1) },
665 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM, 1, IMC1) },
666 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1, IMC1) },
667 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1, IMC1) },
668 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1, IMC1) },
669 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1, IMC1) },
670
671 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0, SOCK) },
672 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0, SOCK) },
673 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1, SOCK) },
674};
675
676static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
677 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell, 10, 2, BROADWELL),
678 {0,} /* 0 terminated list. */
679};
680
681
682/****************************************************************************
683 Ancillary status routines
684 ****************************************************************************/
685
686static inline int numrank(enum type type, u32 mtr)
687{
688 int ranks = (1 << RANK_CNT_BITS(mtr));
689 int max = 4;
690
691 if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
692 max = 8;
693
694 if (ranks > max) {
695 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
696 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
697 return -EINVAL;
698 }
699
700 return ranks;
701}
702
703static inline int numrow(u32 mtr)
704{
705 int rows = (RANK_WIDTH_BITS(mtr) + 12);
706
707 if (rows < 13 || rows > 18) {
708 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
709 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
710 return -EINVAL;
711 }
712
713 return 1 << rows;
714}
715
716static inline int numcol(u32 mtr)
717{
718 int cols = (COL_WIDTH_BITS(mtr) + 10);
719
720 if (cols > 12) {
721 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
722 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
723 return -EINVAL;
724 }
725
726 return 1 << cols;
727}
728
729static struct sbridge_dev *get_sbridge_dev(u8 bus, enum domain dom, int multi_bus,
730 struct sbridge_dev *prev)
731{
732 struct sbridge_dev *sbridge_dev;
733
734 /*
735 * If we have devices scattered across several busses that pertain
736 * to the same memory controller, we'll lump them all together.
737 */
738 if (multi_bus) {
739 return list_first_entry_or_null(&sbridge_edac_list,
740 struct sbridge_dev, list);
741 }
742
743 sbridge_dev = list_entry(prev ? prev->list.next
744 : sbridge_edac_list.next, struct sbridge_dev, list);
745
746 list_for_each_entry_from(sbridge_dev, &sbridge_edac_list, list) {
747 if (sbridge_dev->bus == bus && (dom == SOCK || dom == sbridge_dev->dom))
748 return sbridge_dev;
749 }
750
751 return NULL;
752}
753
754static struct sbridge_dev *alloc_sbridge_dev(u8 bus, enum domain dom,
755 const struct pci_id_table *table)
756{
757 struct sbridge_dev *sbridge_dev;
758
759 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
760 if (!sbridge_dev)
761 return NULL;
762
763 sbridge_dev->pdev = kcalloc(table->n_devs_per_imc,
764 sizeof(*sbridge_dev->pdev),
765 GFP_KERNEL);
766 if (!sbridge_dev->pdev) {
767 kfree(sbridge_dev);
768 return NULL;
769 }
770
771 sbridge_dev->bus = bus;
772 sbridge_dev->dom = dom;
773 sbridge_dev->n_devs = table->n_devs_per_imc;
774 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
775
776 return sbridge_dev;
777}
778
779static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
780{
781 list_del(&sbridge_dev->list);
782 kfree(sbridge_dev->pdev);
783 kfree(sbridge_dev);
784}
785
786static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
787{
788 u32 reg;
789
790 /* Address range is 32:28 */
791 pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
792 return GET_TOLM(reg);
793}
794
795static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
796{
797 u32 reg;
798
799 pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
800 return GET_TOHM(reg);
801}
802
803static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
804{
805 u32 reg;
806
807 pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
808
809 return GET_TOLM(reg);
810}
811
812static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
813{
814 u32 reg;
815
816 pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
817
818 return GET_TOHM(reg);
819}
820
821static u64 rir_limit(u32 reg)
822{
823 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
824}
825
826static u64 sad_limit(u32 reg)
827{
828 return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
829}
830
831static u32 interleave_mode(u32 reg)
832{
833 return GET_BITFIELD(reg, 1, 1);
834}
835
836static u32 dram_attr(u32 reg)
837{
838 return GET_BITFIELD(reg, 2, 3);
839}
840
841static u64 knl_sad_limit(u32 reg)
842{
843 return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
844}
845
846static u32 knl_interleave_mode(u32 reg)
847{
848 return GET_BITFIELD(reg, 1, 2);
849}
850
851static const char * const knl_intlv_mode[] = {
852 "[8:6]", "[10:8]", "[14:12]", "[32:30]"
853};
854
855static const char *get_intlv_mode_str(u32 reg, enum type t)
856{
857 if (t == KNIGHTS_LANDING)
858 return knl_intlv_mode[knl_interleave_mode(reg)];
859 else
860 return interleave_mode(reg) ? "[8:6]" : "[8:6]XOR[18:16]";
861}
862
863static u32 dram_attr_knl(u32 reg)
864{
865 return GET_BITFIELD(reg, 3, 4);
866}
867
868
869static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
870{
871 u32 reg;
872 enum mem_type mtype;
873
874 if (pvt->pci_ddrio) {
875 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
876 &reg);
877 if (GET_BITFIELD(reg, 11, 11))
878 /* FIXME: Can also be LRDIMM */
879 mtype = MEM_RDDR3;
880 else
881 mtype = MEM_DDR3;
882 } else
883 mtype = MEM_UNKNOWN;
884
885 return mtype;
886}
887
888static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
889{
890 u32 reg;
891 bool registered = false;
892 enum mem_type mtype = MEM_UNKNOWN;
893
894 if (!pvt->pci_ddrio)
895 goto out;
896
897 pci_read_config_dword(pvt->pci_ddrio,
898 HASWELL_DDRCRCLKCONTROLS, &reg);
899 /* Is_Rdimm */
900 if (GET_BITFIELD(reg, 16, 16))
901 registered = true;
902
903 pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
904 if (GET_BITFIELD(reg, 14, 14)) {
905 if (registered)
906 mtype = MEM_RDDR4;
907 else
908 mtype = MEM_DDR4;
909 } else {
910 if (registered)
911 mtype = MEM_RDDR3;
912 else
913 mtype = MEM_DDR3;
914 }
915
916out:
917 return mtype;
918}
919
920static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
921{
922 /* for KNL value is fixed */
923 return DEV_X16;
924}
925
926static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
927{
928 /* there's no way to figure out */
929 return DEV_UNKNOWN;
930}
931
932static enum dev_type __ibridge_get_width(u32 mtr)
933{
934 enum dev_type type;
935
936 switch (mtr) {
937 case 3:
938 type = DEV_UNKNOWN;
939 break;
940 case 2:
941 type = DEV_X16;
942 break;
943 case 1:
944 type = DEV_X8;
945 break;
946 case 0:
947 type = DEV_X4;
948 break;
949 }
950
951 return type;
952}
953
954static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
955{
956 /*
957 * ddr3_width on the documentation but also valid for DDR4 on
958 * Haswell
959 */
960 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
961}
962
963static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
964{
965 /* ddr3_width on the documentation but also valid for DDR4 */
966 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
967}
968
969static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
970{
971 /* DDR4 RDIMMS and LRDIMMS are supported */
972 return MEM_RDDR4;
973}
974
975static u8 get_node_id(struct sbridge_pvt *pvt)
976{
977 u32 reg;
978 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
979 return GET_BITFIELD(reg, 0, 2);
980}
981
982static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
983{
984 u32 reg;
985
986 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
987 return GET_BITFIELD(reg, 0, 3);
988}
989
990static u8 knl_get_node_id(struct sbridge_pvt *pvt)
991{
992 u32 reg;
993
994 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
995 return GET_BITFIELD(reg, 0, 2);
996}
997
998
999static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
1000{
1001 u32 reg;
1002
1003 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
1004 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1005}
1006
1007static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
1008{
1009 u64 rc;
1010 u32 reg;
1011
1012 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
1013 rc = GET_BITFIELD(reg, 26, 31);
1014 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
1015 rc = ((reg << 6) | rc) << 26;
1016
1017 return rc | 0x1ffffff;
1018}
1019
1020static u64 knl_get_tolm(struct sbridge_pvt *pvt)
1021{
1022 u32 reg;
1023
1024 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
1025 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
1026}
1027
1028static u64 knl_get_tohm(struct sbridge_pvt *pvt)
1029{
1030 u64 rc;
1031 u32 reg_lo, reg_hi;
1032
1033 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
1034 pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
1035 rc = ((u64)reg_hi << 32) | reg_lo;
1036 return rc | 0x3ffffff;
1037}
1038
1039
1040static u64 haswell_rir_limit(u32 reg)
1041{
1042 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
1043}
1044
1045static inline u8 sad_pkg_socket(u8 pkg)
1046{
1047 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
1048 return ((pkg >> 3) << 2) | (pkg & 0x3);
1049}
1050
1051static inline u8 sad_pkg_ha(u8 pkg)
1052{
1053 return (pkg >> 2) & 0x1;
1054}
1055
1056static int haswell_chan_hash(int idx, u64 addr)
1057{
1058 int i;
1059
1060 /*
1061 * XOR even bits from 12:26 to bit0 of idx,
1062 * odd bits from 13:27 to bit1
1063 */
1064 for (i = 12; i < 28; i += 2)
1065 idx ^= (addr >> i) & 3;
1066
1067 return idx;
1068}
1069
1070/* Low bits of TAD limit, and some metadata. */
1071static const u32 knl_tad_dram_limit_lo[] = {
1072 0x400, 0x500, 0x600, 0x700,
1073 0x800, 0x900, 0xa00, 0xb00,
1074};
1075
1076/* Low bits of TAD offset. */
1077static const u32 knl_tad_dram_offset_lo[] = {
1078 0x404, 0x504, 0x604, 0x704,
1079 0x804, 0x904, 0xa04, 0xb04,
1080};
1081
1082/* High 16 bits of TAD limit and offset. */
1083static const u32 knl_tad_dram_hi[] = {
1084 0x408, 0x508, 0x608, 0x708,
1085 0x808, 0x908, 0xa08, 0xb08,
1086};
1087
1088/* Number of ways a tad entry is interleaved. */
1089static const u32 knl_tad_ways[] = {
1090 8, 6, 4, 3, 2, 1,
1091};
1092
1093/*
1094 * Retrieve the n'th Target Address Decode table entry
1095 * from the memory controller's TAD table.
1096 *
1097 * @pvt: driver private data
1098 * @entry: which entry you want to retrieve
1099 * @mc: which memory controller (0 or 1)
1100 * @offset: output tad range offset
1101 * @limit: output address of first byte above tad range
1102 * @ways: output number of interleave ways
1103 *
1104 * The offset value has curious semantics. It's a sort of running total
1105 * of the sizes of all the memory regions that aren't mapped in this
1106 * tad table.
1107 */
1108static int knl_get_tad(const struct sbridge_pvt *pvt,
1109 const int entry,
1110 const int mc,
1111 u64 *offset,
1112 u64 *limit,
1113 int *ways)
1114{
1115 u32 reg_limit_lo, reg_offset_lo, reg_hi;
1116 struct pci_dev *pci_mc;
1117 int way_id;
1118
1119 switch (mc) {
1120 case 0:
1121 pci_mc = pvt->knl.pci_mc0;
1122 break;
1123 case 1:
1124 pci_mc = pvt->knl.pci_mc1;
1125 break;
1126 default:
1127 WARN_ON(1);
1128 return -EINVAL;
1129 }
1130
1131 pci_read_config_dword(pci_mc,
1132 knl_tad_dram_limit_lo[entry], &reg_limit_lo);
1133 pci_read_config_dword(pci_mc,
1134 knl_tad_dram_offset_lo[entry], &reg_offset_lo);
1135 pci_read_config_dword(pci_mc,
1136 knl_tad_dram_hi[entry], &reg_hi);
1137
1138 /* Is this TAD entry enabled? */
1139 if (!GET_BITFIELD(reg_limit_lo, 0, 0))
1140 return -ENODEV;
1141
1142 way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
1143
1144 if (way_id < ARRAY_SIZE(knl_tad_ways)) {
1145 *ways = knl_tad_ways[way_id];
1146 } else {
1147 *ways = 0;
1148 sbridge_printk(KERN_ERR,
1149 "Unexpected value %d in mc_tad_limit_lo wayness field\n",
1150 way_id);
1151 return -ENODEV;
1152 }
1153
1154 /*
1155 * The least significant 6 bits of base and limit are truncated.
1156 * For limit, we fill the missing bits with 1s.
1157 */
1158 *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
1159 ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
1160 *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
1161 ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
1162
1163 return 0;
1164}
1165
1166/* Determine which memory controller is responsible for a given channel. */
1167static int knl_channel_mc(int channel)
1168{
1169 WARN_ON(channel < 0 || channel >= 6);
1170
1171 return channel < 3 ? 1 : 0;
1172}
1173
1174/*
1175 * Get the Nth entry from EDC_ROUTE_TABLE register.
1176 * (This is the per-tile mapping of logical interleave targets to
1177 * physical EDC modules.)
1178 *
1179 * entry 0: 0:2
1180 * 1: 3:5
1181 * 2: 6:8
1182 * 3: 9:11
1183 * 4: 12:14
1184 * 5: 15:17
1185 * 6: 18:20
1186 * 7: 21:23
1187 * reserved: 24:31
1188 */
1189static u32 knl_get_edc_route(int entry, u32 reg)
1190{
1191 WARN_ON(entry >= KNL_MAX_EDCS);
1192 return GET_BITFIELD(reg, entry*3, (entry*3)+2);
1193}
1194
1195/*
1196 * Get the Nth entry from MC_ROUTE_TABLE register.
1197 * (This is the per-tile mapping of logical interleave targets to
1198 * physical DRAM channels modules.)
1199 *
1200 * entry 0: mc 0:2 channel 18:19
1201 * 1: mc 3:5 channel 20:21
1202 * 2: mc 6:8 channel 22:23
1203 * 3: mc 9:11 channel 24:25
1204 * 4: mc 12:14 channel 26:27
1205 * 5: mc 15:17 channel 28:29
1206 * reserved: 30:31
1207 *
1208 * Though we have 3 bits to identify the MC, we should only see
1209 * the values 0 or 1.
1210 */
1211
1212static u32 knl_get_mc_route(int entry, u32 reg)
1213{
1214 int mc, chan;
1215
1216 WARN_ON(entry >= KNL_MAX_CHANNELS);
1217
1218 mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
1219 chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
1220
1221 return knl_channel_remap(mc, chan);
1222}
1223
1224/*
1225 * Render the EDC_ROUTE register in human-readable form.
1226 * Output string s should be at least KNL_MAX_EDCS*2 bytes.
1227 */
1228static void knl_show_edc_route(u32 reg, char *s)
1229{
1230 int i;
1231
1232 for (i = 0; i < KNL_MAX_EDCS; i++) {
1233 s[i*2] = knl_get_edc_route(i, reg) + '0';
1234 s[i*2+1] = '-';
1235 }
1236
1237 s[KNL_MAX_EDCS*2 - 1] = '\0';
1238}
1239
1240/*
1241 * Render the MC_ROUTE register in human-readable form.
1242 * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
1243 */
1244static void knl_show_mc_route(u32 reg, char *s)
1245{
1246 int i;
1247
1248 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
1249 s[i*2] = knl_get_mc_route(i, reg) + '0';
1250 s[i*2+1] = '-';
1251 }
1252
1253 s[KNL_MAX_CHANNELS*2 - 1] = '\0';
1254}
1255
1256#define KNL_EDC_ROUTE 0xb8
1257#define KNL_MC_ROUTE 0xb4
1258
1259/* Is this dram rule backed by regular DRAM in flat mode? */
1260#define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
1261
1262/* Is this dram rule cached? */
1263#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1264
1265/* Is this rule backed by edc ? */
1266#define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
1267
1268/* Is this rule backed by DRAM, cacheable in EDRAM? */
1269#define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
1270
1271/* Is this rule mod3? */
1272#define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
1273
1274/*
1275 * Figure out how big our RAM modules are.
1276 *
1277 * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
1278 * have to figure this out from the SAD rules, interleave lists, route tables,
1279 * and TAD rules.
1280 *
1281 * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
1282 * inspect the TAD rules to figure out how large the SAD regions really are.
1283 *
1284 * When we know the real size of a SAD region and how many ways it's
1285 * interleaved, we know the individual contribution of each channel to
1286 * TAD is size/ways.
1287 *
1288 * Finally, we have to check whether each channel participates in each SAD
1289 * region.
1290 *
1291 * Fortunately, KNL only supports one DIMM per channel, so once we know how
1292 * much memory the channel uses, we know the DIMM is at least that large.
1293 * (The BIOS might possibly choose not to map all available memory, in which
1294 * case we will underreport the size of the DIMM.)
1295 *
1296 * In theory, we could try to determine the EDC sizes as well, but that would
1297 * only work in flat mode, not in cache mode.
1298 *
1299 * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
1300 * elements)
1301 */
1302static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
1303{
1304 u64 sad_base, sad_size, sad_limit = 0;
1305 u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
1306 int sad_rule = 0;
1307 int tad_rule = 0;
1308 int intrlv_ways, tad_ways;
1309 u32 first_pkg, pkg;
1310 int i;
1311 u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
1312 u32 dram_rule, interleave_reg;
1313 u32 mc_route_reg[KNL_MAX_CHAS];
1314 u32 edc_route_reg[KNL_MAX_CHAS];
1315 int edram_only;
1316 char edc_route_string[KNL_MAX_EDCS*2];
1317 char mc_route_string[KNL_MAX_CHANNELS*2];
1318 int cur_reg_start;
1319 int mc;
1320 int channel;
1321 int way;
1322 int participants[KNL_MAX_CHANNELS];
1323 int participant_count = 0;
1324
1325 for (i = 0; i < KNL_MAX_CHANNELS; i++)
1326 mc_sizes[i] = 0;
1327
1328 /* Read the EDC route table in each CHA. */
1329 cur_reg_start = 0;
1330 for (i = 0; i < KNL_MAX_CHAS; i++) {
1331 pci_read_config_dword(pvt->knl.pci_cha[i],
1332 KNL_EDC_ROUTE, &edc_route_reg[i]);
1333
1334 if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
1335 knl_show_edc_route(edc_route_reg[i-1],
1336 edc_route_string);
1337 if (cur_reg_start == i-1)
1338 edac_dbg(0, "edc route table for CHA %d: %s\n",
1339 cur_reg_start, edc_route_string);
1340 else
1341 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1342 cur_reg_start, i-1, edc_route_string);
1343 cur_reg_start = i;
1344 }
1345 }
1346 knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
1347 if (cur_reg_start == i-1)
1348 edac_dbg(0, "edc route table for CHA %d: %s\n",
1349 cur_reg_start, edc_route_string);
1350 else
1351 edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
1352 cur_reg_start, i-1, edc_route_string);
1353
1354 /* Read the MC route table in each CHA. */
1355 cur_reg_start = 0;
1356 for (i = 0; i < KNL_MAX_CHAS; i++) {
1357 pci_read_config_dword(pvt->knl.pci_cha[i],
1358 KNL_MC_ROUTE, &mc_route_reg[i]);
1359
1360 if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
1361 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1362 if (cur_reg_start == i-1)
1363 edac_dbg(0, "mc route table for CHA %d: %s\n",
1364 cur_reg_start, mc_route_string);
1365 else
1366 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1367 cur_reg_start, i-1, mc_route_string);
1368 cur_reg_start = i;
1369 }
1370 }
1371 knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
1372 if (cur_reg_start == i-1)
1373 edac_dbg(0, "mc route table for CHA %d: %s\n",
1374 cur_reg_start, mc_route_string);
1375 else
1376 edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
1377 cur_reg_start, i-1, mc_route_string);
1378
1379 /* Process DRAM rules */
1380 for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
1381 /* previous limit becomes the new base */
1382 sad_base = sad_limit;
1383
1384 pci_read_config_dword(pvt->pci_sad0,
1385 pvt->info.dram_rule[sad_rule], &dram_rule);
1386
1387 if (!DRAM_RULE_ENABLE(dram_rule))
1388 break;
1389
1390 edram_only = KNL_EDRAM_ONLY(dram_rule);
1391
1392 sad_limit = pvt->info.sad_limit(dram_rule)+1;
1393 sad_size = sad_limit - sad_base;
1394
1395 pci_read_config_dword(pvt->pci_sad0,
1396 pvt->info.interleave_list[sad_rule], &interleave_reg);
1397
1398 /*
1399 * Find out how many ways this dram rule is interleaved.
1400 * We stop when we see the first channel again.
1401 */
1402 first_pkg = sad_pkg(pvt->info.interleave_pkg,
1403 interleave_reg, 0);
1404 for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
1405 pkg = sad_pkg(pvt->info.interleave_pkg,
1406 interleave_reg, intrlv_ways);
1407
1408 if ((pkg & 0x8) == 0) {
1409 /*
1410 * 0 bit means memory is non-local,
1411 * which KNL doesn't support
1412 */
1413 edac_dbg(0, "Unexpected interleave target %d\n",
1414 pkg);
1415 return -1;
1416 }
1417
1418 if (pkg == first_pkg)
1419 break;
1420 }
1421 if (KNL_MOD3(dram_rule))
1422 intrlv_ways *= 3;
1423
1424 edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
1425 sad_rule,
1426 sad_base,
1427 sad_limit,
1428 intrlv_ways,
1429 edram_only ? ", EDRAM" : "");
1430
1431 /*
1432 * Find out how big the SAD region really is by iterating
1433 * over TAD tables (SAD regions may contain holes).
1434 * Each memory controller might have a different TAD table, so
1435 * we have to look at both.
1436 *
1437 * Livespace is the memory that's mapped in this TAD table,
1438 * deadspace is the holes (this could be the MMIO hole, or it
1439 * could be memory that's mapped by the other TAD table but
1440 * not this one).
1441 */
1442 for (mc = 0; mc < 2; mc++) {
1443 sad_actual_size[mc] = 0;
1444 tad_livespace = 0;
1445 for (tad_rule = 0;
1446 tad_rule < ARRAY_SIZE(
1447 knl_tad_dram_limit_lo);
1448 tad_rule++) {
1449 if (knl_get_tad(pvt,
1450 tad_rule,
1451 mc,
1452 &tad_deadspace,
1453 &tad_limit,
1454 &tad_ways))
1455 break;
1456
1457 tad_size = (tad_limit+1) -
1458 (tad_livespace + tad_deadspace);
1459 tad_livespace += tad_size;
1460 tad_base = (tad_limit+1) - tad_size;
1461
1462 if (tad_base < sad_base) {
1463 if (tad_limit > sad_base)
1464 edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
1465 } else if (tad_base < sad_limit) {
1466 if (tad_limit+1 > sad_limit) {
1467 edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
1468 } else {
1469 /* TAD region is completely inside SAD region */
1470 edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
1471 tad_rule, tad_base,
1472 tad_limit, tad_size,
1473 mc);
1474 sad_actual_size[mc] += tad_size;
1475 }
1476 }
1477 tad_base = tad_limit+1;
1478 }
1479 }
1480
1481 for (mc = 0; mc < 2; mc++) {
1482 edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
1483 mc, sad_actual_size[mc], sad_actual_size[mc]);
1484 }
1485
1486 /* Ignore EDRAM rule */
1487 if (edram_only)
1488 continue;
1489
1490 /* Figure out which channels participate in interleave. */
1491 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
1492 participants[channel] = 0;
1493
1494 /* For each channel, does at least one CHA have
1495 * this channel mapped to the given target?
1496 */
1497 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1498 for (way = 0; way < intrlv_ways; way++) {
1499 int target;
1500 int cha;
1501
1502 if (KNL_MOD3(dram_rule))
1503 target = way;
1504 else
1505 target = 0x7 & sad_pkg(
1506 pvt->info.interleave_pkg, interleave_reg, way);
1507
1508 for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
1509 if (knl_get_mc_route(target,
1510 mc_route_reg[cha]) == channel
1511 && !participants[channel]) {
1512 participant_count++;
1513 participants[channel] = 1;
1514 break;
1515 }
1516 }
1517 }
1518 }
1519
1520 if (participant_count != intrlv_ways)
1521 edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
1522 participant_count, intrlv_ways);
1523
1524 for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
1525 mc = knl_channel_mc(channel);
1526 if (participants[channel]) {
1527 edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
1528 channel,
1529 sad_actual_size[mc]/intrlv_ways,
1530 sad_rule);
1531 mc_sizes[channel] +=
1532 sad_actual_size[mc]/intrlv_ways;
1533 }
1534 }
1535 }
1536
1537 return 0;
1538}
1539
1540static void get_source_id(struct mem_ctl_info *mci)
1541{
1542 struct sbridge_pvt *pvt = mci->pvt_info;
1543 u32 reg;
1544
1545 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
1546 pvt->info.type == KNIGHTS_LANDING)
1547 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
1548 else
1549 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
1550
1551 if (pvt->info.type == KNIGHTS_LANDING)
1552 pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
1553 else
1554 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
1555}
1556
1557static int __populate_dimms(struct mem_ctl_info *mci,
1558 u64 knl_mc_sizes[KNL_MAX_CHANNELS],
1559 enum edac_type mode)
1560{
1561 struct sbridge_pvt *pvt = mci->pvt_info;
1562 int channels = pvt->info.type == KNIGHTS_LANDING ? KNL_MAX_CHANNELS
1563 : NUM_CHANNELS;
1564 unsigned int i, j, banks, ranks, rows, cols, npages;
1565 struct dimm_info *dimm;
1566 enum mem_type mtype;
1567 u64 size;
1568
1569 mtype = pvt->info.get_memory_type(pvt);
1570 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
1571 edac_dbg(0, "Memory is registered\n");
1572 else if (mtype == MEM_UNKNOWN)
1573 edac_dbg(0, "Cannot determine memory type\n");
1574 else
1575 edac_dbg(0, "Memory is unregistered\n");
1576
1577 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
1578 banks = 16;
1579 else
1580 banks = 8;
1581
1582 for (i = 0; i < channels; i++) {
1583 u32 mtr;
1584
1585 int max_dimms_per_channel;
1586
1587 if (pvt->info.type == KNIGHTS_LANDING) {
1588 max_dimms_per_channel = 1;
1589 if (!pvt->knl.pci_channel[i])
1590 continue;
1591 } else {
1592 max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
1593 if (!pvt->pci_tad[i])
1594 continue;
1595 }
1596
1597 for (j = 0; j < max_dimms_per_channel; j++) {
1598 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers, i, j, 0);
1599 if (pvt->info.type == KNIGHTS_LANDING) {
1600 pci_read_config_dword(pvt->knl.pci_channel[i],
1601 knl_mtr_reg, &mtr);
1602 } else {
1603 pci_read_config_dword(pvt->pci_tad[i],
1604 mtr_regs[j], &mtr);
1605 }
1606 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
1607 if (IS_DIMM_PRESENT(mtr)) {
1608 if (!IS_ECC_ENABLED(pvt->info.mcmtr)) {
1609 sbridge_printk(KERN_ERR, "CPU SrcID #%d, Ha #%d, Channel #%d has DIMMs, but ECC is disabled\n",
1610 pvt->sbridge_dev->source_id,
1611 pvt->sbridge_dev->dom, i);
1612 return -ENODEV;
1613 }
1614 pvt->channel[i].dimms++;
1615
1616 ranks = numrank(pvt->info.type, mtr);
1617
1618 if (pvt->info.type == KNIGHTS_LANDING) {
1619 /* For DDR4, this is fixed. */
1620 cols = 1 << 10;
1621 rows = knl_mc_sizes[i] /
1622 ((u64) cols * ranks * banks * 8);
1623 } else {
1624 rows = numrow(mtr);
1625 cols = numcol(mtr);
1626 }
1627
1628 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
1629 npages = MiB_TO_PAGES(size);
1630
1631 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1632 pvt->sbridge_dev->mc, pvt->sbridge_dev->dom, i, j,
1633 size, npages,
1634 banks, ranks, rows, cols);
1635
1636 dimm->nr_pages = npages;
1637 dimm->grain = 32;
1638 dimm->dtype = pvt->info.get_width(pvt, mtr);
1639 dimm->mtype = mtype;
1640 dimm->edac_mode = mode;
1641 snprintf(dimm->label, sizeof(dimm->label),
1642 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1643 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom, i, j);
1644 }
1645 }
1646 }
1647
1648 return 0;
1649}
1650
1651static int get_dimm_config(struct mem_ctl_info *mci)
1652{
1653 struct sbridge_pvt *pvt = mci->pvt_info;
1654 u64 knl_mc_sizes[KNL_MAX_CHANNELS];
1655 enum edac_type mode;
1656 u32 reg;
1657
1658 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
1659 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
1660 pvt->sbridge_dev->mc,
1661 pvt->sbridge_dev->node_id,
1662 pvt->sbridge_dev->source_id);
1663
1664 /* KNL doesn't support mirroring or lockstep,
1665 * and is always closed page
1666 */
1667 if (pvt->info.type == KNIGHTS_LANDING) {
1668 mode = EDAC_S4ECD4ED;
1669 pvt->mirror_mode = NON_MIRRORING;
1670 pvt->is_cur_addr_mirrored = false;
1671
1672 if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
1673 return -1;
1674 if (pci_read_config_dword(pvt->pci_ta, KNL_MCMTR, &pvt->info.mcmtr)) {
1675 edac_dbg(0, "Failed to read KNL_MCMTR register\n");
1676 return -ENODEV;
1677 }
1678 } else {
1679 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1680 if (pci_read_config_dword(pvt->pci_ha, HASWELL_HASYSDEFEATURE2, &reg)) {
1681 edac_dbg(0, "Failed to read HASWELL_HASYSDEFEATURE2 register\n");
1682 return -ENODEV;
1683 }
1684 pvt->is_chan_hash = GET_BITFIELD(reg, 21, 21);
1685 if (GET_BITFIELD(reg, 28, 28)) {
1686 pvt->mirror_mode = ADDR_RANGE_MIRRORING;
1687 edac_dbg(0, "Address range partial memory mirroring is enabled\n");
1688 goto next;
1689 }
1690 }
1691 if (pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg)) {
1692 edac_dbg(0, "Failed to read RASENABLES register\n");
1693 return -ENODEV;
1694 }
1695 if (IS_MIRROR_ENABLED(reg)) {
1696 pvt->mirror_mode = FULL_MIRRORING;
1697 edac_dbg(0, "Full memory mirroring is enabled\n");
1698 } else {
1699 pvt->mirror_mode = NON_MIRRORING;
1700 edac_dbg(0, "Memory mirroring is disabled\n");
1701 }
1702
1703next:
1704 if (pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr)) {
1705 edac_dbg(0, "Failed to read MCMTR register\n");
1706 return -ENODEV;
1707 }
1708 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
1709 edac_dbg(0, "Lockstep is enabled\n");
1710 mode = EDAC_S8ECD8ED;
1711 pvt->is_lockstep = true;
1712 } else {
1713 edac_dbg(0, "Lockstep is disabled\n");
1714 mode = EDAC_S4ECD4ED;
1715 pvt->is_lockstep = false;
1716 }
1717 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
1718 edac_dbg(0, "address map is on closed page mode\n");
1719 pvt->is_close_pg = true;
1720 } else {
1721 edac_dbg(0, "address map is on open page mode\n");
1722 pvt->is_close_pg = false;
1723 }
1724 }
1725
1726 return __populate_dimms(mci, knl_mc_sizes, mode);
1727}
1728
1729static void get_memory_layout(const struct mem_ctl_info *mci)
1730{
1731 struct sbridge_pvt *pvt = mci->pvt_info;
1732 int i, j, k, n_sads, n_tads, sad_interl;
1733 u32 reg;
1734 u64 limit, prv = 0;
1735 u64 tmp_mb;
1736 u32 gb, mb;
1737 u32 rir_way;
1738
1739 /*
1740 * Step 1) Get TOLM/TOHM ranges
1741 */
1742
1743 pvt->tolm = pvt->info.get_tolm(pvt);
1744 tmp_mb = (1 + pvt->tolm) >> 20;
1745
1746 gb = div_u64_rem(tmp_mb, 1024, &mb);
1747 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1748 gb, (mb*1000)/1024, (u64)pvt->tolm);
1749
1750 /* Address range is already 45:25 */
1751 pvt->tohm = pvt->info.get_tohm(pvt);
1752 tmp_mb = (1 + pvt->tohm) >> 20;
1753
1754 gb = div_u64_rem(tmp_mb, 1024, &mb);
1755 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1756 gb, (mb*1000)/1024, (u64)pvt->tohm);
1757
1758 /*
1759 * Step 2) Get SAD range and SAD Interleave list
1760 * TAD registers contain the interleave wayness. However, it
1761 * seems simpler to just discover it indirectly, with the
1762 * algorithm bellow.
1763 */
1764 prv = 0;
1765 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1766 /* SAD_LIMIT Address range is 45:26 */
1767 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1768 &reg);
1769 limit = pvt->info.sad_limit(reg);
1770
1771 if (!DRAM_RULE_ENABLE(reg))
1772 continue;
1773
1774 if (limit <= prv)
1775 break;
1776
1777 tmp_mb = (limit + 1) >> 20;
1778 gb = div_u64_rem(tmp_mb, 1024, &mb);
1779 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1780 n_sads,
1781 show_dram_attr(pvt->info.dram_attr(reg)),
1782 gb, (mb*1000)/1024,
1783 ((u64)tmp_mb) << 20L,
1784 get_intlv_mode_str(reg, pvt->info.type),
1785 reg);
1786 prv = limit;
1787
1788 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1789 &reg);
1790 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1791 for (j = 0; j < 8; j++) {
1792 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1793 if (j > 0 && sad_interl == pkg)
1794 break;
1795
1796 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
1797 n_sads, j, pkg);
1798 }
1799 }
1800
1801 if (pvt->info.type == KNIGHTS_LANDING)
1802 return;
1803
1804 /*
1805 * Step 3) Get TAD range
1806 */
1807 prv = 0;
1808 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1809 pci_read_config_dword(pvt->pci_ha, tad_dram_rule[n_tads], &reg);
1810 limit = TAD_LIMIT(reg);
1811 if (limit <= prv)
1812 break;
1813 tmp_mb = (limit + 1) >> 20;
1814
1815 gb = div_u64_rem(tmp_mb, 1024, &mb);
1816 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",
1817 n_tads, gb, (mb*1000)/1024,
1818 ((u64)tmp_mb) << 20L,
1819 (u32)(1 << TAD_SOCK(reg)),
1820 (u32)TAD_CH(reg) + 1,
1821 (u32)TAD_TGT0(reg),
1822 (u32)TAD_TGT1(reg),
1823 (u32)TAD_TGT2(reg),
1824 (u32)TAD_TGT3(reg),
1825 reg);
1826 prv = limit;
1827 }
1828
1829 /*
1830 * Step 4) Get TAD offsets, per each channel
1831 */
1832 for (i = 0; i < NUM_CHANNELS; i++) {
1833 if (!pvt->channel[i].dimms)
1834 continue;
1835 for (j = 0; j < n_tads; j++) {
1836 pci_read_config_dword(pvt->pci_tad[i],
1837 tad_ch_nilv_offset[j],
1838 &reg);
1839 tmp_mb = TAD_OFFSET(reg) >> 20;
1840 gb = div_u64_rem(tmp_mb, 1024, &mb);
1841 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1842 i, j,
1843 gb, (mb*1000)/1024,
1844 ((u64)tmp_mb) << 20L,
1845 reg);
1846 }
1847 }
1848
1849 /*
1850 * Step 6) Get RIR Wayness/Limit, per each channel
1851 */
1852 for (i = 0; i < NUM_CHANNELS; i++) {
1853 if (!pvt->channel[i].dimms)
1854 continue;
1855 for (j = 0; j < MAX_RIR_RANGES; j++) {
1856 pci_read_config_dword(pvt->pci_tad[i],
1857 rir_way_limit[j],
1858 &reg);
1859
1860 if (!IS_RIR_VALID(reg))
1861 continue;
1862
1863 tmp_mb = pvt->info.rir_limit(reg) >> 20;
1864 rir_way = 1 << RIR_WAY(reg);
1865 gb = div_u64_rem(tmp_mb, 1024, &mb);
1866 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1867 i, j,
1868 gb, (mb*1000)/1024,
1869 ((u64)tmp_mb) << 20L,
1870 rir_way,
1871 reg);
1872
1873 for (k = 0; k < rir_way; k++) {
1874 pci_read_config_dword(pvt->pci_tad[i],
1875 rir_offset[j][k],
1876 &reg);
1877 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
1878
1879 gb = div_u64_rem(tmp_mb, 1024, &mb);
1880 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1881 i, j, k,
1882 gb, (mb*1000)/1024,
1883 ((u64)tmp_mb) << 20L,
1884 (u32)RIR_RNK_TGT(pvt->info.type, reg),
1885 reg);
1886 }
1887 }
1888 }
1889}
1890
1891static struct mem_ctl_info *get_mci_for_node_id(u8 node_id, u8 ha)
1892{
1893 struct sbridge_dev *sbridge_dev;
1894
1895 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1896 if (sbridge_dev->node_id == node_id && sbridge_dev->dom == ha)
1897 return sbridge_dev->mci;
1898 }
1899 return NULL;
1900}
1901
1902static int get_memory_error_data(struct mem_ctl_info *mci,
1903 u64 addr,
1904 u8 *socket, u8 *ha,
1905 long *channel_mask,
1906 u8 *rank,
1907 char **area_type, char *msg)
1908{
1909 struct mem_ctl_info *new_mci;
1910 struct sbridge_pvt *pvt = mci->pvt_info;
1911 struct pci_dev *pci_ha;
1912 int n_rir, n_sads, n_tads, sad_way, sck_xch;
1913 int sad_interl, idx, base_ch;
1914 int interleave_mode, shiftup = 0;
1915 unsigned sad_interleave[pvt->info.max_interleave];
1916 u32 reg, dram_rule;
1917 u8 ch_way, sck_way, pkg, sad_ha = 0;
1918 u32 tad_offset;
1919 u32 rir_way;
1920 u32 mb, gb;
1921 u64 ch_addr, offset, limit = 0, prv = 0;
1922
1923
1924 /*
1925 * Step 0) Check if the address is at special memory ranges
1926 * The check bellow is probably enough to fill all cases where
1927 * the error is not inside a memory, except for the legacy
1928 * range (e. g. VGA addresses). It is unlikely, however, that the
1929 * memory controller would generate an error on that range.
1930 */
1931 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
1932 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
1933 return -EINVAL;
1934 }
1935 if (addr >= (u64)pvt->tohm) {
1936 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
1937 return -EINVAL;
1938 }
1939
1940 /*
1941 * Step 1) Get socket
1942 */
1943 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1944 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1945 &reg);
1946
1947 if (!DRAM_RULE_ENABLE(reg))
1948 continue;
1949
1950 limit = pvt->info.sad_limit(reg);
1951 if (limit <= prv) {
1952 sprintf(msg, "Can't discover the memory socket");
1953 return -EINVAL;
1954 }
1955 if (addr <= limit)
1956 break;
1957 prv = limit;
1958 }
1959 if (n_sads == pvt->info.max_sad) {
1960 sprintf(msg, "Can't discover the memory socket");
1961 return -EINVAL;
1962 }
1963 dram_rule = reg;
1964 *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
1965 interleave_mode = pvt->info.interleave_mode(dram_rule);
1966
1967 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1968 &reg);
1969
1970 if (pvt->info.type == SANDY_BRIDGE) {
1971 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1972 for (sad_way = 0; sad_way < 8; sad_way++) {
1973 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1974 if (sad_way > 0 && sad_interl == pkg)
1975 break;
1976 sad_interleave[sad_way] = pkg;
1977 edac_dbg(0, "SAD interleave #%d: %d\n",
1978 sad_way, sad_interleave[sad_way]);
1979 }
1980 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1981 pvt->sbridge_dev->mc,
1982 n_sads,
1983 addr,
1984 limit,
1985 sad_way + 7,
1986 !interleave_mode ? "" : "XOR[18:16]");
1987 if (interleave_mode)
1988 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1989 else
1990 idx = (addr >> 6) & 7;
1991 switch (sad_way) {
1992 case 1:
1993 idx = 0;
1994 break;
1995 case 2:
1996 idx = idx & 1;
1997 break;
1998 case 4:
1999 idx = idx & 3;
2000 break;
2001 case 8:
2002 break;
2003 default:
2004 sprintf(msg, "Can't discover socket interleave");
2005 return -EINVAL;
2006 }
2007 *socket = sad_interleave[idx];
2008 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
2009 idx, sad_way, *socket);
2010 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
2011 int bits, a7mode = A7MODE(dram_rule);
2012
2013 if (a7mode) {
2014 /* A7 mode swaps P9 with P6 */
2015 bits = GET_BITFIELD(addr, 7, 8) << 1;
2016 bits |= GET_BITFIELD(addr, 9, 9);
2017 } else
2018 bits = GET_BITFIELD(addr, 6, 8);
2019
2020 if (interleave_mode == 0) {
2021 /* interleave mode will XOR {8,7,6} with {18,17,16} */
2022 idx = GET_BITFIELD(addr, 16, 18);
2023 idx ^= bits;
2024 } else
2025 idx = bits;
2026
2027 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2028 *socket = sad_pkg_socket(pkg);
2029 sad_ha = sad_pkg_ha(pkg);
2030
2031 if (a7mode) {
2032 /* MCChanShiftUpEnable */
2033 pci_read_config_dword(pvt->pci_ha, HASWELL_HASYSDEFEATURE2, &reg);
2034 shiftup = GET_BITFIELD(reg, 22, 22);
2035 }
2036
2037 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
2038 idx, *socket, sad_ha, shiftup);
2039 } else {
2040 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
2041 idx = (addr >> 6) & 7;
2042 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
2043 *socket = sad_pkg_socket(pkg);
2044 sad_ha = sad_pkg_ha(pkg);
2045 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
2046 idx, *socket, sad_ha);
2047 }
2048
2049 *ha = sad_ha;
2050
2051 /*
2052 * Move to the proper node structure, in order to access the
2053 * right PCI registers
2054 */
2055 new_mci = get_mci_for_node_id(*socket, sad_ha);
2056 if (!new_mci) {
2057 sprintf(msg, "Struct for socket #%u wasn't initialized",
2058 *socket);
2059 return -EINVAL;
2060 }
2061 mci = new_mci;
2062 pvt = mci->pvt_info;
2063
2064 /*
2065 * Step 2) Get memory channel
2066 */
2067 prv = 0;
2068 pci_ha = pvt->pci_ha;
2069 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
2070 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
2071 limit = TAD_LIMIT(reg);
2072 if (limit <= prv) {
2073 sprintf(msg, "Can't discover the memory channel");
2074 return -EINVAL;
2075 }
2076 if (addr <= limit)
2077 break;
2078 prv = limit;
2079 }
2080 if (n_tads == MAX_TAD) {
2081 sprintf(msg, "Can't discover the memory channel");
2082 return -EINVAL;
2083 }
2084
2085 ch_way = TAD_CH(reg) + 1;
2086 sck_way = TAD_SOCK(reg);
2087
2088 if (ch_way == 3)
2089 idx = addr >> 6;
2090 else {
2091 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
2092 if (pvt->is_chan_hash)
2093 idx = haswell_chan_hash(idx, addr);
2094 }
2095 idx = idx % ch_way;
2096
2097 /*
2098 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
2099 */
2100 switch (idx) {
2101 case 0:
2102 base_ch = TAD_TGT0(reg);
2103 break;
2104 case 1:
2105 base_ch = TAD_TGT1(reg);
2106 break;
2107 case 2:
2108 base_ch = TAD_TGT2(reg);
2109 break;
2110 case 3:
2111 base_ch = TAD_TGT3(reg);
2112 break;
2113 default:
2114 sprintf(msg, "Can't discover the TAD target");
2115 return -EINVAL;
2116 }
2117 *channel_mask = 1 << base_ch;
2118
2119 pci_read_config_dword(pvt->pci_tad[base_ch], tad_ch_nilv_offset[n_tads], &tad_offset);
2120
2121 if (pvt->mirror_mode == FULL_MIRRORING ||
2122 (pvt->mirror_mode == ADDR_RANGE_MIRRORING && n_tads == 0)) {
2123 *channel_mask |= 1 << ((base_ch + 2) % 4);
2124 switch(ch_way) {
2125 case 2:
2126 case 4:
2127 sck_xch = (1 << sck_way) * (ch_way >> 1);
2128 break;
2129 default:
2130 sprintf(msg, "Invalid mirror set. Can't decode addr");
2131 return -EINVAL;
2132 }
2133
2134 pvt->is_cur_addr_mirrored = true;
2135 } else {
2136 sck_xch = (1 << sck_way) * ch_way;
2137 pvt->is_cur_addr_mirrored = false;
2138 }
2139
2140 if (pvt->is_lockstep)
2141 *channel_mask |= 1 << ((base_ch + 1) % 4);
2142
2143 offset = TAD_OFFSET(tad_offset);
2144
2145 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",
2146 n_tads,
2147 addr,
2148 limit,
2149 sck_way,
2150 ch_way,
2151 offset,
2152 idx,
2153 base_ch,
2154 *channel_mask);
2155
2156 /* Calculate channel address */
2157 /* Remove the TAD offset */
2158
2159 if (offset > addr) {
2160 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
2161 offset, addr);
2162 return -EINVAL;
2163 }
2164
2165 ch_addr = addr - offset;
2166 ch_addr >>= (6 + shiftup);
2167 ch_addr /= sck_xch;
2168 ch_addr <<= (6 + shiftup);
2169 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
2170
2171 /*
2172 * Step 3) Decode rank
2173 */
2174 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
2175 pci_read_config_dword(pvt->pci_tad[base_ch], rir_way_limit[n_rir], &reg);
2176
2177 if (!IS_RIR_VALID(reg))
2178 continue;
2179
2180 limit = pvt->info.rir_limit(reg);
2181 gb = div_u64_rem(limit >> 20, 1024, &mb);
2182 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
2183 n_rir,
2184 gb, (mb*1000)/1024,
2185 limit,
2186 1 << RIR_WAY(reg));
2187 if (ch_addr <= limit)
2188 break;
2189 }
2190 if (n_rir == MAX_RIR_RANGES) {
2191 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
2192 ch_addr);
2193 return -EINVAL;
2194 }
2195 rir_way = RIR_WAY(reg);
2196
2197 if (pvt->is_close_pg)
2198 idx = (ch_addr >> 6);
2199 else
2200 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
2201 idx %= 1 << rir_way;
2202
2203 pci_read_config_dword(pvt->pci_tad[base_ch], rir_offset[n_rir][idx], &reg);
2204 *rank = RIR_RNK_TGT(pvt->info.type, reg);
2205
2206 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
2207 n_rir,
2208 ch_addr,
2209 limit,
2210 rir_way,
2211 idx);
2212
2213 return 0;
2214}
2215
2216/****************************************************************************
2217 Device initialization routines: put/get, init/exit
2218 ****************************************************************************/
2219
2220/*
2221 * sbridge_put_all_devices 'put' all the devices that we have
2222 * reserved via 'get'
2223 */
2224static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
2225{
2226 int i;
2227
2228 edac_dbg(0, "\n");
2229 for (i = 0; i < sbridge_dev->n_devs; i++) {
2230 struct pci_dev *pdev = sbridge_dev->pdev[i];
2231 if (!pdev)
2232 continue;
2233 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
2234 pdev->bus->number,
2235 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2236 pci_dev_put(pdev);
2237 }
2238}
2239
2240static void sbridge_put_all_devices(void)
2241{
2242 struct sbridge_dev *sbridge_dev, *tmp;
2243
2244 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
2245 sbridge_put_devices(sbridge_dev);
2246 free_sbridge_dev(sbridge_dev);
2247 }
2248}
2249
2250static int sbridge_get_onedevice(struct pci_dev **prev,
2251 u8 *num_mc,
2252 const struct pci_id_table *table,
2253 const unsigned devno,
2254 const int multi_bus)
2255{
2256 struct sbridge_dev *sbridge_dev = NULL;
2257 const struct pci_id_descr *dev_descr = &table->descr[devno];
2258 struct pci_dev *pdev = NULL;
2259 u8 bus = 0;
2260 int i = 0;
2261
2262 sbridge_printk(KERN_DEBUG,
2263 "Seeking for: PCI ID %04x:%04x\n",
2264 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2265
2266 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2267 dev_descr->dev_id, *prev);
2268
2269 if (!pdev) {
2270 if (*prev) {
2271 *prev = pdev;
2272 return 0;
2273 }
2274
2275 if (dev_descr->optional)
2276 return 0;
2277
2278 /* if the HA wasn't found */
2279 if (devno == 0)
2280 return -ENODEV;
2281
2282 sbridge_printk(KERN_INFO,
2283 "Device not found: %04x:%04x\n",
2284 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2285
2286 /* End of list, leave */
2287 return -ENODEV;
2288 }
2289 bus = pdev->bus->number;
2290
2291next_imc:
2292 sbridge_dev = get_sbridge_dev(bus, dev_descr->dom, multi_bus, sbridge_dev);
2293 if (!sbridge_dev) {
2294 /* If the HA1 wasn't found, don't create EDAC second memory controller */
2295 if (dev_descr->dom == IMC1 && devno != 1) {
2296 edac_dbg(0, "Skip IMC1: %04x:%04x (since HA1 was absent)\n",
2297 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2298 pci_dev_put(pdev);
2299 return 0;
2300 }
2301
2302 if (dev_descr->dom == SOCK)
2303 goto out_imc;
2304
2305 sbridge_dev = alloc_sbridge_dev(bus, dev_descr->dom, table);
2306 if (!sbridge_dev) {
2307 pci_dev_put(pdev);
2308 return -ENOMEM;
2309 }
2310 (*num_mc)++;
2311 }
2312
2313 if (sbridge_dev->pdev[sbridge_dev->i_devs]) {
2314 sbridge_printk(KERN_ERR,
2315 "Duplicated device for %04x:%04x\n",
2316 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2317 pci_dev_put(pdev);
2318 return -ENODEV;
2319 }
2320
2321 sbridge_dev->pdev[sbridge_dev->i_devs++] = pdev;
2322
2323 /* pdev belongs to more than one IMC, do extra gets */
2324 if (++i > 1)
2325 pci_dev_get(pdev);
2326
2327 if (dev_descr->dom == SOCK && i < table->n_imcs_per_sock)
2328 goto next_imc;
2329
2330out_imc:
2331 /* Be sure that the device is enabled */
2332 if (unlikely(pci_enable_device(pdev) < 0)) {
2333 sbridge_printk(KERN_ERR,
2334 "Couldn't enable %04x:%04x\n",
2335 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2336 return -ENODEV;
2337 }
2338
2339 edac_dbg(0, "Detected %04x:%04x\n",
2340 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
2341
2342 /*
2343 * As stated on drivers/pci/search.c, the reference count for
2344 * @from is always decremented if it is not %NULL. So, as we need
2345 * to get all devices up to null, we need to do a get for the device
2346 */
2347 pci_dev_get(pdev);
2348
2349 *prev = pdev;
2350
2351 return 0;
2352}
2353
2354/*
2355 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
2356 * devices we want to reference for this driver.
2357 * @num_mc: pointer to the memory controllers count, to be incremented in case
2358 * of success.
2359 * @table: model specific table
2360 *
2361 * returns 0 in case of success or error code
2362 */
2363static int sbridge_get_all_devices(u8 *num_mc,
2364 const struct pci_id_table *table)
2365{
2366 int i, rc;
2367 struct pci_dev *pdev = NULL;
2368 int allow_dups = 0;
2369 int multi_bus = 0;
2370
2371 if (table->type == KNIGHTS_LANDING)
2372 allow_dups = multi_bus = 1;
2373 while (table && table->descr) {
2374 for (i = 0; i < table->n_devs_per_sock; i++) {
2375 if (!allow_dups || i == 0 ||
2376 table->descr[i].dev_id !=
2377 table->descr[i-1].dev_id) {
2378 pdev = NULL;
2379 }
2380 do {
2381 rc = sbridge_get_onedevice(&pdev, num_mc,
2382 table, i, multi_bus);
2383 if (rc < 0) {
2384 if (i == 0) {
2385 i = table->n_devs_per_sock;
2386 break;
2387 }
2388 sbridge_put_all_devices();
2389 return -ENODEV;
2390 }
2391 } while (pdev && !allow_dups);
2392 }
2393 table++;
2394 }
2395
2396 return 0;
2397}
2398
2399/*
2400 * Device IDs for {SBRIDGE,IBRIDGE,HASWELL,BROADWELL}_IMC_HA0_TAD0 are in
2401 * the format: XXXa. So we can convert from a device to the corresponding
2402 * channel like this
2403 */
2404#define TAD_DEV_TO_CHAN(dev) (((dev) & 0xf) - 0xa)
2405
2406static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
2407 struct sbridge_dev *sbridge_dev)
2408{
2409 struct sbridge_pvt *pvt = mci->pvt_info;
2410 struct pci_dev *pdev;
2411 u8 saw_chan_mask = 0;
2412 int i;
2413
2414 for (i = 0; i < sbridge_dev->n_devs; i++) {
2415 pdev = sbridge_dev->pdev[i];
2416 if (!pdev)
2417 continue;
2418
2419 switch (pdev->device) {
2420 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
2421 pvt->pci_sad0 = pdev;
2422 break;
2423 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
2424 pvt->pci_sad1 = pdev;
2425 break;
2426 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
2427 pvt->pci_br0 = pdev;
2428 break;
2429 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2430 pvt->pci_ha = pdev;
2431 break;
2432 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
2433 pvt->pci_ta = pdev;
2434 break;
2435 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
2436 pvt->pci_ras = pdev;
2437 break;
2438 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
2439 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
2440 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
2441 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
2442 {
2443 int id = TAD_DEV_TO_CHAN(pdev->device);
2444 pvt->pci_tad[id] = pdev;
2445 saw_chan_mask |= 1 << id;
2446 }
2447 break;
2448 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
2449 pvt->pci_ddrio = pdev;
2450 break;
2451 default:
2452 goto error;
2453 }
2454
2455 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
2456 pdev->vendor, pdev->device,
2457 sbridge_dev->bus,
2458 pdev);
2459 }
2460
2461 /* Check if everything were registered */
2462 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha ||
2463 !pvt->pci_ras || !pvt->pci_ta)
2464 goto enodev;
2465
2466 if (saw_chan_mask != 0x0f)
2467 goto enodev;
2468 return 0;
2469
2470enodev:
2471 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2472 return -ENODEV;
2473
2474error:
2475 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
2476 PCI_VENDOR_ID_INTEL, pdev->device);
2477 return -EINVAL;
2478}
2479
2480static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
2481 struct sbridge_dev *sbridge_dev)
2482{
2483 struct sbridge_pvt *pvt = mci->pvt_info;
2484 struct pci_dev *pdev;
2485 u8 saw_chan_mask = 0;
2486 int i;
2487
2488 for (i = 0; i < sbridge_dev->n_devs; i++) {
2489 pdev = sbridge_dev->pdev[i];
2490 if (!pdev)
2491 continue;
2492
2493 switch (pdev->device) {
2494 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
2495 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
2496 pvt->pci_ha = pdev;
2497 break;
2498 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2499 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA:
2500 pvt->pci_ta = pdev;
2501 break;
2502 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
2503 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS:
2504 pvt->pci_ras = pdev;
2505 break;
2506 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
2507 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
2508 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
2509 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
2510 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
2511 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
2512 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
2513 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
2514 {
2515 int id = TAD_DEV_TO_CHAN(pdev->device);
2516 pvt->pci_tad[id] = pdev;
2517 saw_chan_mask |= 1 << id;
2518 }
2519 break;
2520 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
2521 pvt->pci_ddrio = pdev;
2522 break;
2523 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
2524 pvt->pci_ddrio = pdev;
2525 break;
2526 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
2527 pvt->pci_sad0 = pdev;
2528 break;
2529 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
2530 pvt->pci_br0 = pdev;
2531 break;
2532 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
2533 pvt->pci_br1 = pdev;
2534 break;
2535 default:
2536 goto error;
2537 }
2538
2539 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2540 sbridge_dev->bus,
2541 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2542 pdev);
2543 }
2544
2545 /* Check if everything were registered */
2546 if (!pvt->pci_sad0 || !pvt->pci_ha || !pvt->pci_br0 ||
2547 !pvt->pci_br1 || !pvt->pci_ras || !pvt->pci_ta)
2548 goto enodev;
2549
2550 if (saw_chan_mask != 0x0f && /* -EN/-EX */
2551 saw_chan_mask != 0x03) /* -EP */
2552 goto enodev;
2553 return 0;
2554
2555enodev:
2556 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2557 return -ENODEV;
2558
2559error:
2560 sbridge_printk(KERN_ERR,
2561 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
2562 pdev->device);
2563 return -EINVAL;
2564}
2565
2566static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
2567 struct sbridge_dev *sbridge_dev)
2568{
2569 struct sbridge_pvt *pvt = mci->pvt_info;
2570 struct pci_dev *pdev;
2571 u8 saw_chan_mask = 0;
2572 int i;
2573
2574 /* there's only one device per system; not tied to any bus */
2575 if (pvt->info.pci_vtd == NULL)
2576 /* result will be checked later */
2577 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2578 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
2579 NULL);
2580
2581 for (i = 0; i < sbridge_dev->n_devs; i++) {
2582 pdev = sbridge_dev->pdev[i];
2583 if (!pdev)
2584 continue;
2585
2586 switch (pdev->device) {
2587 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
2588 pvt->pci_sad0 = pdev;
2589 break;
2590 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
2591 pvt->pci_sad1 = pdev;
2592 break;
2593 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2594 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
2595 pvt->pci_ha = pdev;
2596 break;
2597 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
2598 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
2599 pvt->pci_ta = pdev;
2600 break;
2601 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TM:
2602 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TM:
2603 pvt->pci_ras = pdev;
2604 break;
2605 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
2606 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
2607 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
2608 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
2609 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
2610 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
2611 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
2612 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
2613 {
2614 int id = TAD_DEV_TO_CHAN(pdev->device);
2615 pvt->pci_tad[id] = pdev;
2616 saw_chan_mask |= 1 << id;
2617 }
2618 break;
2619 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
2620 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
2621 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
2622 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
2623 if (!pvt->pci_ddrio)
2624 pvt->pci_ddrio = pdev;
2625 break;
2626 default:
2627 break;
2628 }
2629
2630 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2631 sbridge_dev->bus,
2632 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2633 pdev);
2634 }
2635
2636 /* Check if everything were registered */
2637 if (!pvt->pci_sad0 || !pvt->pci_ha || !pvt->pci_sad1 ||
2638 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2639 goto enodev;
2640
2641 if (saw_chan_mask != 0x0f && /* -EN/-EX */
2642 saw_chan_mask != 0x03) /* -EP */
2643 goto enodev;
2644 return 0;
2645
2646enodev:
2647 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2648 return -ENODEV;
2649}
2650
2651static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
2652 struct sbridge_dev *sbridge_dev)
2653{
2654 struct sbridge_pvt *pvt = mci->pvt_info;
2655 struct pci_dev *pdev;
2656 u8 saw_chan_mask = 0;
2657 int i;
2658
2659 /* there's only one device per system; not tied to any bus */
2660 if (pvt->info.pci_vtd == NULL)
2661 /* result will be checked later */
2662 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
2663 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
2664 NULL);
2665
2666 for (i = 0; i < sbridge_dev->n_devs; i++) {
2667 pdev = sbridge_dev->pdev[i];
2668 if (!pdev)
2669 continue;
2670
2671 switch (pdev->device) {
2672 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
2673 pvt->pci_sad0 = pdev;
2674 break;
2675 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
2676 pvt->pci_sad1 = pdev;
2677 break;
2678 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2679 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2680 pvt->pci_ha = pdev;
2681 break;
2682 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
2683 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2684 pvt->pci_ta = pdev;
2685 break;
2686 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TM:
2687 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TM:
2688 pvt->pci_ras = pdev;
2689 break;
2690 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
2691 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
2692 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
2693 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
2694 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
2695 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
2696 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
2697 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
2698 {
2699 int id = TAD_DEV_TO_CHAN(pdev->device);
2700 pvt->pci_tad[id] = pdev;
2701 saw_chan_mask |= 1 << id;
2702 }
2703 break;
2704 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2705 pvt->pci_ddrio = pdev;
2706 break;
2707 default:
2708 break;
2709 }
2710
2711 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2712 sbridge_dev->bus,
2713 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2714 pdev);
2715 }
2716
2717 /* Check if everything were registered */
2718 if (!pvt->pci_sad0 || !pvt->pci_ha || !pvt->pci_sad1 ||
2719 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2720 goto enodev;
2721
2722 if (saw_chan_mask != 0x0f && /* -EN/-EX */
2723 saw_chan_mask != 0x03) /* -EP */
2724 goto enodev;
2725 return 0;
2726
2727enodev:
2728 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2729 return -ENODEV;
2730}
2731
2732static int knl_mci_bind_devs(struct mem_ctl_info *mci,
2733 struct sbridge_dev *sbridge_dev)
2734{
2735 struct sbridge_pvt *pvt = mci->pvt_info;
2736 struct pci_dev *pdev;
2737 int dev, func;
2738
2739 int i;
2740 int devidx;
2741
2742 for (i = 0; i < sbridge_dev->n_devs; i++) {
2743 pdev = sbridge_dev->pdev[i];
2744 if (!pdev)
2745 continue;
2746
2747 /* Extract PCI device and function. */
2748 dev = (pdev->devfn >> 3) & 0x1f;
2749 func = pdev->devfn & 0x7;
2750
2751 switch (pdev->device) {
2752 case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
2753 if (dev == 8)
2754 pvt->knl.pci_mc0 = pdev;
2755 else if (dev == 9)
2756 pvt->knl.pci_mc1 = pdev;
2757 else {
2758 sbridge_printk(KERN_ERR,
2759 "Memory controller in unexpected place! (dev %d, fn %d)\n",
2760 dev, func);
2761 continue;
2762 }
2763 break;
2764
2765 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
2766 pvt->pci_sad0 = pdev;
2767 break;
2768
2769 case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
2770 pvt->pci_sad1 = pdev;
2771 break;
2772
2773 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
2774 /* There are one of these per tile, and range from
2775 * 1.14.0 to 1.18.5.
2776 */
2777 devidx = ((dev-14)*8)+func;
2778
2779 if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
2780 sbridge_printk(KERN_ERR,
2781 "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
2782 dev, func);
2783 continue;
2784 }
2785
2786 WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
2787
2788 pvt->knl.pci_cha[devidx] = pdev;
2789 break;
2790
2791 case PCI_DEVICE_ID_INTEL_KNL_IMC_CHAN:
2792 devidx = -1;
2793
2794 /*
2795 * MC0 channels 0-2 are device 9 function 2-4,
2796 * MC1 channels 3-5 are device 8 function 2-4.
2797 */
2798
2799 if (dev == 9)
2800 devidx = func-2;
2801 else if (dev == 8)
2802 devidx = 3 + (func-2);
2803
2804 if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
2805 sbridge_printk(KERN_ERR,
2806 "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
2807 dev, func);
2808 continue;
2809 }
2810
2811 WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
2812 pvt->knl.pci_channel[devidx] = pdev;
2813 break;
2814
2815 case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
2816 pvt->knl.pci_mc_info = pdev;
2817 break;
2818
2819 case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
2820 pvt->pci_ta = pdev;
2821 break;
2822
2823 default:
2824 sbridge_printk(KERN_ERR, "Unexpected device %d\n",
2825 pdev->device);
2826 break;
2827 }
2828 }
2829
2830 if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
2831 !pvt->pci_sad0 || !pvt->pci_sad1 ||
2832 !pvt->pci_ta) {
2833 goto enodev;
2834 }
2835
2836 for (i = 0; i < KNL_MAX_CHANNELS; i++) {
2837 if (!pvt->knl.pci_channel[i]) {
2838 sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
2839 goto enodev;
2840 }
2841 }
2842
2843 for (i = 0; i < KNL_MAX_CHAS; i++) {
2844 if (!pvt->knl.pci_cha[i]) {
2845 sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
2846 goto enodev;
2847 }
2848 }
2849
2850 return 0;
2851
2852enodev:
2853 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2854 return -ENODEV;
2855}
2856
2857/****************************************************************************
2858 Error check routines
2859 ****************************************************************************/
2860
2861/*
2862 * While Sandy Bridge has error count registers, SMI BIOS read values from
2863 * and resets the counters. So, they are not reliable for the OS to read
2864 * from them. So, we have no option but to just trust on whatever MCE is
2865 * telling us about the errors.
2866 */
2867static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2868 const struct mce *m)
2869{
2870 struct mem_ctl_info *new_mci;
2871 struct sbridge_pvt *pvt = mci->pvt_info;
2872 enum hw_event_mc_err_type tp_event;
2873 char *type, *optype, msg[256];
2874 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2875 bool overflow = GET_BITFIELD(m->status, 62, 62);
2876 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
2877 bool recoverable;
2878 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2879 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2880 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2881 u32 channel = GET_BITFIELD(m->status, 0, 3);
2882 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2883 long channel_mask, first_channel;
2884 u8 rank, socket, ha;
2885 int rc, dimm;
2886 char *area_type = NULL;
2887
2888 if (pvt->info.type != SANDY_BRIDGE)
2889 recoverable = true;
2890 else
2891 recoverable = GET_BITFIELD(m->status, 56, 56);
2892
2893 if (uncorrected_error) {
2894 core_err_cnt = 1;
2895 if (ripv) {
2896 type = "FATAL";
2897 tp_event = HW_EVENT_ERR_FATAL;
2898 } else {
2899 type = "NON_FATAL";
2900 tp_event = HW_EVENT_ERR_UNCORRECTED;
2901 }
2902 } else {
2903 type = "CORRECTED";
2904 tp_event = HW_EVENT_ERR_CORRECTED;
2905 }
2906
2907 /*
2908 * According with Table 15-9 of the Intel Architecture spec vol 3A,
2909 * memory errors should fit in this mask:
2910 * 000f 0000 1mmm cccc (binary)
2911 * where:
2912 * f = Correction Report Filtering Bit. If 1, subsequent errors
2913 * won't be shown
2914 * mmm = error type
2915 * cccc = channel
2916 * If the mask doesn't match, report an error to the parsing logic
2917 */
2918 switch (optypenum) {
2919 case 0:
2920 optype = "generic undef request error";
2921 break;
2922 case 1:
2923 optype = "memory read error";
2924 break;
2925 case 2:
2926 optype = "memory write error";
2927 break;
2928 case 3:
2929 optype = "addr/cmd error";
2930 break;
2931 case 4:
2932 optype = "memory scrubbing error";
2933 break;
2934 default:
2935 optype = "reserved";
2936 break;
2937 }
2938
2939 if (pvt->info.type == KNIGHTS_LANDING) {
2940 if (channel == 14) {
2941 edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
2942 overflow ? " OVERFLOW" : "",
2943 (uncorrected_error && recoverable)
2944 ? " recoverable" : "",
2945 mscod, errcode,
2946 m->bank);
2947 } else {
2948 char A = *("A");
2949
2950 /*
2951 * Reported channel is in range 0-2, so we can't map it
2952 * back to mc. To figure out mc we check machine check
2953 * bank register that reported this error.
2954 * bank15 means mc0 and bank16 means mc1.
2955 */
2956 channel = knl_channel_remap(m->bank == 16, channel);
2957 channel_mask = 1 << channel;
2958
2959 snprintf(msg, sizeof(msg),
2960 "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
2961 overflow ? " OVERFLOW" : "",
2962 (uncorrected_error && recoverable)
2963 ? " recoverable" : " ",
2964 mscod, errcode, channel, A + channel);
2965 edac_mc_handle_error(tp_event, mci, core_err_cnt,
2966 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
2967 channel, 0, -1,
2968 optype, msg);
2969 }
2970 return;
2971 } else {
2972 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
2973 &channel_mask, &rank, &area_type, msg);
2974 }
2975
2976 if (rc < 0)
2977 goto err_parsing;
2978 new_mci = get_mci_for_node_id(socket, ha);
2979 if (!new_mci) {
2980 strcpy(msg, "Error: socket got corrupted!");
2981 goto err_parsing;
2982 }
2983 mci = new_mci;
2984 pvt = mci->pvt_info;
2985
2986 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
2987
2988 if (rank < 4)
2989 dimm = 0;
2990 else if (rank < 8)
2991 dimm = 1;
2992 else
2993 dimm = 2;
2994
2995
2996 /*
2997 * FIXME: On some memory configurations (mirror, lockstep), the
2998 * Memory Controller can't point the error to a single DIMM. The
2999 * EDAC core should be handling the channel mask, in order to point
3000 * to the group of dimm's where the error may be happening.
3001 */
3002 if (!pvt->is_lockstep && !pvt->is_cur_addr_mirrored && !pvt->is_close_pg)
3003 channel = first_channel;
3004
3005 snprintf(msg, sizeof(msg),
3006 "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
3007 overflow ? " OVERFLOW" : "",
3008 (uncorrected_error && recoverable) ? " recoverable" : "",
3009 area_type,
3010 mscod, errcode,
3011 socket, ha,
3012 channel_mask,
3013 rank);
3014
3015 edac_dbg(0, "%s\n", msg);
3016
3017 /* FIXME: need support for channel mask */
3018
3019 if (channel == CHANNEL_UNSPECIFIED)
3020 channel = -1;
3021
3022 /* Call the helper to output message */
3023 edac_mc_handle_error(tp_event, mci, core_err_cnt,
3024 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
3025 channel, dimm, -1,
3026 optype, msg);
3027 return;
3028err_parsing:
3029 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
3030 -1, -1, -1,
3031 msg, "");
3032
3033}
3034
3035/*
3036 * Check that logging is enabled and that this is the right type
3037 * of error for us to handle.
3038 */
3039static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
3040 void *data)
3041{
3042 struct mce *mce = (struct mce *)data;
3043 struct mem_ctl_info *mci;
3044 char *type;
3045
3046 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
3047 return NOTIFY_DONE;
3048
3049 /*
3050 * Just let mcelog handle it if the error is
3051 * outside the memory controller. A memory error
3052 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
3053 * bit 12 has an special meaning.
3054 */
3055 if ((mce->status & 0xefff) >> 7 != 1)
3056 return NOTIFY_DONE;
3057
3058 /* Check ADDRV bit in STATUS */
3059 if (!GET_BITFIELD(mce->status, 58, 58))
3060 return NOTIFY_DONE;
3061
3062 /* Check MISCV bit in STATUS */
3063 if (!GET_BITFIELD(mce->status, 59, 59))
3064 return NOTIFY_DONE;
3065
3066 /* Check address type in MISC (physical address only) */
3067 if (GET_BITFIELD(mce->misc, 6, 8) != 2)
3068 return NOTIFY_DONE;
3069
3070 mci = get_mci_for_node_id(mce->socketid, IMC0);
3071 if (!mci)
3072 return NOTIFY_DONE;
3073
3074 if (mce->mcgstatus & MCG_STATUS_MCIP)
3075 type = "Exception";
3076 else
3077 type = "Event";
3078
3079 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
3080
3081 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
3082 "Bank %d: %016Lx\n", mce->extcpu, type,
3083 mce->mcgstatus, mce->bank, mce->status);
3084 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
3085 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
3086 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
3087
3088 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
3089 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
3090 mce->time, mce->socketid, mce->apicid);
3091
3092 sbridge_mce_output_error(mci, mce);
3093
3094 /* Advice mcelog that the error were handled */
3095 return NOTIFY_STOP;
3096}
3097
3098static struct notifier_block sbridge_mce_dec = {
3099 .notifier_call = sbridge_mce_check_error,
3100 .priority = MCE_PRIO_EDAC,
3101};
3102
3103/****************************************************************************
3104 EDAC register/unregister logic
3105 ****************************************************************************/
3106
3107static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
3108{
3109 struct mem_ctl_info *mci = sbridge_dev->mci;
3110 struct sbridge_pvt *pvt;
3111
3112 if (unlikely(!mci || !mci->pvt_info)) {
3113 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
3114
3115 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
3116 return;
3117 }
3118
3119 pvt = mci->pvt_info;
3120
3121 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3122 mci, &sbridge_dev->pdev[0]->dev);
3123
3124 /* Remove MC sysfs nodes */
3125 edac_mc_del_mc(mci->pdev);
3126
3127 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
3128 kfree(mci->ctl_name);
3129 edac_mc_free(mci);
3130 sbridge_dev->mci = NULL;
3131}
3132
3133static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
3134{
3135 struct mem_ctl_info *mci;
3136 struct edac_mc_layer layers[2];
3137 struct sbridge_pvt *pvt;
3138 struct pci_dev *pdev = sbridge_dev->pdev[0];
3139 int rc;
3140
3141 /* allocate a new MC control structure */
3142 layers[0].type = EDAC_MC_LAYER_CHANNEL;
3143 layers[0].size = type == KNIGHTS_LANDING ?
3144 KNL_MAX_CHANNELS : NUM_CHANNELS;
3145 layers[0].is_virt_csrow = false;
3146 layers[1].type = EDAC_MC_LAYER_SLOT;
3147 layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
3148 layers[1].is_virt_csrow = true;
3149 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
3150 sizeof(*pvt));
3151
3152 if (unlikely(!mci))
3153 return -ENOMEM;
3154
3155 edac_dbg(0, "MC: mci = %p, dev = %p\n",
3156 mci, &pdev->dev);
3157
3158 pvt = mci->pvt_info;
3159 memset(pvt, 0, sizeof(*pvt));
3160
3161 /* Associate sbridge_dev and mci for future usage */
3162 pvt->sbridge_dev = sbridge_dev;
3163 sbridge_dev->mci = mci;
3164
3165 mci->mtype_cap = type == KNIGHTS_LANDING ?
3166 MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
3167 mci->edac_ctl_cap = EDAC_FLAG_NONE;
3168 mci->edac_cap = EDAC_FLAG_NONE;
3169 mci->mod_name = "sb_edac.c";
3170 mci->dev_name = pci_name(pdev);
3171 mci->ctl_page_to_phys = NULL;
3172
3173 pvt->info.type = type;
3174 switch (type) {
3175 case IVY_BRIDGE:
3176 pvt->info.rankcfgr = IB_RANK_CFG_A;
3177 pvt->info.get_tolm = ibridge_get_tolm;
3178 pvt->info.get_tohm = ibridge_get_tohm;
3179 pvt->info.dram_rule = ibridge_dram_rule;
3180 pvt->info.get_memory_type = get_memory_type;
3181 pvt->info.get_node_id = get_node_id;
3182 pvt->info.rir_limit = rir_limit;
3183 pvt->info.sad_limit = sad_limit;
3184 pvt->info.interleave_mode = interleave_mode;
3185 pvt->info.dram_attr = dram_attr;
3186 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3187 pvt->info.interleave_list = ibridge_interleave_list;
3188 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3189 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3190 pvt->info.get_width = ibridge_get_width;
3191
3192 /* Store pci devices at mci for faster access */
3193 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
3194 if (unlikely(rc < 0))
3195 goto fail0;
3196 get_source_id(mci);
3197 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge SrcID#%d_Ha#%d",
3198 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
3199 break;
3200 case SANDY_BRIDGE:
3201 pvt->info.rankcfgr = SB_RANK_CFG_A;
3202 pvt->info.get_tolm = sbridge_get_tolm;
3203 pvt->info.get_tohm = sbridge_get_tohm;
3204 pvt->info.dram_rule = sbridge_dram_rule;
3205 pvt->info.get_memory_type = get_memory_type;
3206 pvt->info.get_node_id = get_node_id;
3207 pvt->info.rir_limit = rir_limit;
3208 pvt->info.sad_limit = sad_limit;
3209 pvt->info.interleave_mode = interleave_mode;
3210 pvt->info.dram_attr = dram_attr;
3211 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
3212 pvt->info.interleave_list = sbridge_interleave_list;
3213 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
3214 pvt->info.interleave_pkg = sbridge_interleave_pkg;
3215 pvt->info.get_width = sbridge_get_width;
3216
3217 /* Store pci devices at mci for faster access */
3218 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
3219 if (unlikely(rc < 0))
3220 goto fail0;
3221 get_source_id(mci);
3222 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge SrcID#%d_Ha#%d",
3223 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
3224 break;
3225 case HASWELL:
3226 /* rankcfgr isn't used */
3227 pvt->info.get_tolm = haswell_get_tolm;
3228 pvt->info.get_tohm = haswell_get_tohm;
3229 pvt->info.dram_rule = ibridge_dram_rule;
3230 pvt->info.get_memory_type = haswell_get_memory_type;
3231 pvt->info.get_node_id = haswell_get_node_id;
3232 pvt->info.rir_limit = haswell_rir_limit;
3233 pvt->info.sad_limit = sad_limit;
3234 pvt->info.interleave_mode = interleave_mode;
3235 pvt->info.dram_attr = dram_attr;
3236 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3237 pvt->info.interleave_list = ibridge_interleave_list;
3238 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3239 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3240 pvt->info.get_width = ibridge_get_width;
3241
3242 /* Store pci devices at mci for faster access */
3243 rc = haswell_mci_bind_devs(mci, sbridge_dev);
3244 if (unlikely(rc < 0))
3245 goto fail0;
3246 get_source_id(mci);
3247 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell SrcID#%d_Ha#%d",
3248 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
3249 break;
3250 case BROADWELL:
3251 /* rankcfgr isn't used */
3252 pvt->info.get_tolm = haswell_get_tolm;
3253 pvt->info.get_tohm = haswell_get_tohm;
3254 pvt->info.dram_rule = ibridge_dram_rule;
3255 pvt->info.get_memory_type = haswell_get_memory_type;
3256 pvt->info.get_node_id = haswell_get_node_id;
3257 pvt->info.rir_limit = haswell_rir_limit;
3258 pvt->info.sad_limit = sad_limit;
3259 pvt->info.interleave_mode = interleave_mode;
3260 pvt->info.dram_attr = dram_attr;
3261 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
3262 pvt->info.interleave_list = ibridge_interleave_list;
3263 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
3264 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3265 pvt->info.get_width = broadwell_get_width;
3266
3267 /* Store pci devices at mci for faster access */
3268 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
3269 if (unlikely(rc < 0))
3270 goto fail0;
3271 get_source_id(mci);
3272 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell SrcID#%d_Ha#%d",
3273 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
3274 break;
3275 case KNIGHTS_LANDING:
3276 /* pvt->info.rankcfgr == ??? */
3277 pvt->info.get_tolm = knl_get_tolm;
3278 pvt->info.get_tohm = knl_get_tohm;
3279 pvt->info.dram_rule = knl_dram_rule;
3280 pvt->info.get_memory_type = knl_get_memory_type;
3281 pvt->info.get_node_id = knl_get_node_id;
3282 pvt->info.rir_limit = NULL;
3283 pvt->info.sad_limit = knl_sad_limit;
3284 pvt->info.interleave_mode = knl_interleave_mode;
3285 pvt->info.dram_attr = dram_attr_knl;
3286 pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
3287 pvt->info.interleave_list = knl_interleave_list;
3288 pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
3289 pvt->info.interleave_pkg = ibridge_interleave_pkg;
3290 pvt->info.get_width = knl_get_width;
3291
3292 rc = knl_mci_bind_devs(mci, sbridge_dev);
3293 if (unlikely(rc < 0))
3294 goto fail0;
3295 get_source_id(mci);
3296 mci->ctl_name = kasprintf(GFP_KERNEL, "Knights Landing SrcID#%d_Ha#%d",
3297 pvt->sbridge_dev->source_id, pvt->sbridge_dev->dom);
3298 break;
3299 }
3300
3301 /* Get dimm basic config and the memory layout */
3302 rc = get_dimm_config(mci);
3303 if (rc < 0) {
3304 edac_dbg(0, "MC: failed to get_dimm_config()\n");
3305 goto fail;
3306 }
3307 get_memory_layout(mci);
3308
3309 /* record ptr to the generic device */
3310 mci->pdev = &pdev->dev;
3311
3312 /* add this new MC control structure to EDAC's list of MCs */
3313 if (unlikely(edac_mc_add_mc(mci))) {
3314 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
3315 rc = -EINVAL;
3316 goto fail;
3317 }
3318
3319 return 0;
3320
3321fail:
3322 kfree(mci->ctl_name);
3323fail0:
3324 edac_mc_free(mci);
3325 sbridge_dev->mci = NULL;
3326 return rc;
3327}
3328
3329#define ICPU(model, table) \
3330 { X86_VENDOR_INTEL, 6, model, 0, (unsigned long)&table }
3331
3332static const struct x86_cpu_id sbridge_cpuids[] = {
3333 ICPU(INTEL_FAM6_SANDYBRIDGE_X, pci_dev_descr_sbridge_table),
3334 ICPU(INTEL_FAM6_IVYBRIDGE_X, pci_dev_descr_ibridge_table),
3335 ICPU(INTEL_FAM6_HASWELL_X, pci_dev_descr_haswell_table),
3336 ICPU(INTEL_FAM6_BROADWELL_X, pci_dev_descr_broadwell_table),
3337 ICPU(INTEL_FAM6_BROADWELL_XEON_D, pci_dev_descr_broadwell_table),
3338 ICPU(INTEL_FAM6_XEON_PHI_KNL, pci_dev_descr_knl_table),
3339 ICPU(INTEL_FAM6_XEON_PHI_KNM, pci_dev_descr_knl_table),
3340 { }
3341};
3342MODULE_DEVICE_TABLE(x86cpu, sbridge_cpuids);
3343
3344/*
3345 * sbridge_probe Get all devices and register memory controllers
3346 * present.
3347 * return:
3348 * 0 for FOUND a device
3349 * < 0 for error code
3350 */
3351
3352static int sbridge_probe(const struct x86_cpu_id *id)
3353{
3354 int rc = -ENODEV;
3355 u8 mc, num_mc = 0;
3356 struct sbridge_dev *sbridge_dev;
3357 struct pci_id_table *ptable = (struct pci_id_table *)id->driver_data;
3358
3359 /* get the pci devices we want to reserve for our use */
3360 rc = sbridge_get_all_devices(&num_mc, ptable);
3361
3362 if (unlikely(rc < 0)) {
3363 edac_dbg(0, "couldn't get all devices\n");
3364 goto fail0;
3365 }
3366
3367 mc = 0;
3368
3369 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
3370 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
3371 mc, mc + 1, num_mc);
3372
3373 sbridge_dev->mc = mc++;
3374 rc = sbridge_register_mci(sbridge_dev, ptable->type);
3375 if (unlikely(rc < 0))
3376 goto fail1;
3377 }
3378
3379 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
3380
3381 return 0;
3382
3383fail1:
3384 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3385 sbridge_unregister_mci(sbridge_dev);
3386
3387 sbridge_put_all_devices();
3388fail0:
3389 return rc;
3390}
3391
3392/*
3393 * sbridge_remove cleanup
3394 *
3395 */
3396static void sbridge_remove(void)
3397{
3398 struct sbridge_dev *sbridge_dev;
3399
3400 edac_dbg(0, "\n");
3401
3402 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
3403 sbridge_unregister_mci(sbridge_dev);
3404
3405 /* Release PCI resources */
3406 sbridge_put_all_devices();
3407}
3408
3409/*
3410 * sbridge_init Module entry function
3411 * Try to initialize this module for its devices
3412 */
3413static int __init sbridge_init(void)
3414{
3415 const struct x86_cpu_id *id;
3416 int rc;
3417
3418 edac_dbg(2, "\n");
3419
3420 id = x86_match_cpu(sbridge_cpuids);
3421 if (!id)
3422 return -ENODEV;
3423
3424 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
3425 opstate_init();
3426
3427 rc = sbridge_probe(id);
3428
3429 if (rc >= 0) {
3430 mce_register_decode_chain(&sbridge_mce_dec);
3431 if (edac_get_report_status() == EDAC_REPORTING_DISABLED)
3432 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
3433 return 0;
3434 }
3435
3436 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
3437 rc);
3438
3439 return rc;
3440}
3441
3442/*
3443 * sbridge_exit() Module exit function
3444 * Unregister the driver
3445 */
3446static void __exit sbridge_exit(void)
3447{
3448 edac_dbg(2, "\n");
3449 sbridge_remove();
3450 mce_unregister_decode_chain(&sbridge_mce_dec);
3451}
3452
3453module_init(sbridge_init);
3454module_exit(sbridge_exit);
3455
3456module_param(edac_op_state, int, 0444);
3457MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
3458
3459MODULE_LICENSE("GPL");
3460MODULE_AUTHOR("Mauro Carvalho Chehab");
3461MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
3462MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
3463 SBRIDGE_REVISION);