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/firmware.h>
27 #include <asm/kexec_ranges.h>
28 #include <asm/crashdump-ppc64.h>
31 u64 *buf; /* data buffer for usable-memory property */
32 u32 size; /* size allocated for the data buffer */
33 u32 max_entries; /* maximum no. of entries */
34 u32 idx; /* index of current entry */
36 /* usable memory ranges to look up */
37 unsigned int nr_ranges;
38 const struct crash_mem_range *ranges;
41 const struct kexec_file_ops * const kexec_file_loaders[] = {
47 * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
48 * regions like opal/rtas, tce-table, initrd,
49 * kernel, htab which should be avoided while
50 * setting up kexec load segments.
51 * @mem_ranges: Range list to add the memory ranges to.
53 * Returns 0 on success, negative errno on error.
55 static int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
59 ret = add_tce_mem_ranges(mem_ranges);
63 ret = add_initrd_mem_range(mem_ranges);
67 ret = add_htab_mem_range(mem_ranges);
71 ret = add_kernel_mem_range(mem_ranges);
75 ret = add_rtas_mem_range(mem_ranges);
79 ret = add_opal_mem_range(mem_ranges);
83 ret = add_reserved_mem_ranges(mem_ranges);
87 /* exclude memory ranges should be sorted for easy lookup */
88 sort_memory_ranges(*mem_ranges, true);
91 pr_err("Failed to setup exclude memory ranges\n");
96 * get_usable_memory_ranges - Get usable memory ranges. This list includes
97 * regions like crashkernel, opal/rtas & tce-table,
98 * that kdump kernel could use.
99 * @mem_ranges: Range list to add the memory ranges to.
101 * Returns 0 on success, negative errno on error.
103 static int get_usable_memory_ranges(struct crash_mem **mem_ranges)
108 * Early boot failure observed on guests when low memory (first memory
109 * block?) is not added to usable memory. So, add [0, crashk_res.end]
110 * instead of [crashk_res.start, crashk_res.end] to workaround it.
111 * Also, crashed kernel's memory must be added to reserve map to
112 * avoid kdump kernel from using it.
114 ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
118 ret = add_rtas_mem_range(mem_ranges);
122 ret = add_opal_mem_range(mem_ranges);
126 ret = add_tce_mem_ranges(mem_ranges);
129 pr_err("Failed to setup usable memory ranges\n");
134 * get_crash_memory_ranges - Get crash memory ranges. This list includes
135 * first/crashing kernel's memory regions that
136 * would be exported via an elfcore.
137 * @mem_ranges: Range list to add the memory ranges to.
139 * Returns 0 on success, negative errno on error.
141 static int get_crash_memory_ranges(struct crash_mem **mem_ranges)
143 phys_addr_t base, end;
144 struct crash_mem *tmem;
148 for_each_mem_range(i, &base, &end) {
149 u64 size = end - base;
151 /* Skip backup memory region, which needs a separate entry */
152 if (base == BACKUP_SRC_START) {
153 if (size > BACKUP_SRC_SIZE) {
154 base = BACKUP_SRC_END + 1;
155 size -= BACKUP_SRC_SIZE;
160 ret = add_mem_range(mem_ranges, base, size);
164 /* Try merging adjacent ranges before reallocation attempt */
165 if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
166 sort_memory_ranges(*mem_ranges, true);
169 /* Reallocate memory ranges if there is no space to split ranges */
171 if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
172 tmem = realloc_mem_ranges(mem_ranges);
177 /* Exclude crashkernel region */
178 ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
183 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
184 * regions are exported to save their context at the time of
185 * crash, they should actually be backed up just like the
186 * first 64K bytes of memory.
188 ret = add_rtas_mem_range(mem_ranges);
192 ret = add_opal_mem_range(mem_ranges);
196 /* create a separate program header for the backup region */
197 ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
201 sort_memory_ranges(*mem_ranges, false);
204 pr_err("Failed to setup crash memory ranges\n");
209 * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
210 * memory regions that should be added to the
211 * memory reserve map to ensure the region is
212 * protected from any mischief.
213 * @mem_ranges: Range list to add the memory ranges to.
215 * Returns 0 on success, negative errno on error.
217 static int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
221 ret = add_rtas_mem_range(mem_ranges);
225 ret = add_tce_mem_ranges(mem_ranges);
229 ret = add_reserved_mem_ranges(mem_ranges);
232 pr_err("Failed to setup reserved memory ranges\n");
237 * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
238 * in the memory regions between buf_min & buf_max
239 * for the buffer. If found, sets kbuf->mem.
240 * @kbuf: Buffer contents and memory parameters.
241 * @buf_min: Minimum address for the buffer.
242 * @buf_max: Maximum address for the buffer.
244 * Returns 0 on success, negative errno on error.
246 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
247 u64 buf_min, u64 buf_max)
249 int ret = -EADDRNOTAVAIL;
250 phys_addr_t start, end;
253 for_each_mem_range_rev(i, &start, &end) {
255 * memblock uses [start, end) convention while it is
256 * [start, end] here. Fix the off-by-one to have the
264 /* Memory hole not found */
268 /* Adjust memory region based on the given range */
274 start = ALIGN(start, kbuf->buf_align);
275 if (start < end && (end - start + 1) >= kbuf->memsz) {
276 /* Suitable memory range found. Set kbuf->mem */
277 kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
288 * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
289 * suitable buffer with top down approach.
290 * @kbuf: Buffer contents and memory parameters.
291 * @buf_min: Minimum address for the buffer.
292 * @buf_max: Maximum address for the buffer.
293 * @emem: Exclude memory ranges.
295 * Returns 0 on success, negative errno on error.
297 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
298 u64 buf_min, u64 buf_max,
299 const struct crash_mem *emem)
301 int i, ret = 0, err = -EADDRNOTAVAIL;
302 u64 start, end, tmin, tmax;
305 for (i = (emem->nr_ranges - 1); i >= 0; i--) {
306 start = emem->ranges[i].start;
307 end = emem->ranges[i].end;
313 tmin = (end < buf_min ? buf_min : end + 1);
314 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
321 if (tmax < buf_min) {
330 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
336 * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
337 * in the memory regions between buf_min & buf_max
338 * for the buffer. If found, sets kbuf->mem.
339 * @kbuf: Buffer contents and memory parameters.
340 * @buf_min: Minimum address for the buffer.
341 * @buf_max: Maximum address for the buffer.
343 * Returns 0 on success, negative errno on error.
345 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
346 u64 buf_min, u64 buf_max)
348 int ret = -EADDRNOTAVAIL;
349 phys_addr_t start, end;
352 for_each_mem_range(i, &start, &end) {
354 * memblock uses [start, end) convention while it is
355 * [start, end] here. Fix the off-by-one to have the
363 /* Memory hole not found */
367 /* Adjust memory region based on the given range */
373 start = ALIGN(start, kbuf->buf_align);
374 if (start < end && (end - start + 1) >= kbuf->memsz) {
375 /* Suitable memory range found. Set kbuf->mem */
386 * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
387 * suitable buffer with bottom up approach.
388 * @kbuf: Buffer contents and memory parameters.
389 * @buf_min: Minimum address for the buffer.
390 * @buf_max: Maximum address for the buffer.
391 * @emem: Exclude memory ranges.
393 * Returns 0 on success, negative errno on error.
395 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
396 u64 buf_min, u64 buf_max,
397 const struct crash_mem *emem)
399 int i, ret = 0, err = -EADDRNOTAVAIL;
400 u64 start, end, tmin, tmax;
403 for (i = 0; i < emem->nr_ranges; i++) {
404 start = emem->ranges[i].start;
405 end = emem->ranges[i].end;
411 tmax = (start > buf_max ? buf_max : start - 1);
412 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
419 if (tmin > buf_max) {
428 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
434 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
435 * @um_info: Usable memory buffer and ranges info.
436 * @cnt: No. of entries to accommodate.
438 * Frees up the old buffer if memory reallocation fails.
440 * Returns buffer on success, NULL on error.
442 static u64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
447 if ((um_info->idx + cnt) <= um_info->max_entries)
450 new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
451 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
454 um_info->size = new_size;
455 um_info->max_entries = (um_info->size / sizeof(u64));
462 * add_usable_mem - Add the usable memory ranges within the given memory range
464 * @um_info: Usable memory buffer and ranges info.
465 * @base: Base address of memory range to look for.
466 * @end: End address of memory range to look for.
468 * Returns 0 on success, negative errno on error.
470 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
472 u64 loc_base, loc_end;
476 for (i = 0; i < um_info->nr_ranges; i++) {
478 loc_base = um_info->ranges[i].start;
479 loc_end = um_info->ranges[i].end;
480 if (loc_base >= base && loc_end <= end)
482 else if (base < loc_end && end > loc_base) {
491 if (!check_realloc_usable_mem(um_info, 2))
494 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
495 um_info->buf[um_info->idx++] =
496 cpu_to_be64(loc_end - loc_base + 1);
504 * kdump_setup_usable_lmb - This is a callback function that gets called by
505 * walk_drmem_lmbs for every LMB to set its
506 * usable memory ranges.
508 * @usm: linux,drconf-usable-memory property value.
509 * @data: Pointer to usable memory buffer and ranges info.
511 * Returns 0 on success, negative errno on error.
513 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
516 struct umem_info *um_info;
521 * kdump load isn't supported on kernels already booted with
522 * linux,drconf-usable-memory property.
525 pr_err("linux,drconf-usable-memory property already exists!");
530 tmp_idx = um_info->idx;
531 if (!check_realloc_usable_mem(um_info, 1))
535 base = lmb->base_addr;
536 end = base + drmem_lmb_size() - 1;
537 ret = add_usable_mem(um_info, base, end);
540 * Update the no. of ranges added. Two entries (base & size)
541 * for every range added.
543 um_info->buf[tmp_idx] =
544 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
550 #define NODE_PATH_LEN 256
552 * add_usable_mem_property - Add usable memory property for the given
554 * @fdt: Flattened device tree for the kdump kernel.
556 * @um_info: Usable memory buffer and ranges info.
558 * Returns 0 on success, negative errno on error.
560 static int add_usable_mem_property(void *fdt, struct device_node *dn,
561 struct umem_info *um_info)
563 int n_mem_addr_cells, n_mem_size_cells, node;
564 char path[NODE_PATH_LEN];
565 int i, len, ranges, ret;
571 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
572 pr_err("Buffer (%d) too small for memory node: %pOF\n",
576 pr_debug("Memory node path: %s\n", path);
578 /* Now that we know the path, find its offset in kdump kernel's fdt */
579 node = fdt_path_offset(fdt, path);
581 pr_err("Malformed device tree: error reading %s\n", path);
586 /* Get the address & size cells */
587 n_mem_addr_cells = of_n_addr_cells(dn);
588 n_mem_size_cells = of_n_size_cells(dn);
589 pr_debug("address cells: %d, size cells: %d\n", n_mem_addr_cells,
593 if (!check_realloc_usable_mem(um_info, 2)) {
598 prop = of_get_property(dn, "reg", &len);
599 if (!prop || len <= 0) {
605 * "reg" property represents sequence of (addr,size) tuples
606 * each representing a memory range.
608 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
610 for (i = 0; i < ranges; i++) {
611 base = of_read_number(prop, n_mem_addr_cells);
612 prop += n_mem_addr_cells;
613 end = base + of_read_number(prop, n_mem_size_cells) - 1;
614 prop += n_mem_size_cells;
616 ret = add_usable_mem(um_info, base, end);
622 * No kdump kernel usable memory found in this memory node.
623 * Write (0,0) tuple in linux,usable-memory property for
624 * this region to be ignored.
626 if (um_info->idx == 0) {
632 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
633 (um_info->idx * sizeof(u64)));
642 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
643 * and linux,drconf-usable-memory DT properties as
644 * appropriate to restrict its memory usage.
645 * @fdt: Flattened device tree for the kdump kernel.
646 * @usable_mem: Usable memory ranges for kdump kernel.
648 * Returns 0 on success, negative errno on error.
650 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
652 struct umem_info um_info;
653 struct device_node *dn;
657 pr_err("Usable memory ranges for kdump kernel not found\n");
661 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
662 if (node == -FDT_ERR_NOTFOUND)
663 pr_debug("No dynamic reconfiguration memory found\n");
665 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
671 um_info.max_entries = 0;
673 /* Memory ranges to look up */
674 um_info.ranges = &(usable_mem->ranges[0]);
675 um_info.nr_ranges = usable_mem->nr_ranges;
677 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
679 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
683 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
687 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
688 um_info.buf, (um_info.idx * sizeof(u64)));
690 pr_err("Failed to update fdt with linux,drconf-usable-memory property");
696 * Walk through each memory node and set linux,usable-memory property
697 * for the corresponding node in kdump kernel's fdt.
699 for_each_node_by_type(dn, "memory") {
700 ret = add_usable_mem_property(fdt, dn, &um_info);
702 pr_err("Failed to set linux,usable-memory property for %s node",
715 * load_backup_segment - Locate a memory hole to place the backup region.
716 * @image: Kexec image.
717 * @kbuf: Buffer contents and memory parameters.
719 * Returns 0 on success, negative errno on error.
721 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
727 * Setup a source buffer for backup segment.
729 * A source buffer has no meaning for backup region as data will
730 * be copied from backup source, after crash, in the purgatory.
731 * But as load segment code doesn't recognize such segments,
732 * setup a dummy source buffer to keep it happy for now.
734 buf = vzalloc(BACKUP_SRC_SIZE);
739 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
740 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
741 kbuf->top_down = false;
743 ret = kexec_add_buffer(kbuf);
749 image->arch.backup_buf = buf;
750 image->arch.backup_start = kbuf->mem;
755 * update_backup_region_phdr - Update backup region's offset for the core to
756 * export the region appropriately.
757 * @image: Kexec image.
758 * @ehdr: ELF core header.
760 * Assumes an exclusive program header is setup for the backup region
765 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
770 phdr = (Elf64_Phdr *)(ehdr + 1);
771 for (i = 0; i < ehdr->e_phnum; i++) {
772 if (phdr->p_paddr == BACKUP_SRC_START) {
773 phdr->p_offset = image->arch.backup_start;
774 pr_debug("Backup region offset updated to 0x%lx\n",
775 image->arch.backup_start);
782 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
783 * segment needed to load kdump kernel.
784 * @image: Kexec image.
785 * @kbuf: Buffer contents and memory parameters.
787 * Returns 0 on success, negative errno on error.
789 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
791 struct crash_mem *cmem = NULL;
792 unsigned long headers_sz;
793 void *headers = NULL;
796 ret = get_crash_memory_ranges(&cmem);
800 /* Setup elfcorehdr segment */
801 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
803 pr_err("Failed to prepare elf headers for the core\n");
807 /* Fix the offset for backup region in the ELF header */
808 update_backup_region_phdr(image, headers);
810 kbuf->buffer = headers;
811 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
812 kbuf->bufsz = kbuf->memsz = headers_sz;
813 kbuf->top_down = false;
815 ret = kexec_add_buffer(kbuf);
821 image->elf_load_addr = kbuf->mem;
822 image->elf_headers_sz = headers_sz;
823 image->elf_headers = headers;
830 * load_crashdump_segments_ppc64 - Initialize the additional segements needed
831 * to load kdump kernel.
832 * @image: Kexec image.
833 * @kbuf: Buffer contents and memory parameters.
835 * Returns 0 on success, negative errno on error.
837 int load_crashdump_segments_ppc64(struct kimage *image,
838 struct kexec_buf *kbuf)
842 /* Load backup segment - first 64K bytes of the crashing kernel */
843 ret = load_backup_segment(image, kbuf);
845 pr_err("Failed to load backup segment\n");
848 pr_debug("Loaded the backup region at 0x%lx\n", kbuf->mem);
850 /* Load elfcorehdr segment - to export crashing kernel's vmcore */
851 ret = load_elfcorehdr_segment(image, kbuf);
853 pr_err("Failed to load elfcorehdr segment\n");
856 pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
857 image->elf_load_addr, kbuf->bufsz, kbuf->memsz);
863 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
864 * variables and call setup_purgatory() to initialize
865 * common global variable.
866 * @image: kexec image.
867 * @slave_code: Slave code for the purgatory.
868 * @fdt: Flattened device tree for the next kernel.
869 * @kernel_load_addr: Address where the kernel is loaded.
870 * @fdt_load_addr: Address where the flattened device tree is loaded.
872 * Returns 0 on success, negative errno on error.
874 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
875 const void *fdt, unsigned long kernel_load_addr,
876 unsigned long fdt_load_addr)
878 struct device_node *dn = NULL;
881 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
886 if (image->type == KEXEC_TYPE_CRASH) {
887 u32 my_run_at_load = 1;
890 * Tell relocatable kernel to run at load address
891 * via the word meant for that at 0x5c.
893 ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
895 sizeof(my_run_at_load),
901 /* Tell purgatory where to look for backup region */
902 ret = kexec_purgatory_get_set_symbol(image, "backup_start",
903 &image->arch.backup_start,
904 sizeof(image->arch.backup_start),
909 /* Setup OPAL base & entry values */
910 dn = of_find_node_by_path("/ibm,opal");
914 of_property_read_u64(dn, "opal-base-address", &val);
915 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
920 of_property_read_u64(dn, "opal-entry-address", &val);
921 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
926 pr_err("Failed to setup purgatory symbols");
932 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to
933 * setup FDT for kexec/kdump kernel.
934 * @image: kexec image being loaded.
936 * Returns the estimated extra size needed for kexec/kdump kernel FDT.
938 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
942 if (image->type != KEXEC_TYPE_CRASH)
946 * For kdump kernel, account for linux,usable-memory and
947 * linux,drconf-usable-memory properties. Get an approximate on the
948 * number of usable memory entries and use for FDT size estimation.
950 usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
951 (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
952 return (unsigned int)(usm_entries * sizeof(u64));
956 * add_node_props - Reads node properties from device node structure and add
958 * @fdt: Flattened device tree of the kernel
959 * @node_offset: offset of the node to add a property at
960 * @dn: device node pointer
962 * Returns 0 on success, negative errno on error.
964 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)
972 for_each_property_of_node(dn, pp) {
973 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);
975 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));
983 * update_cpus_node - Update cpus node of flattened device tree using of_root
985 * @fdt: Flattened device tree of the kernel.
987 * Returns 0 on success, negative errno on error.
989 static int update_cpus_node(void *fdt)
991 struct device_node *cpus_node, *dn;
992 int cpus_offset, cpus_subnode_offset, ret = 0;
994 cpus_offset = fdt_path_offset(fdt, "/cpus");
995 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {
996 pr_err("Malformed device tree: error reading /cpus node: %s\n",
997 fdt_strerror(cpus_offset));
1001 if (cpus_offset > 0) {
1002 ret = fdt_del_node(fdt, cpus_offset);
1004 pr_err("Error deleting /cpus node: %s\n", fdt_strerror(ret));
1009 /* Add cpus node to fdt */
1010 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "cpus");
1011 if (cpus_offset < 0) {
1012 pr_err("Error creating /cpus node: %s\n", fdt_strerror(cpus_offset));
1016 /* Add cpus node properties */
1017 cpus_node = of_find_node_by_path("/cpus");
1018 ret = add_node_props(fdt, cpus_offset, cpus_node);
1019 of_node_put(cpus_node);
1023 /* Loop through all subnodes of cpus and add them to fdt */
1024 for_each_node_by_type(dn, "cpu") {
1025 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);
1026 if (cpus_subnode_offset < 0) {
1027 pr_err("Unable to add %s subnode: %s\n", dn->full_name,
1028 fdt_strerror(cpus_subnode_offset));
1029 ret = cpus_subnode_offset;
1033 ret = add_node_props(fdt, cpus_subnode_offset, dn);
1042 static int copy_property(void *fdt, int node_offset, const struct device_node *dn,
1043 const char *propname)
1045 const void *prop, *fdtprop;
1046 int len = 0, fdtlen = 0;
1048 prop = of_get_property(dn, propname, &len);
1049 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen);
1051 if (fdtprop && !prop)
1052 return fdt_delprop(fdt, node_offset, propname);
1054 return fdt_setprop(fdt, node_offset, propname, prop, len);
1056 return -FDT_ERR_NOTFOUND;
1059 static int update_pci_dma_nodes(void *fdt, const char *dmapropname)
1061 struct device_node *dn;
1062 int pci_offset, root_offset, ret = 0;
1064 if (!firmware_has_feature(FW_FEATURE_LPAR))
1067 root_offset = fdt_path_offset(fdt, "/");
1068 for_each_node_with_property(dn, dmapropname) {
1069 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn));
1073 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window");
1076 ret = copy_property(fdt, pci_offset, dn, dmapropname);
1085 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
1087 * @image: kexec image being loaded.
1088 * @fdt: Flattened device tree for the next kernel.
1089 * @initrd_load_addr: Address where the next initrd will be loaded.
1090 * @initrd_len: Size of the next initrd, or 0 if there will be none.
1091 * @cmdline: Command line for the next kernel, or NULL if there will
1094 * Returns 0 on success, negative errno on error.
1096 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
1097 unsigned long initrd_load_addr,
1098 unsigned long initrd_len, const char *cmdline)
1100 struct crash_mem *umem = NULL, *rmem = NULL;
1101 int i, nr_ranges, ret;
1104 * Restrict memory usage for kdump kernel by setting up
1105 * usable memory ranges and memory reserve map.
1107 if (image->type == KEXEC_TYPE_CRASH) {
1108 ret = get_usable_memory_ranges(&umem);
1112 ret = update_usable_mem_fdt(fdt, umem);
1114 pr_err("Error setting up usable-memory property for kdump kernel\n");
1119 * Ensure we don't touch crashed kernel's memory except the
1120 * first 64K of RAM, which will be backed up.
1122 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
1123 crashk_res.start - BACKUP_SRC_SIZE);
1125 pr_err("Error reserving crash memory: %s\n",
1130 /* Ensure backup region is not used by kdump/capture kernel */
1131 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
1134 pr_err("Error reserving memory for backup: %s\n",
1140 /* Update cpus nodes information to account hotplug CPUs. */
1141 ret = update_cpus_node(fdt);
1145 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
1146 #define DMA64_PROPNAME "linux,dma64-ddr-window-info"
1147 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME);
1151 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME);
1154 #undef DMA64_PROPNAME
1155 #undef DIRECT64_PROPNAME
1157 /* Update memory reserve map */
1158 ret = get_reserved_memory_ranges(&rmem);
1162 nr_ranges = rmem ? rmem->nr_ranges : 0;
1163 for (i = 0; i < nr_ranges; i++) {
1166 base = rmem->ranges[i].start;
1167 size = rmem->ranges[i].end - base + 1;
1168 ret = fdt_add_mem_rsv(fdt, base, size);
1170 pr_err("Error updating memory reserve map: %s\n",
1183 * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
1184 * tce-table, reserved-ranges & such (exclude
1185 * memory ranges) as they can't be used for kexec
1186 * segment buffer. Sets kbuf->mem when a suitable
1187 * memory hole is found.
1188 * @kbuf: Buffer contents and memory parameters.
1190 * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
1192 * Returns 0 on success, negative errno on error.
1194 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
1196 struct crash_mem **emem;
1197 u64 buf_min, buf_max;
1200 /* Look up the exclude ranges list while locating the memory hole */
1201 emem = &(kbuf->image->arch.exclude_ranges);
1202 if (!(*emem) || ((*emem)->nr_ranges == 0)) {
1203 pr_warn("No exclude range list. Using the default locate mem hole method\n");
1204 return kexec_locate_mem_hole(kbuf);
1207 buf_min = kbuf->buf_min;
1208 buf_max = kbuf->buf_max;
1209 /* Segments for kdump kernel should be within crashkernel region */
1210 if (kbuf->image->type == KEXEC_TYPE_CRASH) {
1211 buf_min = (buf_min < crashk_res.start ?
1212 crashk_res.start : buf_min);
1213 buf_max = (buf_max > crashk_res.end ?
1214 crashk_res.end : buf_max);
1217 if (buf_min > buf_max) {
1218 pr_err("Invalid buffer min and/or max values\n");
1223 ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
1226 ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
1229 /* Add the buffer allocated to the exclude list for the next lookup */
1231 add_mem_range(emem, kbuf->mem, kbuf->memsz);
1232 sort_memory_ranges(*emem, true);
1234 pr_err("Failed to locate memory buffer of size %lu\n",
1241 * arch_kexec_kernel_image_probe - Does additional handling needed to setup
1243 * @image: kexec image being loaded.
1244 * @buf: Buffer pointing to elf data.
1245 * @buf_len: Length of the buffer.
1247 * Returns 0 on success, negative errno on error.
1249 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
1250 unsigned long buf_len)
1254 /* Get exclude memory ranges needed for setting up kexec segments */
1255 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
1257 pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
1261 return kexec_image_probe_default(image, buf, buf_len);
1265 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done
1266 * while loading the image.
1267 * @image: kexec image being loaded.
1269 * Returns 0 on success, negative errno on error.
1271 int arch_kimage_file_post_load_cleanup(struct kimage *image)
1273 kfree(image->arch.exclude_ranges);
1274 image->arch.exclude_ranges = NULL;
1276 vfree(image->arch.backup_buf);
1277 image->arch.backup_buf = NULL;
1279 vfree(image->elf_headers);
1280 image->elf_headers = NULL;
1281 image->elf_headers_sz = 0;
1283 kvfree(image->arch.fdt);
1284 image->arch.fdt = NULL;
1286 return kexec_image_post_load_cleanup_default(image);