/*1* AMD64 class Memory Controller kernel module2*3* Copyright (c) 2009 SoftwareBitMaker.4* Copyright (c) 2009 Advanced Micro Devices, Inc.5*6* This file may be distributed under the terms of the7* GNU General Public License.8*9* Originally Written by Thayne Harbaugh10*11* Changes by Douglas "norsk" Thompson <[email protected]>:12* - K8 CPU Revision D and greater support13*14* Changes by Dave Peterson <[email protected]> <[email protected]>:15* - Module largely rewritten, with new (and hopefully correct)16* code for dealing with node and chip select interleaving,17* various code cleanup, and bug fixes18* - Added support for memory hoisting using DRAM hole address19* register20*21* Changes by Douglas "norsk" Thompson <[email protected]>:22* -K8 Rev (1207) revision support added, required Revision23* specific mini-driver code to support Rev F as well as24* prior revisions25*26* Changes by Douglas "norsk" Thompson <[email protected]>:27* -Family 10h revision support added. New PCI Device IDs,28* indicating new changes. Actual registers modified29* were slight, less than the Rev E to Rev F transition30* but changing the PCI Device ID was the proper thing to31* do, as it provides for almost automactic family32* detection. The mods to Rev F required more family33* information detection.34*35* Changes/Fixes by Borislav Petkov <[email protected]>:36* - misc fixes and code cleanups37*38* This module is based on the following documents39* (available from http://www.amd.com/):40*41* Title: BIOS and Kernel Developer's Guide for AMD Athlon 64 and AMD42* Opteron Processors43* AMD publication #: 2609444*` Revision: 3.2645*46* Title: BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh47* Processors48* AMD publication #: 3255949* Revision: 3.0050* Issue Date: May 200651*52* Title: BIOS and Kernel Developer's Guide (BKDG) For AMD Family 10h53* Processors54* AMD publication #: 3111655* Revision: 3.0056* Issue Date: September 07, 200757*58* Sections in the first 2 documents are no longer in sync with each other.59* The Family 10h BKDG was totally re-written from scratch with a new60* presentation model.61* Therefore, comments that refer to a Document section might be off.62*/6364#include <linux/module.h>65#include <linux/ctype.h>66#include <linux/init.h>67#include <linux/pci.h>68#include <linux/pci_ids.h>69#include <linux/slab.h>70#include <linux/mmzone.h>71#include <linux/edac.h>72#include <asm/msr.h>73#include "edac_core.h"74#include "mce_amd.h"7576#define amd64_debug(fmt, arg...) \77edac_printk(KERN_DEBUG, "amd64", fmt, ##arg)7879#define amd64_info(fmt, arg...) \80edac_printk(KERN_INFO, "amd64", fmt, ##arg)8182#define amd64_notice(fmt, arg...) \83edac_printk(KERN_NOTICE, "amd64", fmt, ##arg)8485#define amd64_warn(fmt, arg...) \86edac_printk(KERN_WARNING, "amd64", fmt, ##arg)8788#define amd64_err(fmt, arg...) \89edac_printk(KERN_ERR, "amd64", fmt, ##arg)9091#define amd64_mc_warn(mci, fmt, arg...) \92edac_mc_chipset_printk(mci, KERN_WARNING, "amd64", fmt, ##arg)9394#define amd64_mc_err(mci, fmt, arg...) \95edac_mc_chipset_printk(mci, KERN_ERR, "amd64", fmt, ##arg)9697/*98* Throughout the comments in this code, the following terms are used:99*100* SysAddr, DramAddr, and InputAddr101*102* These terms come directly from the amd64 documentation103* (AMD publication #26094). They are defined as follows:104*105* SysAddr:106* This is a physical address generated by a CPU core or a device107* doing DMA. If generated by a CPU core, a SysAddr is the result of108* a virtual to physical address translation by the CPU core's address109* translation mechanism (MMU).110*111* DramAddr:112* A DramAddr is derived from a SysAddr by subtracting an offset that113* depends on which node the SysAddr maps to and whether the SysAddr114* is within a range affected by memory hoisting. The DRAM Base115* (section 3.4.4.1) and DRAM Limit (section 3.4.4.2) registers116* determine which node a SysAddr maps to.117*118* If the DRAM Hole Address Register (DHAR) is enabled and the SysAddr119* is within the range of addresses specified by this register, then120* a value x from the DHAR is subtracted from the SysAddr to produce a121* DramAddr. Here, x represents the base address for the node that122* the SysAddr maps to plus an offset due to memory hoisting. See123* section 3.4.8 and the comments in amd64_get_dram_hole_info() and124* sys_addr_to_dram_addr() below for more information.125*126* If the SysAddr is not affected by the DHAR then a value y is127* subtracted from the SysAddr to produce a DramAddr. Here, y is the128* base address for the node that the SysAddr maps to. See section129* 3.4.4 and the comments in sys_addr_to_dram_addr() below for more130* information.131*132* InputAddr:133* A DramAddr is translated to an InputAddr before being passed to the134* memory controller for the node that the DramAddr is associated135* with. The memory controller then maps the InputAddr to a csrow.136* If node interleaving is not in use, then the InputAddr has the same137* value as the DramAddr. Otherwise, the InputAddr is produced by138* discarding the bits used for node interleaving from the DramAddr.139* See section 3.4.4 for more information.140*141* The memory controller for a given node uses its DRAM CS Base and142* DRAM CS Mask registers to map an InputAddr to a csrow. See143* sections 3.5.4 and 3.5.5 for more information.144*/145146#define EDAC_AMD64_VERSION "3.4.0"147#define EDAC_MOD_STR "amd64_edac"148149/* Extended Model from CPUID, for CPU Revision numbers */150#define K8_REV_D 1151#define K8_REV_E 2152#define K8_REV_F 4153154/* Hardware limit on ChipSelect rows per MC and processors per system */155#define NUM_CHIPSELECTS 8156#define DRAM_RANGES 8157158#define ON true159#define OFF false160161/*162* Create a contiguous bitmask starting at bit position @lo and ending at163* position @hi. For example164*165* GENMASK(21, 39) gives us the 64bit vector 0x000000ffffe00000.166*/167#define GENMASK(lo, hi) (((1ULL << ((hi) - (lo) + 1)) - 1) << (lo))168169/*170* PCI-defined configuration space registers171*/172#define PCI_DEVICE_ID_AMD_15H_NB_F1 0x1601173#define PCI_DEVICE_ID_AMD_15H_NB_F2 0x1602174175176/*177* Function 1 - Address Map178*/179#define DRAM_BASE_LO 0x40180#define DRAM_LIMIT_LO 0x44181182#define dram_intlv_en(pvt, i) ((u8)((pvt->ranges[i].base.lo >> 8) & 0x7))183#define dram_rw(pvt, i) ((u8)(pvt->ranges[i].base.lo & 0x3))184#define dram_intlv_sel(pvt, i) ((u8)((pvt->ranges[i].lim.lo >> 8) & 0x7))185#define dram_dst_node(pvt, i) ((u8)(pvt->ranges[i].lim.lo & 0x7))186187#define DHAR 0xf0188#define dhar_valid(pvt) ((pvt)->dhar & BIT(0))189#define dhar_mem_hoist_valid(pvt) ((pvt)->dhar & BIT(1))190#define dhar_base(pvt) ((pvt)->dhar & 0xff000000)191#define k8_dhar_offset(pvt) (((pvt)->dhar & 0x0000ff00) << 16)192193/* NOTE: Extra mask bit vs K8 */194#define f10_dhar_offset(pvt) (((pvt)->dhar & 0x0000ff80) << 16)195196#define DCT_CFG_SEL 0x10C197198#define DRAM_LOCAL_NODE_BASE 0x120199#define DRAM_LOCAL_NODE_LIM 0x124200201#define DRAM_BASE_HI 0x140202#define DRAM_LIMIT_HI 0x144203204205/*206* Function 2 - DRAM controller207*/208#define DCSB0 0x40209#define DCSB1 0x140210#define DCSB_CS_ENABLE BIT(0)211212#define DCSM0 0x60213#define DCSM1 0x160214215#define csrow_enabled(i, dct, pvt) ((pvt)->csels[(dct)].csbases[(i)] & DCSB_CS_ENABLE)216217#define DBAM0 0x80218#define DBAM1 0x180219220/* Extract the DIMM 'type' on the i'th DIMM from the DBAM reg value passed */221#define DBAM_DIMM(i, reg) ((((reg) >> (4*i))) & 0xF)222223#define DBAM_MAX_VALUE 11224225#define DCLR0 0x90226#define DCLR1 0x190227#define REVE_WIDTH_128 BIT(16)228#define WIDTH_128 BIT(11)229230#define DCHR0 0x94231#define DCHR1 0x194232#define DDR3_MODE BIT(8)233234#define DCT_SEL_LO 0x110235#define dct_sel_baseaddr(pvt) ((pvt)->dct_sel_lo & 0xFFFFF800)236#define dct_sel_interleave_addr(pvt) (((pvt)->dct_sel_lo >> 6) & 0x3)237#define dct_high_range_enabled(pvt) ((pvt)->dct_sel_lo & BIT(0))238#define dct_interleave_enabled(pvt) ((pvt)->dct_sel_lo & BIT(2))239240#define dct_ganging_enabled(pvt) ((boot_cpu_data.x86 == 0x10) && ((pvt)->dct_sel_lo & BIT(4)))241242#define dct_data_intlv_enabled(pvt) ((pvt)->dct_sel_lo & BIT(5))243#define dct_memory_cleared(pvt) ((pvt)->dct_sel_lo & BIT(10))244245#define SWAP_INTLV_REG 0x10c246247#define DCT_SEL_HI 0x114248249/*250* Function 3 - Misc Control251*/252#define NBCTL 0x40253254#define NBCFG 0x44255#define NBCFG_CHIPKILL BIT(23)256#define NBCFG_ECC_ENABLE BIT(22)257258/* F3x48: NBSL */259#define F10_NBSL_EXT_ERR_ECC 0x8260#define NBSL_PP_OBS 0x2261262#define SCRCTRL 0x58263264#define F10_ONLINE_SPARE 0xB0265#define online_spare_swap_done(pvt, c) (((pvt)->online_spare >> (1 + 2 * (c))) & 0x1)266#define online_spare_bad_dramcs(pvt, c) (((pvt)->online_spare >> (4 + 4 * (c))) & 0x7)267268#define F10_NB_ARRAY_ADDR 0xB8269#define F10_NB_ARRAY_DRAM_ECC BIT(31)270271/* Bits [2:1] are used to select 16-byte section within a 64-byte cacheline */272#define SET_NB_ARRAY_ADDRESS(section) (((section) & 0x3) << 1)273274#define F10_NB_ARRAY_DATA 0xBC275#define SET_NB_DRAM_INJECTION_WRITE(word, bits) \276(BIT(((word) & 0xF) + 20) | \277BIT(17) | bits)278#define SET_NB_DRAM_INJECTION_READ(word, bits) \279(BIT(((word) & 0xF) + 20) | \280BIT(16) | bits)281282#define NBCAP 0xE8283#define NBCAP_CHIPKILL BIT(4)284#define NBCAP_SECDED BIT(3)285#define NBCAP_DCT_DUAL BIT(0)286287#define EXT_NB_MCA_CFG 0x180288289/* MSRs */290#define MSR_MCGCTL_NBE BIT(4)291292/* AMD sets the first MC device at device ID 0x18. */293static inline u8 get_node_id(struct pci_dev *pdev)294{295return PCI_SLOT(pdev->devfn) - 0x18;296}297298enum amd_families {299K8_CPUS = 0,300F10_CPUS,301F15_CPUS,302NUM_FAMILIES,303};304305/* Error injection control structure */306struct error_injection {307u32 section;308u32 word;309u32 bit_map;310};311312/* low and high part of PCI config space regs */313struct reg_pair {314u32 lo, hi;315};316317/*318* See F1x[1, 0][7C:40] DRAM Base/Limit Registers319*/320struct dram_range {321struct reg_pair base;322struct reg_pair lim;323};324325/* A DCT chip selects collection */326struct chip_select {327u32 csbases[NUM_CHIPSELECTS];328u8 b_cnt;329330u32 csmasks[NUM_CHIPSELECTS];331u8 m_cnt;332};333334struct amd64_pvt {335struct low_ops *ops;336337/* pci_device handles which we utilize */338struct pci_dev *F1, *F2, *F3;339340unsigned mc_node_id; /* MC index of this MC node */341int ext_model; /* extended model value of this node */342int channel_count;343344/* Raw registers */345u32 dclr0; /* DRAM Configuration Low DCT0 reg */346u32 dclr1; /* DRAM Configuration Low DCT1 reg */347u32 dchr0; /* DRAM Configuration High DCT0 reg */348u32 dchr1; /* DRAM Configuration High DCT1 reg */349u32 nbcap; /* North Bridge Capabilities */350u32 nbcfg; /* F10 North Bridge Configuration */351u32 ext_nbcfg; /* Extended F10 North Bridge Configuration */352u32 dhar; /* DRAM Hoist reg */353u32 dbam0; /* DRAM Base Address Mapping reg for DCT0 */354u32 dbam1; /* DRAM Base Address Mapping reg for DCT1 */355356/* one for each DCT */357struct chip_select csels[2];358359/* DRAM base and limit pairs F1x[78,70,68,60,58,50,48,40] */360struct dram_range ranges[DRAM_RANGES];361362u64 top_mem; /* top of memory below 4GB */363u64 top_mem2; /* top of memory above 4GB */364365u32 dct_sel_lo; /* DRAM Controller Select Low */366u32 dct_sel_hi; /* DRAM Controller Select High */367u32 online_spare; /* On-Line spare Reg */368369/* x4 or x8 syndromes in use */370u8 ecc_sym_sz;371372/* place to store error injection parameters prior to issue */373struct error_injection injection;374};375376static inline u64 get_dram_base(struct amd64_pvt *pvt, unsigned i)377{378u64 addr = ((u64)pvt->ranges[i].base.lo & 0xffff0000) << 8;379380if (boot_cpu_data.x86 == 0xf)381return addr;382383return (((u64)pvt->ranges[i].base.hi & 0x000000ff) << 40) | addr;384}385386static inline u64 get_dram_limit(struct amd64_pvt *pvt, unsigned i)387{388u64 lim = (((u64)pvt->ranges[i].lim.lo & 0xffff0000) << 8) | 0x00ffffff;389390if (boot_cpu_data.x86 == 0xf)391return lim;392393return (((u64)pvt->ranges[i].lim.hi & 0x000000ff) << 40) | lim;394}395396static inline u16 extract_syndrome(u64 status)397{398return ((status >> 47) & 0xff) | ((status >> 16) & 0xff00);399}400401/*402* per-node ECC settings descriptor403*/404struct ecc_settings {405u32 old_nbctl;406bool nbctl_valid;407408struct flags {409unsigned long nb_mce_enable:1;410unsigned long nb_ecc_prev:1;411} flags;412};413414#ifdef CONFIG_EDAC_DEBUG415#define NUM_DBG_ATTRS 5416#else417#define NUM_DBG_ATTRS 0418#endif419420#ifdef CONFIG_EDAC_AMD64_ERROR_INJECTION421#define NUM_INJ_ATTRS 5422#else423#define NUM_INJ_ATTRS 0424#endif425426extern struct mcidev_sysfs_attribute amd64_dbg_attrs[NUM_DBG_ATTRS],427amd64_inj_attrs[NUM_INJ_ATTRS];428429/*430* Each of the PCI Device IDs types have their own set of hardware accessor431* functions and per device encoding/decoding logic.432*/433struct low_ops {434int (*early_channel_count) (struct amd64_pvt *pvt);435void (*map_sysaddr_to_csrow) (struct mem_ctl_info *mci, u64 sys_addr,436u16 syndrome);437int (*dbam_to_cs) (struct amd64_pvt *pvt, u8 dct, unsigned cs_mode);438int (*read_dct_pci_cfg) (struct amd64_pvt *pvt, int offset,439u32 *val, const char *func);440};441442struct amd64_family_type {443const char *ctl_name;444u16 f1_id, f3_id;445struct low_ops ops;446};447448int __amd64_write_pci_cfg_dword(struct pci_dev *pdev, int offset,449u32 val, const char *func);450451#define amd64_read_pci_cfg(pdev, offset, val) \452__amd64_read_pci_cfg_dword(pdev, offset, val, __func__)453454#define amd64_write_pci_cfg(pdev, offset, val) \455__amd64_write_pci_cfg_dword(pdev, offset, val, __func__)456457#define amd64_read_dct_pci_cfg(pvt, offset, val) \458pvt->ops->read_dct_pci_cfg(pvt, offset, val, __func__)459460int amd64_get_dram_hole_info(struct mem_ctl_info *mci, u64 *hole_base,461u64 *hole_offset, u64 *hole_size);462463464