GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / powerpc / platforms / pseries / ras.c
1 /*
2  * Copyright (C) 2001 Dave Engebretsen IBM Corporation
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/of.h>
23 #include <linux/fs.h>
24 #include <linux/reboot.h>
25 #include <linux/irq_work.h>
26
27 #include <asm/machdep.h>
28 #include <asm/rtas.h>
29 #include <asm/firmware.h>
30
31 #include "pseries.h"
32
33 static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
34 static DEFINE_SPINLOCK(ras_log_buf_lock);
35
36 static int ras_check_exception_token;
37
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,
41 };
42
43 #define EPOW_SENSOR_TOKEN       9
44 #define EPOW_SENSOR_INDEX       0
45
46 /* EPOW events counter variable */
47 static int num_epow_events;
48
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);
52
53
54 /*
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
57  * subsys stage.
58  */
59 int __init init_ras_hotplug_IRQ(void)
60 {
61         struct device_node *np;
62
63         /* Hotplug Events */
64         np = of_find_node_by_path("/event-sources/hot-plug-events");
65         if (np != NULL) {
66                 if (dlpar_workqueue_init() == 0)
67                         request_event_sources_irqs(np, ras_hotplug_interrupt,
68                                                    "RAS_HOTPLUG");
69                 of_node_put(np);
70         }
71
72         return 0;
73 }
74 machine_late_initcall(pseries, init_ras_hotplug_IRQ);
75
76 /*
77  * Initialize handlers for the set of interrupts caused by hardware errors
78  * and power system events.
79  */
80 static int __init init_ras_IRQ(void)
81 {
82         struct device_node *np;
83
84         ras_check_exception_token = rtas_token("check-exception");
85
86         /* Internal Errors */
87         np = of_find_node_by_path("/event-sources/internal-errors");
88         if (np != NULL) {
89                 request_event_sources_irqs(np, ras_error_interrupt,
90                                            "RAS_ERROR");
91                 of_node_put(np);
92         }
93
94         /* EPOW Events */
95         np = of_find_node_by_path("/event-sources/epow-events");
96         if (np != NULL) {
97                 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
98                 of_node_put(np);
99         }
100
101         return 0;
102 }
103 machine_subsys_initcall(pseries, init_ras_IRQ);
104
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
109
110 static void handle_system_shutdown(char event_modifier)
111 {
112         switch (event_modifier) {
113         case EPOW_SHUTDOWN_NORMAL:
114                 pr_emerg("Power off requested\n");
115                 orderly_poweroff(true);
116                 break;
117
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");
121                 break;
122
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);
127                 break;
128
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);
133                 break;
134
135         default:
136                 pr_err("Unknown power/cooling shutdown event (modifier = %d)\n",
137                         event_modifier);
138         }
139 }
140
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;
147 };
148
149 #define EPOW_RESET                      0
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
156
157 static void rtas_parse_epow_errlog(struct rtas_error_log *log)
158 {
159         struct pseries_errorlog *pseries_log;
160         struct epow_errorlog *epow_log;
161         char action_code;
162         char modifier;
163
164         pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
165         if (pseries_log == NULL)
166                 return;
167
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 */
171
172         switch (action_code) {
173         case EPOW_RESET:
174                 if (num_epow_events) {
175                         pr_info("Non critical power/cooling issue cleared\n");
176                         num_epow_events--;
177                 }
178                 break;
179
180         case EPOW_WARN_COOLING:
181                 pr_info("Non-critical cooling issue detected. Check RTAS error"
182                         " log for details\n");
183                 break;
184
185         case EPOW_WARN_POWER:
186                 pr_info("Non-critical power issue detected. Check RTAS error"
187                         " log for details\n");
188                 break;
189
190         case EPOW_SYSTEM_SHUTDOWN:
191                 handle_system_shutdown(epow_log->event_modifier);
192                 break;
193
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);
198                 break;
199
200         case EPOW_MAIN_ENCLOSURE:
201         case EPOW_POWER_OFF:
202                 pr_emerg("System about to lose power. Check RTAS error log "
203                          " for details. Powering off immediately.\n");
204                 emergency_sync();
205                 kernel_power_off();
206                 break;
207
208         default:
209                 pr_err("Unknown power/cooling event (action code  = %d)\n",
210                         action_code);
211         }
212
213         /* Increment epow events counter variable */
214         if (action_code != EPOW_RESET)
215                 num_epow_events++;
216 }
217
218 static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id)
219 {
220         struct pseries_errorlog *pseries_log;
221         struct pseries_hp_errorlog *hp_elog;
222
223         spin_lock(&ras_log_buf_lock);
224
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());
229
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;
233
234         /*
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.
237          */
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);
241         else
242                 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
243
244         spin_unlock(&ras_log_buf_lock);
245         return IRQ_HANDLED;
246 }
247
248 /* Handle environmental and power warning (EPOW) interrupts. */
249 static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
250 {
251         int status;
252         int state;
253         int critical;
254
255         status = rtas_get_sensor_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
256                                       &state);
257
258         if (state > 3)
259                 critical = 1;           /* Time Critical */
260         else
261                 critical = 0;
262
263         spin_lock(&ras_log_buf_lock);
264
265         status = rtas_call(ras_check_exception_token, 6, 1, NULL,
266                            RTAS_VECTOR_EXTERNAL_INTERRUPT,
267                            virq_to_hw(irq),
268                            RTAS_EPOW_WARNING,
269                            critical, __pa(&ras_log_buf),
270                                 rtas_get_error_log_max());
271
272         log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
273
274         rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
275
276         spin_unlock(&ras_log_buf_lock);
277         return IRQ_HANDLED;
278 }
279
280 /*
281  * Handle hardware error interrupts.
282  *
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.
287  */
288 static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
289 {
290         struct rtas_error_log *rtas_elog;
291         int status;
292         int fatal;
293
294         spin_lock(&ras_log_buf_lock);
295
296         status = rtas_call(ras_check_exception_token, 6, 1, NULL,
297                            RTAS_VECTOR_EXTERNAL_INTERRUPT,
298                            virq_to_hw(irq),
299                            RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
300                            __pa(&ras_log_buf),
301                                 rtas_get_error_log_max());
302
303         rtas_elog = (struct rtas_error_log *)ras_log_buf;
304
305         if (status == 0 &&
306             rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
307                 fatal = 1;
308         else
309                 fatal = 0;
310
311         /* format and print the extended information */
312         log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
313
314         if (fatal) {
315                 pr_emerg("Fatal hardware error detected. Check RTAS error"
316                          " log for details. Powering off immediately\n");
317                 emergency_sync();
318                 kernel_power_off();
319         } else {
320                 pr_err("Recoverable hardware error detected\n");
321         }
322
323         spin_unlock(&ras_log_buf_lock);
324         return IRQ_HANDLED;
325 }
326
327 /*
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.
331  */
332 #define VALID_FWNMI_BUFFER(A) \
333         ((((A) >= 0x7000) && ((A) <= 0x8000 - 16)) || \
334         (((A) >= rtas.base) && ((A) <= (rtas.base + rtas.size - 16))))
335
336 static inline struct rtas_error_log *fwnmi_get_errlog(void)
337 {
338         return (struct rtas_error_log *)local_paca->mce_data_buf;
339 }
340
341 /*
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.
346  *
347  * Use one buffer mce_data_buf per cpu to store RTAS error.
348  *
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.
355  */
356 static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
357 {
358         unsigned long *savep;
359         struct rtas_error_log *h;
360
361         /* Mask top two bits */
362         regs->gpr[3] &= ~(0x3UL << 62);
363
364         if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
365                 printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
366                 return NULL;
367         }
368
369         savep = __va(regs->gpr[3]);
370         regs->gpr[3] = be64_to_cpu(savep[0]);   /* restore original r3 */
371
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));
377         } else {
378                 int len, error_log_length;
379
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);
383         }
384
385         return (struct rtas_error_log *)local_paca->mce_data_buf;
386 }
387
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.
391  */
392 static void fwnmi_release_errinfo(void)
393 {
394         int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
395         if (ret != 0)
396                 printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
397 }
398
399 int pSeries_system_reset_exception(struct pt_regs *regs)
400 {
401 #ifdef __LITTLE_ENDIAN__
402         /*
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.
407          */
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);
412                 regs->msr = 0;
413         }
414 #endif
415
416         if (fwnmi_active) {
417                 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
418                 if (errhdr) {
419                         /* XXX Should look at FWNMI information */
420                 }
421                 fwnmi_release_errinfo();
422         }
423
424         if (smp_handle_nmi_ipi(regs))
425                 return 1;
426
427         return 0; /* need to perform reset */
428 }
429
430 /*
431  * Process MCE rtas errlog event.
432  */
433 static void mce_process_errlog_event(struct irq_work *work)
434 {
435         struct rtas_error_log *err;
436
437         err = fwnmi_get_errlog();
438         log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
439 }
440
441 /*
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.
446  *
447  * Return 1 if corrected (or delivered a signal).
448  * Return 0 if there is nothing we can do.
449  */
450 static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
451 {
452         int recovered = 0;
453         int disposition = rtas_error_disposition(err);
454
455         if (!(regs->msr & MSR_RI)) {
456                 /* If MSR_RI isn't set, we cannot recover */
457                 recovered = 0;
458
459         } else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
460                 /* Platform corrected itself */
461                 recovered = 1;
462
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 "
466                        "be degraded\n");
467                 recovered = 1;
468
469         } else if (user_mode(regs) && !is_global_init(current) &&
470                    rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
471
472                 /*
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.
477                  */
478                 printk(KERN_ERR "MCE: uncorrectable error, killing task "
479                        "%s:%d\n", current->comm, current->pid);
480
481                 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
482                 recovered = 1;
483         }
484
485         /* Queue irq work to log this rtas event later. */
486         irq_work_queue(&mce_errlog_process_work);
487
488         return recovered;
489 }
490
491 /*
492  * Handle a machine check.
493  *
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).
497  *
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.
500  */
501 int pSeries_machine_check_exception(struct pt_regs *regs)
502 {
503         struct rtas_error_log *errp;
504
505         if (fwnmi_active) {
506                 errp = fwnmi_get_errinfo(regs);
507                 fwnmi_release_errinfo();
508                 if (errp && recover_mce(regs, errp))
509                         return 1;
510         }
511
512         return 0;
513 }