[PATCH] MCA/INIT: use per cpu stacks
[pandora-kernel.git] / arch / ia64 / kernel / mca_drv.c
1 /*
2  * File:        mca_drv.c
3  * Purpose:     Generic MCA handling layer
4  *
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>
26
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>
34
35 #include <asm/irq.h>
36 #include <asm/hw_irq.h>
37
38 #include "mca_drv.h"
39
40 /* max size of SAL error record (default) */
41 static int sal_rec_max = 10000;
42
43 /* from mca_drv_asm.S */
44 extern void *mca_handler_bhhook(void);
45
46 static DEFINE_SPINLOCK(mca_bh_lock);
47
48 typedef enum {
49         MCA_IS_LOCAL  = 0,
50         MCA_IS_GLOBAL = 1
51 } mca_type_t;
52
53 #define MAX_PAGE_ISOLATE 1024
54
55 static struct page *page_isolate[MAX_PAGE_ISOLATE];
56 static int num_page_isolate = 0;
57
58 typedef enum {
59         ISOLATE_NG = 0,
60         ISOLATE_OK = 1
61 } isolate_status_t;
62
63 /*
64  *  This pool keeps pointers to the section part of SAL error record
65  */
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;
71
72 /**
73  * mca_page_isolate - isolate a poisoned page in order not to use it later
74  * @paddr:      poisoned memory location
75  *
76  * Return value:
77  *      ISOLATE_OK / ISOLATE_NG
78  */
79
80 static isolate_status_t
81 mca_page_isolate(unsigned long paddr)
82 {
83         int i;
84         struct page *p;
85
86         /* whether physical address is valid or not */
87         if ( !ia64_phys_addr_valid(paddr) ) 
88                 return ISOLATE_NG;
89
90         /* convert physical address to physical page number */
91         p = pfn_to_page(paddr>>PAGE_SHIFT);
92
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 */
97
98         /* limitation check */
99         if( num_page_isolate == MAX_PAGE_ISOLATE ) 
100                 return ISOLATE_NG;
101
102         /* kick pages having attribute 'SLAB' or 'Reserved' */
103         if( PageSlab(p) || PageReserved(p) ) 
104                 return ISOLATE_NG;
105
106         /* add attribute 'Reserved' and register the page */
107         SetPageReserved(p);
108         page_isolate[num_page_isolate++] = p;
109
110         return ISOLATE_OK;
111 }
112
113 /**
114  * mca_hanlder_bh - Kill the process which occurred memory read error
115  * @paddr:      poisoned address received from MCA Handler
116  */
117
118 void
119 mca_handler_bh(unsigned long paddr)
120 {
121         printk(KERN_DEBUG "OS_MCA: process [pid: %d](%s) encounters MCA.\n",
122                 current->pid, current->comm);
123
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);
129         }
130         spin_unlock(&mca_bh_lock);
131
132         /* This process is about to be killed itself */
133         do_exit(SIGKILL);
134 }
135
136 /**
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
140  */
141
142 static void 
143 mca_make_peidx(sal_log_processor_info_t *slpi, peidx_table_t *peidx)
144 {
145         /* 
146          * calculate the start address of
147          *   "struct cpuid_info" and "sal_processor_static_info_t".
148          */
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);
157
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);
164 }
165
166 /**
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
170  *
171  * Return value:
172  *      1 if record has platform error / 0 if not
173  */
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; }
179
180 static int 
181 mca_make_slidx(void *buffer, slidx_table_t *slidx)
182 {
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;
188
189         /*
190          * Initialize index referring current record
191          */
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));
202
203         /*
204          * Extract a Record Header
205          */
206         slidx->header = buffer;
207
208         /*
209          * Extract each section records
210          * (arranged from "int ia64_log_platform_info_print()")
211          */
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, SAL_PLAT_MEM_DEV_ERR_SECT_GUID)) {
218                         platform_err = 1;
219                         LOG_INDEX_ADD_SECT_PTR(slidx->mem_dev_err, sp);
220                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_SEL_DEV_ERR_SECT_GUID)) {
221                         platform_err = 1;
222                         LOG_INDEX_ADD_SECT_PTR(slidx->sel_dev_err, sp);
223                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_PCI_BUS_ERR_SECT_GUID)) {
224                         platform_err = 1;
225                         LOG_INDEX_ADD_SECT_PTR(slidx->pci_bus_err, sp);
226                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_SMBIOS_DEV_ERR_SECT_GUID)) {
227                         platform_err = 1;
228                         LOG_INDEX_ADD_SECT_PTR(slidx->smbios_dev_err, sp);
229                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_PCI_COMP_ERR_SECT_GUID)) {
230                         platform_err = 1;
231                         LOG_INDEX_ADD_SECT_PTR(slidx->pci_comp_err, sp);
232                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_SPECIFIC_ERR_SECT_GUID)) {
233                         platform_err = 1;
234                         LOG_INDEX_ADD_SECT_PTR(slidx->plat_specific_err, sp);
235                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_HOST_CTLR_ERR_SECT_GUID)) {
236                         platform_err = 1;
237                         LOG_INDEX_ADD_SECT_PTR(slidx->host_ctlr_err, sp);
238                 } else if (!efi_guidcmp(sp->guid, SAL_PLAT_BUS_ERR_SECT_GUID)) {
239                         platform_err = 1;
240                         LOG_INDEX_ADD_SECT_PTR(slidx->plat_bus_err, sp);
241                 } else {
242                         LOG_INDEX_ADD_SECT_PTR(slidx->unsupported, sp);
243                 }
244         }
245         slidx->n_sections = sects;
246
247         return platform_err;
248 }
249
250 /**
251  * init_record_index_pools - Initialize pool of lists for SAL record index
252  *
253  * Return value:
254  *      0 on Success / -ENOMEM on Failure
255  */
256 static int 
257 init_record_index_pools(void)
258 {
259         int i;
260         int rec_max_size;  /* Maximum size of SAL error records */
261         int sect_min_size; /* Minimum size of SAL error sections */
262         /* minimum size table of each section */
263         static int sal_log_sect_min_sizes[] = { 
264                 sizeof(sal_log_processor_info_t) + sizeof(sal_processor_static_info_t),
265                 sizeof(sal_log_mem_dev_err_info_t),
266                 sizeof(sal_log_sel_dev_err_info_t),
267                 sizeof(sal_log_pci_bus_err_info_t),
268                 sizeof(sal_log_smbios_dev_err_info_t),
269                 sizeof(sal_log_pci_comp_err_info_t),
270                 sizeof(sal_log_plat_specific_err_info_t),
271                 sizeof(sal_log_host_ctlr_err_info_t),
272                 sizeof(sal_log_plat_bus_err_info_t),
273         };
274
275         /*
276          * MCA handler cannot allocate new memory on flight,
277          * so we preallocate enough memory to handle a SAL record.
278          *
279          * Initialize a handling set of slidx_pool:
280          *   1. Pick up the max size of SAL error records
281          *   2. Pick up the min size of SAL error sections
282          *   3. Allocate the pool as enough to 2 SAL records
283          *     (now we can estimate the maxinum of section in a record.)
284          */
285
286         /* - 1 - */
287         rec_max_size = sal_rec_max;
288
289         /* - 2 - */
290         sect_min_size = sal_log_sect_min_sizes[0];
291         for (i = 1; i < sizeof sal_log_sect_min_sizes/sizeof(size_t); i++)
292                 if (sect_min_size > sal_log_sect_min_sizes[i])
293                         sect_min_size = sal_log_sect_min_sizes[i];
294
295         /* - 3 - */
296         slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
297         slidx_pool.buffer = (slidx_list_t *) kmalloc(slidx_pool.max_idx * sizeof(slidx_list_t), GFP_KERNEL);
298
299         return slidx_pool.buffer ? 0 : -ENOMEM;
300 }
301
302
303 /*****************************************************************************
304  * Recovery functions                                                        *
305  *****************************************************************************/
306
307 /**
308  * is_mca_global - Check whether this MCA is global or not
309  * @peidx:      pointer of index of processor error section
310  * @pbci:       pointer to pal_bus_check_info_t
311  *
312  * Return value:
313  *      MCA_IS_LOCAL / MCA_IS_GLOBAL
314  */
315
316 static mca_type_t
317 is_mca_global(peidx_table_t *peidx, pal_bus_check_info_t *pbci,
318               struct ia64_sal_os_state *sos)
319 {
320         pal_processor_state_info_t *psp = (pal_processor_state_info_t*)peidx_psp(peidx);
321
322         /* 
323          * PAL can request a rendezvous, if the MCA has a global scope.
324          * If "rz_always" flag is set, SAL requests MCA rendezvous 
325          * in spite of global MCA.
326          * Therefore it is local MCA when rendezvous has not been requested.
327          * Failed to rendezvous, the system must be down.
328          */
329         switch (sos->rv_rc) {
330                 case -1: /* SAL rendezvous unsuccessful */
331                         return MCA_IS_GLOBAL;
332                 case  0: /* SAL rendezvous not required */
333                         return MCA_IS_LOCAL;
334                 case  1: /* SAL rendezvous successful int */
335                 case  2: /* SAL rendezvous successful int with init */
336                 default:
337                         break;
338         }
339
340         /*
341          * If One or more Cache/TLB/Reg_File/Uarch_Check is here,
342          * it would be a local MCA. (i.e. processor internal error)
343          */
344         if (psp->tc || psp->cc || psp->rc || psp->uc)
345                 return MCA_IS_LOCAL;
346         
347         /*
348          * Bus_Check structure with Bus_Check.ib (internal bus error) flag set
349          * would be a global MCA. (e.g. a system bus address parity error)
350          */
351         if (!pbci || pbci->ib)
352                 return MCA_IS_GLOBAL;
353
354         /*
355          * Bus_Check structure with Bus_Check.eb (external bus error) flag set
356          * could be either a local MCA or a global MCA.
357          *
358          * Referring Bus_Check.bsi:
359          *   0: Unknown/unclassified
360          *   1: BERR#
361          *   2: BINIT#
362          *   3: Hard Fail
363          * (FIXME: Are these SGI specific or generic bsi values?)
364          */
365         if (pbci->eb)
366                 switch (pbci->bsi) {
367                         case 0:
368                                 /* e.g. a load from poisoned memory */
369                                 return MCA_IS_LOCAL;
370                         case 1:
371                         case 2:
372                         case 3:
373                                 return MCA_IS_GLOBAL;
374                 }
375
376         return MCA_IS_GLOBAL;
377 }
378
379 /**
380  * recover_from_read_error - Try to recover the errors which type are "read"s.
381  * @slidx:      pointer of index of SAL error record
382  * @peidx:      pointer of index of processor error section
383  * @pbci:       pointer of pal_bus_check_info
384  *
385  * Return value:
386  *      1 on Success / 0 on Failure
387  */
388
389 static int
390 recover_from_read_error(slidx_table_t *slidx, peidx_table_t *peidx, pal_bus_check_info_t *pbci,
391                         struct ia64_sal_os_state *sos)
392 {
393         sal_log_mod_error_info_t *smei;
394         pal_min_state_area_t *pmsa;
395         struct ia64_psr *psr1, *psr2;
396         ia64_fptr_t *mca_hdlr_bh = (ia64_fptr_t*)mca_handler_bhhook;
397
398         /* Is target address valid? */
399         if (!pbci->tv)
400                 return 0;
401
402         /*
403          * cpu read or memory-mapped io read
404          *
405          *    offending process  affected process  OS MCA do
406          *     kernel mode        kernel mode       down system
407          *     kernel mode        user   mode       kill the process
408          *     user   mode        kernel mode       down system (*)
409          *     user   mode        user   mode       kill the process
410          *
411          * (*) You could terminate offending user-mode process
412          *    if (pbci->pv && pbci->pl != 0) *and* if you sure
413          *    the process not have any locks of kernel.
414          */
415
416         psr1 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_ipsr);
417
418         /*
419          *  Check the privilege level of interrupted context.
420          *   If it is user-mode, then terminate affected process.
421          */
422         if (psr1->cpl != 0) {
423                 smei = peidx_bus_check(peidx, 0);
424                 if (smei->valid.target_identifier) {
425                         /*
426                          *  setup for resume to bottom half of MCA,
427                          * "mca_handler_bhhook"
428                          */
429                         pmsa = sos->pal_min_state;
430                         /* pass to bhhook as 1st argument (gr8) */
431                         pmsa->pmsa_gr[8-1] = smei->target_identifier;
432                         /* set interrupted return address (but no use) */
433                         pmsa->pmsa_br0 = pmsa->pmsa_iip;
434                         /* change resume address to bottom half */
435                         pmsa->pmsa_iip = mca_hdlr_bh->fp;
436                         pmsa->pmsa_gr[1-1] = mca_hdlr_bh->gp;
437                         /* set cpl with kernel mode */
438                         psr2 = (struct ia64_psr *)&pmsa->pmsa_ipsr;
439                         psr2->cpl = 0;
440                         psr2->ri  = 0;
441                         psr2->i  = 0;
442
443                         return 1;
444                 }
445
446         }
447
448         return 0;
449 }
450
451 /**
452  * recover_from_platform_error - Recover from platform error.
453  * @slidx:      pointer of index of SAL error record
454  * @peidx:      pointer of index of processor error section
455  * @pbci:       pointer of pal_bus_check_info
456  *
457  * Return value:
458  *      1 on Success / 0 on Failure
459  */
460
461 static int
462 recover_from_platform_error(slidx_table_t *slidx, peidx_table_t *peidx, pal_bus_check_info_t *pbci,
463                             struct ia64_sal_os_state *sos)
464 {
465         int status = 0;
466         pal_processor_state_info_t *psp = (pal_processor_state_info_t*)peidx_psp(peidx);
467
468         if (psp->bc && pbci->eb && pbci->bsi == 0) {
469                 switch(pbci->type) {
470                 case 1: /* partial read */
471                 case 3: /* full line(cpu) read */
472                 case 9: /* I/O space read */
473                         status = recover_from_read_error(slidx, peidx, pbci, sos);
474                         break;
475                 case 0: /* unknown */
476                 case 2: /* partial write */
477                 case 4: /* full line write */
478                 case 5: /* implicit or explicit write-back operation */
479                 case 6: /* snoop probe */
480                 case 7: /* incoming or outgoing ptc.g */
481                 case 8: /* write coalescing transactions */
482                 case 10: /* I/O space write */
483                 case 11: /* inter-processor interrupt message(IPI) */
484                 case 12: /* interrupt acknowledge or external task priority cycle */
485                 default:
486                         break;
487                 }
488         }
489
490         return status;
491 }
492
493 /**
494  * recover_from_processor_error
495  * @platform:   whether there are some platform error section or not
496  * @slidx:      pointer of index of SAL error record
497  * @peidx:      pointer of index of processor error section
498  * @pbci:       pointer of pal_bus_check_info
499  *
500  * Return value:
501  *      1 on Success / 0 on Failure
502  */
503 /*
504  *  Later we try to recover when below all conditions are satisfied.
505  *   1. Only one processor error section is exist.
506  *   2. BUS_CHECK is exist and the others are not exist.(Except TLB_CHECK)
507  *   3. The entry of BUS_CHECK_INFO is 1.
508  *   4. "External bus error" flag is set and the others are not set.
509  */
510
511 static int
512 recover_from_processor_error(int platform, slidx_table_t *slidx, peidx_table_t *peidx, pal_bus_check_info_t *pbci,
513                              struct ia64_sal_os_state *sos)
514 {
515         pal_processor_state_info_t *psp = (pal_processor_state_info_t*)peidx_psp(peidx);
516
517         /* 
518          * We cannot recover errors with other than bus_check.
519          */
520         if (psp->cc || psp->rc || psp->uc) 
521                 return 0;
522
523         /*
524          * If there is no bus error, record is weird but we need not to recover.
525          */
526         if (psp->bc == 0 || pbci == NULL)
527                 return 1;
528
529         /*
530          * Sorry, we cannot handle so many.
531          */
532         if (peidx_bus_check_num(peidx) > 1)
533                 return 0;
534         /*
535          * Well, here is only one bus error.
536          */
537         if (pbci->ib || pbci->cc)
538                 return 0;
539         if (pbci->eb && pbci->bsi > 0)
540                 return 0;
541         if (psp->ci == 0)
542                 return 0;
543
544         /*
545          * This is a local MCA and estimated as recoverble external bus error.
546          * (e.g. a load from poisoned memory)
547          * This means "there are some platform errors".
548          */
549         if (platform) 
550                 return recover_from_platform_error(slidx, peidx, pbci, sos);
551         /* 
552          * On account of strange SAL error record, we cannot recover. 
553          */
554         return 0;
555 }
556
557 /**
558  * mca_try_to_recover - Try to recover from MCA
559  * @rec:        pointer to a SAL error record
560  *
561  * Return value:
562  *      1 on Success / 0 on Failure
563  */
564
565 static int
566 mca_try_to_recover(void *rec, 
567         struct ia64_sal_os_state *sos)
568 {
569         int platform_err;
570         int n_proc_err;
571         slidx_table_t slidx;
572         peidx_table_t peidx;
573         pal_bus_check_info_t pbci;
574
575         /* Make index of SAL error record */
576         platform_err = mca_make_slidx(rec, &slidx);
577
578         /* Count processor error sections */
579         n_proc_err = slidx_count(&slidx, proc_err);
580
581          /* Now, OS can recover when there is one processor error section */
582         if (n_proc_err > 1)
583                 return 0;
584         else if (n_proc_err == 0) {
585                 /* Weird SAL record ... We need not to recover */
586
587                 return 1;
588         }
589
590         /* Make index of processor error section */
591         mca_make_peidx((sal_log_processor_info_t*)slidx_first_entry(&slidx.proc_err)->hdr, &peidx);
592
593         /* Extract Processor BUS_CHECK[0] */
594         *((u64*)&pbci) = peidx_check_info(&peidx, bus_check, 0);
595
596         /* Check whether MCA is global or not */
597         if (is_mca_global(&peidx, &pbci, sos))
598                 return 0;
599         
600         /* Try to recover a processor error */
601         return recover_from_processor_error(platform_err, &slidx, &peidx, &pbci, sos);
602 }
603
604 /*
605  * =============================================================================
606  */
607
608 int __init mca_external_handler_init(void)
609 {
610         if (init_record_index_pools())
611                 return -ENOMEM;
612
613         /* register external mca handlers */
614         if (ia64_reg_MCA_extension(mca_try_to_recover)){        
615                 printk(KERN_ERR "ia64_reg_MCA_extension failed.\n");
616                 kfree(slidx_pool.buffer);
617                 return -EFAULT;
618         }
619         return 0;
620 }
621
622 void __exit mca_external_handler_exit(void)
623 {
624         /* unregister external mca handlers */
625         ia64_unreg_MCA_extension();
626         kfree(slidx_pool.buffer);
627 }
628
629 module_init(mca_external_handler_init);
630 module_exit(mca_external_handler_exit);
631
632 module_param(sal_rec_max, int, 0644);
633 MODULE_PARM_DESC(sal_rec_max, "Max size of SAL error record");
634
635 MODULE_DESCRIPTION("ia64 platform dependent mca handler driver");
636 MODULE_LICENSE("GPL");