Path: blob/master/arch/powerpc/platforms/powernv/opal-dump.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* PowerNV OPAL Dump Interface3*4* Copyright 2013,2014 IBM Corp.5*/67#include <linux/kobject.h>8#include <linux/mm.h>9#include <linux/slab.h>10#include <linux/vmalloc.h>11#include <linux/pagemap.h>12#include <linux/delay.h>13#include <linux/interrupt.h>1415#include <asm/opal.h>1617#define DUMP_TYPE_FSP 0x011819struct dump_obj {20struct kobject kobj;21struct bin_attribute dump_attr;22uint32_t id; /* becomes object name */23uint32_t type;24uint32_t size;25char *buffer;26};27#define to_dump_obj(x) container_of(x, struct dump_obj, kobj)2829struct dump_attribute {30struct attribute attr;31ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,32char *buf);33ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,34const char *buf, size_t count);35};36#define to_dump_attr(x) container_of(x, struct dump_attribute, attr)3738static ssize_t dump_id_show(struct dump_obj *dump_obj,39struct dump_attribute *attr,40char *buf)41{42return sprintf(buf, "0x%x\n", dump_obj->id);43}4445static const char* dump_type_to_string(uint32_t type)46{47switch (type) {48case 0x01: return "SP Dump";49case 0x02: return "System/Platform Dump";50case 0x03: return "SMA Dump";51default: return "unknown";52}53}5455static ssize_t dump_type_show(struct dump_obj *dump_obj,56struct dump_attribute *attr,57char *buf)58{5960return sprintf(buf, "0x%x %s\n", dump_obj->type,61dump_type_to_string(dump_obj->type));62}6364static ssize_t dump_ack_show(struct dump_obj *dump_obj,65struct dump_attribute *attr,66char *buf)67{68return sprintf(buf, "ack - acknowledge dump\n");69}7071/*72* Send acknowledgement to OPAL73*/74static int64_t dump_send_ack(uint32_t dump_id)75{76int rc;7778rc = opal_dump_ack(dump_id);79if (rc)80pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",81__func__, dump_id, rc);82return rc;83}8485static ssize_t dump_ack_store(struct dump_obj *dump_obj,86struct dump_attribute *attr,87const char *buf,88size_t count)89{90/*91* Try to self remove this attribute. If we are successful,92* delete the kobject itself.93*/94if (sysfs_remove_file_self(&dump_obj->kobj, &attr->attr)) {95dump_send_ack(dump_obj->id);96kobject_put(&dump_obj->kobj);97}98return count;99}100101/* Attributes of a dump102* The binary attribute of the dump itself is dynamic103* due to the dynamic size of the dump104*/105static struct dump_attribute id_attribute =106__ATTR(id, 0444, dump_id_show, NULL);107static struct dump_attribute type_attribute =108__ATTR(type, 0444, dump_type_show, NULL);109static struct dump_attribute ack_attribute =110__ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);111112static ssize_t init_dump_show(struct dump_obj *dump_obj,113struct dump_attribute *attr,114char *buf)115{116return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");117}118119static int64_t dump_fips_init(uint8_t type)120{121int rc;122123rc = opal_dump_init(type);124if (rc)125pr_warn("%s: Failed to initiate FSP dump (%d)\n",126__func__, rc);127return rc;128}129130static ssize_t init_dump_store(struct dump_obj *dump_obj,131struct dump_attribute *attr,132const char *buf,133size_t count)134{135int rc;136137rc = dump_fips_init(DUMP_TYPE_FSP);138if (rc == OPAL_SUCCESS)139pr_info("%s: Initiated FSP dump\n", __func__);140141return count;142}143144static struct dump_attribute initiate_attribute =145__ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);146147static struct attribute *initiate_attrs[] = {148&initiate_attribute.attr,149NULL,150};151152static const struct attribute_group initiate_attr_group = {153.attrs = initiate_attrs,154};155156static struct kset *dump_kset;157158static ssize_t dump_attr_show(struct kobject *kobj,159struct attribute *attr,160char *buf)161{162struct dump_attribute *attribute;163struct dump_obj *dump;164165attribute = to_dump_attr(attr);166dump = to_dump_obj(kobj);167168if (!attribute->show)169return -EIO;170171return attribute->show(dump, attribute, buf);172}173174static ssize_t dump_attr_store(struct kobject *kobj,175struct attribute *attr,176const char *buf, size_t len)177{178struct dump_attribute *attribute;179struct dump_obj *dump;180181attribute = to_dump_attr(attr);182dump = to_dump_obj(kobj);183184if (!attribute->store)185return -EIO;186187return attribute->store(dump, attribute, buf, len);188}189190static const struct sysfs_ops dump_sysfs_ops = {191.show = dump_attr_show,192.store = dump_attr_store,193};194195static void dump_release(struct kobject *kobj)196{197struct dump_obj *dump;198199dump = to_dump_obj(kobj);200vfree(dump->buffer);201kfree(dump);202}203204static struct attribute *dump_default_attrs[] = {205&id_attribute.attr,206&type_attribute.attr,207&ack_attribute.attr,208NULL,209};210ATTRIBUTE_GROUPS(dump_default);211212static const struct kobj_type dump_ktype = {213.sysfs_ops = &dump_sysfs_ops,214.release = &dump_release,215.default_groups = dump_default_groups,216};217218static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)219{220__be32 id, size, type;221int rc;222223type = cpu_to_be32(0xffffffff);224225rc = opal_dump_info2(&id, &size, &type);226if (rc == OPAL_PARAMETER)227rc = opal_dump_info(&id, &size);228229if (rc) {230pr_warn("%s: Failed to get dump info (%d)\n",231__func__, rc);232return rc;233}234235*dump_id = be32_to_cpu(id);236*dump_size = be32_to_cpu(size);237*dump_type = be32_to_cpu(type);238239return rc;240}241242static int64_t dump_read_data(struct dump_obj *dump)243{244struct opal_sg_list *list;245uint64_t addr;246int64_t rc;247248/* Allocate memory */249dump->buffer = vzalloc(PAGE_ALIGN(dump->size));250if (!dump->buffer) {251pr_err("%s : Failed to allocate memory\n", __func__);252rc = -ENOMEM;253goto out;254}255256/* Generate SG list */257list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);258if (!list) {259rc = -ENOMEM;260goto out;261}262263/* First entry address */264addr = __pa(list);265266/* Fetch data */267rc = OPAL_BUSY_EVENT;268while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {269rc = opal_dump_read(dump->id, addr);270if (rc == OPAL_BUSY_EVENT) {271opal_poll_events(NULL);272msleep(20);273}274}275276if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)277pr_warn("%s: Extract dump failed for ID 0x%x\n",278__func__, dump->id);279280/* Free SG list */281opal_free_sg_list(list);282283out:284return rc;285}286287static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,288const struct bin_attribute *bin_attr,289char *buffer, loff_t pos, size_t count)290{291ssize_t rc;292293struct dump_obj *dump = to_dump_obj(kobj);294295if (!dump->buffer) {296rc = dump_read_data(dump);297298if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {299vfree(dump->buffer);300dump->buffer = NULL;301302return -EIO;303}304if (rc == OPAL_PARTIAL) {305/* On a partial read, we just return EIO306* and rely on userspace to ask us to try307* again.308*/309pr_info("%s: Platform dump partially read. ID = 0x%x\n",310__func__, dump->id);311return -EIO;312}313}314315memcpy(buffer, dump->buffer + pos, count);316317/* You may think we could free the dump buffer now and retrieve318* it again later if needed, but due to current firmware limitation,319* that's not the case. So, once read into userspace once,320* we keep the dump around until it's acknowledged by userspace.321*/322323return count;324}325326static void create_dump_obj(uint32_t id, size_t size, uint32_t type)327{328struct dump_obj *dump;329int rc;330331dump = kzalloc(sizeof(*dump), GFP_KERNEL);332if (!dump)333return;334335dump->kobj.kset = dump_kset;336337kobject_init(&dump->kobj, &dump_ktype);338339sysfs_bin_attr_init(&dump->dump_attr);340341dump->dump_attr.attr.name = "dump";342dump->dump_attr.attr.mode = 0400;343dump->dump_attr.size = size;344dump->dump_attr.read = dump_attr_read;345346dump->id = id;347dump->size = size;348dump->type = type;349350rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);351if (rc) {352kobject_put(&dump->kobj);353return;354}355356/*357* As soon as the sysfs file for this dump is created/activated there is358* a chance the opal_errd daemon (or any userspace) might read and359* acknowledge the dump before kobject_uevent() is called. If that360* happens then there is a potential race between361* dump_ack_store->kobject_put() and kobject_uevent() which leads to a362* use-after-free of a kernfs object resulting in a kernel crash.363*364* To avoid that, we need to take a reference on behalf of the bin file,365* so that our reference remains valid while we call kobject_uevent().366* We then drop our reference before exiting the function, leaving the367* bin file to drop the last reference (if it hasn't already).368*/369370/* Take a reference for the bin file */371kobject_get(&dump->kobj);372rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);373if (rc == 0) {374kobject_uevent(&dump->kobj, KOBJ_ADD);375376pr_info("%s: New platform dump. ID = 0x%x Size %u\n",377__func__, dump->id, dump->size);378} else {379/* Drop reference count taken for bin file */380kobject_put(&dump->kobj);381}382383/* Drop our reference */384kobject_put(&dump->kobj);385return;386}387388static irqreturn_t process_dump(int irq, void *data)389{390int rc;391uint32_t dump_id, dump_size, dump_type;392char name[22];393struct kobject *kobj;394395rc = dump_read_info(&dump_id, &dump_size, &dump_type);396if (rc != OPAL_SUCCESS)397return IRQ_HANDLED;398399sprintf(name, "0x%x-0x%x", dump_type, dump_id);400401/* we may get notified twice, let's handle402* that gracefully and not create two conflicting403* entries.404*/405kobj = kset_find_obj(dump_kset, name);406if (kobj) {407/* Drop reference added by kset_find_obj() */408kobject_put(kobj);409return IRQ_HANDLED;410}411412create_dump_obj(dump_id, dump_size, dump_type);413414return IRQ_HANDLED;415}416417void __init opal_platform_dump_init(void)418{419int rc;420int dump_irq;421422/* Dump not supported by firmware */423if (!opal_check_token(OPAL_DUMP_READ))424return;425426dump_kset = kset_create_and_add("dump", NULL, opal_kobj);427if (!dump_kset) {428pr_warn("%s: Failed to create dump kset\n", __func__);429return;430}431432rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);433if (rc) {434pr_warn("%s: Failed to create initiate dump attr group\n",435__func__);436kobject_put(&dump_kset->kobj);437return;438}439440dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));441if (!dump_irq) {442pr_err("%s: Can't register OPAL event irq (%d)\n",443__func__, dump_irq);444return;445}446447rc = request_threaded_irq(dump_irq, NULL, process_dump,448IRQF_TRIGGER_HIGH | IRQF_ONESHOT,449"opal-dump", NULL);450if (rc) {451pr_err("%s: Can't request OPAL event irq (%d)\n",452__func__, rc);453return;454}455456if (opal_check_token(OPAL_DUMP_RESEND))457opal_dump_resend_notification();458}459460461