disable some mediatekl custom warnings
[GitHub/mt8127/android_kernel_alcatel_ttab.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
7d12e780
DH
43static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
44static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
1da177e4 45
0ebfff14 46
1da177e4
LT
47/*
48 * Initialize handlers for the set of interrupts caused by hardware errors
49 * and power system events.
50 */
51static int __init init_ras_IRQ(void)
52{
53 struct device_node *np;
54
1da177e4
LT
55 ras_check_exception_token = rtas_token("check-exception");
56
57 /* Internal Errors */
58 np = of_find_node_by_path("/event-sources/internal-errors");
59 if (np != NULL) {
32c96f77
MN
60 request_event_sources_irqs(np, ras_error_interrupt,
61 "RAS_ERROR");
1da177e4
LT
62 of_node_put(np);
63 }
64
65 /* EPOW Events */
66 np = of_find_node_by_path("/event-sources/epow-events");
67 if (np != NULL) {
32c96f77 68 request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
1da177e4
LT
69 of_node_put(np);
70 }
71
69ed3324 72 return 0;
1da177e4 73}
55fc0c56 74subsys_initcall(init_ras_IRQ);
1da177e4 75
55fc0c56
AB
76#define EPOW_SHUTDOWN_NORMAL 1
77#define EPOW_SHUTDOWN_ON_UPS 2
78#define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
79#define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
80
81static void handle_system_shutdown(char event_modifier)
82{
83 switch (event_modifier) {
84 case EPOW_SHUTDOWN_NORMAL:
85 pr_emerg("Firmware initiated power off");
86 orderly_poweroff(1);
87 break;
88
89 case EPOW_SHUTDOWN_ON_UPS:
90 pr_emerg("Loss of power reported by firmware, system is "
91 "running on UPS/battery");
92 break;
93
94 case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
95 pr_emerg("Loss of system critical functions reported by "
96 "firmware");
97 pr_emerg("Check RTAS error log for details");
98 orderly_poweroff(1);
99 break;
100
101 case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
102 pr_emerg("Ambient temperature too high reported by firmware");
103 pr_emerg("Check RTAS error log for details");
104 orderly_poweroff(1);
105 break;
106
107 default:
108 pr_err("Unknown power/cooling shutdown event (modifier %d)",
109 event_modifier);
110 }
111}
112
113struct epow_errorlog {
114 unsigned char sensor_value;
115 unsigned char event_modifier;
116 unsigned char extended_modifier;
117 unsigned char reserved;
118 unsigned char platform_reason;
119};
120
121#define EPOW_RESET 0
122#define EPOW_WARN_COOLING 1
123#define EPOW_WARN_POWER 2
124#define EPOW_SYSTEM_SHUTDOWN 3
125#define EPOW_SYSTEM_HALT 4
126#define EPOW_MAIN_ENCLOSURE 5
127#define EPOW_POWER_OFF 7
128
129void rtas_parse_epow_errlog(struct rtas_error_log *log)
130{
131 struct pseries_errorlog *pseries_log;
132 struct epow_errorlog *epow_log;
133 char action_code;
134 char modifier;
135
136 pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
137 if (pseries_log == NULL)
138 return;
139
140 epow_log = (struct epow_errorlog *)pseries_log->data;
141 action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
142 modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
143
144 switch (action_code) {
145 case EPOW_RESET:
146 pr_err("Non critical power or cooling issue cleared");
147 break;
148
149 case EPOW_WARN_COOLING:
150 pr_err("Non critical cooling issue reported by firmware");
151 pr_err("Check RTAS error log for details");
152 break;
153
154 case EPOW_WARN_POWER:
155 pr_err("Non critical power issue reported by firmware");
156 pr_err("Check RTAS error log for details");
157 break;
158
159 case EPOW_SYSTEM_SHUTDOWN:
160 handle_system_shutdown(epow_log->event_modifier);
161 break;
162
163 case EPOW_SYSTEM_HALT:
164 pr_emerg("Firmware initiated power off");
165 orderly_poweroff(1);
166 break;
167
168 case EPOW_MAIN_ENCLOSURE:
169 case EPOW_POWER_OFF:
170 pr_emerg("Critical power/cooling issue reported by firmware");
171 pr_emerg("Check RTAS error log for details");
172 pr_emerg("Immediate power off");
173 emergency_sync();
174 kernel_power_off();
175 break;
176
177 default:
178 pr_err("Unknown power/cooling event (action code %d)",
179 action_code);
180 }
181}
182
183/* Handle environmental and power warning (EPOW) interrupts. */
7d12e780 184static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
1da177e4 185{
55fc0c56
AB
186 int status;
187 int state;
1da177e4
LT
188 int critical;
189
587f83e8 190 status = rtas_get_sensor(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX, &state);
1da177e4
LT
191
192 if (state > 3)
55fc0c56 193 critical = 1; /* Time Critical */
1da177e4
LT
194 else
195 critical = 0;
196
197 spin_lock(&ras_log_buf_lock);
198
199 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
b08e281b 200 RTAS_VECTOR_EXTERNAL_INTERRUPT,
476eb491 201 virq_to_hw(irq),
6f43747f 202 RTAS_EPOW_WARNING,
1da177e4
LT
203 critical, __pa(&ras_log_buf),
204 rtas_get_error_log_max());
205
1da177e4
LT
206 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
207
55fc0c56
AB
208 rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
209
1da177e4
LT
210 spin_unlock(&ras_log_buf_lock);
211 return IRQ_HANDLED;
212}
213
214/*
215 * Handle hardware error interrupts.
216 *
217 * RTAS check-exception is called to collect data on the exception. If
218 * the error is deemed recoverable, we log a warning and return.
219 * For nonrecoverable errors, an error is logged and we stop all processing
220 * as quickly as possible in order to prevent propagation of the failure.
221 */
7d12e780 222static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
1da177e4
LT
223{
224 struct rtas_error_log *rtas_elog;
cc8b5263 225 int status;
1da177e4
LT
226 int fatal;
227
228 spin_lock(&ras_log_buf_lock);
229
230 status = rtas_call(ras_check_exception_token, 6, 1, NULL,
b08e281b 231 RTAS_VECTOR_EXTERNAL_INTERRUPT,
476eb491 232 virq_to_hw(irq),
cc8b5263 233 RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
1da177e4
LT
234 __pa(&ras_log_buf),
235 rtas_get_error_log_max());
236
237 rtas_elog = (struct rtas_error_log *)ras_log_buf;
238
239 if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
240 fatal = 1;
241 else
242 fatal = 0;
243
244 /* format and print the extended information */
245 log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
246
247 if (fatal) {
cc8b5263
AB
248 pr_emerg("Fatal hardware error reported by firmware");
249 pr_emerg("Check RTAS error log for details");
250 pr_emerg("Immediate power off");
251 emergency_sync();
252 kernel_power_off();
1da177e4 253 } else {
cc8b5263 254 pr_err("Recoverable hardware error reported by firmware");
1da177e4
LT
255 }
256
257 spin_unlock(&ras_log_buf_lock);
258 return IRQ_HANDLED;
259}
260
d368514c
AB
261/*
262 * Some versions of FWNMI place the buffer inside the 4kB page starting at
263 * 0x7000. Other versions place it inside the rtas buffer. We check both.
264 */
265#define VALID_FWNMI_BUFFER(A) \
266 ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
267 (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
268
269/*
270 * Get the error information for errors coming through the
1da177e4
LT
271 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
272 * the actual r3 if possible, and a ptr to the error log entry
273 * will be returned if found.
274 *
d368514c
AB
275 * If the RTAS error is not of the extended type, then we put it in a per
276 * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
277 *
278 * The global_mce_data_buf does not have any locks or protection around it,
1da177e4
LT
279 * if a second machine check comes in, or a system reset is done
280 * before we have logged the error, then we will get corruption in the
281 * error log. This is preferable over holding off on calling
282 * ibm,nmi-interlock which would result in us checkstopping if a
283 * second machine check did come in.
284 */
285static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
286{
1da177e4 287 unsigned long *savep;
d368514c 288 struct rtas_error_log *h, *errhdr = NULL;
1da177e4 289
d368514c 290 if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
f0e939ae 291 printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
d368514c
AB
292 return NULL;
293 }
294
295 savep = __va(regs->gpr[3]);
296 regs->gpr[3] = savep[0]; /* restore original r3 */
297
298 /* If it isn't an extended log we can use the per cpu 64bit buffer */
299 h = (struct rtas_error_log *)&savep[1];
300 if (!h->extended) {
301 memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
302 errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
1da177e4 303 } else {
d368514c
AB
304 int len;
305
306 len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
307 memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
308 memcpy(global_mce_data_buf, h, len);
309 errhdr = (struct rtas_error_log *)global_mce_data_buf;
1da177e4 310 }
d368514c 311
1da177e4
LT
312 return errhdr;
313}
314
315/* Call this when done with the data returned by FWNMI_get_errinfo.
316 * It will release the saved data area for other CPUs in the
317 * partition to receive FWNMI errors.
318 */
319static void fwnmi_release_errinfo(void)
320{
321 int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
322 if (ret != 0)
d368514c 323 printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
1da177e4
LT
324}
325
c902be71 326int pSeries_system_reset_exception(struct pt_regs *regs)
1da177e4
LT
327{
328 if (fwnmi_active) {
329 struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
330 if (errhdr) {
331 /* XXX Should look at FWNMI information */
332 }
333 fwnmi_release_errinfo();
334 }
c902be71 335 return 0; /* need to perform reset */
1da177e4
LT
336}
337
338/*
339 * See if we can recover from a machine check exception.
340 * This is only called on power4 (or above) and only via
341 * the Firmware Non-Maskable Interrupts (fwnmi) handler
342 * which provides the error analysis for us.
343 *
344 * Return 1 if corrected (or delivered a signal).
345 * Return 0 if there is nothing we can do.
346 */
d47d1d8a 347static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
1da177e4 348{
d47d1d8a 349 int recovered = 0;
1da177e4 350
d47d1d8a
AB
351 if (!(regs->msr & MSR_RI)) {
352 /* If MSR_RI isn't set, we cannot recover */
353 recovered = 0;
354
355 } else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
1da177e4 356 /* Platform corrected itself */
d47d1d8a
AB
357 recovered = 1;
358
359 } else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
360 /* Platform corrected itself but could be degraded */
361 printk(KERN_ERR "MCE: limited recovery, system may "
362 "be degraded\n");
363 recovered = 1;
364
365 } else if (user_mode(regs) && !is_global_init(current) &&
366 err->severity == RTAS_SEVERITY_ERROR_SYNC) {
367
368 /*
369 * If we received a synchronous error when in userspace
370 * kill the task. Firmware may report details of the fail
371 * asynchronously, so we can't rely on the target and type
372 * fields being valid here.
373 */
374 printk(KERN_ERR "MCE: uncorrectable error, killing task "
375 "%s:%d\n", current->comm, current->pid);
376
377 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
378 recovered = 1;
1da177e4
LT
379 }
380
3f9793e6 381 log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
1da177e4 382
d47d1d8a 383 return recovered;
1da177e4
LT
384}
385
386/*
387 * Handle a machine check.
388 *
389 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
390 * should be present. If so the handler which called us tells us if the
391 * error was recovered (never true if RI=0).
392 *
393 * On hardware prior to Power 4 these exceptions were asynchronous which
394 * means we can't tell exactly where it occurred and so we can't recover.
395 */
396int pSeries_machine_check_exception(struct pt_regs *regs)
397{
398 struct rtas_error_log *errp;
399
400 if (fwnmi_active) {
401 errp = fwnmi_get_errinfo(regs);
402 fwnmi_release_errinfo();
403 if (errp && recover_mce(regs, errp))
404 return 1;
405 }
406
407 return 0;
408}