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