Path: blob/master/arch/powerpc/platforms/powernv/opal.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* PowerNV OPAL high level interfaces3*4* Copyright 2011 IBM Corp.5*/67#define pr_fmt(fmt) "opal: " fmt89#include <linux/printk.h>10#include <linux/types.h>11#include <linux/of.h>12#include <linux/of_fdt.h>13#include <linux/of_platform.h>14#include <linux/of_address.h>15#include <linux/interrupt.h>16#include <linux/notifier.h>17#include <linux/slab.h>18#include <linux/sched.h>19#include <linux/kobject.h>20#include <linux/delay.h>21#include <linux/memblock.h>22#include <linux/kthread.h>23#include <linux/freezer.h>24#include <linux/kmsg_dump.h>25#include <linux/console.h>26#include <linux/sched/debug.h>2728#include <asm/machdep.h>29#include <asm/opal.h>30#include <asm/firmware.h>31#include <asm/mce.h>32#include <asm/imc-pmu.h>33#include <asm/bug.h>3435#include "powernv.h"3637#define OPAL_MSG_QUEUE_MAX 163839struct opal_msg_node {40struct list_head list;41struct opal_msg msg;42};4344static DEFINE_SPINLOCK(msg_list_lock);45static LIST_HEAD(msg_list);4647/* /sys/firmware/opal */48struct kobject *opal_kobj;4950struct opal {51u64 base;52u64 entry;53u64 size;54} opal;5556struct mcheck_recoverable_range {57u64 start_addr;58u64 end_addr;59u64 recover_addr;60};6162static int msg_list_size;6364static struct mcheck_recoverable_range *mc_recoverable_range;65static int mc_recoverable_range_len;6667struct device_node *opal_node;68static DEFINE_SPINLOCK(opal_write_lock);69static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];70static uint32_t opal_heartbeat;71static struct task_struct *kopald_tsk;72static struct opal_msg *opal_msg;73static u32 opal_msg_size __ro_after_init;7475void __init opal_configure_cores(void)76{77u64 reinit_flags = 0;7879/* Do the actual re-init, This will clobber all FPRs, VRs, etc...80*81* It will preserve non volatile GPRs and HSPRG0/1. It will82* also restore HIDs and other SPRs to their original value83* but it might clobber a bunch.84*/85#ifdef __BIG_ENDIAN__86reinit_flags |= OPAL_REINIT_CPUS_HILE_BE;87#else88reinit_flags |= OPAL_REINIT_CPUS_HILE_LE;89#endif9091/*92* POWER9 always support running hash:93* ie. Host hash supports hash guests94* Host radix supports hash/radix guests95*/96if (early_cpu_has_feature(CPU_FTR_ARCH_300)) {97reinit_flags |= OPAL_REINIT_CPUS_MMU_HASH;98if (early_radix_enabled())99reinit_flags |= OPAL_REINIT_CPUS_MMU_RADIX;100}101102opal_reinit_cpus(reinit_flags);103104/* Restore some bits */105if (cur_cpu_spec->cpu_restore)106cur_cpu_spec->cpu_restore();107}108109int __init early_init_dt_scan_opal(unsigned long node,110const char *uname, int depth, void *data)111{112const void *basep, *entryp, *sizep;113int basesz, entrysz, runtimesz;114115if (depth != 1 || strcmp(uname, "ibm,opal") != 0)116return 0;117118basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);119entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);120sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);121122if (!basep || !entryp || !sizep)123return 1;124125opal.base = of_read_number(basep, basesz/4);126opal.entry = of_read_number(entryp, entrysz/4);127opal.size = of_read_number(sizep, runtimesz/4);128129pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",130opal.base, basep, basesz);131pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",132opal.entry, entryp, entrysz);133pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",134opal.size, sizep, runtimesz);135136if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {137powerpc_firmware_features |= FW_FEATURE_OPAL;138pr_debug("OPAL detected !\n");139} else {140panic("OPAL != V3 detected, no longer supported.\n");141}142143return 1;144}145146int __init early_init_dt_scan_recoverable_ranges(unsigned long node,147const char *uname, int depth, void *data)148{149int i, psize, size;150const __be32 *prop;151152if (depth != 1 || strcmp(uname, "ibm,opal") != 0)153return 0;154155prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);156157if (!prop)158return 1;159160pr_debug("Found machine check recoverable ranges.\n");161162/*163* Calculate number of available entries.164*165* Each recoverable address range entry is (start address, len,166* recovery address), 2 cells each for start and recovery address,167* 1 cell for len, totalling 5 cells per entry.168*/169mc_recoverable_range_len = psize / (sizeof(*prop) * 5);170171/* Sanity check */172if (!mc_recoverable_range_len)173return 1;174175/* Size required to hold all the entries. */176size = mc_recoverable_range_len *177sizeof(struct mcheck_recoverable_range);178179/*180* Allocate a buffer to hold the MC recoverable ranges.181*/182mc_recoverable_range = memblock_alloc_or_panic(size, __alignof__(u64));183184for (i = 0; i < mc_recoverable_range_len; i++) {185mc_recoverable_range[i].start_addr =186of_read_number(prop + (i * 5) + 0, 2);187mc_recoverable_range[i].end_addr =188mc_recoverable_range[i].start_addr +189of_read_number(prop + (i * 5) + 2, 1);190mc_recoverable_range[i].recover_addr =191of_read_number(prop + (i * 5) + 3, 2);192193pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",194mc_recoverable_range[i].start_addr,195mc_recoverable_range[i].end_addr,196mc_recoverable_range[i].recover_addr);197}198return 1;199}200201static int __init opal_register_exception_handlers(void)202{203#ifdef __BIG_ENDIAN__204u64 glue;205206if (!(powerpc_firmware_features & FW_FEATURE_OPAL))207return -ENODEV;208209/* Hookup some exception handlers except machine check. We use the210* fwnmi area at 0x7000 to provide the glue space to OPAL211*/212glue = 0x7000;213214/*215* Only ancient OPAL firmware requires this.216* Specifically, firmware from FW810.00 (released June 2014)217* through FW810.20 (Released October 2014).218*219* Check if we are running on newer (post Oct 2014) firmware that220* exports the OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to221* patch the HMI interrupt and we catch it directly in Linux.222*223* For older firmware (i.e < FW810.20), we fallback to old behavior and224* let OPAL patch the HMI vector and handle it inside OPAL firmware.225*226* For newer firmware we catch/handle the HMI directly in Linux.227*/228if (!opal_check_token(OPAL_HANDLE_HMI)) {229pr_info("Old firmware detected, OPAL handles HMIs.\n");230opal_register_exception_handler(231OPAL_HYPERVISOR_MAINTENANCE_HANDLER,2320, glue);233glue += 128;234}235236/*237* Only applicable to ancient firmware, all modern238* (post March 2015/skiboot 5.0) firmware will just return239* OPAL_UNSUPPORTED.240*/241opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);242#endif243244return 0;245}246machine_early_initcall(powernv, opal_register_exception_handlers);247248static void queue_replay_msg(void *msg)249{250struct opal_msg_node *msg_node;251252if (msg_list_size < OPAL_MSG_QUEUE_MAX) {253msg_node = kzalloc(sizeof(*msg_node), GFP_ATOMIC);254if (msg_node) {255INIT_LIST_HEAD(&msg_node->list);256memcpy(&msg_node->msg, msg, sizeof(struct opal_msg));257list_add_tail(&msg_node->list, &msg_list);258msg_list_size++;259} else260pr_warn_once("message queue no memory\n");261262if (msg_list_size >= OPAL_MSG_QUEUE_MAX)263pr_warn_once("message queue full\n");264}265}266267static void dequeue_replay_msg(enum opal_msg_type msg_type)268{269struct opal_msg_node *msg_node, *tmp;270271list_for_each_entry_safe(msg_node, tmp, &msg_list, list) {272if (be32_to_cpu(msg_node->msg.msg_type) != msg_type)273continue;274275atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],276msg_type,277&msg_node->msg);278279list_del(&msg_node->list);280kfree(msg_node);281msg_list_size--;282}283}284285/*286* Opal message notifier based on message type. Allow subscribers to get287* notified for specific messgae type.288*/289int opal_message_notifier_register(enum opal_msg_type msg_type,290struct notifier_block *nb)291{292int ret;293unsigned long flags;294295if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {296pr_warn("%s: Invalid arguments, msg_type:%d\n",297__func__, msg_type);298return -EINVAL;299}300301spin_lock_irqsave(&msg_list_lock, flags);302ret = atomic_notifier_chain_register(303&opal_msg_notifier_head[msg_type], nb);304305/*306* If the registration succeeded, replay any queued messages that came307* in prior to the notifier chain registration. msg_list_lock held here308* to ensure they're delivered prior to any subsequent messages.309*/310if (ret == 0)311dequeue_replay_msg(msg_type);312313spin_unlock_irqrestore(&msg_list_lock, flags);314315return ret;316}317EXPORT_SYMBOL_GPL(opal_message_notifier_register);318319int opal_message_notifier_unregister(enum opal_msg_type msg_type,320struct notifier_block *nb)321{322return atomic_notifier_chain_unregister(323&opal_msg_notifier_head[msg_type], nb);324}325EXPORT_SYMBOL_GPL(opal_message_notifier_unregister);326327static void opal_message_do_notify(uint32_t msg_type, void *msg)328{329unsigned long flags;330bool queued = false;331332spin_lock_irqsave(&msg_list_lock, flags);333if (opal_msg_notifier_head[msg_type].head == NULL) {334/*335* Queue up the msg since no notifiers have registered336* yet for this msg_type.337*/338queue_replay_msg(msg);339queued = true;340}341spin_unlock_irqrestore(&msg_list_lock, flags);342343if (queued)344return;345346/* notify subscribers */347atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],348msg_type, msg);349}350351static void opal_handle_message(void)352{353s64 ret;354u32 type;355356ret = opal_get_msg(__pa(opal_msg), opal_msg_size);357/* No opal message pending. */358if (ret == OPAL_RESOURCE)359return;360361/* check for errors. */362if (ret) {363pr_warn("%s: Failed to retrieve opal message, err=%lld\n",364__func__, ret);365return;366}367368type = be32_to_cpu(opal_msg->msg_type);369370/* Sanity check */371if (type >= OPAL_MSG_TYPE_MAX) {372pr_warn_once("%s: Unknown message type: %u\n", __func__, type);373return;374}375opal_message_do_notify(type, (void *)opal_msg);376}377378static irqreturn_t opal_message_notify(int irq, void *data)379{380opal_handle_message();381return IRQ_HANDLED;382}383384static int __init opal_message_init(struct device_node *opal_node)385{386int ret, i, irq;387388ret = of_property_read_u32(opal_node, "opal-msg-size", &opal_msg_size);389if (ret) {390pr_notice("Failed to read opal-msg-size property\n");391opal_msg_size = sizeof(struct opal_msg);392}393394opal_msg = kmalloc(opal_msg_size, GFP_KERNEL);395if (!opal_msg) {396opal_msg_size = sizeof(struct opal_msg);397/* Try to allocate fixed message size */398opal_msg = kmalloc(opal_msg_size, GFP_KERNEL);399BUG_ON(opal_msg == NULL);400}401402for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)403ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);404405irq = opal_event_request(ilog2(OPAL_EVENT_MSG_PENDING));406if (!irq) {407pr_err("%s: Can't register OPAL event irq (%d)\n",408__func__, irq);409return irq;410}411412ret = request_irq(irq, opal_message_notify,413IRQ_TYPE_LEVEL_HIGH, "opal-msg", NULL);414if (ret) {415pr_err("%s: Can't request OPAL event irq (%d)\n",416__func__, ret);417return ret;418}419420return 0;421}422423ssize_t opal_get_chars(uint32_t vtermno, u8 *buf, size_t count)424{425s64 rc;426__be64 evt, len;427428if (!opal.entry)429return -ENODEV;430opal_poll_events(&evt);431if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)432return 0;433len = cpu_to_be64(count);434rc = opal_console_read(vtermno, &len, buf);435if (rc == OPAL_SUCCESS)436return be64_to_cpu(len);437return 0;438}439440static ssize_t __opal_put_chars(uint32_t vtermno, const u8 *data,441size_t total_len, bool atomic)442{443unsigned long flags = 0 /* shut up gcc */;444ssize_t written;445__be64 olen;446s64 rc;447448if (!opal.entry)449return -ENODEV;450451if (atomic)452spin_lock_irqsave(&opal_write_lock, flags);453rc = opal_console_write_buffer_space(vtermno, &olen);454if (rc || be64_to_cpu(olen) < total_len) {455/* Closed -> drop characters */456if (rc)457written = total_len;458else459written = -EAGAIN;460goto out;461}462463/* Should not get a partial write here because space is available. */464olen = cpu_to_be64(total_len);465rc = opal_console_write(vtermno, &olen, data);466if (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {467if (rc == OPAL_BUSY_EVENT)468opal_poll_events(NULL);469written = -EAGAIN;470goto out;471}472473/* Closed or other error drop */474if (rc != OPAL_SUCCESS) {475written = opal_error_code(rc);476goto out;477}478479written = be64_to_cpu(olen);480if (written < total_len) {481if (atomic) {482/* Should not happen */483pr_warn("atomic console write returned partial "484"len=%zu written=%zd\n", total_len, written);485}486if (!written)487written = -EAGAIN;488}489490out:491if (atomic)492spin_unlock_irqrestore(&opal_write_lock, flags);493494return written;495}496497ssize_t opal_put_chars(uint32_t vtermno, const u8 *data, size_t total_len)498{499return __opal_put_chars(vtermno, data, total_len, false);500}501502/*503* opal_put_chars_atomic will not perform partial-writes. Data will be504* atomically written to the terminal or not at all. This is not strictly505* true at the moment because console space can race with OPAL's console506* writes.507*/508ssize_t opal_put_chars_atomic(uint32_t vtermno, const u8 *data,509size_t total_len)510{511return __opal_put_chars(vtermno, data, total_len, true);512}513514static s64 __opal_flush_console(uint32_t vtermno)515{516s64 rc;517518if (!opal_check_token(OPAL_CONSOLE_FLUSH)) {519__be64 evt;520521/*522* If OPAL_CONSOLE_FLUSH is not implemented in the firmware,523* the console can still be flushed by calling the polling524* function while it has OPAL_EVENT_CONSOLE_OUTPUT events.525*/526WARN_ONCE(1, "opal: OPAL_CONSOLE_FLUSH missing.\n");527528opal_poll_events(&evt);529if (!(be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT))530return OPAL_SUCCESS;531return OPAL_BUSY;532533} else {534rc = opal_console_flush(vtermno);535if (rc == OPAL_BUSY_EVENT) {536opal_poll_events(NULL);537rc = OPAL_BUSY;538}539return rc;540}541542}543544/*545* opal_flush_console spins until the console is flushed546*/547int opal_flush_console(uint32_t vtermno)548{549for (;;) {550s64 rc = __opal_flush_console(vtermno);551552if (rc == OPAL_BUSY || rc == OPAL_PARTIAL) {553mdelay(1);554continue;555}556557return opal_error_code(rc);558}559}560561/*562* opal_flush_chars is an hvc interface that sleeps until the console is563* flushed if wait, otherwise it will return -EBUSY if the console has data,564* -EAGAIN if it has data and some of it was flushed.565*/566int opal_flush_chars(uint32_t vtermno, bool wait)567{568for (;;) {569s64 rc = __opal_flush_console(vtermno);570571if (rc == OPAL_BUSY || rc == OPAL_PARTIAL) {572if (wait) {573msleep(OPAL_BUSY_DELAY_MS);574continue;575}576if (rc == OPAL_PARTIAL)577return -EAGAIN;578}579580return opal_error_code(rc);581}582}583584static int opal_recover_mce(struct pt_regs *regs,585struct machine_check_event *evt)586{587int recovered = 0;588589if (regs_is_unrecoverable(regs)) {590/* If MSR_RI isn't set, we cannot recover */591pr_err("Machine check interrupt unrecoverable: MSR(RI=0)\n");592recovered = 0;593} else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {594/* Platform corrected itself */595recovered = 1;596} else if (evt->severity == MCE_SEV_FATAL) {597/* Fatal machine check */598pr_err("Machine check interrupt is fatal\n");599recovered = 0;600}601602if (!recovered && evt->sync_error) {603/*604* Try to kill processes if we get a synchronous machine check605* (e.g., one caused by execution of this instruction). This606* will devolve into a panic if we try to kill init or are in607* an interrupt etc.608*609* TODO: Queue up this address for hwpoisioning later.610* TODO: This is not quite right for d-side machine611* checks ->nip is not necessarily the important612* address.613*/614if ((user_mode(regs))) {615_exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);616recovered = 1;617} else if (die_will_crash()) {618/*619* die() would kill the kernel, so better to go via620* the platform reboot code that will log the621* machine check.622*/623recovered = 0;624} else {625die_mce("Machine check", regs, SIGBUS);626recovered = 1;627}628}629630return recovered;631}632633void __noreturn pnv_platform_error_reboot(struct pt_regs *regs, const char *msg)634{635panic_flush_kmsg_start();636637pr_emerg("Hardware platform error: %s\n", msg);638if (regs)639show_regs(regs);640smp_send_stop();641642panic_flush_kmsg_end();643644/*645* Don't bother to shut things down because this will646* xstop the system.647*/648if (opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR, msg)649== OPAL_UNSUPPORTED) {650pr_emerg("Reboot type %d not supported for %s\n",651OPAL_REBOOT_PLATFORM_ERROR, msg);652}653654/*655* We reached here. There can be three possibilities:656* 1. We are running on a firmware level that do not support657* opal_cec_reboot2()658* 2. We are running on a firmware level that do not support659* OPAL_REBOOT_PLATFORM_ERROR reboot type.660* 3. We are running on FSP based system that does not need661* opal to trigger checkstop explicitly for error analysis.662* The FSP PRD component would have already got notified663* about this error through other channels.664* 4. We are running on a newer skiboot that by default does665* not cause a checkstop, drops us back to the kernel to666* extract context and state at the time of the error.667*/668669panic(msg);670}671672int opal_machine_check(struct pt_regs *regs)673{674struct machine_check_event evt;675676if (!get_mce_event(&evt, MCE_EVENT_RELEASE))677return 0;678679/* Print things out */680if (evt.version != MCE_V1) {681pr_err("Machine Check Exception, Unknown event version %d !\n",682evt.version);683return 0;684}685machine_check_print_event_info(&evt, user_mode(regs), false);686687if (opal_recover_mce(regs, &evt))688return 1;689690pnv_platform_error_reboot(regs, "Unrecoverable Machine Check exception");691}692693/* Early hmi handler called in real mode. */694int opal_hmi_exception_early(struct pt_regs *regs)695{696s64 rc;697698/*699* call opal hmi handler. Pass paca address as token.700* The return value OPAL_SUCCESS is an indication that there is701* an HMI event generated waiting to pull by Linux.702*/703rc = opal_handle_hmi();704if (rc == OPAL_SUCCESS) {705local_paca->hmi_event_available = 1;706return 1;707}708return 0;709}710711int opal_hmi_exception_early2(struct pt_regs *regs)712{713s64 rc;714__be64 out_flags;715716/*717* call opal hmi handler.718* Check 64-bit flag mask to find out if an event was generated,719* and whether TB is still valid or not etc.720*/721rc = opal_handle_hmi2(&out_flags);722if (rc != OPAL_SUCCESS)723return 0;724725if (be64_to_cpu(out_flags) & OPAL_HMI_FLAGS_NEW_EVENT)726local_paca->hmi_event_available = 1;727if (be64_to_cpu(out_flags) & OPAL_HMI_FLAGS_TOD_TB_FAIL)728tb_invalid = true;729return 1;730}731732/* HMI exception handler called in virtual mode when irqs are next enabled. */733int opal_handle_hmi_exception(struct pt_regs *regs)734{735/*736* Check if HMI event is available.737* if Yes, then wake kopald to process them.738*/739if (!local_paca->hmi_event_available)740return 0;741742local_paca->hmi_event_available = 0;743opal_wake_poller();744745return 1;746}747748static uint64_t find_recovery_address(uint64_t nip)749{750int i;751752for (i = 0; i < mc_recoverable_range_len; i++)753if ((nip >= mc_recoverable_range[i].start_addr) &&754(nip < mc_recoverable_range[i].end_addr))755return mc_recoverable_range[i].recover_addr;756return 0;757}758759bool opal_mce_check_early_recovery(struct pt_regs *regs)760{761uint64_t recover_addr = 0;762763if (!opal.base || !opal.size)764goto out;765766if ((regs->nip >= opal.base) &&767(regs->nip < (opal.base + opal.size)))768recover_addr = find_recovery_address(regs->nip);769770/*771* Setup regs->nip to rfi into fixup address.772*/773if (recover_addr)774regs_set_return_ip(regs, recover_addr);775776out:777return !!recover_addr;778}779780static int __init opal_sysfs_init(void)781{782opal_kobj = kobject_create_and_add("opal", firmware_kobj);783if (!opal_kobj) {784pr_warn("kobject_create_and_add opal failed\n");785return -ENOMEM;786}787788return 0;789}790791static int opal_add_one_export(struct kobject *parent, const char *export_name,792struct device_node *np, const char *prop_name)793{794struct bin_attribute *attr = NULL;795const char *name = NULL;796u64 vals[2];797int rc;798799rc = of_property_read_u64_array(np, prop_name, &vals[0], 2);800if (rc)801goto out;802803attr = kzalloc(sizeof(*attr), GFP_KERNEL);804if (!attr) {805rc = -ENOMEM;806goto out;807}808name = kstrdup(export_name, GFP_KERNEL);809if (!name) {810rc = -ENOMEM;811goto out;812}813814sysfs_bin_attr_init(attr);815attr->attr.name = name;816attr->attr.mode = 0400;817attr->read = sysfs_bin_attr_simple_read;818attr->private = __va(vals[0]);819attr->size = vals[1];820821rc = sysfs_create_bin_file(parent, attr);822out:823if (rc) {824kfree(name);825kfree(attr);826}827828return rc;829}830831static void opal_add_exported_attrs(struct device_node *np,832struct kobject *kobj)833{834struct device_node *child;835struct property *prop;836837for_each_property_of_node(np, prop) {838int rc;839840if (!strcmp(prop->name, "name") ||841!strcmp(prop->name, "phandle"))842continue;843844rc = opal_add_one_export(kobj, prop->name, np, prop->name);845if (rc) {846pr_warn("Unable to add export %pOF/%s, rc = %d!\n",847np, prop->name, rc);848}849}850851for_each_child_of_node(np, child) {852struct kobject *child_kobj;853854child_kobj = kobject_create_and_add(child->name, kobj);855if (!child_kobj) {856pr_err("Unable to create export dir for %pOF\n", child);857continue;858}859860opal_add_exported_attrs(child, child_kobj);861}862}863864/*865* opal_export_attrs: creates a sysfs node for each property listed in866* the device-tree under /ibm,opal/firmware/exports/867* All new sysfs nodes are created under /opal/exports/.868* This allows for reserved memory regions (e.g. HDAT) to be read.869* The new sysfs nodes are only readable by root.870*/871static void opal_export_attrs(void)872{873struct device_node *np;874struct kobject *kobj;875int rc;876877np = of_find_node_by_path("/ibm,opal/firmware/exports");878if (!np)879return;880881/* Create new 'exports' directory - /sys/firmware/opal/exports */882kobj = kobject_create_and_add("exports", opal_kobj);883if (!kobj) {884pr_warn("kobject_create_and_add() of exports failed\n");885of_node_put(np);886return;887}888889opal_add_exported_attrs(np, kobj);890891/*892* NB: symbol_map existed before the generic export interface so it893* lives under the top level opal_kobj.894*/895rc = opal_add_one_export(opal_kobj, "symbol_map",896np->parent, "symbol-map");897if (rc)898pr_warn("Error %d creating OPAL symbols file\n", rc);899900of_node_put(np);901}902903static void __init opal_dump_region_init(void)904{905void *addr;906uint64_t size;907int rc;908909if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))910return;911912/* Register kernel log buffer */913addr = log_buf_addr_get();914if (addr == NULL)915return;916917size = log_buf_len_get();918if (size == 0)919return;920921rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,922__pa(addr), size);923/* Don't warn if this is just an older OPAL that doesn't924* know about that call925*/926if (rc && rc != OPAL_UNSUPPORTED)927pr_warn("DUMP: Failed to register kernel log buffer. "928"rc = %d\n", rc);929}930931static void __init opal_pdev_init(const char *compatible)932{933struct device_node *np;934935for_each_compatible_node(np, NULL, compatible)936of_platform_device_create(np, NULL, NULL);937}938939static void __init opal_imc_init_dev(void)940{941struct device_node *np;942943np = of_find_compatible_node(NULL, NULL, IMC_DTB_COMPAT);944if (np)945of_platform_device_create(np, NULL, NULL);946947of_node_put(np);948}949950static int kopald(void *unused)951{952unsigned long timeout = msecs_to_jiffies(opal_heartbeat) + 1;953954set_freezable();955do {956try_to_freeze();957958opal_handle_events();959960set_current_state(TASK_INTERRUPTIBLE);961if (opal_have_pending_events())962__set_current_state(TASK_RUNNING);963else964schedule_timeout(timeout);965966} while (!kthread_should_stop());967968return 0;969}970971void opal_wake_poller(void)972{973if (kopald_tsk)974wake_up_process(kopald_tsk);975}976977static void __init opal_init_heartbeat(void)978{979/* Old firwmware, we assume the HVC heartbeat is sufficient */980if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",981&opal_heartbeat) != 0)982opal_heartbeat = 0;983984if (opal_heartbeat)985kopald_tsk = kthread_run(kopald, NULL, "kopald");986}987988static int __init opal_init(void)989{990struct device_node *np, *consoles, *leds;991int rc;992993opal_node = of_find_node_by_path("/ibm,opal");994if (!opal_node) {995pr_warn("Device node not found\n");996return -ENODEV;997}998999/* Register OPAL consoles if any ports */1000consoles = of_find_node_by_path("/ibm,opal/consoles");1001if (consoles) {1002for_each_child_of_node(consoles, np) {1003if (!of_node_name_eq(np, "serial"))1004continue;1005of_platform_device_create(np, NULL, NULL);1006}1007of_node_put(consoles);1008}10091010/* Initialise OPAL messaging system */1011opal_message_init(opal_node);10121013/* Initialise OPAL asynchronous completion interface */1014opal_async_comp_init();10151016/* Initialise OPAL sensor interface */1017opal_sensor_init();10181019/* Initialise OPAL hypervisor maintainence interrupt handling */1020opal_hmi_handler_init();10211022/* Create i2c platform devices */1023opal_pdev_init("ibm,opal-i2c");10241025/* Handle non-volatile memory devices */1026opal_pdev_init("pmem-region");10271028/* Setup a heatbeat thread if requested by OPAL */1029opal_init_heartbeat();10301031/* Detect In-Memory Collection counters and create devices*/1032opal_imc_init_dev();10331034/* Create leds platform devices */1035leds = of_find_node_by_path("/ibm,opal/leds");1036if (leds) {1037of_platform_device_create(leds, "opal_leds", NULL);1038of_node_put(leds);1039}10401041/* Initialise OPAL message log interface */1042opal_msglog_init();10431044/* Create "opal" kobject under /sys/firmware */1045rc = opal_sysfs_init();1046if (rc == 0) {1047/* Setup dump region interface */1048opal_dump_region_init();1049/* Setup error log interface */1050rc = opal_elog_init();1051/* Setup code update interface */1052opal_flash_update_init();1053/* Setup platform dump extract interface */1054opal_platform_dump_init();1055/* Setup system parameters interface */1056opal_sys_param_init();1057/* Setup message log sysfs interface. */1058opal_msglog_sysfs_init();1059/* Add all export properties*/1060opal_export_attrs();1061}10621063/* Initialize platform devices: IPMI backend, PRD & flash interface */1064opal_pdev_init("ibm,opal-ipmi");1065opal_pdev_init("ibm,opal-flash");1066opal_pdev_init("ibm,opal-prd");10671068/* Initialise platform device: oppanel interface */1069opal_pdev_init("ibm,opal-oppanel");10701071/* Initialise OPAL kmsg dumper for flushing console on panic */1072opal_kmsg_init();10731074/* Initialise OPAL powercap interface */1075opal_powercap_init();10761077/* Initialise OPAL Power-Shifting-Ratio interface */1078opal_psr_init();10791080/* Initialise OPAL sensor groups */1081opal_sensor_groups_init();10821083/* Initialise OPAL Power control interface */1084opal_power_control_init();10851086/* Initialize OPAL secure variables */1087opal_pdev_init("ibm,secvar-backend");10881089return 0;1090}1091machine_subsys_initcall(powernv, opal_init);10921093void opal_shutdown(void)1094{1095long rc = OPAL_BUSY;10961097opal_event_shutdown();10981099/*1100* Then sync with OPAL which ensure anything that can1101* potentially write to our memory has completed such1102* as an ongoing dump retrieval1103*/1104while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {1105rc = opal_sync_host_reboot();1106if (rc == OPAL_BUSY)1107opal_poll_events(NULL);1108else1109mdelay(10);1110}11111112/* Unregister memory dump region */1113if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))1114opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);1115}11161117/* Export this so that test modules can use it */1118EXPORT_SYMBOL_GPL(opal_invalid_call);1119EXPORT_SYMBOL_GPL(opal_xscom_read);1120EXPORT_SYMBOL_GPL(opal_xscom_write);1121EXPORT_SYMBOL_GPL(opal_ipmi_send);1122EXPORT_SYMBOL_GPL(opal_ipmi_recv);1123EXPORT_SYMBOL_GPL(opal_flash_read);1124EXPORT_SYMBOL_GPL(opal_flash_write);1125EXPORT_SYMBOL_GPL(opal_flash_erase);1126EXPORT_SYMBOL_GPL(opal_prd_msg);1127EXPORT_SYMBOL_GPL(opal_check_token);11281129/* Convert a region of vmalloc memory to an opal sg list */1130struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,1131unsigned long vmalloc_size)1132{1133struct opal_sg_list *sg, *first = NULL;1134unsigned long i = 0;11351136sg = kzalloc(PAGE_SIZE, GFP_KERNEL);1137if (!sg)1138goto nomem;11391140first = sg;11411142while (vmalloc_size > 0) {1143uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;1144uint64_t length = min(vmalloc_size, PAGE_SIZE);11451146sg->entry[i].data = cpu_to_be64(data);1147sg->entry[i].length = cpu_to_be64(length);1148i++;11491150if (i >= SG_ENTRIES_PER_NODE) {1151struct opal_sg_list *next;11521153next = kzalloc(PAGE_SIZE, GFP_KERNEL);1154if (!next)1155goto nomem;11561157sg->length = cpu_to_be64(1158i * sizeof(struct opal_sg_entry) + 16);1159i = 0;1160sg->next = cpu_to_be64(__pa(next));1161sg = next;1162}11631164vmalloc_addr += length;1165vmalloc_size -= length;1166}11671168sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);11691170return first;11711172nomem:1173pr_err("%s : Failed to allocate memory\n", __func__);1174opal_free_sg_list(first);1175return NULL;1176}11771178void opal_free_sg_list(struct opal_sg_list *sg)1179{1180while (sg) {1181uint64_t next = be64_to_cpu(sg->next);11821183kfree(sg);11841185if (next)1186sg = __va(next);1187else1188sg = NULL;1189}1190}11911192int opal_error_code(int rc)1193{1194switch (rc) {1195case OPAL_SUCCESS: return 0;11961197case OPAL_PARAMETER: return -EINVAL;1198case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;1199case OPAL_BUSY:1200case OPAL_BUSY_EVENT: return -EBUSY;1201case OPAL_NO_MEM: return -ENOMEM;1202case OPAL_PERMISSION: return -EPERM;12031204case OPAL_UNSUPPORTED: return -EIO;1205case OPAL_HARDWARE: return -EIO;1206case OPAL_INTERNAL_ERROR: return -EIO;1207case OPAL_TIMEOUT: return -ETIMEDOUT;1208default:1209pr_err("%s: unexpected OPAL error %d\n", __func__, rc);1210return -EIO;1211}1212}12131214void powernv_set_nmmu_ptcr(unsigned long ptcr)1215{1216int rc;12171218if (firmware_has_feature(FW_FEATURE_OPAL)) {1219rc = opal_nmmu_set_ptcr(-1UL, ptcr);1220if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)1221pr_warn("%s: Unable to set nest mmu ptcr\n", __func__);1222}1223}12241225EXPORT_SYMBOL_GPL(opal_poll_events);1226EXPORT_SYMBOL_GPL(opal_rtc_read);1227EXPORT_SYMBOL_GPL(opal_rtc_write);1228EXPORT_SYMBOL_GPL(opal_tpo_read);1229EXPORT_SYMBOL_GPL(opal_tpo_write);1230EXPORT_SYMBOL_GPL(opal_i2c_request);1231/* Export these symbols for PowerNV LED class driver */1232EXPORT_SYMBOL_GPL(opal_leds_get_ind);1233EXPORT_SYMBOL_GPL(opal_leds_set_ind);1234/* Export this symbol for PowerNV Operator Panel class driver */1235EXPORT_SYMBOL_GPL(opal_write_oppanel_async);1236/* Export this for KVM */1237EXPORT_SYMBOL_GPL(opal_int_set_mfrr);1238EXPORT_SYMBOL_GPL(opal_int_eoi);1239EXPORT_SYMBOL_GPL(opal_error_code);1240/* Export the below symbol for NX compression */1241EXPORT_SYMBOL(opal_nx_coproc_init);124212431244