1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Intel client SoC with integrated memory controller using IBECC
5 * Copyright (C) 2020 Intel Corporation
7 * The In-Band ECC (IBECC) IP provides ECC protection to all or specific
8 * regions of the physical memory space. It's used for memory controllers
9 * that don't support the out-of-band ECC which often needs an additional
10 * storage device to each channel for storing ECC data.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/pci.h>
16 #include <linux/slab.h>
17 #include <linux/irq_work.h>
18 #include <linux/llist.h>
19 #include <linux/genalloc.h>
20 #include <linux/edac.h>
21 #include <linux/bits.h>
23 #include <asm/mach_traps.h>
28 #include "edac_module.h"
30 #define IGEN6_REVISION "v2.5"
32 #define EDAC_MOD_STR "igen6_edac"
33 #define IGEN6_NMI_NAME "igen6_ibecc"
36 #define igen6_printk(level, fmt, arg...) \
37 edac_printk(level, "igen6", fmt, ##arg)
39 #define igen6_mc_printk(mci, level, fmt, arg...) \
40 edac_mc_chipset_printk(mci, level, "igen6", fmt, ##arg)
42 #define GET_BITFIELD(v, lo, hi) (((v) & GENMASK_ULL(hi, lo)) >> (lo))
44 #define NUM_IMC 2 /* Max memory controllers */
45 #define NUM_CHANNELS 2 /* Max channels */
46 #define NUM_DIMMS 2 /* Max DIMMs per channel */
48 #define _4GB BIT_ULL(32)
50 /* Size of physical memory */
51 #define TOM_OFFSET 0xa0
52 /* Top of low usable DRAM */
53 #define TOLUD_OFFSET 0xbc
54 /* Capability register C */
55 #define CAPID_C_OFFSET 0xec
56 #define CAPID_C_IBECC BIT(15)
58 /* Capability register E */
59 #define CAPID_E_OFFSET 0xf0
60 #define CAPID_E_IBECC BIT(12)
63 #define ERRSTS_OFFSET 0xc8
64 #define ERRSTS_CE BIT_ULL(6)
65 #define ERRSTS_UE BIT_ULL(7)
68 #define ERRCMD_OFFSET 0xca
69 #define ERRCMD_CE BIT_ULL(6)
70 #define ERRCMD_UE BIT_ULL(7)
72 /* IBECC MMIO base address */
73 #define IBECC_BASE (res_cfg->ibecc_base)
74 #define IBECC_ACTIVATE_OFFSET IBECC_BASE
75 #define IBECC_ACTIVATE_EN BIT(0)
78 #define ECC_ERROR_LOG_OFFSET (IBECC_BASE + res_cfg->ibecc_error_log_offset)
79 #define ECC_ERROR_LOG_CE BIT_ULL(62)
80 #define ECC_ERROR_LOG_UE BIT_ULL(63)
81 #define ECC_ERROR_LOG_ADDR_SHIFT 5
82 #define ECC_ERROR_LOG_ADDR(v) GET_BITFIELD(v, 5, 38)
83 #define ECC_ERROR_LOG_SYND(v) GET_BITFIELD(v, 46, 61)
85 /* Host MMIO base address */
86 #define MCHBAR_OFFSET 0x48
87 #define MCHBAR_EN BIT_ULL(0)
88 #define MCHBAR_BASE(v) (GET_BITFIELD(v, 16, 38) << 16)
89 #define MCHBAR_SIZE 0x10000
91 /* Parameters for the channel decode stage */
92 #define IMC_BASE (res_cfg->imc_base)
93 #define MAD_INTER_CHANNEL_OFFSET IMC_BASE
94 #define MAD_INTER_CHANNEL_DDR_TYPE(v) GET_BITFIELD(v, 0, 2)
95 #define MAD_INTER_CHANNEL_ECHM(v) GET_BITFIELD(v, 3, 3)
96 #define MAD_INTER_CHANNEL_CH_L_MAP(v) GET_BITFIELD(v, 4, 4)
97 #define MAD_INTER_CHANNEL_CH_S_SIZE(v) ((u64)GET_BITFIELD(v, 12, 19) << 29)
99 /* Parameters for DRAM decode stage */
100 #define MAD_INTRA_CH0_OFFSET (IMC_BASE + 4)
101 #define MAD_INTRA_CH_DIMM_L_MAP(v) GET_BITFIELD(v, 0, 0)
103 /* DIMM characteristics */
104 #define MAD_DIMM_CH0_OFFSET (IMC_BASE + 0xc)
105 #define MAD_DIMM_CH_DIMM_L_SIZE(v) ((u64)GET_BITFIELD(v, 0, 6) << 29)
106 #define MAD_DIMM_CH_DLW(v) GET_BITFIELD(v, 7, 8)
107 #define MAD_DIMM_CH_DIMM_S_SIZE(v) ((u64)GET_BITFIELD(v, 16, 22) << 29)
108 #define MAD_DIMM_CH_DSW(v) GET_BITFIELD(v, 24, 25)
110 /* Hash for memory controller selection */
111 #define MAD_MC_HASH_OFFSET (IMC_BASE + 0x1b8)
112 #define MAC_MC_HASH_LSB(v) GET_BITFIELD(v, 1, 3)
114 /* Hash for channel selection */
115 #define CHANNEL_HASH_OFFSET (IMC_BASE + 0x24)
116 /* Hash for enhanced channel selection */
117 #define CHANNEL_EHASH_OFFSET (IMC_BASE + 0x28)
118 #define CHANNEL_HASH_MASK(v) (GET_BITFIELD(v, 6, 19) << 6)
119 #define CHANNEL_HASH_LSB_MASK_BIT(v) GET_BITFIELD(v, 24, 26)
120 #define CHANNEL_HASH_MODE(v) GET_BITFIELD(v, 28, 28)
122 /* Parameters for memory slice decode stage */
123 #define MEM_SLICE_HASH_MASK(v) (GET_BITFIELD(v, 6, 19) << 6)
124 #define MEM_SLICE_HASH_LSB_MASK_BIT(v) GET_BITFIELD(v, 24, 26)
126 static struct res_config {
134 u32 ibecc_error_log_offset;
135 bool (*ibecc_available)(struct pci_dev *pdev);
136 /* Convert error address logged in IBECC to system physical address */
137 u64 (*err_addr_to_sys_addr)(u64 eaddr, int mc);
138 /* Convert error address logged in IBECC to integrated memory controller address */
139 u64 (*err_addr_to_imc_addr)(u64 eaddr, int mc);
144 struct mem_ctl_info *mci;
145 struct pci_dev *pdev;
147 void __iomem *window;
151 u64 dimm_s_size[NUM_CHANNELS];
152 u64 dimm_l_size[NUM_CHANNELS];
153 int dimm_l_map[NUM_CHANNELS];
156 static struct igen6_pvt {
157 struct igen6_imc imc[NUM_IMC];
163 /* The top of low usable DRAM */
164 static u32 igen6_tolud;
165 /* The size of physical memory */
166 static u64 igen6_tom;
168 struct decoded_addr {
175 u64 sub_channel_addr;
179 struct llist_node llnode;
185 * In the NMI handler, the driver uses the lock-less memory allocator
186 * to allocate memory to store the IBECC error logs and links the logs
187 * to the lock-less list. Delay printk() and the work of error reporting
188 * to EDAC core in a worker.
190 #define ECCLOG_POOL_SIZE PAGE_SIZE
191 static LLIST_HEAD(ecclog_llist);
192 static struct gen_pool *ecclog_pool;
193 static char ecclog_buf[ECCLOG_POOL_SIZE];
194 static struct irq_work ecclog_irq_work;
195 static struct work_struct ecclog_work;
197 /* Compute die IDs for Elkhart Lake with IBECC */
198 #define DID_EHL_SKU5 0x4514
199 #define DID_EHL_SKU6 0x4528
200 #define DID_EHL_SKU7 0x452a
201 #define DID_EHL_SKU8 0x4516
202 #define DID_EHL_SKU9 0x452c
203 #define DID_EHL_SKU10 0x452e
204 #define DID_EHL_SKU11 0x4532
205 #define DID_EHL_SKU12 0x4518
206 #define DID_EHL_SKU13 0x451a
207 #define DID_EHL_SKU14 0x4534
208 #define DID_EHL_SKU15 0x4536
210 /* Compute die IDs for ICL-NNPI with IBECC */
211 #define DID_ICL_SKU8 0x4581
212 #define DID_ICL_SKU10 0x4585
213 #define DID_ICL_SKU11 0x4589
214 #define DID_ICL_SKU12 0x458d
216 /* Compute die IDs for Tiger Lake with IBECC */
217 #define DID_TGL_SKU 0x9a14
219 /* Compute die IDs for Alder Lake with IBECC */
220 #define DID_ADL_SKU1 0x4601
221 #define DID_ADL_SKU2 0x4602
222 #define DID_ADL_SKU3 0x4621
223 #define DID_ADL_SKU4 0x4641
225 static bool ehl_ibecc_available(struct pci_dev *pdev)
229 if (pci_read_config_dword(pdev, CAPID_C_OFFSET, &v))
232 return !!(CAPID_C_IBECC & v);
235 static u64 ehl_err_addr_to_sys_addr(u64 eaddr, int mc)
240 static u64 ehl_err_addr_to_imc_addr(u64 eaddr, int mc)
242 if (eaddr < igen6_tolud)
245 if (igen6_tom <= _4GB)
246 return eaddr + igen6_tolud - _4GB;
249 return eaddr + igen6_tolud - igen6_tom;
254 static bool icl_ibecc_available(struct pci_dev *pdev)
258 if (pci_read_config_dword(pdev, CAPID_C_OFFSET, &v))
261 return !(CAPID_C_IBECC & v) &&
262 (boot_cpu_data.x86_stepping >= 1);
265 static bool tgl_ibecc_available(struct pci_dev *pdev)
269 if (pci_read_config_dword(pdev, CAPID_E_OFFSET, &v))
272 return !(CAPID_E_IBECC & v);
275 static u64 mem_addr_to_sys_addr(u64 maddr)
277 if (maddr < igen6_tolud)
280 if (igen6_tom <= _4GB)
281 return maddr - igen6_tolud + _4GB;
284 return maddr - igen6_tolud + igen6_tom;
289 static u64 mem_slice_hash(u64 addr, u64 mask, u64 hash_init, int intlv_bit)
291 u64 hash_addr = addr & mask, hash = hash_init;
292 u64 intlv = (addr >> intlv_bit) & 1;
295 for (i = 6; i < 20; i++)
296 hash ^= (hash_addr >> i) & 1;
301 static u64 tgl_err_addr_to_mem_addr(u64 eaddr, int mc)
303 u64 maddr, hash, mask, ms_s_size;
307 ms_s_size = igen6_pvt->ms_s_size;
308 if (eaddr >= ms_s_size)
309 return eaddr + ms_s_size;
311 ms_hash = igen6_pvt->ms_hash;
313 mask = MEM_SLICE_HASH_MASK(ms_hash);
314 intlv_bit = MEM_SLICE_HASH_LSB_MASK_BIT(ms_hash) + 6;
316 maddr = GET_BITFIELD(eaddr, intlv_bit, 63) << (intlv_bit + 1) |
317 GET_BITFIELD(eaddr, 0, intlv_bit - 1);
319 hash = mem_slice_hash(maddr, mask, mc, intlv_bit);
321 return maddr | (hash << intlv_bit);
324 static u64 tgl_err_addr_to_sys_addr(u64 eaddr, int mc)
326 u64 maddr = tgl_err_addr_to_mem_addr(eaddr, mc);
328 return mem_addr_to_sys_addr(maddr);
331 static u64 tgl_err_addr_to_imc_addr(u64 eaddr, int mc)
336 static u64 adl_err_addr_to_sys_addr(u64 eaddr, int mc)
338 return mem_addr_to_sys_addr(eaddr);
341 static u64 adl_err_addr_to_imc_addr(u64 eaddr, int mc)
343 u64 imc_addr, ms_s_size = igen6_pvt->ms_s_size;
344 struct igen6_imc *imc = &igen6_pvt->imc[mc];
348 if (eaddr >= 2 * ms_s_size)
349 return eaddr - ms_s_size;
351 mc_hash = readl(imc->window + MAD_MC_HASH_OFFSET);
353 intlv_bit = MAC_MC_HASH_LSB(mc_hash) + 6;
355 imc_addr = GET_BITFIELD(eaddr, intlv_bit + 1, 63) << intlv_bit |
356 GET_BITFIELD(eaddr, 0, intlv_bit - 1);
361 static struct res_config ehl_cfg = {
364 .ibecc_base = 0xdc00,
365 .ibecc_available = ehl_ibecc_available,
366 .ibecc_error_log_offset = 0x170,
367 .err_addr_to_sys_addr = ehl_err_addr_to_sys_addr,
368 .err_addr_to_imc_addr = ehl_err_addr_to_imc_addr,
371 static struct res_config icl_cfg = {
374 .ibecc_base = 0xd800,
375 .ibecc_error_log_offset = 0x170,
376 .ibecc_available = icl_ibecc_available,
377 .err_addr_to_sys_addr = ehl_err_addr_to_sys_addr,
378 .err_addr_to_imc_addr = ehl_err_addr_to_imc_addr,
381 static struct res_config tgl_cfg = {
382 .machine_check = true,
387 .ms_hash_offset = 0xac,
388 .ibecc_base = 0xd400,
389 .ibecc_error_log_offset = 0x170,
390 .ibecc_available = tgl_ibecc_available,
391 .err_addr_to_sys_addr = tgl_err_addr_to_sys_addr,
392 .err_addr_to_imc_addr = tgl_err_addr_to_imc_addr,
395 static struct res_config adl_cfg = {
396 .machine_check = true,
399 .ibecc_base = 0xd400,
400 .ibecc_error_log_offset = 0x68,
401 .ibecc_available = tgl_ibecc_available,
402 .err_addr_to_sys_addr = adl_err_addr_to_sys_addr,
403 .err_addr_to_imc_addr = adl_err_addr_to_imc_addr,
406 static const struct pci_device_id igen6_pci_tbl[] = {
407 { PCI_VDEVICE(INTEL, DID_EHL_SKU5), (kernel_ulong_t)&ehl_cfg },
408 { PCI_VDEVICE(INTEL, DID_EHL_SKU6), (kernel_ulong_t)&ehl_cfg },
409 { PCI_VDEVICE(INTEL, DID_EHL_SKU7), (kernel_ulong_t)&ehl_cfg },
410 { PCI_VDEVICE(INTEL, DID_EHL_SKU8), (kernel_ulong_t)&ehl_cfg },
411 { PCI_VDEVICE(INTEL, DID_EHL_SKU9), (kernel_ulong_t)&ehl_cfg },
412 { PCI_VDEVICE(INTEL, DID_EHL_SKU10), (kernel_ulong_t)&ehl_cfg },
413 { PCI_VDEVICE(INTEL, DID_EHL_SKU11), (kernel_ulong_t)&ehl_cfg },
414 { PCI_VDEVICE(INTEL, DID_EHL_SKU12), (kernel_ulong_t)&ehl_cfg },
415 { PCI_VDEVICE(INTEL, DID_EHL_SKU13), (kernel_ulong_t)&ehl_cfg },
416 { PCI_VDEVICE(INTEL, DID_EHL_SKU14), (kernel_ulong_t)&ehl_cfg },
417 { PCI_VDEVICE(INTEL, DID_EHL_SKU15), (kernel_ulong_t)&ehl_cfg },
418 { PCI_VDEVICE(INTEL, DID_ICL_SKU8), (kernel_ulong_t)&icl_cfg },
419 { PCI_VDEVICE(INTEL, DID_ICL_SKU10), (kernel_ulong_t)&icl_cfg },
420 { PCI_VDEVICE(INTEL, DID_ICL_SKU11), (kernel_ulong_t)&icl_cfg },
421 { PCI_VDEVICE(INTEL, DID_ICL_SKU12), (kernel_ulong_t)&icl_cfg },
422 { PCI_VDEVICE(INTEL, DID_TGL_SKU), (kernel_ulong_t)&tgl_cfg },
423 { PCI_VDEVICE(INTEL, DID_ADL_SKU1), (kernel_ulong_t)&adl_cfg },
424 { PCI_VDEVICE(INTEL, DID_ADL_SKU2), (kernel_ulong_t)&adl_cfg },
425 { PCI_VDEVICE(INTEL, DID_ADL_SKU3), (kernel_ulong_t)&adl_cfg },
426 { PCI_VDEVICE(INTEL, DID_ADL_SKU4), (kernel_ulong_t)&adl_cfg },
429 MODULE_DEVICE_TABLE(pci, igen6_pci_tbl);
431 static enum dev_type get_width(int dimm_l, u32 mad_dimm)
433 u32 w = dimm_l ? MAD_DIMM_CH_DLW(mad_dimm) :
434 MAD_DIMM_CH_DSW(mad_dimm);
448 static enum mem_type get_memory_type(u32 mad_inter)
450 u32 t = MAD_INTER_CHANNEL_DDR_TYPE(mad_inter);
468 static int decode_chan_idx(u64 addr, u64 mask, int intlv_bit)
470 u64 hash_addr = addr & mask, hash = 0;
471 u64 intlv = (addr >> intlv_bit) & 1;
474 for (i = 6; i < 20; i++)
475 hash ^= (hash_addr >> i) & 1;
477 return (int)hash ^ intlv;
480 static u64 decode_channel_addr(u64 addr, int intlv_bit)
484 /* Remove the interleave bit and shift upper part down to fill gap */
485 channel_addr = GET_BITFIELD(addr, intlv_bit + 1, 63) << intlv_bit;
486 channel_addr |= GET_BITFIELD(addr, 0, intlv_bit - 1);
491 static void decode_addr(u64 addr, u32 hash, u64 s_size, int l_map,
492 int *idx, u64 *sub_addr)
494 int intlv_bit = CHANNEL_HASH_LSB_MASK_BIT(hash) + 6;
496 if (addr > 2 * s_size) {
497 *sub_addr = addr - s_size;
502 if (CHANNEL_HASH_MODE(hash)) {
503 *sub_addr = decode_channel_addr(addr, intlv_bit);
504 *idx = decode_chan_idx(addr, CHANNEL_HASH_MASK(hash), intlv_bit);
506 *sub_addr = decode_channel_addr(addr, 6);
507 *idx = GET_BITFIELD(addr, 6, 6);
511 static int igen6_decode(struct decoded_addr *res)
513 struct igen6_imc *imc = &igen6_pvt->imc[res->mc];
514 u64 addr = res->imc_addr, sub_addr, s_size;
518 if (addr >= igen6_tom) {
519 edac_dbg(0, "Address 0x%llx out of range\n", addr);
524 hash = readl(imc->window + CHANNEL_HASH_OFFSET);
525 s_size = imc->ch_s_size;
526 l_map = imc->ch_l_map;
527 decode_addr(addr, hash, s_size, l_map, &idx, &sub_addr);
528 res->channel_idx = idx;
529 res->channel_addr = sub_addr;
531 /* Decode sub-channel/DIMM */
532 hash = readl(imc->window + CHANNEL_EHASH_OFFSET);
533 s_size = imc->dimm_s_size[idx];
534 l_map = imc->dimm_l_map[idx];
535 decode_addr(res->channel_addr, hash, s_size, l_map, &idx, &sub_addr);
536 res->sub_channel_idx = idx;
537 res->sub_channel_addr = sub_addr;
542 static void igen6_output_error(struct decoded_addr *res,
543 struct mem_ctl_info *mci, u64 ecclog)
545 enum hw_event_mc_err_type type = ecclog & ECC_ERROR_LOG_UE ?
546 HW_EVENT_ERR_UNCORRECTED :
547 HW_EVENT_ERR_CORRECTED;
549 edac_mc_handle_error(type, mci, 1,
550 res->sys_addr >> PAGE_SHIFT,
551 res->sys_addr & ~PAGE_MASK,
552 ECC_ERROR_LOG_SYND(ecclog),
553 res->channel_idx, res->sub_channel_idx,
557 static struct gen_pool *ecclog_gen_pool_create(void)
559 struct gen_pool *pool;
561 pool = gen_pool_create(ilog2(sizeof(struct ecclog_node)), -1);
565 if (gen_pool_add(pool, (unsigned long)ecclog_buf, ECCLOG_POOL_SIZE, -1)) {
566 gen_pool_destroy(pool);
573 static int ecclog_gen_pool_add(int mc, u64 ecclog)
575 struct ecclog_node *node;
577 node = (void *)gen_pool_alloc(ecclog_pool, sizeof(*node));
582 node->ecclog = ecclog;
583 llist_add(&node->llnode, &ecclog_llist);
589 * Either the memory-mapped I/O status register ECC_ERROR_LOG or the PCI
590 * configuration space status register ERRSTS can indicate whether a
591 * correctable error or an uncorrectable error occurred. We only use the
592 * ECC_ERROR_LOG register to check error type, but need to clear both
593 * registers to enable future error events.
595 static u64 ecclog_read_and_clear(struct igen6_imc *imc)
597 u64 ecclog = readq(imc->window + ECC_ERROR_LOG_OFFSET);
599 if (ecclog & (ECC_ERROR_LOG_CE | ECC_ERROR_LOG_UE)) {
600 /* Clear CE/UE bits by writing 1s */
601 writeq(ecclog, imc->window + ECC_ERROR_LOG_OFFSET);
608 static void errsts_clear(struct igen6_imc *imc)
612 if (pci_read_config_word(imc->pdev, ERRSTS_OFFSET, &errsts)) {
613 igen6_printk(KERN_ERR, "Failed to read ERRSTS\n");
617 /* Clear CE/UE bits by writing 1s */
618 if (errsts & (ERRSTS_CE | ERRSTS_UE))
619 pci_write_config_word(imc->pdev, ERRSTS_OFFSET, errsts);
622 static int errcmd_enable_error_reporting(bool enable)
624 struct igen6_imc *imc = &igen6_pvt->imc[0];
628 rc = pci_read_config_word(imc->pdev, ERRCMD_OFFSET, &errcmd);
633 errcmd |= ERRCMD_CE | ERRSTS_UE;
635 errcmd &= ~(ERRCMD_CE | ERRSTS_UE);
637 rc = pci_write_config_word(imc->pdev, ERRCMD_OFFSET, errcmd);
644 static int ecclog_handler(void)
646 struct igen6_imc *imc;
650 for (i = 0; i < res_cfg->num_imc; i++) {
651 imc = &igen6_pvt->imc[i];
653 /* errsts_clear() isn't NMI-safe. Delay it in the IRQ context */
655 ecclog = ecclog_read_and_clear(imc);
659 if (!ecclog_gen_pool_add(i, ecclog))
660 irq_work_queue(&ecclog_irq_work);
668 static void ecclog_work_cb(struct work_struct *work)
670 struct ecclog_node *node, *tmp;
671 struct mem_ctl_info *mci;
672 struct llist_node *head;
673 struct decoded_addr res;
676 head = llist_del_all(&ecclog_llist);
680 llist_for_each_entry_safe(node, tmp, head, llnode) {
681 memset(&res, 0, sizeof(res));
682 eaddr = ECC_ERROR_LOG_ADDR(node->ecclog) <<
683 ECC_ERROR_LOG_ADDR_SHIFT;
685 res.sys_addr = res_cfg->err_addr_to_sys_addr(eaddr, res.mc);
686 res.imc_addr = res_cfg->err_addr_to_imc_addr(eaddr, res.mc);
688 mci = igen6_pvt->imc[res.mc].mci;
690 edac_dbg(2, "MC %d, ecclog = 0x%llx\n", node->mc, node->ecclog);
691 igen6_mc_printk(mci, KERN_DEBUG, "HANDLING IBECC MEMORY ERROR\n");
692 igen6_mc_printk(mci, KERN_DEBUG, "ADDR 0x%llx ", res.sys_addr);
694 if (!igen6_decode(&res))
695 igen6_output_error(&res, mci, node->ecclog);
697 gen_pool_free(ecclog_pool, (unsigned long)node, sizeof(*node));
701 static void ecclog_irq_work_cb(struct irq_work *irq_work)
705 for (i = 0; i < res_cfg->num_imc; i++)
706 errsts_clear(&igen6_pvt->imc[i]);
708 if (!llist_empty(&ecclog_llist))
709 schedule_work(&ecclog_work);
712 static int ecclog_nmi_handler(unsigned int cmd, struct pt_regs *regs)
714 unsigned char reason;
716 if (!ecclog_handler())
720 * Both In-Band ECC correctable error and uncorrectable error are
721 * reported by SERR# NMI. The NMI generic code (see pci_serr_error())
722 * doesn't clear the bit NMI_REASON_CLEAR_SERR (in port 0x61) to
723 * re-enable the SERR# NMI after NMI handling. So clear this bit here
724 * to re-enable SERR# NMI for receiving future In-Band ECC errors.
726 reason = x86_platform.get_nmi_reason() & NMI_REASON_CLEAR_MASK;
727 reason |= NMI_REASON_CLEAR_SERR;
728 outb(reason, NMI_REASON_PORT);
729 reason &= ~NMI_REASON_CLEAR_SERR;
730 outb(reason, NMI_REASON_PORT);
735 static int ecclog_mce_handler(struct notifier_block *nb, unsigned long val,
738 struct mce *mce = (struct mce *)data;
741 if (mce->kflags & MCE_HANDLED_CEC)
745 * Ignore unless this is a memory related error.
746 * We don't check the bit MCI_STATUS_ADDRV of MCi_STATUS here,
747 * since this bit isn't set on some CPU (e.g., Tiger Lake UP3).
749 if ((mce->status & 0xefff) >> 7 != 1)
752 if (mce->mcgstatus & MCG_STATUS_MCIP)
757 edac_dbg(0, "CPU %d: Machine Check %s: 0x%llx Bank %d: 0x%llx\n",
758 mce->extcpu, type, mce->mcgstatus,
759 mce->bank, mce->status);
760 edac_dbg(0, "TSC 0x%llx\n", mce->tsc);
761 edac_dbg(0, "ADDR 0x%llx\n", mce->addr);
762 edac_dbg(0, "MISC 0x%llx\n", mce->misc);
763 edac_dbg(0, "PROCESSOR %u:0x%x TIME %llu SOCKET %u APIC 0x%x\n",
764 mce->cpuvendor, mce->cpuid, mce->time,
765 mce->socketid, mce->apicid);
767 * We just use the Machine Check for the memory error notification.
768 * Each memory controller is associated with an IBECC instance.
769 * Directly read and clear the error information(error address and
770 * error type) on all the IBECC instances so that we know on which
771 * memory controller the memory error(s) occurred.
773 if (!ecclog_handler())
776 mce->kflags |= MCE_HANDLED_EDAC;
781 static struct notifier_block ecclog_mce_dec = {
782 .notifier_call = ecclog_mce_handler,
783 .priority = MCE_PRIO_EDAC,
786 static bool igen6_check_ecc(struct igen6_imc *imc)
788 u32 activate = readl(imc->window + IBECC_ACTIVATE_OFFSET);
790 return !!(activate & IBECC_ACTIVATE_EN);
793 static int igen6_get_dimm_config(struct mem_ctl_info *mci)
795 struct igen6_imc *imc = mci->pvt_info;
796 u32 mad_inter, mad_intra, mad_dimm;
797 int i, j, ndimms, mc = imc->mc;
798 struct dimm_info *dimm;
806 mad_inter = readl(imc->window + MAD_INTER_CHANNEL_OFFSET);
807 mtype = get_memory_type(mad_inter);
808 ecc = igen6_check_ecc(imc);
809 imc->ch_s_size = MAD_INTER_CHANNEL_CH_S_SIZE(mad_inter);
810 imc->ch_l_map = MAD_INTER_CHANNEL_CH_L_MAP(mad_inter);
812 for (i = 0; i < NUM_CHANNELS; i++) {
813 mad_intra = readl(imc->window + MAD_INTRA_CH0_OFFSET + i * 4);
814 mad_dimm = readl(imc->window + MAD_DIMM_CH0_OFFSET + i * 4);
816 imc->dimm_l_size[i] = MAD_DIMM_CH_DIMM_L_SIZE(mad_dimm);
817 imc->dimm_s_size[i] = MAD_DIMM_CH_DIMM_S_SIZE(mad_dimm);
818 imc->dimm_l_map[i] = MAD_INTRA_CH_DIMM_L_MAP(mad_intra);
819 imc->size += imc->dimm_s_size[i];
820 imc->size += imc->dimm_l_size[i];
823 for (j = 0; j < NUM_DIMMS; j++) {
824 dimm = edac_get_dimm(mci, i, j, 0);
826 if (j ^ imc->dimm_l_map[i]) {
827 dtype = get_width(0, mad_dimm);
828 dsize = imc->dimm_s_size[i];
830 dtype = get_width(1, mad_dimm);
831 dsize = imc->dimm_l_size[i];
840 dimm->nr_pages = MiB_TO_PAGES(dsize >> 20);
841 dimm->edac_mode = EDAC_SECDED;
842 snprintf(dimm->label, sizeof(dimm->label),
843 "MC#%d_Chan#%d_DIMM#%d", mc, i, j);
844 edac_dbg(0, "MC %d, Channel %d, DIMM %d, Size %llu MiB (%u pages)\n",
845 mc, i, j, dsize >> 20, dimm->nr_pages);
850 if (ndimms && !ecc) {
851 igen6_printk(KERN_ERR, "MC%d In-Band ECC is disabled\n", mc);
856 edac_dbg(0, "MC %d, total size %llu MiB\n", mc, imc->size >> 20);
861 #ifdef CONFIG_EDAC_DEBUG
862 /* Top of upper usable DRAM */
863 static u64 igen6_touud;
864 #define TOUUD_OFFSET 0xa8
866 static void igen6_reg_dump(struct igen6_imc *imc)
870 edac_dbg(2, "CHANNEL_HASH : 0x%x\n",
871 readl(imc->window + CHANNEL_HASH_OFFSET));
872 edac_dbg(2, "CHANNEL_EHASH : 0x%x\n",
873 readl(imc->window + CHANNEL_EHASH_OFFSET));
874 edac_dbg(2, "MAD_INTER_CHANNEL: 0x%x\n",
875 readl(imc->window + MAD_INTER_CHANNEL_OFFSET));
876 edac_dbg(2, "ECC_ERROR_LOG : 0x%llx\n",
877 readq(imc->window + ECC_ERROR_LOG_OFFSET));
879 for (i = 0; i < NUM_CHANNELS; i++) {
880 edac_dbg(2, "MAD_INTRA_CH%d : 0x%x\n", i,
881 readl(imc->window + MAD_INTRA_CH0_OFFSET + i * 4));
882 edac_dbg(2, "MAD_DIMM_CH%d : 0x%x\n", i,
883 readl(imc->window + MAD_DIMM_CH0_OFFSET + i * 4));
885 edac_dbg(2, "TOLUD : 0x%x", igen6_tolud);
886 edac_dbg(2, "TOUUD : 0x%llx", igen6_touud);
887 edac_dbg(2, "TOM : 0x%llx", igen6_tom);
890 static struct dentry *igen6_test;
892 static int debugfs_u64_set(void *data, u64 val)
896 if ((val >= igen6_tolud && val < _4GB) || val >= igen6_touud) {
897 edac_dbg(0, "Address 0x%llx out of range\n", val);
901 pr_warn_once("Fake error to 0x%llx injected via debugfs\n", val);
903 val >>= ECC_ERROR_LOG_ADDR_SHIFT;
904 ecclog = (val << ECC_ERROR_LOG_ADDR_SHIFT) | ECC_ERROR_LOG_CE;
906 if (!ecclog_gen_pool_add(0, ecclog))
907 irq_work_queue(&ecclog_irq_work);
911 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
913 static void igen6_debug_setup(void)
915 igen6_test = edac_debugfs_create_dir("igen6_test");
919 if (!edac_debugfs_create_file("addr", 0200, igen6_test,
920 NULL, &fops_u64_wo)) {
921 debugfs_remove(igen6_test);
926 static void igen6_debug_teardown(void)
928 debugfs_remove_recursive(igen6_test);
931 static void igen6_reg_dump(struct igen6_imc *imc) {}
932 static void igen6_debug_setup(void) {}
933 static void igen6_debug_teardown(void) {}
936 static int igen6_pci_setup(struct pci_dev *pdev, u64 *mchbar)
948 if (!res_cfg->ibecc_available(pdev)) {
949 edac_dbg(2, "No In-Band ECC IP\n");
953 if (pci_read_config_dword(pdev, TOLUD_OFFSET, &igen6_tolud)) {
954 igen6_printk(KERN_ERR, "Failed to read TOLUD\n");
958 igen6_tolud &= GENMASK(31, 20);
960 if (pci_read_config_dword(pdev, TOM_OFFSET, &u.v_lo)) {
961 igen6_printk(KERN_ERR, "Failed to read lower TOM\n");
965 if (pci_read_config_dword(pdev, TOM_OFFSET + 4, &u.v_hi)) {
966 igen6_printk(KERN_ERR, "Failed to read upper TOM\n");
970 igen6_tom = u.v & GENMASK_ULL(38, 20);
972 if (pci_read_config_dword(pdev, MCHBAR_OFFSET, &u.v_lo)) {
973 igen6_printk(KERN_ERR, "Failed to read lower MCHBAR\n");
977 if (pci_read_config_dword(pdev, MCHBAR_OFFSET + 4, &u.v_hi)) {
978 igen6_printk(KERN_ERR, "Failed to read upper MCHBAR\n");
982 if (!(u.v & MCHBAR_EN)) {
983 igen6_printk(KERN_ERR, "MCHBAR is disabled\n");
987 *mchbar = MCHBAR_BASE(u.v);
989 #ifdef CONFIG_EDAC_DEBUG
990 if (pci_read_config_dword(pdev, TOUUD_OFFSET, &u.v_lo))
991 edac_dbg(2, "Failed to read lower TOUUD\n");
992 else if (pci_read_config_dword(pdev, TOUUD_OFFSET + 4, &u.v_hi))
993 edac_dbg(2, "Failed to read upper TOUUD\n");
995 igen6_touud = u.v & GENMASK_ULL(38, 20);
1003 static int igen6_register_mci(int mc, u64 mchbar, struct pci_dev *pdev)
1005 struct edac_mc_layer layers[2];
1006 struct mem_ctl_info *mci;
1007 struct igen6_imc *imc;
1008 void __iomem *window;
1013 mchbar += mc * MCHBAR_SIZE;
1014 window = ioremap(mchbar, MCHBAR_SIZE);
1016 igen6_printk(KERN_ERR, "Failed to ioremap 0x%llx\n", mchbar);
1020 layers[0].type = EDAC_MC_LAYER_CHANNEL;
1021 layers[0].size = NUM_CHANNELS;
1022 layers[0].is_virt_csrow = false;
1023 layers[1].type = EDAC_MC_LAYER_SLOT;
1024 layers[1].size = NUM_DIMMS;
1025 layers[1].is_virt_csrow = true;
1027 mci = edac_mc_alloc(mc, ARRAY_SIZE(layers), layers, 0);
1033 mci->ctl_name = kasprintf(GFP_KERNEL, "Intel_client_SoC MC#%d", mc);
1034 if (!mci->ctl_name) {
1039 mci->mtype_cap = MEM_FLAG_LPDDR4 | MEM_FLAG_DDR4;
1040 mci->edac_ctl_cap = EDAC_FLAG_SECDED;
1041 mci->edac_cap = EDAC_FLAG_SECDED;
1042 mci->mod_name = EDAC_MOD_STR;
1043 mci->dev_name = pci_name(pdev);
1044 mci->pvt_info = &igen6_pvt->imc[mc];
1046 imc = mci->pvt_info;
1047 device_initialize(&imc->dev);
1049 * EDAC core uses mci->pdev(pointer of structure device) as
1050 * memory controller ID. The client SoCs attach one or more
1051 * memory controllers to single pci_dev (single pci_dev->dev
1052 * can be for multiple memory controllers).
1054 * To make mci->pdev unique, assign pci_dev->dev to mci->pdev
1055 * for the first memory controller and assign a unique imc->dev
1056 * to mci->pdev for each non-first memory controller.
1058 mci->pdev = mc ? &imc->dev : &pdev->dev;
1061 imc->window = window;
1063 igen6_reg_dump(imc);
1065 rc = igen6_get_dimm_config(mci);
1069 rc = edac_mc_add_mc(mci);
1071 igen6_printk(KERN_ERR, "Failed to register mci#%d\n", mc);
1078 kfree(mci->ctl_name);
1086 static void igen6_unregister_mcis(void)
1088 struct mem_ctl_info *mci;
1089 struct igen6_imc *imc;
1094 for (i = 0; i < res_cfg->num_imc; i++) {
1095 imc = &igen6_pvt->imc[i];
1100 edac_mc_del_mc(mci->pdev);
1101 kfree(mci->ctl_name);
1103 iounmap(imc->window);
1107 static int igen6_mem_slice_setup(u64 mchbar)
1109 struct igen6_imc *imc = &igen6_pvt->imc[0];
1110 u64 base = mchbar + res_cfg->cmf_base;
1111 u32 offset = res_cfg->ms_hash_offset;
1112 u32 size = res_cfg->cmf_size;
1113 u64 ms_s_size, ms_hash;
1119 if (imc[0].size < imc[1].size) {
1120 ms_s_size = imc[0].size;
1123 ms_s_size = imc[1].size;
1127 igen6_pvt->ms_s_size = ms_s_size;
1128 igen6_pvt->ms_l_map = ms_l_map;
1130 edac_dbg(0, "ms_s_size: %llu MiB, ms_l_map %d\n",
1131 ms_s_size >> 20, ms_l_map);
1136 cmf = ioremap(base, size);
1138 igen6_printk(KERN_ERR, "Failed to ioremap cmf 0x%llx\n", base);
1142 ms_hash = readq(cmf + offset);
1143 igen6_pvt->ms_hash = ms_hash;
1145 edac_dbg(0, "MEM_SLICE_HASH: 0x%llx\n", ms_hash);
1152 static int register_err_handler(void)
1156 if (res_cfg->machine_check) {
1157 mce_register_decode_chain(&ecclog_mce_dec);
1161 rc = register_nmi_handler(NMI_SERR, ecclog_nmi_handler,
1164 igen6_printk(KERN_ERR, "Failed to register NMI handler\n");
1171 static void unregister_err_handler(void)
1173 if (res_cfg->machine_check) {
1174 mce_unregister_decode_chain(&ecclog_mce_dec);
1178 unregister_nmi_handler(NMI_SERR, IGEN6_NMI_NAME);
1181 static int igen6_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1188 igen6_pvt = kzalloc(sizeof(*igen6_pvt), GFP_KERNEL);
1192 res_cfg = (struct res_config *)ent->driver_data;
1194 rc = igen6_pci_setup(pdev, &mchbar);
1198 for (i = 0; i < res_cfg->num_imc; i++) {
1199 rc = igen6_register_mci(i, mchbar, pdev);
1204 if (res_cfg->num_imc > 1) {
1205 rc = igen6_mem_slice_setup(mchbar);
1210 ecclog_pool = ecclog_gen_pool_create();
1216 INIT_WORK(&ecclog_work, ecclog_work_cb);
1217 init_irq_work(&ecclog_irq_work, ecclog_irq_work_cb);
1219 /* Check if any pending errors before registering the NMI handler */
1222 rc = register_err_handler();
1226 /* Enable error reporting */
1227 rc = errcmd_enable_error_reporting(true);
1229 igen6_printk(KERN_ERR, "Failed to enable error reporting\n");
1233 igen6_debug_setup();
1236 unregister_nmi_handler(NMI_SERR, IGEN6_NMI_NAME);
1238 gen_pool_destroy(ecclog_pool);
1240 igen6_unregister_mcis();
1246 static void igen6_remove(struct pci_dev *pdev)
1250 igen6_debug_teardown();
1251 errcmd_enable_error_reporting(false);
1252 unregister_err_handler();
1253 irq_work_sync(&ecclog_irq_work);
1254 flush_work(&ecclog_work);
1255 gen_pool_destroy(ecclog_pool);
1256 igen6_unregister_mcis();
1260 static struct pci_driver igen6_driver = {
1261 .name = EDAC_MOD_STR,
1262 .probe = igen6_probe,
1263 .remove = igen6_remove,
1264 .id_table = igen6_pci_tbl,
1267 static int __init igen6_init(void)
1274 owner = edac_get_owner();
1275 if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
1278 edac_op_state = EDAC_OPSTATE_NMI;
1280 rc = pci_register_driver(&igen6_driver);
1284 igen6_printk(KERN_INFO, "%s\n", IGEN6_REVISION);
1289 static void __exit igen6_exit(void)
1293 pci_unregister_driver(&igen6_driver);
1296 module_init(igen6_init);
1297 module_exit(igen6_exit);
1299 MODULE_LICENSE("GPL v2");
1300 MODULE_AUTHOR("Qiuxu Zhuo");
1301 MODULE_DESCRIPTION("MC Driver for Intel client SoC using In-Band ECC");