12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c7e64b9cSStewart Smith /*
3c7e64b9cSStewart Smith  * PowerNV OPAL Dump Interface
4c7e64b9cSStewart Smith  *
5c7e64b9cSStewart Smith  * Copyright 2013,2014 IBM Corp.
6c7e64b9cSStewart Smith  */
7c7e64b9cSStewart Smith 
8c7e64b9cSStewart Smith #include <linux/kobject.h>
9c7e64b9cSStewart Smith #include <linux/mm.h>
10c7e64b9cSStewart Smith #include <linux/slab.h>
11c7e64b9cSStewart Smith #include <linux/vmalloc.h>
12c7e64b9cSStewart Smith #include <linux/pagemap.h>
13c7e64b9cSStewart Smith #include <linux/delay.h>
148034f715SAlistair Popple #include <linux/interrupt.h>
15c7e64b9cSStewart Smith 
16c7e64b9cSStewart Smith #include <asm/opal.h>
17c7e64b9cSStewart Smith 
18c7e64b9cSStewart Smith #define DUMP_TYPE_FSP	0x01
19c7e64b9cSStewart Smith 
20c7e64b9cSStewart Smith struct dump_obj {
21c7e64b9cSStewart Smith 	struct kobject  kobj;
22c7e64b9cSStewart Smith 	struct bin_attribute dump_attr;
23c7e64b9cSStewart Smith 	uint32_t	id;  /* becomes object name */
24c7e64b9cSStewart Smith 	uint32_t	type;
25c7e64b9cSStewart Smith 	uint32_t	size;
26c7e64b9cSStewart Smith 	char		*buffer;
27c7e64b9cSStewart Smith };
28c7e64b9cSStewart Smith #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
29c7e64b9cSStewart Smith 
30c7e64b9cSStewart Smith struct dump_attribute {
31c7e64b9cSStewart Smith 	struct attribute attr;
32c7e64b9cSStewart Smith 	ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
33c7e64b9cSStewart Smith 			char *buf);
34c7e64b9cSStewart Smith 	ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
35c7e64b9cSStewart Smith 			 const char *buf, size_t count);
36c7e64b9cSStewart Smith };
37c7e64b9cSStewart Smith #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
38c7e64b9cSStewart Smith 
39c7e64b9cSStewart Smith static ssize_t dump_id_show(struct dump_obj *dump_obj,
40c7e64b9cSStewart Smith 			    struct dump_attribute *attr,
41c7e64b9cSStewart Smith 			    char *buf)
42c7e64b9cSStewart Smith {
43c7e64b9cSStewart Smith 	return sprintf(buf, "0x%x\n", dump_obj->id);
44c7e64b9cSStewart Smith }
45c7e64b9cSStewart Smith 
46c7e64b9cSStewart Smith static const char* dump_type_to_string(uint32_t type)
47c7e64b9cSStewart Smith {
48c7e64b9cSStewart Smith 	switch (type) {
49c7e64b9cSStewart Smith 	case 0x01: return "SP Dump";
50c7e64b9cSStewart Smith 	case 0x02: return "System/Platform Dump";
51c7e64b9cSStewart Smith 	case 0x03: return "SMA Dump";
52c7e64b9cSStewart Smith 	default: return "unknown";
53c7e64b9cSStewart Smith 	}
54c7e64b9cSStewart Smith }
55c7e64b9cSStewart Smith 
56c7e64b9cSStewart Smith static ssize_t dump_type_show(struct dump_obj *dump_obj,
57c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
58c7e64b9cSStewart Smith 			      char *buf)
59c7e64b9cSStewart Smith {
60c7e64b9cSStewart Smith 
61c7e64b9cSStewart Smith 	return sprintf(buf, "0x%x %s\n", dump_obj->type,
62c7e64b9cSStewart Smith 		       dump_type_to_string(dump_obj->type));
63c7e64b9cSStewart Smith }
64c7e64b9cSStewart Smith 
65c7e64b9cSStewart Smith static ssize_t dump_ack_show(struct dump_obj *dump_obj,
66c7e64b9cSStewart Smith 			     struct dump_attribute *attr,
67c7e64b9cSStewart Smith 			     char *buf)
68c7e64b9cSStewart Smith {
69c7e64b9cSStewart Smith 	return sprintf(buf, "ack - acknowledge dump\n");
70c7e64b9cSStewart Smith }
71c7e64b9cSStewart Smith 
72c7e64b9cSStewart Smith /*
73c7e64b9cSStewart Smith  * Send acknowledgement to OPAL
74c7e64b9cSStewart Smith  */
75c7e64b9cSStewart Smith static int64_t dump_send_ack(uint32_t dump_id)
76c7e64b9cSStewart Smith {
77c7e64b9cSStewart Smith 	int rc;
78c7e64b9cSStewart Smith 
79c7e64b9cSStewart Smith 	rc = opal_dump_ack(dump_id);
80c7e64b9cSStewart Smith 	if (rc)
81c7e64b9cSStewart Smith 		pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
82c7e64b9cSStewart Smith 			__func__, dump_id, rc);
83c7e64b9cSStewart Smith 	return rc;
84c7e64b9cSStewart Smith }
85c7e64b9cSStewart Smith 
86c7e64b9cSStewart Smith static ssize_t dump_ack_store(struct dump_obj *dump_obj,
87c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
88c7e64b9cSStewart Smith 			      const char *buf,
89c7e64b9cSStewart Smith 			      size_t count)
90c7e64b9cSStewart Smith {
91c7e64b9cSStewart Smith 	dump_send_ack(dump_obj->id);
92cc4f265aSStewart Smith 	sysfs_remove_file_self(&dump_obj->kobj, &attr->attr);
93cc4f265aSStewart Smith 	kobject_put(&dump_obj->kobj);
94c7e64b9cSStewart Smith 	return count;
95c7e64b9cSStewart Smith }
96c7e64b9cSStewart Smith 
97c7e64b9cSStewart Smith /* Attributes of a dump
98c7e64b9cSStewart Smith  * The binary attribute of the dump itself is dynamic
99c7e64b9cSStewart Smith  * due to the dynamic size of the dump
100c7e64b9cSStewart Smith  */
101c7e64b9cSStewart Smith static struct dump_attribute id_attribute =
10257ad583fSRussell Currey 	__ATTR(id, 0444, dump_id_show, NULL);
103c7e64b9cSStewart Smith static struct dump_attribute type_attribute =
10457ad583fSRussell Currey 	__ATTR(type, 0444, dump_type_show, NULL);
105c7e64b9cSStewart Smith static struct dump_attribute ack_attribute =
106c7e64b9cSStewart Smith 	__ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
107c7e64b9cSStewart Smith 
108c7e64b9cSStewart Smith static ssize_t init_dump_show(struct dump_obj *dump_obj,
109c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
110c7e64b9cSStewart Smith 			      char *buf)
111c7e64b9cSStewart Smith {
112cdd91b89SVasant Hegde 	return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
113c7e64b9cSStewart Smith }
114c7e64b9cSStewart Smith 
115c7e64b9cSStewart Smith static int64_t dump_fips_init(uint8_t type)
116c7e64b9cSStewart Smith {
117c7e64b9cSStewart Smith 	int rc;
118c7e64b9cSStewart Smith 
119c7e64b9cSStewart Smith 	rc = opal_dump_init(type);
120c7e64b9cSStewart Smith 	if (rc)
121cdd91b89SVasant Hegde 		pr_warn("%s: Failed to initiate FSP dump (%d)\n",
122c7e64b9cSStewart Smith 			__func__, rc);
123c7e64b9cSStewart Smith 	return rc;
124c7e64b9cSStewart Smith }
125c7e64b9cSStewart Smith 
126c7e64b9cSStewart Smith static ssize_t init_dump_store(struct dump_obj *dump_obj,
127c7e64b9cSStewart Smith 			       struct dump_attribute *attr,
128c7e64b9cSStewart Smith 			       const char *buf,
129c7e64b9cSStewart Smith 			       size_t count)
130c7e64b9cSStewart Smith {
131cdd91b89SVasant Hegde 	int rc;
132cdd91b89SVasant Hegde 
133cdd91b89SVasant Hegde 	rc = dump_fips_init(DUMP_TYPE_FSP);
134cdd91b89SVasant Hegde 	if (rc == OPAL_SUCCESS)
135c7e64b9cSStewart Smith 		pr_info("%s: Initiated FSP dump\n", __func__);
136cdd91b89SVasant Hegde 
137c7e64b9cSStewart Smith 	return count;
138c7e64b9cSStewart Smith }
139c7e64b9cSStewart Smith 
140c7e64b9cSStewart Smith static struct dump_attribute initiate_attribute =
141c7e64b9cSStewart Smith 	__ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
142c7e64b9cSStewart Smith 
143c7e64b9cSStewart Smith static struct attribute *initiate_attrs[] = {
144c7e64b9cSStewart Smith 	&initiate_attribute.attr,
145c7e64b9cSStewart Smith 	NULL,
146c7e64b9cSStewart Smith };
147c7e64b9cSStewart Smith 
148c7e64b9cSStewart Smith static struct attribute_group initiate_attr_group = {
149c7e64b9cSStewart Smith 	.attrs = initiate_attrs,
150c7e64b9cSStewart Smith };
151c7e64b9cSStewart Smith 
152c7e64b9cSStewart Smith static struct kset *dump_kset;
153c7e64b9cSStewart Smith 
154c7e64b9cSStewart Smith static ssize_t dump_attr_show(struct kobject *kobj,
155c7e64b9cSStewart Smith 			      struct attribute *attr,
156c7e64b9cSStewart Smith 			      char *buf)
157c7e64b9cSStewart Smith {
158c7e64b9cSStewart Smith 	struct dump_attribute *attribute;
159c7e64b9cSStewart Smith 	struct dump_obj *dump;
160c7e64b9cSStewart Smith 
161c7e64b9cSStewart Smith 	attribute = to_dump_attr(attr);
162c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
163c7e64b9cSStewart Smith 
164c7e64b9cSStewart Smith 	if (!attribute->show)
165c7e64b9cSStewart Smith 		return -EIO;
166c7e64b9cSStewart Smith 
167c7e64b9cSStewart Smith 	return attribute->show(dump, attribute, buf);
168c7e64b9cSStewart Smith }
169c7e64b9cSStewart Smith 
170c7e64b9cSStewart Smith static ssize_t dump_attr_store(struct kobject *kobj,
171c7e64b9cSStewart Smith 			       struct attribute *attr,
172c7e64b9cSStewart Smith 			       const char *buf, size_t len)
173c7e64b9cSStewart Smith {
174c7e64b9cSStewart Smith 	struct dump_attribute *attribute;
175c7e64b9cSStewart Smith 	struct dump_obj *dump;
176c7e64b9cSStewart Smith 
177c7e64b9cSStewart Smith 	attribute = to_dump_attr(attr);
178c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
179c7e64b9cSStewart Smith 
180c7e64b9cSStewart Smith 	if (!attribute->store)
181c7e64b9cSStewart Smith 		return -EIO;
182c7e64b9cSStewart Smith 
183c7e64b9cSStewart Smith 	return attribute->store(dump, attribute, buf, len);
184c7e64b9cSStewart Smith }
185c7e64b9cSStewart Smith 
186c7e64b9cSStewart Smith static const struct sysfs_ops dump_sysfs_ops = {
187c7e64b9cSStewart Smith 	.show = dump_attr_show,
188c7e64b9cSStewart Smith 	.store = dump_attr_store,
189c7e64b9cSStewart Smith };
190c7e64b9cSStewart Smith 
191c7e64b9cSStewart Smith static void dump_release(struct kobject *kobj)
192c7e64b9cSStewart Smith {
193c7e64b9cSStewart Smith 	struct dump_obj *dump;
194c7e64b9cSStewart Smith 
195c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
196c7e64b9cSStewart Smith 	vfree(dump->buffer);
197c7e64b9cSStewart Smith 	kfree(dump);
198c7e64b9cSStewart Smith }
199c7e64b9cSStewart Smith 
200c7e64b9cSStewart Smith static struct attribute *dump_default_attrs[] = {
201c7e64b9cSStewart Smith 	&id_attribute.attr,
202c7e64b9cSStewart Smith 	&type_attribute.attr,
203c7e64b9cSStewart Smith 	&ack_attribute.attr,
204c7e64b9cSStewart Smith 	NULL,
205c7e64b9cSStewart Smith };
206c7e64b9cSStewart Smith 
207c7e64b9cSStewart Smith static struct kobj_type dump_ktype = {
208c7e64b9cSStewart Smith 	.sysfs_ops = &dump_sysfs_ops,
209c7e64b9cSStewart Smith 	.release = &dump_release,
210c7e64b9cSStewart Smith 	.default_attrs = dump_default_attrs,
211c7e64b9cSStewart Smith };
212c7e64b9cSStewart Smith 
2132d6b63bbSAnton Blanchard static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
214c7e64b9cSStewart Smith {
2152d6b63bbSAnton Blanchard 	__be32 id, size, type;
216c7e64b9cSStewart Smith 	int rc;
217c7e64b9cSStewart Smith 
2182d6b63bbSAnton Blanchard 	type = cpu_to_be32(0xffffffff);
219c7e64b9cSStewart Smith 
2202d6b63bbSAnton Blanchard 	rc = opal_dump_info2(&id, &size, &type);
221c7e64b9cSStewart Smith 	if (rc == OPAL_PARAMETER)
2222d6b63bbSAnton Blanchard 		rc = opal_dump_info(&id, &size);
2232d6b63bbSAnton Blanchard 
224a5bbe8fdSMukesh Ojha 	if (rc) {
225a5bbe8fdSMukesh Ojha 		pr_warn("%s: Failed to get dump info (%d)\n",
226a5bbe8fdSMukesh Ojha 			__func__, rc);
227a5bbe8fdSMukesh Ojha 		return rc;
228a5bbe8fdSMukesh Ojha 	}
229a5bbe8fdSMukesh Ojha 
2302d6b63bbSAnton Blanchard 	*dump_id = be32_to_cpu(id);
2312d6b63bbSAnton Blanchard 	*dump_size = be32_to_cpu(size);
2322d6b63bbSAnton Blanchard 	*dump_type = be32_to_cpu(type);
233c7e64b9cSStewart Smith 
234c7e64b9cSStewart Smith 	return rc;
235c7e64b9cSStewart Smith }
236c7e64b9cSStewart Smith 
237c7e64b9cSStewart Smith static int64_t dump_read_data(struct dump_obj *dump)
238c7e64b9cSStewart Smith {
239c7e64b9cSStewart Smith 	struct opal_sg_list *list;
240c7e64b9cSStewart Smith 	uint64_t addr;
241c7e64b9cSStewart Smith 	int64_t rc;
242c7e64b9cSStewart Smith 
243c7e64b9cSStewart Smith 	/* Allocate memory */
244c7e64b9cSStewart Smith 	dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
245c7e64b9cSStewart Smith 	if (!dump->buffer) {
246c7e64b9cSStewart Smith 		pr_err("%s : Failed to allocate memory\n", __func__);
247c7e64b9cSStewart Smith 		rc = -ENOMEM;
248c7e64b9cSStewart Smith 		goto out;
249c7e64b9cSStewart Smith 	}
250c7e64b9cSStewart Smith 
251c7e64b9cSStewart Smith 	/* Generate SG list */
2523441f04bSAnton Blanchard 	list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
253c7e64b9cSStewart Smith 	if (!list) {
254c7e64b9cSStewart Smith 		rc = -ENOMEM;
255c7e64b9cSStewart Smith 		goto out;
256c7e64b9cSStewart Smith 	}
257c7e64b9cSStewart Smith 
258c7e64b9cSStewart Smith 	/* First entry address */
259c7e64b9cSStewart Smith 	addr = __pa(list);
260c7e64b9cSStewart Smith 
261c7e64b9cSStewart Smith 	/* Fetch data */
262c7e64b9cSStewart Smith 	rc = OPAL_BUSY_EVENT;
263c7e64b9cSStewart Smith 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
264c7e64b9cSStewart Smith 		rc = opal_dump_read(dump->id, addr);
265c7e64b9cSStewart Smith 		if (rc == OPAL_BUSY_EVENT) {
266c7e64b9cSStewart Smith 			opal_poll_events(NULL);
267c7e64b9cSStewart Smith 			msleep(20);
268c7e64b9cSStewart Smith 		}
269c7e64b9cSStewart Smith 	}
270c7e64b9cSStewart Smith 
271c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
272c7e64b9cSStewart Smith 		pr_warn("%s: Extract dump failed for ID 0x%x\n",
273c7e64b9cSStewart Smith 			__func__, dump->id);
274c7e64b9cSStewart Smith 
275c7e64b9cSStewart Smith 	/* Free SG list */
2763441f04bSAnton Blanchard 	opal_free_sg_list(list);
277c7e64b9cSStewart Smith 
278c7e64b9cSStewart Smith out:
279c7e64b9cSStewart Smith 	return rc;
280c7e64b9cSStewart Smith }
281c7e64b9cSStewart Smith 
282c7e64b9cSStewart Smith static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
283c7e64b9cSStewart Smith 			      struct bin_attribute *bin_attr,
284c7e64b9cSStewart Smith 			      char *buffer, loff_t pos, size_t count)
285c7e64b9cSStewart Smith {
286c7e64b9cSStewart Smith 	ssize_t rc;
287c7e64b9cSStewart Smith 
288c7e64b9cSStewart Smith 	struct dump_obj *dump = to_dump_obj(kobj);
289c7e64b9cSStewart Smith 
290c7e64b9cSStewart Smith 	if (!dump->buffer) {
291c7e64b9cSStewart Smith 		rc = dump_read_data(dump);
292c7e64b9cSStewart Smith 
293c7e64b9cSStewart Smith 		if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
294c7e64b9cSStewart Smith 			vfree(dump->buffer);
295c7e64b9cSStewart Smith 			dump->buffer = NULL;
296c7e64b9cSStewart Smith 
297c7e64b9cSStewart Smith 			return -EIO;
298c7e64b9cSStewart Smith 		}
299c7e64b9cSStewart Smith 		if (rc == OPAL_PARTIAL) {
300c7e64b9cSStewart Smith 			/* On a partial read, we just return EIO
301c7e64b9cSStewart Smith 			 * and rely on userspace to ask us to try
302c7e64b9cSStewart Smith 			 * again.
303c7e64b9cSStewart Smith 			 */
304c7e64b9cSStewart Smith 			pr_info("%s: Platform dump partially read. ID = 0x%x\n",
305c7e64b9cSStewart Smith 				__func__, dump->id);
306c7e64b9cSStewart Smith 			return -EIO;
307c7e64b9cSStewart Smith 		}
308c7e64b9cSStewart Smith 	}
309c7e64b9cSStewart Smith 
310c7e64b9cSStewart Smith 	memcpy(buffer, dump->buffer + pos, count);
311c7e64b9cSStewart Smith 
312c7e64b9cSStewart Smith 	/* You may think we could free the dump buffer now and retrieve
313c7e64b9cSStewart Smith 	 * it again later if needed, but due to current firmware limitation,
314c7e64b9cSStewart Smith 	 * that's not the case. So, once read into userspace once,
315c7e64b9cSStewart Smith 	 * we keep the dump around until it's acknowledged by userspace.
316c7e64b9cSStewart Smith 	 */
317c7e64b9cSStewart Smith 
318c7e64b9cSStewart Smith 	return count;
319c7e64b9cSStewart Smith }
320c7e64b9cSStewart Smith 
321c7e64b9cSStewart Smith static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
322c7e64b9cSStewart Smith 					uint32_t type)
323c7e64b9cSStewart Smith {
324c7e64b9cSStewart Smith 	struct dump_obj *dump;
325c7e64b9cSStewart Smith 	int rc;
326c7e64b9cSStewart Smith 
327c7e64b9cSStewart Smith 	dump = kzalloc(sizeof(*dump), GFP_KERNEL);
328c7e64b9cSStewart Smith 	if (!dump)
329c7e64b9cSStewart Smith 		return NULL;
330c7e64b9cSStewart Smith 
331c7e64b9cSStewart Smith 	dump->kobj.kset = dump_kset;
332c7e64b9cSStewart Smith 
333c7e64b9cSStewart Smith 	kobject_init(&dump->kobj, &dump_ktype);
334c7e64b9cSStewart Smith 
335c7e64b9cSStewart Smith 	sysfs_bin_attr_init(&dump->dump_attr);
336c7e64b9cSStewart Smith 
337c7e64b9cSStewart Smith 	dump->dump_attr.attr.name = "dump";
338c7e64b9cSStewart Smith 	dump->dump_attr.attr.mode = 0400;
339c7e64b9cSStewart Smith 	dump->dump_attr.size = size;
340c7e64b9cSStewart Smith 	dump->dump_attr.read = dump_attr_read;
341c7e64b9cSStewart Smith 
342c7e64b9cSStewart Smith 	dump->id = id;
343c7e64b9cSStewart Smith 	dump->size = size;
344c7e64b9cSStewart Smith 	dump->type = type;
345c7e64b9cSStewart Smith 
346c7e64b9cSStewart Smith 	rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
347c7e64b9cSStewart Smith 	if (rc) {
348c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
349c7e64b9cSStewart Smith 		return NULL;
350c7e64b9cSStewart Smith 	}
351c7e64b9cSStewart Smith 
352c7e64b9cSStewart Smith 	rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
353c7e64b9cSStewart Smith 	if (rc) {
354c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
355c7e64b9cSStewart Smith 		return NULL;
356c7e64b9cSStewart Smith 	}
357c7e64b9cSStewart Smith 
358c7e64b9cSStewart Smith 	pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
359c7e64b9cSStewart Smith 		__func__, dump->id, dump->size);
360c7e64b9cSStewart Smith 
361c7e64b9cSStewart Smith 	kobject_uevent(&dump->kobj, KOBJ_ADD);
362c7e64b9cSStewart Smith 
363c7e64b9cSStewart Smith 	return dump;
364c7e64b9cSStewart Smith }
365c7e64b9cSStewart Smith 
3668034f715SAlistair Popple static irqreturn_t process_dump(int irq, void *data)
367c7e64b9cSStewart Smith {
368c7e64b9cSStewart Smith 	int rc;
369c7e64b9cSStewart Smith 	uint32_t dump_id, dump_size, dump_type;
370c7e64b9cSStewart Smith 	char name[22];
371a9cbf0b2SMukesh Ojha 	struct kobject *kobj;
372c7e64b9cSStewart Smith 
373c7e64b9cSStewart Smith 	rc = dump_read_info(&dump_id, &dump_size, &dump_type);
374c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS)
375b29336c0SMukesh Ojha 		return IRQ_HANDLED;
376c7e64b9cSStewart Smith 
377c7e64b9cSStewart Smith 	sprintf(name, "0x%x-0x%x", dump_type, dump_id);
378c7e64b9cSStewart Smith 
379c7e64b9cSStewart Smith 	/* we may get notified twice, let's handle
380c7e64b9cSStewart Smith 	 * that gracefully and not create two conflicting
381c7e64b9cSStewart Smith 	 * entries.
382c7e64b9cSStewart Smith 	 */
383a9cbf0b2SMukesh Ojha 	kobj = kset_find_obj(dump_kset, name);
384a9cbf0b2SMukesh Ojha 	if (kobj) {
385a9cbf0b2SMukesh Ojha 		/* Drop reference added by kset_find_obj() */
386a9cbf0b2SMukesh Ojha 		kobject_put(kobj);
387b29336c0SMukesh Ojha 		return IRQ_HANDLED;
388a9cbf0b2SMukesh Ojha 	}
389c7e64b9cSStewart Smith 
390b29336c0SMukesh Ojha 	create_dump_obj(dump_id, dump_size, dump_type);
391c7e64b9cSStewart Smith 
3928034f715SAlistair Popple 	return IRQ_HANDLED;
393c7e64b9cSStewart Smith }
394c7e64b9cSStewart Smith 
395c7e64b9cSStewart Smith void __init opal_platform_dump_init(void)
396c7e64b9cSStewart Smith {
397c7e64b9cSStewart Smith 	int rc;
3988034f715SAlistair Popple 	int dump_irq;
399c7e64b9cSStewart Smith 
400831cf65bSMichael Neuling 	/* ELOG not supported by firmware */
401831cf65bSMichael Neuling 	if (!opal_check_token(OPAL_DUMP_READ))
402831cf65bSMichael Neuling 		return;
403831cf65bSMichael Neuling 
404c7e64b9cSStewart Smith 	dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
405c7e64b9cSStewart Smith 	if (!dump_kset) {
406c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create dump kset\n", __func__);
407c7e64b9cSStewart Smith 		return;
408c7e64b9cSStewart Smith 	}
409c7e64b9cSStewart Smith 
410c7e64b9cSStewart Smith 	rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
411c7e64b9cSStewart Smith 	if (rc) {
412c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create initiate dump attr group\n",
413c7e64b9cSStewart Smith 			__func__);
414c7e64b9cSStewart Smith 		kobject_put(&dump_kset->kobj);
415c7e64b9cSStewart Smith 		return;
416c7e64b9cSStewart Smith 	}
417c7e64b9cSStewart Smith 
4188034f715SAlistair Popple 	dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
4198034f715SAlistair Popple 	if (!dump_irq) {
4208034f715SAlistair Popple 		pr_err("%s: Can't register OPAL event irq (%d)\n",
4218034f715SAlistair Popple 		       __func__, dump_irq);
4228034f715SAlistair Popple 		return;
4238034f715SAlistair Popple 	}
4248034f715SAlistair Popple 
4258034f715SAlistair Popple 	rc = request_threaded_irq(dump_irq, NULL, process_dump,
4268034f715SAlistair Popple 				IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
4278034f715SAlistair Popple 				"opal-dump", NULL);
428c7e64b9cSStewart Smith 	if (rc) {
4298034f715SAlistair Popple 		pr_err("%s: Can't request OPAL event irq (%d)\n",
430c7e64b9cSStewart Smith 		       __func__, rc);
431c7e64b9cSStewart Smith 		return;
432c7e64b9cSStewart Smith 	}
433c7e64b9cSStewart Smith 
4347e73a3b7SStewart Smith 	if (opal_check_token(OPAL_DUMP_RESEND))
435c7e64b9cSStewart Smith 		opal_dump_resend_notification();
436c7e64b9cSStewart Smith }
437