]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - arch/powerpc/kernel/fadump.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-focal-kernel.git] / arch / powerpc / kernel / fadump.c
1 /*
2 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3 * dump with assistance from firmware. This approach does not use kexec,
4 * instead firmware assists in booting the kdump kernel while preserving
5 * memory contents. The most of the code implementation has been adapted
6 * from phyp assisted dump implementation written by Linas Vepstas and
7 * Manish Ahuja
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * Copyright 2011 IBM Corporation
24 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
25 */
26
27 #undef DEBUG
28 #define pr_fmt(fmt) "fadump: " fmt
29
30 #include <linux/string.h>
31 #include <linux/memblock.h>
32 #include <linux/delay.h>
33 #include <linux/seq_file.h>
34 #include <linux/crash_dump.h>
35 #include <linux/kobject.h>
36 #include <linux/sysfs.h>
37 #include <linux/slab.h>
38 #include <linux/cma.h>
39 #include <linux/hugetlb.h>
40
41 #include <asm/debugfs.h>
42 #include <asm/page.h>
43 #include <asm/prom.h>
44 #include <asm/rtas.h>
45 #include <asm/fadump.h>
46 #include <asm/setup.h>
47
48 static struct fw_dump fw_dump;
49 static struct fadump_mem_struct fdm;
50 static const struct fadump_mem_struct *fdm_active;
51 #ifdef CONFIG_CMA
52 static struct cma *fadump_cma;
53 #endif
54
55 static DEFINE_MUTEX(fadump_mutex);
56 struct fad_crash_memory_ranges *crash_memory_ranges;
57 int crash_memory_ranges_size;
58 int crash_mem_ranges;
59 int max_crash_mem_ranges;
60
61 #ifdef CONFIG_CMA
62 /*
63 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
64 *
65 * This function initializes CMA area from fadump reserved memory.
66 * The total size of fadump reserved memory covers for boot memory size
67 * + cpu data size + hpte size and metadata.
68 * Initialize only the area equivalent to boot memory size for CMA use.
69 * The reamining portion of fadump reserved memory will be not given
70 * to CMA and pages for thoes will stay reserved. boot memory size is
71 * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
72 * But for some reason even if it fails we still have the memory reservation
73 * with us and we can still continue doing fadump.
74 */
75 int __init fadump_cma_init(void)
76 {
77 unsigned long long base, size;
78 int rc;
79
80 if (!fw_dump.fadump_enabled)
81 return 0;
82
83 /*
84 * Do not use CMA if user has provided fadump=nocma kernel parameter.
85 * Return 1 to continue with fadump old behaviour.
86 */
87 if (fw_dump.nocma)
88 return 1;
89
90 base = fw_dump.reserve_dump_area_start;
91 size = fw_dump.boot_memory_size;
92
93 if (!size)
94 return 0;
95
96 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
97 if (rc) {
98 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
99 /*
100 * Though the CMA init has failed we still have memory
101 * reservation with us. The reserved memory will be
102 * blocked from production system usage. Hence return 1,
103 * so that we can continue with fadump.
104 */
105 return 1;
106 }
107
108 /*
109 * So we now have successfully initialized cma area for fadump.
110 */
111 pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
112 "bytes of memory reserved for firmware-assisted dump\n",
113 cma_get_size(fadump_cma),
114 (unsigned long)cma_get_base(fadump_cma) >> 20,
115 fw_dump.reserve_dump_area_size);
116 return 1;
117 }
118 #else
119 static int __init fadump_cma_init(void) { return 1; }
120 #endif /* CONFIG_CMA */
121
122 /* Scan the Firmware Assisted dump configuration details. */
123 int __init early_init_dt_scan_fw_dump(unsigned long node,
124 const char *uname, int depth, void *data)
125 {
126 const __be32 *sections;
127 int i, num_sections;
128 int size;
129 const __be32 *token;
130
131 if (depth != 1 || strcmp(uname, "rtas") != 0)
132 return 0;
133
134 /*
135 * Check if Firmware Assisted dump is supported. if yes, check
136 * if dump has been initiated on last reboot.
137 */
138 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
139 if (!token)
140 return 1;
141
142 fw_dump.fadump_supported = 1;
143 fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
144
145 /*
146 * The 'ibm,kernel-dump' rtas node is present only if there is
147 * dump data waiting for us.
148 */
149 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
150 if (fdm_active)
151 fw_dump.dump_active = 1;
152
153 /* Get the sizes required to store dump data for the firmware provided
154 * dump sections.
155 * For each dump section type supported, a 32bit cell which defines
156 * the ID of a supported section followed by two 32 bit cells which
157 * gives teh size of the section in bytes.
158 */
159 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
160 &size);
161
162 if (!sections)
163 return 1;
164
165 num_sections = size / (3 * sizeof(u32));
166
167 for (i = 0; i < num_sections; i++, sections += 3) {
168 u32 type = (u32)of_read_number(sections, 1);
169
170 switch (type) {
171 case FADUMP_CPU_STATE_DATA:
172 fw_dump.cpu_state_data_size =
173 of_read_ulong(&sections[1], 2);
174 break;
175 case FADUMP_HPTE_REGION:
176 fw_dump.hpte_region_size =
177 of_read_ulong(&sections[1], 2);
178 break;
179 }
180 }
181
182 return 1;
183 }
184
185 /*
186 * If fadump is registered, check if the memory provided
187 * falls within boot memory area and reserved memory area.
188 */
189 int is_fadump_memory_area(u64 addr, ulong size)
190 {
191 u64 d_start = fw_dump.reserve_dump_area_start;
192 u64 d_end = d_start + fw_dump.reserve_dump_area_size;
193
194 if (!fw_dump.dump_registered)
195 return 0;
196
197 if (((addr + size) > d_start) && (addr <= d_end))
198 return 1;
199
200 return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
201 }
202
203 int should_fadump_crash(void)
204 {
205 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
206 return 0;
207 return 1;
208 }
209
210 int is_fadump_active(void)
211 {
212 return fw_dump.dump_active;
213 }
214
215 /*
216 * Returns 1, if there are no holes in boot memory area,
217 * 0 otherwise.
218 */
219 static int is_boot_memory_area_contiguous(void)
220 {
221 struct memblock_region *reg;
222 unsigned long tstart, tend;
223 unsigned long start_pfn = PHYS_PFN(RMA_START);
224 unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size);
225 unsigned int ret = 0;
226
227 for_each_memblock(memory, reg) {
228 tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
229 tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
230 if (tstart < tend) {
231 /* Memory hole from start_pfn to tstart */
232 if (tstart > start_pfn)
233 break;
234
235 if (tend == end_pfn) {
236 ret = 1;
237 break;
238 }
239
240 start_pfn = tend + 1;
241 }
242 }
243
244 return ret;
245 }
246
247 /*
248 * Returns true, if there are no holes in reserved memory area,
249 * false otherwise.
250 */
251 static bool is_reserved_memory_area_contiguous(void)
252 {
253 struct memblock_region *reg;
254 unsigned long start, end;
255 unsigned long d_start = fw_dump.reserve_dump_area_start;
256 unsigned long d_end = d_start + fw_dump.reserve_dump_area_size;
257
258 for_each_memblock(memory, reg) {
259 start = max(d_start, (unsigned long)reg->base);
260 end = min(d_end, (unsigned long)(reg->base + reg->size));
261 if (d_start < end) {
262 /* Memory hole from d_start to start */
263 if (start > d_start)
264 break;
265
266 if (end == d_end)
267 return true;
268
269 d_start = end + 1;
270 }
271 }
272
273 return false;
274 }
275
276 /* Print firmware assisted dump configurations for debugging purpose. */
277 static void fadump_show_config(void)
278 {
279 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
280 (fw_dump.fadump_supported ? "present" : "no support"));
281
282 if (!fw_dump.fadump_supported)
283 return;
284
285 pr_debug("Fadump enabled : %s\n",
286 (fw_dump.fadump_enabled ? "yes" : "no"));
287 pr_debug("Dump Active : %s\n",
288 (fw_dump.dump_active ? "yes" : "no"));
289 pr_debug("Dump section sizes:\n");
290 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
291 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
292 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
293 }
294
295 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
296 unsigned long addr)
297 {
298 if (!fdm)
299 return 0;
300
301 memset(fdm, 0, sizeof(struct fadump_mem_struct));
302 addr = addr & PAGE_MASK;
303
304 fdm->header.dump_format_version = cpu_to_be32(0x00000001);
305 fdm->header.dump_num_sections = cpu_to_be16(3);
306 fdm->header.dump_status_flag = 0;
307 fdm->header.offset_first_dump_section =
308 cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
309
310 /*
311 * Fields for disk dump option.
312 * We are not using disk dump option, hence set these fields to 0.
313 */
314 fdm->header.dd_block_size = 0;
315 fdm->header.dd_block_offset = 0;
316 fdm->header.dd_num_blocks = 0;
317 fdm->header.dd_offset_disk_path = 0;
318
319 /* set 0 to disable an automatic dump-reboot. */
320 fdm->header.max_time_auto = 0;
321
322 /* Kernel dump sections */
323 /* cpu state data section. */
324 fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
325 fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
326 fdm->cpu_state_data.source_address = 0;
327 fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
328 fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
329 addr += fw_dump.cpu_state_data_size;
330
331 /* hpte region section */
332 fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
333 fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
334 fdm->hpte_region.source_address = 0;
335 fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
336 fdm->hpte_region.destination_address = cpu_to_be64(addr);
337 addr += fw_dump.hpte_region_size;
338
339 /* RMA region section */
340 fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
341 fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
342 fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
343 fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
344 fdm->rmr_region.destination_address = cpu_to_be64(addr);
345 addr += fw_dump.boot_memory_size;
346
347 return addr;
348 }
349
350 /**
351 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
352 *
353 * Function to find the largest memory size we need to reserve during early
354 * boot process. This will be the size of the memory that is required for a
355 * kernel to boot successfully.
356 *
357 * This function has been taken from phyp-assisted dump feature implementation.
358 *
359 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
360 *
361 * TODO: Come up with better approach to find out more accurate memory size
362 * that is required for a kernel to boot successfully.
363 *
364 */
365 static inline unsigned long fadump_calculate_reserve_size(void)
366 {
367 int ret;
368 unsigned long long base, size;
369
370 if (fw_dump.reserve_bootvar)
371 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
372
373 /*
374 * Check if the size is specified through crashkernel= cmdline
375 * option. If yes, then use that but ignore base as fadump reserves
376 * memory at a predefined offset.
377 */
378 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
379 &size, &base);
380 if (ret == 0 && size > 0) {
381 unsigned long max_size;
382
383 if (fw_dump.reserve_bootvar)
384 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
385
386 fw_dump.reserve_bootvar = (unsigned long)size;
387
388 /*
389 * Adjust if the boot memory size specified is above
390 * the upper limit.
391 */
392 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
393 if (fw_dump.reserve_bootvar > max_size) {
394 fw_dump.reserve_bootvar = max_size;
395 pr_info("Adjusted boot memory size to %luMB\n",
396 (fw_dump.reserve_bootvar >> 20));
397 }
398
399 return fw_dump.reserve_bootvar;
400 } else if (fw_dump.reserve_bootvar) {
401 /*
402 * 'fadump_reserve_mem=' is being used to reserve memory
403 * for firmware-assisted dump.
404 */
405 return fw_dump.reserve_bootvar;
406 }
407
408 /* divide by 20 to get 5% of value */
409 size = memblock_phys_mem_size() / 20;
410
411 /* round it down in multiples of 256 */
412 size = size & ~0x0FFFFFFFUL;
413
414 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
415 if (memory_limit && size > memory_limit)
416 size = memory_limit;
417
418 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
419 }
420
421 /*
422 * Calculate the total memory size required to be reserved for
423 * firmware-assisted dump registration.
424 */
425 static unsigned long get_fadump_area_size(void)
426 {
427 unsigned long size = 0;
428
429 size += fw_dump.cpu_state_data_size;
430 size += fw_dump.hpte_region_size;
431 size += fw_dump.boot_memory_size;
432 size += sizeof(struct fadump_crash_info_header);
433 size += sizeof(struct elfhdr); /* ELF core header.*/
434 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
435 /* Program headers for crash memory regions. */
436 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
437
438 size = PAGE_ALIGN(size);
439 return size;
440 }
441
442 static void __init fadump_reserve_crash_area(unsigned long base,
443 unsigned long size)
444 {
445 struct memblock_region *reg;
446 unsigned long mstart, mend, msize;
447
448 for_each_memblock(memory, reg) {
449 mstart = max_t(unsigned long, base, reg->base);
450 mend = reg->base + reg->size;
451 mend = min(base + size, mend);
452
453 if (mstart < mend) {
454 msize = mend - mstart;
455 memblock_reserve(mstart, msize);
456 pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n",
457 (msize >> 20), mstart);
458 }
459 }
460 }
461
462 int __init fadump_reserve_mem(void)
463 {
464 unsigned long base, size, memory_boundary;
465
466 if (!fw_dump.fadump_enabled)
467 return 0;
468
469 if (!fw_dump.fadump_supported) {
470 printk(KERN_INFO "Firmware-assisted dump is not supported on"
471 " this hardware\n");
472 fw_dump.fadump_enabled = 0;
473 return 0;
474 }
475 /*
476 * Initialize boot memory size
477 * If dump is active then we have already calculated the size during
478 * first kernel.
479 */
480 if (fdm_active)
481 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
482 else {
483 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
484 #ifdef CONFIG_CMA
485 if (!fw_dump.nocma)
486 fw_dump.boot_memory_size =
487 ALIGN(fw_dump.boot_memory_size,
488 FADUMP_CMA_ALIGNMENT);
489 #endif
490 }
491
492 /*
493 * Calculate the memory boundary.
494 * If memory_limit is less than actual memory boundary then reserve
495 * the memory for fadump beyond the memory_limit and adjust the
496 * memory_limit accordingly, so that the running kernel can run with
497 * specified memory_limit.
498 */
499 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
500 size = get_fadump_area_size();
501 if ((memory_limit + size) < memblock_end_of_DRAM())
502 memory_limit += size;
503 else
504 memory_limit = memblock_end_of_DRAM();
505 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
506 " dump, now %#016llx\n", memory_limit);
507 }
508 if (memory_limit)
509 memory_boundary = memory_limit;
510 else
511 memory_boundary = memblock_end_of_DRAM();
512
513 if (fw_dump.dump_active) {
514 pr_info("Firmware-assisted dump is active.\n");
515
516 #ifdef CONFIG_HUGETLB_PAGE
517 /*
518 * FADump capture kernel doesn't care much about hugepages.
519 * In fact, handling hugepages in capture kernel is asking for
520 * trouble. So, disable HugeTLB support when fadump is active.
521 */
522 hugetlb_disabled = true;
523 #endif
524 /*
525 * If last boot has crashed then reserve all the memory
526 * above boot_memory_size so that we don't touch it until
527 * dump is written to disk by userspace tool. This memory
528 * will be released for general use once the dump is saved.
529 */
530 base = fw_dump.boot_memory_size;
531 size = memory_boundary - base;
532 fadump_reserve_crash_area(base, size);
533
534 fw_dump.fadumphdr_addr =
535 be64_to_cpu(fdm_active->rmr_region.destination_address) +
536 be64_to_cpu(fdm_active->rmr_region.source_len);
537 pr_debug("fadumphdr_addr = %pa\n", &fw_dump.fadumphdr_addr);
538 fw_dump.reserve_dump_area_start = base;
539 fw_dump.reserve_dump_area_size = size;
540 } else {
541 size = get_fadump_area_size();
542
543 /*
544 * Reserve memory at an offset closer to bottom of the RAM to
545 * minimize the impact of memory hot-remove operation. We can't
546 * use memblock_find_in_range() here since it doesn't allocate
547 * from bottom to top.
548 */
549 for (base = fw_dump.boot_memory_size;
550 base <= (memory_boundary - size);
551 base += size) {
552 if (memblock_is_region_memory(base, size) &&
553 !memblock_is_region_reserved(base, size))
554 break;
555 }
556 if ((base > (memory_boundary - size)) ||
557 memblock_reserve(base, size)) {
558 pr_err("Failed to reserve memory\n");
559 return 0;
560 }
561
562 pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
563 "assisted dump (System RAM: %ldMB)\n",
564 (unsigned long)(size >> 20),
565 (unsigned long)(base >> 20),
566 (unsigned long)(memblock_phys_mem_size() >> 20));
567
568 fw_dump.reserve_dump_area_start = base;
569 fw_dump.reserve_dump_area_size = size;
570 return fadump_cma_init();
571 }
572 return 1;
573 }
574
575 unsigned long __init arch_reserved_kernel_pages(void)
576 {
577 return memblock_reserved_size() / PAGE_SIZE;
578 }
579
580 /* Look for fadump= cmdline option. */
581 static int __init early_fadump_param(char *p)
582 {
583 if (!p)
584 return 1;
585
586 if (strncmp(p, "on", 2) == 0)
587 fw_dump.fadump_enabled = 1;
588 else if (strncmp(p, "off", 3) == 0)
589 fw_dump.fadump_enabled = 0;
590 else if (strncmp(p, "nocma", 5) == 0) {
591 fw_dump.fadump_enabled = 1;
592 fw_dump.nocma = 1;
593 }
594
595 return 0;
596 }
597 early_param("fadump", early_fadump_param);
598
599 /*
600 * Look for fadump_reserve_mem= cmdline option
601 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
602 * the sooner 'crashkernel=' parameter is accustomed to.
603 */
604 static int __init early_fadump_reserve_mem(char *p)
605 {
606 if (p)
607 fw_dump.reserve_bootvar = memparse(p, &p);
608 return 0;
609 }
610 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
611
612 static int register_fw_dump(struct fadump_mem_struct *fdm)
613 {
614 int rc, err;
615 unsigned int wait_time;
616
617 pr_debug("Registering for firmware-assisted kernel dump...\n");
618
619 /* TODO: Add upper time limit for the delay */
620 do {
621 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
622 FADUMP_REGISTER, fdm,
623 sizeof(struct fadump_mem_struct));
624
625 wait_time = rtas_busy_delay_time(rc);
626 if (wait_time)
627 mdelay(wait_time);
628
629 } while (wait_time);
630
631 err = -EIO;
632 switch (rc) {
633 default:
634 pr_err("Failed to register. Unknown Error(%d).\n", rc);
635 break;
636 case -1:
637 printk(KERN_ERR "Failed to register firmware-assisted kernel"
638 " dump. Hardware Error(%d).\n", rc);
639 break;
640 case -3:
641 if (!is_boot_memory_area_contiguous())
642 pr_err("Can't have holes in boot memory area while registering fadump\n");
643 else if (!is_reserved_memory_area_contiguous())
644 pr_err("Can't have holes in reserved memory area while"
645 " registering fadump\n");
646
647 printk(KERN_ERR "Failed to register firmware-assisted kernel"
648 " dump. Parameter Error(%d).\n", rc);
649 err = -EINVAL;
650 break;
651 case -9:
652 printk(KERN_ERR "firmware-assisted kernel dump is already "
653 " registered.");
654 fw_dump.dump_registered = 1;
655 err = -EEXIST;
656 break;
657 case 0:
658 printk(KERN_INFO "firmware-assisted kernel dump registration"
659 " is successful\n");
660 fw_dump.dump_registered = 1;
661 err = 0;
662 break;
663 }
664 return err;
665 }
666
667 void crash_fadump(struct pt_regs *regs, const char *str)
668 {
669 struct fadump_crash_info_header *fdh = NULL;
670 int old_cpu, this_cpu;
671
672 if (!should_fadump_crash())
673 return;
674
675 /*
676 * old_cpu == -1 means this is the first CPU which has come here,
677 * go ahead and trigger fadump.
678 *
679 * old_cpu != -1 means some other CPU has already on it's way
680 * to trigger fadump, just keep looping here.
681 */
682 this_cpu = smp_processor_id();
683 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
684
685 if (old_cpu != -1) {
686 /*
687 * We can't loop here indefinitely. Wait as long as fadump
688 * is in force. If we race with fadump un-registration this
689 * loop will break and then we go down to normal panic path
690 * and reboot. If fadump is in force the first crashing
691 * cpu will definitely trigger fadump.
692 */
693 while (fw_dump.dump_registered)
694 cpu_relax();
695 return;
696 }
697
698 fdh = __va(fw_dump.fadumphdr_addr);
699 fdh->crashing_cpu = crashing_cpu;
700 crash_save_vmcoreinfo();
701
702 if (regs)
703 fdh->regs = *regs;
704 else
705 ppc_save_regs(&fdh->regs);
706
707 fdh->online_mask = *cpu_online_mask;
708
709 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
710 rtas_os_term((char *)str);
711 }
712
713 #define GPR_MASK 0xffffff0000000000
714 static inline int fadump_gpr_index(u64 id)
715 {
716 int i = -1;
717 char str[3];
718
719 if ((id & GPR_MASK) == REG_ID("GPR")) {
720 /* get the digits at the end */
721 id &= ~GPR_MASK;
722 id >>= 24;
723 str[2] = '\0';
724 str[1] = id & 0xff;
725 str[0] = (id >> 8) & 0xff;
726 sscanf(str, "%d", &i);
727 if (i > 31)
728 i = -1;
729 }
730 return i;
731 }
732
733 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
734 u64 reg_val)
735 {
736 int i;
737
738 i = fadump_gpr_index(reg_id);
739 if (i >= 0)
740 regs->gpr[i] = (unsigned long)reg_val;
741 else if (reg_id == REG_ID("NIA"))
742 regs->nip = (unsigned long)reg_val;
743 else if (reg_id == REG_ID("MSR"))
744 regs->msr = (unsigned long)reg_val;
745 else if (reg_id == REG_ID("CTR"))
746 regs->ctr = (unsigned long)reg_val;
747 else if (reg_id == REG_ID("LR"))
748 regs->link = (unsigned long)reg_val;
749 else if (reg_id == REG_ID("XER"))
750 regs->xer = (unsigned long)reg_val;
751 else if (reg_id == REG_ID("CR"))
752 regs->ccr = (unsigned long)reg_val;
753 else if (reg_id == REG_ID("DAR"))
754 regs->dar = (unsigned long)reg_val;
755 else if (reg_id == REG_ID("DSISR"))
756 regs->dsisr = (unsigned long)reg_val;
757 }
758
759 static struct fadump_reg_entry*
760 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
761 {
762 memset(regs, 0, sizeof(struct pt_regs));
763
764 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
765 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
766 be64_to_cpu(reg_entry->reg_value));
767 reg_entry++;
768 }
769 reg_entry++;
770 return reg_entry;
771 }
772
773 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
774 {
775 struct elf_prstatus prstatus;
776
777 memset(&prstatus, 0, sizeof(prstatus));
778 /*
779 * FIXME: How do i get PID? Do I really need it?
780 * prstatus.pr_pid = ????
781 */
782 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
783 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
784 &prstatus, sizeof(prstatus));
785 return buf;
786 }
787
788 static void fadump_update_elfcore_header(char *bufp)
789 {
790 struct elfhdr *elf;
791 struct elf_phdr *phdr;
792
793 elf = (struct elfhdr *)bufp;
794 bufp += sizeof(struct elfhdr);
795
796 /* First note is a place holder for cpu notes info. */
797 phdr = (struct elf_phdr *)bufp;
798
799 if (phdr->p_type == PT_NOTE) {
800 phdr->p_paddr = fw_dump.cpu_notes_buf;
801 phdr->p_offset = phdr->p_paddr;
802 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
803 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
804 }
805 return;
806 }
807
808 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
809 {
810 void *vaddr;
811 struct page *page;
812 unsigned long order, count, i;
813
814 order = get_order(size);
815 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
816 if (!vaddr)
817 return NULL;
818
819 count = 1 << order;
820 page = virt_to_page(vaddr);
821 for (i = 0; i < count; i++)
822 SetPageReserved(page + i);
823 return vaddr;
824 }
825
826 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
827 {
828 struct page *page;
829 unsigned long order, count, i;
830
831 order = get_order(size);
832 count = 1 << order;
833 page = virt_to_page(vaddr);
834 for (i = 0; i < count; i++)
835 ClearPageReserved(page + i);
836 __free_pages(page, order);
837 }
838
839 /*
840 * Read CPU state dump data and convert it into ELF notes.
841 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
842 * used to access the data to allow for additional fields to be added without
843 * affecting compatibility. Each list of registers for a CPU starts with
844 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
845 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
846 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
847 * of register value. For more details refer to PAPR document.
848 *
849 * Only for the crashing cpu we ignore the CPU dump data and get exact
850 * state from fadump crash info structure populated by first kernel at the
851 * time of crash.
852 */
853 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
854 {
855 struct fadump_reg_save_area_header *reg_header;
856 struct fadump_reg_entry *reg_entry;
857 struct fadump_crash_info_header *fdh = NULL;
858 void *vaddr;
859 unsigned long addr;
860 u32 num_cpus, *note_buf;
861 struct pt_regs regs;
862 int i, rc = 0, cpu = 0;
863
864 if (!fdm->cpu_state_data.bytes_dumped)
865 return -EINVAL;
866
867 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
868 vaddr = __va(addr);
869
870 reg_header = vaddr;
871 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
872 printk(KERN_ERR "Unable to read register save area.\n");
873 return -ENOENT;
874 }
875 pr_debug("--------CPU State Data------------\n");
876 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
877 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
878
879 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
880 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
881 pr_debug("NumCpus : %u\n", num_cpus);
882 vaddr += sizeof(u32);
883 reg_entry = (struct fadump_reg_entry *)vaddr;
884
885 /* Allocate buffer to hold cpu crash notes. */
886 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
887 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
888 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
889 if (!note_buf) {
890 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
891 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
892 return -ENOMEM;
893 }
894 fw_dump.cpu_notes_buf = __pa(note_buf);
895
896 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
897 (num_cpus * sizeof(note_buf_t)), note_buf);
898
899 if (fw_dump.fadumphdr_addr)
900 fdh = __va(fw_dump.fadumphdr_addr);
901
902 for (i = 0; i < num_cpus; i++) {
903 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
904 printk(KERN_ERR "Unable to read CPU state data\n");
905 rc = -ENOENT;
906 goto error_out;
907 }
908 /* Lower 4 bytes of reg_value contains logical cpu id */
909 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
910 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
911 SKIP_TO_NEXT_CPU(reg_entry);
912 continue;
913 }
914 pr_debug("Reading register data for cpu %d...\n", cpu);
915 if (fdh && fdh->crashing_cpu == cpu) {
916 regs = fdh->regs;
917 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
918 SKIP_TO_NEXT_CPU(reg_entry);
919 } else {
920 reg_entry++;
921 reg_entry = fadump_read_registers(reg_entry, &regs);
922 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
923 }
924 }
925 final_note(note_buf);
926
927 if (fdh) {
928 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
929 fdh->elfcorehdr_addr);
930 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
931 }
932 return 0;
933
934 error_out:
935 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
936 fw_dump.cpu_notes_buf_size);
937 fw_dump.cpu_notes_buf = 0;
938 fw_dump.cpu_notes_buf_size = 0;
939 return rc;
940
941 }
942
943 /*
944 * Validate and process the dump data stored by firmware before exporting
945 * it through '/proc/vmcore'.
946 */
947 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
948 {
949 struct fadump_crash_info_header *fdh;
950 int rc = 0;
951
952 if (!fdm_active || !fw_dump.fadumphdr_addr)
953 return -EINVAL;
954
955 /* Check if the dump data is valid. */
956 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
957 (fdm_active->cpu_state_data.error_flags != 0) ||
958 (fdm_active->rmr_region.error_flags != 0)) {
959 printk(KERN_ERR "Dump taken by platform is not valid\n");
960 return -EINVAL;
961 }
962 if ((fdm_active->rmr_region.bytes_dumped !=
963 fdm_active->rmr_region.source_len) ||
964 !fdm_active->cpu_state_data.bytes_dumped) {
965 printk(KERN_ERR "Dump taken by platform is incomplete\n");
966 return -EINVAL;
967 }
968
969 /* Validate the fadump crash info header */
970 fdh = __va(fw_dump.fadumphdr_addr);
971 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
972 printk(KERN_ERR "Crash info header is not valid.\n");
973 return -EINVAL;
974 }
975
976 rc = fadump_build_cpu_notes(fdm_active);
977 if (rc)
978 return rc;
979
980 /*
981 * We are done validating dump info and elfcore header is now ready
982 * to be exported. set elfcorehdr_addr so that vmcore module will
983 * export the elfcore header through '/proc/vmcore'.
984 */
985 elfcorehdr_addr = fdh->elfcorehdr_addr;
986
987 return 0;
988 }
989
990 static void free_crash_memory_ranges(void)
991 {
992 kfree(crash_memory_ranges);
993 crash_memory_ranges = NULL;
994 crash_memory_ranges_size = 0;
995 max_crash_mem_ranges = 0;
996 }
997
998 /*
999 * Allocate or reallocate crash memory ranges array in incremental units
1000 * of PAGE_SIZE.
1001 */
1002 static int allocate_crash_memory_ranges(void)
1003 {
1004 struct fad_crash_memory_ranges *new_array;
1005 u64 new_size;
1006
1007 new_size = crash_memory_ranges_size + PAGE_SIZE;
1008 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
1009 new_size);
1010
1011 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
1012 if (new_array == NULL) {
1013 pr_err("Insufficient memory for setting up crash memory ranges\n");
1014 free_crash_memory_ranges();
1015 return -ENOMEM;
1016 }
1017
1018 crash_memory_ranges = new_array;
1019 crash_memory_ranges_size = new_size;
1020 max_crash_mem_ranges = (new_size /
1021 sizeof(struct fad_crash_memory_ranges));
1022 return 0;
1023 }
1024
1025 static inline int fadump_add_crash_memory(unsigned long long base,
1026 unsigned long long end)
1027 {
1028 u64 start, size;
1029 bool is_adjacent = false;
1030
1031 if (base == end)
1032 return 0;
1033
1034 /*
1035 * Fold adjacent memory ranges to bring down the memory ranges/
1036 * PT_LOAD segments count.
1037 */
1038 if (crash_mem_ranges) {
1039 start = crash_memory_ranges[crash_mem_ranges - 1].base;
1040 size = crash_memory_ranges[crash_mem_ranges - 1].size;
1041
1042 if ((start + size) == base)
1043 is_adjacent = true;
1044 }
1045 if (!is_adjacent) {
1046 /* resize the array on reaching the limit */
1047 if (crash_mem_ranges == max_crash_mem_ranges) {
1048 int ret;
1049
1050 ret = allocate_crash_memory_ranges();
1051 if (ret)
1052 return ret;
1053 }
1054
1055 start = base;
1056 crash_memory_ranges[crash_mem_ranges].base = start;
1057 crash_mem_ranges++;
1058 }
1059
1060 crash_memory_ranges[crash_mem_ranges - 1].size = (end - start);
1061 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
1062 (crash_mem_ranges - 1), start, end - 1, (end - start));
1063 return 0;
1064 }
1065
1066 static int fadump_exclude_reserved_area(unsigned long long start,
1067 unsigned long long end)
1068 {
1069 unsigned long long ra_start, ra_end;
1070 int ret = 0;
1071
1072 ra_start = fw_dump.reserve_dump_area_start;
1073 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1074
1075 if ((ra_start < end) && (ra_end > start)) {
1076 if ((start < ra_start) && (end > ra_end)) {
1077 ret = fadump_add_crash_memory(start, ra_start);
1078 if (ret)
1079 return ret;
1080
1081 ret = fadump_add_crash_memory(ra_end, end);
1082 } else if (start < ra_start) {
1083 ret = fadump_add_crash_memory(start, ra_start);
1084 } else if (ra_end < end) {
1085 ret = fadump_add_crash_memory(ra_end, end);
1086 }
1087 } else
1088 ret = fadump_add_crash_memory(start, end);
1089
1090 return ret;
1091 }
1092
1093 static int fadump_init_elfcore_header(char *bufp)
1094 {
1095 struct elfhdr *elf;
1096
1097 elf = (struct elfhdr *) bufp;
1098 bufp += sizeof(struct elfhdr);
1099 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1100 elf->e_ident[EI_CLASS] = ELF_CLASS;
1101 elf->e_ident[EI_DATA] = ELF_DATA;
1102 elf->e_ident[EI_VERSION] = EV_CURRENT;
1103 elf->e_ident[EI_OSABI] = ELF_OSABI;
1104 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1105 elf->e_type = ET_CORE;
1106 elf->e_machine = ELF_ARCH;
1107 elf->e_version = EV_CURRENT;
1108 elf->e_entry = 0;
1109 elf->e_phoff = sizeof(struct elfhdr);
1110 elf->e_shoff = 0;
1111 #if defined(_CALL_ELF)
1112 elf->e_flags = _CALL_ELF;
1113 #else
1114 elf->e_flags = 0;
1115 #endif
1116 elf->e_ehsize = sizeof(struct elfhdr);
1117 elf->e_phentsize = sizeof(struct elf_phdr);
1118 elf->e_phnum = 0;
1119 elf->e_shentsize = 0;
1120 elf->e_shnum = 0;
1121 elf->e_shstrndx = 0;
1122
1123 return 0;
1124 }
1125
1126 /*
1127 * Traverse through memblock structure and setup crash memory ranges. These
1128 * ranges will be used create PT_LOAD program headers in elfcore header.
1129 */
1130 static int fadump_setup_crash_memory_ranges(void)
1131 {
1132 struct memblock_region *reg;
1133 unsigned long long start, end;
1134 int ret;
1135
1136 pr_debug("Setup crash memory ranges.\n");
1137 crash_mem_ranges = 0;
1138
1139 /*
1140 * add the first memory chunk (RMA_START through boot_memory_size) as
1141 * a separate memory chunk. The reason is, at the time crash firmware
1142 * will move the content of this memory chunk to different location
1143 * specified during fadump registration. We need to create a separate
1144 * program header for this chunk with the correct offset.
1145 */
1146 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
1147 if (ret)
1148 return ret;
1149
1150 for_each_memblock(memory, reg) {
1151 start = (unsigned long long)reg->base;
1152 end = start + (unsigned long long)reg->size;
1153
1154 /*
1155 * skip the first memory chunk that is already added (RMA_START
1156 * through boot_memory_size). This logic needs a relook if and
1157 * when RMA_START changes to a non-zero value.
1158 */
1159 BUILD_BUG_ON(RMA_START != 0);
1160 if (start < fw_dump.boot_memory_size) {
1161 if (end > fw_dump.boot_memory_size)
1162 start = fw_dump.boot_memory_size;
1163 else
1164 continue;
1165 }
1166
1167 /* add this range excluding the reserved dump area. */
1168 ret = fadump_exclude_reserved_area(start, end);
1169 if (ret)
1170 return ret;
1171 }
1172
1173 return 0;
1174 }
1175
1176 /*
1177 * If the given physical address falls within the boot memory region then
1178 * return the relocated address that points to the dump region reserved
1179 * for saving initial boot memory contents.
1180 */
1181 static inline unsigned long fadump_relocate(unsigned long paddr)
1182 {
1183 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
1184 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
1185 else
1186 return paddr;
1187 }
1188
1189 static int fadump_create_elfcore_headers(char *bufp)
1190 {
1191 struct elfhdr *elf;
1192 struct elf_phdr *phdr;
1193 int i;
1194
1195 fadump_init_elfcore_header(bufp);
1196 elf = (struct elfhdr *)bufp;
1197 bufp += sizeof(struct elfhdr);
1198
1199 /*
1200 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1201 * will be populated during second kernel boot after crash. Hence
1202 * this PT_NOTE will always be the first elf note.
1203 *
1204 * NOTE: Any new ELF note addition should be placed after this note.
1205 */
1206 phdr = (struct elf_phdr *)bufp;
1207 bufp += sizeof(struct elf_phdr);
1208 phdr->p_type = PT_NOTE;
1209 phdr->p_flags = 0;
1210 phdr->p_vaddr = 0;
1211 phdr->p_align = 0;
1212
1213 phdr->p_offset = 0;
1214 phdr->p_paddr = 0;
1215 phdr->p_filesz = 0;
1216 phdr->p_memsz = 0;
1217
1218 (elf->e_phnum)++;
1219
1220 /* setup ELF PT_NOTE for vmcoreinfo */
1221 phdr = (struct elf_phdr *)bufp;
1222 bufp += sizeof(struct elf_phdr);
1223 phdr->p_type = PT_NOTE;
1224 phdr->p_flags = 0;
1225 phdr->p_vaddr = 0;
1226 phdr->p_align = 0;
1227
1228 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
1229 phdr->p_offset = phdr->p_paddr;
1230 phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1231
1232 /* Increment number of program headers. */
1233 (elf->e_phnum)++;
1234
1235 /* setup PT_LOAD sections. */
1236
1237 for (i = 0; i < crash_mem_ranges; i++) {
1238 unsigned long long mbase, msize;
1239 mbase = crash_memory_ranges[i].base;
1240 msize = crash_memory_ranges[i].size;
1241
1242 if (!msize)
1243 continue;
1244
1245 phdr = (struct elf_phdr *)bufp;
1246 bufp += sizeof(struct elf_phdr);
1247 phdr->p_type = PT_LOAD;
1248 phdr->p_flags = PF_R|PF_W|PF_X;
1249 phdr->p_offset = mbase;
1250
1251 if (mbase == RMA_START) {
1252 /*
1253 * The entire RMA region will be moved by firmware
1254 * to the specified destination_address. Hence set
1255 * the correct offset.
1256 */
1257 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
1258 }
1259
1260 phdr->p_paddr = mbase;
1261 phdr->p_vaddr = (unsigned long)__va(mbase);
1262 phdr->p_filesz = msize;
1263 phdr->p_memsz = msize;
1264 phdr->p_align = 0;
1265
1266 /* Increment number of program headers. */
1267 (elf->e_phnum)++;
1268 }
1269 return 0;
1270 }
1271
1272 static unsigned long init_fadump_header(unsigned long addr)
1273 {
1274 struct fadump_crash_info_header *fdh;
1275
1276 if (!addr)
1277 return 0;
1278
1279 fw_dump.fadumphdr_addr = addr;
1280 fdh = __va(addr);
1281 addr += sizeof(struct fadump_crash_info_header);
1282
1283 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1284 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1285 fdh->elfcorehdr_addr = addr;
1286 /* We will set the crashing cpu id in crash_fadump() during crash. */
1287 fdh->crashing_cpu = CPU_UNKNOWN;
1288
1289 return addr;
1290 }
1291
1292 static int register_fadump(void)
1293 {
1294 unsigned long addr;
1295 void *vaddr;
1296 int ret;
1297
1298 /*
1299 * If no memory is reserved then we can not register for firmware-
1300 * assisted dump.
1301 */
1302 if (!fw_dump.reserve_dump_area_size)
1303 return -ENODEV;
1304
1305 ret = fadump_setup_crash_memory_ranges();
1306 if (ret)
1307 return ret;
1308
1309 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
1310 /* Initialize fadump crash info header. */
1311 addr = init_fadump_header(addr);
1312 vaddr = __va(addr);
1313
1314 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1315 fadump_create_elfcore_headers(vaddr);
1316
1317 /* register the future kernel dump with firmware. */
1318 return register_fw_dump(&fdm);
1319 }
1320
1321 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
1322 {
1323 int rc = 0;
1324 unsigned int wait_time;
1325
1326 pr_debug("Un-register firmware-assisted dump\n");
1327
1328 /* TODO: Add upper time limit for the delay */
1329 do {
1330 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1331 FADUMP_UNREGISTER, fdm,
1332 sizeof(struct fadump_mem_struct));
1333
1334 wait_time = rtas_busy_delay_time(rc);
1335 if (wait_time)
1336 mdelay(wait_time);
1337 } while (wait_time);
1338
1339 if (rc) {
1340 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1341 " unexpected error(%d).\n", rc);
1342 return rc;
1343 }
1344 fw_dump.dump_registered = 0;
1345 return 0;
1346 }
1347
1348 static int fadump_invalidate_dump(const struct fadump_mem_struct *fdm)
1349 {
1350 int rc = 0;
1351 unsigned int wait_time;
1352
1353 pr_debug("Invalidating firmware-assisted dump registration\n");
1354
1355 /* TODO: Add upper time limit for the delay */
1356 do {
1357 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1358 FADUMP_INVALIDATE, fdm,
1359 sizeof(struct fadump_mem_struct));
1360
1361 wait_time = rtas_busy_delay_time(rc);
1362 if (wait_time)
1363 mdelay(wait_time);
1364 } while (wait_time);
1365
1366 if (rc) {
1367 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1368 return rc;
1369 }
1370 fw_dump.dump_active = 0;
1371 fdm_active = NULL;
1372 return 0;
1373 }
1374
1375 void fadump_cleanup(void)
1376 {
1377 /* Invalidate the registration only if dump is active. */
1378 if (fw_dump.dump_active) {
1379 /* pass the same memory dump structure provided by platform */
1380 fadump_invalidate_dump(fdm_active);
1381 } else if (fw_dump.dump_registered) {
1382 /* Un-register Firmware-assisted dump if it was registered. */
1383 fadump_unregister_dump(&fdm);
1384 free_crash_memory_ranges();
1385 }
1386 }
1387
1388 static void fadump_free_reserved_memory(unsigned long start_pfn,
1389 unsigned long end_pfn)
1390 {
1391 unsigned long pfn;
1392 unsigned long time_limit = jiffies + HZ;
1393
1394 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1395 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1396
1397 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1398 free_reserved_page(pfn_to_page(pfn));
1399
1400 if (time_after(jiffies, time_limit)) {
1401 cond_resched();
1402 time_limit = jiffies + HZ;
1403 }
1404 }
1405 }
1406
1407 /*
1408 * Skip memory holes and free memory that was actually reserved.
1409 */
1410 static void fadump_release_reserved_area(unsigned long start, unsigned long end)
1411 {
1412 struct memblock_region *reg;
1413 unsigned long tstart, tend;
1414 unsigned long start_pfn = PHYS_PFN(start);
1415 unsigned long end_pfn = PHYS_PFN(end);
1416
1417 for_each_memblock(memory, reg) {
1418 tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
1419 tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
1420 if (tstart < tend) {
1421 fadump_free_reserved_memory(tstart, tend);
1422
1423 if (tend == end_pfn)
1424 break;
1425
1426 start_pfn = tend + 1;
1427 }
1428 }
1429 }
1430
1431 /*
1432 * Release the memory that was reserved in early boot to preserve the memory
1433 * contents. The released memory will be available for general use.
1434 */
1435 static void fadump_release_memory(unsigned long begin, unsigned long end)
1436 {
1437 unsigned long ra_start, ra_end;
1438
1439 ra_start = fw_dump.reserve_dump_area_start;
1440 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1441
1442 /*
1443 * exclude the dump reserve area. Will reuse it for next
1444 * fadump registration.
1445 */
1446 if (begin < ra_end && end > ra_start) {
1447 if (begin < ra_start)
1448 fadump_release_reserved_area(begin, ra_start);
1449 if (end > ra_end)
1450 fadump_release_reserved_area(ra_end, end);
1451 } else
1452 fadump_release_reserved_area(begin, end);
1453 }
1454
1455 static void fadump_invalidate_release_mem(void)
1456 {
1457 unsigned long reserved_area_start, reserved_area_end;
1458 unsigned long destination_address;
1459
1460 mutex_lock(&fadump_mutex);
1461 if (!fw_dump.dump_active) {
1462 mutex_unlock(&fadump_mutex);
1463 return;
1464 }
1465
1466 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1467 fadump_cleanup();
1468 mutex_unlock(&fadump_mutex);
1469
1470 /*
1471 * Save the current reserved memory bounds we will require them
1472 * later for releasing the memory for general use.
1473 */
1474 reserved_area_start = fw_dump.reserve_dump_area_start;
1475 reserved_area_end = reserved_area_start +
1476 fw_dump.reserve_dump_area_size;
1477 /*
1478 * Setup reserve_dump_area_start and its size so that we can
1479 * reuse this reserved memory for Re-registration.
1480 */
1481 fw_dump.reserve_dump_area_start = destination_address;
1482 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1483
1484 fadump_release_memory(reserved_area_start, reserved_area_end);
1485 if (fw_dump.cpu_notes_buf) {
1486 fadump_cpu_notes_buf_free(
1487 (unsigned long)__va(fw_dump.cpu_notes_buf),
1488 fw_dump.cpu_notes_buf_size);
1489 fw_dump.cpu_notes_buf = 0;
1490 fw_dump.cpu_notes_buf_size = 0;
1491 }
1492 /* Initialize the kernel dump memory structure for FAD registration. */
1493 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1494 }
1495
1496 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1497 struct kobj_attribute *attr,
1498 const char *buf, size_t count)
1499 {
1500 int input = -1;
1501
1502 if (!fw_dump.dump_active)
1503 return -EPERM;
1504
1505 if (kstrtoint(buf, 0, &input))
1506 return -EINVAL;
1507
1508 if (input == 1) {
1509 /*
1510 * Take away the '/proc/vmcore'. We are releasing the dump
1511 * memory, hence it will not be valid anymore.
1512 */
1513 #ifdef CONFIG_PROC_VMCORE
1514 vmcore_cleanup();
1515 #endif
1516 fadump_invalidate_release_mem();
1517
1518 } else
1519 return -EINVAL;
1520 return count;
1521 }
1522
1523 static ssize_t fadump_enabled_show(struct kobject *kobj,
1524 struct kobj_attribute *attr,
1525 char *buf)
1526 {
1527 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1528 }
1529
1530 static ssize_t fadump_register_show(struct kobject *kobj,
1531 struct kobj_attribute *attr,
1532 char *buf)
1533 {
1534 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1535 }
1536
1537 static ssize_t fadump_register_store(struct kobject *kobj,
1538 struct kobj_attribute *attr,
1539 const char *buf, size_t count)
1540 {
1541 int ret = 0;
1542 int input = -1;
1543
1544 if (!fw_dump.fadump_enabled || fdm_active)
1545 return -EPERM;
1546
1547 if (kstrtoint(buf, 0, &input))
1548 return -EINVAL;
1549
1550 mutex_lock(&fadump_mutex);
1551
1552 switch (input) {
1553 case 0:
1554 if (fw_dump.dump_registered == 0) {
1555 goto unlock_out;
1556 }
1557 /* Un-register Firmware-assisted dump */
1558 fadump_unregister_dump(&fdm);
1559 break;
1560 case 1:
1561 if (fw_dump.dump_registered == 1) {
1562 /* Un-register Firmware-assisted dump */
1563 fadump_unregister_dump(&fdm);
1564 }
1565 /* Register Firmware-assisted dump */
1566 ret = register_fadump();
1567 break;
1568 default:
1569 ret = -EINVAL;
1570 break;
1571 }
1572
1573 unlock_out:
1574 mutex_unlock(&fadump_mutex);
1575 return ret < 0 ? ret : count;
1576 }
1577
1578 static int fadump_region_show(struct seq_file *m, void *private)
1579 {
1580 const struct fadump_mem_struct *fdm_ptr;
1581
1582 if (!fw_dump.fadump_enabled)
1583 return 0;
1584
1585 mutex_lock(&fadump_mutex);
1586 if (fdm_active)
1587 fdm_ptr = fdm_active;
1588 else {
1589 mutex_unlock(&fadump_mutex);
1590 fdm_ptr = &fdm;
1591 }
1592
1593 seq_printf(m,
1594 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1595 "Dumped: %#llx\n",
1596 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1597 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1598 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1599 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1600 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1601 seq_printf(m,
1602 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1603 "Dumped: %#llx\n",
1604 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1605 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1606 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1607 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1608 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1609 seq_printf(m,
1610 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1611 "Dumped: %#llx\n",
1612 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1613 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1614 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1615 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1616 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1617
1618 if (!fdm_active ||
1619 (fw_dump.reserve_dump_area_start ==
1620 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1621 goto out;
1622
1623 /* Dump is active. Show reserved memory region. */
1624 seq_printf(m,
1625 " : [%#016llx-%#016llx] %#llx bytes, "
1626 "Dumped: %#llx\n",
1627 (unsigned long long)fw_dump.reserve_dump_area_start,
1628 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1629 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1630 fw_dump.reserve_dump_area_start,
1631 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1632 fw_dump.reserve_dump_area_start);
1633 out:
1634 if (fdm_active)
1635 mutex_unlock(&fadump_mutex);
1636 return 0;
1637 }
1638
1639 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1640 0200, NULL,
1641 fadump_release_memory_store);
1642 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1643 0444, fadump_enabled_show,
1644 NULL);
1645 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1646 0644, fadump_register_show,
1647 fadump_register_store);
1648
1649 DEFINE_SHOW_ATTRIBUTE(fadump_region);
1650
1651 static void fadump_init_files(void)
1652 {
1653 struct dentry *debugfs_file;
1654 int rc = 0;
1655
1656 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1657 if (rc)
1658 printk(KERN_ERR "fadump: unable to create sysfs file"
1659 " fadump_enabled (%d)\n", rc);
1660
1661 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1662 if (rc)
1663 printk(KERN_ERR "fadump: unable to create sysfs file"
1664 " fadump_registered (%d)\n", rc);
1665
1666 debugfs_file = debugfs_create_file("fadump_region", 0444,
1667 powerpc_debugfs_root, NULL,
1668 &fadump_region_fops);
1669 if (!debugfs_file)
1670 printk(KERN_ERR "fadump: unable to create debugfs file"
1671 " fadump_region\n");
1672
1673 if (fw_dump.dump_active) {
1674 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1675 if (rc)
1676 printk(KERN_ERR "fadump: unable to create sysfs file"
1677 " fadump_release_mem (%d)\n", rc);
1678 }
1679 return;
1680 }
1681
1682 /*
1683 * Prepare for firmware-assisted dump.
1684 */
1685 int __init setup_fadump(void)
1686 {
1687 if (!fw_dump.fadump_enabled)
1688 return 0;
1689
1690 if (!fw_dump.fadump_supported) {
1691 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1692 " this hardware\n");
1693 return 0;
1694 }
1695
1696 fadump_show_config();
1697 /*
1698 * If dump data is available then see if it is valid and prepare for
1699 * saving it to the disk.
1700 */
1701 if (fw_dump.dump_active) {
1702 /*
1703 * if dump process fails then invalidate the registration
1704 * and release memory before proceeding for re-registration.
1705 */
1706 if (process_fadump(fdm_active) < 0)
1707 fadump_invalidate_release_mem();
1708 }
1709 /* Initialize the kernel dump memory structure for FAD registration. */
1710 else if (fw_dump.reserve_dump_area_size)
1711 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1712 fadump_init_files();
1713
1714 return 1;
1715 }
1716 subsys_initcall(setup_fadump);