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 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
,
212 static struct kobj_type dump_ktype
= {
213 .sysfs_ops
= &dump_sysfs_ops
,
214 .release
= &dump_release
,
215 .default_attrs
= dump_default_attrs
,
218 static int64_t dump_read_info(uint32_t *dump_id
, uint32_t *dump_size
, uint32_t *dump_type
)
220 __be32 id
, size
, type
;
223 type
= cpu_to_be32(0xffffffff);
225 rc
= opal_dump_info2(&id
, &size
, &type
);
226 if (rc
== OPAL_PARAMETER
)
227 rc
= opal_dump_info(&id
, &size
);
230 pr_warn("%s: Failed to get dump info (%d)\n",
235 *dump_id
= be32_to_cpu(id
);
236 *dump_size
= be32_to_cpu(size
);
237 *dump_type
= be32_to_cpu(type
);
242 static int64_t dump_read_data(struct dump_obj
*dump
)
244 struct opal_sg_list
*list
;
248 /* Allocate memory */
249 dump
->buffer
= vzalloc(PAGE_ALIGN(dump
->size
));
251 pr_err("%s : Failed to allocate memory\n", __func__
);
256 /* Generate SG list */
257 list
= opal_vmalloc_to_sg_list(dump
->buffer
, dump
->size
);
263 /* First entry address */
267 rc
= OPAL_BUSY_EVENT
;
268 while (rc
== OPAL_BUSY
|| rc
== OPAL_BUSY_EVENT
) {
269 rc
= opal_dump_read(dump
->id
, addr
);
270 if (rc
== OPAL_BUSY_EVENT
) {
271 opal_poll_events(NULL
);
276 if (rc
!= OPAL_SUCCESS
&& rc
!= OPAL_PARTIAL
)
277 pr_warn("%s: Extract dump failed for ID 0x%x\n",
281 opal_free_sg_list(list
);
287 static ssize_t
dump_attr_read(struct file
*filep
, struct kobject
*kobj
,
288 struct bin_attribute
*bin_attr
,
289 char *buffer
, loff_t pos
, size_t count
)
293 struct dump_obj
*dump
= to_dump_obj(kobj
);
296 rc
= dump_read_data(dump
);
298 if (rc
!= OPAL_SUCCESS
&& rc
!= OPAL_PARTIAL
) {
304 if (rc
== OPAL_PARTIAL
) {
305 /* On a partial read, we just return EIO
306 * and rely on userspace to ask us to try
309 pr_info("%s: Platform dump partially read. ID = 0x%x\n",
315 memcpy(buffer
, dump
->buffer
+ pos
, count
);
317 /* You may think we could free the dump buffer now and retrieve
318 * 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.
326 static void create_dump_obj(uint32_t id
, size_t size
, uint32_t type
)
328 struct dump_obj
*dump
;
331 dump
= kzalloc(sizeof(*dump
), GFP_KERNEL
);
335 dump
->kobj
.kset
= dump_kset
;
337 kobject_init(&dump
->kobj
, &dump_ktype
);
339 sysfs_bin_attr_init(&dump
->dump_attr
);
341 dump
->dump_attr
.attr
.name
= "dump";
342 dump
->dump_attr
.attr
.mode
= 0400;
343 dump
->dump_attr
.size
= size
;
344 dump
->dump_attr
.read
= dump_attr_read
;
350 rc
= kobject_add(&dump
->kobj
, NULL
, "0x%x-0x%x", type
, id
);
352 kobject_put(&dump
->kobj
);
357 * As soon as the sysfs file for this dump is created/activated there is
358 * a chance the opal_errd daemon (or any userspace) might read and
359 * acknowledge the dump before kobject_uevent() is called. If that
360 * happens then there is a potential race between
361 * dump_ack_store->kobject_put() and kobject_uevent() which leads to a
362 * use-after-free of a kernfs object resulting in a kernel crash.
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 the
367 * bin file to drop the last reference (if it hasn't already).
370 /* Take a reference for the bin file */
371 kobject_get(&dump
->kobj
);
372 rc
= sysfs_create_bin_file(&dump
->kobj
, &dump
->dump_attr
);
374 kobject_uevent(&dump
->kobj
, KOBJ_ADD
);
376 pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
377 __func__
, dump
->id
, dump
->size
);
379 /* Drop reference count taken for bin file */
380 kobject_put(&dump
->kobj
);
383 /* Drop our reference */
384 kobject_put(&dump
->kobj
);
388 static irqreturn_t
process_dump(int irq
, void *data
)
391 uint32_t dump_id
, dump_size
, dump_type
;
393 struct kobject
*kobj
;
395 rc
= dump_read_info(&dump_id
, &dump_size
, &dump_type
);
396 if (rc
!= OPAL_SUCCESS
)
399 sprintf(name
, "0x%x-0x%x", dump_type
, dump_id
);
401 /* we may get notified twice, let's handle
402 * that gracefully and not create two conflicting
405 kobj
= kset_find_obj(dump_kset
, name
);
407 /* Drop reference added by kset_find_obj() */
412 create_dump_obj(dump_id
, dump_size
, dump_type
);
417 void __init
opal_platform_dump_init(void)
422 /* ELOG not supported by firmware */
423 if (!opal_check_token(OPAL_DUMP_READ
))
426 dump_kset
= kset_create_and_add("dump", NULL
, opal_kobj
);
428 pr_warn("%s: Failed to create dump kset\n", __func__
);
432 rc
= sysfs_create_group(&dump_kset
->kobj
, &initiate_attr_group
);
434 pr_warn("%s: Failed to create initiate dump attr group\n",
436 kobject_put(&dump_kset
->kobj
);
440 dump_irq
= opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL
));
442 pr_err("%s: Can't register OPAL event irq (%d)\n",
447 rc
= request_threaded_irq(dump_irq
, NULL
, process_dump
,
448 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
451 pr_err("%s: Can't request OPAL event irq (%d)\n",
456 if (opal_check_token(OPAL_DUMP_RESEND
))
457 opal_dump_resend_notification();