]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/acpi/apei/ghes.c
ras: acpi / apei: generate trace event for unrecognized CPER section
[mirror_ubuntu-zesty-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/uuid.h>
48 #include <linux/ras.h>
49
50 #include <acpi/actbl1.h>
51 #include <acpi/ghes.h>
52 #include <acpi/apei.h>
53 #include <asm/tlbflush.h>
54 #include <ras/ras_event.h>
55
56 #include "apei-internal.h"
57
58 #define GHES_PFX "GHES: "
59
60 #define GHES_ESTATUS_MAX_SIZE 65536
61 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
62
63 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
64
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
70 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
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) \
77 ((struct acpi_hest_generic_status *) \
78 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
79
80 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
81 (sizeof(struct ghes_estatus_node) + (estatus_len))
82 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
83 ((struct acpi_hest_generic_status *) \
84 ((struct ghes_estatus_node *)(estatus_node) + 1))
85
86 static 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
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 */
96 bool ghes_disable;
97 module_param_named(disable, ghes_disable, bool, 0);
98
99 /*
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.
103 *
104 * RCU is used for these lists, so ghes_list_mutex is only used for
105 * list changing, not for traversing.
106 */
107 static LIST_HEAD(ghes_sci);
108 static DEFINE_MUTEX(ghes_list_mutex);
109
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 /*
118 * Two virtual pages are used, one for IRQ/PROCESS context, the other for
119 * NMI context (optionally).
120 */
121 #define GHES_IOREMAP_PAGES 2
122 #define GHES_IOREMAP_IRQ_PAGE(base) (base)
123 #define GHES_IOREMAP_NMI_PAGE(base) ((base) + PAGE_SIZE)
124
125 /* virtual memory area for atomic ioremap */
126 static 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 */
131 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
132 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
133
134 static struct gen_pool *ghes_estatus_pool;
135 static unsigned long ghes_estatus_pool_size_request;
136
137 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
138 static atomic_t ghes_estatus_cache_alloced;
139
140 static int ghes_panic_timeout __read_mostly = 30;
141
142 static 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
154 static void ghes_ioremap_exit(void)
155 {
156 free_vm_area(ghes_ioremap_area);
157 }
158
159 static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
160 {
161 unsigned long vaddr;
162 phys_addr_t paddr;
163 pgprot_t prot;
164
165 vaddr = (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area->addr);
166
167 paddr = pfn << PAGE_SHIFT;
168 prot = arch_apei_get_mem_attribute(paddr);
169 ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
170
171 return (void __iomem *)vaddr;
172 }
173
174 static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
175 {
176 unsigned long vaddr, paddr;
177 pgprot_t prot;
178
179 vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
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);
185
186 return (void __iomem *)vaddr;
187 }
188
189 static 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);
196 arch_apei_flush_tlb_one(vaddr);
197 }
198
199 static 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);
206 arch_apei_flush_tlb_one(vaddr);
207 }
208
209 static 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
217 static 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
224 static 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
231 static 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
253 static int map_gen_v2(struct ghes *ghes)
254 {
255 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
256 }
257
258 static void unmap_gen_v2(struct ghes *ghes)
259 {
260 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
261 }
262
263 static 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);
272
273 ghes->generic = generic;
274 if (is_hest_type_generic_v2(ghes)) {
275 rc = map_gen_v2(ghes);
276 if (rc)
277 goto err_free;
278 }
279
280 rc = apei_map_generic_address(&generic->error_status_address);
281 if (rc)
282 goto err_unmap_read_ack_addr;
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;
294 goto err_unmap_status_addr;
295 }
296
297 return ghes;
298
299 err_unmap_status_addr:
300 apei_unmap_generic_address(&generic->error_status_address);
301 err_unmap_read_ack_addr:
302 if (is_hest_type_generic_v2(ghes))
303 unmap_gen_v2(ghes);
304 err_free:
305 kfree(ghes);
306 return ERR_PTR(rc);
307 }
308
309 static void ghes_fini(struct ghes *ghes)
310 {
311 kfree(ghes->estatus);
312 apei_unmap_generic_address(&ghes->generic->error_status_address);
313 if (is_hest_type_generic_v2(ghes))
314 unmap_gen_v2(ghes);
315 }
316
317 static inline int ghes_severity(int severity)
318 {
319 switch (severity) {
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;
328 default:
329 /* Unknown, go panic */
330 return GHES_SEV_PANIC;
331 }
332 }
333
334 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
335 int from_phys)
336 {
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 }
369 }
370
371 static 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
378 rc = apei_read(&buf_paddr, &g->error_status_address);
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
389 ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
390 sizeof(*ghes->estatus), 1);
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;
398 len = cper_estatus_len(ghes->estatus);
399 if (len < sizeof(*ghes->estatus))
400 goto err_read_block;
401 if (len > ghes->generic->error_block_length)
402 goto err_read_block;
403 if (cper_estatus_check_header(ghes->estatus))
404 goto err_read_block;
405 ghes_copy_tofrom_phys(ghes->estatus + 1,
406 buf_paddr + sizeof(*ghes->estatus),
407 len - sizeof(*ghes->estatus), 1);
408 if (cper_estatus_check(ghes->estatus))
409 goto err_read_block;
410 rc = 0;
411
412 err_read_block:
413 if (rc && !silent && printk_ratelimit())
414 pr_warning(FW_WARN GHES_PFX
415 "Failed to read error status block!\n");
416 return rc;
417 }
418
419 static 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
429 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
430 {
431 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
432 unsigned long pfn;
433 int flags = -1;
434 int sec_sev = ghes_severity(gdata->error_severity);
435 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
436
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;
446 }
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);
457 #endif
458 }
459
460 static void ghes_do_proc(struct ghes *ghes,
461 const struct acpi_hest_generic_status *estatus)
462 {
463 int sev, sec_sev;
464 struct acpi_hest_generic_data *gdata;
465 uuid_le sec_type;
466 uuid_le *fru_id = &NULL_UUID_LE;
467 char *fru_text = "";
468
469 sev = ghes_severity(estatus->error_severity);
470 apei_estatus_for_each_section(estatus, gdata) {
471 sec_sev = ghes_severity(gdata->error_severity);
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
480 if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
481 CPER_SEC_PLATFORM_MEM)) {
482 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
483
484 ghes_edac_report_mem_error(ghes, sev, mem_err);
485
486 arch_apei_report_mem_error(sev, mem_err);
487 ghes_handle_memory_failure(gdata, sev);
488 }
489 #ifdef CONFIG_ACPI_APEI_PCIEAER
490 else if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
491 CPER_SEC_PCIE)) {
492 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
493
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;
500
501 devfn = PCI_DEVFN(pcie_err->device_id.device,
502 pcie_err->device_id.function);
503 aer_severity = cper_severity_to_aer(gdata->error_severity);
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
513 aer_recover_queue(pcie_err->device_id.segment,
514 pcie_err->device_id.bus,
515 devfn, aer_severity,
516 (struct aer_capability_regs *)
517 pcie_err->aer_info);
518 }
519
520 }
521 #endif
522 else {
523 void *err = acpi_hest_get_payload(gdata);
524
525 log_non_standard_event(&sec_type, fru_id, fru_text,
526 sec_sev, err,
527 gdata->error_data_length);
528 }
529 }
530 }
531
532 static void __ghes_print_estatus(const char *pfx,
533 const struct acpi_hest_generic *generic,
534 const struct acpi_hest_generic_status *estatus)
535 {
536 static atomic_t seqno;
537 unsigned int curr_seqno;
538 char pfx_seq[64];
539
540 if (pfx == NULL) {
541 if (ghes_severity(estatus->error_severity) <=
542 GHES_SEV_CORRECTED)
543 pfx = KERN_WARNING;
544 else
545 pfx = KERN_ERR;
546 }
547 curr_seqno = atomic_inc_return(&seqno);
548 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
549 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
550 pfx_seq, generic->header.source_id);
551 cper_estatus_print(pfx_seq, estatus);
552 }
553
554 static int ghes_print_estatus(const char *pfx,
555 const struct acpi_hest_generic *generic,
556 const struct acpi_hest_generic_status *estatus)
557 {
558 /* Not more than 2 messages every 5 seconds */
559 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
560 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
561 struct ratelimit_state *ratelimit;
562
563 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
564 ratelimit = &ratelimit_corrected;
565 else
566 ratelimit = &ratelimit_uncorrected;
567 if (__ratelimit(ratelimit)) {
568 __ghes_print_estatus(pfx, generic, estatus);
569 return 1;
570 }
571 return 0;
572 }
573
574 /*
575 * GHES error status reporting throttle, to report more kinds of
576 * errors, instead of just most frequently occurred errors.
577 */
578 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
579 {
580 u32 len;
581 int i, cached = 0;
582 unsigned long long now;
583 struct ghes_estatus_cache *cache;
584 struct acpi_hest_generic_status *cache_estatus;
585
586 len = cper_estatus_len(estatus);
587 rcu_read_lock();
588 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
589 cache = rcu_dereference(ghes_estatus_caches[i]);
590 if (cache == NULL)
591 continue;
592 if (len != cache->estatus_len)
593 continue;
594 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
595 if (memcmp(estatus, cache_estatus, len))
596 continue;
597 atomic_inc(&cache->count);
598 now = sched_clock();
599 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
600 cached = 1;
601 break;
602 }
603 rcu_read_unlock();
604 return cached;
605 }
606
607 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
608 struct acpi_hest_generic *generic,
609 struct acpi_hest_generic_status *estatus)
610 {
611 int alloced;
612 u32 len, cache_len;
613 struct ghes_estatus_cache *cache;
614 struct acpi_hest_generic_status *cache_estatus;
615
616 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
617 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
618 atomic_dec(&ghes_estatus_cache_alloced);
619 return NULL;
620 }
621 len = cper_estatus_len(estatus);
622 cache_len = GHES_ESTATUS_CACHE_LEN(len);
623 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
624 if (!cache) {
625 atomic_dec(&ghes_estatus_cache_alloced);
626 return NULL;
627 }
628 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
629 memcpy(cache_estatus, estatus, len);
630 cache->estatus_len = len;
631 atomic_set(&cache->count, 0);
632 cache->generic = generic;
633 cache->time_in = sched_clock();
634 return cache;
635 }
636
637 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
638 {
639 u32 len;
640
641 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
642 len = GHES_ESTATUS_CACHE_LEN(len);
643 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
644 atomic_dec(&ghes_estatus_cache_alloced);
645 }
646
647 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
648 {
649 struct ghes_estatus_cache *cache;
650
651 cache = container_of(head, struct ghes_estatus_cache, rcu);
652 ghes_estatus_cache_free(cache);
653 }
654
655 static void ghes_estatus_cache_add(
656 struct acpi_hest_generic *generic,
657 struct acpi_hest_generic_status *estatus)
658 {
659 int i, slot = -1, count;
660 unsigned long long now, duration, period, max_period = 0;
661 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
662
663 new_cache = ghes_estatus_cache_alloc(generic, estatus);
664 if (new_cache == NULL)
665 return;
666 rcu_read_lock();
667 now = sched_clock();
668 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
669 cache = rcu_dereference(ghes_estatus_caches[i]);
670 if (cache == NULL) {
671 slot = i;
672 slot_cache = NULL;
673 break;
674 }
675 duration = now - cache->time_in;
676 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
677 slot = i;
678 slot_cache = cache;
679 break;
680 }
681 count = atomic_read(&cache->count);
682 period = duration;
683 do_div(period, (count + 1));
684 if (period > max_period) {
685 max_period = period;
686 slot = i;
687 slot_cache = cache;
688 }
689 }
690 /* new_cache must be put into array after its contents are written */
691 smp_wmb();
692 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
693 slot_cache, new_cache) == slot_cache) {
694 if (slot_cache)
695 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
696 } else
697 ghes_estatus_cache_free(new_cache);
698 rcu_read_unlock();
699 }
700
701 static int ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
702 {
703 int rc;
704 u64 val = 0;
705
706 rc = apei_read(&val, &gv2->read_ack_register);
707 if (rc)
708 return rc;
709
710 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
711 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
712
713 return apei_write(val, &gv2->read_ack_register);
714 }
715
716 static void __ghes_panic(struct ghes *ghes)
717 {
718 __ghes_print_estatus(KERN_EMERG, ghes->generic, ghes->estatus);
719
720 /* reboot to log the error! */
721 if (!panic_timeout)
722 panic_timeout = ghes_panic_timeout;
723 panic("Fatal hardware error!");
724 }
725
726 static int ghes_proc(struct ghes *ghes)
727 {
728 int rc;
729
730 rc = ghes_read_estatus(ghes, 0);
731 if (rc)
732 goto out;
733
734 if (ghes_severity(ghes->estatus->error_severity) >= GHES_SEV_PANIC) {
735 __ghes_panic(ghes);
736 }
737
738 if (!ghes_estatus_cached(ghes->estatus)) {
739 if (ghes_print_estatus(NULL, ghes->generic, ghes->estatus))
740 ghes_estatus_cache_add(ghes->generic, ghes->estatus);
741 }
742 ghes_do_proc(ghes, ghes->estatus);
743
744 /*
745 * GHESv2 type HEST entries introduce support for error acknowledgment,
746 * so only acknowledge the error if this support is present.
747 */
748 if (is_hest_type_generic_v2(ghes)) {
749 rc = ghes_ack_error(ghes->generic_v2);
750 if (rc)
751 return rc;
752 }
753 out:
754 ghes_clear_estatus(ghes);
755 return rc;
756 }
757
758 static void ghes_add_timer(struct ghes *ghes)
759 {
760 struct acpi_hest_generic *g = ghes->generic;
761 unsigned long expire;
762
763 if (!g->notify.poll_interval) {
764 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
765 g->header.source_id);
766 return;
767 }
768 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
769 ghes->timer.expires = round_jiffies_relative(expire);
770 add_timer(&ghes->timer);
771 }
772
773 static void ghes_poll_func(unsigned long data)
774 {
775 struct ghes *ghes = (void *)data;
776
777 ghes_proc(ghes);
778 if (!(ghes->flags & GHES_EXITING))
779 ghes_add_timer(ghes);
780 }
781
782 static irqreturn_t ghes_irq_func(int irq, void *data)
783 {
784 struct ghes *ghes = data;
785 int rc;
786
787 rc = ghes_proc(ghes);
788 if (rc)
789 return IRQ_NONE;
790
791 return IRQ_HANDLED;
792 }
793
794 static int ghes_notify_sci(struct notifier_block *this,
795 unsigned long event, void *data)
796 {
797 struct ghes *ghes;
798 int ret = NOTIFY_DONE;
799
800 rcu_read_lock();
801 list_for_each_entry_rcu(ghes, &ghes_sci, list) {
802 if (!ghes_proc(ghes))
803 ret = NOTIFY_OK;
804 }
805 rcu_read_unlock();
806
807 return ret;
808 }
809
810 static struct notifier_block ghes_notifier_sci = {
811 .notifier_call = ghes_notify_sci,
812 };
813
814 #ifdef CONFIG_ACPI_APEI_SEA
815 static LIST_HEAD(ghes_sea);
816
817 void ghes_notify_sea(void)
818 {
819 struct ghes *ghes;
820
821 /*
822 * synchronize_rcu() will wait for nmi_exit(), so no need to
823 * rcu_read_lock().
824 */
825 list_for_each_entry_rcu(ghes, &ghes_sea, list) {
826 ghes_proc(ghes);
827 }
828 }
829
830 static void ghes_sea_add(struct ghes *ghes)
831 {
832 mutex_lock(&ghes_list_mutex);
833 list_add_rcu(&ghes->list, &ghes_sea);
834 mutex_unlock(&ghes_list_mutex);
835 }
836
837 static void ghes_sea_remove(struct ghes *ghes)
838 {
839 mutex_lock(&ghes_list_mutex);
840 list_del_rcu(&ghes->list);
841 mutex_unlock(&ghes_list_mutex);
842 synchronize_rcu();
843 }
844 #else /* CONFIG_ACPI_APEI_SEA */
845 static inline void ghes_sea_add(struct ghes *ghes)
846 {
847 pr_err(GHES_PFX "ID: %d, trying to add SEA notification which is not supported\n",
848 ghes->generic->header.source_id);
849 }
850
851 static inline void ghes_sea_remove(struct ghes *ghes)
852 {
853 pr_err(GHES_PFX "ID: %d, trying to remove SEA notification which is not supported\n",
854 ghes->generic->header.source_id);
855 }
856 #endif /* CONFIG_ACPI_APEI_SEA */
857
858 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
859 /*
860 * printk is not safe in NMI context. So in NMI handler, we allocate
861 * required memory from lock-less memory allocator
862 * (ghes_estatus_pool), save estatus into it, put them into lock-less
863 * list (ghes_estatus_llist), then delay printk into IRQ context via
864 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
865 * required pool size by all NMI error source.
866 */
867 static struct llist_head ghes_estatus_llist;
868 static struct irq_work ghes_proc_irq_work;
869
870 /*
871 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
872 * having only one concurrent reader.
873 */
874 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
875
876 static LIST_HEAD(ghes_nmi);
877
878 static void ghes_proc_in_irq(struct irq_work *irq_work)
879 {
880 struct llist_node *llnode, *next;
881 struct ghes_estatus_node *estatus_node;
882 struct acpi_hest_generic *generic;
883 struct acpi_hest_generic_status *estatus;
884 u32 len, node_len;
885
886 llnode = llist_del_all(&ghes_estatus_llist);
887 /*
888 * Because the time order of estatus in list is reversed,
889 * revert it back to proper order.
890 */
891 llnode = llist_reverse_order(llnode);
892 while (llnode) {
893 next = llnode->next;
894 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
895 llnode);
896 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
897 len = cper_estatus_len(estatus);
898 node_len = GHES_ESTATUS_NODE_LEN(len);
899 ghes_do_proc(estatus_node->ghes, estatus);
900 if (!ghes_estatus_cached(estatus)) {
901 generic = estatus_node->generic;
902 if (ghes_print_estatus(NULL, generic, estatus))
903 ghes_estatus_cache_add(generic, estatus);
904 }
905 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
906 node_len);
907 llnode = next;
908 }
909 }
910
911 static void ghes_print_queued_estatus(void)
912 {
913 struct llist_node *llnode;
914 struct ghes_estatus_node *estatus_node;
915 struct acpi_hest_generic *generic;
916 struct acpi_hest_generic_status *estatus;
917 u32 len, node_len;
918
919 llnode = llist_del_all(&ghes_estatus_llist);
920 /*
921 * Because the time order of estatus in list is reversed,
922 * revert it back to proper order.
923 */
924 llnode = llist_reverse_order(llnode);
925 while (llnode) {
926 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
927 llnode);
928 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
929 len = cper_estatus_len(estatus);
930 node_len = GHES_ESTATUS_NODE_LEN(len);
931 generic = estatus_node->generic;
932 ghes_print_estatus(NULL, generic, estatus);
933 llnode = llnode->next;
934 }
935 }
936
937 /* Save estatus for further processing in IRQ context */
938 static void __process_error(struct ghes *ghes)
939 {
940 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
941 u32 len, node_len;
942 struct ghes_estatus_node *estatus_node;
943 struct acpi_hest_generic_status *estatus;
944
945 if (ghes_estatus_cached(ghes->estatus))
946 return;
947
948 len = cper_estatus_len(ghes->estatus);
949 node_len = GHES_ESTATUS_NODE_LEN(len);
950
951 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
952 if (!estatus_node)
953 return;
954
955 estatus_node->ghes = ghes;
956 estatus_node->generic = ghes->generic;
957 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
958 memcpy(estatus, ghes->estatus, len);
959 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
960 #endif
961 }
962
963 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
964 {
965 struct ghes *ghes;
966 int sev, ret = NMI_DONE;
967
968 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
969 return ret;
970
971 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
972 if (ghes_read_estatus(ghes, 1)) {
973 ghes_clear_estatus(ghes);
974 continue;
975 } else {
976 ret = NMI_HANDLED;
977 }
978
979 sev = ghes_severity(ghes->estatus->error_severity);
980 if (sev >= GHES_SEV_PANIC) {
981 oops_begin();
982 ghes_print_queued_estatus();
983 __ghes_panic(ghes);
984 }
985
986 if (!(ghes->flags & GHES_TO_CLEAR))
987 continue;
988
989 __process_error(ghes);
990 ghes_clear_estatus(ghes);
991 }
992
993 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
994 if (ret == NMI_HANDLED)
995 irq_work_queue(&ghes_proc_irq_work);
996 #endif
997 atomic_dec(&ghes_in_nmi);
998 return ret;
999 }
1000
1001 static unsigned long ghes_esource_prealloc_size(
1002 const struct acpi_hest_generic *generic)
1003 {
1004 unsigned long block_length, prealloc_records, prealloc_size;
1005
1006 block_length = min_t(unsigned long, generic->error_block_length,
1007 GHES_ESTATUS_MAX_SIZE);
1008 prealloc_records = max_t(unsigned long,
1009 generic->records_to_preallocate, 1);
1010 prealloc_size = min_t(unsigned long, block_length * prealloc_records,
1011 GHES_ESOURCE_PREALLOC_MAX_SIZE);
1012
1013 return prealloc_size;
1014 }
1015
1016 static void ghes_estatus_pool_shrink(unsigned long len)
1017 {
1018 ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
1019 }
1020
1021 static void ghes_nmi_add(struct ghes *ghes)
1022 {
1023 unsigned long len;
1024
1025 len = ghes_esource_prealloc_size(ghes->generic);
1026 ghes_estatus_pool_expand(len);
1027 mutex_lock(&ghes_list_mutex);
1028 if (list_empty(&ghes_nmi))
1029 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1030 list_add_rcu(&ghes->list, &ghes_nmi);
1031 mutex_unlock(&ghes_list_mutex);
1032 }
1033
1034 static void ghes_nmi_remove(struct ghes *ghes)
1035 {
1036 unsigned long len;
1037
1038 mutex_lock(&ghes_list_mutex);
1039 list_del_rcu(&ghes->list);
1040 if (list_empty(&ghes_nmi))
1041 unregister_nmi_handler(NMI_LOCAL, "ghes");
1042 mutex_unlock(&ghes_list_mutex);
1043 /*
1044 * To synchronize with NMI handler, ghes can only be
1045 * freed after NMI handler finishes.
1046 */
1047 synchronize_rcu();
1048 len = ghes_esource_prealloc_size(ghes->generic);
1049 ghes_estatus_pool_shrink(len);
1050 }
1051
1052 static void ghes_nmi_init_cxt(void)
1053 {
1054 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1055 }
1056 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1057 static inline void ghes_nmi_add(struct ghes *ghes)
1058 {
1059 pr_err(GHES_PFX "ID: %d, trying to add NMI notification which is not supported!\n",
1060 ghes->generic->header.source_id);
1061 BUG();
1062 }
1063
1064 static inline void ghes_nmi_remove(struct ghes *ghes)
1065 {
1066 pr_err(GHES_PFX "ID: %d, trying to remove NMI notification which is not supported!\n",
1067 ghes->generic->header.source_id);
1068 BUG();
1069 }
1070
1071 static inline void ghes_nmi_init_cxt(void)
1072 {
1073 }
1074 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1075
1076 static int ghes_probe(struct platform_device *ghes_dev)
1077 {
1078 struct acpi_hest_generic *generic;
1079 struct ghes *ghes = NULL;
1080
1081 int rc = -EINVAL;
1082
1083 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1084 if (!generic->enabled)
1085 return -ENODEV;
1086
1087 switch (generic->notify.type) {
1088 case ACPI_HEST_NOTIFY_POLLED:
1089 case ACPI_HEST_NOTIFY_EXTERNAL:
1090 case ACPI_HEST_NOTIFY_SCI:
1091 break;
1092 case ACPI_HEST_NOTIFY_SEA:
1093 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1094 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1095 generic->header.source_id);
1096 rc = -ENOTSUPP;
1097 goto err;
1098 }
1099 break;
1100 case ACPI_HEST_NOTIFY_NMI:
1101 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1102 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1103 generic->header.source_id);
1104 goto err;
1105 }
1106 break;
1107 case ACPI_HEST_NOTIFY_LOCAL:
1108 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1109 generic->header.source_id);
1110 goto err;
1111 default:
1112 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1113 generic->notify.type, generic->header.source_id);
1114 goto err;
1115 }
1116
1117 rc = -EIO;
1118 if (generic->error_block_length <
1119 sizeof(struct acpi_hest_generic_status)) {
1120 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1121 generic->error_block_length,
1122 generic->header.source_id);
1123 goto err;
1124 }
1125 ghes = ghes_new(generic);
1126 if (IS_ERR(ghes)) {
1127 rc = PTR_ERR(ghes);
1128 ghes = NULL;
1129 goto err;
1130 }
1131
1132 rc = ghes_edac_register(ghes, &ghes_dev->dev);
1133 if (rc < 0)
1134 goto err;
1135
1136 switch (generic->notify.type) {
1137 case ACPI_HEST_NOTIFY_POLLED:
1138 ghes->timer.function = ghes_poll_func;
1139 ghes->timer.data = (unsigned long)ghes;
1140 init_timer_deferrable(&ghes->timer);
1141 ghes_add_timer(ghes);
1142 break;
1143 case ACPI_HEST_NOTIFY_EXTERNAL:
1144 /* External interrupt vector is GSI */
1145 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1146 if (rc) {
1147 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1148 generic->header.source_id);
1149 goto err_edac_unreg;
1150 }
1151 rc = request_irq(ghes->irq, ghes_irq_func, 0, "GHES IRQ", ghes);
1152 if (rc) {
1153 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1154 generic->header.source_id);
1155 goto err_edac_unreg;
1156 }
1157 break;
1158 case ACPI_HEST_NOTIFY_SCI:
1159 mutex_lock(&ghes_list_mutex);
1160 if (list_empty(&ghes_sci))
1161 register_acpi_hed_notifier(&ghes_notifier_sci);
1162 list_add_rcu(&ghes->list, &ghes_sci);
1163 mutex_unlock(&ghes_list_mutex);
1164 break;
1165 case ACPI_HEST_NOTIFY_SEA:
1166 ghes_sea_add(ghes);
1167 break;
1168 case ACPI_HEST_NOTIFY_NMI:
1169 ghes_nmi_add(ghes);
1170 break;
1171 default:
1172 BUG();
1173 }
1174 platform_set_drvdata(ghes_dev, ghes);
1175
1176 return 0;
1177 err_edac_unreg:
1178 ghes_edac_unregister(ghes);
1179 err:
1180 if (ghes) {
1181 ghes_fini(ghes);
1182 kfree(ghes);
1183 }
1184 return rc;
1185 }
1186
1187 static int ghes_remove(struct platform_device *ghes_dev)
1188 {
1189 struct ghes *ghes;
1190 struct acpi_hest_generic *generic;
1191
1192 ghes = platform_get_drvdata(ghes_dev);
1193 generic = ghes->generic;
1194
1195 ghes->flags |= GHES_EXITING;
1196 switch (generic->notify.type) {
1197 case ACPI_HEST_NOTIFY_POLLED:
1198 del_timer_sync(&ghes->timer);
1199 break;
1200 case ACPI_HEST_NOTIFY_EXTERNAL:
1201 free_irq(ghes->irq, ghes);
1202 break;
1203 case ACPI_HEST_NOTIFY_SCI:
1204 mutex_lock(&ghes_list_mutex);
1205 list_del_rcu(&ghes->list);
1206 if (list_empty(&ghes_sci))
1207 unregister_acpi_hed_notifier(&ghes_notifier_sci);
1208 mutex_unlock(&ghes_list_mutex);
1209 break;
1210 case ACPI_HEST_NOTIFY_SEA:
1211 ghes_sea_remove(ghes);
1212 break;
1213 case ACPI_HEST_NOTIFY_NMI:
1214 ghes_nmi_remove(ghes);
1215 break;
1216 default:
1217 BUG();
1218 break;
1219 }
1220
1221 ghes_fini(ghes);
1222
1223 ghes_edac_unregister(ghes);
1224
1225 kfree(ghes);
1226
1227 platform_set_drvdata(ghes_dev, NULL);
1228
1229 return 0;
1230 }
1231
1232 static struct platform_driver ghes_platform_driver = {
1233 .driver = {
1234 .name = "GHES",
1235 },
1236 .probe = ghes_probe,
1237 .remove = ghes_remove,
1238 };
1239
1240 static int __init ghes_init(void)
1241 {
1242 int rc;
1243
1244 if (acpi_disabled)
1245 return -ENODEV;
1246
1247 if (hest_disable) {
1248 pr_info(GHES_PFX "HEST is not enabled!\n");
1249 return -EINVAL;
1250 }
1251
1252 if (ghes_disable) {
1253 pr_info(GHES_PFX "GHES is not enabled!\n");
1254 return -EINVAL;
1255 }
1256
1257 ghes_nmi_init_cxt();
1258
1259 rc = ghes_ioremap_init();
1260 if (rc)
1261 goto err;
1262
1263 rc = ghes_estatus_pool_init();
1264 if (rc)
1265 goto err_ioremap_exit;
1266
1267 rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
1268 GHES_ESTATUS_CACHE_ALLOCED_MAX);
1269 if (rc)
1270 goto err_pool_exit;
1271
1272 rc = platform_driver_register(&ghes_platform_driver);
1273 if (rc)
1274 goto err_pool_exit;
1275
1276 rc = apei_osc_setup();
1277 if (rc == 0 && osc_sb_apei_support_acked)
1278 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1279 else if (rc == 0 && !osc_sb_apei_support_acked)
1280 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1281 else if (rc && osc_sb_apei_support_acked)
1282 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1283 else
1284 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1285
1286 return 0;
1287 err_pool_exit:
1288 ghes_estatus_pool_exit();
1289 err_ioremap_exit:
1290 ghes_ioremap_exit();
1291 err:
1292 return rc;
1293 }
1294 device_initcall(ghes_init);