1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PowerNV OPAL high level interfaces 4 * 5 * Copyright 2011 IBM Corp. 6 */ 7 8 #define pr_fmt(fmt) "opal: " fmt 9 10 #include <linux/printk.h> 11 #include <linux/types.h> 12 #include <linux/of.h> 13 #include <linux/of_fdt.h> 14 #include <linux/of_platform.h> 15 #include <linux/of_address.h> 16 #include <linux/interrupt.h> 17 #include <linux/notifier.h> 18 #include <linux/slab.h> 19 #include <linux/sched.h> 20 #include <linux/kobject.h> 21 #include <linux/delay.h> 22 #include <linux/memblock.h> 23 #include <linux/kthread.h> 24 #include <linux/freezer.h> 25 #include <linux/kmsg_dump.h> 26 #include <linux/console.h> 27 #include <linux/sched/debug.h> 28 29 #include <asm/machdep.h> 30 #include <asm/opal.h> 31 #include <asm/firmware.h> 32 #include <asm/mce.h> 33 #include <asm/imc-pmu.h> 34 #include <asm/bug.h> 35 36 #include "powernv.h" 37 38 /* /sys/firmware/opal */ 39 struct kobject *opal_kobj; 40 41 struct opal { 42 u64 base; 43 u64 entry; 44 u64 size; 45 } opal; 46 47 struct mcheck_recoverable_range { 48 u64 start_addr; 49 u64 end_addr; 50 u64 recover_addr; 51 }; 52 53 static struct mcheck_recoverable_range *mc_recoverable_range; 54 static int mc_recoverable_range_len; 55 56 struct device_node *opal_node; 57 static DEFINE_SPINLOCK(opal_write_lock); 58 static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX]; 59 static uint32_t opal_heartbeat; 60 static struct task_struct *kopald_tsk; 61 62 void opal_configure_cores(void) 63 { 64 u64 reinit_flags = 0; 65 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__ 73 reinit_flags |= OPAL_REINIT_CPUS_HILE_BE; 74 #else 75 reinit_flags |= OPAL_REINIT_CPUS_HILE_LE; 76 #endif 77 78 /* 79 * POWER9 always support running hash: 80 * ie. Host hash supports hash guests 81 * Host radix supports hash/radix guests 82 */ 83 if (early_cpu_has_feature(CPU_FTR_ARCH_300)) { 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 91 /* Restore some bits */ 92 if (cur_cpu_spec->cpu_restore) 93 cur_cpu_spec->cpu_restore(); 94 } 95 96 int __init early_init_dt_scan_opal(unsigned long node, 97 const char *uname, int depth, void *data) 98 { 99 const void *basep, *entryp, *sizep; 100 int basesz, entrysz, runtimesz; 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); 107 sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz); 108 109 if (!basep || !entryp || !sizep) 110 return 1; 111 112 opal.base = of_read_number(basep, basesz/4); 113 opal.entry = of_read_number(entryp, entrysz/4); 114 opal.size = of_read_number(sizep, runtimesz/4); 115 116 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n", 117 opal.base, basep, basesz); 118 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n", 119 opal.entry, entryp, entrysz); 120 pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n", 121 opal.size, sizep, runtimesz); 122 123 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) { 124 powerpc_firmware_features |= FW_FEATURE_OPAL; 125 pr_debug("OPAL detected !\n"); 126 } else { 127 panic("OPAL != V3 detected, no longer supported.\n"); 128 } 129 130 return 1; 131 } 132 133 int __init early_init_dt_scan_recoverable_ranges(unsigned long node, 134 const char *uname, int depth, void *data) 135 { 136 int i, psize, size; 137 const __be32 *prop; 138 139 if (depth != 1 || strcmp(uname, "ibm,opal") != 0) 140 return 0; 141 142 prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize); 143 144 if (!prop) 145 return 1; 146 147 pr_debug("Found machine check recoverable ranges.\n"); 148 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 166 /* 167 * Allocate a buffer to hold the MC recoverable ranges. 168 */ 169 mc_recoverable_range = memblock_alloc(size, __alignof__(u64)); 170 if (!mc_recoverable_range) 171 panic("%s: Failed to allocate %u bytes align=0x%lx\n", 172 __func__, size, __alignof__(u64)); 173 174 for (i = 0; i < mc_recoverable_range_len; i++) { 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 } 188 return 1; 189 } 190 191 static int __init opal_register_exception_handlers(void) 192 { 193 #ifdef __BIG_ENDIAN__ 194 u64 glue; 195 196 if (!(powerpc_firmware_features & FW_FEATURE_OPAL)) 197 return -ENODEV; 198 199 /* Hookup some exception handlers except machine check. We use the 200 * fwnmi area at 0x7000 to provide the glue space to OPAL 201 */ 202 glue = 0x7000; 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)) { 217 pr_info("Old firmware detected, OPAL handles HMIs.\n"); 218 opal_register_exception_handler( 219 OPAL_HYPERVISOR_MAINTENANCE_HANDLER, 220 0, glue); 221 glue += 128; 222 } 223 224 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue); 225 #endif 226 227 return 0; 228 } 229 machine_early_initcall(powernv, opal_register_exception_handlers); 230 231 /* 232 * Opal message notifier based on message type. Allow subscribers to get 233 * notified for specific messgae type. 234 */ 235 int opal_message_notifier_register(enum opal_msg_type msg_type, 236 struct notifier_block *nb) 237 { 238 if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) { 239 pr_warn("%s: Invalid arguments, msg_type:%d\n", 240 __func__, msg_type); 241 return -EINVAL; 242 } 243 244 return atomic_notifier_chain_register( 245 &opal_msg_notifier_head[msg_type], nb); 246 } 247 EXPORT_SYMBOL_GPL(opal_message_notifier_register); 248 249 int opal_message_notifier_unregister(enum opal_msg_type msg_type, 250 struct notifier_block *nb) 251 { 252 return atomic_notifier_chain_unregister( 253 &opal_msg_notifier_head[msg_type], nb); 254 } 255 EXPORT_SYMBOL_GPL(opal_message_notifier_unregister); 256 257 static 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 264 static 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; 272 u32 type; 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) { 281 pr_warn("%s: Failed to retrieve opal message, err=%lld\n", 282 __func__, ret); 283 return; 284 } 285 286 type = be32_to_cpu(msg.msg_type); 287 288 /* Sanity check */ 289 if (type >= OPAL_MSG_TYPE_MAX) { 290 pr_warn_once("%s: Unknown message type: %u\n", __func__, type); 291 return; 292 } 293 opal_message_do_notify(type, (void *)&msg); 294 } 295 296 static irqreturn_t opal_message_notify(int irq, void *data) 297 { 298 opal_handle_message(); 299 return IRQ_HANDLED; 300 } 301 302 static int __init opal_message_init(void) 303 { 304 int ret, i, irq; 305 306 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++) 307 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]); 308 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); 318 if (ret) { 319 pr_err("%s: Can't request OPAL event irq (%d)\n", 320 __func__, ret); 321 return ret; 322 } 323 324 return 0; 325 } 326 327 int opal_get_chars(uint32_t vtermno, char *buf, int count) 328 { 329 s64 rc; 330 __be64 evt, len; 331 332 if (!opal.entry) 333 return -ENODEV; 334 opal_poll_events(&evt); 335 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0) 336 return 0; 337 len = cpu_to_be64(count); 338 rc = opal_console_read(vtermno, &len, buf); 339 if (rc == OPAL_SUCCESS) 340 return be64_to_cpu(len); 341 return 0; 342 } 343 344 static int __opal_put_chars(uint32_t vtermno, const char *data, int total_len, bool atomic) 345 { 346 unsigned long flags = 0 /* shut up gcc */; 347 int written; 348 __be64 olen; 349 s64 rc; 350 351 if (!opal.entry) 352 return -ENODEV; 353 354 if (atomic) 355 spin_lock_irqsave(&opal_write_lock, flags); 356 rc = opal_console_write_buffer_space(vtermno, &olen); 357 if (rc || be64_to_cpu(olen) < total_len) { 358 /* Closed -> drop characters */ 359 if (rc) 360 written = total_len; 361 else 362 written = -EAGAIN; 363 goto out; 364 } 365 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) { 370 if (rc == OPAL_BUSY_EVENT) 371 opal_poll_events(NULL); 372 written = -EAGAIN; 373 goto out; 374 } 375 376 /* Closed or other error drop */ 377 if (rc != OPAL_SUCCESS) { 378 written = opal_error_code(rc); 379 goto out; 380 } 381 382 written = be64_to_cpu(olen); 383 if (written < total_len) { 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 } 389 if (!written) 390 written = -EAGAIN; 391 } 392 393 out: 394 if (atomic) 395 spin_unlock_irqrestore(&opal_write_lock, flags); 396 397 return written; 398 } 399 400 int 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 */ 411 int 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 416 static s64 __opal_flush_console(uint32_t vtermno) 417 { 418 s64 rc; 419 420 if (!opal_check_token(OPAL_CONSOLE_FLUSH)) { 421 __be64 evt; 422 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 */ 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; 434 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; 442 } 443 444 } 445 446 /* 447 * opal_flush_console spins until the console is flushed 448 */ 449 int 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 */ 468 int 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; 477 } 478 if (rc == OPAL_PARTIAL) 479 return -EAGAIN; 480 } 481 482 return opal_error_code(rc); 483 } 484 } 485 486 static int opal_recover_mce(struct pt_regs *regs, 487 struct machine_check_event *evt) 488 { 489 int recovered = 0; 490 491 if (!(regs->msr & MSR_RI)) { 492 /* If MSR_RI isn't set, we cannot recover */ 493 pr_err("Machine check interrupt unrecoverable: MSR(RI=0)\n"); 494 recovered = 0; 495 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) { 496 /* Platform corrected itself */ 497 recovered = 1; 498 } else if (evt->severity == MCE_SEV_FATAL) { 499 /* Fatal machine check */ 500 pr_err("Machine check interrupt is fatal\n"); 501 recovered = 0; 502 } 503 504 if (!recovered && evt->sync_error) { 505 /* 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. 510 * 511 * TODO: Queue up this address for hwpoisioning later. 512 * TODO: This is not quite right for d-side machine 513 * checks ->nip is not necessarily the important 514 * address. 515 */ 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 } 530 } 531 532 return recovered; 533 } 534 535 void __noreturn pnv_platform_error_reboot(struct pt_regs *regs, const char *msg) 536 { 537 panic_flush_kmsg_start(); 538 539 pr_emerg("Hardware platform error: %s\n", msg); 540 if (regs) 541 show_regs(regs); 542 smp_send_stop(); 543 544 panic_flush_kmsg_end(); 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. 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. 569 */ 570 571 panic(msg); 572 } 573 574 int opal_machine_check(struct pt_regs *regs) 575 { 576 struct machine_check_event evt; 577 578 if (!get_mce_event(&evt, MCE_EVENT_RELEASE)) 579 return 0; 580 581 /* Print things out */ 582 if (evt.version != MCE_V1) { 583 pr_err("Machine Check Exception, Unknown event version %d !\n", 584 evt.version); 585 return 0; 586 } 587 machine_check_print_event_info(&evt, user_mode(regs), false); 588 589 if (opal_recover_mce(regs, &evt)) 590 return 1; 591 592 pnv_platform_error_reboot(regs, "Unrecoverable Machine Check exception"); 593 } 594 595 /* Early hmi handler called in real mode. */ 596 int opal_hmi_exception_early(struct pt_regs *regs) 597 { 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 } 610 return 0; 611 } 612 613 int 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 634 /* HMI exception handler called in virtual mode during check_irq_replay. */ 635 int opal_handle_hmi_exception(struct pt_regs *regs) 636 { 637 /* 638 * Check if HMI event is available. 639 * if Yes, then wake kopald to process them. 640 */ 641 if (!local_paca->hmi_event_available) 642 return 0; 643 644 local_paca->hmi_event_available = 0; 645 opal_wake_poller(); 646 647 return 1; 648 } 649 650 static 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 661 bool 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) && 669 (regs->nip < (opal.base + opal.size))) 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 678 out: 679 return !!recover_addr; 680 } 681 682 static 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 693 static 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 701 static BIN_ATTR_RO(symbol_map, 0); 702 703 static 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 726 static 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 */ 741 static 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 768 attr = kzalloc(sizeof(*attr), GFP_KERNEL); 769 770 if (attr == NULL) { 771 pr_warn("Failed kmalloc for bin_attribute!"); 772 continue; 773 } 774 775 sysfs_bin_attr_init(attr); 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 800 static void __init opal_dump_region_init(void) 801 { 802 void *addr; 803 uint64_t size; 804 int rc; 805 806 if (!opal_check_token(OPAL_REGISTER_DUMP_REGION)) 807 return; 808 809 /* Register kernel log buffer */ 810 addr = log_buf_addr_get(); 811 if (addr == NULL) 812 return; 813 814 size = log_buf_len_get(); 815 if (size == 0) 816 return; 817 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 } 827 828 static void opal_pdev_init(const char *compatible) 829 { 830 struct device_node *np; 831 832 for_each_compatible_node(np, NULL, compatible) 833 of_platform_device_create(np, NULL, NULL); 834 } 835 836 static 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 845 static int kopald(void *unused) 846 { 847 unsigned long timeout = msecs_to_jiffies(opal_heartbeat) + 1; 848 849 set_freezable(); 850 do { 851 try_to_freeze(); 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 861 } while (!kthread_should_stop()); 862 863 return 0; 864 } 865 866 void opal_wake_poller(void) 867 { 868 if (kopald_tsk) 869 wake_up_process(kopald_tsk); 870 } 871 872 static 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) 880 kopald_tsk = kthread_run(kopald, NULL, "kopald"); 881 } 882 883 static int __init opal_init(void) 884 { 885 struct device_node *np, *consoles, *leds; 886 int rc; 887 888 opal_node = of_find_node_by_path("/ibm,opal"); 889 if (!opal_node) { 890 pr_warn("Device node not found\n"); 891 return -ENODEV; 892 } 893 894 /* Register OPAL consoles if any ports */ 895 consoles = of_find_node_by_path("/ibm,opal/consoles"); 896 if (consoles) { 897 for_each_child_of_node(consoles, np) { 898 if (!of_node_name_eq(np, "serial")) 899 continue; 900 of_platform_device_create(np, NULL, NULL); 901 } 902 of_node_put(consoles); 903 } 904 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 917 /* Create i2c platform devices */ 918 opal_pdev_init("ibm,opal-i2c"); 919 920 /* Handle non-volatile memory devices */ 921 opal_pdev_init("pmem-region"); 922 923 /* Setup a heatbeat thread if requested by OPAL */ 924 opal_init_heartbeat(); 925 926 /* Detect In-Memory Collection counters and create devices*/ 927 opal_imc_init_dev(); 928 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 936 /* Initialise OPAL message log interface */ 937 opal_msglog_init(); 938 939 /* Create "opal" kobject under /sys/firmware */ 940 rc = opal_sysfs_init(); 941 if (rc == 0) { 942 /* Export symbol map to userspace */ 943 opal_export_symmap(); 944 /* Setup dump region interface */ 945 opal_dump_region_init(); 946 /* Setup error log interface */ 947 rc = opal_elog_init(); 948 /* Setup code update interface */ 949 opal_flash_update_init(); 950 /* Setup platform dump extract interface */ 951 opal_platform_dump_init(); 952 /* Setup system parameters interface */ 953 opal_sys_param_init(); 954 /* Setup message log sysfs interface. */ 955 opal_msglog_sysfs_init(); 956 } 957 958 /* Export all properties */ 959 opal_export_attrs(); 960 961 /* Initialize platform devices: IPMI backend, PRD & flash interface */ 962 opal_pdev_init("ibm,opal-ipmi"); 963 opal_pdev_init("ibm,opal-flash"); 964 opal_pdev_init("ibm,opal-prd"); 965 966 /* Initialise platform device: oppanel interface */ 967 opal_pdev_init("ibm,opal-oppanel"); 968 969 /* Initialise OPAL kmsg dumper for flushing console on panic */ 970 opal_kmsg_init(); 971 972 /* Initialise OPAL powercap interface */ 973 opal_powercap_init(); 974 975 /* Initialise OPAL Power-Shifting-Ratio interface */ 976 opal_psr_init(); 977 978 /* Initialise OPAL sensor groups */ 979 opal_sensor_groups_init(); 980 981 /* Initialise OPAL Power control interface */ 982 opal_power_control_init(); 983 984 return 0; 985 } 986 machine_subsys_initcall(powernv, opal_init); 987 988 void opal_shutdown(void) 989 { 990 long rc = OPAL_BUSY; 991 992 opal_event_shutdown(); 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 } 1006 1007 /* Unregister memory dump region */ 1008 if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION)) 1009 opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF); 1010 } 1011 1012 /* Export this so that test modules can use it */ 1013 EXPORT_SYMBOL_GPL(opal_invalid_call); 1014 EXPORT_SYMBOL_GPL(opal_xscom_read); 1015 EXPORT_SYMBOL_GPL(opal_xscom_write); 1016 EXPORT_SYMBOL_GPL(opal_ipmi_send); 1017 EXPORT_SYMBOL_GPL(opal_ipmi_recv); 1018 EXPORT_SYMBOL_GPL(opal_flash_read); 1019 EXPORT_SYMBOL_GPL(opal_flash_write); 1020 EXPORT_SYMBOL_GPL(opal_flash_erase); 1021 EXPORT_SYMBOL_GPL(opal_prd_msg); 1022 EXPORT_SYMBOL_GPL(opal_check_token); 1023 1024 /* Convert a region of vmalloc memory to an opal sg list */ 1025 struct 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 1067 nomem: 1068 pr_err("%s : Failed to allocate memory\n", __func__); 1069 opal_free_sg_list(first); 1070 return NULL; 1071 } 1072 1073 void 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 } 1086 1087 int 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; 1094 case OPAL_BUSY: 1095 case OPAL_BUSY_EVENT: return -EBUSY; 1096 case OPAL_NO_MEM: return -ENOMEM; 1097 case OPAL_PERMISSION: return -EPERM; 1098 1099 case OPAL_UNSUPPORTED: return -EIO; 1100 case OPAL_HARDWARE: return -EIO; 1101 case OPAL_INTERNAL_ERROR: return -EIO; 1102 case OPAL_TIMEOUT: return -ETIMEDOUT; 1103 default: 1104 pr_err("%s: unexpected OPAL error %d\n", __func__, rc); 1105 return -EIO; 1106 } 1107 } 1108 1109 void 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 1120 EXPORT_SYMBOL_GPL(opal_poll_events); 1121 EXPORT_SYMBOL_GPL(opal_rtc_read); 1122 EXPORT_SYMBOL_GPL(opal_rtc_write); 1123 EXPORT_SYMBOL_GPL(opal_tpo_read); 1124 EXPORT_SYMBOL_GPL(opal_tpo_write); 1125 EXPORT_SYMBOL_GPL(opal_i2c_request); 1126 /* Export these symbols for PowerNV LED class driver */ 1127 EXPORT_SYMBOL_GPL(opal_leds_get_ind); 1128 EXPORT_SYMBOL_GPL(opal_leds_set_ind); 1129 /* Export this symbol for PowerNV Operator Panel class driver */ 1130 EXPORT_SYMBOL_GPL(opal_write_oppanel_async); 1131 /* Export this for KVM */ 1132 EXPORT_SYMBOL_GPL(opal_int_set_mfrr); 1133 EXPORT_SYMBOL_GPL(opal_int_eoi); 1134 EXPORT_SYMBOL_GPL(opal_error_code); 1135 /* Export the below symbol for NX compression */ 1136 EXPORT_SYMBOL(opal_nx_coproc_init); 1137