1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * APEI Generic Hardware Error Source support
4 *
5 * Generic Hardware Error Source provides a way to report platform
6 * hardware errors (such as that from chipset). It works in so called
7 * "Firmware First" mode, that is, hardware errors are reported to
8 * firmware firstly, then reported to Linux by firmware. This way,
9 * some non-standard hardware error registers or non-standard hardware
10 * link can be checked by firmware to produce more hardware error
11 * information for Linux.
12 *
13 * For more information about Generic Hardware Error Source, please
14 * refer to ACPI Specification version 4.0, section 17.3.2.6
15 *
16 * Copyright 2010,2011 Intel Corp.
17 * Author: Huang Ying <ying.huang@intel.com>
18 */
19
20 #include <linux/arm_sdei.h>
21 #include <linux/kernel.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/acpi.h>
25 #include <linux/bitfield.h>
26 #include <linux/io.h>
27 #include <linux/interrupt.h>
28 #include <linux/timer.h>
29 #include <linux/cper.h>
30 #include <linux/cleanup.h>
31 #include <linux/platform_device.h>
32 #include <linux/minmax.h>
33 #include <linux/mutex.h>
34 #include <linux/ratelimit.h>
35 #include <linux/vmalloc.h>
36 #include <linux/irq_work.h>
37 #include <linux/llist.h>
38 #include <linux/genalloc.h>
39 #include <linux/kfifo.h>
40 #include <linux/pci.h>
41 #include <linux/pfn.h>
42 #include <linux/aer.h>
43 #include <linux/nmi.h>
44 #include <linux/sched/clock.h>
45 #include <linux/uuid.h>
46 #include <linux/ras.h>
47 #include <linux/task_work.h>
48 #include <linux/vmcore_info.h>
49
50 #include <acpi/actbl1.h>
51 #include <acpi/ghes.h>
52 #include <acpi/apei.h>
53 #include <asm/fixmap.h>
54 #include <asm/tlbflush.h>
55 #include <cxl/event.h>
56 #include <ras/ras_event.h>
57
58 #include "apei-internal.h"
59
60 #define GHES_PFX "GHES: "
61
62 #define GHES_ESTATUS_MAX_SIZE 65536
63 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
64
65 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
66
67 /* This is just an estimation for memory pool allocation */
68 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
69
70 #define GHES_ESTATUS_CACHES_SIZE 4
71
72 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
73 /* Prevent too many caches are allocated because of RCU */
74 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
75
76 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
77 (sizeof(struct ghes_estatus_cache) + (estatus_len))
78 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
79 ((struct acpi_hest_generic_status *) \
80 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
81
82 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
83 (sizeof(struct ghes_estatus_node) + (estatus_len))
84 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
85 ((struct acpi_hest_generic_status *) \
86 ((struct ghes_estatus_node *)(estatus_node) + 1))
87
88 #define GHES_VENDOR_ENTRY_LEN(gdata_len) \
89 (sizeof(struct ghes_vendor_record_entry) + (gdata_len))
90 #define GHES_GDATA_FROM_VENDOR_ENTRY(vendor_entry) \
91 ((struct acpi_hest_generic_data *) \
92 ((struct ghes_vendor_record_entry *)(vendor_entry) + 1))
93
94 /*
95 * NMI-like notifications vary by architecture, before the compiler can prune
96 * unused static functions it needs a value for these enums.
97 */
98 #ifndef CONFIG_ARM_SDE_INTERFACE
99 #define FIX_APEI_GHES_SDEI_NORMAL __end_of_fixed_addresses
100 #define FIX_APEI_GHES_SDEI_CRITICAL __end_of_fixed_addresses
101 #endif
102
103 static ATOMIC_NOTIFIER_HEAD(ghes_report_chain);
104
is_hest_type_generic_v2(struct ghes * ghes)105 static inline bool is_hest_type_generic_v2(struct ghes *ghes)
106 {
107 return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
108 }
109
110 /*
111 * A platform may describe one error source for the handling of synchronous
112 * errors (e.g. MCE or SEA), or for handling asynchronous errors (e.g. SCI
113 * or External Interrupt). On x86, the HEST notifications are always
114 * asynchronous, so only SEA on ARM is delivered as a synchronous
115 * notification.
116 */
is_hest_sync_notify(struct ghes * ghes)117 static inline bool is_hest_sync_notify(struct ghes *ghes)
118 {
119 u8 notify_type = ghes->generic->notify.type;
120
121 return notify_type == ACPI_HEST_NOTIFY_SEA;
122 }
123
124 /*
125 * This driver isn't really modular, however for the time being,
126 * continuing to use module_param is the easiest way to remain
127 * compatible with existing boot arg use cases.
128 */
129 bool ghes_disable;
130 module_param_named(disable, ghes_disable, bool, 0);
131
132 /*
133 * "ghes.edac_force_enable" forcibly enables ghes_edac and skips the platform
134 * check.
135 */
136 static bool ghes_edac_force_enable;
137 module_param_named(edac_force_enable, ghes_edac_force_enable, bool, 0);
138
139 /*
140 * All error sources notified with HED (Hardware Error Device) share a
141 * single notifier callback, so they need to be linked and checked one
142 * by one. This holds true for NMI too.
143 *
144 * RCU is used for these lists, so ghes_list_mutex is only used for
145 * list changing, not for traversing.
146 */
147 static LIST_HEAD(ghes_hed);
148 static DEFINE_MUTEX(ghes_list_mutex);
149
150 /*
151 * A list of GHES devices which are given to the corresponding EDAC driver
152 * ghes_edac for further use.
153 */
154 static LIST_HEAD(ghes_devs);
155 static DEFINE_MUTEX(ghes_devs_mutex);
156
157 /*
158 * Because the memory area used to transfer hardware error information
159 * from BIOS to Linux can be determined only in NMI, IRQ or timer
160 * handler, but general ioremap can not be used in atomic context, so
161 * the fixmap is used instead.
162 *
163 * This spinlock is used to prevent the fixmap entry from being used
164 * simultaneously.
165 */
166 static DEFINE_SPINLOCK(ghes_notify_lock_irq);
167
168 struct ghes_vendor_record_entry {
169 struct work_struct work;
170 int error_severity;
171 char vendor_record[];
172 };
173
174 static struct gen_pool *ghes_estatus_pool;
175
176 static struct ghes_estatus_cache __rcu *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
177 static atomic_t ghes_estatus_cache_alloced;
178
ghes_map(u64 pfn,enum fixed_addresses fixmap_idx)179 static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
180 {
181 phys_addr_t paddr;
182 pgprot_t prot;
183
184 paddr = PFN_PHYS(pfn);
185 prot = arch_apei_get_mem_attribute(paddr);
186 __set_fixmap(fixmap_idx, paddr, prot);
187
188 return (void __iomem *) __fix_to_virt(fixmap_idx);
189 }
190
ghes_unmap(void __iomem * vaddr,enum fixed_addresses fixmap_idx)191 static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
192 {
193 int _idx = virt_to_fix((unsigned long)vaddr);
194
195 WARN_ON_ONCE(fixmap_idx != _idx);
196 clear_fixmap(fixmap_idx);
197 }
198
ghes_estatus_pool_init(unsigned int num_ghes)199 int ghes_estatus_pool_init(unsigned int num_ghes)
200 {
201 unsigned long addr, len;
202 int rc;
203
204 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
205 if (!ghes_estatus_pool)
206 return -ENOMEM;
207
208 len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
209 len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
210
211 addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
212 if (!addr)
213 goto err_pool_alloc;
214
215 rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
216 if (rc)
217 goto err_pool_add;
218
219 return 0;
220
221 err_pool_add:
222 vfree((void *)addr);
223
224 err_pool_alloc:
225 gen_pool_destroy(ghes_estatus_pool);
226
227 return -ENOMEM;
228 }
229
230 /**
231 * ghes_estatus_pool_region_free - free previously allocated memory
232 * from the ghes_estatus_pool.
233 * @addr: address of memory to free.
234 * @size: size of memory to free.
235 *
236 * Returns none.
237 */
ghes_estatus_pool_region_free(unsigned long addr,u32 size)238 void ghes_estatus_pool_region_free(unsigned long addr, u32 size)
239 {
240 gen_pool_free(ghes_estatus_pool, addr, size);
241 }
242 EXPORT_SYMBOL_GPL(ghes_estatus_pool_region_free);
243
map_gen_v2(struct ghes * ghes)244 static int map_gen_v2(struct ghes *ghes)
245 {
246 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
247 }
248
unmap_gen_v2(struct ghes * ghes)249 static void unmap_gen_v2(struct ghes *ghes)
250 {
251 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
252 }
253
ghes_ack_error(struct acpi_hest_generic_v2 * gv2)254 static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
255 {
256 int rc;
257 u64 val = 0;
258
259 rc = apei_read(&val, &gv2->read_ack_register);
260 if (rc)
261 return;
262
263 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
264 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
265
266 apei_write(val, &gv2->read_ack_register);
267 }
268
ghes_new(struct acpi_hest_generic * generic)269 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
270 {
271 struct ghes *ghes;
272 unsigned int error_block_length;
273 int rc;
274
275 ghes = kzalloc_obj(*ghes);
276 if (!ghes)
277 return ERR_PTR(-ENOMEM);
278
279 ghes->generic = generic;
280 if (is_hest_type_generic_v2(ghes)) {
281 rc = map_gen_v2(ghes);
282 if (rc)
283 goto err_free;
284 }
285
286 rc = apei_map_generic_address(&generic->error_status_address);
287 if (rc)
288 goto err_unmap_read_ack_addr;
289 error_block_length = generic->error_block_length;
290 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
291 pr_warn(FW_WARN GHES_PFX
292 "Error status block length is too long: %u for "
293 "generic hardware error source: %d.\n",
294 error_block_length, generic->header.source_id);
295 error_block_length = GHES_ESTATUS_MAX_SIZE;
296 }
297 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
298 ghes->estatus_length = error_block_length;
299 if (!ghes->estatus) {
300 rc = -ENOMEM;
301 goto err_unmap_status_addr;
302 }
303
304 return ghes;
305
306 err_unmap_status_addr:
307 apei_unmap_generic_address(&generic->error_status_address);
308 err_unmap_read_ack_addr:
309 if (is_hest_type_generic_v2(ghes))
310 unmap_gen_v2(ghes);
311 err_free:
312 kfree(ghes);
313 return ERR_PTR(rc);
314 }
315
ghes_fini(struct ghes * ghes)316 static void ghes_fini(struct ghes *ghes)
317 {
318 kfree(ghes->estatus);
319 apei_unmap_generic_address(&ghes->generic->error_status_address);
320 if (is_hest_type_generic_v2(ghes))
321 unmap_gen_v2(ghes);
322 }
323
ghes_severity(int severity)324 static inline int ghes_severity(int severity)
325 {
326 switch (severity) {
327 case CPER_SEV_INFORMATIONAL:
328 return GHES_SEV_NO;
329 case CPER_SEV_CORRECTED:
330 return GHES_SEV_CORRECTED;
331 case CPER_SEV_RECOVERABLE:
332 return GHES_SEV_RECOVERABLE;
333 case CPER_SEV_FATAL:
334 return GHES_SEV_PANIC;
335 default:
336 /* Unknown, go panic */
337 return GHES_SEV_PANIC;
338 }
339 }
340
ghes_copy_tofrom_phys(void * buffer,u64 paddr,u32 len,int from_phys,enum fixed_addresses fixmap_idx)341 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
342 int from_phys,
343 enum fixed_addresses fixmap_idx)
344 {
345 void __iomem *vaddr;
346 u64 offset;
347 u32 trunk;
348
349 while (len > 0) {
350 offset = paddr - (paddr & PAGE_MASK);
351 vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
352 trunk = PAGE_SIZE - offset;
353 trunk = min(trunk, len);
354 if (from_phys)
355 memcpy_fromio(buffer, vaddr + offset, trunk);
356 else
357 memcpy_toio(vaddr + offset, buffer, trunk);
358 len -= trunk;
359 paddr += trunk;
360 buffer += trunk;
361 ghes_unmap(vaddr, fixmap_idx);
362 }
363 }
364
365 /* Check the top-level record header has an appropriate size. */
__ghes_check_estatus(struct ghes * ghes,struct acpi_hest_generic_status * estatus)366 static int __ghes_check_estatus(struct ghes *ghes,
367 struct acpi_hest_generic_status *estatus)
368 {
369 u32 len = cper_estatus_len(estatus);
370 u32 max_len = min(ghes->generic->error_block_length,
371 ghes->estatus_length);
372
373 if (len < sizeof(*estatus)) {
374 pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
375 return -EIO;
376 }
377
378 if (!len || len > max_len) {
379 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
380 return -EIO;
381 }
382
383 if (cper_estatus_check_header(estatus)) {
384 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
385 return -EIO;
386 }
387
388 return 0;
389 }
390
391 /* Read the CPER block, returning its address, and header in estatus. */
__ghes_peek_estatus(struct ghes * ghes,struct acpi_hest_generic_status * estatus,u64 * buf_paddr,enum fixed_addresses fixmap_idx)392 static int __ghes_peek_estatus(struct ghes *ghes,
393 struct acpi_hest_generic_status *estatus,
394 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
395 {
396 struct acpi_hest_generic *g = ghes->generic;
397 int rc;
398
399 rc = apei_read(buf_paddr, &g->error_status_address);
400 if (rc) {
401 *buf_paddr = 0;
402 pr_warn_ratelimited(FW_WARN GHES_PFX
403 "Failed to read error status block address for hardware error source: %d.\n",
404 g->header.source_id);
405 return -EIO;
406 }
407 if (!*buf_paddr)
408 return -ENOENT;
409
410 ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
411 fixmap_idx);
412 if (!estatus->block_status) {
413 *buf_paddr = 0;
414 return -ENOENT;
415 }
416
417 return 0;
418 }
419
__ghes_read_estatus(struct acpi_hest_generic_status * estatus,u64 buf_paddr,enum fixed_addresses fixmap_idx,size_t buf_len)420 static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
421 u64 buf_paddr, enum fixed_addresses fixmap_idx,
422 size_t buf_len)
423 {
424 ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
425 if (cper_estatus_check(estatus)) {
426 pr_warn_ratelimited(FW_WARN GHES_PFX
427 "Failed to read error status block!\n");
428 return -EIO;
429 }
430
431 return 0;
432 }
433
ghes_read_estatus(struct ghes * ghes,struct acpi_hest_generic_status * estatus,u64 * buf_paddr,enum fixed_addresses fixmap_idx)434 static int ghes_read_estatus(struct ghes *ghes,
435 struct acpi_hest_generic_status *estatus,
436 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
437 {
438 int rc;
439
440 rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
441 if (rc)
442 return rc;
443
444 rc = __ghes_check_estatus(ghes, estatus);
445 if (rc)
446 return rc;
447
448 return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
449 cper_estatus_len(estatus));
450 }
451
ghes_clear_estatus(struct ghes * ghes,struct acpi_hest_generic_status * estatus,u64 buf_paddr,enum fixed_addresses fixmap_idx)452 static void ghes_clear_estatus(struct ghes *ghes,
453 struct acpi_hest_generic_status *estatus,
454 u64 buf_paddr, enum fixed_addresses fixmap_idx)
455 {
456 estatus->block_status = 0;
457
458 if (!buf_paddr)
459 return;
460
461 ghes_copy_tofrom_phys(estatus, buf_paddr,
462 sizeof(estatus->block_status), 0,
463 fixmap_idx);
464
465 /*
466 * GHESv2 type HEST entries introduce support for error acknowledgment,
467 * so only acknowledge the error if this support is present.
468 */
469 if (is_hest_type_generic_v2(ghes))
470 ghes_ack_error(ghes->generic_v2);
471 }
472
473 /**
474 * struct ghes_task_work - for synchronous RAS event
475 *
476 * @twork: callback_head for task work
477 * @pfn: page frame number of corrupted page
478 * @flags: work control flags
479 *
480 * Structure to pass task work to be handled before
481 * returning to user-space via task_work_add().
482 */
483 struct ghes_task_work {
484 struct callback_head twork;
485 u64 pfn;
486 int flags;
487 };
488
memory_failure_cb(struct callback_head * twork)489 static void memory_failure_cb(struct callback_head *twork)
490 {
491 struct ghes_task_work *twcb = container_of(twork, struct ghes_task_work, twork);
492 int ret;
493
494 ret = memory_failure(twcb->pfn, twcb->flags);
495 gen_pool_free(ghes_estatus_pool, (unsigned long)twcb, sizeof(*twcb));
496
497 if (!ret || ret == -EHWPOISON || ret == -EOPNOTSUPP)
498 return;
499
500 pr_err("%#llx: Sending SIGBUS to %s:%d due to hardware memory corruption\n",
501 twcb->pfn, current->comm, task_pid_nr(current));
502 force_sig(SIGBUS);
503 }
504
ghes_do_memory_failure(u64 physical_addr,int flags)505 static bool ghes_do_memory_failure(u64 physical_addr, int flags)
506 {
507 struct ghes_task_work *twcb;
508 unsigned long pfn;
509
510 if (!IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE))
511 return false;
512
513 pfn = PHYS_PFN(physical_addr);
514
515 if (flags == MF_ACTION_REQUIRED && current->mm) {
516 twcb = (void *)gen_pool_alloc(ghes_estatus_pool, sizeof(*twcb));
517 if (!twcb)
518 return false;
519
520 twcb->pfn = pfn;
521 twcb->flags = flags;
522 init_task_work(&twcb->twork, memory_failure_cb);
523 task_work_add(current, &twcb->twork, TWA_RESUME);
524 return true;
525 }
526
527 memory_failure_queue(pfn, flags);
528 return true;
529 }
530
ghes_handle_memory_failure(struct acpi_hest_generic_data * gdata,int sev,bool sync)531 static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
532 int sev, bool sync)
533 {
534 int flags = -1;
535 int sec_sev = ghes_severity(gdata->error_severity);
536 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
537
538 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
539 return false;
540
541 /* iff following two events can be handled properly by now */
542 if (sec_sev == GHES_SEV_CORRECTED &&
543 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
544 flags = MF_SOFT_OFFLINE;
545 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
546 flags = sync ? MF_ACTION_REQUIRED : 0;
547
548 if (flags != -1)
549 return ghes_do_memory_failure(mem_err->physical_addr, flags);
550
551 return false;
552 }
553
ghes_handle_arm_hw_error(struct acpi_hest_generic_data * gdata,int sev,bool sync)554 static bool ghes_handle_arm_hw_error(struct acpi_hest_generic_data *gdata,
555 int sev, bool sync)
556 {
557 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
558 int flags = sync ? MF_ACTION_REQUIRED : 0;
559 int length = gdata->error_data_length;
560 char error_type[120];
561 bool queued = false;
562 int sec_sev, i;
563 char *p;
564
565 sec_sev = ghes_severity(gdata->error_severity);
566 if (length >= sizeof(*err)) {
567 log_arm_hw_error(err, sec_sev);
568 } else {
569 pr_warn(FW_BUG "arm error length: %d\n", length);
570 pr_warn(FW_BUG "length is too small\n");
571 pr_warn(FW_BUG "firmware-generated error record is incorrect\n");
572 return false;
573 }
574
575 if (sev != GHES_SEV_RECOVERABLE || sec_sev != GHES_SEV_RECOVERABLE)
576 return false;
577
578 p = (char *)(err + 1);
579 length -= sizeof(*err);
580
581 for (i = 0; i < err->err_info_num; i++) {
582 struct cper_arm_err_info *err_info;
583 bool is_cache, has_pa;
584
585 /* Ensure we have enough data for the error info header */
586 if (length < sizeof(*err_info))
587 break;
588
589 err_info = (struct cper_arm_err_info *)p;
590
591 /* Validate the claimed length before using it */
592 length -= err_info->length;
593 if (length < 0)
594 break;
595
596 is_cache = err_info->type & CPER_ARM_CACHE_ERROR;
597 has_pa = (err_info->validation_bits & CPER_ARM_INFO_VALID_PHYSICAL_ADDR);
598
599 /*
600 * The field (err_info->error_info & BIT(26)) is fixed to set to
601 * 1 in some old firmware of HiSilicon Kunpeng920. We assume that
602 * firmware won't mix corrected errors in an uncorrected section,
603 * and don't filter out 'corrected' error here.
604 */
605 if (is_cache && has_pa) {
606 queued = ghes_do_memory_failure(err_info->physical_fault_addr, flags);
607 p += err_info->length;
608 continue;
609 }
610
611 cper_bits_to_str(error_type, sizeof(error_type),
612 FIELD_GET(CPER_ARM_ERR_TYPE_MASK, err_info->type),
613 cper_proc_error_type_strs,
614 ARRAY_SIZE(cper_proc_error_type_strs));
615
616 pr_warn_ratelimited(FW_WARN GHES_PFX
617 "Unhandled processor error type 0x%02x: %s%s\n",
618 err_info->type, error_type,
619 (err_info->type & ~CPER_ARM_ERR_TYPE_MASK) ? " with reserved bit(s)" : "");
620 p += err_info->length;
621 }
622
623 return queued;
624 }
625
626 /*
627 * PCIe AER errors need to be sent to the AER driver for reporting and
628 * recovery. The GHES severities map to the following AER severities and
629 * require the following handling:
630 *
631 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
632 * These need to be reported by the AER driver but no recovery is
633 * necessary.
634 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
635 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
636 * These both need to be reported and recovered from by the AER driver.
637 * GHES_SEV_PANIC does not make it to this handling since the kernel must
638 * panic.
639 */
ghes_handle_aer(struct acpi_hest_generic_data * gdata)640 static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
641 {
642 #ifdef CONFIG_ACPI_APEI_PCIEAER
643 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
644
645 if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
646 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
647 unsigned int devfn;
648 int aer_severity;
649 u8 *aer_info;
650
651 devfn = PCI_DEVFN(pcie_err->device_id.device,
652 pcie_err->device_id.function);
653 aer_severity = cper_severity_to_aer(gdata->error_severity);
654
655 /*
656 * If firmware reset the component to contain
657 * the error, we must reinitialize it before
658 * use, so treat it as a fatal AER error.
659 */
660 if (gdata->flags & CPER_SEC_RESET)
661 aer_severity = AER_FATAL;
662
663 aer_info = (void *)gen_pool_alloc(ghes_estatus_pool,
664 sizeof(struct aer_capability_regs));
665 if (!aer_info)
666 return;
667 memcpy(aer_info, pcie_err->aer_info, sizeof(struct aer_capability_regs));
668
669 aer_recover_queue(pcie_err->device_id.segment,
670 pcie_err->device_id.bus,
671 devfn, aer_severity,
672 (struct aer_capability_regs *)
673 aer_info);
674 }
675 #endif
676 }
677
678 static BLOCKING_NOTIFIER_HEAD(vendor_record_notify_list);
679
ghes_register_vendor_record_notifier(struct notifier_block * nb)680 int ghes_register_vendor_record_notifier(struct notifier_block *nb)
681 {
682 return blocking_notifier_chain_register(&vendor_record_notify_list, nb);
683 }
684 EXPORT_SYMBOL_GPL(ghes_register_vendor_record_notifier);
685
ghes_unregister_vendor_record_notifier(struct notifier_block * nb)686 void ghes_unregister_vendor_record_notifier(struct notifier_block *nb)
687 {
688 blocking_notifier_chain_unregister(&vendor_record_notify_list, nb);
689 }
690 EXPORT_SYMBOL_GPL(ghes_unregister_vendor_record_notifier);
691
ghes_vendor_record_notifier_destroy(void * nb)692 static void ghes_vendor_record_notifier_destroy(void *nb)
693 {
694 ghes_unregister_vendor_record_notifier(nb);
695 }
696
devm_ghes_register_vendor_record_notifier(struct device * dev,struct notifier_block * nb)697 int devm_ghes_register_vendor_record_notifier(struct device *dev,
698 struct notifier_block *nb)
699 {
700 int ret;
701
702 ret = ghes_register_vendor_record_notifier(nb);
703 if (ret)
704 return ret;
705
706 return devm_add_action_or_reset(dev, ghes_vendor_record_notifier_destroy, nb);
707 }
708 EXPORT_SYMBOL_GPL(devm_ghes_register_vendor_record_notifier);
709
ghes_vendor_record_work_func(struct work_struct * work)710 static void ghes_vendor_record_work_func(struct work_struct *work)
711 {
712 struct ghes_vendor_record_entry *entry;
713 struct acpi_hest_generic_data *gdata;
714 u32 len;
715
716 entry = container_of(work, struct ghes_vendor_record_entry, work);
717 gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry);
718
719 blocking_notifier_call_chain(&vendor_record_notify_list,
720 entry->error_severity, gdata);
721
722 len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata));
723 gen_pool_free(ghes_estatus_pool, (unsigned long)entry, len);
724 }
725
ghes_defer_non_standard_event(struct acpi_hest_generic_data * gdata,int sev)726 static void ghes_defer_non_standard_event(struct acpi_hest_generic_data *gdata,
727 int sev)
728 {
729 struct acpi_hest_generic_data *copied_gdata;
730 struct ghes_vendor_record_entry *entry;
731 u32 len;
732
733 len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata));
734 entry = (void *)gen_pool_alloc(ghes_estatus_pool, len);
735 if (!entry)
736 return;
737
738 copied_gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry);
739 memcpy(copied_gdata, gdata, acpi_hest_get_record_size(gdata));
740 entry->error_severity = sev;
741
742 INIT_WORK(&entry->work, ghes_vendor_record_work_func);
743 schedule_work(&entry->work);
744 }
745
746 /* Room for 8 entries */
747 #define CXL_CPER_PROT_ERR_FIFO_DEPTH 8
748 static DEFINE_KFIFO(cxl_cper_prot_err_fifo, struct cxl_cper_prot_err_work_data,
749 CXL_CPER_PROT_ERR_FIFO_DEPTH);
750
751 /* Synchronize schedule_work() with cxl_cper_prot_err_work changes */
752 static DEFINE_RAW_SPINLOCK(cxl_cper_prot_err_work_lock);
753 struct work_struct *cxl_cper_prot_err_work;
754
cxl_cper_post_prot_err(struct cxl_cper_sec_prot_err * prot_err,int severity)755 static void cxl_cper_post_prot_err(struct cxl_cper_sec_prot_err *prot_err,
756 int severity)
757 {
758 #ifdef CONFIG_ACPI_APEI_PCIEAER
759 struct cxl_cper_prot_err_work_data wd;
760
761 if (cxl_cper_sec_prot_err_valid(prot_err))
762 return;
763
764 guard(raw_spinlock_irqsave)(&cxl_cper_prot_err_work_lock);
765
766 if (!cxl_cper_prot_err_work)
767 return;
768
769 if (cxl_cper_setup_prot_err_work_data(&wd, prot_err, severity))
770 return;
771
772 if (!kfifo_put(&cxl_cper_prot_err_fifo, wd)) {
773 pr_err_ratelimited("CXL CPER kfifo overflow\n");
774 return;
775 }
776
777 schedule_work(cxl_cper_prot_err_work);
778 #endif
779 }
780
cxl_cper_register_prot_err_work(struct work_struct * work)781 void cxl_cper_register_prot_err_work(struct work_struct *work)
782 {
783 guard(raw_spinlock_irqsave)(&cxl_cper_prot_err_work_lock);
784
785 if (WARN_ONCE(cxl_cper_prot_err_work,
786 "CPER-CXL kfifo consumer already registered\n"))
787 return;
788 cxl_cper_prot_err_work = work;
789 }
790 EXPORT_SYMBOL_FOR_MODULES(cxl_cper_register_prot_err_work, "cxl_core");
791
cxl_cper_unregister_prot_err_work(void)792 void cxl_cper_unregister_prot_err_work(void)
793 {
794 struct work_struct *old;
795
796 scoped_guard(raw_spinlock_irqsave, &cxl_cper_prot_err_work_lock) {
797 WARN_ONCE(!cxl_cper_prot_err_work,
798 "CPER-CXL kfifo consumer not registered on unregister\n");
799 old = cxl_cper_prot_err_work;
800 cxl_cper_prot_err_work = NULL;
801 }
802
803 if (old)
804 cancel_work_sync(old);
805
806 /* Discard stale entries so they are not replayed on next module load */
807 kfifo_reset(&cxl_cper_prot_err_fifo);
808 }
809 EXPORT_SYMBOL_FOR_MODULES(cxl_cper_unregister_prot_err_work, "cxl_core");
810
cxl_cper_prot_err_kfifo_get(struct cxl_cper_prot_err_work_data * wd)811 int cxl_cper_prot_err_kfifo_get(struct cxl_cper_prot_err_work_data *wd)
812 {
813 return kfifo_get(&cxl_cper_prot_err_fifo, wd);
814 }
815 EXPORT_SYMBOL_FOR_MODULES(cxl_cper_prot_err_kfifo_get, "cxl_core");
816
817 /* Room for 8 entries for each of the 4 event log queues */
818 #define CXL_CPER_FIFO_DEPTH 32
819 DEFINE_KFIFO(cxl_cper_fifo, struct cxl_cper_work_data, CXL_CPER_FIFO_DEPTH);
820
821 /* Synchronize schedule_work() with cxl_cper_work changes */
822 static DEFINE_RAW_SPINLOCK(cxl_cper_work_lock);
823 struct work_struct *cxl_cper_work;
824
cxl_cper_post_event(enum cxl_event_type event_type,struct cxl_cper_event_rec * rec)825 static void cxl_cper_post_event(enum cxl_event_type event_type,
826 struct cxl_cper_event_rec *rec)
827 {
828 struct cxl_cper_work_data wd;
829
830 if (rec->hdr.length <= sizeof(rec->hdr) ||
831 rec->hdr.length > sizeof(*rec)) {
832 pr_err(FW_WARN "CXL CPER Invalid section length (%u)\n",
833 rec->hdr.length);
834 return;
835 }
836
837 if (!(rec->hdr.validation_bits & CPER_CXL_COMP_EVENT_LOG_VALID)) {
838 pr_err(FW_WARN "CXL CPER invalid event\n");
839 return;
840 }
841
842 guard(raw_spinlock_irqsave)(&cxl_cper_work_lock);
843
844 if (!cxl_cper_work)
845 return;
846
847 wd.event_type = event_type;
848 memcpy(&wd.rec, rec, sizeof(wd.rec));
849
850 if (!kfifo_put(&cxl_cper_fifo, wd)) {
851 pr_err_ratelimited("CXL CPER kfifo overflow\n");
852 return;
853 }
854
855 schedule_work(cxl_cper_work);
856 }
857
cxl_cper_register_work(struct work_struct * work)858 int cxl_cper_register_work(struct work_struct *work)
859 {
860 guard(raw_spinlock_irqsave)(&cxl_cper_work_lock);
861 if (WARN_ONCE(cxl_cper_work,
862 "CXL CPER kfifo consumer already registered\n"))
863 return -EINVAL;
864
865 cxl_cper_work = work;
866 return 0;
867 }
868 EXPORT_SYMBOL_NS_GPL(cxl_cper_register_work, "CXL");
869
cxl_cper_unregister_work(struct work_struct * work)870 void cxl_cper_unregister_work(struct work_struct *work)
871 {
872 scoped_guard(raw_spinlock_irqsave, &cxl_cper_work_lock) {
873 if (WARN_ONCE(cxl_cper_work != work,
874 "CXL CPER kfifo consumer mismatch on unregister\n"))
875 return;
876 cxl_cper_work = NULL;
877 }
878
879 cancel_work_sync(work);
880
881 /* Discard stale entries so they are not replayed on next module load */
882 kfifo_reset(&cxl_cper_fifo);
883 }
884 EXPORT_SYMBOL_NS_GPL(cxl_cper_unregister_work, "CXL");
885
cxl_cper_kfifo_get(struct cxl_cper_work_data * wd)886 int cxl_cper_kfifo_get(struct cxl_cper_work_data *wd)
887 {
888 return kfifo_get(&cxl_cper_fifo, wd);
889 }
890 EXPORT_SYMBOL_NS_GPL(cxl_cper_kfifo_get, "CXL");
891
ghes_log_hwerr(int sev,guid_t * sec_type)892 static void ghes_log_hwerr(int sev, guid_t *sec_type)
893 {
894 if (sev != CPER_SEV_RECOVERABLE)
895 return;
896
897 if (guid_equal(sec_type, &CPER_SEC_PROC_ARM) ||
898 guid_equal(sec_type, &CPER_SEC_PROC_GENERIC) ||
899 guid_equal(sec_type, &CPER_SEC_PROC_IA)) {
900 hwerr_log_error_type(HWERR_RECOV_CPU);
901 return;
902 }
903
904 if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR) ||
905 guid_equal(sec_type, &CPER_SEC_CXL_GEN_MEDIA_GUID) ||
906 guid_equal(sec_type, &CPER_SEC_CXL_DRAM_GUID) ||
907 guid_equal(sec_type, &CPER_SEC_CXL_MEM_MODULE_GUID)) {
908 hwerr_log_error_type(HWERR_RECOV_CXL);
909 return;
910 }
911
912 if (guid_equal(sec_type, &CPER_SEC_PCIE) ||
913 guid_equal(sec_type, &CPER_SEC_PCI_X_BUS)) {
914 hwerr_log_error_type(HWERR_RECOV_PCI);
915 return;
916 }
917
918 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
919 hwerr_log_error_type(HWERR_RECOV_MEMORY);
920 return;
921 }
922
923 hwerr_log_error_type(HWERR_RECOV_OTHERS);
924 }
925
ghes_do_proc(struct ghes * ghes,const struct acpi_hest_generic_status * estatus)926 static void ghes_do_proc(struct ghes *ghes,
927 const struct acpi_hest_generic_status *estatus)
928 {
929 int sev, sec_sev;
930 struct acpi_hest_generic_data *gdata;
931 guid_t *sec_type;
932 const guid_t *fru_id = &guid_null;
933 char *fru_text = "";
934 bool queued = false;
935 bool sync = is_hest_sync_notify(ghes);
936
937 sev = ghes_severity(estatus->error_severity);
938 apei_estatus_for_each_section(estatus, gdata) {
939 sec_type = (guid_t *)gdata->section_type;
940 sec_sev = ghes_severity(gdata->error_severity);
941 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
942 fru_id = (guid_t *)gdata->fru_id;
943
944 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
945 fru_text = gdata->fru_text;
946
947 ghes_log_hwerr(sev, sec_type);
948 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
949 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
950
951 atomic_notifier_call_chain(&ghes_report_chain, sev, mem_err);
952
953 arch_apei_report_mem_error(sev, mem_err);
954 queued = ghes_handle_memory_failure(gdata, sev, sync);
955 } else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
956 ghes_handle_aer(gdata);
957 } else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
958 queued = ghes_handle_arm_hw_error(gdata, sev, sync);
959 } else if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR)) {
960 struct cxl_cper_sec_prot_err *prot_err = acpi_hest_get_payload(gdata);
961
962 cxl_cper_post_prot_err(prot_err, gdata->error_severity);
963 } else if (guid_equal(sec_type, &CPER_SEC_CXL_GEN_MEDIA_GUID)) {
964 struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata);
965
966 cxl_cper_post_event(CXL_CPER_EVENT_GEN_MEDIA, rec);
967 } else if (guid_equal(sec_type, &CPER_SEC_CXL_DRAM_GUID)) {
968 struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata);
969
970 cxl_cper_post_event(CXL_CPER_EVENT_DRAM, rec);
971 } else if (guid_equal(sec_type, &CPER_SEC_CXL_MEM_MODULE_GUID)) {
972 struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata);
973
974 cxl_cper_post_event(CXL_CPER_EVENT_MEM_MODULE, rec);
975 } else {
976 void *err = acpi_hest_get_payload(gdata);
977
978 ghes_defer_non_standard_event(gdata, sev);
979 log_non_standard_event(sec_type, fru_id, fru_text,
980 sec_sev, err,
981 gdata->error_data_length);
982 }
983 }
984
985 /*
986 * If no memory failure work is queued for abnormal synchronous
987 * errors, do a force kill.
988 */
989 if (sync && !queued) {
990 dev_err(ghes->dev,
991 HW_ERR GHES_PFX "%s:%d: synchronous unrecoverable error (SIGBUS)\n",
992 current->comm, task_pid_nr(current));
993 force_sig(SIGBUS);
994 }
995 }
996
__ghes_print_estatus(const char * pfx,const struct acpi_hest_generic * generic,const struct acpi_hest_generic_status * estatus)997 static void __ghes_print_estatus(const char *pfx,
998 const struct acpi_hest_generic *generic,
999 const struct acpi_hest_generic_status *estatus)
1000 {
1001 static atomic_t seqno;
1002 unsigned int curr_seqno;
1003 char pfx_seq[64];
1004
1005 if (pfx == NULL) {
1006 if (ghes_severity(estatus->error_severity) <=
1007 GHES_SEV_CORRECTED)
1008 pfx = KERN_WARNING;
1009 else
1010 pfx = KERN_ERR;
1011 }
1012 curr_seqno = atomic_inc_return(&seqno);
1013 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
1014 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
1015 pfx_seq, generic->header.source_id);
1016 cper_estatus_print(pfx_seq, estatus);
1017 }
1018
ghes_print_estatus(const char * pfx,const struct acpi_hest_generic * generic,const struct acpi_hest_generic_status * estatus)1019 static int ghes_print_estatus(const char *pfx,
1020 const struct acpi_hest_generic *generic,
1021 const struct acpi_hest_generic_status *estatus)
1022 {
1023 /* Not more than 2 messages every 5 seconds */
1024 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
1025 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
1026 struct ratelimit_state *ratelimit;
1027
1028 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
1029 ratelimit = &ratelimit_corrected;
1030 else
1031 ratelimit = &ratelimit_uncorrected;
1032 if (__ratelimit(ratelimit)) {
1033 __ghes_print_estatus(pfx, generic, estatus);
1034 return 1;
1035 }
1036 return 0;
1037 }
1038
1039 /*
1040 * GHES error status reporting throttle, to report more kinds of
1041 * errors, instead of just most frequently occurred errors.
1042 */
ghes_estatus_cached(struct acpi_hest_generic_status * estatus)1043 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
1044 {
1045 u32 len;
1046 int i, cached = 0;
1047 unsigned long long now;
1048 struct ghes_estatus_cache *cache;
1049 struct acpi_hest_generic_status *cache_estatus;
1050
1051 len = cper_estatus_len(estatus);
1052 rcu_read_lock();
1053 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
1054 cache = rcu_dereference(ghes_estatus_caches[i]);
1055 if (cache == NULL)
1056 continue;
1057 if (len != cache->estatus_len)
1058 continue;
1059 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
1060 if (memcmp(estatus, cache_estatus, len))
1061 continue;
1062 atomic_inc(&cache->count);
1063 now = sched_clock();
1064 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
1065 cached = 1;
1066 break;
1067 }
1068 rcu_read_unlock();
1069 return cached;
1070 }
1071
ghes_estatus_cache_alloc(struct acpi_hest_generic * generic,struct acpi_hest_generic_status * estatus)1072 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
1073 struct acpi_hest_generic *generic,
1074 struct acpi_hest_generic_status *estatus)
1075 {
1076 int alloced;
1077 u32 len, cache_len;
1078 struct ghes_estatus_cache *cache;
1079 struct acpi_hest_generic_status *cache_estatus;
1080
1081 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
1082 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
1083 atomic_dec(&ghes_estatus_cache_alloced);
1084 return NULL;
1085 }
1086 len = cper_estatus_len(estatus);
1087 cache_len = GHES_ESTATUS_CACHE_LEN(len);
1088 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
1089 if (!cache) {
1090 atomic_dec(&ghes_estatus_cache_alloced);
1091 return NULL;
1092 }
1093 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
1094 memcpy(cache_estatus, estatus, len);
1095 cache->estatus_len = len;
1096 atomic_set(&cache->count, 0);
1097 cache->generic = generic;
1098 cache->time_in = sched_clock();
1099 return cache;
1100 }
1101
ghes_estatus_cache_rcu_free(struct rcu_head * head)1102 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
1103 {
1104 struct ghes_estatus_cache *cache;
1105 u32 len;
1106
1107 cache = container_of(head, struct ghes_estatus_cache, rcu);
1108 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
1109 len = GHES_ESTATUS_CACHE_LEN(len);
1110 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
1111 atomic_dec(&ghes_estatus_cache_alloced);
1112 }
1113
1114 static void
ghes_estatus_cache_add(struct acpi_hest_generic * generic,struct acpi_hest_generic_status * estatus)1115 ghes_estatus_cache_add(struct acpi_hest_generic *generic,
1116 struct acpi_hest_generic_status *estatus)
1117 {
1118 unsigned long long now, duration, period, max_period = 0;
1119 struct ghes_estatus_cache *cache, *new_cache;
1120 struct ghes_estatus_cache __rcu *victim;
1121 int i, slot = -1, count;
1122
1123 new_cache = ghes_estatus_cache_alloc(generic, estatus);
1124 if (!new_cache)
1125 return;
1126
1127 rcu_read_lock();
1128 now = sched_clock();
1129 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
1130 cache = rcu_dereference(ghes_estatus_caches[i]);
1131 if (cache == NULL) {
1132 slot = i;
1133 break;
1134 }
1135 duration = now - cache->time_in;
1136 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
1137 slot = i;
1138 break;
1139 }
1140 count = atomic_read(&cache->count);
1141 period = duration;
1142 do_div(period, (count + 1));
1143 if (period > max_period) {
1144 max_period = period;
1145 slot = i;
1146 }
1147 }
1148 rcu_read_unlock();
1149
1150 if (slot != -1) {
1151 /*
1152 * Use release semantics to ensure that ghes_estatus_cached()
1153 * running on another CPU will see the updated cache fields if
1154 * it can see the new value of the pointer.
1155 */
1156 victim = xchg_release(&ghes_estatus_caches[slot],
1157 RCU_INITIALIZER(new_cache));
1158
1159 /*
1160 * At this point, victim may point to a cached item different
1161 * from the one based on which we selected the slot. Instead of
1162 * going to the loop again to pick another slot, let's just
1163 * drop the other item anyway: this may cause a false cache
1164 * miss later on, but that won't cause any problems.
1165 */
1166 if (victim)
1167 call_rcu(&unrcu_pointer(victim)->rcu,
1168 ghes_estatus_cache_rcu_free);
1169 }
1170 }
1171
__ghes_panic(struct ghes * ghes,struct acpi_hest_generic_status * estatus,u64 buf_paddr,enum fixed_addresses fixmap_idx)1172 static void __ghes_panic(struct ghes *ghes,
1173 struct acpi_hest_generic_status *estatus,
1174 u64 buf_paddr, enum fixed_addresses fixmap_idx)
1175 {
1176 const char *msg = GHES_PFX "Fatal hardware error";
1177
1178 __ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
1179
1180 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
1181
1182 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
1183
1184 if (!panic_timeout)
1185 pr_emerg("%s but panic disabled\n", msg);
1186
1187 panic(msg);
1188 }
1189
ghes_proc(struct ghes * ghes)1190 static int ghes_proc(struct ghes *ghes)
1191 {
1192 struct acpi_hest_generic_status *estatus = ghes->estatus;
1193 u64 buf_paddr;
1194 int rc;
1195
1196 rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
1197 if (rc)
1198 goto out;
1199
1200 if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
1201 __ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
1202
1203 if (!ghes_estatus_cached(estatus)) {
1204 if (ghes_print_estatus(NULL, ghes->generic, estatus))
1205 ghes_estatus_cache_add(ghes->generic, estatus);
1206 }
1207 ghes_do_proc(ghes, estatus);
1208
1209 out:
1210 ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
1211
1212 return rc;
1213 }
1214
ghes_add_timer(struct ghes * ghes)1215 static void ghes_add_timer(struct ghes *ghes)
1216 {
1217 struct acpi_hest_generic *g = ghes->generic;
1218 unsigned long expire;
1219
1220 if (!g->notify.poll_interval) {
1221 pr_warn(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
1222 g->header.source_id);
1223 return;
1224 }
1225 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
1226 ghes->timer.expires = round_jiffies_relative(expire);
1227 add_timer(&ghes->timer);
1228 }
1229
ghes_poll_func(struct timer_list * t)1230 static void ghes_poll_func(struct timer_list *t)
1231 {
1232 struct ghes *ghes = timer_container_of(ghes, t, timer);
1233 unsigned long flags;
1234
1235 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1236 ghes_proc(ghes);
1237 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1238 if (!(ghes->flags & GHES_EXITING))
1239 ghes_add_timer(ghes);
1240 }
1241
ghes_irq_func(int irq,void * data)1242 static irqreturn_t ghes_irq_func(int irq, void *data)
1243 {
1244 struct ghes *ghes = data;
1245 unsigned long flags;
1246 int rc;
1247
1248 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1249 rc = ghes_proc(ghes);
1250 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1251 if (rc)
1252 return IRQ_NONE;
1253
1254 return IRQ_HANDLED;
1255 }
1256
ghes_notify_hed(struct notifier_block * this,unsigned long event,void * data)1257 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
1258 void *data)
1259 {
1260 struct ghes *ghes;
1261 unsigned long flags;
1262 int ret = NOTIFY_DONE;
1263
1264 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1265 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
1266 if (!ghes_proc(ghes))
1267 ret = NOTIFY_OK;
1268 }
1269 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1270
1271 return ret;
1272 }
1273
1274 static struct notifier_block ghes_notifier_hed = {
1275 .notifier_call = ghes_notify_hed,
1276 };
1277
1278 /*
1279 * Handlers for CPER records may not be NMI safe. For example,
1280 * memory_failure_queue() takes spinlocks and calls schedule_work_on().
1281 * In any NMI-like handler, memory from ghes_estatus_pool is used to save
1282 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
1283 * ghes_proc_in_irq() to run in IRQ context where each estatus in
1284 * ghes_estatus_llist is processed.
1285 *
1286 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
1287 * to suppress frequent messages.
1288 */
1289 static struct llist_head ghes_estatus_llist;
1290 static struct irq_work ghes_proc_irq_work;
1291
ghes_proc_in_irq(struct irq_work * irq_work)1292 static void ghes_proc_in_irq(struct irq_work *irq_work)
1293 {
1294 struct llist_node *llnode, *next;
1295 struct ghes_estatus_node *estatus_node;
1296 struct acpi_hest_generic *generic;
1297 struct acpi_hest_generic_status *estatus;
1298 u32 len, node_len;
1299
1300 llnode = llist_del_all(&ghes_estatus_llist);
1301 /*
1302 * Because the time order of estatus in list is reversed,
1303 * revert it back to proper order.
1304 */
1305 llnode = llist_reverse_order(llnode);
1306 while (llnode) {
1307 next = llnode->next;
1308 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
1309 llnode);
1310 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1311 len = cper_estatus_len(estatus);
1312 node_len = GHES_ESTATUS_NODE_LEN(len);
1313
1314 ghes_do_proc(estatus_node->ghes, estatus);
1315
1316 if (!ghes_estatus_cached(estatus)) {
1317 generic = estatus_node->generic;
1318 if (ghes_print_estatus(NULL, generic, estatus))
1319 ghes_estatus_cache_add(generic, estatus);
1320 }
1321 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
1322 node_len);
1323
1324 llnode = next;
1325 }
1326 }
1327
ghes_print_queued_estatus(void)1328 static void ghes_print_queued_estatus(void)
1329 {
1330 struct llist_node *llnode;
1331 struct ghes_estatus_node *estatus_node;
1332 struct acpi_hest_generic *generic;
1333 struct acpi_hest_generic_status *estatus;
1334
1335 llnode = llist_del_all(&ghes_estatus_llist);
1336 /*
1337 * Because the time order of estatus in list is reversed,
1338 * revert it back to proper order.
1339 */
1340 llnode = llist_reverse_order(llnode);
1341 while (llnode) {
1342 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
1343 llnode);
1344 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1345 generic = estatus_node->generic;
1346 ghes_print_estatus(NULL, generic, estatus);
1347 llnode = llnode->next;
1348 }
1349 }
1350
ghes_in_nmi_queue_one_entry(struct ghes * ghes,enum fixed_addresses fixmap_idx)1351 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
1352 enum fixed_addresses fixmap_idx)
1353 {
1354 struct acpi_hest_generic_status *estatus, tmp_header;
1355 struct ghes_estatus_node *estatus_node;
1356 u32 len, node_len;
1357 u64 buf_paddr;
1358 int sev, rc;
1359
1360 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
1361 return -EOPNOTSUPP;
1362
1363 rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
1364 if (rc) {
1365 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1366 return rc;
1367 }
1368
1369 rc = __ghes_check_estatus(ghes, &tmp_header);
1370 if (rc) {
1371 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1372 return rc;
1373 }
1374
1375 len = cper_estatus_len(&tmp_header);
1376 node_len = GHES_ESTATUS_NODE_LEN(len);
1377 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
1378 if (!estatus_node)
1379 return -ENOMEM;
1380
1381 estatus_node->ghes = ghes;
1382 estatus_node->generic = ghes->generic;
1383 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1384
1385 if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
1386 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
1387 rc = -ENOENT;
1388 goto no_work;
1389 }
1390
1391 sev = ghes_severity(estatus->error_severity);
1392 if (sev >= GHES_SEV_PANIC) {
1393 ghes_print_queued_estatus();
1394 __ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
1395 }
1396
1397 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1398
1399 /* This error has been reported before, don't process it again. */
1400 if (ghes_estatus_cached(estatus)) {
1401 /*
1402 * Return failure on duplicate SEA entries so that the
1403 * subsequent SEA handler invocation sends a SIGBUS signal to
1404 * the task to prevent it from re-entering the handler loop.
1405 */
1406 if (is_hest_sync_notify(ghes))
1407 rc = -ECANCELED;
1408 goto no_work;
1409 }
1410
1411 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
1412
1413 return rc;
1414
1415 no_work:
1416 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
1417 node_len);
1418
1419 return rc;
1420 }
1421
ghes_in_nmi_spool_from_list(struct list_head * rcu_list,enum fixed_addresses fixmap_idx)1422 static int __maybe_unused ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
1423 enum fixed_addresses fixmap_idx)
1424 {
1425 int ret = -ENOENT;
1426 struct ghes *ghes;
1427
1428 rcu_read_lock();
1429 list_for_each_entry_rcu(ghes, rcu_list, list) {
1430 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
1431 ret = 0;
1432 }
1433 rcu_read_unlock();
1434
1435 if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
1436 irq_work_queue(&ghes_proc_irq_work);
1437
1438 return ret;
1439 }
1440
1441 /**
1442 * ghes_has_active_errors - Check if there are active errors in error sources
1443 * @ghes_list: List of GHES entries to check for active errors
1444 *
1445 * This function iterates through all GHES entries in the given list and
1446 * checks if any of them has active error status by reading the error
1447 * status register.
1448 *
1449 * Return: true if at least one source has active error, false otherwise.
1450 */
ghes_has_active_errors(struct list_head * ghes_list)1451 static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
1452 {
1453 struct ghes *ghes;
1454
1455 guard(rcu)();
1456 list_for_each_entry_rcu(ghes, ghes_list, list) {
1457 if (ghes->error_status_vaddr &&
1458 readl(ghes->error_status_vaddr))
1459 return true;
1460 }
1461
1462 return false;
1463 }
1464
1465 /**
1466 * ghes_map_error_status - Map error status address to virtual address
1467 * @ghes: pointer to GHES structure
1468 *
1469 * Reads the error status address from ACPI HEST table and maps it to a virtual
1470 * address that can be accessed by the kernel.
1471 *
1472 * Return: 0 on success, error code on failure.
1473 */
ghes_map_error_status(struct ghes * ghes)1474 static int __maybe_unused ghes_map_error_status(struct ghes *ghes)
1475 {
1476 struct acpi_hest_generic *g = ghes->generic;
1477 u64 paddr;
1478 int rc;
1479
1480 rc = apei_read(&paddr, &g->error_status_address);
1481 if (rc)
1482 return rc;
1483
1484 ghes->error_status_vaddr =
1485 acpi_os_ioremap(paddr, sizeof(ghes->estatus->block_status));
1486 if (!ghes->error_status_vaddr)
1487 return -EINVAL;
1488
1489 return 0;
1490 }
1491
1492 /**
1493 * ghes_unmap_error_status - Unmap error status virtual address
1494 * @ghes: pointer to GHES structure
1495 *
1496 * Unmaps the error status address if it was previously mapped.
1497 */
ghes_unmap_error_status(struct ghes * ghes)1498 static void __maybe_unused ghes_unmap_error_status(struct ghes *ghes)
1499 {
1500 if (ghes->error_status_vaddr) {
1501 iounmap(ghes->error_status_vaddr);
1502 ghes->error_status_vaddr = NULL;
1503 }
1504 }
1505
1506 #ifdef CONFIG_ACPI_APEI_SEA
1507 static LIST_HEAD(ghes_sea);
1508
1509 /*
1510 * Return 0 only if one of the SEA error sources successfully reported an error
1511 * record sent from the firmware.
1512 */
ghes_notify_sea(void)1513 int ghes_notify_sea(void)
1514 {
1515 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
1516 int rv;
1517
1518 if (!ghes_has_active_errors(&ghes_sea))
1519 return -ENOENT;
1520
1521 raw_spin_lock(&ghes_notify_lock_sea);
1522 rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
1523 raw_spin_unlock(&ghes_notify_lock_sea);
1524
1525 return rv;
1526 }
1527
ghes_sea_add(struct ghes * ghes)1528 static int ghes_sea_add(struct ghes *ghes)
1529 {
1530 int rc;
1531
1532 rc = ghes_map_error_status(ghes);
1533 if (rc)
1534 return rc;
1535
1536 mutex_lock(&ghes_list_mutex);
1537 list_add_rcu(&ghes->list, &ghes_sea);
1538 mutex_unlock(&ghes_list_mutex);
1539
1540 return 0;
1541 }
1542
ghes_sea_remove(struct ghes * ghes)1543 static void ghes_sea_remove(struct ghes *ghes)
1544 {
1545 mutex_lock(&ghes_list_mutex);
1546 list_del_rcu(&ghes->list);
1547 mutex_unlock(&ghes_list_mutex);
1548 ghes_unmap_error_status(ghes);
1549 synchronize_rcu();
1550 }
1551 #else /* CONFIG_ACPI_APEI_SEA */
ghes_sea_add(struct ghes * ghes)1552 static inline int ghes_sea_add(struct ghes *ghes) { return -EINVAL; }
ghes_sea_remove(struct ghes * ghes)1553 static inline void ghes_sea_remove(struct ghes *ghes) { }
1554 #endif /* CONFIG_ACPI_APEI_SEA */
1555
1556 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
1557 /*
1558 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
1559 * having only one concurrent reader.
1560 */
1561 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
1562
1563 static LIST_HEAD(ghes_nmi);
1564
ghes_notify_nmi(unsigned int cmd,struct pt_regs * regs)1565 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
1566 {
1567 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1568 int ret = NMI_DONE;
1569
1570 if (!ghes_has_active_errors(&ghes_nmi))
1571 return ret;
1572
1573 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1574 return ret;
1575
1576 raw_spin_lock(&ghes_notify_lock_nmi);
1577 if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1578 ret = NMI_HANDLED;
1579 raw_spin_unlock(&ghes_notify_lock_nmi);
1580
1581 atomic_dec(&ghes_in_nmi);
1582 return ret;
1583 }
1584
ghes_nmi_add(struct ghes * ghes)1585 static int ghes_nmi_add(struct ghes *ghes)
1586 {
1587 int rc;
1588
1589 rc = ghes_map_error_status(ghes);
1590 if (rc)
1591 return rc;
1592
1593 mutex_lock(&ghes_list_mutex);
1594 if (list_empty(&ghes_nmi))
1595 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1596 list_add_rcu(&ghes->list, &ghes_nmi);
1597 mutex_unlock(&ghes_list_mutex);
1598
1599 return 0;
1600 }
1601
ghes_nmi_remove(struct ghes * ghes)1602 static void ghes_nmi_remove(struct ghes *ghes)
1603 {
1604 mutex_lock(&ghes_list_mutex);
1605 list_del_rcu(&ghes->list);
1606 if (list_empty(&ghes_nmi))
1607 unregister_nmi_handler(NMI_LOCAL, "ghes");
1608 mutex_unlock(&ghes_list_mutex);
1609
1610 ghes_unmap_error_status(ghes);
1611
1612 /*
1613 * To synchronize with NMI handler, ghes can only be
1614 * freed after NMI handler finishes.
1615 */
1616 synchronize_rcu();
1617 }
1618 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
ghes_nmi_add(struct ghes * ghes)1619 static inline int ghes_nmi_add(struct ghes *ghes) { return -EINVAL; }
ghes_nmi_remove(struct ghes * ghes)1620 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1621 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1622
ghes_nmi_init_cxt(void)1623 static void ghes_nmi_init_cxt(void)
1624 {
1625 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1626 }
1627
__ghes_sdei_callback(struct ghes * ghes,enum fixed_addresses fixmap_idx)1628 static int __ghes_sdei_callback(struct ghes *ghes,
1629 enum fixed_addresses fixmap_idx)
1630 {
1631 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1632 irq_work_queue(&ghes_proc_irq_work);
1633
1634 return 0;
1635 }
1636
1637 return -ENOENT;
1638 }
1639
ghes_sdei_normal_callback(u32 event_num,struct pt_regs * regs,void * arg)1640 static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1641 void *arg)
1642 {
1643 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1644 struct ghes *ghes = arg;
1645 int err;
1646
1647 raw_spin_lock(&ghes_notify_lock_sdei_normal);
1648 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1649 raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1650
1651 return err;
1652 }
1653
ghes_sdei_critical_callback(u32 event_num,struct pt_regs * regs,void * arg)1654 static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1655 void *arg)
1656 {
1657 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1658 struct ghes *ghes = arg;
1659 int err;
1660
1661 raw_spin_lock(&ghes_notify_lock_sdei_critical);
1662 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1663 raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1664
1665 return err;
1666 }
1667
apei_sdei_register_ghes(struct ghes * ghes)1668 static int apei_sdei_register_ghes(struct ghes *ghes)
1669 {
1670 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1671 return -EOPNOTSUPP;
1672
1673 return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1674 ghes_sdei_critical_callback);
1675 }
1676
apei_sdei_unregister_ghes(struct ghes * ghes)1677 static int apei_sdei_unregister_ghes(struct ghes *ghes)
1678 {
1679 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1680 return -EOPNOTSUPP;
1681
1682 return sdei_unregister_ghes(ghes);
1683 }
1684
ghes_probe(struct platform_device * ghes_dev)1685 static int ghes_probe(struct platform_device *ghes_dev)
1686 {
1687 struct acpi_hest_generic *generic;
1688 struct ghes *ghes = NULL;
1689 unsigned long flags;
1690
1691 int rc = -EINVAL;
1692
1693 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1694 if (!generic->enabled)
1695 return -ENODEV;
1696
1697 switch (generic->notify.type) {
1698 case ACPI_HEST_NOTIFY_POLLED:
1699 case ACPI_HEST_NOTIFY_EXTERNAL:
1700 case ACPI_HEST_NOTIFY_SCI:
1701 case ACPI_HEST_NOTIFY_GSIV:
1702 case ACPI_HEST_NOTIFY_GPIO:
1703 break;
1704
1705 case ACPI_HEST_NOTIFY_SEA:
1706 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1707 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1708 generic->header.source_id);
1709 rc = -ENOTSUPP;
1710 goto err;
1711 }
1712 break;
1713 case ACPI_HEST_NOTIFY_NMI:
1714 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1715 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1716 generic->header.source_id);
1717 goto err;
1718 }
1719 break;
1720 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1721 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1722 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1723 generic->header.source_id);
1724 goto err;
1725 }
1726 break;
1727 case ACPI_HEST_NOTIFY_LOCAL:
1728 pr_warn(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1729 generic->header.source_id);
1730 goto err;
1731 default:
1732 pr_warn(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1733 generic->notify.type, generic->header.source_id);
1734 goto err;
1735 }
1736
1737 rc = -EIO;
1738 if (generic->error_block_length <
1739 sizeof(struct acpi_hest_generic_status)) {
1740 pr_warn(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1741 generic->error_block_length, generic->header.source_id);
1742 goto err;
1743 }
1744 ghes = ghes_new(generic);
1745 if (IS_ERR(ghes)) {
1746 rc = PTR_ERR(ghes);
1747 ghes = NULL;
1748 goto err;
1749 }
1750
1751 switch (generic->notify.type) {
1752 case ACPI_HEST_NOTIFY_POLLED:
1753 timer_setup(&ghes->timer, ghes_poll_func, 0);
1754 ghes_add_timer(ghes);
1755 break;
1756 case ACPI_HEST_NOTIFY_EXTERNAL:
1757 /* External interrupt vector is GSI */
1758 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1759 if (rc) {
1760 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1761 generic->header.source_id);
1762 goto err;
1763 }
1764 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1765 "GHES IRQ", ghes);
1766 if (rc) {
1767 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1768 generic->header.source_id);
1769 goto err;
1770 }
1771 break;
1772
1773 case ACPI_HEST_NOTIFY_SCI:
1774 case ACPI_HEST_NOTIFY_GSIV:
1775 case ACPI_HEST_NOTIFY_GPIO:
1776 mutex_lock(&ghes_list_mutex);
1777 if (list_empty(&ghes_hed))
1778 register_acpi_hed_notifier(&ghes_notifier_hed);
1779 list_add_rcu(&ghes->list, &ghes_hed);
1780 mutex_unlock(&ghes_list_mutex);
1781 break;
1782
1783 case ACPI_HEST_NOTIFY_SEA:
1784 rc = ghes_sea_add(ghes);
1785 if (rc)
1786 goto err;
1787 break;
1788 case ACPI_HEST_NOTIFY_NMI:
1789 rc = ghes_nmi_add(ghes);
1790 if (rc)
1791 goto err;
1792 break;
1793 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1794 rc = apei_sdei_register_ghes(ghes);
1795 if (rc)
1796 goto err;
1797 break;
1798 default:
1799 BUG();
1800 }
1801
1802 platform_set_drvdata(ghes_dev, ghes);
1803
1804 ghes->dev = &ghes_dev->dev;
1805
1806 mutex_lock(&ghes_devs_mutex);
1807 list_add_tail(&ghes->elist, &ghes_devs);
1808 mutex_unlock(&ghes_devs_mutex);
1809
1810 /* Handle any pending errors right away */
1811 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1812 ghes_proc(ghes);
1813 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1814
1815 return 0;
1816
1817 err:
1818 if (ghes) {
1819 ghes_fini(ghes);
1820 kfree(ghes);
1821 }
1822 return rc;
1823 }
1824
ghes_remove(struct platform_device * ghes_dev)1825 static void ghes_remove(struct platform_device *ghes_dev)
1826 {
1827 int rc;
1828 struct ghes *ghes;
1829 struct acpi_hest_generic *generic;
1830
1831 ghes = platform_get_drvdata(ghes_dev);
1832 generic = ghes->generic;
1833
1834 ghes->flags |= GHES_EXITING;
1835 switch (generic->notify.type) {
1836 case ACPI_HEST_NOTIFY_POLLED:
1837 timer_shutdown_sync(&ghes->timer);
1838 break;
1839 case ACPI_HEST_NOTIFY_EXTERNAL:
1840 free_irq(ghes->irq, ghes);
1841 break;
1842
1843 case ACPI_HEST_NOTIFY_SCI:
1844 case ACPI_HEST_NOTIFY_GSIV:
1845 case ACPI_HEST_NOTIFY_GPIO:
1846 mutex_lock(&ghes_list_mutex);
1847 list_del_rcu(&ghes->list);
1848 if (list_empty(&ghes_hed))
1849 unregister_acpi_hed_notifier(&ghes_notifier_hed);
1850 mutex_unlock(&ghes_list_mutex);
1851 synchronize_rcu();
1852 break;
1853
1854 case ACPI_HEST_NOTIFY_SEA:
1855 ghes_sea_remove(ghes);
1856 break;
1857 case ACPI_HEST_NOTIFY_NMI:
1858 ghes_nmi_remove(ghes);
1859 break;
1860 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1861 rc = apei_sdei_unregister_ghes(ghes);
1862 if (rc) {
1863 /*
1864 * Returning early results in a resource leak, but we're
1865 * only here if stopping the hardware failed.
1866 */
1867 dev_err(&ghes_dev->dev, "Failed to unregister ghes (%pe)\n",
1868 ERR_PTR(rc));
1869 return;
1870 }
1871 break;
1872 default:
1873 BUG();
1874 break;
1875 }
1876
1877 ghes_fini(ghes);
1878
1879 mutex_lock(&ghes_devs_mutex);
1880 list_del(&ghes->elist);
1881 mutex_unlock(&ghes_devs_mutex);
1882
1883 kfree(ghes);
1884 }
1885
1886 static struct platform_driver ghes_platform_driver = {
1887 .driver = {
1888 .name = "GHES",
1889 },
1890 .probe = ghes_probe,
1891 .remove = ghes_remove,
1892 };
1893
acpi_ghes_init(void)1894 void __init acpi_ghes_init(void)
1895 {
1896 int rc;
1897
1898 acpi_sdei_init();
1899
1900 if (acpi_disabled)
1901 return;
1902
1903 switch (hest_disable) {
1904 case HEST_NOT_FOUND:
1905 return;
1906 case HEST_DISABLED:
1907 pr_info(GHES_PFX "HEST is not enabled!\n");
1908 return;
1909 default:
1910 break;
1911 }
1912
1913 if (ghes_disable) {
1914 pr_info(GHES_PFX "GHES is not enabled!\n");
1915 return;
1916 }
1917
1918 ghes_nmi_init_cxt();
1919
1920 rc = platform_driver_register(&ghes_platform_driver);
1921 if (rc)
1922 return;
1923
1924 rc = apei_osc_setup();
1925 if (rc == 0 && osc_sb_apei_support_acked)
1926 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1927 else if (rc == 0 && !osc_sb_apei_support_acked)
1928 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1929 else if (rc && osc_sb_apei_support_acked)
1930 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1931 else
1932 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1933 }
1934
1935 /*
1936 * Known x86 systems that prefer GHES error reporting:
1937 */
1938 static struct acpi_platform_list plat_list[] = {
1939 {"HPE ", "Server ", 0, ACPI_SIG_FADT, all_versions},
1940 {"__ZX__", "EDK2 ", 3, ACPI_SIG_FADT, greater_than_or_equal},
1941 {"_BYO_ ", "BYOSOFT ", 3, ACPI_SIG_FADT, greater_than_or_equal},
1942 { } /* End */
1943 };
1944
ghes_get_devices(void)1945 struct list_head *ghes_get_devices(void)
1946 {
1947 int idx = -1;
1948
1949 if (IS_ENABLED(CONFIG_X86)) {
1950 idx = acpi_match_platform_list(plat_list);
1951 if (idx < 0) {
1952 if (!ghes_edac_force_enable)
1953 return NULL;
1954
1955 pr_warn_once("Force-loading ghes_edac on an unsupported platform. You're on your own!\n");
1956 }
1957 } else if (list_empty(&ghes_devs)) {
1958 return NULL;
1959 }
1960
1961 return &ghes_devs;
1962 }
1963 EXPORT_SYMBOL_GPL(ghes_get_devices);
1964
ghes_register_report_chain(struct notifier_block * nb)1965 void ghes_register_report_chain(struct notifier_block *nb)
1966 {
1967 atomic_notifier_chain_register(&ghes_report_chain, nb);
1968 }
1969 EXPORT_SYMBOL_GPL(ghes_register_report_chain);
1970
ghes_unregister_report_chain(struct notifier_block * nb)1971 void ghes_unregister_report_chain(struct notifier_block *nb)
1972 {
1973 atomic_notifier_chain_unregister(&ghes_report_chain, nb);
1974 }
1975 EXPORT_SYMBOL_GPL(ghes_unregister_report_chain);
1976