blob: bef8a428c429edac48f0a2e738a4460ab15fef54 [file] [log] [blame]
Thomas Gleixner12237552019-05-27 08:55:19 +02001// SPDX-License-Identifier: GPL-2.0-only
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -03002/*
3 * GHES/EDAC Linux driver
4 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02005 * Copyright (c) 2013 by Mauro Carvalho Chehab
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -03006 *
7 * Red Hat Inc. http://www.redhat.com
8 */
9
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -030010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -030012#include <acpi/ghes.h>
13#include <linux/edac.h>
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -030014#include <linux/dmi.h>
Mauro Carvalho Chehab78d88e82016-10-29 15:16:34 -020015#include "edac_module.h"
Mauro Carvalho Chehab8ae8f502013-02-19 21:35:41 -030016#include <ras/ras_event.h>
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -030017
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -030018struct ghes_edac_pvt {
19 struct list_head list;
20 struct ghes *ghes;
21 struct mem_ctl_info *mci;
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -030022
23 /* Buffers for the error handling routine */
Robert Richter501eb402019-11-06 09:33:25 +000024 char other_detail[400];
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -030025 char msg[80];
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -030026};
27
Robert Richter23f61b92019-11-05 20:07:51 +000028static 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 */
Borislav Petkov0fe5f282017-08-16 10:33:44 +020035static struct ghes_edac_pvt *ghes_pvt;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -030036
Robert Richter23f61b92019-11-05 20:07:51 +000037/* GHES registration mutex */
38static DEFINE_MUTEX(ghes_reg_mutex);
39
Borislav Petkov0fe5f282017-08-16 10:33:44 +020040/*
41 * Sync with other, potentially concurrent callers of
42 * ghes_edac_report_mem_error(). We don't know what the
43 * "inventive" firmware would do.
44 */
45static DEFINE_SPINLOCK(ghes_lock);
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -030046
Toshi Kani5deed6b2017-08-23 16:54:45 -060047/* "ghes_edac.force_load=1" skips the platform check */
48static bool __read_mostly force_load;
49module_param(force_load, bool, 0);
50
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -030051/* Memory Device - Type 17 of SMBIOS spec */
52struct memdev_dmi_entry {
53 u8 type;
54 u8 length;
55 u16 handle;
56 u16 phys_mem_array_handle;
57 u16 mem_err_info_handle;
58 u16 total_width;
59 u16 data_width;
60 u16 size;
61 u8 form_factor;
62 u8 device_set;
63 u8 device_locator;
64 u8 bank_locator;
65 u8 memory_type;
66 u16 type_detail;
67 u16 speed;
68 u8 manufacturer;
69 u8 serial_number;
70 u8 asset_tag;
71 u8 part_number;
72 u8 attributes;
73 u32 extended_size;
74 u16 conf_mem_clk_speed;
75} __attribute__((__packed__));
76
77struct ghes_edac_dimm_fill {
78 struct mem_ctl_info *mci;
Robert Richterd55c79a2019-09-02 12:33:41 +000079 unsigned int count;
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -030080};
81
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -030082static void ghes_edac_count_dimms(const struct dmi_header *dh, void *arg)
83{
84 int *num_dimm = arg;
85
86 if (dh->type == DMI_ENTRY_MEM_DEVICE)
87 (*num_dimm)++;
88}
89
Robert Richter23f61b92019-11-05 20:07:51 +000090static int get_dimm_smbios_index(struct mem_ctl_info *mci, u16 handle)
Fan Wuc798c882018-09-19 01:59:00 +000091{
Robert Richterc498afa2019-11-06 09:33:07 +000092 struct dimm_info *dimm;
Fan Wuc798c882018-09-19 01:59:00 +000093
Robert Richterc498afa2019-11-06 09:33:07 +000094 mci_for_each_dimm(mci, dimm) {
95 if (dimm->smbios_handle == handle)
96 return dimm->idx;
Fan Wuc798c882018-09-19 01:59:00 +000097 }
Robert Richterc498afa2019-11-06 09:33:07 +000098
Fan Wuc798c882018-09-19 01:59:00 +000099 return -1;
100}
101
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300102static void ghes_edac_dmidecode(const struct dmi_header *dh, void *arg)
103{
104 struct ghes_edac_dimm_fill *dimm_fill = arg;
105 struct mem_ctl_info *mci = dimm_fill->mci;
106
107 if (dh->type == DMI_ENTRY_MEM_DEVICE) {
108 struct memdev_dmi_entry *entry = (struct memdev_dmi_entry *)dh;
Robert Richterbc9ad9e2019-11-06 09:33:02 +0000109 struct dimm_info *dimm = edac_get_dimm(mci, dimm_fill->count, 0, 0);
Borislav Petkova0671c32018-05-12 14:32:43 +0200110 u16 rdr_mask = BIT(7) | BIT(13);
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300111
112 if (entry->size == 0xffff) {
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300113 pr_info("Can't get DIMM%i size\n",
114 dimm_fill->count);
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300115 dimm->nr_pages = MiB_TO_PAGES(32);/* Unknown */
116 } else if (entry->size == 0x7fff) {
117 dimm->nr_pages = MiB_TO_PAGES(entry->extended_size);
118 } else {
Borislav Petkova0671c32018-05-12 14:32:43 +0200119 if (entry->size & BIT(15))
120 dimm->nr_pages = MiB_TO_PAGES((entry->size & 0x7fff) << 10);
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300121 else
122 dimm->nr_pages = MiB_TO_PAGES(entry->size);
123 }
124
125 switch (entry->memory_type) {
126 case 0x12:
Borislav Petkova0671c32018-05-12 14:32:43 +0200127 if (entry->type_detail & BIT(13))
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300128 dimm->mtype = MEM_RDDR;
129 else
130 dimm->mtype = MEM_DDR;
131 break;
132 case 0x13:
Borislav Petkova0671c32018-05-12 14:32:43 +0200133 if (entry->type_detail & BIT(13))
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300134 dimm->mtype = MEM_RDDR2;
135 else
136 dimm->mtype = MEM_DDR2;
137 break;
138 case 0x14:
139 dimm->mtype = MEM_FB_DDR2;
140 break;
141 case 0x18:
Borislav Petkova0671c32018-05-12 14:32:43 +0200142 if (entry->type_detail & BIT(12))
Toshi Kaniad0d73b2018-05-09 16:20:30 -0600143 dimm->mtype = MEM_NVDIMM;
Borislav Petkova0671c32018-05-12 14:32:43 +0200144 else if (entry->type_detail & BIT(13))
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300145 dimm->mtype = MEM_RDDR3;
146 else
147 dimm->mtype = MEM_DDR3;
148 break;
Toshi Kaniad0d73b2018-05-09 16:20:30 -0600149 case 0x1a:
Borislav Petkova0671c32018-05-12 14:32:43 +0200150 if (entry->type_detail & BIT(12))
Toshi Kaniad0d73b2018-05-09 16:20:30 -0600151 dimm->mtype = MEM_NVDIMM;
Borislav Petkova0671c32018-05-12 14:32:43 +0200152 else if (entry->type_detail & BIT(13))
Toshi Kaniad0d73b2018-05-09 16:20:30 -0600153 dimm->mtype = MEM_RDDR4;
154 else
155 dimm->mtype = MEM_DDR4;
156 break;
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300157 default:
Borislav Petkova0671c32018-05-12 14:32:43 +0200158 if (entry->type_detail & BIT(6))
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300159 dimm->mtype = MEM_RMBS;
Borislav Petkova0671c32018-05-12 14:32:43 +0200160 else if ((entry->type_detail & rdr_mask) == rdr_mask)
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300161 dimm->mtype = MEM_RDR;
Borislav Petkova0671c32018-05-12 14:32:43 +0200162 else if (entry->type_detail & BIT(7))
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300163 dimm->mtype = MEM_SDR;
Borislav Petkova0671c32018-05-12 14:32:43 +0200164 else if (entry->type_detail & BIT(9))
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300165 dimm->mtype = MEM_EDO;
166 else
167 dimm->mtype = MEM_UNKNOWN;
168 }
169
170 /*
171 * Actually, we can only detect if the memory has bits for
172 * checksum or not
173 */
174 if (entry->total_width == entry->data_width)
175 dimm->edac_mode = EDAC_NONE;
176 else
177 dimm->edac_mode = EDAC_SECDED;
178
179 dimm->dtype = DEV_UNKNOWN;
180 dimm->grain = 128; /* Likely, worse case */
181
182 /*
183 * FIXME: It shouldn't be hard to also fill the DIMM labels
184 */
185
186 if (dimm->nr_pages) {
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300187 edac_dbg(1, "DIMM%i: %s size = %d MB%s\n",
Aravind Gopalakrishnan58a9c252015-09-16 15:53:29 -0500188 dimm_fill->count, edac_mem_types[dimm->mtype],
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300189 PAGES_TO_MiB(dimm->nr_pages),
190 (dimm->edac_mode != EDAC_NONE) ? "(ECC)" : "");
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300191 edac_dbg(2, "\ttype %d, detail 0x%02x, width %d(total %d)\n",
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300192 entry->memory_type, entry->type_detail,
193 entry->total_width, entry->data_width);
194 }
195
Fan Wuc798c882018-09-19 01:59:00 +0000196 dimm->smbios_handle = entry->handle;
197
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300198 dimm_fill->count++;
199 }
200}
201
Alexandru Gagniuc305d0e02018-04-30 16:33:50 -0500202void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300203{
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300204 struct edac_raw_error_desc *e;
205 struct mem_ctl_info *mci;
Robert Richter23f61b92019-11-05 20:07:51 +0000206 struct ghes_edac_pvt *pvt;
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200207 unsigned long flags;
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300208 char *p;
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300209
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200210 /*
211 * We can do the locking below because GHES defers error processing
212 * from NMI to IRQ context. Whenever that changes, we'd at least
213 * know.
214 */
215 if (WARN_ON_ONCE(in_nmi()))
216 return;
217
218 spin_lock_irqsave(&ghes_lock, flags);
219
Robert Richter23f61b92019-11-05 20:07:51 +0000220 pvt = ghes_pvt;
221 if (!pvt)
222 goto unlock;
223
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300224 mci = pvt->mci;
225 e = &mci->error_desc;
226
227 /* Cleans the error report buffer */
228 memset(e, 0, sizeof (*e));
229 e->error_count = 1;
Robert Richter7088e292019-11-06 09:33:23 +0000230 e->grain = 1;
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300231 strcpy(e->label, "unknown label");
232 e->msg = pvt->msg;
233 e->other_detail = pvt->other_detail;
234 e->top_layer = -1;
235 e->mid_layer = -1;
236 e->low_layer = -1;
237 *pvt->other_detail = '\0';
238 *pvt->msg = '\0';
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300239
240 switch (sev) {
241 case GHES_SEV_CORRECTED:
Robert Richter672ef0e2020-01-23 09:02:54 +0000242 e->type = HW_EVENT_ERR_CORRECTED;
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300243 break;
244 case GHES_SEV_RECOVERABLE:
Robert Richter672ef0e2020-01-23 09:02:54 +0000245 e->type = HW_EVENT_ERR_UNCORRECTED;
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300246 break;
247 case GHES_SEV_PANIC:
Robert Richter672ef0e2020-01-23 09:02:54 +0000248 e->type = HW_EVENT_ERR_FATAL;
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300249 break;
250 default:
251 case GHES_SEV_NO:
Robert Richter672ef0e2020-01-23 09:02:54 +0000252 e->type = HW_EVENT_ERR_INFO;
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300253 }
254
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300255 edac_dbg(1, "error validation_bits: 0x%08llx\n",
256 (long long)mem_err->validation_bits);
257
258 /* Error type, mapped on e->msg */
259 if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_TYPE) {
260 p = pvt->msg;
261 switch (mem_err->error_type) {
262 case 0:
263 p += sprintf(p, "Unknown");
264 break;
265 case 1:
266 p += sprintf(p, "No error");
267 break;
268 case 2:
269 p += sprintf(p, "Single-bit ECC");
270 break;
271 case 3:
272 p += sprintf(p, "Multi-bit ECC");
273 break;
274 case 4:
275 p += sprintf(p, "Single-symbol ChipKill ECC");
276 break;
277 case 5:
278 p += sprintf(p, "Multi-symbol ChipKill ECC");
279 break;
280 case 6:
281 p += sprintf(p, "Master abort");
282 break;
283 case 7:
284 p += sprintf(p, "Target abort");
285 break;
286 case 8:
287 p += sprintf(p, "Parity Error");
288 break;
289 case 9:
290 p += sprintf(p, "Watchdog timeout");
291 break;
292 case 10:
293 p += sprintf(p, "Invalid address");
294 break;
295 case 11:
296 p += sprintf(p, "Mirror Broken");
297 break;
298 case 12:
299 p += sprintf(p, "Memory Sparing");
300 break;
301 case 13:
302 p += sprintf(p, "Scrub corrected error");
303 break;
304 case 14:
305 p += sprintf(p, "Scrub uncorrected error");
306 break;
307 case 15:
308 p += sprintf(p, "Physical Memory Map-out event");
309 break;
310 default:
311 p += sprintf(p, "reserved error (%d)",
312 mem_err->error_type);
313 }
314 } else {
315 strcpy(pvt->msg, "unknown error");
316 }
317
318 /* Error address */
Chen, Gong147de142013-10-18 14:30:13 -0700319 if (mem_err->validation_bits & CPER_MEM_VALID_PA) {
Robert Richter7c104932019-11-06 09:33:20 +0000320 e->page_frame_number = PHYS_PFN(mem_err->physical_addr);
321 e->offset_in_page = offset_in_page(mem_err->physical_addr);
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300322 }
323
324 /* Error grain */
Chen, Gong147de142013-10-18 14:30:13 -0700325 if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
Robert Richter7088e292019-11-06 09:33:23 +0000326 e->grain = ~mem_err->physical_addr_mask + 1;
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300327
328 /* Memory error location, mapped on e->location */
329 p = e->location;
330 if (mem_err->validation_bits & CPER_MEM_VALID_NODE)
331 p += sprintf(p, "node:%d ", mem_err->node);
332 if (mem_err->validation_bits & CPER_MEM_VALID_CARD)
333 p += sprintf(p, "card:%d ", mem_err->card);
334 if (mem_err->validation_bits & CPER_MEM_VALID_MODULE)
335 p += sprintf(p, "module:%d ", mem_err->module);
Chen, Gong56507692013-10-18 14:30:38 -0700336 if (mem_err->validation_bits & CPER_MEM_VALID_RANK_NUMBER)
337 p += sprintf(p, "rank:%d ", mem_err->rank);
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300338 if (mem_err->validation_bits & CPER_MEM_VALID_BANK)
339 p += sprintf(p, "bank:%d ", mem_err->bank);
340 if (mem_err->validation_bits & CPER_MEM_VALID_ROW)
341 p += sprintf(p, "row:%d ", mem_err->row);
342 if (mem_err->validation_bits & CPER_MEM_VALID_COLUMN)
343 p += sprintf(p, "col:%d ", mem_err->column);
344 if (mem_err->validation_bits & CPER_MEM_VALID_BIT_POSITION)
345 p += sprintf(p, "bit_pos:%d ", mem_err->bit_pos);
Chen, Gong56507692013-10-18 14:30:38 -0700346 if (mem_err->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
347 const char *bank = NULL, *device = NULL;
Fan Wuc798c882018-09-19 01:59:00 +0000348 int index = -1;
349
Chen, Gong56507692013-10-18 14:30:38 -0700350 dmi_memdev_name(mem_err->mem_dev_handle, &bank, &device);
351 if (bank != NULL && device != NULL)
352 p += sprintf(p, "DIMM location:%s %s ", bank, device);
353 else
354 p += sprintf(p, "DIMM DMI handle: 0x%.4x ",
355 mem_err->mem_dev_handle);
Fan Wuc798c882018-09-19 01:59:00 +0000356
Robert Richter23f61b92019-11-05 20:07:51 +0000357 index = get_dimm_smbios_index(mci, mem_err->mem_dev_handle);
Fan Wuc798c882018-09-19 01:59:00 +0000358 if (index >= 0) {
359 e->top_layer = index;
360 e->enable_per_layer_report = true;
361 }
362
Chen, Gong56507692013-10-18 14:30:38 -0700363 }
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300364 if (p > e->location)
365 *(p - 1) = '\0';
366
367 /* All other fields are mapped on e->other_detail */
368 p = pvt->other_detail;
Robert Richter501eb402019-11-06 09:33:25 +0000369 p += snprintf(p, sizeof(pvt->other_detail),
370 "APEI location: %s ", e->location);
Mauro Carvalho Chehab689c9cd2013-02-19 19:24:12 -0300371 if (mem_err->validation_bits & CPER_MEM_VALID_ERROR_STATUS) {
372 u64 status = mem_err->error_status;
373
374 p += sprintf(p, "status(0x%016llx): ", (long long)status);
375 switch ((status >> 8) & 0xff) {
376 case 1:
377 p += sprintf(p, "Error detected internal to the component ");
378 break;
379 case 16:
380 p += sprintf(p, "Error detected in the bus ");
381 break;
382 case 4:
383 p += sprintf(p, "Storage error in DRAM memory ");
384 break;
385 case 5:
386 p += sprintf(p, "Storage error in TLB ");
387 break;
388 case 6:
389 p += sprintf(p, "Storage error in cache ");
390 break;
391 case 7:
392 p += sprintf(p, "Error in one or more functional units ");
393 break;
394 case 8:
395 p += sprintf(p, "component failed self test ");
396 break;
397 case 9:
398 p += sprintf(p, "Overflow or undervalue of internal queue ");
399 break;
400 case 17:
401 p += sprintf(p, "Virtual address not found on IO-TLB or IO-PDIR ");
402 break;
403 case 18:
404 p += sprintf(p, "Improper access error ");
405 break;
406 case 19:
407 p += sprintf(p, "Access to a memory address which is not mapped to any component ");
408 break;
409 case 20:
410 p += sprintf(p, "Loss of Lockstep ");
411 break;
412 case 21:
413 p += sprintf(p, "Response not associated with a request ");
414 break;
415 case 22:
416 p += sprintf(p, "Bus parity error - must also set the A, C, or D Bits ");
417 break;
418 case 23:
419 p += sprintf(p, "Detection of a PATH_ERROR ");
420 break;
421 case 25:
422 p += sprintf(p, "Bus operation timeout ");
423 break;
424 case 26:
425 p += sprintf(p, "A read was issued to data that has been poisoned ");
426 break;
427 default:
428 p += sprintf(p, "reserved ");
429 break;
430 }
431 }
432 if (mem_err->validation_bits & CPER_MEM_VALID_REQUESTOR_ID)
433 p += sprintf(p, "requestorID: 0x%016llx ",
434 (long long)mem_err->requestor_id);
435 if (mem_err->validation_bits & CPER_MEM_VALID_RESPONDER_ID)
436 p += sprintf(p, "responderID: 0x%016llx ",
437 (long long)mem_err->responder_id);
438 if (mem_err->validation_bits & CPER_MEM_VALID_TARGET_ID)
439 p += sprintf(p, "targetID: 0x%016llx ",
440 (long long)mem_err->responder_id);
441 if (p > pvt->other_detail)
442 *(p - 1) = '\0';
Mauro Carvalho Chehabf04c62a2013-02-15 06:36:27 -0300443
Robert Richter91b327f2020-01-23 09:02:56 +0000444 edac_raw_mc_handle_error(e);
Robert Richter23f61b92019-11-05 20:07:51 +0000445
446unlock:
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200447 spin_unlock_irqrestore(&ghes_lock, flags);
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300448}
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300449
Toshi Kani5deed6b2017-08-23 16:54:45 -0600450/*
451 * Known systems that are safe to enable this module.
452 */
453static struct acpi_platform_list plat_list[] = {
454 {"HPE ", "Server ", 0, ACPI_SIG_FADT, all_versions},
455 { } /* End */
456};
457
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300458int ghes_edac_register(struct ghes *ghes, struct device *dev)
459{
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300460 bool fake = false;
Robert Richter23f61b92019-11-05 20:07:51 +0000461 int rc = 0, num_dimm = 0;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300462 struct mem_ctl_info *mci;
Robert Richter23f61b92019-11-05 20:07:51 +0000463 struct ghes_edac_pvt *pvt;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300464 struct edac_mc_layer layers[1];
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300465 struct ghes_edac_dimm_fill dimm_fill;
Robert Richter23f61b92019-11-05 20:07:51 +0000466 unsigned long flags;
Borislav Petkoveaa3a1d2018-05-18 13:13:31 +0200467 int idx = -1;
Toshi Kani5deed6b2017-08-23 16:54:45 -0600468
Borislav Petkoveaa3a1d2018-05-18 13:13:31 +0200469 if (IS_ENABLED(CONFIG_X86)) {
470 /* Check if safe to enable on this system */
471 idx = acpi_match_platform_list(plat_list);
472 if (!force_load && idx < 0)
473 return -ENODEV;
474 } else {
475 idx = 0;
476 }
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300477
Robert Richter23f61b92019-11-05 20:07:51 +0000478 /* finish another registration/unregistration instance first */
479 mutex_lock(&ghes_reg_mutex);
480
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200481 /*
482 * We have only one logical memory controller to which all DIMMs belong.
483 */
Robert Richter23f61b92019-11-05 20:07:51 +0000484 if (refcount_inc_not_zero(&ghes_refcount))
485 goto unlock;
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200486
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300487 /* Get the number of DIMMs */
488 dmi_walk(ghes_edac_count_dimms, &num_dimm);
489
490 /* Check if we've got a bogus BIOS */
491 if (num_dimm == 0) {
492 fake = true;
493 num_dimm = 1;
494 }
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300495
496 layers[0].type = EDAC_MC_LAYER_ALL_MEM;
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300497 layers[0].size = num_dimm;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300498 layers[0].is_virt_csrow = true;
499
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200500 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, sizeof(struct ghes_edac_pvt));
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300501 if (!mci) {
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300502 pr_info("Can't allocate memory for EDAC data\n");
Robert Richter23f61b92019-11-05 20:07:51 +0000503 rc = -ENOMEM;
504 goto unlock;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300505 }
506
Robert Richter23f61b92019-11-05 20:07:51 +0000507 pvt = mci->pvt_info;
508 pvt->ghes = ghes;
509 pvt->mci = mci;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300510
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200511 mci->pdev = dev;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300512 mci->mtype_cap = MEM_FLAG_EMPTY;
513 mci->edac_ctl_cap = EDAC_FLAG_NONE;
514 mci->edac_cap = EDAC_FLAG_NONE;
515 mci->mod_name = "ghes_edac.c";
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300516 mci->ctl_name = "ghes_edac";
517 mci->dev_name = "ghes";
518
Toshi Kani5deed6b2017-08-23 16:54:45 -0600519 if (fake) {
520 pr_info("This system has a very crappy BIOS: It doesn't even list the DIMMS.\n");
521 pr_info("Its SMBIOS info is wrong. It is doubtful that the error report would\n");
522 pr_info("work on such system. Use this driver with caution\n");
523 } else if (idx < 0) {
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200524 pr_info("This EDAC driver relies on BIOS to enumerate memory and get error reports.\n");
525 pr_info("Unfortunately, not all BIOSes reflect the memory layout correctly.\n");
526 pr_info("So, the end result of using this driver varies from vendor to vendor.\n");
527 pr_info("If you find incorrect reports, please contact your hardware vendor\n");
528 pr_info("to correct its BIOS.\n");
529 pr_info("This system has %d DIMM sockets.\n", num_dimm);
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300530 }
531
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300532 if (!fake) {
Borislav Petkov0fe5f282017-08-16 10:33:44 +0200533 dimm_fill.count = 0;
534 dimm_fill.mci = mci;
535 dmi_walk(ghes_edac_dmidecode, &dimm_fill);
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300536 } else {
Robert Richterbc9ad9e2019-11-06 09:33:02 +0000537 struct dimm_info *dimm = edac_get_dimm(mci, 0, 0, 0);
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300538
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300539 dimm->nr_pages = 1;
Mauro Carvalho Chehab32fa1f52013-02-14 09:11:08 -0300540 dimm->grain = 128;
541 dimm->mtype = MEM_UNKNOWN;
542 dimm->dtype = DEV_UNKNOWN;
543 dimm->edac_mode = EDAC_SECDED;
544 }
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300545
546 rc = edac_mc_add_mc(mci);
547 if (rc < 0) {
Mauro Carvalho Chehabd2a68562013-02-15 09:06:38 -0300548 pr_info("Can't register at EDAC core\n");
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300549 edac_mc_free(mci);
Robert Richter23f61b92019-11-05 20:07:51 +0000550 rc = -ENODEV;
551 goto unlock;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300552 }
Robert Richter23f61b92019-11-05 20:07:51 +0000553
554 spin_lock_irqsave(&ghes_lock, flags);
555 ghes_pvt = pvt;
556 spin_unlock_irqrestore(&ghes_lock, flags);
557
Robert Richter16214bd2019-11-21 21:36:57 +0000558 /* only set on success */
559 refcount_set(&ghes_refcount, 1);
Robert Richter23f61b92019-11-05 20:07:51 +0000560
561unlock:
562 mutex_unlock(&ghes_reg_mutex);
563
564 return rc;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300565}
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300566
567void ghes_edac_unregister(struct ghes *ghes)
568{
569 struct mem_ctl_info *mci;
Robert Richter23f61b92019-11-05 20:07:51 +0000570 unsigned long flags;
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300571
Robert Richter23f61b92019-11-05 20:07:51 +0000572 mutex_lock(&ghes_reg_mutex);
Sughosh Ganua66bdf52018-04-26 15:46:49 +0530573
Robert Richter23f61b92019-11-05 20:07:51 +0000574 if (!refcount_dec_and_test(&ghes_refcount))
575 goto unlock;
James Morse1e72e672019-10-14 18:19:18 +0100576
Robert Richter23f61b92019-11-05 20:07:51 +0000577 /*
578 * Wait for the irq handler being finished.
579 */
580 spin_lock_irqsave(&ghes_lock, flags);
581 mci = ghes_pvt ? ghes_pvt->mci : NULL;
James Morse1e72e672019-10-14 18:19:18 +0100582 ghes_pvt = NULL;
Robert Richter23f61b92019-11-05 20:07:51 +0000583 spin_unlock_irqrestore(&ghes_lock, flags);
584
585 if (!mci)
586 goto unlock;
587
588 mci = edac_mc_del_mc(mci->pdev);
589 if (mci)
590 edac_mc_free(mci);
591
592unlock:
593 mutex_unlock(&ghes_reg_mutex);
Mauro Carvalho Chehab77c5f5d2013-02-15 06:11:57 -0300594}