blob: 103acbbfcf9a513ff219404e0fe8894df1f563db [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Huang Yingd334a492010-05-18 14:35:20 +08002/*
3 * APEI Generic Hardware Error Source support
4 *
5 * Generic Hardware Error Source provides a way to report platform
6 * hardware errors (such as that from chipset). It works in so called
7 * "Firmware First" mode, that is, hardware errors are reported to
8 * firmware firstly, then reported to Linux by firmware. This way,
9 * some non-standard hardware error registers or non-standard hardware
10 * link can be checked by firmware to produce more hardware error
11 * information for Linux.
12 *
13 * For more information about Generic Hardware Error Source, please
14 * refer to ACPI Specification version 4.0, section 17.3.2.6
15 *
Huang Ying67eb2e92011-07-13 13:14:25 +080016 * Copyright 2010,2011 Intel Corp.
Huang Yingd334a492010-05-18 14:35:20 +080017 * Author: Huang Ying <ying.huang@intel.com>
Huang Yingd334a492010-05-18 14:35:20 +080018 */
19
James Morsef9f05392019-01-29 18:49:02 +000020#include <linux/arm_sdei.h>
Huang Yingd334a492010-05-18 14:35:20 +080021#include <linux/kernel.h>
Paul Gortmaker020bf062016-02-15 00:27:50 -050022#include <linux/moduleparam.h>
Huang Yingd334a492010-05-18 14:35:20 +080023#include <linux/init.h>
24#include <linux/acpi.h>
25#include <linux/io.h>
26#include <linux/interrupt.h>
Huang Ying81e88fd2011-01-12 14:44:55 +080027#include <linux/timer.h>
Huang Yingd334a492010-05-18 14:35:20 +080028#include <linux/cper.h>
Huang Ying7ad6e942010-08-02 15:48:24 +080029#include <linux/platform_device.h>
30#include <linux/mutex.h>
Huang Ying32c361f2010-12-07 10:22:31 +080031#include <linux/ratelimit.h>
Huang Ying81e88fd2011-01-12 14:44:55 +080032#include <linux/vmalloc.h>
Huang Ying67eb2e92011-07-13 13:14:25 +080033#include <linux/irq_work.h>
34#include <linux/llist.h>
35#include <linux/genalloc.h>
Huang Yinga654e5e2011-12-08 11:25:41 +080036#include <linux/pci.h>
James Morseb4840792019-01-29 18:48:52 +000037#include <linux/pfn.h>
Huang Yinga654e5e2011-12-08 11:25:41 +080038#include <linux/aer.h>
Tomasz Nowicki44a69f62014-07-22 11:20:12 +020039#include <linux/nmi.h>
Ingo Molnare6017572017-02-01 16:36:40 +010040#include <linux/sched/clock.h>
Tyler Baicar297b64c2017-06-21 12:17:12 -060041#include <linux/uuid.h>
42#include <linux/ras.h>
Mauro Carvalho Chehab40e06412013-02-15 05:41:22 -030043
Tyler Baicar42aa5602017-06-21 12:17:04 -060044#include <acpi/actbl1.h>
Mauro Carvalho Chehab40e06412013-02-15 05:41:22 -030045#include <acpi/ghes.h>
Tomasz Nowicki9dae3d02014-07-22 11:20:11 +020046#include <acpi/apei.h>
James Morse4f89fa22017-11-06 18:44:24 +000047#include <asm/fixmap.h>
Huang Ying81e88fd2011-01-12 14:44:55 +080048#include <asm/tlbflush.h>
Tyler Baicar297b64c2017-06-21 12:17:12 -060049#include <ras/ras_event.h>
Huang Yingd334a492010-05-18 14:35:20 +080050
51#include "apei-internal.h"
52
53#define GHES_PFX "GHES: "
54
55#define GHES_ESTATUS_MAX_SIZE 65536
Huang Ying67eb2e92011-07-13 13:14:25 +080056#define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
57
58#define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
59
Huang Ying152cef42011-07-13 13:14:26 +080060/* This is just an estimation for memory pool allocation */
61#define GHES_ESTATUS_CACHE_AVG_SIZE 512
62
63#define GHES_ESTATUS_CACHES_SIZE 4
64
Len Brown70cb6e12011-08-02 18:00:21 -040065#define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
Huang Ying152cef42011-07-13 13:14:26 +080066/* Prevent too many caches are allocated because of RCU */
67#define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
68
69#define GHES_ESTATUS_CACHE_LEN(estatus_len) \
70 (sizeof(struct ghes_estatus_cache) + (estatus_len))
71#define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
Lv Zheng0a00fd52014-06-03 16:32:53 +080072 ((struct acpi_hest_generic_status *) \
Huang Ying152cef42011-07-13 13:14:26 +080073 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
74
Huang Ying67eb2e92011-07-13 13:14:25 +080075#define GHES_ESTATUS_NODE_LEN(estatus_len) \
76 (sizeof(struct ghes_estatus_node) + (estatus_len))
Chen, Gong88f074f2013-10-18 14:28:59 -070077#define GHES_ESTATUS_FROM_NODE(estatus_node) \
Lv Zheng0a00fd52014-06-03 16:32:53 +080078 ((struct acpi_hest_generic_status *) \
Huang Ying67eb2e92011-07-13 13:14:25 +080079 ((struct ghes_estatus_node *)(estatus_node) + 1))
Huang Yingd334a492010-05-18 14:35:20 +080080
James Morsef9f05392019-01-29 18:49:02 +000081/*
82 * NMI-like notifications vary by architecture, before the compiler can prune
83 * unused static functions it needs a value for these enums.
84 */
85#ifndef CONFIG_ARM_SDE_INTERFACE
86#define FIX_APEI_GHES_SDEI_NORMAL __end_of_fixed_addresses
87#define FIX_APEI_GHES_SDEI_CRITICAL __end_of_fixed_addresses
88#endif
89
Tyler Baicar42aa5602017-06-21 12:17:04 -060090static inline bool is_hest_type_generic_v2(struct ghes *ghes)
91{
92 return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
93}
94
Paul Gortmaker020bf062016-02-15 00:27:50 -050095/*
96 * This driver isn't really modular, however for the time being,
97 * continuing to use module_param is the easiest way to remain
98 * compatible with existing boot arg use cases.
99 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030100bool ghes_disable;
Huang Yingb6a95012011-07-13 13:14:19 +0800101module_param_named(disable, ghes_disable, bool, 0);
102
Huang Yingd334a492010-05-18 14:35:20 +0800103/*
Shiju Jose7bf130e2017-05-19 11:39:11 +0200104 * All error sources notified with HED (Hardware Error Device) share a
105 * single notifier callback, so they need to be linked and checked one
106 * by one. This holds true for NMI too.
Huang Yingd334a492010-05-18 14:35:20 +0800107 *
Huang Ying81e88fd2011-01-12 14:44:55 +0800108 * RCU is used for these lists, so ghes_list_mutex is only used for
109 * list changing, not for traversing.
Huang Yingd334a492010-05-18 14:35:20 +0800110 */
Shiju Jose7bf130e2017-05-19 11:39:11 +0200111static LIST_HEAD(ghes_hed);
Huang Ying7ad6e942010-08-02 15:48:24 +0800112static DEFINE_MUTEX(ghes_list_mutex);
Huang Yingd334a492010-05-18 14:35:20 +0800113
Huang Ying81e88fd2011-01-12 14:44:55 +0800114/*
Huang Ying81e88fd2011-01-12 14:44:55 +0800115 * Because the memory area used to transfer hardware error information
116 * from BIOS to Linux can be determined only in NMI, IRQ or timer
117 * handler, but general ioremap can not be used in atomic context, so
James Morse4f89fa22017-11-06 18:44:24 +0000118 * the fixmap is used instead.
James Morse520e18a2017-11-06 18:44:25 +0000119 *
James Morse3b880cb2019-01-29 18:48:51 +0000120 * This spinlock is used to prevent the fixmap entry from being used
James Morse4f89fa22017-11-06 18:44:24 +0000121 * simultaneously.
Huang Ying81e88fd2011-01-12 14:44:55 +0800122 */
James Morse3b880cb2019-01-29 18:48:51 +0000123static DEFINE_SPINLOCK(ghes_notify_lock_irq);
Huang Ying81e88fd2011-01-12 14:44:55 +0800124
Huang Ying67eb2e92011-07-13 13:14:25 +0800125static struct gen_pool *ghes_estatus_pool;
126static unsigned long ghes_estatus_pool_size_request;
Huang Ying67eb2e92011-07-13 13:14:25 +0800127
Borislav Petkov8f7c31f2014-09-29 13:33:17 +0200128static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
Huang Ying152cef42011-07-13 13:14:26 +0800129static atomic_t ghes_estatus_cache_alloced;
130
Jonathan (Zhixiong) Zhang2fb58532017-06-21 12:17:10 -0600131static int ghes_panic_timeout __read_mostly = 30;
132
James Morseb4840792019-01-29 18:48:52 +0000133static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
Huang Ying81e88fd2011-01-12 14:44:55 +0800134{
Tyler Baicar7edda082017-06-21 12:17:09 -0600135 phys_addr_t paddr;
136 pgprot_t prot;
Huang Ying81e88fd2011-01-12 14:44:55 +0800137
James Morseb4840792019-01-29 18:48:52 +0000138 paddr = PFN_PHYS(pfn);
Tyler Baicar7edda082017-06-21 12:17:09 -0600139 prot = arch_apei_get_mem_attribute(paddr);
James Morseb4840792019-01-29 18:48:52 +0000140 __set_fixmap(fixmap_idx, paddr, prot);
Huang Ying81e88fd2011-01-12 14:44:55 +0800141
James Morseb4840792019-01-29 18:48:52 +0000142 return (void __iomem *) __fix_to_virt(fixmap_idx);
Huang Ying81e88fd2011-01-12 14:44:55 +0800143}
144
James Morseb4840792019-01-29 18:48:52 +0000145static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
Huang Ying81e88fd2011-01-12 14:44:55 +0800146{
James Morseb4840792019-01-29 18:48:52 +0000147 int _idx = virt_to_fix((unsigned long)vaddr);
Huang Ying81e88fd2011-01-12 14:44:55 +0800148
James Morseb4840792019-01-29 18:48:52 +0000149 WARN_ON_ONCE(fixmap_idx != _idx);
150 clear_fixmap(fixmap_idx);
Huang Ying81e88fd2011-01-12 14:44:55 +0800151}
152
James Morsefb7be082019-01-29 18:48:41 +0000153int ghes_estatus_pool_init(int num_ghes)
Huang Ying67eb2e92011-07-13 13:14:25 +0800154{
James Morsefb7be082019-01-29 18:48:41 +0000155 unsigned long addr, len;
Liguang Zhang6abc7622019-07-15 14:58:44 +0800156 int rc;
James Morsefb7be082019-01-29 18:48:41 +0000157
Huang Ying67eb2e92011-07-13 13:14:25 +0800158 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
159 if (!ghes_estatus_pool)
160 return -ENOMEM;
Huang Ying67eb2e92011-07-13 13:14:25 +0800161
James Morsefb7be082019-01-29 18:48:41 +0000162 len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
163 len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
Huang Ying67eb2e92011-07-13 13:14:25 +0800164
James Morsefb7be082019-01-29 18:48:41 +0000165 ghes_estatus_pool_size_request = PAGE_ALIGN(len);
James Morse0ac234b2019-01-29 18:48:39 +0000166 addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
167 if (!addr)
Liguang Zhang6abc7622019-07-15 14:58:44 +0800168 goto err_pool_alloc;
James Morse0ac234b2019-01-29 18:48:39 +0000169
170 /*
171 * New allocation must be visible in all pgd before it can be found by
172 * an NMI allocating from the pool.
173 */
174 vmalloc_sync_all();
175
Liguang Zhang6abc7622019-07-15 14:58:44 +0800176 rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
177 if (rc)
178 goto err_pool_add;
179
180 return 0;
181
182err_pool_add:
183 vfree((void *)addr);
184
185err_pool_alloc:
186 gen_pool_destroy(ghes_estatus_pool);
187
188 return -ENOMEM;
Huang Ying67eb2e92011-07-13 13:14:25 +0800189}
190
Tyler Baicar42aa5602017-06-21 12:17:04 -0600191static int map_gen_v2(struct ghes *ghes)
192{
193 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
194}
195
196static void unmap_gen_v2(struct ghes *ghes)
197{
198 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
199}
200
James Morse06ddead2019-01-29 18:48:46 +0000201static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
202{
203 int rc;
204 u64 val = 0;
205
206 rc = apei_read(&val, &gv2->read_ack_register);
207 if (rc)
208 return;
209
210 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
211 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
212
213 apei_write(val, &gv2->read_ack_register);
214}
215
Huang Yingd334a492010-05-18 14:35:20 +0800216static struct ghes *ghes_new(struct acpi_hest_generic *generic)
217{
218 struct ghes *ghes;
219 unsigned int error_block_length;
220 int rc;
221
222 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
223 if (!ghes)
224 return ERR_PTR(-ENOMEM);
Tyler Baicar42aa5602017-06-21 12:17:04 -0600225
Huang Yingd334a492010-05-18 14:35:20 +0800226 ghes->generic = generic;
Tyler Baicar42aa5602017-06-21 12:17:04 -0600227 if (is_hest_type_generic_v2(ghes)) {
228 rc = map_gen_v2(ghes);
229 if (rc)
230 goto err_free;
231 }
232
Huang Ying34ddeb02012-06-12 11:20:19 +0800233 rc = apei_map_generic_address(&generic->error_status_address);
Huang Yingd334a492010-05-18 14:35:20 +0800234 if (rc)
Tyler Baicar42aa5602017-06-21 12:17:04 -0600235 goto err_unmap_read_ack_addr;
Huang Yingd334a492010-05-18 14:35:20 +0800236 error_block_length = generic->error_block_length;
237 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
Kefeng Wang933ca4e2019-10-18 11:18:25 +0800238 pr_warn(FW_WARN GHES_PFX
239 "Error status block length is too long: %u for "
240 "generic hardware error source: %d.\n",
241 error_block_length, generic->header.source_id);
Huang Yingd334a492010-05-18 14:35:20 +0800242 error_block_length = GHES_ESTATUS_MAX_SIZE;
243 }
244 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
245 if (!ghes->estatus) {
246 rc = -ENOMEM;
Tyler Baicar42aa5602017-06-21 12:17:04 -0600247 goto err_unmap_status_addr;
Huang Yingd334a492010-05-18 14:35:20 +0800248 }
249
250 return ghes;
251
Tyler Baicar42aa5602017-06-21 12:17:04 -0600252err_unmap_status_addr:
Huang Ying34ddeb02012-06-12 11:20:19 +0800253 apei_unmap_generic_address(&generic->error_status_address);
Tyler Baicar42aa5602017-06-21 12:17:04 -0600254err_unmap_read_ack_addr:
255 if (is_hest_type_generic_v2(ghes))
256 unmap_gen_v2(ghes);
Huang Yingd334a492010-05-18 14:35:20 +0800257err_free:
258 kfree(ghes);
259 return ERR_PTR(rc);
260}
261
262static void ghes_fini(struct ghes *ghes)
263{
264 kfree(ghes->estatus);
Huang Ying34ddeb02012-06-12 11:20:19 +0800265 apei_unmap_generic_address(&ghes->generic->error_status_address);
Tyler Baicar42aa5602017-06-21 12:17:04 -0600266 if (is_hest_type_generic_v2(ghes))
267 unmap_gen_v2(ghes);
Huang Yingd334a492010-05-18 14:35:20 +0800268}
269
Huang Yingd334a492010-05-18 14:35:20 +0800270static inline int ghes_severity(int severity)
271{
272 switch (severity) {
Huang Yingad4ecef2010-08-02 15:48:23 +0800273 case CPER_SEV_INFORMATIONAL:
274 return GHES_SEV_NO;
275 case CPER_SEV_CORRECTED:
276 return GHES_SEV_CORRECTED;
277 case CPER_SEV_RECOVERABLE:
278 return GHES_SEV_RECOVERABLE;
279 case CPER_SEV_FATAL:
280 return GHES_SEV_PANIC;
Huang Yingd334a492010-05-18 14:35:20 +0800281 default:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300282 /* Unknown, go panic */
Huang Yingad4ecef2010-08-02 15:48:23 +0800283 return GHES_SEV_PANIC;
Huang Yingd334a492010-05-18 14:35:20 +0800284 }
285}
286
Huang Ying81e88fd2011-01-12 14:44:55 +0800287static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
James Morseb4840792019-01-29 18:48:52 +0000288 int from_phys,
289 enum fixed_addresses fixmap_idx)
Huang Yingd334a492010-05-18 14:35:20 +0800290{
Huang Ying81e88fd2011-01-12 14:44:55 +0800291 void __iomem *vaddr;
Huang Ying81e88fd2011-01-12 14:44:55 +0800292 u64 offset;
293 u32 trunk;
Huang Yingd334a492010-05-18 14:35:20 +0800294
Huang Ying81e88fd2011-01-12 14:44:55 +0800295 while (len > 0) {
296 offset = paddr - (paddr & PAGE_MASK);
James Morseb4840792019-01-29 18:48:52 +0000297 vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
Huang Ying81e88fd2011-01-12 14:44:55 +0800298 trunk = PAGE_SIZE - offset;
299 trunk = min(trunk, len);
300 if (from_phys)
301 memcpy_fromio(buffer, vaddr + offset, trunk);
302 else
303 memcpy_toio(vaddr + offset, buffer, trunk);
304 len -= trunk;
305 paddr += trunk;
306 buffer += trunk;
James Morseb4840792019-01-29 18:48:52 +0000307 ghes_unmap(vaddr, fixmap_idx);
Huang Ying81e88fd2011-01-12 14:44:55 +0800308 }
Huang Yingd334a492010-05-18 14:35:20 +0800309}
310
James Morsef2a681b2019-01-29 18:48:54 +0000311/* Check the top-level record header has an appropriate size. */
312static int __ghes_check_estatus(struct ghes *ghes,
313 struct acpi_hest_generic_status *estatus)
314{
315 u32 len = cper_estatus_len(estatus);
316
317 if (len < sizeof(*estatus)) {
318 pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
319 return -EIO;
320 }
321
322 if (len > ghes->generic->error_block_length) {
323 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
324 return -EIO;
325 }
326
327 if (cper_estatus_check_header(estatus)) {
328 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
329 return -EIO;
330 }
331
332 return 0;
333}
334
James Morsee00a6e32019-01-29 18:48:55 +0000335/* Read the CPER block, returning its address, and header in estatus. */
336static int __ghes_peek_estatus(struct ghes *ghes,
337 struct acpi_hest_generic_status *estatus,
338 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
Huang Yingd334a492010-05-18 14:35:20 +0800339{
340 struct acpi_hest_generic *g = ghes->generic;
Huang Yingd334a492010-05-18 14:35:20 +0800341 int rc;
342
James Morseeeb25552019-01-29 18:48:42 +0000343 rc = apei_read(buf_paddr, &g->error_status_address);
Huang Yingd334a492010-05-18 14:35:20 +0800344 if (rc) {
James Morseeeb25552019-01-29 18:48:42 +0000345 *buf_paddr = 0;
James Morse93066e92019-01-29 18:48:38 +0000346 pr_warn_ratelimited(FW_WARN GHES_PFX
Huang Yingd334a492010-05-18 14:35:20 +0800347"Failed to read error status block address for hardware error source: %d.\n",
348 g->header.source_id);
349 return -EIO;
350 }
James Morseeeb25552019-01-29 18:48:42 +0000351 if (!*buf_paddr)
Huang Yingd334a492010-05-18 14:35:20 +0800352 return -ENOENT;
353
James Morsef2a7e052019-01-29 18:48:53 +0000354 ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
355 fixmap_idx);
356 if (!estatus->block_status) {
James Morseeeb25552019-01-29 18:48:42 +0000357 *buf_paddr = 0;
Huang Yingd334a492010-05-18 14:35:20 +0800358 return -ENOENT;
James Morseeeb25552019-01-29 18:48:42 +0000359 }
Huang Yingd334a492010-05-18 14:35:20 +0800360
Liguang Zhang371b8682019-06-25 13:15:28 +0800361 return 0;
James Morsee00a6e32019-01-29 18:48:55 +0000362}
James Morsef2a681b2019-01-29 18:48:54 +0000363
James Morsee00a6e32019-01-29 18:48:55 +0000364static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
365 u64 buf_paddr, enum fixed_addresses fixmap_idx,
366 size_t buf_len)
367{
368 ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
James Morsef2a681b2019-01-29 18:48:54 +0000369 if (cper_estatus_check(estatus)) {
James Morse93066e92019-01-29 18:48:38 +0000370 pr_warn_ratelimited(FW_WARN GHES_PFX
371 "Failed to read error status block!\n");
James Morsef2a681b2019-01-29 18:48:54 +0000372 return -EIO;
373 }
James Morseeeb25552019-01-29 18:48:42 +0000374
James Morsef2a681b2019-01-29 18:48:54 +0000375 return 0;
Huang Yingd334a492010-05-18 14:35:20 +0800376}
377
James Morsee00a6e32019-01-29 18:48:55 +0000378static int ghes_read_estatus(struct ghes *ghes,
379 struct acpi_hest_generic_status *estatus,
380 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
381{
382 int rc;
383
384 rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
385 if (rc)
386 return rc;
387
388 rc = __ghes_check_estatus(ghes, estatus);
389 if (rc)
390 return rc;
391
392 return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
393 cper_estatus_len(estatus));
394}
395
James Morsef2a7e052019-01-29 18:48:53 +0000396static void ghes_clear_estatus(struct ghes *ghes,
397 struct acpi_hest_generic_status *estatus,
398 u64 buf_paddr, enum fixed_addresses fixmap_idx)
Huang Yingd334a492010-05-18 14:35:20 +0800399{
James Morsef2a7e052019-01-29 18:48:53 +0000400 estatus->block_status = 0;
James Morseeeb25552019-01-29 18:48:42 +0000401
402 if (!buf_paddr)
403 return;
404
James Morsef2a7e052019-01-29 18:48:53 +0000405 ghes_copy_tofrom_phys(estatus, buf_paddr,
406 sizeof(estatus->block_status), 0,
James Morseb4840792019-01-29 18:48:52 +0000407 fixmap_idx);
James Morse06ddead2019-01-29 18:48:46 +0000408
409 /*
410 * GHESv2 type HEST entries introduce support for error acknowledgment,
411 * so only acknowledge the error if this support is present.
412 */
413 if (is_hest_type_generic_v2(ghes))
414 ghes_ack_error(ghes->generic_v2);
Huang Yingd334a492010-05-18 14:35:20 +0800415}
416
Lv Zheng0a00fd52014-06-03 16:32:53 +0800417static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
Naveen N. Raocf870c72013-07-10 14:57:01 +0530418{
419#ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
420 unsigned long pfn;
Chen, Gongca104ed2013-11-25 02:15:01 -0500421 int flags = -1;
Naveen N. Raocf870c72013-07-10 14:57:01 +0530422 int sec_sev = ghes_severity(gdata->error_severity);
Tyler Baicarbbcc2e72017-06-21 12:17:05 -0600423 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
Naveen N. Raocf870c72013-07-10 14:57:01 +0530424
Chen, Gongca104ed2013-11-25 02:15:01 -0500425 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
426 return;
427
428 pfn = mem_err->physical_addr >> PAGE_SHIFT;
429 if (!pfn_valid(pfn)) {
430 pr_warn_ratelimited(FW_WARN GHES_PFX
431 "Invalid address in generic error data: %#llx\n",
432 mem_err->physical_addr);
433 return;
434 }
435
436 /* iff following two events can be handled properly by now */
Naveen N. Raocf870c72013-07-10 14:57:01 +0530437 if (sec_sev == GHES_SEV_CORRECTED &&
Chen, Gongca104ed2013-11-25 02:15:01 -0500438 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
439 flags = MF_SOFT_OFFLINE;
440 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
441 flags = 0;
442
443 if (flags != -1)
Eric W. Biederman83b57532017-07-09 18:14:01 -0500444 memory_failure_queue(pfn, flags);
Naveen N. Raocf870c72013-07-10 14:57:01 +0530445#endif
446}
447
Tyler Baicar9852ce92017-11-28 16:48:09 -0500448/*
449 * PCIe AER errors need to be sent to the AER driver for reporting and
450 * recovery. The GHES severities map to the following AER severities and
451 * require the following handling:
452 *
453 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
454 * These need to be reported by the AER driver but no recovery is
455 * necessary.
456 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
457 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
458 * These both need to be reported and recovered from by the AER driver.
459 * GHES_SEV_PANIC does not make it to this handling since the kernel must
460 * panic.
461 */
462static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
Tyler Baicar3c5b977f2017-11-28 16:48:08 -0500463{
464#ifdef CONFIG_ACPI_APEI_PCIEAER
465 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
466
Tyler Baicar9852ce92017-11-28 16:48:09 -0500467 if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
Tyler Baicar3c5b977f2017-11-28 16:48:08 -0500468 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
469 unsigned int devfn;
470 int aer_severity;
471
472 devfn = PCI_DEVFN(pcie_err->device_id.device,
473 pcie_err->device_id.function);
474 aer_severity = cper_severity_to_aer(gdata->error_severity);
475
476 /*
477 * If firmware reset the component to contain
478 * the error, we must reinitialize it before
479 * use, so treat it as a fatal AER error.
480 */
481 if (gdata->flags & CPER_SEC_RESET)
482 aer_severity = AER_FATAL;
483
484 aer_recover_queue(pcie_err->device_id.segment,
485 pcie_err->device_id.bus,
486 devfn, aer_severity,
487 (struct aer_capability_regs *)
488 pcie_err->aer_info);
489 }
490#endif
491}
492
Mauro Carvalho Chehab21480542013-02-15 06:10:39 -0300493static void ghes_do_proc(struct ghes *ghes,
Lv Zheng0a00fd52014-06-03 16:32:53 +0800494 const struct acpi_hest_generic_status *estatus)
Huang Yingd334a492010-05-18 14:35:20 +0800495{
Huang Yingba61ca42011-07-13 13:14:28 +0800496 int sev, sec_sev;
Lv Zheng0a00fd52014-06-03 16:32:53 +0800497 struct acpi_hest_generic_data *gdata;
Andy Shevchenko5b536962017-06-05 19:40:43 +0300498 guid_t *sec_type;
Andy Shevchenkobb100b62019-07-17 13:55:43 +0300499 const guid_t *fru_id = &guid_null;
Tyler Baicar297b64c2017-06-21 12:17:12 -0600500 char *fru_text = "";
Huang Yingd334a492010-05-18 14:35:20 +0800501
Huang Ying67eb2e92011-07-13 13:14:25 +0800502 sev = ghes_severity(estatus->error_severity);
503 apei_estatus_for_each_section(estatus, gdata) {
Andy Shevchenko5b536962017-06-05 19:40:43 +0300504 sec_type = (guid_t *)gdata->section_type;
Huang Yingba61ca42011-07-13 13:14:28 +0800505 sec_sev = ghes_severity(gdata->error_severity);
Tyler Baicar297b64c2017-06-21 12:17:12 -0600506 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
507 fru_id = (guid_t *)gdata->fru_id;
508
509 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
510 fru_text = gdata->fru_text;
511
Andy Shevchenko5b536962017-06-05 19:40:43 +0300512 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
Tyler Baicarbbcc2e72017-06-21 12:17:05 -0600513 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
514
Alexandru Gagniuc305d0e02018-04-30 16:33:50 -0500515 ghes_edac_report_mem_error(sev, mem_err);
Mauro Carvalho Chehab21480542013-02-15 06:10:39 -0300516
Tomasz Nowicki9dae3d02014-07-22 11:20:11 +0200517 arch_apei_report_mem_error(sev, mem_err);
Naveen N. Raocf870c72013-07-10 14:57:01 +0530518 ghes_handle_memory_failure(gdata, sev);
Huang Yingba61ca42011-07-13 13:14:28 +0800519 }
Andy Shevchenko5b536962017-06-05 19:40:43 +0300520 else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
Tyler Baicar9852ce92017-11-28 16:48:09 -0500521 ghes_handle_aer(gdata);
Huang Yinga654e5e2011-12-08 11:25:41 +0800522 }
Tyler Baicare9279e82017-06-21 12:17:13 -0600523 else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
524 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
525
526 log_arm_hw_error(err);
527 } else {
Tyler Baicar297b64c2017-06-21 12:17:12 -0600528 void *err = acpi_hest_get_payload(gdata);
529
530 log_non_standard_event(sec_type, fru_id, fru_text,
531 sec_sev, err,
532 gdata->error_data_length);
533 }
Huang Yingd334a492010-05-18 14:35:20 +0800534 }
Huang Ying32c361f2010-12-07 10:22:31 +0800535}
Huang Yingd334a492010-05-18 14:35:20 +0800536
Huang Ying67eb2e92011-07-13 13:14:25 +0800537static void __ghes_print_estatus(const char *pfx,
538 const struct acpi_hest_generic *generic,
Lv Zheng0a00fd52014-06-03 16:32:53 +0800539 const struct acpi_hest_generic_status *estatus)
Huang Ying32c361f2010-12-07 10:22:31 +0800540{
Huang Ying5ba82ab2011-12-08 11:25:44 +0800541 static atomic_t seqno;
542 unsigned int curr_seqno;
543 char pfx_seq[64];
544
Huang Ying32c361f2010-12-07 10:22:31 +0800545 if (pfx == NULL) {
Huang Ying67eb2e92011-07-13 13:14:25 +0800546 if (ghes_severity(estatus->error_severity) <=
Huang Ying32c361f2010-12-07 10:22:31 +0800547 GHES_SEV_CORRECTED)
Huang Ying5ba82ab2011-12-08 11:25:44 +0800548 pfx = KERN_WARNING;
Huang Ying32c361f2010-12-07 10:22:31 +0800549 else
Huang Ying5ba82ab2011-12-08 11:25:44 +0800550 pfx = KERN_ERR;
Huang Ying32c361f2010-12-07 10:22:31 +0800551 }
Huang Ying5ba82ab2011-12-08 11:25:44 +0800552 curr_seqno = atomic_inc_return(&seqno);
553 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
Huang Ying55883402011-07-13 13:14:15 +0800554 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
Huang Ying5ba82ab2011-12-08 11:25:44 +0800555 pfx_seq, generic->header.source_id);
Chen, Gong88f074f2013-10-18 14:28:59 -0700556 cper_estatus_print(pfx_seq, estatus);
Huang Ying55883402011-07-13 13:14:15 +0800557}
558
Huang Ying152cef42011-07-13 13:14:26 +0800559static int ghes_print_estatus(const char *pfx,
560 const struct acpi_hest_generic *generic,
Lv Zheng0a00fd52014-06-03 16:32:53 +0800561 const struct acpi_hest_generic_status *estatus)
Huang Ying55883402011-07-13 13:14:15 +0800562{
563 /* Not more than 2 messages every 5 seconds */
Huang Ying67eb2e92011-07-13 13:14:25 +0800564 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
565 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
566 struct ratelimit_state *ratelimit;
Huang Ying55883402011-07-13 13:14:15 +0800567
Huang Ying67eb2e92011-07-13 13:14:25 +0800568 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
569 ratelimit = &ratelimit_corrected;
570 else
571 ratelimit = &ratelimit_uncorrected;
Huang Ying152cef42011-07-13 13:14:26 +0800572 if (__ratelimit(ratelimit)) {
Huang Ying67eb2e92011-07-13 13:14:25 +0800573 __ghes_print_estatus(pfx, generic, estatus);
Huang Ying152cef42011-07-13 13:14:26 +0800574 return 1;
575 }
576 return 0;
577}
578
579/*
580 * GHES error status reporting throttle, to report more kinds of
581 * errors, instead of just most frequently occurred errors.
582 */
Lv Zheng0a00fd52014-06-03 16:32:53 +0800583static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
Huang Ying152cef42011-07-13 13:14:26 +0800584{
585 u32 len;
586 int i, cached = 0;
587 unsigned long long now;
588 struct ghes_estatus_cache *cache;
Lv Zheng0a00fd52014-06-03 16:32:53 +0800589 struct acpi_hest_generic_status *cache_estatus;
Huang Ying152cef42011-07-13 13:14:26 +0800590
Chen, Gong88f074f2013-10-18 14:28:59 -0700591 len = cper_estatus_len(estatus);
Huang Ying152cef42011-07-13 13:14:26 +0800592 rcu_read_lock();
593 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
594 cache = rcu_dereference(ghes_estatus_caches[i]);
595 if (cache == NULL)
596 continue;
597 if (len != cache->estatus_len)
598 continue;
599 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
600 if (memcmp(estatus, cache_estatus, len))
601 continue;
602 atomic_inc(&cache->count);
603 now = sched_clock();
604 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
605 cached = 1;
606 break;
607 }
608 rcu_read_unlock();
609 return cached;
610}
611
612static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
613 struct acpi_hest_generic *generic,
Lv Zheng0a00fd52014-06-03 16:32:53 +0800614 struct acpi_hest_generic_status *estatus)
Huang Ying152cef42011-07-13 13:14:26 +0800615{
616 int alloced;
617 u32 len, cache_len;
618 struct ghes_estatus_cache *cache;
Lv Zheng0a00fd52014-06-03 16:32:53 +0800619 struct acpi_hest_generic_status *cache_estatus;
Huang Ying152cef42011-07-13 13:14:26 +0800620
621 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
622 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
623 atomic_dec(&ghes_estatus_cache_alloced);
624 return NULL;
625 }
Chen, Gong88f074f2013-10-18 14:28:59 -0700626 len = cper_estatus_len(estatus);
Huang Ying152cef42011-07-13 13:14:26 +0800627 cache_len = GHES_ESTATUS_CACHE_LEN(len);
628 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
629 if (!cache) {
630 atomic_dec(&ghes_estatus_cache_alloced);
631 return NULL;
632 }
633 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
634 memcpy(cache_estatus, estatus, len);
635 cache->estatus_len = len;
636 atomic_set(&cache->count, 0);
637 cache->generic = generic;
638 cache->time_in = sched_clock();
639 return cache;
640}
641
642static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
643{
644 u32 len;
645
Chen, Gong88f074f2013-10-18 14:28:59 -0700646 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
Huang Ying152cef42011-07-13 13:14:26 +0800647 len = GHES_ESTATUS_CACHE_LEN(len);
648 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
649 atomic_dec(&ghes_estatus_cache_alloced);
650}
651
652static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
653{
654 struct ghes_estatus_cache *cache;
655
656 cache = container_of(head, struct ghes_estatus_cache, rcu);
657 ghes_estatus_cache_free(cache);
658}
659
660static void ghes_estatus_cache_add(
661 struct acpi_hest_generic *generic,
Lv Zheng0a00fd52014-06-03 16:32:53 +0800662 struct acpi_hest_generic_status *estatus)
Huang Ying152cef42011-07-13 13:14:26 +0800663{
664 int i, slot = -1, count;
665 unsigned long long now, duration, period, max_period = 0;
666 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
667
668 new_cache = ghes_estatus_cache_alloc(generic, estatus);
669 if (new_cache == NULL)
670 return;
671 rcu_read_lock();
672 now = sched_clock();
673 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
674 cache = rcu_dereference(ghes_estatus_caches[i]);
675 if (cache == NULL) {
676 slot = i;
677 slot_cache = NULL;
678 break;
679 }
680 duration = now - cache->time_in;
681 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
682 slot = i;
683 slot_cache = cache;
684 break;
685 }
686 count = atomic_read(&cache->count);
Len Brown70cb6e12011-08-02 18:00:21 -0400687 period = duration;
688 do_div(period, (count + 1));
Huang Ying152cef42011-07-13 13:14:26 +0800689 if (period > max_period) {
690 max_period = period;
691 slot = i;
692 slot_cache = cache;
693 }
694 }
695 /* new_cache must be put into array after its contents are written */
696 smp_wmb();
697 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
698 slot_cache, new_cache) == slot_cache) {
699 if (slot_cache)
700 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
701 } else
702 ghes_estatus_cache_free(new_cache);
703 rcu_read_unlock();
Huang Yingd334a492010-05-18 14:35:20 +0800704}
705
James Morsef2a7e052019-01-29 18:48:53 +0000706static void __ghes_panic(struct ghes *ghes,
707 struct acpi_hest_generic_status *estatus,
708 u64 buf_paddr, enum fixed_addresses fixmap_idx)
Jonathan (Zhixiong) Zhang2fb58532017-06-21 12:17:10 -0600709{
James Morsef2a7e052019-01-29 18:48:53 +0000710 __ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
Jonathan (Zhixiong) Zhang2fb58532017-06-21 12:17:10 -0600711
James Morsef2a7e052019-01-29 18:48:53 +0000712 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
Lenny Szubowicz98cff8b2018-12-19 11:50:52 -0500713
Jonathan (Zhixiong) Zhang2fb58532017-06-21 12:17:10 -0600714 /* reboot to log the error! */
715 if (!panic_timeout)
716 panic_timeout = ghes_panic_timeout;
717 panic("Fatal hardware error!");
718}
719
Huang Yingd334a492010-05-18 14:35:20 +0800720static int ghes_proc(struct ghes *ghes)
721{
James Morsef2a7e052019-01-29 18:48:53 +0000722 struct acpi_hest_generic_status *estatus = ghes->estatus;
James Morseeeb25552019-01-29 18:48:42 +0000723 u64 buf_paddr;
Huang Yingd334a492010-05-18 14:35:20 +0800724 int rc;
725
James Morsef2a7e052019-01-29 18:48:53 +0000726 rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
Huang Yingd334a492010-05-18 14:35:20 +0800727 if (rc)
728 goto out;
Jonathan (Zhixiong) Zhang2fb58532017-06-21 12:17:10 -0600729
James Morsef2a7e052019-01-29 18:48:53 +0000730 if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
731 __ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
Jonathan (Zhixiong) Zhang2fb58532017-06-21 12:17:10 -0600732
James Morsef2a7e052019-01-29 18:48:53 +0000733 if (!ghes_estatus_cached(estatus)) {
734 if (ghes_print_estatus(NULL, ghes->generic, estatus))
735 ghes_estatus_cache_add(ghes->generic, estatus);
Huang Ying152cef42011-07-13 13:14:26 +0800736 }
James Morsef2a7e052019-01-29 18:48:53 +0000737 ghes_do_proc(ghes, estatus);
Tyler Baicar42aa5602017-06-21 12:17:04 -0600738
Tyler Baicaraaf2c2f2017-08-28 10:53:41 -0600739out:
James Morsef2a7e052019-01-29 18:48:53 +0000740 ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
Tyler Baicaraaf2c2f2017-08-28 10:53:41 -0600741
Punit Agrawal806487a2016-10-18 17:07:19 +0100742 return rc;
Huang Yingd334a492010-05-18 14:35:20 +0800743}
744
Huang Ying81e88fd2011-01-12 14:44:55 +0800745static void ghes_add_timer(struct ghes *ghes)
746{
747 struct acpi_hest_generic *g = ghes->generic;
748 unsigned long expire;
749
750 if (!g->notify.poll_interval) {
Kefeng Wang933ca4e2019-10-18 11:18:25 +0800751 pr_warn(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
752 g->header.source_id);
Huang Ying81e88fd2011-01-12 14:44:55 +0800753 return;
754 }
755 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
756 ghes->timer.expires = round_jiffies_relative(expire);
757 add_timer(&ghes->timer);
758}
759
Kees Cookd5272002017-10-12 16:19:27 -0700760static void ghes_poll_func(struct timer_list *t)
Huang Ying81e88fd2011-01-12 14:44:55 +0800761{
Kees Cookd5272002017-10-12 16:19:27 -0700762 struct ghes *ghes = from_timer(ghes, t, timer);
James Morse3b880cb2019-01-29 18:48:51 +0000763 unsigned long flags;
Huang Ying81e88fd2011-01-12 14:44:55 +0800764
James Morse3b880cb2019-01-29 18:48:51 +0000765 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
Huang Ying81e88fd2011-01-12 14:44:55 +0800766 ghes_proc(ghes);
James Morse3b880cb2019-01-29 18:48:51 +0000767 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
Huang Ying81e88fd2011-01-12 14:44:55 +0800768 if (!(ghes->flags & GHES_EXITING))
769 ghes_add_timer(ghes);
770}
771
772static irqreturn_t ghes_irq_func(int irq, void *data)
773{
774 struct ghes *ghes = data;
James Morse3b880cb2019-01-29 18:48:51 +0000775 unsigned long flags;
Huang Ying81e88fd2011-01-12 14:44:55 +0800776 int rc;
777
James Morse3b880cb2019-01-29 18:48:51 +0000778 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
Huang Ying81e88fd2011-01-12 14:44:55 +0800779 rc = ghes_proc(ghes);
James Morse3b880cb2019-01-29 18:48:51 +0000780 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
Huang Ying81e88fd2011-01-12 14:44:55 +0800781 if (rc)
782 return IRQ_NONE;
783
784 return IRQ_HANDLED;
785}
786
Shiju Jose7bf130e2017-05-19 11:39:11 +0200787static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
788 void *data)
Huang Yingd334a492010-05-18 14:35:20 +0800789{
790 struct ghes *ghes;
James Morse3b880cb2019-01-29 18:48:51 +0000791 unsigned long flags;
Huang Yingd334a492010-05-18 14:35:20 +0800792 int ret = NOTIFY_DONE;
793
James Morse3b880cb2019-01-29 18:48:51 +0000794 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
Huang Yingd334a492010-05-18 14:35:20 +0800795 rcu_read_lock();
Shiju Jose7bf130e2017-05-19 11:39:11 +0200796 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
Huang Yingd334a492010-05-18 14:35:20 +0800797 if (!ghes_proc(ghes))
798 ret = NOTIFY_OK;
799 }
800 rcu_read_unlock();
James Morse3b880cb2019-01-29 18:48:51 +0000801 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
Huang Yingd334a492010-05-18 14:35:20 +0800802
803 return ret;
804}
805
Shiju Jose7bf130e2017-05-19 11:39:11 +0200806static struct notifier_block ghes_notifier_hed = {
807 .notifier_call = ghes_notify_hed,
Tomasz Nowicki44a69f62014-07-22 11:20:12 +0200808};
809
Tomasz Nowicki44a69f62014-07-22 11:20:12 +0200810/*
James Morse9c9d0802019-01-29 18:48:47 +0000811 * Handlers for CPER records may not be NMI safe. For example,
812 * memory_failure_queue() takes spinlocks and calls schedule_work_on().
813 * In any NMI-like handler, memory from ghes_estatus_pool is used to save
814 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
815 * ghes_proc_in_irq() to run in IRQ context where each estatus in
816 * ghes_estatus_llist is processed.
817 *
818 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
819 * to suppress frequent messages.
Tomasz Nowicki44a69f62014-07-22 11:20:12 +0200820 */
821static struct llist_head ghes_estatus_llist;
822static struct irq_work ghes_proc_irq_work;
823
Huang Ying46d12f02011-12-08 11:25:45 +0800824static void ghes_proc_in_irq(struct irq_work *irq_work)
825{
826 struct llist_node *llnode, *next;
827 struct ghes_estatus_node *estatus_node;
828 struct acpi_hest_generic *generic;
Lv Zheng0a00fd52014-06-03 16:32:53 +0800829 struct acpi_hest_generic_status *estatus;
Huang Ying46d12f02011-12-08 11:25:45 +0800830 u32 len, node_len;
831
832 llnode = llist_del_all(&ghes_estatus_llist);
833 /*
834 * Because the time order of estatus in list is reversed,
835 * revert it back to proper order.
836 */
Chen, Gong8d21d4c2014-07-28 02:50:59 -0400837 llnode = llist_reverse_order(llnode);
Huang Ying67eb2e92011-07-13 13:14:25 +0800838 while (llnode) {
839 next = llnode->next;
840 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
841 llnode);
842 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
Chen, Gong88f074f2013-10-18 14:28:59 -0700843 len = cper_estatus_len(estatus);
Huang Ying67eb2e92011-07-13 13:14:25 +0800844 node_len = GHES_ESTATUS_NODE_LEN(len);
Mauro Carvalho Chehab21480542013-02-15 06:10:39 -0300845 ghes_do_proc(estatus_node->ghes, estatus);
Huang Ying152cef42011-07-13 13:14:26 +0800846 if (!ghes_estatus_cached(estatus)) {
847 generic = estatus_node->generic;
848 if (ghes_print_estatus(NULL, generic, estatus))
849 ghes_estatus_cache_add(generic, estatus);
850 }
Huang Ying67eb2e92011-07-13 13:14:25 +0800851 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
852 node_len);
853 llnode = next;
854 }
855}
856
Huang Ying46d12f02011-12-08 11:25:45 +0800857static void ghes_print_queued_estatus(void)
858{
859 struct llist_node *llnode;
860 struct ghes_estatus_node *estatus_node;
861 struct acpi_hest_generic *generic;
Lv Zheng0a00fd52014-06-03 16:32:53 +0800862 struct acpi_hest_generic_status *estatus;
Huang Ying46d12f02011-12-08 11:25:45 +0800863
864 llnode = llist_del_all(&ghes_estatus_llist);
865 /*
866 * Because the time order of estatus in list is reversed,
867 * revert it back to proper order.
868 */
Chen, Gong8d21d4c2014-07-28 02:50:59 -0400869 llnode = llist_reverse_order(llnode);
Huang Ying46d12f02011-12-08 11:25:45 +0800870 while (llnode) {
871 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
872 llnode);
873 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
Huang Ying46d12f02011-12-08 11:25:45 +0800874 generic = estatus_node->generic;
875 ghes_print_estatus(NULL, generic, estatus);
876 llnode = llnode->next;
877 }
878}
879
James Morsed9f608d2019-01-29 18:48:56 +0000880static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
881 enum fixed_addresses fixmap_idx)
Borislav Petkov11568492015-03-18 09:41:35 +0100882{
James Morsed9f608d2019-01-29 18:48:56 +0000883 struct acpi_hest_generic_status *estatus, tmp_header;
Borislav Petkov11568492015-03-18 09:41:35 +0100884 struct ghes_estatus_node *estatus_node;
James Morsed9f608d2019-01-29 18:48:56 +0000885 u32 len, node_len;
886 u64 buf_paddr;
887 int sev, rc;
Borislav Petkov11568492015-03-18 09:41:35 +0100888
James Morsef2a7e052019-01-29 18:48:53 +0000889 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
James Morsed9f608d2019-01-29 18:48:56 +0000890 return -EOPNOTSUPP;
Borislav Petkov11568492015-03-18 09:41:35 +0100891
James Morsed9f608d2019-01-29 18:48:56 +0000892 rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
893 if (rc) {
894 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
895 return rc;
896 }
James Morsef2a7e052019-01-29 18:48:53 +0000897
James Morsed9f608d2019-01-29 18:48:56 +0000898 rc = __ghes_check_estatus(ghes, &tmp_header);
899 if (rc) {
900 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
901 return rc;
902 }
903
904 len = cper_estatus_len(&tmp_header);
Borislav Petkov11568492015-03-18 09:41:35 +0100905 node_len = GHES_ESTATUS_NODE_LEN(len);
Borislav Petkov11568492015-03-18 09:41:35 +0100906 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
907 if (!estatus_node)
James Morsed9f608d2019-01-29 18:48:56 +0000908 return -ENOMEM;
Borislav Petkov11568492015-03-18 09:41:35 +0100909
910 estatus_node->ghes = ghes;
911 estatus_node->generic = ghes->generic;
912 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
Borislav Petkov11568492015-03-18 09:41:35 +0100913
James Morsed9f608d2019-01-29 18:48:56 +0000914 if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
James Morsef2a7e052019-01-29 18:48:53 +0000915 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
James Morsed9f608d2019-01-29 18:48:56 +0000916 rc = -ENOENT;
917 goto no_work;
James Morseee2eb3d2019-01-29 18:48:45 +0000918 }
919
James Morsef2a7e052019-01-29 18:48:53 +0000920 sev = ghes_severity(estatus->error_severity);
James Morseee2eb3d2019-01-29 18:48:45 +0000921 if (sev >= GHES_SEV_PANIC) {
922 ghes_print_queued_estatus();
James Morsef2a7e052019-01-29 18:48:53 +0000923 __ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
James Morseee2eb3d2019-01-29 18:48:45 +0000924 }
925
James Morsed9f608d2019-01-29 18:48:56 +0000926 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
James Morseee2eb3d2019-01-29 18:48:45 +0000927
James Morsed9f608d2019-01-29 18:48:56 +0000928 /* This error has been reported before, don't process it again. */
929 if (ghes_estatus_cached(estatus))
930 goto no_work;
931
932 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
933
934 return rc;
935
936no_work:
937 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
938 node_len);
939
940 return rc;
James Morseee2eb3d2019-01-29 18:48:45 +0000941}
942
James Morseb4840792019-01-29 18:48:52 +0000943static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
944 enum fixed_addresses fixmap_idx)
James Morseee2eb3d2019-01-29 18:48:45 +0000945{
946 int ret = -ENOENT;
Borislav Petkov6169ddf2015-03-18 09:55:21 +0100947 struct ghes *ghes;
James Morseee2eb3d2019-01-29 18:48:45 +0000948
949 rcu_read_lock();
950 list_for_each_entry_rcu(ghes, rcu_list, list) {
James Morseb4840792019-01-29 18:48:52 +0000951 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
James Morseee2eb3d2019-01-29 18:48:45 +0000952 ret = 0;
953 }
954 rcu_read_unlock();
955
956 if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
957 irq_work_queue(&ghes_proc_irq_work);
958
959 return ret;
960}
James Morse9c9d0802019-01-29 18:48:47 +0000961
962#ifdef CONFIG_ACPI_APEI_SEA
963static LIST_HEAD(ghes_sea);
964
965/*
966 * Return 0 only if one of the SEA error sources successfully reported an error
967 * record sent from the firmware.
968 */
969int ghes_notify_sea(void)
970{
James Morse3b880cb2019-01-29 18:48:51 +0000971 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
972 int rv;
973
974 raw_spin_lock(&ghes_notify_lock_sea);
James Morseb972d2e2019-01-29 18:48:57 +0000975 rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
James Morse3b880cb2019-01-29 18:48:51 +0000976 raw_spin_unlock(&ghes_notify_lock_sea);
977
978 return rv;
James Morse9c9d0802019-01-29 18:48:47 +0000979}
980
981static void ghes_sea_add(struct ghes *ghes)
982{
983 mutex_lock(&ghes_list_mutex);
984 list_add_rcu(&ghes->list, &ghes_sea);
985 mutex_unlock(&ghes_list_mutex);
986}
987
988static void ghes_sea_remove(struct ghes *ghes)
989{
990 mutex_lock(&ghes_list_mutex);
991 list_del_rcu(&ghes->list);
992 mutex_unlock(&ghes_list_mutex);
993 synchronize_rcu();
994}
995#else /* CONFIG_ACPI_APEI_SEA */
996static inline void ghes_sea_add(struct ghes *ghes) { }
997static inline void ghes_sea_remove(struct ghes *ghes) { }
998#endif /* CONFIG_ACPI_APEI_SEA */
999
1000#ifdef CONFIG_HAVE_ACPI_APEI_NMI
1001/*
1002 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
1003 * having only one concurrent reader.
1004 */
1005static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
1006
1007static LIST_HEAD(ghes_nmi);
James Morseee2eb3d2019-01-29 18:48:45 +00001008
1009static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
1010{
James Morse3b880cb2019-01-29 18:48:51 +00001011 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
James Morseee2eb3d2019-01-29 18:48:45 +00001012 int ret = NMI_DONE;
Huang Ying81e88fd2011-01-12 14:44:55 +08001013
Jiri Kosina6fe9e7c2015-03-27 10:05:00 +01001014 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1015 return ret;
1016
James Morse3b880cb2019-01-29 18:48:51 +00001017 raw_spin_lock(&ghes_notify_lock_nmi);
James Morseb4840792019-01-29 18:48:52 +00001018 if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
James Morseee2eb3d2019-01-29 18:48:45 +00001019 ret = NMI_HANDLED;
James Morse3b880cb2019-01-29 18:48:51 +00001020 raw_spin_unlock(&ghes_notify_lock_nmi);
Borislav Petkov6169ddf2015-03-18 09:55:21 +01001021
Jiri Kosina6fe9e7c2015-03-27 10:05:00 +01001022 atomic_dec(&ghes_in_nmi);
Huang Ying81e88fd2011-01-12 14:44:55 +08001023 return ret;
1024}
1025
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001026static void ghes_nmi_add(struct ghes *ghes)
1027{
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001028 mutex_lock(&ghes_list_mutex);
1029 if (list_empty(&ghes_nmi))
1030 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1031 list_add_rcu(&ghes->list, &ghes_nmi);
1032 mutex_unlock(&ghes_list_mutex);
1033}
1034
1035static void ghes_nmi_remove(struct ghes *ghes)
1036{
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001037 mutex_lock(&ghes_list_mutex);
1038 list_del_rcu(&ghes->list);
1039 if (list_empty(&ghes_nmi))
1040 unregister_nmi_handler(NMI_LOCAL, "ghes");
1041 mutex_unlock(&ghes_list_mutex);
1042 /*
1043 * To synchronize with NMI handler, ghes can only be
1044 * freed after NMI handler finishes.
1045 */
1046 synchronize_rcu();
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001047}
James Morse255097c2019-01-29 18:48:48 +00001048#else /* CONFIG_HAVE_ACPI_APEI_NMI */
1049static inline void ghes_nmi_add(struct ghes *ghes) { }
1050static inline void ghes_nmi_remove(struct ghes *ghes) { }
1051#endif /* CONFIG_HAVE_ACPI_APEI_NMI */
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001052
1053static void ghes_nmi_init_cxt(void)
1054{
1055 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1056}
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001057
James Morsef9f05392019-01-29 18:49:02 +00001058static int __ghes_sdei_callback(struct ghes *ghes,
1059 enum fixed_addresses fixmap_idx)
1060{
1061 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1062 irq_work_queue(&ghes_proc_irq_work);
1063
1064 return 0;
1065 }
1066
1067 return -ENOENT;
1068}
1069
1070static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1071 void *arg)
1072{
1073 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1074 struct ghes *ghes = arg;
1075 int err;
1076
1077 raw_spin_lock(&ghes_notify_lock_sdei_normal);
1078 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1079 raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1080
1081 return err;
1082}
1083
1084static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1085 void *arg)
1086{
1087 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1088 struct ghes *ghes = arg;
1089 int err;
1090
1091 raw_spin_lock(&ghes_notify_lock_sdei_critical);
1092 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1093 raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1094
1095 return err;
1096}
1097
1098static int apei_sdei_register_ghes(struct ghes *ghes)
1099{
1100 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1101 return -EOPNOTSUPP;
1102
1103 return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1104 ghes_sdei_critical_callback);
1105}
1106
1107static int apei_sdei_unregister_ghes(struct ghes *ghes)
1108{
1109 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1110 return -EOPNOTSUPP;
1111
1112 return sdei_unregister_ghes(ghes);
1113}
1114
Bill Pembertonda095fd32012-11-19 13:22:46 -05001115static int ghes_probe(struct platform_device *ghes_dev)
Huang Yingd334a492010-05-18 14:35:20 +08001116{
1117 struct acpi_hest_generic *generic;
1118 struct ghes *ghes = NULL;
James Morse3b880cb2019-01-29 18:48:51 +00001119 unsigned long flags;
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001120
Huang Ying7ad6e942010-08-02 15:48:24 +08001121 int rc = -EINVAL;
Huang Yingd334a492010-05-18 14:35:20 +08001122
Jin Dongming1dd6b202010-09-29 19:53:53 +08001123 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
Huang Yingd334a492010-05-18 14:35:20 +08001124 if (!generic->enabled)
Huang Ying7ad6e942010-08-02 15:48:24 +08001125 return -ENODEV;
Huang Yingd334a492010-05-18 14:35:20 +08001126
Huang Ying81e88fd2011-01-12 14:44:55 +08001127 switch (generic->notify.type) {
1128 case ACPI_HEST_NOTIFY_POLLED:
1129 case ACPI_HEST_NOTIFY_EXTERNAL:
1130 case ACPI_HEST_NOTIFY_SCI:
Shiju Jose7bf130e2017-05-19 11:39:11 +02001131 case ACPI_HEST_NOTIFY_GSIV:
1132 case ACPI_HEST_NOTIFY_GPIO:
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001133 break;
Shiju Jose7bf130e2017-05-19 11:39:11 +02001134
Tyler Baicar7edda082017-06-21 12:17:09 -06001135 case ACPI_HEST_NOTIFY_SEA:
1136 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1137 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1138 generic->header.source_id);
1139 rc = -ENOTSUPP;
1140 goto err;
1141 }
1142 break;
Huang Ying81e88fd2011-01-12 14:44:55 +08001143 case ACPI_HEST_NOTIFY_NMI:
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001144 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1145 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1146 generic->header.source_id);
1147 goto err;
1148 }
Huang Ying81e88fd2011-01-12 14:44:55 +08001149 break;
James Morsef9f05392019-01-29 18:49:02 +00001150 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1151 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1152 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1153 generic->header.source_id);
1154 goto err;
1155 }
1156 break;
Huang Ying81e88fd2011-01-12 14:44:55 +08001157 case ACPI_HEST_NOTIFY_LOCAL:
Kefeng Wang933ca4e2019-10-18 11:18:25 +08001158 pr_warn(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1159 generic->header.source_id);
Huang Yingd334a492010-05-18 14:35:20 +08001160 goto err;
Huang Ying81e88fd2011-01-12 14:44:55 +08001161 default:
Kefeng Wang933ca4e2019-10-18 11:18:25 +08001162 pr_warn(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1163 generic->notify.type, generic->header.source_id);
Huang Ying81e88fd2011-01-12 14:44:55 +08001164 goto err;
Huang Yingd334a492010-05-18 14:35:20 +08001165 }
Huang Ying81e88fd2011-01-12 14:44:55 +08001166
1167 rc = -EIO;
1168 if (generic->error_block_length <
Lv Zheng0a00fd52014-06-03 16:32:53 +08001169 sizeof(struct acpi_hest_generic_status)) {
Kefeng Wang933ca4e2019-10-18 11:18:25 +08001170 pr_warn(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1171 generic->error_block_length, generic->header.source_id);
Huang Yingd334a492010-05-18 14:35:20 +08001172 goto err;
1173 }
1174 ghes = ghes_new(generic);
1175 if (IS_ERR(ghes)) {
1176 rc = PTR_ERR(ghes);
1177 ghes = NULL;
1178 goto err;
1179 }
Mauro Carvalho Chehab21480542013-02-15 06:10:39 -03001180
Huang Ying81e88fd2011-01-12 14:44:55 +08001181 switch (generic->notify.type) {
1182 case ACPI_HEST_NOTIFY_POLLED:
Bhaskar Upadhayacea79e72020-01-08 09:17:38 -08001183 timer_setup(&ghes->timer, ghes_poll_func, 0);
Huang Ying81e88fd2011-01-12 14:44:55 +08001184 ghes_add_timer(ghes);
1185 break;
1186 case ACPI_HEST_NOTIFY_EXTERNAL:
1187 /* External interrupt vector is GSI */
Wei Yongjuna98d4f62013-06-03 02:08:39 +00001188 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1189 if (rc) {
Huang Ying81e88fd2011-01-12 14:44:55 +08001190 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1191 generic->header.source_id);
Borislav Petkovcc7f3f12018-04-23 14:16:46 +02001192 goto err;
Huang Ying81e88fd2011-01-12 14:44:55 +08001193 }
Loc Hobdb94582017-07-21 11:24:37 -07001194 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1195 "GHES IRQ", ghes);
Wei Yongjuna98d4f62013-06-03 02:08:39 +00001196 if (rc) {
Huang Ying81e88fd2011-01-12 14:44:55 +08001197 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1198 generic->header.source_id);
Borislav Petkovcc7f3f12018-04-23 14:16:46 +02001199 goto err;
Huang Ying81e88fd2011-01-12 14:44:55 +08001200 }
1201 break;
Shiju Jose7bf130e2017-05-19 11:39:11 +02001202
Huang Ying81e88fd2011-01-12 14:44:55 +08001203 case ACPI_HEST_NOTIFY_SCI:
Shiju Jose7bf130e2017-05-19 11:39:11 +02001204 case ACPI_HEST_NOTIFY_GSIV:
1205 case ACPI_HEST_NOTIFY_GPIO:
Huang Ying7ad6e942010-08-02 15:48:24 +08001206 mutex_lock(&ghes_list_mutex);
Shiju Jose7bf130e2017-05-19 11:39:11 +02001207 if (list_empty(&ghes_hed))
1208 register_acpi_hed_notifier(&ghes_notifier_hed);
1209 list_add_rcu(&ghes->list, &ghes_hed);
Huang Ying7ad6e942010-08-02 15:48:24 +08001210 mutex_unlock(&ghes_list_mutex);
Huang Ying81e88fd2011-01-12 14:44:55 +08001211 break;
Shiju Jose7bf130e2017-05-19 11:39:11 +02001212
Tyler Baicar7edda082017-06-21 12:17:09 -06001213 case ACPI_HEST_NOTIFY_SEA:
1214 ghes_sea_add(ghes);
1215 break;
Huang Ying81e88fd2011-01-12 14:44:55 +08001216 case ACPI_HEST_NOTIFY_NMI:
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001217 ghes_nmi_add(ghes);
Huang Ying81e88fd2011-01-12 14:44:55 +08001218 break;
James Morsef9f05392019-01-29 18:49:02 +00001219 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1220 rc = apei_sdei_register_ghes(ghes);
1221 if (rc)
1222 goto err;
1223 break;
Huang Ying81e88fd2011-01-12 14:44:55 +08001224 default:
1225 BUG();
Huang Yingd334a492010-05-18 14:35:20 +08001226 }
Borislav Petkovcc7f3f12018-04-23 14:16:46 +02001227
Huang Ying7ad6e942010-08-02 15:48:24 +08001228 platform_set_drvdata(ghes_dev, ghes);
Huang Yingd334a492010-05-18 14:35:20 +08001229
Borislav Petkovcc7f3f12018-04-23 14:16:46 +02001230 ghes_edac_register(ghes, &ghes_dev->dev);
1231
Tyler Baicar77b246b2017-06-21 12:17:15 -06001232 /* Handle any pending errors right away */
James Morse3b880cb2019-01-29 18:48:51 +00001233 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
Tyler Baicar77b246b2017-06-21 12:17:15 -06001234 ghes_proc(ghes);
James Morse3b880cb2019-01-29 18:48:51 +00001235 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
Tyler Baicar77b246b2017-06-21 12:17:15 -06001236
Huang Yingd334a492010-05-18 14:35:20 +08001237 return 0;
Borislav Petkovcc7f3f12018-04-23 14:16:46 +02001238
Huang Yingd334a492010-05-18 14:35:20 +08001239err:
Huang Ying7ad6e942010-08-02 15:48:24 +08001240 if (ghes) {
Huang Yingd334a492010-05-18 14:35:20 +08001241 ghes_fini(ghes);
1242 kfree(ghes);
1243 }
Huang Ying7ad6e942010-08-02 15:48:24 +08001244 return rc;
Huang Yingd334a492010-05-18 14:35:20 +08001245}
1246
Bill Pembertonb59bc2f2012-11-21 23:13:09 +01001247static int ghes_remove(struct platform_device *ghes_dev)
Huang Ying7ad6e942010-08-02 15:48:24 +08001248{
James Morsef9f05392019-01-29 18:49:02 +00001249 int rc;
Huang Ying7ad6e942010-08-02 15:48:24 +08001250 struct ghes *ghes;
1251 struct acpi_hest_generic *generic;
1252
1253 ghes = platform_get_drvdata(ghes_dev);
1254 generic = ghes->generic;
1255
Huang Ying81e88fd2011-01-12 14:44:55 +08001256 ghes->flags |= GHES_EXITING;
Huang Ying7ad6e942010-08-02 15:48:24 +08001257 switch (generic->notify.type) {
Huang Ying81e88fd2011-01-12 14:44:55 +08001258 case ACPI_HEST_NOTIFY_POLLED:
1259 del_timer_sync(&ghes->timer);
1260 break;
1261 case ACPI_HEST_NOTIFY_EXTERNAL:
1262 free_irq(ghes->irq, ghes);
1263 break;
Shiju Jose7bf130e2017-05-19 11:39:11 +02001264
Huang Ying7ad6e942010-08-02 15:48:24 +08001265 case ACPI_HEST_NOTIFY_SCI:
Shiju Jose7bf130e2017-05-19 11:39:11 +02001266 case ACPI_HEST_NOTIFY_GSIV:
1267 case ACPI_HEST_NOTIFY_GPIO:
Huang Ying7ad6e942010-08-02 15:48:24 +08001268 mutex_lock(&ghes_list_mutex);
1269 list_del_rcu(&ghes->list);
Shiju Jose7bf130e2017-05-19 11:39:11 +02001270 if (list_empty(&ghes_hed))
1271 unregister_acpi_hed_notifier(&ghes_notifier_hed);
Huang Ying7ad6e942010-08-02 15:48:24 +08001272 mutex_unlock(&ghes_list_mutex);
James Morse7d64f822017-03-16 14:30:39 +00001273 synchronize_rcu();
Huang Ying7ad6e942010-08-02 15:48:24 +08001274 break;
Shiju Jose7bf130e2017-05-19 11:39:11 +02001275
Tyler Baicar7edda082017-06-21 12:17:09 -06001276 case ACPI_HEST_NOTIFY_SEA:
1277 ghes_sea_remove(ghes);
1278 break;
Huang Ying81e88fd2011-01-12 14:44:55 +08001279 case ACPI_HEST_NOTIFY_NMI:
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001280 ghes_nmi_remove(ghes);
Huang Ying81e88fd2011-01-12 14:44:55 +08001281 break;
James Morsef9f05392019-01-29 18:49:02 +00001282 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1283 rc = apei_sdei_unregister_ghes(ghes);
1284 if (rc)
1285 return rc;
1286 break;
Huang Ying7ad6e942010-08-02 15:48:24 +08001287 default:
1288 BUG();
1289 break;
1290 }
1291
Huang Ying7ad6e942010-08-02 15:48:24 +08001292 ghes_fini(ghes);
Mauro Carvalho Chehab21480542013-02-15 06:10:39 -03001293
1294 ghes_edac_unregister(ghes);
1295
Huang Ying7ad6e942010-08-02 15:48:24 +08001296 kfree(ghes);
1297
1298 platform_set_drvdata(ghes_dev, NULL);
1299
1300 return 0;
1301}
1302
1303static struct platform_driver ghes_platform_driver = {
1304 .driver = {
1305 .name = "GHES",
Huang Ying7ad6e942010-08-02 15:48:24 +08001306 },
1307 .probe = ghes_probe,
1308 .remove = ghes_remove,
1309};
1310
Huang Yingd334a492010-05-18 14:35:20 +08001311static int __init ghes_init(void)
1312{
Huang Ying81e88fd2011-01-12 14:44:55 +08001313 int rc;
1314
Huang Yingd334a492010-05-18 14:35:20 +08001315 if (acpi_disabled)
1316 return -ENODEV;
1317
Punit Agrawale931d0d2017-08-29 14:20:20 +01001318 switch (hest_disable) {
1319 case HEST_NOT_FOUND:
1320 return -ENODEV;
1321 case HEST_DISABLED:
Huang Yingd334a492010-05-18 14:35:20 +08001322 pr_info(GHES_PFX "HEST is not enabled!\n");
1323 return -EINVAL;
Punit Agrawale931d0d2017-08-29 14:20:20 +01001324 default:
1325 break;
Huang Yingd334a492010-05-18 14:35:20 +08001326 }
1327
Huang Yingb6a95012011-07-13 13:14:19 +08001328 if (ghes_disable) {
1329 pr_info(GHES_PFX "GHES is not enabled!\n");
1330 return -EINVAL;
1331 }
1332
Tomasz Nowicki44a69f62014-07-22 11:20:12 +02001333 ghes_nmi_init_cxt();
Huang Ying67eb2e92011-07-13 13:14:25 +08001334
Huang Ying67eb2e92011-07-13 13:14:25 +08001335 rc = platform_driver_register(&ghes_platform_driver);
1336 if (rc)
James Morsee1471332019-01-29 18:48:40 +00001337 goto err;
Huang Ying67eb2e92011-07-13 13:14:25 +08001338
Huang Ying9fb0bfe2011-07-13 13:14:21 +08001339 rc = apei_osc_setup();
1340 if (rc == 0 && osc_sb_apei_support_acked)
1341 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1342 else if (rc == 0 && !osc_sb_apei_support_acked)
1343 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1344 else if (rc && osc_sb_apei_support_acked)
1345 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1346 else
1347 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1348
Huang Ying81e88fd2011-01-12 14:44:55 +08001349 return 0;
Huang Ying81e88fd2011-01-12 14:44:55 +08001350err:
1351 return rc;
Huang Yingd334a492010-05-18 14:35:20 +08001352}
Paul Gortmaker020bf062016-02-15 00:27:50 -05001353device_initcall(ghes_init);