[IA64] mca_drv cleanup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ia64 / kernel / mca_drv.c
blob80f83d6cdbfcac4a7bf1e2f85aaefe314dce1819
1 /*
2 * File: mca_drv.c
3 * Purpose: Generic MCA handling layer
5 * Copyright (C) 2004 FUJITSU LIMITED
6 * Copyright (C) Hidetoshi Seto (seto.hidetoshi@jp.fujitsu.com)
7 * Copyright (C) 2005 Silicon Graphics, Inc
8 * Copyright (C) 2005 Keith Owens <kaos@sgi.com>
9 */
10 #include <linux/config.h>
11 #include <linux/types.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kallsyms.h>
17 #include <linux/smp_lock.h>
18 #include <linux/bootmem.h>
19 #include <linux/acpi.h>
20 #include <linux/timer.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/smp.h>
24 #include <linux/workqueue.h>
25 #include <linux/mm.h>
27 #include <asm/delay.h>
28 #include <asm/machvec.h>
29 #include <asm/page.h>
30 #include <asm/ptrace.h>
31 #include <asm/system.h>
32 #include <asm/sal.h>
33 #include <asm/mca.h>
35 #include <asm/irq.h>
36 #include <asm/hw_irq.h>
38 #include "mca_drv.h"
40 /* max size of SAL error record (default) */
41 static int sal_rec_max = 10000;
43 /* from mca_drv_asm.S */
44 extern void *mca_handler_bhhook(void);
46 static DEFINE_SPINLOCK(mca_bh_lock);
48 typedef enum {
49 MCA_IS_LOCAL = 0,
50 MCA_IS_GLOBAL = 1
51 } mca_type_t;
53 #define MAX_PAGE_ISOLATE 1024
55 static struct page *page_isolate[MAX_PAGE_ISOLATE];
56 static int num_page_isolate = 0;
58 typedef enum {
59 ISOLATE_NG = 0,
60 ISOLATE_OK = 1
61 } isolate_status_t;
64 * This pool keeps pointers to the section part of SAL error record
66 static struct {
67 slidx_list_t *buffer; /* section pointer list pool */
68 int cur_idx; /* Current index of section pointer list pool */
69 int max_idx; /* Maximum index of section pointer list pool */
70 } slidx_pool;
72 /**
73 * mca_page_isolate - isolate a poisoned page in order not to use it later
74 * @paddr: poisoned memory location
76 * Return value:
77 * ISOLATE_OK / ISOLATE_NG
80 static isolate_status_t
81 mca_page_isolate(unsigned long paddr)
83 int i;
84 struct page *p;
86 /* whether physical address is valid or not */
87 if (!ia64_phys_addr_valid(paddr))
88 return ISOLATE_NG;
90 /* convert physical address to physical page number */
91 p = pfn_to_page(paddr>>PAGE_SHIFT);
93 /* check whether a page number have been already registered or not */
94 for (i = 0; i < num_page_isolate; i++)
95 if (page_isolate[i] == p)
96 return ISOLATE_OK; /* already listed */
98 /* limitation check */
99 if (num_page_isolate == MAX_PAGE_ISOLATE)
100 return ISOLATE_NG;
102 /* kick pages having attribute 'SLAB' or 'Reserved' */
103 if (PageSlab(p) || PageReserved(p))
104 return ISOLATE_NG;
106 /* add attribute 'Reserved' and register the page */
107 SetPageReserved(p);
108 page_isolate[num_page_isolate++] = p;
110 return ISOLATE_OK;
114 * mca_hanlder_bh - Kill the process which occurred memory read error
115 * @paddr: poisoned address received from MCA Handler
118 void
119 mca_handler_bh(unsigned long paddr)
121 printk(KERN_DEBUG "OS_MCA: process [pid: %d](%s) encounters MCA.\n",
122 current->pid, current->comm);
124 spin_lock(&mca_bh_lock);
125 if (mca_page_isolate(paddr) == ISOLATE_OK) {
126 printk(KERN_DEBUG "Page isolation: ( %lx ) success.\n", paddr);
127 } else {
128 printk(KERN_DEBUG "Page isolation: ( %lx ) failure.\n", paddr);
130 spin_unlock(&mca_bh_lock);
132 /* This process is about to be killed itself */
133 do_exit(SIGKILL);
137 * mca_make_peidx - Make index of processor error section
138 * @slpi: pointer to record of processor error section
139 * @peidx: pointer to index of processor error section
142 static void
143 mca_make_peidx(sal_log_processor_info_t *slpi, peidx_table_t *peidx)
146 * calculate the start address of
147 * "struct cpuid_info" and "sal_processor_static_info_t".
149 u64 total_check_num = slpi->valid.num_cache_check
150 + slpi->valid.num_tlb_check
151 + slpi->valid.num_bus_check
152 + slpi->valid.num_reg_file_check
153 + slpi->valid.num_ms_check;
154 u64 head_size = sizeof(sal_log_mod_error_info_t) * total_check_num
155 + sizeof(sal_log_processor_info_t);
156 u64 mid_size = slpi->valid.cpuid_info * sizeof(struct sal_cpuid_info);
158 peidx_head(peidx) = slpi;
159 peidx_mid(peidx) = (struct sal_cpuid_info *)
160 (slpi->valid.cpuid_info ? ((char*)slpi + head_size) : NULL);
161 peidx_bottom(peidx) = (sal_processor_static_info_t *)
162 (slpi->valid.psi_static_struct ?
163 ((char*)slpi + head_size + mid_size) : NULL);
167 * mca_make_slidx - Make index of SAL error record
168 * @buffer: pointer to SAL error record
169 * @slidx: pointer to index of SAL error record
171 * Return value:
172 * 1 if record has platform error / 0 if not
174 #define LOG_INDEX_ADD_SECT_PTR(sect, ptr) \
175 {slidx_list_t *hl = &slidx_pool.buffer[slidx_pool.cur_idx]; \
176 hl->hdr = ptr; \
177 list_add(&hl->list, &(sect)); \
178 slidx_pool.cur_idx = (slidx_pool.cur_idx + 1)%slidx_pool.max_idx; }
180 static int
181 mca_make_slidx(void *buffer, slidx_table_t *slidx)
183 int platform_err = 0;
184 int record_len = ((sal_log_record_header_t*)buffer)->len;
185 u32 ercd_pos;
186 int sects;
187 sal_log_section_hdr_t *sp;
190 * Initialize index referring current record
192 INIT_LIST_HEAD(&(slidx->proc_err));
193 INIT_LIST_HEAD(&(slidx->mem_dev_err));
194 INIT_LIST_HEAD(&(slidx->sel_dev_err));
195 INIT_LIST_HEAD(&(slidx->pci_bus_err));
196 INIT_LIST_HEAD(&(slidx->smbios_dev_err));
197 INIT_LIST_HEAD(&(slidx->pci_comp_err));
198 INIT_LIST_HEAD(&(slidx->plat_specific_err));
199 INIT_LIST_HEAD(&(slidx->host_ctlr_err));
200 INIT_LIST_HEAD(&(slidx->plat_bus_err));
201 INIT_LIST_HEAD(&(slidx->unsupported));
204 * Extract a Record Header
206 slidx->header = buffer;
209 * Extract each section records
210 * (arranged from "int ia64_log_platform_info_print()")
212 for (ercd_pos = sizeof(sal_log_record_header_t), sects = 0;
213 ercd_pos < record_len; ercd_pos += sp->len, sects++) {
214 sp = (sal_log_section_hdr_t *)((char*)buffer + ercd_pos);
215 if (!efi_guidcmp(sp->guid, SAL_PROC_DEV_ERR_SECT_GUID)) {
216 LOG_INDEX_ADD_SECT_PTR(slidx->proc_err, sp);
217 } else if (!efi_guidcmp(sp->guid,
218 SAL_PLAT_MEM_DEV_ERR_SECT_GUID)) {
219 platform_err = 1;
220 LOG_INDEX_ADD_SECT_PTR(slidx->mem_dev_err, sp);
221 } else if (!efi_guidcmp(sp->guid,
222 SAL_PLAT_SEL_DEV_ERR_SECT_GUID)) {
223 platform_err = 1;
224 LOG_INDEX_ADD_SECT_PTR(slidx->sel_dev_err, sp);
225 } else if (!efi_guidcmp(sp->guid,
226 SAL_PLAT_PCI_BUS_ERR_SECT_GUID)) {
227 platform_err = 1;
228 LOG_INDEX_ADD_SECT_PTR(slidx->pci_bus_err, sp);
229 } else if (!efi_guidcmp(sp->guid,
230 SAL_PLAT_SMBIOS_DEV_ERR_SECT_GUID)) {
231 platform_err = 1;
232 LOG_INDEX_ADD_SECT_PTR(slidx->smbios_dev_err, sp);
233 } else if (!efi_guidcmp(sp->guid,
234 SAL_PLAT_PCI_COMP_ERR_SECT_GUID)) {
235 platform_err = 1;
236 LOG_INDEX_ADD_SECT_PTR(slidx->pci_comp_err, sp);
237 } else if (!efi_guidcmp(sp->guid,
238 SAL_PLAT_SPECIFIC_ERR_SECT_GUID)) {
239 platform_err = 1;
240 LOG_INDEX_ADD_SECT_PTR(slidx->plat_specific_err, sp);
241 } else if (!efi_guidcmp(sp->guid,
242 SAL_PLAT_HOST_CTLR_ERR_SECT_GUID)) {
243 platform_err = 1;
244 LOG_INDEX_ADD_SECT_PTR(slidx->host_ctlr_err, sp);
245 } else if (!efi_guidcmp(sp->guid,
246 SAL_PLAT_BUS_ERR_SECT_GUID)) {
247 platform_err = 1;
248 LOG_INDEX_ADD_SECT_PTR(slidx->plat_bus_err, sp);
249 } else {
250 LOG_INDEX_ADD_SECT_PTR(slidx->unsupported, sp);
253 slidx->n_sections = sects;
255 return platform_err;
259 * init_record_index_pools - Initialize pool of lists for SAL record index
261 * Return value:
262 * 0 on Success / -ENOMEM on Failure
264 static int
265 init_record_index_pools(void)
267 int i;
268 int rec_max_size; /* Maximum size of SAL error records */
269 int sect_min_size; /* Minimum size of SAL error sections */
270 /* minimum size table of each section */
271 static int sal_log_sect_min_sizes[] = {
272 sizeof(sal_log_processor_info_t)
273 + sizeof(sal_processor_static_info_t),
274 sizeof(sal_log_mem_dev_err_info_t),
275 sizeof(sal_log_sel_dev_err_info_t),
276 sizeof(sal_log_pci_bus_err_info_t),
277 sizeof(sal_log_smbios_dev_err_info_t),
278 sizeof(sal_log_pci_comp_err_info_t),
279 sizeof(sal_log_plat_specific_err_info_t),
280 sizeof(sal_log_host_ctlr_err_info_t),
281 sizeof(sal_log_plat_bus_err_info_t),
285 * MCA handler cannot allocate new memory on flight,
286 * so we preallocate enough memory to handle a SAL record.
288 * Initialize a handling set of slidx_pool:
289 * 1. Pick up the max size of SAL error records
290 * 2. Pick up the min size of SAL error sections
291 * 3. Allocate the pool as enough to 2 SAL records
292 * (now we can estimate the maxinum of section in a record.)
295 /* - 1 - */
296 rec_max_size = sal_rec_max;
298 /* - 2 - */
299 sect_min_size = sal_log_sect_min_sizes[0];
300 for (i = 1; i < sizeof sal_log_sect_min_sizes/sizeof(size_t); i++)
301 if (sect_min_size > sal_log_sect_min_sizes[i])
302 sect_min_size = sal_log_sect_min_sizes[i];
304 /* - 3 - */
305 slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
306 slidx_pool.buffer = (slidx_list_t *)
307 kmalloc(slidx_pool.max_idx * sizeof(slidx_list_t), GFP_KERNEL);
309 return slidx_pool.buffer ? 0 : -ENOMEM;
313 /*****************************************************************************
314 * Recovery functions *
315 *****************************************************************************/
318 * is_mca_global - Check whether this MCA is global or not
319 * @peidx: pointer of index of processor error section
320 * @pbci: pointer to pal_bus_check_info_t
321 * @sos: pointer to hand off struct between SAL and OS
323 * Return value:
324 * MCA_IS_LOCAL / MCA_IS_GLOBAL
327 static mca_type_t
328 is_mca_global(peidx_table_t *peidx, pal_bus_check_info_t *pbci,
329 struct ia64_sal_os_state *sos)
331 pal_processor_state_info_t *psp =
332 (pal_processor_state_info_t*)peidx_psp(peidx);
335 * PAL can request a rendezvous, if the MCA has a global scope.
336 * If "rz_always" flag is set, SAL requests MCA rendezvous
337 * in spite of global MCA.
338 * Therefore it is local MCA when rendezvous has not been requested.
339 * Failed to rendezvous, the system must be down.
341 switch (sos->rv_rc) {
342 case -1: /* SAL rendezvous unsuccessful */
343 return MCA_IS_GLOBAL;
344 case 0: /* SAL rendezvous not required */
345 return MCA_IS_LOCAL;
346 case 1: /* SAL rendezvous successful int */
347 case 2: /* SAL rendezvous successful int with init */
348 default:
349 break;
353 * If One or more Cache/TLB/Reg_File/Uarch_Check is here,
354 * it would be a local MCA. (i.e. processor internal error)
356 if (psp->tc || psp->cc || psp->rc || psp->uc)
357 return MCA_IS_LOCAL;
360 * Bus_Check structure with Bus_Check.ib (internal bus error) flag set
361 * would be a global MCA. (e.g. a system bus address parity error)
363 if (!pbci || pbci->ib)
364 return MCA_IS_GLOBAL;
367 * Bus_Check structure with Bus_Check.eb (external bus error) flag set
368 * could be either a local MCA or a global MCA.
370 * Referring Bus_Check.bsi:
371 * 0: Unknown/unclassified
372 * 1: BERR#
373 * 2: BINIT#
374 * 3: Hard Fail
375 * (FIXME: Are these SGI specific or generic bsi values?)
377 if (pbci->eb)
378 switch (pbci->bsi) {
379 case 0:
380 /* e.g. a load from poisoned memory */
381 return MCA_IS_LOCAL;
382 case 1:
383 case 2:
384 case 3:
385 return MCA_IS_GLOBAL;
388 return MCA_IS_GLOBAL;
392 * recover_from_read_error - Try to recover the errors which type are "read"s.
393 * @slidx: pointer of index of SAL error record
394 * @peidx: pointer of index of processor error section
395 * @pbci: pointer of pal_bus_check_info
396 * @sos: pointer to hand off struct between SAL and OS
398 * Return value:
399 * 1 on Success / 0 on Failure
402 static int
403 recover_from_read_error(slidx_table_t *slidx,
404 peidx_table_t *peidx, pal_bus_check_info_t *pbci,
405 struct ia64_sal_os_state *sos)
407 sal_log_mod_error_info_t *smei;
408 pal_min_state_area_t *pmsa;
409 struct ia64_psr *psr1, *psr2;
410 ia64_fptr_t *mca_hdlr_bh = (ia64_fptr_t*)mca_handler_bhhook;
412 /* Is target address valid? */
413 if (!pbci->tv)
414 return 0;
417 * cpu read or memory-mapped io read
419 * offending process affected process OS MCA do
420 * kernel mode kernel mode down system
421 * kernel mode user mode kill the process
422 * user mode kernel mode down system (*)
423 * user mode user mode kill the process
425 * (*) You could terminate offending user-mode process
426 * if (pbci->pv && pbci->pl != 0) *and* if you sure
427 * the process not have any locks of kernel.
430 psr1 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_ipsr);
433 * Check the privilege level of interrupted context.
434 * If it is user-mode, then terminate affected process.
436 if (psr1->cpl != 0) {
437 smei = peidx_bus_check(peidx, 0);
438 if (smei->valid.target_identifier) {
440 * setup for resume to bottom half of MCA,
441 * "mca_handler_bhhook"
443 pmsa = sos->pal_min_state;
444 /* pass to bhhook as 1st argument (gr8) */
445 pmsa->pmsa_gr[8-1] = smei->target_identifier;
446 /* set interrupted return address (but no use) */
447 pmsa->pmsa_br0 = pmsa->pmsa_iip;
448 /* change resume address to bottom half */
449 pmsa->pmsa_iip = mca_hdlr_bh->fp;
450 pmsa->pmsa_gr[1-1] = mca_hdlr_bh->gp;
451 /* set cpl with kernel mode */
452 psr2 = (struct ia64_psr *)&pmsa->pmsa_ipsr;
453 psr2->cpl = 0;
454 psr2->ri = 0;
455 psr2->i = 0;
457 return 1;
462 return 0;
466 * recover_from_platform_error - Recover from platform error.
467 * @slidx: pointer of index of SAL error record
468 * @peidx: pointer of index of processor error section
469 * @pbci: pointer of pal_bus_check_info
470 * @sos: pointer to hand off struct between SAL and OS
472 * Return value:
473 * 1 on Success / 0 on Failure
476 static int
477 recover_from_platform_error(slidx_table_t *slidx, peidx_table_t *peidx,
478 pal_bus_check_info_t *pbci,
479 struct ia64_sal_os_state *sos)
481 int status = 0;
482 pal_processor_state_info_t *psp =
483 (pal_processor_state_info_t*)peidx_psp(peidx);
485 if (psp->bc && pbci->eb && pbci->bsi == 0) {
486 switch(pbci->type) {
487 case 1: /* partial read */
488 case 3: /* full line(cpu) read */
489 case 9: /* I/O space read */
490 status = recover_from_read_error(slidx, peidx, pbci,
491 sos);
492 break;
493 case 0: /* unknown */
494 case 2: /* partial write */
495 case 4: /* full line write */
496 case 5: /* implicit or explicit write-back operation */
497 case 6: /* snoop probe */
498 case 7: /* incoming or outgoing ptc.g */
499 case 8: /* write coalescing transactions */
500 case 10: /* I/O space write */
501 case 11: /* inter-processor interrupt message(IPI) */
502 case 12: /* interrupt acknowledge or
503 external task priority cycle */
504 default:
505 break;
509 return status;
513 * recover_from_processor_error
514 * @platform: whether there are some platform error section or not
515 * @slidx: pointer of index of SAL error record
516 * @peidx: pointer of index of processor error section
517 * @pbci: pointer of pal_bus_check_info
518 * @sos: pointer to hand off struct between SAL and OS
520 * Return value:
521 * 1 on Success / 0 on Failure
524 * Later we try to recover when below all conditions are satisfied.
525 * 1. Only one processor error section is exist.
526 * 2. BUS_CHECK is exist and the others are not exist.(Except TLB_CHECK)
527 * 3. The entry of BUS_CHECK_INFO is 1.
528 * 4. "External bus error" flag is set and the others are not set.
531 static int
532 recover_from_processor_error(int platform, slidx_table_t *slidx,
533 peidx_table_t *peidx, pal_bus_check_info_t *pbci,
534 struct ia64_sal_os_state *sos)
536 pal_processor_state_info_t *psp =
537 (pal_processor_state_info_t*)peidx_psp(peidx);
540 * We cannot recover errors with other than bus_check.
542 if (psp->cc || psp->rc || psp->uc)
543 return 0;
546 * If there is no bus error, record is weird but we need not to recover.
548 if (psp->bc == 0 || pbci == NULL)
549 return 1;
552 * Sorry, we cannot handle so many.
554 if (peidx_bus_check_num(peidx) > 1)
555 return 0;
557 * Well, here is only one bus error.
559 if (pbci->ib || pbci->cc)
560 return 0;
561 if (pbci->eb && pbci->bsi > 0)
562 return 0;
563 if (psp->ci == 0)
564 return 0;
567 * This is a local MCA and estimated as recoverble external bus error.
568 * (e.g. a load from poisoned memory)
569 * This means "there are some platform errors".
571 if (platform)
572 return recover_from_platform_error(slidx, peidx, pbci, sos);
574 * On account of strange SAL error record, we cannot recover.
576 return 0;
580 * mca_try_to_recover - Try to recover from MCA
581 * @rec: pointer to a SAL error record
582 * @sos: pointer to hand off struct between SAL and OS
584 * Return value:
585 * 1 on Success / 0 on Failure
588 static int
589 mca_try_to_recover(void *rec, struct ia64_sal_os_state *sos)
591 int platform_err;
592 int n_proc_err;
593 slidx_table_t slidx;
594 peidx_table_t peidx;
595 pal_bus_check_info_t pbci;
597 /* Make index of SAL error record */
598 platform_err = mca_make_slidx(rec, &slidx);
600 /* Count processor error sections */
601 n_proc_err = slidx_count(&slidx, proc_err);
603 /* Now, OS can recover when there is one processor error section */
604 if (n_proc_err > 1)
605 return 0;
606 else if (n_proc_err == 0) {
607 /* Weird SAL record ... We need not to recover */
609 return 1;
612 /* Make index of processor error section */
613 mca_make_peidx((sal_log_processor_info_t*)
614 slidx_first_entry(&slidx.proc_err)->hdr, &peidx);
616 /* Extract Processor BUS_CHECK[0] */
617 *((u64*)&pbci) = peidx_check_info(&peidx, bus_check, 0);
619 /* Check whether MCA is global or not */
620 if (is_mca_global(&peidx, &pbci, sos))
621 return 0;
623 /* Try to recover a processor error */
624 return recover_from_processor_error(platform_err, &slidx, &peidx,
625 &pbci, sos);
629 * =============================================================================
632 int __init mca_external_handler_init(void)
634 if (init_record_index_pools())
635 return -ENOMEM;
637 /* register external mca handlers */
638 if (ia64_reg_MCA_extension(mca_try_to_recover)) {
639 printk(KERN_ERR "ia64_reg_MCA_extension failed.\n");
640 kfree(slidx_pool.buffer);
641 return -EFAULT;
643 return 0;
646 void __exit mca_external_handler_exit(void)
648 /* unregister external mca handlers */
649 ia64_unreg_MCA_extension();
650 kfree(slidx_pool.buffer);
653 module_init(mca_external_handler_init);
654 module_exit(mca_external_handler_exit);
656 module_param(sal_rec_max, int, 0644);
657 MODULE_PARM_DESC(sal_rec_max, "Max size of SAL error record");
659 MODULE_DESCRIPTION("ia64 platform dependent mca handler driver");
660 MODULE_LICENSE("GPL");