blob: b0021ac3e0a8cb9c4edddaff180043394f20c915 [file] [log] [blame]
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +00001/*
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 Herrenschmidtc8742f82014-12-11 12:39:49 +110012#define pr_fmt(fmt) "opal: " fmt
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000013
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +110014#include <linux/printk.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000015#include <linux/types.h>
16#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050017#include <linux/of_fdt.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000018#include <linux/of_platform.h>
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +000019#include <linux/interrupt.h>
Gavin Shan1bc98de2013-06-20 18:13:22 +080020#include <linux/notifier.h>
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100021#include <linux/slab.h>
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +053022#include <linux/sched.h>
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053023#include <linux/kobject.h>
Vasant Hegdef7d98d12014-01-15 17:02:04 +110024#include <linux/delay.h>
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053025#include <linux/memblock.h>
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +110026#include <linux/kthread.h>
27#include <linux/freezer.h>
Michael Ellermanb14726c2014-07-15 22:22:24 +100028
29#include <asm/machdep.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000030#include <asm/opal.h>
31#include <asm/firmware.h>
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +053032#include <asm/mce.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000033
34#include "powernv.h"
35
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053036/* /sys/firmware/opal */
37struct kobject *opal_kobj;
38
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000039struct opal {
40 u64 base;
41 u64 entry;
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053042 u64 size;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000043} opal;
44
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053045struct mcheck_recoverable_range {
46 u64 start_addr;
47 u64 end_addr;
48 u64 recover_addr;
49};
50
51static struct mcheck_recoverable_range *mc_recoverable_range;
52static int mc_recoverable_range_len;
53
Joel Stanleybfc36892014-04-01 14:28:19 +103054struct device_node *opal_node;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000055static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100056static unsigned int *opal_irqs;
57static unsigned int opal_irq_count;
Gavin Shan1bc98de2013-06-20 18:13:22 +080058static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +053059static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
Gavin Shan1bc98de2013-06-20 18:13:22 +080060static DEFINE_SPINLOCK(opal_notifier_lock);
61static uint64_t last_notified_mask = 0x0ul;
62static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +110063static uint32_t opal_heartbeat;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000064
Benjamin Herrenschmidt49266162014-05-20 11:01:28 +100065static void opal_reinit_cores(void)
66{
67 /* Do the actual re-init, This will clobber all FPRs, VRs, etc...
68 *
69 * It will preserve non volatile GPRs and HSPRG0/1. It will
70 * also restore HIDs and other SPRs to their original value
71 * but it might clobber a bunch.
72 */
73#ifdef __BIG_ENDIAN__
74 opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
75#else
76 opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_LE);
77#endif
78}
79
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000080int __init early_init_dt_scan_opal(unsigned long node,
81 const char *uname, int depth, void *data)
82{
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053083 const void *basep, *entryp, *sizep;
Rob Herring9d0c4df2014-04-01 23:49:03 -050084 int basesz, entrysz, runtimesz;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000085
86 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
87 return 0;
88
89 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
90 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053091 sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000092
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053093 if (!basep || !entryp || !sizep)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000094 return 1;
95
96 opal.base = of_read_number(basep, basesz/4);
97 opal.entry = of_read_number(entryp, entrysz/4);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053098 opal.size = of_read_number(sizep, runtimesz/4);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000099
Rob Herring9d0c4df2014-04-01 23:49:03 -0500100 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000101 opal.base, basep, basesz);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500102 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000103 opal.entry, entryp, entrysz);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500104 pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530105 opal.size, sizep, runtimesz);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000106
107 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +1000108 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
109 powerpc_firmware_features |= FW_FEATURE_OPALv2;
110 powerpc_firmware_features |= FW_FEATURE_OPALv3;
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000111 pr_info("OPAL V3 detected !\n");
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +1000112 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000113 powerpc_firmware_features |= FW_FEATURE_OPALv2;
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000114 pr_info("OPAL V2 detected !\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000115 } else {
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000116 pr_info("OPAL V1 detected !\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000117 }
118
Benjamin Herrenschmidt49266162014-05-20 11:01:28 +1000119 /* Reinit all cores with the right endian */
120 opal_reinit_cores();
121
122 /* Restore some bits */
123 if (cur_cpu_spec->cpu_restore)
124 cur_cpu_spec->cpu_restore();
125
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000126 return 1;
127}
128
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530129int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
130 const char *uname, int depth, void *data)
131{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500132 int i, psize, size;
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530133 const __be32 *prop;
134
135 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
136 return 0;
137
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530138 prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530139
140 if (!prop)
141 return 1;
142
143 pr_debug("Found machine check recoverable ranges.\n");
144
145 /*
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530146 * Calculate number of available entries.
147 *
148 * Each recoverable address range entry is (start address, len,
149 * recovery address), 2 cells each for start and recovery address,
150 * 1 cell for len, totalling 5 cells per entry.
151 */
152 mc_recoverable_range_len = psize / (sizeof(*prop) * 5);
153
154 /* Sanity check */
155 if (!mc_recoverable_range_len)
156 return 1;
157
158 /* Size required to hold all the entries. */
159 size = mc_recoverable_range_len *
160 sizeof(struct mcheck_recoverable_range);
161
162 /*
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530163 * Allocate a buffer to hold the MC recoverable ranges. We would be
164 * accessing them in real mode, hence it needs to be within
165 * RMO region.
166 */
167 mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64),
168 ppc64_rma_size));
169 memset(mc_recoverable_range, 0, size);
170
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530171 for (i = 0; i < mc_recoverable_range_len; i++) {
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530172 mc_recoverable_range[i].start_addr =
173 of_read_number(prop + (i * 5) + 0, 2);
174 mc_recoverable_range[i].end_addr =
175 mc_recoverable_range[i].start_addr +
176 of_read_number(prop + (i * 5) + 2, 1);
177 mc_recoverable_range[i].recover_addr =
178 of_read_number(prop + (i * 5) + 3, 2);
179
180 pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
181 mc_recoverable_range[i].start_addr,
182 mc_recoverable_range[i].end_addr,
183 mc_recoverable_range[i].recover_addr);
184 }
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530185 return 1;
186}
187
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000188static int __init opal_register_exception_handlers(void)
189{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000190#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000191 u64 glue;
192
193 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
194 return -ENODEV;
195
Mahesh Salgaonkar28446de2013-10-30 20:05:58 +0530196 /* Hookup some exception handlers except machine check. We use the
197 * fwnmi area at 0x7000 to provide the glue space to OPAL
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000198 */
199 glue = 0x7000;
Mahesh Salgaonkar6507955c2014-10-10 21:28:26 +0530200
201 /*
202 * Check if we are running on newer firmware that exports
203 * OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to patch
204 * the HMI interrupt and we catch it directly in Linux.
205 *
206 * For older firmware (i.e currently released POWER8 System Firmware
207 * as of today <= SV810_087), we fallback to old behavior and let OPAL
208 * patch the HMI vector and handle it inside OPAL firmware.
209 *
210 * For newer firmware (in development/yet to be released) we will
211 * start catching/handling HMI directly in Linux.
212 */
213 if (!opal_check_token(OPAL_HANDLE_HMI)) {
Michael Ellerman08135132015-01-28 15:10:33 +1100214 pr_info("Old firmware detected, OPAL handles HMIs.\n");
Mahesh Salgaonkar6507955c2014-10-10 21:28:26 +0530215 opal_register_exception_handler(
216 OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
217 0, glue);
218 glue += 128;
219 }
220
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000221 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000222#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000223
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000224 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000225}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000226machine_early_initcall(powernv, opal_register_exception_handlers);
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000227
Gavin Shan1bc98de2013-06-20 18:13:22 +0800228int opal_notifier_register(struct notifier_block *nb)
229{
230 if (!nb) {
231 pr_warning("%s: Invalid argument (%p)\n",
232 __func__, nb);
233 return -EINVAL;
234 }
235
236 atomic_notifier_chain_register(&opal_notifier_head, nb);
237 return 0;
238}
Benjamin Herrenschmidt798af002014-03-28 13:36:31 +1100239EXPORT_SYMBOL_GPL(opal_notifier_register);
240
241int opal_notifier_unregister(struct notifier_block *nb)
242{
243 if (!nb) {
244 pr_warning("%s: Invalid argument (%p)\n",
245 __func__, nb);
246 return -EINVAL;
247 }
248
249 atomic_notifier_chain_unregister(&opal_notifier_head, nb);
250 return 0;
251}
252EXPORT_SYMBOL_GPL(opal_notifier_unregister);
Gavin Shan1bc98de2013-06-20 18:13:22 +0800253
254static void opal_do_notifier(uint64_t events)
255{
256 unsigned long flags;
257 uint64_t changed_mask;
258
259 if (atomic_read(&opal_notifier_hold))
260 return;
261
262 spin_lock_irqsave(&opal_notifier_lock, flags);
263 changed_mask = last_notified_mask ^ events;
264 last_notified_mask = events;
265 spin_unlock_irqrestore(&opal_notifier_lock, flags);
266
267 /*
268 * We feed with the event bits and changed bits for
269 * enough information to the callback.
270 */
271 atomic_notifier_call_chain(&opal_notifier_head,
272 events, (void *)changed_mask);
273}
274
275void opal_notifier_update_evt(uint64_t evt_mask,
276 uint64_t evt_val)
277{
278 unsigned long flags;
279
280 spin_lock_irqsave(&opal_notifier_lock, flags);
281 last_notified_mask &= ~evt_mask;
282 last_notified_mask |= evt_val;
283 spin_unlock_irqrestore(&opal_notifier_lock, flags);
284}
285
286void opal_notifier_enable(void)
287{
288 int64_t rc;
Anton Blanchard56b4c992014-04-22 15:01:24 +1000289 __be64 evt = 0;
Gavin Shan1bc98de2013-06-20 18:13:22 +0800290
291 atomic_set(&opal_notifier_hold, 0);
292
293 /* Process pending events */
294 rc = opal_poll_events(&evt);
295 if (rc == OPAL_SUCCESS && evt)
Anton Blanchard56b4c992014-04-22 15:01:24 +1000296 opal_do_notifier(be64_to_cpu(evt));
Gavin Shan1bc98de2013-06-20 18:13:22 +0800297}
298
299void opal_notifier_disable(void)
300{
301 atomic_set(&opal_notifier_hold, 1);
302}
303
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530304/*
305 * Opal message notifier based on message type. Allow subscribers to get
306 * notified for specific messgae type.
307 */
308int opal_message_notifier_register(enum OpalMessageType msg_type,
309 struct notifier_block *nb)
310{
Neelesh Gupta792f96e2015-02-11 11:57:06 +0530311 if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {
312 pr_warning("%s: Invalid arguments, msg_type:%d\n",
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530313 __func__, msg_type);
314 return -EINVAL;
315 }
Neelesh Gupta792f96e2015-02-11 11:57:06 +0530316
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530317 return atomic_notifier_chain_register(
318 &opal_msg_notifier_head[msg_type], nb);
319}
320
321static void opal_message_do_notify(uint32_t msg_type, void *msg)
322{
323 /* notify subscribers */
324 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
325 msg_type, msg);
326}
327
328static void opal_handle_message(void)
329{
330 s64 ret;
331 /*
332 * TODO: pre-allocate a message buffer depending on opal-msg-size
333 * value in /proc/device-tree.
334 */
335 static struct opal_msg msg;
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100336 u32 type;
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530337
338 ret = opal_get_msg(__pa(&msg), sizeof(msg));
339 /* No opal message pending. */
340 if (ret == OPAL_RESOURCE)
341 return;
342
343 /* check for errors. */
344 if (ret) {
Masanari Iida1a84db52014-08-29 23:37:33 +0900345 pr_warning("%s: Failed to retrieve opal message, err=%lld\n",
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530346 __func__, ret);
347 return;
348 }
349
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100350 type = be32_to_cpu(msg.msg_type);
351
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530352 /* Sanity check */
Neelesh Gupta792f96e2015-02-11 11:57:06 +0530353 if (type >= OPAL_MSG_TYPE_MAX) {
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100354 pr_warning("%s: Unknown message type: %u\n", __func__, type);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530355 return;
356 }
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100357 opal_message_do_notify(type, (void *)&msg);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530358}
359
360static int opal_message_notify(struct notifier_block *nb,
361 unsigned long events, void *change)
362{
363 if (events & OPAL_EVENT_MSG_PENDING)
364 opal_handle_message();
365 return 0;
366}
367
368static struct notifier_block opal_message_nb = {
369 .notifier_call = opal_message_notify,
370 .next = NULL,
371 .priority = 0,
372};
373
374static int __init opal_message_init(void)
375{
376 int ret, i;
377
378 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
379 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
380
381 ret = opal_notifier_register(&opal_message_nb);
382 if (ret) {
383 pr_err("%s: Can't register OPAL event notifier (%d)\n",
384 __func__, ret);
385 return ret;
386 }
387 return 0;
388}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000389machine_early_initcall(powernv, opal_message_init);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530390
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000391int opal_get_chars(uint32_t vtermno, char *buf, int count)
392{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000393 s64 rc;
394 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000395
396 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000397 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000398 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000399 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000400 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000401 len = cpu_to_be64(count);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500402 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000403 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000404 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000405 return 0;
406}
407
408int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
409{
410 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000411 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000412 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000413 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000414 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000415
416 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000417 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000418
419 /* We want put_chars to be atomic to avoid mangling of hvsi
420 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000421 * -EAGAIN if there isn't enough.
422 *
423 * Unfortunately, opal_console_write_buffer_space() doesn't
424 * appear to work on opal v1, so we just assume there is
425 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000426 */
427 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000428 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000429 rc = opal_console_write_buffer_space(vtermno, &olen);
430 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000431 if (rc || len < total_len) {
432 spin_unlock_irqrestore(&opal_write_lock, flags);
433 /* Closed -> drop characters */
434 if (rc)
435 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000436 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000437 return -EAGAIN;
438 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000439 }
440
441 /* We still try to handle partial completions, though they
442 * should no longer happen.
443 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000444 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000445 while(total_len > 0 && (rc == OPAL_BUSY ||
446 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000447 olen = cpu_to_be64(total_len);
448 rc = opal_console_write(vtermno, &olen, data);
449 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000450
451 /* Closed or other error drop */
452 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
453 rc != OPAL_BUSY_EVENT) {
454 written = total_len;
455 break;
456 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000457 if (rc == OPAL_SUCCESS) {
458 total_len -= len;
459 data += len;
460 written += len;
461 }
462 /* This is a bit nasty but we need that for the console to
463 * flush when there aren't any interrupts. We will clean
464 * things a bit later to limit that to synchronous path
465 * such as the kernel console and xmon/udbg
466 */
467 do
468 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000469 while(rc == OPAL_SUCCESS &&
470 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000471 }
472 spin_unlock_irqrestore(&opal_write_lock, flags);
473 return written;
474}
475
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530476static int opal_recover_mce(struct pt_regs *regs,
477 struct machine_check_event *evt)
478{
479 int recovered = 0;
480 uint64_t ea = get_mce_fault_addr(evt);
481
482 if (!(regs->msr & MSR_RI)) {
483 /* If MSR_RI isn't set, we cannot recover */
484 recovered = 0;
485 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
486 /* Platform corrected itself */
487 recovered = 1;
488 } else if (ea && !is_kernel_addr(ea)) {
489 /*
490 * Faulting address is not in kernel text. We should be fine.
491 * We need to find which process uses this address.
492 * For now, kill the task if we have received exception when
493 * in userspace.
494 *
495 * TODO: Queue up this address for hwpoisioning later.
496 */
497 if (user_mode(regs) && !is_global_init(current)) {
498 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
499 recovered = 1;
500 } else
501 recovered = 0;
502 } else if (user_mode(regs) && !is_global_init(current) &&
503 evt->severity == MCE_SEV_ERROR_SYNC) {
504 /*
505 * If we have received a synchronous error when in userspace
506 * kill the task.
507 */
508 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
509 recovered = 1;
510 }
511 return recovered;
512}
513
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000514int opal_machine_check(struct pt_regs *regs)
515{
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530516 struct machine_check_event evt;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000517
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530518 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
519 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000520
521 /* Print things out */
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530522 if (evt.version != MCE_V1) {
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000523 pr_err("Machine Check Exception, Unknown event version %d !\n",
524 evt.version);
525 return 0;
526 }
Mahesh Salgaonkarb5ff4212013-10-30 20:05:49 +0530527 machine_check_print_event_info(&evt);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000528
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530529 if (opal_recover_mce(regs, &evt))
530 return 1;
531 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000532}
533
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530534/* Early hmi handler called in real mode. */
535int opal_hmi_exception_early(struct pt_regs *regs)
536{
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530537 s64 rc;
538
539 /*
540 * call opal hmi handler. Pass paca address as token.
541 * The return value OPAL_SUCCESS is an indication that there is
542 * an HMI event generated waiting to pull by Linux.
543 */
544 rc = opal_handle_hmi();
545 if (rc == OPAL_SUCCESS) {
546 local_paca->hmi_event_available = 1;
547 return 1;
548 }
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530549 return 0;
550}
551
552/* HMI exception handler called in virtual mode during check_irq_replay. */
553int opal_handle_hmi_exception(struct pt_regs *regs)
554{
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530555 s64 rc;
556 __be64 evt = 0;
557
558 /*
559 * Check if HMI event is available.
560 * if Yes, then call opal_poll_events to pull opal messages and
561 * process them.
562 */
563 if (!local_paca->hmi_event_available)
564 return 0;
565
566 local_paca->hmi_event_available = 0;
567 rc = opal_poll_events(&evt);
568 if (rc == OPAL_SUCCESS && evt)
569 opal_do_notifier(be64_to_cpu(evt));
570
571 return 1;
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530572}
573
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530574static uint64_t find_recovery_address(uint64_t nip)
575{
576 int i;
577
578 for (i = 0; i < mc_recoverable_range_len; i++)
579 if ((nip >= mc_recoverable_range[i].start_addr) &&
580 (nip < mc_recoverable_range[i].end_addr))
581 return mc_recoverable_range[i].recover_addr;
582 return 0;
583}
584
585bool opal_mce_check_early_recovery(struct pt_regs *regs)
586{
587 uint64_t recover_addr = 0;
588
589 if (!opal.base || !opal.size)
590 goto out;
591
592 if ((regs->nip >= opal.base) &&
593 (regs->nip <= (opal.base + opal.size)))
594 recover_addr = find_recovery_address(regs->nip);
595
596 /*
597 * Setup regs->nip to rfi into fixup address.
598 */
599 if (recover_addr)
600 regs->nip = recover_addr;
601
602out:
603 return !!recover_addr;
604}
605
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000606static irqreturn_t opal_interrupt(int irq, void *data)
607{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000608 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000609
610 opal_handle_interrupt(virq_to_hw(irq), &events);
611
Anton Blanchard56b4c992014-04-22 15:01:24 +1000612 opal_do_notifier(be64_to_cpu(events));
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000613
614 return IRQ_HANDLED;
615}
616
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530617static int opal_sysfs_init(void)
618{
619 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
620 if (!opal_kobj) {
621 pr_warn("kobject_create_and_add opal failed\n");
622 return -ENOMEM;
623 }
624
625 return 0;
626}
627
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +1100628static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj,
629 struct bin_attribute *bin_attr,
630 char *buf, loff_t off, size_t count)
631{
632 return memory_read_from_buffer(buf, count, &off, bin_attr->private,
633 bin_attr->size);
634}
635
636static BIN_ATTR_RO(symbol_map, 0);
637
638static void opal_export_symmap(void)
639{
640 const __be64 *syms;
641 unsigned int size;
642 struct device_node *fw;
643 int rc;
644
645 fw = of_find_node_by_path("/ibm,opal/firmware");
646 if (!fw)
647 return;
648 syms = of_get_property(fw, "symbol-map", &size);
649 if (!syms || size != 2 * sizeof(__be64))
650 return;
651
652 /* Setup attributes */
653 bin_attr_symbol_map.private = __va(be64_to_cpu(syms[0]));
654 bin_attr_symbol_map.size = be64_to_cpu(syms[1]);
655
656 rc = sysfs_create_bin_file(opal_kobj, &bin_attr_symbol_map);
657 if (rc)
658 pr_warn("Error %d creating OPAL symbols file\n", rc);
659}
660
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530661static void __init opal_dump_region_init(void)
662{
663 void *addr;
664 uint64_t size;
665 int rc;
666
667 /* Register kernel log buffer */
668 addr = log_buf_addr_get();
Pranith Kumar6501ab5e32015-01-21 21:26:09 -0500669 if (addr == NULL)
670 return;
671
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530672 size = log_buf_len_get();
Pranith Kumar6501ab5e32015-01-21 21:26:09 -0500673 if (size == 0)
674 return;
675
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530676 rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
677 __pa(addr), size);
678 /* Don't warn if this is just an older OPAL that doesn't
679 * know about that call
680 */
681 if (rc && rc != OPAL_UNSUPPORTED)
682 pr_warn("DUMP: Failed to register kernel log buffer. "
683 "rc = %d\n", rc);
684}
Jeremy Kerr608b2862014-11-06 11:38:27 +0800685
686static void opal_ipmi_init(struct device_node *opal_node)
687{
688 struct device_node *np;
689
690 for_each_child_of_node(opal_node, np)
691 if (of_device_is_compatible(np, "ibm,opal-ipmi"))
692 of_platform_device_create(np, NULL, NULL);
693}
694
Neelesh Gupta47083452014-12-13 23:31:05 +0530695static void opal_i2c_create_devs(void)
696{
697 struct device_node *np;
698
699 for_each_compatible_node(np, NULL, "ibm,opal-i2c")
700 of_platform_device_create(np, NULL, NULL);
701}
702
Gavin Shanc1c3a522015-01-23 14:25:05 +1100703static void __init opal_irq_init(struct device_node *dn)
704{
705 const __be32 *irqs;
706 int i, irqlen;
707
708 /* Get interrupt property */
709 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
Gavin Shan31494cf2015-01-23 14:25:06 +1100710 opal_irq_count = irqs ? (irqlen / 4) : 0;
711 pr_debug("Found %d interrupts reserved for OPAL\n", opal_irq_count);
712 if (!opal_irq_count)
713 return;
Gavin Shanc1c3a522015-01-23 14:25:05 +1100714
715 /* Install interrupt handlers */
Gavin Shanc1c3a522015-01-23 14:25:05 +1100716 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
717 for (i = 0; irqs && i < opal_irq_count; i++, irqs++) {
718 unsigned int irq, virq;
719 int rc;
720
721 /* Get hardware and virtual IRQ */
722 irq = be32_to_cpup(irqs);
723 virq = irq_create_mapping(NULL, irq);
724 if (virq == NO_IRQ) {
725 pr_warn("Failed to map irq 0x%x\n", irq);
726 continue;
727 }
728
729 /* Install interrupt handler */
730 rc = request_irq(virq, opal_interrupt, 0, "opal", NULL);
731 if (rc) {
732 irq_dispose_mapping(virq);
733 pr_warn("Error %d requesting irq %d (0x%x)\n",
734 rc, virq, irq);
735 continue;
736 }
737
738 /* Cache IRQ */
739 opal_irqs[i] = virq;
740 }
741}
742
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100743static int kopald(void *unused)
744{
745 set_freezable();
746 do {
747 try_to_freeze();
748 opal_poll_events(NULL);
749 msleep_interruptible(opal_heartbeat);
750 } while (!kthread_should_stop());
751
752 return 0;
753}
754
755static void opal_init_heartbeat(void)
756{
757 /* Old firwmware, we assume the HVC heartbeat is sufficient */
758 if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
759 &opal_heartbeat) != 0)
760 opal_heartbeat = 0;
761
762 if (opal_heartbeat)
763 kthread_run(kopald, NULL, "kopald");
764}
765
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000766static int __init opal_init(void)
767{
768 struct device_node *np, *consoles;
Gavin Shanc1c3a522015-01-23 14:25:05 +1100769 int rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000770
771 opal_node = of_find_node_by_path("/ibm,opal");
772 if (!opal_node) {
Michael Ellerman08135132015-01-28 15:10:33 +1100773 pr_warn("Device node not found\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000774 return -ENODEV;
775 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000776
777 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000778 if (firmware_has_feature(FW_FEATURE_OPALv2))
779 consoles = of_find_node_by_path("/ibm,opal/consoles");
780 else
781 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000782 if (consoles) {
783 for_each_child_of_node(consoles, np) {
784 if (strcmp(np->name, "serial"))
785 continue;
786 of_platform_device_create(np, NULL, NULL);
787 }
788 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000789 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000790
Neelesh Gupta47083452014-12-13 23:31:05 +0530791 /* Create i2c platform devices */
792 opal_i2c_create_devs();
793
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100794 /* Setup a heatbeat thread if requested by OPAL */
795 opal_init_heartbeat();
796
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000797 /* Find all OPAL interrupts and request them */
Gavin Shanc1c3a522015-01-23 14:25:05 +1100798 opal_irq_init(opal_node);
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530799
800 /* Create "opal" kobject under /sys/firmware */
801 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530802 if (rc == 0) {
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +1100803 /* Export symbol map to userspace */
804 opal_export_symmap();
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530805 /* Setup dump region interface */
806 opal_dump_region_init();
Stewart Smith774fea12014-02-28 11:58:32 +1100807 /* Setup error log interface */
808 rc = opal_elog_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530809 /* Setup code update interface */
810 opal_flash_init();
Stewart Smithc7e64b92014-03-03 10:25:42 +1100811 /* Setup platform dump extract interface */
812 opal_platform_dump_init();
Neelesh Gupta4029cd62014-03-07 11:02:09 +0530813 /* Setup system parameters interface */
814 opal_sys_param_init();
Joel Stanleybfc36892014-04-01 14:28:19 +1030815 /* Setup message log interface. */
816 opal_msglog_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530817 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530818
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100819 /* Initialize OPAL IPMI backend */
Jeremy Kerr608b2862014-11-06 11:38:27 +0800820 opal_ipmi_init(opal_node);
821
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000822 return 0;
823}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000824machine_subsys_initcall(powernv, opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000825
826void opal_shutdown(void)
827{
828 unsigned int i;
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100829 long rc = OPAL_BUSY;
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000830
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100831 /* First free interrupts, which will also mask them */
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000832 for (i = 0; i < opal_irq_count; i++) {
833 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000834 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000835 opal_irqs[i] = 0;
836 }
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100837
838 /*
839 * Then sync with OPAL which ensure anything that can
840 * potentially write to our memory has completed such
841 * as an ongoing dump retrieval
842 */
843 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
844 rc = opal_sync_host_reboot();
845 if (rc == OPAL_BUSY)
846 opal_poll_events(NULL);
847 else
848 mdelay(10);
849 }
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530850
851 /* Unregister memory dump region */
852 opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000853}
Joel Stanleye28b05e2014-04-01 14:28:20 +1030854
855/* Export this so that test modules can use it */
856EXPORT_SYMBOL_GPL(opal_invalid_call);
Jeremy Kerr608b2862014-11-06 11:38:27 +0800857EXPORT_SYMBOL_GPL(opal_ipmi_send);
858EXPORT_SYMBOL_GPL(opal_ipmi_recv);
Anton Blanchard3441f042014-04-22 15:01:26 +1000859
860/* Convert a region of vmalloc memory to an opal sg list */
861struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
862 unsigned long vmalloc_size)
863{
864 struct opal_sg_list *sg, *first = NULL;
865 unsigned long i = 0;
866
867 sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
868 if (!sg)
869 goto nomem;
870
871 first = sg;
872
873 while (vmalloc_size > 0) {
874 uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
875 uint64_t length = min(vmalloc_size, PAGE_SIZE);
876
877 sg->entry[i].data = cpu_to_be64(data);
878 sg->entry[i].length = cpu_to_be64(length);
879 i++;
880
881 if (i >= SG_ENTRIES_PER_NODE) {
882 struct opal_sg_list *next;
883
884 next = kzalloc(PAGE_SIZE, GFP_KERNEL);
885 if (!next)
886 goto nomem;
887
888 sg->length = cpu_to_be64(
889 i * sizeof(struct opal_sg_entry) + 16);
890 i = 0;
891 sg->next = cpu_to_be64(__pa(next));
892 sg = next;
893 }
894
895 vmalloc_addr += length;
896 vmalloc_size -= length;
897 }
898
899 sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
900
901 return first;
902
903nomem:
904 pr_err("%s : Failed to allocate memory\n", __func__);
905 opal_free_sg_list(first);
906 return NULL;
907}
908
909void opal_free_sg_list(struct opal_sg_list *sg)
910{
911 while (sg) {
912 uint64_t next = be64_to_cpu(sg->next);
913
914 kfree(sg);
915
916 if (next)
917 sg = __va(next);
918 else
919 sg = NULL;
920 }
921}
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530922
923EXPORT_SYMBOL_GPL(opal_poll_events);
924EXPORT_SYMBOL_GPL(opal_rtc_read);
925EXPORT_SYMBOL_GPL(opal_rtc_write);
926EXPORT_SYMBOL_GPL(opal_tpo_read);
927EXPORT_SYMBOL_GPL(opal_tpo_write);
Neelesh Gupta47083452014-12-13 23:31:05 +0530928EXPORT_SYMBOL_GPL(opal_i2c_request);