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 | |
Benjamin Herrenschmidt | c8742f8 | 2014-12-11 12:39:49 +1100 | [diff] [blame] | 12 | #define pr_fmt(fmt) "opal: " fmt |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 13 | |
Benjamin Herrenschmidt | c8742f8 | 2014-12-11 12:39:49 +1100 | [diff] [blame] | 14 | #include <linux/printk.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/of.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 17 | #include <linux/of_fdt.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 18 | #include <linux/of_platform.h> |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 20 | #include <linux/notifier.h> |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 21 | #include <linux/slab.h> |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 22 | #include <linux/sched.h> |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 23 | #include <linux/kobject.h> |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 24 | #include <linux/delay.h> |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 25 | #include <linux/memblock.h> |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 26 | #include <linux/kthread.h> |
| 27 | #include <linux/freezer.h> |
Michael Ellerman | b14726c | 2014-07-15 22:22:24 +1000 | [diff] [blame] | 28 | |
| 29 | #include <asm/machdep.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 30 | #include <asm/opal.h> |
| 31 | #include <asm/firmware.h> |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 32 | #include <asm/mce.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 33 | |
| 34 | #include "powernv.h" |
| 35 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 36 | /* /sys/firmware/opal */ |
| 37 | struct kobject *opal_kobj; |
| 38 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 39 | struct opal { |
| 40 | u64 base; |
| 41 | u64 entry; |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 42 | u64 size; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 43 | } opal; |
| 44 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 45 | struct mcheck_recoverable_range { |
| 46 | u64 start_addr; |
| 47 | u64 end_addr; |
| 48 | u64 recover_addr; |
| 49 | }; |
| 50 | |
| 51 | static struct mcheck_recoverable_range *mc_recoverable_range; |
| 52 | static int mc_recoverable_range_len; |
| 53 | |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 54 | struct device_node *opal_node; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(opal_write_lock); |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 56 | static ATOMIC_NOTIFIER_HEAD(opal_notifier_head); |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 57 | 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] | 58 | static DEFINE_SPINLOCK(opal_notifier_lock); |
| 59 | static uint64_t last_notified_mask = 0x0ul; |
| 60 | static atomic_t opal_notifier_hold = ATOMIC_INIT(0); |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 61 | static uint32_t opal_heartbeat; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 62 | |
Benjamin Herrenschmidt | 4926616 | 2014-05-20 11:01:28 +1000 | [diff] [blame] | 63 | static void opal_reinit_cores(void) |
| 64 | { |
| 65 | /* Do the actual re-init, This will clobber all FPRs, VRs, etc... |
| 66 | * |
| 67 | * It will preserve non volatile GPRs and HSPRG0/1. It will |
| 68 | * also restore HIDs and other SPRs to their original value |
| 69 | * but it might clobber a bunch. |
| 70 | */ |
| 71 | #ifdef __BIG_ENDIAN__ |
| 72 | opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE); |
| 73 | #else |
| 74 | opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_LE); |
| 75 | #endif |
| 76 | } |
| 77 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 78 | int __init early_init_dt_scan_opal(unsigned long node, |
| 79 | const char *uname, int depth, void *data) |
| 80 | { |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 81 | const void *basep, *entryp, *sizep; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 82 | int basesz, entrysz, runtimesz; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 83 | |
| 84 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) |
| 85 | return 0; |
| 86 | |
| 87 | basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz); |
| 88 | entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz); |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 89 | sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 90 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 91 | if (!basep || !entryp || !sizep) |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 92 | return 1; |
| 93 | |
| 94 | opal.base = of_read_number(basep, basesz/4); |
| 95 | opal.entry = of_read_number(entryp, entrysz/4); |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 96 | opal.size = of_read_number(sizep, runtimesz/4); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 97 | |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 98 | pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n", |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 99 | opal.base, basep, basesz); |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 100 | pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n", |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 101 | opal.entry, entryp, entrysz); |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 102 | pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n", |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 103 | opal.size, sizep, runtimesz); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 104 | |
| 105 | powerpc_firmware_features |= FW_FEATURE_OPAL; |
Benjamin Herrenschmidt | 75b93da | 2013-05-14 15:10:02 +1000 | [diff] [blame] | 106 | if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) { |
| 107 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
| 108 | powerpc_firmware_features |= FW_FEATURE_OPALv3; |
Anton Blanchard | 9a4f5cd | 2014-09-17 14:39:38 +1000 | [diff] [blame] | 109 | pr_info("OPAL V3 detected !\n"); |
Benjamin Herrenschmidt | 75b93da | 2013-05-14 15:10:02 +1000 | [diff] [blame] | 110 | } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) { |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 111 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
Anton Blanchard | 9a4f5cd | 2014-09-17 14:39:38 +1000 | [diff] [blame] | 112 | pr_info("OPAL V2 detected !\n"); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 113 | } else { |
Anton Blanchard | 9a4f5cd | 2014-09-17 14:39:38 +1000 | [diff] [blame] | 114 | pr_info("OPAL V1 detected !\n"); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Benjamin Herrenschmidt | 4926616 | 2014-05-20 11:01:28 +1000 | [diff] [blame] | 117 | /* Reinit all cores with the right endian */ |
| 118 | opal_reinit_cores(); |
| 119 | |
| 120 | /* Restore some bits */ |
| 121 | if (cur_cpu_spec->cpu_restore) |
| 122 | cur_cpu_spec->cpu_restore(); |
| 123 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 124 | return 1; |
| 125 | } |
| 126 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 127 | int __init early_init_dt_scan_recoverable_ranges(unsigned long node, |
| 128 | const char *uname, int depth, void *data) |
| 129 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 130 | int i, psize, size; |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 131 | const __be32 *prop; |
| 132 | |
| 133 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) |
| 134 | return 0; |
| 135 | |
Mahesh Salgaonkar | 6e556b4 | 2014-03-30 11:03:23 +0530 | [diff] [blame] | 136 | prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize); |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 137 | |
| 138 | if (!prop) |
| 139 | return 1; |
| 140 | |
| 141 | pr_debug("Found machine check recoverable ranges.\n"); |
| 142 | |
| 143 | /* |
Mahesh Salgaonkar | 6e556b4 | 2014-03-30 11:03:23 +0530 | [diff] [blame] | 144 | * Calculate number of available entries. |
| 145 | * |
| 146 | * Each recoverable address range entry is (start address, len, |
| 147 | * recovery address), 2 cells each for start and recovery address, |
| 148 | * 1 cell for len, totalling 5 cells per entry. |
| 149 | */ |
| 150 | mc_recoverable_range_len = psize / (sizeof(*prop) * 5); |
| 151 | |
| 152 | /* Sanity check */ |
| 153 | if (!mc_recoverable_range_len) |
| 154 | return 1; |
| 155 | |
| 156 | /* Size required to hold all the entries. */ |
| 157 | size = mc_recoverable_range_len * |
| 158 | sizeof(struct mcheck_recoverable_range); |
| 159 | |
| 160 | /* |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 161 | * Allocate a buffer to hold the MC recoverable ranges. We would be |
| 162 | * accessing them in real mode, hence it needs to be within |
| 163 | * RMO region. |
| 164 | */ |
| 165 | mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64), |
| 166 | ppc64_rma_size)); |
| 167 | memset(mc_recoverable_range, 0, size); |
| 168 | |
Mahesh Salgaonkar | 6e556b4 | 2014-03-30 11:03:23 +0530 | [diff] [blame] | 169 | for (i = 0; i < mc_recoverable_range_len; i++) { |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 170 | mc_recoverable_range[i].start_addr = |
| 171 | of_read_number(prop + (i * 5) + 0, 2); |
| 172 | mc_recoverable_range[i].end_addr = |
| 173 | mc_recoverable_range[i].start_addr + |
| 174 | of_read_number(prop + (i * 5) + 2, 1); |
| 175 | mc_recoverable_range[i].recover_addr = |
| 176 | of_read_number(prop + (i * 5) + 3, 2); |
| 177 | |
| 178 | pr_debug("Machine check recoverable range: %llx..%llx: %llx\n", |
| 179 | mc_recoverable_range[i].start_addr, |
| 180 | mc_recoverable_range[i].end_addr, |
| 181 | mc_recoverable_range[i].recover_addr); |
| 182 | } |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 183 | return 1; |
| 184 | } |
| 185 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 186 | static int __init opal_register_exception_handlers(void) |
| 187 | { |
Benjamin Herrenschmidt | 2918609 | 2013-09-23 12:05:04 +1000 | [diff] [blame] | 188 | #ifdef __BIG_ENDIAN__ |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 189 | u64 glue; |
| 190 | |
| 191 | if (!(powerpc_firmware_features & FW_FEATURE_OPAL)) |
| 192 | return -ENODEV; |
| 193 | |
Mahesh Salgaonkar | 28446de | 2013-10-30 20:05:58 +0530 | [diff] [blame] | 194 | /* Hookup some exception handlers except machine check. We use the |
| 195 | * fwnmi area at 0x7000 to provide the glue space to OPAL |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 196 | */ |
| 197 | glue = 0x7000; |
Mahesh Salgaonkar | 6507955c | 2014-10-10 21:28:26 +0530 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Check if we are running on newer firmware that exports |
| 201 | * OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to patch |
| 202 | * the HMI interrupt and we catch it directly in Linux. |
| 203 | * |
| 204 | * For older firmware (i.e currently released POWER8 System Firmware |
| 205 | * as of today <= SV810_087), we fallback to old behavior and let OPAL |
| 206 | * patch the HMI vector and handle it inside OPAL firmware. |
| 207 | * |
| 208 | * For newer firmware (in development/yet to be released) we will |
| 209 | * start catching/handling HMI directly in Linux. |
| 210 | */ |
| 211 | if (!opal_check_token(OPAL_HANDLE_HMI)) { |
Michael Ellerman | 0813513 | 2015-01-28 15:10:33 +1100 | [diff] [blame] | 212 | pr_info("Old firmware detected, OPAL handles HMIs.\n"); |
Mahesh Salgaonkar | 6507955c | 2014-10-10 21:28:26 +0530 | [diff] [blame] | 213 | opal_register_exception_handler( |
| 214 | OPAL_HYPERVISOR_MAINTENANCE_HANDLER, |
| 215 | 0, glue); |
| 216 | glue += 128; |
| 217 | } |
| 218 | |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 219 | opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue); |
Benjamin Herrenschmidt | 2918609 | 2013-09-23 12:05:04 +1000 | [diff] [blame] | 220 | #endif |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 221 | |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 222 | return 0; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 223 | } |
Michael Ellerman | b14726c | 2014-07-15 22:22:24 +1000 | [diff] [blame] | 224 | machine_early_initcall(powernv, opal_register_exception_handlers); |
Jeremy Kerr | c4463b3 | 2013-05-01 22:31:50 +0000 | [diff] [blame] | 225 | |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 226 | int opal_notifier_register(struct notifier_block *nb) |
| 227 | { |
| 228 | if (!nb) { |
| 229 | pr_warning("%s: Invalid argument (%p)\n", |
| 230 | __func__, nb); |
| 231 | return -EINVAL; |
| 232 | } |
| 233 | |
| 234 | atomic_notifier_chain_register(&opal_notifier_head, nb); |
| 235 | return 0; |
| 236 | } |
Benjamin Herrenschmidt | 798af00 | 2014-03-28 13:36:31 +1100 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(opal_notifier_register); |
| 238 | |
| 239 | int opal_notifier_unregister(struct notifier_block *nb) |
| 240 | { |
| 241 | if (!nb) { |
| 242 | pr_warning("%s: Invalid argument (%p)\n", |
| 243 | __func__, nb); |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | |
| 247 | atomic_notifier_chain_unregister(&opal_notifier_head, nb); |
| 248 | return 0; |
| 249 | } |
| 250 | EXPORT_SYMBOL_GPL(opal_notifier_unregister); |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 251 | |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 252 | void opal_do_notifier(uint64_t events) |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 253 | { |
| 254 | unsigned long flags; |
| 255 | uint64_t changed_mask; |
| 256 | |
| 257 | if (atomic_read(&opal_notifier_hold)) |
| 258 | return; |
| 259 | |
| 260 | spin_lock_irqsave(&opal_notifier_lock, flags); |
| 261 | changed_mask = last_notified_mask ^ events; |
| 262 | last_notified_mask = events; |
| 263 | spin_unlock_irqrestore(&opal_notifier_lock, flags); |
| 264 | |
| 265 | /* |
| 266 | * We feed with the event bits and changed bits for |
| 267 | * enough information to the callback. |
| 268 | */ |
| 269 | atomic_notifier_call_chain(&opal_notifier_head, |
| 270 | events, (void *)changed_mask); |
| 271 | } |
| 272 | |
| 273 | void opal_notifier_update_evt(uint64_t evt_mask, |
| 274 | uint64_t evt_val) |
| 275 | { |
| 276 | unsigned long flags; |
| 277 | |
| 278 | spin_lock_irqsave(&opal_notifier_lock, flags); |
| 279 | last_notified_mask &= ~evt_mask; |
| 280 | last_notified_mask |= evt_val; |
| 281 | spin_unlock_irqrestore(&opal_notifier_lock, flags); |
| 282 | } |
| 283 | |
| 284 | void opal_notifier_enable(void) |
| 285 | { |
| 286 | int64_t rc; |
Anton Blanchard | 56b4c99 | 2014-04-22 15:01:24 +1000 | [diff] [blame] | 287 | __be64 evt = 0; |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 288 | |
| 289 | atomic_set(&opal_notifier_hold, 0); |
| 290 | |
| 291 | /* Process pending events */ |
| 292 | rc = opal_poll_events(&evt); |
| 293 | if (rc == OPAL_SUCCESS && evt) |
Anton Blanchard | 56b4c99 | 2014-04-22 15:01:24 +1000 | [diff] [blame] | 294 | opal_do_notifier(be64_to_cpu(evt)); |
Gavin Shan | 1bc98de | 2013-06-20 18:13:22 +0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void opal_notifier_disable(void) |
| 298 | { |
| 299 | atomic_set(&opal_notifier_hold, 1); |
| 300 | } |
| 301 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 302 | /* |
| 303 | * Opal message notifier based on message type. Allow subscribers to get |
| 304 | * notified for specific messgae type. |
| 305 | */ |
Michael Ellerman | d7cf83f | 2015-02-17 20:01:54 +1100 | [diff] [blame] | 306 | int opal_message_notifier_register(enum opal_msg_type msg_type, |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 307 | struct notifier_block *nb) |
| 308 | { |
Neelesh Gupta | 792f96e | 2015-02-11 11:57:06 +0530 | [diff] [blame] | 309 | if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) { |
| 310 | pr_warning("%s: Invalid arguments, msg_type:%d\n", |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 311 | __func__, msg_type); |
| 312 | return -EINVAL; |
| 313 | } |
Neelesh Gupta | 792f96e | 2015-02-11 11:57:06 +0530 | [diff] [blame] | 314 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 315 | return atomic_notifier_chain_register( |
| 316 | &opal_msg_notifier_head[msg_type], nb); |
| 317 | } |
| 318 | |
Michael Ellerman | df60f57 | 2015-03-26 20:03:16 +1100 | [diff] [blame] | 319 | int opal_message_notifier_unregister(enum opal_msg_type msg_type, |
Neelesh Gupta | b921e90 | 2015-02-11 11:57:23 +0530 | [diff] [blame] | 320 | struct notifier_block *nb) |
| 321 | { |
| 322 | return atomic_notifier_chain_unregister( |
| 323 | &opal_msg_notifier_head[msg_type], nb); |
| 324 | } |
| 325 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 326 | static void opal_message_do_notify(uint32_t msg_type, void *msg) |
| 327 | { |
| 328 | /* notify subscribers */ |
| 329 | atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type], |
| 330 | msg_type, msg); |
| 331 | } |
| 332 | |
| 333 | static void opal_handle_message(void) |
| 334 | { |
| 335 | s64 ret; |
| 336 | /* |
| 337 | * TODO: pre-allocate a message buffer depending on opal-msg-size |
| 338 | * value in /proc/device-tree. |
| 339 | */ |
| 340 | static struct opal_msg msg; |
Anton Blanchard | bb4398e | 2014-03-28 16:33:33 +1100 | [diff] [blame] | 341 | u32 type; |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 342 | |
| 343 | ret = opal_get_msg(__pa(&msg), sizeof(msg)); |
| 344 | /* No opal message pending. */ |
| 345 | if (ret == OPAL_RESOURCE) |
| 346 | return; |
| 347 | |
| 348 | /* check for errors. */ |
| 349 | if (ret) { |
Masanari Iida | 1a84db5 | 2014-08-29 23:37:33 +0900 | [diff] [blame] | 350 | pr_warning("%s: Failed to retrieve opal message, err=%lld\n", |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 351 | __func__, ret); |
| 352 | return; |
| 353 | } |
| 354 | |
Anton Blanchard | bb4398e | 2014-03-28 16:33:33 +1100 | [diff] [blame] | 355 | type = be32_to_cpu(msg.msg_type); |
| 356 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 357 | /* Sanity check */ |
Neelesh Gupta | 792f96e | 2015-02-11 11:57:06 +0530 | [diff] [blame] | 358 | if (type >= OPAL_MSG_TYPE_MAX) { |
Anton Blanchard | bb4398e | 2014-03-28 16:33:33 +1100 | [diff] [blame] | 359 | pr_warning("%s: Unknown message type: %u\n", __func__, type); |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 360 | return; |
| 361 | } |
Anton Blanchard | bb4398e | 2014-03-28 16:33:33 +1100 | [diff] [blame] | 362 | opal_message_do_notify(type, (void *)&msg); |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 363 | } |
| 364 | |
Alistair Popple | a295af2 | 2015-05-15 14:06:41 +1000 | [diff] [blame^] | 365 | static irqreturn_t opal_message_notify(int irq, void *data) |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 366 | { |
Alistair Popple | a295af2 | 2015-05-15 14:06:41 +1000 | [diff] [blame^] | 367 | opal_handle_message(); |
| 368 | return IRQ_HANDLED; |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 369 | } |
| 370 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 371 | static int __init opal_message_init(void) |
| 372 | { |
Alistair Popple | a295af2 | 2015-05-15 14:06:41 +1000 | [diff] [blame^] | 373 | int ret, i, irq; |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 374 | |
| 375 | for (i = 0; i < OPAL_MSG_TYPE_MAX; i++) |
| 376 | ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]); |
| 377 | |
Alistair Popple | a295af2 | 2015-05-15 14:06:41 +1000 | [diff] [blame^] | 378 | irq = opal_event_request(ilog2(OPAL_EVENT_MSG_PENDING)); |
| 379 | if (!irq) { |
| 380 | pr_err("%s: Can't register OPAL event irq (%d)\n", |
| 381 | __func__, irq); |
| 382 | return irq; |
| 383 | } |
| 384 | |
| 385 | ret = request_irq(irq, opal_message_notify, |
| 386 | IRQ_TYPE_LEVEL_HIGH, "opal-msg", NULL); |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 387 | if (ret) { |
Alistair Popple | a295af2 | 2015-05-15 14:06:41 +1000 | [diff] [blame^] | 388 | pr_err("%s: Can't request OPAL event irq (%d)\n", |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 389 | __func__, ret); |
| 390 | return ret; |
| 391 | } |
Alistair Popple | a295af2 | 2015-05-15 14:06:41 +1000 | [diff] [blame^] | 392 | |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 393 | return 0; |
| 394 | } |
Mahesh Salgaonkar | 2436636 | 2013-11-18 15:35:58 +0530 | [diff] [blame] | 395 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 396 | int opal_get_chars(uint32_t vtermno, char *buf, int count) |
| 397 | { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 398 | s64 rc; |
| 399 | __be64 evt, len; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 400 | |
| 401 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 402 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 403 | opal_poll_events(&evt); |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 404 | if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0) |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 405 | return 0; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 406 | len = cpu_to_be64(count); |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 407 | rc = opal_console_read(vtermno, &len, buf); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 408 | if (rc == OPAL_SUCCESS) |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 409 | return be64_to_cpu(len); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | int opal_put_chars(uint32_t vtermno, const char *data, int total_len) |
| 414 | { |
| 415 | int written = 0; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 416 | __be64 olen; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 417 | s64 len, rc; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 418 | unsigned long flags; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 419 | __be64 evt; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 420 | |
| 421 | if (!opal.entry) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 422 | return -ENODEV; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 423 | |
| 424 | /* We want put_chars to be atomic to avoid mangling of hvsi |
| 425 | * packets. To do that, we first test for room and return |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 426 | * -EAGAIN if there isn't enough. |
| 427 | * |
| 428 | * Unfortunately, opal_console_write_buffer_space() doesn't |
| 429 | * appear to work on opal v1, so we just assume there is |
| 430 | * enough room and be done with it |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 431 | */ |
| 432 | spin_lock_irqsave(&opal_write_lock, flags); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 433 | if (firmware_has_feature(FW_FEATURE_OPALv2)) { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 434 | rc = opal_console_write_buffer_space(vtermno, &olen); |
| 435 | len = be64_to_cpu(olen); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 436 | if (rc || len < total_len) { |
| 437 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 438 | /* Closed -> drop characters */ |
| 439 | if (rc) |
| 440 | return total_len; |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 441 | opal_poll_events(NULL); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 442 | return -EAGAIN; |
| 443 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* We still try to handle partial completions, though they |
| 447 | * should no longer happen. |
| 448 | */ |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 449 | rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 450 | while(total_len > 0 && (rc == OPAL_BUSY || |
| 451 | rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) { |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 452 | olen = cpu_to_be64(total_len); |
| 453 | rc = opal_console_write(vtermno, &olen, data); |
| 454 | len = be64_to_cpu(olen); |
Benjamin Herrenschmidt | 1de1455 | 2013-05-08 14:14:26 +1000 | [diff] [blame] | 455 | |
| 456 | /* Closed or other error drop */ |
| 457 | if (rc != OPAL_SUCCESS && rc != OPAL_BUSY && |
| 458 | rc != OPAL_BUSY_EVENT) { |
| 459 | written = total_len; |
| 460 | break; |
| 461 | } |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 462 | if (rc == OPAL_SUCCESS) { |
| 463 | total_len -= len; |
| 464 | data += len; |
| 465 | written += len; |
| 466 | } |
| 467 | /* This is a bit nasty but we need that for the console to |
| 468 | * flush when there aren't any interrupts. We will clean |
| 469 | * things a bit later to limit that to synchronous path |
| 470 | * such as the kernel console and xmon/udbg |
| 471 | */ |
| 472 | do |
| 473 | opal_poll_events(&evt); |
Benjamin Herrenschmidt | 4f89363 | 2013-09-23 12:05:02 +1000 | [diff] [blame] | 474 | while(rc == OPAL_SUCCESS && |
| 475 | (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT)); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 476 | } |
| 477 | spin_unlock_irqrestore(&opal_write_lock, flags); |
| 478 | return written; |
| 479 | } |
| 480 | |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 481 | static int opal_recover_mce(struct pt_regs *regs, |
| 482 | struct machine_check_event *evt) |
| 483 | { |
| 484 | int recovered = 0; |
| 485 | uint64_t ea = get_mce_fault_addr(evt); |
| 486 | |
| 487 | if (!(regs->msr & MSR_RI)) { |
| 488 | /* If MSR_RI isn't set, we cannot recover */ |
| 489 | recovered = 0; |
| 490 | } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) { |
| 491 | /* Platform corrected itself */ |
| 492 | recovered = 1; |
| 493 | } else if (ea && !is_kernel_addr(ea)) { |
| 494 | /* |
| 495 | * Faulting address is not in kernel text. We should be fine. |
| 496 | * We need to find which process uses this address. |
| 497 | * For now, kill the task if we have received exception when |
| 498 | * in userspace. |
| 499 | * |
| 500 | * TODO: Queue up this address for hwpoisioning later. |
| 501 | */ |
| 502 | if (user_mode(regs) && !is_global_init(current)) { |
| 503 | _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip); |
| 504 | recovered = 1; |
| 505 | } else |
| 506 | recovered = 0; |
| 507 | } else if (user_mode(regs) && !is_global_init(current) && |
| 508 | evt->severity == MCE_SEV_ERROR_SYNC) { |
| 509 | /* |
| 510 | * If we have received a synchronous error when in userspace |
| 511 | * kill the task. |
| 512 | */ |
| 513 | _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip); |
| 514 | recovered = 1; |
| 515 | } |
| 516 | return recovered; |
| 517 | } |
| 518 | |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 519 | int opal_machine_check(struct pt_regs *regs) |
| 520 | { |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 521 | struct machine_check_event evt; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 522 | |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 523 | if (!get_mce_event(&evt, MCE_EVENT_RELEASE)) |
| 524 | return 0; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 525 | |
| 526 | /* Print things out */ |
Mahesh Salgaonkar | 36df96f | 2013-10-30 20:05:40 +0530 | [diff] [blame] | 527 | if (evt.version != MCE_V1) { |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 528 | pr_err("Machine Check Exception, Unknown event version %d !\n", |
| 529 | evt.version); |
| 530 | return 0; |
| 531 | } |
Mahesh Salgaonkar | b5ff421 | 2013-10-30 20:05:49 +0530 | [diff] [blame] | 532 | machine_check_print_event_info(&evt); |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 533 | |
Mahesh Salgaonkar | b63a0ff | 2013-10-30 20:06:13 +0530 | [diff] [blame] | 534 | if (opal_recover_mce(regs, &evt)) |
| 535 | return 1; |
| 536 | return 0; |
Benjamin Herrenschmidt | ed79ba9 | 2011-09-19 17:45:04 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Mahesh Salgaonkar | 0869b6f | 2014-07-29 18:40:01 +0530 | [diff] [blame] | 539 | /* Early hmi handler called in real mode. */ |
| 540 | int opal_hmi_exception_early(struct pt_regs *regs) |
| 541 | { |
Mahesh Salgaonkar | 0ef95b4 | 2014-07-29 18:40:07 +0530 | [diff] [blame] | 542 | s64 rc; |
| 543 | |
| 544 | /* |
| 545 | * call opal hmi handler. Pass paca address as token. |
| 546 | * The return value OPAL_SUCCESS is an indication that there is |
| 547 | * an HMI event generated waiting to pull by Linux. |
| 548 | */ |
| 549 | rc = opal_handle_hmi(); |
| 550 | if (rc == OPAL_SUCCESS) { |
| 551 | local_paca->hmi_event_available = 1; |
| 552 | return 1; |
| 553 | } |
Mahesh Salgaonkar | 0869b6f | 2014-07-29 18:40:01 +0530 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /* HMI exception handler called in virtual mode during check_irq_replay. */ |
| 558 | int opal_handle_hmi_exception(struct pt_regs *regs) |
| 559 | { |
Mahesh Salgaonkar | 0ef95b4 | 2014-07-29 18:40:07 +0530 | [diff] [blame] | 560 | s64 rc; |
| 561 | __be64 evt = 0; |
| 562 | |
| 563 | /* |
| 564 | * Check if HMI event is available. |
| 565 | * if Yes, then call opal_poll_events to pull opal messages and |
| 566 | * process them. |
| 567 | */ |
| 568 | if (!local_paca->hmi_event_available) |
| 569 | return 0; |
| 570 | |
| 571 | local_paca->hmi_event_available = 0; |
| 572 | rc = opal_poll_events(&evt); |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 573 | if (rc == OPAL_SUCCESS && evt) { |
Mahesh Salgaonkar | 0ef95b4 | 2014-07-29 18:40:07 +0530 | [diff] [blame] | 574 | opal_do_notifier(be64_to_cpu(evt)); |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 575 | opal_handle_events(be64_to_cpu(evt)); |
| 576 | } |
Mahesh Salgaonkar | 0ef95b4 | 2014-07-29 18:40:07 +0530 | [diff] [blame] | 577 | |
| 578 | return 1; |
Mahesh Salgaonkar | 0869b6f | 2014-07-29 18:40:01 +0530 | [diff] [blame] | 579 | } |
| 580 | |
Mahesh Salgaonkar | 55672ec | 2013-12-16 10:46:24 +0530 | [diff] [blame] | 581 | static uint64_t find_recovery_address(uint64_t nip) |
| 582 | { |
| 583 | int i; |
| 584 | |
| 585 | for (i = 0; i < mc_recoverable_range_len; i++) |
| 586 | if ((nip >= mc_recoverable_range[i].start_addr) && |
| 587 | (nip < mc_recoverable_range[i].end_addr)) |
| 588 | return mc_recoverable_range[i].recover_addr; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | bool opal_mce_check_early_recovery(struct pt_regs *regs) |
| 593 | { |
| 594 | uint64_t recover_addr = 0; |
| 595 | |
| 596 | if (!opal.base || !opal.size) |
| 597 | goto out; |
| 598 | |
| 599 | if ((regs->nip >= opal.base) && |
| 600 | (regs->nip <= (opal.base + opal.size))) |
| 601 | recover_addr = find_recovery_address(regs->nip); |
| 602 | |
| 603 | /* |
| 604 | * Setup regs->nip to rfi into fixup address. |
| 605 | */ |
| 606 | if (recover_addr) |
| 607 | regs->nip = recover_addr; |
| 608 | |
| 609 | out: |
| 610 | return !!recover_addr; |
| 611 | } |
| 612 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 613 | static int opal_sysfs_init(void) |
| 614 | { |
| 615 | opal_kobj = kobject_create_and_add("opal", firmware_kobj); |
| 616 | if (!opal_kobj) { |
| 617 | pr_warn("kobject_create_and_add opal failed\n"); |
| 618 | return -ENOMEM; |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Benjamin Herrenschmidt | c8742f8 | 2014-12-11 12:39:49 +1100 | [diff] [blame] | 624 | static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj, |
| 625 | struct bin_attribute *bin_attr, |
| 626 | char *buf, loff_t off, size_t count) |
| 627 | { |
| 628 | return memory_read_from_buffer(buf, count, &off, bin_attr->private, |
| 629 | bin_attr->size); |
| 630 | } |
| 631 | |
| 632 | static BIN_ATTR_RO(symbol_map, 0); |
| 633 | |
| 634 | static void opal_export_symmap(void) |
| 635 | { |
| 636 | const __be64 *syms; |
| 637 | unsigned int size; |
| 638 | struct device_node *fw; |
| 639 | int rc; |
| 640 | |
| 641 | fw = of_find_node_by_path("/ibm,opal/firmware"); |
| 642 | if (!fw) |
| 643 | return; |
| 644 | syms = of_get_property(fw, "symbol-map", &size); |
| 645 | if (!syms || size != 2 * sizeof(__be64)) |
| 646 | return; |
| 647 | |
| 648 | /* Setup attributes */ |
| 649 | bin_attr_symbol_map.private = __va(be64_to_cpu(syms[0])); |
| 650 | bin_attr_symbol_map.size = be64_to_cpu(syms[1]); |
| 651 | |
| 652 | rc = sysfs_create_bin_file(opal_kobj, &bin_attr_symbol_map); |
| 653 | if (rc) |
| 654 | pr_warn("Error %d creating OPAL symbols file\n", rc); |
| 655 | } |
| 656 | |
Vasant Hegde | b09c2ec | 2014-08-09 11:15:45 +0530 | [diff] [blame] | 657 | static void __init opal_dump_region_init(void) |
| 658 | { |
| 659 | void *addr; |
| 660 | uint64_t size; |
| 661 | int rc; |
| 662 | |
Stewart Smith | b962f5a | 2015-02-12 16:25:27 +1100 | [diff] [blame] | 663 | if (!opal_check_token(OPAL_REGISTER_DUMP_REGION)) |
| 664 | return; |
| 665 | |
Vasant Hegde | b09c2ec | 2014-08-09 11:15:45 +0530 | [diff] [blame] | 666 | /* Register kernel log buffer */ |
| 667 | addr = log_buf_addr_get(); |
Pranith Kumar | 6501ab5e3 | 2015-01-21 21:26:09 -0500 | [diff] [blame] | 668 | if (addr == NULL) |
| 669 | return; |
| 670 | |
Vasant Hegde | b09c2ec | 2014-08-09 11:15:45 +0530 | [diff] [blame] | 671 | size = log_buf_len_get(); |
Pranith Kumar | 6501ab5e3 | 2015-01-21 21:26:09 -0500 | [diff] [blame] | 672 | if (size == 0) |
| 673 | return; |
| 674 | |
Vasant Hegde | b09c2ec | 2014-08-09 11:15:45 +0530 | [diff] [blame] | 675 | rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF, |
| 676 | __pa(addr), size); |
| 677 | /* Don't warn if this is just an older OPAL that doesn't |
| 678 | * know about that call |
| 679 | */ |
| 680 | if (rc && rc != OPAL_UNSUPPORTED) |
| 681 | pr_warn("DUMP: Failed to register kernel log buffer. " |
| 682 | "rc = %d\n", rc); |
| 683 | } |
Jeremy Kerr | 608b286 | 2014-11-06 11:38:27 +0800 | [diff] [blame] | 684 | |
Cyril Bur | ed59190 | 2015-04-01 14:05:30 +0800 | [diff] [blame] | 685 | static void opal_flash_init(struct device_node *opal_node) |
| 686 | { |
| 687 | struct device_node *np; |
| 688 | |
| 689 | for_each_child_of_node(opal_node, np) |
| 690 | if (of_device_is_compatible(np, "ibm,opal-flash")) |
| 691 | of_platform_device_create(np, NULL, NULL); |
| 692 | } |
| 693 | |
Jeremy Kerr | 608b286 | 2014-11-06 11:38:27 +0800 | [diff] [blame] | 694 | static void opal_ipmi_init(struct device_node *opal_node) |
| 695 | { |
| 696 | struct device_node *np; |
| 697 | |
| 698 | for_each_child_of_node(opal_node, np) |
| 699 | if (of_device_is_compatible(np, "ibm,opal-ipmi")) |
| 700 | of_platform_device_create(np, NULL, NULL); |
| 701 | } |
| 702 | |
Neelesh Gupta | 4708345 | 2014-12-13 23:31:05 +0530 | [diff] [blame] | 703 | static void opal_i2c_create_devs(void) |
| 704 | { |
| 705 | struct device_node *np; |
| 706 | |
| 707 | for_each_compatible_node(np, NULL, "ibm,opal-i2c") |
| 708 | of_platform_device_create(np, NULL, NULL); |
| 709 | } |
| 710 | |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 711 | static int kopald(void *unused) |
| 712 | { |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 713 | __be64 events; |
| 714 | |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 715 | set_freezable(); |
| 716 | do { |
| 717 | try_to_freeze(); |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 718 | opal_poll_events(&events); |
| 719 | opal_handle_events(be64_to_cpu(events)); |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 720 | msleep_interruptible(opal_heartbeat); |
| 721 | } while (!kthread_should_stop()); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static void opal_init_heartbeat(void) |
| 727 | { |
| 728 | /* Old firwmware, we assume the HVC heartbeat is sufficient */ |
| 729 | if (of_property_read_u32(opal_node, "ibm,heartbeat-ms", |
| 730 | &opal_heartbeat) != 0) |
| 731 | opal_heartbeat = 0; |
| 732 | |
| 733 | if (opal_heartbeat) |
| 734 | kthread_run(kopald, NULL, "kopald"); |
| 735 | } |
| 736 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 737 | static int __init opal_init(void) |
| 738 | { |
| 739 | struct device_node *np, *consoles; |
Gavin Shan | c1c3a52 | 2015-01-23 14:25:05 +1100 | [diff] [blame] | 740 | int rc; |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 741 | |
| 742 | opal_node = of_find_node_by_path("/ibm,opal"); |
| 743 | if (!opal_node) { |
Michael Ellerman | 0813513 | 2015-01-28 15:10:33 +1100 | [diff] [blame] | 744 | pr_warn("Device node not found\n"); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 745 | return -ENODEV; |
| 746 | } |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 747 | |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 748 | /* Initialise OPAL events */ |
| 749 | opal_event_init(); |
| 750 | |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 751 | /* Register OPAL consoles if any ports */ |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 752 | if (firmware_has_feature(FW_FEATURE_OPALv2)) |
| 753 | consoles = of_find_node_by_path("/ibm,opal/consoles"); |
| 754 | else |
| 755 | consoles = of_node_get(opal_node); |
Benjamin Herrenschmidt | 2db29d2 | 2013-07-15 13:03:14 +1000 | [diff] [blame] | 756 | if (consoles) { |
| 757 | for_each_child_of_node(consoles, np) { |
| 758 | if (strcmp(np->name, "serial")) |
| 759 | continue; |
| 760 | of_platform_device_create(np, NULL, NULL); |
| 761 | } |
| 762 | of_node_put(consoles); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 763 | } |
Benjamin Herrenschmidt | a125e09 | 2011-09-19 17:45:03 +0000 | [diff] [blame] | 764 | |
Alistair Popple | 96e023e | 2015-05-15 14:06:36 +1000 | [diff] [blame] | 765 | /* Initialise OPAL messaging system */ |
| 766 | opal_message_init(); |
| 767 | |
| 768 | /* Initialise OPAL asynchronous completion interface */ |
| 769 | opal_async_comp_init(); |
| 770 | |
| 771 | /* Initialise OPAL sensor interface */ |
| 772 | opal_sensor_init(); |
| 773 | |
| 774 | /* Initialise OPAL hypervisor maintainence interrupt handling */ |
| 775 | opal_hmi_handler_init(); |
| 776 | |
Neelesh Gupta | 4708345 | 2014-12-13 23:31:05 +0530 | [diff] [blame] | 777 | /* Create i2c platform devices */ |
| 778 | opal_i2c_create_devs(); |
| 779 | |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 780 | /* Setup a heatbeat thread if requested by OPAL */ |
| 781 | opal_init_heartbeat(); |
| 782 | |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 783 | /* Create "opal" kobject under /sys/firmware */ |
| 784 | rc = opal_sysfs_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 785 | if (rc == 0) { |
Benjamin Herrenschmidt | c8742f8 | 2014-12-11 12:39:49 +1100 | [diff] [blame] | 786 | /* Export symbol map to userspace */ |
| 787 | opal_export_symmap(); |
Vasant Hegde | b09c2ec | 2014-08-09 11:15:45 +0530 | [diff] [blame] | 788 | /* Setup dump region interface */ |
| 789 | opal_dump_region_init(); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 790 | /* Setup error log interface */ |
| 791 | rc = opal_elog_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 792 | /* Setup code update interface */ |
Cyril Bur | ed59190 | 2015-04-01 14:05:30 +0800 | [diff] [blame] | 793 | opal_flash_update_init(); |
Stewart Smith | c7e64b9 | 2014-03-03 10:25:42 +1100 | [diff] [blame] | 794 | /* Setup platform dump extract interface */ |
| 795 | opal_platform_dump_init(); |
Neelesh Gupta | 4029cd6 | 2014-03-07 11:02:09 +0530 | [diff] [blame] | 796 | /* Setup system parameters interface */ |
| 797 | opal_sys_param_init(); |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 798 | /* Setup message log interface. */ |
| 799 | opal_msglog_init(); |
Vasant Hegde | 50bd615 | 2013-10-24 16:04:58 +0530 | [diff] [blame] | 800 | } |
Vasant Hegde | 6f68b5e | 2013-08-27 15:09:52 +0530 | [diff] [blame] | 801 | |
Benjamin Herrenschmidt | 3bf5756 | 2014-11-14 16:13:50 +1100 | [diff] [blame] | 802 | /* Initialize OPAL IPMI backend */ |
Jeremy Kerr | 608b286 | 2014-11-06 11:38:27 +0800 | [diff] [blame] | 803 | opal_ipmi_init(opal_node); |
| 804 | |
Cyril Bur | ed59190 | 2015-04-01 14:05:30 +0800 | [diff] [blame] | 805 | opal_flash_init(opal_node); |
| 806 | |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 807 | return 0; |
| 808 | } |
Michael Ellerman | b14726c | 2014-07-15 22:22:24 +1000 | [diff] [blame] | 809 | machine_subsys_initcall(powernv, opal_init); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 810 | |
| 811 | void opal_shutdown(void) |
| 812 | { |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 813 | long rc = OPAL_BUSY; |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 814 | |
Alistair Popple | 9f0fd04 | 2015-05-15 14:06:37 +1000 | [diff] [blame] | 815 | opal_event_shutdown(); |
Vasant Hegde | f7d98d1 | 2014-01-15 17:02:04 +1100 | [diff] [blame] | 816 | |
| 817 | /* |
| 818 | * Then sync with OPAL which ensure anything that can |
| 819 | * potentially write to our memory has completed such |
| 820 | * as an ongoing dump retrieval |
| 821 | */ |
| 822 | while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { |
| 823 | rc = opal_sync_host_reboot(); |
| 824 | if (rc == OPAL_BUSY) |
| 825 | opal_poll_events(NULL); |
| 826 | else |
| 827 | mdelay(10); |
| 828 | } |
Vasant Hegde | b09c2ec | 2014-08-09 11:15:45 +0530 | [diff] [blame] | 829 | |
| 830 | /* Unregister memory dump region */ |
Stewart Smith | b962f5a | 2015-02-12 16:25:27 +1100 | [diff] [blame] | 831 | if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION)) |
| 832 | opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF); |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 833 | } |
Joel Stanley | e28b05e | 2014-04-01 14:28:20 +1030 | [diff] [blame] | 834 | |
| 835 | /* Export this so that test modules can use it */ |
| 836 | EXPORT_SYMBOL_GPL(opal_invalid_call); |
Jeremy Kerr | 608b286 | 2014-11-06 11:38:27 +0800 | [diff] [blame] | 837 | EXPORT_SYMBOL_GPL(opal_ipmi_send); |
| 838 | EXPORT_SYMBOL_GPL(opal_ipmi_recv); |
Cyril Bur | ed59190 | 2015-04-01 14:05:30 +0800 | [diff] [blame] | 839 | EXPORT_SYMBOL_GPL(opal_flash_read); |
| 840 | EXPORT_SYMBOL_GPL(opal_flash_write); |
| 841 | EXPORT_SYMBOL_GPL(opal_flash_erase); |
Anton Blanchard | 3441f04 | 2014-04-22 15:01:26 +1000 | [diff] [blame] | 842 | |
| 843 | /* Convert a region of vmalloc memory to an opal sg list */ |
| 844 | struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr, |
| 845 | unsigned long vmalloc_size) |
| 846 | { |
| 847 | struct opal_sg_list *sg, *first = NULL; |
| 848 | unsigned long i = 0; |
| 849 | |
| 850 | sg = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 851 | if (!sg) |
| 852 | goto nomem; |
| 853 | |
| 854 | first = sg; |
| 855 | |
| 856 | while (vmalloc_size > 0) { |
| 857 | uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT; |
| 858 | uint64_t length = min(vmalloc_size, PAGE_SIZE); |
| 859 | |
| 860 | sg->entry[i].data = cpu_to_be64(data); |
| 861 | sg->entry[i].length = cpu_to_be64(length); |
| 862 | i++; |
| 863 | |
| 864 | if (i >= SG_ENTRIES_PER_NODE) { |
| 865 | struct opal_sg_list *next; |
| 866 | |
| 867 | next = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 868 | if (!next) |
| 869 | goto nomem; |
| 870 | |
| 871 | sg->length = cpu_to_be64( |
| 872 | i * sizeof(struct opal_sg_entry) + 16); |
| 873 | i = 0; |
| 874 | sg->next = cpu_to_be64(__pa(next)); |
| 875 | sg = next; |
| 876 | } |
| 877 | |
| 878 | vmalloc_addr += length; |
| 879 | vmalloc_size -= length; |
| 880 | } |
| 881 | |
| 882 | sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16); |
| 883 | |
| 884 | return first; |
| 885 | |
| 886 | nomem: |
| 887 | pr_err("%s : Failed to allocate memory\n", __func__); |
| 888 | opal_free_sg_list(first); |
| 889 | return NULL; |
| 890 | } |
| 891 | |
| 892 | void opal_free_sg_list(struct opal_sg_list *sg) |
| 893 | { |
| 894 | while (sg) { |
| 895 | uint64_t next = be64_to_cpu(sg->next); |
| 896 | |
| 897 | kfree(sg); |
| 898 | |
| 899 | if (next) |
| 900 | sg = __va(next); |
| 901 | else |
| 902 | sg = NULL; |
| 903 | } |
| 904 | } |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 905 | |
Cédric Le Goater | e3c5c2e | 2015-03-30 12:06:09 +0200 | [diff] [blame] | 906 | int opal_error_code(int rc) |
| 907 | { |
| 908 | switch (rc) { |
| 909 | case OPAL_SUCCESS: return 0; |
| 910 | |
| 911 | case OPAL_PARAMETER: return -EINVAL; |
| 912 | case OPAL_ASYNC_COMPLETION: return -EINPROGRESS; |
| 913 | case OPAL_BUSY_EVENT: return -EBUSY; |
| 914 | case OPAL_NO_MEM: return -ENOMEM; |
| 915 | |
| 916 | case OPAL_UNSUPPORTED: return -EIO; |
| 917 | case OPAL_HARDWARE: return -EIO; |
| 918 | case OPAL_INTERNAL_ERROR: return -EIO; |
| 919 | default: |
| 920 | pr_err("%s: unexpected OPAL error %d\n", __func__, rc); |
| 921 | return -EIO; |
| 922 | } |
| 923 | } |
| 924 | |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 925 | EXPORT_SYMBOL_GPL(opal_poll_events); |
| 926 | EXPORT_SYMBOL_GPL(opal_rtc_read); |
| 927 | EXPORT_SYMBOL_GPL(opal_rtc_write); |
| 928 | EXPORT_SYMBOL_GPL(opal_tpo_read); |
| 929 | EXPORT_SYMBOL_GPL(opal_tpo_write); |
Neelesh Gupta | 4708345 | 2014-12-13 23:31:05 +0530 | [diff] [blame] | 930 | EXPORT_SYMBOL_GPL(opal_i2c_request); |