2 * Copyright (C) 2001 Dave Engebretsen IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
24 #include <linux/reboot.h>
25 #include <linux/irq_work.h>
27 #include <asm/machdep.h>
29 #include <asm/firmware.h>
33 static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
34 static DEFINE_SPINLOCK(ras_log_buf_lock);
36 static int ras_check_exception_token;
38 static void mce_process_errlog_event(struct irq_work *work);
39 static struct irq_work mce_errlog_process_work = {
40 .func = mce_process_errlog_event,
43 #define EPOW_SENSOR_TOKEN 9
44 #define EPOW_SENSOR_INDEX 0
46 /* EPOW events counter variable */
47 static int num_epow_events;
49 static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id);
50 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
51 static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
55 * Enable the hotplug interrupt late because processing them may touch other
56 * devices or systems (e.g. hugepages) that have not been initialized at the
59 int __init init_ras_hotplug_IRQ(void)
61 struct device_node *np;
64 np = of_find_node_by_path("/event-sources/hot-plug-events");
66 if (dlpar_workqueue_init() == 0)
67 request_event_sources_irqs(np, ras_hotplug_interrupt,
74 machine_late_initcall(pseries, init_ras_hotplug_IRQ);
77 * Initialize handlers for the set of interrupts caused by hardware errors
78 * and power system events.
80 static int __init init_ras_IRQ(void)
82 struct device_node *np;
84 ras_check_exception_token = rtas_token("check-exception");
87 np = of_find_node_by_path("/event-sources/internal-errors");
89 request_event_sources_irqs(np, ras_error_interrupt,
95 np = of_find_node_by_path("/event-sources/epow-events");
97 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
103 machine_subsys_initcall(pseries, init_ras_IRQ);
105 #define EPOW_SHUTDOWN_NORMAL 1
106 #define EPOW_SHUTDOWN_ON_UPS 2
107 #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
108 #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
110 static void handle_system_shutdown(char event_modifier)
112 switch (event_modifier) {
113 case EPOW_SHUTDOWN_NORMAL:
114 pr_emerg("Power off requested\n");
115 orderly_poweroff(true);
118 case EPOW_SHUTDOWN_ON_UPS:
119 pr_emerg("Loss of system power detected. System is running on"
120 " UPS/battery. Check RTAS error log for details\n");
123 case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
124 pr_emerg("Loss of system critical functions detected. Check"
125 " RTAS error log for details\n");
126 orderly_poweroff(true);
129 case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
130 pr_emerg("High ambient temperature detected. Check RTAS"
131 " error log for details\n");
132 orderly_poweroff(true);
136 pr_err("Unknown power/cooling shutdown event (modifier = %d)\n",
141 struct epow_errorlog {
142 unsigned char sensor_value;
143 unsigned char event_modifier;
144 unsigned char extended_modifier;
145 unsigned char reserved;
146 unsigned char platform_reason;
150 #define EPOW_WARN_COOLING 1
151 #define EPOW_WARN_POWER 2
152 #define EPOW_SYSTEM_SHUTDOWN 3
153 #define EPOW_SYSTEM_HALT 4
154 #define EPOW_MAIN_ENCLOSURE 5
155 #define EPOW_POWER_OFF 7
157 static void rtas_parse_epow_errlog(struct rtas_error_log *log)
159 struct pseries_errorlog *pseries_log;
160 struct epow_errorlog *epow_log;
164 pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
165 if (pseries_log == NULL)
168 epow_log = (struct epow_errorlog *)pseries_log->data;
169 action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
170 modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
172 switch (action_code) {
174 if (num_epow_events) {
175 pr_info("Non critical power/cooling issue cleared\n");
180 case EPOW_WARN_COOLING:
181 pr_info("Non-critical cooling issue detected. Check RTAS error"
182 " log for details\n");
185 case EPOW_WARN_POWER:
186 pr_info("Non-critical power issue detected. Check RTAS error"
187 " log for details\n");
190 case EPOW_SYSTEM_SHUTDOWN:
191 handle_system_shutdown(epow_log->event_modifier);
194 case EPOW_SYSTEM_HALT:
195 pr_emerg("Critical power/cooling issue detected. Check RTAS"
196 " error log for details. Powering off.\n");
197 orderly_poweroff(true);
200 case EPOW_MAIN_ENCLOSURE:
202 pr_emerg("System about to lose power. Check RTAS error log "
203 " for details. Powering off immediately.\n");
209 pr_err("Unknown power/cooling event (action code = %d)\n",
213 /* Increment epow events counter variable */
214 if (action_code != EPOW_RESET)
218 static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id)
220 struct pseries_errorlog *pseries_log;
221 struct pseries_hp_errorlog *hp_elog;
223 spin_lock(&ras_log_buf_lock);
225 rtas_call(ras_check_exception_token, 6, 1, NULL,
226 RTAS_VECTOR_EXTERNAL_INTERRUPT, virq_to_hw(irq),
227 RTAS_HOTPLUG_EVENTS, 0, __pa(&ras_log_buf),
228 rtas_get_error_log_max());
230 pseries_log = get_pseries_errorlog((struct rtas_error_log *)ras_log_buf,
231 PSERIES_ELOG_SECT_ID_HOTPLUG);
232 hp_elog = (struct pseries_hp_errorlog *)pseries_log->data;
235 * Since PCI hotplug is not currently supported on pseries, put PCI
236 * hotplug events on the ras_log_buf to be handled by rtas_errd.
238 if (hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_MEM ||
239 hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_CPU)
240 queue_hotplug_event(hp_elog, NULL, NULL);
242 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
244 spin_unlock(&ras_log_buf_lock);
248 /* Handle environmental and power warning (EPOW) interrupts. */
249 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
255 status = rtas_get_sensor_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
259 critical = 1; /* Time Critical */
263 spin_lock(&ras_log_buf_lock);
265 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
266 RTAS_VECTOR_EXTERNAL_INTERRUPT,
269 critical, __pa(&ras_log_buf),
270 rtas_get_error_log_max());
272 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
274 rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
276 spin_unlock(&ras_log_buf_lock);
281 * Handle hardware error interrupts.
283 * RTAS check-exception is called to collect data on the exception. If
284 * the error is deemed recoverable, we log a warning and return.
285 * For nonrecoverable errors, an error is logged and we stop all processing
286 * as quickly as possible in order to prevent propagation of the failure.
288 static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
290 struct rtas_error_log *rtas_elog;
294 spin_lock(&ras_log_buf_lock);
296 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
297 RTAS_VECTOR_EXTERNAL_INTERRUPT,
299 RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
301 rtas_get_error_log_max());
303 rtas_elog = (struct rtas_error_log *)ras_log_buf;
306 rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
311 /* format and print the extended information */
312 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
315 pr_emerg("Fatal hardware error detected. Check RTAS error"
316 " log for details. Powering off immediately\n");
320 pr_err("Recoverable hardware error detected\n");
323 spin_unlock(&ras_log_buf_lock);
328 * Some versions of FWNMI place the buffer inside the 4kB page starting at
329 * 0x7000. Other versions place it inside the rtas buffer. We check both.
330 * Minimum size of the buffer is 16 bytes.
332 #define VALID_FWNMI_BUFFER(A) \
333 ((((A) >= 0x7000) && ((A) <= 0x8000 - 16)) || \
334 (((A) >= rtas.base) && ((A) <= (rtas.base + rtas.size - 16))))
336 static inline struct rtas_error_log *fwnmi_get_errlog(void)
338 return (struct rtas_error_log *)local_paca->mce_data_buf;
342 * Get the error information for errors coming through the
343 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
344 * the actual r3 if possible, and a ptr to the error log entry
345 * will be returned if found.
347 * Use one buffer mce_data_buf per cpu to store RTAS error.
349 * The mce_data_buf does not have any locks or protection around it,
350 * if a second machine check comes in, or a system reset is done
351 * before we have logged the error, then we will get corruption in the
352 * error log. This is preferable over holding off on calling
353 * ibm,nmi-interlock which would result in us checkstopping if a
354 * second machine check did come in.
356 static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
358 unsigned long *savep;
359 struct rtas_error_log *h;
361 /* Mask top two bits */
362 regs->gpr[3] &= ~(0x3UL << 62);
364 if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
365 printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
369 savep = __va(regs->gpr[3]);
370 regs->gpr[3] = be64_to_cpu(savep[0]); /* restore original r3 */
372 h = (struct rtas_error_log *)&savep[1];
373 /* Use the per cpu buffer from paca to store rtas error log */
374 memset(local_paca->mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
375 if (!rtas_error_extended(h)) {
376 memcpy(local_paca->mce_data_buf, h, sizeof(__u64));
378 int len, error_log_length;
380 error_log_length = 8 + rtas_error_extended_log_length(h);
381 len = min_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
382 memcpy(local_paca->mce_data_buf, h, len);
385 return (struct rtas_error_log *)local_paca->mce_data_buf;
388 /* Call this when done with the data returned by FWNMI_get_errinfo.
389 * It will release the saved data area for other CPUs in the
390 * partition to receive FWNMI errors.
392 static void fwnmi_release_errinfo(void)
394 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
396 printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
399 int pSeries_system_reset_exception(struct pt_regs *regs)
401 #ifdef __LITTLE_ENDIAN__
403 * Some firmware byteswaps SRR registers and gives incorrect SRR1. Try
404 * to detect the bad SRR1 pattern here. Flip the NIP back to correct
405 * endian for reporting purposes. Unfortunately the MSR can't be fixed,
406 * so clear it. It will be missing MSR_RI so we won't try to recover.
408 if ((be64_to_cpu(regs->msr) &
409 (MSR_LE|MSR_RI|MSR_DR|MSR_IR|MSR_ME|MSR_PR|
410 MSR_ILE|MSR_HV|MSR_SF)) == (MSR_DR|MSR_SF)) {
411 regs->nip = be64_to_cpu((__be64)regs->nip);
417 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
419 /* XXX Should look at FWNMI information */
421 fwnmi_release_errinfo();
424 if (smp_handle_nmi_ipi(regs))
427 return 0; /* need to perform reset */
431 * Process MCE rtas errlog event.
433 static void mce_process_errlog_event(struct irq_work *work)
435 struct rtas_error_log *err;
437 err = fwnmi_get_errlog();
438 log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
442 * See if we can recover from a machine check exception.
443 * This is only called on power4 (or above) and only via
444 * the Firmware Non-Maskable Interrupts (fwnmi) handler
445 * which provides the error analysis for us.
447 * Return 1 if corrected (or delivered a signal).
448 * Return 0 if there is nothing we can do.
450 static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
453 int disposition = rtas_error_disposition(err);
455 if (!(regs->msr & MSR_RI)) {
456 /* If MSR_RI isn't set, we cannot recover */
459 } else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
460 /* Platform corrected itself */
463 } else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
464 /* Platform corrected itself but could be degraded */
465 printk(KERN_ERR "MCE: limited recovery, system may "
469 } else if (user_mode(regs) && !is_global_init(current) &&
470 rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
473 * If we received a synchronous error when in userspace
474 * kill the task. Firmware may report details of the fail
475 * asynchronously, so we can't rely on the target and type
476 * fields being valid here.
478 printk(KERN_ERR "MCE: uncorrectable error, killing task "
479 "%s:%d\n", current->comm, current->pid);
481 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
485 /* Queue irq work to log this rtas event later. */
486 irq_work_queue(&mce_errlog_process_work);
492 * Handle a machine check.
494 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
495 * should be present. If so the handler which called us tells us if the
496 * error was recovered (never true if RI=0).
498 * On hardware prior to Power 4 these exceptions were asynchronous which
499 * means we can't tell exactly where it occurred and so we can't recover.
501 int pSeries_machine_check_exception(struct pt_regs *regs)
503 struct rtas_error_log *errp;
506 errp = fwnmi_get_errinfo(regs);
507 fwnmi_release_errinfo();
508 if (errp && recover_mce(regs, errp))