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> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 16 | #include <linux/of_fdt.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 17 | #include <linux/of_platform.h> |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 19 | #include <linux/notifier.h> |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 20 | #include <linux/slab.h> |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 21 | #include <linux/sched.h> |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 22 | #include <linux/kobject.h> |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 23 | #include <linux/delay.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 24 | #include <asm/opal.h> |
| 25 | #include <asm/firmware.h> |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 26 | #include <asm/mce.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 27 | |
| 28 | #include "powernv.h" |
| 29 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 30 | /* /sys/firmware/opal */ |
| 31 | struct kobject *opal_kobj; |
| 32 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 33 | struct opal { |
| 34 | u64 base; |
| 35 | u64 entry; |
| 36 | } opal; |
| 37 | |
| 38 | static struct device_node *opal_node; |
| 39 | static DEFINE_SPINLOCK(opal_write_lock); |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 40 | extern u64 opal_mc_secondary_handler[]; |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 41 | static unsigned int *opal_irqs; |
| 42 | static unsigned int opal_irq_count; |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 43 | static ATOMIC_NOTIFIER_HEAD(opal_notifier_head); |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 44 | static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX]; |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 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); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 48 | |
| 49 | int __init early_init_dt_scan_opal(unsigned long node, |
| 50 | const char *uname, int depth, void *data) |
| 51 | { |
| 52 | const void *basep, *entryp; |
| 53 | unsigned long basesz, entrysz; |
| 54 | |
| 55 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) |
| 56 | return 0; |
| 57 | |
| 58 | basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz); |
| 59 | entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz); |
| 60 | |
| 61 | if (!basep || !entryp) |
| 62 | return 1; |
| 63 | |
| 64 | opal.base = of_read_number(basep, basesz/4); |
| 65 | opal.entry = of_read_number(entryp, entrysz/4); |
| 66 | |
| 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); |
| 71 | |
| 72 | powerpc_firmware_features |= FW_FEATURE_OPAL; |
Benjamin Herrenschmidt | 75b93da | 2013-05-14 15:10:02 +1000 | [diff] [blame] | 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")) { |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 78 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
| 79 | printk("OPAL V2 detected !\n"); |
| 80 | } else { |
| 81 | printk("OPAL V1 detected !\n"); |
| 82 | } |
| 83 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | static int __init opal_register_exception_handlers(void) |
| 88 | { |
Benjamin Herrenschmidt | 2918609 | 2013-09-23 12:05:04 +1000 | [diff] [blame] | 89 | #ifdef __BIG_ENDIAN__ |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 90 | u64 glue; |
| 91 | |
| 92 | if (!(powerpc_firmware_features & FW_FEATURE_OPAL)) |
| 93 | return -ENODEV; |
| 94 | |
Mahesh Salgaonkar | 28446de | 2013-10-30 20:05:58 +0530 | [diff] [blame] | 95 | /* Hookup some exception handlers except machine check. We use the |
| 96 | * fwnmi area at 0x7000 to provide the glue space to OPAL |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 97 | */ |
| 98 | glue = 0x7000; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 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); |
Benjamin Herrenschmidt | 2918609 | 2013-09-23 12:05:04 +1000 | [diff] [blame] | 103 | #endif |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 104 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 105 | return 0; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 108 | early_initcall(opal_register_exception_handlers); |
| 109 | |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 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 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 172 | /* |
| 173 | * Opal message notifier based on message type. Allow subscribers to get |
| 174 | * notified for specific messgae type. |
| 175 | */ |
| 176 | int opal_message_notifier_register(enum OpalMessageType msg_type, |
| 177 | struct notifier_block *nb) |
| 178 | { |
| 179 | if (!nb) { |
| 180 | pr_warning("%s: Invalid argument (%p)\n", |
| 181 | __func__, nb); |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | if (msg_type > OPAL_MSG_TYPE_MAX) { |
| 185 | pr_warning("%s: Invalid message type argument (%d)\n", |
| 186 | __func__, msg_type); |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | return atomic_notifier_chain_register( |
| 190 | &opal_msg_notifier_head[msg_type], nb); |
| 191 | } |
| 192 | |
| 193 | static void opal_message_do_notify(uint32_t msg_type, void *msg) |
| 194 | { |
| 195 | /* notify subscribers */ |
| 196 | atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type], |
| 197 | msg_type, msg); |
| 198 | } |
| 199 | |
| 200 | static void opal_handle_message(void) |
| 201 | { |
| 202 | s64 ret; |
| 203 | /* |
| 204 | * TODO: pre-allocate a message buffer depending on opal-msg-size |
| 205 | * value in /proc/device-tree. |
| 206 | */ |
| 207 | static struct opal_msg msg; |
| 208 | |
| 209 | ret = opal_get_msg(__pa(&msg), sizeof(msg)); |
| 210 | /* No opal message pending. */ |
| 211 | if (ret == OPAL_RESOURCE) |
| 212 | return; |
| 213 | |
| 214 | /* check for errors. */ |
| 215 | if (ret) { |
| 216 | pr_warning("%s: Failed to retrive opal message, err=%lld\n", |
| 217 | __func__, ret); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | /* Sanity check */ |
| 222 | if (msg.msg_type > OPAL_MSG_TYPE_MAX) { |
| 223 | pr_warning("%s: Unknown message type: %u\n", |
| 224 | __func__, msg.msg_type); |
| 225 | return; |
| 226 | } |
| 227 | opal_message_do_notify(msg.msg_type, (void *)&msg); |
| 228 | } |
| 229 | |
| 230 | static int opal_message_notify(struct notifier_block *nb, |
| 231 | unsigned long events, void *change) |
| 232 | { |
| 233 | if (events & OPAL_EVENT_MSG_PENDING) |
| 234 | opal_handle_message(); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static struct notifier_block opal_message_nb = { |
| 239 | .notifier_call = opal_message_notify, |
| 240 | .next = NULL, |
| 241 | .priority = 0, |
| 242 | }; |
| 243 | |
| 244 | static int __init opal_message_init(void) |
| 245 | { |
| 246 | int ret, i; |
| 247 | |
| 248 | for (i = 0; i < OPAL_MSG_TYPE_MAX; i++) |
| 249 | ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]); |
| 250 | |
| 251 | ret = opal_notifier_register(&opal_message_nb); |
| 252 | if (ret) { |
| 253 | pr_err("%s: Can't register OPAL event notifier (%d)\n", |
| 254 | __func__, ret); |
| 255 | return ret; |
| 256 | } |
| 257 | return 0; |
| 258 | } |
| 259 | early_initcall(opal_message_init); |
| 260 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 261 | int opal_get_chars(uint32_t vtermno, char *buf, int count) |
| 262 | { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 263 | s64 rc; |
| 264 | __be64 evt, len; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 265 | |
| 266 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 267 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 268 | opal_poll_events(&evt); |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 269 | if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0) |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 270 | return 0; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 271 | len = cpu_to_be64(count); |
| 272 | rc = opal_console_read(vtermno, &len, buf); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 273 | if (rc == OPAL_SUCCESS) |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 274 | return be64_to_cpu(len); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int opal_put_chars(uint32_t vtermno, const char *data, int total_len) |
| 279 | { |
| 280 | int written = 0; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 281 | __be64 olen; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 282 | s64 len, rc; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 283 | unsigned long flags; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 284 | __be64 evt; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 285 | |
| 286 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 287 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 288 | |
| 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 |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 291 | * -EAGAIN if there isn't enough. |
| 292 | * |
| 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 |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 296 | */ |
| 297 | spin_lock_irqsave(&opal_write_lock, flags); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 298 | if (firmware_has_feature(FW_FEATURE_OPALv2)) { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 299 | rc = opal_console_write_buffer_space(vtermno, &olen); |
| 300 | len = be64_to_cpu(olen); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 301 | if (rc || len < total_len) { |
| 302 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 303 | /* Closed -> drop characters */ |
| 304 | if (rc) |
| 305 | return total_len; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 306 | opal_poll_events(NULL); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 307 | return -EAGAIN; |
| 308 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* We still try to handle partial completions, though they |
| 312 | * should no longer happen. |
| 313 | */ |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 314 | rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 315 | while(total_len > 0 && (rc == OPAL_BUSY || |
| 316 | rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 317 | olen = cpu_to_be64(total_len); |
| 318 | rc = opal_console_write(vtermno, &olen, data); |
| 319 | len = be64_to_cpu(olen); |
Benjamin Herrenschmidt | 1de1455 | 2013-05-08 14:14:26 +1000 | [diff] [blame] | 320 | |
| 321 | /* Closed or other error drop */ |
| 322 | if (rc != OPAL_SUCCESS && rc != OPAL_BUSY && |
| 323 | rc != OPAL_BUSY_EVENT) { |
| 324 | written = total_len; |
| 325 | break; |
| 326 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 327 | if (rc == OPAL_SUCCESS) { |
| 328 | total_len -= len; |
| 329 | data += len; |
| 330 | written += len; |
| 331 | } |
| 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 |
| 336 | */ |
| 337 | do |
| 338 | opal_poll_events(&evt); |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 339 | while(rc == OPAL_SUCCESS && |
| 340 | (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT)); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 341 | } |
| 342 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 343 | return written; |
| 344 | } |
| 345 | |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 346 | static int opal_recover_mce(struct pt_regs *regs, |
| 347 | struct machine_check_event *evt) |
| 348 | { |
| 349 | int recovered = 0; |
| 350 | uint64_t ea = get_mce_fault_addr(evt); |
| 351 | |
| 352 | if (!(regs->msr & MSR_RI)) { |
| 353 | /* If MSR_RI isn't set, we cannot recover */ |
| 354 | recovered = 0; |
| 355 | } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) { |
| 356 | /* Platform corrected itself */ |
| 357 | recovered = 1; |
| 358 | } else if (ea && !is_kernel_addr(ea)) { |
| 359 | /* |
| 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 |
| 363 | * in userspace. |
| 364 | * |
| 365 | * TODO: Queue up this address for hwpoisioning later. |
| 366 | */ |
| 367 | if (user_mode(regs) && !is_global_init(current)) { |
| 368 | _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip); |
| 369 | recovered = 1; |
| 370 | } else |
| 371 | recovered = 0; |
| 372 | } else if (user_mode(regs) && !is_global_init(current) && |
| 373 | evt->severity == MCE_SEV_ERROR_SYNC) { |
| 374 | /* |
| 375 | * If we have received a synchronous error when in userspace |
| 376 | * kill the task. |
| 377 | */ |
| 378 | _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip); |
| 379 | recovered = 1; |
| 380 | } |
| 381 | return recovered; |
| 382 | } |
| 383 | |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 384 | int opal_machine_check(struct pt_regs *regs) |
| 385 | { |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 386 | struct machine_check_event evt; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 387 | |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 388 | if (!get_mce_event(&evt, MCE_EVENT_RELEASE)) |
| 389 | return 0; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 390 | |
| 391 | /* Print things out */ |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 392 | if (evt.version != MCE_V1) { |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 393 | pr_err("Machine Check Exception, Unknown event version %d !\n", |
| 394 | evt.version); |
| 395 | return 0; |
| 396 | } |
Mahesh Salgaonkar | b5ff421 | 2013-10-30 20:05:49 +0530 | [diff] [blame] | 397 | machine_check_print_event_info(&evt); |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 398 | |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 399 | if (opal_recover_mce(regs, &evt)) |
| 400 | return 1; |
| 401 | return 0; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 404 | static irqreturn_t opal_interrupt(int irq, void *data) |
| 405 | { |
Anton Blanchard | 5e4da53 | 2013-09-23 12:05:06 +1000 | [diff] [blame] | 406 | __be64 events; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 407 | |
| 408 | opal_handle_interrupt(virq_to_hw(irq), &events); |
| 409 | |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 410 | opal_do_notifier(events); |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 411 | |
| 412 | return IRQ_HANDLED; |
| 413 | } |
| 414 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 415 | static int opal_sysfs_init(void) |
| 416 | { |
| 417 | opal_kobj = kobject_create_and_add("opal", firmware_kobj); |
| 418 | if (!opal_kobj) { |
| 419 | pr_warn("kobject_create_and_add opal failed\n"); |
| 420 | return -ENOMEM; |
| 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 426 | static int __init opal_init(void) |
| 427 | { |
| 428 | struct device_node *np, *consoles; |
Alistair Popple | 1cc79bc | 2013-09-23 12:04:54 +1000 | [diff] [blame] | 429 | const __be32 *irqs; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 430 | int rc, i, irqlen; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 431 | |
| 432 | opal_node = of_find_node_by_path("/ibm,opal"); |
| 433 | if (!opal_node) { |
| 434 | pr_warn("opal: Node not found\n"); |
| 435 | return -ENODEV; |
| 436 | } |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 437 | |
| 438 | /* Register OPAL consoles if any ports */ |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 439 | if (firmware_has_feature(FW_FEATURE_OPALv2)) |
| 440 | consoles = of_find_node_by_path("/ibm,opal/consoles"); |
| 441 | else |
| 442 | consoles = of_node_get(opal_node); |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 443 | if (consoles) { |
| 444 | for_each_child_of_node(consoles, np) { |
| 445 | if (strcmp(np->name, "serial")) |
| 446 | continue; |
| 447 | of_platform_device_create(np, NULL, NULL); |
| 448 | } |
| 449 | of_node_put(consoles); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 450 | } |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 451 | |
| 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); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 456 | opal_irq_count = irqlen / 4; |
| 457 | opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL); |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 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); |
| 461 | if (irq == NO_IRQ) { |
| 462 | pr_warning("opal: Failed to map irq 0x%x\n", hwirq); |
| 463 | continue; |
| 464 | } |
| 465 | rc = request_irq(irq, opal_interrupt, 0, "opal", NULL); |
| 466 | if (rc) |
| 467 | pr_warning("opal: Error %d requesting irq %d" |
| 468 | " (0x%x)\n", rc, irq, hwirq); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 469 | opal_irqs[i] = irq; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 470 | } |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 471 | |
| 472 | /* Create "opal" kobject under /sys/firmware */ |
| 473 | rc = opal_sysfs_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 474 | if (rc == 0) { |
| 475 | /* Setup code update interface */ |
| 476 | opal_flash_init(); |
| 477 | } |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 478 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | subsys_initcall(opal_init); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 482 | |
| 483 | void opal_shutdown(void) |
| 484 | { |
| 485 | unsigned int i; |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 486 | long rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 487 | |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 488 | /* First free interrupts, which will also mask them */ |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 489 | for (i = 0; i < opal_irq_count; i++) { |
| 490 | if (opal_irqs[i]) |
Anton Blanchard | b0d436c | 2013-08-07 02:01:24 +1000 | [diff] [blame] | 491 | free_irq(opal_irqs[i], NULL); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 492 | opal_irqs[i] = 0; |
| 493 | } |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 494 | |
| 495 | /* |
| 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 |
| 499 | */ |
| 500 | while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { |
| 501 | rc = opal_sync_host_reboot(); |
| 502 | if (rc == OPAL_BUSY) |
| 503 | opal_poll_events(NULL); |
| 504 | else |
| 505 | mdelay(10); |
| 506 | } |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 507 | } |