xref: /openbmc/linux/drivers/acpi/apei/ghes.c (revision e657c18a)
1 /*
2  * APEI Generic Hardware Error Source support
3  *
4  * Generic Hardware Error Source provides a way to report platform
5  * hardware errors (such as that from chipset). It works in so called
6  * "Firmware First" mode, that is, hardware errors are reported to
7  * firmware firstly, then reported to Linux by firmware. This way,
8  * some non-standard hardware error registers or non-standard hardware
9  * link can be checked by firmware to produce more hardware error
10  * information for Linux.
11  *
12  * For more information about Generic Hardware Error Source, please
13  * refer to ACPI Specification version 4.0, section 17.3.2.6
14  *
15  * Copyright 2010,2011 Intel Corp.
16  *   Author: Huang Ying <ying.huang@intel.com>
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License version
20  * 2 as published by the Free Software Foundation;
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  */
27 
28 #include <linux/arm_sdei.h>
29 #include <linux/kernel.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/acpi.h>
33 #include <linux/io.h>
34 #include <linux/interrupt.h>
35 #include <linux/timer.h>
36 #include <linux/cper.h>
37 #include <linux/platform_device.h>
38 #include <linux/mutex.h>
39 #include <linux/ratelimit.h>
40 #include <linux/vmalloc.h>
41 #include <linux/irq_work.h>
42 #include <linux/llist.h>
43 #include <linux/genalloc.h>
44 #include <linux/pci.h>
45 #include <linux/pfn.h>
46 #include <linux/aer.h>
47 #include <linux/nmi.h>
48 #include <linux/sched/clock.h>
49 #include <linux/uuid.h>
50 #include <linux/ras.h>
51 
52 #include <acpi/actbl1.h>
53 #include <acpi/ghes.h>
54 #include <acpi/apei.h>
55 #include <asm/fixmap.h>
56 #include <asm/tlbflush.h>
57 #include <ras/ras_event.h>
58 
59 #include "apei-internal.h"
60 
61 #define GHES_PFX	"GHES: "
62 
63 #define GHES_ESTATUS_MAX_SIZE		65536
64 #define GHES_ESOURCE_PREALLOC_MAX_SIZE	65536
65 
66 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
67 
68 /* This is just an estimation for memory pool allocation */
69 #define GHES_ESTATUS_CACHE_AVG_SIZE	512
70 
71 #define GHES_ESTATUS_CACHES_SIZE	4
72 
73 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC	10000000000ULL
74 /* Prevent too many caches are allocated because of RCU */
75 #define GHES_ESTATUS_CACHE_ALLOCED_MAX	(GHES_ESTATUS_CACHES_SIZE * 3 / 2)
76 
77 #define GHES_ESTATUS_CACHE_LEN(estatus_len)			\
78 	(sizeof(struct ghes_estatus_cache) + (estatus_len))
79 #define GHES_ESTATUS_FROM_CACHE(estatus_cache)			\
80 	((struct acpi_hest_generic_status *)				\
81 	 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
82 
83 #define GHES_ESTATUS_NODE_LEN(estatus_len)			\
84 	(sizeof(struct ghes_estatus_node) + (estatus_len))
85 #define GHES_ESTATUS_FROM_NODE(estatus_node)			\
86 	((struct acpi_hest_generic_status *)				\
87 	 ((struct ghes_estatus_node *)(estatus_node) + 1))
88 
89 /*
90  *  NMI-like notifications vary by architecture, before the compiler can prune
91  *  unused static functions it needs a value for these enums.
92  */
93 #ifndef CONFIG_ARM_SDE_INTERFACE
94 #define FIX_APEI_GHES_SDEI_NORMAL	__end_of_fixed_addresses
95 #define FIX_APEI_GHES_SDEI_CRITICAL	__end_of_fixed_addresses
96 #endif
97 
98 static inline bool is_hest_type_generic_v2(struct ghes *ghes)
99 {
100 	return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
101 }
102 
103 /*
104  * This driver isn't really modular, however for the time being,
105  * continuing to use module_param is the easiest way to remain
106  * compatible with existing boot arg use cases.
107  */
108 bool ghes_disable;
109 module_param_named(disable, ghes_disable, bool, 0);
110 
111 /*
112  * All error sources notified with HED (Hardware Error Device) share a
113  * single notifier callback, so they need to be linked and checked one
114  * by one. This holds true for NMI too.
115  *
116  * RCU is used for these lists, so ghes_list_mutex is only used for
117  * list changing, not for traversing.
118  */
119 static LIST_HEAD(ghes_hed);
120 static DEFINE_MUTEX(ghes_list_mutex);
121 
122 /*
123  * Because the memory area used to transfer hardware error information
124  * from BIOS to Linux can be determined only in NMI, IRQ or timer
125  * handler, but general ioremap can not be used in atomic context, so
126  * the fixmap is used instead.
127  *
128  * This spinlock is used to prevent the fixmap entry from being used
129  * simultaneously.
130  */
131 static DEFINE_SPINLOCK(ghes_notify_lock_irq);
132 
133 static struct gen_pool *ghes_estatus_pool;
134 static unsigned long ghes_estatus_pool_size_request;
135 
136 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
137 static atomic_t ghes_estatus_cache_alloced;
138 
139 static int ghes_panic_timeout __read_mostly = 30;
140 
141 static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
142 {
143 	phys_addr_t paddr;
144 	pgprot_t prot;
145 
146 	paddr = PFN_PHYS(pfn);
147 	prot = arch_apei_get_mem_attribute(paddr);
148 	__set_fixmap(fixmap_idx, paddr, prot);
149 
150 	return (void __iomem *) __fix_to_virt(fixmap_idx);
151 }
152 
153 static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
154 {
155 	int _idx = virt_to_fix((unsigned long)vaddr);
156 
157 	WARN_ON_ONCE(fixmap_idx != _idx);
158 	clear_fixmap(fixmap_idx);
159 }
160 
161 int ghes_estatus_pool_init(int num_ghes)
162 {
163 	unsigned long addr, len;
164 
165 	ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
166 	if (!ghes_estatus_pool)
167 		return -ENOMEM;
168 
169 	len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
170 	len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
171 
172 	ghes_estatus_pool_size_request = PAGE_ALIGN(len);
173 	addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
174 	if (!addr)
175 		return -ENOMEM;
176 
177 	/*
178 	 * New allocation must be visible in all pgd before it can be found by
179 	 * an NMI allocating from the pool.
180 	 */
181 	vmalloc_sync_all();
182 
183 	return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
184 }
185 
186 static int map_gen_v2(struct ghes *ghes)
187 {
188 	return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
189 }
190 
191 static void unmap_gen_v2(struct ghes *ghes)
192 {
193 	apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
194 }
195 
196 static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
197 {
198 	int rc;
199 	u64 val = 0;
200 
201 	rc = apei_read(&val, &gv2->read_ack_register);
202 	if (rc)
203 		return;
204 
205 	val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
206 	val |= gv2->read_ack_write    << gv2->read_ack_register.bit_offset;
207 
208 	apei_write(val, &gv2->read_ack_register);
209 }
210 
211 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
212 {
213 	struct ghes *ghes;
214 	unsigned int error_block_length;
215 	int rc;
216 
217 	ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
218 	if (!ghes)
219 		return ERR_PTR(-ENOMEM);
220 
221 	ghes->generic = generic;
222 	if (is_hest_type_generic_v2(ghes)) {
223 		rc = map_gen_v2(ghes);
224 		if (rc)
225 			goto err_free;
226 	}
227 
228 	rc = apei_map_generic_address(&generic->error_status_address);
229 	if (rc)
230 		goto err_unmap_read_ack_addr;
231 	error_block_length = generic->error_block_length;
232 	if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
233 		pr_warning(FW_WARN GHES_PFX
234 			   "Error status block length is too long: %u for "
235 			   "generic hardware error source: %d.\n",
236 			   error_block_length, generic->header.source_id);
237 		error_block_length = GHES_ESTATUS_MAX_SIZE;
238 	}
239 	ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
240 	if (!ghes->estatus) {
241 		rc = -ENOMEM;
242 		goto err_unmap_status_addr;
243 	}
244 
245 	return ghes;
246 
247 err_unmap_status_addr:
248 	apei_unmap_generic_address(&generic->error_status_address);
249 err_unmap_read_ack_addr:
250 	if (is_hest_type_generic_v2(ghes))
251 		unmap_gen_v2(ghes);
252 err_free:
253 	kfree(ghes);
254 	return ERR_PTR(rc);
255 }
256 
257 static void ghes_fini(struct ghes *ghes)
258 {
259 	kfree(ghes->estatus);
260 	apei_unmap_generic_address(&ghes->generic->error_status_address);
261 	if (is_hest_type_generic_v2(ghes))
262 		unmap_gen_v2(ghes);
263 }
264 
265 static inline int ghes_severity(int severity)
266 {
267 	switch (severity) {
268 	case CPER_SEV_INFORMATIONAL:
269 		return GHES_SEV_NO;
270 	case CPER_SEV_CORRECTED:
271 		return GHES_SEV_CORRECTED;
272 	case CPER_SEV_RECOVERABLE:
273 		return GHES_SEV_RECOVERABLE;
274 	case CPER_SEV_FATAL:
275 		return GHES_SEV_PANIC;
276 	default:
277 		/* Unknown, go panic */
278 		return GHES_SEV_PANIC;
279 	}
280 }
281 
282 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
283 				  int from_phys,
284 				  enum fixed_addresses fixmap_idx)
285 {
286 	void __iomem *vaddr;
287 	u64 offset;
288 	u32 trunk;
289 
290 	while (len > 0) {
291 		offset = paddr - (paddr & PAGE_MASK);
292 		vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
293 		trunk = PAGE_SIZE - offset;
294 		trunk = min(trunk, len);
295 		if (from_phys)
296 			memcpy_fromio(buffer, vaddr + offset, trunk);
297 		else
298 			memcpy_toio(vaddr + offset, buffer, trunk);
299 		len -= trunk;
300 		paddr += trunk;
301 		buffer += trunk;
302 		ghes_unmap(vaddr, fixmap_idx);
303 	}
304 }
305 
306 /* Check the top-level record header has an appropriate size. */
307 static int __ghes_check_estatus(struct ghes *ghes,
308 				struct acpi_hest_generic_status *estatus)
309 {
310 	u32 len = cper_estatus_len(estatus);
311 
312 	if (len < sizeof(*estatus)) {
313 		pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
314 		return -EIO;
315 	}
316 
317 	if (len > ghes->generic->error_block_length) {
318 		pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
319 		return -EIO;
320 	}
321 
322 	if (cper_estatus_check_header(estatus)) {
323 		pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
324 		return -EIO;
325 	}
326 
327 	return 0;
328 }
329 
330 /* Read the CPER block, returning its address, and header in estatus. */
331 static int __ghes_peek_estatus(struct ghes *ghes,
332 			       struct acpi_hest_generic_status *estatus,
333 			       u64 *buf_paddr, enum fixed_addresses fixmap_idx)
334 {
335 	struct acpi_hest_generic *g = ghes->generic;
336 	int rc;
337 
338 	rc = apei_read(buf_paddr, &g->error_status_address);
339 	if (rc) {
340 		*buf_paddr = 0;
341 		pr_warn_ratelimited(FW_WARN GHES_PFX
342 "Failed to read error status block address for hardware error source: %d.\n",
343 				   g->header.source_id);
344 		return -EIO;
345 	}
346 	if (!*buf_paddr)
347 		return -ENOENT;
348 
349 	ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
350 			      fixmap_idx);
351 	if (!estatus->block_status) {
352 		*buf_paddr = 0;
353 		return -ENOENT;
354 	}
355 
356 	return __ghes_check_estatus(ghes, estatus);
357 }
358 
359 static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
360 			       u64 buf_paddr, enum fixed_addresses fixmap_idx,
361 			       size_t buf_len)
362 {
363 	ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
364 	if (cper_estatus_check(estatus)) {
365 		pr_warn_ratelimited(FW_WARN GHES_PFX
366 				    "Failed to read error status block!\n");
367 		return -EIO;
368 	}
369 
370 	return 0;
371 }
372 
373 static int ghes_read_estatus(struct ghes *ghes,
374 			     struct acpi_hest_generic_status *estatus,
375 			     u64 *buf_paddr, enum fixed_addresses fixmap_idx)
376 {
377 	int rc;
378 
379 	rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
380 	if (rc)
381 		return rc;
382 
383 	rc = __ghes_check_estatus(ghes, estatus);
384 	if (rc)
385 		return rc;
386 
387 	return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
388 				   cper_estatus_len(estatus));
389 }
390 
391 static void ghes_clear_estatus(struct ghes *ghes,
392 			       struct acpi_hest_generic_status *estatus,
393 			       u64 buf_paddr, enum fixed_addresses fixmap_idx)
394 {
395 	estatus->block_status = 0;
396 
397 	if (!buf_paddr)
398 		return;
399 
400 	ghes_copy_tofrom_phys(estatus, buf_paddr,
401 			      sizeof(estatus->block_status), 0,
402 			      fixmap_idx);
403 
404 	/*
405 	 * GHESv2 type HEST entries introduce support for error acknowledgment,
406 	 * so only acknowledge the error if this support is present.
407 	 */
408 	if (is_hest_type_generic_v2(ghes))
409 		ghes_ack_error(ghes->generic_v2);
410 }
411 
412 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
413 {
414 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
415 	unsigned long pfn;
416 	int flags = -1;
417 	int sec_sev = ghes_severity(gdata->error_severity);
418 	struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
419 
420 	if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
421 		return;
422 
423 	pfn = mem_err->physical_addr >> PAGE_SHIFT;
424 	if (!pfn_valid(pfn)) {
425 		pr_warn_ratelimited(FW_WARN GHES_PFX
426 		"Invalid address in generic error data: %#llx\n",
427 		mem_err->physical_addr);
428 		return;
429 	}
430 
431 	/* iff following two events can be handled properly by now */
432 	if (sec_sev == GHES_SEV_CORRECTED &&
433 	    (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
434 		flags = MF_SOFT_OFFLINE;
435 	if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
436 		flags = 0;
437 
438 	if (flags != -1)
439 		memory_failure_queue(pfn, flags);
440 #endif
441 }
442 
443 /*
444  * PCIe AER errors need to be sent to the AER driver for reporting and
445  * recovery. The GHES severities map to the following AER severities and
446  * require the following handling:
447  *
448  * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
449  *     These need to be reported by the AER driver but no recovery is
450  *     necessary.
451  * GHES_SEV_RECOVERABLE -> AER_NONFATAL
452  * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
453  *     These both need to be reported and recovered from by the AER driver.
454  * GHES_SEV_PANIC does not make it to this handling since the kernel must
455  *     panic.
456  */
457 static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
458 {
459 #ifdef CONFIG_ACPI_APEI_PCIEAER
460 	struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
461 
462 	if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
463 	    pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
464 		unsigned int devfn;
465 		int aer_severity;
466 
467 		devfn = PCI_DEVFN(pcie_err->device_id.device,
468 				  pcie_err->device_id.function);
469 		aer_severity = cper_severity_to_aer(gdata->error_severity);
470 
471 		/*
472 		 * If firmware reset the component to contain
473 		 * the error, we must reinitialize it before
474 		 * use, so treat it as a fatal AER error.
475 		 */
476 		if (gdata->flags & CPER_SEC_RESET)
477 			aer_severity = AER_FATAL;
478 
479 		aer_recover_queue(pcie_err->device_id.segment,
480 				  pcie_err->device_id.bus,
481 				  devfn, aer_severity,
482 				  (struct aer_capability_regs *)
483 				  pcie_err->aer_info);
484 	}
485 #endif
486 }
487 
488 static void ghes_do_proc(struct ghes *ghes,
489 			 const struct acpi_hest_generic_status *estatus)
490 {
491 	int sev, sec_sev;
492 	struct acpi_hest_generic_data *gdata;
493 	guid_t *sec_type;
494 	guid_t *fru_id = &NULL_UUID_LE;
495 	char *fru_text = "";
496 
497 	sev = ghes_severity(estatus->error_severity);
498 	apei_estatus_for_each_section(estatus, gdata) {
499 		sec_type = (guid_t *)gdata->section_type;
500 		sec_sev = ghes_severity(gdata->error_severity);
501 		if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
502 			fru_id = (guid_t *)gdata->fru_id;
503 
504 		if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
505 			fru_text = gdata->fru_text;
506 
507 		if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
508 			struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
509 
510 			ghes_edac_report_mem_error(sev, mem_err);
511 
512 			arch_apei_report_mem_error(sev, mem_err);
513 			ghes_handle_memory_failure(gdata, sev);
514 		}
515 		else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
516 			ghes_handle_aer(gdata);
517 		}
518 		else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
519 			struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
520 
521 			log_arm_hw_error(err);
522 		} else {
523 			void *err = acpi_hest_get_payload(gdata);
524 
525 			log_non_standard_event(sec_type, fru_id, fru_text,
526 					       sec_sev, err,
527 					       gdata->error_data_length);
528 		}
529 	}
530 }
531 
532 static void __ghes_print_estatus(const char *pfx,
533 				 const struct acpi_hest_generic *generic,
534 				 const struct acpi_hest_generic_status *estatus)
535 {
536 	static atomic_t seqno;
537 	unsigned int curr_seqno;
538 	char pfx_seq[64];
539 
540 	if (pfx == NULL) {
541 		if (ghes_severity(estatus->error_severity) <=
542 		    GHES_SEV_CORRECTED)
543 			pfx = KERN_WARNING;
544 		else
545 			pfx = KERN_ERR;
546 	}
547 	curr_seqno = atomic_inc_return(&seqno);
548 	snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
549 	printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
550 	       pfx_seq, generic->header.source_id);
551 	cper_estatus_print(pfx_seq, estatus);
552 }
553 
554 static int ghes_print_estatus(const char *pfx,
555 			      const struct acpi_hest_generic *generic,
556 			      const struct acpi_hest_generic_status *estatus)
557 {
558 	/* Not more than 2 messages every 5 seconds */
559 	static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
560 	static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
561 	struct ratelimit_state *ratelimit;
562 
563 	if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
564 		ratelimit = &ratelimit_corrected;
565 	else
566 		ratelimit = &ratelimit_uncorrected;
567 	if (__ratelimit(ratelimit)) {
568 		__ghes_print_estatus(pfx, generic, estatus);
569 		return 1;
570 	}
571 	return 0;
572 }
573 
574 /*
575  * GHES error status reporting throttle, to report more kinds of
576  * errors, instead of just most frequently occurred errors.
577  */
578 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
579 {
580 	u32 len;
581 	int i, cached = 0;
582 	unsigned long long now;
583 	struct ghes_estatus_cache *cache;
584 	struct acpi_hest_generic_status *cache_estatus;
585 
586 	len = cper_estatus_len(estatus);
587 	rcu_read_lock();
588 	for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
589 		cache = rcu_dereference(ghes_estatus_caches[i]);
590 		if (cache == NULL)
591 			continue;
592 		if (len != cache->estatus_len)
593 			continue;
594 		cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
595 		if (memcmp(estatus, cache_estatus, len))
596 			continue;
597 		atomic_inc(&cache->count);
598 		now = sched_clock();
599 		if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
600 			cached = 1;
601 		break;
602 	}
603 	rcu_read_unlock();
604 	return cached;
605 }
606 
607 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
608 	struct acpi_hest_generic *generic,
609 	struct acpi_hest_generic_status *estatus)
610 {
611 	int alloced;
612 	u32 len, cache_len;
613 	struct ghes_estatus_cache *cache;
614 	struct acpi_hest_generic_status *cache_estatus;
615 
616 	alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
617 	if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
618 		atomic_dec(&ghes_estatus_cache_alloced);
619 		return NULL;
620 	}
621 	len = cper_estatus_len(estatus);
622 	cache_len = GHES_ESTATUS_CACHE_LEN(len);
623 	cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
624 	if (!cache) {
625 		atomic_dec(&ghes_estatus_cache_alloced);
626 		return NULL;
627 	}
628 	cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
629 	memcpy(cache_estatus, estatus, len);
630 	cache->estatus_len = len;
631 	atomic_set(&cache->count, 0);
632 	cache->generic = generic;
633 	cache->time_in = sched_clock();
634 	return cache;
635 }
636 
637 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
638 {
639 	u32 len;
640 
641 	len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
642 	len = GHES_ESTATUS_CACHE_LEN(len);
643 	gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
644 	atomic_dec(&ghes_estatus_cache_alloced);
645 }
646 
647 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
648 {
649 	struct ghes_estatus_cache *cache;
650 
651 	cache = container_of(head, struct ghes_estatus_cache, rcu);
652 	ghes_estatus_cache_free(cache);
653 }
654 
655 static void ghes_estatus_cache_add(
656 	struct acpi_hest_generic *generic,
657 	struct acpi_hest_generic_status *estatus)
658 {
659 	int i, slot = -1, count;
660 	unsigned long long now, duration, period, max_period = 0;
661 	struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
662 
663 	new_cache = ghes_estatus_cache_alloc(generic, estatus);
664 	if (new_cache == NULL)
665 		return;
666 	rcu_read_lock();
667 	now = sched_clock();
668 	for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
669 		cache = rcu_dereference(ghes_estatus_caches[i]);
670 		if (cache == NULL) {
671 			slot = i;
672 			slot_cache = NULL;
673 			break;
674 		}
675 		duration = now - cache->time_in;
676 		if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
677 			slot = i;
678 			slot_cache = cache;
679 			break;
680 		}
681 		count = atomic_read(&cache->count);
682 		period = duration;
683 		do_div(period, (count + 1));
684 		if (period > max_period) {
685 			max_period = period;
686 			slot = i;
687 			slot_cache = cache;
688 		}
689 	}
690 	/* new_cache must be put into array after its contents are written */
691 	smp_wmb();
692 	if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
693 				  slot_cache, new_cache) == slot_cache) {
694 		if (slot_cache)
695 			call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
696 	} else
697 		ghes_estatus_cache_free(new_cache);
698 	rcu_read_unlock();
699 }
700 
701 static void __ghes_panic(struct ghes *ghes,
702 			 struct acpi_hest_generic_status *estatus,
703 			 u64 buf_paddr, enum fixed_addresses fixmap_idx)
704 {
705 	__ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
706 
707 	ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
708 
709 	/* reboot to log the error! */
710 	if (!panic_timeout)
711 		panic_timeout = ghes_panic_timeout;
712 	panic("Fatal hardware error!");
713 }
714 
715 static int ghes_proc(struct ghes *ghes)
716 {
717 	struct acpi_hest_generic_status *estatus = ghes->estatus;
718 	u64 buf_paddr;
719 	int rc;
720 
721 	rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
722 	if (rc)
723 		goto out;
724 
725 	if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
726 		__ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
727 
728 	if (!ghes_estatus_cached(estatus)) {
729 		if (ghes_print_estatus(NULL, ghes->generic, estatus))
730 			ghes_estatus_cache_add(ghes->generic, estatus);
731 	}
732 	ghes_do_proc(ghes, estatus);
733 
734 out:
735 	ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
736 
737 	return rc;
738 }
739 
740 static void ghes_add_timer(struct ghes *ghes)
741 {
742 	struct acpi_hest_generic *g = ghes->generic;
743 	unsigned long expire;
744 
745 	if (!g->notify.poll_interval) {
746 		pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
747 			   g->header.source_id);
748 		return;
749 	}
750 	expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
751 	ghes->timer.expires = round_jiffies_relative(expire);
752 	add_timer(&ghes->timer);
753 }
754 
755 static void ghes_poll_func(struct timer_list *t)
756 {
757 	struct ghes *ghes = from_timer(ghes, t, timer);
758 	unsigned long flags;
759 
760 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
761 	ghes_proc(ghes);
762 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
763 	if (!(ghes->flags & GHES_EXITING))
764 		ghes_add_timer(ghes);
765 }
766 
767 static irqreturn_t ghes_irq_func(int irq, void *data)
768 {
769 	struct ghes *ghes = data;
770 	unsigned long flags;
771 	int rc;
772 
773 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
774 	rc = ghes_proc(ghes);
775 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
776 	if (rc)
777 		return IRQ_NONE;
778 
779 	return IRQ_HANDLED;
780 }
781 
782 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
783 			   void *data)
784 {
785 	struct ghes *ghes;
786 	unsigned long flags;
787 	int ret = NOTIFY_DONE;
788 
789 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
790 	rcu_read_lock();
791 	list_for_each_entry_rcu(ghes, &ghes_hed, list) {
792 		if (!ghes_proc(ghes))
793 			ret = NOTIFY_OK;
794 	}
795 	rcu_read_unlock();
796 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
797 
798 	return ret;
799 }
800 
801 static struct notifier_block ghes_notifier_hed = {
802 	.notifier_call = ghes_notify_hed,
803 };
804 
805 /*
806  * Handlers for CPER records may not be NMI safe. For example,
807  * memory_failure_queue() takes spinlocks and calls schedule_work_on().
808  * In any NMI-like handler, memory from ghes_estatus_pool is used to save
809  * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
810  * ghes_proc_in_irq() to run in IRQ context where each estatus in
811  * ghes_estatus_llist is processed.
812  *
813  * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
814  * to suppress frequent messages.
815  */
816 static struct llist_head ghes_estatus_llist;
817 static struct irq_work ghes_proc_irq_work;
818 
819 static void ghes_proc_in_irq(struct irq_work *irq_work)
820 {
821 	struct llist_node *llnode, *next;
822 	struct ghes_estatus_node *estatus_node;
823 	struct acpi_hest_generic *generic;
824 	struct acpi_hest_generic_status *estatus;
825 	u32 len, node_len;
826 
827 	llnode = llist_del_all(&ghes_estatus_llist);
828 	/*
829 	 * Because the time order of estatus in list is reversed,
830 	 * revert it back to proper order.
831 	 */
832 	llnode = llist_reverse_order(llnode);
833 	while (llnode) {
834 		next = llnode->next;
835 		estatus_node = llist_entry(llnode, struct ghes_estatus_node,
836 					   llnode);
837 		estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
838 		len = cper_estatus_len(estatus);
839 		node_len = GHES_ESTATUS_NODE_LEN(len);
840 		ghes_do_proc(estatus_node->ghes, estatus);
841 		if (!ghes_estatus_cached(estatus)) {
842 			generic = estatus_node->generic;
843 			if (ghes_print_estatus(NULL, generic, estatus))
844 				ghes_estatus_cache_add(generic, estatus);
845 		}
846 		gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
847 			      node_len);
848 		llnode = next;
849 	}
850 }
851 
852 static void ghes_print_queued_estatus(void)
853 {
854 	struct llist_node *llnode;
855 	struct ghes_estatus_node *estatus_node;
856 	struct acpi_hest_generic *generic;
857 	struct acpi_hest_generic_status *estatus;
858 
859 	llnode = llist_del_all(&ghes_estatus_llist);
860 	/*
861 	 * Because the time order of estatus in list is reversed,
862 	 * revert it back to proper order.
863 	 */
864 	llnode = llist_reverse_order(llnode);
865 	while (llnode) {
866 		estatus_node = llist_entry(llnode, struct ghes_estatus_node,
867 					   llnode);
868 		estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
869 		generic = estatus_node->generic;
870 		ghes_print_estatus(NULL, generic, estatus);
871 		llnode = llnode->next;
872 	}
873 }
874 
875 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
876 				       enum fixed_addresses fixmap_idx)
877 {
878 	struct acpi_hest_generic_status *estatus, tmp_header;
879 	struct ghes_estatus_node *estatus_node;
880 	u32 len, node_len;
881 	u64 buf_paddr;
882 	int sev, rc;
883 
884 	if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
885 		return -EOPNOTSUPP;
886 
887 	rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
888 	if (rc) {
889 		ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
890 		return rc;
891 	}
892 
893 	rc = __ghes_check_estatus(ghes, &tmp_header);
894 	if (rc) {
895 		ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
896 		return rc;
897 	}
898 
899 	len = cper_estatus_len(&tmp_header);
900 	node_len = GHES_ESTATUS_NODE_LEN(len);
901 	estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
902 	if (!estatus_node)
903 		return -ENOMEM;
904 
905 	estatus_node->ghes = ghes;
906 	estatus_node->generic = ghes->generic;
907 	estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
908 
909 	if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
910 		ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
911 		rc = -ENOENT;
912 		goto no_work;
913 	}
914 
915 	sev = ghes_severity(estatus->error_severity);
916 	if (sev >= GHES_SEV_PANIC) {
917 		ghes_print_queued_estatus();
918 		__ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
919 	}
920 
921 	ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
922 
923 	/* This error has been reported before, don't process it again. */
924 	if (ghes_estatus_cached(estatus))
925 		goto no_work;
926 
927 	llist_add(&estatus_node->llnode, &ghes_estatus_llist);
928 
929 	return rc;
930 
931 no_work:
932 	gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
933 		      node_len);
934 
935 	return rc;
936 }
937 
938 static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
939 				       enum fixed_addresses fixmap_idx)
940 {
941 	int ret = -ENOENT;
942 	struct ghes *ghes;
943 
944 	rcu_read_lock();
945 	list_for_each_entry_rcu(ghes, rcu_list, list) {
946 		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
947 			ret = 0;
948 	}
949 	rcu_read_unlock();
950 
951 	if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
952 		irq_work_queue(&ghes_proc_irq_work);
953 
954 	return ret;
955 }
956 
957 #ifdef CONFIG_ACPI_APEI_SEA
958 static LIST_HEAD(ghes_sea);
959 
960 /*
961  * Return 0 only if one of the SEA error sources successfully reported an error
962  * record sent from the firmware.
963  */
964 int ghes_notify_sea(void)
965 {
966 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
967 	int rv;
968 
969 	raw_spin_lock(&ghes_notify_lock_sea);
970 	rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
971 	raw_spin_unlock(&ghes_notify_lock_sea);
972 
973 	return rv;
974 }
975 
976 static void ghes_sea_add(struct ghes *ghes)
977 {
978 	mutex_lock(&ghes_list_mutex);
979 	list_add_rcu(&ghes->list, &ghes_sea);
980 	mutex_unlock(&ghes_list_mutex);
981 }
982 
983 static void ghes_sea_remove(struct ghes *ghes)
984 {
985 	mutex_lock(&ghes_list_mutex);
986 	list_del_rcu(&ghes->list);
987 	mutex_unlock(&ghes_list_mutex);
988 	synchronize_rcu();
989 }
990 #else /* CONFIG_ACPI_APEI_SEA */
991 static inline void ghes_sea_add(struct ghes *ghes) { }
992 static inline void ghes_sea_remove(struct ghes *ghes) { }
993 #endif /* CONFIG_ACPI_APEI_SEA */
994 
995 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
996 /*
997  * NMI may be triggered on any CPU, so ghes_in_nmi is used for
998  * having only one concurrent reader.
999  */
1000 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
1001 
1002 static LIST_HEAD(ghes_nmi);
1003 
1004 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
1005 {
1006 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1007 	int ret = NMI_DONE;
1008 
1009 	if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1010 		return ret;
1011 
1012 	raw_spin_lock(&ghes_notify_lock_nmi);
1013 	if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1014 		ret = NMI_HANDLED;
1015 	raw_spin_unlock(&ghes_notify_lock_nmi);
1016 
1017 	atomic_dec(&ghes_in_nmi);
1018 	return ret;
1019 }
1020 
1021 static void ghes_nmi_add(struct ghes *ghes)
1022 {
1023 	mutex_lock(&ghes_list_mutex);
1024 	if (list_empty(&ghes_nmi))
1025 		register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1026 	list_add_rcu(&ghes->list, &ghes_nmi);
1027 	mutex_unlock(&ghes_list_mutex);
1028 }
1029 
1030 static void ghes_nmi_remove(struct ghes *ghes)
1031 {
1032 	mutex_lock(&ghes_list_mutex);
1033 	list_del_rcu(&ghes->list);
1034 	if (list_empty(&ghes_nmi))
1035 		unregister_nmi_handler(NMI_LOCAL, "ghes");
1036 	mutex_unlock(&ghes_list_mutex);
1037 	/*
1038 	 * To synchronize with NMI handler, ghes can only be
1039 	 * freed after NMI handler finishes.
1040 	 */
1041 	synchronize_rcu();
1042 }
1043 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1044 static inline void ghes_nmi_add(struct ghes *ghes) { }
1045 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1046 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1047 
1048 static void ghes_nmi_init_cxt(void)
1049 {
1050 	init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1051 }
1052 
1053 static int __ghes_sdei_callback(struct ghes *ghes,
1054 				enum fixed_addresses fixmap_idx)
1055 {
1056 	if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1057 		irq_work_queue(&ghes_proc_irq_work);
1058 
1059 		return 0;
1060 	}
1061 
1062 	return -ENOENT;
1063 }
1064 
1065 static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1066 				      void *arg)
1067 {
1068 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1069 	struct ghes *ghes = arg;
1070 	int err;
1071 
1072 	raw_spin_lock(&ghes_notify_lock_sdei_normal);
1073 	err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1074 	raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1075 
1076 	return err;
1077 }
1078 
1079 static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1080 				       void *arg)
1081 {
1082 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1083 	struct ghes *ghes = arg;
1084 	int err;
1085 
1086 	raw_spin_lock(&ghes_notify_lock_sdei_critical);
1087 	err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1088 	raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1089 
1090 	return err;
1091 }
1092 
1093 static int apei_sdei_register_ghes(struct ghes *ghes)
1094 {
1095 	if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1096 		return -EOPNOTSUPP;
1097 
1098 	return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1099 				 ghes_sdei_critical_callback);
1100 }
1101 
1102 static int apei_sdei_unregister_ghes(struct ghes *ghes)
1103 {
1104 	if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1105 		return -EOPNOTSUPP;
1106 
1107 	return sdei_unregister_ghes(ghes);
1108 }
1109 
1110 static int ghes_probe(struct platform_device *ghes_dev)
1111 {
1112 	struct acpi_hest_generic *generic;
1113 	struct ghes *ghes = NULL;
1114 	unsigned long flags;
1115 
1116 	int rc = -EINVAL;
1117 
1118 	generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1119 	if (!generic->enabled)
1120 		return -ENODEV;
1121 
1122 	switch (generic->notify.type) {
1123 	case ACPI_HEST_NOTIFY_POLLED:
1124 	case ACPI_HEST_NOTIFY_EXTERNAL:
1125 	case ACPI_HEST_NOTIFY_SCI:
1126 	case ACPI_HEST_NOTIFY_GSIV:
1127 	case ACPI_HEST_NOTIFY_GPIO:
1128 		break;
1129 
1130 	case ACPI_HEST_NOTIFY_SEA:
1131 		if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1132 			pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1133 				generic->header.source_id);
1134 			rc = -ENOTSUPP;
1135 			goto err;
1136 		}
1137 		break;
1138 	case ACPI_HEST_NOTIFY_NMI:
1139 		if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1140 			pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1141 				generic->header.source_id);
1142 			goto err;
1143 		}
1144 		break;
1145 	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1146 		if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1147 			pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1148 				generic->header.source_id);
1149 			goto err;
1150 		}
1151 		break;
1152 	case ACPI_HEST_NOTIFY_LOCAL:
1153 		pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1154 			   generic->header.source_id);
1155 		goto err;
1156 	default:
1157 		pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1158 			   generic->notify.type, generic->header.source_id);
1159 		goto err;
1160 	}
1161 
1162 	rc = -EIO;
1163 	if (generic->error_block_length <
1164 	    sizeof(struct acpi_hest_generic_status)) {
1165 		pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1166 			   generic->error_block_length,
1167 			   generic->header.source_id);
1168 		goto err;
1169 	}
1170 	ghes = ghes_new(generic);
1171 	if (IS_ERR(ghes)) {
1172 		rc = PTR_ERR(ghes);
1173 		ghes = NULL;
1174 		goto err;
1175 	}
1176 
1177 	switch (generic->notify.type) {
1178 	case ACPI_HEST_NOTIFY_POLLED:
1179 		timer_setup(&ghes->timer, ghes_poll_func, TIMER_DEFERRABLE);
1180 		ghes_add_timer(ghes);
1181 		break;
1182 	case ACPI_HEST_NOTIFY_EXTERNAL:
1183 		/* External interrupt vector is GSI */
1184 		rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1185 		if (rc) {
1186 			pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1187 			       generic->header.source_id);
1188 			goto err;
1189 		}
1190 		rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1191 				 "GHES IRQ", ghes);
1192 		if (rc) {
1193 			pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1194 			       generic->header.source_id);
1195 			goto err;
1196 		}
1197 		break;
1198 
1199 	case ACPI_HEST_NOTIFY_SCI:
1200 	case ACPI_HEST_NOTIFY_GSIV:
1201 	case ACPI_HEST_NOTIFY_GPIO:
1202 		mutex_lock(&ghes_list_mutex);
1203 		if (list_empty(&ghes_hed))
1204 			register_acpi_hed_notifier(&ghes_notifier_hed);
1205 		list_add_rcu(&ghes->list, &ghes_hed);
1206 		mutex_unlock(&ghes_list_mutex);
1207 		break;
1208 
1209 	case ACPI_HEST_NOTIFY_SEA:
1210 		ghes_sea_add(ghes);
1211 		break;
1212 	case ACPI_HEST_NOTIFY_NMI:
1213 		ghes_nmi_add(ghes);
1214 		break;
1215 	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1216 		rc = apei_sdei_register_ghes(ghes);
1217 		if (rc)
1218 			goto err;
1219 		break;
1220 	default:
1221 		BUG();
1222 	}
1223 
1224 	platform_set_drvdata(ghes_dev, ghes);
1225 
1226 	ghes_edac_register(ghes, &ghes_dev->dev);
1227 
1228 	/* Handle any pending errors right away */
1229 	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1230 	ghes_proc(ghes);
1231 	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1232 
1233 	return 0;
1234 
1235 err:
1236 	if (ghes) {
1237 		ghes_fini(ghes);
1238 		kfree(ghes);
1239 	}
1240 	return rc;
1241 }
1242 
1243 static int ghes_remove(struct platform_device *ghes_dev)
1244 {
1245 	int rc;
1246 	struct ghes *ghes;
1247 	struct acpi_hest_generic *generic;
1248 
1249 	ghes = platform_get_drvdata(ghes_dev);
1250 	generic = ghes->generic;
1251 
1252 	ghes->flags |= GHES_EXITING;
1253 	switch (generic->notify.type) {
1254 	case ACPI_HEST_NOTIFY_POLLED:
1255 		del_timer_sync(&ghes->timer);
1256 		break;
1257 	case ACPI_HEST_NOTIFY_EXTERNAL:
1258 		free_irq(ghes->irq, ghes);
1259 		break;
1260 
1261 	case ACPI_HEST_NOTIFY_SCI:
1262 	case ACPI_HEST_NOTIFY_GSIV:
1263 	case ACPI_HEST_NOTIFY_GPIO:
1264 		mutex_lock(&ghes_list_mutex);
1265 		list_del_rcu(&ghes->list);
1266 		if (list_empty(&ghes_hed))
1267 			unregister_acpi_hed_notifier(&ghes_notifier_hed);
1268 		mutex_unlock(&ghes_list_mutex);
1269 		synchronize_rcu();
1270 		break;
1271 
1272 	case ACPI_HEST_NOTIFY_SEA:
1273 		ghes_sea_remove(ghes);
1274 		break;
1275 	case ACPI_HEST_NOTIFY_NMI:
1276 		ghes_nmi_remove(ghes);
1277 		break;
1278 	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1279 		rc = apei_sdei_unregister_ghes(ghes);
1280 		if (rc)
1281 			return rc;
1282 		break;
1283 	default:
1284 		BUG();
1285 		break;
1286 	}
1287 
1288 	ghes_fini(ghes);
1289 
1290 	ghes_edac_unregister(ghes);
1291 
1292 	kfree(ghes);
1293 
1294 	platform_set_drvdata(ghes_dev, NULL);
1295 
1296 	return 0;
1297 }
1298 
1299 static struct platform_driver ghes_platform_driver = {
1300 	.driver		= {
1301 		.name	= "GHES",
1302 	},
1303 	.probe		= ghes_probe,
1304 	.remove		= ghes_remove,
1305 };
1306 
1307 static int __init ghes_init(void)
1308 {
1309 	int rc;
1310 
1311 	if (acpi_disabled)
1312 		return -ENODEV;
1313 
1314 	switch (hest_disable) {
1315 	case HEST_NOT_FOUND:
1316 		return -ENODEV;
1317 	case HEST_DISABLED:
1318 		pr_info(GHES_PFX "HEST is not enabled!\n");
1319 		return -EINVAL;
1320 	default:
1321 		break;
1322 	}
1323 
1324 	if (ghes_disable) {
1325 		pr_info(GHES_PFX "GHES is not enabled!\n");
1326 		return -EINVAL;
1327 	}
1328 
1329 	ghes_nmi_init_cxt();
1330 
1331 	rc = platform_driver_register(&ghes_platform_driver);
1332 	if (rc)
1333 		goto err;
1334 
1335 	rc = apei_osc_setup();
1336 	if (rc == 0 && osc_sb_apei_support_acked)
1337 		pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1338 	else if (rc == 0 && !osc_sb_apei_support_acked)
1339 		pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1340 	else if (rc && osc_sb_apei_support_acked)
1341 		pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1342 	else
1343 		pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1344 
1345 	return 0;
1346 err:
1347 	return rc;
1348 }
1349 device_initcall(ghes_init);
1350