xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/pseries/ras.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2001 Dave Engebretsen IBM Corporation
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/sched.h>
7*4882a593Smuzhiyun #include <linux/interrupt.h>
8*4882a593Smuzhiyun #include <linux/irq.h>
9*4882a593Smuzhiyun #include <linux/of.h>
10*4882a593Smuzhiyun #include <linux/fs.h>
11*4882a593Smuzhiyun #include <linux/reboot.h>
12*4882a593Smuzhiyun #include <linux/irq_work.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <asm/machdep.h>
15*4882a593Smuzhiyun #include <asm/rtas.h>
16*4882a593Smuzhiyun #include <asm/firmware.h>
17*4882a593Smuzhiyun #include <asm/mce.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "pseries.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
22*4882a593Smuzhiyun static DEFINE_SPINLOCK(ras_log_buf_lock);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static int ras_check_exception_token;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static void mce_process_errlog_event(struct irq_work *work);
27*4882a593Smuzhiyun static struct irq_work mce_errlog_process_work = {
28*4882a593Smuzhiyun 	.func = mce_process_errlog_event,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define EPOW_SENSOR_TOKEN	9
32*4882a593Smuzhiyun #define EPOW_SENSOR_INDEX	0
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* EPOW events counter variable */
35*4882a593Smuzhiyun static int num_epow_events;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id);
38*4882a593Smuzhiyun static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
39*4882a593Smuzhiyun static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /* RTAS pseries MCE errorlog section. */
42*4882a593Smuzhiyun struct pseries_mc_errorlog {
43*4882a593Smuzhiyun 	__be32	fru_id;
44*4882a593Smuzhiyun 	__be32	proc_id;
45*4882a593Smuzhiyun 	u8	error_type;
46*4882a593Smuzhiyun 	/*
47*4882a593Smuzhiyun 	 * sub_err_type (1 byte). Bit fields depends on error_type
48*4882a593Smuzhiyun 	 *
49*4882a593Smuzhiyun 	 *   MSB0
50*4882a593Smuzhiyun 	 *   |
51*4882a593Smuzhiyun 	 *   V
52*4882a593Smuzhiyun 	 *   01234567
53*4882a593Smuzhiyun 	 *   XXXXXXXX
54*4882a593Smuzhiyun 	 *
55*4882a593Smuzhiyun 	 * For error_type == MC_ERROR_TYPE_UE
56*4882a593Smuzhiyun 	 *   XXXXXXXX
57*4882a593Smuzhiyun 	 *   X		1: Permanent or Transient UE.
58*4882a593Smuzhiyun 	 *    X		1: Effective address provided.
59*4882a593Smuzhiyun 	 *     X	1: Logical address provided.
60*4882a593Smuzhiyun 	 *      XX	2: Reserved.
61*4882a593Smuzhiyun 	 *        XXX	3: Type of UE error.
62*4882a593Smuzhiyun 	 *
63*4882a593Smuzhiyun 	 * For error_type != MC_ERROR_TYPE_UE
64*4882a593Smuzhiyun 	 *   XXXXXXXX
65*4882a593Smuzhiyun 	 *   X		1: Effective address provided.
66*4882a593Smuzhiyun 	 *    XXXXX	5: Reserved.
67*4882a593Smuzhiyun 	 *         XX	2: Type of SLB/ERAT/TLB error.
68*4882a593Smuzhiyun 	 */
69*4882a593Smuzhiyun 	u8	sub_err_type;
70*4882a593Smuzhiyun 	u8	reserved_1[6];
71*4882a593Smuzhiyun 	__be64	effective_address;
72*4882a593Smuzhiyun 	__be64	logical_address;
73*4882a593Smuzhiyun } __packed;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* RTAS pseries MCE error types */
76*4882a593Smuzhiyun #define MC_ERROR_TYPE_UE		0x00
77*4882a593Smuzhiyun #define MC_ERROR_TYPE_SLB		0x01
78*4882a593Smuzhiyun #define MC_ERROR_TYPE_ERAT		0x02
79*4882a593Smuzhiyun #define MC_ERROR_TYPE_UNKNOWN		0x03
80*4882a593Smuzhiyun #define MC_ERROR_TYPE_TLB		0x04
81*4882a593Smuzhiyun #define MC_ERROR_TYPE_D_CACHE		0x05
82*4882a593Smuzhiyun #define MC_ERROR_TYPE_I_CACHE		0x07
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* RTAS pseries MCE error sub types */
85*4882a593Smuzhiyun #define MC_ERROR_UE_INDETERMINATE		0
86*4882a593Smuzhiyun #define MC_ERROR_UE_IFETCH			1
87*4882a593Smuzhiyun #define MC_ERROR_UE_PAGE_TABLE_WALK_IFETCH	2
88*4882a593Smuzhiyun #define MC_ERROR_UE_LOAD_STORE			3
89*4882a593Smuzhiyun #define MC_ERROR_UE_PAGE_TABLE_WALK_LOAD_STORE	4
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #define UE_EFFECTIVE_ADDR_PROVIDED		0x40
92*4882a593Smuzhiyun #define UE_LOGICAL_ADDR_PROVIDED		0x20
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define MC_ERROR_SLB_PARITY		0
95*4882a593Smuzhiyun #define MC_ERROR_SLB_MULTIHIT		1
96*4882a593Smuzhiyun #define MC_ERROR_SLB_INDETERMINATE	2
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun #define MC_ERROR_ERAT_PARITY		1
99*4882a593Smuzhiyun #define MC_ERROR_ERAT_MULTIHIT		2
100*4882a593Smuzhiyun #define MC_ERROR_ERAT_INDETERMINATE	3
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #define MC_ERROR_TLB_PARITY		1
103*4882a593Smuzhiyun #define MC_ERROR_TLB_MULTIHIT		2
104*4882a593Smuzhiyun #define MC_ERROR_TLB_INDETERMINATE	3
105*4882a593Smuzhiyun 
rtas_mc_error_sub_type(const struct pseries_mc_errorlog * mlog)106*4882a593Smuzhiyun static inline u8 rtas_mc_error_sub_type(const struct pseries_mc_errorlog *mlog)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	switch (mlog->error_type) {
109*4882a593Smuzhiyun 	case	MC_ERROR_TYPE_UE:
110*4882a593Smuzhiyun 		return (mlog->sub_err_type & 0x07);
111*4882a593Smuzhiyun 	case	MC_ERROR_TYPE_SLB:
112*4882a593Smuzhiyun 	case	MC_ERROR_TYPE_ERAT:
113*4882a593Smuzhiyun 	case	MC_ERROR_TYPE_TLB:
114*4882a593Smuzhiyun 		return (mlog->sub_err_type & 0x03);
115*4882a593Smuzhiyun 	default:
116*4882a593Smuzhiyun 		return 0;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun  * Enable the hotplug interrupt late because processing them may touch other
122*4882a593Smuzhiyun  * devices or systems (e.g. hugepages) that have not been initialized at the
123*4882a593Smuzhiyun  * subsys stage.
124*4882a593Smuzhiyun  */
init_ras_hotplug_IRQ(void)125*4882a593Smuzhiyun int __init init_ras_hotplug_IRQ(void)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	struct device_node *np;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	/* Hotplug Events */
130*4882a593Smuzhiyun 	np = of_find_node_by_path("/event-sources/hot-plug-events");
131*4882a593Smuzhiyun 	if (np != NULL) {
132*4882a593Smuzhiyun 		if (dlpar_workqueue_init() == 0)
133*4882a593Smuzhiyun 			request_event_sources_irqs(np, ras_hotplug_interrupt,
134*4882a593Smuzhiyun 						   "RAS_HOTPLUG");
135*4882a593Smuzhiyun 		of_node_put(np);
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun machine_late_initcall(pseries, init_ras_hotplug_IRQ);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun  * Initialize handlers for the set of interrupts caused by hardware errors
144*4882a593Smuzhiyun  * and power system events.
145*4882a593Smuzhiyun  */
init_ras_IRQ(void)146*4882a593Smuzhiyun static int __init init_ras_IRQ(void)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	struct device_node *np;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	ras_check_exception_token = rtas_token("check-exception");
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	/* Internal Errors */
153*4882a593Smuzhiyun 	np = of_find_node_by_path("/event-sources/internal-errors");
154*4882a593Smuzhiyun 	if (np != NULL) {
155*4882a593Smuzhiyun 		request_event_sources_irqs(np, ras_error_interrupt,
156*4882a593Smuzhiyun 					   "RAS_ERROR");
157*4882a593Smuzhiyun 		of_node_put(np);
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* EPOW Events */
161*4882a593Smuzhiyun 	np = of_find_node_by_path("/event-sources/epow-events");
162*4882a593Smuzhiyun 	if (np != NULL) {
163*4882a593Smuzhiyun 		request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
164*4882a593Smuzhiyun 		of_node_put(np);
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun machine_subsys_initcall(pseries, init_ras_IRQ);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun #define EPOW_SHUTDOWN_NORMAL				1
172*4882a593Smuzhiyun #define EPOW_SHUTDOWN_ON_UPS				2
173*4882a593Smuzhiyun #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS	3
174*4882a593Smuzhiyun #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH	4
175*4882a593Smuzhiyun 
handle_system_shutdown(char event_modifier)176*4882a593Smuzhiyun static void handle_system_shutdown(char event_modifier)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	switch (event_modifier) {
179*4882a593Smuzhiyun 	case EPOW_SHUTDOWN_NORMAL:
180*4882a593Smuzhiyun 		pr_emerg("Power off requested\n");
181*4882a593Smuzhiyun 		orderly_poweroff(true);
182*4882a593Smuzhiyun 		break;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	case EPOW_SHUTDOWN_ON_UPS:
185*4882a593Smuzhiyun 		pr_emerg("Loss of system power detected. System is running on"
186*4882a593Smuzhiyun 			 " UPS/battery. Check RTAS error log for details\n");
187*4882a593Smuzhiyun 		break;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
190*4882a593Smuzhiyun 		pr_emerg("Loss of system critical functions detected. Check"
191*4882a593Smuzhiyun 			 " RTAS error log for details\n");
192*4882a593Smuzhiyun 		orderly_poweroff(true);
193*4882a593Smuzhiyun 		break;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
196*4882a593Smuzhiyun 		pr_emerg("High ambient temperature detected. Check RTAS"
197*4882a593Smuzhiyun 			 " error log for details\n");
198*4882a593Smuzhiyun 		orderly_poweroff(true);
199*4882a593Smuzhiyun 		break;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	default:
202*4882a593Smuzhiyun 		pr_err("Unknown power/cooling shutdown event (modifier = %d)\n",
203*4882a593Smuzhiyun 			event_modifier);
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun struct epow_errorlog {
208*4882a593Smuzhiyun 	unsigned char sensor_value;
209*4882a593Smuzhiyun 	unsigned char event_modifier;
210*4882a593Smuzhiyun 	unsigned char extended_modifier;
211*4882a593Smuzhiyun 	unsigned char reserved;
212*4882a593Smuzhiyun 	unsigned char platform_reason;
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun #define EPOW_RESET			0
216*4882a593Smuzhiyun #define EPOW_WARN_COOLING		1
217*4882a593Smuzhiyun #define EPOW_WARN_POWER			2
218*4882a593Smuzhiyun #define EPOW_SYSTEM_SHUTDOWN		3
219*4882a593Smuzhiyun #define EPOW_SYSTEM_HALT		4
220*4882a593Smuzhiyun #define EPOW_MAIN_ENCLOSURE		5
221*4882a593Smuzhiyun #define EPOW_POWER_OFF			7
222*4882a593Smuzhiyun 
rtas_parse_epow_errlog(struct rtas_error_log * log)223*4882a593Smuzhiyun static void rtas_parse_epow_errlog(struct rtas_error_log *log)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	struct pseries_errorlog *pseries_log;
226*4882a593Smuzhiyun 	struct epow_errorlog *epow_log;
227*4882a593Smuzhiyun 	char action_code;
228*4882a593Smuzhiyun 	char modifier;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
231*4882a593Smuzhiyun 	if (pseries_log == NULL)
232*4882a593Smuzhiyun 		return;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	epow_log = (struct epow_errorlog *)pseries_log->data;
235*4882a593Smuzhiyun 	action_code = epow_log->sensor_value & 0xF;	/* bottom 4 bits */
236*4882a593Smuzhiyun 	modifier = epow_log->event_modifier & 0xF;	/* bottom 4 bits */
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	switch (action_code) {
239*4882a593Smuzhiyun 	case EPOW_RESET:
240*4882a593Smuzhiyun 		if (num_epow_events) {
241*4882a593Smuzhiyun 			pr_info("Non critical power/cooling issue cleared\n");
242*4882a593Smuzhiyun 			num_epow_events--;
243*4882a593Smuzhiyun 		}
244*4882a593Smuzhiyun 		break;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	case EPOW_WARN_COOLING:
247*4882a593Smuzhiyun 		pr_info("Non-critical cooling issue detected. Check RTAS error"
248*4882a593Smuzhiyun 			" log for details\n");
249*4882a593Smuzhiyun 		break;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	case EPOW_WARN_POWER:
252*4882a593Smuzhiyun 		pr_info("Non-critical power issue detected. Check RTAS error"
253*4882a593Smuzhiyun 			" log for details\n");
254*4882a593Smuzhiyun 		break;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	case EPOW_SYSTEM_SHUTDOWN:
257*4882a593Smuzhiyun 		handle_system_shutdown(modifier);
258*4882a593Smuzhiyun 		break;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	case EPOW_SYSTEM_HALT:
261*4882a593Smuzhiyun 		pr_emerg("Critical power/cooling issue detected. Check RTAS"
262*4882a593Smuzhiyun 			 " error log for details. Powering off.\n");
263*4882a593Smuzhiyun 		orderly_poweroff(true);
264*4882a593Smuzhiyun 		break;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	case EPOW_MAIN_ENCLOSURE:
267*4882a593Smuzhiyun 	case EPOW_POWER_OFF:
268*4882a593Smuzhiyun 		pr_emerg("System about to lose power. Check RTAS error log "
269*4882a593Smuzhiyun 			 " for details. Powering off immediately.\n");
270*4882a593Smuzhiyun 		emergency_sync();
271*4882a593Smuzhiyun 		kernel_power_off();
272*4882a593Smuzhiyun 		break;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	default:
275*4882a593Smuzhiyun 		pr_err("Unknown power/cooling event (action code  = %d)\n",
276*4882a593Smuzhiyun 			action_code);
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	/* Increment epow events counter variable */
280*4882a593Smuzhiyun 	if (action_code != EPOW_RESET)
281*4882a593Smuzhiyun 		num_epow_events++;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
ras_hotplug_interrupt(int irq,void * dev_id)284*4882a593Smuzhiyun static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	struct pseries_errorlog *pseries_log;
287*4882a593Smuzhiyun 	struct pseries_hp_errorlog *hp_elog;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	spin_lock(&ras_log_buf_lock);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	rtas_call(ras_check_exception_token, 6, 1, NULL,
292*4882a593Smuzhiyun 		  RTAS_VECTOR_EXTERNAL_INTERRUPT, virq_to_hw(irq),
293*4882a593Smuzhiyun 		  RTAS_HOTPLUG_EVENTS, 0, __pa(&ras_log_buf),
294*4882a593Smuzhiyun 		  rtas_get_error_log_max());
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	pseries_log = get_pseries_errorlog((struct rtas_error_log *)ras_log_buf,
297*4882a593Smuzhiyun 					   PSERIES_ELOG_SECT_ID_HOTPLUG);
298*4882a593Smuzhiyun 	hp_elog = (struct pseries_hp_errorlog *)pseries_log->data;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/*
301*4882a593Smuzhiyun 	 * Since PCI hotplug is not currently supported on pseries, put PCI
302*4882a593Smuzhiyun 	 * hotplug events on the ras_log_buf to be handled by rtas_errd.
303*4882a593Smuzhiyun 	 */
304*4882a593Smuzhiyun 	if (hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_MEM ||
305*4882a593Smuzhiyun 	    hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_CPU ||
306*4882a593Smuzhiyun 	    hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_PMEM)
307*4882a593Smuzhiyun 		queue_hotplug_event(hp_elog);
308*4882a593Smuzhiyun 	else
309*4882a593Smuzhiyun 		log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	spin_unlock(&ras_log_buf_lock);
312*4882a593Smuzhiyun 	return IRQ_HANDLED;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun /* Handle environmental and power warning (EPOW) interrupts. */
ras_epow_interrupt(int irq,void * dev_id)316*4882a593Smuzhiyun static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	int status;
319*4882a593Smuzhiyun 	int state;
320*4882a593Smuzhiyun 	int critical;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	status = rtas_get_sensor_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
323*4882a593Smuzhiyun 				      &state);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (state > 3)
326*4882a593Smuzhiyun 		critical = 1;		/* Time Critical */
327*4882a593Smuzhiyun 	else
328*4882a593Smuzhiyun 		critical = 0;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	spin_lock(&ras_log_buf_lock);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
333*4882a593Smuzhiyun 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
334*4882a593Smuzhiyun 			   virq_to_hw(irq),
335*4882a593Smuzhiyun 			   RTAS_EPOW_WARNING,
336*4882a593Smuzhiyun 			   critical, __pa(&ras_log_buf),
337*4882a593Smuzhiyun 				rtas_get_error_log_max());
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	spin_unlock(&ras_log_buf_lock);
344*4882a593Smuzhiyun 	return IRQ_HANDLED;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun  * Handle hardware error interrupts.
349*4882a593Smuzhiyun  *
350*4882a593Smuzhiyun  * RTAS check-exception is called to collect data on the exception.  If
351*4882a593Smuzhiyun  * the error is deemed recoverable, we log a warning and return.
352*4882a593Smuzhiyun  * For nonrecoverable errors, an error is logged and we stop all processing
353*4882a593Smuzhiyun  * as quickly as possible in order to prevent propagation of the failure.
354*4882a593Smuzhiyun  */
ras_error_interrupt(int irq,void * dev_id)355*4882a593Smuzhiyun static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	struct rtas_error_log *rtas_elog;
358*4882a593Smuzhiyun 	int status;
359*4882a593Smuzhiyun 	int fatal;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	spin_lock(&ras_log_buf_lock);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	status = rtas_call(ras_check_exception_token, 6, 1, NULL,
364*4882a593Smuzhiyun 			   RTAS_VECTOR_EXTERNAL_INTERRUPT,
365*4882a593Smuzhiyun 			   virq_to_hw(irq),
366*4882a593Smuzhiyun 			   RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
367*4882a593Smuzhiyun 			   __pa(&ras_log_buf),
368*4882a593Smuzhiyun 				rtas_get_error_log_max());
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	rtas_elog = (struct rtas_error_log *)ras_log_buf;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (status == 0 &&
373*4882a593Smuzhiyun 	    rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
374*4882a593Smuzhiyun 		fatal = 1;
375*4882a593Smuzhiyun 	else
376*4882a593Smuzhiyun 		fatal = 0;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* format and print the extended information */
379*4882a593Smuzhiyun 	log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	if (fatal) {
382*4882a593Smuzhiyun 		pr_emerg("Fatal hardware error detected. Check RTAS error"
383*4882a593Smuzhiyun 			 " log for details. Powering off immediately\n");
384*4882a593Smuzhiyun 		emergency_sync();
385*4882a593Smuzhiyun 		kernel_power_off();
386*4882a593Smuzhiyun 	} else {
387*4882a593Smuzhiyun 		pr_err("Recoverable hardware error detected\n");
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	spin_unlock(&ras_log_buf_lock);
391*4882a593Smuzhiyun 	return IRQ_HANDLED;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun  * Some versions of FWNMI place the buffer inside the 4kB page starting at
396*4882a593Smuzhiyun  * 0x7000. Other versions place it inside the rtas buffer. We check both.
397*4882a593Smuzhiyun  * Minimum size of the buffer is 16 bytes.
398*4882a593Smuzhiyun  */
399*4882a593Smuzhiyun #define VALID_FWNMI_BUFFER(A) \
400*4882a593Smuzhiyun 	((((A) >= 0x7000) && ((A) <= 0x8000 - 16)) || \
401*4882a593Smuzhiyun 	(((A) >= rtas.base) && ((A) <= (rtas.base + rtas.size - 16))))
402*4882a593Smuzhiyun 
fwnmi_get_errlog(void)403*4882a593Smuzhiyun static inline struct rtas_error_log *fwnmi_get_errlog(void)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	return (struct rtas_error_log *)local_paca->mce_data_buf;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
fwnmi_get_savep(struct pt_regs * regs)408*4882a593Smuzhiyun static __be64 *fwnmi_get_savep(struct pt_regs *regs)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun 	unsigned long savep_ra;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* Mask top two bits */
413*4882a593Smuzhiyun 	savep_ra = regs->gpr[3] & ~(0x3UL << 62);
414*4882a593Smuzhiyun 	if (!VALID_FWNMI_BUFFER(savep_ra)) {
415*4882a593Smuzhiyun 		printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
416*4882a593Smuzhiyun 		return NULL;
417*4882a593Smuzhiyun 	}
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	return __va(savep_ra);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /*
423*4882a593Smuzhiyun  * Get the error information for errors coming through the
424*4882a593Smuzhiyun  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
425*4882a593Smuzhiyun  * the actual r3 if possible, and a ptr to the error log entry
426*4882a593Smuzhiyun  * will be returned if found.
427*4882a593Smuzhiyun  *
428*4882a593Smuzhiyun  * Use one buffer mce_data_buf per cpu to store RTAS error.
429*4882a593Smuzhiyun  *
430*4882a593Smuzhiyun  * The mce_data_buf does not have any locks or protection around it,
431*4882a593Smuzhiyun  * if a second machine check comes in, or a system reset is done
432*4882a593Smuzhiyun  * before we have logged the error, then we will get corruption in the
433*4882a593Smuzhiyun  * error log.  This is preferable over holding off on calling
434*4882a593Smuzhiyun  * ibm,nmi-interlock which would result in us checkstopping if a
435*4882a593Smuzhiyun  * second machine check did come in.
436*4882a593Smuzhiyun  */
fwnmi_get_errinfo(struct pt_regs * regs)437*4882a593Smuzhiyun static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	struct rtas_error_log *h;
440*4882a593Smuzhiyun 	__be64 *savep;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	savep = fwnmi_get_savep(regs);
443*4882a593Smuzhiyun 	if (!savep)
444*4882a593Smuzhiyun 		return NULL;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	regs->gpr[3] = be64_to_cpu(savep[0]); /* restore original r3 */
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	h = (struct rtas_error_log *)&savep[1];
449*4882a593Smuzhiyun 	/* Use the per cpu buffer from paca to store rtas error log */
450*4882a593Smuzhiyun 	memset(local_paca->mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
451*4882a593Smuzhiyun 	if (!rtas_error_extended(h)) {
452*4882a593Smuzhiyun 		memcpy(local_paca->mce_data_buf, h, sizeof(__u64));
453*4882a593Smuzhiyun 	} else {
454*4882a593Smuzhiyun 		int len, error_log_length;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		error_log_length = 8 + rtas_error_extended_log_length(h);
457*4882a593Smuzhiyun 		len = min_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
458*4882a593Smuzhiyun 		memcpy(local_paca->mce_data_buf, h, len);
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	return (struct rtas_error_log *)local_paca->mce_data_buf;
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /* Call this when done with the data returned by FWNMI_get_errinfo.
465*4882a593Smuzhiyun  * It will release the saved data area for other CPUs in the
466*4882a593Smuzhiyun  * partition to receive FWNMI errors.
467*4882a593Smuzhiyun  */
fwnmi_release_errinfo(void)468*4882a593Smuzhiyun static void fwnmi_release_errinfo(void)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	struct rtas_args rtas_args;
471*4882a593Smuzhiyun 	int ret;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/*
474*4882a593Smuzhiyun 	 * On pseries, the machine check stack is limited to under 4GB, so
475*4882a593Smuzhiyun 	 * args can be on-stack.
476*4882a593Smuzhiyun 	 */
477*4882a593Smuzhiyun 	rtas_call_unlocked(&rtas_args, ibm_nmi_interlock_token, 0, 1, NULL);
478*4882a593Smuzhiyun 	ret = be32_to_cpu(rtas_args.rets[0]);
479*4882a593Smuzhiyun 	if (ret != 0)
480*4882a593Smuzhiyun 		printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun 
pSeries_system_reset_exception(struct pt_regs * regs)483*4882a593Smuzhiyun int pSeries_system_reset_exception(struct pt_regs *regs)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun #ifdef __LITTLE_ENDIAN__
486*4882a593Smuzhiyun 	/*
487*4882a593Smuzhiyun 	 * Some firmware byteswaps SRR registers and gives incorrect SRR1. Try
488*4882a593Smuzhiyun 	 * to detect the bad SRR1 pattern here. Flip the NIP back to correct
489*4882a593Smuzhiyun 	 * endian for reporting purposes. Unfortunately the MSR can't be fixed,
490*4882a593Smuzhiyun 	 * so clear it. It will be missing MSR_RI so we won't try to recover.
491*4882a593Smuzhiyun 	 */
492*4882a593Smuzhiyun 	if ((be64_to_cpu(regs->msr) &
493*4882a593Smuzhiyun 			(MSR_LE|MSR_RI|MSR_DR|MSR_IR|MSR_ME|MSR_PR|
494*4882a593Smuzhiyun 			 MSR_ILE|MSR_HV|MSR_SF)) == (MSR_DR|MSR_SF)) {
495*4882a593Smuzhiyun 		regs->nip = be64_to_cpu((__be64)regs->nip);
496*4882a593Smuzhiyun 		regs->msr = 0;
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun #endif
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	if (fwnmi_active) {
501*4882a593Smuzhiyun 		__be64 *savep;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		/*
504*4882a593Smuzhiyun 		 * Firmware (PowerVM and KVM) saves r3 to a save area like
505*4882a593Smuzhiyun 		 * machine check, which is not exactly what PAPR (2.9)
506*4882a593Smuzhiyun 		 * suggests but there is no way to detect otherwise, so this
507*4882a593Smuzhiyun 		 * is the interface now.
508*4882a593Smuzhiyun 		 *
509*4882a593Smuzhiyun 		 * System resets do not save any error log or require an
510*4882a593Smuzhiyun 		 * "ibm,nmi-interlock" rtas call to release.
511*4882a593Smuzhiyun 		 */
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 		savep = fwnmi_get_savep(regs);
514*4882a593Smuzhiyun 		if (savep)
515*4882a593Smuzhiyun 			regs->gpr[3] = be64_to_cpu(savep[0]); /* restore original r3 */
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	if (smp_handle_nmi_ipi(regs))
519*4882a593Smuzhiyun 		return 1;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	return 0; /* need to perform reset */
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
mce_handle_err_realmode(int disposition,u8 error_type)524*4882a593Smuzhiyun static int mce_handle_err_realmode(int disposition, u8 error_type)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun #ifdef CONFIG_PPC_BOOK3S_64
527*4882a593Smuzhiyun 	if (disposition == RTAS_DISP_NOT_RECOVERED) {
528*4882a593Smuzhiyun 		switch (error_type) {
529*4882a593Smuzhiyun 		case	MC_ERROR_TYPE_SLB:
530*4882a593Smuzhiyun 		case	MC_ERROR_TYPE_ERAT:
531*4882a593Smuzhiyun 			/*
532*4882a593Smuzhiyun 			 * Store the old slb content in paca before flushing.
533*4882a593Smuzhiyun 			 * Print this when we go to virtual mode.
534*4882a593Smuzhiyun 			 * There are chances that we may hit MCE again if there
535*4882a593Smuzhiyun 			 * is a parity error on the SLB entry we trying to read
536*4882a593Smuzhiyun 			 * for saving. Hence limit the slb saving to single
537*4882a593Smuzhiyun 			 * level of recursion.
538*4882a593Smuzhiyun 			 */
539*4882a593Smuzhiyun 			if (local_paca->in_mce == 1)
540*4882a593Smuzhiyun 				slb_save_contents(local_paca->mce_faulty_slbs);
541*4882a593Smuzhiyun 			flush_and_reload_slb();
542*4882a593Smuzhiyun 			disposition = RTAS_DISP_FULLY_RECOVERED;
543*4882a593Smuzhiyun 			break;
544*4882a593Smuzhiyun 		default:
545*4882a593Smuzhiyun 			break;
546*4882a593Smuzhiyun 		}
547*4882a593Smuzhiyun 	} else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
548*4882a593Smuzhiyun 		/* Platform corrected itself but could be degraded */
549*4882a593Smuzhiyun 		pr_err("MCE: limited recovery, system may be degraded\n");
550*4882a593Smuzhiyun 		disposition = RTAS_DISP_FULLY_RECOVERED;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun #endif
553*4882a593Smuzhiyun 	return disposition;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
mce_handle_err_virtmode(struct pt_regs * regs,struct rtas_error_log * errp,struct pseries_mc_errorlog * mce_log,int disposition)556*4882a593Smuzhiyun static int mce_handle_err_virtmode(struct pt_regs *regs,
557*4882a593Smuzhiyun 				   struct rtas_error_log *errp,
558*4882a593Smuzhiyun 				   struct pseries_mc_errorlog *mce_log,
559*4882a593Smuzhiyun 				   int disposition)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	struct mce_error_info mce_err = { 0 };
562*4882a593Smuzhiyun 	int initiator = rtas_error_initiator(errp);
563*4882a593Smuzhiyun 	int severity = rtas_error_severity(errp);
564*4882a593Smuzhiyun 	unsigned long eaddr = 0, paddr = 0;
565*4882a593Smuzhiyun 	u8 error_type, err_sub_type;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	if (!mce_log)
568*4882a593Smuzhiyun 		goto out;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	error_type = mce_log->error_type;
571*4882a593Smuzhiyun 	err_sub_type = rtas_mc_error_sub_type(mce_log);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	if (initiator == RTAS_INITIATOR_UNKNOWN)
574*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_UNKNOWN;
575*4882a593Smuzhiyun 	else if (initiator == RTAS_INITIATOR_CPU)
576*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_CPU;
577*4882a593Smuzhiyun 	else if (initiator == RTAS_INITIATOR_PCI)
578*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_PCI;
579*4882a593Smuzhiyun 	else if (initiator == RTAS_INITIATOR_ISA)
580*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_ISA;
581*4882a593Smuzhiyun 	else if (initiator == RTAS_INITIATOR_MEMORY)
582*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_MEMORY;
583*4882a593Smuzhiyun 	else if (initiator == RTAS_INITIATOR_POWERMGM)
584*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_POWERMGM;
585*4882a593Smuzhiyun 	else
586*4882a593Smuzhiyun 		mce_err.initiator = MCE_INITIATOR_UNKNOWN;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (severity == RTAS_SEVERITY_NO_ERROR)
589*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_NO_ERROR;
590*4882a593Smuzhiyun 	else if (severity == RTAS_SEVERITY_EVENT)
591*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_WARNING;
592*4882a593Smuzhiyun 	else if (severity == RTAS_SEVERITY_WARNING)
593*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_WARNING;
594*4882a593Smuzhiyun 	else if (severity == RTAS_SEVERITY_ERROR_SYNC)
595*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_SEVERE;
596*4882a593Smuzhiyun 	else if (severity == RTAS_SEVERITY_ERROR)
597*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_SEVERE;
598*4882a593Smuzhiyun 	else if (severity == RTAS_SEVERITY_FATAL)
599*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_FATAL;
600*4882a593Smuzhiyun 	else
601*4882a593Smuzhiyun 		mce_err.severity = MCE_SEV_FATAL;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	if (severity <= RTAS_SEVERITY_ERROR_SYNC)
604*4882a593Smuzhiyun 		mce_err.sync_error = true;
605*4882a593Smuzhiyun 	else
606*4882a593Smuzhiyun 		mce_err.sync_error = false;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	mce_err.error_type = MCE_ERROR_TYPE_UNKNOWN;
609*4882a593Smuzhiyun 	mce_err.error_class = MCE_ECLASS_UNKNOWN;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	switch (error_type) {
612*4882a593Smuzhiyun 	case MC_ERROR_TYPE_UE:
613*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_UE;
614*4882a593Smuzhiyun 		mce_common_process_ue(regs, &mce_err);
615*4882a593Smuzhiyun 		if (mce_err.ignore_event)
616*4882a593Smuzhiyun 			disposition = RTAS_DISP_FULLY_RECOVERED;
617*4882a593Smuzhiyun 		switch (err_sub_type) {
618*4882a593Smuzhiyun 		case MC_ERROR_UE_IFETCH:
619*4882a593Smuzhiyun 			mce_err.u.ue_error_type = MCE_UE_ERROR_IFETCH;
620*4882a593Smuzhiyun 			break;
621*4882a593Smuzhiyun 		case MC_ERROR_UE_PAGE_TABLE_WALK_IFETCH:
622*4882a593Smuzhiyun 			mce_err.u.ue_error_type = MCE_UE_ERROR_PAGE_TABLE_WALK_IFETCH;
623*4882a593Smuzhiyun 			break;
624*4882a593Smuzhiyun 		case MC_ERROR_UE_LOAD_STORE:
625*4882a593Smuzhiyun 			mce_err.u.ue_error_type = MCE_UE_ERROR_LOAD_STORE;
626*4882a593Smuzhiyun 			break;
627*4882a593Smuzhiyun 		case MC_ERROR_UE_PAGE_TABLE_WALK_LOAD_STORE:
628*4882a593Smuzhiyun 			mce_err.u.ue_error_type = MCE_UE_ERROR_PAGE_TABLE_WALK_LOAD_STORE;
629*4882a593Smuzhiyun 			break;
630*4882a593Smuzhiyun 		case MC_ERROR_UE_INDETERMINATE:
631*4882a593Smuzhiyun 		default:
632*4882a593Smuzhiyun 			mce_err.u.ue_error_type = MCE_UE_ERROR_INDETERMINATE;
633*4882a593Smuzhiyun 			break;
634*4882a593Smuzhiyun 		}
635*4882a593Smuzhiyun 		if (mce_log->sub_err_type & UE_EFFECTIVE_ADDR_PROVIDED)
636*4882a593Smuzhiyun 			eaddr = be64_to_cpu(mce_log->effective_address);
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 		if (mce_log->sub_err_type & UE_LOGICAL_ADDR_PROVIDED) {
639*4882a593Smuzhiyun 			paddr = be64_to_cpu(mce_log->logical_address);
640*4882a593Smuzhiyun 		} else if (mce_log->sub_err_type & UE_EFFECTIVE_ADDR_PROVIDED) {
641*4882a593Smuzhiyun 			unsigned long pfn;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 			pfn = addr_to_pfn(regs, eaddr);
644*4882a593Smuzhiyun 			if (pfn != ULONG_MAX)
645*4882a593Smuzhiyun 				paddr = pfn << PAGE_SHIFT;
646*4882a593Smuzhiyun 		}
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		break;
649*4882a593Smuzhiyun 	case MC_ERROR_TYPE_SLB:
650*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_SLB;
651*4882a593Smuzhiyun 		switch (err_sub_type) {
652*4882a593Smuzhiyun 		case MC_ERROR_SLB_PARITY:
653*4882a593Smuzhiyun 			mce_err.u.slb_error_type = MCE_SLB_ERROR_PARITY;
654*4882a593Smuzhiyun 			break;
655*4882a593Smuzhiyun 		case MC_ERROR_SLB_MULTIHIT:
656*4882a593Smuzhiyun 			mce_err.u.slb_error_type = MCE_SLB_ERROR_MULTIHIT;
657*4882a593Smuzhiyun 			break;
658*4882a593Smuzhiyun 		case MC_ERROR_SLB_INDETERMINATE:
659*4882a593Smuzhiyun 		default:
660*4882a593Smuzhiyun 			mce_err.u.slb_error_type = MCE_SLB_ERROR_INDETERMINATE;
661*4882a593Smuzhiyun 			break;
662*4882a593Smuzhiyun 		}
663*4882a593Smuzhiyun 		if (mce_log->sub_err_type & 0x80)
664*4882a593Smuzhiyun 			eaddr = be64_to_cpu(mce_log->effective_address);
665*4882a593Smuzhiyun 		break;
666*4882a593Smuzhiyun 	case MC_ERROR_TYPE_ERAT:
667*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_ERAT;
668*4882a593Smuzhiyun 		switch (err_sub_type) {
669*4882a593Smuzhiyun 		case MC_ERROR_ERAT_PARITY:
670*4882a593Smuzhiyun 			mce_err.u.erat_error_type = MCE_ERAT_ERROR_PARITY;
671*4882a593Smuzhiyun 			break;
672*4882a593Smuzhiyun 		case MC_ERROR_ERAT_MULTIHIT:
673*4882a593Smuzhiyun 			mce_err.u.erat_error_type = MCE_ERAT_ERROR_MULTIHIT;
674*4882a593Smuzhiyun 			break;
675*4882a593Smuzhiyun 		case MC_ERROR_ERAT_INDETERMINATE:
676*4882a593Smuzhiyun 		default:
677*4882a593Smuzhiyun 			mce_err.u.erat_error_type = MCE_ERAT_ERROR_INDETERMINATE;
678*4882a593Smuzhiyun 			break;
679*4882a593Smuzhiyun 		}
680*4882a593Smuzhiyun 		if (mce_log->sub_err_type & 0x80)
681*4882a593Smuzhiyun 			eaddr = be64_to_cpu(mce_log->effective_address);
682*4882a593Smuzhiyun 		break;
683*4882a593Smuzhiyun 	case MC_ERROR_TYPE_TLB:
684*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_TLB;
685*4882a593Smuzhiyun 		switch (err_sub_type) {
686*4882a593Smuzhiyun 		case MC_ERROR_TLB_PARITY:
687*4882a593Smuzhiyun 			mce_err.u.tlb_error_type = MCE_TLB_ERROR_PARITY;
688*4882a593Smuzhiyun 			break;
689*4882a593Smuzhiyun 		case MC_ERROR_TLB_MULTIHIT:
690*4882a593Smuzhiyun 			mce_err.u.tlb_error_type = MCE_TLB_ERROR_MULTIHIT;
691*4882a593Smuzhiyun 			break;
692*4882a593Smuzhiyun 		case MC_ERROR_TLB_INDETERMINATE:
693*4882a593Smuzhiyun 		default:
694*4882a593Smuzhiyun 			mce_err.u.tlb_error_type = MCE_TLB_ERROR_INDETERMINATE;
695*4882a593Smuzhiyun 			break;
696*4882a593Smuzhiyun 		}
697*4882a593Smuzhiyun 		if (mce_log->sub_err_type & 0x80)
698*4882a593Smuzhiyun 			eaddr = be64_to_cpu(mce_log->effective_address);
699*4882a593Smuzhiyun 		break;
700*4882a593Smuzhiyun 	case MC_ERROR_TYPE_D_CACHE:
701*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_DCACHE;
702*4882a593Smuzhiyun 		break;
703*4882a593Smuzhiyun 	case MC_ERROR_TYPE_I_CACHE:
704*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_DCACHE;
705*4882a593Smuzhiyun 		break;
706*4882a593Smuzhiyun 	case MC_ERROR_TYPE_UNKNOWN:
707*4882a593Smuzhiyun 	default:
708*4882a593Smuzhiyun 		mce_err.error_type = MCE_ERROR_TYPE_UNKNOWN;
709*4882a593Smuzhiyun 		break;
710*4882a593Smuzhiyun 	}
711*4882a593Smuzhiyun out:
712*4882a593Smuzhiyun 	save_mce_event(regs, disposition == RTAS_DISP_FULLY_RECOVERED,
713*4882a593Smuzhiyun 		       &mce_err, regs->nip, eaddr, paddr);
714*4882a593Smuzhiyun 	return disposition;
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun 
mce_handle_error(struct pt_regs * regs,struct rtas_error_log * errp)717*4882a593Smuzhiyun static int mce_handle_error(struct pt_regs *regs, struct rtas_error_log *errp)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	struct pseries_errorlog *pseries_log;
720*4882a593Smuzhiyun 	struct pseries_mc_errorlog *mce_log = NULL;
721*4882a593Smuzhiyun 	int disposition = rtas_error_disposition(errp);
722*4882a593Smuzhiyun 	u8 error_type;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	if (!rtas_error_extended(errp))
725*4882a593Smuzhiyun 		goto out;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	pseries_log = get_pseries_errorlog(errp, PSERIES_ELOG_SECT_ID_MCE);
728*4882a593Smuzhiyun 	if (!pseries_log)
729*4882a593Smuzhiyun 		goto out;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	mce_log = (struct pseries_mc_errorlog *)pseries_log->data;
732*4882a593Smuzhiyun 	error_type = mce_log->error_type;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	disposition = mce_handle_err_realmode(disposition, error_type);
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	/*
737*4882a593Smuzhiyun 	 * Enable translation as we will be accessing per-cpu variables
738*4882a593Smuzhiyun 	 * in save_mce_event() which may fall outside RMO region, also
739*4882a593Smuzhiyun 	 * leave it enabled because subsequently we will be queuing work
740*4882a593Smuzhiyun 	 * to workqueues where again per-cpu variables accessed, besides
741*4882a593Smuzhiyun 	 * fwnmi_release_errinfo() crashes when called in realmode on
742*4882a593Smuzhiyun 	 * pseries.
743*4882a593Smuzhiyun 	 * Note: All the realmode handling like flushing SLB entries for
744*4882a593Smuzhiyun 	 *       SLB multihit is done by now.
745*4882a593Smuzhiyun 	 */
746*4882a593Smuzhiyun out:
747*4882a593Smuzhiyun 	mtmsr(mfmsr() | MSR_IR | MSR_DR);
748*4882a593Smuzhiyun 	disposition = mce_handle_err_virtmode(regs, errp, mce_log,
749*4882a593Smuzhiyun 					      disposition);
750*4882a593Smuzhiyun 	return disposition;
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun /*
754*4882a593Smuzhiyun  * Process MCE rtas errlog event.
755*4882a593Smuzhiyun  */
mce_process_errlog_event(struct irq_work * work)756*4882a593Smuzhiyun static void mce_process_errlog_event(struct irq_work *work)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun 	struct rtas_error_log *err;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	err = fwnmi_get_errlog();
761*4882a593Smuzhiyun 	log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun /*
765*4882a593Smuzhiyun  * See if we can recover from a machine check exception.
766*4882a593Smuzhiyun  * This is only called on power4 (or above) and only via
767*4882a593Smuzhiyun  * the Firmware Non-Maskable Interrupts (fwnmi) handler
768*4882a593Smuzhiyun  * which provides the error analysis for us.
769*4882a593Smuzhiyun  *
770*4882a593Smuzhiyun  * Return 1 if corrected (or delivered a signal).
771*4882a593Smuzhiyun  * Return 0 if there is nothing we can do.
772*4882a593Smuzhiyun  */
recover_mce(struct pt_regs * regs,struct machine_check_event * evt)773*4882a593Smuzhiyun static int recover_mce(struct pt_regs *regs, struct machine_check_event *evt)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun 	int recovered = 0;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	if (!(regs->msr & MSR_RI)) {
778*4882a593Smuzhiyun 		/* If MSR_RI isn't set, we cannot recover */
779*4882a593Smuzhiyun 		pr_err("Machine check interrupt unrecoverable: MSR(RI=0)\n");
780*4882a593Smuzhiyun 		recovered = 0;
781*4882a593Smuzhiyun 	} else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
782*4882a593Smuzhiyun 		/* Platform corrected itself */
783*4882a593Smuzhiyun 		recovered = 1;
784*4882a593Smuzhiyun 	} else if (evt->severity == MCE_SEV_FATAL) {
785*4882a593Smuzhiyun 		/* Fatal machine check */
786*4882a593Smuzhiyun 		pr_err("Machine check interrupt is fatal\n");
787*4882a593Smuzhiyun 		recovered = 0;
788*4882a593Smuzhiyun 	}
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	if (!recovered && evt->sync_error) {
791*4882a593Smuzhiyun 		/*
792*4882a593Smuzhiyun 		 * Try to kill processes if we get a synchronous machine check
793*4882a593Smuzhiyun 		 * (e.g., one caused by execution of this instruction). This
794*4882a593Smuzhiyun 		 * will devolve into a panic if we try to kill init or are in
795*4882a593Smuzhiyun 		 * an interrupt etc.
796*4882a593Smuzhiyun 		 *
797*4882a593Smuzhiyun 		 * TODO: Queue up this address for hwpoisioning later.
798*4882a593Smuzhiyun 		 * TODO: This is not quite right for d-side machine
799*4882a593Smuzhiyun 		 *       checks ->nip is not necessarily the important
800*4882a593Smuzhiyun 		 *       address.
801*4882a593Smuzhiyun 		 */
802*4882a593Smuzhiyun 		if ((user_mode(regs))) {
803*4882a593Smuzhiyun 			_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
804*4882a593Smuzhiyun 			recovered = 1;
805*4882a593Smuzhiyun 		} else if (die_will_crash()) {
806*4882a593Smuzhiyun 			/*
807*4882a593Smuzhiyun 			 * die() would kill the kernel, so better to go via
808*4882a593Smuzhiyun 			 * the platform reboot code that will log the
809*4882a593Smuzhiyun 			 * machine check.
810*4882a593Smuzhiyun 			 */
811*4882a593Smuzhiyun 			recovered = 0;
812*4882a593Smuzhiyun 		} else {
813*4882a593Smuzhiyun 			die("Machine check", regs, SIGBUS);
814*4882a593Smuzhiyun 			recovered = 1;
815*4882a593Smuzhiyun 		}
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	return recovered;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun /*
822*4882a593Smuzhiyun  * Handle a machine check.
823*4882a593Smuzhiyun  *
824*4882a593Smuzhiyun  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
825*4882a593Smuzhiyun  * should be present.  If so the handler which called us tells us if the
826*4882a593Smuzhiyun  * error was recovered (never true if RI=0).
827*4882a593Smuzhiyun  *
828*4882a593Smuzhiyun  * On hardware prior to Power 4 these exceptions were asynchronous which
829*4882a593Smuzhiyun  * means we can't tell exactly where it occurred and so we can't recover.
830*4882a593Smuzhiyun  */
pSeries_machine_check_exception(struct pt_regs * regs)831*4882a593Smuzhiyun int pSeries_machine_check_exception(struct pt_regs *regs)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	struct machine_check_event evt;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
836*4882a593Smuzhiyun 		return 0;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	/* Print things out */
839*4882a593Smuzhiyun 	if (evt.version != MCE_V1) {
840*4882a593Smuzhiyun 		pr_err("Machine Check Exception, Unknown event version %d !\n",
841*4882a593Smuzhiyun 		       evt.version);
842*4882a593Smuzhiyun 		return 0;
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 	machine_check_print_event_info(&evt, user_mode(regs), false);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	if (recover_mce(regs, &evt))
847*4882a593Smuzhiyun 		return 1;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	return 0;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun 
pseries_machine_check_realmode(struct pt_regs * regs)852*4882a593Smuzhiyun long pseries_machine_check_realmode(struct pt_regs *regs)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	struct rtas_error_log *errp;
855*4882a593Smuzhiyun 	int disposition;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	if (fwnmi_active) {
858*4882a593Smuzhiyun 		errp = fwnmi_get_errinfo(regs);
859*4882a593Smuzhiyun 		/*
860*4882a593Smuzhiyun 		 * Call to fwnmi_release_errinfo() in real mode causes kernel
861*4882a593Smuzhiyun 		 * to panic. Hence we will call it as soon as we go into
862*4882a593Smuzhiyun 		 * virtual mode.
863*4882a593Smuzhiyun 		 */
864*4882a593Smuzhiyun 		disposition = mce_handle_error(regs, errp);
865*4882a593Smuzhiyun 		fwnmi_release_errinfo();
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 		/* Queue irq work to log this rtas event later. */
868*4882a593Smuzhiyun 		irq_work_queue(&mce_errlog_process_work);
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 		if (disposition == RTAS_DISP_FULLY_RECOVERED)
871*4882a593Smuzhiyun 			return 1;
872*4882a593Smuzhiyun 	}
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	return 0;
875*4882a593Smuzhiyun }
876