1 // SPDX-License-Identifier: GPL-2.0-only
4 * Purpose: Generic MCA handling layer
6 * Copyright (C) 2004 FUJITSU LIMITED
7 * Copyright (C) 2004 Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
8 * Copyright (C) 2005 Silicon Graphics, Inc
9 * Copyright (C) 2005 Keith Owens <kaos@sgi.com>
10 * Copyright (C) 2006 Russ Anderson <rja@sgi.com>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kallsyms.h>
18 #include <linux/memblock.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>
26 #include <linux/slab.h>
28 #include <asm/delay.h>
30 #include <asm/ptrace.h>
35 #include <asm/hw_irq.h>
39 /* max size of SAL error record (default) */
40 static int sal_rec_max = 10000;
42 /* from mca_drv_asm.S */
43 extern void *mca_handler_bhhook(void);
45 static DEFINE_SPINLOCK(mca_bh_lock);
52 #define MAX_PAGE_ISOLATE 1024
54 static struct page *page_isolate[MAX_PAGE_ISOLATE];
55 static int num_page_isolate = 0;
64 MCA_NOT_RECOVERED = 0,
69 * This pool keeps pointers to the section part of SAL error record
72 slidx_list_t *buffer; /* section pointer list pool */
73 int cur_idx; /* Current index of section pointer list pool */
74 int max_idx; /* Maximum index of section pointer list pool */
78 fatal_mca(const char *fmt, ...)
84 vsnprintf(buf, sizeof(buf), fmt, args);
86 ia64_mca_printk(KERN_ALERT "MCA: %s\n", buf);
88 return MCA_NOT_RECOVERED;
92 mca_recovered(const char *fmt, ...)
98 vsnprintf(buf, sizeof(buf), fmt, args);
100 ia64_mca_printk(KERN_INFO "MCA: %s\n", buf);
102 return MCA_RECOVERED;
106 * mca_page_isolate - isolate a poisoned page in order not to use it later
107 * @paddr: poisoned memory location
110 * one of isolate_status_t, ISOLATE_OK/NG/NONE.
113 static isolate_status_t
114 mca_page_isolate(unsigned long paddr)
119 /* whether physical address is valid or not */
120 if (!ia64_phys_addr_valid(paddr))
123 if (!pfn_valid(paddr >> PAGE_SHIFT))
126 /* convert physical address to physical page number */
127 p = pfn_to_page(paddr>>PAGE_SHIFT);
129 /* check whether a page number have been already registered or not */
130 for (i = 0; i < num_page_isolate; i++)
131 if (page_isolate[i] == p)
132 return ISOLATE_OK; /* already listed */
134 /* limitation check */
135 if (num_page_isolate == MAX_PAGE_ISOLATE)
138 /* kick pages having attribute 'SLAB' or 'Reserved' */
139 if (PageSlab(p) || PageReserved(p))
142 /* add attribute 'Reserved' and register the page */
145 page_isolate[num_page_isolate++] = p;
151 * mca_hanlder_bh - Kill the process which occurred memory read error
152 * @paddr: poisoned address received from MCA Handler
156 mca_handler_bh(unsigned long paddr, void *iip, unsigned long ipsr)
159 printk(KERN_ERR "OS_MCA: process [cpu %d, pid: %d, uid: %d, "
160 "iip: %p, psr: 0x%lx,paddr: 0x%lx](%s) encounters MCA.\n",
161 raw_smp_processor_id(), current->pid,
162 from_kuid(&init_user_ns, current_uid()),
163 iip, ipsr, paddr, current->comm);
165 spin_lock(&mca_bh_lock);
166 switch (mca_page_isolate(paddr)) {
168 printk(KERN_DEBUG "Page isolation: ( %lx ) success.\n", paddr);
171 printk(KERN_CRIT "Page isolation: ( %lx ) failure.\n", paddr);
176 spin_unlock(&mca_bh_lock);
178 /* This process is about to be killed itself */
179 make_task_dead(SIGKILL);
183 * mca_make_peidx - Make index of processor error section
184 * @slpi: pointer to record of processor error section
185 * @peidx: pointer to index of processor error section
189 mca_make_peidx(sal_log_processor_info_t *slpi, peidx_table_t *peidx)
192 * calculate the start address of
193 * "struct cpuid_info" and "sal_processor_static_info_t".
195 u64 total_check_num = slpi->valid.num_cache_check
196 + slpi->valid.num_tlb_check
197 + slpi->valid.num_bus_check
198 + slpi->valid.num_reg_file_check
199 + slpi->valid.num_ms_check;
200 u64 head_size = sizeof(sal_log_mod_error_info_t) * total_check_num
201 + sizeof(sal_log_processor_info_t);
202 u64 mid_size = slpi->valid.cpuid_info * sizeof(struct sal_cpuid_info);
204 peidx_head(peidx) = slpi;
205 peidx_mid(peidx) = (struct sal_cpuid_info *)
206 (slpi->valid.cpuid_info ? ((char*)slpi + head_size) : NULL);
207 peidx_bottom(peidx) = (sal_processor_static_info_t *)
208 (slpi->valid.psi_static_struct ?
209 ((char*)slpi + head_size + mid_size) : NULL);
213 * mca_make_slidx - Make index of SAL error record
214 * @buffer: pointer to SAL error record
215 * @slidx: pointer to index of SAL error record
218 * 1 if record has platform error / 0 if not
220 #define LOG_INDEX_ADD_SECT_PTR(sect, ptr) \
221 {slidx_list_t *hl = &slidx_pool.buffer[slidx_pool.cur_idx]; \
223 list_add(&hl->list, &(sect)); \
224 slidx_pool.cur_idx = (slidx_pool.cur_idx + 1)%slidx_pool.max_idx; }
227 mca_make_slidx(void *buffer, slidx_table_t *slidx)
229 int platform_err = 0;
230 int record_len = ((sal_log_record_header_t*)buffer)->len;
233 sal_log_section_hdr_t *sp;
236 * Initialize index referring current record
238 INIT_LIST_HEAD(&(slidx->proc_err));
239 INIT_LIST_HEAD(&(slidx->mem_dev_err));
240 INIT_LIST_HEAD(&(slidx->sel_dev_err));
241 INIT_LIST_HEAD(&(slidx->pci_bus_err));
242 INIT_LIST_HEAD(&(slidx->smbios_dev_err));
243 INIT_LIST_HEAD(&(slidx->pci_comp_err));
244 INIT_LIST_HEAD(&(slidx->plat_specific_err));
245 INIT_LIST_HEAD(&(slidx->host_ctlr_err));
246 INIT_LIST_HEAD(&(slidx->plat_bus_err));
247 INIT_LIST_HEAD(&(slidx->unsupported));
250 * Extract a Record Header
252 slidx->header = buffer;
255 * Extract each section records
256 * (arranged from "int ia64_log_platform_info_print()")
258 for (ercd_pos = sizeof(sal_log_record_header_t), sects = 0;
259 ercd_pos < record_len; ercd_pos += sp->len, sects++) {
260 sp = (sal_log_section_hdr_t *)((char*)buffer + ercd_pos);
261 if (!efi_guidcmp(sp->guid, SAL_PROC_DEV_ERR_SECT_GUID)) {
262 LOG_INDEX_ADD_SECT_PTR(slidx->proc_err, sp);
263 } else if (!efi_guidcmp(sp->guid,
264 SAL_PLAT_MEM_DEV_ERR_SECT_GUID)) {
266 LOG_INDEX_ADD_SECT_PTR(slidx->mem_dev_err, sp);
267 } else if (!efi_guidcmp(sp->guid,
268 SAL_PLAT_SEL_DEV_ERR_SECT_GUID)) {
270 LOG_INDEX_ADD_SECT_PTR(slidx->sel_dev_err, sp);
271 } else if (!efi_guidcmp(sp->guid,
272 SAL_PLAT_PCI_BUS_ERR_SECT_GUID)) {
274 LOG_INDEX_ADD_SECT_PTR(slidx->pci_bus_err, sp);
275 } else if (!efi_guidcmp(sp->guid,
276 SAL_PLAT_SMBIOS_DEV_ERR_SECT_GUID)) {
278 LOG_INDEX_ADD_SECT_PTR(slidx->smbios_dev_err, sp);
279 } else if (!efi_guidcmp(sp->guid,
280 SAL_PLAT_PCI_COMP_ERR_SECT_GUID)) {
282 LOG_INDEX_ADD_SECT_PTR(slidx->pci_comp_err, sp);
283 } else if (!efi_guidcmp(sp->guid,
284 SAL_PLAT_SPECIFIC_ERR_SECT_GUID)) {
286 LOG_INDEX_ADD_SECT_PTR(slidx->plat_specific_err, sp);
287 } else if (!efi_guidcmp(sp->guid,
288 SAL_PLAT_HOST_CTLR_ERR_SECT_GUID)) {
290 LOG_INDEX_ADD_SECT_PTR(slidx->host_ctlr_err, sp);
291 } else if (!efi_guidcmp(sp->guid,
292 SAL_PLAT_BUS_ERR_SECT_GUID)) {
294 LOG_INDEX_ADD_SECT_PTR(slidx->plat_bus_err, sp);
296 LOG_INDEX_ADD_SECT_PTR(slidx->unsupported, sp);
299 slidx->n_sections = sects;
305 * init_record_index_pools - Initialize pool of lists for SAL record index
308 * 0 on Success / -ENOMEM on Failure
311 init_record_index_pools(void)
314 int rec_max_size; /* Maximum size of SAL error records */
315 int sect_min_size; /* Minimum size of SAL error sections */
316 /* minimum size table of each section */
317 static int sal_log_sect_min_sizes[] = {
318 sizeof(sal_log_processor_info_t)
319 + sizeof(sal_processor_static_info_t),
320 sizeof(sal_log_mem_dev_err_info_t),
321 sizeof(sal_log_sel_dev_err_info_t),
322 sizeof(sal_log_pci_bus_err_info_t),
323 sizeof(sal_log_smbios_dev_err_info_t),
324 sizeof(sal_log_pci_comp_err_info_t),
325 sizeof(sal_log_plat_specific_err_info_t),
326 sizeof(sal_log_host_ctlr_err_info_t),
327 sizeof(sal_log_plat_bus_err_info_t),
331 * MCA handler cannot allocate new memory on flight,
332 * so we preallocate enough memory to handle a SAL record.
334 * Initialize a handling set of slidx_pool:
335 * 1. Pick up the max size of SAL error records
336 * 2. Pick up the min size of SAL error sections
337 * 3. Allocate the pool as enough to 2 SAL records
338 * (now we can estimate the maxinum of section in a record.)
342 rec_max_size = sal_rec_max;
345 sect_min_size = sal_log_sect_min_sizes[0];
346 for (i = 1; i < ARRAY_SIZE(sal_log_sect_min_sizes); i++)
347 if (sect_min_size > sal_log_sect_min_sizes[i])
348 sect_min_size = sal_log_sect_min_sizes[i];
351 slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
353 kmalloc_array(slidx_pool.max_idx, sizeof(slidx_list_t),
356 return slidx_pool.buffer ? 0 : -ENOMEM;
360 /*****************************************************************************
361 * Recovery functions *
362 *****************************************************************************/
365 * is_mca_global - Check whether this MCA is global or not
366 * @peidx: pointer of index of processor error section
367 * @pbci: pointer to pal_bus_check_info_t
368 * @sos: pointer to hand off struct between SAL and OS
371 * MCA_IS_LOCAL / MCA_IS_GLOBAL
375 is_mca_global(peidx_table_t *peidx, pal_bus_check_info_t *pbci,
376 struct ia64_sal_os_state *sos)
378 pal_processor_state_info_t *psp =
379 (pal_processor_state_info_t*)peidx_psp(peidx);
382 * PAL can request a rendezvous, if the MCA has a global scope.
383 * If "rz_always" flag is set, SAL requests MCA rendezvous
384 * in spite of global MCA.
385 * Therefore it is local MCA when rendezvous has not been requested.
386 * Failed to rendezvous, the system must be down.
388 switch (sos->rv_rc) {
389 case -1: /* SAL rendezvous unsuccessful */
390 return MCA_IS_GLOBAL;
391 case 0: /* SAL rendezvous not required */
393 case 1: /* SAL rendezvous successful int */
394 case 2: /* SAL rendezvous successful int with init */
400 * If One or more Cache/TLB/Reg_File/Uarch_Check is here,
401 * it would be a local MCA. (i.e. processor internal error)
403 if (psp->tc || psp->cc || psp->rc || psp->uc)
407 * Bus_Check structure with Bus_Check.ib (internal bus error) flag set
408 * would be a global MCA. (e.g. a system bus address parity error)
410 if (!pbci || pbci->ib)
411 return MCA_IS_GLOBAL;
414 * Bus_Check structure with Bus_Check.eb (external bus error) flag set
415 * could be either a local MCA or a global MCA.
417 * Referring Bus_Check.bsi:
418 * 0: Unknown/unclassified
422 * (FIXME: Are these SGI specific or generic bsi values?)
427 /* e.g. a load from poisoned memory */
432 return MCA_IS_GLOBAL;
435 return MCA_IS_GLOBAL;
439 * get_target_identifier - Get the valid Cache or Bus check target identifier.
440 * @peidx: pointer of index of processor error section
443 * target address on Success / 0 on Failure
446 get_target_identifier(peidx_table_t *peidx)
448 u64 target_address = 0;
449 sal_log_mod_error_info_t *smei;
450 pal_cache_check_info_t *pcci;
454 * Look through the cache checks for a valid target identifier
455 * If more than one valid target identifier, return the one
456 * with the lowest cache level.
458 for (i = 0; i < peidx_cache_check_num(peidx); i++) {
459 smei = (sal_log_mod_error_info_t *)peidx_cache_check(peidx, i);
460 if (smei->valid.target_identifier && smei->target_identifier) {
461 pcci = (pal_cache_check_info_t *)&(smei->check_info);
462 if (!target_address || (pcci->level < level)) {
463 target_address = smei->target_identifier;
470 return target_address;
473 * Look at the bus check for a valid target identifier
475 smei = peidx_bus_check(peidx, 0);
476 if (smei && smei->valid.target_identifier)
477 return smei->target_identifier;
483 * recover_from_read_error - Try to recover the errors which type are "read"s.
484 * @slidx: pointer of index of SAL error record
485 * @peidx: pointer of index of processor error section
486 * @pbci: pointer of pal_bus_check_info
487 * @sos: pointer to hand off struct between SAL and OS
490 * 1 on Success / 0 on Failure
494 recover_from_read_error(slidx_table_t *slidx,
495 peidx_table_t *peidx, pal_bus_check_info_t *pbci,
496 struct ia64_sal_os_state *sos)
498 u64 target_identifier;
499 pal_min_state_area_t *pmsa;
500 struct ia64_psr *psr1, *psr2;
501 ia64_fptr_t *mca_hdlr_bh = (ia64_fptr_t*)mca_handler_bhhook;
503 /* Is target address valid? */
504 target_identifier = get_target_identifier(peidx);
505 if (!target_identifier)
506 return fatal_mca("target address not valid");
509 * cpu read or memory-mapped io read
511 * offending process affected process OS MCA do
512 * kernel mode kernel mode down system
513 * kernel mode user mode kill the process
514 * user mode kernel mode down system (*)
515 * user mode user mode kill the process
517 * (*) You could terminate offending user-mode process
518 * if (pbci->pv && pbci->pl != 0) *and* if you sure
519 * the process not have any locks of kernel.
522 /* Is minstate valid? */
523 if (!peidx_bottom(peidx) || !(peidx_bottom(peidx)->valid.minstate))
524 return fatal_mca("minstate not valid");
525 psr1 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_ipsr);
526 psr2 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_xpsr);
529 * Check the privilege level of interrupted context.
530 * If it is user-mode, then terminate affected process.
533 pmsa = sos->pal_min_state;
534 if (psr1->cpl != 0 ||
535 ((psr2->cpl != 0) && mca_recover_range(pmsa->pmsa_iip))) {
537 * setup for resume to bottom half of MCA,
538 * "mca_handler_bhhook"
540 /* pass to bhhook as argument (gr8, ...) */
541 pmsa->pmsa_gr[8-1] = target_identifier;
542 pmsa->pmsa_gr[9-1] = pmsa->pmsa_iip;
543 pmsa->pmsa_gr[10-1] = pmsa->pmsa_ipsr;
544 /* set interrupted return address (but no use) */
545 pmsa->pmsa_br0 = pmsa->pmsa_iip;
546 /* change resume address to bottom half */
547 pmsa->pmsa_iip = mca_hdlr_bh->fp;
548 pmsa->pmsa_gr[1-1] = mca_hdlr_bh->gp;
549 /* set cpl with kernel mode */
550 psr2 = (struct ia64_psr *)&pmsa->pmsa_ipsr;
556 return mca_recovered("user memory corruption. "
557 "kill affected process - recovered.");
560 return fatal_mca("kernel context not recovered, iip 0x%lx\n",
565 * recover_from_platform_error - Recover from platform error.
566 * @slidx: pointer of index of SAL error record
567 * @peidx: pointer of index of processor error section
568 * @pbci: pointer of pal_bus_check_info
569 * @sos: pointer to hand off struct between SAL and OS
572 * 1 on Success / 0 on Failure
576 recover_from_platform_error(slidx_table_t *slidx, peidx_table_t *peidx,
577 pal_bus_check_info_t *pbci,
578 struct ia64_sal_os_state *sos)
581 pal_processor_state_info_t *psp =
582 (pal_processor_state_info_t*)peidx_psp(peidx);
584 if (psp->bc && pbci->eb && pbci->bsi == 0) {
586 case 1: /* partial read */
587 case 3: /* full line(cpu) read */
588 case 9: /* I/O space read */
589 status = recover_from_read_error(slidx, peidx, pbci,
592 case 0: /* unknown */
593 case 2: /* partial write */
594 case 4: /* full line write */
595 case 5: /* implicit or explicit write-back operation */
596 case 6: /* snoop probe */
597 case 7: /* incoming or outgoing ptc.g */
598 case 8: /* write coalescing transactions */
599 case 10: /* I/O space write */
600 case 11: /* inter-processor interrupt message(IPI) */
601 case 12: /* interrupt acknowledge or
602 external task priority cycle */
606 } else if (psp->cc && !psp->bc) { /* Cache error */
607 status = recover_from_read_error(slidx, peidx, pbci, sos);
614 * recover_from_tlb_check
615 * @peidx: pointer of index of processor error section
618 * 1 on Success / 0 on Failure
621 recover_from_tlb_check(peidx_table_t *peidx)
623 sal_log_mod_error_info_t *smei;
624 pal_tlb_check_info_t *ptci;
626 smei = (sal_log_mod_error_info_t *)peidx_tlb_check(peidx, 0);
627 ptci = (pal_tlb_check_info_t *)&(smei->check_info);
630 * Look for signature of a duplicate TLB DTC entry, which is
631 * a SW bug and always fatal.
633 if (ptci->op == PAL_TLB_CHECK_OP_PURGE
634 && !(ptci->itr || ptci->dtc || ptci->itc))
635 return fatal_mca("Duplicate TLB entry");
637 return mca_recovered("TLB check recovered");
641 * recover_from_processor_error
642 * @platform: whether there are some platform error section or not
643 * @slidx: pointer of index of SAL error record
644 * @peidx: pointer of index of processor error section
645 * @pbci: pointer of pal_bus_check_info
646 * @sos: pointer to hand off struct between SAL and OS
649 * 1 on Success / 0 on Failure
653 recover_from_processor_error(int platform, slidx_table_t *slidx,
654 peidx_table_t *peidx, pal_bus_check_info_t *pbci,
655 struct ia64_sal_os_state *sos)
657 pal_processor_state_info_t *psp =
658 (pal_processor_state_info_t*)peidx_psp(peidx);
661 * Processor recovery status must key off of the PAL recovery
662 * status in the Processor State Parameter.
666 * The machine check is corrected.
669 return mca_recovered("machine check is already corrected.");
672 * The error was not contained. Software must be reset.
674 if (psp->us || psp->ci == 0)
675 return fatal_mca("error not contained");
678 * Look for recoverable TLB check
680 if (psp->tc && !(psp->cc || psp->bc || psp->rc || psp->uc))
681 return recover_from_tlb_check(peidx);
684 * The cache check and bus check bits have four possible states
686 * 1 1 Memory error, attempt recovery
687 * 1 0 Cache error, attempt recovery
688 * 0 1 I/O error, attempt recovery
689 * 0 0 Other error type, not recovered
691 if (psp->cc == 0 && (psp->bc == 0 || pbci == NULL))
692 return fatal_mca("No cache or bus check");
695 * Cannot handle more than one bus check.
697 if (peidx_bus_check_num(peidx) > 1)
698 return fatal_mca("Too many bus checks");
701 return fatal_mca("Internal Bus error");
702 if (pbci->eb && pbci->bsi > 0)
703 return fatal_mca("External bus check fatal status");
706 * This is a local MCA and estimated as a recoverable error.
709 return recover_from_platform_error(slidx, peidx, pbci, sos);
712 * On account of strange SAL error record, we cannot recover.
714 return fatal_mca("Strange SAL record");
718 * mca_try_to_recover - Try to recover from MCA
719 * @rec: pointer to a SAL error record
720 * @sos: pointer to hand off struct between SAL and OS
723 * 1 on Success / 0 on Failure
727 mca_try_to_recover(void *rec, struct ia64_sal_os_state *sos)
733 pal_bus_check_info_t pbci;
735 /* Make index of SAL error record */
736 platform_err = mca_make_slidx(rec, &slidx);
738 /* Count processor error sections */
739 n_proc_err = slidx_count(&slidx, proc_err);
741 /* Now, OS can recover when there is one processor error section */
743 return fatal_mca("Too Many Errors");
744 else if (n_proc_err == 0)
745 /* Weird SAL record ... We can't do anything */
746 return fatal_mca("Weird SAL record");
748 /* Make index of processor error section */
749 mca_make_peidx((sal_log_processor_info_t*)
750 slidx_first_entry(&slidx.proc_err)->hdr, &peidx);
752 /* Extract Processor BUS_CHECK[0] */
753 *((u64*)&pbci) = peidx_check_info(&peidx, bus_check, 0);
755 /* Check whether MCA is global or not */
756 if (is_mca_global(&peidx, &pbci, sos))
757 return fatal_mca("global MCA");
759 /* Try to recover a processor error */
760 return recover_from_processor_error(platform_err, &slidx, &peidx,
765 * =============================================================================
768 int __init mca_external_handler_init(void)
770 if (init_record_index_pools())
773 /* register external mca handlers */
774 if (ia64_reg_MCA_extension(mca_try_to_recover)) {
775 printk(KERN_ERR "ia64_reg_MCA_extension failed.\n");
776 kfree(slidx_pool.buffer);
782 void __exit mca_external_handler_exit(void)
784 /* unregister external mca handlers */
785 ia64_unreg_MCA_extension();
786 kfree(slidx_pool.buffer);
789 module_init(mca_external_handler_init);
790 module_exit(mca_external_handler_exit);
792 module_param(sal_rec_max, int, 0644);
793 MODULE_PARM_DESC(sal_rec_max, "Max size of SAL error record");
795 MODULE_DESCRIPTION("ia64 platform dependent mca handler driver");
796 MODULE_LICENSE("GPL");