1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerNV OPAL Dump Interface
5 * Copyright 2013,2014 IBM Corp.
8 #include <linux/kobject.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <linux/pagemap.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
18 #define DUMP_TYPE_FSP 0x01
22 struct bin_attribute dump_attr;
23 uint32_t id; /* becomes object name */
28 #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
30 struct dump_attribute {
31 struct attribute attr;
32 ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
34 ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
35 const char *buf, size_t count);
37 #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
39 static ssize_t dump_id_show(struct dump_obj *dump_obj,
40 struct dump_attribute *attr,
43 return sprintf(buf, "0x%x\n", dump_obj->id);
46 static const char* dump_type_to_string(uint32_t type)
49 case 0x01: return "SP Dump";
50 case 0x02: return "System/Platform Dump";
51 case 0x03: return "SMA Dump";
52 default: return "unknown";
56 static ssize_t dump_type_show(struct dump_obj *dump_obj,
57 struct dump_attribute *attr,
61 return sprintf(buf, "0x%x %s\n", dump_obj->type,
62 dump_type_to_string(dump_obj->type));
65 static ssize_t dump_ack_show(struct dump_obj *dump_obj,
66 struct dump_attribute *attr,
69 return sprintf(buf, "ack - acknowledge dump\n");
73 * Send acknowledgement to OPAL
75 static int64_t dump_send_ack(uint32_t dump_id)
79 rc = opal_dump_ack(dump_id);
81 pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
82 __func__, dump_id, rc);
86 static ssize_t dump_ack_store(struct dump_obj *dump_obj,
87 struct dump_attribute *attr,
92 * Try to self remove this attribute. If we are successful,
93 * delete the kobject itself.
95 if (sysfs_remove_file_self(&dump_obj->kobj, &attr->attr)) {
96 dump_send_ack(dump_obj->id);
97 kobject_put(&dump_obj->kobj);
102 /* Attributes of a dump
103 * The binary attribute of the dump itself is dynamic
104 * due to the dynamic size of the dump
106 static struct dump_attribute id_attribute =
107 __ATTR(id, 0444, dump_id_show, NULL);
108 static struct dump_attribute type_attribute =
109 __ATTR(type, 0444, dump_type_show, NULL);
110 static struct dump_attribute ack_attribute =
111 __ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
113 static ssize_t init_dump_show(struct dump_obj *dump_obj,
114 struct dump_attribute *attr,
117 return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
120 static int64_t dump_fips_init(uint8_t type)
124 rc = opal_dump_init(type);
126 pr_warn("%s: Failed to initiate FSP dump (%d)\n",
131 static ssize_t init_dump_store(struct dump_obj *dump_obj,
132 struct dump_attribute *attr,
138 rc = dump_fips_init(DUMP_TYPE_FSP);
139 if (rc == OPAL_SUCCESS)
140 pr_info("%s: Initiated FSP dump\n", __func__);
145 static struct dump_attribute initiate_attribute =
146 __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
148 static struct attribute *initiate_attrs[] = {
149 &initiate_attribute.attr,
153 static const struct attribute_group initiate_attr_group = {
154 .attrs = initiate_attrs,
157 static struct kset *dump_kset;
159 static ssize_t dump_attr_show(struct kobject *kobj,
160 struct attribute *attr,
163 struct dump_attribute *attribute;
164 struct dump_obj *dump;
166 attribute = to_dump_attr(attr);
167 dump = to_dump_obj(kobj);
169 if (!attribute->show)
172 return attribute->show(dump, attribute, buf);
175 static ssize_t dump_attr_store(struct kobject *kobj,
176 struct attribute *attr,
177 const char *buf, size_t len)
179 struct dump_attribute *attribute;
180 struct dump_obj *dump;
182 attribute = to_dump_attr(attr);
183 dump = to_dump_obj(kobj);
185 if (!attribute->store)
188 return attribute->store(dump, attribute, buf, len);
191 static const struct sysfs_ops dump_sysfs_ops = {
192 .show = dump_attr_show,
193 .store = dump_attr_store,
196 static void dump_release(struct kobject *kobj)
198 struct dump_obj *dump;
200 dump = to_dump_obj(kobj);
205 static struct attribute *dump_default_attrs[] = {
207 &type_attribute.attr,
211 ATTRIBUTE_GROUPS(dump_default);
213 static struct kobj_type dump_ktype = {
214 .sysfs_ops = &dump_sysfs_ops,
215 .release = &dump_release,
216 .default_groups = dump_default_groups,
219 static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
221 __be32 id, size, type;
224 type = cpu_to_be32(0xffffffff);
226 rc = opal_dump_info2(&id, &size, &type);
227 if (rc == OPAL_PARAMETER)
228 rc = opal_dump_info(&id, &size);
231 pr_warn("%s: Failed to get dump info (%d)\n",
236 *dump_id = be32_to_cpu(id);
237 *dump_size = be32_to_cpu(size);
238 *dump_type = be32_to_cpu(type);
243 static int64_t dump_read_data(struct dump_obj *dump)
245 struct opal_sg_list *list;
249 /* Allocate memory */
250 dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
252 pr_err("%s : Failed to allocate memory\n", __func__);
257 /* Generate SG list */
258 list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
264 /* First entry address */
268 rc = OPAL_BUSY_EVENT;
269 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
270 rc = opal_dump_read(dump->id, addr);
271 if (rc == OPAL_BUSY_EVENT) {
272 opal_poll_events(NULL);
277 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
278 pr_warn("%s: Extract dump failed for ID 0x%x\n",
282 opal_free_sg_list(list);
288 static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
289 struct bin_attribute *bin_attr,
290 char *buffer, loff_t pos, size_t count)
294 struct dump_obj *dump = to_dump_obj(kobj);
297 rc = dump_read_data(dump);
299 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
305 if (rc == OPAL_PARTIAL) {
306 /* On a partial read, we just return EIO
307 * and rely on userspace to ask us to try
310 pr_info("%s: Platform dump partially read. ID = 0x%x\n",
316 memcpy(buffer, dump->buffer + pos, count);
318 /* You may think we could free the dump buffer now and retrieve
319 * it again later if needed, but due to current firmware limitation,
320 * that's not the case. So, once read into userspace once,
321 * we keep the dump around until it's acknowledged by userspace.
327 static void create_dump_obj(uint32_t id, size_t size, uint32_t type)
329 struct dump_obj *dump;
332 dump = kzalloc(sizeof(*dump), GFP_KERNEL);
336 dump->kobj.kset = dump_kset;
338 kobject_init(&dump->kobj, &dump_ktype);
340 sysfs_bin_attr_init(&dump->dump_attr);
342 dump->dump_attr.attr.name = "dump";
343 dump->dump_attr.attr.mode = 0400;
344 dump->dump_attr.size = size;
345 dump->dump_attr.read = dump_attr_read;
351 rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
353 kobject_put(&dump->kobj);
358 * As soon as the sysfs file for this dump is created/activated there is
359 * a chance the opal_errd daemon (or any userspace) might read and
360 * acknowledge the dump before kobject_uevent() is called. If that
361 * happens then there is a potential race between
362 * dump_ack_store->kobject_put() and kobject_uevent() which leads to a
363 * use-after-free of a kernfs object resulting in a kernel crash.
365 * To avoid that, we need to take a reference on behalf of the bin file,
366 * so that our reference remains valid while we call kobject_uevent().
367 * We then drop our reference before exiting the function, leaving the
368 * bin file to drop the last reference (if it hasn't already).
371 /* Take a reference for the bin file */
372 kobject_get(&dump->kobj);
373 rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
375 kobject_uevent(&dump->kobj, KOBJ_ADD);
377 pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
378 __func__, dump->id, dump->size);
380 /* Drop reference count taken for bin file */
381 kobject_put(&dump->kobj);
384 /* Drop our reference */
385 kobject_put(&dump->kobj);
389 static irqreturn_t process_dump(int irq, void *data)
392 uint32_t dump_id, dump_size, dump_type;
394 struct kobject *kobj;
396 rc = dump_read_info(&dump_id, &dump_size, &dump_type);
397 if (rc != OPAL_SUCCESS)
400 sprintf(name, "0x%x-0x%x", dump_type, dump_id);
402 /* we may get notified twice, let's handle
403 * that gracefully and not create two conflicting
406 kobj = kset_find_obj(dump_kset, name);
408 /* Drop reference added by kset_find_obj() */
413 create_dump_obj(dump_id, dump_size, dump_type);
418 void __init opal_platform_dump_init(void)
423 /* Dump not supported by firmware */
424 if (!opal_check_token(OPAL_DUMP_READ))
427 dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
429 pr_warn("%s: Failed to create dump kset\n", __func__);
433 rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
435 pr_warn("%s: Failed to create initiate dump attr group\n",
437 kobject_put(&dump_kset->kobj);
441 dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
443 pr_err("%s: Can't register OPAL event irq (%d)\n",
448 rc = request_threaded_irq(dump_irq, NULL, process_dump,
449 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
452 pr_err("%s: Can't request OPAL event irq (%d)\n",
457 if (opal_check_token(OPAL_DUMP_RESEND))
458 opal_dump_resend_notification();