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