]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/edac/ghes_edac.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / edac / ghes_edac.c
1 /*
2 * GHES/EDAC Linux driver
3 *
4 * This file may be distributed under the terms of the GNU General Public
5 * License version 2.
6 *
7 * Copyright (c) 2013 by Mauro Carvalho Chehab
8 *
9 * Red Hat Inc. http://www.redhat.com
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <acpi/ghes.h>
15 #include <linux/edac.h>
16 #include <linux/dmi.h>
17 #include "edac_module.h"
18 #include <ras/ras_event.h>
19
20 struct ghes_edac_pvt {
21 struct list_head list;
22 struct ghes *ghes;
23 struct mem_ctl_info *mci;
24
25 /* Buffers for the error handling routine */
26 char detail_location[240];
27 char other_detail[160];
28 char msg[80];
29 };
30
31 static atomic_t ghes_init = ATOMIC_INIT(0);
32 static struct ghes_edac_pvt *ghes_pvt;
33
34 /*
35 * Sync with other, potentially concurrent callers of
36 * ghes_edac_report_mem_error(). We don't know what the
37 * "inventive" firmware would do.
38 */
39 static DEFINE_SPINLOCK(ghes_lock);
40
41 /* "ghes_edac.force_load=1" skips the platform check */
42 static bool __read_mostly force_load;
43 module_param(force_load, bool, 0);
44
45 /* Memory Device - Type 17 of SMBIOS spec */
46 struct memdev_dmi_entry {
47 u8 type;
48 u8 length;
49 u16 handle;
50 u16 phys_mem_array_handle;
51 u16 mem_err_info_handle;
52 u16 total_width;
53 u16 data_width;
54 u16 size;
55 u8 form_factor;
56 u8 device_set;
57 u8 device_locator;
58 u8 bank_locator;
59 u8 memory_type;
60 u16 type_detail;
61 u16 speed;
62 u8 manufacturer;
63 u8 serial_number;
64 u8 asset_tag;
65 u8 part_number;
66 u8 attributes;
67 u32 extended_size;
68 u16 conf_mem_clk_speed;
69 } __attribute__((__packed__));
70
71 struct ghes_edac_dimm_fill {
72 struct mem_ctl_info *mci;
73 unsigned count;
74 };
75
76 static void ghes_edac_count_dimms(const struct dmi_header *dh, void *arg)
77 {
78 int *num_dimm = arg;
79
80 if (dh->type == DMI_ENTRY_MEM_DEVICE)
81 (*num_dimm)++;
82 }
83
84 static void ghes_edac_dmidecode(const struct dmi_header *dh, void *arg)
85 {
86 struct ghes_edac_dimm_fill *dimm_fill = arg;
87 struct mem_ctl_info *mci = dimm_fill->mci;
88
89 if (dh->type == DMI_ENTRY_MEM_DEVICE) {
90 struct memdev_dmi_entry *entry = (struct memdev_dmi_entry *)dh;
91 struct dimm_info *dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms,
92 mci->n_layers,
93 dimm_fill->count, 0, 0);
94
95 if (entry->size == 0xffff) {
96 pr_info("Can't get DIMM%i size\n",
97 dimm_fill->count);
98 dimm->nr_pages = MiB_TO_PAGES(32);/* Unknown */
99 } else if (entry->size == 0x7fff) {
100 dimm->nr_pages = MiB_TO_PAGES(entry->extended_size);
101 } else {
102 if (entry->size & 1 << 15)
103 dimm->nr_pages = MiB_TO_PAGES((entry->size &
104 0x7fff) << 10);
105 else
106 dimm->nr_pages = MiB_TO_PAGES(entry->size);
107 }
108
109 switch (entry->memory_type) {
110 case 0x12:
111 if (entry->type_detail & 1 << 13)
112 dimm->mtype = MEM_RDDR;
113 else
114 dimm->mtype = MEM_DDR;
115 break;
116 case 0x13:
117 if (entry->type_detail & 1 << 13)
118 dimm->mtype = MEM_RDDR2;
119 else
120 dimm->mtype = MEM_DDR2;
121 break;
122 case 0x14:
123 dimm->mtype = MEM_FB_DDR2;
124 break;
125 case 0x18:
126 if (entry->type_detail & 1 << 13)
127 dimm->mtype = MEM_RDDR3;
128 else
129 dimm->mtype = MEM_DDR3;
130 break;
131 default:
132 if (entry->type_detail & 1 << 6)
133 dimm->mtype = MEM_RMBS;
134 else if ((entry->type_detail & ((1 << 7) | (1 << 13)))
135 == ((1 << 7) | (1 << 13)))
136 dimm->mtype = MEM_RDR;
137 else if (entry->type_detail & 1 << 7)
138 dimm->mtype = MEM_SDR;
139 else if (entry->type_detail & 1 << 9)
140 dimm->mtype = MEM_EDO;
141 else
142 dimm->mtype = MEM_UNKNOWN;
143 }
144
145 /*
146 * Actually, we can only detect if the memory has bits for
147 * checksum or not
148 */
149 if (entry->total_width == entry->data_width)
150 dimm->edac_mode = EDAC_NONE;
151 else
152 dimm->edac_mode = EDAC_SECDED;
153
154 dimm->dtype = DEV_UNKNOWN;
155 dimm->grain = 128; /* Likely, worse case */
156
157 /*
158 * FIXME: It shouldn't be hard to also fill the DIMM labels
159 */
160
161 if (dimm->nr_pages) {
162 edac_dbg(1, "DIMM%i: %s size = %d MB%s\n",
163 dimm_fill->count, edac_mem_types[dimm->mtype],
164 PAGES_TO_MiB(dimm->nr_pages),
165 (dimm->edac_mode != EDAC_NONE) ? "(ECC)" : "");
166 edac_dbg(2, "\ttype %d, detail 0x%02x, width %d(total %d)\n",
167 entry->memory_type, entry->type_detail,
168 entry->total_width, entry->data_width);
169 }
170
171 dimm_fill->count++;
172 }
173 }
174
175 void ghes_edac_report_mem_error(struct ghes *ghes, int sev,
176 struct cper_sec_mem_err *mem_err)
177 {
178 enum hw_event_mc_err_type type;
179 struct edac_raw_error_desc *e;
180 struct mem_ctl_info *mci;
181 struct ghes_edac_pvt *pvt = ghes_pvt;
182 unsigned long flags;
183 char *p;
184 u8 grain_bits;
185
186 if (!pvt) {
187 pr_err("Internal error: Can't find EDAC structure\n");
188 return;
189 }
190
191 /*
192 * We can do the locking below because GHES defers error processing
193 * from NMI to IRQ context. Whenever that changes, we'd at least
194 * know.
195 */
196 if (WARN_ON_ONCE(in_nmi()))
197 return;
198
199 spin_lock_irqsave(&ghes_lock, flags);
200
201 mci = pvt->mci;
202 e = &mci->error_desc;
203
204 /* Cleans the error report buffer */
205 memset(e, 0, sizeof (*e));
206 e->error_count = 1;
207 e->grain = 1;
208 strcpy(e->label, "unknown label");
209 e->msg = pvt->msg;
210 e->other_detail = pvt->other_detail;
211 e->top_layer = -1;
212 e->mid_layer = -1;
213 e->low_layer = -1;
214 *pvt->other_detail = '\0';
215 *pvt->msg = '\0';
216
217 switch (sev) {
218 case GHES_SEV_CORRECTED:
219 type = HW_EVENT_ERR_CORRECTED;
220 break;
221 case GHES_SEV_RECOVERABLE:
222 type = HW_EVENT_ERR_UNCORRECTED;
223 break;
224 case GHES_SEV_PANIC:
225 type = HW_EVENT_ERR_FATAL;
226 break;
227 default:
228 case GHES_SEV_NO:
229 type = HW_EVENT_ERR_INFO;
230 }
231
232 edac_dbg(1, "error validation_bits: 0x%08llx\n",
233 (long long)mem_err->validation_bits);
234
235 /* Error type, mapped on e->msg */
236 if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_TYPE) {
237 p = pvt->msg;
238 switch (mem_err->error_type) {
239 case 0:
240 p += sprintf(p, "Unknown");
241 break;
242 case 1:
243 p += sprintf(p, "No error");
244 break;
245 case 2:
246 p += sprintf(p, "Single-bit ECC");
247 break;
248 case 3:
249 p += sprintf(p, "Multi-bit ECC");
250 break;
251 case 4:
252 p += sprintf(p, "Single-symbol ChipKill ECC");
253 break;
254 case 5:
255 p += sprintf(p, "Multi-symbol ChipKill ECC");
256 break;
257 case 6:
258 p += sprintf(p, "Master abort");
259 break;
260 case 7:
261 p += sprintf(p, "Target abort");
262 break;
263 case 8:
264 p += sprintf(p, "Parity Error");
265 break;
266 case 9:
267 p += sprintf(p, "Watchdog timeout");
268 break;
269 case 10:
270 p += sprintf(p, "Invalid address");
271 break;
272 case 11:
273 p += sprintf(p, "Mirror Broken");
274 break;
275 case 12:
276 p += sprintf(p, "Memory Sparing");
277 break;
278 case 13:
279 p += sprintf(p, "Scrub corrected error");
280 break;
281 case 14:
282 p += sprintf(p, "Scrub uncorrected error");
283 break;
284 case 15:
285 p += sprintf(p, "Physical Memory Map-out event");
286 break;
287 default:
288 p += sprintf(p, "reserved error (%d)",
289 mem_err->error_type);
290 }
291 } else {
292 strcpy(pvt->msg, "unknown error");
293 }
294
295 /* Error address */
296 if (mem_err->validation_bits & CPER_MEM_VALID_PA) {
297 e->page_frame_number = mem_err->physical_addr >> PAGE_SHIFT;
298 e->offset_in_page = mem_err->physical_addr & ~PAGE_MASK;
299 }
300
301 /* Error grain */
302 if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
303 e->grain = ~mem_err->physical_addr_mask + 1;
304
305 /* Memory error location, mapped on e->location */
306 p = e->location;
307 if (mem_err->validation_bits & CPER_MEM_VALID_NODE)
308 p += sprintf(p, "node:%d ", mem_err->node);
309 if (mem_err->validation_bits & CPER_MEM_VALID_CARD)
310 p += sprintf(p, "card:%d ", mem_err->card);
311 if (mem_err->validation_bits & CPER_MEM_VALID_MODULE)
312 p += sprintf(p, "module:%d ", mem_err->module);
313 if (mem_err->validation_bits & CPER_MEM_VALID_RANK_NUMBER)
314 p += sprintf(p, "rank:%d ", mem_err->rank);
315 if (mem_err->validation_bits & CPER_MEM_VALID_BANK)
316 p += sprintf(p, "bank:%d ", mem_err->bank);
317 if (mem_err->validation_bits & CPER_MEM_VALID_ROW)
318 p += sprintf(p, "row:%d ", mem_err->row);
319 if (mem_err->validation_bits & CPER_MEM_VALID_COLUMN)
320 p += sprintf(p, "col:%d ", mem_err->column);
321 if (mem_err->validation_bits & CPER_MEM_VALID_BIT_POSITION)
322 p += sprintf(p, "bit_pos:%d ", mem_err->bit_pos);
323 if (mem_err->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
324 const char *bank = NULL, *device = NULL;
325 dmi_memdev_name(mem_err->mem_dev_handle, &bank, &device);
326 if (bank != NULL && device != NULL)
327 p += sprintf(p, "DIMM location:%s %s ", bank, device);
328 else
329 p += sprintf(p, "DIMM DMI handle: 0x%.4x ",
330 mem_err->mem_dev_handle);
331 }
332 if (p > e->location)
333 *(p - 1) = '\0';
334
335 /* All other fields are mapped on e->other_detail */
336 p = pvt->other_detail;
337 if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_STATUS) {
338 u64 status = mem_err->error_status;
339
340 p += sprintf(p, "status(0x%016llx): ", (long long)status);
341 switch ((status >> 8) & 0xff) {
342 case 1:
343 p += sprintf(p, "Error detected internal to the component ");
344 break;
345 case 16:
346 p += sprintf(p, "Error detected in the bus ");
347 break;
348 case 4:
349 p += sprintf(p, "Storage error in DRAM memory ");
350 break;
351 case 5:
352 p += sprintf(p, "Storage error in TLB ");
353 break;
354 case 6:
355 p += sprintf(p, "Storage error in cache ");
356 break;
357 case 7:
358 p += sprintf(p, "Error in one or more functional units ");
359 break;
360 case 8:
361 p += sprintf(p, "component failed self test ");
362 break;
363 case 9:
364 p += sprintf(p, "Overflow or undervalue of internal queue ");
365 break;
366 case 17:
367 p += sprintf(p, "Virtual address not found on IO-TLB or IO-PDIR ");
368 break;
369 case 18:
370 p += sprintf(p, "Improper access error ");
371 break;
372 case 19:
373 p += sprintf(p, "Access to a memory address which is not mapped to any component ");
374 break;
375 case 20:
376 p += sprintf(p, "Loss of Lockstep ");
377 break;
378 case 21:
379 p += sprintf(p, "Response not associated with a request ");
380 break;
381 case 22:
382 p += sprintf(p, "Bus parity error - must also set the A, C, or D Bits ");
383 break;
384 case 23:
385 p += sprintf(p, "Detection of a PATH_ERROR ");
386 break;
387 case 25:
388 p += sprintf(p, "Bus operation timeout ");
389 break;
390 case 26:
391 p += sprintf(p, "A read was issued to data that has been poisoned ");
392 break;
393 default:
394 p += sprintf(p, "reserved ");
395 break;
396 }
397 }
398 if (mem_err->validation_bits & CPER_MEM_VALID_REQUESTOR_ID)
399 p += sprintf(p, "requestorID: 0x%016llx ",
400 (long long)mem_err->requestor_id);
401 if (mem_err->validation_bits & CPER_MEM_VALID_RESPONDER_ID)
402 p += sprintf(p, "responderID: 0x%016llx ",
403 (long long)mem_err->responder_id);
404 if (mem_err->validation_bits & CPER_MEM_VALID_TARGET_ID)
405 p += sprintf(p, "targetID: 0x%016llx ",
406 (long long)mem_err->responder_id);
407 if (p > pvt->other_detail)
408 *(p - 1) = '\0';
409
410 /* Sanity-check driver-supplied grain value. */
411 if (WARN_ON_ONCE(!e->grain))
412 e->grain = 1;
413
414 grain_bits = fls_long(e->grain - 1);
415
416 /* Generate the trace event */
417 snprintf(pvt->detail_location, sizeof(pvt->detail_location),
418 "APEI location: %s %s", e->location, e->other_detail);
419 trace_mc_event(type, e->msg, e->label, e->error_count,
420 mci->mc_idx, e->top_layer, e->mid_layer, e->low_layer,
421 (e->page_frame_number << PAGE_SHIFT) | e->offset_in_page,
422 grain_bits, e->syndrome, pvt->detail_location);
423
424 edac_raw_mc_handle_error(type, mci, e);
425 spin_unlock_irqrestore(&ghes_lock, flags);
426 }
427
428 /*
429 * Known systems that are safe to enable this module.
430 */
431 static struct acpi_platform_list plat_list[] = {
432 {"HPE ", "Server ", 0, ACPI_SIG_FADT, all_versions},
433 { } /* End */
434 };
435
436 int ghes_edac_register(struct ghes *ghes, struct device *dev)
437 {
438 bool fake = false;
439 int rc, num_dimm = 0;
440 struct mem_ctl_info *mci;
441 struct edac_mc_layer layers[1];
442 struct ghes_edac_dimm_fill dimm_fill;
443 int idx;
444
445 /* Check if safe to enable on this system */
446 idx = acpi_match_platform_list(plat_list);
447 if (!force_load && idx < 0)
448 return 0;
449
450 /*
451 * We have only one logical memory controller to which all DIMMs belong.
452 */
453 if (atomic_inc_return(&ghes_init) > 1)
454 return 0;
455
456 /* Get the number of DIMMs */
457 dmi_walk(ghes_edac_count_dimms, &num_dimm);
458
459 /* Check if we've got a bogus BIOS */
460 if (num_dimm == 0) {
461 fake = true;
462 num_dimm = 1;
463 }
464
465 layers[0].type = EDAC_MC_LAYER_ALL_MEM;
466 layers[0].size = num_dimm;
467 layers[0].is_virt_csrow = true;
468
469 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(struct ghes_edac_pvt));
470 if (!mci) {
471 pr_info("Can't allocate memory for EDAC data\n");
472 return -ENOMEM;
473 }
474
475 ghes_pvt = mci->pvt_info;
476 ghes_pvt->ghes = ghes;
477 ghes_pvt->mci = mci;
478
479 mci->pdev = dev;
480 mci->mtype_cap = MEM_FLAG_EMPTY;
481 mci->edac_ctl_cap = EDAC_FLAG_NONE;
482 mci->edac_cap = EDAC_FLAG_NONE;
483 mci->mod_name = "ghes_edac.c";
484 mci->ctl_name = "ghes_edac";
485 mci->dev_name = "ghes";
486
487 if (fake) {
488 pr_info("This system has a very crappy BIOS: It doesn't even list the DIMMS.\n");
489 pr_info("Its SMBIOS info is wrong. It is doubtful that the error report would\n");
490 pr_info("work on such system. Use this driver with caution\n");
491 } else if (idx < 0) {
492 pr_info("This EDAC driver relies on BIOS to enumerate memory and get error reports.\n");
493 pr_info("Unfortunately, not all BIOSes reflect the memory layout correctly.\n");
494 pr_info("So, the end result of using this driver varies from vendor to vendor.\n");
495 pr_info("If you find incorrect reports, please contact your hardware vendor\n");
496 pr_info("to correct its BIOS.\n");
497 pr_info("This system has %d DIMM sockets.\n", num_dimm);
498 }
499
500 if (!fake) {
501 dimm_fill.count = 0;
502 dimm_fill.mci = mci;
503 dmi_walk(ghes_edac_dmidecode, &dimm_fill);
504 } else {
505 struct dimm_info *dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms,
506 mci->n_layers, 0, 0, 0);
507
508 dimm->nr_pages = 1;
509 dimm->grain = 128;
510 dimm->mtype = MEM_UNKNOWN;
511 dimm->dtype = DEV_UNKNOWN;
512 dimm->edac_mode = EDAC_SECDED;
513 }
514
515 rc = edac_mc_add_mc(mci);
516 if (rc < 0) {
517 pr_info("Can't register at EDAC core\n");
518 edac_mc_free(mci);
519 return -ENODEV;
520 }
521 return 0;
522 }
523
524 void ghes_edac_unregister(struct ghes *ghes)
525 {
526 struct mem_ctl_info *mci;
527
528 if (atomic_dec_return(&ghes_init))
529 return;
530
531 mci = ghes_pvt->mci;
532 ghes_pvt = NULL;
533 edac_mc_del_mc(mci->pdev);
534 edac_mc_free(mci);
535 }