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