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