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