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>
22 #include <asm/interrupt.h>
23 #include <asm/machdep.h>
26 #include <asm/asm-prototypes.h>
30 static void machine_check_process_queued_event(struct irq_work *work);
31 static void machine_check_ue_irq_work(struct irq_work *work);
32 static void machine_check_ue_event(struct machine_check_event *evt);
33 static void machine_process_ue_event(struct work_struct *work);
35 static struct irq_work mce_event_process_work = {
36 .func = machine_check_process_queued_event,
39 static struct irq_work mce_ue_event_irq_work = {
40 .func = machine_check_ue_irq_work,
43 static DECLARE_WORK(mce_ue_event_work, machine_process_ue_event);
45 static BLOCKING_NOTIFIER_HEAD(mce_notifier_list);
47 int mce_register_notifier(struct notifier_block *nb)
49 return blocking_notifier_chain_register(&mce_notifier_list, nb);
51 EXPORT_SYMBOL_GPL(mce_register_notifier);
53 int mce_unregister_notifier(struct notifier_block *nb)
55 return blocking_notifier_chain_unregister(&mce_notifier_list, nb);
57 EXPORT_SYMBOL_GPL(mce_unregister_notifier);
59 static void mce_set_error_info(struct machine_check_event *mce,
60 struct mce_error_info *mce_err)
62 mce->error_type = mce_err->error_type;
63 switch (mce_err->error_type) {
64 case MCE_ERROR_TYPE_UE:
65 mce->u.ue_error.ue_error_type = mce_err->u.ue_error_type;
67 case MCE_ERROR_TYPE_SLB:
68 mce->u.slb_error.slb_error_type = mce_err->u.slb_error_type;
70 case MCE_ERROR_TYPE_ERAT:
71 mce->u.erat_error.erat_error_type = mce_err->u.erat_error_type;
73 case MCE_ERROR_TYPE_TLB:
74 mce->u.tlb_error.tlb_error_type = mce_err->u.tlb_error_type;
76 case MCE_ERROR_TYPE_USER:
77 mce->u.user_error.user_error_type = mce_err->u.user_error_type;
79 case MCE_ERROR_TYPE_RA:
80 mce->u.ra_error.ra_error_type = mce_err->u.ra_error_type;
82 case MCE_ERROR_TYPE_LINK:
83 mce->u.link_error.link_error_type = mce_err->u.link_error_type;
85 case MCE_ERROR_TYPE_UNKNOWN:
92 * Decode and save high level MCE information into per cpu buffer which
93 * is an array of machine_check_event structure.
95 void save_mce_event(struct pt_regs *regs, long handled,
96 struct mce_error_info *mce_err,
97 uint64_t nip, uint64_t addr, uint64_t phys_addr)
99 int index = local_paca->mce_info->mce_nest_count++;
100 struct machine_check_event *mce;
102 mce = &local_paca->mce_info->mce_event[index];
104 * Return if we don't have enough space to log mce event.
105 * mce_nest_count may go beyond MAX_MC_EVT but that's ok,
106 * the check below will stop buffer overrun.
108 if (index >= MAX_MC_EVT)
111 /* Populate generic machine check info */
112 mce->version = MCE_V1;
114 mce->srr1 = regs->msr;
115 mce->gpr3 = regs->gpr[3];
117 mce->cpu = get_paca()->paca_index;
119 /* Mark it recovered if we have handled it and MSR(RI=1). */
120 if (handled && (regs->msr & MSR_RI))
121 mce->disposition = MCE_DISPOSITION_RECOVERED;
123 mce->disposition = MCE_DISPOSITION_NOT_RECOVERED;
125 mce->initiator = mce_err->initiator;
126 mce->severity = mce_err->severity;
127 mce->sync_error = mce_err->sync_error;
128 mce->error_class = mce_err->error_class;
131 * Populate the mce error_type and type-specific error_type.
133 mce_set_error_info(mce, mce_err);
134 if (mce->error_type == MCE_ERROR_TYPE_UE)
135 mce->u.ue_error.ignore_event = mce_err->ignore_event;
140 if (mce->error_type == MCE_ERROR_TYPE_TLB) {
141 mce->u.tlb_error.effective_address_provided = true;
142 mce->u.tlb_error.effective_address = addr;
143 } else if (mce->error_type == MCE_ERROR_TYPE_SLB) {
144 mce->u.slb_error.effective_address_provided = true;
145 mce->u.slb_error.effective_address = addr;
146 } else if (mce->error_type == MCE_ERROR_TYPE_ERAT) {
147 mce->u.erat_error.effective_address_provided = true;
148 mce->u.erat_error.effective_address = addr;
149 } else if (mce->error_type == MCE_ERROR_TYPE_USER) {
150 mce->u.user_error.effective_address_provided = true;
151 mce->u.user_error.effective_address = addr;
152 } else if (mce->error_type == MCE_ERROR_TYPE_RA) {
153 mce->u.ra_error.effective_address_provided = true;
154 mce->u.ra_error.effective_address = addr;
155 } else if (mce->error_type == MCE_ERROR_TYPE_LINK) {
156 mce->u.link_error.effective_address_provided = true;
157 mce->u.link_error.effective_address = addr;
158 } else if (mce->error_type == MCE_ERROR_TYPE_UE) {
159 mce->u.ue_error.effective_address_provided = true;
160 mce->u.ue_error.effective_address = addr;
161 if (phys_addr != ULONG_MAX) {
162 mce->u.ue_error.physical_address_provided = true;
163 mce->u.ue_error.physical_address = phys_addr;
164 machine_check_ue_event(mce);
172 * mce Pointer to machine_check_event structure to be filled.
173 * release Flag to indicate whether to free the event slot or not.
174 * 0 <= do not release the mce event. Caller will invoke
175 * release_mce_event() once event has been consumed.
176 * 1 <= release the slot.
181 * get_mce_event() will be called by platform specific machine check
182 * handle routine and in KVM.
183 * When we call get_mce_event(), we are still in interrupt context and
184 * preemption will not be scheduled until ret_from_expect() routine
187 int get_mce_event(struct machine_check_event *mce, bool release)
189 int index = local_paca->mce_info->mce_nest_count - 1;
190 struct machine_check_event *mc_evt;
197 /* Check if we have MCE info to process. */
198 if (index < MAX_MC_EVT) {
199 mc_evt = &local_paca->mce_info->mce_event[index];
200 /* Copy the event structure and release the original */
207 /* Decrement the count to free the slot. */
209 local_paca->mce_info->mce_nest_count--;
214 void release_mce_event(void)
216 get_mce_event(NULL, true);
219 static void machine_check_ue_irq_work(struct irq_work *work)
221 schedule_work(&mce_ue_event_work);
225 * Queue up the MCE event which then can be handled later.
227 static void machine_check_ue_event(struct machine_check_event *evt)
231 index = local_paca->mce_info->mce_ue_count++;
232 /* If queue is full, just return for now. */
233 if (index >= MAX_MC_EVT) {
234 local_paca->mce_info->mce_ue_count--;
237 memcpy(&local_paca->mce_info->mce_ue_event_queue[index],
240 /* Queue work to process this event later. */
241 irq_work_queue(&mce_ue_event_irq_work);
245 * Queue up the MCE event which then can be handled later.
247 void machine_check_queue_event(void)
250 struct machine_check_event evt;
252 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
255 index = local_paca->mce_info->mce_queue_count++;
256 /* If queue is full, just return for now. */
257 if (index >= MAX_MC_EVT) {
258 local_paca->mce_info->mce_queue_count--;
261 memcpy(&local_paca->mce_info->mce_event_queue[index],
264 /* Queue irq work to process this event later. */
265 irq_work_queue(&mce_event_process_work);
268 void mce_common_process_ue(struct pt_regs *regs,
269 struct mce_error_info *mce_err)
271 const struct exception_table_entry *entry;
273 entry = search_kernel_exception_table(regs->nip);
275 mce_err->ignore_event = true;
276 regs->nip = extable_fixup(entry);
281 * process pending MCE event from the mce event queue. This function will be
282 * called during syscall exit.
284 static void machine_process_ue_event(struct work_struct *work)
287 struct machine_check_event *evt;
289 while (local_paca->mce_info->mce_ue_count > 0) {
290 index = local_paca->mce_info->mce_ue_count - 1;
291 evt = &local_paca->mce_info->mce_ue_event_queue[index];
292 blocking_notifier_call_chain(&mce_notifier_list, 0, evt);
293 #ifdef CONFIG_MEMORY_FAILURE
295 * This should probably queued elsewhere, but
298 * Don't report this machine check because the caller has a
299 * asked us to ignore the event, it has a fixup handler which
300 * will do the appropriate error handling and reporting.
302 if (evt->error_type == MCE_ERROR_TYPE_UE) {
303 if (evt->u.ue_error.ignore_event) {
304 local_paca->mce_info->mce_ue_count--;
308 if (evt->u.ue_error.physical_address_provided) {
311 pfn = evt->u.ue_error.physical_address >>
313 memory_failure(pfn, 0);
315 pr_warn("Failed to identify bad address from "
316 "where the uncorrectable error (UE) "
320 local_paca->mce_info->mce_ue_count--;
324 * process pending MCE event from the mce event queue. This function will be
325 * called during syscall exit.
327 static void machine_check_process_queued_event(struct irq_work *work)
330 struct machine_check_event *evt;
332 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
335 * For now just print it to console.
336 * TODO: log this error event to FSP or nvram.
338 while (local_paca->mce_info->mce_queue_count > 0) {
339 index = local_paca->mce_info->mce_queue_count - 1;
340 evt = &local_paca->mce_info->mce_event_queue[index];
342 if (evt->error_type == MCE_ERROR_TYPE_UE &&
343 evt->u.ue_error.ignore_event) {
344 local_paca->mce_info->mce_queue_count--;
347 machine_check_print_event_info(evt, false, false);
348 local_paca->mce_info->mce_queue_count--;
352 void machine_check_print_event_info(struct machine_check_event *evt,
353 bool user_mode, bool in_guest)
355 const char *level, *sevstr, *subtype, *err_type, *initiator;
356 uint64_t ea = 0, pa = 0;
360 static const char *mc_ue_types[] = {
363 "Page table walk ifetch",
365 "Page table walk Load/Store",
367 static const char *mc_slb_types[] = {
372 static const char *mc_erat_types[] = {
377 static const char *mc_tlb_types[] = {
382 static const char *mc_user_types[] = {
387 static const char *mc_ra_types[] = {
389 "Instruction fetch (bad)",
390 "Instruction fetch (foreign)",
391 "Page table walk ifetch (bad)",
392 "Page table walk ifetch (foreign)",
395 "Page table walk Load/Store (bad)",
396 "Page table walk Load/Store (foreign)",
397 "Load/Store (foreign)",
399 static const char *mc_link_types[] = {
401 "Instruction fetch (timeout)",
402 "Page table walk ifetch (timeout)",
405 "Page table walk Load/Store (timeout)",
407 static const char *mc_error_class[] = {
410 "Probable Hardware error (some chance of software cause)",
412 "Probable Software error (some chance of hardware cause)",
415 /* Print things out */
416 if (evt->version != MCE_V1) {
417 pr_err("Machine Check Exception, Unknown event version %d !\n",
421 switch (evt->severity) {
422 case MCE_SEV_NO_ERROR:
426 case MCE_SEV_WARNING:
427 level = KERN_WARNING;
441 switch(evt->initiator) {
442 case MCE_INITIATOR_CPU:
445 case MCE_INITIATOR_PCI:
448 case MCE_INITIATOR_ISA:
451 case MCE_INITIATOR_MEMORY:
452 initiator = "Memory";
454 case MCE_INITIATOR_POWERMGM:
455 initiator = "Power Management";
457 case MCE_INITIATOR_UNKNOWN:
459 initiator = "Unknown";
463 switch (evt->error_type) {
464 case MCE_ERROR_TYPE_UE:
466 subtype = evt->u.ue_error.ue_error_type <
467 ARRAY_SIZE(mc_ue_types) ?
468 mc_ue_types[evt->u.ue_error.ue_error_type]
470 if (evt->u.ue_error.effective_address_provided)
471 ea = evt->u.ue_error.effective_address;
472 if (evt->u.ue_error.physical_address_provided)
473 pa = evt->u.ue_error.physical_address;
475 case MCE_ERROR_TYPE_SLB:
477 subtype = evt->u.slb_error.slb_error_type <
478 ARRAY_SIZE(mc_slb_types) ?
479 mc_slb_types[evt->u.slb_error.slb_error_type]
481 if (evt->u.slb_error.effective_address_provided)
482 ea = evt->u.slb_error.effective_address;
484 case MCE_ERROR_TYPE_ERAT:
486 subtype = evt->u.erat_error.erat_error_type <
487 ARRAY_SIZE(mc_erat_types) ?
488 mc_erat_types[evt->u.erat_error.erat_error_type]
490 if (evt->u.erat_error.effective_address_provided)
491 ea = evt->u.erat_error.effective_address;
493 case MCE_ERROR_TYPE_TLB:
495 subtype = evt->u.tlb_error.tlb_error_type <
496 ARRAY_SIZE(mc_tlb_types) ?
497 mc_tlb_types[evt->u.tlb_error.tlb_error_type]
499 if (evt->u.tlb_error.effective_address_provided)
500 ea = evt->u.tlb_error.effective_address;
502 case MCE_ERROR_TYPE_USER:
504 subtype = evt->u.user_error.user_error_type <
505 ARRAY_SIZE(mc_user_types) ?
506 mc_user_types[evt->u.user_error.user_error_type]
508 if (evt->u.user_error.effective_address_provided)
509 ea = evt->u.user_error.effective_address;
511 case MCE_ERROR_TYPE_RA:
512 err_type = "Real address";
513 subtype = evt->u.ra_error.ra_error_type <
514 ARRAY_SIZE(mc_ra_types) ?
515 mc_ra_types[evt->u.ra_error.ra_error_type]
517 if (evt->u.ra_error.effective_address_provided)
518 ea = evt->u.ra_error.effective_address;
520 case MCE_ERROR_TYPE_LINK:
522 subtype = evt->u.link_error.link_error_type <
523 ARRAY_SIZE(mc_link_types) ?
524 mc_link_types[evt->u.link_error.link_error_type]
526 if (evt->u.link_error.effective_address_provided)
527 ea = evt->u.link_error.effective_address;
529 case MCE_ERROR_TYPE_DCACHE:
530 err_type = "D-Cache";
533 case MCE_ERROR_TYPE_ICACHE:
534 err_type = "I-Cache";
538 case MCE_ERROR_TYPE_UNKNOWN:
539 err_type = "Unknown";
544 dar_str[0] = pa_str[0] = '\0';
545 if (ea && evt->srr0 != ea) {
546 /* Load/Store address */
547 n = sprintf(dar_str, "DAR: %016llx ", ea);
549 sprintf(dar_str + n, "paddr: %016llx ", pa);
551 sprintf(pa_str, " paddr: %016llx", pa);
554 printk("%sMCE: CPU%d: machine check (%s) %s %s %s %s[%s]\n",
555 level, evt->cpu, sevstr, in_guest ? "Guest" : "",
556 err_type, subtype, dar_str,
557 evt->disposition == MCE_DISPOSITION_RECOVERED ?
558 "Recovered" : "Not recovered");
560 if (in_guest || user_mode) {
561 printk("%sMCE: CPU%d: PID: %d Comm: %s %sNIP: [%016llx]%s\n",
562 level, evt->cpu, current->pid, current->comm,
563 in_guest ? "Guest " : "", evt->srr0, pa_str);
565 printk("%sMCE: CPU%d: NIP: [%016llx] %pS%s\n",
566 level, evt->cpu, evt->srr0, (void *)evt->srr0, pa_str);
569 printk("%sMCE: CPU%d: Initiator %s\n", level, evt->cpu, initiator);
571 subtype = evt->error_class < ARRAY_SIZE(mc_error_class) ?
572 mc_error_class[evt->error_class] : "Unknown";
573 printk("%sMCE: CPU%d: %s\n", level, evt->cpu, subtype);
575 #ifdef CONFIG_PPC_BOOK3S_64
576 /* Display faulty slb contents for SLB errors. */
577 if (evt->error_type == MCE_ERROR_TYPE_SLB && !in_guest)
578 slb_dump_contents(local_paca->mce_faulty_slbs);
581 EXPORT_SYMBOL_GPL(machine_check_print_event_info);
584 * This function is called in real mode. Strictly no printk's please.
586 * regs->nip and regs->msr contains srr0 and ssr1.
588 DEFINE_INTERRUPT_HANDLER_NMI(machine_check_early)
592 hv_nmi_check_nonrecoverable(regs);
595 * See if platform is capable of handling machine check.
597 if (ppc_md.machine_check_early)
598 handled = ppc_md.machine_check_early(regs);
603 /* Possible meanings for HMER_DEBUG_TRIG bit being set on POWER9 */
606 DTRIG_VECTOR_CI, /* need to emulate vector CI load instr */
607 DTRIG_SUSPEND_ESCAPE, /* need to escape from TM suspend mode */
608 } hmer_debug_trig_function;
610 static int init_debug_trig_function(void)
613 struct device_node *cpun;
614 struct property *prop = NULL;
617 /* First look in the device tree */
619 cpun = of_get_cpu_node(smp_processor_id(), NULL);
621 of_property_for_each_string(cpun, "ibm,hmi-special-triggers",
623 if (strcmp(str, "bit17-vector-ci-load") == 0)
624 hmer_debug_trig_function = DTRIG_VECTOR_CI;
625 else if (strcmp(str, "bit17-tm-suspend-escape") == 0)
626 hmer_debug_trig_function = DTRIG_SUSPEND_ESCAPE;
632 /* If we found the property, don't look at PVR */
636 pvr = mfspr(SPRN_PVR);
637 /* Check for POWER9 Nimbus (scale-out) */
638 if ((PVR_VER(pvr) == PVR_POWER9) && (pvr & 0xe000) == 0) {
639 /* DD2.2 and later */
640 if ((pvr & 0xfff) >= 0x202)
641 hmer_debug_trig_function = DTRIG_SUSPEND_ESCAPE;
642 /* DD2.0 and DD2.1 - used for vector CI load emulation */
643 else if ((pvr & 0xfff) >= 0x200)
644 hmer_debug_trig_function = DTRIG_VECTOR_CI;
648 switch (hmer_debug_trig_function) {
649 case DTRIG_VECTOR_CI:
650 pr_debug("HMI debug trigger used for vector CI load\n");
652 case DTRIG_SUSPEND_ESCAPE:
653 pr_debug("HMI debug trigger used for TM suspend escape\n");
660 __initcall(init_debug_trig_function);
663 * Handle HMIs that occur as a result of a debug trigger.
665 * -1 means this is not a HMI cause that we know about
666 * 0 means no further handling is required
667 * 1 means further handling is required
669 long hmi_handle_debugtrig(struct pt_regs *regs)
671 unsigned long hmer = mfspr(SPRN_HMER);
674 /* HMER_DEBUG_TRIG bit is used for various workarounds on P9 */
675 if (!((hmer & HMER_DEBUG_TRIG)
676 && hmer_debug_trig_function != DTRIG_UNKNOWN))
679 hmer &= ~HMER_DEBUG_TRIG;
680 /* HMER is a write-AND register */
681 mtspr(SPRN_HMER, ~HMER_DEBUG_TRIG);
683 switch (hmer_debug_trig_function) {
684 case DTRIG_VECTOR_CI:
686 * Now to avoid problems with soft-disable we
687 * only do the emulation if we are coming from
690 if (regs && user_mode(regs))
691 ret = local_paca->hmi_p9_special_emu = 1;
700 * See if any other HMI causes remain to be handled
702 if (hmer & mfspr(SPRN_HMEER))
711 DEFINE_INTERRUPT_HANDLER_NMI(hmi_exception_realmode)
715 local_paca->hmi_irqs++;
717 ret = hmi_handle_debugtrig(regs);
721 wait_for_subcore_guest_exit();
723 if (ppc_md.hmi_exception_early)
724 ppc_md.hmi_exception_early(regs);
726 wait_for_tb_resync();
731 void __init mce_init(void)
733 struct mce_info *mce_info;
737 limit = min(ppc64_bolted_size(), ppc64_rma_size);
738 for_each_possible_cpu(i) {
739 mce_info = memblock_alloc_try_nid(sizeof(*mce_info),
740 __alignof__(*mce_info),
742 limit, cpu_to_node(i));
745 paca_ptrs[i]->mce_info = mce_info;
749 panic("Failed to allocate memory for MCE event data\n");