1 // SPDX-License-Identifier: GPL-2.0
4 * Shared code by both skx_edac and i10nm_edac. Originally split out
5 * from the skx_edac driver.
7 * This file is linked into both skx_edac and i10nm_edac drivers. In
8 * order to avoid link errors, this file must be like a pure library
9 * without including symbols and defines which would otherwise conflict,
10 * when linked once into a module and into a built-in object, at the
11 * same time. For example, __this_module symbol references when that
12 * file is being linked into a built-in object.
14 * Copyright (c) 2018, Intel Corporation.
17 #include <linux/acpi.h>
18 #include <linux/dmi.h>
19 #include <linux/adxl.h>
20 #include <acpi/nfit.h>
22 #include "edac_module.h"
23 #include "skx_common.h"
25 static const char * const component_names[] = {
26 [INDEX_SOCKET] = "ProcessorSocketId",
27 [INDEX_MEMCTRL] = "MemoryControllerId",
28 [INDEX_CHANNEL] = "ChannelId",
29 [INDEX_DIMM] = "DimmSlotId",
30 [INDEX_CS] = "ChipSelect",
31 [INDEX_NM_MEMCTRL] = "NmMemoryControllerId",
32 [INDEX_NM_CHANNEL] = "NmChannelId",
33 [INDEX_NM_DIMM] = "NmDimmSlotId",
34 [INDEX_NM_CS] = "NmChipSelect",
37 static int component_indices[ARRAY_SIZE(component_names)];
38 static int adxl_component_count;
39 static const char * const *adxl_component_names;
40 static u64 *adxl_values;
41 static char *adxl_msg;
42 static unsigned long adxl_nm_bitmap;
44 static char skx_msg[MSG_SIZE];
45 static skx_decode_f driver_decode;
46 static skx_show_retry_log_f skx_show_retry_rd_err_log;
47 static u64 skx_tolm, skx_tohm;
48 static LIST_HEAD(dev_edac_list);
49 static bool skx_mem_cfg_2lm;
51 int __init skx_adxl_get(void)
53 const char * const *names;
56 names = adxl_get_component_names();
58 skx_printk(KERN_NOTICE, "No firmware support for address translation.\n");
62 for (i = 0; i < INDEX_MAX; i++) {
63 for (j = 0; names[j]; j++) {
64 if (!strcmp(component_names[i], names[j])) {
65 component_indices[i] = j;
67 if (i >= INDEX_NM_FIRST)
68 adxl_nm_bitmap |= 1 << i;
74 if (!names[j] && i < INDEX_NM_FIRST)
78 if (skx_mem_cfg_2lm) {
80 skx_printk(KERN_NOTICE, "Not enough ADXL components for 2-level memory.\n");
82 edac_dbg(2, "adxl_nm_bitmap: 0x%lx\n", adxl_nm_bitmap);
85 adxl_component_names = names;
87 adxl_component_count++;
89 adxl_values = kcalloc(adxl_component_count, sizeof(*adxl_values),
92 adxl_component_count = 0;
96 adxl_msg = kzalloc(MSG_SIZE, GFP_KERNEL);
98 adxl_component_count = 0;
105 skx_printk(KERN_ERR, "'%s' is not matched from DSM parameters: ",
107 for (j = 0; names[j]; j++)
108 skx_printk(KERN_CONT, "%s ", names[j]);
109 skx_printk(KERN_CONT, "\n");
114 void __exit skx_adxl_put(void)
120 static bool skx_adxl_decode(struct decoded_addr *res, bool error_in_1st_level_mem)
125 if (res->addr >= skx_tohm || (res->addr >= skx_tolm &&
126 res->addr < BIT_ULL(32))) {
127 edac_dbg(0, "Address 0x%llx out of range\n", res->addr);
131 if (adxl_decode(res->addr, adxl_values)) {
132 edac_dbg(0, "Failed to decode 0x%llx\n", res->addr);
136 res->socket = (int)adxl_values[component_indices[INDEX_SOCKET]];
137 if (error_in_1st_level_mem) {
138 res->imc = (adxl_nm_bitmap & BIT_NM_MEMCTRL) ?
139 (int)adxl_values[component_indices[INDEX_NM_MEMCTRL]] : -1;
140 res->channel = (adxl_nm_bitmap & BIT_NM_CHANNEL) ?
141 (int)adxl_values[component_indices[INDEX_NM_CHANNEL]] : -1;
142 res->dimm = (adxl_nm_bitmap & BIT_NM_DIMM) ?
143 (int)adxl_values[component_indices[INDEX_NM_DIMM]] : -1;
144 res->cs = (adxl_nm_bitmap & BIT_NM_CS) ?
145 (int)adxl_values[component_indices[INDEX_NM_CS]] : -1;
147 res->imc = (int)adxl_values[component_indices[INDEX_MEMCTRL]];
148 res->channel = (int)adxl_values[component_indices[INDEX_CHANNEL]];
149 res->dimm = (int)adxl_values[component_indices[INDEX_DIMM]];
150 res->cs = (int)adxl_values[component_indices[INDEX_CS]];
153 if (res->imc > NUM_IMC - 1 || res->imc < 0) {
154 skx_printk(KERN_ERR, "Bad imc %d\n", res->imc);
158 list_for_each_entry(d, &dev_edac_list, list) {
159 if (d->imc[0].src_id == res->socket) {
166 skx_printk(KERN_ERR, "No device for src_id %d imc %d\n",
167 res->socket, res->imc);
171 for (i = 0; i < adxl_component_count; i++) {
172 if (adxl_values[i] == ~0x0ull)
175 len += snprintf(adxl_msg + len, MSG_SIZE - len, " %s:0x%llx",
176 adxl_component_names[i], adxl_values[i]);
177 if (MSG_SIZE - len <= 0)
181 res->decoded_by_adxl = true;
186 void skx_set_mem_cfg(bool mem_cfg_2lm)
188 skx_mem_cfg_2lm = mem_cfg_2lm;
191 void skx_set_decode(skx_decode_f decode, skx_show_retry_log_f show_retry_log)
193 driver_decode = decode;
194 skx_show_retry_rd_err_log = show_retry_log;
197 int skx_get_src_id(struct skx_dev *d, int off, u8 *id)
201 if (pci_read_config_dword(d->util_all, off, ®)) {
202 skx_printk(KERN_ERR, "Failed to read src id\n");
206 *id = GET_BITFIELD(reg, 12, 14);
210 int skx_get_node_id(struct skx_dev *d, u8 *id)
214 if (pci_read_config_dword(d->util_all, 0xf4, ®)) {
215 skx_printk(KERN_ERR, "Failed to read node id\n");
219 *id = GET_BITFIELD(reg, 0, 2);
223 static int get_width(u32 mtr)
225 switch (GET_BITFIELD(mtr, 8, 9)) {
237 * We use the per-socket device @cfg->did to count how many sockets are present,
238 * and to detemine which PCI buses are associated with each socket. Allocate
239 * and build the full list of all the skx_dev structures that we need here.
241 int skx_get_all_bus_mappings(struct res_config *cfg, struct list_head **list)
243 struct pci_dev *pdev, *prev;
250 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, cfg->decs_did, prev);
254 d = kzalloc(sizeof(*d), GFP_KERNEL);
260 if (pci_read_config_dword(pdev, cfg->busno_cfg_offset, ®)) {
263 skx_printk(KERN_ERR, "Failed to read bus idx\n");
267 d->bus[0] = GET_BITFIELD(reg, 0, 7);
268 d->bus[1] = GET_BITFIELD(reg, 8, 15);
269 if (cfg->type == SKX) {
270 d->seg = pci_domain_nr(pdev->bus);
271 d->bus[2] = GET_BITFIELD(reg, 16, 23);
272 d->bus[3] = GET_BITFIELD(reg, 24, 31);
274 d->seg = GET_BITFIELD(reg, 16, 23);
277 edac_dbg(2, "busses: 0x%x, 0x%x, 0x%x, 0x%x\n",
278 d->bus[0], d->bus[1], d->bus[2], d->bus[3]);
279 list_add_tail(&d->list, &dev_edac_list);
284 *list = &dev_edac_list;
288 int skx_get_hi_lo(unsigned int did, int off[], u64 *tolm, u64 *tohm)
290 struct pci_dev *pdev;
293 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, did, NULL);
295 edac_dbg(2, "Can't get tolm/tohm\n");
299 if (pci_read_config_dword(pdev, off[0], ®)) {
300 skx_printk(KERN_ERR, "Failed to read tolm\n");
305 if (pci_read_config_dword(pdev, off[1], ®)) {
306 skx_printk(KERN_ERR, "Failed to read lower tohm\n");
311 if (pci_read_config_dword(pdev, off[2], ®)) {
312 skx_printk(KERN_ERR, "Failed to read upper tohm\n");
315 skx_tohm |= (u64)reg << 32;
320 edac_dbg(2, "tolm = 0x%llx tohm = 0x%llx\n", skx_tolm, skx_tohm);
327 static int skx_get_dimm_attr(u32 reg, int lobit, int hibit, int add,
328 int minval, int maxval, const char *name)
330 u32 val = GET_BITFIELD(reg, lobit, hibit);
332 if (val < minval || val > maxval) {
333 edac_dbg(2, "bad %s = %d (raw=0x%x)\n", name, val, reg);
339 #define numrank(reg) skx_get_dimm_attr(reg, 12, 13, 0, 0, 2, "ranks")
340 #define numrow(reg) skx_get_dimm_attr(reg, 2, 4, 12, 1, 6, "rows")
341 #define numcol(reg) skx_get_dimm_attr(reg, 0, 1, 10, 0, 2, "cols")
343 int skx_get_dimm_info(u32 mtr, u32 mcmtr, u32 amap, struct dimm_info *dimm,
344 struct skx_imc *imc, int chan, int dimmno,
345 struct res_config *cfg)
347 int banks, ranks, rows, cols, npages;
351 ranks = numrank(mtr);
353 cols = imc->hbm_mc ? 6 : numcol(mtr);
358 } else if (cfg->support_ddr5 && (amap & 0x8)) {
367 * Compute size in 8-byte (2^3) words, then shift to MiB (2^20)
369 size = ((1ull << (rows + cols + ranks)) * banks) >> (20 - 3);
370 npages = MiB_TO_PAGES(size);
372 edac_dbg(0, "mc#%d: channel %d, dimm %d, %lld MiB (%d pages) bank: %d, rank: %d, row: 0x%x, col: 0x%x\n",
373 imc->mc, chan, dimmno, size, npages,
374 banks, 1 << ranks, rows, cols);
376 imc->chan[chan].dimms[dimmno].close_pg = GET_BITFIELD(mcmtr, 0, 0);
377 imc->chan[chan].dimms[dimmno].bank_xor_enable = GET_BITFIELD(mcmtr, 9, 9);
378 imc->chan[chan].dimms[dimmno].fine_grain_bank = GET_BITFIELD(amap, 0, 0);
379 imc->chan[chan].dimms[dimmno].rowbits = rows;
380 imc->chan[chan].dimms[dimmno].colbits = cols;
382 dimm->nr_pages = npages;
384 dimm->dtype = get_width(mtr);
386 dimm->edac_mode = EDAC_SECDED; /* likely better than this */
389 snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_HBMC#%u_Chan#%u",
390 imc->src_id, imc->lmc, chan);
392 snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u",
393 imc->src_id, imc->lmc, chan, dimmno);
398 int skx_get_nvdimm_info(struct dimm_info *dimm, struct skx_imc *imc,
399 int chan, int dimmno, const char *mod_str)
406 dev_handle = ACPI_NFIT_BUILD_DEVICE_HANDLE(dimmno, chan, imc->lmc,
409 smbios_handle = nfit_get_smbios_id(dev_handle, &flags);
410 if (smbios_handle == -EOPNOTSUPP) {
411 pr_warn_once("%s: Can't find size of NVDIMM. Try enabling CONFIG_ACPI_NFIT\n", mod_str);
415 if (smbios_handle < 0) {
416 skx_printk(KERN_ERR, "Can't find handle for NVDIMM ADR=0x%x\n", dev_handle);
420 if (flags & ACPI_NFIT_MEM_MAP_FAILED) {
421 skx_printk(KERN_ERR, "NVDIMM ADR=0x%x is not mapped\n", dev_handle);
425 size = dmi_memdev_size(smbios_handle);
427 skx_printk(KERN_ERR, "Can't find size for NVDIMM ADR=0x%x/SMBIOS=0x%x\n",
428 dev_handle, smbios_handle);
431 dimm->nr_pages = size >> PAGE_SHIFT;
433 dimm->dtype = DEV_UNKNOWN;
434 dimm->mtype = MEM_NVDIMM;
435 dimm->edac_mode = EDAC_SECDED; /* likely better than this */
437 edac_dbg(0, "mc#%d: channel %d, dimm %d, %llu MiB (%u pages)\n",
438 imc->mc, chan, dimmno, size >> 20, dimm->nr_pages);
440 snprintf(dimm->label, sizeof(dimm->label), "CPU_SrcID#%u_MC#%u_Chan#%u_DIMM#%u",
441 imc->src_id, imc->lmc, chan, dimmno);
443 return (size == 0 || size == ~0ull) ? 0 : 1;
446 int skx_register_mci(struct skx_imc *imc, struct pci_dev *pdev,
447 const char *ctl_name, const char *mod_str,
448 get_dimm_config_f get_dimm_config,
449 struct res_config *cfg)
451 struct mem_ctl_info *mci;
452 struct edac_mc_layer layers[2];
456 /* Allocate a new MC control structure */
457 layers[0].type = EDAC_MC_LAYER_CHANNEL;
458 layers[0].size = NUM_CHANNELS;
459 layers[0].is_virt_csrow = false;
460 layers[1].type = EDAC_MC_LAYER_SLOT;
461 layers[1].size = NUM_DIMMS;
462 layers[1].is_virt_csrow = true;
463 mci = edac_mc_alloc(imc->mc, ARRAY_SIZE(layers), layers,
464 sizeof(struct skx_pvt));
469 edac_dbg(0, "MC#%d: mci = %p\n", imc->mc, mci);
471 /* Associate skx_dev and mci for future usage */
476 mci->ctl_name = kasprintf(GFP_KERNEL, "%s#%d IMC#%d", ctl_name,
477 imc->node_id, imc->lmc);
478 if (!mci->ctl_name) {
483 mci->mtype_cap = MEM_FLAG_DDR4 | MEM_FLAG_NVDIMM;
484 if (cfg->support_ddr5)
485 mci->mtype_cap |= MEM_FLAG_DDR5;
486 mci->edac_ctl_cap = EDAC_FLAG_NONE;
487 mci->edac_cap = EDAC_FLAG_NONE;
488 mci->mod_name = mod_str;
489 mci->dev_name = pci_name(pdev);
490 mci->ctl_page_to_phys = NULL;
492 rc = get_dimm_config(mci, cfg);
496 /* Record ptr to the generic device */
497 mci->pdev = &pdev->dev;
499 /* Add this new MC control structure to EDAC's list of MCs */
500 if (unlikely(edac_mc_add_mc(mci))) {
501 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
509 kfree(mci->ctl_name);
516 static void skx_unregister_mci(struct skx_imc *imc)
518 struct mem_ctl_info *mci = imc->mci;
523 edac_dbg(0, "MC%d: mci = %p\n", imc->mc, mci);
525 /* Remove MC sysfs nodes */
526 edac_mc_del_mc(mci->pdev);
528 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
529 kfree(mci->ctl_name);
533 static void skx_mce_output_error(struct mem_ctl_info *mci,
535 struct decoded_addr *res)
537 enum hw_event_mc_err_type tp_event;
539 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
540 bool overflow = GET_BITFIELD(m->status, 62, 62);
541 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
542 bool scrub_err = false;
545 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
546 u32 mscod = GET_BITFIELD(m->status, 16, 31);
547 u32 errcode = GET_BITFIELD(m->status, 0, 15);
548 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
550 recoverable = GET_BITFIELD(m->status, 56, 56);
552 if (uncorrected_error) {
555 tp_event = HW_EVENT_ERR_UNCORRECTED;
557 tp_event = HW_EVENT_ERR_FATAL;
560 tp_event = HW_EVENT_ERR_CORRECTED;
565 optype = "generic undef request error";
568 optype = "memory read error";
571 optype = "memory write error";
574 optype = "addr/cmd error";
577 optype = "memory scrubbing error";
585 if (res->decoded_by_adxl) {
586 len = snprintf(skx_msg, MSG_SIZE, "%s%s err_code:0x%04x:0x%04x %s",
587 overflow ? " OVERFLOW" : "",
588 (uncorrected_error && recoverable) ? " recoverable" : "",
589 mscod, errcode, adxl_msg);
591 len = snprintf(skx_msg, MSG_SIZE,
592 "%s%s err_code:0x%04x:0x%04x ProcessorSocketId:0x%x MemoryControllerId:0x%x PhysicalRankId:0x%x Row:0x%x Column:0x%x Bank:0x%x BankGroup:0x%x",
593 overflow ? " OVERFLOW" : "",
594 (uncorrected_error && recoverable) ? " recoverable" : "",
596 res->socket, res->imc, res->rank,
597 res->row, res->column, res->bank_address, res->bank_group);
600 if (skx_show_retry_rd_err_log)
601 skx_show_retry_rd_err_log(res, skx_msg + len, MSG_SIZE - len, scrub_err);
603 edac_dbg(0, "%s\n", skx_msg);
605 /* Call the helper to output message */
606 edac_mc_handle_error(tp_event, mci, core_err_cnt,
607 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
608 res->channel, res->dimm, -1,
612 static bool skx_error_in_1st_level_mem(const struct mce *m)
616 if (!skx_mem_cfg_2lm)
619 errcode = GET_BITFIELD(m->status, 0, 15) & MCACOD_MEM_ERR_MASK;
621 return errcode == MCACOD_EXT_MEM_ERR;
624 static bool skx_error_in_mem(const struct mce *m)
628 errcode = GET_BITFIELD(m->status, 0, 15) & MCACOD_MEM_ERR_MASK;
630 return (errcode == MCACOD_MEM_CTL_ERR || errcode == MCACOD_EXT_MEM_ERR);
633 int skx_mce_check_error(struct notifier_block *nb, unsigned long val,
636 struct mce *mce = (struct mce *)data;
637 struct decoded_addr res;
638 struct mem_ctl_info *mci;
641 if (mce->kflags & MCE_HANDLED_CEC)
644 /* Ignore unless this is memory related with an address */
645 if (!skx_error_in_mem(mce) || !(mce->status & MCI_STATUS_ADDRV))
648 memset(&res, 0, sizeof(res));
650 res.addr = mce->addr & MCI_ADDR_PHYSADDR;
652 /* Try driver decoder first */
653 if (!(driver_decode && driver_decode(&res))) {
654 /* Then try firmware decoder (ACPI DSM methods) */
655 if (!(adxl_component_count && skx_adxl_decode(&res, skx_error_in_1st_level_mem(mce))))
659 mci = res.dev->imc[res.imc].mci;
664 if (mce->mcgstatus & MCG_STATUS_MCIP)
669 skx_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
671 skx_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: 0x%llx "
672 "Bank %d: 0x%llx\n", mce->extcpu, type,
673 mce->mcgstatus, mce->bank, mce->status);
674 skx_mc_printk(mci, KERN_DEBUG, "TSC 0x%llx ", mce->tsc);
675 skx_mc_printk(mci, KERN_DEBUG, "ADDR 0x%llx ", mce->addr);
676 skx_mc_printk(mci, KERN_DEBUG, "MISC 0x%llx ", mce->misc);
678 skx_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:0x%x TIME %llu SOCKET "
679 "%u APIC 0x%x\n", mce->cpuvendor, mce->cpuid,
680 mce->time, mce->socketid, mce->apicid);
682 skx_mce_output_error(mci, mce, &res);
684 mce->kflags |= MCE_HANDLED_EDAC;
688 void skx_remove(void)
691 struct skx_dev *d, *tmp;
695 list_for_each_entry_safe(d, tmp, &dev_edac_list, list) {
697 for (i = 0; i < NUM_IMC; i++) {
699 skx_unregister_mci(&d->imc[i]);
702 pci_dev_put(d->imc[i].mdev);
705 iounmap(d->imc[i].mbase);
707 for (j = 0; j < NUM_CHANNELS; j++) {
708 if (d->imc[i].chan[j].cdev)
709 pci_dev_put(d->imc[i].chan[j].cdev);
713 pci_dev_put(d->util_all);
715 pci_dev_put(d->pcu_cr3);
717 pci_dev_put(d->sad_all);
719 pci_dev_put(d->uracu);