1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Intel(R) 10nm server memory controller.
4 * Copyright (c) 2019, Intel Corporation.
8 #include <linux/kernel.h>
10 #include <asm/cpu_device_id.h>
11 #include <asm/intel-family.h>
13 #include "edac_module.h"
14 #include "skx_common.h"
16 #define I10NM_REVISION "v0.0.5"
17 #define EDAC_MOD_STR "i10nm_edac"
20 #define i10nm_printk(level, fmt, arg...) \
21 edac_printk(level, "i10nm", fmt, ##arg)
23 #define I10NM_GET_SCK_BAR(d, reg) \
24 pci_read_config_dword((d)->uracu, 0xd0, &(reg))
25 #define I10NM_GET_IMC_BAR(d, i, reg) \
26 pci_read_config_dword((d)->uracu, 0xd8 + (i) * 4, &(reg))
27 #define I10NM_GET_SAD(d, offset, i, reg)\
28 pci_read_config_dword((d)->sad_all, (offset) + (i) * 8, &(reg))
29 #define I10NM_GET_HBM_IMC_BAR(d, reg) \
30 pci_read_config_dword((d)->uracu, 0xd4, &(reg))
31 #define I10NM_GET_CAPID3_CFG(d, reg) \
32 pci_read_config_dword((d)->pcu_cr3, 0x90, &(reg))
33 #define I10NM_GET_DIMMMTR(m, i, j) \
34 readl((m)->mbase + ((m)->hbm_mc ? 0x80c : 0x2080c) + \
35 (i) * (m)->chan_mmio_sz + (j) * 4)
36 #define I10NM_GET_MCDDRTCFG(m, i) \
37 readl((m)->mbase + ((m)->hbm_mc ? 0x970 : 0x20970) + \
38 (i) * (m)->chan_mmio_sz)
39 #define I10NM_GET_MCMTR(m, i) \
40 readl((m)->mbase + ((m)->hbm_mc ? 0xef8 : 0x20ef8) + \
41 (i) * (m)->chan_mmio_sz)
42 #define I10NM_GET_AMAP(m, i) \
43 readl((m)->mbase + ((m)->hbm_mc ? 0x814 : 0x20814) + \
44 (i) * (m)->chan_mmio_sz)
45 #define I10NM_GET_REG32(m, i, offset) \
46 readl((m)->mbase + (i) * (m)->chan_mmio_sz + (offset))
47 #define I10NM_GET_REG64(m, i, offset) \
48 readq((m)->mbase + (i) * (m)->chan_mmio_sz + (offset))
49 #define I10NM_SET_REG32(m, i, offset, v) \
50 writel(v, (m)->mbase + (i) * (m)->chan_mmio_sz + (offset))
52 #define I10NM_GET_SCK_MMIO_BASE(reg) (GET_BITFIELD(reg, 0, 28) << 23)
53 #define I10NM_GET_IMC_MMIO_OFFSET(reg) (GET_BITFIELD(reg, 0, 10) << 12)
54 #define I10NM_GET_IMC_MMIO_SIZE(reg) ((GET_BITFIELD(reg, 13, 23) - \
55 GET_BITFIELD(reg, 0, 10) + 1) << 12)
56 #define I10NM_GET_HBM_IMC_MMIO_OFFSET(reg) \
57 ((GET_BITFIELD(reg, 0, 10) << 12) + 0x140000)
59 #define I10NM_HBM_IMC_MMIO_SIZE 0x9000
60 #define I10NM_IS_HBM_PRESENT(reg) GET_BITFIELD(reg, 27, 30)
61 #define I10NM_IS_HBM_IMC(reg) GET_BITFIELD(reg, 29, 29)
63 #define I10NM_MAX_SAD 16
64 #define I10NM_SAD_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
65 #define I10NM_SAD_NM_CACHEABLE(reg) GET_BITFIELD(reg, 5, 5)
67 #define RETRY_RD_ERR_LOG_UC BIT(1)
68 #define RETRY_RD_ERR_LOG_NOOVER BIT(14)
69 #define RETRY_RD_ERR_LOG_EN BIT(15)
70 #define RETRY_RD_ERR_LOG_NOOVER_UC (BIT(14) | BIT(1))
71 #define RETRY_RD_ERR_LOG_OVER_UC_V (BIT(2) | BIT(1) | BIT(0))
73 static struct list_head *i10nm_edac_list;
75 static struct res_config *res_cfg;
76 static int retry_rd_err_log;
78 static u32 offsets_scrub_icx[] = {0x22c60, 0x22c54, 0x22c5c, 0x22c58, 0x22c28, 0x20ed8};
79 static u32 offsets_scrub_spr[] = {0x22c60, 0x22c54, 0x22f08, 0x22c58, 0x22c28, 0x20ed8};
80 static u32 offsets_demand_icx[] = {0x22e54, 0x22e60, 0x22e64, 0x22e58, 0x22e5c, 0x20ee0};
81 static u32 offsets_demand_spr[] = {0x22e54, 0x22e60, 0x22f10, 0x22e58, 0x22e5c, 0x20ee0};
83 static void __enable_retry_rd_err_log(struct skx_imc *imc, int chan, bool enable)
90 s = I10NM_GET_REG32(imc, chan, res_cfg->offsets_scrub[0]);
91 d = I10NM_GET_REG32(imc, chan, res_cfg->offsets_demand[0]);
94 /* Save default configurations */
95 imc->chan[chan].retry_rd_err_log_s = s;
96 imc->chan[chan].retry_rd_err_log_d = d;
98 s &= ~RETRY_RD_ERR_LOG_NOOVER_UC;
99 s |= RETRY_RD_ERR_LOG_EN;
100 d &= ~RETRY_RD_ERR_LOG_NOOVER_UC;
101 d |= RETRY_RD_ERR_LOG_EN;
103 /* Restore default configurations */
104 if (imc->chan[chan].retry_rd_err_log_s & RETRY_RD_ERR_LOG_UC)
105 s |= RETRY_RD_ERR_LOG_UC;
106 if (imc->chan[chan].retry_rd_err_log_s & RETRY_RD_ERR_LOG_NOOVER)
107 s |= RETRY_RD_ERR_LOG_NOOVER;
108 if (!(imc->chan[chan].retry_rd_err_log_s & RETRY_RD_ERR_LOG_EN))
109 s &= ~RETRY_RD_ERR_LOG_EN;
110 if (imc->chan[chan].retry_rd_err_log_d & RETRY_RD_ERR_LOG_UC)
111 d |= RETRY_RD_ERR_LOG_UC;
112 if (imc->chan[chan].retry_rd_err_log_d & RETRY_RD_ERR_LOG_NOOVER)
113 d |= RETRY_RD_ERR_LOG_NOOVER;
114 if (!(imc->chan[chan].retry_rd_err_log_d & RETRY_RD_ERR_LOG_EN))
115 d &= ~RETRY_RD_ERR_LOG_EN;
118 I10NM_SET_REG32(imc, chan, res_cfg->offsets_scrub[0], s);
119 I10NM_SET_REG32(imc, chan, res_cfg->offsets_demand[0], d);
122 static void enable_retry_rd_err_log(bool enable)
129 list_for_each_entry(d, i10nm_edac_list, list)
130 for (i = 0; i < I10NM_NUM_IMC; i++)
131 for (j = 0; j < I10NM_NUM_CHANNELS; j++)
132 __enable_retry_rd_err_log(&d->imc[i], j, enable);
135 static void show_retry_rd_err_log(struct decoded_addr *res, char *msg,
136 int len, bool scrub_err)
138 struct skx_imc *imc = &res->dev->imc[res->imc];
139 u32 log0, log1, log2, log3, log4;
140 u32 corr0, corr1, corr2, corr3;
148 offsets = scrub_err ? res_cfg->offsets_scrub : res_cfg->offsets_demand;
150 log0 = I10NM_GET_REG32(imc, res->channel, offsets[0]);
151 log1 = I10NM_GET_REG32(imc, res->channel, offsets[1]);
152 log3 = I10NM_GET_REG32(imc, res->channel, offsets[3]);
153 log4 = I10NM_GET_REG32(imc, res->channel, offsets[4]);
154 log5 = I10NM_GET_REG64(imc, res->channel, offsets[5]);
156 if (res_cfg->type == SPR) {
157 log2a = I10NM_GET_REG64(imc, res->channel, offsets[2]);
158 n = snprintf(msg, len, " retry_rd_err_log[%.8x %.8x %.16llx %.8x %.8x %.16llx]",
159 log0, log1, log2a, log3, log4, log5);
161 log2 = I10NM_GET_REG32(imc, res->channel, offsets[2]);
162 n = snprintf(msg, len, " retry_rd_err_log[%.8x %.8x %.8x %.8x %.8x %.16llx]",
163 log0, log1, log2, log3, log4, log5);
166 corr0 = I10NM_GET_REG32(imc, res->channel, 0x22c18);
167 corr1 = I10NM_GET_REG32(imc, res->channel, 0x22c1c);
168 corr2 = I10NM_GET_REG32(imc, res->channel, 0x22c20);
169 corr3 = I10NM_GET_REG32(imc, res->channel, 0x22c24);
172 snprintf(msg + n, len - n,
173 " correrrcnt[%.4x %.4x %.4x %.4x %.4x %.4x %.4x %.4x]",
174 corr0 & 0xffff, corr0 >> 16,
175 corr1 & 0xffff, corr1 >> 16,
176 corr2 & 0xffff, corr2 >> 16,
177 corr3 & 0xffff, corr3 >> 16);
179 /* Clear status bits */
180 if (retry_rd_err_log == 2 && (log0 & RETRY_RD_ERR_LOG_OVER_UC_V)) {
181 log0 &= ~RETRY_RD_ERR_LOG_OVER_UC_V;
182 I10NM_SET_REG32(imc, res->channel, offsets[0], log0);
186 static struct pci_dev *pci_get_dev_wrapper(int dom, unsigned int bus,
187 unsigned int dev, unsigned int fun)
189 struct pci_dev *pdev;
191 pdev = pci_get_domain_bus_and_slot(dom, bus, PCI_DEVFN(dev, fun));
193 edac_dbg(2, "No device %02x:%02x.%x\n",
198 if (unlikely(pci_enable_device(pdev) < 0)) {
199 edac_dbg(2, "Failed to enable device %02x:%02x.%x\n",
209 static bool i10nm_check_2lm(struct res_config *cfg)
215 list_for_each_entry(d, i10nm_edac_list, list) {
216 d->sad_all = pci_get_dev_wrapper(d->seg, d->bus[1],
217 PCI_SLOT(cfg->sad_all_devfn),
218 PCI_FUNC(cfg->sad_all_devfn));
222 for (i = 0; i < I10NM_MAX_SAD; i++) {
223 I10NM_GET_SAD(d, cfg->sad_all_offset, i, reg);
224 if (I10NM_SAD_ENABLE(reg) && I10NM_SAD_NM_CACHEABLE(reg)) {
225 edac_dbg(2, "2-level memory configuration.\n");
234 static int i10nm_get_ddr_munits(void)
236 struct pci_dev *mdev;
244 list_for_each_entry(d, i10nm_edac_list, list) {
245 d->util_all = pci_get_dev_wrapper(d->seg, d->bus[1], 29, 1);
249 d->uracu = pci_get_dev_wrapper(d->seg, d->bus[0], 0, 1);
253 if (I10NM_GET_SCK_BAR(d, reg)) {
254 i10nm_printk(KERN_ERR, "Failed to socket bar\n");
258 base = I10NM_GET_SCK_MMIO_BASE(reg);
259 edac_dbg(2, "socket%d mmio base 0x%llx (reg 0x%x)\n",
262 for (i = 0; i < I10NM_NUM_DDR_IMC; i++) {
263 mdev = pci_get_dev_wrapper(d->seg, d->bus[0],
265 if (i == 0 && !mdev) {
266 i10nm_printk(KERN_ERR, "No IMC found\n");
272 d->imc[i].mdev = mdev;
274 if (I10NM_GET_IMC_BAR(d, i, reg)) {
275 i10nm_printk(KERN_ERR, "Failed to get mc bar\n");
279 off = I10NM_GET_IMC_MMIO_OFFSET(reg);
280 size = I10NM_GET_IMC_MMIO_SIZE(reg);
281 edac_dbg(2, "mc%d mmio base 0x%llx size 0x%lx (reg 0x%x)\n",
282 i, base + off, size, reg);
284 mbase = ioremap(base + off, size);
286 i10nm_printk(KERN_ERR, "Failed to ioremap 0x%llx\n",
291 d->imc[i].mbase = mbase;
298 static bool i10nm_check_hbm_imc(struct skx_dev *d)
302 if (I10NM_GET_CAPID3_CFG(d, reg)) {
303 i10nm_printk(KERN_ERR, "Failed to get capid3_cfg\n");
307 return I10NM_IS_HBM_PRESENT(reg) != 0;
310 static int i10nm_get_hbm_munits(void)
312 struct pci_dev *mdev;
319 list_for_each_entry(d, i10nm_edac_list, list) {
320 d->pcu_cr3 = pci_get_dev_wrapper(d->seg, d->bus[1], 30, 3);
324 if (!i10nm_check_hbm_imc(d)) {
325 i10nm_printk(KERN_DEBUG, "No hbm memory\n");
329 if (I10NM_GET_SCK_BAR(d, reg)) {
330 i10nm_printk(KERN_ERR, "Failed to get socket bar\n");
333 base = I10NM_GET_SCK_MMIO_BASE(reg);
335 if (I10NM_GET_HBM_IMC_BAR(d, reg)) {
336 i10nm_printk(KERN_ERR, "Failed to get hbm mc bar\n");
339 base += I10NM_GET_HBM_IMC_MMIO_OFFSET(reg);
341 lmc = I10NM_NUM_DDR_IMC;
343 for (i = 0; i < I10NM_NUM_HBM_IMC; i++) {
344 mdev = pci_get_dev_wrapper(d->seg, d->bus[0],
345 12 + i / 4, 1 + i % 4);
346 if (i == 0 && !mdev) {
347 i10nm_printk(KERN_ERR, "No hbm mc found\n");
353 d->imc[lmc].mdev = mdev;
354 off = i * I10NM_HBM_IMC_MMIO_SIZE;
356 edac_dbg(2, "hbm mc%d mmio base 0x%llx size 0x%x\n",
357 lmc, base + off, I10NM_HBM_IMC_MMIO_SIZE);
359 mbase = ioremap(base + off, I10NM_HBM_IMC_MMIO_SIZE);
361 pci_dev_put(d->imc[lmc].mdev);
362 d->imc[lmc].mdev = NULL;
364 i10nm_printk(KERN_ERR, "Failed to ioremap for hbm mc 0x%llx\n",
369 d->imc[lmc].mbase = mbase;
370 d->imc[lmc].hbm_mc = true;
372 mcmtr = I10NM_GET_MCMTR(&d->imc[lmc], 0);
373 if (!I10NM_IS_HBM_IMC(mcmtr)) {
374 iounmap(d->imc[lmc].mbase);
375 d->imc[lmc].mbase = NULL;
376 d->imc[lmc].hbm_mc = false;
377 pci_dev_put(d->imc[lmc].mdev);
378 d->imc[lmc].mdev = NULL;
380 i10nm_printk(KERN_ERR, "This isn't an hbm mc!\n");
391 static struct res_config i10nm_cfg0 = {
394 .busno_cfg_offset = 0xcc,
395 .ddr_chan_mmio_sz = 0x4000,
396 .sad_all_devfn = PCI_DEVFN(29, 0),
397 .sad_all_offset = 0x108,
398 .offsets_scrub = offsets_scrub_icx,
399 .offsets_demand = offsets_demand_icx,
402 static struct res_config i10nm_cfg1 = {
405 .busno_cfg_offset = 0xd0,
406 .ddr_chan_mmio_sz = 0x4000,
407 .sad_all_devfn = PCI_DEVFN(29, 0),
408 .sad_all_offset = 0x108,
409 .offsets_scrub = offsets_scrub_icx,
410 .offsets_demand = offsets_demand_icx,
413 static struct res_config spr_cfg = {
416 .busno_cfg_offset = 0xd0,
417 .ddr_chan_mmio_sz = 0x8000,
418 .hbm_chan_mmio_sz = 0x4000,
419 .support_ddr5 = true,
420 .sad_all_devfn = PCI_DEVFN(10, 0),
421 .sad_all_offset = 0x300,
422 .offsets_scrub = offsets_scrub_spr,
423 .offsets_demand = offsets_demand_spr,
426 static const struct x86_cpu_id i10nm_cpuids[] = {
427 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(ATOM_TREMONT_D, X86_STEPPINGS(0x0, 0x3), &i10nm_cfg0),
428 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(ATOM_TREMONT_D, X86_STEPPINGS(0x4, 0xf), &i10nm_cfg1),
429 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(ICELAKE_X, X86_STEPPINGS(0x0, 0x3), &i10nm_cfg0),
430 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(ICELAKE_X, X86_STEPPINGS(0x4, 0xf), &i10nm_cfg1),
431 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(ICELAKE_D, X86_STEPPINGS(0x0, 0xf), &i10nm_cfg1),
432 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(SAPPHIRERAPIDS_X, X86_STEPPINGS(0x0, 0xf), &spr_cfg),
435 MODULE_DEVICE_TABLE(x86cpu, i10nm_cpuids);
437 static bool i10nm_check_ecc(struct skx_imc *imc, int chan)
441 mcmtr = I10NM_GET_MCMTR(imc, chan);
442 edac_dbg(1, "ch%d mcmtr reg %x\n", chan, mcmtr);
444 return !!GET_BITFIELD(mcmtr, 2, 2);
447 static int i10nm_get_dimm_config(struct mem_ctl_info *mci,
448 struct res_config *cfg)
450 struct skx_pvt *pvt = mci->pvt_info;
451 struct skx_imc *imc = pvt->imc;
452 u32 mtr, amap, mcddrtcfg;
453 struct dimm_info *dimm;
456 for (i = 0; i < imc->num_channels; i++) {
461 amap = I10NM_GET_AMAP(imc, i);
462 mcddrtcfg = I10NM_GET_MCDDRTCFG(imc, i);
463 for (j = 0; j < imc->num_dimms; j++) {
464 dimm = edac_get_dimm(mci, i, j, 0);
465 mtr = I10NM_GET_DIMMMTR(imc, i, j);
466 edac_dbg(1, "dimmmtr 0x%x mcddrtcfg 0x%x (mc%d ch%d dimm%d)\n",
467 mtr, mcddrtcfg, imc->mc, i, j);
469 if (IS_DIMM_PRESENT(mtr))
470 ndimms += skx_get_dimm_info(mtr, 0, amap, dimm,
472 else if (IS_NVDIMM_PRESENT(mcddrtcfg, j))
473 ndimms += skx_get_nvdimm_info(dimm, imc, i, j,
476 if (ndimms && !i10nm_check_ecc(imc, i)) {
477 i10nm_printk(KERN_ERR, "ECC is disabled on imc %d channel %d\n",
486 static struct notifier_block i10nm_mce_dec = {
487 .notifier_call = skx_mce_check_error,
488 .priority = MCE_PRIO_EDAC,
491 #ifdef CONFIG_EDAC_DEBUG
494 * Exercise the address decode logic by writing an address to
495 * /sys/kernel/debug/edac/i10nm_test/addr.
497 static struct dentry *i10nm_test;
499 static int debugfs_u64_set(void *data, u64 val)
503 pr_warn_once("Fake error to 0x%llx injected via debugfs\n", val);
505 memset(&m, 0, sizeof(m));
506 /* ADDRV + MemRd + Unknown channel */
507 m.status = MCI_STATUS_ADDRV + 0x90;
508 /* One corrected error */
509 m.status |= BIT_ULL(MCI_STATUS_CEC_SHIFT);
511 skx_mce_check_error(NULL, 0, &m);
515 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
517 static void setup_i10nm_debug(void)
519 i10nm_test = edac_debugfs_create_dir("i10nm_test");
523 if (!edac_debugfs_create_file("addr", 0200, i10nm_test,
524 NULL, &fops_u64_wo)) {
525 debugfs_remove(i10nm_test);
530 static void teardown_i10nm_debug(void)
532 debugfs_remove_recursive(i10nm_test);
535 static inline void setup_i10nm_debug(void) {}
536 static inline void teardown_i10nm_debug(void) {}
537 #endif /*CONFIG_EDAC_DEBUG*/
539 static int __init i10nm_init(void)
541 u8 mc = 0, src_id = 0, node_id = 0;
542 const struct x86_cpu_id *id;
543 struct res_config *cfg;
546 int rc, i, off[3] = {0xd0, 0xc8, 0xcc};
551 owner = edac_get_owner();
552 if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
555 if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
558 id = x86_match_cpu(i10nm_cpuids);
562 cfg = (struct res_config *)id->driver_data;
565 rc = skx_get_hi_lo(0x09a2, off, &tolm, &tohm);
569 rc = skx_get_all_bus_mappings(cfg, &i10nm_edac_list);
573 i10nm_printk(KERN_ERR, "No memory controllers found\n");
577 skx_set_mem_cfg(i10nm_check_2lm(cfg));
579 rc = i10nm_get_ddr_munits();
581 if (i10nm_get_hbm_munits() && rc)
584 list_for_each_entry(d, i10nm_edac_list, list) {
585 rc = skx_get_src_id(d, 0xf8, &src_id);
589 rc = skx_get_node_id(d, &node_id);
593 edac_dbg(2, "src_id = %d node_id = %d\n", src_id, node_id);
594 for (i = 0; i < I10NM_NUM_IMC; i++) {
600 d->imc[i].src_id = src_id;
601 d->imc[i].node_id = node_id;
602 if (d->imc[i].hbm_mc) {
603 d->imc[i].chan_mmio_sz = cfg->hbm_chan_mmio_sz;
604 d->imc[i].num_channels = I10NM_NUM_HBM_CHANNELS;
605 d->imc[i].num_dimms = I10NM_NUM_HBM_DIMMS;
607 d->imc[i].chan_mmio_sz = cfg->ddr_chan_mmio_sz;
608 d->imc[i].num_channels = I10NM_NUM_DDR_CHANNELS;
609 d->imc[i].num_dimms = I10NM_NUM_DDR_DIMMS;
612 rc = skx_register_mci(&d->imc[i], d->imc[i].mdev,
613 "Intel_10nm Socket", EDAC_MOD_STR,
614 i10nm_get_dimm_config, cfg);
625 mce_register_decode_chain(&i10nm_mce_dec);
628 if (retry_rd_err_log && res_cfg->offsets_scrub && res_cfg->offsets_demand) {
629 skx_set_decode(NULL, show_retry_rd_err_log);
630 if (retry_rd_err_log == 2)
631 enable_retry_rd_err_log(true);
634 i10nm_printk(KERN_INFO, "%s\n", I10NM_REVISION);
642 static void __exit i10nm_exit(void)
646 if (retry_rd_err_log && res_cfg->offsets_scrub && res_cfg->offsets_demand) {
647 skx_set_decode(NULL, NULL);
648 if (retry_rd_err_log == 2)
649 enable_retry_rd_err_log(false);
652 teardown_i10nm_debug();
653 mce_unregister_decode_chain(&i10nm_mce_dec);
658 module_init(i10nm_init);
659 module_exit(i10nm_exit);
661 module_param(retry_rd_err_log, int, 0444);
662 MODULE_PARM_DESC(retry_rd_err_log, "retry_rd_err_log: 0=off(default), 1=bios(Linux doesn't reset any control bits, but just reports values.), 2=linux(Linux tries to take control and resets mode bits, clear valid/UC bits after reading.)");
664 MODULE_LICENSE("GPL v2");
665 MODULE_DESCRIPTION("MC Driver for Intel 10nm server processors");