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