1 // SPDX-License-Identifier: GPL-2.0-only
3 * ppc64 code to implement the kexec_file_load syscall
5 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
6 * Copyright (C) 2004 IBM Corp.
7 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
8 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
9 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
10 * Copyright (C) 2020 IBM Corporation
12 * Based on kexec-tools' kexec-ppc64.c, kexec-elf-rel-ppc64.c, fs2dt.c.
13 * Heavily modified for the kernel by
14 * Hari Bathini, IBM Corporation.
17 #include <linux/kexec.h>
18 #include <linux/of_fdt.h>
19 #include <linux/libfdt.h>
20 #include <linux/of_device.h>
21 #include <linux/memblock.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <asm/setup.h>
25 #include <asm/drmem.h>
26 #include <asm/kexec_ranges.h>
27 #include <asm/crashdump-ppc64.h>
30 u64 *buf; /* data buffer for usable-memory property */
31 u32 size; /* size allocated for the data buffer */
32 u32 max_entries; /* maximum no. of entries */
33 u32 idx; /* index of current entry */
35 /* usable memory ranges to look up */
36 unsigned int nr_ranges;
37 const struct crash_mem_range *ranges;
40 const struct kexec_file_ops * const kexec_file_loaders[] = {
46 * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
47 * regions like opal/rtas, tce-table, initrd,
48 * kernel, htab which should be avoided while
49 * setting up kexec load segments.
50 * @mem_ranges: Range list to add the memory ranges to.
52 * Returns 0 on success, negative errno on error.
54 static int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
58 ret = add_tce_mem_ranges(mem_ranges);
62 ret = add_initrd_mem_range(mem_ranges);
66 ret = add_htab_mem_range(mem_ranges);
70 ret = add_kernel_mem_range(mem_ranges);
74 ret = add_rtas_mem_range(mem_ranges);
78 ret = add_opal_mem_range(mem_ranges);
82 ret = add_reserved_mem_ranges(mem_ranges);
86 /* exclude memory ranges should be sorted for easy lookup */
87 sort_memory_ranges(*mem_ranges, true);
90 pr_err("Failed to setup exclude memory ranges\n");
95 * get_usable_memory_ranges - Get usable memory ranges. This list includes
96 * regions like crashkernel, opal/rtas & tce-table,
97 * that kdump kernel could use.
98 * @mem_ranges: Range list to add the memory ranges to.
100 * Returns 0 on success, negative errno on error.
102 static int get_usable_memory_ranges(struct crash_mem **mem_ranges)
107 * Early boot failure observed on guests when low memory (first memory
108 * block?) is not added to usable memory. So, add [0, crashk_res.end]
109 * instead of [crashk_res.start, crashk_res.end] to workaround it.
110 * Also, crashed kernel's memory must be added to reserve map to
111 * avoid kdump kernel from using it.
113 ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
117 ret = add_rtas_mem_range(mem_ranges);
121 ret = add_opal_mem_range(mem_ranges);
125 ret = add_tce_mem_ranges(mem_ranges);
128 pr_err("Failed to setup usable memory ranges\n");
133 * get_crash_memory_ranges - Get crash memory ranges. This list includes
134 * first/crashing kernel's memory regions that
135 * would be exported via an elfcore.
136 * @mem_ranges: Range list to add the memory ranges to.
138 * Returns 0 on success, negative errno on error.
140 static int get_crash_memory_ranges(struct crash_mem **mem_ranges)
142 phys_addr_t base, end;
143 struct crash_mem *tmem;
147 for_each_mem_range(i, &base, &end) {
148 u64 size = end - base;
150 /* Skip backup memory region, which needs a separate entry */
151 if (base == BACKUP_SRC_START) {
152 if (size > BACKUP_SRC_SIZE) {
153 base = BACKUP_SRC_END + 1;
154 size -= BACKUP_SRC_SIZE;
159 ret = add_mem_range(mem_ranges, base, size);
163 /* Try merging adjacent ranges before reallocation attempt */
164 if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
165 sort_memory_ranges(*mem_ranges, true);
168 /* Reallocate memory ranges if there is no space to split ranges */
170 if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
171 tmem = realloc_mem_ranges(mem_ranges);
176 /* Exclude crashkernel region */
177 ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
182 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
183 * regions are exported to save their context at the time of
184 * crash, they should actually be backed up just like the
185 * first 64K bytes of memory.
187 ret = add_rtas_mem_range(mem_ranges);
191 ret = add_opal_mem_range(mem_ranges);
195 /* create a separate program header for the backup region */
196 ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
200 sort_memory_ranges(*mem_ranges, false);
203 pr_err("Failed to setup crash memory ranges\n");
208 * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
209 * memory regions that should be added to the
210 * memory reserve map to ensure the region is
211 * protected from any mischief.
212 * @mem_ranges: Range list to add the memory ranges to.
214 * Returns 0 on success, negative errno on error.
216 static int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
220 ret = add_rtas_mem_range(mem_ranges);
224 ret = add_tce_mem_ranges(mem_ranges);
228 ret = add_reserved_mem_ranges(mem_ranges);
231 pr_err("Failed to setup reserved memory ranges\n");
236 * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
237 * in the memory regions between buf_min & buf_max
238 * for the buffer. If found, sets kbuf->mem.
239 * @kbuf: Buffer contents and memory parameters.
240 * @buf_min: Minimum address for the buffer.
241 * @buf_max: Maximum address for the buffer.
243 * Returns 0 on success, negative errno on error.
245 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
246 u64 buf_min, u64 buf_max)
248 int ret = -EADDRNOTAVAIL;
249 phys_addr_t start, end;
252 for_each_mem_range_rev(i, &start, &end) {
254 * memblock uses [start, end) convention while it is
255 * [start, end] here. Fix the off-by-one to have the
263 /* Memory hole not found */
267 /* Adjust memory region based on the given range */
273 start = ALIGN(start, kbuf->buf_align);
274 if (start < end && (end - start + 1) >= kbuf->memsz) {
275 /* Suitable memory range found. Set kbuf->mem */
276 kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
287 * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
288 * suitable buffer with top down approach.
289 * @kbuf: Buffer contents and memory parameters.
290 * @buf_min: Minimum address for the buffer.
291 * @buf_max: Maximum address for the buffer.
292 * @emem: Exclude memory ranges.
294 * Returns 0 on success, negative errno on error.
296 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
297 u64 buf_min, u64 buf_max,
298 const struct crash_mem *emem)
300 int i, ret = 0, err = -EADDRNOTAVAIL;
301 u64 start, end, tmin, tmax;
304 for (i = (emem->nr_ranges - 1); i >= 0; i--) {
305 start = emem->ranges[i].start;
306 end = emem->ranges[i].end;
312 tmin = (end < buf_min ? buf_min : end + 1);
313 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
320 if (tmax < buf_min) {
329 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
335 * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
336 * in the memory regions between buf_min & buf_max
337 * for the buffer. If found, sets kbuf->mem.
338 * @kbuf: Buffer contents and memory parameters.
339 * @buf_min: Minimum address for the buffer.
340 * @buf_max: Maximum address for the buffer.
342 * Returns 0 on success, negative errno on error.
344 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
345 u64 buf_min, u64 buf_max)
347 int ret = -EADDRNOTAVAIL;
348 phys_addr_t start, end;
351 for_each_mem_range(i, &start, &end) {
353 * memblock uses [start, end) convention while it is
354 * [start, end] here. Fix the off-by-one to have the
362 /* Memory hole not found */
366 /* Adjust memory region based on the given range */
372 start = ALIGN(start, kbuf->buf_align);
373 if (start < end && (end - start + 1) >= kbuf->memsz) {
374 /* Suitable memory range found. Set kbuf->mem */
385 * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
386 * suitable buffer with bottom up approach.
387 * @kbuf: Buffer contents and memory parameters.
388 * @buf_min: Minimum address for the buffer.
389 * @buf_max: Maximum address for the buffer.
390 * @emem: Exclude memory ranges.
392 * Returns 0 on success, negative errno on error.
394 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
395 u64 buf_min, u64 buf_max,
396 const struct crash_mem *emem)
398 int i, ret = 0, err = -EADDRNOTAVAIL;
399 u64 start, end, tmin, tmax;
402 for (i = 0; i < emem->nr_ranges; i++) {
403 start = emem->ranges[i].start;
404 end = emem->ranges[i].end;
410 tmax = (start > buf_max ? buf_max : start - 1);
411 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
418 if (tmin > buf_max) {
427 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
433 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
434 * @um_info: Usable memory buffer and ranges info.
435 * @cnt: No. of entries to accommodate.
437 * Frees up the old buffer if memory reallocation fails.
439 * Returns buffer on success, NULL on error.
441 static u64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
446 if ((um_info->idx + cnt) <= um_info->max_entries)
449 new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
450 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
453 um_info->size = new_size;
454 um_info->max_entries = (um_info->size / sizeof(u64));
461 * add_usable_mem - Add the usable memory ranges within the given memory range
463 * @um_info: Usable memory buffer and ranges info.
464 * @base: Base address of memory range to look for.
465 * @end: End address of memory range to look for.
467 * Returns 0 on success, negative errno on error.
469 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
471 u64 loc_base, loc_end;
475 for (i = 0; i < um_info->nr_ranges; i++) {
477 loc_base = um_info->ranges[i].start;
478 loc_end = um_info->ranges[i].end;
479 if (loc_base >= base && loc_end <= end)
481 else if (base < loc_end && end > loc_base) {
490 if (!check_realloc_usable_mem(um_info, 2))
493 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
494 um_info->buf[um_info->idx++] =
495 cpu_to_be64(loc_end - loc_base + 1);
503 * kdump_setup_usable_lmb - This is a callback function that gets called by
504 * walk_drmem_lmbs for every LMB to set its
505 * usable memory ranges.
507 * @usm: linux,drconf-usable-memory property value.
508 * @data: Pointer to usable memory buffer and ranges info.
510 * Returns 0 on success, negative errno on error.
512 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
515 struct umem_info *um_info;
520 * kdump load isn't supported on kernels already booted with
521 * linux,drconf-usable-memory property.
524 pr_err("linux,drconf-usable-memory property already exists!");
529 tmp_idx = um_info->idx;
530 if (!check_realloc_usable_mem(um_info, 1))
534 base = lmb->base_addr;
535 end = base + drmem_lmb_size() - 1;
536 ret = add_usable_mem(um_info, base, end);
539 * Update the no. of ranges added. Two entries (base & size)
540 * for every range added.
542 um_info->buf[tmp_idx] =
543 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
549 #define NODE_PATH_LEN 256
551 * add_usable_mem_property - Add usable memory property for the given
553 * @fdt: Flattened device tree for the kdump kernel.
555 * @um_info: Usable memory buffer and ranges info.
557 * Returns 0 on success, negative errno on error.
559 static int add_usable_mem_property(void *fdt, struct device_node *dn,
560 struct umem_info *um_info)
562 int n_mem_addr_cells, n_mem_size_cells, node;
563 char path[NODE_PATH_LEN];
564 int i, len, ranges, ret;
570 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
571 pr_err("Buffer (%d) too small for memory node: %pOF\n",
575 pr_debug("Memory node path: %s\n", path);
577 /* Now that we know the path, find its offset in kdump kernel's fdt */
578 node = fdt_path_offset(fdt, path);
580 pr_err("Malformed device tree: error reading %s\n", path);
585 /* Get the address & size cells */
586 n_mem_addr_cells = of_n_addr_cells(dn);
587 n_mem_size_cells = of_n_size_cells(dn);
588 pr_debug("address cells: %d, size cells: %d\n", n_mem_addr_cells,
592 if (!check_realloc_usable_mem(um_info, 2)) {
597 prop = of_get_property(dn, "reg", &len);
598 if (!prop || len <= 0) {
604 * "reg" property represents sequence of (addr,size) tuples
605 * each representing a memory range.
607 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
609 for (i = 0; i < ranges; i++) {
610 base = of_read_number(prop, n_mem_addr_cells);
611 prop += n_mem_addr_cells;
612 end = base + of_read_number(prop, n_mem_size_cells) - 1;
613 prop += n_mem_size_cells;
615 ret = add_usable_mem(um_info, base, end);
621 * No kdump kernel usable memory found in this memory node.
622 * Write (0,0) tuple in linux,usable-memory property for
623 * this region to be ignored.
625 if (um_info->idx == 0) {
631 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
632 (um_info->idx * sizeof(u64)));
641 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
642 * and linux,drconf-usable-memory DT properties as
643 * appropriate to restrict its memory usage.
644 * @fdt: Flattened device tree for the kdump kernel.
645 * @usable_mem: Usable memory ranges for kdump kernel.
647 * Returns 0 on success, negative errno on error.
649 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
651 struct umem_info um_info;
652 struct device_node *dn;
656 pr_err("Usable memory ranges for kdump kernel not found\n");
660 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
661 if (node == -FDT_ERR_NOTFOUND)
662 pr_debug("No dynamic reconfiguration memory found\n");
664 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
670 um_info.max_entries = 0;
672 /* Memory ranges to look up */
673 um_info.ranges = &(usable_mem->ranges[0]);
674 um_info.nr_ranges = usable_mem->nr_ranges;
676 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
678 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
682 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
686 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
687 um_info.buf, (um_info.idx * sizeof(u64)));
689 pr_err("Failed to update fdt with linux,drconf-usable-memory property");
695 * Walk through each memory node and set linux,usable-memory property
696 * for the corresponding node in kdump kernel's fdt.
698 for_each_node_by_type(dn, "memory") {
699 ret = add_usable_mem_property(fdt, dn, &um_info);
701 pr_err("Failed to set linux,usable-memory property for %s node",
713 * load_backup_segment - Locate a memory hole to place the backup region.
714 * @image: Kexec image.
715 * @kbuf: Buffer contents and memory parameters.
717 * Returns 0 on success, negative errno on error.
719 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
725 * Setup a source buffer for backup segment.
727 * A source buffer has no meaning for backup region as data will
728 * be copied from backup source, after crash, in the purgatory.
729 * But as load segment code doesn't recognize such segments,
730 * setup a dummy source buffer to keep it happy for now.
732 buf = vzalloc(BACKUP_SRC_SIZE);
737 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
738 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
739 kbuf->top_down = false;
741 ret = kexec_add_buffer(kbuf);
747 image->arch.backup_buf = buf;
748 image->arch.backup_start = kbuf->mem;
753 * update_backup_region_phdr - Update backup region's offset for the core to
754 * export the region appropriately.
755 * @image: Kexec image.
756 * @ehdr: ELF core header.
758 * Assumes an exclusive program header is setup for the backup region
763 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
768 phdr = (Elf64_Phdr *)(ehdr + 1);
769 for (i = 0; i < ehdr->e_phnum; i++) {
770 if (phdr->p_paddr == BACKUP_SRC_START) {
771 phdr->p_offset = image->arch.backup_start;
772 pr_debug("Backup region offset updated to 0x%lx\n",
773 image->arch.backup_start);
780 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
781 * segment needed to load kdump kernel.
782 * @image: Kexec image.
783 * @kbuf: Buffer contents and memory parameters.
785 * Returns 0 on success, negative errno on error.
787 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
789 struct crash_mem *cmem = NULL;
790 unsigned long headers_sz;
791 void *headers = NULL;
794 ret = get_crash_memory_ranges(&cmem);
798 /* Setup elfcorehdr segment */
799 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
801 pr_err("Failed to prepare elf headers for the core\n");
805 /* Fix the offset for backup region in the ELF header */
806 update_backup_region_phdr(image, headers);
808 kbuf->buffer = headers;
809 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
810 kbuf->bufsz = kbuf->memsz = headers_sz;
811 kbuf->top_down = false;
813 ret = kexec_add_buffer(kbuf);
819 image->arch.elfcorehdr_addr = kbuf->mem;
820 image->arch.elf_headers_sz = headers_sz;
821 image->arch.elf_headers = headers;
828 * load_crashdump_segments_ppc64 - Initialize the additional segements needed
829 * to load kdump kernel.
830 * @image: Kexec image.
831 * @kbuf: Buffer contents and memory parameters.
833 * Returns 0 on success, negative errno on error.
835 int load_crashdump_segments_ppc64(struct kimage *image,
836 struct kexec_buf *kbuf)
840 /* Load backup segment - first 64K bytes of the crashing kernel */
841 ret = load_backup_segment(image, kbuf);
843 pr_err("Failed to load backup segment\n");
846 pr_debug("Loaded the backup region at 0x%lx\n", kbuf->mem);
848 /* Load elfcorehdr segment - to export crashing kernel's vmcore */
849 ret = load_elfcorehdr_segment(image, kbuf);
851 pr_err("Failed to load elfcorehdr segment\n");
854 pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
855 image->arch.elfcorehdr_addr, kbuf->bufsz, kbuf->memsz);
861 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
862 * variables and call setup_purgatory() to initialize
863 * common global variable.
864 * @image: kexec image.
865 * @slave_code: Slave code for the purgatory.
866 * @fdt: Flattened device tree for the next kernel.
867 * @kernel_load_addr: Address where the kernel is loaded.
868 * @fdt_load_addr: Address where the flattened device tree is loaded.
870 * Returns 0 on success, negative errno on error.
872 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
873 const void *fdt, unsigned long kernel_load_addr,
874 unsigned long fdt_load_addr)
876 struct device_node *dn = NULL;
879 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
884 if (image->type == KEXEC_TYPE_CRASH) {
885 u32 my_run_at_load = 1;
888 * Tell relocatable kernel to run at load address
889 * via the word meant for that at 0x5c.
891 ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
893 sizeof(my_run_at_load),
899 /* Tell purgatory where to look for backup region */
900 ret = kexec_purgatory_get_set_symbol(image, "backup_start",
901 &image->arch.backup_start,
902 sizeof(image->arch.backup_start),
907 /* Setup OPAL base & entry values */
908 dn = of_find_node_by_path("/ibm,opal");
912 of_property_read_u64(dn, "opal-base-address", &val);
913 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
918 of_property_read_u64(dn, "opal-entry-address", &val);
919 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
924 pr_err("Failed to setup purgatory symbols");
930 * kexec_fdt_totalsize_ppc64 - Return the estimated size needed to setup FDT
931 * for kexec/kdump kernel.
932 * @image: kexec image being loaded.
934 * Returns the estimated size needed for kexec/kdump kernel FDT.
936 unsigned int kexec_fdt_totalsize_ppc64(struct kimage *image)
938 unsigned int fdt_size;
942 * The below estimate more than accounts for a typical kexec case where
943 * the additional space is to accommodate things like kexec cmdline,
944 * chosen node with properties for initrd start & end addresses and
945 * a property to indicate kexec boot..
947 fdt_size = fdt_totalsize(initial_boot_params) + (2 * COMMAND_LINE_SIZE);
948 if (image->type != KEXEC_TYPE_CRASH)
952 * For kdump kernel, also account for linux,usable-memory and
953 * linux,drconf-usable-memory properties. Get an approximate on the
954 * number of usable memory entries and use for FDT size estimation.
956 usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
957 (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
958 fdt_size += (unsigned int)(usm_entries * sizeof(u64));
964 * add_node_props - Reads node properties from device node structure and add
966 * @fdt: Flattened device tree of the kernel
967 * @node_offset: offset of the node to add a property at
968 * @dn: device node pointer
970 * Returns 0 on success, negative errno on error.
972 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)
980 for_each_property_of_node(dn, pp) {
981 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);
983 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));
991 * update_cpus_node - Update cpus node of flattened device tree using of_root
993 * @fdt: Flattened device tree of the kernel.
995 * Returns 0 on success, negative errno on error.
997 static int update_cpus_node(void *fdt)
999 struct device_node *cpus_node, *dn;
1000 int cpus_offset, cpus_subnode_offset, ret = 0;
1002 cpus_offset = fdt_path_offset(fdt, "/cpus");
1003 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {
1004 pr_err("Malformed device tree: error reading /cpus node: %s\n",
1005 fdt_strerror(cpus_offset));
1009 if (cpus_offset > 0) {
1010 ret = fdt_del_node(fdt, cpus_offset);
1012 pr_err("Error deleting /cpus node: %s\n", fdt_strerror(ret));
1017 /* Add cpus node to fdt */
1018 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "cpus");
1019 if (cpus_offset < 0) {
1020 pr_err("Error creating /cpus node: %s\n", fdt_strerror(cpus_offset));
1024 /* Add cpus node properties */
1025 cpus_node = of_find_node_by_path("/cpus");
1026 ret = add_node_props(fdt, cpus_offset, cpus_node);
1027 of_node_put(cpus_node);
1031 /* Loop through all subnodes of cpus and add them to fdt */
1032 for_each_node_by_type(dn, "cpu") {
1033 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);
1034 if (cpus_subnode_offset < 0) {
1035 pr_err("Unable to add %s subnode: %s\n", dn->full_name,
1036 fdt_strerror(cpus_subnode_offset));
1037 ret = cpus_subnode_offset;
1041 ret = add_node_props(fdt, cpus_subnode_offset, dn);
1051 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
1053 * @image: kexec image being loaded.
1054 * @fdt: Flattened device tree for the next kernel.
1055 * @initrd_load_addr: Address where the next initrd will be loaded.
1056 * @initrd_len: Size of the next initrd, or 0 if there will be none.
1057 * @cmdline: Command line for the next kernel, or NULL if there will
1060 * Returns 0 on success, negative errno on error.
1062 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
1063 unsigned long initrd_load_addr,
1064 unsigned long initrd_len, const char *cmdline)
1066 struct crash_mem *umem = NULL, *rmem = NULL;
1067 int i, nr_ranges, ret;
1069 ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);
1074 * Restrict memory usage for kdump kernel by setting up
1075 * usable memory ranges and memory reserve map.
1077 if (image->type == KEXEC_TYPE_CRASH) {
1078 ret = get_usable_memory_ranges(&umem);
1082 ret = update_usable_mem_fdt(fdt, umem);
1084 pr_err("Error setting up usable-memory property for kdump kernel\n");
1089 * Ensure we don't touch crashed kernel's memory except the
1090 * first 64K of RAM, which will be backed up.
1092 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
1093 crashk_res.start - BACKUP_SRC_SIZE);
1095 pr_err("Error reserving crash memory: %s\n",
1100 /* Ensure backup region is not used by kdump/capture kernel */
1101 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
1104 pr_err("Error reserving memory for backup: %s\n",
1110 /* Update cpus nodes information to account hotplug CPUs. */
1111 ret = update_cpus_node(fdt);
1115 /* Update memory reserve map */
1116 ret = get_reserved_memory_ranges(&rmem);
1120 nr_ranges = rmem ? rmem->nr_ranges : 0;
1121 for (i = 0; i < nr_ranges; i++) {
1124 base = rmem->ranges[i].start;
1125 size = rmem->ranges[i].end - base + 1;
1126 ret = fdt_add_mem_rsv(fdt, base, size);
1128 pr_err("Error updating memory reserve map: %s\n",
1141 * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
1142 * tce-table, reserved-ranges & such (exclude
1143 * memory ranges) as they can't be used for kexec
1144 * segment buffer. Sets kbuf->mem when a suitable
1145 * memory hole is found.
1146 * @kbuf: Buffer contents and memory parameters.
1148 * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
1150 * Returns 0 on success, negative errno on error.
1152 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
1154 struct crash_mem **emem;
1155 u64 buf_min, buf_max;
1158 /* Look up the exclude ranges list while locating the memory hole */
1159 emem = &(kbuf->image->arch.exclude_ranges);
1160 if (!(*emem) || ((*emem)->nr_ranges == 0)) {
1161 pr_warn("No exclude range list. Using the default locate mem hole method\n");
1162 return kexec_locate_mem_hole(kbuf);
1165 buf_min = kbuf->buf_min;
1166 buf_max = kbuf->buf_max;
1167 /* Segments for kdump kernel should be within crashkernel region */
1168 if (kbuf->image->type == KEXEC_TYPE_CRASH) {
1169 buf_min = (buf_min < crashk_res.start ?
1170 crashk_res.start : buf_min);
1171 buf_max = (buf_max > crashk_res.end ?
1172 crashk_res.end : buf_max);
1175 if (buf_min > buf_max) {
1176 pr_err("Invalid buffer min and/or max values\n");
1181 ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
1184 ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
1187 /* Add the buffer allocated to the exclude list for the next lookup */
1189 add_mem_range(emem, kbuf->mem, kbuf->memsz);
1190 sort_memory_ranges(*emem, true);
1192 pr_err("Failed to locate memory buffer of size %lu\n",
1199 * arch_kexec_kernel_image_probe - Does additional handling needed to setup
1201 * @image: kexec image being loaded.
1202 * @buf: Buffer pointing to elf data.
1203 * @buf_len: Length of the buffer.
1205 * Returns 0 on success, negative errno on error.
1207 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
1208 unsigned long buf_len)
1212 /* Get exclude memory ranges needed for setting up kexec segments */
1213 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
1215 pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
1219 return kexec_image_probe_default(image, buf, buf_len);
1223 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done
1224 * while loading the image.
1225 * @image: kexec image being loaded.
1227 * Returns 0 on success, negative errno on error.
1229 int arch_kimage_file_post_load_cleanup(struct kimage *image)
1231 kfree(image->arch.exclude_ranges);
1232 image->arch.exclude_ranges = NULL;
1234 vfree(image->arch.backup_buf);
1235 image->arch.backup_buf = NULL;
1237 vfree(image->arch.elf_headers);
1238 image->arch.elf_headers = NULL;
1239 image->arch.elf_headers_sz = 0;
1241 return kexec_image_post_load_cleanup_default(image);