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