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> |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 24 | #include <linux/memblock.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 25 | #include <asm/opal.h> |
| 26 | #include <asm/firmware.h> |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 27 | #include <asm/mce.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 28 | |
| 29 | #include "powernv.h" |
| 30 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 31 | /* /sys/firmware/opal */ |
| 32 | struct kobject *opal_kobj; |
| 33 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 34 | struct opal { |
| 35 | u64 base; |
| 36 | u64 entry; |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 37 | u64 size; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 38 | } opal; |
| 39 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 40 | struct mcheck_recoverable_range { |
| 41 | u64 start_addr; |
| 42 | u64 end_addr; |
| 43 | u64 recover_addr; |
| 44 | }; |
| 45 | |
| 46 | static struct mcheck_recoverable_range *mc_recoverable_range; |
| 47 | static int mc_recoverable_range_len; |
| 48 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 49 | static struct device_node *opal_node; |
| 50 | static DEFINE_SPINLOCK(opal_write_lock); |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 51 | extern u64 opal_mc_secondary_handler[]; |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 52 | static unsigned int *opal_irqs; |
| 53 | static unsigned int opal_irq_count; |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 54 | static ATOMIC_NOTIFIER_HEAD(opal_notifier_head); |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 55 | 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] | 56 | static DEFINE_SPINLOCK(opal_notifier_lock); |
| 57 | static uint64_t last_notified_mask = 0x0ul; |
| 58 | static atomic_t opal_notifier_hold = ATOMIC_INIT(0); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 59 | |
| 60 | int __init early_init_dt_scan_opal(unsigned long node, |
| 61 | const char *uname, int depth, void *data) |
| 62 | { |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 63 | const void *basep, *entryp, *sizep; |
| 64 | unsigned long basesz, entrysz, runtimesz; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 65 | |
| 66 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) |
| 67 | return 0; |
| 68 | |
| 69 | basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz); |
| 70 | entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz); |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 71 | sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 72 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 73 | if (!basep || !entryp || !sizep) |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 74 | return 1; |
| 75 | |
| 76 | opal.base = of_read_number(basep, basesz/4); |
| 77 | opal.entry = of_read_number(entryp, entrysz/4); |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 78 | opal.size = of_read_number(sizep, runtimesz/4); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 79 | |
| 80 | pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n", |
| 81 | opal.base, basep, basesz); |
| 82 | pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n", |
| 83 | opal.entry, entryp, entrysz); |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 84 | pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%ld)\n", |
| 85 | opal.size, sizep, runtimesz); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 86 | |
| 87 | powerpc_firmware_features |= FW_FEATURE_OPAL; |
Benjamin Herrenschmidt | 75b93da | 2013-05-14 15:10:02 +1000 | [diff] [blame] | 88 | if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) { |
| 89 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
| 90 | powerpc_firmware_features |= FW_FEATURE_OPALv3; |
| 91 | printk("OPAL V3 detected !\n"); |
| 92 | } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) { |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 93 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
| 94 | printk("OPAL V2 detected !\n"); |
| 95 | } else { |
| 96 | printk("OPAL V1 detected !\n"); |
| 97 | } |
| 98 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 102 | int __init early_init_dt_scan_recoverable_ranges(unsigned long node, |
| 103 | const char *uname, int depth, void *data) |
| 104 | { |
| 105 | unsigned long i, size; |
| 106 | const __be32 *prop; |
| 107 | |
| 108 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) |
| 109 | return 0; |
| 110 | |
| 111 | prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &size); |
| 112 | |
| 113 | if (!prop) |
| 114 | return 1; |
| 115 | |
| 116 | pr_debug("Found machine check recoverable ranges.\n"); |
| 117 | |
| 118 | /* |
| 119 | * Allocate a buffer to hold the MC recoverable ranges. We would be |
| 120 | * accessing them in real mode, hence it needs to be within |
| 121 | * RMO region. |
| 122 | */ |
| 123 | mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64), |
| 124 | ppc64_rma_size)); |
| 125 | memset(mc_recoverable_range, 0, size); |
| 126 | |
| 127 | /* |
| 128 | * Each recoverable address entry is an (start address,len, |
| 129 | * recover address) pair, * 2 cells each, totalling 4 cells per entry. |
| 130 | */ |
| 131 | for (i = 0; i < size / (sizeof(*prop) * 5); i++) { |
| 132 | mc_recoverable_range[i].start_addr = |
| 133 | of_read_number(prop + (i * 5) + 0, 2); |
| 134 | mc_recoverable_range[i].end_addr = |
| 135 | mc_recoverable_range[i].start_addr + |
| 136 | of_read_number(prop + (i * 5) + 2, 1); |
| 137 | mc_recoverable_range[i].recover_addr = |
| 138 | of_read_number(prop + (i * 5) + 3, 2); |
| 139 | |
| 140 | pr_debug("Machine check recoverable range: %llx..%llx: %llx\n", |
| 141 | mc_recoverable_range[i].start_addr, |
| 142 | mc_recoverable_range[i].end_addr, |
| 143 | mc_recoverable_range[i].recover_addr); |
| 144 | } |
| 145 | mc_recoverable_range_len = i; |
| 146 | return 1; |
| 147 | } |
| 148 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 149 | static int __init opal_register_exception_handlers(void) |
| 150 | { |
Benjamin Herrenschmidt | 2918609 | 2013-09-23 12:05:04 +1000 | [diff] [blame] | 151 | #ifdef __BIG_ENDIAN__ |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 152 | u64 glue; |
| 153 | |
| 154 | if (!(powerpc_firmware_features & FW_FEATURE_OPAL)) |
| 155 | return -ENODEV; |
| 156 | |
Mahesh Salgaonkar | 28446de | 2013-10-30 20:05:58 +0530 | [diff] [blame] | 157 | /* Hookup some exception handlers except machine check. We use the |
| 158 | * fwnmi area at 0x7000 to provide the glue space to OPAL |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 159 | */ |
| 160 | glue = 0x7000; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 161 | opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER, |
| 162 | 0, glue); |
| 163 | glue += 128; |
| 164 | opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue); |
Benjamin Herrenschmidt | 2918609 | 2013-09-23 12:05:04 +1000 | [diff] [blame] | 165 | #endif |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 166 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 167 | return 0; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 170 | early_initcall(opal_register_exception_handlers); |
| 171 | |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 172 | int opal_notifier_register(struct notifier_block *nb) |
| 173 | { |
| 174 | if (!nb) { |
| 175 | pr_warning("%s: Invalid argument (%p)\n", |
| 176 | __func__, nb); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
| 180 | atomic_notifier_chain_register(&opal_notifier_head, nb); |
| 181 | return 0; |
| 182 | } |
Benjamin Herrenschmidt | 798af00 | 2014-03-28 13:36:31 +1100 | [diff] [blame^] | 183 | EXPORT_SYMBOL_GPL(opal_notifier_register); |
| 184 | |
| 185 | int opal_notifier_unregister(struct notifier_block *nb) |
| 186 | { |
| 187 | if (!nb) { |
| 188 | pr_warning("%s: Invalid argument (%p)\n", |
| 189 | __func__, nb); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | atomic_notifier_chain_unregister(&opal_notifier_head, nb); |
| 194 | return 0; |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(opal_notifier_unregister); |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 197 | |
| 198 | static void opal_do_notifier(uint64_t events) |
| 199 | { |
| 200 | unsigned long flags; |
| 201 | uint64_t changed_mask; |
| 202 | |
| 203 | if (atomic_read(&opal_notifier_hold)) |
| 204 | return; |
| 205 | |
| 206 | spin_lock_irqsave(&opal_notifier_lock, flags); |
| 207 | changed_mask = last_notified_mask ^ events; |
| 208 | last_notified_mask = events; |
| 209 | spin_unlock_irqrestore(&opal_notifier_lock, flags); |
| 210 | |
| 211 | /* |
| 212 | * We feed with the event bits and changed bits for |
| 213 | * enough information to the callback. |
| 214 | */ |
| 215 | atomic_notifier_call_chain(&opal_notifier_head, |
| 216 | events, (void *)changed_mask); |
| 217 | } |
| 218 | |
| 219 | void opal_notifier_update_evt(uint64_t evt_mask, |
| 220 | uint64_t evt_val) |
| 221 | { |
| 222 | unsigned long flags; |
| 223 | |
| 224 | spin_lock_irqsave(&opal_notifier_lock, flags); |
| 225 | last_notified_mask &= ~evt_mask; |
| 226 | last_notified_mask |= evt_val; |
| 227 | spin_unlock_irqrestore(&opal_notifier_lock, flags); |
| 228 | } |
| 229 | |
| 230 | void opal_notifier_enable(void) |
| 231 | { |
| 232 | int64_t rc; |
| 233 | uint64_t evt = 0; |
| 234 | |
| 235 | atomic_set(&opal_notifier_hold, 0); |
| 236 | |
| 237 | /* Process pending events */ |
| 238 | rc = opal_poll_events(&evt); |
| 239 | if (rc == OPAL_SUCCESS && evt) |
| 240 | opal_do_notifier(evt); |
| 241 | } |
| 242 | |
| 243 | void opal_notifier_disable(void) |
| 244 | { |
| 245 | atomic_set(&opal_notifier_hold, 1); |
| 246 | } |
| 247 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 248 | /* |
| 249 | * Opal message notifier based on message type. Allow subscribers to get |
| 250 | * notified for specific messgae type. |
| 251 | */ |
| 252 | int opal_message_notifier_register(enum OpalMessageType msg_type, |
| 253 | struct notifier_block *nb) |
| 254 | { |
| 255 | if (!nb) { |
| 256 | pr_warning("%s: Invalid argument (%p)\n", |
| 257 | __func__, nb); |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | if (msg_type > OPAL_MSG_TYPE_MAX) { |
| 261 | pr_warning("%s: Invalid message type argument (%d)\n", |
| 262 | __func__, msg_type); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | return atomic_notifier_chain_register( |
| 266 | &opal_msg_notifier_head[msg_type], nb); |
| 267 | } |
| 268 | |
| 269 | static void opal_message_do_notify(uint32_t msg_type, void *msg) |
| 270 | { |
| 271 | /* notify subscribers */ |
| 272 | atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type], |
| 273 | msg_type, msg); |
| 274 | } |
| 275 | |
| 276 | static void opal_handle_message(void) |
| 277 | { |
| 278 | s64 ret; |
| 279 | /* |
| 280 | * TODO: pre-allocate a message buffer depending on opal-msg-size |
| 281 | * value in /proc/device-tree. |
| 282 | */ |
| 283 | static struct opal_msg msg; |
| 284 | |
| 285 | ret = opal_get_msg(__pa(&msg), sizeof(msg)); |
| 286 | /* No opal message pending. */ |
| 287 | if (ret == OPAL_RESOURCE) |
| 288 | return; |
| 289 | |
| 290 | /* check for errors. */ |
| 291 | if (ret) { |
| 292 | pr_warning("%s: Failed to retrive opal message, err=%lld\n", |
| 293 | __func__, ret); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | /* Sanity check */ |
| 298 | if (msg.msg_type > OPAL_MSG_TYPE_MAX) { |
| 299 | pr_warning("%s: Unknown message type: %u\n", |
| 300 | __func__, msg.msg_type); |
| 301 | return; |
| 302 | } |
| 303 | opal_message_do_notify(msg.msg_type, (void *)&msg); |
| 304 | } |
| 305 | |
| 306 | static int opal_message_notify(struct notifier_block *nb, |
| 307 | unsigned long events, void *change) |
| 308 | { |
| 309 | if (events & OPAL_EVENT_MSG_PENDING) |
| 310 | opal_handle_message(); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static struct notifier_block opal_message_nb = { |
| 315 | .notifier_call = opal_message_notify, |
| 316 | .next = NULL, |
| 317 | .priority = 0, |
| 318 | }; |
| 319 | |
| 320 | static int __init opal_message_init(void) |
| 321 | { |
| 322 | int ret, i; |
| 323 | |
| 324 | for (i = 0; i < OPAL_MSG_TYPE_MAX; i++) |
| 325 | ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]); |
| 326 | |
| 327 | ret = opal_notifier_register(&opal_message_nb); |
| 328 | if (ret) { |
| 329 | pr_err("%s: Can't register OPAL event notifier (%d)\n", |
| 330 | __func__, ret); |
| 331 | return ret; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | early_initcall(opal_message_init); |
| 336 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 337 | int opal_get_chars(uint32_t vtermno, char *buf, int count) |
| 338 | { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 339 | s64 rc; |
| 340 | __be64 evt, len; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 341 | |
| 342 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 343 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 344 | opal_poll_events(&evt); |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 345 | if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0) |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 346 | return 0; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 347 | len = cpu_to_be64(count); |
| 348 | rc = opal_console_read(vtermno, &len, buf); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 349 | if (rc == OPAL_SUCCESS) |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 350 | return be64_to_cpu(len); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | int opal_put_chars(uint32_t vtermno, const char *data, int total_len) |
| 355 | { |
| 356 | int written = 0; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 357 | __be64 olen; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 358 | s64 len, rc; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 359 | unsigned long flags; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 360 | __be64 evt; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 361 | |
| 362 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 363 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 364 | |
| 365 | /* We want put_chars to be atomic to avoid mangling of hvsi |
| 366 | * packets. To do that, we first test for room and return |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 367 | * -EAGAIN if there isn't enough. |
| 368 | * |
| 369 | * Unfortunately, opal_console_write_buffer_space() doesn't |
| 370 | * appear to work on opal v1, so we just assume there is |
| 371 | * enough room and be done with it |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 372 | */ |
| 373 | spin_lock_irqsave(&opal_write_lock, flags); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 374 | if (firmware_has_feature(FW_FEATURE_OPALv2)) { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 375 | rc = opal_console_write_buffer_space(vtermno, &olen); |
| 376 | len = be64_to_cpu(olen); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 377 | if (rc || len < total_len) { |
| 378 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 379 | /* Closed -> drop characters */ |
| 380 | if (rc) |
| 381 | return total_len; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 382 | opal_poll_events(NULL); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 383 | return -EAGAIN; |
| 384 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* We still try to handle partial completions, though they |
| 388 | * should no longer happen. |
| 389 | */ |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 390 | rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 391 | while(total_len > 0 && (rc == OPAL_BUSY || |
| 392 | rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 393 | olen = cpu_to_be64(total_len); |
| 394 | rc = opal_console_write(vtermno, &olen, data); |
| 395 | len = be64_to_cpu(olen); |
Benjamin Herrenschmidt | 1de1455 | 2013-05-08 14:14:26 +1000 | [diff] [blame] | 396 | |
| 397 | /* Closed or other error drop */ |
| 398 | if (rc != OPAL_SUCCESS && rc != OPAL_BUSY && |
| 399 | rc != OPAL_BUSY_EVENT) { |
| 400 | written = total_len; |
| 401 | break; |
| 402 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 403 | if (rc == OPAL_SUCCESS) { |
| 404 | total_len -= len; |
| 405 | data += len; |
| 406 | written += len; |
| 407 | } |
| 408 | /* This is a bit nasty but we need that for the console to |
| 409 | * flush when there aren't any interrupts. We will clean |
| 410 | * things a bit later to limit that to synchronous path |
| 411 | * such as the kernel console and xmon/udbg |
| 412 | */ |
| 413 | do |
| 414 | opal_poll_events(&evt); |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 415 | while(rc == OPAL_SUCCESS && |
| 416 | (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT)); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 417 | } |
| 418 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 419 | return written; |
| 420 | } |
| 421 | |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 422 | static int opal_recover_mce(struct pt_regs *regs, |
| 423 | struct machine_check_event *evt) |
| 424 | { |
| 425 | int recovered = 0; |
| 426 | uint64_t ea = get_mce_fault_addr(evt); |
| 427 | |
| 428 | if (!(regs->msr & MSR_RI)) { |
| 429 | /* If MSR_RI isn't set, we cannot recover */ |
| 430 | recovered = 0; |
| 431 | } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) { |
| 432 | /* Platform corrected itself */ |
| 433 | recovered = 1; |
| 434 | } else if (ea && !is_kernel_addr(ea)) { |
| 435 | /* |
| 436 | * Faulting address is not in kernel text. We should be fine. |
| 437 | * We need to find which process uses this address. |
| 438 | * For now, kill the task if we have received exception when |
| 439 | * in userspace. |
| 440 | * |
| 441 | * TODO: Queue up this address for hwpoisioning later. |
| 442 | */ |
| 443 | if (user_mode(regs) && !is_global_init(current)) { |
| 444 | _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip); |
| 445 | recovered = 1; |
| 446 | } else |
| 447 | recovered = 0; |
| 448 | } else if (user_mode(regs) && !is_global_init(current) && |
| 449 | evt->severity == MCE_SEV_ERROR_SYNC) { |
| 450 | /* |
| 451 | * If we have received a synchronous error when in userspace |
| 452 | * kill the task. |
| 453 | */ |
| 454 | _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip); |
| 455 | recovered = 1; |
| 456 | } |
| 457 | return recovered; |
| 458 | } |
| 459 | |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 460 | int opal_machine_check(struct pt_regs *regs) |
| 461 | { |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 462 | struct machine_check_event evt; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 463 | |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 464 | if (!get_mce_event(&evt, MCE_EVENT_RELEASE)) |
| 465 | return 0; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 466 | |
| 467 | /* Print things out */ |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 468 | if (evt.version != MCE_V1) { |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 469 | pr_err("Machine Check Exception, Unknown event version %d !\n", |
| 470 | evt.version); |
| 471 | return 0; |
| 472 | } |
Mahesh Salgaonkar | b5ff421 | 2013-10-30 20:05:49 +0530 | [diff] [blame] | 473 | machine_check_print_event_info(&evt); |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 474 | |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 475 | if (opal_recover_mce(regs, &evt)) |
| 476 | return 1; |
| 477 | return 0; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 480 | static uint64_t find_recovery_address(uint64_t nip) |
| 481 | { |
| 482 | int i; |
| 483 | |
| 484 | for (i = 0; i < mc_recoverable_range_len; i++) |
| 485 | if ((nip >= mc_recoverable_range[i].start_addr) && |
| 486 | (nip < mc_recoverable_range[i].end_addr)) |
| 487 | return mc_recoverable_range[i].recover_addr; |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | bool opal_mce_check_early_recovery(struct pt_regs *regs) |
| 492 | { |
| 493 | uint64_t recover_addr = 0; |
| 494 | |
| 495 | if (!opal.base || !opal.size) |
| 496 | goto out; |
| 497 | |
| 498 | if ((regs->nip >= opal.base) && |
| 499 | (regs->nip <= (opal.base + opal.size))) |
| 500 | recover_addr = find_recovery_address(regs->nip); |
| 501 | |
| 502 | /* |
| 503 | * Setup regs->nip to rfi into fixup address. |
| 504 | */ |
| 505 | if (recover_addr) |
| 506 | regs->nip = recover_addr; |
| 507 | |
| 508 | out: |
| 509 | return !!recover_addr; |
| 510 | } |
| 511 | |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 512 | static irqreturn_t opal_interrupt(int irq, void *data) |
| 513 | { |
Anton Blanchard | 5e4da53 | 2013-09-23 12:05:06 +1000 | [diff] [blame] | 514 | __be64 events; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 515 | |
| 516 | opal_handle_interrupt(virq_to_hw(irq), &events); |
| 517 | |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 518 | opal_do_notifier(events); |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 519 | |
| 520 | return IRQ_HANDLED; |
| 521 | } |
| 522 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 523 | static int opal_sysfs_init(void) |
| 524 | { |
| 525 | opal_kobj = kobject_create_and_add("opal", firmware_kobj); |
| 526 | if (!opal_kobj) { |
| 527 | pr_warn("kobject_create_and_add opal failed\n"); |
| 528 | return -ENOMEM; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 534 | static int __init opal_init(void) |
| 535 | { |
| 536 | struct device_node *np, *consoles; |
Alistair Popple | 1cc79bc | 2013-09-23 12:04:54 +1000 | [diff] [blame] | 537 | const __be32 *irqs; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 538 | int rc, i, irqlen; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 539 | |
| 540 | opal_node = of_find_node_by_path("/ibm,opal"); |
| 541 | if (!opal_node) { |
| 542 | pr_warn("opal: Node not found\n"); |
| 543 | return -ENODEV; |
| 544 | } |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 545 | |
| 546 | /* Register OPAL consoles if any ports */ |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 547 | if (firmware_has_feature(FW_FEATURE_OPALv2)) |
| 548 | consoles = of_find_node_by_path("/ibm,opal/consoles"); |
| 549 | else |
| 550 | consoles = of_node_get(opal_node); |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 551 | if (consoles) { |
| 552 | for_each_child_of_node(consoles, np) { |
| 553 | if (strcmp(np->name, "serial")) |
| 554 | continue; |
| 555 | of_platform_device_create(np, NULL, NULL); |
| 556 | } |
| 557 | of_node_put(consoles); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 558 | } |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 559 | |
| 560 | /* Find all OPAL interrupts and request them */ |
| 561 | irqs = of_get_property(opal_node, "opal-interrupts", &irqlen); |
| 562 | pr_debug("opal: Found %d interrupts reserved for OPAL\n", |
| 563 | irqs ? (irqlen / 4) : 0); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 564 | opal_irq_count = irqlen / 4; |
| 565 | opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL); |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 566 | for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) { |
| 567 | unsigned int hwirq = be32_to_cpup(irqs); |
| 568 | unsigned int irq = irq_create_mapping(NULL, hwirq); |
| 569 | if (irq == NO_IRQ) { |
| 570 | pr_warning("opal: Failed to map irq 0x%x\n", hwirq); |
| 571 | continue; |
| 572 | } |
| 573 | rc = request_irq(irq, opal_interrupt, 0, "opal", NULL); |
| 574 | if (rc) |
| 575 | pr_warning("opal: Error %d requesting irq %d" |
| 576 | " (0x%x)\n", rc, irq, hwirq); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 577 | opal_irqs[i] = irq; |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 578 | } |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 579 | |
| 580 | /* Create "opal" kobject under /sys/firmware */ |
| 581 | rc = opal_sysfs_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 582 | if (rc == 0) { |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 583 | /* Setup error log interface */ |
| 584 | rc = opal_elog_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 585 | /* Setup code update interface */ |
| 586 | opal_flash_init(); |
Stewart Smith | c7e64b9 | 2014-03-03 10:25:42 +1100 | [diff] [blame] | 587 | /* Setup platform dump extract interface */ |
| 588 | opal_platform_dump_init(); |
Neelesh Gupta | 4029cd6 | 2014-03-07 11:02:09 +0530 | [diff] [blame] | 589 | /* Setup system parameters interface */ |
| 590 | opal_sys_param_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 591 | } |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 592 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | subsys_initcall(opal_init); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 596 | |
| 597 | void opal_shutdown(void) |
| 598 | { |
| 599 | unsigned int i; |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 600 | long rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 601 | |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 602 | /* First free interrupts, which will also mask them */ |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 603 | for (i = 0; i < opal_irq_count; i++) { |
| 604 | if (opal_irqs[i]) |
Anton Blanchard | b0d436c | 2013-08-07 02:01:24 +1000 | [diff] [blame] | 605 | free_irq(opal_irqs[i], NULL); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 606 | opal_irqs[i] = 0; |
| 607 | } |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * Then sync with OPAL which ensure anything that can |
| 611 | * potentially write to our memory has completed such |
| 612 | * as an ongoing dump retrieval |
| 613 | */ |
| 614 | while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { |
| 615 | rc = opal_sync_host_reboot(); |
| 616 | if (rc == OPAL_BUSY) |
| 617 | opal_poll_events(NULL); |
| 618 | else |
| 619 | mdelay(10); |
| 620 | } |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 621 | } |