1*d9953105SMichael Ellerman /* 2*d9953105SMichael Ellerman * Copyright (C) 2001 Dave Engebretsen IBM Corporation 3*d9953105SMichael Ellerman * 4*d9953105SMichael Ellerman * This program is free software; you can redistribute it and/or modify 5*d9953105SMichael Ellerman * it under the terms of the GNU General Public License as published by 6*d9953105SMichael Ellerman * the Free Software Foundation; either version 2 of the License, or 7*d9953105SMichael Ellerman * (at your option) any later version. 8*d9953105SMichael Ellerman * 9*d9953105SMichael Ellerman * This program is distributed in the hope that it will be useful, 10*d9953105SMichael Ellerman * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*d9953105SMichael Ellerman * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*d9953105SMichael Ellerman * GNU General Public License for more details. 13*d9953105SMichael Ellerman * 14*d9953105SMichael Ellerman * You should have received a copy of the GNU General Public License 15*d9953105SMichael Ellerman * along with this program; if not, write to the Free Software 16*d9953105SMichael Ellerman * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17*d9953105SMichael Ellerman */ 18*d9953105SMichael Ellerman 19*d9953105SMichael Ellerman /* Change Activity: 20*d9953105SMichael Ellerman * 2001/09/21 : engebret : Created with minimal EPOW and HW exception support. 21*d9953105SMichael Ellerman * End Change Activity 22*d9953105SMichael Ellerman */ 23*d9953105SMichael Ellerman 24*d9953105SMichael Ellerman #include <linux/errno.h> 25*d9953105SMichael Ellerman #include <linux/threads.h> 26*d9953105SMichael Ellerman #include <linux/kernel_stat.h> 27*d9953105SMichael Ellerman #include <linux/signal.h> 28*d9953105SMichael Ellerman #include <linux/sched.h> 29*d9953105SMichael Ellerman #include <linux/ioport.h> 30*d9953105SMichael Ellerman #include <linux/interrupt.h> 31*d9953105SMichael Ellerman #include <linux/timex.h> 32*d9953105SMichael Ellerman #include <linux/init.h> 33*d9953105SMichael Ellerman #include <linux/slab.h> 34*d9953105SMichael Ellerman #include <linux/pci.h> 35*d9953105SMichael Ellerman #include <linux/delay.h> 36*d9953105SMichael Ellerman #include <linux/irq.h> 37*d9953105SMichael Ellerman #include <linux/random.h> 38*d9953105SMichael Ellerman #include <linux/sysrq.h> 39*d9953105SMichael Ellerman #include <linux/bitops.h> 40*d9953105SMichael Ellerman 41*d9953105SMichael Ellerman #include <asm/uaccess.h> 42*d9953105SMichael Ellerman #include <asm/system.h> 43*d9953105SMichael Ellerman #include <asm/io.h> 44*d9953105SMichael Ellerman #include <asm/pgtable.h> 45*d9953105SMichael Ellerman #include <asm/irq.h> 46*d9953105SMichael Ellerman #include <asm/cache.h> 47*d9953105SMichael Ellerman #include <asm/prom.h> 48*d9953105SMichael Ellerman #include <asm/ptrace.h> 49*d9953105SMichael Ellerman #include <asm/machdep.h> 50*d9953105SMichael Ellerman #include <asm/rtas.h> 51*d9953105SMichael Ellerman #include <asm/ppcdebug.h> 52*d9953105SMichael Ellerman 53*d9953105SMichael Ellerman static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX]; 54*d9953105SMichael Ellerman static DEFINE_SPINLOCK(ras_log_buf_lock); 55*d9953105SMichael Ellerman 56*d9953105SMichael Ellerman char mce_data_buf[RTAS_ERROR_LOG_MAX] 57*d9953105SMichael Ellerman ; 58*d9953105SMichael Ellerman /* This is true if we are using the firmware NMI handler (typically LPAR) */ 59*d9953105SMichael Ellerman extern int fwnmi_active; 60*d9953105SMichael Ellerman 61*d9953105SMichael Ellerman static int ras_get_sensor_state_token; 62*d9953105SMichael Ellerman static int ras_check_exception_token; 63*d9953105SMichael Ellerman 64*d9953105SMichael Ellerman #define EPOW_SENSOR_TOKEN 9 65*d9953105SMichael Ellerman #define EPOW_SENSOR_INDEX 0 66*d9953105SMichael Ellerman #define RAS_VECTOR_OFFSET 0x500 67*d9953105SMichael Ellerman 68*d9953105SMichael Ellerman static irqreturn_t ras_epow_interrupt(int irq, void *dev_id, 69*d9953105SMichael Ellerman struct pt_regs * regs); 70*d9953105SMichael Ellerman static irqreturn_t ras_error_interrupt(int irq, void *dev_id, 71*d9953105SMichael Ellerman struct pt_regs * regs); 72*d9953105SMichael Ellerman 73*d9953105SMichael Ellerman /* #define DEBUG */ 74*d9953105SMichael Ellerman 75*d9953105SMichael Ellerman static void request_ras_irqs(struct device_node *np, char *propname, 76*d9953105SMichael Ellerman irqreturn_t (*handler)(int, void *, struct pt_regs *), 77*d9953105SMichael Ellerman const char *name) 78*d9953105SMichael Ellerman { 79*d9953105SMichael Ellerman unsigned int *ireg, len, i; 80*d9953105SMichael Ellerman int virq, n_intr; 81*d9953105SMichael Ellerman 82*d9953105SMichael Ellerman ireg = (unsigned int *)get_property(np, propname, &len); 83*d9953105SMichael Ellerman if (ireg == NULL) 84*d9953105SMichael Ellerman return; 85*d9953105SMichael Ellerman n_intr = prom_n_intr_cells(np); 86*d9953105SMichael Ellerman len /= n_intr * sizeof(*ireg); 87*d9953105SMichael Ellerman 88*d9953105SMichael Ellerman for (i = 0; i < len; i++) { 89*d9953105SMichael Ellerman virq = virt_irq_create_mapping(*ireg); 90*d9953105SMichael Ellerman if (virq == NO_IRQ) { 91*d9953105SMichael Ellerman printk(KERN_ERR "Unable to allocate interrupt " 92*d9953105SMichael Ellerman "number for %s\n", np->full_name); 93*d9953105SMichael Ellerman return; 94*d9953105SMichael Ellerman } 95*d9953105SMichael Ellerman if (request_irq(irq_offset_up(virq), handler, 0, name, NULL)) { 96*d9953105SMichael Ellerman printk(KERN_ERR "Unable to request interrupt %d for " 97*d9953105SMichael Ellerman "%s\n", irq_offset_up(virq), np->full_name); 98*d9953105SMichael Ellerman return; 99*d9953105SMichael Ellerman } 100*d9953105SMichael Ellerman ireg += n_intr; 101*d9953105SMichael Ellerman } 102*d9953105SMichael Ellerman } 103*d9953105SMichael Ellerman 104*d9953105SMichael Ellerman /* 105*d9953105SMichael Ellerman * Initialize handlers for the set of interrupts caused by hardware errors 106*d9953105SMichael Ellerman * and power system events. 107*d9953105SMichael Ellerman */ 108*d9953105SMichael Ellerman static int __init init_ras_IRQ(void) 109*d9953105SMichael Ellerman { 110*d9953105SMichael Ellerman struct device_node *np; 111*d9953105SMichael Ellerman 112*d9953105SMichael Ellerman ras_get_sensor_state_token = rtas_token("get-sensor-state"); 113*d9953105SMichael Ellerman ras_check_exception_token = rtas_token("check-exception"); 114*d9953105SMichael Ellerman 115*d9953105SMichael Ellerman /* Internal Errors */ 116*d9953105SMichael Ellerman np = of_find_node_by_path("/event-sources/internal-errors"); 117*d9953105SMichael Ellerman if (np != NULL) { 118*d9953105SMichael Ellerman request_ras_irqs(np, "open-pic-interrupt", ras_error_interrupt, 119*d9953105SMichael Ellerman "RAS_ERROR"); 120*d9953105SMichael Ellerman request_ras_irqs(np, "interrupts", ras_error_interrupt, 121*d9953105SMichael Ellerman "RAS_ERROR"); 122*d9953105SMichael Ellerman of_node_put(np); 123*d9953105SMichael Ellerman } 124*d9953105SMichael Ellerman 125*d9953105SMichael Ellerman /* EPOW Events */ 126*d9953105SMichael Ellerman np = of_find_node_by_path("/event-sources/epow-events"); 127*d9953105SMichael Ellerman if (np != NULL) { 128*d9953105SMichael Ellerman request_ras_irqs(np, "open-pic-interrupt", ras_epow_interrupt, 129*d9953105SMichael Ellerman "RAS_EPOW"); 130*d9953105SMichael Ellerman request_ras_irqs(np, "interrupts", ras_epow_interrupt, 131*d9953105SMichael Ellerman "RAS_EPOW"); 132*d9953105SMichael Ellerman of_node_put(np); 133*d9953105SMichael Ellerman } 134*d9953105SMichael Ellerman 135*d9953105SMichael Ellerman return 1; 136*d9953105SMichael Ellerman } 137*d9953105SMichael Ellerman __initcall(init_ras_IRQ); 138*d9953105SMichael Ellerman 139*d9953105SMichael Ellerman /* 140*d9953105SMichael Ellerman * Handle power subsystem events (EPOW). 141*d9953105SMichael Ellerman * 142*d9953105SMichael Ellerman * Presently we just log the event has occurred. This should be fixed 143*d9953105SMichael Ellerman * to examine the type of power failure and take appropriate action where 144*d9953105SMichael Ellerman * the time horizon permits something useful to be done. 145*d9953105SMichael Ellerman */ 146*d9953105SMichael Ellerman static irqreturn_t 147*d9953105SMichael Ellerman ras_epow_interrupt(int irq, void *dev_id, struct pt_regs * regs) 148*d9953105SMichael Ellerman { 149*d9953105SMichael Ellerman int status = 0xdeadbeef; 150*d9953105SMichael Ellerman int state = 0; 151*d9953105SMichael Ellerman int critical; 152*d9953105SMichael Ellerman 153*d9953105SMichael Ellerman status = rtas_call(ras_get_sensor_state_token, 2, 2, &state, 154*d9953105SMichael Ellerman EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX); 155*d9953105SMichael Ellerman 156*d9953105SMichael Ellerman if (state > 3) 157*d9953105SMichael Ellerman critical = 1; /* Time Critical */ 158*d9953105SMichael Ellerman else 159*d9953105SMichael Ellerman critical = 0; 160*d9953105SMichael Ellerman 161*d9953105SMichael Ellerman spin_lock(&ras_log_buf_lock); 162*d9953105SMichael Ellerman 163*d9953105SMichael Ellerman status = rtas_call(ras_check_exception_token, 6, 1, NULL, 164*d9953105SMichael Ellerman RAS_VECTOR_OFFSET, 165*d9953105SMichael Ellerman virt_irq_to_real(irq_offset_down(irq)), 166*d9953105SMichael Ellerman RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS, 167*d9953105SMichael Ellerman critical, __pa(&ras_log_buf), 168*d9953105SMichael Ellerman rtas_get_error_log_max()); 169*d9953105SMichael Ellerman 170*d9953105SMichael Ellerman udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n", 171*d9953105SMichael Ellerman *((unsigned long *)&ras_log_buf), status, state); 172*d9953105SMichael Ellerman printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n", 173*d9953105SMichael Ellerman *((unsigned long *)&ras_log_buf), status, state); 174*d9953105SMichael Ellerman 175*d9953105SMichael Ellerman /* format and print the extended information */ 176*d9953105SMichael Ellerman log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0); 177*d9953105SMichael Ellerman 178*d9953105SMichael Ellerman spin_unlock(&ras_log_buf_lock); 179*d9953105SMichael Ellerman return IRQ_HANDLED; 180*d9953105SMichael Ellerman } 181*d9953105SMichael Ellerman 182*d9953105SMichael Ellerman /* 183*d9953105SMichael Ellerman * Handle hardware error interrupts. 184*d9953105SMichael Ellerman * 185*d9953105SMichael Ellerman * RTAS check-exception is called to collect data on the exception. If 186*d9953105SMichael Ellerman * the error is deemed recoverable, we log a warning and return. 187*d9953105SMichael Ellerman * For nonrecoverable errors, an error is logged and we stop all processing 188*d9953105SMichael Ellerman * as quickly as possible in order to prevent propagation of the failure. 189*d9953105SMichael Ellerman */ 190*d9953105SMichael Ellerman static irqreturn_t 191*d9953105SMichael Ellerman ras_error_interrupt(int irq, void *dev_id, struct pt_regs * regs) 192*d9953105SMichael Ellerman { 193*d9953105SMichael Ellerman struct rtas_error_log *rtas_elog; 194*d9953105SMichael Ellerman int status = 0xdeadbeef; 195*d9953105SMichael Ellerman int fatal; 196*d9953105SMichael Ellerman 197*d9953105SMichael Ellerman spin_lock(&ras_log_buf_lock); 198*d9953105SMichael Ellerman 199*d9953105SMichael Ellerman status = rtas_call(ras_check_exception_token, 6, 1, NULL, 200*d9953105SMichael Ellerman RAS_VECTOR_OFFSET, 201*d9953105SMichael Ellerman virt_irq_to_real(irq_offset_down(irq)), 202*d9953105SMichael Ellerman RTAS_INTERNAL_ERROR, 1 /*Time Critical */, 203*d9953105SMichael Ellerman __pa(&ras_log_buf), 204*d9953105SMichael Ellerman rtas_get_error_log_max()); 205*d9953105SMichael Ellerman 206*d9953105SMichael Ellerman rtas_elog = (struct rtas_error_log *)ras_log_buf; 207*d9953105SMichael Ellerman 208*d9953105SMichael Ellerman if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC)) 209*d9953105SMichael Ellerman fatal = 1; 210*d9953105SMichael Ellerman else 211*d9953105SMichael Ellerman fatal = 0; 212*d9953105SMichael Ellerman 213*d9953105SMichael Ellerman /* format and print the extended information */ 214*d9953105SMichael Ellerman log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal); 215*d9953105SMichael Ellerman 216*d9953105SMichael Ellerman if (fatal) { 217*d9953105SMichael Ellerman udbg_printf("Fatal HW Error <0x%lx 0x%x>\n", 218*d9953105SMichael Ellerman *((unsigned long *)&ras_log_buf), status); 219*d9953105SMichael Ellerman printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n", 220*d9953105SMichael Ellerman *((unsigned long *)&ras_log_buf), status); 221*d9953105SMichael Ellerman 222*d9953105SMichael Ellerman #ifndef DEBUG 223*d9953105SMichael Ellerman /* Don't actually power off when debugging so we can test 224*d9953105SMichael Ellerman * without actually failing while injecting errors. 225*d9953105SMichael Ellerman * Error data will not be logged to syslog. 226*d9953105SMichael Ellerman */ 227*d9953105SMichael Ellerman ppc_md.power_off(); 228*d9953105SMichael Ellerman #endif 229*d9953105SMichael Ellerman } else { 230*d9953105SMichael Ellerman udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n", 231*d9953105SMichael Ellerman *((unsigned long *)&ras_log_buf), status); 232*d9953105SMichael Ellerman printk(KERN_WARNING 233*d9953105SMichael Ellerman "Warning: Recoverable hardware error <0x%lx 0x%x>\n", 234*d9953105SMichael Ellerman *((unsigned long *)&ras_log_buf), status); 235*d9953105SMichael Ellerman } 236*d9953105SMichael Ellerman 237*d9953105SMichael Ellerman spin_unlock(&ras_log_buf_lock); 238*d9953105SMichael Ellerman return IRQ_HANDLED; 239*d9953105SMichael Ellerman } 240*d9953105SMichael Ellerman 241*d9953105SMichael Ellerman /* Get the error information for errors coming through the 242*d9953105SMichael Ellerman * FWNMI vectors. The pt_regs' r3 will be updated to reflect 243*d9953105SMichael Ellerman * the actual r3 if possible, and a ptr to the error log entry 244*d9953105SMichael Ellerman * will be returned if found. 245*d9953105SMichael Ellerman * 246*d9953105SMichael Ellerman * The mce_data_buf does not have any locks or protection around it, 247*d9953105SMichael Ellerman * if a second machine check comes in, or a system reset is done 248*d9953105SMichael Ellerman * before we have logged the error, then we will get corruption in the 249*d9953105SMichael Ellerman * error log. This is preferable over holding off on calling 250*d9953105SMichael Ellerman * ibm,nmi-interlock which would result in us checkstopping if a 251*d9953105SMichael Ellerman * second machine check did come in. 252*d9953105SMichael Ellerman */ 253*d9953105SMichael Ellerman static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs) 254*d9953105SMichael Ellerman { 255*d9953105SMichael Ellerman unsigned long errdata = regs->gpr[3]; 256*d9953105SMichael Ellerman struct rtas_error_log *errhdr = NULL; 257*d9953105SMichael Ellerman unsigned long *savep; 258*d9953105SMichael Ellerman 259*d9953105SMichael Ellerman if ((errdata >= 0x7000 && errdata < 0x7fff0) || 260*d9953105SMichael Ellerman (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) { 261*d9953105SMichael Ellerman savep = __va(errdata); 262*d9953105SMichael Ellerman regs->gpr[3] = savep[0]; /* restore original r3 */ 263*d9953105SMichael Ellerman memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX); 264*d9953105SMichael Ellerman memcpy(mce_data_buf, (char *)(savep + 1), RTAS_ERROR_LOG_MAX); 265*d9953105SMichael Ellerman errhdr = (struct rtas_error_log *)mce_data_buf; 266*d9953105SMichael Ellerman } else { 267*d9953105SMichael Ellerman printk("FWNMI: corrupt r3\n"); 268*d9953105SMichael Ellerman } 269*d9953105SMichael Ellerman return errhdr; 270*d9953105SMichael Ellerman } 271*d9953105SMichael Ellerman 272*d9953105SMichael Ellerman /* Call this when done with the data returned by FWNMI_get_errinfo. 273*d9953105SMichael Ellerman * It will release the saved data area for other CPUs in the 274*d9953105SMichael Ellerman * partition to receive FWNMI errors. 275*d9953105SMichael Ellerman */ 276*d9953105SMichael Ellerman static void fwnmi_release_errinfo(void) 277*d9953105SMichael Ellerman { 278*d9953105SMichael Ellerman int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL); 279*d9953105SMichael Ellerman if (ret != 0) 280*d9953105SMichael Ellerman printk("FWNMI: nmi-interlock failed: %d\n", ret); 281*d9953105SMichael Ellerman } 282*d9953105SMichael Ellerman 283*d9953105SMichael Ellerman void pSeries_system_reset_exception(struct pt_regs *regs) 284*d9953105SMichael Ellerman { 285*d9953105SMichael Ellerman if (fwnmi_active) { 286*d9953105SMichael Ellerman struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs); 287*d9953105SMichael Ellerman if (errhdr) { 288*d9953105SMichael Ellerman /* XXX Should look at FWNMI information */ 289*d9953105SMichael Ellerman } 290*d9953105SMichael Ellerman fwnmi_release_errinfo(); 291*d9953105SMichael Ellerman } 292*d9953105SMichael Ellerman } 293*d9953105SMichael Ellerman 294*d9953105SMichael Ellerman /* 295*d9953105SMichael Ellerman * See if we can recover from a machine check exception. 296*d9953105SMichael Ellerman * This is only called on power4 (or above) and only via 297*d9953105SMichael Ellerman * the Firmware Non-Maskable Interrupts (fwnmi) handler 298*d9953105SMichael Ellerman * which provides the error analysis for us. 299*d9953105SMichael Ellerman * 300*d9953105SMichael Ellerman * Return 1 if corrected (or delivered a signal). 301*d9953105SMichael Ellerman * Return 0 if there is nothing we can do. 302*d9953105SMichael Ellerman */ 303*d9953105SMichael Ellerman static int recover_mce(struct pt_regs *regs, struct rtas_error_log * err) 304*d9953105SMichael Ellerman { 305*d9953105SMichael Ellerman int nonfatal = 0; 306*d9953105SMichael Ellerman 307*d9953105SMichael Ellerman if (err->disposition == RTAS_DISP_FULLY_RECOVERED) { 308*d9953105SMichael Ellerman /* Platform corrected itself */ 309*d9953105SMichael Ellerman nonfatal = 1; 310*d9953105SMichael Ellerman } else if ((regs->msr & MSR_RI) && 311*d9953105SMichael Ellerman user_mode(regs) && 312*d9953105SMichael Ellerman err->severity == RTAS_SEVERITY_ERROR_SYNC && 313*d9953105SMichael Ellerman err->disposition == RTAS_DISP_NOT_RECOVERED && 314*d9953105SMichael Ellerman err->target == RTAS_TARGET_MEMORY && 315*d9953105SMichael Ellerman err->type == RTAS_TYPE_ECC_UNCORR && 316*d9953105SMichael Ellerman !(current->pid == 0 || current->pid == 1)) { 317*d9953105SMichael Ellerman /* Kill off a user process with an ECC error */ 318*d9953105SMichael Ellerman printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n", 319*d9953105SMichael Ellerman current->pid); 320*d9953105SMichael Ellerman /* XXX something better for ECC error? */ 321*d9953105SMichael Ellerman _exception(SIGBUS, regs, BUS_ADRERR, regs->nip); 322*d9953105SMichael Ellerman nonfatal = 1; 323*d9953105SMichael Ellerman } 324*d9953105SMichael Ellerman 325*d9953105SMichael Ellerman log_error((char *)err, ERR_TYPE_RTAS_LOG, !nonfatal); 326*d9953105SMichael Ellerman 327*d9953105SMichael Ellerman return nonfatal; 328*d9953105SMichael Ellerman } 329*d9953105SMichael Ellerman 330*d9953105SMichael Ellerman /* 331*d9953105SMichael Ellerman * Handle a machine check. 332*d9953105SMichael Ellerman * 333*d9953105SMichael Ellerman * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi) 334*d9953105SMichael Ellerman * should be present. If so the handler which called us tells us if the 335*d9953105SMichael Ellerman * error was recovered (never true if RI=0). 336*d9953105SMichael Ellerman * 337*d9953105SMichael Ellerman * On hardware prior to Power 4 these exceptions were asynchronous which 338*d9953105SMichael Ellerman * means we can't tell exactly where it occurred and so we can't recover. 339*d9953105SMichael Ellerman */ 340*d9953105SMichael Ellerman int pSeries_machine_check_exception(struct pt_regs *regs) 341*d9953105SMichael Ellerman { 342*d9953105SMichael Ellerman struct rtas_error_log *errp; 343*d9953105SMichael Ellerman 344*d9953105SMichael Ellerman if (fwnmi_active) { 345*d9953105SMichael Ellerman errp = fwnmi_get_errinfo(regs); 346*d9953105SMichael Ellerman fwnmi_release_errinfo(); 347*d9953105SMichael Ellerman if (errp && recover_mce(regs, errp)) 348*d9953105SMichael Ellerman return 1; 349*d9953105SMichael Ellerman } 350*d9953105SMichael Ellerman 351*d9953105SMichael Ellerman return 0; 352*d9953105SMichael Ellerman } 353