2 * PowerNV OPAL high level interfaces
4 * Copyright 2011 IBM Corp.
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.
14 #include <linux/types.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 <linux/delay.h>
25 #include <asm/firmware.h>
30 /* /sys/firmware/opal */
31 struct kobject *opal_kobj;
38 static struct device_node *opal_node;
39 static DEFINE_SPINLOCK(opal_write_lock);
40 extern u64 opal_mc_secondary_handler[];
41 static unsigned int *opal_irqs;
42 static unsigned int opal_irq_count;
43 static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
44 static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
45 static DEFINE_SPINLOCK(opal_notifier_lock);
46 static uint64_t last_notified_mask = 0x0ul;
47 static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
49 int __init early_init_dt_scan_opal(unsigned long node,
50 const char *uname, int depth, void *data)
52 const void *basep, *entryp;
53 unsigned long basesz, entrysz;
55 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
58 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
59 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
61 if (!basep || !entryp)
64 opal.base = of_read_number(basep, basesz/4);
65 opal.entry = of_read_number(entryp, entrysz/4);
67 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
68 opal.base, basep, basesz);
69 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
70 opal.entry, entryp, entrysz);
72 powerpc_firmware_features |= FW_FEATURE_OPAL;
73 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
74 powerpc_firmware_features |= FW_FEATURE_OPALv2;
75 powerpc_firmware_features |= FW_FEATURE_OPALv3;
76 printk("OPAL V3 detected !\n");
77 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
78 powerpc_firmware_features |= FW_FEATURE_OPALv2;
79 printk("OPAL V2 detected !\n");
81 printk("OPAL V1 detected !\n");
87 static int __init opal_register_exception_handlers(void)
92 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
95 /* Hookup some exception handlers except machine check. We use the
96 * fwnmi area at 0x7000 to provide the glue space to OPAL
99 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
102 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
108 early_initcall(opal_register_exception_handlers);
110 int opal_notifier_register(struct notifier_block *nb)
113 pr_warning("%s: Invalid argument (%p)\n",
118 atomic_notifier_chain_register(&opal_notifier_head, nb);
122 static void opal_do_notifier(uint64_t events)
125 uint64_t changed_mask;
127 if (atomic_read(&opal_notifier_hold))
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);
136 * We feed with the event bits and changed bits for
137 * enough information to the callback.
139 atomic_notifier_call_chain(&opal_notifier_head,
140 events, (void *)changed_mask);
143 void opal_notifier_update_evt(uint64_t evt_mask,
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);
154 void opal_notifier_enable(void)
159 atomic_set(&opal_notifier_hold, 0);
161 /* Process pending events */
162 rc = opal_poll_events(&evt);
163 if (rc == OPAL_SUCCESS && evt)
164 opal_do_notifier(evt);
167 void opal_notifier_disable(void)
169 atomic_set(&opal_notifier_hold, 1);
173 * Opal message notifier based on message type. Allow subscribers to get
174 * notified for specific messgae type.
176 int opal_message_notifier_register(enum OpalMessageType msg_type,
177 struct notifier_block *nb)
180 pr_warning("%s: Invalid argument (%p)\n",
184 if (msg_type > OPAL_MSG_TYPE_MAX) {
185 pr_warning("%s: Invalid message type argument (%d)\n",
189 return atomic_notifier_chain_register(
190 &opal_msg_notifier_head[msg_type], nb);
193 static void opal_message_do_notify(uint32_t msg_type, void *msg)
195 /* notify subscribers */
196 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
200 static void opal_handle_message(void)
204 * TODO: pre-allocate a message buffer depending on opal-msg-size
205 * value in /proc/device-tree.
207 static struct opal_msg msg;
209 ret = opal_get_msg(__pa(&msg), sizeof(msg));
210 /* No opal message pending. */
211 if (ret == OPAL_RESOURCE)
214 /* check for errors. */
216 pr_warning("%s: Failed to retrive opal message, err=%lld\n",
222 if (msg.msg_type > OPAL_MSG_TYPE_MAX) {
223 pr_warning("%s: Unknown message type: %u\n",
224 __func__, msg.msg_type);
227 opal_message_do_notify(msg.msg_type, (void *)&msg);
230 static int opal_message_notify(struct notifier_block *nb,
231 unsigned long events, void *change)
233 if (events & OPAL_EVENT_MSG_PENDING)
234 opal_handle_message();
238 static struct notifier_block opal_message_nb = {
239 .notifier_call = opal_message_notify,
244 static int __init opal_message_init(void)
248 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
249 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
251 ret = opal_notifier_register(&opal_message_nb);
253 pr_err("%s: Can't register OPAL event notifier (%d)\n",
259 early_initcall(opal_message_init);
261 int opal_get_chars(uint32_t vtermno, char *buf, int count)
268 opal_poll_events(&evt);
269 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
271 len = cpu_to_be64(count);
272 rc = opal_console_read(vtermno, &len, buf);
273 if (rc == OPAL_SUCCESS)
274 return be64_to_cpu(len);
278 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
289 /* We want put_chars to be atomic to avoid mangling of hvsi
290 * packets. To do that, we first test for room and return
291 * -EAGAIN if there isn't enough.
293 * Unfortunately, opal_console_write_buffer_space() doesn't
294 * appear to work on opal v1, so we just assume there is
295 * enough room and be done with it
297 spin_lock_irqsave(&opal_write_lock, flags);
298 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
299 rc = opal_console_write_buffer_space(vtermno, &olen);
300 len = be64_to_cpu(olen);
301 if (rc || len < total_len) {
302 spin_unlock_irqrestore(&opal_write_lock, flags);
303 /* Closed -> drop characters */
306 opal_poll_events(NULL);
311 /* We still try to handle partial completions, though they
312 * should no longer happen.
315 while(total_len > 0 && (rc == OPAL_BUSY ||
316 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
317 olen = cpu_to_be64(total_len);
318 rc = opal_console_write(vtermno, &olen, data);
319 len = be64_to_cpu(olen);
321 /* Closed or other error drop */
322 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
323 rc != OPAL_BUSY_EVENT) {
327 if (rc == OPAL_SUCCESS) {
332 /* This is a bit nasty but we need that for the console to
333 * flush when there aren't any interrupts. We will clean
334 * things a bit later to limit that to synchronous path
335 * such as the kernel console and xmon/udbg
338 opal_poll_events(&evt);
339 while(rc == OPAL_SUCCESS &&
340 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
342 spin_unlock_irqrestore(&opal_write_lock, flags);
346 static int opal_recover_mce(struct pt_regs *regs,
347 struct machine_check_event *evt)
350 uint64_t ea = get_mce_fault_addr(evt);
352 if (!(regs->msr & MSR_RI)) {
353 /* If MSR_RI isn't set, we cannot recover */
355 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
356 /* Platform corrected itself */
358 } else if (ea && !is_kernel_addr(ea)) {
360 * Faulting address is not in kernel text. We should be fine.
361 * We need to find which process uses this address.
362 * For now, kill the task if we have received exception when
365 * TODO: Queue up this address for hwpoisioning later.
367 if (user_mode(regs) && !is_global_init(current)) {
368 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
372 } else if (user_mode(regs) && !is_global_init(current) &&
373 evt->severity == MCE_SEV_ERROR_SYNC) {
375 * If we have received a synchronous error when in userspace
378 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
384 int opal_machine_check(struct pt_regs *regs)
386 struct machine_check_event evt;
388 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
391 /* Print things out */
392 if (evt.version != MCE_V1) {
393 pr_err("Machine Check Exception, Unknown event version %d !\n",
397 machine_check_print_event_info(&evt);
399 if (opal_recover_mce(regs, &evt))
404 static irqreturn_t opal_interrupt(int irq, void *data)
408 opal_handle_interrupt(virq_to_hw(irq), &events);
410 opal_do_notifier(events);
415 static int opal_sysfs_init(void)
417 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
419 pr_warn("kobject_create_and_add opal failed\n");
426 static int __init opal_init(void)
428 struct device_node *np, *consoles;
432 opal_node = of_find_node_by_path("/ibm,opal");
434 pr_warn("opal: Node not found\n");
438 /* Register OPAL consoles if any ports */
439 if (firmware_has_feature(FW_FEATURE_OPALv2))
440 consoles = of_find_node_by_path("/ibm,opal/consoles");
442 consoles = of_node_get(opal_node);
444 for_each_child_of_node(consoles, np) {
445 if (strcmp(np->name, "serial"))
447 of_platform_device_create(np, NULL, NULL);
449 of_node_put(consoles);
452 /* Find all OPAL interrupts and request them */
453 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
454 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
455 irqs ? (irqlen / 4) : 0);
456 opal_irq_count = irqlen / 4;
457 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
458 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
459 unsigned int hwirq = be32_to_cpup(irqs);
460 unsigned int irq = irq_create_mapping(NULL, hwirq);
462 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
465 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
467 pr_warning("opal: Error %d requesting irq %d"
468 " (0x%x)\n", rc, irq, hwirq);
472 /* Create "opal" kobject under /sys/firmware */
473 rc = opal_sysfs_init();
475 /* Setup code update interface */
481 subsys_initcall(opal_init);
483 void opal_shutdown(void)
488 /* First free interrupts, which will also mask them */
489 for (i = 0; i < opal_irq_count; i++) {
491 free_irq(opal_irqs[i], NULL);
496 * Then sync with OPAL which ensure anything that can
497 * potentially write to our memory has completed such
498 * as an ongoing dump retrieval
500 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
501 rc = opal_sync_host_reboot();
503 opal_poll_events(NULL);