]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/powerpc/platforms/pseries/ras.c
Merge remote-tracking branches 'asoc/topic/cs35l32', 'asoc/topic/cs35l34', 'asoc...
[mirror_ubuntu-jammy-kernel.git] / arch / powerpc / platforms / pseries / ras.c
CommitLineData
1da177e4 1/*
1da177e4 2 * Copyright (C) 2001 Dave Engebretsen IBM Corporation
d9953105 3 *
1da177e4
LT
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.
d9953105 8 *
1da177e4
LT
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.
d9953105 13 *
1da177e4
LT
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
1da177e4 19#include <linux/sched.h>
1da177e4 20#include <linux/interrupt.h>
1da177e4 21#include <linux/irq.h>
90128997 22#include <linux/of.h>
55fc0c56
AB
23#include <linux/fs.h>
24#include <linux/reboot.h>
1da177e4 25
1da177e4
LT
26#include <asm/machdep.h>
27#include <asm/rtas.h>
8c4f1f29 28#include <asm/firmware.h>
1da177e4 29
577830b0 30#include "pseries.h"
c902be71 31
1da177e4
LT
32static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
33static DEFINE_SPINLOCK(ras_log_buf_lock);
34
d368514c
AB
35static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
36static DEFINE_PER_CPU(__u64, mce_data_buf);
1da177e4 37
1da177e4
LT
38static int ras_check_exception_token;
39
40#define EPOW_SENSOR_TOKEN 9
41#define EPOW_SENSOR_INDEX 0
1da177e4 42
b4af279a
VP
43/* EPOW events counter variable */
44static int num_epow_events;
45
b7d9eb39 46static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id);
7d12e780
DH
47static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
48static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
1da177e4 49
0ebfff14 50
1da177e4
LT
51/*
52 * Initialize handlers for the set of interrupts caused by hardware errors
53 * and power system events.
54 */
55static int __init init_ras_IRQ(void)
56{
57 struct device_node *np;
58
1da177e4
LT
59 ras_check_exception_token = rtas_token("check-exception");
60
61 /* Internal Errors */
62 np = of_find_node_by_path("/event-sources/internal-errors");
63 if (np != NULL) {
32c96f77
MN
64 request_event_sources_irqs(np, ras_error_interrupt,
65 "RAS_ERROR");
1da177e4
LT
66 of_node_put(np);
67 }
68
b7d9eb39
JA
69 /* Hotplug Events */
70 np = of_find_node_by_path("/event-sources/hot-plug-events");
71 if (np != NULL) {
e2d59152
ME
72 if (dlpar_workqueue_init() == 0)
73 request_event_sources_irqs(np, ras_hotplug_interrupt,
b7d9eb39
JA
74 "RAS_HOTPLUG");
75 of_node_put(np);
76 }
77
1da177e4
LT
78 /* EPOW Events */
79 np = of_find_node_by_path("/event-sources/epow-events");
80 if (np != NULL) {
32c96f77 81 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
1da177e4
LT
82 of_node_put(np);
83 }
84
69ed3324 85 return 0;
1da177e4 86}
8e83e905 87machine_subsys_initcall(pseries, init_ras_IRQ);
1da177e4 88
55fc0c56
AB
89#define EPOW_SHUTDOWN_NORMAL 1
90#define EPOW_SHUTDOWN_ON_UPS 2
91#define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
92#define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
93
94static void handle_system_shutdown(char event_modifier)
95{
96 switch (event_modifier) {
97 case EPOW_SHUTDOWN_NORMAL:
b4af279a 98 pr_emerg("Power off requested\n");
1b7e0cbe 99 orderly_poweroff(true);
55fc0c56
AB
100 break;
101
102 case EPOW_SHUTDOWN_ON_UPS:
b4af279a
VP
103 pr_emerg("Loss of system power detected. System is running on"
104 " UPS/battery. Check RTAS error log for details\n");
79872e35 105 orderly_poweroff(true);
55fc0c56
AB
106 break;
107
108 case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
b4af279a
VP
109 pr_emerg("Loss of system critical functions detected. Check"
110 " RTAS error log for details\n");
1b7e0cbe 111 orderly_poweroff(true);
55fc0c56
AB
112 break;
113
114 case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
b4af279a
VP
115 pr_emerg("High ambient temperature detected. Check RTAS"
116 " error log for details\n");
1b7e0cbe 117 orderly_poweroff(true);
55fc0c56
AB
118 break;
119
120 default:
b4af279a 121 pr_err("Unknown power/cooling shutdown event (modifier = %d)\n",
55fc0c56
AB
122 event_modifier);
123 }
124}
125
126struct epow_errorlog {
127 unsigned char sensor_value;
128 unsigned char event_modifier;
129 unsigned char extended_modifier;
130 unsigned char reserved;
131 unsigned char platform_reason;
132};
133
134#define EPOW_RESET 0
135#define EPOW_WARN_COOLING 1
136#define EPOW_WARN_POWER 2
137#define EPOW_SYSTEM_SHUTDOWN 3
138#define EPOW_SYSTEM_HALT 4
139#define EPOW_MAIN_ENCLOSURE 5
140#define EPOW_POWER_OFF 7
141
e51df2c1 142static void rtas_parse_epow_errlog(struct rtas_error_log *log)
55fc0c56
AB
143{
144 struct pseries_errorlog *pseries_log;
145 struct epow_errorlog *epow_log;
146 char action_code;
147 char modifier;
148
149 pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
150 if (pseries_log == NULL)
151 return;
152
153 epow_log = (struct epow_errorlog *)pseries_log->data;
154 action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
155 modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
156
157 switch (action_code) {
158 case EPOW_RESET:
b4af279a
VP
159 if (num_epow_events) {
160 pr_info("Non critical power/cooling issue cleared\n");
161 num_epow_events--;
162 }
55fc0c56
AB
163 break;
164
165 case EPOW_WARN_COOLING:
b4af279a
VP
166 pr_info("Non-critical cooling issue detected. Check RTAS error"
167 " log for details\n");
55fc0c56
AB
168 break;
169
170 case EPOW_WARN_POWER:
b4af279a
VP
171 pr_info("Non-critical power issue detected. Check RTAS error"
172 " log for details\n");
55fc0c56
AB
173 break;
174
175 case EPOW_SYSTEM_SHUTDOWN:
176 handle_system_shutdown(epow_log->event_modifier);
177 break;
178
179 case EPOW_SYSTEM_HALT:
b4af279a
VP
180 pr_emerg("Critical power/cooling issue detected. Check RTAS"
181 " error log for details. Powering off.\n");
1b7e0cbe 182 orderly_poweroff(true);
55fc0c56
AB
183 break;
184
185 case EPOW_MAIN_ENCLOSURE:
186 case EPOW_POWER_OFF:
b4af279a
VP
187 pr_emerg("System about to lose power. Check RTAS error log "
188 " for details. Powering off immediately.\n");
55fc0c56
AB
189 emergency_sync();
190 kernel_power_off();
191 break;
192
193 default:
b4af279a 194 pr_err("Unknown power/cooling event (action code = %d)\n",
55fc0c56
AB
195 action_code);
196 }
b4af279a
VP
197
198 /* Increment epow events counter variable */
199 if (action_code != EPOW_RESET)
200 num_epow_events++;
55fc0c56
AB
201}
202
b7d9eb39
JA
203static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id)
204{
205 struct pseries_errorlog *pseries_log;
206 struct pseries_hp_errorlog *hp_elog;
207
208 spin_lock(&ras_log_buf_lock);
209
210 rtas_call(ras_check_exception_token, 6, 1, NULL,
211 RTAS_VECTOR_EXTERNAL_INTERRUPT, virq_to_hw(irq),
212 RTAS_HOTPLUG_EVENTS, 0, __pa(&ras_log_buf),
213 rtas_get_error_log_max());
214
215 pseries_log = get_pseries_errorlog((struct rtas_error_log *)ras_log_buf,
216 PSERIES_ELOG_SECT_ID_HOTPLUG);
217 hp_elog = (struct pseries_hp_errorlog *)pseries_log->data;
218
219 /*
220 * Since PCI hotplug is not currently supported on pseries, put PCI
221 * hotplug events on the ras_log_buf to be handled by rtas_errd.
222 */
223 if (hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_MEM ||
224 hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_CPU)
225 queue_hotplug_event(hp_elog, NULL, NULL);
226 else
227 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
228
229 spin_unlock(&ras_log_buf_lock);
230 return IRQ_HANDLED;
231}
232
55fc0c56 233/* Handle environmental and power warning (EPOW) interrupts. */
7d12e780 234static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
1da177e4 235{
55fc0c56
AB
236 int status;
237 int state;
1da177e4
LT
238 int critical;
239
1c2cb594
TH
240 status = rtas_get_sensor_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
241 &state);
1da177e4
LT
242
243 if (state > 3)
55fc0c56 244 critical = 1; /* Time Critical */
1da177e4
LT
245 else
246 critical = 0;
247
248 spin_lock(&ras_log_buf_lock);
249
250 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
b08e281b 251 RTAS_VECTOR_EXTERNAL_INTERRUPT,
476eb491 252 virq_to_hw(irq),
6f43747f 253 RTAS_EPOW_WARNING,
1da177e4
LT
254 critical, __pa(&ras_log_buf),
255 rtas_get_error_log_max());
256
1da177e4
LT
257 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
258
55fc0c56
AB
259 rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
260
1da177e4
LT
261 spin_unlock(&ras_log_buf_lock);
262 return IRQ_HANDLED;
263}
264
265/*
266 * Handle hardware error interrupts.
267 *
268 * RTAS check-exception is called to collect data on the exception. If
269 * the error is deemed recoverable, we log a warning and return.
270 * For nonrecoverable errors, an error is logged and we stop all processing
271 * as quickly as possible in order to prevent propagation of the failure.
272 */
7d12e780 273static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
1da177e4
LT
274{
275 struct rtas_error_log *rtas_elog;
cc8b5263 276 int status;
1da177e4
LT
277 int fatal;
278
279 spin_lock(&ras_log_buf_lock);
280
281 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
b08e281b 282 RTAS_VECTOR_EXTERNAL_INTERRUPT,
476eb491 283 virq_to_hw(irq),
cc8b5263 284 RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
1da177e4
LT
285 __pa(&ras_log_buf),
286 rtas_get_error_log_max());
287
288 rtas_elog = (struct rtas_error_log *)ras_log_buf;
289
a08a53ea
GK
290 if (status == 0 &&
291 rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
1da177e4
LT
292 fatal = 1;
293 else
294 fatal = 0;
295
296 /* format and print the extended information */
297 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
298
299 if (fatal) {
b4af279a
VP
300 pr_emerg("Fatal hardware error detected. Check RTAS error"
301 " log for details. Powering off immediately\n");
cc8b5263
AB
302 emergency_sync();
303 kernel_power_off();
1da177e4 304 } else {
b4af279a 305 pr_err("Recoverable hardware error detected\n");
1da177e4
LT
306 }
307
308 spin_unlock(&ras_log_buf_lock);
309 return IRQ_HANDLED;
310}
311
d368514c
AB
312/*
313 * Some versions of FWNMI place the buffer inside the 4kB page starting at
314 * 0x7000. Other versions place it inside the rtas buffer. We check both.
315 */
316#define VALID_FWNMI_BUFFER(A) \
317 ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
318 (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
319
320/*
321 * Get the error information for errors coming through the
1da177e4
LT
322 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
323 * the actual r3 if possible, and a ptr to the error log entry
324 * will be returned if found.
325 *
d368514c
AB
326 * If the RTAS error is not of the extended type, then we put it in a per
327 * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
328 *
329 * The global_mce_data_buf does not have any locks or protection around it,
1da177e4
LT
330 * if a second machine check comes in, or a system reset is done
331 * before we have logged the error, then we will get corruption in the
332 * error log. This is preferable over holding off on calling
333 * ibm,nmi-interlock which would result in us checkstopping if a
334 * second machine check did come in.
335 */
336static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
337{
1da177e4 338 unsigned long *savep;
d368514c 339 struct rtas_error_log *h, *errhdr = NULL;
1da177e4 340
ee1dd1e3
MS
341 /* Mask top two bits */
342 regs->gpr[3] &= ~(0x3UL << 62);
343
d368514c 344 if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
f0e939ae 345 printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
d368514c
AB
346 return NULL;
347 }
348
349 savep = __va(regs->gpr[3]);
350 regs->gpr[3] = savep[0]; /* restore original r3 */
351
352 /* If it isn't an extended log we can use the per cpu 64bit buffer */
353 h = (struct rtas_error_log *)&savep[1];
a08a53ea 354 if (!rtas_error_extended(h)) {
69111bac
CL
355 memcpy(this_cpu_ptr(&mce_data_buf), h, sizeof(__u64));
356 errhdr = (struct rtas_error_log *)this_cpu_ptr(&mce_data_buf);
1da177e4 357 } else {
a08a53ea 358 int len, error_log_length;
d368514c 359
a08a53ea
GK
360 error_log_length = 8 + rtas_error_extended_log_length(h);
361 len = max_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
d368514c
AB
362 memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
363 memcpy(global_mce_data_buf, h, len);
364 errhdr = (struct rtas_error_log *)global_mce_data_buf;
1da177e4 365 }
d368514c 366
1da177e4
LT
367 return errhdr;
368}
369
370/* Call this when done with the data returned by FWNMI_get_errinfo.
371 * It will release the saved data area for other CPUs in the
372 * partition to receive FWNMI errors.
373 */
374static void fwnmi_release_errinfo(void)
375{
376 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
377 if (ret != 0)
d368514c 378 printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
1da177e4
LT
379}
380
c902be71 381int pSeries_system_reset_exception(struct pt_regs *regs)
1da177e4 382{
bded0706
NP
383#ifdef __LITTLE_ENDIAN__
384 /*
385 * Some firmware byteswaps SRR registers and gives incorrect SRR1. Try
386 * to detect the bad SRR1 pattern here. Flip the NIP back to correct
387 * endian for reporting purposes. Unfortunately the MSR can't be fixed,
388 * so clear it. It will be missing MSR_RI so we won't try to recover.
389 */
390 if ((be64_to_cpu(regs->msr) &
391 (MSR_LE|MSR_RI|MSR_DR|MSR_IR|MSR_ME|MSR_PR|
392 MSR_ILE|MSR_HV|MSR_SF)) == (MSR_DR|MSR_SF)) {
393 regs->nip = be64_to_cpu((__be64)regs->nip);
394 regs->msr = 0;
395 }
396#endif
397
1da177e4
LT
398 if (fwnmi_active) {
399 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
400 if (errhdr) {
401 /* XXX Should look at FWNMI information */
402 }
403 fwnmi_release_errinfo();
404 }
102c05e8
NP
405
406 if (smp_handle_nmi_ipi(regs))
407 return 1;
408
c902be71 409 return 0; /* need to perform reset */
1da177e4
LT
410}
411
412/*
413 * See if we can recover from a machine check exception.
414 * This is only called on power4 (or above) and only via
415 * the Firmware Non-Maskable Interrupts (fwnmi) handler
416 * which provides the error analysis for us.
417 *
418 * Return 1 if corrected (or delivered a signal).
419 * Return 0 if there is nothing we can do.
420 */
d47d1d8a 421static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
1da177e4 422{
d47d1d8a 423 int recovered = 0;
a08a53ea 424 int disposition = rtas_error_disposition(err);
1da177e4 425
d47d1d8a
AB
426 if (!(regs->msr & MSR_RI)) {
427 /* If MSR_RI isn't set, we cannot recover */
428 recovered = 0;
429
a08a53ea 430 } else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
1da177e4 431 /* Platform corrected itself */
d47d1d8a
AB
432 recovered = 1;
433
a08a53ea 434 } else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
d47d1d8a
AB
435 /* Platform corrected itself but could be degraded */
436 printk(KERN_ERR "MCE: limited recovery, system may "
437 "be degraded\n");
438 recovered = 1;
439
440 } else if (user_mode(regs) && !is_global_init(current) &&
a08a53ea 441 rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
d47d1d8a
AB
442
443 /*
444 * If we received a synchronous error when in userspace
445 * kill the task. Firmware may report details of the fail
446 * asynchronously, so we can't rely on the target and type
447 * fields being valid here.
448 */
449 printk(KERN_ERR "MCE: uncorrectable error, killing task "
450 "%s:%d\n", current->comm, current->pid);
451
452 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
453 recovered = 1;
1da177e4
LT
454 }
455
3f9793e6 456 log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
1da177e4 457
d47d1d8a 458 return recovered;
1da177e4
LT
459}
460
461/*
462 * Handle a machine check.
463 *
464 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
465 * should be present. If so the handler which called us tells us if the
466 * error was recovered (never true if RI=0).
467 *
468 * On hardware prior to Power 4 these exceptions were asynchronous which
469 * means we can't tell exactly where it occurred and so we can't recover.
470 */
471int pSeries_machine_check_exception(struct pt_regs *regs)
472{
473 struct rtas_error_log *errp;
474
475 if (fwnmi_active) {
476 errp = fwnmi_get_errinfo(regs);
477 fwnmi_release_errinfo();
478 if (errp && recover_mce(regs, errp))
479 return 1;
480 }
481
482 return 0;
483}