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