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