powerpc/powernv: Reorder OPAL subsystem initialisation
[linux-2.6-block.git] / arch / powerpc / platforms / powernv / opal.c
CommitLineData
14a43e69
BH
1/*
2 * PowerNV OPAL high level interfaces
3 *
4 * Copyright 2011 IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
c8742f85 12#define pr_fmt(fmt) "opal: " fmt
14a43e69 13
c8742f85 14#include <linux/printk.h>
14a43e69
BH
15#include <linux/types.h>
16#include <linux/of.h>
26a2056e 17#include <linux/of_fdt.h>
14a43e69 18#include <linux/of_platform.h>
a125e092 19#include <linux/interrupt.h>
1bc98de2 20#include <linux/notifier.h>
73ed148a 21#include <linux/slab.h>
b63a0ffe 22#include <linux/sched.h>
6f68b5e2 23#include <linux/kobject.h>
f7d98d18 24#include <linux/delay.h>
55672ecf 25#include <linux/memblock.h>
3bf57561
BH
26#include <linux/kthread.h>
27#include <linux/freezer.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>
14a43e69
BH
33
34#include "powernv.h"
35
6f68b5e2
VH
36/* /sys/firmware/opal */
37struct kobject *opal_kobj;
38
14a43e69
BH
39struct opal {
40 u64 base;
41 u64 entry;
55672ecf 42 u64 size;
14a43e69
BH
43} opal;
44
55672ecf
MS
45struct 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
bfc36894 54struct device_node *opal_node;
14a43e69 55static DEFINE_SPINLOCK(opal_write_lock);
73ed148a
BH
56static unsigned int *opal_irqs;
57static unsigned int opal_irq_count;
1bc98de2 58static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
24366360 59static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
1bc98de2
GS
60static DEFINE_SPINLOCK(opal_notifier_lock);
61static uint64_t last_notified_mask = 0x0ul;
62static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
3bf57561 63static uint32_t opal_heartbeat;
14a43e69 64
4926616c
BH
65static 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
14a43e69
BH
80int __init early_init_dt_scan_opal(unsigned long node,
81 const char *uname, int depth, void *data)
82{
55672ecf 83 const void *basep, *entryp, *sizep;
9d0c4dfe 84 int basesz, entrysz, runtimesz;
14a43e69
BH
85
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);
55672ecf 91 sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
14a43e69 92
55672ecf 93 if (!basep || !entryp || !sizep)
14a43e69
BH
94 return 1;
95
96 opal.base = of_read_number(basep, basesz/4);
97 opal.entry = of_read_number(entryp, entrysz/4);
55672ecf 98 opal.size = of_read_number(sizep, runtimesz/4);
14a43e69 99
9d0c4dfe 100 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",
14a43e69 101 opal.base, basep, basesz);
9d0c4dfe 102 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",
14a43e69 103 opal.entry, entryp, entrysz);
9d0c4dfe 104 pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",
55672ecf 105 opal.size, sizep, runtimesz);
14a43e69
BH
106
107 powerpc_firmware_features |= FW_FEATURE_OPAL;
75b93da4
BH
108 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
109 powerpc_firmware_features |= FW_FEATURE_OPALv2;
110 powerpc_firmware_features |= FW_FEATURE_OPALv3;
9a4f5cd0 111 pr_info("OPAL V3 detected !\n");
75b93da4 112 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
14a43e69 113 powerpc_firmware_features |= FW_FEATURE_OPALv2;
9a4f5cd0 114 pr_info("OPAL V2 detected !\n");
14a43e69 115 } else {
9a4f5cd0 116 pr_info("OPAL V1 detected !\n");
14a43e69
BH
117 }
118
4926616c
BH
119 /* 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
c4463b37
JK
126 return 1;
127}
128
55672ecf
MS
129int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
130 const char *uname, int depth, void *data)
131{
9d0c4dfe 132 int i, psize, size;
55672ecf
MS
133 const __be32 *prop;
134
135 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
136 return 0;
137
6e556b47 138 prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
55672ecf
MS
139
140 if (!prop)
141 return 1;
142
143 pr_debug("Found machine check recoverable ranges.\n");
144
6e556b47
MS
145 /*
146 * 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
55672ecf
MS
162 /*
163 * 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
6e556b47 171 for (i = 0; i < mc_recoverable_range_len; i++) {
55672ecf
MS
172 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 }
55672ecf
MS
185 return 1;
186}
187
c4463b37
JK
188static int __init opal_register_exception_handlers(void)
189{
29186097 190#ifdef __BIG_ENDIAN__
c4463b37
JK
191 u64 glue;
192
193 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
194 return -ENODEV;
195
28446de2
MS
196 /* Hookup some exception handlers except machine check. We use the
197 * fwnmi area at 0x7000 to provide the glue space to OPAL
ed79ba9e
BH
198 */
199 glue = 0x7000;
6507955c
MS
200
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)) {
08135139 214 pr_info("Old firmware detected, OPAL handles HMIs.\n");
6507955c
MS
215 opal_register_exception_handler(
216 OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
217 0, glue);
218 glue += 128;
219 }
220
ed79ba9e 221 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
29186097 222#endif
ed79ba9e 223
c4463b37 224 return 0;
14a43e69 225}
b14726c5 226machine_early_initcall(powernv, opal_register_exception_handlers);
c4463b37 227
1bc98de2
GS
228int 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}
798af00c
BH
239EXPORT_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);
1bc98de2
GS
253
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;
56b4c993 289 __be64 evt = 0;
1bc98de2
GS
290
291 atomic_set(&opal_notifier_hold, 0);
292
293 /* Process pending events */
294 rc = opal_poll_events(&evt);
295 if (rc == OPAL_SUCCESS && evt)
56b4c993 296 opal_do_notifier(be64_to_cpu(evt));
1bc98de2
GS
297}
298
299void opal_notifier_disable(void)
300{
301 atomic_set(&opal_notifier_hold, 1);
302}
303
24366360
MS
304/*
305 * Opal message notifier based on message type. Allow subscribers to get
306 * notified for specific messgae type.
307 */
d7cf83fc 308int opal_message_notifier_register(enum opal_msg_type msg_type,
24366360
MS
309 struct notifier_block *nb)
310{
792f96e9
NG
311 if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {
312 pr_warning("%s: Invalid arguments, msg_type:%d\n",
24366360
MS
313 __func__, msg_type);
314 return -EINVAL;
315 }
792f96e9 316
24366360
MS
317 return atomic_notifier_chain_register(
318 &opal_msg_notifier_head[msg_type], nb);
319}
320
df60f576 321int opal_message_notifier_unregister(enum opal_msg_type msg_type,
b921e902
NG
322 struct notifier_block *nb)
323{
324 return atomic_notifier_chain_unregister(
325 &opal_msg_notifier_head[msg_type], nb);
326}
327
24366360
MS
328static void opal_message_do_notify(uint32_t msg_type, void *msg)
329{
330 /* notify subscribers */
331 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
332 msg_type, msg);
333}
334
335static void opal_handle_message(void)
336{
337 s64 ret;
338 /*
339 * TODO: pre-allocate a message buffer depending on opal-msg-size
340 * value in /proc/device-tree.
341 */
342 static struct opal_msg msg;
bb4398e1 343 u32 type;
24366360
MS
344
345 ret = opal_get_msg(__pa(&msg), sizeof(msg));
346 /* No opal message pending. */
347 if (ret == OPAL_RESOURCE)
348 return;
349
350 /* check for errors. */
351 if (ret) {
1a84db56 352 pr_warning("%s: Failed to retrieve opal message, err=%lld\n",
24366360
MS
353 __func__, ret);
354 return;
355 }
356
bb4398e1
AB
357 type = be32_to_cpu(msg.msg_type);
358
24366360 359 /* Sanity check */
792f96e9 360 if (type >= OPAL_MSG_TYPE_MAX) {
bb4398e1 361 pr_warning("%s: Unknown message type: %u\n", __func__, type);
24366360
MS
362 return;
363 }
bb4398e1 364 opal_message_do_notify(type, (void *)&msg);
24366360
MS
365}
366
367static int opal_message_notify(struct notifier_block *nb,
368 unsigned long events, void *change)
369{
370 if (events & OPAL_EVENT_MSG_PENDING)
371 opal_handle_message();
372 return 0;
373}
374
375static struct notifier_block opal_message_nb = {
376 .notifier_call = opal_message_notify,
377 .next = NULL,
378 .priority = 0,
379};
380
381static int __init opal_message_init(void)
382{
383 int ret, i;
384
385 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
386 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
387
388 ret = opal_notifier_register(&opal_message_nb);
389 if (ret) {
390 pr_err("%s: Can't register OPAL event notifier (%d)\n",
391 __func__, ret);
392 return ret;
393 }
394 return 0;
395}
24366360 396
14a43e69
BH
397int opal_get_chars(uint32_t vtermno, char *buf, int count)
398{
4f89363b
BH
399 s64 rc;
400 __be64 evt, len;
14a43e69
BH
401
402 if (!opal.entry)
daea1175 403 return -ENODEV;
14a43e69 404 opal_poll_events(&evt);
4f89363b 405 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
14a43e69 406 return 0;
4f89363b 407 len = cpu_to_be64(count);
9d0c4dfe 408 rc = opal_console_read(vtermno, &len, buf);
14a43e69 409 if (rc == OPAL_SUCCESS)
4f89363b 410 return be64_to_cpu(len);
14a43e69
BH
411 return 0;
412}
413
414int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
415{
416 int written = 0;
4f89363b 417 __be64 olen;
daea1175 418 s64 len, rc;
14a43e69 419 unsigned long flags;
4f89363b 420 __be64 evt;
14a43e69
BH
421
422 if (!opal.entry)
daea1175 423 return -ENODEV;
14a43e69
BH
424
425 /* We want put_chars to be atomic to avoid mangling of hvsi
426 * packets. To do that, we first test for room and return
daea1175
BH
427 * -EAGAIN if there isn't enough.
428 *
429 * Unfortunately, opal_console_write_buffer_space() doesn't
430 * appear to work on opal v1, so we just assume there is
431 * enough room and be done with it
14a43e69
BH
432 */
433 spin_lock_irqsave(&opal_write_lock, flags);
daea1175 434 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
4f89363b
BH
435 rc = opal_console_write_buffer_space(vtermno, &olen);
436 len = be64_to_cpu(olen);
daea1175
BH
437 if (rc || len < total_len) {
438 spin_unlock_irqrestore(&opal_write_lock, flags);
439 /* Closed -> drop characters */
440 if (rc)
441 return total_len;
4f89363b 442 opal_poll_events(NULL);
daea1175
BH
443 return -EAGAIN;
444 }
14a43e69
BH
445 }
446
447 /* We still try to handle partial completions, though they
448 * should no longer happen.
449 */
daea1175 450 rc = OPAL_BUSY;
14a43e69
BH
451 while(total_len > 0 && (rc == OPAL_BUSY ||
452 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
4f89363b
BH
453 olen = cpu_to_be64(total_len);
454 rc = opal_console_write(vtermno, &olen, data);
455 len = be64_to_cpu(olen);
1de1455f
BH
456
457 /* Closed or other error drop */
458 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
459 rc != OPAL_BUSY_EVENT) {
460 written = total_len;
461 break;
462 }
14a43e69
BH
463 if (rc == OPAL_SUCCESS) {
464 total_len -= len;
465 data += len;
466 written += len;
467 }
468 /* This is a bit nasty but we need that for the console to
469 * flush when there aren't any interrupts. We will clean
470 * things a bit later to limit that to synchronous path
471 * such as the kernel console and xmon/udbg
472 */
473 do
474 opal_poll_events(&evt);
4f89363b
BH
475 while(rc == OPAL_SUCCESS &&
476 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
14a43e69
BH
477 }
478 spin_unlock_irqrestore(&opal_write_lock, flags);
479 return written;
480}
481
b63a0ffe
MS
482static int opal_recover_mce(struct pt_regs *regs,
483 struct machine_check_event *evt)
484{
485 int recovered = 0;
486 uint64_t ea = get_mce_fault_addr(evt);
487
488 if (!(regs->msr & MSR_RI)) {
489 /* If MSR_RI isn't set, we cannot recover */
490 recovered = 0;
491 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
492 /* Platform corrected itself */
493 recovered = 1;
494 } else if (ea && !is_kernel_addr(ea)) {
495 /*
496 * Faulting address is not in kernel text. We should be fine.
497 * We need to find which process uses this address.
498 * For now, kill the task if we have received exception when
499 * in userspace.
500 *
501 * TODO: Queue up this address for hwpoisioning later.
502 */
503 if (user_mode(regs) && !is_global_init(current)) {
504 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
505 recovered = 1;
506 } else
507 recovered = 0;
508 } else if (user_mode(regs) && !is_global_init(current) &&
509 evt->severity == MCE_SEV_ERROR_SYNC) {
510 /*
511 * If we have received a synchronous error when in userspace
512 * kill the task.
513 */
514 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
515 recovered = 1;
516 }
517 return recovered;
518}
519
ed79ba9e
BH
520int opal_machine_check(struct pt_regs *regs)
521{
36df96f8 522 struct machine_check_event evt;
ed79ba9e 523
36df96f8
MS
524 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
525 return 0;
ed79ba9e
BH
526
527 /* Print things out */
36df96f8 528 if (evt.version != MCE_V1) {
ed79ba9e
BH
529 pr_err("Machine Check Exception, Unknown event version %d !\n",
530 evt.version);
531 return 0;
532 }
b5ff4211 533 machine_check_print_event_info(&evt);
ed79ba9e 534
b63a0ffe
MS
535 if (opal_recover_mce(regs, &evt))
536 return 1;
537 return 0;
ed79ba9e
BH
538}
539
0869b6fd
MS
540/* Early hmi handler called in real mode. */
541int opal_hmi_exception_early(struct pt_regs *regs)
542{
0ef95b41
MS
543 s64 rc;
544
545 /*
546 * call opal hmi handler. Pass paca address as token.
547 * The return value OPAL_SUCCESS is an indication that there is
548 * an HMI event generated waiting to pull by Linux.
549 */
550 rc = opal_handle_hmi();
551 if (rc == OPAL_SUCCESS) {
552 local_paca->hmi_event_available = 1;
553 return 1;
554 }
0869b6fd
MS
555 return 0;
556}
557
558/* HMI exception handler called in virtual mode during check_irq_replay. */
559int opal_handle_hmi_exception(struct pt_regs *regs)
560{
0ef95b41
MS
561 s64 rc;
562 __be64 evt = 0;
563
564 /*
565 * Check if HMI event is available.
566 * if Yes, then call opal_poll_events to pull opal messages and
567 * process them.
568 */
569 if (!local_paca->hmi_event_available)
570 return 0;
571
572 local_paca->hmi_event_available = 0;
573 rc = opal_poll_events(&evt);
574 if (rc == OPAL_SUCCESS && evt)
575 opal_do_notifier(be64_to_cpu(evt));
576
577 return 1;
0869b6fd
MS
578}
579
55672ecf
MS
580static uint64_t find_recovery_address(uint64_t nip)
581{
582 int i;
583
584 for (i = 0; i < mc_recoverable_range_len; i++)
585 if ((nip >= mc_recoverable_range[i].start_addr) &&
586 (nip < mc_recoverable_range[i].end_addr))
587 return mc_recoverable_range[i].recover_addr;
588 return 0;
589}
590
591bool opal_mce_check_early_recovery(struct pt_regs *regs)
592{
593 uint64_t recover_addr = 0;
594
595 if (!opal.base || !opal.size)
596 goto out;
597
598 if ((regs->nip >= opal.base) &&
599 (regs->nip <= (opal.base + opal.size)))
600 recover_addr = find_recovery_address(regs->nip);
601
602 /*
603 * Setup regs->nip to rfi into fixup address.
604 */
605 if (recover_addr)
606 regs->nip = recover_addr;
607
608out:
609 return !!recover_addr;
610}
611
a125e092
BH
612static irqreturn_t opal_interrupt(int irq, void *data)
613{
5e4da530 614 __be64 events;
a125e092
BH
615
616 opal_handle_interrupt(virq_to_hw(irq), &events);
617
56b4c993 618 opal_do_notifier(be64_to_cpu(events));
a125e092
BH
619
620 return IRQ_HANDLED;
621}
622
6f68b5e2
VH
623static int opal_sysfs_init(void)
624{
625 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
626 if (!opal_kobj) {
627 pr_warn("kobject_create_and_add opal failed\n");
628 return -ENOMEM;
629 }
630
631 return 0;
632}
633
c8742f85
BH
634static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj,
635 struct bin_attribute *bin_attr,
636 char *buf, loff_t off, size_t count)
637{
638 return memory_read_from_buffer(buf, count, &off, bin_attr->private,
639 bin_attr->size);
640}
641
642static BIN_ATTR_RO(symbol_map, 0);
643
644static void opal_export_symmap(void)
645{
646 const __be64 *syms;
647 unsigned int size;
648 struct device_node *fw;
649 int rc;
650
651 fw = of_find_node_by_path("/ibm,opal/firmware");
652 if (!fw)
653 return;
654 syms = of_get_property(fw, "symbol-map", &size);
655 if (!syms || size != 2 * sizeof(__be64))
656 return;
657
658 /* Setup attributes */
659 bin_attr_symbol_map.private = __va(be64_to_cpu(syms[0]));
660 bin_attr_symbol_map.size = be64_to_cpu(syms[1]);
661
662 rc = sysfs_create_bin_file(opal_kobj, &bin_attr_symbol_map);
663 if (rc)
664 pr_warn("Error %d creating OPAL symbols file\n", rc);
665}
666
b09c2ec4
VH
667static void __init opal_dump_region_init(void)
668{
669 void *addr;
670 uint64_t size;
671 int rc;
672
b962f5a4
SS
673 if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))
674 return;
675
b09c2ec4
VH
676 /* Register kernel log buffer */
677 addr = log_buf_addr_get();
6501ab5e
PK
678 if (addr == NULL)
679 return;
680
b09c2ec4 681 size = log_buf_len_get();
6501ab5e
PK
682 if (size == 0)
683 return;
684
b09c2ec4
VH
685 rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
686 __pa(addr), size);
687 /* Don't warn if this is just an older OPAL that doesn't
688 * know about that call
689 */
690 if (rc && rc != OPAL_UNSUPPORTED)
691 pr_warn("DUMP: Failed to register kernel log buffer. "
692 "rc = %d\n", rc);
693}
608b286d 694
ed59190e
CB
695static void opal_flash_init(struct device_node *opal_node)
696{
697 struct device_node *np;
698
699 for_each_child_of_node(opal_node, np)
700 if (of_device_is_compatible(np, "ibm,opal-flash"))
701 of_platform_device_create(np, NULL, NULL);
702}
703
608b286d
JK
704static void opal_ipmi_init(struct device_node *opal_node)
705{
706 struct device_node *np;
707
708 for_each_child_of_node(opal_node, np)
709 if (of_device_is_compatible(np, "ibm,opal-ipmi"))
710 of_platform_device_create(np, NULL, NULL);
711}
712
47083450
NG
713static void opal_i2c_create_devs(void)
714{
715 struct device_node *np;
716
717 for_each_compatible_node(np, NULL, "ibm,opal-i2c")
718 of_platform_device_create(np, NULL, NULL);
719}
720
c1c3a526
GS
721static void __init opal_irq_init(struct device_node *dn)
722{
723 const __be32 *irqs;
724 int i, irqlen;
725
726 /* Get interrupt property */
727 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
31494cf3
GS
728 opal_irq_count = irqs ? (irqlen / 4) : 0;
729 pr_debug("Found %d interrupts reserved for OPAL\n", opal_irq_count);
730 if (!opal_irq_count)
731 return;
c1c3a526
GS
732
733 /* Install interrupt handlers */
c1c3a526
GS
734 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
735 for (i = 0; irqs && i < opal_irq_count; i++, irqs++) {
736 unsigned int irq, virq;
737 int rc;
738
739 /* Get hardware and virtual IRQ */
740 irq = be32_to_cpup(irqs);
741 virq = irq_create_mapping(NULL, irq);
742 if (virq == NO_IRQ) {
743 pr_warn("Failed to map irq 0x%x\n", irq);
744 continue;
745 }
746
747 /* Install interrupt handler */
748 rc = request_irq(virq, opal_interrupt, 0, "opal", NULL);
749 if (rc) {
750 irq_dispose_mapping(virq);
751 pr_warn("Error %d requesting irq %d (0x%x)\n",
752 rc, virq, irq);
753 continue;
754 }
755
756 /* Cache IRQ */
757 opal_irqs[i] = virq;
758 }
759}
760
3bf57561
BH
761static int kopald(void *unused)
762{
763 set_freezable();
764 do {
765 try_to_freeze();
766 opal_poll_events(NULL);
767 msleep_interruptible(opal_heartbeat);
768 } while (!kthread_should_stop());
769
770 return 0;
771}
772
773static void opal_init_heartbeat(void)
774{
775 /* Old firwmware, we assume the HVC heartbeat is sufficient */
776 if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
777 &opal_heartbeat) != 0)
778 opal_heartbeat = 0;
779
780 if (opal_heartbeat)
781 kthread_run(kopald, NULL, "kopald");
782}
783
14a43e69
BH
784static int __init opal_init(void)
785{
786 struct device_node *np, *consoles;
c1c3a526 787 int rc;
14a43e69
BH
788
789 opal_node = of_find_node_by_path("/ibm,opal");
790 if (!opal_node) {
08135139 791 pr_warn("Device node not found\n");
14a43e69
BH
792 return -ENODEV;
793 }
2db29d28
BH
794
795 /* Register OPAL consoles if any ports */
14a43e69
BH
796 if (firmware_has_feature(FW_FEATURE_OPALv2))
797 consoles = of_find_node_by_path("/ibm,opal/consoles");
798 else
799 consoles = of_node_get(opal_node);
2db29d28
BH
800 if (consoles) {
801 for_each_child_of_node(consoles, np) {
802 if (strcmp(np->name, "serial"))
803 continue;
804 of_platform_device_create(np, NULL, NULL);
805 }
806 of_node_put(consoles);
14a43e69 807 }
a125e092 808
96e023e7
AP
809 /* Initialise OPAL messaging system */
810 opal_message_init();
811
812 /* Initialise OPAL asynchronous completion interface */
813 opal_async_comp_init();
814
815 /* Initialise OPAL sensor interface */
816 opal_sensor_init();
817
818 /* Initialise OPAL hypervisor maintainence interrupt handling */
819 opal_hmi_handler_init();
820
47083450
NG
821 /* Create i2c platform devices */
822 opal_i2c_create_devs();
823
3bf57561
BH
824 /* Setup a heatbeat thread if requested by OPAL */
825 opal_init_heartbeat();
826
a125e092 827 /* Find all OPAL interrupts and request them */
c1c3a526 828 opal_irq_init(opal_node);
6f68b5e2
VH
829
830 /* Create "opal" kobject under /sys/firmware */
831 rc = opal_sysfs_init();
50bd6153 832 if (rc == 0) {
c8742f85
BH
833 /* Export symbol map to userspace */
834 opal_export_symmap();
b09c2ec4
VH
835 /* Setup dump region interface */
836 opal_dump_region_init();
774fea1a
SS
837 /* Setup error log interface */
838 rc = opal_elog_init();
50bd6153 839 /* Setup code update interface */
ed59190e 840 opal_flash_update_init();
c7e64b9c
SS
841 /* Setup platform dump extract interface */
842 opal_platform_dump_init();
4029cd66
NG
843 /* Setup system parameters interface */
844 opal_sys_param_init();
bfc36894
JS
845 /* Setup message log interface. */
846 opal_msglog_init();
50bd6153 847 }
6f68b5e2 848
3bf57561 849 /* Initialize OPAL IPMI backend */
608b286d
JK
850 opal_ipmi_init(opal_node);
851
ed59190e
CB
852 opal_flash_init(opal_node);
853
14a43e69
BH
854 return 0;
855}
b14726c5 856machine_subsys_initcall(powernv, opal_init);
73ed148a
BH
857
858void opal_shutdown(void)
859{
860 unsigned int i;
f7d98d18 861 long rc = OPAL_BUSY;
73ed148a 862
f7d98d18 863 /* First free interrupts, which will also mask them */
73ed148a
BH
864 for (i = 0; i < opal_irq_count; i++) {
865 if (opal_irqs[i])
b0d436c7 866 free_irq(opal_irqs[i], NULL);
73ed148a
BH
867 opal_irqs[i] = 0;
868 }
f7d98d18
VH
869
870 /*
871 * Then sync with OPAL which ensure anything that can
872 * potentially write to our memory has completed such
873 * as an ongoing dump retrieval
874 */
875 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
876 rc = opal_sync_host_reboot();
877 if (rc == OPAL_BUSY)
878 opal_poll_events(NULL);
879 else
880 mdelay(10);
881 }
b09c2ec4
VH
882
883 /* Unregister memory dump region */
b962f5a4
SS
884 if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
885 opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
73ed148a 886}
e28b05e7
JS
887
888/* Export this so that test modules can use it */
889EXPORT_SYMBOL_GPL(opal_invalid_call);
608b286d
JK
890EXPORT_SYMBOL_GPL(opal_ipmi_send);
891EXPORT_SYMBOL_GPL(opal_ipmi_recv);
ed59190e
CB
892EXPORT_SYMBOL_GPL(opal_flash_read);
893EXPORT_SYMBOL_GPL(opal_flash_write);
894EXPORT_SYMBOL_GPL(opal_flash_erase);
3441f04b
AB
895
896/* Convert a region of vmalloc memory to an opal sg list */
897struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
898 unsigned long vmalloc_size)
899{
900 struct opal_sg_list *sg, *first = NULL;
901 unsigned long i = 0;
902
903 sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
904 if (!sg)
905 goto nomem;
906
907 first = sg;
908
909 while (vmalloc_size > 0) {
910 uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
911 uint64_t length = min(vmalloc_size, PAGE_SIZE);
912
913 sg->entry[i].data = cpu_to_be64(data);
914 sg->entry[i].length = cpu_to_be64(length);
915 i++;
916
917 if (i >= SG_ENTRIES_PER_NODE) {
918 struct opal_sg_list *next;
919
920 next = kzalloc(PAGE_SIZE, GFP_KERNEL);
921 if (!next)
922 goto nomem;
923
924 sg->length = cpu_to_be64(
925 i * sizeof(struct opal_sg_entry) + 16);
926 i = 0;
927 sg->next = cpu_to_be64(__pa(next));
928 sg = next;
929 }
930
931 vmalloc_addr += length;
932 vmalloc_size -= length;
933 }
934
935 sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
936
937 return first;
938
939nomem:
940 pr_err("%s : Failed to allocate memory\n", __func__);
941 opal_free_sg_list(first);
942 return NULL;
943}
944
945void opal_free_sg_list(struct opal_sg_list *sg)
946{
947 while (sg) {
948 uint64_t next = be64_to_cpu(sg->next);
949
950 kfree(sg);
951
952 if (next)
953 sg = __va(next);
954 else
955 sg = NULL;
956 }
957}
16b1d26e 958
e3c5c2e0
CLG
959int opal_error_code(int rc)
960{
961 switch (rc) {
962 case OPAL_SUCCESS: return 0;
963
964 case OPAL_PARAMETER: return -EINVAL;
965 case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
966 case OPAL_BUSY_EVENT: return -EBUSY;
967 case OPAL_NO_MEM: return -ENOMEM;
968
969 case OPAL_UNSUPPORTED: return -EIO;
970 case OPAL_HARDWARE: return -EIO;
971 case OPAL_INTERNAL_ERROR: return -EIO;
972 default:
973 pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
974 return -EIO;
975 }
976}
977
16b1d26e
NG
978EXPORT_SYMBOL_GPL(opal_poll_events);
979EXPORT_SYMBOL_GPL(opal_rtc_read);
980EXPORT_SYMBOL_GPL(opal_rtc_write);
981EXPORT_SYMBOL_GPL(opal_tpo_read);
982EXPORT_SYMBOL_GPL(opal_tpo_write);
47083450 983EXPORT_SYMBOL_GPL(opal_i2c_request);