1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Machine check exception handling.
5 * Copyright 2013 IBM Corporation
6 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
10 #define pr_fmt(fmt) "mce: " fmt
12 #include <linux/hardirq.h>
13 #include <linux/types.h>
14 #include <linux/ptrace.h>
15 #include <linux/percpu.h>
16 #include <linux/export.h>
17 #include <linux/irq_work.h>
18 #include <linux/extable.h>
19 #include <linux/ftrace.h>
20 #include <linux/memblock.h>
23 #include <asm/interrupt.h>
24 #include <asm/machdep.h>
27 #include <asm/asm-prototypes.h>
31 static void machine_check_process_queued_event(struct irq_work *work);
32 static void machine_check_ue_irq_work(struct irq_work *work);
33 static void machine_check_ue_event(struct machine_check_event *evt);
34 static void machine_process_ue_event(struct work_struct *work);
36 static struct irq_work mce_event_process_work = {
37 .func = machine_check_process_queued_event,
40 static struct irq_work mce_ue_event_irq_work = {
41 .func = machine_check_ue_irq_work,
44 static DECLARE_WORK(mce_ue_event_work, machine_process_ue_event);
46 static BLOCKING_NOTIFIER_HEAD(mce_notifier_list);
48 int mce_register_notifier(struct notifier_block *nb)
50 return blocking_notifier_chain_register(&mce_notifier_list, nb);
52 EXPORT_SYMBOL_GPL(mce_register_notifier);
54 int mce_unregister_notifier(struct notifier_block *nb)
56 return blocking_notifier_chain_unregister(&mce_notifier_list, nb);
58 EXPORT_SYMBOL_GPL(mce_unregister_notifier);
60 static void mce_set_error_info(struct machine_check_event *mce,
61 struct mce_error_info *mce_err)
63 mce->error_type = mce_err->error_type;
64 switch (mce_err->error_type) {
65 case MCE_ERROR_TYPE_UE:
66 mce->u.ue_error.ue_error_type = mce_err->u.ue_error_type;
68 case MCE_ERROR_TYPE_SLB:
69 mce->u.slb_error.slb_error_type = mce_err->u.slb_error_type;
71 case MCE_ERROR_TYPE_ERAT:
72 mce->u.erat_error.erat_error_type = mce_err->u.erat_error_type;
74 case MCE_ERROR_TYPE_TLB:
75 mce->u.tlb_error.tlb_error_type = mce_err->u.tlb_error_type;
77 case MCE_ERROR_TYPE_USER:
78 mce->u.user_error.user_error_type = mce_err->u.user_error_type;
80 case MCE_ERROR_TYPE_RA:
81 mce->u.ra_error.ra_error_type = mce_err->u.ra_error_type;
83 case MCE_ERROR_TYPE_LINK:
84 mce->u.link_error.link_error_type = mce_err->u.link_error_type;
86 case MCE_ERROR_TYPE_UNKNOWN:
93 * Decode and save high level MCE information into per cpu buffer which
94 * is an array of machine_check_event structure.
96 void save_mce_event(struct pt_regs *regs, long handled,
97 struct mce_error_info *mce_err,
98 uint64_t nip, uint64_t addr, uint64_t phys_addr)
100 int index = local_paca->mce_info->mce_nest_count++;
101 struct machine_check_event *mce;
103 mce = &local_paca->mce_info->mce_event[index];
105 * Return if we don't have enough space to log mce event.
106 * mce_nest_count may go beyond MAX_MC_EVT but that's ok,
107 * the check below will stop buffer overrun.
109 if (index >= MAX_MC_EVT)
112 /* Populate generic machine check info */
113 mce->version = MCE_V1;
115 mce->srr1 = regs->msr;
116 mce->gpr3 = regs->gpr[3];
118 mce->cpu = get_paca()->paca_index;
120 /* Mark it recovered if we have handled it and MSR(RI=1). */
121 if (handled && (regs->msr & MSR_RI))
122 mce->disposition = MCE_DISPOSITION_RECOVERED;
124 mce->disposition = MCE_DISPOSITION_NOT_RECOVERED;
126 mce->initiator = mce_err->initiator;
127 mce->severity = mce_err->severity;
128 mce->sync_error = mce_err->sync_error;
129 mce->error_class = mce_err->error_class;
132 * Populate the mce error_type and type-specific error_type.
134 mce_set_error_info(mce, mce_err);
135 if (mce->error_type == MCE_ERROR_TYPE_UE)
136 mce->u.ue_error.ignore_event = mce_err->ignore_event;
141 if (mce->error_type == MCE_ERROR_TYPE_TLB) {
142 mce->u.tlb_error.effective_address_provided = true;
143 mce->u.tlb_error.effective_address = addr;
144 } else if (mce->error_type == MCE_ERROR_TYPE_SLB) {
145 mce->u.slb_error.effective_address_provided = true;
146 mce->u.slb_error.effective_address = addr;
147 } else if (mce->error_type == MCE_ERROR_TYPE_ERAT) {
148 mce->u.erat_error.effective_address_provided = true;
149 mce->u.erat_error.effective_address = addr;
150 } else if (mce->error_type == MCE_ERROR_TYPE_USER) {
151 mce->u.user_error.effective_address_provided = true;
152 mce->u.user_error.effective_address = addr;
153 } else if (mce->error_type == MCE_ERROR_TYPE_RA) {
154 mce->u.ra_error.effective_address_provided = true;
155 mce->u.ra_error.effective_address = addr;
156 } else if (mce->error_type == MCE_ERROR_TYPE_LINK) {
157 mce->u.link_error.effective_address_provided = true;
158 mce->u.link_error.effective_address = addr;
159 } else if (mce->error_type == MCE_ERROR_TYPE_UE) {
160 mce->u.ue_error.effective_address_provided = true;
161 mce->u.ue_error.effective_address = addr;
162 if (phys_addr != ULONG_MAX) {
163 mce->u.ue_error.physical_address_provided = true;
164 mce->u.ue_error.physical_address = phys_addr;
165 machine_check_ue_event(mce);
173 * mce Pointer to machine_check_event structure to be filled.
174 * release Flag to indicate whether to free the event slot or not.
175 * 0 <= do not release the mce event. Caller will invoke
176 * release_mce_event() once event has been consumed.
177 * 1 <= release the slot.
182 * get_mce_event() will be called by platform specific machine check
183 * handle routine and in KVM.
184 * When we call get_mce_event(), we are still in interrupt context and
185 * preemption will not be scheduled until ret_from_expect() routine
188 int get_mce_event(struct machine_check_event *mce, bool release)
190 int index = local_paca->mce_info->mce_nest_count - 1;
191 struct machine_check_event *mc_evt;
198 /* Check if we have MCE info to process. */
199 if (index < MAX_MC_EVT) {
200 mc_evt = &local_paca->mce_info->mce_event[index];
201 /* Copy the event structure and release the original */
208 /* Decrement the count to free the slot. */
210 local_paca->mce_info->mce_nest_count--;
215 void release_mce_event(void)
217 get_mce_event(NULL, true);
220 static void machine_check_ue_irq_work(struct irq_work *work)
222 schedule_work(&mce_ue_event_work);
226 * Queue up the MCE event which then can be handled later.
228 static void machine_check_ue_event(struct machine_check_event *evt)
232 index = local_paca->mce_info->mce_ue_count++;
233 /* If queue is full, just return for now. */
234 if (index >= MAX_MC_EVT) {
235 local_paca->mce_info->mce_ue_count--;
238 memcpy(&local_paca->mce_info->mce_ue_event_queue[index],
241 /* Queue work to process this event later. */
242 irq_work_queue(&mce_ue_event_irq_work);
246 * Queue up the MCE event which then can be handled later.
248 void machine_check_queue_event(void)
251 struct machine_check_event evt;
254 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
257 index = local_paca->mce_info->mce_queue_count++;
258 /* If queue is full, just return for now. */
259 if (index >= MAX_MC_EVT) {
260 local_paca->mce_info->mce_queue_count--;
263 memcpy(&local_paca->mce_info->mce_event_queue[index],
267 * Queue irq work to process this event later. Before
268 * queuing the work enable translation for non radix LPAR,
269 * as irq_work_queue may try to access memory outside RMO
272 if (!radix_enabled() && firmware_has_feature(FW_FEATURE_LPAR)) {
274 mtmsr(msr | MSR_IR | MSR_DR);
275 irq_work_queue(&mce_event_process_work);
278 irq_work_queue(&mce_event_process_work);
282 void mce_common_process_ue(struct pt_regs *regs,
283 struct mce_error_info *mce_err)
285 const struct exception_table_entry *entry;
287 entry = search_kernel_exception_table(regs->nip);
289 mce_err->ignore_event = true;
290 regs_set_return_ip(regs, extable_fixup(entry));
295 * process pending MCE event from the mce event queue. This function will be
296 * called during syscall exit.
298 static void machine_process_ue_event(struct work_struct *work)
301 struct machine_check_event *evt;
303 while (local_paca->mce_info->mce_ue_count > 0) {
304 index = local_paca->mce_info->mce_ue_count - 1;
305 evt = &local_paca->mce_info->mce_ue_event_queue[index];
306 blocking_notifier_call_chain(&mce_notifier_list, 0, evt);
307 #ifdef CONFIG_MEMORY_FAILURE
309 * This should probably queued elsewhere, but
312 * Don't report this machine check because the caller has a
313 * asked us to ignore the event, it has a fixup handler which
314 * will do the appropriate error handling and reporting.
316 if (evt->error_type == MCE_ERROR_TYPE_UE) {
317 if (evt->u.ue_error.ignore_event) {
318 local_paca->mce_info->mce_ue_count--;
322 if (evt->u.ue_error.physical_address_provided) {
325 pfn = evt->u.ue_error.physical_address >>
327 memory_failure(pfn, 0);
329 pr_warn("Failed to identify bad address from "
330 "where the uncorrectable error (UE) "
334 local_paca->mce_info->mce_ue_count--;
338 * process pending MCE event from the mce event queue. This function will be
339 * called during syscall exit.
341 static void machine_check_process_queued_event(struct irq_work *work)
344 struct machine_check_event *evt;
346 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
349 * For now just print it to console.
350 * TODO: log this error event to FSP or nvram.
352 while (local_paca->mce_info->mce_queue_count > 0) {
353 index = local_paca->mce_info->mce_queue_count - 1;
354 evt = &local_paca->mce_info->mce_event_queue[index];
356 if (evt->error_type == MCE_ERROR_TYPE_UE &&
357 evt->u.ue_error.ignore_event) {
358 local_paca->mce_info->mce_queue_count--;
361 machine_check_print_event_info(evt, false, false);
362 local_paca->mce_info->mce_queue_count--;
366 void machine_check_print_event_info(struct machine_check_event *evt,
367 bool user_mode, bool in_guest)
369 const char *level, *sevstr, *subtype, *err_type, *initiator;
370 uint64_t ea = 0, pa = 0;
374 static const char *mc_ue_types[] = {
377 "Page table walk ifetch",
379 "Page table walk Load/Store",
381 static const char *mc_slb_types[] = {
386 static const char *mc_erat_types[] = {
391 static const char *mc_tlb_types[] = {
396 static const char *mc_user_types[] = {
401 static const char *mc_ra_types[] = {
403 "Instruction fetch (bad)",
404 "Instruction fetch (foreign)",
405 "Page table walk ifetch (bad)",
406 "Page table walk ifetch (foreign)",
409 "Page table walk Load/Store (bad)",
410 "Page table walk Load/Store (foreign)",
411 "Load/Store (foreign)",
413 static const char *mc_link_types[] = {
415 "Instruction fetch (timeout)",
416 "Page table walk ifetch (timeout)",
419 "Page table walk Load/Store (timeout)",
421 static const char *mc_error_class[] = {
424 "Probable Hardware error (some chance of software cause)",
426 "Probable Software error (some chance of hardware cause)",
429 /* Print things out */
430 if (evt->version != MCE_V1) {
431 pr_err("Machine Check Exception, Unknown event version %d !\n",
435 switch (evt->severity) {
436 case MCE_SEV_NO_ERROR:
440 case MCE_SEV_WARNING:
441 level = KERN_WARNING;
455 switch(evt->initiator) {
456 case MCE_INITIATOR_CPU:
459 case MCE_INITIATOR_PCI:
462 case MCE_INITIATOR_ISA:
465 case MCE_INITIATOR_MEMORY:
466 initiator = "Memory";
468 case MCE_INITIATOR_POWERMGM:
469 initiator = "Power Management";
471 case MCE_INITIATOR_UNKNOWN:
473 initiator = "Unknown";
477 switch (evt->error_type) {
478 case MCE_ERROR_TYPE_UE:
480 subtype = evt->u.ue_error.ue_error_type <
481 ARRAY_SIZE(mc_ue_types) ?
482 mc_ue_types[evt->u.ue_error.ue_error_type]
484 if (evt->u.ue_error.effective_address_provided)
485 ea = evt->u.ue_error.effective_address;
486 if (evt->u.ue_error.physical_address_provided)
487 pa = evt->u.ue_error.physical_address;
489 case MCE_ERROR_TYPE_SLB:
491 subtype = evt->u.slb_error.slb_error_type <
492 ARRAY_SIZE(mc_slb_types) ?
493 mc_slb_types[evt->u.slb_error.slb_error_type]
495 if (evt->u.slb_error.effective_address_provided)
496 ea = evt->u.slb_error.effective_address;
498 case MCE_ERROR_TYPE_ERAT:
500 subtype = evt->u.erat_error.erat_error_type <
501 ARRAY_SIZE(mc_erat_types) ?
502 mc_erat_types[evt->u.erat_error.erat_error_type]
504 if (evt->u.erat_error.effective_address_provided)
505 ea = evt->u.erat_error.effective_address;
507 case MCE_ERROR_TYPE_TLB:
509 subtype = evt->u.tlb_error.tlb_error_type <
510 ARRAY_SIZE(mc_tlb_types) ?
511 mc_tlb_types[evt->u.tlb_error.tlb_error_type]
513 if (evt->u.tlb_error.effective_address_provided)
514 ea = evt->u.tlb_error.effective_address;
516 case MCE_ERROR_TYPE_USER:
518 subtype = evt->u.user_error.user_error_type <
519 ARRAY_SIZE(mc_user_types) ?
520 mc_user_types[evt->u.user_error.user_error_type]
522 if (evt->u.user_error.effective_address_provided)
523 ea = evt->u.user_error.effective_address;
525 case MCE_ERROR_TYPE_RA:
526 err_type = "Real address";
527 subtype = evt->u.ra_error.ra_error_type <
528 ARRAY_SIZE(mc_ra_types) ?
529 mc_ra_types[evt->u.ra_error.ra_error_type]
531 if (evt->u.ra_error.effective_address_provided)
532 ea = evt->u.ra_error.effective_address;
534 case MCE_ERROR_TYPE_LINK:
536 subtype = evt->u.link_error.link_error_type <
537 ARRAY_SIZE(mc_link_types) ?
538 mc_link_types[evt->u.link_error.link_error_type]
540 if (evt->u.link_error.effective_address_provided)
541 ea = evt->u.link_error.effective_address;
543 case MCE_ERROR_TYPE_DCACHE:
544 err_type = "D-Cache";
547 case MCE_ERROR_TYPE_ICACHE:
548 err_type = "I-Cache";
552 case MCE_ERROR_TYPE_UNKNOWN:
553 err_type = "Unknown";
558 dar_str[0] = pa_str[0] = '\0';
559 if (ea && evt->srr0 != ea) {
560 /* Load/Store address */
561 n = sprintf(dar_str, "DAR: %016llx ", ea);
563 sprintf(dar_str + n, "paddr: %016llx ", pa);
565 sprintf(pa_str, " paddr: %016llx", pa);
568 printk("%sMCE: CPU%d: machine check (%s) %s %s %s %s[%s]\n",
569 level, evt->cpu, sevstr, in_guest ? "Guest" : "",
570 err_type, subtype, dar_str,
571 evt->disposition == MCE_DISPOSITION_RECOVERED ?
572 "Recovered" : "Not recovered");
574 if (in_guest || user_mode) {
575 printk("%sMCE: CPU%d: PID: %d Comm: %s %sNIP: [%016llx]%s\n",
576 level, evt->cpu, current->pid, current->comm,
577 in_guest ? "Guest " : "", evt->srr0, pa_str);
579 printk("%sMCE: CPU%d: NIP: [%016llx] %pS%s\n",
580 level, evt->cpu, evt->srr0, (void *)evt->srr0, pa_str);
583 printk("%sMCE: CPU%d: Initiator %s\n", level, evt->cpu, initiator);
585 subtype = evt->error_class < ARRAY_SIZE(mc_error_class) ?
586 mc_error_class[evt->error_class] : "Unknown";
587 printk("%sMCE: CPU%d: %s\n", level, evt->cpu, subtype);
589 #ifdef CONFIG_PPC_BOOK3S_64
590 /* Display faulty slb contents for SLB errors. */
591 if (evt->error_type == MCE_ERROR_TYPE_SLB && !in_guest)
592 slb_dump_contents(local_paca->mce_faulty_slbs);
595 EXPORT_SYMBOL_GPL(machine_check_print_event_info);
598 * This function is called in real mode. Strictly no printk's please.
600 * regs->nip and regs->msr contains srr0 and ssr1.
602 DEFINE_INTERRUPT_HANDLER_NMI(machine_check_early)
606 hv_nmi_check_nonrecoverable(regs);
609 * See if platform is capable of handling machine check.
611 if (ppc_md.machine_check_early)
612 handled = ppc_md.machine_check_early(regs);
617 /* Possible meanings for HMER_DEBUG_TRIG bit being set on POWER9 */
620 DTRIG_VECTOR_CI, /* need to emulate vector CI load instr */
621 DTRIG_SUSPEND_ESCAPE, /* need to escape from TM suspend mode */
622 } hmer_debug_trig_function;
624 static int init_debug_trig_function(void)
627 struct device_node *cpun;
628 struct property *prop = NULL;
631 /* First look in the device tree */
633 cpun = of_get_cpu_node(smp_processor_id(), NULL);
635 of_property_for_each_string(cpun, "ibm,hmi-special-triggers",
637 if (strcmp(str, "bit17-vector-ci-load") == 0)
638 hmer_debug_trig_function = DTRIG_VECTOR_CI;
639 else if (strcmp(str, "bit17-tm-suspend-escape") == 0)
640 hmer_debug_trig_function = DTRIG_SUSPEND_ESCAPE;
646 /* If we found the property, don't look at PVR */
650 pvr = mfspr(SPRN_PVR);
651 /* Check for POWER9 Nimbus (scale-out) */
652 if ((PVR_VER(pvr) == PVR_POWER9) && (pvr & 0xe000) == 0) {
653 /* DD2.2 and later */
654 if ((pvr & 0xfff) >= 0x202)
655 hmer_debug_trig_function = DTRIG_SUSPEND_ESCAPE;
656 /* DD2.0 and DD2.1 - used for vector CI load emulation */
657 else if ((pvr & 0xfff) >= 0x200)
658 hmer_debug_trig_function = DTRIG_VECTOR_CI;
662 switch (hmer_debug_trig_function) {
663 case DTRIG_VECTOR_CI:
664 pr_debug("HMI debug trigger used for vector CI load\n");
666 case DTRIG_SUSPEND_ESCAPE:
667 pr_debug("HMI debug trigger used for TM suspend escape\n");
674 __initcall(init_debug_trig_function);
677 * Handle HMIs that occur as a result of a debug trigger.
679 * -1 means this is not a HMI cause that we know about
680 * 0 means no further handling is required
681 * 1 means further handling is required
683 long hmi_handle_debugtrig(struct pt_regs *regs)
685 unsigned long hmer = mfspr(SPRN_HMER);
688 /* HMER_DEBUG_TRIG bit is used for various workarounds on P9 */
689 if (!((hmer & HMER_DEBUG_TRIG)
690 && hmer_debug_trig_function != DTRIG_UNKNOWN))
693 hmer &= ~HMER_DEBUG_TRIG;
694 /* HMER is a write-AND register */
695 mtspr(SPRN_HMER, ~HMER_DEBUG_TRIG);
697 switch (hmer_debug_trig_function) {
698 case DTRIG_VECTOR_CI:
700 * Now to avoid problems with soft-disable we
701 * only do the emulation if we are coming from
704 if (regs && user_mode(regs))
705 ret = local_paca->hmi_p9_special_emu = 1;
714 * See if any other HMI causes remain to be handled
716 if (hmer & mfspr(SPRN_HMEER))
725 DEFINE_INTERRUPT_HANDLER_NMI(hmi_exception_realmode)
729 local_paca->hmi_irqs++;
731 ret = hmi_handle_debugtrig(regs);
735 wait_for_subcore_guest_exit();
737 if (ppc_md.hmi_exception_early)
738 ppc_md.hmi_exception_early(regs);
740 wait_for_tb_resync();
745 void __init mce_init(void)
747 struct mce_info *mce_info;
751 limit = min(ppc64_bolted_size(), ppc64_rma_size);
752 for_each_possible_cpu(i) {
753 mce_info = memblock_alloc_try_nid(sizeof(*mce_info),
754 __alignof__(*mce_info),
756 limit, cpu_to_node(i));
759 paca_ptrs[i]->mce_info = mce_info;
763 panic("Failed to allocate memory for MCE event data\n");