xref: /linux/arch/powerpc/platforms/powernv/opal-core.c (revision 8ff5d8f2008889bb6f46125d5a0638e8749e29bd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Interface for exporting the OPAL ELF core.
4  * Heavily inspired from fs/proc/vmcore.c
5  *
6  * Copyright 2019, Hari Bathini, IBM Corporation.
7  */
8 
9 #define pr_fmt(fmt) "opal core: " fmt
10 
11 #include <linux/memblock.h>
12 #include <linux/uaccess.h>
13 #include <linux/proc_fs.h>
14 #include <linux/elf.h>
15 #include <linux/elfcore.h>
16 #include <linux/kobject.h>
17 #include <linux/sysfs.h>
18 #include <linux/slab.h>
19 #include <linux/vmcore_info.h>
20 #include <linux/of.h>
21 
22 #include <asm/page.h>
23 #include <asm/opal.h>
24 #include <asm/fadump-internal.h>
25 
26 #include "opal-fadump.h"
27 
28 #define MAX_PT_LOAD_CNT		8
29 
30 /* NT_AUXV note related info */
31 #define AUXV_CNT		1
32 #define AUXV_DESC_SZ		(((2 * AUXV_CNT) + 1) * sizeof(Elf64_Off))
33 
34 struct opalcore_config {
35 	u32			num_cpus;
36 	/* PIR value of crashing CPU */
37 	u32			crashing_cpu;
38 
39 	/* CPU state data info from F/W */
40 	u64			cpu_state_destination_vaddr;
41 	u64			cpu_state_data_size;
42 	u64			cpu_state_entry_size;
43 
44 	/* OPAL memory to be exported as PT_LOAD segments */
45 	u64			ptload_addr[MAX_PT_LOAD_CNT];
46 	u64			ptload_size[MAX_PT_LOAD_CNT];
47 	u64			ptload_cnt;
48 
49 	/* Pointer to the first PT_LOAD in the ELF core file */
50 	Elf64_Phdr		*ptload_phdr;
51 
52 	/* Total size of opalcore file. */
53 	size_t			opalcore_size;
54 
55 	/* Buffer for all the ELF core headers and the PT_NOTE */
56 	size_t			opalcorebuf_sz;
57 	char			*opalcorebuf;
58 
59 	/* NT_AUXV buffer */
60 	char			auxv_buf[AUXV_DESC_SZ];
61 };
62 
63 struct opalcore {
64 	struct list_head	list;
65 	u64			paddr;
66 	size_t			size;
67 	loff_t			offset;
68 };
69 
70 static LIST_HEAD(opalcore_list);
71 static struct opalcore_config *oc_conf;
72 static const struct opal_mpipl_fadump *opalc_metadata;
73 static const struct opal_mpipl_fadump *opalc_cpu_metadata;
74 static struct kobject *mpipl_kobj;
75 
76 /*
77  * Set crashing CPU's signal to SIGUSR1. if the kernel is triggered
78  * by kernel, SIGTERM otherwise.
79  */
80 bool kernel_initiated;
81 
82 static struct opalcore * __init get_new_element(void)
83 {
84 	return kzalloc_obj(struct opalcore);
85 }
86 
87 static inline int is_opalcore_usable(void)
88 {
89 	return (oc_conf && oc_conf->opalcorebuf != NULL) ? 1 : 0;
90 }
91 
92 static Elf64_Word *__init append_elf64_note(Elf64_Word *buf, char *name,
93 				     u32 type, void *data,
94 				     size_t data_len)
95 {
96 	Elf64_Nhdr *note = (Elf64_Nhdr *)buf;
97 	Elf64_Word namesz = strlen(name) + 1;
98 
99 	note->n_namesz = cpu_to_be32(namesz);
100 	note->n_descsz = cpu_to_be32(data_len);
101 	note->n_type   = cpu_to_be32(type);
102 	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf64_Word));
103 	memcpy(buf, name, namesz);
104 	buf += DIV_ROUND_UP(namesz, sizeof(Elf64_Word));
105 	memcpy(buf, data, data_len);
106 	buf += DIV_ROUND_UP(data_len, sizeof(Elf64_Word));
107 
108 	return buf;
109 }
110 
111 static void __init fill_prstatus(struct elf_prstatus *prstatus, int pir,
112 			  struct pt_regs *regs)
113 {
114 	memset(prstatus, 0, sizeof(struct elf_prstatus));
115 	elf_core_copy_regs(&(prstatus->pr_reg), regs);
116 
117 	/*
118 	 * Overload PID with PIR value.
119 	 * As a PIR value could also be '0', add an offset of '100'
120 	 * to every PIR to avoid misinterpretations in GDB.
121 	 */
122 	prstatus->common.pr_pid  = cpu_to_be32(100 + pir);
123 	prstatus->common.pr_ppid = cpu_to_be32(1);
124 
125 	/*
126 	 * Indicate SIGUSR1 for crash initiated from kernel.
127 	 * SIGTERM otherwise.
128 	 */
129 	if (pir == oc_conf->crashing_cpu) {
130 		short sig;
131 
132 		sig = kernel_initiated ? SIGUSR1 : SIGTERM;
133 		prstatus->common.pr_cursig = cpu_to_be16(sig);
134 	}
135 }
136 
137 static Elf64_Word *__init auxv_to_elf64_notes(Elf64_Word *buf,
138 				       u64 opal_boot_entry)
139 {
140 	Elf64_Off *bufp = (Elf64_Off *)oc_conf->auxv_buf;
141 	int idx = 0;
142 
143 	memset(bufp, 0, AUXV_DESC_SZ);
144 
145 	/* Entry point of OPAL */
146 	bufp[idx++] = cpu_to_be64(AT_ENTRY);
147 	bufp[idx++] = cpu_to_be64(opal_boot_entry);
148 
149 	/* end of vector */
150 	bufp[idx++] = cpu_to_be64(AT_NULL);
151 
152 	buf = append_elf64_note(buf, NN_AUXV, NT_AUXV,
153 				oc_conf->auxv_buf, AUXV_DESC_SZ);
154 	return buf;
155 }
156 
157 /*
158  * Read from the ELF header and then the crash dump.
159  * Returns number of bytes read on success, -errno on failure.
160  */
161 static ssize_t read_opalcore(struct file *file, struct kobject *kobj,
162 			     const struct bin_attribute *bin_attr, char *to,
163 			     loff_t pos, size_t count)
164 {
165 	struct opalcore *m;
166 	ssize_t tsz, avail;
167 	loff_t tpos = pos;
168 
169 	if (pos >= oc_conf->opalcore_size)
170 		return 0;
171 
172 	/* Adjust count if it goes beyond opalcore size */
173 	avail = oc_conf->opalcore_size - pos;
174 	if (count > avail)
175 		count = avail;
176 
177 	if (count == 0)
178 		return 0;
179 
180 	/* Read ELF core header and/or PT_NOTE segment */
181 	if (tpos < oc_conf->opalcorebuf_sz) {
182 		tsz = min_t(size_t, oc_conf->opalcorebuf_sz - tpos, count);
183 		memcpy(to, oc_conf->opalcorebuf + tpos, tsz);
184 		to += tsz;
185 		tpos += tsz;
186 		count -= tsz;
187 	}
188 
189 	list_for_each_entry(m, &opalcore_list, list) {
190 		/* nothing more to read here */
191 		if (count == 0)
192 			break;
193 
194 		if (tpos < m->offset + m->size) {
195 			void *addr;
196 
197 			tsz = min_t(size_t, m->offset + m->size - tpos, count);
198 			addr = (void *)(m->paddr + tpos - m->offset);
199 			memcpy(to, __va(addr), tsz);
200 			to += tsz;
201 			tpos += tsz;
202 			count -= tsz;
203 		}
204 	}
205 
206 	return (tpos - pos);
207 }
208 
209 static struct bin_attribute opal_core_attr __ro_after_init = {
210 	.attr = {.name = "core", .mode = 0400},
211 	.read = read_opalcore
212 };
213 
214 /*
215  * Read CPU state dump data and convert it into ELF notes.
216  *
217  * Each register entry is of 16 bytes, A numerical identifier along with
218  * a GPR/SPR flag in the first 8 bytes and the register value in the next
219  * 8 bytes. For more details refer to F/W documentation.
220  */
221 static Elf64_Word * __init opalcore_append_cpu_notes(Elf64_Word *buf)
222 {
223 	u32 thread_pir, size_per_thread, regs_offset, regs_cnt, reg_esize;
224 	struct hdat_fadump_thread_hdr *thdr;
225 	struct elf_prstatus prstatus;
226 	Elf64_Word *first_cpu_note;
227 	struct pt_regs regs;
228 	char *bufp;
229 	int i;
230 
231 	size_per_thread = oc_conf->cpu_state_entry_size;
232 	bufp = __va(oc_conf->cpu_state_destination_vaddr);
233 
234 	/*
235 	 * Offset for register entries, entry size and registers count is
236 	 * duplicated in every thread header in keeping with HDAT format.
237 	 * Use these values from the first thread header.
238 	 */
239 	thdr = (struct hdat_fadump_thread_hdr *)bufp;
240 	regs_offset = (offsetof(struct hdat_fadump_thread_hdr, offset) +
241 		       be32_to_cpu(thdr->offset));
242 	reg_esize = be32_to_cpu(thdr->esize);
243 	regs_cnt  = be32_to_cpu(thdr->ecnt);
244 
245 	pr_debug("--------CPU State Data------------\n");
246 	pr_debug("NumCpus     : %u\n", oc_conf->num_cpus);
247 	pr_debug("\tOffset: %u, Entry size: %u, Cnt: %u\n",
248 		 regs_offset, reg_esize, regs_cnt);
249 
250 	/*
251 	 * Skip past the first CPU note. Fill this note with the
252 	 * crashing CPU's prstatus.
253 	 */
254 	first_cpu_note = buf;
255 	buf = append_elf64_note(buf, NN_PRSTATUS, NT_PRSTATUS,
256 				&prstatus, sizeof(prstatus));
257 
258 	for (i = 0; i < oc_conf->num_cpus; i++, bufp += size_per_thread) {
259 		thdr = (struct hdat_fadump_thread_hdr *)bufp;
260 		thread_pir = be32_to_cpu(thdr->pir);
261 
262 		pr_debug("[%04d] PIR: 0x%x, core state: 0x%02x\n",
263 			 i, thread_pir, thdr->core_state);
264 
265 		/*
266 		 * Register state data of MAX cores is provided by firmware,
267 		 * but some of this cores may not be active. So, while
268 		 * processing register state data, check core state and
269 		 * skip threads that belong to inactive cores.
270 		 */
271 		if (thdr->core_state == HDAT_FADUMP_CORE_INACTIVE)
272 			continue;
273 
274 		opal_fadump_read_regs((bufp + regs_offset), regs_cnt,
275 				      reg_esize, false, &regs);
276 
277 		pr_debug("PIR 0x%x - R1 : 0x%llx, NIP : 0x%llx\n", thread_pir,
278 			 be64_to_cpu(regs.gpr[1]), be64_to_cpu(regs.nip));
279 		fill_prstatus(&prstatus, thread_pir, &regs);
280 
281 		if (thread_pir != oc_conf->crashing_cpu) {
282 			buf = append_elf64_note(buf, NN_PRSTATUS,
283 						NT_PRSTATUS, &prstatus,
284 						sizeof(prstatus));
285 		} else {
286 			/*
287 			 * Add crashing CPU as the first NT_PRSTATUS note for
288 			 * GDB to process the core file appropriately.
289 			 */
290 			append_elf64_note(first_cpu_note, NN_PRSTATUS,
291 					  NT_PRSTATUS, &prstatus,
292 					  sizeof(prstatus));
293 		}
294 	}
295 
296 	return buf;
297 }
298 
299 static int __init create_opalcore(void)
300 {
301 	u64 opal_boot_entry, opal_base_addr, paddr;
302 	u32 hdr_size, cpu_notes_size, count;
303 	struct device_node *dn;
304 	struct opalcore *new;
305 	loff_t opalcore_off;
306 	Elf64_Phdr *phdr;
307 	Elf64_Ehdr *elf;
308 	int i, ret;
309 	char *bufp;
310 
311 	/* Get size of header & CPU notes for OPAL core */
312 	hdr_size = (sizeof(Elf64_Ehdr) +
313 		    ((oc_conf->ptload_cnt + 1) * sizeof(Elf64_Phdr)));
314 	cpu_notes_size = ((oc_conf->num_cpus * (CRASH_CORE_NOTE_HEAD_BYTES +
315 			  CRASH_CORE_NOTE_NAME_BYTES +
316 			  CRASH_CORE_NOTE_DESC_BYTES)) +
317 			  (CRASH_CORE_NOTE_HEAD_BYTES +
318 			  CRASH_CORE_NOTE_NAME_BYTES + AUXV_DESC_SZ));
319 
320 	/* Allocate buffer to setup OPAL core */
321 	oc_conf->opalcorebuf_sz = PAGE_ALIGN(hdr_size + cpu_notes_size);
322 	oc_conf->opalcorebuf = alloc_pages_exact(oc_conf->opalcorebuf_sz,
323 						 GFP_KERNEL | __GFP_ZERO);
324 	if (!oc_conf->opalcorebuf) {
325 		pr_err("Not enough memory to setup OPAL core (size: %lu)\n",
326 		       oc_conf->opalcorebuf_sz);
327 		oc_conf->opalcorebuf_sz = 0;
328 		return -ENOMEM;
329 	}
330 	pr_debug("opalcorebuf = 0x%llx\n", (u64)oc_conf->opalcorebuf);
331 
332 	/* Read OPAL related device-tree entries */
333 	dn = of_find_node_by_name(NULL, "ibm,opal");
334 	if (dn) {
335 		ret = of_property_read_u64(dn, "opal-base-address",
336 					   &opal_base_addr);
337 		pr_debug("opal-base-address: %llx\n", opal_base_addr);
338 		ret |= of_property_read_u64(dn, "opal-boot-address",
339 					    &opal_boot_entry);
340 		pr_debug("opal-boot-address: %llx\n", opal_boot_entry);
341 	}
342 	if (!dn || ret)
343 		pr_warn("WARNING: Failed to read OPAL base & entry values\n");
344 
345 	of_node_put(dn);
346 
347 	/* Use count to keep track of the program headers */
348 	count = 0;
349 
350 	bufp = oc_conf->opalcorebuf;
351 	elf = (Elf64_Ehdr *)bufp;
352 	bufp += sizeof(Elf64_Ehdr);
353 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
354 	elf->e_ident[EI_CLASS] = ELF_CLASS;
355 	elf->e_ident[EI_DATA] = ELFDATA2MSB;
356 	elf->e_ident[EI_VERSION] = EV_CURRENT;
357 	elf->e_ident[EI_OSABI] = ELF_OSABI;
358 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
359 	elf->e_type = cpu_to_be16(ET_CORE);
360 	elf->e_machine = cpu_to_be16(ELF_ARCH);
361 	elf->e_version = cpu_to_be32(EV_CURRENT);
362 	elf->e_entry = 0;
363 	elf->e_phoff = cpu_to_be64(sizeof(Elf64_Ehdr));
364 	elf->e_shoff = 0;
365 	elf->e_flags = 0;
366 
367 	elf->e_ehsize = cpu_to_be16(sizeof(Elf64_Ehdr));
368 	elf->e_phentsize = cpu_to_be16(sizeof(Elf64_Phdr));
369 	elf->e_phnum = 0;
370 	elf->e_shentsize = 0;
371 	elf->e_shnum = 0;
372 	elf->e_shstrndx = 0;
373 
374 	phdr = (Elf64_Phdr *)bufp;
375 	bufp += sizeof(Elf64_Phdr);
376 	phdr->p_type	= cpu_to_be32(PT_NOTE);
377 	phdr->p_flags	= 0;
378 	phdr->p_align	= 0;
379 	phdr->p_paddr	= phdr->p_vaddr = 0;
380 	phdr->p_offset	= cpu_to_be64(hdr_size);
381 	phdr->p_filesz	= phdr->p_memsz = cpu_to_be64(cpu_notes_size);
382 	count++;
383 
384 	opalcore_off = oc_conf->opalcorebuf_sz;
385 	oc_conf->ptload_phdr  = (Elf64_Phdr *)bufp;
386 	paddr = 0;
387 	for (i = 0; i < oc_conf->ptload_cnt; i++) {
388 		phdr = (Elf64_Phdr *)bufp;
389 		bufp += sizeof(Elf64_Phdr);
390 		phdr->p_type	= cpu_to_be32(PT_LOAD);
391 		phdr->p_flags	= cpu_to_be32(PF_R|PF_W|PF_X);
392 		phdr->p_align	= 0;
393 
394 		new = get_new_element();
395 		if (!new)
396 			return -ENOMEM;
397 		new->paddr  = oc_conf->ptload_addr[i];
398 		new->size   = oc_conf->ptload_size[i];
399 		new->offset = opalcore_off;
400 		list_add_tail(&new->list, &opalcore_list);
401 
402 		phdr->p_paddr	= cpu_to_be64(paddr);
403 		phdr->p_vaddr	= cpu_to_be64(opal_base_addr + paddr);
404 		phdr->p_filesz	= phdr->p_memsz  =
405 			cpu_to_be64(oc_conf->ptload_size[i]);
406 		phdr->p_offset	= cpu_to_be64(opalcore_off);
407 
408 		count++;
409 		opalcore_off += oc_conf->ptload_size[i];
410 		paddr += oc_conf->ptload_size[i];
411 	}
412 
413 	elf->e_phnum = cpu_to_be16(count);
414 
415 	bufp = (char *)opalcore_append_cpu_notes((Elf64_Word *)bufp);
416 	bufp = (char *)auxv_to_elf64_notes((Elf64_Word *)bufp, opal_boot_entry);
417 
418 	oc_conf->opalcore_size = opalcore_off;
419 	return 0;
420 }
421 
422 static void opalcore_cleanup(void)
423 {
424 	if (oc_conf == NULL)
425 		return;
426 
427 	/* Remove OPAL core sysfs file */
428 	sysfs_remove_bin_file(mpipl_kobj, &opal_core_attr);
429 	oc_conf->ptload_phdr = NULL;
430 	oc_conf->ptload_cnt = 0;
431 
432 	/* free the buffer used for setting up OPAL core */
433 	if (oc_conf->opalcorebuf) {
434 		free_pages_exact(oc_conf->opalcorebuf, oc_conf->opalcorebuf_sz);
435 		oc_conf->opalcorebuf = NULL;
436 		oc_conf->opalcorebuf_sz = 0;
437 	}
438 
439 	kfree(oc_conf);
440 	oc_conf = NULL;
441 }
442 __exitcall(opalcore_cleanup);
443 
444 static void __init opalcore_config_init(void)
445 {
446 	u32 idx, cpu_data_version;
447 	struct device_node *np;
448 	const __be32 *prop;
449 	u64 addr = 0;
450 	int i, ret;
451 
452 	np = of_find_node_by_path("/ibm,opal/dump");
453 	if (np == NULL)
454 		return;
455 
456 	if (!of_device_is_compatible(np, "ibm,opal-dump")) {
457 		pr_warn("Support missing for this f/w version!\n");
458 		return;
459 	}
460 
461 	/* Check if dump has been initiated on last reboot */
462 	prop = of_get_property(np, "mpipl-boot", NULL);
463 	if (!prop) {
464 		of_node_put(np);
465 		return;
466 	}
467 
468 	/* Get OPAL metadata */
469 	ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_OPAL, &addr);
470 	if ((ret != OPAL_SUCCESS) || !addr) {
471 		pr_err("Failed to get OPAL metadata (%d)\n", ret);
472 		goto error_out;
473 	}
474 
475 	addr = be64_to_cpu(addr);
476 	pr_debug("OPAL metadata addr: %llx\n", addr);
477 	opalc_metadata = __va(addr);
478 
479 	/* Get OPAL CPU metadata */
480 	ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_CPU, &addr);
481 	if ((ret != OPAL_SUCCESS) || !addr) {
482 		pr_err("Failed to get OPAL CPU metadata (%d)\n", ret);
483 		goto error_out;
484 	}
485 
486 	addr = be64_to_cpu(addr);
487 	pr_debug("CPU metadata addr: %llx\n", addr);
488 	opalc_cpu_metadata = __va(addr);
489 
490 	/* Allocate memory for config buffer */
491 	oc_conf = kzalloc_obj(struct opalcore_config);
492 	if (oc_conf == NULL)
493 		goto error_out;
494 
495 	/* Parse OPAL metadata */
496 	if (opalc_metadata->version != OPAL_MPIPL_VERSION) {
497 		pr_warn("Supported OPAL metadata version: %u, found: %u!\n",
498 			OPAL_MPIPL_VERSION, opalc_metadata->version);
499 		pr_warn("WARNING: F/W using newer OPAL metadata format!!\n");
500 	}
501 
502 	oc_conf->ptload_cnt = 0;
503 	idx = be32_to_cpu(opalc_metadata->region_cnt);
504 	if (idx > MAX_PT_LOAD_CNT) {
505 		pr_warn("WARNING: OPAL regions count (%d) adjusted to limit (%d)",
506 			idx, MAX_PT_LOAD_CNT);
507 		idx = MAX_PT_LOAD_CNT;
508 	}
509 	for (i = 0; i < idx; i++) {
510 		oc_conf->ptload_addr[oc_conf->ptload_cnt] =
511 				be64_to_cpu(opalc_metadata->region[i].dest);
512 		oc_conf->ptload_size[oc_conf->ptload_cnt++] =
513 				be64_to_cpu(opalc_metadata->region[i].size);
514 	}
515 	oc_conf->ptload_cnt = i;
516 	oc_conf->crashing_cpu = be32_to_cpu(opalc_metadata->crashing_pir);
517 
518 	if (!oc_conf->ptload_cnt) {
519 		pr_err("OPAL memory regions not found\n");
520 		goto error_out;
521 	}
522 
523 	/* Parse OPAL CPU metadata */
524 	cpu_data_version = be32_to_cpu(opalc_cpu_metadata->cpu_data_version);
525 	if (cpu_data_version != HDAT_FADUMP_CPU_DATA_VER) {
526 		pr_warn("Supported CPU data version: %u, found: %u!\n",
527 			HDAT_FADUMP_CPU_DATA_VER, cpu_data_version);
528 		pr_warn("WARNING: F/W using newer CPU state data format!!\n");
529 	}
530 
531 	addr = be64_to_cpu(opalc_cpu_metadata->region[0].dest);
532 	if (!addr) {
533 		pr_err("CPU state data not found!\n");
534 		goto error_out;
535 	}
536 	oc_conf->cpu_state_destination_vaddr = (u64)__va(addr);
537 
538 	oc_conf->cpu_state_data_size =
539 			be64_to_cpu(opalc_cpu_metadata->region[0].size);
540 	oc_conf->cpu_state_entry_size =
541 			be32_to_cpu(opalc_cpu_metadata->cpu_data_size);
542 
543 	if ((oc_conf->cpu_state_entry_size == 0) ||
544 	    (oc_conf->cpu_state_entry_size > oc_conf->cpu_state_data_size)) {
545 		pr_err("CPU state data is invalid.\n");
546 		goto error_out;
547 	}
548 	oc_conf->num_cpus = (oc_conf->cpu_state_data_size /
549 			     oc_conf->cpu_state_entry_size);
550 
551 	of_node_put(np);
552 	return;
553 
554 error_out:
555 	pr_err("Could not export /sys/firmware/opal/core\n");
556 	opalcore_cleanup();
557 	of_node_put(np);
558 }
559 
560 static ssize_t release_core_store(struct kobject *kobj,
561 				  struct kobj_attribute *attr,
562 				  const char *buf, size_t count)
563 {
564 	int input = -1;
565 
566 	if (kstrtoint(buf, 0, &input))
567 		return -EINVAL;
568 
569 	if (input == 1) {
570 		if (oc_conf == NULL) {
571 			pr_err("'/sys/firmware/opal/core' file not accessible!\n");
572 			return -EPERM;
573 		}
574 
575 		/*
576 		 * Take away '/sys/firmware/opal/core' and release all memory
577 		 * used for exporting this file.
578 		 */
579 		opalcore_cleanup();
580 	} else
581 		return -EINVAL;
582 
583 	return count;
584 }
585 
586 static struct kobj_attribute opalcore_rel_attr = __ATTR_WO(release_core);
587 
588 static struct attribute *mpipl_attr[] = {
589 	&opalcore_rel_attr.attr,
590 	NULL,
591 };
592 
593 static const struct bin_attribute *const mpipl_bin_attr[] = {
594 	&opal_core_attr,
595 	NULL,
596 
597 };
598 
599 static const struct attribute_group mpipl_group = {
600 	.attrs = mpipl_attr,
601 	.bin_attrs =  mpipl_bin_attr,
602 };
603 
604 static int __init opalcore_init(void)
605 {
606 	int rc = -1;
607 
608 	opalcore_config_init();
609 
610 	if (oc_conf == NULL)
611 		return rc;
612 
613 	create_opalcore();
614 
615 	/*
616 	 * If oc_conf->opalcorebuf= is set in the 2nd kernel,
617 	 * then capture the dump.
618 	 */
619 	if (!(is_opalcore_usable())) {
620 		pr_err("Failed to export /sys/firmware/opal/mpipl/core\n");
621 		opalcore_cleanup();
622 		return rc;
623 	}
624 
625 	/* Set OPAL core file size */
626 	opal_core_attr.size = oc_conf->opalcore_size;
627 
628 	mpipl_kobj = kobject_create_and_add("mpipl", opal_kobj);
629 	if (!mpipl_kobj) {
630 		pr_err("unable to create mpipl kobject\n");
631 		return -ENOMEM;
632 	}
633 
634 	/* Export OPAL core sysfs file */
635 	rc = sysfs_create_group(mpipl_kobj, &mpipl_group);
636 	if (rc) {
637 		pr_err("mpipl sysfs group creation failed (%d)", rc);
638 		opalcore_cleanup();
639 		return rc;
640 	}
641 	/* The /sys/firmware/opal/core is moved to /sys/firmware/opal/mpipl/
642 	 * directory, need to create symlink at old location to maintain
643 	 * backward compatibility.
644 	 */
645 	rc = compat_only_sysfs_link_entry_to_kobj(opal_kobj, mpipl_kobj,
646 						  "core", NULL);
647 	if (rc) {
648 		pr_err("unable to create core symlink (%d)\n", rc);
649 		return rc;
650 	}
651 
652 	return 0;
653 }
654 fs_initcall(opalcore_init);
655