xref: /openbmc/linux/drivers/edac/skx_base.c (revision 71b1e3ba)
198f2fc82SQiuxu Zhuo // SPDX-License-Identifier: GPL-2.0
298f2fc82SQiuxu Zhuo /*
398f2fc82SQiuxu Zhuo  * EDAC driver for Intel(R) Xeon(R) Skylake processors
498f2fc82SQiuxu Zhuo  * Copyright (c) 2016, Intel Corporation.
598f2fc82SQiuxu Zhuo  */
698f2fc82SQiuxu Zhuo 
798f2fc82SQiuxu Zhuo #include <linux/kernel.h>
898f2fc82SQiuxu Zhuo #include <linux/processor.h>
998f2fc82SQiuxu Zhuo #include <asm/cpu_device_id.h>
1098f2fc82SQiuxu Zhuo #include <asm/intel-family.h>
1198f2fc82SQiuxu Zhuo #include <asm/mce.h>
1298f2fc82SQiuxu Zhuo 
1398f2fc82SQiuxu Zhuo #include "edac_module.h"
1498f2fc82SQiuxu Zhuo #include "skx_common.h"
1598f2fc82SQiuxu Zhuo 
1698f2fc82SQiuxu Zhuo #define EDAC_MOD_STR    "skx_edac"
1798f2fc82SQiuxu Zhuo 
1898f2fc82SQiuxu Zhuo /*
1998f2fc82SQiuxu Zhuo  * Debug macros
2098f2fc82SQiuxu Zhuo  */
2198f2fc82SQiuxu Zhuo #define skx_printk(level, fmt, arg...)			\
2298f2fc82SQiuxu Zhuo 	edac_printk(level, "skx", fmt, ##arg)
2398f2fc82SQiuxu Zhuo 
2498f2fc82SQiuxu Zhuo #define skx_mc_printk(mci, level, fmt, arg...)		\
2598f2fc82SQiuxu Zhuo 	edac_mc_chipset_printk(mci, level, "skx", fmt, ##arg)
2698f2fc82SQiuxu Zhuo 
2798f2fc82SQiuxu Zhuo static struct list_head *skx_edac_list;
2898f2fc82SQiuxu Zhuo 
2998f2fc82SQiuxu Zhuo static u64 skx_tolm, skx_tohm;
3098f2fc82SQiuxu Zhuo static int skx_num_sockets;
3198f2fc82SQiuxu Zhuo static unsigned int nvdimm_count;
3298f2fc82SQiuxu Zhuo 
3398f2fc82SQiuxu Zhuo #define	MASK26	0x3FFFFFF		/* Mask for 2^26 */
3498f2fc82SQiuxu Zhuo #define MASK29	0x1FFFFFFF		/* Mask for 2^29 */
3598f2fc82SQiuxu Zhuo 
get_skx_dev(struct pci_bus * bus,u8 idx)3698f2fc82SQiuxu Zhuo static struct skx_dev *get_skx_dev(struct pci_bus *bus, u8 idx)
3798f2fc82SQiuxu Zhuo {
3898f2fc82SQiuxu Zhuo 	struct skx_dev *d;
3998f2fc82SQiuxu Zhuo 
4098f2fc82SQiuxu Zhuo 	list_for_each_entry(d, skx_edac_list, list) {
4198f2fc82SQiuxu Zhuo 		if (d->seg == pci_domain_nr(bus) && d->bus[idx] == bus->number)
4298f2fc82SQiuxu Zhuo 			return d;
4398f2fc82SQiuxu Zhuo 	}
4498f2fc82SQiuxu Zhuo 
4598f2fc82SQiuxu Zhuo 	return NULL;
4698f2fc82SQiuxu Zhuo }
4798f2fc82SQiuxu Zhuo 
4898f2fc82SQiuxu Zhuo enum munittype {
49e80634a7STony Luck 	CHAN0, CHAN1, CHAN2, SAD_ALL, UTIL_ALL, SAD,
50e80634a7STony Luck 	ERRCHAN0, ERRCHAN1, ERRCHAN2,
5198f2fc82SQiuxu Zhuo };
5298f2fc82SQiuxu Zhuo 
5398f2fc82SQiuxu Zhuo struct munit {
5498f2fc82SQiuxu Zhuo 	u16	did;
5598f2fc82SQiuxu Zhuo 	u16	devfn[SKX_NUM_IMC];
5698f2fc82SQiuxu Zhuo 	u8	busidx;
5798f2fc82SQiuxu Zhuo 	u8	per_socket;
5898f2fc82SQiuxu Zhuo 	enum munittype mtype;
5998f2fc82SQiuxu Zhuo };
6098f2fc82SQiuxu Zhuo 
6198f2fc82SQiuxu Zhuo /*
6298f2fc82SQiuxu Zhuo  * List of PCI device ids that we need together with some device
6398f2fc82SQiuxu Zhuo  * number and function numbers to tell which memory controller the
6498f2fc82SQiuxu Zhuo  * device belongs to.
6598f2fc82SQiuxu Zhuo  */
6698f2fc82SQiuxu Zhuo static const struct munit skx_all_munits[] = {
6798f2fc82SQiuxu Zhuo 	{ 0x2054, { }, 1, 1, SAD_ALL },
6898f2fc82SQiuxu Zhuo 	{ 0x2055, { }, 1, 1, UTIL_ALL },
6998f2fc82SQiuxu Zhuo 	{ 0x2040, { PCI_DEVFN(10, 0), PCI_DEVFN(12, 0) }, 2, 2, CHAN0 },
7098f2fc82SQiuxu Zhuo 	{ 0x2044, { PCI_DEVFN(10, 4), PCI_DEVFN(12, 4) }, 2, 2, CHAN1 },
7198f2fc82SQiuxu Zhuo 	{ 0x2048, { PCI_DEVFN(11, 0), PCI_DEVFN(13, 0) }, 2, 2, CHAN2 },
72e80634a7STony Luck 	{ 0x2043, { PCI_DEVFN(10, 3), PCI_DEVFN(12, 3) }, 2, 2, ERRCHAN0 },
73e80634a7STony Luck 	{ 0x2047, { PCI_DEVFN(10, 7), PCI_DEVFN(12, 7) }, 2, 2, ERRCHAN1 },
74e80634a7STony Luck 	{ 0x204b, { PCI_DEVFN(11, 3), PCI_DEVFN(13, 3) }, 2, 2, ERRCHAN2 },
7598f2fc82SQiuxu Zhuo 	{ 0x208e, { }, 1, 0, SAD },
7698f2fc82SQiuxu Zhuo 	{ }
7798f2fc82SQiuxu Zhuo };
7898f2fc82SQiuxu Zhuo 
get_all_munits(const struct munit * m)7998f2fc82SQiuxu Zhuo static int get_all_munits(const struct munit *m)
8098f2fc82SQiuxu Zhuo {
8198f2fc82SQiuxu Zhuo 	struct pci_dev *pdev, *prev;
8298f2fc82SQiuxu Zhuo 	struct skx_dev *d;
8398f2fc82SQiuxu Zhuo 	u32 reg;
8498f2fc82SQiuxu Zhuo 	int i = 0, ndev = 0;
8598f2fc82SQiuxu Zhuo 
8698f2fc82SQiuxu Zhuo 	prev = NULL;
8798f2fc82SQiuxu Zhuo 	for (;;) {
8898f2fc82SQiuxu Zhuo 		pdev = pci_get_device(PCI_VENDOR_ID_INTEL, m->did, prev);
8998f2fc82SQiuxu Zhuo 		if (!pdev)
9098f2fc82SQiuxu Zhuo 			break;
9198f2fc82SQiuxu Zhuo 		ndev++;
9298f2fc82SQiuxu Zhuo 		if (m->per_socket == SKX_NUM_IMC) {
9398f2fc82SQiuxu Zhuo 			for (i = 0; i < SKX_NUM_IMC; i++)
9498f2fc82SQiuxu Zhuo 				if (m->devfn[i] == pdev->devfn)
9598f2fc82SQiuxu Zhuo 					break;
9698f2fc82SQiuxu Zhuo 			if (i == SKX_NUM_IMC)
9798f2fc82SQiuxu Zhuo 				goto fail;
9898f2fc82SQiuxu Zhuo 		}
9998f2fc82SQiuxu Zhuo 		d = get_skx_dev(pdev->bus, m->busidx);
10098f2fc82SQiuxu Zhuo 		if (!d)
10198f2fc82SQiuxu Zhuo 			goto fail;
10298f2fc82SQiuxu Zhuo 
10398f2fc82SQiuxu Zhuo 		/* Be sure that the device is enabled */
10498f2fc82SQiuxu Zhuo 		if (unlikely(pci_enable_device(pdev) < 0)) {
10598f2fc82SQiuxu Zhuo 			skx_printk(KERN_ERR, "Couldn't enable device %04x:%04x\n",
10698f2fc82SQiuxu Zhuo 				   PCI_VENDOR_ID_INTEL, m->did);
10798f2fc82SQiuxu Zhuo 			goto fail;
10898f2fc82SQiuxu Zhuo 		}
10998f2fc82SQiuxu Zhuo 
11098f2fc82SQiuxu Zhuo 		switch (m->mtype) {
111e80634a7STony Luck 		case CHAN0:
112e80634a7STony Luck 		case CHAN1:
113e80634a7STony Luck 		case CHAN2:
11498f2fc82SQiuxu Zhuo 			pci_dev_get(pdev);
11598f2fc82SQiuxu Zhuo 			d->imc[i].chan[m->mtype].cdev = pdev;
11698f2fc82SQiuxu Zhuo 			break;
117e80634a7STony Luck 		case ERRCHAN0:
118e80634a7STony Luck 		case ERRCHAN1:
119e80634a7STony Luck 		case ERRCHAN2:
120e80634a7STony Luck 			pci_dev_get(pdev);
121e80634a7STony Luck 			d->imc[i].chan[m->mtype - ERRCHAN0].edev = pdev;
122e80634a7STony Luck 			break;
12398f2fc82SQiuxu Zhuo 		case SAD_ALL:
12498f2fc82SQiuxu Zhuo 			pci_dev_get(pdev);
12598f2fc82SQiuxu Zhuo 			d->sad_all = pdev;
12698f2fc82SQiuxu Zhuo 			break;
12798f2fc82SQiuxu Zhuo 		case UTIL_ALL:
12898f2fc82SQiuxu Zhuo 			pci_dev_get(pdev);
12998f2fc82SQiuxu Zhuo 			d->util_all = pdev;
13098f2fc82SQiuxu Zhuo 			break;
13198f2fc82SQiuxu Zhuo 		case SAD:
13298f2fc82SQiuxu Zhuo 			/*
13398f2fc82SQiuxu Zhuo 			 * one of these devices per core, including cores
13498f2fc82SQiuxu Zhuo 			 * that don't exist on this SKU. Ignore any that
13598f2fc82SQiuxu Zhuo 			 * read a route table of zero, make sure all the
13698f2fc82SQiuxu Zhuo 			 * non-zero values match.
13798f2fc82SQiuxu Zhuo 			 */
13898f2fc82SQiuxu Zhuo 			pci_read_config_dword(pdev, 0xB4, &reg);
13998f2fc82SQiuxu Zhuo 			if (reg != 0) {
14098f2fc82SQiuxu Zhuo 				if (d->mcroute == 0) {
14198f2fc82SQiuxu Zhuo 					d->mcroute = reg;
14298f2fc82SQiuxu Zhuo 				} else if (d->mcroute != reg) {
14398f2fc82SQiuxu Zhuo 					skx_printk(KERN_ERR, "mcroute mismatch\n");
14498f2fc82SQiuxu Zhuo 					goto fail;
14598f2fc82SQiuxu Zhuo 				}
14698f2fc82SQiuxu Zhuo 			}
14798f2fc82SQiuxu Zhuo 			ndev--;
14898f2fc82SQiuxu Zhuo 			break;
14998f2fc82SQiuxu Zhuo 		}
15098f2fc82SQiuxu Zhuo 
15198f2fc82SQiuxu Zhuo 		prev = pdev;
15298f2fc82SQiuxu Zhuo 	}
15398f2fc82SQiuxu Zhuo 
15498f2fc82SQiuxu Zhuo 	return ndev;
15598f2fc82SQiuxu Zhuo fail:
15698f2fc82SQiuxu Zhuo 	pci_dev_put(pdev);
15798f2fc82SQiuxu Zhuo 	return -ENODEV;
15898f2fc82SQiuxu Zhuo }
15998f2fc82SQiuxu Zhuo 
160ee5340abSQiuxu Zhuo static struct res_config skx_cfg = {
161ee5340abSQiuxu Zhuo 	.type			= SKX,
162ee5340abSQiuxu Zhuo 	.decs_did		= 0x2016,
163ee5340abSQiuxu Zhuo 	.busno_cfg_offset	= 0xcc,
164ee5340abSQiuxu Zhuo };
165ee5340abSQiuxu Zhuo 
16698f2fc82SQiuxu Zhuo static const struct x86_cpu_id skx_cpuids[] = {
1678807e155SQiuxu Zhuo 	X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(SKYLAKE_X, X86_STEPPINGS(0x0, 0xf), &skx_cfg),
16898f2fc82SQiuxu Zhuo 	{ }
16998f2fc82SQiuxu Zhuo };
17098f2fc82SQiuxu Zhuo MODULE_DEVICE_TABLE(x86cpu, skx_cpuids);
17198f2fc82SQiuxu Zhuo 
skx_check_ecc(u32 mcmtr)17210320950SQiuxu Zhuo static bool skx_check_ecc(u32 mcmtr)
17398f2fc82SQiuxu Zhuo {
17410320950SQiuxu Zhuo 	return !!GET_BITFIELD(mcmtr, 2, 2);
17598f2fc82SQiuxu Zhuo }
17698f2fc82SQiuxu Zhuo 
skx_get_dimm_config(struct mem_ctl_info * mci,struct res_config * cfg)177479f58ddSQiuxu Zhuo static int skx_get_dimm_config(struct mem_ctl_info *mci, struct res_config *cfg)
17898f2fc82SQiuxu Zhuo {
17998f2fc82SQiuxu Zhuo 	struct skx_pvt *pvt = mci->pvt_info;
18010320950SQiuxu Zhuo 	u32 mtr, mcmtr, amap, mcddrtcfg;
18198f2fc82SQiuxu Zhuo 	struct skx_imc *imc = pvt->imc;
18298f2fc82SQiuxu Zhuo 	struct dimm_info *dimm;
18398f2fc82SQiuxu Zhuo 	int i, j;
18498f2fc82SQiuxu Zhuo 	int ndimms;
18598f2fc82SQiuxu Zhuo 
18610320950SQiuxu Zhuo 	/* Only the mcmtr on the first channel is effective */
18710320950SQiuxu Zhuo 	pci_read_config_dword(imc->chan[0].cdev, 0x87c, &mcmtr);
18810320950SQiuxu Zhuo 
18998f2fc82SQiuxu Zhuo 	for (i = 0; i < SKX_NUM_CHANNELS; i++) {
19098f2fc82SQiuxu Zhuo 		ndimms = 0;
19198f2fc82SQiuxu Zhuo 		pci_read_config_dword(imc->chan[i].cdev, 0x8C, &amap);
19298f2fc82SQiuxu Zhuo 		pci_read_config_dword(imc->chan[i].cdev, 0x400, &mcddrtcfg);
19398f2fc82SQiuxu Zhuo 		for (j = 0; j < SKX_NUM_DIMMS; j++) {
194bc9ad9e4SRobert Richter 			dimm = edac_get_dimm(mci, i, j, 0);
19598f2fc82SQiuxu Zhuo 			pci_read_config_dword(imc->chan[i].cdev,
19698f2fc82SQiuxu Zhuo 					      0x80 + 4 * j, &mtr);
19798f2fc82SQiuxu Zhuo 			if (IS_DIMM_PRESENT(mtr)) {
198479f58ddSQiuxu Zhuo 				ndimms += skx_get_dimm_info(mtr, mcmtr, amap, dimm, imc, i, j, cfg);
19998f2fc82SQiuxu Zhuo 			} else if (IS_NVDIMM_PRESENT(mcddrtcfg, j)) {
20098f2fc82SQiuxu Zhuo 				ndimms += skx_get_nvdimm_info(dimm, imc, i, j,
20198f2fc82SQiuxu Zhuo 							      EDAC_MOD_STR);
20298f2fc82SQiuxu Zhuo 				nvdimm_count++;
20398f2fc82SQiuxu Zhuo 			}
20498f2fc82SQiuxu Zhuo 		}
20510320950SQiuxu Zhuo 		if (ndimms && !skx_check_ecc(mcmtr)) {
20698f2fc82SQiuxu Zhuo 			skx_printk(KERN_ERR, "ECC is disabled on imc %d\n", imc->mc);
20798f2fc82SQiuxu Zhuo 			return -ENODEV;
20898f2fc82SQiuxu Zhuo 		}
20998f2fc82SQiuxu Zhuo 	}
21098f2fc82SQiuxu Zhuo 
21198f2fc82SQiuxu Zhuo 	return 0;
21298f2fc82SQiuxu Zhuo }
21398f2fc82SQiuxu Zhuo 
21498f2fc82SQiuxu Zhuo #define	SKX_MAX_SAD 24
21598f2fc82SQiuxu Zhuo 
21698f2fc82SQiuxu Zhuo #define SKX_GET_SAD(d, i, reg)	\
21798f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->sad_all, 0x60 + 8 * (i), &(reg))
21898f2fc82SQiuxu Zhuo #define SKX_GET_ILV(d, i, reg)	\
21998f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->sad_all, 0x64 + 8 * (i), &(reg))
22098f2fc82SQiuxu Zhuo 
22198f2fc82SQiuxu Zhuo #define	SKX_SAD_MOD3MODE(sad)	GET_BITFIELD((sad), 30, 31)
22298f2fc82SQiuxu Zhuo #define	SKX_SAD_MOD3(sad)	GET_BITFIELD((sad), 27, 27)
22398f2fc82SQiuxu Zhuo #define SKX_SAD_LIMIT(sad)	(((u64)GET_BITFIELD((sad), 7, 26) << 26) | MASK26)
22498f2fc82SQiuxu Zhuo #define	SKX_SAD_MOD3ASMOD2(sad)	GET_BITFIELD((sad), 5, 6)
22598f2fc82SQiuxu Zhuo #define	SKX_SAD_ATTR(sad)	GET_BITFIELD((sad), 3, 4)
22698f2fc82SQiuxu Zhuo #define	SKX_SAD_INTERLEAVE(sad)	GET_BITFIELD((sad), 1, 2)
22798f2fc82SQiuxu Zhuo #define SKX_SAD_ENABLE(sad)	GET_BITFIELD((sad), 0, 0)
22898f2fc82SQiuxu Zhuo 
22998f2fc82SQiuxu Zhuo #define SKX_ILV_REMOTE(tgt)	(((tgt) & 8) == 0)
23098f2fc82SQiuxu Zhuo #define SKX_ILV_TARGET(tgt)	((tgt) & 7)
23198f2fc82SQiuxu Zhuo 
skx_show_retry_rd_err_log(struct decoded_addr * res,char * msg,int len,bool scrub_err)232e80634a7STony Luck static void skx_show_retry_rd_err_log(struct decoded_addr *res,
233cf4e6d52SYouquan Song 				      char *msg, int len,
234cf4e6d52SYouquan Song 				      bool scrub_err)
235e80634a7STony Luck {
236e80634a7STony Luck 	u32 log0, log1, log2, log3, log4;
237e80634a7STony Luck 	u32 corr0, corr1, corr2, corr3;
238e80634a7STony Luck 	struct pci_dev *edev;
239e80634a7STony Luck 	int n;
240e80634a7STony Luck 
241e80634a7STony Luck 	edev = res->dev->imc[res->imc].chan[res->channel].edev;
242e80634a7STony Luck 
243e80634a7STony Luck 	pci_read_config_dword(edev, 0x154, &log0);
244e80634a7STony Luck 	pci_read_config_dword(edev, 0x148, &log1);
245e80634a7STony Luck 	pci_read_config_dword(edev, 0x150, &log2);
246e80634a7STony Luck 	pci_read_config_dword(edev, 0x15c, &log3);
247e80634a7STony Luck 	pci_read_config_dword(edev, 0x114, &log4);
248e80634a7STony Luck 
249e80634a7STony Luck 	n = snprintf(msg, len, " retry_rd_err_log[%.8x %.8x %.8x %.8x %.8x]",
250e80634a7STony Luck 		     log0, log1, log2, log3, log4);
251e80634a7STony Luck 
252e80634a7STony Luck 	pci_read_config_dword(edev, 0x104, &corr0);
253e80634a7STony Luck 	pci_read_config_dword(edev, 0x108, &corr1);
254e80634a7STony Luck 	pci_read_config_dword(edev, 0x10c, &corr2);
255e80634a7STony Luck 	pci_read_config_dword(edev, 0x110, &corr3);
256e80634a7STony Luck 
257e80634a7STony Luck 	if (len - n > 0)
258e80634a7STony Luck 		snprintf(msg + n, len - n,
259e80634a7STony Luck 			 " correrrcnt[%.4x %.4x %.4x %.4x %.4x %.4x %.4x %.4x]",
260e80634a7STony Luck 			 corr0 & 0xffff, corr0 >> 16,
261e80634a7STony Luck 			 corr1 & 0xffff, corr1 >> 16,
262e80634a7STony Luck 			 corr2 & 0xffff, corr2 >> 16,
263e80634a7STony Luck 			 corr3 & 0xffff, corr3 >> 16);
264e80634a7STony Luck }
265e80634a7STony Luck 
skx_sad_decode(struct decoded_addr * res)26698f2fc82SQiuxu Zhuo static bool skx_sad_decode(struct decoded_addr *res)
26798f2fc82SQiuxu Zhuo {
26898f2fc82SQiuxu Zhuo 	struct skx_dev *d = list_first_entry(skx_edac_list, typeof(*d), list);
26998f2fc82SQiuxu Zhuo 	u64 addr = res->addr;
27098f2fc82SQiuxu Zhuo 	int i, idx, tgt, lchan, shift;
27198f2fc82SQiuxu Zhuo 	u32 sad, ilv;
27298f2fc82SQiuxu Zhuo 	u64 limit, prev_limit;
27398f2fc82SQiuxu Zhuo 	int remote = 0;
27498f2fc82SQiuxu Zhuo 
27598f2fc82SQiuxu Zhuo 	/* Simple sanity check for I/O space or out of range */
27698f2fc82SQiuxu Zhuo 	if (addr >= skx_tohm || (addr >= skx_tolm && addr < BIT_ULL(32))) {
27798f2fc82SQiuxu Zhuo 		edac_dbg(0, "Address 0x%llx out of range\n", addr);
27898f2fc82SQiuxu Zhuo 		return false;
27998f2fc82SQiuxu Zhuo 	}
28098f2fc82SQiuxu Zhuo 
28198f2fc82SQiuxu Zhuo restart:
28298f2fc82SQiuxu Zhuo 	prev_limit = 0;
28398f2fc82SQiuxu Zhuo 	for (i = 0; i < SKX_MAX_SAD; i++) {
28498f2fc82SQiuxu Zhuo 		SKX_GET_SAD(d, i, sad);
28598f2fc82SQiuxu Zhuo 		limit = SKX_SAD_LIMIT(sad);
28698f2fc82SQiuxu Zhuo 		if (SKX_SAD_ENABLE(sad)) {
28798f2fc82SQiuxu Zhuo 			if (addr >= prev_limit && addr <= limit)
28898f2fc82SQiuxu Zhuo 				goto sad_found;
28998f2fc82SQiuxu Zhuo 		}
29098f2fc82SQiuxu Zhuo 		prev_limit = limit + 1;
29198f2fc82SQiuxu Zhuo 	}
29298f2fc82SQiuxu Zhuo 	edac_dbg(0, "No SAD entry for 0x%llx\n", addr);
29398f2fc82SQiuxu Zhuo 	return false;
29498f2fc82SQiuxu Zhuo 
29598f2fc82SQiuxu Zhuo sad_found:
29698f2fc82SQiuxu Zhuo 	SKX_GET_ILV(d, i, ilv);
29798f2fc82SQiuxu Zhuo 
29898f2fc82SQiuxu Zhuo 	switch (SKX_SAD_INTERLEAVE(sad)) {
29998f2fc82SQiuxu Zhuo 	case 0:
30098f2fc82SQiuxu Zhuo 		idx = GET_BITFIELD(addr, 6, 8);
30198f2fc82SQiuxu Zhuo 		break;
30298f2fc82SQiuxu Zhuo 	case 1:
30398f2fc82SQiuxu Zhuo 		idx = GET_BITFIELD(addr, 8, 10);
30498f2fc82SQiuxu Zhuo 		break;
30598f2fc82SQiuxu Zhuo 	case 2:
30698f2fc82SQiuxu Zhuo 		idx = GET_BITFIELD(addr, 12, 14);
30798f2fc82SQiuxu Zhuo 		break;
30898f2fc82SQiuxu Zhuo 	case 3:
30998f2fc82SQiuxu Zhuo 		idx = GET_BITFIELD(addr, 30, 32);
31098f2fc82SQiuxu Zhuo 		break;
31198f2fc82SQiuxu Zhuo 	}
31298f2fc82SQiuxu Zhuo 
31398f2fc82SQiuxu Zhuo 	tgt = GET_BITFIELD(ilv, 4 * idx, 4 * idx + 3);
31498f2fc82SQiuxu Zhuo 
31598f2fc82SQiuxu Zhuo 	/* If point to another node, find it and start over */
31698f2fc82SQiuxu Zhuo 	if (SKX_ILV_REMOTE(tgt)) {
31798f2fc82SQiuxu Zhuo 		if (remote) {
31898f2fc82SQiuxu Zhuo 			edac_dbg(0, "Double remote!\n");
31998f2fc82SQiuxu Zhuo 			return false;
32098f2fc82SQiuxu Zhuo 		}
32198f2fc82SQiuxu Zhuo 		remote = 1;
32298f2fc82SQiuxu Zhuo 		list_for_each_entry(d, skx_edac_list, list) {
32398f2fc82SQiuxu Zhuo 			if (d->imc[0].src_id == SKX_ILV_TARGET(tgt))
32498f2fc82SQiuxu Zhuo 				goto restart;
32598f2fc82SQiuxu Zhuo 		}
32698f2fc82SQiuxu Zhuo 		edac_dbg(0, "Can't find node %d\n", SKX_ILV_TARGET(tgt));
32798f2fc82SQiuxu Zhuo 		return false;
32898f2fc82SQiuxu Zhuo 	}
32998f2fc82SQiuxu Zhuo 
33098f2fc82SQiuxu Zhuo 	if (SKX_SAD_MOD3(sad) == 0) {
33198f2fc82SQiuxu Zhuo 		lchan = SKX_ILV_TARGET(tgt);
33298f2fc82SQiuxu Zhuo 	} else {
33398f2fc82SQiuxu Zhuo 		switch (SKX_SAD_MOD3MODE(sad)) {
33498f2fc82SQiuxu Zhuo 		case 0:
33598f2fc82SQiuxu Zhuo 			shift = 6;
33698f2fc82SQiuxu Zhuo 			break;
33798f2fc82SQiuxu Zhuo 		case 1:
33898f2fc82SQiuxu Zhuo 			shift = 8;
33998f2fc82SQiuxu Zhuo 			break;
34098f2fc82SQiuxu Zhuo 		case 2:
34198f2fc82SQiuxu Zhuo 			shift = 12;
34298f2fc82SQiuxu Zhuo 			break;
34398f2fc82SQiuxu Zhuo 		default:
34498f2fc82SQiuxu Zhuo 			edac_dbg(0, "illegal mod3mode\n");
34598f2fc82SQiuxu Zhuo 			return false;
34698f2fc82SQiuxu Zhuo 		}
34798f2fc82SQiuxu Zhuo 		switch (SKX_SAD_MOD3ASMOD2(sad)) {
34898f2fc82SQiuxu Zhuo 		case 0:
34998f2fc82SQiuxu Zhuo 			lchan = (addr >> shift) % 3;
35098f2fc82SQiuxu Zhuo 			break;
35198f2fc82SQiuxu Zhuo 		case 1:
35298f2fc82SQiuxu Zhuo 			lchan = (addr >> shift) % 2;
35398f2fc82SQiuxu Zhuo 			break;
35498f2fc82SQiuxu Zhuo 		case 2:
35598f2fc82SQiuxu Zhuo 			lchan = (addr >> shift) % 2;
35698f2fc82SQiuxu Zhuo 			lchan = (lchan << 1) | !lchan;
35798f2fc82SQiuxu Zhuo 			break;
35898f2fc82SQiuxu Zhuo 		case 3:
35998f2fc82SQiuxu Zhuo 			lchan = ((addr >> shift) % 2) << 1;
36098f2fc82SQiuxu Zhuo 			break;
36198f2fc82SQiuxu Zhuo 		}
36298f2fc82SQiuxu Zhuo 		lchan = (lchan << 1) | (SKX_ILV_TARGET(tgt) & 1);
36398f2fc82SQiuxu Zhuo 	}
36498f2fc82SQiuxu Zhuo 
36598f2fc82SQiuxu Zhuo 	res->dev = d;
36698f2fc82SQiuxu Zhuo 	res->socket = d->imc[0].src_id;
36798f2fc82SQiuxu Zhuo 	res->imc = GET_BITFIELD(d->mcroute, lchan * 3, lchan * 3 + 2);
36898f2fc82SQiuxu Zhuo 	res->channel = GET_BITFIELD(d->mcroute, lchan * 2 + 18, lchan * 2 + 19);
36998f2fc82SQiuxu Zhuo 
37098f2fc82SQiuxu Zhuo 	edac_dbg(2, "0x%llx: socket=%d imc=%d channel=%d\n",
37198f2fc82SQiuxu Zhuo 		 res->addr, res->socket, res->imc, res->channel);
37298f2fc82SQiuxu Zhuo 	return true;
37398f2fc82SQiuxu Zhuo }
37498f2fc82SQiuxu Zhuo 
37598f2fc82SQiuxu Zhuo #define	SKX_MAX_TAD 8
37698f2fc82SQiuxu Zhuo 
37798f2fc82SQiuxu Zhuo #define SKX_GET_TADBASE(d, mc, i, reg)			\
37898f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x850 + 4 * (i), &(reg))
37998f2fc82SQiuxu Zhuo #define SKX_GET_TADWAYNESS(d, mc, i, reg)		\
38098f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x880 + 4 * (i), &(reg))
38198f2fc82SQiuxu Zhuo #define SKX_GET_TADCHNILVOFFSET(d, mc, ch, i, reg)	\
38298f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->imc[mc].chan[ch].cdev, 0x90 + 4 * (i), &(reg))
38398f2fc82SQiuxu Zhuo 
38498f2fc82SQiuxu Zhuo #define	SKX_TAD_BASE(b)		((u64)GET_BITFIELD((b), 12, 31) << 26)
38598f2fc82SQiuxu Zhuo #define SKX_TAD_SKT_GRAN(b)	GET_BITFIELD((b), 4, 5)
38698f2fc82SQiuxu Zhuo #define SKX_TAD_CHN_GRAN(b)	GET_BITFIELD((b), 6, 7)
38798f2fc82SQiuxu Zhuo #define	SKX_TAD_LIMIT(b)	(((u64)GET_BITFIELD((b), 12, 31) << 26) | MASK26)
38898f2fc82SQiuxu Zhuo #define	SKX_TAD_OFFSET(b)	((u64)GET_BITFIELD((b), 4, 23) << 26)
38998f2fc82SQiuxu Zhuo #define	SKX_TAD_SKTWAYS(b)	(1 << GET_BITFIELD((b), 10, 11))
39098f2fc82SQiuxu Zhuo #define	SKX_TAD_CHNWAYS(b)	(GET_BITFIELD((b), 8, 9) + 1)
39198f2fc82SQiuxu Zhuo 
39298f2fc82SQiuxu Zhuo /* which bit used for both socket and channel interleave */
39398f2fc82SQiuxu Zhuo static int skx_granularity[] = { 6, 8, 12, 30 };
39498f2fc82SQiuxu Zhuo 
skx_do_interleave(u64 addr,int shift,int ways,u64 lowbits)39598f2fc82SQiuxu Zhuo static u64 skx_do_interleave(u64 addr, int shift, int ways, u64 lowbits)
39698f2fc82SQiuxu Zhuo {
39798f2fc82SQiuxu Zhuo 	addr >>= shift;
39898f2fc82SQiuxu Zhuo 	addr /= ways;
39998f2fc82SQiuxu Zhuo 	addr <<= shift;
40098f2fc82SQiuxu Zhuo 
40198f2fc82SQiuxu Zhuo 	return addr | (lowbits & ((1ull << shift) - 1));
40298f2fc82SQiuxu Zhuo }
40398f2fc82SQiuxu Zhuo 
skx_tad_decode(struct decoded_addr * res)40498f2fc82SQiuxu Zhuo static bool skx_tad_decode(struct decoded_addr *res)
40598f2fc82SQiuxu Zhuo {
40698f2fc82SQiuxu Zhuo 	int i;
40798f2fc82SQiuxu Zhuo 	u32 base, wayness, chnilvoffset;
40898f2fc82SQiuxu Zhuo 	int skt_interleave_bit, chn_interleave_bit;
40998f2fc82SQiuxu Zhuo 	u64 channel_addr;
41098f2fc82SQiuxu Zhuo 
41198f2fc82SQiuxu Zhuo 	for (i = 0; i < SKX_MAX_TAD; i++) {
41298f2fc82SQiuxu Zhuo 		SKX_GET_TADBASE(res->dev, res->imc, i, base);
41398f2fc82SQiuxu Zhuo 		SKX_GET_TADWAYNESS(res->dev, res->imc, i, wayness);
41498f2fc82SQiuxu Zhuo 		if (SKX_TAD_BASE(base) <= res->addr && res->addr <= SKX_TAD_LIMIT(wayness))
41598f2fc82SQiuxu Zhuo 			goto tad_found;
41698f2fc82SQiuxu Zhuo 	}
41798f2fc82SQiuxu Zhuo 	edac_dbg(0, "No TAD entry for 0x%llx\n", res->addr);
41898f2fc82SQiuxu Zhuo 	return false;
41998f2fc82SQiuxu Zhuo 
42098f2fc82SQiuxu Zhuo tad_found:
42198f2fc82SQiuxu Zhuo 	res->sktways = SKX_TAD_SKTWAYS(wayness);
42298f2fc82SQiuxu Zhuo 	res->chanways = SKX_TAD_CHNWAYS(wayness);
42398f2fc82SQiuxu Zhuo 	skt_interleave_bit = skx_granularity[SKX_TAD_SKT_GRAN(base)];
42498f2fc82SQiuxu Zhuo 	chn_interleave_bit = skx_granularity[SKX_TAD_CHN_GRAN(base)];
42598f2fc82SQiuxu Zhuo 
42698f2fc82SQiuxu Zhuo 	SKX_GET_TADCHNILVOFFSET(res->dev, res->imc, res->channel, i, chnilvoffset);
42798f2fc82SQiuxu Zhuo 	channel_addr = res->addr - SKX_TAD_OFFSET(chnilvoffset);
42898f2fc82SQiuxu Zhuo 
42998f2fc82SQiuxu Zhuo 	if (res->chanways == 3 && skt_interleave_bit > chn_interleave_bit) {
43098f2fc82SQiuxu Zhuo 		/* Must handle channel first, then socket */
43198f2fc82SQiuxu Zhuo 		channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
43298f2fc82SQiuxu Zhuo 						 res->chanways, channel_addr);
43398f2fc82SQiuxu Zhuo 		channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
43498f2fc82SQiuxu Zhuo 						 res->sktways, channel_addr);
43598f2fc82SQiuxu Zhuo 	} else {
43698f2fc82SQiuxu Zhuo 		/* Handle socket then channel. Preserve low bits from original address */
43798f2fc82SQiuxu Zhuo 		channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
43898f2fc82SQiuxu Zhuo 						 res->sktways, res->addr);
43998f2fc82SQiuxu Zhuo 		channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
44098f2fc82SQiuxu Zhuo 						 res->chanways, res->addr);
44198f2fc82SQiuxu Zhuo 	}
44298f2fc82SQiuxu Zhuo 
44398f2fc82SQiuxu Zhuo 	res->chan_addr = channel_addr;
44498f2fc82SQiuxu Zhuo 
44598f2fc82SQiuxu Zhuo 	edac_dbg(2, "0x%llx: chan_addr=0x%llx sktways=%d chanways=%d\n",
44698f2fc82SQiuxu Zhuo 		 res->addr, res->chan_addr, res->sktways, res->chanways);
44798f2fc82SQiuxu Zhuo 	return true;
44898f2fc82SQiuxu Zhuo }
44998f2fc82SQiuxu Zhuo 
45098f2fc82SQiuxu Zhuo #define SKX_MAX_RIR 4
45198f2fc82SQiuxu Zhuo 
45298f2fc82SQiuxu Zhuo #define SKX_GET_RIRWAYNESS(d, mc, ch, i, reg)		\
45398f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->imc[mc].chan[ch].cdev,	\
45498f2fc82SQiuxu Zhuo 			      0x108 + 4 * (i), &(reg))
45598f2fc82SQiuxu Zhuo #define SKX_GET_RIRILV(d, mc, ch, idx, i, reg)		\
45698f2fc82SQiuxu Zhuo 	pci_read_config_dword((d)->imc[mc].chan[ch].cdev,	\
45798f2fc82SQiuxu Zhuo 			      0x120 + 16 * (idx) + 4 * (i), &(reg))
45898f2fc82SQiuxu Zhuo 
45998f2fc82SQiuxu Zhuo #define	SKX_RIR_VALID(b) GET_BITFIELD((b), 31, 31)
46098f2fc82SQiuxu Zhuo #define	SKX_RIR_LIMIT(b) (((u64)GET_BITFIELD((b), 1, 11) << 29) | MASK29)
46198f2fc82SQiuxu Zhuo #define	SKX_RIR_WAYS(b) (1 << GET_BITFIELD((b), 28, 29))
46298f2fc82SQiuxu Zhuo #define	SKX_RIR_CHAN_RANK(b) GET_BITFIELD((b), 16, 19)
46398f2fc82SQiuxu Zhuo #define	SKX_RIR_OFFSET(b) ((u64)(GET_BITFIELD((b), 2, 15) << 26))
46498f2fc82SQiuxu Zhuo 
skx_rir_decode(struct decoded_addr * res)46598f2fc82SQiuxu Zhuo static bool skx_rir_decode(struct decoded_addr *res)
46698f2fc82SQiuxu Zhuo {
46798f2fc82SQiuxu Zhuo 	int i, idx, chan_rank;
46898f2fc82SQiuxu Zhuo 	int shift;
46998f2fc82SQiuxu Zhuo 	u32 rirway, rirlv;
47098f2fc82SQiuxu Zhuo 	u64 rank_addr, prev_limit = 0, limit;
47198f2fc82SQiuxu Zhuo 
47298f2fc82SQiuxu Zhuo 	if (res->dev->imc[res->imc].chan[res->channel].dimms[0].close_pg)
47398f2fc82SQiuxu Zhuo 		shift = 6;
47498f2fc82SQiuxu Zhuo 	else
47598f2fc82SQiuxu Zhuo 		shift = 13;
47698f2fc82SQiuxu Zhuo 
47798f2fc82SQiuxu Zhuo 	for (i = 0; i < SKX_MAX_RIR; i++) {
47898f2fc82SQiuxu Zhuo 		SKX_GET_RIRWAYNESS(res->dev, res->imc, res->channel, i, rirway);
47998f2fc82SQiuxu Zhuo 		limit = SKX_RIR_LIMIT(rirway);
48098f2fc82SQiuxu Zhuo 		if (SKX_RIR_VALID(rirway)) {
48198f2fc82SQiuxu Zhuo 			if (prev_limit <= res->chan_addr &&
48298f2fc82SQiuxu Zhuo 			    res->chan_addr <= limit)
48398f2fc82SQiuxu Zhuo 				goto rir_found;
48498f2fc82SQiuxu Zhuo 		}
48598f2fc82SQiuxu Zhuo 		prev_limit = limit;
48698f2fc82SQiuxu Zhuo 	}
48798f2fc82SQiuxu Zhuo 	edac_dbg(0, "No RIR entry for 0x%llx\n", res->addr);
48898f2fc82SQiuxu Zhuo 	return false;
48998f2fc82SQiuxu Zhuo 
49098f2fc82SQiuxu Zhuo rir_found:
49198f2fc82SQiuxu Zhuo 	rank_addr = res->chan_addr >> shift;
49298f2fc82SQiuxu Zhuo 	rank_addr /= SKX_RIR_WAYS(rirway);
49398f2fc82SQiuxu Zhuo 	rank_addr <<= shift;
49498f2fc82SQiuxu Zhuo 	rank_addr |= res->chan_addr & GENMASK_ULL(shift - 1, 0);
49598f2fc82SQiuxu Zhuo 
49698f2fc82SQiuxu Zhuo 	res->rank_address = rank_addr;
49798f2fc82SQiuxu Zhuo 	idx = (res->chan_addr >> shift) % SKX_RIR_WAYS(rirway);
49898f2fc82SQiuxu Zhuo 
49998f2fc82SQiuxu Zhuo 	SKX_GET_RIRILV(res->dev, res->imc, res->channel, idx, i, rirlv);
50098f2fc82SQiuxu Zhuo 	res->rank_address = rank_addr - SKX_RIR_OFFSET(rirlv);
50198f2fc82SQiuxu Zhuo 	chan_rank = SKX_RIR_CHAN_RANK(rirlv);
50298f2fc82SQiuxu Zhuo 	res->channel_rank = chan_rank;
50398f2fc82SQiuxu Zhuo 	res->dimm = chan_rank / 4;
50498f2fc82SQiuxu Zhuo 	res->rank = chan_rank % 4;
50598f2fc82SQiuxu Zhuo 
50698f2fc82SQiuxu Zhuo 	edac_dbg(2, "0x%llx: dimm=%d rank=%d chan_rank=%d rank_addr=0x%llx\n",
50798f2fc82SQiuxu Zhuo 		 res->addr, res->dimm, res->rank,
50898f2fc82SQiuxu Zhuo 		 res->channel_rank, res->rank_address);
50998f2fc82SQiuxu Zhuo 	return true;
51098f2fc82SQiuxu Zhuo }
51198f2fc82SQiuxu Zhuo 
51298f2fc82SQiuxu Zhuo static u8 skx_close_row[] = {
513*71b1e3baSQiuxu Zhuo 	15, 16, 17, 18, 20, 21, 22, 28, 10, 11, 12, 13, 29, 30, 31, 32, 33, 34
51498f2fc82SQiuxu Zhuo };
51598f2fc82SQiuxu Zhuo 
51698f2fc82SQiuxu Zhuo static u8 skx_close_column[] = {
51798f2fc82SQiuxu Zhuo 	3, 4, 5, 14, 19, 23, 24, 25, 26, 27
51898f2fc82SQiuxu Zhuo };
51998f2fc82SQiuxu Zhuo 
52098f2fc82SQiuxu Zhuo static u8 skx_open_row[] = {
521*71b1e3baSQiuxu Zhuo 	14, 15, 16, 20, 28, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34
52298f2fc82SQiuxu Zhuo };
52398f2fc82SQiuxu Zhuo 
52498f2fc82SQiuxu Zhuo static u8 skx_open_column[] = {
52598f2fc82SQiuxu Zhuo 	3, 4, 5, 6, 7, 8, 9, 10, 11, 12
52698f2fc82SQiuxu Zhuo };
52798f2fc82SQiuxu Zhuo 
52898f2fc82SQiuxu Zhuo static u8 skx_open_fine_column[] = {
52998f2fc82SQiuxu Zhuo 	3, 4, 5, 7, 8, 9, 10, 11, 12, 13
53098f2fc82SQiuxu Zhuo };
53198f2fc82SQiuxu Zhuo 
skx_bits(u64 addr,int nbits,u8 * bits)53298f2fc82SQiuxu Zhuo static int skx_bits(u64 addr, int nbits, u8 *bits)
53398f2fc82SQiuxu Zhuo {
53498f2fc82SQiuxu Zhuo 	int i, res = 0;
53598f2fc82SQiuxu Zhuo 
53698f2fc82SQiuxu Zhuo 	for (i = 0; i < nbits; i++)
53798f2fc82SQiuxu Zhuo 		res |= ((addr >> bits[i]) & 1) << i;
53898f2fc82SQiuxu Zhuo 	return res;
53998f2fc82SQiuxu Zhuo }
54098f2fc82SQiuxu Zhuo 
skx_bank_bits(u64 addr,int b0,int b1,int do_xor,int x0,int x1)54198f2fc82SQiuxu Zhuo static int skx_bank_bits(u64 addr, int b0, int b1, int do_xor, int x0, int x1)
54298f2fc82SQiuxu Zhuo {
54398f2fc82SQiuxu Zhuo 	int ret = GET_BITFIELD(addr, b0, b0) | (GET_BITFIELD(addr, b1, b1) << 1);
54498f2fc82SQiuxu Zhuo 
54598f2fc82SQiuxu Zhuo 	if (do_xor)
54698f2fc82SQiuxu Zhuo 		ret ^= GET_BITFIELD(addr, x0, x0) | (GET_BITFIELD(addr, x1, x1) << 1);
54798f2fc82SQiuxu Zhuo 
54898f2fc82SQiuxu Zhuo 	return ret;
54998f2fc82SQiuxu Zhuo }
55098f2fc82SQiuxu Zhuo 
skx_mad_decode(struct decoded_addr * r)55198f2fc82SQiuxu Zhuo static bool skx_mad_decode(struct decoded_addr *r)
55298f2fc82SQiuxu Zhuo {
55398f2fc82SQiuxu Zhuo 	struct skx_dimm *dimm = &r->dev->imc[r->imc].chan[r->channel].dimms[r->dimm];
55498f2fc82SQiuxu Zhuo 	int bg0 = dimm->fine_grain_bank ? 6 : 13;
55598f2fc82SQiuxu Zhuo 
55698f2fc82SQiuxu Zhuo 	if (dimm->close_pg) {
55798f2fc82SQiuxu Zhuo 		r->row = skx_bits(r->rank_address, dimm->rowbits, skx_close_row);
55898f2fc82SQiuxu Zhuo 		r->column = skx_bits(r->rank_address, dimm->colbits, skx_close_column);
55998f2fc82SQiuxu Zhuo 		r->column |= 0x400; /* C10 is autoprecharge, always set */
56098f2fc82SQiuxu Zhuo 		r->bank_address = skx_bank_bits(r->rank_address, 8, 9, dimm->bank_xor_enable, 22, 28);
56198f2fc82SQiuxu Zhuo 		r->bank_group = skx_bank_bits(r->rank_address, 6, 7, dimm->bank_xor_enable, 20, 21);
56298f2fc82SQiuxu Zhuo 	} else {
56398f2fc82SQiuxu Zhuo 		r->row = skx_bits(r->rank_address, dimm->rowbits, skx_open_row);
56498f2fc82SQiuxu Zhuo 		if (dimm->fine_grain_bank)
56598f2fc82SQiuxu Zhuo 			r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_fine_column);
56698f2fc82SQiuxu Zhuo 		else
56798f2fc82SQiuxu Zhuo 			r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_column);
56898f2fc82SQiuxu Zhuo 		r->bank_address = skx_bank_bits(r->rank_address, 18, 19, dimm->bank_xor_enable, 22, 23);
56998f2fc82SQiuxu Zhuo 		r->bank_group = skx_bank_bits(r->rank_address, bg0, 17, dimm->bank_xor_enable, 20, 21);
57098f2fc82SQiuxu Zhuo 	}
57198f2fc82SQiuxu Zhuo 	r->row &= (1u << dimm->rowbits) - 1;
57298f2fc82SQiuxu Zhuo 
57398f2fc82SQiuxu Zhuo 	edac_dbg(2, "0x%llx: row=0x%x col=0x%x bank_addr=%d bank_group=%d\n",
57498f2fc82SQiuxu Zhuo 		 r->addr, r->row, r->column, r->bank_address,
57598f2fc82SQiuxu Zhuo 		 r->bank_group);
57698f2fc82SQiuxu Zhuo 	return true;
57798f2fc82SQiuxu Zhuo }
57898f2fc82SQiuxu Zhuo 
skx_decode(struct decoded_addr * res)57998f2fc82SQiuxu Zhuo static bool skx_decode(struct decoded_addr *res)
58098f2fc82SQiuxu Zhuo {
58198f2fc82SQiuxu Zhuo 	return skx_sad_decode(res) && skx_tad_decode(res) &&
58298f2fc82SQiuxu Zhuo 		skx_rir_decode(res) && skx_mad_decode(res);
58398f2fc82SQiuxu Zhuo }
58498f2fc82SQiuxu Zhuo 
58598f2fc82SQiuxu Zhuo static struct notifier_block skx_mce_dec = {
58698f2fc82SQiuxu Zhuo 	.notifier_call	= skx_mce_check_error,
58798f2fc82SQiuxu Zhuo 	.priority	= MCE_PRIO_EDAC,
58898f2fc82SQiuxu Zhuo };
58998f2fc82SQiuxu Zhuo 
590fe783516SQiuxu Zhuo #ifdef CONFIG_EDAC_DEBUG
591fe783516SQiuxu Zhuo /*
592fe783516SQiuxu Zhuo  * Debug feature.
593fe783516SQiuxu Zhuo  * Exercise the address decode logic by writing an address to
594fe783516SQiuxu Zhuo  * /sys/kernel/debug/edac/skx_test/addr.
595fe783516SQiuxu Zhuo  */
596fe783516SQiuxu Zhuo static struct dentry *skx_test;
597fe783516SQiuxu Zhuo 
debugfs_u64_set(void * data,u64 val)598fe783516SQiuxu Zhuo static int debugfs_u64_set(void *data, u64 val)
599fe783516SQiuxu Zhuo {
600fe783516SQiuxu Zhuo 	struct mce m;
601fe783516SQiuxu Zhuo 
602fe783516SQiuxu Zhuo 	pr_warn_once("Fake error to 0x%llx injected via debugfs\n", val);
603fe783516SQiuxu Zhuo 
604fe783516SQiuxu Zhuo 	memset(&m, 0, sizeof(m));
605fe783516SQiuxu Zhuo 	/* ADDRV + MemRd + Unknown channel */
606fe783516SQiuxu Zhuo 	m.status = MCI_STATUS_ADDRV + 0x90;
607fe783516SQiuxu Zhuo 	/* One corrected error */
608fe783516SQiuxu Zhuo 	m.status |= BIT_ULL(MCI_STATUS_CEC_SHIFT);
609fe783516SQiuxu Zhuo 	m.addr = val;
610fe783516SQiuxu Zhuo 	skx_mce_check_error(NULL, 0, &m);
611fe783516SQiuxu Zhuo 
612fe783516SQiuxu Zhuo 	return 0;
613fe783516SQiuxu Zhuo }
614fe783516SQiuxu Zhuo DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
615fe783516SQiuxu Zhuo 
setup_skx_debug(void)616fe783516SQiuxu Zhuo static void setup_skx_debug(void)
617fe783516SQiuxu Zhuo {
618fe783516SQiuxu Zhuo 	skx_test = edac_debugfs_create_dir("skx_test");
619fe783516SQiuxu Zhuo 	if (!skx_test)
620fe783516SQiuxu Zhuo 		return;
621fe783516SQiuxu Zhuo 
622fe783516SQiuxu Zhuo 	if (!edac_debugfs_create_file("addr", 0200, skx_test,
623fe783516SQiuxu Zhuo 				      NULL, &fops_u64_wo)) {
624fe783516SQiuxu Zhuo 		debugfs_remove(skx_test);
625fe783516SQiuxu Zhuo 		skx_test = NULL;
626fe783516SQiuxu Zhuo 	}
627fe783516SQiuxu Zhuo }
628fe783516SQiuxu Zhuo 
teardown_skx_debug(void)629fe783516SQiuxu Zhuo static void teardown_skx_debug(void)
630fe783516SQiuxu Zhuo {
631fe783516SQiuxu Zhuo 	debugfs_remove_recursive(skx_test);
632fe783516SQiuxu Zhuo }
633fe783516SQiuxu Zhuo #else
setup_skx_debug(void)634fe783516SQiuxu Zhuo static inline void setup_skx_debug(void) {}
teardown_skx_debug(void)635fe783516SQiuxu Zhuo static inline void teardown_skx_debug(void) {}
636fe783516SQiuxu Zhuo #endif /*CONFIG_EDAC_DEBUG*/
637fe783516SQiuxu Zhuo 
63898f2fc82SQiuxu Zhuo /*
63998f2fc82SQiuxu Zhuo  * skx_init:
64098f2fc82SQiuxu Zhuo  *	make sure we are running on the correct cpu model
64198f2fc82SQiuxu Zhuo  *	search for all the devices we need
64298f2fc82SQiuxu Zhuo  *	check which DIMMs are present.
64398f2fc82SQiuxu Zhuo  */
skx_init(void)64498f2fc82SQiuxu Zhuo static int __init skx_init(void)
64598f2fc82SQiuxu Zhuo {
64698f2fc82SQiuxu Zhuo 	const struct x86_cpu_id *id;
647ee5340abSQiuxu Zhuo 	struct res_config *cfg;
64898f2fc82SQiuxu Zhuo 	const struct munit *m;
64998f2fc82SQiuxu Zhuo 	const char *owner;
65098f2fc82SQiuxu Zhuo 	int rc = 0, i, off[3] = {0xd0, 0xd4, 0xd8};
65198f2fc82SQiuxu Zhuo 	u8 mc = 0, src_id, node_id;
65298f2fc82SQiuxu Zhuo 	struct skx_dev *d;
65398f2fc82SQiuxu Zhuo 
65498f2fc82SQiuxu Zhuo 	edac_dbg(2, "\n");
65598f2fc82SQiuxu Zhuo 
656315bada6SJia He 	if (ghes_get_devices())
657315bada6SJia He 		return -EBUSY;
658315bada6SJia He 
65998f2fc82SQiuxu Zhuo 	owner = edac_get_owner();
66098f2fc82SQiuxu Zhuo 	if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
66198f2fc82SQiuxu Zhuo 		return -EBUSY;
66298f2fc82SQiuxu Zhuo 
663f0a029ffSLuck, Tony 	if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
664f0a029ffSLuck, Tony 		return -ENODEV;
665f0a029ffSLuck, Tony 
66698f2fc82SQiuxu Zhuo 	id = x86_match_cpu(skx_cpuids);
66798f2fc82SQiuxu Zhuo 	if (!id)
66898f2fc82SQiuxu Zhuo 		return -ENODEV;
66998f2fc82SQiuxu Zhuo 
670ee5340abSQiuxu Zhuo 	cfg = (struct res_config *)id->driver_data;
671ee5340abSQiuxu Zhuo 
67298f2fc82SQiuxu Zhuo 	rc = skx_get_hi_lo(0x2034, off, &skx_tolm, &skx_tohm);
67398f2fc82SQiuxu Zhuo 	if (rc)
67498f2fc82SQiuxu Zhuo 		return rc;
67598f2fc82SQiuxu Zhuo 
676ee5340abSQiuxu Zhuo 	rc = skx_get_all_bus_mappings(cfg, &skx_edac_list);
67798f2fc82SQiuxu Zhuo 	if (rc < 0)
67898f2fc82SQiuxu Zhuo 		goto fail;
67998f2fc82SQiuxu Zhuo 	if (rc == 0) {
68098f2fc82SQiuxu Zhuo 		edac_dbg(2, "No memory controllers found\n");
68198f2fc82SQiuxu Zhuo 		return -ENODEV;
68298f2fc82SQiuxu Zhuo 	}
68398f2fc82SQiuxu Zhuo 	skx_num_sockets = rc;
68498f2fc82SQiuxu Zhuo 
68598f2fc82SQiuxu Zhuo 	for (m = skx_all_munits; m->did; m++) {
68698f2fc82SQiuxu Zhuo 		rc = get_all_munits(m);
68798f2fc82SQiuxu Zhuo 		if (rc < 0)
68898f2fc82SQiuxu Zhuo 			goto fail;
68998f2fc82SQiuxu Zhuo 		if (rc != m->per_socket * skx_num_sockets) {
69098f2fc82SQiuxu Zhuo 			edac_dbg(2, "Expected %d, got %d of 0x%x\n",
69198f2fc82SQiuxu Zhuo 				 m->per_socket * skx_num_sockets, rc, m->did);
69298f2fc82SQiuxu Zhuo 			rc = -ENODEV;
69398f2fc82SQiuxu Zhuo 			goto fail;
69498f2fc82SQiuxu Zhuo 		}
69598f2fc82SQiuxu Zhuo 	}
69698f2fc82SQiuxu Zhuo 
69798f2fc82SQiuxu Zhuo 	list_for_each_entry(d, skx_edac_list, list) {
6981dc78f1fSQiuxu Zhuo 		rc = skx_get_src_id(d, 0xf0, &src_id);
69998f2fc82SQiuxu Zhuo 		if (rc < 0)
70098f2fc82SQiuxu Zhuo 			goto fail;
70198f2fc82SQiuxu Zhuo 		rc = skx_get_node_id(d, &node_id);
70298f2fc82SQiuxu Zhuo 		if (rc < 0)
70398f2fc82SQiuxu Zhuo 			goto fail;
70498f2fc82SQiuxu Zhuo 		edac_dbg(2, "src_id=%d node_id=%d\n", src_id, node_id);
70598f2fc82SQiuxu Zhuo 		for (i = 0; i < SKX_NUM_IMC; i++) {
70698f2fc82SQiuxu Zhuo 			d->imc[i].mc = mc++;
70798f2fc82SQiuxu Zhuo 			d->imc[i].lmc = i;
70898f2fc82SQiuxu Zhuo 			d->imc[i].src_id = src_id;
70998f2fc82SQiuxu Zhuo 			d->imc[i].node_id = node_id;
71098f2fc82SQiuxu Zhuo 			rc = skx_register_mci(&d->imc[i], d->imc[i].chan[0].cdev,
71198f2fc82SQiuxu Zhuo 					      "Skylake Socket", EDAC_MOD_STR,
712479f58ddSQiuxu Zhuo 					      skx_get_dimm_config, cfg);
71398f2fc82SQiuxu Zhuo 			if (rc < 0)
71498f2fc82SQiuxu Zhuo 				goto fail;
71598f2fc82SQiuxu Zhuo 		}
71698f2fc82SQiuxu Zhuo 	}
71798f2fc82SQiuxu Zhuo 
718e80634a7STony Luck 	skx_set_decode(skx_decode, skx_show_retry_rd_err_log);
71998f2fc82SQiuxu Zhuo 
720fe32f366SQiuxu Zhuo 	if (nvdimm_count && skx_adxl_get() != -ENODEV) {
721fe32f366SQiuxu Zhuo 		skx_set_decode(NULL, skx_show_retry_rd_err_log);
722fe32f366SQiuxu Zhuo 	} else {
723fe32f366SQiuxu Zhuo 		if (nvdimm_count)
72498f2fc82SQiuxu Zhuo 			skx_printk(KERN_NOTICE, "Only decoding DDR4 address!\n");
725fe32f366SQiuxu Zhuo 		skx_set_decode(skx_decode, skx_show_retry_rd_err_log);
726fe32f366SQiuxu Zhuo 	}
72798f2fc82SQiuxu Zhuo 
72898f2fc82SQiuxu Zhuo 	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
72998f2fc82SQiuxu Zhuo 	opstate_init();
73098f2fc82SQiuxu Zhuo 
731fe783516SQiuxu Zhuo 	setup_skx_debug();
73298f2fc82SQiuxu Zhuo 
73398f2fc82SQiuxu Zhuo 	mce_register_decode_chain(&skx_mce_dec);
73498f2fc82SQiuxu Zhuo 
73598f2fc82SQiuxu Zhuo 	return 0;
73698f2fc82SQiuxu Zhuo fail:
73798f2fc82SQiuxu Zhuo 	skx_remove();
73898f2fc82SQiuxu Zhuo 	return rc;
73998f2fc82SQiuxu Zhuo }
74098f2fc82SQiuxu Zhuo 
skx_exit(void)74198f2fc82SQiuxu Zhuo static void __exit skx_exit(void)
74298f2fc82SQiuxu Zhuo {
74398f2fc82SQiuxu Zhuo 	edac_dbg(2, "\n");
74498f2fc82SQiuxu Zhuo 	mce_unregister_decode_chain(&skx_mce_dec);
74598f2fc82SQiuxu Zhuo 	teardown_skx_debug();
74698f2fc82SQiuxu Zhuo 	if (nvdimm_count)
74798f2fc82SQiuxu Zhuo 		skx_adxl_put();
74898f2fc82SQiuxu Zhuo 	skx_remove();
74998f2fc82SQiuxu Zhuo }
75098f2fc82SQiuxu Zhuo 
75198f2fc82SQiuxu Zhuo module_init(skx_init);
75298f2fc82SQiuxu Zhuo module_exit(skx_exit);
75398f2fc82SQiuxu Zhuo 
75498f2fc82SQiuxu Zhuo module_param(edac_op_state, int, 0444);
75598f2fc82SQiuxu Zhuo MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
75698f2fc82SQiuxu Zhuo 
75798f2fc82SQiuxu Zhuo MODULE_LICENSE("GPL v2");
75898f2fc82SQiuxu Zhuo MODULE_AUTHOR("Tony Luck");
75998f2fc82SQiuxu Zhuo MODULE_DESCRIPTION("MC Driver for Intel Skylake server processors");
760