18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * AMD64 class Memory Controller kernel module
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 2009 SoftwareBitMaker.
58c2ecf20Sopenharmony_ci * Copyright (c) 2009-15 Advanced Micro Devices, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This file may be distributed under the terms of the
88c2ecf20Sopenharmony_ci * GNU General Public License.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/ctype.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/mmzone.h>
188c2ecf20Sopenharmony_ci#include <linux/edac.h>
198c2ecf20Sopenharmony_ci#include <asm/cpu_device_id.h>
208c2ecf20Sopenharmony_ci#include <asm/msr.h>
218c2ecf20Sopenharmony_ci#include "edac_module.h"
228c2ecf20Sopenharmony_ci#include "mce_amd.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define amd64_info(fmt, arg...) \
258c2ecf20Sopenharmony_ci	edac_printk(KERN_INFO, "amd64", fmt, ##arg)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define amd64_warn(fmt, arg...) \
288c2ecf20Sopenharmony_ci	edac_printk(KERN_WARNING, "amd64", "Warning: " fmt, ##arg)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define amd64_err(fmt, arg...) \
318c2ecf20Sopenharmony_ci	edac_printk(KERN_ERR, "amd64", "Error: " fmt, ##arg)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define amd64_mc_warn(mci, fmt, arg...) \
348c2ecf20Sopenharmony_ci	edac_mc_chipset_printk(mci, KERN_WARNING, "amd64", fmt, ##arg)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define amd64_mc_err(mci, fmt, arg...) \
378c2ecf20Sopenharmony_ci	edac_mc_chipset_printk(mci, KERN_ERR, "amd64", fmt, ##arg)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * Throughout the comments in this code, the following terms are used:
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci *	SysAddr, DramAddr, and InputAddr
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci *  These terms come directly from the amd64 documentation
458c2ecf20Sopenharmony_ci * (AMD publication #26094).  They are defined as follows:
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci *     SysAddr:
488c2ecf20Sopenharmony_ci *         This is a physical address generated by a CPU core or a device
498c2ecf20Sopenharmony_ci *         doing DMA.  If generated by a CPU core, a SysAddr is the result of
508c2ecf20Sopenharmony_ci *         a virtual to physical address translation by the CPU core's address
518c2ecf20Sopenharmony_ci *         translation mechanism (MMU).
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci *     DramAddr:
548c2ecf20Sopenharmony_ci *         A DramAddr is derived from a SysAddr by subtracting an offset that
558c2ecf20Sopenharmony_ci *         depends on which node the SysAddr maps to and whether the SysAddr
568c2ecf20Sopenharmony_ci *         is within a range affected by memory hoisting.  The DRAM Base
578c2ecf20Sopenharmony_ci *         (section 3.4.4.1) and DRAM Limit (section 3.4.4.2) registers
588c2ecf20Sopenharmony_ci *         determine which node a SysAddr maps to.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci *         If the DRAM Hole Address Register (DHAR) is enabled and the SysAddr
618c2ecf20Sopenharmony_ci *         is within the range of addresses specified by this register, then
628c2ecf20Sopenharmony_ci *         a value x from the DHAR is subtracted from the SysAddr to produce a
638c2ecf20Sopenharmony_ci *         DramAddr.  Here, x represents the base address for the node that
648c2ecf20Sopenharmony_ci *         the SysAddr maps to plus an offset due to memory hoisting.  See
658c2ecf20Sopenharmony_ci *         section 3.4.8 and the comments in amd64_get_dram_hole_info() and
668c2ecf20Sopenharmony_ci *         sys_addr_to_dram_addr() below for more information.
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci *         If the SysAddr is not affected by the DHAR then a value y is
698c2ecf20Sopenharmony_ci *         subtracted from the SysAddr to produce a DramAddr.  Here, y is the
708c2ecf20Sopenharmony_ci *         base address for the node that the SysAddr maps to.  See section
718c2ecf20Sopenharmony_ci *         3.4.4 and the comments in sys_addr_to_dram_addr() below for more
728c2ecf20Sopenharmony_ci *         information.
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci *     InputAddr:
758c2ecf20Sopenharmony_ci *         A DramAddr is translated to an InputAddr before being passed to the
768c2ecf20Sopenharmony_ci *         memory controller for the node that the DramAddr is associated
778c2ecf20Sopenharmony_ci *         with.  The memory controller then maps the InputAddr to a csrow.
788c2ecf20Sopenharmony_ci *         If node interleaving is not in use, then the InputAddr has the same
798c2ecf20Sopenharmony_ci *         value as the DramAddr.  Otherwise, the InputAddr is produced by
808c2ecf20Sopenharmony_ci *         discarding the bits used for node interleaving from the DramAddr.
818c2ecf20Sopenharmony_ci *         See section 3.4.4 for more information.
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci *         The memory controller for a given node uses its DRAM CS Base and
848c2ecf20Sopenharmony_ci *         DRAM CS Mask registers to map an InputAddr to a csrow.  See
858c2ecf20Sopenharmony_ci *         sections 3.5.4 and 3.5.5 for more information.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define EDAC_AMD64_VERSION		"3.5.0"
898c2ecf20Sopenharmony_ci#define EDAC_MOD_STR			"amd64_edac"
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* Extended Model from CPUID, for CPU Revision numbers */
928c2ecf20Sopenharmony_ci#define K8_REV_D			1
938c2ecf20Sopenharmony_ci#define K8_REV_E			2
948c2ecf20Sopenharmony_ci#define K8_REV_F			4
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* Hardware limit on ChipSelect rows per MC and processors per system */
978c2ecf20Sopenharmony_ci#define NUM_CHIPSELECTS			8
988c2ecf20Sopenharmony_ci#define DRAM_RANGES			8
998c2ecf20Sopenharmony_ci#define NUM_CONTROLLERS			8
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define ON true
1028c2ecf20Sopenharmony_ci#define OFF false
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * PCI-defined configuration space registers
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_15H_NB_F1	0x1601
1088c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_15H_NB_F2	0x1602
1098c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_15H_M30H_NB_F1 0x141b
1108c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_15H_M30H_NB_F2 0x141c
1118c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_15H_M60H_NB_F1 0x1571
1128c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_15H_M60H_NB_F2 0x1572
1138c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_16H_NB_F1	0x1531
1148c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_16H_NB_F2	0x1532
1158c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_16H_M30H_NB_F1 0x1581
1168c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_16H_M30H_NB_F2 0x1582
1178c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_DF_F0	0x1460
1188c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_DF_F6	0x1466
1198c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M10H_DF_F0 0x15e8
1208c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M10H_DF_F6 0x15ee
1218c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M30H_DF_F0 0x1490
1228c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M30H_DF_F6 0x1496
1238c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M60H_DF_F0 0x1448
1248c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M60H_DF_F6 0x144e
1258c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M70H_DF_F0 0x1440
1268c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_17H_M70H_DF_F6 0x1446
1278c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_19H_DF_F0	0x1650
1288c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AMD_19H_DF_F6	0x1656
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/*
1318c2ecf20Sopenharmony_ci * Function 1 - Address Map
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_ci#define DRAM_BASE_LO			0x40
1348c2ecf20Sopenharmony_ci#define DRAM_LIMIT_LO			0x44
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/*
1378c2ecf20Sopenharmony_ci * F15 M30h D18F1x2[1C:00]
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_ci#define DRAM_CONT_BASE			0x200
1408c2ecf20Sopenharmony_ci#define DRAM_CONT_LIMIT			0x204
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/*
1438c2ecf20Sopenharmony_ci * F15 M30h D18F1x2[4C:40]
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_ci#define DRAM_CONT_HIGH_OFF		0x240
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define dram_rw(pvt, i)			((u8)(pvt->ranges[i].base.lo & 0x3))
1488c2ecf20Sopenharmony_ci#define dram_intlv_sel(pvt, i)		((u8)((pvt->ranges[i].lim.lo >> 8) & 0x7))
1498c2ecf20Sopenharmony_ci#define dram_dst_node(pvt, i)		((u8)(pvt->ranges[i].lim.lo & 0x7))
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#define DHAR				0xf0
1528c2ecf20Sopenharmony_ci#define dhar_mem_hoist_valid(pvt)	((pvt)->dhar & BIT(1))
1538c2ecf20Sopenharmony_ci#define dhar_base(pvt)			((pvt)->dhar & 0xff000000)
1548c2ecf20Sopenharmony_ci#define k8_dhar_offset(pvt)		(((pvt)->dhar & 0x0000ff00) << 16)
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci					/* NOTE: Extra mask bit vs K8 */
1578c2ecf20Sopenharmony_ci#define f10_dhar_offset(pvt)		(((pvt)->dhar & 0x0000ff80) << 16)
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define DCT_CFG_SEL			0x10C
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#define DRAM_LOCAL_NODE_BASE		0x120
1628c2ecf20Sopenharmony_ci#define DRAM_LOCAL_NODE_LIM		0x124
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define DRAM_BASE_HI			0x140
1658c2ecf20Sopenharmony_ci#define DRAM_LIMIT_HI			0x144
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Function 2 - DRAM controller
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_ci#define DCSB0				0x40
1728c2ecf20Sopenharmony_ci#define DCSB1				0x140
1738c2ecf20Sopenharmony_ci#define DCSB_CS_ENABLE			BIT(0)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define DCSM0				0x60
1768c2ecf20Sopenharmony_ci#define DCSM1				0x160
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#define csrow_enabled(i, dct, pvt)	((pvt)->csels[(dct)].csbases[(i)]     & DCSB_CS_ENABLE)
1798c2ecf20Sopenharmony_ci#define csrow_sec_enabled(i, dct, pvt)	((pvt)->csels[(dct)].csbases_sec[(i)] & DCSB_CS_ENABLE)
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#define DRAM_CONTROL			0x78
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci#define DBAM0				0x80
1848c2ecf20Sopenharmony_ci#define DBAM1				0x180
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/* Extract the DIMM 'type' on the i'th DIMM from the DBAM reg value passed */
1878c2ecf20Sopenharmony_ci#define DBAM_DIMM(i, reg)		((((reg) >> (4*(i)))) & 0xF)
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci#define DBAM_MAX_VALUE			11
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define DCLR0				0x90
1928c2ecf20Sopenharmony_ci#define DCLR1				0x190
1938c2ecf20Sopenharmony_ci#define REVE_WIDTH_128			BIT(16)
1948c2ecf20Sopenharmony_ci#define WIDTH_128			BIT(11)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define DCHR0				0x94
1978c2ecf20Sopenharmony_ci#define DCHR1				0x194
1988c2ecf20Sopenharmony_ci#define DDR3_MODE			BIT(8)
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci#define DCT_SEL_LO			0x110
2018c2ecf20Sopenharmony_ci#define dct_high_range_enabled(pvt)	((pvt)->dct_sel_lo & BIT(0))
2028c2ecf20Sopenharmony_ci#define dct_interleave_enabled(pvt)	((pvt)->dct_sel_lo & BIT(2))
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#define dct_ganging_enabled(pvt)	((boot_cpu_data.x86 == 0x10) && ((pvt)->dct_sel_lo & BIT(4)))
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#define dct_data_intlv_enabled(pvt)	((pvt)->dct_sel_lo & BIT(5))
2078c2ecf20Sopenharmony_ci#define dct_memory_cleared(pvt)		((pvt)->dct_sel_lo & BIT(10))
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#define SWAP_INTLV_REG			0x10c
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci#define DCT_SEL_HI			0x114
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define F15H_M60H_SCRCTRL		0x1C8
2148c2ecf20Sopenharmony_ci#define F17H_SCR_BASE_ADDR		0x48
2158c2ecf20Sopenharmony_ci#define F17H_SCR_LIMIT_ADDR		0x4C
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/*
2188c2ecf20Sopenharmony_ci * Function 3 - Misc Control
2198c2ecf20Sopenharmony_ci */
2208c2ecf20Sopenharmony_ci#define NBCTL				0x40
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#define NBCFG				0x44
2238c2ecf20Sopenharmony_ci#define NBCFG_CHIPKILL			BIT(23)
2248c2ecf20Sopenharmony_ci#define NBCFG_ECC_ENABLE		BIT(22)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/* F3x48: NBSL */
2278c2ecf20Sopenharmony_ci#define F10_NBSL_EXT_ERR_ECC		0x8
2288c2ecf20Sopenharmony_ci#define NBSL_PP_OBS			0x2
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci#define SCRCTRL				0x58
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci#define F10_ONLINE_SPARE		0xB0
2338c2ecf20Sopenharmony_ci#define online_spare_swap_done(pvt, c)	(((pvt)->online_spare >> (1 + 2 * (c))) & 0x1)
2348c2ecf20Sopenharmony_ci#define online_spare_bad_dramcs(pvt, c)	(((pvt)->online_spare >> (4 + 4 * (c))) & 0x7)
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#define F10_NB_ARRAY_ADDR		0xB8
2378c2ecf20Sopenharmony_ci#define F10_NB_ARRAY_DRAM		BIT(31)
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* Bits [2:1] are used to select 16-byte section within a 64-byte cacheline  */
2408c2ecf20Sopenharmony_ci#define SET_NB_ARRAY_ADDR(section)	(((section) & 0x3) << 1)
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci#define F10_NB_ARRAY_DATA		0xBC
2438c2ecf20Sopenharmony_ci#define F10_NB_ARR_ECC_WR_REQ		BIT(17)
2448c2ecf20Sopenharmony_ci#define SET_NB_DRAM_INJECTION_WRITE(inj)  \
2458c2ecf20Sopenharmony_ci					(BIT(((inj.word) & 0xF) + 20) | \
2468c2ecf20Sopenharmony_ci					F10_NB_ARR_ECC_WR_REQ | inj.bit_map)
2478c2ecf20Sopenharmony_ci#define SET_NB_DRAM_INJECTION_READ(inj)  \
2488c2ecf20Sopenharmony_ci					(BIT(((inj.word) & 0xF) + 20) | \
2498c2ecf20Sopenharmony_ci					BIT(16) |  inj.bit_map)
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci#define NBCAP				0xE8
2538c2ecf20Sopenharmony_ci#define NBCAP_CHIPKILL			BIT(4)
2548c2ecf20Sopenharmony_ci#define NBCAP_SECDED			BIT(3)
2558c2ecf20Sopenharmony_ci#define NBCAP_DCT_DUAL			BIT(0)
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci#define EXT_NB_MCA_CFG			0x180
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/* MSRs */
2608c2ecf20Sopenharmony_ci#define MSR_MCGCTL_NBE			BIT(4)
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci/* F17h */
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/* F0: */
2658c2ecf20Sopenharmony_ci#define DF_DHAR				0x104
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/* UMC CH register offsets */
2688c2ecf20Sopenharmony_ci#define UMCCH_BASE_ADDR			0x0
2698c2ecf20Sopenharmony_ci#define UMCCH_BASE_ADDR_SEC		0x10
2708c2ecf20Sopenharmony_ci#define UMCCH_ADDR_MASK			0x20
2718c2ecf20Sopenharmony_ci#define UMCCH_ADDR_MASK_SEC		0x28
2728c2ecf20Sopenharmony_ci#define UMCCH_ADDR_CFG			0x30
2738c2ecf20Sopenharmony_ci#define UMCCH_DIMM_CFG			0x80
2748c2ecf20Sopenharmony_ci#define UMCCH_UMC_CFG			0x100
2758c2ecf20Sopenharmony_ci#define UMCCH_SDP_CTRL			0x104
2768c2ecf20Sopenharmony_ci#define UMCCH_ECC_CTRL			0x14C
2778c2ecf20Sopenharmony_ci#define UMCCH_ECC_BAD_SYMBOL		0xD90
2788c2ecf20Sopenharmony_ci#define UMCCH_UMC_CAP			0xDF0
2798c2ecf20Sopenharmony_ci#define UMCCH_UMC_CAP_HI		0xDF4
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/* UMC CH bitfields */
2828c2ecf20Sopenharmony_ci#define UMC_ECC_CHIPKILL_CAP		BIT(31)
2838c2ecf20Sopenharmony_ci#define UMC_ECC_ENABLED			BIT(30)
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci#define UMC_SDP_INIT			BIT(31)
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cienum amd_families {
2888c2ecf20Sopenharmony_ci	K8_CPUS = 0,
2898c2ecf20Sopenharmony_ci	F10_CPUS,
2908c2ecf20Sopenharmony_ci	F15_CPUS,
2918c2ecf20Sopenharmony_ci	F15_M30H_CPUS,
2928c2ecf20Sopenharmony_ci	F15_M60H_CPUS,
2938c2ecf20Sopenharmony_ci	F16_CPUS,
2948c2ecf20Sopenharmony_ci	F16_M30H_CPUS,
2958c2ecf20Sopenharmony_ci	F17_CPUS,
2968c2ecf20Sopenharmony_ci	F17_M10H_CPUS,
2978c2ecf20Sopenharmony_ci	F17_M30H_CPUS,
2988c2ecf20Sopenharmony_ci	F17_M60H_CPUS,
2998c2ecf20Sopenharmony_ci	F17_M70H_CPUS,
3008c2ecf20Sopenharmony_ci	F19_CPUS,
3018c2ecf20Sopenharmony_ci	NUM_FAMILIES,
3028c2ecf20Sopenharmony_ci};
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/* Error injection control structure */
3058c2ecf20Sopenharmony_cistruct error_injection {
3068c2ecf20Sopenharmony_ci	u32	 section;
3078c2ecf20Sopenharmony_ci	u32	 word;
3088c2ecf20Sopenharmony_ci	u32	 bit_map;
3098c2ecf20Sopenharmony_ci};
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/* low and high part of PCI config space regs */
3128c2ecf20Sopenharmony_cistruct reg_pair {
3138c2ecf20Sopenharmony_ci	u32 lo, hi;
3148c2ecf20Sopenharmony_ci};
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci/*
3178c2ecf20Sopenharmony_ci * See F1x[1, 0][7C:40] DRAM Base/Limit Registers
3188c2ecf20Sopenharmony_ci */
3198c2ecf20Sopenharmony_cistruct dram_range {
3208c2ecf20Sopenharmony_ci	struct reg_pair base;
3218c2ecf20Sopenharmony_ci	struct reg_pair lim;
3228c2ecf20Sopenharmony_ci};
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci/* A DCT chip selects collection */
3258c2ecf20Sopenharmony_cistruct chip_select {
3268c2ecf20Sopenharmony_ci	u32 csbases[NUM_CHIPSELECTS];
3278c2ecf20Sopenharmony_ci	u32 csbases_sec[NUM_CHIPSELECTS];
3288c2ecf20Sopenharmony_ci	u8 b_cnt;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	u32 csmasks[NUM_CHIPSELECTS];
3318c2ecf20Sopenharmony_ci	u32 csmasks_sec[NUM_CHIPSELECTS];
3328c2ecf20Sopenharmony_ci	u8 m_cnt;
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistruct amd64_umc {
3368c2ecf20Sopenharmony_ci	u32 dimm_cfg;		/* DIMM Configuration reg */
3378c2ecf20Sopenharmony_ci	u32 umc_cfg;		/* Configuration reg */
3388c2ecf20Sopenharmony_ci	u32 sdp_ctrl;		/* SDP Control reg */
3398c2ecf20Sopenharmony_ci	u32 ecc_ctrl;		/* DRAM ECC Control reg */
3408c2ecf20Sopenharmony_ci	u32 umc_cap_hi;		/* Capabilities High reg */
3418c2ecf20Sopenharmony_ci};
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistruct amd64_pvt {
3448c2ecf20Sopenharmony_ci	struct low_ops *ops;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* pci_device handles which we utilize */
3478c2ecf20Sopenharmony_ci	struct pci_dev *F0, *F1, *F2, *F3, *F6;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	u16 mc_node_id;		/* MC index of this MC node */
3508c2ecf20Sopenharmony_ci	u8 fam;			/* CPU family */
3518c2ecf20Sopenharmony_ci	u8 model;		/* ... model */
3528c2ecf20Sopenharmony_ci	u8 stepping;		/* ... stepping */
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	int ext_model;		/* extended model value of this node */
3558c2ecf20Sopenharmony_ci	int channel_count;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* Raw registers */
3588c2ecf20Sopenharmony_ci	u32 dclr0;		/* DRAM Configuration Low DCT0 reg */
3598c2ecf20Sopenharmony_ci	u32 dclr1;		/* DRAM Configuration Low DCT1 reg */
3608c2ecf20Sopenharmony_ci	u32 dchr0;		/* DRAM Configuration High DCT0 reg */
3618c2ecf20Sopenharmony_ci	u32 dchr1;		/* DRAM Configuration High DCT1 reg */
3628c2ecf20Sopenharmony_ci	u32 nbcap;		/* North Bridge Capabilities */
3638c2ecf20Sopenharmony_ci	u32 nbcfg;		/* F10 North Bridge Configuration */
3648c2ecf20Sopenharmony_ci	u32 ext_nbcfg;		/* Extended F10 North Bridge Configuration */
3658c2ecf20Sopenharmony_ci	u32 dhar;		/* DRAM Hoist reg */
3668c2ecf20Sopenharmony_ci	u32 dbam0;		/* DRAM Base Address Mapping reg for DCT0 */
3678c2ecf20Sopenharmony_ci	u32 dbam1;		/* DRAM Base Address Mapping reg for DCT1 */
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	/* one for each DCT/UMC */
3708c2ecf20Sopenharmony_ci	struct chip_select csels[NUM_CONTROLLERS];
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* DRAM base and limit pairs F1x[78,70,68,60,58,50,48,40] */
3738c2ecf20Sopenharmony_ci	struct dram_range ranges[DRAM_RANGES];
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	u64 top_mem;		/* top of memory below 4GB */
3768c2ecf20Sopenharmony_ci	u64 top_mem2;		/* top of memory above 4GB */
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	u32 dct_sel_lo;		/* DRAM Controller Select Low */
3798c2ecf20Sopenharmony_ci	u32 dct_sel_hi;		/* DRAM Controller Select High */
3808c2ecf20Sopenharmony_ci	u32 online_spare;	/* On-Line spare Reg */
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* x4, x8, or x16 syndromes in use */
3838c2ecf20Sopenharmony_ci	u8 ecc_sym_sz;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/* place to store error injection parameters prior to issue */
3868c2ecf20Sopenharmony_ci	struct error_injection injection;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* cache the dram_type */
3898c2ecf20Sopenharmony_ci	enum mem_type dram_type;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	struct amd64_umc *umc;	/* UMC registers */
3928c2ecf20Sopenharmony_ci};
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cienum err_codes {
3958c2ecf20Sopenharmony_ci	DECODE_OK	=  0,
3968c2ecf20Sopenharmony_ci	ERR_NODE	= -1,
3978c2ecf20Sopenharmony_ci	ERR_CSROW	= -2,
3988c2ecf20Sopenharmony_ci	ERR_CHANNEL	= -3,
3998c2ecf20Sopenharmony_ci	ERR_SYND	= -4,
4008c2ecf20Sopenharmony_ci	ERR_NORM_ADDR	= -5,
4018c2ecf20Sopenharmony_ci};
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistruct err_info {
4048c2ecf20Sopenharmony_ci	int err_code;
4058c2ecf20Sopenharmony_ci	struct mem_ctl_info *src_mci;
4068c2ecf20Sopenharmony_ci	int csrow;
4078c2ecf20Sopenharmony_ci	int channel;
4088c2ecf20Sopenharmony_ci	u16 syndrome;
4098c2ecf20Sopenharmony_ci	u32 page;
4108c2ecf20Sopenharmony_ci	u32 offset;
4118c2ecf20Sopenharmony_ci};
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic inline u32 get_umc_base(u8 channel)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	/* chY: 0xY50000 */
4168c2ecf20Sopenharmony_ci	return 0x50000 + (channel << 20);
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic inline u64 get_dram_base(struct amd64_pvt *pvt, u8 i)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	u64 addr = ((u64)pvt->ranges[i].base.lo & 0xffff0000) << 8;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (boot_cpu_data.x86 == 0xf)
4248c2ecf20Sopenharmony_ci		return addr;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return (((u64)pvt->ranges[i].base.hi & 0x000000ff) << 40) | addr;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic inline u64 get_dram_limit(struct amd64_pvt *pvt, u8 i)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	u64 lim = (((u64)pvt->ranges[i].lim.lo & 0xffff0000) << 8) | 0x00ffffff;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (boot_cpu_data.x86 == 0xf)
4348c2ecf20Sopenharmony_ci		return lim;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	return (((u64)pvt->ranges[i].lim.hi & 0x000000ff) << 40) | lim;
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cistatic inline u16 extract_syndrome(u64 status)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	return ((status >> 47) & 0xff) | ((status >> 16) & 0xff00);
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic inline u8 dct_sel_interleave_addr(struct amd64_pvt *pvt)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	if (pvt->fam == 0x15 && pvt->model >= 0x30)
4478c2ecf20Sopenharmony_ci		return (((pvt->dct_sel_hi >> 9) & 0x1) << 2) |
4488c2ecf20Sopenharmony_ci			((pvt->dct_sel_lo >> 6) & 0x3);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	return	((pvt)->dct_sel_lo >> 6) & 0x3;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci/*
4538c2ecf20Sopenharmony_ci * per-node ECC settings descriptor
4548c2ecf20Sopenharmony_ci */
4558c2ecf20Sopenharmony_cistruct ecc_settings {
4568c2ecf20Sopenharmony_ci	u32 old_nbctl;
4578c2ecf20Sopenharmony_ci	bool nbctl_valid;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	struct flags {
4608c2ecf20Sopenharmony_ci		unsigned long nb_mce_enable:1;
4618c2ecf20Sopenharmony_ci		unsigned long nb_ecc_prev:1;
4628c2ecf20Sopenharmony_ci	} flags;
4638c2ecf20Sopenharmony_ci};
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci#ifdef CONFIG_EDAC_DEBUG
4668c2ecf20Sopenharmony_ciextern const struct attribute_group amd64_edac_dbg_group;
4678c2ecf20Sopenharmony_ci#endif
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci#ifdef CONFIG_EDAC_AMD64_ERROR_INJECTION
4708c2ecf20Sopenharmony_ciextern const struct attribute_group amd64_edac_inj_group;
4718c2ecf20Sopenharmony_ci#endif
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci/*
4748c2ecf20Sopenharmony_ci * Each of the PCI Device IDs types have their own set of hardware accessor
4758c2ecf20Sopenharmony_ci * functions and per device encoding/decoding logic.
4768c2ecf20Sopenharmony_ci */
4778c2ecf20Sopenharmony_cistruct low_ops {
4788c2ecf20Sopenharmony_ci	int (*early_channel_count)	(struct amd64_pvt *pvt);
4798c2ecf20Sopenharmony_ci	void (*map_sysaddr_to_csrow)	(struct mem_ctl_info *mci, u64 sys_addr,
4808c2ecf20Sopenharmony_ci					 struct err_info *);
4818c2ecf20Sopenharmony_ci	int (*dbam_to_cs)		(struct amd64_pvt *pvt, u8 dct,
4828c2ecf20Sopenharmony_ci					 unsigned cs_mode, int cs_mask_nr);
4838c2ecf20Sopenharmony_ci};
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistruct amd64_family_type {
4868c2ecf20Sopenharmony_ci	const char *ctl_name;
4878c2ecf20Sopenharmony_ci	u16 f0_id, f1_id, f2_id, f6_id;
4888c2ecf20Sopenharmony_ci	/* Maximum number of memory controllers per die/node. */
4898c2ecf20Sopenharmony_ci	u8 max_mcs;
4908c2ecf20Sopenharmony_ci	struct low_ops ops;
4918c2ecf20Sopenharmony_ci};
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ciint __amd64_read_pci_cfg_dword(struct pci_dev *pdev, int offset,
4948c2ecf20Sopenharmony_ci			       u32 *val, const char *func);
4958c2ecf20Sopenharmony_ciint __amd64_write_pci_cfg_dword(struct pci_dev *pdev, int offset,
4968c2ecf20Sopenharmony_ci				u32 val, const char *func);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci#define amd64_read_pci_cfg(pdev, offset, val)	\
4998c2ecf20Sopenharmony_ci	__amd64_read_pci_cfg_dword(pdev, offset, val, __func__)
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci#define amd64_write_pci_cfg(pdev, offset, val)	\
5028c2ecf20Sopenharmony_ci	__amd64_write_pci_cfg_dword(pdev, offset, val, __func__)
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ciint amd64_get_dram_hole_info(struct mem_ctl_info *mci, u64 *hole_base,
5058c2ecf20Sopenharmony_ci			     u64 *hole_offset, u64 *hole_size);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci#define to_mci(k) container_of(k, struct mem_ctl_info, dev)
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci/* Injection helpers */
5108c2ecf20Sopenharmony_cistatic inline void disable_caches(void *dummy)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	write_cr0(read_cr0() | X86_CR0_CD);
5138c2ecf20Sopenharmony_ci	wbinvd();
5148c2ecf20Sopenharmony_ci}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic inline void enable_caches(void *dummy)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	write_cr0(read_cr0() & ~X86_CR0_CD);
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic inline u8 dram_intlv_en(struct amd64_pvt *pvt, unsigned int i)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	if (pvt->fam == 0x15 && pvt->model >= 0x30) {
5248c2ecf20Sopenharmony_ci		u32 tmp;
5258c2ecf20Sopenharmony_ci		amd64_read_pci_cfg(pvt->F1, DRAM_CONT_LIMIT, &tmp);
5268c2ecf20Sopenharmony_ci		return (u8) tmp & 0xF;
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci	return (u8) (pvt->ranges[i].base.lo >> 8) & 0x7;
5298c2ecf20Sopenharmony_ci}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic inline u8 dhar_valid(struct amd64_pvt *pvt)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	if (pvt->fam == 0x15 && pvt->model >= 0x30) {
5348c2ecf20Sopenharmony_ci		u32 tmp;
5358c2ecf20Sopenharmony_ci		amd64_read_pci_cfg(pvt->F1, DRAM_CONT_BASE, &tmp);
5368c2ecf20Sopenharmony_ci		return (tmp >> 1) & BIT(0);
5378c2ecf20Sopenharmony_ci	}
5388c2ecf20Sopenharmony_ci	return (pvt)->dhar & BIT(0);
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic inline u32 dct_sel_baseaddr(struct amd64_pvt *pvt)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	if (pvt->fam == 0x15 && pvt->model >= 0x30) {
5448c2ecf20Sopenharmony_ci		u32 tmp;
5458c2ecf20Sopenharmony_ci		amd64_read_pci_cfg(pvt->F1, DRAM_CONT_BASE, &tmp);
5468c2ecf20Sopenharmony_ci		return (tmp >> 11) & 0x1FFF;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci	return (pvt)->dct_sel_lo & 0xFFFFF800;
5498c2ecf20Sopenharmony_ci}
550