Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 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> |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame^] | 17 | #include <linux/interrupt.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 18 | #include <asm/opal.h> |
| 19 | #include <asm/firmware.h> |
| 20 | |
| 21 | #include "powernv.h" |
| 22 | |
| 23 | struct opal { |
| 24 | u64 base; |
| 25 | u64 entry; |
| 26 | } opal; |
| 27 | |
| 28 | static struct device_node *opal_node; |
| 29 | static DEFINE_SPINLOCK(opal_write_lock); |
| 30 | |
| 31 | int __init early_init_dt_scan_opal(unsigned long node, |
| 32 | const char *uname, int depth, void *data) |
| 33 | { |
| 34 | const void *basep, *entryp; |
| 35 | unsigned long basesz, entrysz; |
| 36 | |
| 37 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) |
| 38 | return 0; |
| 39 | |
| 40 | basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz); |
| 41 | entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz); |
| 42 | |
| 43 | if (!basep || !entryp) |
| 44 | return 1; |
| 45 | |
| 46 | opal.base = of_read_number(basep, basesz/4); |
| 47 | opal.entry = of_read_number(entryp, entrysz/4); |
| 48 | |
| 49 | pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n", |
| 50 | opal.base, basep, basesz); |
| 51 | pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n", |
| 52 | opal.entry, entryp, entrysz); |
| 53 | |
| 54 | powerpc_firmware_features |= FW_FEATURE_OPAL; |
| 55 | if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) { |
| 56 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
| 57 | printk("OPAL V2 detected !\n"); |
| 58 | } else { |
| 59 | printk("OPAL V1 detected !\n"); |
| 60 | } |
| 61 | |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | int opal_get_chars(uint32_t vtermno, char *buf, int count) |
| 66 | { |
| 67 | s64 len, rc; |
| 68 | u64 evt; |
| 69 | |
| 70 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 71 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 72 | opal_poll_events(&evt); |
| 73 | if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0) |
| 74 | return 0; |
| 75 | len = count; |
| 76 | rc = opal_console_read(vtermno, &len, buf); |
| 77 | if (rc == OPAL_SUCCESS) |
| 78 | return len; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int opal_put_chars(uint32_t vtermno, const char *data, int total_len) |
| 83 | { |
| 84 | int written = 0; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 85 | s64 len, rc; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 86 | unsigned long flags; |
| 87 | u64 evt; |
| 88 | |
| 89 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 90 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 91 | |
| 92 | /* We want put_chars to be atomic to avoid mangling of hvsi |
| 93 | * packets. To do that, we first test for room and return |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 94 | * -EAGAIN if there isn't enough. |
| 95 | * |
| 96 | * Unfortunately, opal_console_write_buffer_space() doesn't |
| 97 | * appear to work on opal v1, so we just assume there is |
| 98 | * enough room and be done with it |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 99 | */ |
| 100 | spin_lock_irqsave(&opal_write_lock, flags); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 101 | if (firmware_has_feature(FW_FEATURE_OPALv2)) { |
| 102 | rc = opal_console_write_buffer_space(vtermno, &len); |
| 103 | if (rc || len < total_len) { |
| 104 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 105 | /* Closed -> drop characters */ |
| 106 | if (rc) |
| 107 | return total_len; |
| 108 | opal_poll_events(&evt); |
| 109 | return -EAGAIN; |
| 110 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* We still try to handle partial completions, though they |
| 114 | * should no longer happen. |
| 115 | */ |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 116 | rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 117 | while(total_len > 0 && (rc == OPAL_BUSY || |
| 118 | rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) { |
| 119 | len = total_len; |
| 120 | rc = opal_console_write(vtermno, &len, data); |
| 121 | if (rc == OPAL_SUCCESS) { |
| 122 | total_len -= len; |
| 123 | data += len; |
| 124 | written += len; |
| 125 | } |
| 126 | /* This is a bit nasty but we need that for the console to |
| 127 | * flush when there aren't any interrupts. We will clean |
| 128 | * things a bit later to limit that to synchronous path |
| 129 | * such as the kernel console and xmon/udbg |
| 130 | */ |
| 131 | do |
| 132 | opal_poll_events(&evt); |
| 133 | while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT)); |
| 134 | } |
| 135 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 136 | return written; |
| 137 | } |
| 138 | |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame^] | 139 | static irqreturn_t opal_interrupt(int irq, void *data) |
| 140 | { |
| 141 | uint64_t events; |
| 142 | |
| 143 | opal_handle_interrupt(virq_to_hw(irq), &events); |
| 144 | |
| 145 | /* XXX TODO: Do something with the events */ |
| 146 | |
| 147 | return IRQ_HANDLED; |
| 148 | } |
| 149 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 150 | static int __init opal_init(void) |
| 151 | { |
| 152 | struct device_node *np, *consoles; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame^] | 153 | const u32 *irqs; |
| 154 | int rc, i, irqlen; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 155 | |
| 156 | opal_node = of_find_node_by_path("/ibm,opal"); |
| 157 | if (!opal_node) { |
| 158 | pr_warn("opal: Node not found\n"); |
| 159 | return -ENODEV; |
| 160 | } |
| 161 | if (firmware_has_feature(FW_FEATURE_OPALv2)) |
| 162 | consoles = of_find_node_by_path("/ibm,opal/consoles"); |
| 163 | else |
| 164 | consoles = of_node_get(opal_node); |
| 165 | |
| 166 | /* Register serial ports */ |
| 167 | for_each_child_of_node(consoles, np) { |
| 168 | if (strcmp(np->name, "serial")) |
| 169 | continue; |
| 170 | of_platform_device_create(np, NULL, NULL); |
| 171 | } |
| 172 | of_node_put(consoles); |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame^] | 173 | |
| 174 | /* Find all OPAL interrupts and request them */ |
| 175 | irqs = of_get_property(opal_node, "opal-interrupts", &irqlen); |
| 176 | pr_debug("opal: Found %d interrupts reserved for OPAL\n", |
| 177 | irqs ? (irqlen / 4) : 0); |
| 178 | for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) { |
| 179 | unsigned int hwirq = be32_to_cpup(irqs); |
| 180 | unsigned int irq = irq_create_mapping(NULL, hwirq); |
| 181 | if (irq == NO_IRQ) { |
| 182 | pr_warning("opal: Failed to map irq 0x%x\n", hwirq); |
| 183 | continue; |
| 184 | } |
| 185 | rc = request_irq(irq, opal_interrupt, 0, "opal", NULL); |
| 186 | if (rc) |
| 187 | pr_warning("opal: Error %d requesting irq %d" |
| 188 | " (0x%x)\n", rc, irq, hwirq); |
| 189 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | subsys_initcall(opal_init); |