1 // SPDX-License-Identifier: GPL-2.0-only
3 * GHES/EDAC Linux driver
5 * Copyright (c) 2013 by Mauro Carvalho Chehab
7 * Red Hat Inc. https://www.redhat.com
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <acpi/ghes.h>
13 #include <linux/edac.h>
14 #include <linux/dmi.h>
15 #include "edac_module.h"
16 #include <ras/ras_event.h>
18 #define OTHER_DETAIL_LEN 400
21 struct mem_ctl_info *mci;
23 /* Buffers for the error handling routine */
24 char other_detail[OTHER_DETAIL_LEN];
28 static refcount_t ghes_refcount = REFCOUNT_INIT(0);
31 * Access to ghes_pvt must be protected by ghes_lock. The spinlock
32 * also provides the necessary (implicit) memory barrier for the SMP
33 * case to make the pointer visible on another CPU.
35 static struct ghes_pvt *ghes_pvt;
38 * This driver's representation of the system hardware, as collected
41 static struct ghes_hw_desc {
43 struct dimm_info *dimms;
46 /* GHES registration mutex */
47 static DEFINE_MUTEX(ghes_reg_mutex);
50 * Sync with other, potentially concurrent callers of
51 * ghes_edac_report_mem_error(). We don't know what the
52 * "inventive" firmware would do.
54 static DEFINE_SPINLOCK(ghes_lock);
56 /* "ghes_edac.force_load=1" skips the platform check */
57 static bool __read_mostly force_load;
58 module_param(force_load, bool, 0);
60 static bool system_scanned;
62 /* Memory Device - Type 17 of SMBIOS spec */
63 struct memdev_dmi_entry {
67 u16 phys_mem_array_handle;
68 u16 mem_err_info_handle;
85 u16 conf_mem_clk_speed;
86 } __attribute__((__packed__));
88 static struct dimm_info *find_dimm_by_handle(struct mem_ctl_info *mci, u16 handle)
90 struct dimm_info *dimm;
92 mci_for_each_dimm(mci, dimm) {
93 if (dimm->smbios_handle == handle)
100 static void dimm_setup_label(struct dimm_info *dimm, u16 handle)
102 const char *bank = NULL, *device = NULL;
104 dmi_memdev_name(handle, &bank, &device);
106 /* both strings must be non-zero */
107 if (bank && *bank && device && *device)
108 snprintf(dimm->label, sizeof(dimm->label), "%s %s", bank, device);
111 static void assign_dmi_dimm_info(struct dimm_info *dimm, struct memdev_dmi_entry *entry)
113 u16 rdr_mask = BIT(7) | BIT(13);
115 if (entry->size == 0xffff) {
116 pr_info("Can't get DIMM%i size\n", dimm->idx);
117 dimm->nr_pages = MiB_TO_PAGES(32);/* Unknown */
118 } else if (entry->size == 0x7fff) {
119 dimm->nr_pages = MiB_TO_PAGES(entry->extended_size);
121 if (entry->size & BIT(15))
122 dimm->nr_pages = MiB_TO_PAGES((entry->size & 0x7fff) << 10);
124 dimm->nr_pages = MiB_TO_PAGES(entry->size);
127 switch (entry->memory_type) {
129 if (entry->type_detail & BIT(13))
130 dimm->mtype = MEM_RDDR;
132 dimm->mtype = MEM_DDR;
135 if (entry->type_detail & BIT(13))
136 dimm->mtype = MEM_RDDR2;
138 dimm->mtype = MEM_DDR2;
141 dimm->mtype = MEM_FB_DDR2;
144 if (entry->type_detail & BIT(12))
145 dimm->mtype = MEM_NVDIMM;
146 else if (entry->type_detail & BIT(13))
147 dimm->mtype = MEM_RDDR3;
149 dimm->mtype = MEM_DDR3;
152 if (entry->type_detail & BIT(12))
153 dimm->mtype = MEM_NVDIMM;
154 else if (entry->type_detail & BIT(13))
155 dimm->mtype = MEM_RDDR4;
157 dimm->mtype = MEM_DDR4;
160 if (entry->type_detail & BIT(6))
161 dimm->mtype = MEM_RMBS;
162 else if ((entry->type_detail & rdr_mask) == rdr_mask)
163 dimm->mtype = MEM_RDR;
164 else if (entry->type_detail & BIT(7))
165 dimm->mtype = MEM_SDR;
166 else if (entry->type_detail & BIT(9))
167 dimm->mtype = MEM_EDO;
169 dimm->mtype = MEM_UNKNOWN;
173 * Actually, we can only detect if the memory has bits for
176 if (entry->total_width == entry->data_width)
177 dimm->edac_mode = EDAC_NONE;
179 dimm->edac_mode = EDAC_SECDED;
181 dimm->dtype = DEV_UNKNOWN;
182 dimm->grain = 128; /* Likely, worse case */
184 dimm_setup_label(dimm, entry->handle);
186 if (dimm->nr_pages) {
187 edac_dbg(1, "DIMM%i: %s size = %d MB%s\n",
188 dimm->idx, edac_mem_types[dimm->mtype],
189 PAGES_TO_MiB(dimm->nr_pages),
190 (dimm->edac_mode != EDAC_NONE) ? "(ECC)" : "");
191 edac_dbg(2, "\ttype %d, detail 0x%02x, width %d(total %d)\n",
192 entry->memory_type, entry->type_detail,
193 entry->total_width, entry->data_width);
196 dimm->smbios_handle = entry->handle;
199 static void enumerate_dimms(const struct dmi_header *dh, void *arg)
201 struct memdev_dmi_entry *entry = (struct memdev_dmi_entry *)dh;
202 struct ghes_hw_desc *hw = (struct ghes_hw_desc *)arg;
205 if (dh->type != DMI_ENTRY_MEM_DEVICE)
208 /* Enlarge the array with additional 16 */
209 if (!hw->num_dimms || !(hw->num_dimms % 16)) {
210 struct dimm_info *new;
212 new = krealloc_array(hw->dimms, hw->num_dimms + 16,
213 sizeof(struct dimm_info), GFP_KERNEL);
222 d = &hw->dimms[hw->num_dimms];
223 d->idx = hw->num_dimms;
225 assign_dmi_dimm_info(d, entry);
230 static void ghes_scan_system(void)
235 dmi_walk(enumerate_dimms, &ghes_hw);
237 system_scanned = true;
240 static int print_mem_error_other_detail(const struct cper_sec_mem_err *mem, char *msg,
241 const char *location, unsigned int len)
251 n += scnprintf(msg + n, len - n, "APEI location: %s ", location);
253 if (!(mem->validation_bits & CPER_MEM_VALID_ERROR_STATUS))
256 n += scnprintf(msg + n, len - n, "status(0x%016llx): ", mem->error_status);
257 n += scnprintf(msg + n, len - n, "%s ", cper_mem_err_status_str(mem->error_status));
265 void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)
267 struct cper_mem_err_compact cmem;
268 struct edac_raw_error_desc *e;
269 struct mem_ctl_info *mci;
270 struct ghes_pvt *pvt;
275 * We can do the locking below because GHES defers error processing
276 * from NMI to IRQ context. Whenever that changes, we'd at least
279 if (WARN_ON_ONCE(in_nmi()))
282 spin_lock_irqsave(&ghes_lock, flags);
289 e = &mci->error_desc;
291 /* Cleans the error report buffer */
292 memset(e, 0, sizeof (*e));
296 e->other_detail = pvt->other_detail;
300 *pvt->other_detail = '\0';
304 case GHES_SEV_CORRECTED:
305 e->type = HW_EVENT_ERR_CORRECTED;
307 case GHES_SEV_RECOVERABLE:
308 e->type = HW_EVENT_ERR_UNCORRECTED;
311 e->type = HW_EVENT_ERR_FATAL;
315 e->type = HW_EVENT_ERR_INFO;
318 edac_dbg(1, "error validation_bits: 0x%08llx\n",
319 (long long)mem_err->validation_bits);
321 /* Error type, mapped on e->msg */
322 if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_TYPE) {
323 u8 etype = mem_err->error_type;
326 p += snprintf(p, sizeof(pvt->msg), "%s", cper_mem_err_type_str(etype));
328 strcpy(pvt->msg, "unknown error");
332 if (mem_err->validation_bits & CPER_MEM_VALID_PA) {
333 e->page_frame_number = PHYS_PFN(mem_err->physical_addr);
334 e->offset_in_page = offset_in_page(mem_err->physical_addr);
338 if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
339 e->grain = ~mem_err->physical_addr_mask + 1;
341 /* Memory error location, mapped on e->location */
343 cper_mem_err_pack(mem_err, &cmem);
344 p += cper_mem_err_location(&cmem, p);
346 if (mem_err->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
347 struct dimm_info *dimm;
349 p += cper_dimm_err_location(&cmem, p);
350 dimm = find_dimm_by_handle(mci, mem_err->mem_dev_handle);
352 e->top_layer = dimm->idx;
353 strcpy(e->label, dimm->label);
360 strcpy(e->label, "unknown memory");
362 /* All other fields are mapped on e->other_detail */
363 p = pvt->other_detail;
364 p += print_mem_error_other_detail(mem_err, p, e->location, OTHER_DETAIL_LEN);
365 if (p > pvt->other_detail)
368 edac_raw_mc_handle_error(e);
371 spin_unlock_irqrestore(&ghes_lock, flags);
375 * Known systems that are safe to enable this module.
377 static struct acpi_platform_list plat_list[] = {
378 {"HPE ", "Server ", 0, ACPI_SIG_FADT, all_versions},
382 int ghes_edac_register(struct ghes *ghes, struct device *dev)
385 struct mem_ctl_info *mci;
386 struct ghes_pvt *pvt;
387 struct edac_mc_layer layers[1];
392 if (IS_ENABLED(CONFIG_X86)) {
393 /* Check if safe to enable on this system */
394 idx = acpi_match_platform_list(plat_list);
395 if (!force_load && idx < 0)
402 /* finish another registration/unregistration instance first */
403 mutex_lock(&ghes_reg_mutex);
406 * We have only one logical memory controller to which all DIMMs belong.
408 if (refcount_inc_not_zero(&ghes_refcount))
413 /* Check if we've got a bogus BIOS */
414 if (!ghes_hw.num_dimms) {
416 ghes_hw.num_dimms = 1;
419 layers[0].type = EDAC_MC_LAYER_ALL_MEM;
420 layers[0].size = ghes_hw.num_dimms;
421 layers[0].is_virt_csrow = true;
423 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(struct ghes_pvt));
425 pr_info("Can't allocate memory for EDAC data\n");
434 mci->mtype_cap = MEM_FLAG_EMPTY;
435 mci->edac_ctl_cap = EDAC_FLAG_NONE;
436 mci->edac_cap = EDAC_FLAG_NONE;
437 mci->mod_name = "ghes_edac.c";
438 mci->ctl_name = "ghes_edac";
439 mci->dev_name = "ghes";
442 pr_info("This system has a very crappy BIOS: It doesn't even list the DIMMS.\n");
443 pr_info("Its SMBIOS info is wrong. It is doubtful that the error report would\n");
444 pr_info("work on such system. Use this driver with caution\n");
445 } else if (idx < 0) {
446 pr_info("This EDAC driver relies on BIOS to enumerate memory and get error reports.\n");
447 pr_info("Unfortunately, not all BIOSes reflect the memory layout correctly.\n");
448 pr_info("So, the end result of using this driver varies from vendor to vendor.\n");
449 pr_info("If you find incorrect reports, please contact your hardware vendor\n");
450 pr_info("to correct its BIOS.\n");
451 pr_info("This system has %d DIMM sockets.\n", ghes_hw.num_dimms);
455 struct dimm_info *src, *dst;
458 mci_for_each_dimm(mci, dst) {
459 src = &ghes_hw.dimms[i];
462 dst->smbios_handle = src->smbios_handle;
463 dst->nr_pages = src->nr_pages;
464 dst->mtype = src->mtype;
465 dst->edac_mode = src->edac_mode;
466 dst->dtype = src->dtype;
467 dst->grain = src->grain;
470 * If no src->label, preserve default label assigned
473 if (strlen(src->label))
474 memcpy(dst->label, src->label, sizeof(src->label));
480 struct dimm_info *dimm = edac_get_dimm(mci, 0, 0, 0);
484 dimm->mtype = MEM_UNKNOWN;
485 dimm->dtype = DEV_UNKNOWN;
486 dimm->edac_mode = EDAC_SECDED;
489 rc = edac_mc_add_mc(mci);
491 pr_info("Can't register with the EDAC core\n");
497 spin_lock_irqsave(&ghes_lock, flags);
499 spin_unlock_irqrestore(&ghes_lock, flags);
501 /* only set on success */
502 refcount_set(&ghes_refcount, 1);
506 /* Not needed anymore */
507 kfree(ghes_hw.dimms);
508 ghes_hw.dimms = NULL;
510 mutex_unlock(&ghes_reg_mutex);
515 void ghes_edac_unregister(struct ghes *ghes)
517 struct mem_ctl_info *mci;
523 mutex_lock(&ghes_reg_mutex);
525 system_scanned = false;
526 memset(&ghes_hw, 0, sizeof(struct ghes_hw_desc));
528 if (!refcount_dec_and_test(&ghes_refcount))
532 * Wait for the irq handler being finished.
534 spin_lock_irqsave(&ghes_lock, flags);
535 mci = ghes_pvt ? ghes_pvt->mci : NULL;
537 spin_unlock_irqrestore(&ghes_lock, flags);
542 mci = edac_mc_del_mc(mci->pdev);
547 mutex_unlock(&ghes_reg_mutex);