18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * EDAC driver for Intel(R) Xeon(R) Skylake processors
48c2ecf20Sopenharmony_ci * Copyright (c) 2016, Intel Corporation.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/processor.h>
98c2ecf20Sopenharmony_ci#include <asm/cpu_device_id.h>
108c2ecf20Sopenharmony_ci#include <asm/intel-family.h>
118c2ecf20Sopenharmony_ci#include <asm/mce.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "edac_module.h"
148c2ecf20Sopenharmony_ci#include "skx_common.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define EDAC_MOD_STR    "skx_edac"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * Debug macros
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci#define skx_printk(level, fmt, arg...)			\
228c2ecf20Sopenharmony_ci	edac_printk(level, "skx", fmt, ##arg)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define skx_mc_printk(mci, level, fmt, arg...)		\
258c2ecf20Sopenharmony_ci	edac_mc_chipset_printk(mci, level, "skx", fmt, ##arg)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic struct list_head *skx_edac_list;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic u64 skx_tolm, skx_tohm;
308c2ecf20Sopenharmony_cistatic int skx_num_sockets;
318c2ecf20Sopenharmony_cistatic unsigned int nvdimm_count;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define	MASK26	0x3FFFFFF		/* Mask for 2^26 */
348c2ecf20Sopenharmony_ci#define MASK29	0x1FFFFFFF		/* Mask for 2^29 */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic struct skx_dev *get_skx_dev(struct pci_bus *bus, u8 idx)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct skx_dev *d;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	list_for_each_entry(d, skx_edac_list, list) {
418c2ecf20Sopenharmony_ci		if (d->seg == pci_domain_nr(bus) && d->bus[idx] == bus->number)
428c2ecf20Sopenharmony_ci			return d;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return NULL;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cienum munittype {
498c2ecf20Sopenharmony_ci	CHAN0, CHAN1, CHAN2, SAD_ALL, UTIL_ALL, SAD,
508c2ecf20Sopenharmony_ci	ERRCHAN0, ERRCHAN1, ERRCHAN2,
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct munit {
548c2ecf20Sopenharmony_ci	u16	did;
558c2ecf20Sopenharmony_ci	u16	devfn[SKX_NUM_IMC];
568c2ecf20Sopenharmony_ci	u8	busidx;
578c2ecf20Sopenharmony_ci	u8	per_socket;
588c2ecf20Sopenharmony_ci	enum munittype mtype;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * List of PCI device ids that we need together with some device
638c2ecf20Sopenharmony_ci * number and function numbers to tell which memory controller the
648c2ecf20Sopenharmony_ci * device belongs to.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistatic const struct munit skx_all_munits[] = {
678c2ecf20Sopenharmony_ci	{ 0x2054, { }, 1, 1, SAD_ALL },
688c2ecf20Sopenharmony_ci	{ 0x2055, { }, 1, 1, UTIL_ALL },
698c2ecf20Sopenharmony_ci	{ 0x2040, { PCI_DEVFN(10, 0), PCI_DEVFN(12, 0) }, 2, 2, CHAN0 },
708c2ecf20Sopenharmony_ci	{ 0x2044, { PCI_DEVFN(10, 4), PCI_DEVFN(12, 4) }, 2, 2, CHAN1 },
718c2ecf20Sopenharmony_ci	{ 0x2048, { PCI_DEVFN(11, 0), PCI_DEVFN(13, 0) }, 2, 2, CHAN2 },
728c2ecf20Sopenharmony_ci	{ 0x2043, { PCI_DEVFN(10, 3), PCI_DEVFN(12, 3) }, 2, 2, ERRCHAN0 },
738c2ecf20Sopenharmony_ci	{ 0x2047, { PCI_DEVFN(10, 7), PCI_DEVFN(12, 7) }, 2, 2, ERRCHAN1 },
748c2ecf20Sopenharmony_ci	{ 0x204b, { PCI_DEVFN(11, 3), PCI_DEVFN(13, 3) }, 2, 2, ERRCHAN2 },
758c2ecf20Sopenharmony_ci	{ 0x208e, { }, 1, 0, SAD },
768c2ecf20Sopenharmony_ci	{ }
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int get_all_munits(const struct munit *m)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct pci_dev *pdev, *prev;
828c2ecf20Sopenharmony_ci	struct skx_dev *d;
838c2ecf20Sopenharmony_ci	u32 reg;
848c2ecf20Sopenharmony_ci	int i = 0, ndev = 0;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	prev = NULL;
878c2ecf20Sopenharmony_ci	for (;;) {
888c2ecf20Sopenharmony_ci		pdev = pci_get_device(PCI_VENDOR_ID_INTEL, m->did, prev);
898c2ecf20Sopenharmony_ci		if (!pdev)
908c2ecf20Sopenharmony_ci			break;
918c2ecf20Sopenharmony_ci		ndev++;
928c2ecf20Sopenharmony_ci		if (m->per_socket == SKX_NUM_IMC) {
938c2ecf20Sopenharmony_ci			for (i = 0; i < SKX_NUM_IMC; i++)
948c2ecf20Sopenharmony_ci				if (m->devfn[i] == pdev->devfn)
958c2ecf20Sopenharmony_ci					break;
968c2ecf20Sopenharmony_ci			if (i == SKX_NUM_IMC)
978c2ecf20Sopenharmony_ci				goto fail;
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci		d = get_skx_dev(pdev->bus, m->busidx);
1008c2ecf20Sopenharmony_ci		if (!d)
1018c2ecf20Sopenharmony_ci			goto fail;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		/* Be sure that the device is enabled */
1048c2ecf20Sopenharmony_ci		if (unlikely(pci_enable_device(pdev) < 0)) {
1058c2ecf20Sopenharmony_ci			skx_printk(KERN_ERR, "Couldn't enable device %04x:%04x\n",
1068c2ecf20Sopenharmony_ci				   PCI_VENDOR_ID_INTEL, m->did);
1078c2ecf20Sopenharmony_ci			goto fail;
1088c2ecf20Sopenharmony_ci		}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		switch (m->mtype) {
1118c2ecf20Sopenharmony_ci		case CHAN0:
1128c2ecf20Sopenharmony_ci		case CHAN1:
1138c2ecf20Sopenharmony_ci		case CHAN2:
1148c2ecf20Sopenharmony_ci			pci_dev_get(pdev);
1158c2ecf20Sopenharmony_ci			d->imc[i].chan[m->mtype].cdev = pdev;
1168c2ecf20Sopenharmony_ci			break;
1178c2ecf20Sopenharmony_ci		case ERRCHAN0:
1188c2ecf20Sopenharmony_ci		case ERRCHAN1:
1198c2ecf20Sopenharmony_ci		case ERRCHAN2:
1208c2ecf20Sopenharmony_ci			pci_dev_get(pdev);
1218c2ecf20Sopenharmony_ci			d->imc[i].chan[m->mtype - ERRCHAN0].edev = pdev;
1228c2ecf20Sopenharmony_ci			break;
1238c2ecf20Sopenharmony_ci		case SAD_ALL:
1248c2ecf20Sopenharmony_ci			pci_dev_get(pdev);
1258c2ecf20Sopenharmony_ci			d->sad_all = pdev;
1268c2ecf20Sopenharmony_ci			break;
1278c2ecf20Sopenharmony_ci		case UTIL_ALL:
1288c2ecf20Sopenharmony_ci			pci_dev_get(pdev);
1298c2ecf20Sopenharmony_ci			d->util_all = pdev;
1308c2ecf20Sopenharmony_ci			break;
1318c2ecf20Sopenharmony_ci		case SAD:
1328c2ecf20Sopenharmony_ci			/*
1338c2ecf20Sopenharmony_ci			 * one of these devices per core, including cores
1348c2ecf20Sopenharmony_ci			 * that don't exist on this SKU. Ignore any that
1358c2ecf20Sopenharmony_ci			 * read a route table of zero, make sure all the
1368c2ecf20Sopenharmony_ci			 * non-zero values match.
1378c2ecf20Sopenharmony_ci			 */
1388c2ecf20Sopenharmony_ci			pci_read_config_dword(pdev, 0xB4, &reg);
1398c2ecf20Sopenharmony_ci			if (reg != 0) {
1408c2ecf20Sopenharmony_ci				if (d->mcroute == 0) {
1418c2ecf20Sopenharmony_ci					d->mcroute = reg;
1428c2ecf20Sopenharmony_ci				} else if (d->mcroute != reg) {
1438c2ecf20Sopenharmony_ci					skx_printk(KERN_ERR, "mcroute mismatch\n");
1448c2ecf20Sopenharmony_ci					goto fail;
1458c2ecf20Sopenharmony_ci				}
1468c2ecf20Sopenharmony_ci			}
1478c2ecf20Sopenharmony_ci			ndev--;
1488c2ecf20Sopenharmony_ci			break;
1498c2ecf20Sopenharmony_ci		}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		prev = pdev;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	return ndev;
1558c2ecf20Sopenharmony_cifail:
1568c2ecf20Sopenharmony_ci	pci_dev_put(pdev);
1578c2ecf20Sopenharmony_ci	return -ENODEV;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic struct res_config skx_cfg = {
1618c2ecf20Sopenharmony_ci	.type			= SKX,
1628c2ecf20Sopenharmony_ci	.decs_did		= 0x2016,
1638c2ecf20Sopenharmony_ci	.busno_cfg_offset	= 0xcc,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const struct x86_cpu_id skx_cpuids[] = {
1678c2ecf20Sopenharmony_ci	X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(SKYLAKE_X, X86_STEPPINGS(0x0, 0xf), &skx_cfg),
1688c2ecf20Sopenharmony_ci	{ }
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(x86cpu, skx_cpuids);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic bool skx_check_ecc(u32 mcmtr)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return !!GET_BITFIELD(mcmtr, 2, 2);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int skx_get_dimm_config(struct mem_ctl_info *mci)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	struct skx_pvt *pvt = mci->pvt_info;
1808c2ecf20Sopenharmony_ci	u32 mtr, mcmtr, amap, mcddrtcfg;
1818c2ecf20Sopenharmony_ci	struct skx_imc *imc = pvt->imc;
1828c2ecf20Sopenharmony_ci	struct dimm_info *dimm;
1838c2ecf20Sopenharmony_ci	int i, j;
1848c2ecf20Sopenharmony_ci	int ndimms;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* Only the mcmtr on the first channel is effective */
1878c2ecf20Sopenharmony_ci	pci_read_config_dword(imc->chan[0].cdev, 0x87c, &mcmtr);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	for (i = 0; i < SKX_NUM_CHANNELS; i++) {
1908c2ecf20Sopenharmony_ci		ndimms = 0;
1918c2ecf20Sopenharmony_ci		pci_read_config_dword(imc->chan[i].cdev, 0x8C, &amap);
1928c2ecf20Sopenharmony_ci		pci_read_config_dword(imc->chan[i].cdev, 0x400, &mcddrtcfg);
1938c2ecf20Sopenharmony_ci		for (j = 0; j < SKX_NUM_DIMMS; j++) {
1948c2ecf20Sopenharmony_ci			dimm = edac_get_dimm(mci, i, j, 0);
1958c2ecf20Sopenharmony_ci			pci_read_config_dword(imc->chan[i].cdev,
1968c2ecf20Sopenharmony_ci					      0x80 + 4 * j, &mtr);
1978c2ecf20Sopenharmony_ci			if (IS_DIMM_PRESENT(mtr)) {
1988c2ecf20Sopenharmony_ci				ndimms += skx_get_dimm_info(mtr, mcmtr, amap, dimm, imc, i, j);
1998c2ecf20Sopenharmony_ci			} else if (IS_NVDIMM_PRESENT(mcddrtcfg, j)) {
2008c2ecf20Sopenharmony_ci				ndimms += skx_get_nvdimm_info(dimm, imc, i, j,
2018c2ecf20Sopenharmony_ci							      EDAC_MOD_STR);
2028c2ecf20Sopenharmony_ci				nvdimm_count++;
2038c2ecf20Sopenharmony_ci			}
2048c2ecf20Sopenharmony_ci		}
2058c2ecf20Sopenharmony_ci		if (ndimms && !skx_check_ecc(mcmtr)) {
2068c2ecf20Sopenharmony_ci			skx_printk(KERN_ERR, "ECC is disabled on imc %d\n", imc->mc);
2078c2ecf20Sopenharmony_ci			return -ENODEV;
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return 0;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci#define	SKX_MAX_SAD 24
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#define SKX_GET_SAD(d, i, reg)	\
2178c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->sad_all, 0x60 + 8 * (i), &(reg))
2188c2ecf20Sopenharmony_ci#define SKX_GET_ILV(d, i, reg)	\
2198c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->sad_all, 0x64 + 8 * (i), &(reg))
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci#define	SKX_SAD_MOD3MODE(sad)	GET_BITFIELD((sad), 30, 31)
2228c2ecf20Sopenharmony_ci#define	SKX_SAD_MOD3(sad)	GET_BITFIELD((sad), 27, 27)
2238c2ecf20Sopenharmony_ci#define SKX_SAD_LIMIT(sad)	(((u64)GET_BITFIELD((sad), 7, 26) << 26) | MASK26)
2248c2ecf20Sopenharmony_ci#define	SKX_SAD_MOD3ASMOD2(sad)	GET_BITFIELD((sad), 5, 6)
2258c2ecf20Sopenharmony_ci#define	SKX_SAD_ATTR(sad)	GET_BITFIELD((sad), 3, 4)
2268c2ecf20Sopenharmony_ci#define	SKX_SAD_INTERLEAVE(sad)	GET_BITFIELD((sad), 1, 2)
2278c2ecf20Sopenharmony_ci#define SKX_SAD_ENABLE(sad)	GET_BITFIELD((sad), 0, 0)
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#define SKX_ILV_REMOTE(tgt)	(((tgt) & 8) == 0)
2308c2ecf20Sopenharmony_ci#define SKX_ILV_TARGET(tgt)	((tgt) & 7)
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic void skx_show_retry_rd_err_log(struct decoded_addr *res,
2338c2ecf20Sopenharmony_ci				      char *msg, int len)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	u32 log0, log1, log2, log3, log4;
2368c2ecf20Sopenharmony_ci	u32 corr0, corr1, corr2, corr3;
2378c2ecf20Sopenharmony_ci	struct pci_dev *edev;
2388c2ecf20Sopenharmony_ci	int n;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	edev = res->dev->imc[res->imc].chan[res->channel].edev;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x154, &log0);
2438c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x148, &log1);
2448c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x150, &log2);
2458c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x15c, &log3);
2468c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x114, &log4);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	n = snprintf(msg, len, " retry_rd_err_log[%.8x %.8x %.8x %.8x %.8x]",
2498c2ecf20Sopenharmony_ci		     log0, log1, log2, log3, log4);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x104, &corr0);
2528c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x108, &corr1);
2538c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x10c, &corr2);
2548c2ecf20Sopenharmony_ci	pci_read_config_dword(edev, 0x110, &corr3);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (len - n > 0)
2578c2ecf20Sopenharmony_ci		snprintf(msg + n, len - n,
2588c2ecf20Sopenharmony_ci			 " correrrcnt[%.4x %.4x %.4x %.4x %.4x %.4x %.4x %.4x]",
2598c2ecf20Sopenharmony_ci			 corr0 & 0xffff, corr0 >> 16,
2608c2ecf20Sopenharmony_ci			 corr1 & 0xffff, corr1 >> 16,
2618c2ecf20Sopenharmony_ci			 corr2 & 0xffff, corr2 >> 16,
2628c2ecf20Sopenharmony_ci			 corr3 & 0xffff, corr3 >> 16);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic bool skx_sad_decode(struct decoded_addr *res)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	struct skx_dev *d = list_first_entry(skx_edac_list, typeof(*d), list);
2688c2ecf20Sopenharmony_ci	u64 addr = res->addr;
2698c2ecf20Sopenharmony_ci	int i, idx, tgt, lchan, shift;
2708c2ecf20Sopenharmony_ci	u32 sad, ilv;
2718c2ecf20Sopenharmony_ci	u64 limit, prev_limit;
2728c2ecf20Sopenharmony_ci	int remote = 0;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Simple sanity check for I/O space or out of range */
2758c2ecf20Sopenharmony_ci	if (addr >= skx_tohm || (addr >= skx_tolm && addr < BIT_ULL(32))) {
2768c2ecf20Sopenharmony_ci		edac_dbg(0, "Address 0x%llx out of range\n", addr);
2778c2ecf20Sopenharmony_ci		return false;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cirestart:
2818c2ecf20Sopenharmony_ci	prev_limit = 0;
2828c2ecf20Sopenharmony_ci	for (i = 0; i < SKX_MAX_SAD; i++) {
2838c2ecf20Sopenharmony_ci		SKX_GET_SAD(d, i, sad);
2848c2ecf20Sopenharmony_ci		limit = SKX_SAD_LIMIT(sad);
2858c2ecf20Sopenharmony_ci		if (SKX_SAD_ENABLE(sad)) {
2868c2ecf20Sopenharmony_ci			if (addr >= prev_limit && addr <= limit)
2878c2ecf20Sopenharmony_ci				goto sad_found;
2888c2ecf20Sopenharmony_ci		}
2898c2ecf20Sopenharmony_ci		prev_limit = limit + 1;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci	edac_dbg(0, "No SAD entry for 0x%llx\n", addr);
2928c2ecf20Sopenharmony_ci	return false;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cisad_found:
2958c2ecf20Sopenharmony_ci	SKX_GET_ILV(d, i, ilv);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	switch (SKX_SAD_INTERLEAVE(sad)) {
2988c2ecf20Sopenharmony_ci	case 0:
2998c2ecf20Sopenharmony_ci		idx = GET_BITFIELD(addr, 6, 8);
3008c2ecf20Sopenharmony_ci		break;
3018c2ecf20Sopenharmony_ci	case 1:
3028c2ecf20Sopenharmony_ci		idx = GET_BITFIELD(addr, 8, 10);
3038c2ecf20Sopenharmony_ci		break;
3048c2ecf20Sopenharmony_ci	case 2:
3058c2ecf20Sopenharmony_ci		idx = GET_BITFIELD(addr, 12, 14);
3068c2ecf20Sopenharmony_ci		break;
3078c2ecf20Sopenharmony_ci	case 3:
3088c2ecf20Sopenharmony_ci		idx = GET_BITFIELD(addr, 30, 32);
3098c2ecf20Sopenharmony_ci		break;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	tgt = GET_BITFIELD(ilv, 4 * idx, 4 * idx + 3);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* If point to another node, find it and start over */
3158c2ecf20Sopenharmony_ci	if (SKX_ILV_REMOTE(tgt)) {
3168c2ecf20Sopenharmony_ci		if (remote) {
3178c2ecf20Sopenharmony_ci			edac_dbg(0, "Double remote!\n");
3188c2ecf20Sopenharmony_ci			return false;
3198c2ecf20Sopenharmony_ci		}
3208c2ecf20Sopenharmony_ci		remote = 1;
3218c2ecf20Sopenharmony_ci		list_for_each_entry(d, skx_edac_list, list) {
3228c2ecf20Sopenharmony_ci			if (d->imc[0].src_id == SKX_ILV_TARGET(tgt))
3238c2ecf20Sopenharmony_ci				goto restart;
3248c2ecf20Sopenharmony_ci		}
3258c2ecf20Sopenharmony_ci		edac_dbg(0, "Can't find node %d\n", SKX_ILV_TARGET(tgt));
3268c2ecf20Sopenharmony_ci		return false;
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (SKX_SAD_MOD3(sad) == 0) {
3308c2ecf20Sopenharmony_ci		lchan = SKX_ILV_TARGET(tgt);
3318c2ecf20Sopenharmony_ci	} else {
3328c2ecf20Sopenharmony_ci		switch (SKX_SAD_MOD3MODE(sad)) {
3338c2ecf20Sopenharmony_ci		case 0:
3348c2ecf20Sopenharmony_ci			shift = 6;
3358c2ecf20Sopenharmony_ci			break;
3368c2ecf20Sopenharmony_ci		case 1:
3378c2ecf20Sopenharmony_ci			shift = 8;
3388c2ecf20Sopenharmony_ci			break;
3398c2ecf20Sopenharmony_ci		case 2:
3408c2ecf20Sopenharmony_ci			shift = 12;
3418c2ecf20Sopenharmony_ci			break;
3428c2ecf20Sopenharmony_ci		default:
3438c2ecf20Sopenharmony_ci			edac_dbg(0, "illegal mod3mode\n");
3448c2ecf20Sopenharmony_ci			return false;
3458c2ecf20Sopenharmony_ci		}
3468c2ecf20Sopenharmony_ci		switch (SKX_SAD_MOD3ASMOD2(sad)) {
3478c2ecf20Sopenharmony_ci		case 0:
3488c2ecf20Sopenharmony_ci			lchan = (addr >> shift) % 3;
3498c2ecf20Sopenharmony_ci			break;
3508c2ecf20Sopenharmony_ci		case 1:
3518c2ecf20Sopenharmony_ci			lchan = (addr >> shift) % 2;
3528c2ecf20Sopenharmony_ci			break;
3538c2ecf20Sopenharmony_ci		case 2:
3548c2ecf20Sopenharmony_ci			lchan = (addr >> shift) % 2;
3558c2ecf20Sopenharmony_ci			lchan = (lchan << 1) | !lchan;
3568c2ecf20Sopenharmony_ci			break;
3578c2ecf20Sopenharmony_ci		case 3:
3588c2ecf20Sopenharmony_ci			lchan = ((addr >> shift) % 2) << 1;
3598c2ecf20Sopenharmony_ci			break;
3608c2ecf20Sopenharmony_ci		}
3618c2ecf20Sopenharmony_ci		lchan = (lchan << 1) | (SKX_ILV_TARGET(tgt) & 1);
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	res->dev = d;
3658c2ecf20Sopenharmony_ci	res->socket = d->imc[0].src_id;
3668c2ecf20Sopenharmony_ci	res->imc = GET_BITFIELD(d->mcroute, lchan * 3, lchan * 3 + 2);
3678c2ecf20Sopenharmony_ci	res->channel = GET_BITFIELD(d->mcroute, lchan * 2 + 18, lchan * 2 + 19);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	edac_dbg(2, "0x%llx: socket=%d imc=%d channel=%d\n",
3708c2ecf20Sopenharmony_ci		 res->addr, res->socket, res->imc, res->channel);
3718c2ecf20Sopenharmony_ci	return true;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci#define	SKX_MAX_TAD 8
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci#define SKX_GET_TADBASE(d, mc, i, reg)			\
3778c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x850 + 4 * (i), &(reg))
3788c2ecf20Sopenharmony_ci#define SKX_GET_TADWAYNESS(d, mc, i, reg)		\
3798c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->imc[mc].chan[0].cdev, 0x880 + 4 * (i), &(reg))
3808c2ecf20Sopenharmony_ci#define SKX_GET_TADCHNILVOFFSET(d, mc, ch, i, reg)	\
3818c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->imc[mc].chan[ch].cdev, 0x90 + 4 * (i), &(reg))
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci#define	SKX_TAD_BASE(b)		((u64)GET_BITFIELD((b), 12, 31) << 26)
3848c2ecf20Sopenharmony_ci#define SKX_TAD_SKT_GRAN(b)	GET_BITFIELD((b), 4, 5)
3858c2ecf20Sopenharmony_ci#define SKX_TAD_CHN_GRAN(b)	GET_BITFIELD((b), 6, 7)
3868c2ecf20Sopenharmony_ci#define	SKX_TAD_LIMIT(b)	(((u64)GET_BITFIELD((b), 12, 31) << 26) | MASK26)
3878c2ecf20Sopenharmony_ci#define	SKX_TAD_OFFSET(b)	((u64)GET_BITFIELD((b), 4, 23) << 26)
3888c2ecf20Sopenharmony_ci#define	SKX_TAD_SKTWAYS(b)	(1 << GET_BITFIELD((b), 10, 11))
3898c2ecf20Sopenharmony_ci#define	SKX_TAD_CHNWAYS(b)	(GET_BITFIELD((b), 8, 9) + 1)
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci/* which bit used for both socket and channel interleave */
3928c2ecf20Sopenharmony_cistatic int skx_granularity[] = { 6, 8, 12, 30 };
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic u64 skx_do_interleave(u64 addr, int shift, int ways, u64 lowbits)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	addr >>= shift;
3978c2ecf20Sopenharmony_ci	addr /= ways;
3988c2ecf20Sopenharmony_ci	addr <<= shift;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return addr | (lowbits & ((1ull << shift) - 1));
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic bool skx_tad_decode(struct decoded_addr *res)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	int i;
4068c2ecf20Sopenharmony_ci	u32 base, wayness, chnilvoffset;
4078c2ecf20Sopenharmony_ci	int skt_interleave_bit, chn_interleave_bit;
4088c2ecf20Sopenharmony_ci	u64 channel_addr;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	for (i = 0; i < SKX_MAX_TAD; i++) {
4118c2ecf20Sopenharmony_ci		SKX_GET_TADBASE(res->dev, res->imc, i, base);
4128c2ecf20Sopenharmony_ci		SKX_GET_TADWAYNESS(res->dev, res->imc, i, wayness);
4138c2ecf20Sopenharmony_ci		if (SKX_TAD_BASE(base) <= res->addr && res->addr <= SKX_TAD_LIMIT(wayness))
4148c2ecf20Sopenharmony_ci			goto tad_found;
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci	edac_dbg(0, "No TAD entry for 0x%llx\n", res->addr);
4178c2ecf20Sopenharmony_ci	return false;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_citad_found:
4208c2ecf20Sopenharmony_ci	res->sktways = SKX_TAD_SKTWAYS(wayness);
4218c2ecf20Sopenharmony_ci	res->chanways = SKX_TAD_CHNWAYS(wayness);
4228c2ecf20Sopenharmony_ci	skt_interleave_bit = skx_granularity[SKX_TAD_SKT_GRAN(base)];
4238c2ecf20Sopenharmony_ci	chn_interleave_bit = skx_granularity[SKX_TAD_CHN_GRAN(base)];
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	SKX_GET_TADCHNILVOFFSET(res->dev, res->imc, res->channel, i, chnilvoffset);
4268c2ecf20Sopenharmony_ci	channel_addr = res->addr - SKX_TAD_OFFSET(chnilvoffset);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (res->chanways == 3 && skt_interleave_bit > chn_interleave_bit) {
4298c2ecf20Sopenharmony_ci		/* Must handle channel first, then socket */
4308c2ecf20Sopenharmony_ci		channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
4318c2ecf20Sopenharmony_ci						 res->chanways, channel_addr);
4328c2ecf20Sopenharmony_ci		channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
4338c2ecf20Sopenharmony_ci						 res->sktways, channel_addr);
4348c2ecf20Sopenharmony_ci	} else {
4358c2ecf20Sopenharmony_ci		/* Handle socket then channel. Preserve low bits from original address */
4368c2ecf20Sopenharmony_ci		channel_addr = skx_do_interleave(channel_addr, skt_interleave_bit,
4378c2ecf20Sopenharmony_ci						 res->sktways, res->addr);
4388c2ecf20Sopenharmony_ci		channel_addr = skx_do_interleave(channel_addr, chn_interleave_bit,
4398c2ecf20Sopenharmony_ci						 res->chanways, res->addr);
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	res->chan_addr = channel_addr;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	edac_dbg(2, "0x%llx: chan_addr=0x%llx sktways=%d chanways=%d\n",
4458c2ecf20Sopenharmony_ci		 res->addr, res->chan_addr, res->sktways, res->chanways);
4468c2ecf20Sopenharmony_ci	return true;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci#define SKX_MAX_RIR 4
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci#define SKX_GET_RIRWAYNESS(d, mc, ch, i, reg)		\
4528c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->imc[mc].chan[ch].cdev,	\
4538c2ecf20Sopenharmony_ci			      0x108 + 4 * (i), &(reg))
4548c2ecf20Sopenharmony_ci#define SKX_GET_RIRILV(d, mc, ch, idx, i, reg)		\
4558c2ecf20Sopenharmony_ci	pci_read_config_dword((d)->imc[mc].chan[ch].cdev,	\
4568c2ecf20Sopenharmony_ci			      0x120 + 16 * (idx) + 4 * (i), &(reg))
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci#define	SKX_RIR_VALID(b) GET_BITFIELD((b), 31, 31)
4598c2ecf20Sopenharmony_ci#define	SKX_RIR_LIMIT(b) (((u64)GET_BITFIELD((b), 1, 11) << 29) | MASK29)
4608c2ecf20Sopenharmony_ci#define	SKX_RIR_WAYS(b) (1 << GET_BITFIELD((b), 28, 29))
4618c2ecf20Sopenharmony_ci#define	SKX_RIR_CHAN_RANK(b) GET_BITFIELD((b), 16, 19)
4628c2ecf20Sopenharmony_ci#define	SKX_RIR_OFFSET(b) ((u64)(GET_BITFIELD((b), 2, 15) << 26))
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic bool skx_rir_decode(struct decoded_addr *res)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	int i, idx, chan_rank;
4678c2ecf20Sopenharmony_ci	int shift;
4688c2ecf20Sopenharmony_ci	u32 rirway, rirlv;
4698c2ecf20Sopenharmony_ci	u64 rank_addr, prev_limit = 0, limit;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	if (res->dev->imc[res->imc].chan[res->channel].dimms[0].close_pg)
4728c2ecf20Sopenharmony_ci		shift = 6;
4738c2ecf20Sopenharmony_ci	else
4748c2ecf20Sopenharmony_ci		shift = 13;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	for (i = 0; i < SKX_MAX_RIR; i++) {
4778c2ecf20Sopenharmony_ci		SKX_GET_RIRWAYNESS(res->dev, res->imc, res->channel, i, rirway);
4788c2ecf20Sopenharmony_ci		limit = SKX_RIR_LIMIT(rirway);
4798c2ecf20Sopenharmony_ci		if (SKX_RIR_VALID(rirway)) {
4808c2ecf20Sopenharmony_ci			if (prev_limit <= res->chan_addr &&
4818c2ecf20Sopenharmony_ci			    res->chan_addr <= limit)
4828c2ecf20Sopenharmony_ci				goto rir_found;
4838c2ecf20Sopenharmony_ci		}
4848c2ecf20Sopenharmony_ci		prev_limit = limit;
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci	edac_dbg(0, "No RIR entry for 0x%llx\n", res->addr);
4878c2ecf20Sopenharmony_ci	return false;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cirir_found:
4908c2ecf20Sopenharmony_ci	rank_addr = res->chan_addr >> shift;
4918c2ecf20Sopenharmony_ci	rank_addr /= SKX_RIR_WAYS(rirway);
4928c2ecf20Sopenharmony_ci	rank_addr <<= shift;
4938c2ecf20Sopenharmony_ci	rank_addr |= res->chan_addr & GENMASK_ULL(shift - 1, 0);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	res->rank_address = rank_addr;
4968c2ecf20Sopenharmony_ci	idx = (res->chan_addr >> shift) % SKX_RIR_WAYS(rirway);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	SKX_GET_RIRILV(res->dev, res->imc, res->channel, idx, i, rirlv);
4998c2ecf20Sopenharmony_ci	res->rank_address = rank_addr - SKX_RIR_OFFSET(rirlv);
5008c2ecf20Sopenharmony_ci	chan_rank = SKX_RIR_CHAN_RANK(rirlv);
5018c2ecf20Sopenharmony_ci	res->channel_rank = chan_rank;
5028c2ecf20Sopenharmony_ci	res->dimm = chan_rank / 4;
5038c2ecf20Sopenharmony_ci	res->rank = chan_rank % 4;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	edac_dbg(2, "0x%llx: dimm=%d rank=%d chan_rank=%d rank_addr=0x%llx\n",
5068c2ecf20Sopenharmony_ci		 res->addr, res->dimm, res->rank,
5078c2ecf20Sopenharmony_ci		 res->channel_rank, res->rank_address);
5088c2ecf20Sopenharmony_ci	return true;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic u8 skx_close_row[] = {
5128c2ecf20Sopenharmony_ci	15, 16, 17, 18, 20, 21, 22, 28, 10, 11, 12, 13, 29, 30, 31, 32, 33, 34
5138c2ecf20Sopenharmony_ci};
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic u8 skx_close_column[] = {
5168c2ecf20Sopenharmony_ci	3, 4, 5, 14, 19, 23, 24, 25, 26, 27
5178c2ecf20Sopenharmony_ci};
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic u8 skx_open_row[] = {
5208c2ecf20Sopenharmony_ci	14, 15, 16, 20, 28, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34
5218c2ecf20Sopenharmony_ci};
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic u8 skx_open_column[] = {
5248c2ecf20Sopenharmony_ci	3, 4, 5, 6, 7, 8, 9, 10, 11, 12
5258c2ecf20Sopenharmony_ci};
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic u8 skx_open_fine_column[] = {
5288c2ecf20Sopenharmony_ci	3, 4, 5, 7, 8, 9, 10, 11, 12, 13
5298c2ecf20Sopenharmony_ci};
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic int skx_bits(u64 addr, int nbits, u8 *bits)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	int i, res = 0;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	for (i = 0; i < nbits; i++)
5368c2ecf20Sopenharmony_ci		res |= ((addr >> bits[i]) & 1) << i;
5378c2ecf20Sopenharmony_ci	return res;
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_cistatic int skx_bank_bits(u64 addr, int b0, int b1, int do_xor, int x0, int x1)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	int ret = GET_BITFIELD(addr, b0, b0) | (GET_BITFIELD(addr, b1, b1) << 1);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (do_xor)
5458c2ecf20Sopenharmony_ci		ret ^= GET_BITFIELD(addr, x0, x0) | (GET_BITFIELD(addr, x1, x1) << 1);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	return ret;
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic bool skx_mad_decode(struct decoded_addr *r)
5518c2ecf20Sopenharmony_ci{
5528c2ecf20Sopenharmony_ci	struct skx_dimm *dimm = &r->dev->imc[r->imc].chan[r->channel].dimms[r->dimm];
5538c2ecf20Sopenharmony_ci	int bg0 = dimm->fine_grain_bank ? 6 : 13;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (dimm->close_pg) {
5568c2ecf20Sopenharmony_ci		r->row = skx_bits(r->rank_address, dimm->rowbits, skx_close_row);
5578c2ecf20Sopenharmony_ci		r->column = skx_bits(r->rank_address, dimm->colbits, skx_close_column);
5588c2ecf20Sopenharmony_ci		r->column |= 0x400; /* C10 is autoprecharge, always set */
5598c2ecf20Sopenharmony_ci		r->bank_address = skx_bank_bits(r->rank_address, 8, 9, dimm->bank_xor_enable, 22, 28);
5608c2ecf20Sopenharmony_ci		r->bank_group = skx_bank_bits(r->rank_address, 6, 7, dimm->bank_xor_enable, 20, 21);
5618c2ecf20Sopenharmony_ci	} else {
5628c2ecf20Sopenharmony_ci		r->row = skx_bits(r->rank_address, dimm->rowbits, skx_open_row);
5638c2ecf20Sopenharmony_ci		if (dimm->fine_grain_bank)
5648c2ecf20Sopenharmony_ci			r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_fine_column);
5658c2ecf20Sopenharmony_ci		else
5668c2ecf20Sopenharmony_ci			r->column = skx_bits(r->rank_address, dimm->colbits, skx_open_column);
5678c2ecf20Sopenharmony_ci		r->bank_address = skx_bank_bits(r->rank_address, 18, 19, dimm->bank_xor_enable, 22, 23);
5688c2ecf20Sopenharmony_ci		r->bank_group = skx_bank_bits(r->rank_address, bg0, 17, dimm->bank_xor_enable, 20, 21);
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci	r->row &= (1u << dimm->rowbits) - 1;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	edac_dbg(2, "0x%llx: row=0x%x col=0x%x bank_addr=%d bank_group=%d\n",
5738c2ecf20Sopenharmony_ci		 r->addr, r->row, r->column, r->bank_address,
5748c2ecf20Sopenharmony_ci		 r->bank_group);
5758c2ecf20Sopenharmony_ci	return true;
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic bool skx_decode(struct decoded_addr *res)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	return skx_sad_decode(res) && skx_tad_decode(res) &&
5818c2ecf20Sopenharmony_ci		skx_rir_decode(res) && skx_mad_decode(res);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic struct notifier_block skx_mce_dec = {
5858c2ecf20Sopenharmony_ci	.notifier_call	= skx_mce_check_error,
5868c2ecf20Sopenharmony_ci	.priority	= MCE_PRIO_EDAC,
5878c2ecf20Sopenharmony_ci};
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci#ifdef CONFIG_EDAC_DEBUG
5908c2ecf20Sopenharmony_ci/*
5918c2ecf20Sopenharmony_ci * Debug feature.
5928c2ecf20Sopenharmony_ci * Exercise the address decode logic by writing an address to
5938c2ecf20Sopenharmony_ci * /sys/kernel/debug/edac/skx_test/addr.
5948c2ecf20Sopenharmony_ci */
5958c2ecf20Sopenharmony_cistatic struct dentry *skx_test;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic int debugfs_u64_set(void *data, u64 val)
5988c2ecf20Sopenharmony_ci{
5998c2ecf20Sopenharmony_ci	struct mce m;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	pr_warn_once("Fake error to 0x%llx injected via debugfs\n", val);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	memset(&m, 0, sizeof(m));
6048c2ecf20Sopenharmony_ci	/* ADDRV + MemRd + Unknown channel */
6058c2ecf20Sopenharmony_ci	m.status = MCI_STATUS_ADDRV + 0x90;
6068c2ecf20Sopenharmony_ci	/* One corrected error */
6078c2ecf20Sopenharmony_ci	m.status |= BIT_ULL(MCI_STATUS_CEC_SHIFT);
6088c2ecf20Sopenharmony_ci	m.addr = val;
6098c2ecf20Sopenharmony_ci	skx_mce_check_error(NULL, 0, &m);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	return 0;
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ciDEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_cistatic void setup_skx_debug(void)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	skx_test = edac_debugfs_create_dir("skx_test");
6188c2ecf20Sopenharmony_ci	if (!skx_test)
6198c2ecf20Sopenharmony_ci		return;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (!edac_debugfs_create_file("addr", 0200, skx_test,
6228c2ecf20Sopenharmony_ci				      NULL, &fops_u64_wo)) {
6238c2ecf20Sopenharmony_ci		debugfs_remove(skx_test);
6248c2ecf20Sopenharmony_ci		skx_test = NULL;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_cistatic void teardown_skx_debug(void)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	debugfs_remove_recursive(skx_test);
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci#else
6338c2ecf20Sopenharmony_cistatic inline void setup_skx_debug(void) {}
6348c2ecf20Sopenharmony_cistatic inline void teardown_skx_debug(void) {}
6358c2ecf20Sopenharmony_ci#endif /*CONFIG_EDAC_DEBUG*/
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci/*
6388c2ecf20Sopenharmony_ci * skx_init:
6398c2ecf20Sopenharmony_ci *	make sure we are running on the correct cpu model
6408c2ecf20Sopenharmony_ci *	search for all the devices we need
6418c2ecf20Sopenharmony_ci *	check which DIMMs are present.
6428c2ecf20Sopenharmony_ci */
6438c2ecf20Sopenharmony_cistatic int __init skx_init(void)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	const struct x86_cpu_id *id;
6468c2ecf20Sopenharmony_ci	struct res_config *cfg;
6478c2ecf20Sopenharmony_ci	const struct munit *m;
6488c2ecf20Sopenharmony_ci	const char *owner;
6498c2ecf20Sopenharmony_ci	int rc = 0, i, off[3] = {0xd0, 0xd4, 0xd8};
6508c2ecf20Sopenharmony_ci	u8 mc = 0, src_id, node_id;
6518c2ecf20Sopenharmony_ci	struct skx_dev *d;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	edac_dbg(2, "\n");
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	owner = edac_get_owner();
6568c2ecf20Sopenharmony_ci	if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
6578c2ecf20Sopenharmony_ci		return -EBUSY;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
6608c2ecf20Sopenharmony_ci		return -ENODEV;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	id = x86_match_cpu(skx_cpuids);
6638c2ecf20Sopenharmony_ci	if (!id)
6648c2ecf20Sopenharmony_ci		return -ENODEV;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	cfg = (struct res_config *)id->driver_data;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	rc = skx_get_hi_lo(0x2034, off, &skx_tolm, &skx_tohm);
6698c2ecf20Sopenharmony_ci	if (rc)
6708c2ecf20Sopenharmony_ci		return rc;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	rc = skx_get_all_bus_mappings(cfg, &skx_edac_list);
6738c2ecf20Sopenharmony_ci	if (rc < 0)
6748c2ecf20Sopenharmony_ci		goto fail;
6758c2ecf20Sopenharmony_ci	if (rc == 0) {
6768c2ecf20Sopenharmony_ci		edac_dbg(2, "No memory controllers found\n");
6778c2ecf20Sopenharmony_ci		return -ENODEV;
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci	skx_num_sockets = rc;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	for (m = skx_all_munits; m->did; m++) {
6828c2ecf20Sopenharmony_ci		rc = get_all_munits(m);
6838c2ecf20Sopenharmony_ci		if (rc < 0)
6848c2ecf20Sopenharmony_ci			goto fail;
6858c2ecf20Sopenharmony_ci		if (rc != m->per_socket * skx_num_sockets) {
6868c2ecf20Sopenharmony_ci			edac_dbg(2, "Expected %d, got %d of 0x%x\n",
6878c2ecf20Sopenharmony_ci				 m->per_socket * skx_num_sockets, rc, m->did);
6888c2ecf20Sopenharmony_ci			rc = -ENODEV;
6898c2ecf20Sopenharmony_ci			goto fail;
6908c2ecf20Sopenharmony_ci		}
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	list_for_each_entry(d, skx_edac_list, list) {
6948c2ecf20Sopenharmony_ci		rc = skx_get_src_id(d, 0xf0, &src_id);
6958c2ecf20Sopenharmony_ci		if (rc < 0)
6968c2ecf20Sopenharmony_ci			goto fail;
6978c2ecf20Sopenharmony_ci		rc = skx_get_node_id(d, &node_id);
6988c2ecf20Sopenharmony_ci		if (rc < 0)
6998c2ecf20Sopenharmony_ci			goto fail;
7008c2ecf20Sopenharmony_ci		edac_dbg(2, "src_id=%d node_id=%d\n", src_id, node_id);
7018c2ecf20Sopenharmony_ci		for (i = 0; i < SKX_NUM_IMC; i++) {
7028c2ecf20Sopenharmony_ci			d->imc[i].mc = mc++;
7038c2ecf20Sopenharmony_ci			d->imc[i].lmc = i;
7048c2ecf20Sopenharmony_ci			d->imc[i].src_id = src_id;
7058c2ecf20Sopenharmony_ci			d->imc[i].node_id = node_id;
7068c2ecf20Sopenharmony_ci			rc = skx_register_mci(&d->imc[i], d->imc[i].chan[0].cdev,
7078c2ecf20Sopenharmony_ci					      "Skylake Socket", EDAC_MOD_STR,
7088c2ecf20Sopenharmony_ci					      skx_get_dimm_config);
7098c2ecf20Sopenharmony_ci			if (rc < 0)
7108c2ecf20Sopenharmony_ci				goto fail;
7118c2ecf20Sopenharmony_ci		}
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	skx_set_decode(skx_decode, skx_show_retry_rd_err_log);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	if (nvdimm_count && skx_adxl_get() == -ENODEV)
7178c2ecf20Sopenharmony_ci		skx_printk(KERN_NOTICE, "Only decoding DDR4 address!\n");
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
7208c2ecf20Sopenharmony_ci	opstate_init();
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	setup_skx_debug();
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	mce_register_decode_chain(&skx_mce_dec);
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	return 0;
7278c2ecf20Sopenharmony_cifail:
7288c2ecf20Sopenharmony_ci	skx_remove();
7298c2ecf20Sopenharmony_ci	return rc;
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic void __exit skx_exit(void)
7338c2ecf20Sopenharmony_ci{
7348c2ecf20Sopenharmony_ci	edac_dbg(2, "\n");
7358c2ecf20Sopenharmony_ci	mce_unregister_decode_chain(&skx_mce_dec);
7368c2ecf20Sopenharmony_ci	teardown_skx_debug();
7378c2ecf20Sopenharmony_ci	if (nvdimm_count)
7388c2ecf20Sopenharmony_ci		skx_adxl_put();
7398c2ecf20Sopenharmony_ci	skx_remove();
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cimodule_init(skx_init);
7438c2ecf20Sopenharmony_cimodule_exit(skx_exit);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_cimodule_param(edac_op_state, int, 0444);
7468c2ecf20Sopenharmony_ciMODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
7498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tony Luck");
7508c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MC Driver for Intel Skylake server processors");
751