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/sched/task.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/kallsyms.h>
19 #include <linux/memblock.h>
20 #include <linux/acpi.h>
21 #include <linux/timer.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/smp.h>
25 #include <linux/workqueue.h>
27 #include <linux/slab.h>
29 #include <asm/delay.h>
31 #include <asm/ptrace.h>
36 #include <asm/hw_irq.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);
53 #define MAX_PAGE_ISOLATE 1024
55 static struct page *page_isolate[MAX_PAGE_ISOLATE];
56 static int num_page_isolate = 0;
65 MCA_NOT_RECOVERED = 0,
70 * This pool keeps pointers to the section part of SAL error record
73 slidx_list_t *buffer; /* section pointer list pool */
74 int cur_idx; /* Current index of section pointer list pool */
75 int max_idx; /* Maximum index of section pointer list pool */
79 fatal_mca(const char *fmt, ...)
85 vsnprintf(buf, sizeof(buf), fmt, args);
87 ia64_mca_printk(KERN_ALERT "MCA: %s\n", buf);
89 return MCA_NOT_RECOVERED;
93 mca_recovered(const char *fmt, ...)
99 vsnprintf(buf, sizeof(buf), fmt, args);
101 ia64_mca_printk(KERN_INFO "MCA: %s\n", buf);
103 return MCA_RECOVERED;
107 * mca_page_isolate - isolate a poisoned page in order not to use it later
108 * @paddr: poisoned memory location
111 * one of isolate_status_t, ISOLATE_OK/NG/NONE.
114 static isolate_status_t
115 mca_page_isolate(unsigned long paddr)
120 /* whether physical address is valid or not */
121 if (!ia64_phys_addr_valid(paddr))
124 if (!pfn_valid(paddr >> PAGE_SHIFT))
127 /* convert physical address to physical page number */
128 p = pfn_to_page(paddr>>PAGE_SHIFT);
130 /* check whether a page number have been already registered or not */
131 for (i = 0; i < num_page_isolate; i++)
132 if (page_isolate[i] == p)
133 return ISOLATE_OK; /* already listed */
135 /* limitation check */
136 if (num_page_isolate == MAX_PAGE_ISOLATE)
139 /* kick pages having attribute 'SLAB' or 'Reserved' */
140 if (PageSlab(p) || PageReserved(p))
143 /* add attribute 'Reserved' and register the page */
146 page_isolate[num_page_isolate++] = p;
152 * mca_hanlder_bh - Kill the process which occurred memory read error
153 * @paddr: poisoned address received from MCA Handler
157 mca_handler_bh(unsigned long paddr, void *iip, unsigned long ipsr)
160 printk(KERN_ERR "OS_MCA: process [cpu %d, pid: %d, uid: %d, "
161 "iip: %p, psr: 0x%lx,paddr: 0x%lx](%s) encounters MCA.\n",
162 raw_smp_processor_id(), current->pid,
163 from_kuid(&init_user_ns, current_uid()),
164 iip, ipsr, paddr, current->comm);
166 spin_lock(&mca_bh_lock);
167 switch (mca_page_isolate(paddr)) {
169 printk(KERN_DEBUG "Page isolation: ( %lx ) success.\n", paddr);
172 printk(KERN_CRIT "Page isolation: ( %lx ) failure.\n", paddr);
177 spin_unlock(&mca_bh_lock);
179 /* This process is about to be killed itself */
180 make_task_dead(SIGKILL);
184 * mca_make_peidx - Make index of processor error section
185 * @slpi: pointer to record of processor error section
186 * @peidx: pointer to index of processor error section
190 mca_make_peidx(sal_log_processor_info_t *slpi, peidx_table_t *peidx)
193 * calculate the start address of
194 * "struct cpuid_info" and "sal_processor_static_info_t".
196 u64 total_check_num = slpi->valid.num_cache_check
197 + slpi->valid.num_tlb_check
198 + slpi->valid.num_bus_check
199 + slpi->valid.num_reg_file_check
200 + slpi->valid.num_ms_check;
201 u64 head_size = sizeof(sal_log_mod_error_info_t) * total_check_num
202 + sizeof(sal_log_processor_info_t);
203 u64 mid_size = slpi->valid.cpuid_info * sizeof(struct sal_cpuid_info);
205 peidx_head(peidx) = slpi;
206 peidx_mid(peidx) = (struct sal_cpuid_info *)
207 (slpi->valid.cpuid_info ? ((char*)slpi + head_size) : NULL);
208 peidx_bottom(peidx) = (sal_processor_static_info_t *)
209 (slpi->valid.psi_static_struct ?
210 ((char*)slpi + head_size + mid_size) : NULL);
214 * mca_make_slidx - Make index of SAL error record
215 * @buffer: pointer to SAL error record
216 * @slidx: pointer to index of SAL error record
219 * 1 if record has platform error / 0 if not
221 #define LOG_INDEX_ADD_SECT_PTR(sect, ptr) \
222 {slidx_list_t *hl = &slidx_pool.buffer[slidx_pool.cur_idx]; \
224 list_add(&hl->list, &(sect)); \
225 slidx_pool.cur_idx = (slidx_pool.cur_idx + 1)%slidx_pool.max_idx; }
228 mca_make_slidx(void *buffer, slidx_table_t *slidx)
230 int platform_err = 0;
231 int record_len = ((sal_log_record_header_t*)buffer)->len;
234 sal_log_section_hdr_t *sp;
237 * Initialize index referring current record
239 INIT_LIST_HEAD(&(slidx->proc_err));
240 INIT_LIST_HEAD(&(slidx->mem_dev_err));
241 INIT_LIST_HEAD(&(slidx->sel_dev_err));
242 INIT_LIST_HEAD(&(slidx->pci_bus_err));
243 INIT_LIST_HEAD(&(slidx->smbios_dev_err));
244 INIT_LIST_HEAD(&(slidx->pci_comp_err));
245 INIT_LIST_HEAD(&(slidx->plat_specific_err));
246 INIT_LIST_HEAD(&(slidx->host_ctlr_err));
247 INIT_LIST_HEAD(&(slidx->plat_bus_err));
248 INIT_LIST_HEAD(&(slidx->unsupported));
251 * Extract a Record Header
253 slidx->header = buffer;
256 * Extract each section records
257 * (arranged from "int ia64_log_platform_info_print()")
259 for (ercd_pos = sizeof(sal_log_record_header_t), sects = 0;
260 ercd_pos < record_len; ercd_pos += sp->len, sects++) {
261 sp = (sal_log_section_hdr_t *)((char*)buffer + ercd_pos);
262 if (!efi_guidcmp(sp->guid, SAL_PROC_DEV_ERR_SECT_GUID)) {
263 LOG_INDEX_ADD_SECT_PTR(slidx->proc_err, sp);
264 } else if (!efi_guidcmp(sp->guid,
265 SAL_PLAT_MEM_DEV_ERR_SECT_GUID)) {
267 LOG_INDEX_ADD_SECT_PTR(slidx->mem_dev_err, sp);
268 } else if (!efi_guidcmp(sp->guid,
269 SAL_PLAT_SEL_DEV_ERR_SECT_GUID)) {
271 LOG_INDEX_ADD_SECT_PTR(slidx->sel_dev_err, sp);
272 } else if (!efi_guidcmp(sp->guid,
273 SAL_PLAT_PCI_BUS_ERR_SECT_GUID)) {
275 LOG_INDEX_ADD_SECT_PTR(slidx->pci_bus_err, sp);
276 } else if (!efi_guidcmp(sp->guid,
277 SAL_PLAT_SMBIOS_DEV_ERR_SECT_GUID)) {
279 LOG_INDEX_ADD_SECT_PTR(slidx->smbios_dev_err, sp);
280 } else if (!efi_guidcmp(sp->guid,
281 SAL_PLAT_PCI_COMP_ERR_SECT_GUID)) {
283 LOG_INDEX_ADD_SECT_PTR(slidx->pci_comp_err, sp);
284 } else if (!efi_guidcmp(sp->guid,
285 SAL_PLAT_SPECIFIC_ERR_SECT_GUID)) {
287 LOG_INDEX_ADD_SECT_PTR(slidx->plat_specific_err, sp);
288 } else if (!efi_guidcmp(sp->guid,
289 SAL_PLAT_HOST_CTLR_ERR_SECT_GUID)) {
291 LOG_INDEX_ADD_SECT_PTR(slidx->host_ctlr_err, sp);
292 } else if (!efi_guidcmp(sp->guid,
293 SAL_PLAT_BUS_ERR_SECT_GUID)) {
295 LOG_INDEX_ADD_SECT_PTR(slidx->plat_bus_err, sp);
297 LOG_INDEX_ADD_SECT_PTR(slidx->unsupported, sp);
300 slidx->n_sections = sects;
306 * init_record_index_pools - Initialize pool of lists for SAL record index
309 * 0 on Success / -ENOMEM on Failure
312 init_record_index_pools(void)
315 int rec_max_size; /* Maximum size of SAL error records */
316 int sect_min_size; /* Minimum size of SAL error sections */
317 /* minimum size table of each section */
318 static int sal_log_sect_min_sizes[] = {
319 sizeof(sal_log_processor_info_t)
320 + sizeof(sal_processor_static_info_t),
321 sizeof(sal_log_mem_dev_err_info_t),
322 sizeof(sal_log_sel_dev_err_info_t),
323 sizeof(sal_log_pci_bus_err_info_t),
324 sizeof(sal_log_smbios_dev_err_info_t),
325 sizeof(sal_log_pci_comp_err_info_t),
326 sizeof(sal_log_plat_specific_err_info_t),
327 sizeof(sal_log_host_ctlr_err_info_t),
328 sizeof(sal_log_plat_bus_err_info_t),
332 * MCA handler cannot allocate new memory on flight,
333 * so we preallocate enough memory to handle a SAL record.
335 * Initialize a handling set of slidx_pool:
336 * 1. Pick up the max size of SAL error records
337 * 2. Pick up the min size of SAL error sections
338 * 3. Allocate the pool as enough to 2 SAL records
339 * (now we can estimate the maxinum of section in a record.)
343 rec_max_size = sal_rec_max;
346 sect_min_size = sal_log_sect_min_sizes[0];
347 for (i = 1; i < ARRAY_SIZE(sal_log_sect_min_sizes); i++)
348 if (sect_min_size > sal_log_sect_min_sizes[i])
349 sect_min_size = sal_log_sect_min_sizes[i];
352 slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
354 kmalloc_array(slidx_pool.max_idx, sizeof(slidx_list_t),
357 return slidx_pool.buffer ? 0 : -ENOMEM;
361 /*****************************************************************************
362 * Recovery functions *
363 *****************************************************************************/
366 * is_mca_global - Check whether this MCA is global or not
367 * @peidx: pointer of index of processor error section
368 * @pbci: pointer to pal_bus_check_info_t
369 * @sos: pointer to hand off struct between SAL and OS
372 * MCA_IS_LOCAL / MCA_IS_GLOBAL
376 is_mca_global(peidx_table_t *peidx, pal_bus_check_info_t *pbci,
377 struct ia64_sal_os_state *sos)
379 pal_processor_state_info_t *psp =
380 (pal_processor_state_info_t*)peidx_psp(peidx);
383 * PAL can request a rendezvous, if the MCA has a global scope.
384 * If "rz_always" flag is set, SAL requests MCA rendezvous
385 * in spite of global MCA.
386 * Therefore it is local MCA when rendezvous has not been requested.
387 * Failed to rendezvous, the system must be down.
389 switch (sos->rv_rc) {
390 case -1: /* SAL rendezvous unsuccessful */
391 return MCA_IS_GLOBAL;
392 case 0: /* SAL rendezvous not required */
394 case 1: /* SAL rendezvous successful int */
395 case 2: /* SAL rendezvous successful int with init */
401 * If One or more Cache/TLB/Reg_File/Uarch_Check is here,
402 * it would be a local MCA. (i.e. processor internal error)
404 if (psp->tc || psp->cc || psp->rc || psp->uc)
408 * Bus_Check structure with Bus_Check.ib (internal bus error) flag set
409 * would be a global MCA. (e.g. a system bus address parity error)
411 if (!pbci || pbci->ib)
412 return MCA_IS_GLOBAL;
415 * Bus_Check structure with Bus_Check.eb (external bus error) flag set
416 * could be either a local MCA or a global MCA.
418 * Referring Bus_Check.bsi:
419 * 0: Unknown/unclassified
423 * (FIXME: Are these SGI specific or generic bsi values?)
428 /* e.g. a load from poisoned memory */
433 return MCA_IS_GLOBAL;
436 return MCA_IS_GLOBAL;
440 * get_target_identifier - Get the valid Cache or Bus check target identifier.
441 * @peidx: pointer of index of processor error section
444 * target address on Success / 0 on Failure
447 get_target_identifier(peidx_table_t *peidx)
449 u64 target_address = 0;
450 sal_log_mod_error_info_t *smei;
451 pal_cache_check_info_t *pcci;
455 * Look through the cache checks for a valid target identifier
456 * If more than one valid target identifier, return the one
457 * with the lowest cache level.
459 for (i = 0; i < peidx_cache_check_num(peidx); i++) {
460 smei = (sal_log_mod_error_info_t *)peidx_cache_check(peidx, i);
461 if (smei->valid.target_identifier && smei->target_identifier) {
462 pcci = (pal_cache_check_info_t *)&(smei->check_info);
463 if (!target_address || (pcci->level < level)) {
464 target_address = smei->target_identifier;
471 return target_address;
474 * Look at the bus check for a valid target identifier
476 smei = peidx_bus_check(peidx, 0);
477 if (smei && smei->valid.target_identifier)
478 return smei->target_identifier;
484 * recover_from_read_error - Try to recover the errors which type are "read"s.
485 * @slidx: pointer of index of SAL error record
486 * @peidx: pointer of index of processor error section
487 * @pbci: pointer of pal_bus_check_info
488 * @sos: pointer to hand off struct between SAL and OS
491 * 1 on Success / 0 on Failure
495 recover_from_read_error(slidx_table_t *slidx,
496 peidx_table_t *peidx, pal_bus_check_info_t *pbci,
497 struct ia64_sal_os_state *sos)
499 u64 target_identifier;
500 pal_min_state_area_t *pmsa;
501 struct ia64_psr *psr1, *psr2;
502 ia64_fptr_t *mca_hdlr_bh = (ia64_fptr_t*)mca_handler_bhhook;
504 /* Is target address valid? */
505 target_identifier = get_target_identifier(peidx);
506 if (!target_identifier)
507 return fatal_mca("target address not valid");
510 * cpu read or memory-mapped io read
512 * offending process affected process OS MCA do
513 * kernel mode kernel mode down system
514 * kernel mode user mode kill the process
515 * user mode kernel mode down system (*)
516 * user mode user mode kill the process
518 * (*) You could terminate offending user-mode process
519 * if (pbci->pv && pbci->pl != 0) *and* if you sure
520 * the process not have any locks of kernel.
523 /* Is minstate valid? */
524 if (!peidx_bottom(peidx) || !(peidx_bottom(peidx)->valid.minstate))
525 return fatal_mca("minstate not valid");
526 psr1 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_ipsr);
527 psr2 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_xpsr);
530 * Check the privilege level of interrupted context.
531 * If it is user-mode, then terminate affected process.
534 pmsa = sos->pal_min_state;
535 if (psr1->cpl != 0 ||
536 ((psr2->cpl != 0) && mca_recover_range(pmsa->pmsa_iip))) {
538 * setup for resume to bottom half of MCA,
539 * "mca_handler_bhhook"
541 /* pass to bhhook as argument (gr8, ...) */
542 pmsa->pmsa_gr[8-1] = target_identifier;
543 pmsa->pmsa_gr[9-1] = pmsa->pmsa_iip;
544 pmsa->pmsa_gr[10-1] = pmsa->pmsa_ipsr;
545 /* set interrupted return address (but no use) */
546 pmsa->pmsa_br0 = pmsa->pmsa_iip;
547 /* change resume address to bottom half */
548 pmsa->pmsa_iip = mca_hdlr_bh->fp;
549 pmsa->pmsa_gr[1-1] = mca_hdlr_bh->gp;
550 /* set cpl with kernel mode */
551 psr2 = (struct ia64_psr *)&pmsa->pmsa_ipsr;
557 return mca_recovered("user memory corruption. "
558 "kill affected process - recovered.");
561 return fatal_mca("kernel context not recovered, iip 0x%lx\n",
566 * recover_from_platform_error - Recover from platform error.
567 * @slidx: pointer of index of SAL error record
568 * @peidx: pointer of index of processor error section
569 * @pbci: pointer of pal_bus_check_info
570 * @sos: pointer to hand off struct between SAL and OS
573 * 1 on Success / 0 on Failure
577 recover_from_platform_error(slidx_table_t *slidx, peidx_table_t *peidx,
578 pal_bus_check_info_t *pbci,
579 struct ia64_sal_os_state *sos)
582 pal_processor_state_info_t *psp =
583 (pal_processor_state_info_t*)peidx_psp(peidx);
585 if (psp->bc && pbci->eb && pbci->bsi == 0) {
587 case 1: /* partial read */
588 case 3: /* full line(cpu) read */
589 case 9: /* I/O space read */
590 status = recover_from_read_error(slidx, peidx, pbci,
593 case 0: /* unknown */
594 case 2: /* partial write */
595 case 4: /* full line write */
596 case 5: /* implicit or explicit write-back operation */
597 case 6: /* snoop probe */
598 case 7: /* incoming or outgoing ptc.g */
599 case 8: /* write coalescing transactions */
600 case 10: /* I/O space write */
601 case 11: /* inter-processor interrupt message(IPI) */
602 case 12: /* interrupt acknowledge or
603 external task priority cycle */
607 } else if (psp->cc && !psp->bc) { /* Cache error */
608 status = recover_from_read_error(slidx, peidx, pbci, sos);
615 * recover_from_tlb_check
616 * @peidx: pointer of index of processor error section
619 * 1 on Success / 0 on Failure
622 recover_from_tlb_check(peidx_table_t *peidx)
624 sal_log_mod_error_info_t *smei;
625 pal_tlb_check_info_t *ptci;
627 smei = (sal_log_mod_error_info_t *)peidx_tlb_check(peidx, 0);
628 ptci = (pal_tlb_check_info_t *)&(smei->check_info);
631 * Look for signature of a duplicate TLB DTC entry, which is
632 * a SW bug and always fatal.
634 if (ptci->op == PAL_TLB_CHECK_OP_PURGE
635 && !(ptci->itr || ptci->dtc || ptci->itc))
636 return fatal_mca("Duplicate TLB entry");
638 return mca_recovered("TLB check recovered");
642 * recover_from_processor_error
643 * @platform: whether there are some platform error section or not
644 * @slidx: pointer of index of SAL error record
645 * @peidx: pointer of index of processor error section
646 * @pbci: pointer of pal_bus_check_info
647 * @sos: pointer to hand off struct between SAL and OS
650 * 1 on Success / 0 on Failure
654 recover_from_processor_error(int platform, slidx_table_t *slidx,
655 peidx_table_t *peidx, pal_bus_check_info_t *pbci,
656 struct ia64_sal_os_state *sos)
658 pal_processor_state_info_t *psp =
659 (pal_processor_state_info_t*)peidx_psp(peidx);
662 * Processor recovery status must key off of the PAL recovery
663 * status in the Processor State Parameter.
667 * The machine check is corrected.
670 return mca_recovered("machine check is already corrected.");
673 * The error was not contained. Software must be reset.
675 if (psp->us || psp->ci == 0)
676 return fatal_mca("error not contained");
679 * Look for recoverable TLB check
681 if (psp->tc && !(psp->cc || psp->bc || psp->rc || psp->uc))
682 return recover_from_tlb_check(peidx);
685 * The cache check and bus check bits have four possible states
687 * 1 1 Memory error, attempt recovery
688 * 1 0 Cache error, attempt recovery
689 * 0 1 I/O error, attempt recovery
690 * 0 0 Other error type, not recovered
692 if (psp->cc == 0 && (psp->bc == 0 || pbci == NULL))
693 return fatal_mca("No cache or bus check");
696 * Cannot handle more than one bus check.
698 if (peidx_bus_check_num(peidx) > 1)
699 return fatal_mca("Too many bus checks");
702 return fatal_mca("Internal Bus error");
703 if (pbci->eb && pbci->bsi > 0)
704 return fatal_mca("External bus check fatal status");
707 * This is a local MCA and estimated as a recoverable error.
710 return recover_from_platform_error(slidx, peidx, pbci, sos);
713 * On account of strange SAL error record, we cannot recover.
715 return fatal_mca("Strange SAL record");
719 * mca_try_to_recover - Try to recover from MCA
720 * @rec: pointer to a SAL error record
721 * @sos: pointer to hand off struct between SAL and OS
724 * 1 on Success / 0 on Failure
728 mca_try_to_recover(void *rec, struct ia64_sal_os_state *sos)
734 pal_bus_check_info_t pbci;
736 /* Make index of SAL error record */
737 platform_err = mca_make_slidx(rec, &slidx);
739 /* Count processor error sections */
740 n_proc_err = slidx_count(&slidx, proc_err);
742 /* Now, OS can recover when there is one processor error section */
744 return fatal_mca("Too Many Errors");
745 else if (n_proc_err == 0)
746 /* Weird SAL record ... We can't do anything */
747 return fatal_mca("Weird SAL record");
749 /* Make index of processor error section */
750 mca_make_peidx((sal_log_processor_info_t*)
751 slidx_first_entry(&slidx.proc_err)->hdr, &peidx);
753 /* Extract Processor BUS_CHECK[0] */
754 *((u64*)&pbci) = peidx_check_info(&peidx, bus_check, 0);
756 /* Check whether MCA is global or not */
757 if (is_mca_global(&peidx, &pbci, sos))
758 return fatal_mca("global MCA");
760 /* Try to recover a processor error */
761 return recover_from_processor_error(platform_err, &slidx, &peidx,
766 * =============================================================================
769 int __init mca_external_handler_init(void)
771 if (init_record_index_pools())
774 /* register external mca handlers */
775 if (ia64_reg_MCA_extension(mca_try_to_recover)) {
776 printk(KERN_ERR "ia64_reg_MCA_extension failed.\n");
777 kfree(slidx_pool.buffer);
783 void __exit mca_external_handler_exit(void)
785 /* unregister external mca handlers */
786 ia64_unreg_MCA_extension();
787 kfree(slidx_pool.buffer);
790 module_init(mca_external_handler_init);
791 module_exit(mca_external_handler_exit);
793 module_param(sal_rec_max, int, 0644);
794 MODULE_PARM_DESC(sal_rec_max, "Max size of SAL error record");
796 MODULE_DESCRIPTION("ia64 platform dependent mca handler driver");
797 MODULE_LICENSE("GPL");