xref: /linux/drivers/acpi/apei/ghes.c (revision 441fa10a5a1978e7a2f751f2d6f6a9194056262e)
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 
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  */
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 
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 
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 
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  */
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 
244 static int map_gen_v2(struct ghes *ghes)
245 {
246 	return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
247 }
248 
249 static void unmap_gen_v2(struct ghes *ghes)
250 {
251 	apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
252 }
253 
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 
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 
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 
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 
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. */
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. */
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 
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 
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 
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 
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 
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 
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 
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  */
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 
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 
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 
692 static void ghes_vendor_record_notifier_destroy(void *nb)
693 {
694 	ghes_unregister_vendor_record_notifier(nb);
695 }
696 
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 
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 
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_SPINLOCK(cxl_cper_prot_err_work_lock);
753 struct work_struct *cxl_cper_prot_err_work;
754 
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(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 
781 int cxl_cper_register_prot_err_work(struct work_struct *work)
782 {
783 	if (cxl_cper_prot_err_work)
784 		return -EINVAL;
785 
786 	guard(spinlock)(&cxl_cper_prot_err_work_lock);
787 	cxl_cper_prot_err_work = work;
788 	return 0;
789 }
790 EXPORT_SYMBOL_NS_GPL(cxl_cper_register_prot_err_work, "CXL");
791 
792 int cxl_cper_unregister_prot_err_work(struct work_struct *work)
793 {
794 	if (cxl_cper_prot_err_work != work)
795 		return -EINVAL;
796 
797 	guard(spinlock)(&cxl_cper_prot_err_work_lock);
798 	cxl_cper_prot_err_work = NULL;
799 	return 0;
800 }
801 EXPORT_SYMBOL_NS_GPL(cxl_cper_unregister_prot_err_work, "CXL");
802 
803 int cxl_cper_prot_err_kfifo_get(struct cxl_cper_prot_err_work_data *wd)
804 {
805 	return kfifo_get(&cxl_cper_prot_err_fifo, wd);
806 }
807 EXPORT_SYMBOL_NS_GPL(cxl_cper_prot_err_kfifo_get, "CXL");
808 
809 /* Room for 8 entries for each of the 4 event log queues */
810 #define CXL_CPER_FIFO_DEPTH 32
811 DEFINE_KFIFO(cxl_cper_fifo, struct cxl_cper_work_data, CXL_CPER_FIFO_DEPTH);
812 
813 /* Synchronize schedule_work() with cxl_cper_work changes */
814 static DEFINE_SPINLOCK(cxl_cper_work_lock);
815 struct work_struct *cxl_cper_work;
816 
817 static void cxl_cper_post_event(enum cxl_event_type event_type,
818 				struct cxl_cper_event_rec *rec)
819 {
820 	struct cxl_cper_work_data wd;
821 
822 	if (rec->hdr.length <= sizeof(rec->hdr) ||
823 	    rec->hdr.length > sizeof(*rec)) {
824 		pr_err(FW_WARN "CXL CPER Invalid section length (%u)\n",
825 		       rec->hdr.length);
826 		return;
827 	}
828 
829 	if (!(rec->hdr.validation_bits & CPER_CXL_COMP_EVENT_LOG_VALID)) {
830 		pr_err(FW_WARN "CXL CPER invalid event\n");
831 		return;
832 	}
833 
834 	guard(spinlock_irqsave)(&cxl_cper_work_lock);
835 
836 	if (!cxl_cper_work)
837 		return;
838 
839 	wd.event_type = event_type;
840 	memcpy(&wd.rec, rec, sizeof(wd.rec));
841 
842 	if (!kfifo_put(&cxl_cper_fifo, wd)) {
843 		pr_err_ratelimited("CXL CPER kfifo overflow\n");
844 		return;
845 	}
846 
847 	schedule_work(cxl_cper_work);
848 }
849 
850 int cxl_cper_register_work(struct work_struct *work)
851 {
852 	if (cxl_cper_work)
853 		return -EINVAL;
854 
855 	guard(spinlock)(&cxl_cper_work_lock);
856 	cxl_cper_work = work;
857 	return 0;
858 }
859 EXPORT_SYMBOL_NS_GPL(cxl_cper_register_work, "CXL");
860 
861 int cxl_cper_unregister_work(struct work_struct *work)
862 {
863 	if (cxl_cper_work != work)
864 		return -EINVAL;
865 
866 	guard(spinlock)(&cxl_cper_work_lock);
867 	cxl_cper_work = NULL;
868 	return 0;
869 }
870 EXPORT_SYMBOL_NS_GPL(cxl_cper_unregister_work, "CXL");
871 
872 int cxl_cper_kfifo_get(struct cxl_cper_work_data *wd)
873 {
874 	return kfifo_get(&cxl_cper_fifo, wd);
875 }
876 EXPORT_SYMBOL_NS_GPL(cxl_cper_kfifo_get, "CXL");
877 
878 static void ghes_log_hwerr(int sev, guid_t *sec_type)
879 {
880 	if (sev != CPER_SEV_RECOVERABLE)
881 		return;
882 
883 	if (guid_equal(sec_type, &CPER_SEC_PROC_ARM) ||
884 	    guid_equal(sec_type, &CPER_SEC_PROC_GENERIC) ||
885 	    guid_equal(sec_type, &CPER_SEC_PROC_IA)) {
886 		hwerr_log_error_type(HWERR_RECOV_CPU);
887 		return;
888 	}
889 
890 	if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR) ||
891 	    guid_equal(sec_type, &CPER_SEC_CXL_GEN_MEDIA_GUID) ||
892 	    guid_equal(sec_type, &CPER_SEC_CXL_DRAM_GUID) ||
893 	    guid_equal(sec_type, &CPER_SEC_CXL_MEM_MODULE_GUID)) {
894 		hwerr_log_error_type(HWERR_RECOV_CXL);
895 		return;
896 	}
897 
898 	if (guid_equal(sec_type, &CPER_SEC_PCIE) ||
899 	    guid_equal(sec_type, &CPER_SEC_PCI_X_BUS)) {
900 		hwerr_log_error_type(HWERR_RECOV_PCI);
901 		return;
902 	}
903 
904 	if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
905 		hwerr_log_error_type(HWERR_RECOV_MEMORY);
906 		return;
907 	}
908 
909 	hwerr_log_error_type(HWERR_RECOV_OTHERS);
910 }
911 
912 static void ghes_do_proc(struct ghes *ghes,
913 			 const struct acpi_hest_generic_status *estatus)
914 {
915 	int sev, sec_sev;
916 	struct acpi_hest_generic_data *gdata;
917 	guid_t *sec_type;
918 	const guid_t *fru_id = &guid_null;
919 	char *fru_text = "";
920 	bool queued = false;
921 	bool sync = is_hest_sync_notify(ghes);
922 
923 	sev = ghes_severity(estatus->error_severity);
924 	apei_estatus_for_each_section(estatus, gdata) {
925 		sec_type = (guid_t *)gdata->section_type;
926 		sec_sev = ghes_severity(gdata->error_severity);
927 		if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
928 			fru_id = (guid_t *)gdata->fru_id;
929 
930 		if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
931 			fru_text = gdata->fru_text;
932 
933 		ghes_log_hwerr(sev, sec_type);
934 		if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
935 			struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
936 
937 			atomic_notifier_call_chain(&ghes_report_chain, sev, mem_err);
938 
939 			arch_apei_report_mem_error(sev, mem_err);
940 			queued = ghes_handle_memory_failure(gdata, sev, sync);
941 		} else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
942 			ghes_handle_aer(gdata);
943 		} else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
944 			queued = ghes_handle_arm_hw_error(gdata, sev, sync);
945 		} else if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR)) {
946 			struct cxl_cper_sec_prot_err *prot_err = acpi_hest_get_payload(gdata);
947 
948 			cxl_cper_post_prot_err(prot_err, gdata->error_severity);
949 		} else if (guid_equal(sec_type, &CPER_SEC_CXL_GEN_MEDIA_GUID)) {
950 			struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata);
951 
952 			cxl_cper_post_event(CXL_CPER_EVENT_GEN_MEDIA, rec);
953 		} else if (guid_equal(sec_type, &CPER_SEC_CXL_DRAM_GUID)) {
954 			struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata);
955 
956 			cxl_cper_post_event(CXL_CPER_EVENT_DRAM, rec);
957 		} else if (guid_equal(sec_type, &CPER_SEC_CXL_MEM_MODULE_GUID)) {
958 			struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata);
959 
960 			cxl_cper_post_event(CXL_CPER_EVENT_MEM_MODULE, rec);
961 		} else {
962 			void *err = acpi_hest_get_payload(gdata);
963 
964 			ghes_defer_non_standard_event(gdata, sev);
965 			log_non_standard_event(sec_type, fru_id, fru_text,
966 					       sec_sev, err,
967 					       gdata->error_data_length);
968 		}
969 	}
970 
971 	/*
972 	 * If no memory failure work is queued for abnormal synchronous
973 	 * errors, do a force kill.
974 	 */
975 	if (sync && !queued) {
976 		dev_err(ghes->dev,
977 			HW_ERR GHES_PFX "%s:%d: synchronous unrecoverable error (SIGBUS)\n",
978 			current->comm, task_pid_nr(current));
979 		force_sig(SIGBUS);
980 	}
981 }
982 
983 static void __ghes_print_estatus(const char *pfx,
984 				 const struct acpi_hest_generic *generic,
985 				 const struct acpi_hest_generic_status *estatus)
986 {
987 	static atomic_t seqno;
988 	unsigned int curr_seqno;
989 	char pfx_seq[64];
990 
991 	if (pfx == NULL) {
992 		if (ghes_severity(estatus->error_severity) <=
993 		    GHES_SEV_CORRECTED)
994 			pfx = KERN_WARNING;
995 		else
996 			pfx = KERN_ERR;
997 	}
998 	curr_seqno = atomic_inc_return(&seqno);
999 	snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
1000 	printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
1001 	       pfx_seq, generic->header.source_id);
1002 	cper_estatus_print(pfx_seq, estatus);
1003 }
1004 
1005 static int ghes_print_estatus(const char *pfx,
1006 			      const struct acpi_hest_generic *generic,
1007 			      const struct acpi_hest_generic_status *estatus)
1008 {
1009 	/* Not more than 2 messages every 5 seconds */
1010 	static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
1011 	static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
1012 	struct ratelimit_state *ratelimit;
1013 
1014 	if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
1015 		ratelimit = &ratelimit_corrected;
1016 	else
1017 		ratelimit = &ratelimit_uncorrected;
1018 	if (__ratelimit(ratelimit)) {
1019 		__ghes_print_estatus(pfx, generic, estatus);
1020 		return 1;
1021 	}
1022 	return 0;
1023 }
1024 
1025 /*
1026  * GHES error status reporting throttle, to report more kinds of
1027  * errors, instead of just most frequently occurred errors.
1028  */
1029 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
1030 {
1031 	u32 len;
1032 	int i, cached = 0;
1033 	unsigned long long now;
1034 	struct ghes_estatus_cache *cache;
1035 	struct acpi_hest_generic_status *cache_estatus;
1036 
1037 	len = cper_estatus_len(estatus);
1038 	rcu_read_lock();
1039 	for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
1040 		cache = rcu_dereference(ghes_estatus_caches[i]);
1041 		if (cache == NULL)
1042 			continue;
1043 		if (len != cache->estatus_len)
1044 			continue;
1045 		cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
1046 		if (memcmp(estatus, cache_estatus, len))
1047 			continue;
1048 		atomic_inc(&cache->count);
1049 		now = sched_clock();
1050 		if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
1051 			cached = 1;
1052 		break;
1053 	}
1054 	rcu_read_unlock();
1055 	return cached;
1056 }
1057 
1058 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
1059 	struct acpi_hest_generic *generic,
1060 	struct acpi_hest_generic_status *estatus)
1061 {
1062 	int alloced;
1063 	u32 len, cache_len;
1064 	struct ghes_estatus_cache *cache;
1065 	struct acpi_hest_generic_status *cache_estatus;
1066 
1067 	alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
1068 	if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
1069 		atomic_dec(&ghes_estatus_cache_alloced);
1070 		return NULL;
1071 	}
1072 	len = cper_estatus_len(estatus);
1073 	cache_len = GHES_ESTATUS_CACHE_LEN(len);
1074 	cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
1075 	if (!cache) {
1076 		atomic_dec(&ghes_estatus_cache_alloced);
1077 		return NULL;
1078 	}
1079 	cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
1080 	memcpy(cache_estatus, estatus, len);
1081 	cache->estatus_len = len;
1082 	atomic_set(&cache->count, 0);
1083 	cache->generic = generic;
1084 	cache->time_in = sched_clock();
1085 	return cache;
1086 }
1087 
1088 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
1089 {
1090 	struct ghes_estatus_cache *cache;
1091 	u32 len;
1092 
1093 	cache = container_of(head, struct ghes_estatus_cache, rcu);
1094 	len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
1095 	len = GHES_ESTATUS_CACHE_LEN(len);
1096 	gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
1097 	atomic_dec(&ghes_estatus_cache_alloced);
1098 }
1099 
1100 static void
1101 ghes_estatus_cache_add(struct acpi_hest_generic *generic,
1102 		       struct acpi_hest_generic_status *estatus)
1103 {
1104 	unsigned long long now, duration, period, max_period = 0;
1105 	struct ghes_estatus_cache *cache, *new_cache;
1106 	struct ghes_estatus_cache __rcu *victim;
1107 	int i, slot = -1, count;
1108 
1109 	new_cache = ghes_estatus_cache_alloc(generic, estatus);
1110 	if (!new_cache)
1111 		return;
1112 
1113 	rcu_read_lock();
1114 	now = sched_clock();
1115 	for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
1116 		cache = rcu_dereference(ghes_estatus_caches[i]);
1117 		if (cache == NULL) {
1118 			slot = i;
1119 			break;
1120 		}
1121 		duration = now - cache->time_in;
1122 		if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
1123 			slot = i;
1124 			break;
1125 		}
1126 		count = atomic_read(&cache->count);
1127 		period = duration;
1128 		do_div(period, (count + 1));
1129 		if (period > max_period) {
1130 			max_period = period;
1131 			slot = i;
1132 		}
1133 	}
1134 	rcu_read_unlock();
1135 
1136 	if (slot != -1) {
1137 		/*
1138 		 * Use release semantics to ensure that ghes_estatus_cached()
1139 		 * running on another CPU will see the updated cache fields if
1140 		 * it can see the new value of the pointer.
1141 		 */
1142 		victim = xchg_release(&ghes_estatus_caches[slot],
1143 				      RCU_INITIALIZER(new_cache));
1144 
1145 		/*
1146 		 * At this point, victim may point to a cached item different
1147 		 * from the one based on which we selected the slot. Instead of
1148 		 * going to the loop again to pick another slot, let's just
1149 		 * drop the other item anyway: this may cause a false cache
1150 		 * miss later on, but that won't cause any problems.
1151 		 */
1152 		if (victim)
1153 			call_rcu(&unrcu_pointer(victim)->rcu,
1154 				 ghes_estatus_cache_rcu_free);
1155 	}
1156 }
1157 
1158 static void __ghes_panic(struct ghes *ghes,
1159 			 struct acpi_hest_generic_status *estatus,
1160 			 u64 buf_paddr, enum fixed_addresses fixmap_idx)
1161 {
1162 	const char *msg = GHES_PFX "Fatal hardware error";
1163 
1164 	__ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
1165 
1166 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
1167 
1168 	ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
1169 
1170 	if (!panic_timeout)
1171 		pr_emerg("%s but panic disabled\n", msg);
1172 
1173 	panic(msg);
1174 }
1175 
1176 static int ghes_proc(struct ghes *ghes)
1177 {
1178 	struct acpi_hest_generic_status *estatus = ghes->estatus;
1179 	u64 buf_paddr;
1180 	int rc;
1181 
1182 	rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
1183 	if (rc)
1184 		goto out;
1185 
1186 	if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
1187 		__ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
1188 
1189 	if (!ghes_estatus_cached(estatus)) {
1190 		if (ghes_print_estatus(NULL, ghes->generic, estatus))
1191 			ghes_estatus_cache_add(ghes->generic, estatus);
1192 	}
1193 	ghes_do_proc(ghes, estatus);
1194 
1195 out:
1196 	ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
1197 
1198 	return rc;
1199 }
1200 
1201 static void ghes_add_timer(struct ghes *ghes)
1202 {
1203 	struct acpi_hest_generic *g = ghes->generic;
1204 	unsigned long expire;
1205 
1206 	if (!g->notify.poll_interval) {
1207 		pr_warn(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
1208 			g->header.source_id);
1209 		return;
1210 	}
1211 	expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
1212 	ghes->timer.expires = round_jiffies_relative(expire);
1213 	add_timer(&ghes->timer);
1214 }
1215 
1216 static void ghes_poll_func(struct timer_list *t)
1217 {
1218 	struct ghes *ghes = timer_container_of(ghes, t, timer);
1219 	unsigned long flags;
1220 
1221 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1222 	ghes_proc(ghes);
1223 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1224 	if (!(ghes->flags & GHES_EXITING))
1225 		ghes_add_timer(ghes);
1226 }
1227 
1228 static irqreturn_t ghes_irq_func(int irq, void *data)
1229 {
1230 	struct ghes *ghes = data;
1231 	unsigned long flags;
1232 	int rc;
1233 
1234 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1235 	rc = ghes_proc(ghes);
1236 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1237 	if (rc)
1238 		return IRQ_NONE;
1239 
1240 	return IRQ_HANDLED;
1241 }
1242 
1243 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
1244 			   void *data)
1245 {
1246 	struct ghes *ghes;
1247 	unsigned long flags;
1248 	int ret = NOTIFY_DONE;
1249 
1250 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1251 	list_for_each_entry_rcu(ghes, &ghes_hed, list) {
1252 		if (!ghes_proc(ghes))
1253 			ret = NOTIFY_OK;
1254 	}
1255 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1256 
1257 	return ret;
1258 }
1259 
1260 static struct notifier_block ghes_notifier_hed = {
1261 	.notifier_call = ghes_notify_hed,
1262 };
1263 
1264 /*
1265  * Handlers for CPER records may not be NMI safe. For example,
1266  * memory_failure_queue() takes spinlocks and calls schedule_work_on().
1267  * In any NMI-like handler, memory from ghes_estatus_pool is used to save
1268  * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
1269  * ghes_proc_in_irq() to run in IRQ context where each estatus in
1270  * ghes_estatus_llist is processed.
1271  *
1272  * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
1273  * to suppress frequent messages.
1274  */
1275 static struct llist_head ghes_estatus_llist;
1276 static struct irq_work ghes_proc_irq_work;
1277 
1278 static void ghes_proc_in_irq(struct irq_work *irq_work)
1279 {
1280 	struct llist_node *llnode, *next;
1281 	struct ghes_estatus_node *estatus_node;
1282 	struct acpi_hest_generic *generic;
1283 	struct acpi_hest_generic_status *estatus;
1284 	u32 len, node_len;
1285 
1286 	llnode = llist_del_all(&ghes_estatus_llist);
1287 	/*
1288 	 * Because the time order of estatus in list is reversed,
1289 	 * revert it back to proper order.
1290 	 */
1291 	llnode = llist_reverse_order(llnode);
1292 	while (llnode) {
1293 		next = llnode->next;
1294 		estatus_node = llist_entry(llnode, struct ghes_estatus_node,
1295 					   llnode);
1296 		estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1297 		len = cper_estatus_len(estatus);
1298 		node_len = GHES_ESTATUS_NODE_LEN(len);
1299 
1300 		ghes_do_proc(estatus_node->ghes, estatus);
1301 
1302 		if (!ghes_estatus_cached(estatus)) {
1303 			generic = estatus_node->generic;
1304 			if (ghes_print_estatus(NULL, generic, estatus))
1305 				ghes_estatus_cache_add(generic, estatus);
1306 		}
1307 		gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
1308 			      node_len);
1309 
1310 		llnode = next;
1311 	}
1312 }
1313 
1314 static void ghes_print_queued_estatus(void)
1315 {
1316 	struct llist_node *llnode;
1317 	struct ghes_estatus_node *estatus_node;
1318 	struct acpi_hest_generic *generic;
1319 	struct acpi_hest_generic_status *estatus;
1320 
1321 	llnode = llist_del_all(&ghes_estatus_llist);
1322 	/*
1323 	 * Because the time order of estatus in list is reversed,
1324 	 * revert it back to proper order.
1325 	 */
1326 	llnode = llist_reverse_order(llnode);
1327 	while (llnode) {
1328 		estatus_node = llist_entry(llnode, struct ghes_estatus_node,
1329 					   llnode);
1330 		estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1331 		generic = estatus_node->generic;
1332 		ghes_print_estatus(NULL, generic, estatus);
1333 		llnode = llnode->next;
1334 	}
1335 }
1336 
1337 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
1338 				       enum fixed_addresses fixmap_idx)
1339 {
1340 	struct acpi_hest_generic_status *estatus, tmp_header;
1341 	struct ghes_estatus_node *estatus_node;
1342 	u32 len, node_len;
1343 	u64 buf_paddr;
1344 	int sev, rc;
1345 
1346 	if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
1347 		return -EOPNOTSUPP;
1348 
1349 	rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
1350 	if (rc) {
1351 		ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1352 		return rc;
1353 	}
1354 
1355 	rc = __ghes_check_estatus(ghes, &tmp_header);
1356 	if (rc) {
1357 		ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1358 		return rc;
1359 	}
1360 
1361 	len = cper_estatus_len(&tmp_header);
1362 	node_len = GHES_ESTATUS_NODE_LEN(len);
1363 	estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
1364 	if (!estatus_node)
1365 		return -ENOMEM;
1366 
1367 	estatus_node->ghes = ghes;
1368 	estatus_node->generic = ghes->generic;
1369 	estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1370 
1371 	if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
1372 		ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
1373 		rc = -ENOENT;
1374 		goto no_work;
1375 	}
1376 
1377 	sev = ghes_severity(estatus->error_severity);
1378 	if (sev >= GHES_SEV_PANIC) {
1379 		ghes_print_queued_estatus();
1380 		__ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
1381 	}
1382 
1383 	ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1384 
1385 	/* This error has been reported before, don't process it again. */
1386 	if (ghes_estatus_cached(estatus))
1387 		goto no_work;
1388 
1389 	llist_add(&estatus_node->llnode, &ghes_estatus_llist);
1390 
1391 	return rc;
1392 
1393 no_work:
1394 	gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
1395 		      node_len);
1396 
1397 	return rc;
1398 }
1399 
1400 static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
1401 				       enum fixed_addresses fixmap_idx)
1402 {
1403 	int ret = -ENOENT;
1404 	struct ghes *ghes;
1405 
1406 	rcu_read_lock();
1407 	list_for_each_entry_rcu(ghes, rcu_list, list) {
1408 		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
1409 			ret = 0;
1410 	}
1411 	rcu_read_unlock();
1412 
1413 	if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
1414 		irq_work_queue(&ghes_proc_irq_work);
1415 
1416 	return ret;
1417 }
1418 
1419 /**
1420  * ghes_has_active_errors - Check if there are active errors in error sources
1421  * @ghes_list: List of GHES entries to check for active errors
1422  *
1423  * This function iterates through all GHES entries in the given list and
1424  * checks if any of them has active error status by reading the error
1425  * status register.
1426  *
1427  * Return: true if at least one source has active error, false otherwise.
1428  */
1429 static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
1430 {
1431 	struct ghes *ghes;
1432 
1433 	guard(rcu)();
1434 	list_for_each_entry_rcu(ghes, ghes_list, list) {
1435 		if (ghes->error_status_vaddr &&
1436 		    readl(ghes->error_status_vaddr))
1437 			return true;
1438 	}
1439 
1440 	return false;
1441 }
1442 
1443 /**
1444  * ghes_map_error_status - Map error status address to virtual address
1445  * @ghes: pointer to GHES structure
1446  *
1447  * Reads the error status address from ACPI HEST table and maps it to a virtual
1448  * address that can be accessed by the kernel.
1449  *
1450  * Return: 0 on success, error code on failure.
1451  */
1452 static int __maybe_unused ghes_map_error_status(struct ghes *ghes)
1453 {
1454 	struct acpi_hest_generic *g = ghes->generic;
1455 	u64 paddr;
1456 	int rc;
1457 
1458 	rc = apei_read(&paddr, &g->error_status_address);
1459 	if (rc)
1460 		return rc;
1461 
1462 	ghes->error_status_vaddr =
1463 		acpi_os_ioremap(paddr, sizeof(ghes->estatus->block_status));
1464 	if (!ghes->error_status_vaddr)
1465 		return -EINVAL;
1466 
1467 	return 0;
1468 }
1469 
1470 /**
1471  * ghes_unmap_error_status - Unmap error status virtual address
1472  * @ghes: pointer to GHES structure
1473  *
1474  * Unmaps the error status address if it was previously mapped.
1475  */
1476 static void __maybe_unused ghes_unmap_error_status(struct ghes *ghes)
1477 {
1478 	if (ghes->error_status_vaddr) {
1479 		iounmap(ghes->error_status_vaddr);
1480 		ghes->error_status_vaddr = NULL;
1481 	}
1482 }
1483 
1484 #ifdef CONFIG_ACPI_APEI_SEA
1485 static LIST_HEAD(ghes_sea);
1486 
1487 /*
1488  * Return 0 only if one of the SEA error sources successfully reported an error
1489  * record sent from the firmware.
1490  */
1491 int ghes_notify_sea(void)
1492 {
1493 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
1494 	int rv;
1495 
1496 	if (!ghes_has_active_errors(&ghes_sea))
1497 		return -ENOENT;
1498 
1499 	raw_spin_lock(&ghes_notify_lock_sea);
1500 	rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
1501 	raw_spin_unlock(&ghes_notify_lock_sea);
1502 
1503 	return rv;
1504 }
1505 
1506 static int ghes_sea_add(struct ghes *ghes)
1507 {
1508 	int rc;
1509 
1510 	rc = ghes_map_error_status(ghes);
1511 	if (rc)
1512 		return rc;
1513 
1514 	mutex_lock(&ghes_list_mutex);
1515 	list_add_rcu(&ghes->list, &ghes_sea);
1516 	mutex_unlock(&ghes_list_mutex);
1517 
1518 	return 0;
1519 }
1520 
1521 static void ghes_sea_remove(struct ghes *ghes)
1522 {
1523 	mutex_lock(&ghes_list_mutex);
1524 	list_del_rcu(&ghes->list);
1525 	mutex_unlock(&ghes_list_mutex);
1526 	ghes_unmap_error_status(ghes);
1527 	synchronize_rcu();
1528 }
1529 #else /* CONFIG_ACPI_APEI_SEA */
1530 static inline int ghes_sea_add(struct ghes *ghes) { return -EINVAL; }
1531 static inline void ghes_sea_remove(struct ghes *ghes) { }
1532 #endif /* CONFIG_ACPI_APEI_SEA */
1533 
1534 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
1535 /*
1536  * NMI may be triggered on any CPU, so ghes_in_nmi is used for
1537  * having only one concurrent reader.
1538  */
1539 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
1540 
1541 static LIST_HEAD(ghes_nmi);
1542 
1543 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
1544 {
1545 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1546 	int ret = NMI_DONE;
1547 
1548 	if (!ghes_has_active_errors(&ghes_nmi))
1549 		return ret;
1550 
1551 	if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1552 		return ret;
1553 
1554 	raw_spin_lock(&ghes_notify_lock_nmi);
1555 	if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1556 		ret = NMI_HANDLED;
1557 	raw_spin_unlock(&ghes_notify_lock_nmi);
1558 
1559 	atomic_dec(&ghes_in_nmi);
1560 	return ret;
1561 }
1562 
1563 static int ghes_nmi_add(struct ghes *ghes)
1564 {
1565 	int rc;
1566 
1567 	rc = ghes_map_error_status(ghes);
1568 	if (rc)
1569 		return rc;
1570 
1571 	mutex_lock(&ghes_list_mutex);
1572 	if (list_empty(&ghes_nmi))
1573 		register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1574 	list_add_rcu(&ghes->list, &ghes_nmi);
1575 	mutex_unlock(&ghes_list_mutex);
1576 
1577 	return 0;
1578 }
1579 
1580 static void ghes_nmi_remove(struct ghes *ghes)
1581 {
1582 	mutex_lock(&ghes_list_mutex);
1583 	list_del_rcu(&ghes->list);
1584 	if (list_empty(&ghes_nmi))
1585 		unregister_nmi_handler(NMI_LOCAL, "ghes");
1586 	mutex_unlock(&ghes_list_mutex);
1587 
1588 	ghes_unmap_error_status(ghes);
1589 
1590 	/*
1591 	 * To synchronize with NMI handler, ghes can only be
1592 	 * freed after NMI handler finishes.
1593 	 */
1594 	synchronize_rcu();
1595 }
1596 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1597 static inline int ghes_nmi_add(struct ghes *ghes) { return -EINVAL; }
1598 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1599 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1600 
1601 static void ghes_nmi_init_cxt(void)
1602 {
1603 	init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1604 }
1605 
1606 static int __ghes_sdei_callback(struct ghes *ghes,
1607 				enum fixed_addresses fixmap_idx)
1608 {
1609 	if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1610 		irq_work_queue(&ghes_proc_irq_work);
1611 
1612 		return 0;
1613 	}
1614 
1615 	return -ENOENT;
1616 }
1617 
1618 static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1619 				      void *arg)
1620 {
1621 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1622 	struct ghes *ghes = arg;
1623 	int err;
1624 
1625 	raw_spin_lock(&ghes_notify_lock_sdei_normal);
1626 	err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1627 	raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1628 
1629 	return err;
1630 }
1631 
1632 static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1633 				       void *arg)
1634 {
1635 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1636 	struct ghes *ghes = arg;
1637 	int err;
1638 
1639 	raw_spin_lock(&ghes_notify_lock_sdei_critical);
1640 	err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1641 	raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1642 
1643 	return err;
1644 }
1645 
1646 static int apei_sdei_register_ghes(struct ghes *ghes)
1647 {
1648 	if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1649 		return -EOPNOTSUPP;
1650 
1651 	return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1652 				 ghes_sdei_critical_callback);
1653 }
1654 
1655 static int apei_sdei_unregister_ghes(struct ghes *ghes)
1656 {
1657 	if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1658 		return -EOPNOTSUPP;
1659 
1660 	return sdei_unregister_ghes(ghes);
1661 }
1662 
1663 static int ghes_probe(struct platform_device *ghes_dev)
1664 {
1665 	struct acpi_hest_generic *generic;
1666 	struct ghes *ghes = NULL;
1667 	unsigned long flags;
1668 
1669 	int rc = -EINVAL;
1670 
1671 	generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1672 	if (!generic->enabled)
1673 		return -ENODEV;
1674 
1675 	switch (generic->notify.type) {
1676 	case ACPI_HEST_NOTIFY_POLLED:
1677 	case ACPI_HEST_NOTIFY_EXTERNAL:
1678 	case ACPI_HEST_NOTIFY_SCI:
1679 	case ACPI_HEST_NOTIFY_GSIV:
1680 	case ACPI_HEST_NOTIFY_GPIO:
1681 		break;
1682 
1683 	case ACPI_HEST_NOTIFY_SEA:
1684 		if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1685 			pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1686 				generic->header.source_id);
1687 			rc = -ENOTSUPP;
1688 			goto err;
1689 		}
1690 		break;
1691 	case ACPI_HEST_NOTIFY_NMI:
1692 		if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1693 			pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1694 				generic->header.source_id);
1695 			goto err;
1696 		}
1697 		break;
1698 	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1699 		if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1700 			pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1701 				generic->header.source_id);
1702 			goto err;
1703 		}
1704 		break;
1705 	case ACPI_HEST_NOTIFY_LOCAL:
1706 		pr_warn(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1707 			generic->header.source_id);
1708 		goto err;
1709 	default:
1710 		pr_warn(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1711 			generic->notify.type, generic->header.source_id);
1712 		goto err;
1713 	}
1714 
1715 	rc = -EIO;
1716 	if (generic->error_block_length <
1717 	    sizeof(struct acpi_hest_generic_status)) {
1718 		pr_warn(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1719 			generic->error_block_length, generic->header.source_id);
1720 		goto err;
1721 	}
1722 	ghes = ghes_new(generic);
1723 	if (IS_ERR(ghes)) {
1724 		rc = PTR_ERR(ghes);
1725 		ghes = NULL;
1726 		goto err;
1727 	}
1728 
1729 	switch (generic->notify.type) {
1730 	case ACPI_HEST_NOTIFY_POLLED:
1731 		timer_setup(&ghes->timer, ghes_poll_func, 0);
1732 		ghes_add_timer(ghes);
1733 		break;
1734 	case ACPI_HEST_NOTIFY_EXTERNAL:
1735 		/* External interrupt vector is GSI */
1736 		rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1737 		if (rc) {
1738 			pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1739 			       generic->header.source_id);
1740 			goto err;
1741 		}
1742 		rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1743 				 "GHES IRQ", ghes);
1744 		if (rc) {
1745 			pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1746 			       generic->header.source_id);
1747 			goto err;
1748 		}
1749 		break;
1750 
1751 	case ACPI_HEST_NOTIFY_SCI:
1752 	case ACPI_HEST_NOTIFY_GSIV:
1753 	case ACPI_HEST_NOTIFY_GPIO:
1754 		mutex_lock(&ghes_list_mutex);
1755 		if (list_empty(&ghes_hed))
1756 			register_acpi_hed_notifier(&ghes_notifier_hed);
1757 		list_add_rcu(&ghes->list, &ghes_hed);
1758 		mutex_unlock(&ghes_list_mutex);
1759 		break;
1760 
1761 	case ACPI_HEST_NOTIFY_SEA:
1762 		rc = ghes_sea_add(ghes);
1763 		if (rc)
1764 			goto err;
1765 		break;
1766 	case ACPI_HEST_NOTIFY_NMI:
1767 		rc = ghes_nmi_add(ghes);
1768 		if (rc)
1769 			goto err;
1770 		break;
1771 	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1772 		rc = apei_sdei_register_ghes(ghes);
1773 		if (rc)
1774 			goto err;
1775 		break;
1776 	default:
1777 		BUG();
1778 	}
1779 
1780 	platform_set_drvdata(ghes_dev, ghes);
1781 
1782 	ghes->dev = &ghes_dev->dev;
1783 
1784 	mutex_lock(&ghes_devs_mutex);
1785 	list_add_tail(&ghes->elist, &ghes_devs);
1786 	mutex_unlock(&ghes_devs_mutex);
1787 
1788 	/* Handle any pending errors right away */
1789 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1790 	ghes_proc(ghes);
1791 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1792 
1793 	return 0;
1794 
1795 err:
1796 	if (ghes) {
1797 		ghes_fini(ghes);
1798 		kfree(ghes);
1799 	}
1800 	return rc;
1801 }
1802 
1803 static void ghes_remove(struct platform_device *ghes_dev)
1804 {
1805 	int rc;
1806 	struct ghes *ghes;
1807 	struct acpi_hest_generic *generic;
1808 
1809 	ghes = platform_get_drvdata(ghes_dev);
1810 	generic = ghes->generic;
1811 
1812 	ghes->flags |= GHES_EXITING;
1813 	switch (generic->notify.type) {
1814 	case ACPI_HEST_NOTIFY_POLLED:
1815 		timer_shutdown_sync(&ghes->timer);
1816 		break;
1817 	case ACPI_HEST_NOTIFY_EXTERNAL:
1818 		free_irq(ghes->irq, ghes);
1819 		break;
1820 
1821 	case ACPI_HEST_NOTIFY_SCI:
1822 	case ACPI_HEST_NOTIFY_GSIV:
1823 	case ACPI_HEST_NOTIFY_GPIO:
1824 		mutex_lock(&ghes_list_mutex);
1825 		list_del_rcu(&ghes->list);
1826 		if (list_empty(&ghes_hed))
1827 			unregister_acpi_hed_notifier(&ghes_notifier_hed);
1828 		mutex_unlock(&ghes_list_mutex);
1829 		synchronize_rcu();
1830 		break;
1831 
1832 	case ACPI_HEST_NOTIFY_SEA:
1833 		ghes_sea_remove(ghes);
1834 		break;
1835 	case ACPI_HEST_NOTIFY_NMI:
1836 		ghes_nmi_remove(ghes);
1837 		break;
1838 	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1839 		rc = apei_sdei_unregister_ghes(ghes);
1840 		if (rc) {
1841 			/*
1842 			 * Returning early results in a resource leak, but we're
1843 			 * only here if stopping the hardware failed.
1844 			 */
1845 			dev_err(&ghes_dev->dev, "Failed to unregister ghes (%pe)\n",
1846 				ERR_PTR(rc));
1847 			return;
1848 		}
1849 		break;
1850 	default:
1851 		BUG();
1852 		break;
1853 	}
1854 
1855 	ghes_fini(ghes);
1856 
1857 	mutex_lock(&ghes_devs_mutex);
1858 	list_del(&ghes->elist);
1859 	mutex_unlock(&ghes_devs_mutex);
1860 
1861 	kfree(ghes);
1862 }
1863 
1864 static struct platform_driver ghes_platform_driver = {
1865 	.driver		= {
1866 		.name	= "GHES",
1867 	},
1868 	.probe		= ghes_probe,
1869 	.remove		= ghes_remove,
1870 };
1871 
1872 void __init acpi_ghes_init(void)
1873 {
1874 	int rc;
1875 
1876 	acpi_sdei_init();
1877 
1878 	if (acpi_disabled)
1879 		return;
1880 
1881 	switch (hest_disable) {
1882 	case HEST_NOT_FOUND:
1883 		return;
1884 	case HEST_DISABLED:
1885 		pr_info(GHES_PFX "HEST is not enabled!\n");
1886 		return;
1887 	default:
1888 		break;
1889 	}
1890 
1891 	if (ghes_disable) {
1892 		pr_info(GHES_PFX "GHES is not enabled!\n");
1893 		return;
1894 	}
1895 
1896 	ghes_nmi_init_cxt();
1897 
1898 	rc = platform_driver_register(&ghes_platform_driver);
1899 	if (rc)
1900 		return;
1901 
1902 	rc = apei_osc_setup();
1903 	if (rc == 0 && osc_sb_apei_support_acked)
1904 		pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1905 	else if (rc == 0 && !osc_sb_apei_support_acked)
1906 		pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1907 	else if (rc && osc_sb_apei_support_acked)
1908 		pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1909 	else
1910 		pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1911 }
1912 
1913 /*
1914  * Known x86 systems that prefer GHES error reporting:
1915  */
1916 static struct acpi_platform_list plat_list[] = {
1917 	{"HPE   ", "Server  ", 0, ACPI_SIG_FADT, all_versions},
1918 	{"__ZX__", "EDK2    ", 3, ACPI_SIG_FADT, greater_than_or_equal},
1919 	{"_BYO_ ", "BYOSOFT ", 3, ACPI_SIG_FADT, greater_than_or_equal},
1920 	{ } /* End */
1921 };
1922 
1923 struct list_head *ghes_get_devices(void)
1924 {
1925 	int idx = -1;
1926 
1927 	if (IS_ENABLED(CONFIG_X86)) {
1928 		idx = acpi_match_platform_list(plat_list);
1929 		if (idx < 0) {
1930 			if (!ghes_edac_force_enable)
1931 				return NULL;
1932 
1933 			pr_warn_once("Force-loading ghes_edac on an unsupported platform. You're on your own!\n");
1934 		}
1935 	} else if (list_empty(&ghes_devs)) {
1936 		return NULL;
1937 	}
1938 
1939 	return &ghes_devs;
1940 }
1941 EXPORT_SYMBOL_GPL(ghes_get_devices);
1942 
1943 void ghes_register_report_chain(struct notifier_block *nb)
1944 {
1945 	atomic_notifier_chain_register(&ghes_report_chain, nb);
1946 }
1947 EXPORT_SYMBOL_GPL(ghes_register_report_chain);
1948 
1949 void ghes_unregister_report_chain(struct notifier_block *nb)
1950 {
1951 	atomic_notifier_chain_unregister(&ghes_report_chain, nb);
1952 }
1953 EXPORT_SYMBOL_GPL(ghes_unregister_report_chain);
1954