Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Driver for Intel(R) 10nm server memory controller. |
| 4 | * Copyright (c) 2019, Intel Corporation. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <asm/cpu_device_id.h> |
| 10 | #include <asm/intel-family.h> |
| 11 | #include <asm/mce.h> |
| 12 | #include "edac_module.h" |
| 13 | #include "skx_common.h" |
| 14 | |
| 15 | #define I10NM_REVISION "v0.0.3" |
| 16 | #define EDAC_MOD_STR "i10nm_edac" |
| 17 | |
| 18 | /* Debug macros */ |
| 19 | #define i10nm_printk(level, fmt, arg...) \ |
| 20 | edac_printk(level, "i10nm", fmt, ##arg) |
| 21 | |
| 22 | #define I10NM_GET_SCK_BAR(d, reg) \ |
| 23 | pci_read_config_dword((d)->uracu, 0xd0, &(reg)) |
| 24 | #define I10NM_GET_IMC_BAR(d, i, reg) \ |
| 25 | pci_read_config_dword((d)->uracu, 0xd8 + (i) * 4, &(reg)) |
| 26 | #define I10NM_GET_DIMMMTR(m, i, j) \ |
| 27 | (*(u32 *)((m)->mbase + 0x2080c + (i) * 0x4000 + (j) * 4)) |
| 28 | #define I10NM_GET_MCDDRTCFG(m, i, j) \ |
| 29 | (*(u32 *)((m)->mbase + 0x20970 + (i) * 0x4000 + (j) * 4)) |
| 30 | |
| 31 | #define I10NM_GET_SCK_MMIO_BASE(reg) (GET_BITFIELD(reg, 0, 28) << 23) |
| 32 | #define I10NM_GET_IMC_MMIO_OFFSET(reg) (GET_BITFIELD(reg, 0, 10) << 12) |
| 33 | #define I10NM_GET_IMC_MMIO_SIZE(reg) ((GET_BITFIELD(reg, 13, 23) - \ |
| 34 | GET_BITFIELD(reg, 0, 10) + 1) << 12) |
| 35 | |
| 36 | static struct list_head *i10nm_edac_list; |
| 37 | |
| 38 | static struct pci_dev *pci_get_dev_wrapper(int dom, unsigned int bus, |
| 39 | unsigned int dev, unsigned int fun) |
| 40 | { |
| 41 | struct pci_dev *pdev; |
| 42 | |
| 43 | pdev = pci_get_domain_bus_and_slot(dom, bus, PCI_DEVFN(dev, fun)); |
| 44 | if (!pdev) { |
| 45 | edac_dbg(2, "No device %02x:%02x.%x\n", |
| 46 | bus, dev, fun); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | if (unlikely(pci_enable_device(pdev) < 0)) { |
| 51 | edac_dbg(2, "Failed to enable device %02x:%02x.%x\n", |
| 52 | bus, dev, fun); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | pci_dev_get(pdev); |
| 57 | |
| 58 | return pdev; |
| 59 | } |
| 60 | |
| 61 | static int i10nm_get_all_munits(void) |
| 62 | { |
| 63 | struct pci_dev *mdev; |
| 64 | void __iomem *mbase; |
| 65 | unsigned long size; |
| 66 | struct skx_dev *d; |
| 67 | int i, j = 0; |
| 68 | u32 reg, off; |
| 69 | u64 base; |
| 70 | |
| 71 | list_for_each_entry(d, i10nm_edac_list, list) { |
| 72 | d->util_all = pci_get_dev_wrapper(d->seg, d->bus[1], 29, 1); |
| 73 | if (!d->util_all) |
| 74 | return -ENODEV; |
| 75 | |
| 76 | d->uracu = pci_get_dev_wrapper(d->seg, d->bus[0], 0, 1); |
| 77 | if (!d->uracu) |
| 78 | return -ENODEV; |
| 79 | |
| 80 | if (I10NM_GET_SCK_BAR(d, reg)) { |
| 81 | i10nm_printk(KERN_ERR, "Failed to socket bar\n"); |
| 82 | return -ENODEV; |
| 83 | } |
| 84 | |
| 85 | base = I10NM_GET_SCK_MMIO_BASE(reg); |
| 86 | edac_dbg(2, "socket%d mmio base 0x%llx (reg 0x%x)\n", |
| 87 | j++, base, reg); |
| 88 | |
| 89 | for (i = 0; i < I10NM_NUM_IMC; i++) { |
| 90 | mdev = pci_get_dev_wrapper(d->seg, d->bus[0], |
| 91 | 12 + i, 0); |
| 92 | if (i == 0 && !mdev) { |
| 93 | i10nm_printk(KERN_ERR, "No IMC found\n"); |
| 94 | return -ENODEV; |
| 95 | } |
| 96 | if (!mdev) |
| 97 | continue; |
| 98 | |
| 99 | d->imc[i].mdev = mdev; |
| 100 | |
| 101 | if (I10NM_GET_IMC_BAR(d, i, reg)) { |
| 102 | i10nm_printk(KERN_ERR, "Failed to get mc bar\n"); |
| 103 | return -ENODEV; |
| 104 | } |
| 105 | |
| 106 | off = I10NM_GET_IMC_MMIO_OFFSET(reg); |
| 107 | size = I10NM_GET_IMC_MMIO_SIZE(reg); |
| 108 | edac_dbg(2, "mc%d mmio base 0x%llx size 0x%lx (reg 0x%x)\n", |
| 109 | i, base + off, size, reg); |
| 110 | |
| 111 | mbase = ioremap(base + off, size); |
| 112 | if (!mbase) { |
| 113 | i10nm_printk(KERN_ERR, "Failed to ioremap 0x%llx\n", |
| 114 | base + off); |
| 115 | return -ENODEV; |
| 116 | } |
| 117 | |
| 118 | d->imc[i].mbase = mbase; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static const struct x86_cpu_id i10nm_cpuids[] = { |
Peter Zijlstra | 5ebb34e | 2019-08-27 21:48:24 +0200 | [diff] [blame] | 126 | { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_TREMONT_D, 0, 0 }, |
Qiuxu Zhuo | 5c5d3ac | 2019-06-13 16:40:27 +0800 | [diff] [blame] | 127 | { X86_VENDOR_INTEL, 6, INTEL_FAM6_ICELAKE_X, 0, 0 }, |
Peter Zijlstra | 5ebb34e | 2019-08-27 21:48:24 +0200 | [diff] [blame] | 128 | { X86_VENDOR_INTEL, 6, INTEL_FAM6_ICELAKE_D, 0, 0 }, |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 129 | { } |
| 130 | }; |
| 131 | MODULE_DEVICE_TABLE(x86cpu, i10nm_cpuids); |
| 132 | |
| 133 | static bool i10nm_check_ecc(struct skx_imc *imc, int chan) |
| 134 | { |
| 135 | u32 mcmtr; |
| 136 | |
| 137 | mcmtr = *(u32 *)(imc->mbase + 0x20ef8 + chan * 0x4000); |
| 138 | edac_dbg(1, "ch%d mcmtr reg %x\n", chan, mcmtr); |
| 139 | |
| 140 | return !!GET_BITFIELD(mcmtr, 2, 2); |
| 141 | } |
| 142 | |
| 143 | static int i10nm_get_dimm_config(struct mem_ctl_info *mci) |
| 144 | { |
| 145 | struct skx_pvt *pvt = mci->pvt_info; |
| 146 | struct skx_imc *imc = pvt->imc; |
| 147 | struct dimm_info *dimm; |
| 148 | u32 mtr, mcddrtcfg; |
| 149 | int i, j, ndimms; |
| 150 | |
| 151 | for (i = 0; i < I10NM_NUM_CHANNELS; i++) { |
| 152 | if (!imc->mbase) |
| 153 | continue; |
| 154 | |
| 155 | ndimms = 0; |
| 156 | for (j = 0; j < I10NM_NUM_DIMMS; j++) { |
Robert Richter | bc9ad9e | 2019-11-06 09:33:02 +0000 | [diff] [blame] | 157 | dimm = edac_get_dimm(mci, i, j, 0); |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 158 | mtr = I10NM_GET_DIMMMTR(imc, i, j); |
| 159 | mcddrtcfg = I10NM_GET_MCDDRTCFG(imc, i, j); |
| 160 | edac_dbg(1, "dimmmtr 0x%x mcddrtcfg 0x%x (mc%d ch%d dimm%d)\n", |
| 161 | mtr, mcddrtcfg, imc->mc, i, j); |
| 162 | |
| 163 | if (IS_DIMM_PRESENT(mtr)) |
| 164 | ndimms += skx_get_dimm_info(mtr, 0, dimm, |
| 165 | imc, i, j); |
| 166 | else if (IS_NVDIMM_PRESENT(mcddrtcfg, j)) |
| 167 | ndimms += skx_get_nvdimm_info(dimm, imc, i, j, |
| 168 | EDAC_MOD_STR); |
| 169 | } |
Qiuxu Zhuo | c4a1dd9 | 2019-06-26 14:16:38 +0800 | [diff] [blame] | 170 | if (ndimms && !i10nm_check_ecc(imc, i)) { |
| 171 | i10nm_printk(KERN_ERR, "ECC is disabled on imc %d channel %d\n", |
| 172 | imc->mc, i); |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 173 | return -ENODEV; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static struct notifier_block i10nm_mce_dec = { |
| 181 | .notifier_call = skx_mce_check_error, |
| 182 | .priority = MCE_PRIO_EDAC, |
| 183 | }; |
| 184 | |
Qiuxu Zhuo | fe78351 | 2019-03-21 15:13:39 -0700 | [diff] [blame] | 185 | #ifdef CONFIG_EDAC_DEBUG |
| 186 | /* |
| 187 | * Debug feature. |
| 188 | * Exercise the address decode logic by writing an address to |
| 189 | * /sys/kernel/debug/edac/i10nm_test/addr. |
| 190 | */ |
| 191 | static struct dentry *i10nm_test; |
| 192 | |
| 193 | static int debugfs_u64_set(void *data, u64 val) |
| 194 | { |
| 195 | struct mce m; |
| 196 | |
| 197 | pr_warn_once("Fake error to 0x%llx injected via debugfs\n", val); |
| 198 | |
| 199 | memset(&m, 0, sizeof(m)); |
| 200 | /* ADDRV + MemRd + Unknown channel */ |
| 201 | m.status = MCI_STATUS_ADDRV + 0x90; |
| 202 | /* One corrected error */ |
| 203 | m.status |= BIT_ULL(MCI_STATUS_CEC_SHIFT); |
| 204 | m.addr = val; |
| 205 | skx_mce_check_error(NULL, 0, &m); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); |
| 210 | |
| 211 | static void setup_i10nm_debug(void) |
| 212 | { |
| 213 | i10nm_test = edac_debugfs_create_dir("i10nm_test"); |
| 214 | if (!i10nm_test) |
| 215 | return; |
| 216 | |
| 217 | if (!edac_debugfs_create_file("addr", 0200, i10nm_test, |
| 218 | NULL, &fops_u64_wo)) { |
| 219 | debugfs_remove(i10nm_test); |
| 220 | i10nm_test = NULL; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static void teardown_i10nm_debug(void) |
| 225 | { |
| 226 | debugfs_remove_recursive(i10nm_test); |
| 227 | } |
| 228 | #else |
| 229 | static inline void setup_i10nm_debug(void) {} |
| 230 | static inline void teardown_i10nm_debug(void) {} |
| 231 | #endif /*CONFIG_EDAC_DEBUG*/ |
| 232 | |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 233 | static int __init i10nm_init(void) |
| 234 | { |
| 235 | u8 mc = 0, src_id = 0, node_id = 0; |
| 236 | const struct x86_cpu_id *id; |
| 237 | const char *owner; |
| 238 | struct skx_dev *d; |
| 239 | int rc, i, off[3] = {0xd0, 0xc8, 0xcc}; |
| 240 | u64 tolm, tohm; |
| 241 | |
| 242 | edac_dbg(2, "\n"); |
| 243 | |
| 244 | owner = edac_get_owner(); |
| 245 | if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR))) |
| 246 | return -EBUSY; |
| 247 | |
| 248 | id = x86_match_cpu(i10nm_cpuids); |
| 249 | if (!id) |
| 250 | return -ENODEV; |
| 251 | |
| 252 | rc = skx_get_hi_lo(0x09a2, off, &tolm, &tohm); |
| 253 | if (rc) |
| 254 | return rc; |
| 255 | |
| 256 | rc = skx_get_all_bus_mappings(0x3452, 0xcc, I10NM, &i10nm_edac_list); |
| 257 | if (rc < 0) |
| 258 | goto fail; |
| 259 | if (rc == 0) { |
| 260 | i10nm_printk(KERN_ERR, "No memory controllers found\n"); |
| 261 | return -ENODEV; |
| 262 | } |
| 263 | |
| 264 | rc = i10nm_get_all_munits(); |
| 265 | if (rc < 0) |
| 266 | goto fail; |
| 267 | |
| 268 | list_for_each_entry(d, i10nm_edac_list, list) { |
Qiuxu Zhuo | 1dc78f1 | 2019-06-26 14:16:55 +0800 | [diff] [blame] | 269 | rc = skx_get_src_id(d, 0xf8, &src_id); |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 270 | if (rc < 0) |
| 271 | goto fail; |
| 272 | |
| 273 | rc = skx_get_node_id(d, &node_id); |
| 274 | if (rc < 0) |
| 275 | goto fail; |
| 276 | |
| 277 | edac_dbg(2, "src_id = %d node_id = %d\n", src_id, node_id); |
| 278 | for (i = 0; i < I10NM_NUM_IMC; i++) { |
| 279 | if (!d->imc[i].mdev) |
| 280 | continue; |
| 281 | |
| 282 | d->imc[i].mc = mc++; |
| 283 | d->imc[i].lmc = i; |
| 284 | d->imc[i].src_id = src_id; |
| 285 | d->imc[i].node_id = node_id; |
| 286 | |
| 287 | rc = skx_register_mci(&d->imc[i], d->imc[i].mdev, |
| 288 | "Intel_10nm Socket", EDAC_MOD_STR, |
| 289 | i10nm_get_dimm_config); |
| 290 | if (rc < 0) |
| 291 | goto fail; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | rc = skx_adxl_get(); |
| 296 | if (rc) |
| 297 | goto fail; |
| 298 | |
| 299 | opstate_init(); |
| 300 | mce_register_decode_chain(&i10nm_mce_dec); |
Qiuxu Zhuo | fe78351 | 2019-03-21 15:13:39 -0700 | [diff] [blame] | 301 | setup_i10nm_debug(); |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 302 | |
| 303 | i10nm_printk(KERN_INFO, "%s\n", I10NM_REVISION); |
| 304 | |
| 305 | return 0; |
| 306 | fail: |
| 307 | skx_remove(); |
| 308 | return rc; |
| 309 | } |
| 310 | |
| 311 | static void __exit i10nm_exit(void) |
| 312 | { |
| 313 | edac_dbg(2, "\n"); |
Qiuxu Zhuo | fe78351 | 2019-03-21 15:13:39 -0700 | [diff] [blame] | 314 | teardown_i10nm_debug(); |
Qiuxu Zhuo | d4dc89d | 2019-01-30 11:15:19 -0800 | [diff] [blame] | 315 | mce_unregister_decode_chain(&i10nm_mce_dec); |
| 316 | skx_adxl_put(); |
| 317 | skx_remove(); |
| 318 | } |
| 319 | |
| 320 | module_init(i10nm_init); |
| 321 | module_exit(i10nm_exit); |
| 322 | |
| 323 | MODULE_LICENSE("GPL v2"); |
| 324 | MODULE_DESCRIPTION("MC Driver for Intel 10nm server processors"); |