Merge tag 'mm-stable-2022-08-03' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / edac / ghes_edac.c
CommitLineData
12237550 1// SPDX-License-Identifier: GPL-2.0-only
77c5f5d2
MCC
2/*
3 * GHES/EDAC Linux driver
4 *
37e59f87 5 * Copyright (c) 2013 by Mauro Carvalho Chehab
77c5f5d2 6 *
7d4c1ea2 7 * Red Hat Inc. https://www.redhat.com
77c5f5d2
MCC
8 */
9
d2a68566
MCC
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
77c5f5d2
MCC
12#include <acpi/ghes.h>
13#include <linux/edac.h>
32fa1f53 14#include <linux/dmi.h>
78d88e8a 15#include "edac_module.h"
8ae8f50a 16#include <ras/ras_event.h>
77c5f5d2 17
ed27b5df
SX
18#define OTHER_DETAIL_LEN 400
19
b001694d 20struct ghes_pvt {
77c5f5d2 21 struct mem_ctl_info *mci;
689c9cd8
MCC
22
23 /* Buffers for the error handling routine */
ed27b5df 24 char other_detail[OTHER_DETAIL_LEN];
689c9cd8 25 char msg[80];
77c5f5d2
MCC
26};
27
23f61b9f
RR
28static refcount_t ghes_refcount = REFCOUNT_INIT(0);
29
30/*
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.
34 */
b001694d 35static struct ghes_pvt *ghes_pvt;
77c5f5d2 36
b9cae277
BP
37/*
38 * This driver's representation of the system hardware, as collected
39 * from DMI.
40 */
815fad6e 41static struct ghes_hw_desc {
b9cae277
BP
42 int num_dimms;
43 struct dimm_info *dimms;
44} ghes_hw;
45
23f61b9f
RR
46/* GHES registration mutex */
47static DEFINE_MUTEX(ghes_reg_mutex);
48
0fe5f281
BP
49/*
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.
53 */
54static DEFINE_SPINLOCK(ghes_lock);
d2a68566 55
5deed6b6
TK
56/* "ghes_edac.force_load=1" skips the platform check */
57static bool __read_mostly force_load;
58module_param(force_load, bool, 0);
59
b972fdba
SJ
60static bool system_scanned;
61
32fa1f53
MCC
62/* Memory Device - Type 17 of SMBIOS spec */
63struct memdev_dmi_entry {
64 u8 type;
65 u8 length;
66 u16 handle;
67 u16 phys_mem_array_handle;
68 u16 mem_err_info_handle;
69 u16 total_width;
70 u16 data_width;
71 u16 size;
72 u8 form_factor;
73 u8 device_set;
74 u8 device_locator;
75 u8 bank_locator;
76 u8 memory_type;
77 u16 type_detail;
78 u16 speed;
79 u8 manufacturer;
80 u8 serial_number;
81 u8 asset_tag;
82 u8 part_number;
83 u8 attributes;
84 u32 extended_size;
85 u16 conf_mem_clk_speed;
86} __attribute__((__packed__));
87
cb51a371 88static struct dimm_info *find_dimm_by_handle(struct mem_ctl_info *mci, u16 handle)
c798c88f 89{
c498afaf 90 struct dimm_info *dimm;
c798c88f 91
c498afaf
RR
92 mci_for_each_dimm(mci, dimm) {
93 if (dimm->smbios_handle == handle)
cb51a371 94 return dimm;
c798c88f 95 }
c498afaf 96
cb51a371
RR
97 return NULL;
98}
99
100static void dimm_setup_label(struct dimm_info *dimm, u16 handle)
101{
102 const char *bank = NULL, *device = NULL;
103
104 dmi_memdev_name(handle, &bank, &device);
105
5e2805d5
TK
106 /*
107 * Set to a NULL string when both bank and device are zero. In this case,
108 * the label assigned by default will be preserved.
109 */
110 snprintf(dimm->label, sizeof(dimm->label), "%s%s%s",
111 (bank && *bank) ? bank : "",
112 (bank && *bank && device && *device) ? " " : "",
113 (device && *device) ? device : "");
c798c88f
FW
114}
115
b9cae277 116static void assign_dmi_dimm_info(struct dimm_info *dimm, struct memdev_dmi_entry *entry)
32fa1f53 117{
b9cae277 118 u16 rdr_mask = BIT(7) | BIT(13);
32fa1f53 119
b9cae277
BP
120 if (entry->size == 0xffff) {
121 pr_info("Can't get DIMM%i size\n", dimm->idx);
122 dimm->nr_pages = MiB_TO_PAGES(32);/* Unknown */
123 } else if (entry->size == 0x7fff) {
124 dimm->nr_pages = MiB_TO_PAGES(entry->extended_size);
125 } else {
126 if (entry->size & BIT(15))
127 dimm->nr_pages = MiB_TO_PAGES((entry->size & 0x7fff) << 10);
128 else
129 dimm->nr_pages = MiB_TO_PAGES(entry->size);
130 }
32fa1f53 131
b9cae277
BP
132 switch (entry->memory_type) {
133 case 0x12:
134 if (entry->type_detail & BIT(13))
135 dimm->mtype = MEM_RDDR;
136 else
137 dimm->mtype = MEM_DDR;
138 break;
139 case 0x13:
140 if (entry->type_detail & BIT(13))
141 dimm->mtype = MEM_RDDR2;
32fa1f53 142 else
b9cae277
BP
143 dimm->mtype = MEM_DDR2;
144 break;
145 case 0x14:
146 dimm->mtype = MEM_FB_DDR2;
147 break;
148 case 0x18:
149 if (entry->type_detail & BIT(12))
150 dimm->mtype = MEM_NVDIMM;
151 else if (entry->type_detail & BIT(13))
152 dimm->mtype = MEM_RDDR3;
153 else
154 dimm->mtype = MEM_DDR3;
155 break;
156 case 0x1a:
157 if (entry->type_detail & BIT(12))
158 dimm->mtype = MEM_NVDIMM;
159 else if (entry->type_detail & BIT(13))
160 dimm->mtype = MEM_RDDR4;
161 else
162 dimm->mtype = MEM_DDR4;
163 break;
164 default:
165 if (entry->type_detail & BIT(6))
166 dimm->mtype = MEM_RMBS;
167 else if ((entry->type_detail & rdr_mask) == rdr_mask)
168 dimm->mtype = MEM_RDR;
169 else if (entry->type_detail & BIT(7))
170 dimm->mtype = MEM_SDR;
171 else if (entry->type_detail & BIT(9))
172 dimm->mtype = MEM_EDO;
173 else
174 dimm->mtype = MEM_UNKNOWN;
175 }
32fa1f53 176
b9cae277
BP
177 /*
178 * Actually, we can only detect if the memory has bits for
179 * checksum or not
180 */
181 if (entry->total_width == entry->data_width)
182 dimm->edac_mode = EDAC_NONE;
183 else
184 dimm->edac_mode = EDAC_SECDED;
185
186 dimm->dtype = DEV_UNKNOWN;
187 dimm->grain = 128; /* Likely, worse case */
32fa1f53 188
b9cae277 189 dimm_setup_label(dimm, entry->handle);
c798c88f 190
b9cae277
BP
191 if (dimm->nr_pages) {
192 edac_dbg(1, "DIMM%i: %s size = %d MB%s\n",
193 dimm->idx, edac_mem_types[dimm->mtype],
194 PAGES_TO_MiB(dimm->nr_pages),
195 (dimm->edac_mode != EDAC_NONE) ? "(ECC)" : "");
196 edac_dbg(2, "\ttype %d, detail 0x%02x, width %d(total %d)\n",
197 entry->memory_type, entry->type_detail,
198 entry->total_width, entry->data_width);
32fa1f53 199 }
b9cae277
BP
200
201 dimm->smbios_handle = entry->handle;
202}
203
204static void enumerate_dimms(const struct dmi_header *dh, void *arg)
205{
206 struct memdev_dmi_entry *entry = (struct memdev_dmi_entry *)dh;
207 struct ghes_hw_desc *hw = (struct ghes_hw_desc *)arg;
208 struct dimm_info *d;
209
210 if (dh->type != DMI_ENTRY_MEM_DEVICE)
211 return;
212
213 /* Enlarge the array with additional 16 */
214 if (!hw->num_dimms || !(hw->num_dimms % 16)) {
215 struct dimm_info *new;
216
af11be05
BG
217 new = krealloc_array(hw->dimms, hw->num_dimms + 16,
218 sizeof(struct dimm_info), GFP_KERNEL);
b9cae277
BP
219 if (!new) {
220 WARN_ON_ONCE(1);
221 return;
222 }
223
224 hw->dimms = new;
225 }
226
227 d = &hw->dimms[hw->num_dimms];
228 d->idx = hw->num_dimms;
229
230 assign_dmi_dimm_info(d, entry);
231
232 hw->num_dimms++;
233}
234
235static void ghes_scan_system(void)
236{
b972fdba 237 if (system_scanned)
b9cae277
BP
238 return;
239
240 dmi_walk(enumerate_dimms, &ghes_hw);
241
b972fdba 242 system_scanned = true;
32fa1f53
MCC
243}
244
ed27b5df
SX
245static int print_mem_error_other_detail(const struct cper_sec_mem_err *mem, char *msg,
246 const char *location, unsigned int len)
247{
248 u32 n;
249
250 if (!msg)
251 return 0;
252
253 n = 0;
254 len -= 1;
255
256 n += scnprintf(msg + n, len - n, "APEI location: %s ", location);
257
258 if (!(mem->validation_bits & CPER_MEM_VALID_ERROR_STATUS))
259 goto out;
260
261 n += scnprintf(msg + n, len - n, "status(0x%016llx): ", mem->error_status);
262 n += scnprintf(msg + n, len - n, "%s ", cper_mem_err_status_str(mem->error_status));
263
264out:
265 msg[n] = '\0';
266
267 return n;
268}
269
305d0e00 270void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)
77c5f5d2 271{
ed27b5df 272 struct cper_mem_err_compact cmem;
f04c62a7
MCC
273 struct edac_raw_error_desc *e;
274 struct mem_ctl_info *mci;
b001694d 275 struct ghes_pvt *pvt;
0fe5f281 276 unsigned long flags;
689c9cd8 277 char *p;
f04c62a7 278
0fe5f281
BP
279 /*
280 * We can do the locking below because GHES defers error processing
281 * from NMI to IRQ context. Whenever that changes, we'd at least
282 * know.
283 */
284 if (WARN_ON_ONCE(in_nmi()))
285 return;
286
287 spin_lock_irqsave(&ghes_lock, flags);
288
23f61b9f
RR
289 pvt = ghes_pvt;
290 if (!pvt)
291 goto unlock;
292
f04c62a7
MCC
293 mci = pvt->mci;
294 e = &mci->error_desc;
295
296 /* Cleans the error report buffer */
297 memset(e, 0, sizeof (*e));
298 e->error_count = 1;
7088e29e 299 e->grain = 1;
689c9cd8
MCC
300 e->msg = pvt->msg;
301 e->other_detail = pvt->other_detail;
302 e->top_layer = -1;
303 e->mid_layer = -1;
304 e->low_layer = -1;
305 *pvt->other_detail = '\0';
306 *pvt->msg = '\0';
f04c62a7
MCC
307
308 switch (sev) {
309 case GHES_SEV_CORRECTED:
672ef0e5 310 e->type = HW_EVENT_ERR_CORRECTED;
f04c62a7
MCC
311 break;
312 case GHES_SEV_RECOVERABLE:
672ef0e5 313 e->type = HW_EVENT_ERR_UNCORRECTED;
f04c62a7
MCC
314 break;
315 case GHES_SEV_PANIC:
672ef0e5 316 e->type = HW_EVENT_ERR_FATAL;
f04c62a7
MCC
317 break;
318 default:
319 case GHES_SEV_NO:
672ef0e5 320 e->type = HW_EVENT_ERR_INFO;
f04c62a7
MCC
321 }
322
689c9cd8
MCC
323 edac_dbg(1, "error validation_bits: 0x%08llx\n",
324 (long long)mem_err->validation_bits);
325
326 /* Error type, mapped on e->msg */
327 if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_TYPE) {
ed27b5df
SX
328 u8 etype = mem_err->error_type;
329
689c9cd8 330 p = pvt->msg;
ed27b5df 331 p += snprintf(p, sizeof(pvt->msg), "%s", cper_mem_err_type_str(etype));
689c9cd8
MCC
332 } else {
333 strcpy(pvt->msg, "unknown error");
334 }
335
336 /* Error address */
147de147 337 if (mem_err->validation_bits & CPER_MEM_VALID_PA) {
7c104931
RR
338 e->page_frame_number = PHYS_PFN(mem_err->physical_addr);
339 e->offset_in_page = offset_in_page(mem_err->physical_addr);
689c9cd8
MCC
340 }
341
342 /* Error grain */
147de147 343 if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
7088e29e 344 e->grain = ~mem_err->physical_addr_mask + 1;
689c9cd8
MCC
345
346 /* Memory error location, mapped on e->location */
347 p = e->location;
ed27b5df
SX
348 cper_mem_err_pack(mem_err, &cmem);
349 p += cper_mem_err_location(&cmem, p);
350
56507694 351 if (mem_err->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
cb51a371 352 struct dimm_info *dimm;
c798c88f 353
ed27b5df 354 p += cper_dimm_err_location(&cmem, p);
cb51a371
RR
355 dimm = find_dimm_by_handle(mci, mem_err->mem_dev_handle);
356 if (dimm) {
357 e->top_layer = dimm->idx;
358 strcpy(e->label, dimm->label);
359 }
56507694 360 }
689c9cd8
MCC
361 if (p > e->location)
362 *(p - 1) = '\0';
363
cb51a371
RR
364 if (!*e->label)
365 strcpy(e->label, "unknown memory");
366
689c9cd8
MCC
367 /* All other fields are mapped on e->other_detail */
368 p = pvt->other_detail;
ed27b5df 369 p += print_mem_error_other_detail(mem_err, p, e->location, OTHER_DETAIL_LEN);
689c9cd8
MCC
370 if (p > pvt->other_detail)
371 *(p - 1) = '\0';
f04c62a7 372
91b327f6 373 edac_raw_mc_handle_error(e);
23f61b9f
RR
374
375unlock:
0fe5f281 376 spin_unlock_irqrestore(&ghes_lock, flags);
77c5f5d2 377}
77c5f5d2 378
5deed6b6
TK
379/*
380 * Known systems that are safe to enable this module.
381 */
382static struct acpi_platform_list plat_list[] = {
383 {"HPE ", "Server ", 0, ACPI_SIG_FADT, all_versions},
384 { } /* End */
385};
386
77c5f5d2
MCC
387int ghes_edac_register(struct ghes *ghes, struct device *dev)
388{
32fa1f53 389 bool fake = false;
77c5f5d2 390 struct mem_ctl_info *mci;
b001694d 391 struct ghes_pvt *pvt;
77c5f5d2 392 struct edac_mc_layer layers[1];
23f61b9f 393 unsigned long flags;
eaa3a1d4 394 int idx = -1;
b9cae277 395 int rc = 0;
5deed6b6 396
eaa3a1d4
BP
397 if (IS_ENABLED(CONFIG_X86)) {
398 /* Check if safe to enable on this system */
399 idx = acpi_match_platform_list(plat_list);
400 if (!force_load && idx < 0)
401 return -ENODEV;
402 } else {
251c54ea 403 force_load = true;
eaa3a1d4
BP
404 idx = 0;
405 }
32fa1f53 406
23f61b9f
RR
407 /* finish another registration/unregistration instance first */
408 mutex_lock(&ghes_reg_mutex);
409
0fe5f281
BP
410 /*
411 * We have only one logical memory controller to which all DIMMs belong.
412 */
23f61b9f
RR
413 if (refcount_inc_not_zero(&ghes_refcount))
414 goto unlock;
0fe5f281 415
b9cae277 416 ghes_scan_system();
32fa1f53
MCC
417
418 /* Check if we've got a bogus BIOS */
b9cae277 419 if (!ghes_hw.num_dimms) {
32fa1f53 420 fake = true;
b9cae277 421 ghes_hw.num_dimms = 1;
32fa1f53 422 }
77c5f5d2
MCC
423
424 layers[0].type = EDAC_MC_LAYER_ALL_MEM;
b9cae277 425 layers[0].size = ghes_hw.num_dimms;
77c5f5d2
MCC
426 layers[0].is_virt_csrow = true;
427
b001694d 428 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(struct ghes_pvt));
77c5f5d2 429 if (!mci) {
d2a68566 430 pr_info("Can't allocate memory for EDAC data\n");
23f61b9f
RR
431 rc = -ENOMEM;
432 goto unlock;
77c5f5d2
MCC
433 }
434
23f61b9f 435 pvt = mci->pvt_info;
23f61b9f 436 pvt->mci = mci;
77c5f5d2 437
0fe5f281 438 mci->pdev = dev;
77c5f5d2
MCC
439 mci->mtype_cap = MEM_FLAG_EMPTY;
440 mci->edac_ctl_cap = EDAC_FLAG_NONE;
441 mci->edac_cap = EDAC_FLAG_NONE;
442 mci->mod_name = "ghes_edac.c";
77c5f5d2
MCC
443 mci->ctl_name = "ghes_edac";
444 mci->dev_name = "ghes";
445
5deed6b6
TK
446 if (fake) {
447 pr_info("This system has a very crappy BIOS: It doesn't even list the DIMMS.\n");
448 pr_info("Its SMBIOS info is wrong. It is doubtful that the error report would\n");
449 pr_info("work on such system. Use this driver with caution\n");
450 } else if (idx < 0) {
0fe5f281
BP
451 pr_info("This EDAC driver relies on BIOS to enumerate memory and get error reports.\n");
452 pr_info("Unfortunately, not all BIOSes reflect the memory layout correctly.\n");
453 pr_info("So, the end result of using this driver varies from vendor to vendor.\n");
454 pr_info("If you find incorrect reports, please contact your hardware vendor\n");
455 pr_info("to correct its BIOS.\n");
b9cae277 456 pr_info("This system has %d DIMM sockets.\n", ghes_hw.num_dimms);
d2a68566
MCC
457 }
458
32fa1f53 459 if (!fake) {
b9cae277
BP
460 struct dimm_info *src, *dst;
461 int i = 0;
462
463 mci_for_each_dimm(mci, dst) {
464 src = &ghes_hw.dimms[i];
465
466 dst->idx = src->idx;
467 dst->smbios_handle = src->smbios_handle;
468 dst->nr_pages = src->nr_pages;
469 dst->mtype = src->mtype;
470 dst->edac_mode = src->edac_mode;
471 dst->dtype = src->dtype;
472 dst->grain = src->grain;
473
474 /*
475 * If no src->label, preserve default label assigned
476 * from EDAC core.
477 */
478 if (strlen(src->label))
479 memcpy(dst->label, src->label, sizeof(src->label));
480
481 i++;
482 }
483
32fa1f53 484 } else {
bc9ad9e4 485 struct dimm_info *dimm = edac_get_dimm(mci, 0, 0, 0);
77c5f5d2 486
d2a68566 487 dimm->nr_pages = 1;
32fa1f53
MCC
488 dimm->grain = 128;
489 dimm->mtype = MEM_UNKNOWN;
490 dimm->dtype = DEV_UNKNOWN;
491 dimm->edac_mode = EDAC_SECDED;
492 }
77c5f5d2
MCC
493
494 rc = edac_mc_add_mc(mci);
495 if (rc < 0) {
b9cae277 496 pr_info("Can't register with the EDAC core\n");
77c5f5d2 497 edac_mc_free(mci);
23f61b9f
RR
498 rc = -ENODEV;
499 goto unlock;
77c5f5d2 500 }
23f61b9f
RR
501
502 spin_lock_irqsave(&ghes_lock, flags);
503 ghes_pvt = pvt;
504 spin_unlock_irqrestore(&ghes_lock, flags);
505
16214bd9
RR
506 /* only set on success */
507 refcount_set(&ghes_refcount, 1);
23f61b9f
RR
508
509unlock:
b9cae277
BP
510
511 /* Not needed anymore */
512 kfree(ghes_hw.dimms);
513 ghes_hw.dimms = NULL;
514
23f61b9f
RR
515 mutex_unlock(&ghes_reg_mutex);
516
517 return rc;
77c5f5d2 518}
77c5f5d2
MCC
519
520void ghes_edac_unregister(struct ghes *ghes)
521{
522 struct mem_ctl_info *mci;
23f61b9f 523 unsigned long flags;
0fe5f281 524
251c54ea
BP
525 if (!force_load)
526 return;
527
23f61b9f 528 mutex_lock(&ghes_reg_mutex);
a66bdf5d 529
b972fdba 530 system_scanned = false;
cd8100f1 531 memset(&ghes_hw, 0, sizeof(struct ghes_hw_desc));
b972fdba 532
23f61b9f
RR
533 if (!refcount_dec_and_test(&ghes_refcount))
534 goto unlock;
1e72e673 535
23f61b9f
RR
536 /*
537 * Wait for the irq handler being finished.
538 */
539 spin_lock_irqsave(&ghes_lock, flags);
540 mci = ghes_pvt ? ghes_pvt->mci : NULL;
1e72e673 541 ghes_pvt = NULL;
23f61b9f
RR
542 spin_unlock_irqrestore(&ghes_lock, flags);
543
544 if (!mci)
545 goto unlock;
546
547 mci = edac_mc_del_mc(mci->pdev);
548 if (mci)
549 edac_mc_free(mci);
550
551unlock:
552 mutex_unlock(&ghes_reg_mutex);
77c5f5d2 553}