1 /*
2  * PowerNV OPAL high level interfaces
3  *
4  * Copyright 2011 IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #undef DEBUG
13 
14 #include <linux/types.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <linux/interrupt.h>
18 #include <linux/notifier.h>
19 #include <linux/slab.h>
20 #include <asm/opal.h>
21 #include <asm/firmware.h>
22 
23 #include "powernv.h"
24 
25 struct opal {
26 	u64 base;
27 	u64 entry;
28 } opal;
29 
30 static struct device_node *opal_node;
31 static DEFINE_SPINLOCK(opal_write_lock);
32 extern u64 opal_mc_secondary_handler[];
33 static unsigned int *opal_irqs;
34 static unsigned int opal_irq_count;
35 static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
36 static DEFINE_SPINLOCK(opal_notifier_lock);
37 static uint64_t last_notified_mask = 0x0ul;
38 static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
39 
40 int __init early_init_dt_scan_opal(unsigned long node,
41 				   const char *uname, int depth, void *data)
42 {
43 	const void *basep, *entryp;
44 	unsigned long basesz, entrysz;
45 
46 	if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
47 		return 0;
48 
49 	basep  = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
50 	entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
51 
52 	if (!basep || !entryp)
53 		return 1;
54 
55 	opal.base = of_read_number(basep, basesz/4);
56 	opal.entry = of_read_number(entryp, entrysz/4);
57 
58 	pr_debug("OPAL Base  = 0x%llx (basep=%p basesz=%ld)\n",
59 		 opal.base, basep, basesz);
60 	pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
61 		 opal.entry, entryp, entrysz);
62 
63 	powerpc_firmware_features |= FW_FEATURE_OPAL;
64 	if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
65 		powerpc_firmware_features |= FW_FEATURE_OPALv2;
66 		powerpc_firmware_features |= FW_FEATURE_OPALv3;
67 		printk("OPAL V3 detected !\n");
68 	} else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
69 		powerpc_firmware_features |= FW_FEATURE_OPALv2;
70 		printk("OPAL V2 detected !\n");
71 	} else {
72 		printk("OPAL V1 detected !\n");
73 	}
74 
75 	return 1;
76 }
77 
78 static int __init opal_register_exception_handlers(void)
79 {
80 	u64 glue;
81 
82 	if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
83 		return -ENODEV;
84 
85 	/* Hookup some exception handlers. We use the fwnmi area at 0x7000
86 	 * to provide the glue space to OPAL
87 	 */
88 	glue = 0x7000;
89 	opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
90 					__pa(opal_mc_secondary_handler[0]),
91 					glue);
92 	glue += 128;
93 	opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
94 					0, glue);
95 	glue += 128;
96 	opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
97 
98 	return 0;
99 }
100 
101 early_initcall(opal_register_exception_handlers);
102 
103 int opal_notifier_register(struct notifier_block *nb)
104 {
105 	if (!nb) {
106 		pr_warning("%s: Invalid argument (%p)\n",
107 			   __func__, nb);
108 		return -EINVAL;
109 	}
110 
111 	atomic_notifier_chain_register(&opal_notifier_head, nb);
112 	return 0;
113 }
114 
115 static void opal_do_notifier(uint64_t events)
116 {
117 	unsigned long flags;
118 	uint64_t changed_mask;
119 
120 	if (atomic_read(&opal_notifier_hold))
121 		return;
122 
123 	spin_lock_irqsave(&opal_notifier_lock, flags);
124 	changed_mask = last_notified_mask ^ events;
125 	last_notified_mask = events;
126 	spin_unlock_irqrestore(&opal_notifier_lock, flags);
127 
128 	/*
129 	 * We feed with the event bits and changed bits for
130 	 * enough information to the callback.
131 	 */
132 	atomic_notifier_call_chain(&opal_notifier_head,
133 				   events, (void *)changed_mask);
134 }
135 
136 void opal_notifier_update_evt(uint64_t evt_mask,
137 			      uint64_t evt_val)
138 {
139 	unsigned long flags;
140 
141 	spin_lock_irqsave(&opal_notifier_lock, flags);
142 	last_notified_mask &= ~evt_mask;
143 	last_notified_mask |= evt_val;
144 	spin_unlock_irqrestore(&opal_notifier_lock, flags);
145 }
146 
147 void opal_notifier_enable(void)
148 {
149 	int64_t rc;
150 	uint64_t evt = 0;
151 
152 	atomic_set(&opal_notifier_hold, 0);
153 
154 	/* Process pending events */
155 	rc = opal_poll_events(&evt);
156 	if (rc == OPAL_SUCCESS && evt)
157 		opal_do_notifier(evt);
158 }
159 
160 void opal_notifier_disable(void)
161 {
162 	atomic_set(&opal_notifier_hold, 1);
163 }
164 
165 int opal_get_chars(uint32_t vtermno, char *buf, int count)
166 {
167 	s64 len, rc;
168 	u64 evt;
169 
170 	if (!opal.entry)
171 		return -ENODEV;
172 	opal_poll_events(&evt);
173 	if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0)
174 		return 0;
175 	len = count;
176 	rc = opal_console_read(vtermno, &len, buf);
177 	if (rc == OPAL_SUCCESS)
178 		return len;
179 	return 0;
180 }
181 
182 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
183 {
184 	int written = 0;
185 	s64 len, rc;
186 	unsigned long flags;
187 	u64 evt;
188 
189 	if (!opal.entry)
190 		return -ENODEV;
191 
192 	/* We want put_chars to be atomic to avoid mangling of hvsi
193 	 * packets. To do that, we first test for room and return
194 	 * -EAGAIN if there isn't enough.
195 	 *
196 	 * Unfortunately, opal_console_write_buffer_space() doesn't
197 	 * appear to work on opal v1, so we just assume there is
198 	 * enough room and be done with it
199 	 */
200 	spin_lock_irqsave(&opal_write_lock, flags);
201 	if (firmware_has_feature(FW_FEATURE_OPALv2)) {
202 		rc = opal_console_write_buffer_space(vtermno, &len);
203 		if (rc || len < total_len) {
204 			spin_unlock_irqrestore(&opal_write_lock, flags);
205 			/* Closed -> drop characters */
206 			if (rc)
207 				return total_len;
208 			opal_poll_events(&evt);
209 			return -EAGAIN;
210 		}
211 	}
212 
213 	/* We still try to handle partial completions, though they
214 	 * should no longer happen.
215 	 */
216 	rc = OPAL_BUSY;
217 	while(total_len > 0 && (rc == OPAL_BUSY ||
218 				rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
219 		len = total_len;
220 		rc = opal_console_write(vtermno, &len, data);
221 
222 		/* Closed or other error drop */
223 		if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
224 		    rc != OPAL_BUSY_EVENT) {
225 			written = total_len;
226 			break;
227 		}
228 		if (rc == OPAL_SUCCESS) {
229 			total_len -= len;
230 			data += len;
231 			written += len;
232 		}
233 		/* This is a bit nasty but we need that for the console to
234 		 * flush when there aren't any interrupts. We will clean
235 		 * things a bit later to limit that to synchronous path
236 		 * such as the kernel console and xmon/udbg
237 		 */
238 		do
239 			opal_poll_events(&evt);
240 		while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
241 	}
242 	spin_unlock_irqrestore(&opal_write_lock, flags);
243 	return written;
244 }
245 
246 int opal_machine_check(struct pt_regs *regs)
247 {
248 	struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
249 	struct opal_machine_check_event evt;
250 	const char *level, *sevstr, *subtype;
251 	static const char *opal_mc_ue_types[] = {
252 		"Indeterminate",
253 		"Instruction fetch",
254 		"Page table walk ifetch",
255 		"Load/Store",
256 		"Page table walk Load/Store",
257 	};
258 	static const char *opal_mc_slb_types[] = {
259 		"Indeterminate",
260 		"Parity",
261 		"Multihit",
262 	};
263 	static const char *opal_mc_erat_types[] = {
264 		"Indeterminate",
265 		"Parity",
266 		"Multihit",
267 	};
268 	static const char *opal_mc_tlb_types[] = {
269 		"Indeterminate",
270 		"Parity",
271 		"Multihit",
272 	};
273 
274 	/* Copy the event structure and release the original */
275 	evt = *opal_evt;
276 	opal_evt->in_use = 0;
277 
278 	/* Print things out */
279 	if (evt.version != OpalMCE_V1) {
280 		pr_err("Machine Check Exception, Unknown event version %d !\n",
281 		       evt.version);
282 		return 0;
283 	}
284 	switch(evt.severity) {
285 	case OpalMCE_SEV_NO_ERROR:
286 		level = KERN_INFO;
287 		sevstr = "Harmless";
288 		break;
289 	case OpalMCE_SEV_WARNING:
290 		level = KERN_WARNING;
291 		sevstr = "";
292 		break;
293 	case OpalMCE_SEV_ERROR_SYNC:
294 		level = KERN_ERR;
295 		sevstr = "Severe";
296 		break;
297 	case OpalMCE_SEV_FATAL:
298 	default:
299 		level = KERN_ERR;
300 		sevstr = "Fatal";
301 		break;
302 	}
303 
304 	printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
305 	       evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
306 	       "Recovered" : "[Not recovered");
307 	printk("%s  Initiator: %s\n", level,
308 	       evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
309 	switch(evt.error_type) {
310 	case OpalMCE_ERROR_TYPE_UE:
311 		subtype = evt.u.ue_error.ue_error_type <
312 			ARRAY_SIZE(opal_mc_ue_types) ?
313 			opal_mc_ue_types[evt.u.ue_error.ue_error_type]
314 			: "Unknown";
315 		printk("%s  Error type: UE [%s]\n", level, subtype);
316 		if (evt.u.ue_error.effective_address_provided)
317 			printk("%s    Effective address: %016llx\n",
318 			       level, evt.u.ue_error.effective_address);
319 		if (evt.u.ue_error.physical_address_provided)
320 			printk("%s      Physial address: %016llx\n",
321 			       level, evt.u.ue_error.physical_address);
322 		break;
323 	case OpalMCE_ERROR_TYPE_SLB:
324 		subtype = evt.u.slb_error.slb_error_type <
325 			ARRAY_SIZE(opal_mc_slb_types) ?
326 			opal_mc_slb_types[evt.u.slb_error.slb_error_type]
327 			: "Unknown";
328 		printk("%s  Error type: SLB [%s]\n", level, subtype);
329 		if (evt.u.slb_error.effective_address_provided)
330 			printk("%s    Effective address: %016llx\n",
331 			       level, evt.u.slb_error.effective_address);
332 		break;
333 	case OpalMCE_ERROR_TYPE_ERAT:
334 		subtype = evt.u.erat_error.erat_error_type <
335 			ARRAY_SIZE(opal_mc_erat_types) ?
336 			opal_mc_erat_types[evt.u.erat_error.erat_error_type]
337 			: "Unknown";
338 		printk("%s  Error type: ERAT [%s]\n", level, subtype);
339 		if (evt.u.erat_error.effective_address_provided)
340 			printk("%s    Effective address: %016llx\n",
341 			       level, evt.u.erat_error.effective_address);
342 		break;
343 	case OpalMCE_ERROR_TYPE_TLB:
344 		subtype = evt.u.tlb_error.tlb_error_type <
345 			ARRAY_SIZE(opal_mc_tlb_types) ?
346 			opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
347 			: "Unknown";
348 		printk("%s  Error type: TLB [%s]\n", level, subtype);
349 		if (evt.u.tlb_error.effective_address_provided)
350 			printk("%s    Effective address: %016llx\n",
351 			       level, evt.u.tlb_error.effective_address);
352 		break;
353 	default:
354 	case OpalMCE_ERROR_TYPE_UNKNOWN:
355 		printk("%s  Error type: Unknown\n", level);
356 		break;
357 	}
358 	return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
359 }
360 
361 static irqreturn_t opal_interrupt(int irq, void *data)
362 {
363 	uint64_t events;
364 
365 	opal_handle_interrupt(virq_to_hw(irq), &events);
366 
367 	opal_do_notifier(events);
368 
369 	return IRQ_HANDLED;
370 }
371 
372 static int __init opal_init(void)
373 {
374 	struct device_node *np, *consoles;
375 	const u32 *irqs;
376 	int rc, i, irqlen;
377 
378 	opal_node = of_find_node_by_path("/ibm,opal");
379 	if (!opal_node) {
380 		pr_warn("opal: Node not found\n");
381 		return -ENODEV;
382 	}
383 
384 	/* Register OPAL consoles if any ports */
385 	if (firmware_has_feature(FW_FEATURE_OPALv2))
386 		consoles = of_find_node_by_path("/ibm,opal/consoles");
387 	else
388 		consoles = of_node_get(opal_node);
389 	if (consoles) {
390 		for_each_child_of_node(consoles, np) {
391 			if (strcmp(np->name, "serial"))
392 				continue;
393 			of_platform_device_create(np, NULL, NULL);
394 		}
395 		of_node_put(consoles);
396 	}
397 
398 	/* Find all OPAL interrupts and request them */
399 	irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
400 	pr_debug("opal: Found %d interrupts reserved for OPAL\n",
401 		 irqs ? (irqlen / 4) : 0);
402 	opal_irq_count = irqlen / 4;
403 	opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
404 	for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
405 		unsigned int hwirq = be32_to_cpup(irqs);
406 		unsigned int irq = irq_create_mapping(NULL, hwirq);
407 		if (irq == NO_IRQ) {
408 			pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
409 			continue;
410 		}
411 		rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
412 		if (rc)
413 			pr_warning("opal: Error %d requesting irq %d"
414 				   " (0x%x)\n", rc, irq, hwirq);
415 		opal_irqs[i] = irq;
416 	}
417 	return 0;
418 }
419 subsys_initcall(opal_init);
420 
421 void opal_shutdown(void)
422 {
423 	unsigned int i;
424 
425 	for (i = 0; i < opal_irq_count; i++) {
426 		if (opal_irqs[i])
427 			free_irq(opal_irqs[i], NULL);
428 		opal_irqs[i] = 0;
429 	}
430 }
431