]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - arch/powerpc/platforms/powernv/opal.c
UBUNTU: Ubuntu-5.3.0-29.31
[mirror_ubuntu-eoan-kernel.git] / arch / powerpc / platforms / powernv / opal.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
14a43e69
BH
2/*
3 * PowerNV OPAL high level interfaces
4 *
5 * Copyright 2011 IBM Corp.
14a43e69
BH
6 */
7
c8742f85 8#define pr_fmt(fmt) "opal: " fmt
14a43e69 9
c8742f85 10#include <linux/printk.h>
14a43e69
BH
11#include <linux/types.h>
12#include <linux/of.h>
26a2056e 13#include <linux/of_fdt.h>
14a43e69 14#include <linux/of_platform.h>
8f95faaa 15#include <linux/of_address.h>
a125e092 16#include <linux/interrupt.h>
1bc98de2 17#include <linux/notifier.h>
73ed148a 18#include <linux/slab.h>
b63a0ffe 19#include <linux/sched.h>
6f68b5e2 20#include <linux/kobject.h>
f7d98d18 21#include <linux/delay.h>
55672ecf 22#include <linux/memblock.h>
3bf57561
BH
23#include <linux/kthread.h>
24#include <linux/freezer.h>
b746e3e0
NP
25#include <linux/kmsg_dump.h>
26#include <linux/console.h>
27#include <linux/sched/debug.h>
b14726c5
ME
28
29#include <asm/machdep.h>
14a43e69
BH
30#include <asm/opal.h>
31#include <asm/firmware.h>
36df96f8 32#include <asm/mce.h>
8f95faaa 33#include <asm/imc-pmu.h>
6fcd6baa 34#include <asm/bug.h>
14a43e69
BH
35
36#include "powernv.h"
37
6f68b5e2
VH
38/* /sys/firmware/opal */
39struct kobject *opal_kobj;
40
14a43e69
BH
41struct opal {
42 u64 base;
43 u64 entry;
55672ecf 44 u64 size;
14a43e69
BH
45} opal;
46
55672ecf
MS
47struct mcheck_recoverable_range {
48 u64 start_addr;
49 u64 end_addr;
50 u64 recover_addr;
51};
52
53static struct mcheck_recoverable_range *mc_recoverable_range;
54static int mc_recoverable_range_len;
55
bfc36894 56struct device_node *opal_node;
14a43e69 57static DEFINE_SPINLOCK(opal_write_lock);
24366360 58static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
3bf57561 59static uint32_t opal_heartbeat;
a203658b 60static struct task_struct *kopald_tsk;
14a43e69 61
d3cbff1b 62void opal_configure_cores(void)
4926616c 63{
1c0eaf0f
BH
64 u64 reinit_flags = 0;
65
4926616c
BH
66 /* Do the actual re-init, This will clobber all FPRs, VRs, etc...
67 *
68 * It will preserve non volatile GPRs and HSPRG0/1. It will
69 * also restore HIDs and other SPRs to their original value
70 * but it might clobber a bunch.
71 */
72#ifdef __BIG_ENDIAN__
1c0eaf0f 73 reinit_flags |= OPAL_REINIT_CPUS_HILE_BE;
4926616c 74#else
1c0eaf0f 75 reinit_flags |= OPAL_REINIT_CPUS_HILE_LE;
4926616c 76#endif
d3cbff1b 77
1c0eaf0f
BH
78 /*
79 * POWER9 always support running hash:
80 * ie. Host hash supports hash guests
81 * Host radix supports hash/radix guests
82 */
a70b487b 83 if (early_cpu_has_feature(CPU_FTR_ARCH_300)) {
1c0eaf0f
BH
84 reinit_flags |= OPAL_REINIT_CPUS_MMU_HASH;
85 if (early_radix_enabled())
86 reinit_flags |= OPAL_REINIT_CPUS_MMU_RADIX;
87 }
88
89 opal_reinit_cpus(reinit_flags);
90
d3cbff1b
BH
91 /* Restore some bits */
92 if (cur_cpu_spec->cpu_restore)
93 cur_cpu_spec->cpu_restore();
4926616c
BH
94}
95
14a43e69
BH
96int __init early_init_dt_scan_opal(unsigned long node,
97 const char *uname, int depth, void *data)
98{
55672ecf 99 const void *basep, *entryp, *sizep;
9d0c4dfe 100 int basesz, entrysz, runtimesz;
14a43e69
BH
101
102 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
103 return 0;
104
105 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
106 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
55672ecf 107 sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
14a43e69 108
55672ecf 109 if (!basep || !entryp || !sizep)
14a43e69
BH
110 return 1;
111
112 opal.base = of_read_number(basep, basesz/4);
113 opal.entry = of_read_number(entryp, entrysz/4);
55672ecf 114 opal.size = of_read_number(sizep, runtimesz/4);
14a43e69 115
9d0c4dfe 116 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",
14a43e69 117 opal.base, basep, basesz);
9d0c4dfe 118 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",
14a43e69 119 opal.entry, entryp, entrysz);
9d0c4dfe 120 pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",
55672ecf 121 opal.size, sizep, runtimesz);
14a43e69 122
75b93da4 123 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
e4d54f71 124 powerpc_firmware_features |= FW_FEATURE_OPAL;
5138b314 125 pr_debug("OPAL detected !\n");
14a43e69 126 } else {
786842b6 127 panic("OPAL != V3 detected, no longer supported.\n");
14a43e69
BH
128 }
129
c4463b37
JK
130 return 1;
131}
132
55672ecf
MS
133int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
134 const char *uname, int depth, void *data)
135{
9d0c4dfe 136 int i, psize, size;
55672ecf
MS
137 const __be32 *prop;
138
139 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
140 return 0;
141
6e556b47 142 prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
55672ecf
MS
143
144 if (!prop)
145 return 1;
146
147 pr_debug("Found machine check recoverable ranges.\n");
148
6e556b47
MS
149 /*
150 * Calculate number of available entries.
151 *
152 * Each recoverable address range entry is (start address, len,
153 * recovery address), 2 cells each for start and recovery address,
154 * 1 cell for len, totalling 5 cells per entry.
155 */
156 mc_recoverable_range_len = psize / (sizeof(*prop) * 5);
157
158 /* Sanity check */
159 if (!mc_recoverable_range_len)
160 return 1;
161
162 /* Size required to hold all the entries. */
163 size = mc_recoverable_range_len *
164 sizeof(struct mcheck_recoverable_range);
165
55672ecf 166 /*
76b42e28 167 * Allocate a buffer to hold the MC recoverable ranges.
55672ecf 168 */
b63a07d6 169 mc_recoverable_range = memblock_alloc(size, __alignof__(u64));
8a7f97b9
MR
170 if (!mc_recoverable_range)
171 panic("%s: Failed to allocate %u bytes align=0x%lx\n",
172 __func__, size, __alignof__(u64));
55672ecf 173
6e556b47 174 for (i = 0; i < mc_recoverable_range_len; i++) {
55672ecf
MS
175 mc_recoverable_range[i].start_addr =
176 of_read_number(prop + (i * 5) + 0, 2);
177 mc_recoverable_range[i].end_addr =
178 mc_recoverable_range[i].start_addr +
179 of_read_number(prop + (i * 5) + 2, 1);
180 mc_recoverable_range[i].recover_addr =
181 of_read_number(prop + (i * 5) + 3, 2);
182
183 pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
184 mc_recoverable_range[i].start_addr,
185 mc_recoverable_range[i].end_addr,
186 mc_recoverable_range[i].recover_addr);
187 }
55672ecf
MS
188 return 1;
189}
190
c4463b37
JK
191static int __init opal_register_exception_handlers(void)
192{
29186097 193#ifdef __BIG_ENDIAN__
c4463b37
JK
194 u64 glue;
195
196 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
197 return -ENODEV;
198
28446de2
MS
199 /* Hookup some exception handlers except machine check. We use the
200 * fwnmi area at 0x7000 to provide the glue space to OPAL
ed79ba9e
BH
201 */
202 glue = 0x7000;
6507955c
MS
203
204 /*
1549c42d
SS
205 * Only ancient OPAL firmware requires this.
206 * Specifically, firmware from FW810.00 (released June 2014)
207 * through FW810.20 (Released October 2014).
6507955c 208 *
1549c42d
SS
209 * Check if we are running on newer (post Oct 2014) firmware that
210 * exports the OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to
211 * patch the HMI interrupt and we catch it directly in Linux.
6507955c 212 *
1549c42d
SS
213 * For older firmware (i.e < FW810.20), we fallback to old behavior and
214 * let OPAL patch the HMI vector and handle it inside OPAL firmware.
215 *
216 * For newer firmware we catch/handle the HMI directly in Linux.
6507955c
MS
217 */
218 if (!opal_check_token(OPAL_HANDLE_HMI)) {
08135139 219 pr_info("Old firmware detected, OPAL handles HMIs.\n");
6507955c
MS
220 opal_register_exception_handler(
221 OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
222 0, glue);
223 glue += 128;
224 }
225
1549c42d
SS
226 /*
227 * Only applicable to ancient firmware, all modern
228 * (post March 2015/skiboot 5.0) firmware will just return
229 * OPAL_UNSUPPORTED.
230 */
ed79ba9e 231 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
29186097 232#endif
ed79ba9e 233
c4463b37 234 return 0;
14a43e69 235}
b14726c5 236machine_early_initcall(powernv, opal_register_exception_handlers);
c4463b37 237
24366360
MS
238/*
239 * Opal message notifier based on message type. Allow subscribers to get
240 * notified for specific messgae type.
241 */
d7cf83fc 242int opal_message_notifier_register(enum opal_msg_type msg_type,
24366360
MS
243 struct notifier_block *nb)
244{
792f96e9 245 if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {
f2c2cbcc
JP
246 pr_warn("%s: Invalid arguments, msg_type:%d\n",
247 __func__, msg_type);
24366360
MS
248 return -EINVAL;
249 }
792f96e9 250
24366360
MS
251 return atomic_notifier_chain_register(
252 &opal_msg_notifier_head[msg_type], nb);
253}
594fcb9e 254EXPORT_SYMBOL_GPL(opal_message_notifier_register);
24366360 255
df60f576 256int opal_message_notifier_unregister(enum opal_msg_type msg_type,
b921e902
NG
257 struct notifier_block *nb)
258{
259 return atomic_notifier_chain_unregister(
260 &opal_msg_notifier_head[msg_type], nb);
261}
594fcb9e 262EXPORT_SYMBOL_GPL(opal_message_notifier_unregister);
b921e902 263
24366360
MS
264static void opal_message_do_notify(uint32_t msg_type, void *msg)
265{
266 /* notify subscribers */
267 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
268 msg_type, msg);
269}
270
271static void opal_handle_message(void)
272{
273 s64 ret;
274 /*
275 * TODO: pre-allocate a message buffer depending on opal-msg-size
276 * value in /proc/device-tree.
277 */
278 static struct opal_msg msg;
bb4398e1 279 u32 type;
24366360
MS
280
281 ret = opal_get_msg(__pa(&msg), sizeof(msg));
282 /* No opal message pending. */
283 if (ret == OPAL_RESOURCE)
284 return;
285
286 /* check for errors. */
287 if (ret) {
f2c2cbcc
JP
288 pr_warn("%s: Failed to retrieve opal message, err=%lld\n",
289 __func__, ret);
24366360
MS
290 return;
291 }
292
bb4398e1
AB
293 type = be32_to_cpu(msg.msg_type);
294
24366360 295 /* Sanity check */
792f96e9 296 if (type >= OPAL_MSG_TYPE_MAX) {
98da62b7 297 pr_warn_once("%s: Unknown message type: %u\n", __func__, type);
24366360
MS
298 return;
299 }
bb4398e1 300 opal_message_do_notify(type, (void *)&msg);
24366360
MS
301}
302
a295af24 303static irqreturn_t opal_message_notify(int irq, void *data)
24366360 304{
a295af24
AP
305 opal_handle_message();
306 return IRQ_HANDLED;
24366360
MS
307}
308
24366360
MS
309static int __init opal_message_init(void)
310{
a295af24 311 int ret, i, irq;
24366360
MS
312
313 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
314 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
315
a295af24
AP
316 irq = opal_event_request(ilog2(OPAL_EVENT_MSG_PENDING));
317 if (!irq) {
318 pr_err("%s: Can't register OPAL event irq (%d)\n",
319 __func__, irq);
320 return irq;
321 }
322
323 ret = request_irq(irq, opal_message_notify,
324 IRQ_TYPE_LEVEL_HIGH, "opal-msg", NULL);
24366360 325 if (ret) {
a295af24 326 pr_err("%s: Can't request OPAL event irq (%d)\n",
24366360
MS
327 __func__, ret);
328 return ret;
329 }
a295af24 330
24366360
MS
331 return 0;
332}
24366360 333
14a43e69
BH
334int opal_get_chars(uint32_t vtermno, char *buf, int count)
335{
4f89363b
BH
336 s64 rc;
337 __be64 evt, len;
14a43e69
BH
338
339 if (!opal.entry)
daea1175 340 return -ENODEV;
14a43e69 341 opal_poll_events(&evt);
4f89363b 342 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
14a43e69 343 return 0;
4f89363b 344 len = cpu_to_be64(count);
9d0c4dfe 345 rc = opal_console_read(vtermno, &len, buf);
14a43e69 346 if (rc == OPAL_SUCCESS)
4f89363b 347 return be64_to_cpu(len);
14a43e69
BH
348 return 0;
349}
350
17cc1dd4 351static int __opal_put_chars(uint32_t vtermno, const char *data, int total_len, bool atomic)
14a43e69 352{
17cc1dd4 353 unsigned long flags = 0 /* shut up gcc */;
b74d2807
NP
354 int written;
355 __be64 olen;
356 s64 rc;
14a43e69
BH
357
358 if (!opal.entry)
daea1175 359 return -ENODEV;
14a43e69 360
17cc1dd4
NP
361 if (atomic)
362 spin_lock_irqsave(&opal_write_lock, flags);
e4d54f71 363 rc = opal_console_write_buffer_space(vtermno, &olen);
b74d2807 364 if (rc || be64_to_cpu(olen) < total_len) {
e4d54f71
SS
365 /* Closed -> drop characters */
366 if (rc)
b74d2807
NP
367 written = total_len;
368 else
369 written = -EAGAIN;
370 goto out;
14a43e69
BH
371 }
372
b74d2807
NP
373 /* Should not get a partial write here because space is available. */
374 olen = cpu_to_be64(total_len);
375 rc = opal_console_write(vtermno, &olen, data);
376 if (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
95b861a7 377 if (rc == OPAL_BUSY_EVENT)
b74d2807 378 opal_poll_events(NULL);
b74d2807
NP
379 written = -EAGAIN;
380 goto out;
381 }
36d2dabc 382
b74d2807
NP
383 /* Closed or other error drop */
384 if (rc != OPAL_SUCCESS) {
385 written = opal_error_code(rc);
386 goto out;
387 }
36d2dabc 388
b74d2807
NP
389 written = be64_to_cpu(olen);
390 if (written < total_len) {
17cc1dd4
NP
391 if (atomic) {
392 /* Should not happen */
393 pr_warn("atomic console write returned partial "
394 "len=%d written=%d\n", total_len, written);
395 }
b74d2807
NP
396 if (!written)
397 written = -EAGAIN;
d2a2262e 398 }
b74d2807
NP
399
400out:
17cc1dd4
NP
401 if (atomic)
402 spin_unlock_irqrestore(&opal_write_lock, flags);
d2a2262e
NP
403
404 return written;
405}
406
17cc1dd4
NP
407int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
408{
409 return __opal_put_chars(vtermno, data, total_len, false);
410}
411
412/*
413 * opal_put_chars_atomic will not perform partial-writes. Data will be
414 * atomically written to the terminal or not at all. This is not strictly
415 * true at the moment because console space can race with OPAL's console
416 * writes.
417 */
418int opal_put_chars_atomic(uint32_t vtermno, const char *data, int total_len)
419{
420 return __opal_put_chars(vtermno, data, total_len, true);
421}
422
95b861a7 423static s64 __opal_flush_console(uint32_t vtermno)
d2a2262e
NP
424{
425 s64 rc;
426
427 if (!opal_check_token(OPAL_CONSOLE_FLUSH)) {
428 __be64 evt;
429
d2a2262e
NP
430 /*
431 * If OPAL_CONSOLE_FLUSH is not implemented in the firmware,
432 * the console can still be flushed by calling the polling
433 * function while it has OPAL_EVENT_CONSOLE_OUTPUT events.
434 */
95b861a7
NP
435 WARN_ONCE(1, "opal: OPAL_CONSOLE_FLUSH missing.\n");
436
437 opal_poll_events(&evt);
438 if (!(be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT))
439 return OPAL_SUCCESS;
440 return OPAL_BUSY;
d2a2262e 441
95b861a7
NP
442 } else {
443 rc = opal_console_flush(vtermno);
444 if (rc == OPAL_BUSY_EVENT) {
445 opal_poll_events(NULL);
446 rc = OPAL_BUSY;
447 }
448 return rc;
14a43e69 449 }
d2a2262e 450
95b861a7
NP
451}
452
453/*
454 * opal_flush_console spins until the console is flushed
455 */
456int opal_flush_console(uint32_t vtermno)
457{
458 for (;;) {
459 s64 rc = __opal_flush_console(vtermno);
460
461 if (rc == OPAL_BUSY || rc == OPAL_PARTIAL) {
462 mdelay(1);
463 continue;
464 }
465
466 return opal_error_code(rc);
467 }
468}
469
470/*
471 * opal_flush_chars is an hvc interface that sleeps until the console is
472 * flushed if wait, otherwise it will return -EBUSY if the console has data,
473 * -EAGAIN if it has data and some of it was flushed.
474 */
475int opal_flush_chars(uint32_t vtermno, bool wait)
476{
477 for (;;) {
478 s64 rc = __opal_flush_console(vtermno);
479
480 if (rc == OPAL_BUSY || rc == OPAL_PARTIAL) {
481 if (wait) {
482 msleep(OPAL_BUSY_DELAY_MS);
483 continue;
d2a2262e 484 }
95b861a7
NP
485 if (rc == OPAL_PARTIAL)
486 return -EAGAIN;
d2a2262e 487 }
d2a2262e 488
95b861a7
NP
489 return opal_error_code(rc);
490 }
14a43e69
BH
491}
492
b63a0ffe
MS
493static int opal_recover_mce(struct pt_regs *regs,
494 struct machine_check_event *evt)
495{
496 int recovered = 0;
b63a0ffe
MS
497
498 if (!(regs->msr & MSR_RI)) {
499 /* If MSR_RI isn't set, we cannot recover */
c74dd88e 500 pr_err("Machine check interrupt unrecoverable: MSR(RI=0)\n");
b63a0ffe
MS
501 recovered = 0;
502 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
503 /* Platform corrected itself */
504 recovered = 1;
1363875b
NP
505 } else if (evt->severity == MCE_SEV_FATAL) {
506 /* Fatal machine check */
507 pr_err("Machine check interrupt is fatal\n");
508 recovered = 0;
6fcd6baa
NP
509 }
510
cda6618d 511 if (!recovered && evt->sync_error) {
b63a0ffe 512 /*
6fcd6baa
NP
513 * Try to kill processes if we get a synchronous machine check
514 * (e.g., one caused by execution of this instruction). This
515 * will devolve into a panic if we try to kill init or are in
516 * an interrupt etc.
b63a0ffe
MS
517 *
518 * TODO: Queue up this address for hwpoisioning later.
6fcd6baa
NP
519 * TODO: This is not quite right for d-side machine
520 * checks ->nip is not necessarily the important
521 * address.
b63a0ffe 522 */
6fcd6baa
NP
523 if ((user_mode(regs))) {
524 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
525 recovered = 1;
526 } else if (die_will_crash()) {
527 /*
528 * die() would kill the kernel, so better to go via
529 * the platform reboot code that will log the
530 * machine check.
531 */
532 recovered = 0;
533 } else {
534 die("Machine check", regs, SIGBUS);
535 recovered = 1;
536 }
b63a0ffe 537 }
6fcd6baa 538
b63a0ffe
MS
539 return recovered;
540}
541
62dea077 542void __noreturn pnv_platform_error_reboot(struct pt_regs *regs, const char *msg)
b746e3e0 543{
35adacd6
NP
544 panic_flush_kmsg_start();
545
b746e3e0
NP
546 pr_emerg("Hardware platform error: %s\n", msg);
547 if (regs)
548 show_regs(regs);
549 smp_send_stop();
35adacd6
NP
550
551 panic_flush_kmsg_end();
b746e3e0
NP
552
553 /*
554 * Don't bother to shut things down because this will
555 * xstop the system.
556 */
557 if (opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR, msg)
558 == OPAL_UNSUPPORTED) {
559 pr_emerg("Reboot type %d not supported for %s\n",
560 OPAL_REBOOT_PLATFORM_ERROR, msg);
561 }
562
563 /*
564 * We reached here. There can be three possibilities:
565 * 1. We are running on a firmware level that do not support
566 * opal_cec_reboot2()
567 * 2. We are running on a firmware level that do not support
568 * OPAL_REBOOT_PLATFORM_ERROR reboot type.
569 * 3. We are running on FSP based system that does not need
570 * opal to trigger checkstop explicitly for error analysis.
571 * The FSP PRD component would have already got notified
572 * about this error through other channels.
5ee573e8
BS
573 * 4. We are running on a newer skiboot that by default does
574 * not cause a checkstop, drops us back to the kernel to
575 * extract context and state at the time of the error.
b746e3e0
NP
576 */
577
5ee573e8 578 panic(msg);
b746e3e0
NP
579}
580
ed79ba9e
BH
581int opal_machine_check(struct pt_regs *regs)
582{
36df96f8 583 struct machine_check_event evt;
ed79ba9e 584
36df96f8
MS
585 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
586 return 0;
ed79ba9e
BH
587
588 /* Print things out */
36df96f8 589 if (evt.version != MCE_V1) {
ed79ba9e
BH
590 pr_err("Machine Check Exception, Unknown event version %d !\n",
591 evt.version);
592 return 0;
593 }
c0577201 594 machine_check_print_event_info(&evt, user_mode(regs), false);
ed79ba9e 595
b63a0ffe
MS
596 if (opal_recover_mce(regs, &evt))
597 return 1;
e784b649 598
b746e3e0 599 pnv_platform_error_reboot(regs, "Unrecoverable Machine Check exception");
ed79ba9e
BH
600}
601
0869b6fd
MS
602/* Early hmi handler called in real mode. */
603int opal_hmi_exception_early(struct pt_regs *regs)
604{
0ef95b41
MS
605 s64 rc;
606
607 /*
608 * call opal hmi handler. Pass paca address as token.
609 * The return value OPAL_SUCCESS is an indication that there is
610 * an HMI event generated waiting to pull by Linux.
611 */
612 rc = opal_handle_hmi();
613 if (rc == OPAL_SUCCESS) {
614 local_paca->hmi_event_available = 1;
615 return 1;
616 }
0869b6fd
MS
617 return 0;
618}
619
de269129
MS
620int opal_hmi_exception_early2(struct pt_regs *regs)
621{
622 s64 rc;
623 __be64 out_flags;
624
625 /*
626 * call opal hmi handler.
627 * Check 64-bit flag mask to find out if an event was generated,
628 * and whether TB is still valid or not etc.
629 */
630 rc = opal_handle_hmi2(&out_flags);
631 if (rc != OPAL_SUCCESS)
632 return 0;
633
634 if (be64_to_cpu(out_flags) & OPAL_HMI_FLAGS_NEW_EVENT)
635 local_paca->hmi_event_available = 1;
636 if (be64_to_cpu(out_flags) & OPAL_HMI_FLAGS_TOD_TB_FAIL)
637 tb_invalid = true;
638 return 1;
639}
640
0869b6fd
MS
641/* HMI exception handler called in virtual mode during check_irq_replay. */
642int opal_handle_hmi_exception(struct pt_regs *regs)
643{
0ef95b41
MS
644 /*
645 * Check if HMI event is available.
56c0b48b 646 * if Yes, then wake kopald to process them.
0ef95b41
MS
647 */
648 if (!local_paca->hmi_event_available)
649 return 0;
650
651 local_paca->hmi_event_available = 0;
56c0b48b 652 opal_wake_poller();
0ef95b41
MS
653
654 return 1;
0869b6fd
MS
655}
656
55672ecf
MS
657static uint64_t find_recovery_address(uint64_t nip)
658{
659 int i;
660
661 for (i = 0; i < mc_recoverable_range_len; i++)
662 if ((nip >= mc_recoverable_range[i].start_addr) &&
663 (nip < mc_recoverable_range[i].end_addr))
664 return mc_recoverable_range[i].recover_addr;
665 return 0;
666}
667
668bool opal_mce_check_early_recovery(struct pt_regs *regs)
669{
670 uint64_t recover_addr = 0;
671
672 if (!opal.base || !opal.size)
673 goto out;
674
675 if ((regs->nip >= opal.base) &&
dc3799bb 676 (regs->nip < (opal.base + opal.size)))
55672ecf
MS
677 recover_addr = find_recovery_address(regs->nip);
678
679 /*
680 * Setup regs->nip to rfi into fixup address.
681 */
682 if (recover_addr)
683 regs->nip = recover_addr;
684
685out:
686 return !!recover_addr;
687}
688
6f68b5e2
VH
689static int opal_sysfs_init(void)
690{
691 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
692 if (!opal_kobj) {
693 pr_warn("kobject_create_and_add opal failed\n");
694 return -ENOMEM;
695 }
696
697 return 0;
698}
699
c8742f85
BH
700static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj,
701 struct bin_attribute *bin_attr,
702 char *buf, loff_t off, size_t count)
703{
704 return memory_read_from_buffer(buf, count, &off, bin_attr->private,
705 bin_attr->size);
706}
707
ea66b673
AD
708static struct bin_attribute symbol_map_attr = {
709 .attr = {.name = "symbol_map", .mode = 0400},
710 .read = symbol_map_read
711};
c8742f85
BH
712
713static void opal_export_symmap(void)
714{
715 const __be64 *syms;
716 unsigned int size;
717 struct device_node *fw;
718 int rc;
719
720 fw = of_find_node_by_path("/ibm,opal/firmware");
721 if (!fw)
722 return;
723 syms = of_get_property(fw, "symbol-map", &size);
724 if (!syms || size != 2 * sizeof(__be64))
725 return;
726
727 /* Setup attributes */
ea66b673
AD
728 symbol_map_attr.private = __va(be64_to_cpu(syms[0]));
729 symbol_map_attr.size = be64_to_cpu(syms[1]);
c8742f85 730
ea66b673 731 rc = sysfs_create_bin_file(opal_kobj, &symbol_map_attr);
c8742f85
BH
732 if (rc)
733 pr_warn("Error %d creating OPAL symbols file\n", rc);
734}
735
11fe909d
MB
736static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
737 struct bin_attribute *bin_attr, char *buf,
738 loff_t off, size_t count)
739{
740 return memory_read_from_buffer(buf, count, &off, bin_attr->private,
741 bin_attr->size);
742}
743
744/*
745 * opal_export_attrs: creates a sysfs node for each property listed in
746 * the device-tree under /ibm,opal/firmware/exports/
747 * All new sysfs nodes are created under /opal/exports/.
748 * This allows for reserved memory regions (e.g. HDAT) to be read.
749 * The new sysfs nodes are only readable by root.
750 */
751static void opal_export_attrs(void)
752{
753 struct bin_attribute *attr;
754 struct device_node *np;
755 struct property *prop;
756 struct kobject *kobj;
757 u64 vals[2];
758 int rc;
759
760 np = of_find_node_by_path("/ibm,opal/firmware/exports");
761 if (!np)
762 return;
763
764 /* Create new 'exports' directory - /sys/firmware/opal/exports */
765 kobj = kobject_create_and_add("exports", opal_kobj);
766 if (!kobj) {
767 pr_warn("kobject_create_and_add() of exports failed\n");
768 return;
769 }
770
771 for_each_property_of_node(np, prop) {
772 if (!strcmp(prop->name, "name") || !strcmp(prop->name, "phandle"))
773 continue;
774
775 if (of_property_read_u64_array(np, prop->name, &vals[0], 2))
776 continue;
777
83c49190 778 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
11fe909d
MB
779
780 if (attr == NULL) {
781 pr_warn("Failed kmalloc for bin_attribute!");
782 continue;
783 }
784
83c49190 785 sysfs_bin_attr_init(attr);
11fe909d
MB
786 attr->attr.name = kstrdup(prop->name, GFP_KERNEL);
787 attr->attr.mode = 0400;
788 attr->read = export_attr_read;
789 attr->private = __va(vals[0]);
790 attr->size = vals[1];
791
792 if (attr->attr.name == NULL) {
793 pr_warn("Failed kstrdup for bin_attribute attr.name");
794 kfree(attr);
795 continue;
796 }
797
798 rc = sysfs_create_bin_file(kobj, attr);
799 if (rc) {
800 pr_warn("Error %d creating OPAL sysfs exports/%s file\n",
801 rc, prop->name);
802 kfree(attr->attr.name);
803 kfree(attr);
804 }
805 }
806
807 of_node_put(np);
808}
809
b09c2ec4
VH
810static void __init opal_dump_region_init(void)
811{
812 void *addr;
813 uint64_t size;
814 int rc;
815
b962f5a4
SS
816 if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))
817 return;
818
b09c2ec4
VH
819 /* Register kernel log buffer */
820 addr = log_buf_addr_get();
6501ab5e
PK
821 if (addr == NULL)
822 return;
823
b09c2ec4 824 size = log_buf_len_get();
6501ab5e
PK
825 if (size == 0)
826 return;
827
b09c2ec4
VH
828 rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
829 __pa(addr), size);
830 /* Don't warn if this is just an older OPAL that doesn't
831 * know about that call
832 */
833 if (rc && rc != OPAL_UNSUPPORTED)
834 pr_warn("DUMP: Failed to register kernel log buffer. "
835 "rc = %d\n", rc);
836}
608b286d 837
9e4f51bd 838static void opal_pdev_init(const char *compatible)
ed59190e
CB
839{
840 struct device_node *np;
841
9e4f51bd 842 for_each_compatible_node(np, NULL, compatible)
47083450
NG
843 of_platform_device_create(np, NULL, NULL);
844}
845
8f95faaa
MS
846static void __init opal_imc_init_dev(void)
847{
848 struct device_node *np;
849
850 np = of_find_compatible_node(NULL, NULL, IMC_DTB_COMPAT);
851 if (np)
852 of_platform_device_create(np, NULL, NULL);
853}
854
3bf57561
BH
855static int kopald(void *unused)
856{
a203658b 857 unsigned long timeout = msecs_to_jiffies(opal_heartbeat) + 1;
9f0fd049 858
3bf57561
BH
859 set_freezable();
860 do {
861 try_to_freeze();
56c0b48b
NP
862
863 opal_handle_events();
864
865 set_current_state(TASK_INTERRUPTIBLE);
866 if (opal_have_pending_events())
867 __set_current_state(TASK_RUNNING);
868 else
869 schedule_timeout(timeout);
870
3bf57561
BH
871 } while (!kthread_should_stop());
872
873 return 0;
874}
875
a203658b
BH
876void opal_wake_poller(void)
877{
878 if (kopald_tsk)
879 wake_up_process(kopald_tsk);
880}
881
3bf57561
BH
882static void opal_init_heartbeat(void)
883{
884 /* Old firwmware, we assume the HVC heartbeat is sufficient */
885 if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
886 &opal_heartbeat) != 0)
887 opal_heartbeat = 0;
888
889 if (opal_heartbeat)
a203658b 890 kopald_tsk = kthread_run(kopald, NULL, "kopald");
3bf57561
BH
891}
892
14a43e69
BH
893static int __init opal_init(void)
894{
c159b596 895 struct device_node *np, *consoles, *leds;
c1c3a526 896 int rc;
14a43e69
BH
897
898 opal_node = of_find_node_by_path("/ibm,opal");
899 if (!opal_node) {
08135139 900 pr_warn("Device node not found\n");
14a43e69
BH
901 return -ENODEV;
902 }
2db29d28
BH
903
904 /* Register OPAL consoles if any ports */
7261aafc 905 consoles = of_find_node_by_path("/ibm,opal/consoles");
2db29d28
BH
906 if (consoles) {
907 for_each_child_of_node(consoles, np) {
2c8e65b5 908 if (!of_node_name_eq(np, "serial"))
2db29d28
BH
909 continue;
910 of_platform_device_create(np, NULL, NULL);
911 }
912 of_node_put(consoles);
14a43e69 913 }
a125e092 914
96e023e7
AP
915 /* Initialise OPAL messaging system */
916 opal_message_init();
917
918 /* Initialise OPAL asynchronous completion interface */
919 opal_async_comp_init();
920
921 /* Initialise OPAL sensor interface */
922 opal_sensor_init();
923
924 /* Initialise OPAL hypervisor maintainence interrupt handling */
925 opal_hmi_handler_init();
926
47083450 927 /* Create i2c platform devices */
9e4f51bd 928 opal_pdev_init("ibm,opal-i2c");
47083450 929
3013e173
OH
930 /* Handle non-volatile memory devices */
931 opal_pdev_init("pmem-region");
932
3bf57561
BH
933 /* Setup a heatbeat thread if requested by OPAL */
934 opal_init_heartbeat();
935
8f95faaa
MS
936 /* Detect In-Memory Collection counters and create devices*/
937 opal_imc_init_dev();
938
c159b596
VH
939 /* Create leds platform devices */
940 leds = of_find_node_by_path("/ibm,opal/leds");
941 if (leds) {
942 of_platform_device_create(leds, "opal_leds", NULL);
943 of_node_put(leds);
944 }
945
9b4fffa1
AD
946 /* Initialise OPAL message log interface */
947 opal_msglog_init();
948
6f68b5e2
VH
949 /* Create "opal" kobject under /sys/firmware */
950 rc = opal_sysfs_init();
50bd6153 951 if (rc == 0) {
c8742f85
BH
952 /* Export symbol map to userspace */
953 opal_export_symmap();
b09c2ec4
VH
954 /* Setup dump region interface */
955 opal_dump_region_init();
774fea1a
SS
956 /* Setup error log interface */
957 rc = opal_elog_init();
50bd6153 958 /* Setup code update interface */
ed59190e 959 opal_flash_update_init();
c7e64b9c
SS
960 /* Setup platform dump extract interface */
961 opal_platform_dump_init();
4029cd66
NG
962 /* Setup system parameters interface */
963 opal_sys_param_init();
9b4fffa1
AD
964 /* Setup message log sysfs interface. */
965 opal_msglog_sysfs_init();
50bd6153 966 }
6f68b5e2 967
11fe909d
MB
968 /* Export all properties */
969 opal_export_attrs();
970
0d7cd855 971 /* Initialize platform devices: IPMI backend, PRD & flash interface */
9e4f51bd
JM
972 opal_pdev_init("ibm,opal-ipmi");
973 opal_pdev_init("ibm,opal-flash");
974 opal_pdev_init("ibm,opal-prd");
ed59190e 975
43a1dd9b 976 /* Initialise platform device: oppanel interface */
9e4f51bd 977 opal_pdev_init("ibm,opal-oppanel");
43a1dd9b 978
affddff6
RC
979 /* Initialise OPAL kmsg dumper for flushing console on panic */
980 opal_kmsg_init();
981
cb8b340d
SB
982 /* Initialise OPAL powercap interface */
983 opal_powercap_init();
984
8e84b2d1
SB
985 /* Initialise OPAL Power-Shifting-Ratio interface */
986 opal_psr_init();
987
bf957155
SB
988 /* Initialise OPAL sensor groups */
989 opal_sensor_groups_init();
990
08fb726d
MS
991 /* Initialise OPAL Power control interface */
992 opal_power_control_init();
993
14a43e69
BH
994 return 0;
995}
b14726c5 996machine_subsys_initcall(powernv, opal_init);
73ed148a
BH
997
998void opal_shutdown(void)
999{
f7d98d18 1000 long rc = OPAL_BUSY;
73ed148a 1001
9f0fd049 1002 opal_event_shutdown();
f7d98d18
VH
1003
1004 /*
1005 * Then sync with OPAL which ensure anything that can
1006 * potentially write to our memory has completed such
1007 * as an ongoing dump retrieval
1008 */
1009 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
1010 rc = opal_sync_host_reboot();
1011 if (rc == OPAL_BUSY)
1012 opal_poll_events(NULL);
1013 else
1014 mdelay(10);
1015 }
b09c2ec4
VH
1016
1017 /* Unregister memory dump region */
b962f5a4
SS
1018 if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
1019 opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
73ed148a 1020}
e28b05e7
JS
1021
1022/* Export this so that test modules can use it */
1023EXPORT_SYMBOL_GPL(opal_invalid_call);
594fcb9e
JK
1024EXPORT_SYMBOL_GPL(opal_xscom_read);
1025EXPORT_SYMBOL_GPL(opal_xscom_write);
608b286d
JK
1026EXPORT_SYMBOL_GPL(opal_ipmi_send);
1027EXPORT_SYMBOL_GPL(opal_ipmi_recv);
ed59190e
CB
1028EXPORT_SYMBOL_GPL(opal_flash_read);
1029EXPORT_SYMBOL_GPL(opal_flash_write);
1030EXPORT_SYMBOL_GPL(opal_flash_erase);
0d7cd855 1031EXPORT_SYMBOL_GPL(opal_prd_msg);
6e708000 1032EXPORT_SYMBOL_GPL(opal_check_token);
3441f04b
AB
1033
1034/* Convert a region of vmalloc memory to an opal sg list */
1035struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
1036 unsigned long vmalloc_size)
1037{
1038 struct opal_sg_list *sg, *first = NULL;
1039 unsigned long i = 0;
1040
1041 sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
1042 if (!sg)
1043 goto nomem;
1044
1045 first = sg;
1046
1047 while (vmalloc_size > 0) {
1048 uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
1049 uint64_t length = min(vmalloc_size, PAGE_SIZE);
1050
1051 sg->entry[i].data = cpu_to_be64(data);
1052 sg->entry[i].length = cpu_to_be64(length);
1053 i++;
1054
1055 if (i >= SG_ENTRIES_PER_NODE) {
1056 struct opal_sg_list *next;
1057
1058 next = kzalloc(PAGE_SIZE, GFP_KERNEL);
1059 if (!next)
1060 goto nomem;
1061
1062 sg->length = cpu_to_be64(
1063 i * sizeof(struct opal_sg_entry) + 16);
1064 i = 0;
1065 sg->next = cpu_to_be64(__pa(next));
1066 sg = next;
1067 }
1068
1069 vmalloc_addr += length;
1070 vmalloc_size -= length;
1071 }
1072
1073 sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
1074
1075 return first;
1076
1077nomem:
1078 pr_err("%s : Failed to allocate memory\n", __func__);
1079 opal_free_sg_list(first);
1080 return NULL;
1081}
1082
1083void opal_free_sg_list(struct opal_sg_list *sg)
1084{
1085 while (sg) {
1086 uint64_t next = be64_to_cpu(sg->next);
1087
1088 kfree(sg);
1089
1090 if (next)
1091 sg = __va(next);
1092 else
1093 sg = NULL;
1094 }
1095}
16b1d26e 1096
e3c5c2e0
CLG
1097int opal_error_code(int rc)
1098{
1099 switch (rc) {
1100 case OPAL_SUCCESS: return 0;
1101
1102 case OPAL_PARAMETER: return -EINVAL;
1103 case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
77adbd22 1104 case OPAL_BUSY:
e3c5c2e0
CLG
1105 case OPAL_BUSY_EVENT: return -EBUSY;
1106 case OPAL_NO_MEM: return -ENOMEM;
14aae78f 1107 case OPAL_PERMISSION: return -EPERM;
e3c5c2e0
CLG
1108
1109 case OPAL_UNSUPPORTED: return -EIO;
1110 case OPAL_HARDWARE: return -EIO;
1111 case OPAL_INTERNAL_ERROR: return -EIO;
cb8b340d 1112 case OPAL_TIMEOUT: return -ETIMEDOUT;
e3c5c2e0
CLG
1113 default:
1114 pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
1115 return -EIO;
1116 }
1117}
1118
1d0761d2
AP
1119void powernv_set_nmmu_ptcr(unsigned long ptcr)
1120{
1121 int rc;
1122
1123 if (firmware_has_feature(FW_FEATURE_OPAL)) {
1124 rc = opal_nmmu_set_ptcr(-1UL, ptcr);
1125 if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
1126 pr_warn("%s: Unable to set nest mmu ptcr\n", __func__);
1127 }
1128}
1129
16b1d26e
NG
1130EXPORT_SYMBOL_GPL(opal_poll_events);
1131EXPORT_SYMBOL_GPL(opal_rtc_read);
1132EXPORT_SYMBOL_GPL(opal_rtc_write);
1133EXPORT_SYMBOL_GPL(opal_tpo_read);
1134EXPORT_SYMBOL_GPL(opal_tpo_write);
47083450 1135EXPORT_SYMBOL_GPL(opal_i2c_request);
c159b596
VH
1136/* Export these symbols for PowerNV LED class driver */
1137EXPORT_SYMBOL_GPL(opal_leds_get_ind);
1138EXPORT_SYMBOL_GPL(opal_leds_set_ind);
43a1dd9b
SJS
1139/* Export this symbol for PowerNV Operator Panel class driver */
1140EXPORT_SYMBOL_GPL(opal_write_oppanel_async);
ffe6d810
PM
1141/* Export this for KVM */
1142EXPORT_SYMBOL_GPL(opal_int_set_mfrr);
5af50993 1143EXPORT_SYMBOL_GPL(opal_int_eoi);
77adbd22 1144EXPORT_SYMBOL_GPL(opal_error_code);
656ecc16
HM
1145/* Export the below symbol for NX compression */
1146EXPORT_SYMBOL(opal_nx_coproc_init);