blob: d1a40218e17e9896407277a9356eb5c8280a990c [file] [log] [blame]
Huang Yingd334a492010-05-18 14:35:20 +08001/*
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 *
Huang Ying67eb2e92011-07-13 13:14:25 +080015 * Copyright 2010,2011 Intel Corp.
Huang Yingd334a492010-05-18 14:35:20 +080016 * 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 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/acpi.h>
36#include <linux/io.h>
37#include <linux/interrupt.h>
Huang Ying81e88fd2011-01-12 14:44:55 +080038#include <linux/timer.h>
Huang Yingd334a492010-05-18 14:35:20 +080039#include <linux/cper.h>
40#include <linux/kdebug.h>
Huang Ying7ad6e942010-08-02 15:48:24 +080041#include <linux/platform_device.h>
42#include <linux/mutex.h>
Huang Ying32c361f2010-12-07 10:22:31 +080043#include <linux/ratelimit.h>
Huang Ying81e88fd2011-01-12 14:44:55 +080044#include <linux/vmalloc.h>
Huang Ying67eb2e92011-07-13 13:14:25 +080045#include <linux/irq_work.h>
46#include <linux/llist.h>
47#include <linux/genalloc.h>
Huang Yingd334a492010-05-18 14:35:20 +080048#include <acpi/apei.h>
49#include <acpi/atomicio.h>
50#include <acpi/hed.h>
51#include <asm/mce.h>
Huang Ying81e88fd2011-01-12 14:44:55 +080052#include <asm/tlbflush.h>
Huang Yingd334a492010-05-18 14:35:20 +080053
54#include "apei-internal.h"
55
56#define GHES_PFX "GHES: "
57
58#define GHES_ESTATUS_MAX_SIZE 65536
Huang Ying67eb2e92011-07-13 13:14:25 +080059#define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
60
61#define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
62
63#define GHES_ESTATUS_NODE_LEN(estatus_len) \
64 (sizeof(struct ghes_estatus_node) + (estatus_len))
65#define GHES_ESTATUS_FROM_NODE(estatus_node) \
66 ((struct acpi_hest_generic_status *) \
67 ((struct ghes_estatus_node *)(estatus_node) + 1))
Huang Yingd334a492010-05-18 14:35:20 +080068
69/*
Huang Ying81e88fd2011-01-12 14:44:55 +080070 * One struct ghes is created for each generic hardware error source.
Huang Yingd334a492010-05-18 14:35:20 +080071 * It provides the context for APEI hardware error timer/IRQ/SCI/NMI
Huang Ying81e88fd2011-01-12 14:44:55 +080072 * handler.
Huang Yingd334a492010-05-18 14:35:20 +080073 *
74 * estatus: memory buffer for error status block, allocated during
75 * HEST parsing.
76 */
77#define GHES_TO_CLEAR 0x0001
Huang Ying81e88fd2011-01-12 14:44:55 +080078#define GHES_EXITING 0x0002
Huang Yingd334a492010-05-18 14:35:20 +080079
80struct ghes {
81 struct acpi_hest_generic *generic;
82 struct acpi_hest_generic_status *estatus;
Huang Yingd334a492010-05-18 14:35:20 +080083 u64 buffer_paddr;
84 unsigned long flags;
Huang Ying81e88fd2011-01-12 14:44:55 +080085 union {
86 struct list_head list;
87 struct timer_list timer;
88 unsigned int irq;
89 };
Huang Yingd334a492010-05-18 14:35:20 +080090};
91
Huang Ying67eb2e92011-07-13 13:14:25 +080092struct ghes_estatus_node {
93 struct llist_node llnode;
94 struct acpi_hest_generic *generic;
95};
96
Huang Yingb6a95012011-07-13 13:14:19 +080097int ghes_disable;
98module_param_named(disable, ghes_disable, bool, 0);
99
Huang Ying81e88fd2011-01-12 14:44:55 +0800100static int ghes_panic_timeout __read_mostly = 30;
101
Huang Yingd334a492010-05-18 14:35:20 +0800102/*
Huang Ying81e88fd2011-01-12 14:44:55 +0800103 * All error sources notified with SCI shares one notifier function,
104 * so they need to be linked and checked one by one. This is applied
105 * to NMI too.
Huang Yingd334a492010-05-18 14:35:20 +0800106 *
Huang Ying81e88fd2011-01-12 14:44:55 +0800107 * RCU is used for these lists, so ghes_list_mutex is only used for
108 * list changing, not for traversing.
Huang Yingd334a492010-05-18 14:35:20 +0800109 */
110static LIST_HEAD(ghes_sci);
Huang Ying81e88fd2011-01-12 14:44:55 +0800111static LIST_HEAD(ghes_nmi);
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/*
115 * NMI may be triggered on any CPU, so ghes_nmi_lock is used for
116 * mutual exclusion.
117 */
118static DEFINE_RAW_SPINLOCK(ghes_nmi_lock);
119
120/*
121 * Because the memory area used to transfer hardware error information
122 * from BIOS to Linux can be determined only in NMI, IRQ or timer
123 * handler, but general ioremap can not be used in atomic context, so
124 * a special version of atomic ioremap is implemented for that.
125 */
126
127/*
128 * Two virtual pages are used, one for NMI context, the other for
129 * IRQ/PROCESS context
130 */
131#define GHES_IOREMAP_PAGES 2
132#define GHES_IOREMAP_NMI_PAGE(base) (base)
133#define GHES_IOREMAP_IRQ_PAGE(base) ((base) + PAGE_SIZE)
134
135/* virtual memory area for atomic ioremap */
136static struct vm_struct *ghes_ioremap_area;
137/*
138 * These 2 spinlock is used to prevent atomic ioremap virtual memory
139 * area from being mapped simultaneously.
140 */
141static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
142static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
143
Huang Ying67eb2e92011-07-13 13:14:25 +0800144/*
145 * printk is not safe in NMI context. So in NMI handler, we allocate
146 * required memory from lock-less memory allocator
147 * (ghes_estatus_pool), save estatus into it, put them into lock-less
148 * list (ghes_estatus_llist), then delay printk into IRQ context via
149 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
150 * required pool size by all NMI error source.
151 */
152static struct gen_pool *ghes_estatus_pool;
153static unsigned long ghes_estatus_pool_size_request;
154static struct llist_head ghes_estatus_llist;
155static struct irq_work ghes_proc_irq_work;
156
Huang Ying81e88fd2011-01-12 14:44:55 +0800157static int ghes_ioremap_init(void)
158{
159 ghes_ioremap_area = __get_vm_area(PAGE_SIZE * GHES_IOREMAP_PAGES,
160 VM_IOREMAP, VMALLOC_START, VMALLOC_END);
161 if (!ghes_ioremap_area) {
162 pr_err(GHES_PFX "Failed to allocate virtual memory area for atomic ioremap.\n");
163 return -ENOMEM;
164 }
165
166 return 0;
167}
168
169static void ghes_ioremap_exit(void)
170{
171 free_vm_area(ghes_ioremap_area);
172}
173
174static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
175{
176 unsigned long vaddr;
177
178 vaddr = (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area->addr);
179 ioremap_page_range(vaddr, vaddr + PAGE_SIZE,
180 pfn << PAGE_SHIFT, PAGE_KERNEL);
181
182 return (void __iomem *)vaddr;
183}
184
185static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
186{
187 unsigned long vaddr;
188
189 vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
190 ioremap_page_range(vaddr, vaddr + PAGE_SIZE,
191 pfn << PAGE_SHIFT, PAGE_KERNEL);
192
193 return (void __iomem *)vaddr;
194}
195
196static void ghes_iounmap_nmi(void __iomem *vaddr_ptr)
197{
198 unsigned long vaddr = (unsigned long __force)vaddr_ptr;
199 void *base = ghes_ioremap_area->addr;
200
201 BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_NMI_PAGE(base));
202 unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
203 __flush_tlb_one(vaddr);
204}
205
206static void ghes_iounmap_irq(void __iomem *vaddr_ptr)
207{
208 unsigned long vaddr = (unsigned long __force)vaddr_ptr;
209 void *base = ghes_ioremap_area->addr;
210
211 BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_IRQ_PAGE(base));
212 unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
213 __flush_tlb_one(vaddr);
214}
215
Huang Ying67eb2e92011-07-13 13:14:25 +0800216static int ghes_estatus_pool_init(void)
217{
218 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
219 if (!ghes_estatus_pool)
220 return -ENOMEM;
221 return 0;
222}
223
224static void ghes_estatus_pool_free_chunk_page(struct gen_pool *pool,
225 struct gen_pool_chunk *chunk,
226 void *data)
227{
228 free_page(chunk->start_addr);
229}
230
231static void ghes_estatus_pool_exit(void)
232{
233 gen_pool_for_each_chunk(ghes_estatus_pool,
234 ghes_estatus_pool_free_chunk_page, NULL);
235 gen_pool_destroy(ghes_estatus_pool);
236}
237
238static int ghes_estatus_pool_expand(unsigned long len)
239{
240 unsigned long i, pages, size, addr;
241 int ret;
242
243 ghes_estatus_pool_size_request += PAGE_ALIGN(len);
244 size = gen_pool_size(ghes_estatus_pool);
245 if (size >= ghes_estatus_pool_size_request)
246 return 0;
247 pages = (ghes_estatus_pool_size_request - size) / PAGE_SIZE;
248 for (i = 0; i < pages; i++) {
249 addr = __get_free_page(GFP_KERNEL);
250 if (!addr)
251 return -ENOMEM;
252 ret = gen_pool_add(ghes_estatus_pool, addr, PAGE_SIZE, -1);
253 if (ret)
254 return ret;
255 }
256
257 return 0;
258}
259
260static void ghes_estatus_pool_shrink(unsigned long len)
261{
262 ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
263}
264
Huang Yingd334a492010-05-18 14:35:20 +0800265static struct ghes *ghes_new(struct acpi_hest_generic *generic)
266{
267 struct ghes *ghes;
268 unsigned int error_block_length;
269 int rc;
270
271 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
272 if (!ghes)
273 return ERR_PTR(-ENOMEM);
274 ghes->generic = generic;
Huang Yingd334a492010-05-18 14:35:20 +0800275 rc = acpi_pre_map_gar(&generic->error_status_address);
276 if (rc)
277 goto err_free;
278 error_block_length = generic->error_block_length;
279 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
280 pr_warning(FW_WARN GHES_PFX
281 "Error status block length is too long: %u for "
282 "generic hardware error source: %d.\n",
283 error_block_length, generic->header.source_id);
284 error_block_length = GHES_ESTATUS_MAX_SIZE;
285 }
286 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
287 if (!ghes->estatus) {
288 rc = -ENOMEM;
289 goto err_unmap;
290 }
291
292 return ghes;
293
294err_unmap:
295 acpi_post_unmap_gar(&generic->error_status_address);
296err_free:
297 kfree(ghes);
298 return ERR_PTR(rc);
299}
300
301static void ghes_fini(struct ghes *ghes)
302{
303 kfree(ghes->estatus);
304 acpi_post_unmap_gar(&ghes->generic->error_status_address);
305}
306
307enum {
Huang Yingad4ecef2010-08-02 15:48:23 +0800308 GHES_SEV_NO = 0x0,
309 GHES_SEV_CORRECTED = 0x1,
310 GHES_SEV_RECOVERABLE = 0x2,
311 GHES_SEV_PANIC = 0x3,
Huang Yingd334a492010-05-18 14:35:20 +0800312};
313
314static inline int ghes_severity(int severity)
315{
316 switch (severity) {
Huang Yingad4ecef2010-08-02 15:48:23 +0800317 case CPER_SEV_INFORMATIONAL:
318 return GHES_SEV_NO;
319 case CPER_SEV_CORRECTED:
320 return GHES_SEV_CORRECTED;
321 case CPER_SEV_RECOVERABLE:
322 return GHES_SEV_RECOVERABLE;
323 case CPER_SEV_FATAL:
324 return GHES_SEV_PANIC;
Huang Yingd334a492010-05-18 14:35:20 +0800325 default:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300326 /* Unknown, go panic */
Huang Yingad4ecef2010-08-02 15:48:23 +0800327 return GHES_SEV_PANIC;
Huang Yingd334a492010-05-18 14:35:20 +0800328 }
329}
330
Huang Ying81e88fd2011-01-12 14:44:55 +0800331static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
332 int from_phys)
Huang Yingd334a492010-05-18 14:35:20 +0800333{
Huang Ying81e88fd2011-01-12 14:44:55 +0800334 void __iomem *vaddr;
335 unsigned long flags = 0;
336 int in_nmi = in_nmi();
337 u64 offset;
338 u32 trunk;
Huang Yingd334a492010-05-18 14:35:20 +0800339
Huang Ying81e88fd2011-01-12 14:44:55 +0800340 while (len > 0) {
341 offset = paddr - (paddr & PAGE_MASK);
342 if (in_nmi) {
343 raw_spin_lock(&ghes_ioremap_lock_nmi);
344 vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
345 } else {
346 spin_lock_irqsave(&ghes_ioremap_lock_irq, flags);
347 vaddr = ghes_ioremap_pfn_irq(paddr >> PAGE_SHIFT);
348 }
349 trunk = PAGE_SIZE - offset;
350 trunk = min(trunk, len);
351 if (from_phys)
352 memcpy_fromio(buffer, vaddr + offset, trunk);
353 else
354 memcpy_toio(vaddr + offset, buffer, trunk);
355 len -= trunk;
356 paddr += trunk;
357 buffer += trunk;
358 if (in_nmi) {
359 ghes_iounmap_nmi(vaddr);
360 raw_spin_unlock(&ghes_ioremap_lock_nmi);
361 } else {
362 ghes_iounmap_irq(vaddr);
363 spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
364 }
365 }
Huang Yingd334a492010-05-18 14:35:20 +0800366}
367
368static int ghes_read_estatus(struct ghes *ghes, int silent)
369{
370 struct acpi_hest_generic *g = ghes->generic;
371 u64 buf_paddr;
372 u32 len;
373 int rc;
374
375 rc = acpi_atomic_read(&buf_paddr, &g->error_status_address);
376 if (rc) {
377 if (!silent && printk_ratelimit())
378 pr_warning(FW_WARN GHES_PFX
379"Failed to read error status block address for hardware error source: %d.\n",
380 g->header.source_id);
381 return -EIO;
382 }
383 if (!buf_paddr)
384 return -ENOENT;
385
Huang Ying81e88fd2011-01-12 14:44:55 +0800386 ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
387 sizeof(*ghes->estatus), 1);
Huang Yingd334a492010-05-18 14:35:20 +0800388 if (!ghes->estatus->block_status)
389 return -ENOENT;
390
391 ghes->buffer_paddr = buf_paddr;
392 ghes->flags |= GHES_TO_CLEAR;
393
394 rc = -EIO;
395 len = apei_estatus_len(ghes->estatus);
396 if (len < sizeof(*ghes->estatus))
397 goto err_read_block;
398 if (len > ghes->generic->error_block_length)
399 goto err_read_block;
400 if (apei_estatus_check_header(ghes->estatus))
401 goto err_read_block;
Huang Ying81e88fd2011-01-12 14:44:55 +0800402 ghes_copy_tofrom_phys(ghes->estatus + 1,
403 buf_paddr + sizeof(*ghes->estatus),
404 len - sizeof(*ghes->estatus), 1);
Huang Yingd334a492010-05-18 14:35:20 +0800405 if (apei_estatus_check(ghes->estatus))
406 goto err_read_block;
407 rc = 0;
408
409err_read_block:
Huang Ying81e88fd2011-01-12 14:44:55 +0800410 if (rc && !silent && printk_ratelimit())
Huang Yingd334a492010-05-18 14:35:20 +0800411 pr_warning(FW_WARN GHES_PFX
412 "Failed to read error status block!\n");
413 return rc;
414}
415
416static void ghes_clear_estatus(struct ghes *ghes)
417{
418 ghes->estatus->block_status = 0;
419 if (!(ghes->flags & GHES_TO_CLEAR))
420 return;
421 ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
422 sizeof(ghes->estatus->block_status), 0);
423 ghes->flags &= ~GHES_TO_CLEAR;
424}
425
Huang Ying67eb2e92011-07-13 13:14:25 +0800426static void ghes_do_proc(const struct acpi_hest_generic_status *estatus)
Huang Yingd334a492010-05-18 14:35:20 +0800427{
Huang Yingad4ecef2010-08-02 15:48:23 +0800428 int sev, processed = 0;
Huang Yingd334a492010-05-18 14:35:20 +0800429 struct acpi_hest_generic_data *gdata;
430
Huang Ying67eb2e92011-07-13 13:14:25 +0800431 sev = ghes_severity(estatus->error_severity);
432 apei_estatus_for_each_section(estatus, gdata) {
Huang Yingd334a492010-05-18 14:35:20 +0800433#ifdef CONFIG_X86_MCE
434 if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
435 CPER_SEC_PLATFORM_MEM)) {
436 apei_mce_report_mem_error(
Huang Yingad4ecef2010-08-02 15:48:23 +0800437 sev == GHES_SEV_CORRECTED,
Huang Yingd334a492010-05-18 14:35:20 +0800438 (struct cper_sec_mem_err *)(gdata+1));
439 processed = 1;
440 }
441#endif
442 }
Huang Ying32c361f2010-12-07 10:22:31 +0800443}
Huang Yingd334a492010-05-18 14:35:20 +0800444
Huang Ying67eb2e92011-07-13 13:14:25 +0800445static void __ghes_print_estatus(const char *pfx,
446 const struct acpi_hest_generic *generic,
447 const struct acpi_hest_generic_status *estatus)
Huang Ying32c361f2010-12-07 10:22:31 +0800448{
Huang Ying32c361f2010-12-07 10:22:31 +0800449 if (pfx == NULL) {
Huang Ying67eb2e92011-07-13 13:14:25 +0800450 if (ghes_severity(estatus->error_severity) <=
Huang Ying32c361f2010-12-07 10:22:31 +0800451 GHES_SEV_CORRECTED)
452 pfx = KERN_WARNING HW_ERR;
453 else
454 pfx = KERN_ERR HW_ERR;
455 }
Huang Ying55883402011-07-13 13:14:15 +0800456 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
Huang Ying67eb2e92011-07-13 13:14:25 +0800457 pfx, generic->header.source_id);
458 apei_estatus_print(pfx, estatus);
Huang Ying55883402011-07-13 13:14:15 +0800459}
460
Huang Ying67eb2e92011-07-13 13:14:25 +0800461static void ghes_print_estatus(const char *pfx,
462 const struct acpi_hest_generic *generic,
463 const struct acpi_hest_generic_status *estatus)
Huang Ying55883402011-07-13 13:14:15 +0800464{
465 /* Not more than 2 messages every 5 seconds */
Huang Ying67eb2e92011-07-13 13:14:25 +0800466 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
467 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
468 struct ratelimit_state *ratelimit;
Huang Ying55883402011-07-13 13:14:15 +0800469
Huang Ying67eb2e92011-07-13 13:14:25 +0800470 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
471 ratelimit = &ratelimit_corrected;
472 else
473 ratelimit = &ratelimit_uncorrected;
474 if (__ratelimit(ratelimit))
475 __ghes_print_estatus(pfx, generic, estatus);
Huang Yingd334a492010-05-18 14:35:20 +0800476}
477
478static int ghes_proc(struct ghes *ghes)
479{
480 int rc;
481
482 rc = ghes_read_estatus(ghes, 0);
483 if (rc)
484 goto out;
Huang Ying67eb2e92011-07-13 13:14:25 +0800485 ghes_print_estatus(NULL, ghes->generic, ghes->estatus);
486 ghes_do_proc(ghes->estatus);
Huang Yingd334a492010-05-18 14:35:20 +0800487
488out:
489 ghes_clear_estatus(ghes);
490 return 0;
491}
492
Huang Ying81e88fd2011-01-12 14:44:55 +0800493static void ghes_add_timer(struct ghes *ghes)
494{
495 struct acpi_hest_generic *g = ghes->generic;
496 unsigned long expire;
497
498 if (!g->notify.poll_interval) {
499 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
500 g->header.source_id);
501 return;
502 }
503 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
504 ghes->timer.expires = round_jiffies_relative(expire);
505 add_timer(&ghes->timer);
506}
507
508static void ghes_poll_func(unsigned long data)
509{
510 struct ghes *ghes = (void *)data;
511
512 ghes_proc(ghes);
513 if (!(ghes->flags & GHES_EXITING))
514 ghes_add_timer(ghes);
515}
516
517static irqreturn_t ghes_irq_func(int irq, void *data)
518{
519 struct ghes *ghes = data;
520 int rc;
521
522 rc = ghes_proc(ghes);
523 if (rc)
524 return IRQ_NONE;
525
526 return IRQ_HANDLED;
527}
528
Huang Yingd334a492010-05-18 14:35:20 +0800529static int ghes_notify_sci(struct notifier_block *this,
530 unsigned long event, void *data)
531{
532 struct ghes *ghes;
533 int ret = NOTIFY_DONE;
534
535 rcu_read_lock();
536 list_for_each_entry_rcu(ghes, &ghes_sci, list) {
537 if (!ghes_proc(ghes))
538 ret = NOTIFY_OK;
539 }
540 rcu_read_unlock();
541
542 return ret;
543}
544
Huang Ying67eb2e92011-07-13 13:14:25 +0800545static void ghes_proc_in_irq(struct irq_work *irq_work)
546{
547 struct llist_node *llnode, *next, *tail = NULL;
548 struct ghes_estatus_node *estatus_node;
549 struct acpi_hest_generic_status *estatus;
550 u32 len, node_len;
551
552 /*
553 * Because the time order of estatus in list is reversed,
554 * revert it back to proper order.
555 */
556 llnode = llist_del_all(&ghes_estatus_llist);
557 while (llnode) {
558 next = llnode->next;
559 llnode->next = tail;
560 tail = llnode;
561 llnode = next;
562 }
563 llnode = tail;
564 while (llnode) {
565 next = llnode->next;
566 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
567 llnode);
568 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
569 len = apei_estatus_len(estatus);
570 node_len = GHES_ESTATUS_NODE_LEN(len);
571 ghes_do_proc(estatus);
572 ghes_print_estatus(NULL, estatus_node->generic, estatus);
573 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
574 node_len);
575 llnode = next;
576 }
577}
578
Huang Ying81e88fd2011-01-12 14:44:55 +0800579static int ghes_notify_nmi(struct notifier_block *this,
580 unsigned long cmd, void *data)
581{
582 struct ghes *ghes, *ghes_global = NULL;
583 int sev, sev_global = -1;
584 int ret = NOTIFY_DONE;
585
586 if (cmd != DIE_NMI)
587 return ret;
588
589 raw_spin_lock(&ghes_nmi_lock);
590 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
591 if (ghes_read_estatus(ghes, 1)) {
592 ghes_clear_estatus(ghes);
593 continue;
594 }
595 sev = ghes_severity(ghes->estatus->error_severity);
596 if (sev > sev_global) {
597 sev_global = sev;
598 ghes_global = ghes;
599 }
600 ret = NOTIFY_STOP;
601 }
602
603 if (ret == NOTIFY_DONE)
604 goto out;
605
606 if (sev_global >= GHES_SEV_PANIC) {
607 oops_begin();
Huang Ying67eb2e92011-07-13 13:14:25 +0800608 __ghes_print_estatus(KERN_EMERG HW_ERR, ghes_global->generic,
609 ghes_global->estatus);
Huang Ying81e88fd2011-01-12 14:44:55 +0800610 /* reboot to log the error! */
611 if (panic_timeout == 0)
612 panic_timeout = ghes_panic_timeout;
613 panic("Fatal hardware error!");
614 }
615
616 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
Huang Ying67eb2e92011-07-13 13:14:25 +0800617#ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
618 u32 len, node_len;
619 struct ghes_estatus_node *estatus_node;
620 struct acpi_hest_generic_status *estatus;
621#endif
Huang Ying81e88fd2011-01-12 14:44:55 +0800622 if (!(ghes->flags & GHES_TO_CLEAR))
623 continue;
Huang Ying67eb2e92011-07-13 13:14:25 +0800624#ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
625 /* Save estatus for further processing in IRQ context */
626 len = apei_estatus_len(ghes->estatus);
627 node_len = GHES_ESTATUS_NODE_LEN(len);
628 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool,
629 node_len);
630 if (estatus_node) {
631 estatus_node->generic = ghes->generic;
632 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
633 memcpy(estatus, ghes->estatus, len);
634 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
635 }
636#endif
Huang Ying81e88fd2011-01-12 14:44:55 +0800637 ghes_clear_estatus(ghes);
638 }
Huang Ying67eb2e92011-07-13 13:14:25 +0800639#ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
640 irq_work_queue(&ghes_proc_irq_work);
641#endif
Huang Ying81e88fd2011-01-12 14:44:55 +0800642
643out:
644 raw_spin_unlock(&ghes_nmi_lock);
645 return ret;
646}
647
Huang Yingd334a492010-05-18 14:35:20 +0800648static struct notifier_block ghes_notifier_sci = {
649 .notifier_call = ghes_notify_sci,
650};
651
Huang Ying81e88fd2011-01-12 14:44:55 +0800652static struct notifier_block ghes_notifier_nmi = {
653 .notifier_call = ghes_notify_nmi,
654};
655
Huang Ying67eb2e92011-07-13 13:14:25 +0800656static unsigned long ghes_esource_prealloc_size(
657 const struct acpi_hest_generic *generic)
658{
659 unsigned long block_length, prealloc_records, prealloc_size;
660
661 block_length = min_t(unsigned long, generic->error_block_length,
662 GHES_ESTATUS_MAX_SIZE);
663 prealloc_records = max_t(unsigned long,
664 generic->records_to_preallocate, 1);
665 prealloc_size = min_t(unsigned long, block_length * prealloc_records,
666 GHES_ESOURCE_PREALLOC_MAX_SIZE);
667
668 return prealloc_size;
669}
670
Huang Ying7ad6e942010-08-02 15:48:24 +0800671static int __devinit ghes_probe(struct platform_device *ghes_dev)
Huang Yingd334a492010-05-18 14:35:20 +0800672{
673 struct acpi_hest_generic *generic;
674 struct ghes *ghes = NULL;
Huang Ying67eb2e92011-07-13 13:14:25 +0800675 unsigned long len;
Huang Ying7ad6e942010-08-02 15:48:24 +0800676 int rc = -EINVAL;
Huang Yingd334a492010-05-18 14:35:20 +0800677
Jin Dongming1dd6b202010-09-29 19:53:53 +0800678 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
Huang Yingd334a492010-05-18 14:35:20 +0800679 if (!generic->enabled)
Huang Ying7ad6e942010-08-02 15:48:24 +0800680 return -ENODEV;
Huang Yingd334a492010-05-18 14:35:20 +0800681
Huang Ying81e88fd2011-01-12 14:44:55 +0800682 switch (generic->notify.type) {
683 case ACPI_HEST_NOTIFY_POLLED:
684 case ACPI_HEST_NOTIFY_EXTERNAL:
685 case ACPI_HEST_NOTIFY_SCI:
686 case ACPI_HEST_NOTIFY_NMI:
687 break;
688 case ACPI_HEST_NOTIFY_LOCAL:
689 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
Huang Yingd334a492010-05-18 14:35:20 +0800690 generic->header.source_id);
691 goto err;
Huang Ying81e88fd2011-01-12 14:44:55 +0800692 default:
693 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
694 generic->notify.type, generic->header.source_id);
695 goto err;
Huang Yingd334a492010-05-18 14:35:20 +0800696 }
Huang Ying81e88fd2011-01-12 14:44:55 +0800697
698 rc = -EIO;
699 if (generic->error_block_length <
700 sizeof(struct acpi_hest_generic_status)) {
701 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
702 generic->error_block_length,
Huang Yingd334a492010-05-18 14:35:20 +0800703 generic->header.source_id);
704 goto err;
705 }
706 ghes = ghes_new(generic);
707 if (IS_ERR(ghes)) {
708 rc = PTR_ERR(ghes);
709 ghes = NULL;
710 goto err;
711 }
Huang Ying81e88fd2011-01-12 14:44:55 +0800712 switch (generic->notify.type) {
713 case ACPI_HEST_NOTIFY_POLLED:
714 ghes->timer.function = ghes_poll_func;
715 ghes->timer.data = (unsigned long)ghes;
716 init_timer_deferrable(&ghes->timer);
717 ghes_add_timer(ghes);
718 break;
719 case ACPI_HEST_NOTIFY_EXTERNAL:
720 /* External interrupt vector is GSI */
721 if (acpi_gsi_to_irq(generic->notify.vector, &ghes->irq)) {
722 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
723 generic->header.source_id);
724 goto err;
725 }
726 if (request_irq(ghes->irq, ghes_irq_func,
727 0, "GHES IRQ", ghes)) {
728 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
729 generic->header.source_id);
730 goto err;
731 }
732 break;
733 case ACPI_HEST_NOTIFY_SCI:
Huang Ying7ad6e942010-08-02 15:48:24 +0800734 mutex_lock(&ghes_list_mutex);
Huang Yingd334a492010-05-18 14:35:20 +0800735 if (list_empty(&ghes_sci))
736 register_acpi_hed_notifier(&ghes_notifier_sci);
737 list_add_rcu(&ghes->list, &ghes_sci);
Huang Ying7ad6e942010-08-02 15:48:24 +0800738 mutex_unlock(&ghes_list_mutex);
Huang Ying81e88fd2011-01-12 14:44:55 +0800739 break;
740 case ACPI_HEST_NOTIFY_NMI:
Huang Ying67eb2e92011-07-13 13:14:25 +0800741 len = ghes_esource_prealloc_size(generic);
742 ghes_estatus_pool_expand(len);
Huang Ying81e88fd2011-01-12 14:44:55 +0800743 mutex_lock(&ghes_list_mutex);
744 if (list_empty(&ghes_nmi))
745 register_die_notifier(&ghes_notifier_nmi);
746 list_add_rcu(&ghes->list, &ghes_nmi);
747 mutex_unlock(&ghes_list_mutex);
748 break;
749 default:
750 BUG();
Huang Yingd334a492010-05-18 14:35:20 +0800751 }
Huang Ying7ad6e942010-08-02 15:48:24 +0800752 platform_set_drvdata(ghes_dev, ghes);
Huang Yingd334a492010-05-18 14:35:20 +0800753
754 return 0;
755err:
Huang Ying7ad6e942010-08-02 15:48:24 +0800756 if (ghes) {
Huang Yingd334a492010-05-18 14:35:20 +0800757 ghes_fini(ghes);
758 kfree(ghes);
759 }
Huang Ying7ad6e942010-08-02 15:48:24 +0800760 return rc;
Huang Yingd334a492010-05-18 14:35:20 +0800761}
762
Huang Ying7ad6e942010-08-02 15:48:24 +0800763static int __devexit ghes_remove(struct platform_device *ghes_dev)
764{
765 struct ghes *ghes;
766 struct acpi_hest_generic *generic;
Huang Ying67eb2e92011-07-13 13:14:25 +0800767 unsigned long len;
Huang Ying7ad6e942010-08-02 15:48:24 +0800768
769 ghes = platform_get_drvdata(ghes_dev);
770 generic = ghes->generic;
771
Huang Ying81e88fd2011-01-12 14:44:55 +0800772 ghes->flags |= GHES_EXITING;
Huang Ying7ad6e942010-08-02 15:48:24 +0800773 switch (generic->notify.type) {
Huang Ying81e88fd2011-01-12 14:44:55 +0800774 case ACPI_HEST_NOTIFY_POLLED:
775 del_timer_sync(&ghes->timer);
776 break;
777 case ACPI_HEST_NOTIFY_EXTERNAL:
778 free_irq(ghes->irq, ghes);
779 break;
Huang Ying7ad6e942010-08-02 15:48:24 +0800780 case ACPI_HEST_NOTIFY_SCI:
781 mutex_lock(&ghes_list_mutex);
782 list_del_rcu(&ghes->list);
783 if (list_empty(&ghes_sci))
784 unregister_acpi_hed_notifier(&ghes_notifier_sci);
785 mutex_unlock(&ghes_list_mutex);
786 break;
Huang Ying81e88fd2011-01-12 14:44:55 +0800787 case ACPI_HEST_NOTIFY_NMI:
788 mutex_lock(&ghes_list_mutex);
789 list_del_rcu(&ghes->list);
790 if (list_empty(&ghes_nmi))
791 unregister_die_notifier(&ghes_notifier_nmi);
792 mutex_unlock(&ghes_list_mutex);
793 /*
794 * To synchronize with NMI handler, ghes can only be
795 * freed after NMI handler finishes.
796 */
797 synchronize_rcu();
Huang Ying67eb2e92011-07-13 13:14:25 +0800798 len = ghes_esource_prealloc_size(generic);
799 ghes_estatus_pool_shrink(len);
Huang Ying81e88fd2011-01-12 14:44:55 +0800800 break;
Huang Ying7ad6e942010-08-02 15:48:24 +0800801 default:
802 BUG();
803 break;
804 }
805
Huang Ying7ad6e942010-08-02 15:48:24 +0800806 ghes_fini(ghes);
807 kfree(ghes);
808
809 platform_set_drvdata(ghes_dev, NULL);
810
811 return 0;
812}
813
814static struct platform_driver ghes_platform_driver = {
815 .driver = {
816 .name = "GHES",
817 .owner = THIS_MODULE,
818 },
819 .probe = ghes_probe,
820 .remove = ghes_remove,
821};
822
Huang Yingd334a492010-05-18 14:35:20 +0800823static int __init ghes_init(void)
824{
Huang Ying81e88fd2011-01-12 14:44:55 +0800825 int rc;
826
Huang Yingd334a492010-05-18 14:35:20 +0800827 if (acpi_disabled)
828 return -ENODEV;
829
830 if (hest_disable) {
831 pr_info(GHES_PFX "HEST is not enabled!\n");
832 return -EINVAL;
833 }
834
Huang Yingb6a95012011-07-13 13:14:19 +0800835 if (ghes_disable) {
836 pr_info(GHES_PFX "GHES is not enabled!\n");
837 return -EINVAL;
838 }
839
Huang Ying67eb2e92011-07-13 13:14:25 +0800840 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
841
Huang Ying81e88fd2011-01-12 14:44:55 +0800842 rc = ghes_ioremap_init();
843 if (rc)
844 goto err;
845
Huang Ying67eb2e92011-07-13 13:14:25 +0800846 rc = ghes_estatus_pool_init();
Huang Ying81e88fd2011-01-12 14:44:55 +0800847 if (rc)
848 goto err_ioremap_exit;
849
Huang Ying67eb2e92011-07-13 13:14:25 +0800850 rc = platform_driver_register(&ghes_platform_driver);
851 if (rc)
852 goto err_pool_exit;
853
Huang Ying9fb0bfe2011-07-13 13:14:21 +0800854 rc = apei_osc_setup();
855 if (rc == 0 && osc_sb_apei_support_acked)
856 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
857 else if (rc == 0 && !osc_sb_apei_support_acked)
858 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
859 else if (rc && osc_sb_apei_support_acked)
860 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
861 else
862 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
863
Huang Ying81e88fd2011-01-12 14:44:55 +0800864 return 0;
Huang Ying67eb2e92011-07-13 13:14:25 +0800865err_pool_exit:
866 ghes_estatus_pool_exit();
Huang Ying81e88fd2011-01-12 14:44:55 +0800867err_ioremap_exit:
868 ghes_ioremap_exit();
869err:
870 return rc;
Huang Yingd334a492010-05-18 14:35:20 +0800871}
872
873static void __exit ghes_exit(void)
874{
Huang Ying7ad6e942010-08-02 15:48:24 +0800875 platform_driver_unregister(&ghes_platform_driver);
Huang Ying67eb2e92011-07-13 13:14:25 +0800876 ghes_estatus_pool_exit();
Huang Ying81e88fd2011-01-12 14:44:55 +0800877 ghes_ioremap_exit();
Huang Yingd334a492010-05-18 14:35:20 +0800878}
879
880module_init(ghes_init);
881module_exit(ghes_exit);
882
883MODULE_AUTHOR("Huang Ying");
884MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
885MODULE_LICENSE("GPL");
Huang Ying7ad6e942010-08-02 15:48:24 +0800886MODULE_ALIAS("platform:GHES");