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