Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Intel E3-1200
  4 * Copyright (C) 2014 Jason Baron <jbaron@akamai.com>
  5 *
  6 * Support for the E3-1200 processor family. Heavily based on previous
  7 * Intel EDAC drivers.
  8 *
  9 * Since the DRAM controller is on the cpu chip, we can use its PCI device
 10 * id to identify these processors.
 11 *
 12 * PCI DRAM controller device ids (Taken from The PCI ID Repository - https://pci-ids.ucw.cz/)
 13 *
 14 * 0108: Xeon E3-1200 Processor Family DRAM Controller
 15 * 010c: Xeon E3-1200/2nd Generation Core Processor Family DRAM Controller
 16 * 0150: Xeon E3-1200 v2/3rd Gen Core processor DRAM Controller
 17 * 0158: Xeon E3-1200 v2/Ivy Bridge DRAM Controller
 18 * 015c: Xeon E3-1200 v2/3rd Gen Core processor DRAM Controller
 19 * 0c04: Xeon E3-1200 v3/4th Gen Core Processor DRAM Controller
 20 * 0c08: Xeon E3-1200 v3 Processor DRAM Controller
 21 * 1918: Xeon E3-1200 v5 Skylake Host Bridge/DRAM Registers
 22 * 5918: Xeon E3-1200 Xeon E3-1200 v6/7th Gen Core Processor Host Bridge/DRAM Registers
 23 * 190f: 6th Gen Core Dual-Core Processor Host Bridge/DRAM Registers
 24 * 191f: 6th Gen Core Quad-Core Processor Host Bridge/DRAM Registers
 25 * 3e..: 8th/9th Gen Core Processor Host Bridge/DRAM Registers
 26 *
 27 * Based on Intel specification:
 28 * https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/xeon-e3-1200v3-vol-2-datasheet.pdf
 29 * http://www.intel.com/content/www/us/en/processors/xeon/xeon-e3-1200-family-vol-2-datasheet.html
 30 * https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/desktop-6th-gen-core-family-datasheet-vol-2.pdf
 31 * https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/xeon-e3-1200v6-vol-2-datasheet.pdf
 32 * https://www.intel.com/content/www/us/en/processors/core/7th-gen-core-family-mobile-h-processor-lines-datasheet-vol-2.html
 33 * https://www.intel.com/content/www/us/en/products/docs/processors/core/8th-gen-core-family-datasheet-vol-2.html
 34 *
 35 * According to the above datasheet (p.16):
 36 * "
 37 * 6. Software must not access B0/D0/F0 32-bit memory-mapped registers with
 38 * requests that cross a DW boundary.
 39 * "
 40 *
 41 * Thus, we make use of the explicit: lo_hi_readq(), which breaks the readq into
 42 * 2 readl() calls. This restriction may be lifted in subsequent chip releases,
 43 * but lo_hi_readq() ensures that we are safe across all e3-1200 processors.
 44 */
 45
 46#include <linux/module.h>
 47#include <linux/init.h>
 48#include <linux/pci.h>
 49#include <linux/pci_ids.h>
 50#include <linux/edac.h>
 51
 52#include <linux/io-64-nonatomic-lo-hi.h>
 53#include "edac_module.h"
 54
 55#define EDAC_MOD_STR "ie31200_edac"
 56
 57#define ie31200_printk(level, fmt, arg...) \
 58	edac_printk(level, "ie31200", fmt, ##arg)
 59
 60#define PCI_DEVICE_ID_INTEL_IE31200_HB_1  0x0108
 61#define PCI_DEVICE_ID_INTEL_IE31200_HB_2  0x010c
 62#define PCI_DEVICE_ID_INTEL_IE31200_HB_3  0x0150
 63#define PCI_DEVICE_ID_INTEL_IE31200_HB_4  0x0158
 64#define PCI_DEVICE_ID_INTEL_IE31200_HB_5  0x015c
 65#define PCI_DEVICE_ID_INTEL_IE31200_HB_6  0x0c04
 66#define PCI_DEVICE_ID_INTEL_IE31200_HB_7  0x0c08
 67#define PCI_DEVICE_ID_INTEL_IE31200_HB_8  0x190F
 68#define PCI_DEVICE_ID_INTEL_IE31200_HB_9  0x1918
 69#define PCI_DEVICE_ID_INTEL_IE31200_HB_10 0x191F
 70#define PCI_DEVICE_ID_INTEL_IE31200_HB_11 0x5918
 71
 72/* Coffee Lake-S */
 73#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_MASK 0x3e00
 74#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_1    0x3e0f
 75#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_2    0x3e18
 76#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_3    0x3e1f
 77#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_4    0x3e30
 78#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_5    0x3e31
 79#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_6    0x3e32
 80#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_7    0x3e33
 81#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_8    0x3ec2
 82#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_9    0x3ec6
 83#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_10   0x3eca
 84
 85/* Test if HB is for Skylake or later. */
 86#define DEVICE_ID_SKYLAKE_OR_LATER(did)                                        \
 87	(((did) == PCI_DEVICE_ID_INTEL_IE31200_HB_8) ||                        \
 88	 ((did) == PCI_DEVICE_ID_INTEL_IE31200_HB_9) ||                        \
 89	 ((did) == PCI_DEVICE_ID_INTEL_IE31200_HB_10) ||                       \
 90	 ((did) == PCI_DEVICE_ID_INTEL_IE31200_HB_11) ||                       \
 91	 (((did) & PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_MASK) ==                 \
 92	  PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_MASK))
 93
 94#define IE31200_DIMMS			4
 95#define IE31200_RANKS			8
 96#define IE31200_RANKS_PER_CHANNEL	4
 97#define IE31200_DIMMS_PER_CHANNEL	2
 98#define IE31200_CHANNELS		2
 99
100/* Intel IE31200 register addresses - device 0 function 0 - DRAM Controller */
101#define IE31200_MCHBAR_LOW		0x48
102#define IE31200_MCHBAR_HIGH		0x4c
103#define IE31200_MCHBAR_MASK		GENMASK_ULL(38, 15)
104#define IE31200_MMR_WINDOW_SIZE		BIT(15)
105
106/*
107 * Error Status Register (16b)
108 *
109 * 15    reserved
110 * 14    Isochronous TBWRR Run Behind FIFO Full
111 *       (ITCV)
112 * 13    Isochronous TBWRR Run Behind FIFO Put
113 *       (ITSTV)
114 * 12    reserved
115 * 11    MCH Thermal Sensor Event
116 *       for SMI/SCI/SERR (GTSE)
117 * 10    reserved
118 *  9    LOCK to non-DRAM Memory Flag (LCKF)
119 *  8    reserved
120 *  7    DRAM Throttle Flag (DTF)
121 *  6:2  reserved
122 *  1    Multi-bit DRAM ECC Error Flag (DMERR)
123 *  0    Single-bit DRAM ECC Error Flag (DSERR)
124 */
125#define IE31200_ERRSTS			0xc8
126#define IE31200_ERRSTS_UE		BIT(1)
127#define IE31200_ERRSTS_CE		BIT(0)
128#define IE31200_ERRSTS_BITS		(IE31200_ERRSTS_UE | IE31200_ERRSTS_CE)
129
130/*
131 * Channel 0 ECC Error Log (64b)
132 *
133 * 63:48 Error Column Address (ERRCOL)
134 * 47:32 Error Row Address (ERRROW)
135 * 31:29 Error Bank Address (ERRBANK)
136 * 28:27 Error Rank Address (ERRRANK)
137 * 26:24 reserved
138 * 23:16 Error Syndrome (ERRSYND)
139 * 15: 2 reserved
140 *    1  Multiple Bit Error Status (MERRSTS)
141 *    0  Correctable Error Status (CERRSTS)
142 */
143
144#define IE31200_C0ECCERRLOG			0x40c8
145#define IE31200_C1ECCERRLOG			0x44c8
146#define IE31200_C0ECCERRLOG_SKL			0x4048
147#define IE31200_C1ECCERRLOG_SKL			0x4448
148#define IE31200_ECCERRLOG_CE			BIT(0)
149#define IE31200_ECCERRLOG_UE			BIT(1)
150#define IE31200_ECCERRLOG_RANK_BITS		GENMASK_ULL(28, 27)
151#define IE31200_ECCERRLOG_RANK_SHIFT		27
152#define IE31200_ECCERRLOG_SYNDROME_BITS		GENMASK_ULL(23, 16)
153#define IE31200_ECCERRLOG_SYNDROME_SHIFT	16
154
155#define IE31200_ECCERRLOG_SYNDROME(log)		   \
156	((log & IE31200_ECCERRLOG_SYNDROME_BITS) >> \
157	 IE31200_ECCERRLOG_SYNDROME_SHIFT)
158
159#define IE31200_CAPID0			0xe4
160#define IE31200_CAPID0_PDCD		BIT(4)
161#define IE31200_CAPID0_DDPCD		BIT(6)
162#define IE31200_CAPID0_ECC		BIT(1)
163
164#define IE31200_MAD_DIMM_0_OFFSET		0x5004
165#define IE31200_MAD_DIMM_0_OFFSET_SKL		0x500C
166#define IE31200_MAD_DIMM_SIZE			GENMASK_ULL(7, 0)
167#define IE31200_MAD_DIMM_A_RANK			BIT(17)
168#define IE31200_MAD_DIMM_A_RANK_SHIFT		17
169#define IE31200_MAD_DIMM_A_RANK_SKL		BIT(10)
170#define IE31200_MAD_DIMM_A_RANK_SKL_SHIFT	10
171#define IE31200_MAD_DIMM_A_WIDTH		BIT(19)
172#define IE31200_MAD_DIMM_A_WIDTH_SHIFT		19
173#define IE31200_MAD_DIMM_A_WIDTH_SKL		GENMASK_ULL(9, 8)
174#define IE31200_MAD_DIMM_A_WIDTH_SKL_SHIFT	8
175
176/* Skylake reports 1GB increments, everything else is 256MB */
177#define IE31200_PAGES(n, skl)	\
178	(n << (28 + (2 * skl) - PAGE_SHIFT))
179
180static int nr_channels;
181static struct pci_dev *mci_pdev;
182static int ie31200_registered = 1;
183
184struct ie31200_priv {
185	void __iomem *window;
186	void __iomem *c0errlog;
187	void __iomem *c1errlog;
188};
189
190enum ie31200_chips {
191	IE31200 = 0,
192};
193
194struct ie31200_dev_info {
195	const char *ctl_name;
196};
197
198struct ie31200_error_info {
199	u16 errsts;
200	u16 errsts2;
201	u64 eccerrlog[IE31200_CHANNELS];
202};
203
204static const struct ie31200_dev_info ie31200_devs[] = {
205	[IE31200] = {
206		.ctl_name = "IE31200"
207	},
208};
209
210struct dimm_data {
211	u8 size; /* in multiples of 256MB, except Skylake is 1GB */
212	u8 dual_rank : 1,
213	   x16_width : 2; /* 0 means x8 width */
214};
215
216static int how_many_channels(struct pci_dev *pdev)
217{
218	int n_channels;
219	unsigned char capid0_2b; /* 2nd byte of CAPID0 */
220
221	pci_read_config_byte(pdev, IE31200_CAPID0 + 1, &capid0_2b);
222
223	/* check PDCD: Dual Channel Disable */
224	if (capid0_2b & IE31200_CAPID0_PDCD) {
225		edac_dbg(0, "In single channel mode\n");
226		n_channels = 1;
227	} else {
228		edac_dbg(0, "In dual channel mode\n");
229		n_channels = 2;
230	}
231
232	/* check DDPCD - check if both channels are filled */
233	if (capid0_2b & IE31200_CAPID0_DDPCD)
234		edac_dbg(0, "2 DIMMS per channel disabled\n");
235	else
236		edac_dbg(0, "2 DIMMS per channel enabled\n");
237
238	return n_channels;
239}
240
241static bool ecc_capable(struct pci_dev *pdev)
242{
243	unsigned char capid0_4b; /* 4th byte of CAPID0 */
244
245	pci_read_config_byte(pdev, IE31200_CAPID0 + 3, &capid0_4b);
246	if (capid0_4b & IE31200_CAPID0_ECC)
247		return false;
248	return true;
249}
250
251static int eccerrlog_row(u64 log)
252{
253	return ((log & IE31200_ECCERRLOG_RANK_BITS) >>
254				IE31200_ECCERRLOG_RANK_SHIFT);
255}
256
257static void ie31200_clear_error_info(struct mem_ctl_info *mci)
258{
259	/*
260	 * Clear any error bits.
261	 * (Yes, we really clear bits by writing 1 to them.)
262	 */
263	pci_write_bits16(to_pci_dev(mci->pdev), IE31200_ERRSTS,
264			 IE31200_ERRSTS_BITS, IE31200_ERRSTS_BITS);
265}
266
267static void ie31200_get_and_clear_error_info(struct mem_ctl_info *mci,
268					     struct ie31200_error_info *info)
269{
270	struct pci_dev *pdev;
271	struct ie31200_priv *priv = mci->pvt_info;
272
273	pdev = to_pci_dev(mci->pdev);
274
275	/*
276	 * This is a mess because there is no atomic way to read all the
277	 * registers at once and the registers can transition from CE being
278	 * overwritten by UE.
279	 */
280	pci_read_config_word(pdev, IE31200_ERRSTS, &info->errsts);
281	if (!(info->errsts & IE31200_ERRSTS_BITS))
282		return;
283
284	info->eccerrlog[0] = lo_hi_readq(priv->c0errlog);
285	if (nr_channels == 2)
286		info->eccerrlog[1] = lo_hi_readq(priv->c1errlog);
287
288	pci_read_config_word(pdev, IE31200_ERRSTS, &info->errsts2);
289
290	/*
291	 * If the error is the same for both reads then the first set
292	 * of reads is valid.  If there is a change then there is a CE
293	 * with no info and the second set of reads is valid and
294	 * should be UE info.
295	 */
296	if ((info->errsts ^ info->errsts2) & IE31200_ERRSTS_BITS) {
297		info->eccerrlog[0] = lo_hi_readq(priv->c0errlog);
298		if (nr_channels == 2)
299			info->eccerrlog[1] =
300				lo_hi_readq(priv->c1errlog);
301	}
302
303	ie31200_clear_error_info(mci);
304}
305
306static void ie31200_process_error_info(struct mem_ctl_info *mci,
307				       struct ie31200_error_info *info)
308{
309	int channel;
310	u64 log;
311
312	if (!(info->errsts & IE31200_ERRSTS_BITS))
313		return;
314
315	if ((info->errsts ^ info->errsts2) & IE31200_ERRSTS_BITS) {
316		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
317				     -1, -1, -1, "UE overwrote CE", "");
318		info->errsts = info->errsts2;
319	}
320
321	for (channel = 0; channel < nr_channels; channel++) {
322		log = info->eccerrlog[channel];
323		if (log & IE31200_ECCERRLOG_UE) {
324			edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
325					     0, 0, 0,
326					     eccerrlog_row(log),
327					     channel, -1,
328					     "ie31200 UE", "");
329		} else if (log & IE31200_ECCERRLOG_CE) {
330			edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
331					     0, 0,
332					     IE31200_ECCERRLOG_SYNDROME(log),
333					     eccerrlog_row(log),
334					     channel, -1,
335					     "ie31200 CE", "");
336		}
337	}
338}
339
340static void ie31200_check(struct mem_ctl_info *mci)
341{
342	struct ie31200_error_info info;
343
 
344	ie31200_get_and_clear_error_info(mci, &info);
345	ie31200_process_error_info(mci, &info);
346}
347
348static void __iomem *ie31200_map_mchbar(struct pci_dev *pdev)
349{
350	union {
351		u64 mchbar;
352		struct {
353			u32 mchbar_low;
354			u32 mchbar_high;
355		};
356	} u;
357	void __iomem *window;
358
359	pci_read_config_dword(pdev, IE31200_MCHBAR_LOW, &u.mchbar_low);
360	pci_read_config_dword(pdev, IE31200_MCHBAR_HIGH, &u.mchbar_high);
361	u.mchbar &= IE31200_MCHBAR_MASK;
362
363	if (u.mchbar != (resource_size_t)u.mchbar) {
364		ie31200_printk(KERN_ERR, "mmio space beyond accessible range (0x%llx)\n",
365			       (unsigned long long)u.mchbar);
366		return NULL;
367	}
368
369	window = ioremap(u.mchbar, IE31200_MMR_WINDOW_SIZE);
370	if (!window)
371		ie31200_printk(KERN_ERR, "Cannot map mmio space at 0x%llx\n",
372			       (unsigned long long)u.mchbar);
373
374	return window;
375}
376
377static void __skl_populate_dimm_info(struct dimm_data *dd, u32 addr_decode,
378				     int chan)
379{
380	dd->size = (addr_decode >> (chan << 4)) & IE31200_MAD_DIMM_SIZE;
381	dd->dual_rank = (addr_decode & (IE31200_MAD_DIMM_A_RANK_SKL << (chan << 4))) ? 1 : 0;
382	dd->x16_width = ((addr_decode & (IE31200_MAD_DIMM_A_WIDTH_SKL << (chan << 4))) >>
383				(IE31200_MAD_DIMM_A_WIDTH_SKL_SHIFT + (chan << 4)));
384}
385
386static void __populate_dimm_info(struct dimm_data *dd, u32 addr_decode,
387				 int chan)
388{
389	dd->size = (addr_decode >> (chan << 3)) & IE31200_MAD_DIMM_SIZE;
390	dd->dual_rank = (addr_decode & (IE31200_MAD_DIMM_A_RANK << chan)) ? 1 : 0;
391	dd->x16_width = (addr_decode & (IE31200_MAD_DIMM_A_WIDTH << chan)) ? 1 : 0;
392}
393
394static void populate_dimm_info(struct dimm_data *dd, u32 addr_decode, int chan,
395			       bool skl)
396{
397	if (skl)
398		__skl_populate_dimm_info(dd, addr_decode, chan);
399	else
400		__populate_dimm_info(dd, addr_decode, chan);
401}
402
403
404static int ie31200_probe1(struct pci_dev *pdev, int dev_idx)
405{
406	int i, j, ret;
407	struct mem_ctl_info *mci = NULL;
408	struct edac_mc_layer layers[2];
409	struct dimm_data dimm_info[IE31200_CHANNELS][IE31200_DIMMS_PER_CHANNEL];
410	void __iomem *window;
411	struct ie31200_priv *priv;
412	u32 addr_decode, mad_offset;
413
414	/*
415	 * Kaby Lake, Coffee Lake seem to work like Skylake. Please re-visit
416	 * this logic when adding new CPU support.
417	 */
418	bool skl = DEVICE_ID_SKYLAKE_OR_LATER(pdev->device);
419
420	edac_dbg(0, "MC:\n");
421
422	if (!ecc_capable(pdev)) {
423		ie31200_printk(KERN_INFO, "No ECC support\n");
424		return -ENODEV;
425	}
426
427	nr_channels = how_many_channels(pdev);
428	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
429	layers[0].size = IE31200_DIMMS;
430	layers[0].is_virt_csrow = true;
431	layers[1].type = EDAC_MC_LAYER_CHANNEL;
432	layers[1].size = nr_channels;
433	layers[1].is_virt_csrow = false;
434	mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
435			    sizeof(struct ie31200_priv));
436	if (!mci)
437		return -ENOMEM;
438
439	window = ie31200_map_mchbar(pdev);
440	if (!window) {
441		ret = -ENODEV;
442		goto fail_free;
443	}
444
445	edac_dbg(3, "MC: init mci\n");
446	mci->pdev = &pdev->dev;
447	if (skl)
448		mci->mtype_cap = MEM_FLAG_DDR4;
449	else
450		mci->mtype_cap = MEM_FLAG_DDR3;
451	mci->edac_ctl_cap = EDAC_FLAG_SECDED;
452	mci->edac_cap = EDAC_FLAG_SECDED;
453	mci->mod_name = EDAC_MOD_STR;
454	mci->ctl_name = ie31200_devs[dev_idx].ctl_name;
455	mci->dev_name = pci_name(pdev);
456	mci->edac_check = ie31200_check;
457	mci->ctl_page_to_phys = NULL;
458	priv = mci->pvt_info;
459	priv->window = window;
460	if (skl) {
461		priv->c0errlog = window + IE31200_C0ECCERRLOG_SKL;
462		priv->c1errlog = window + IE31200_C1ECCERRLOG_SKL;
463		mad_offset = IE31200_MAD_DIMM_0_OFFSET_SKL;
464	} else {
465		priv->c0errlog = window + IE31200_C0ECCERRLOG;
466		priv->c1errlog = window + IE31200_C1ECCERRLOG;
467		mad_offset = IE31200_MAD_DIMM_0_OFFSET;
468	}
469
470	/* populate DIMM info */
471	for (i = 0; i < IE31200_CHANNELS; i++) {
472		addr_decode = readl(window + mad_offset +
473					(i * 4));
474		edac_dbg(0, "addr_decode: 0x%x\n", addr_decode);
475		for (j = 0; j < IE31200_DIMMS_PER_CHANNEL; j++) {
476			populate_dimm_info(&dimm_info[i][j], addr_decode, j,
477					   skl);
478			edac_dbg(0, "size: 0x%x, rank: %d, width: %d\n",
479				 dimm_info[i][j].size,
480				 dimm_info[i][j].dual_rank,
481				 dimm_info[i][j].x16_width);
482		}
483	}
484
485	/*
486	 * The dram rank boundary (DRB) reg values are boundary addresses
487	 * for each DRAM rank with a granularity of 64MB.  DRB regs are
488	 * cumulative; the last one will contain the total memory
489	 * contained in all ranks.
490	 */
491	for (i = 0; i < IE31200_DIMMS_PER_CHANNEL; i++) {
492		for (j = 0; j < IE31200_CHANNELS; j++) {
493			struct dimm_info *dimm;
494			unsigned long nr_pages;
495
496			nr_pages = IE31200_PAGES(dimm_info[j][i].size, skl);
497			if (nr_pages == 0)
498				continue;
499
500			if (dimm_info[j][i].dual_rank) {
501				nr_pages = nr_pages / 2;
502				dimm = edac_get_dimm(mci, (i * 2) + 1, j, 0);
503				dimm->nr_pages = nr_pages;
504				edac_dbg(0, "set nr pages: 0x%lx\n", nr_pages);
505				dimm->grain = 8; /* just a guess */
506				if (skl)
507					dimm->mtype = MEM_DDR4;
508				else
509					dimm->mtype = MEM_DDR3;
510				dimm->dtype = DEV_UNKNOWN;
511				dimm->edac_mode = EDAC_UNKNOWN;
512			}
513			dimm = edac_get_dimm(mci, i * 2, j, 0);
514			dimm->nr_pages = nr_pages;
515			edac_dbg(0, "set nr pages: 0x%lx\n", nr_pages);
516			dimm->grain = 8; /* same guess */
517			if (skl)
518				dimm->mtype = MEM_DDR4;
519			else
520				dimm->mtype = MEM_DDR3;
521			dimm->dtype = DEV_UNKNOWN;
522			dimm->edac_mode = EDAC_UNKNOWN;
523		}
524	}
525
526	ie31200_clear_error_info(mci);
527
528	if (edac_mc_add_mc(mci)) {
529		edac_dbg(3, "MC: failed edac_mc_add_mc()\n");
530		ret = -ENODEV;
531		goto fail_unmap;
532	}
533
534	/* get this far and it's successful */
535	edac_dbg(3, "MC: success\n");
536	return 0;
537
538fail_unmap:
539	iounmap(window);
540
541fail_free:
542	edac_mc_free(mci);
543
544	return ret;
545}
546
547static int ie31200_init_one(struct pci_dev *pdev,
548			    const struct pci_device_id *ent)
549{
550	int rc;
551
552	edac_dbg(0, "MC:\n");
553	if (pci_enable_device(pdev) < 0)
554		return -EIO;
555	rc = ie31200_probe1(pdev, ent->driver_data);
556	if (rc == 0 && !mci_pdev)
557		mci_pdev = pci_dev_get(pdev);
558
559	return rc;
560}
561
562static void ie31200_remove_one(struct pci_dev *pdev)
563{
564	struct mem_ctl_info *mci;
565	struct ie31200_priv *priv;
566
567	edac_dbg(0, "\n");
568	pci_dev_put(mci_pdev);
569	mci_pdev = NULL;
570	mci = edac_mc_del_mc(&pdev->dev);
571	if (!mci)
572		return;
573	priv = mci->pvt_info;
574	iounmap(priv->window);
575	edac_mc_free(mci);
576}
577
578static const struct pci_device_id ie31200_pci_tbl[] = {
579	{ PCI_VEND_DEV(INTEL, IE31200_HB_1),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
580	{ PCI_VEND_DEV(INTEL, IE31200_HB_2),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
581	{ PCI_VEND_DEV(INTEL, IE31200_HB_3),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
582	{ PCI_VEND_DEV(INTEL, IE31200_HB_4),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
583	{ PCI_VEND_DEV(INTEL, IE31200_HB_5),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
584	{ PCI_VEND_DEV(INTEL, IE31200_HB_6),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
585	{ PCI_VEND_DEV(INTEL, IE31200_HB_7),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
586	{ PCI_VEND_DEV(INTEL, IE31200_HB_8),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
587	{ PCI_VEND_DEV(INTEL, IE31200_HB_9),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
588	{ PCI_VEND_DEV(INTEL, IE31200_HB_10),     PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
589	{ PCI_VEND_DEV(INTEL, IE31200_HB_11),     PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
590	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_1),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
591	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_2),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
592	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_3),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
593	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_4),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
594	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_5),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
595	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_6),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
596	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_7),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
597	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_8),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
598	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_9),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
599	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_10), PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
600	{ 0, } /* 0 terminated list. */
601};
602MODULE_DEVICE_TABLE(pci, ie31200_pci_tbl);
603
604static struct pci_driver ie31200_driver = {
605	.name = EDAC_MOD_STR,
606	.probe = ie31200_init_one,
607	.remove = ie31200_remove_one,
608	.id_table = ie31200_pci_tbl,
609};
610
611static int __init ie31200_init(void)
612{
613	int pci_rc, i;
614
615	edac_dbg(3, "MC:\n");
616	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
617	opstate_init();
618
619	pci_rc = pci_register_driver(&ie31200_driver);
620	if (pci_rc < 0)
621		goto fail0;
622
623	if (!mci_pdev) {
624		ie31200_registered = 0;
625		for (i = 0; ie31200_pci_tbl[i].vendor != 0; i++) {
626			mci_pdev = pci_get_device(ie31200_pci_tbl[i].vendor,
627						  ie31200_pci_tbl[i].device,
628						  NULL);
629			if (mci_pdev)
630				break;
631		}
632		if (!mci_pdev) {
633			edac_dbg(0, "ie31200 pci_get_device fail\n");
634			pci_rc = -ENODEV;
635			goto fail1;
636		}
637		pci_rc = ie31200_init_one(mci_pdev, &ie31200_pci_tbl[i]);
638		if (pci_rc < 0) {
639			edac_dbg(0, "ie31200 init fail\n");
640			pci_rc = -ENODEV;
641			goto fail1;
642		}
643	}
644	return 0;
645
646fail1:
647	pci_unregister_driver(&ie31200_driver);
648fail0:
649	pci_dev_put(mci_pdev);
650
651	return pci_rc;
652}
653
654static void __exit ie31200_exit(void)
655{
656	edac_dbg(3, "MC:\n");
657	pci_unregister_driver(&ie31200_driver);
658	if (!ie31200_registered)
659		ie31200_remove_one(mci_pdev);
660}
661
662module_init(ie31200_init);
663module_exit(ie31200_exit);
664
665MODULE_LICENSE("GPL");
666MODULE_AUTHOR("Jason Baron <jbaron@akamai.com>");
667MODULE_DESCRIPTION("MC support for Intel Processor E31200 memory hub controllers");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Intel E3-1200
  4 * Copyright (C) 2014 Jason Baron <jbaron@akamai.com>
  5 *
  6 * Support for the E3-1200 processor family. Heavily based on previous
  7 * Intel EDAC drivers.
  8 *
  9 * Since the DRAM controller is on the cpu chip, we can use its PCI device
 10 * id to identify these processors.
 11 *
 12 * PCI DRAM controller device ids (Taken from The PCI ID Repository - http://pci-ids.ucw.cz/)
 13 *
 14 * 0108: Xeon E3-1200 Processor Family DRAM Controller
 15 * 010c: Xeon E3-1200/2nd Generation Core Processor Family DRAM Controller
 16 * 0150: Xeon E3-1200 v2/3rd Gen Core processor DRAM Controller
 17 * 0158: Xeon E3-1200 v2/Ivy Bridge DRAM Controller
 18 * 015c: Xeon E3-1200 v2/3rd Gen Core processor DRAM Controller
 19 * 0c04: Xeon E3-1200 v3/4th Gen Core Processor DRAM Controller
 20 * 0c08: Xeon E3-1200 v3 Processor DRAM Controller
 21 * 1918: Xeon E3-1200 v5 Skylake Host Bridge/DRAM Registers
 22 * 5918: Xeon E3-1200 Xeon E3-1200 v6/7th Gen Core Processor Host Bridge/DRAM Registers
 
 
 23 * 3e..: 8th/9th Gen Core Processor Host Bridge/DRAM Registers
 24 *
 25 * Based on Intel specification:
 26 * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/xeon-e3-1200v3-vol-2-datasheet.pdf
 27 * http://www.intel.com/content/www/us/en/processors/xeon/xeon-e3-1200-family-vol-2-datasheet.html
 28 * http://www.intel.com/content/www/us/en/processors/core/7th-gen-core-family-mobile-h-processor-lines-datasheet-vol-2.html
 
 
 29 * https://www.intel.com/content/www/us/en/products/docs/processors/core/8th-gen-core-family-datasheet-vol-2.html
 30 *
 31 * According to the above datasheet (p.16):
 32 * "
 33 * 6. Software must not access B0/D0/F0 32-bit memory-mapped registers with
 34 * requests that cross a DW boundary.
 35 * "
 36 *
 37 * Thus, we make use of the explicit: lo_hi_readq(), which breaks the readq into
 38 * 2 readl() calls. This restriction may be lifted in subsequent chip releases,
 39 * but lo_hi_readq() ensures that we are safe across all e3-1200 processors.
 40 */
 41
 42#include <linux/module.h>
 43#include <linux/init.h>
 44#include <linux/pci.h>
 45#include <linux/pci_ids.h>
 46#include <linux/edac.h>
 47
 48#include <linux/io-64-nonatomic-lo-hi.h>
 49#include "edac_module.h"
 50
 51#define EDAC_MOD_STR "ie31200_edac"
 52
 53#define ie31200_printk(level, fmt, arg...) \
 54	edac_printk(level, "ie31200", fmt, ##arg)
 55
 56#define PCI_DEVICE_ID_INTEL_IE31200_HB_1 0x0108
 57#define PCI_DEVICE_ID_INTEL_IE31200_HB_2 0x010c
 58#define PCI_DEVICE_ID_INTEL_IE31200_HB_3 0x0150
 59#define PCI_DEVICE_ID_INTEL_IE31200_HB_4 0x0158
 60#define PCI_DEVICE_ID_INTEL_IE31200_HB_5 0x015c
 61#define PCI_DEVICE_ID_INTEL_IE31200_HB_6 0x0c04
 62#define PCI_DEVICE_ID_INTEL_IE31200_HB_7 0x0c08
 63#define PCI_DEVICE_ID_INTEL_IE31200_HB_8 0x1918
 64#define PCI_DEVICE_ID_INTEL_IE31200_HB_9 0x5918
 
 
 65
 66/* Coffee Lake-S */
 67#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_MASK 0x3e00
 68#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_1    0x3e0f
 69#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_2    0x3e18
 70#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_3    0x3e1f
 71#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_4    0x3e30
 72#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_5    0x3e31
 73#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_6    0x3e32
 74#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_7    0x3e33
 75#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_8    0x3ec2
 76#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_9    0x3ec6
 77#define PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_10   0x3eca
 78
 79/* Test if HB is for Skylake or later. */
 80#define DEVICE_ID_SKYLAKE_OR_LATER(did)                                        \
 81	(((did) == PCI_DEVICE_ID_INTEL_IE31200_HB_8) ||                        \
 82	 ((did) == PCI_DEVICE_ID_INTEL_IE31200_HB_9) ||                        \
 
 
 83	 (((did) & PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_MASK) ==                 \
 84	  PCI_DEVICE_ID_INTEL_IE31200_HB_CFL_MASK))
 85
 86#define IE31200_DIMMS			4
 87#define IE31200_RANKS			8
 88#define IE31200_RANKS_PER_CHANNEL	4
 89#define IE31200_DIMMS_PER_CHANNEL	2
 90#define IE31200_CHANNELS		2
 91
 92/* Intel IE31200 register addresses - device 0 function 0 - DRAM Controller */
 93#define IE31200_MCHBAR_LOW		0x48
 94#define IE31200_MCHBAR_HIGH		0x4c
 95#define IE31200_MCHBAR_MASK		GENMASK_ULL(38, 15)
 96#define IE31200_MMR_WINDOW_SIZE		BIT(15)
 97
 98/*
 99 * Error Status Register (16b)
100 *
101 * 15    reserved
102 * 14    Isochronous TBWRR Run Behind FIFO Full
103 *       (ITCV)
104 * 13    Isochronous TBWRR Run Behind FIFO Put
105 *       (ITSTV)
106 * 12    reserved
107 * 11    MCH Thermal Sensor Event
108 *       for SMI/SCI/SERR (GTSE)
109 * 10    reserved
110 *  9    LOCK to non-DRAM Memory Flag (LCKF)
111 *  8    reserved
112 *  7    DRAM Throttle Flag (DTF)
113 *  6:2  reserved
114 *  1    Multi-bit DRAM ECC Error Flag (DMERR)
115 *  0    Single-bit DRAM ECC Error Flag (DSERR)
116 */
117#define IE31200_ERRSTS			0xc8
118#define IE31200_ERRSTS_UE		BIT(1)
119#define IE31200_ERRSTS_CE		BIT(0)
120#define IE31200_ERRSTS_BITS		(IE31200_ERRSTS_UE | IE31200_ERRSTS_CE)
121
122/*
123 * Channel 0 ECC Error Log (64b)
124 *
125 * 63:48 Error Column Address (ERRCOL)
126 * 47:32 Error Row Address (ERRROW)
127 * 31:29 Error Bank Address (ERRBANK)
128 * 28:27 Error Rank Address (ERRRANK)
129 * 26:24 reserved
130 * 23:16 Error Syndrome (ERRSYND)
131 * 15: 2 reserved
132 *    1  Multiple Bit Error Status (MERRSTS)
133 *    0  Correctable Error Status (CERRSTS)
134 */
135
136#define IE31200_C0ECCERRLOG			0x40c8
137#define IE31200_C1ECCERRLOG			0x44c8
138#define IE31200_C0ECCERRLOG_SKL			0x4048
139#define IE31200_C1ECCERRLOG_SKL			0x4448
140#define IE31200_ECCERRLOG_CE			BIT(0)
141#define IE31200_ECCERRLOG_UE			BIT(1)
142#define IE31200_ECCERRLOG_RANK_BITS		GENMASK_ULL(28, 27)
143#define IE31200_ECCERRLOG_RANK_SHIFT		27
144#define IE31200_ECCERRLOG_SYNDROME_BITS		GENMASK_ULL(23, 16)
145#define IE31200_ECCERRLOG_SYNDROME_SHIFT	16
146
147#define IE31200_ECCERRLOG_SYNDROME(log)		   \
148	((log & IE31200_ECCERRLOG_SYNDROME_BITS) >> \
149	 IE31200_ECCERRLOG_SYNDROME_SHIFT)
150
151#define IE31200_CAPID0			0xe4
152#define IE31200_CAPID0_PDCD		BIT(4)
153#define IE31200_CAPID0_DDPCD		BIT(6)
154#define IE31200_CAPID0_ECC		BIT(1)
155
156#define IE31200_MAD_DIMM_0_OFFSET		0x5004
157#define IE31200_MAD_DIMM_0_OFFSET_SKL		0x500C
158#define IE31200_MAD_DIMM_SIZE			GENMASK_ULL(7, 0)
159#define IE31200_MAD_DIMM_A_RANK			BIT(17)
160#define IE31200_MAD_DIMM_A_RANK_SHIFT		17
161#define IE31200_MAD_DIMM_A_RANK_SKL		BIT(10)
162#define IE31200_MAD_DIMM_A_RANK_SKL_SHIFT	10
163#define IE31200_MAD_DIMM_A_WIDTH		BIT(19)
164#define IE31200_MAD_DIMM_A_WIDTH_SHIFT		19
165#define IE31200_MAD_DIMM_A_WIDTH_SKL		GENMASK_ULL(9, 8)
166#define IE31200_MAD_DIMM_A_WIDTH_SKL_SHIFT	8
167
168/* Skylake reports 1GB increments, everything else is 256MB */
169#define IE31200_PAGES(n, skl)	\
170	(n << (28 + (2 * skl) - PAGE_SHIFT))
171
172static int nr_channels;
173static struct pci_dev *mci_pdev;
174static int ie31200_registered = 1;
175
176struct ie31200_priv {
177	void __iomem *window;
178	void __iomem *c0errlog;
179	void __iomem *c1errlog;
180};
181
182enum ie31200_chips {
183	IE31200 = 0,
184};
185
186struct ie31200_dev_info {
187	const char *ctl_name;
188};
189
190struct ie31200_error_info {
191	u16 errsts;
192	u16 errsts2;
193	u64 eccerrlog[IE31200_CHANNELS];
194};
195
196static const struct ie31200_dev_info ie31200_devs[] = {
197	[IE31200] = {
198		.ctl_name = "IE31200"
199	},
200};
201
202struct dimm_data {
203	u8 size; /* in multiples of 256MB, except Skylake is 1GB */
204	u8 dual_rank : 1,
205	   x16_width : 2; /* 0 means x8 width */
206};
207
208static int how_many_channels(struct pci_dev *pdev)
209{
210	int n_channels;
211	unsigned char capid0_2b; /* 2nd byte of CAPID0 */
212
213	pci_read_config_byte(pdev, IE31200_CAPID0 + 1, &capid0_2b);
214
215	/* check PDCD: Dual Channel Disable */
216	if (capid0_2b & IE31200_CAPID0_PDCD) {
217		edac_dbg(0, "In single channel mode\n");
218		n_channels = 1;
219	} else {
220		edac_dbg(0, "In dual channel mode\n");
221		n_channels = 2;
222	}
223
224	/* check DDPCD - check if both channels are filled */
225	if (capid0_2b & IE31200_CAPID0_DDPCD)
226		edac_dbg(0, "2 DIMMS per channel disabled\n");
227	else
228		edac_dbg(0, "2 DIMMS per channel enabled\n");
229
230	return n_channels;
231}
232
233static bool ecc_capable(struct pci_dev *pdev)
234{
235	unsigned char capid0_4b; /* 4th byte of CAPID0 */
236
237	pci_read_config_byte(pdev, IE31200_CAPID0 + 3, &capid0_4b);
238	if (capid0_4b & IE31200_CAPID0_ECC)
239		return false;
240	return true;
241}
242
243static int eccerrlog_row(u64 log)
244{
245	return ((log & IE31200_ECCERRLOG_RANK_BITS) >>
246				IE31200_ECCERRLOG_RANK_SHIFT);
247}
248
249static void ie31200_clear_error_info(struct mem_ctl_info *mci)
250{
251	/*
252	 * Clear any error bits.
253	 * (Yes, we really clear bits by writing 1 to them.)
254	 */
255	pci_write_bits16(to_pci_dev(mci->pdev), IE31200_ERRSTS,
256			 IE31200_ERRSTS_BITS, IE31200_ERRSTS_BITS);
257}
258
259static void ie31200_get_and_clear_error_info(struct mem_ctl_info *mci,
260					     struct ie31200_error_info *info)
261{
262	struct pci_dev *pdev;
263	struct ie31200_priv *priv = mci->pvt_info;
264
265	pdev = to_pci_dev(mci->pdev);
266
267	/*
268	 * This is a mess because there is no atomic way to read all the
269	 * registers at once and the registers can transition from CE being
270	 * overwritten by UE.
271	 */
272	pci_read_config_word(pdev, IE31200_ERRSTS, &info->errsts);
273	if (!(info->errsts & IE31200_ERRSTS_BITS))
274		return;
275
276	info->eccerrlog[0] = lo_hi_readq(priv->c0errlog);
277	if (nr_channels == 2)
278		info->eccerrlog[1] = lo_hi_readq(priv->c1errlog);
279
280	pci_read_config_word(pdev, IE31200_ERRSTS, &info->errsts2);
281
282	/*
283	 * If the error is the same for both reads then the first set
284	 * of reads is valid.  If there is a change then there is a CE
285	 * with no info and the second set of reads is valid and
286	 * should be UE info.
287	 */
288	if ((info->errsts ^ info->errsts2) & IE31200_ERRSTS_BITS) {
289		info->eccerrlog[0] = lo_hi_readq(priv->c0errlog);
290		if (nr_channels == 2)
291			info->eccerrlog[1] =
292				lo_hi_readq(priv->c1errlog);
293	}
294
295	ie31200_clear_error_info(mci);
296}
297
298static void ie31200_process_error_info(struct mem_ctl_info *mci,
299				       struct ie31200_error_info *info)
300{
301	int channel;
302	u64 log;
303
304	if (!(info->errsts & IE31200_ERRSTS_BITS))
305		return;
306
307	if ((info->errsts ^ info->errsts2) & IE31200_ERRSTS_BITS) {
308		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
309				     -1, -1, -1, "UE overwrote CE", "");
310		info->errsts = info->errsts2;
311	}
312
313	for (channel = 0; channel < nr_channels; channel++) {
314		log = info->eccerrlog[channel];
315		if (log & IE31200_ECCERRLOG_UE) {
316			edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
317					     0, 0, 0,
318					     eccerrlog_row(log),
319					     channel, -1,
320					     "ie31200 UE", "");
321		} else if (log & IE31200_ECCERRLOG_CE) {
322			edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
323					     0, 0,
324					     IE31200_ECCERRLOG_SYNDROME(log),
325					     eccerrlog_row(log),
326					     channel, -1,
327					     "ie31200 CE", "");
328		}
329	}
330}
331
332static void ie31200_check(struct mem_ctl_info *mci)
333{
334	struct ie31200_error_info info;
335
336	edac_dbg(1, "MC%d\n", mci->mc_idx);
337	ie31200_get_and_clear_error_info(mci, &info);
338	ie31200_process_error_info(mci, &info);
339}
340
341static void __iomem *ie31200_map_mchbar(struct pci_dev *pdev)
342{
343	union {
344		u64 mchbar;
345		struct {
346			u32 mchbar_low;
347			u32 mchbar_high;
348		};
349	} u;
350	void __iomem *window;
351
352	pci_read_config_dword(pdev, IE31200_MCHBAR_LOW, &u.mchbar_low);
353	pci_read_config_dword(pdev, IE31200_MCHBAR_HIGH, &u.mchbar_high);
354	u.mchbar &= IE31200_MCHBAR_MASK;
355
356	if (u.mchbar != (resource_size_t)u.mchbar) {
357		ie31200_printk(KERN_ERR, "mmio space beyond accessible range (0x%llx)\n",
358			       (unsigned long long)u.mchbar);
359		return NULL;
360	}
361
362	window = ioremap(u.mchbar, IE31200_MMR_WINDOW_SIZE);
363	if (!window)
364		ie31200_printk(KERN_ERR, "Cannot map mmio space at 0x%llx\n",
365			       (unsigned long long)u.mchbar);
366
367	return window;
368}
369
370static void __skl_populate_dimm_info(struct dimm_data *dd, u32 addr_decode,
371				     int chan)
372{
373	dd->size = (addr_decode >> (chan << 4)) & IE31200_MAD_DIMM_SIZE;
374	dd->dual_rank = (addr_decode & (IE31200_MAD_DIMM_A_RANK_SKL << (chan << 4))) ? 1 : 0;
375	dd->x16_width = ((addr_decode & (IE31200_MAD_DIMM_A_WIDTH_SKL << (chan << 4))) >>
376				(IE31200_MAD_DIMM_A_WIDTH_SKL_SHIFT + (chan << 4)));
377}
378
379static void __populate_dimm_info(struct dimm_data *dd, u32 addr_decode,
380				 int chan)
381{
382	dd->size = (addr_decode >> (chan << 3)) & IE31200_MAD_DIMM_SIZE;
383	dd->dual_rank = (addr_decode & (IE31200_MAD_DIMM_A_RANK << chan)) ? 1 : 0;
384	dd->x16_width = (addr_decode & (IE31200_MAD_DIMM_A_WIDTH << chan)) ? 1 : 0;
385}
386
387static void populate_dimm_info(struct dimm_data *dd, u32 addr_decode, int chan,
388			       bool skl)
389{
390	if (skl)
391		__skl_populate_dimm_info(dd, addr_decode, chan);
392	else
393		__populate_dimm_info(dd, addr_decode, chan);
394}
395
396
397static int ie31200_probe1(struct pci_dev *pdev, int dev_idx)
398{
399	int i, j, ret;
400	struct mem_ctl_info *mci = NULL;
401	struct edac_mc_layer layers[2];
402	struct dimm_data dimm_info[IE31200_CHANNELS][IE31200_DIMMS_PER_CHANNEL];
403	void __iomem *window;
404	struct ie31200_priv *priv;
405	u32 addr_decode, mad_offset;
406
407	/*
408	 * Kaby Lake, Coffee Lake seem to work like Skylake. Please re-visit
409	 * this logic when adding new CPU support.
410	 */
411	bool skl = DEVICE_ID_SKYLAKE_OR_LATER(pdev->device);
412
413	edac_dbg(0, "MC:\n");
414
415	if (!ecc_capable(pdev)) {
416		ie31200_printk(KERN_INFO, "No ECC support\n");
417		return -ENODEV;
418	}
419
420	nr_channels = how_many_channels(pdev);
421	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
422	layers[0].size = IE31200_DIMMS;
423	layers[0].is_virt_csrow = true;
424	layers[1].type = EDAC_MC_LAYER_CHANNEL;
425	layers[1].size = nr_channels;
426	layers[1].is_virt_csrow = false;
427	mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
428			    sizeof(struct ie31200_priv));
429	if (!mci)
430		return -ENOMEM;
431
432	window = ie31200_map_mchbar(pdev);
433	if (!window) {
434		ret = -ENODEV;
435		goto fail_free;
436	}
437
438	edac_dbg(3, "MC: init mci\n");
439	mci->pdev = &pdev->dev;
440	if (skl)
441		mci->mtype_cap = MEM_FLAG_DDR4;
442	else
443		mci->mtype_cap = MEM_FLAG_DDR3;
444	mci->edac_ctl_cap = EDAC_FLAG_SECDED;
445	mci->edac_cap = EDAC_FLAG_SECDED;
446	mci->mod_name = EDAC_MOD_STR;
447	mci->ctl_name = ie31200_devs[dev_idx].ctl_name;
448	mci->dev_name = pci_name(pdev);
449	mci->edac_check = ie31200_check;
450	mci->ctl_page_to_phys = NULL;
451	priv = mci->pvt_info;
452	priv->window = window;
453	if (skl) {
454		priv->c0errlog = window + IE31200_C0ECCERRLOG_SKL;
455		priv->c1errlog = window + IE31200_C1ECCERRLOG_SKL;
456		mad_offset = IE31200_MAD_DIMM_0_OFFSET_SKL;
457	} else {
458		priv->c0errlog = window + IE31200_C0ECCERRLOG;
459		priv->c1errlog = window + IE31200_C1ECCERRLOG;
460		mad_offset = IE31200_MAD_DIMM_0_OFFSET;
461	}
462
463	/* populate DIMM info */
464	for (i = 0; i < IE31200_CHANNELS; i++) {
465		addr_decode = readl(window + mad_offset +
466					(i * 4));
467		edac_dbg(0, "addr_decode: 0x%x\n", addr_decode);
468		for (j = 0; j < IE31200_DIMMS_PER_CHANNEL; j++) {
469			populate_dimm_info(&dimm_info[i][j], addr_decode, j,
470					   skl);
471			edac_dbg(0, "size: 0x%x, rank: %d, width: %d\n",
472				 dimm_info[i][j].size,
473				 dimm_info[i][j].dual_rank,
474				 dimm_info[i][j].x16_width);
475		}
476	}
477
478	/*
479	 * The dram rank boundary (DRB) reg values are boundary addresses
480	 * for each DRAM rank with a granularity of 64MB.  DRB regs are
481	 * cumulative; the last one will contain the total memory
482	 * contained in all ranks.
483	 */
484	for (i = 0; i < IE31200_DIMMS_PER_CHANNEL; i++) {
485		for (j = 0; j < IE31200_CHANNELS; j++) {
486			struct dimm_info *dimm;
487			unsigned long nr_pages;
488
489			nr_pages = IE31200_PAGES(dimm_info[j][i].size, skl);
490			if (nr_pages == 0)
491				continue;
492
493			if (dimm_info[j][i].dual_rank) {
494				nr_pages = nr_pages / 2;
495				dimm = edac_get_dimm(mci, (i * 2) + 1, j, 0);
496				dimm->nr_pages = nr_pages;
497				edac_dbg(0, "set nr pages: 0x%lx\n", nr_pages);
498				dimm->grain = 8; /* just a guess */
499				if (skl)
500					dimm->mtype = MEM_DDR4;
501				else
502					dimm->mtype = MEM_DDR3;
503				dimm->dtype = DEV_UNKNOWN;
504				dimm->edac_mode = EDAC_UNKNOWN;
505			}
506			dimm = edac_get_dimm(mci, i * 2, j, 0);
507			dimm->nr_pages = nr_pages;
508			edac_dbg(0, "set nr pages: 0x%lx\n", nr_pages);
509			dimm->grain = 8; /* same guess */
510			if (skl)
511				dimm->mtype = MEM_DDR4;
512			else
513				dimm->mtype = MEM_DDR3;
514			dimm->dtype = DEV_UNKNOWN;
515			dimm->edac_mode = EDAC_UNKNOWN;
516		}
517	}
518
519	ie31200_clear_error_info(mci);
520
521	if (edac_mc_add_mc(mci)) {
522		edac_dbg(3, "MC: failed edac_mc_add_mc()\n");
523		ret = -ENODEV;
524		goto fail_unmap;
525	}
526
527	/* get this far and it's successful */
528	edac_dbg(3, "MC: success\n");
529	return 0;
530
531fail_unmap:
532	iounmap(window);
533
534fail_free:
535	edac_mc_free(mci);
536
537	return ret;
538}
539
540static int ie31200_init_one(struct pci_dev *pdev,
541			    const struct pci_device_id *ent)
542{
543	int rc;
544
545	edac_dbg(0, "MC:\n");
546	if (pci_enable_device(pdev) < 0)
547		return -EIO;
548	rc = ie31200_probe1(pdev, ent->driver_data);
549	if (rc == 0 && !mci_pdev)
550		mci_pdev = pci_dev_get(pdev);
551
552	return rc;
553}
554
555static void ie31200_remove_one(struct pci_dev *pdev)
556{
557	struct mem_ctl_info *mci;
558	struct ie31200_priv *priv;
559
560	edac_dbg(0, "\n");
561	pci_dev_put(mci_pdev);
562	mci_pdev = NULL;
563	mci = edac_mc_del_mc(&pdev->dev);
564	if (!mci)
565		return;
566	priv = mci->pvt_info;
567	iounmap(priv->window);
568	edac_mc_free(mci);
569}
570
571static const struct pci_device_id ie31200_pci_tbl[] = {
572	{ PCI_VEND_DEV(INTEL, IE31200_HB_1),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
573	{ PCI_VEND_DEV(INTEL, IE31200_HB_2),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
574	{ PCI_VEND_DEV(INTEL, IE31200_HB_3),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
575	{ PCI_VEND_DEV(INTEL, IE31200_HB_4),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
576	{ PCI_VEND_DEV(INTEL, IE31200_HB_5),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
577	{ PCI_VEND_DEV(INTEL, IE31200_HB_6),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
578	{ PCI_VEND_DEV(INTEL, IE31200_HB_7),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
579	{ PCI_VEND_DEV(INTEL, IE31200_HB_8),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
580	{ PCI_VEND_DEV(INTEL, IE31200_HB_9),      PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
 
 
581	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_1),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
582	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_2),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
583	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_3),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
584	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_4),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
585	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_5),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
586	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_6),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
587	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_7),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
588	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_8),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
589	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_9),  PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
590	{ PCI_VEND_DEV(INTEL, IE31200_HB_CFL_10), PCI_ANY_ID, PCI_ANY_ID, 0, 0, IE31200 },
591	{ 0, } /* 0 terminated list. */
592};
593MODULE_DEVICE_TABLE(pci, ie31200_pci_tbl);
594
595static struct pci_driver ie31200_driver = {
596	.name = EDAC_MOD_STR,
597	.probe = ie31200_init_one,
598	.remove = ie31200_remove_one,
599	.id_table = ie31200_pci_tbl,
600};
601
602static int __init ie31200_init(void)
603{
604	int pci_rc, i;
605
606	edac_dbg(3, "MC:\n");
607	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
608	opstate_init();
609
610	pci_rc = pci_register_driver(&ie31200_driver);
611	if (pci_rc < 0)
612		goto fail0;
613
614	if (!mci_pdev) {
615		ie31200_registered = 0;
616		for (i = 0; ie31200_pci_tbl[i].vendor != 0; i++) {
617			mci_pdev = pci_get_device(ie31200_pci_tbl[i].vendor,
618						  ie31200_pci_tbl[i].device,
619						  NULL);
620			if (mci_pdev)
621				break;
622		}
623		if (!mci_pdev) {
624			edac_dbg(0, "ie31200 pci_get_device fail\n");
625			pci_rc = -ENODEV;
626			goto fail1;
627		}
628		pci_rc = ie31200_init_one(mci_pdev, &ie31200_pci_tbl[i]);
629		if (pci_rc < 0) {
630			edac_dbg(0, "ie31200 init fail\n");
631			pci_rc = -ENODEV;
632			goto fail1;
633		}
634	}
635	return 0;
636
637fail1:
638	pci_unregister_driver(&ie31200_driver);
639fail0:
640	pci_dev_put(mci_pdev);
641
642	return pci_rc;
643}
644
645static void __exit ie31200_exit(void)
646{
647	edac_dbg(3, "MC:\n");
648	pci_unregister_driver(&ie31200_driver);
649	if (!ie31200_registered)
650		ie31200_remove_one(mci_pdev);
651}
652
653module_init(ie31200_init);
654module_exit(ie31200_exit);
655
656MODULE_LICENSE("GPL");
657MODULE_AUTHOR("Jason Baron <jbaron@akamai.com>");
658MODULE_DESCRIPTION("MC support for Intel Processor E31200 memory hub controllers");