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/sched.h>
22 #include <linux/kobject.h>
23 #include <asm/opal.h>
24 #include <asm/firmware.h>
25 #include <asm/mce.h>
26 
27 #include "powernv.h"
28 
29 /* /sys/firmware/opal */
30 struct kobject *opal_kobj;
31 
32 struct opal {
33 	u64 base;
34 	u64 entry;
35 } opal;
36 
37 static struct device_node *opal_node;
38 static DEFINE_SPINLOCK(opal_write_lock);
39 extern u64 opal_mc_secondary_handler[];
40 static unsigned int *opal_irqs;
41 static unsigned int opal_irq_count;
42 static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
43 static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
44 static DEFINE_SPINLOCK(opal_notifier_lock);
45 static uint64_t last_notified_mask = 0x0ul;
46 static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
47 
48 int __init early_init_dt_scan_opal(unsigned long node,
49 				   const char *uname, int depth, void *data)
50 {
51 	const void *basep, *entryp;
52 	unsigned long basesz, entrysz;
53 
54 	if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
55 		return 0;
56 
57 	basep  = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
58 	entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
59 
60 	if (!basep || !entryp)
61 		return 1;
62 
63 	opal.base = of_read_number(basep, basesz/4);
64 	opal.entry = of_read_number(entryp, entrysz/4);
65 
66 	pr_debug("OPAL Base  = 0x%llx (basep=%p basesz=%ld)\n",
67 		 opal.base, basep, basesz);
68 	pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
69 		 opal.entry, entryp, entrysz);
70 
71 	powerpc_firmware_features |= FW_FEATURE_OPAL;
72 	if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
73 		powerpc_firmware_features |= FW_FEATURE_OPALv2;
74 		powerpc_firmware_features |= FW_FEATURE_OPALv3;
75 		printk("OPAL V3 detected !\n");
76 	} else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
77 		powerpc_firmware_features |= FW_FEATURE_OPALv2;
78 		printk("OPAL V2 detected !\n");
79 	} else {
80 		printk("OPAL V1 detected !\n");
81 	}
82 
83 	return 1;
84 }
85 
86 static int __init opal_register_exception_handlers(void)
87 {
88 #ifdef __BIG_ENDIAN__
89 	u64 glue;
90 
91 	if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
92 		return -ENODEV;
93 
94 	/* Hookup some exception handlers except machine check. We use the
95 	 * fwnmi area at 0x7000 to provide the glue space to OPAL
96 	 */
97 	glue = 0x7000;
98 	opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
99 					0, glue);
100 	glue += 128;
101 	opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
102 #endif
103 
104 	return 0;
105 }
106 
107 early_initcall(opal_register_exception_handlers);
108 
109 int opal_notifier_register(struct notifier_block *nb)
110 {
111 	if (!nb) {
112 		pr_warning("%s: Invalid argument (%p)\n",
113 			   __func__, nb);
114 		return -EINVAL;
115 	}
116 
117 	atomic_notifier_chain_register(&opal_notifier_head, nb);
118 	return 0;
119 }
120 
121 static void opal_do_notifier(uint64_t events)
122 {
123 	unsigned long flags;
124 	uint64_t changed_mask;
125 
126 	if (atomic_read(&opal_notifier_hold))
127 		return;
128 
129 	spin_lock_irqsave(&opal_notifier_lock, flags);
130 	changed_mask = last_notified_mask ^ events;
131 	last_notified_mask = events;
132 	spin_unlock_irqrestore(&opal_notifier_lock, flags);
133 
134 	/*
135 	 * We feed with the event bits and changed bits for
136 	 * enough information to the callback.
137 	 */
138 	atomic_notifier_call_chain(&opal_notifier_head,
139 				   events, (void *)changed_mask);
140 }
141 
142 void opal_notifier_update_evt(uint64_t evt_mask,
143 			      uint64_t evt_val)
144 {
145 	unsigned long flags;
146 
147 	spin_lock_irqsave(&opal_notifier_lock, flags);
148 	last_notified_mask &= ~evt_mask;
149 	last_notified_mask |= evt_val;
150 	spin_unlock_irqrestore(&opal_notifier_lock, flags);
151 }
152 
153 void opal_notifier_enable(void)
154 {
155 	int64_t rc;
156 	uint64_t evt = 0;
157 
158 	atomic_set(&opal_notifier_hold, 0);
159 
160 	/* Process pending events */
161 	rc = opal_poll_events(&evt);
162 	if (rc == OPAL_SUCCESS && evt)
163 		opal_do_notifier(evt);
164 }
165 
166 void opal_notifier_disable(void)
167 {
168 	atomic_set(&opal_notifier_hold, 1);
169 }
170 
171 /*
172  * Opal message notifier based on message type. Allow subscribers to get
173  * notified for specific messgae type.
174  */
175 int opal_message_notifier_register(enum OpalMessageType msg_type,
176 					struct notifier_block *nb)
177 {
178 	if (!nb) {
179 		pr_warning("%s: Invalid argument (%p)\n",
180 			   __func__, nb);
181 		return -EINVAL;
182 	}
183 	if (msg_type > OPAL_MSG_TYPE_MAX) {
184 		pr_warning("%s: Invalid message type argument (%d)\n",
185 			   __func__, msg_type);
186 		return -EINVAL;
187 	}
188 	return atomic_notifier_chain_register(
189 				&opal_msg_notifier_head[msg_type], nb);
190 }
191 
192 static void opal_message_do_notify(uint32_t msg_type, void *msg)
193 {
194 	/* notify subscribers */
195 	atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
196 					msg_type, msg);
197 }
198 
199 static void opal_handle_message(void)
200 {
201 	s64 ret;
202 	/*
203 	 * TODO: pre-allocate a message buffer depending on opal-msg-size
204 	 * value in /proc/device-tree.
205 	 */
206 	static struct opal_msg msg;
207 
208 	ret = opal_get_msg(__pa(&msg), sizeof(msg));
209 	/* No opal message pending. */
210 	if (ret == OPAL_RESOURCE)
211 		return;
212 
213 	/* check for errors. */
214 	if (ret) {
215 		pr_warning("%s: Failed to retrive opal message, err=%lld\n",
216 				__func__, ret);
217 		return;
218 	}
219 
220 	/* Sanity check */
221 	if (msg.msg_type > OPAL_MSG_TYPE_MAX) {
222 		pr_warning("%s: Unknown message type: %u\n",
223 				__func__, msg.msg_type);
224 		return;
225 	}
226 	opal_message_do_notify(msg.msg_type, (void *)&msg);
227 }
228 
229 static int opal_message_notify(struct notifier_block *nb,
230 			  unsigned long events, void *change)
231 {
232 	if (events & OPAL_EVENT_MSG_PENDING)
233 		opal_handle_message();
234 	return 0;
235 }
236 
237 static struct notifier_block opal_message_nb = {
238 	.notifier_call	= opal_message_notify,
239 	.next		= NULL,
240 	.priority	= 0,
241 };
242 
243 static int __init opal_message_init(void)
244 {
245 	int ret, i;
246 
247 	for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
248 		ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
249 
250 	ret = opal_notifier_register(&opal_message_nb);
251 	if (ret) {
252 		pr_err("%s: Can't register OPAL event notifier (%d)\n",
253 		       __func__, ret);
254 		return ret;
255 	}
256 	return 0;
257 }
258 early_initcall(opal_message_init);
259 
260 int opal_get_chars(uint32_t vtermno, char *buf, int count)
261 {
262 	s64 rc;
263 	__be64 evt, len;
264 
265 	if (!opal.entry)
266 		return -ENODEV;
267 	opal_poll_events(&evt);
268 	if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
269 		return 0;
270 	len = cpu_to_be64(count);
271 	rc = opal_console_read(vtermno, &len, buf);
272 	if (rc == OPAL_SUCCESS)
273 		return be64_to_cpu(len);
274 	return 0;
275 }
276 
277 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
278 {
279 	int written = 0;
280 	__be64 olen;
281 	s64 len, rc;
282 	unsigned long flags;
283 	__be64 evt;
284 
285 	if (!opal.entry)
286 		return -ENODEV;
287 
288 	/* We want put_chars to be atomic to avoid mangling of hvsi
289 	 * packets. To do that, we first test for room and return
290 	 * -EAGAIN if there isn't enough.
291 	 *
292 	 * Unfortunately, opal_console_write_buffer_space() doesn't
293 	 * appear to work on opal v1, so we just assume there is
294 	 * enough room and be done with it
295 	 */
296 	spin_lock_irqsave(&opal_write_lock, flags);
297 	if (firmware_has_feature(FW_FEATURE_OPALv2)) {
298 		rc = opal_console_write_buffer_space(vtermno, &olen);
299 		len = be64_to_cpu(olen);
300 		if (rc || len < total_len) {
301 			spin_unlock_irqrestore(&opal_write_lock, flags);
302 			/* Closed -> drop characters */
303 			if (rc)
304 				return total_len;
305 			opal_poll_events(NULL);
306 			return -EAGAIN;
307 		}
308 	}
309 
310 	/* We still try to handle partial completions, though they
311 	 * should no longer happen.
312 	 */
313 	rc = OPAL_BUSY;
314 	while(total_len > 0 && (rc == OPAL_BUSY ||
315 				rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
316 		olen = cpu_to_be64(total_len);
317 		rc = opal_console_write(vtermno, &olen, data);
318 		len = be64_to_cpu(olen);
319 
320 		/* Closed or other error drop */
321 		if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
322 		    rc != OPAL_BUSY_EVENT) {
323 			written = total_len;
324 			break;
325 		}
326 		if (rc == OPAL_SUCCESS) {
327 			total_len -= len;
328 			data += len;
329 			written += len;
330 		}
331 		/* This is a bit nasty but we need that for the console to
332 		 * flush when there aren't any interrupts. We will clean
333 		 * things a bit later to limit that to synchronous path
334 		 * such as the kernel console and xmon/udbg
335 		 */
336 		do
337 			opal_poll_events(&evt);
338 		while(rc == OPAL_SUCCESS &&
339 			(be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
340 	}
341 	spin_unlock_irqrestore(&opal_write_lock, flags);
342 	return written;
343 }
344 
345 static int opal_recover_mce(struct pt_regs *regs,
346 					struct machine_check_event *evt)
347 {
348 	int recovered = 0;
349 	uint64_t ea = get_mce_fault_addr(evt);
350 
351 	if (!(regs->msr & MSR_RI)) {
352 		/* If MSR_RI isn't set, we cannot recover */
353 		recovered = 0;
354 	} else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
355 		/* Platform corrected itself */
356 		recovered = 1;
357 	} else if (ea && !is_kernel_addr(ea)) {
358 		/*
359 		 * Faulting address is not in kernel text. We should be fine.
360 		 * We need to find which process uses this address.
361 		 * For now, kill the task if we have received exception when
362 		 * in userspace.
363 		 *
364 		 * TODO: Queue up this address for hwpoisioning later.
365 		 */
366 		if (user_mode(regs) && !is_global_init(current)) {
367 			_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
368 			recovered = 1;
369 		} else
370 			recovered = 0;
371 	} else if (user_mode(regs) && !is_global_init(current) &&
372 		evt->severity == MCE_SEV_ERROR_SYNC) {
373 		/*
374 		 * If we have received a synchronous error when in userspace
375 		 * kill the task.
376 		 */
377 		_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
378 		recovered = 1;
379 	}
380 	return recovered;
381 }
382 
383 int opal_machine_check(struct pt_regs *regs)
384 {
385 	struct machine_check_event evt;
386 
387 	if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
388 		return 0;
389 
390 	/* Print things out */
391 	if (evt.version != MCE_V1) {
392 		pr_err("Machine Check Exception, Unknown event version %d !\n",
393 		       evt.version);
394 		return 0;
395 	}
396 	machine_check_print_event_info(&evt);
397 
398 	if (opal_recover_mce(regs, &evt))
399 		return 1;
400 	return 0;
401 }
402 
403 static irqreturn_t opal_interrupt(int irq, void *data)
404 {
405 	__be64 events;
406 
407 	opal_handle_interrupt(virq_to_hw(irq), &events);
408 
409 	opal_do_notifier(events);
410 
411 	return IRQ_HANDLED;
412 }
413 
414 static int opal_sysfs_init(void)
415 {
416 	opal_kobj = kobject_create_and_add("opal", firmware_kobj);
417 	if (!opal_kobj) {
418 		pr_warn("kobject_create_and_add opal failed\n");
419 		return -ENOMEM;
420 	}
421 
422 	return 0;
423 }
424 
425 static int __init opal_init(void)
426 {
427 	struct device_node *np, *consoles;
428 	const __be32 *irqs;
429 	int rc, i, irqlen;
430 
431 	opal_node = of_find_node_by_path("/ibm,opal");
432 	if (!opal_node) {
433 		pr_warn("opal: Node not found\n");
434 		return -ENODEV;
435 	}
436 
437 	/* Register OPAL consoles if any ports */
438 	if (firmware_has_feature(FW_FEATURE_OPALv2))
439 		consoles = of_find_node_by_path("/ibm,opal/consoles");
440 	else
441 		consoles = of_node_get(opal_node);
442 	if (consoles) {
443 		for_each_child_of_node(consoles, np) {
444 			if (strcmp(np->name, "serial"))
445 				continue;
446 			of_platform_device_create(np, NULL, NULL);
447 		}
448 		of_node_put(consoles);
449 	}
450 
451 	/* Find all OPAL interrupts and request them */
452 	irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
453 	pr_debug("opal: Found %d interrupts reserved for OPAL\n",
454 		 irqs ? (irqlen / 4) : 0);
455 	opal_irq_count = irqlen / 4;
456 	opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
457 	for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
458 		unsigned int hwirq = be32_to_cpup(irqs);
459 		unsigned int irq = irq_create_mapping(NULL, hwirq);
460 		if (irq == NO_IRQ) {
461 			pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
462 			continue;
463 		}
464 		rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
465 		if (rc)
466 			pr_warning("opal: Error %d requesting irq %d"
467 				   " (0x%x)\n", rc, irq, hwirq);
468 		opal_irqs[i] = irq;
469 	}
470 
471 	/* Create "opal" kobject under /sys/firmware */
472 	rc = opal_sysfs_init();
473 	if (rc == 0) {
474 		/* Setup code update interface */
475 		opal_flash_init();
476 	}
477 
478 	return 0;
479 }
480 subsys_initcall(opal_init);
481 
482 void opal_shutdown(void)
483 {
484 	unsigned int i;
485 
486 	for (i = 0; i < opal_irq_count; i++) {
487 		if (opal_irqs[i])
488 			free_irq(opal_irqs[i], NULL);
489 		opal_irqs[i] = 0;
490 	}
491 }
492