1 // SPDX-License-Identifier: GPL-2.0-only
3 * ppc64 code to implement the kexec_file_load syscall
6 * Copyright (C) 2004 IBM Corp.
7 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
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>
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>
29 #include <asm/mmzone.h>
30 #include <asm/iommu.h>
32 #include <asm/plpks.h>
33 #include <asm/cputhreads.h>
36 __be64 *buf; /* data buffer for usable-memory property */
37 u32 size; /* size allocated for the data buffer */
38 u32 max_entries; /* maximum no. of entries */
39 u32 idx; /* index of current entry */
41 /* usable memory ranges to look up */
42 unsigned int nr_ranges;
43 const struct range *ranges;
46 const struct kexec_file_ops * const kexec_file_loaders[] = {
52 * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
53 * in the memory regions between buf_min & buf_max
54 * for the buffer. If found, sets kbuf->mem.
55 * @kbuf: Buffer contents and memory parameters.
56 * @buf_min: Minimum address for the buffer.
57 * @buf_max: Maximum address for the buffer.
59 * Returns 0 on success, negative errno on error.
61 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
62 u64 buf_min, u64 buf_max)
64 int ret = -EADDRNOTAVAIL;
65 phys_addr_t start, end;
68 for_each_mem_range_rev(i, &start, &end) {
70 * memblock uses [start, end) convention while it is
71 * [start, end] here. Fix the off-by-one to have the
79 /* Memory hole not found */
83 /* Adjust memory region based on the given range */
89 start = ALIGN(start, kbuf->buf_align);
90 if (start < end && (end - start + 1) >= kbuf->memsz) {
91 /* Suitable memory range found. Set kbuf->mem */
92 kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
103 * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
104 * suitable buffer with top down approach.
105 * @kbuf: Buffer contents and memory parameters.
106 * @buf_min: Minimum address for the buffer.
107 * @buf_max: Maximum address for the buffer.
108 * @emem: Exclude memory ranges.
110 * Returns 0 on success, negative errno on error.
112 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
113 u64 buf_min, u64 buf_max,
114 const struct crash_mem *emem)
116 int i, ret = 0, err = -EADDRNOTAVAIL;
117 u64 start, end, tmin, tmax;
120 for (i = (emem->nr_ranges - 1); i >= 0; i--) {
121 start = emem->ranges[i].start;
122 end = emem->ranges[i].end;
128 tmin = (end < buf_min ? buf_min : end + 1);
129 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
136 if (tmax < buf_min) {
145 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
151 * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
152 * in the memory regions between buf_min & buf_max
153 * for the buffer. If found, sets kbuf->mem.
154 * @kbuf: Buffer contents and memory parameters.
155 * @buf_min: Minimum address for the buffer.
156 * @buf_max: Maximum address for the buffer.
158 * Returns 0 on success, negative errno on error.
160 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
161 u64 buf_min, u64 buf_max)
163 int ret = -EADDRNOTAVAIL;
164 phys_addr_t start, end;
167 for_each_mem_range(i, &start, &end) {
169 * memblock uses [start, end) convention while it is
170 * [start, end] here. Fix the off-by-one to have the
178 /* Memory hole not found */
182 /* Adjust memory region based on the given range */
188 start = ALIGN(start, kbuf->buf_align);
189 if (start < end && (end - start + 1) >= kbuf->memsz) {
190 /* Suitable memory range found. Set kbuf->mem */
201 * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
202 * suitable buffer with bottom up approach.
203 * @kbuf: Buffer contents and memory parameters.
204 * @buf_min: Minimum address for the buffer.
205 * @buf_max: Maximum address for the buffer.
206 * @emem: Exclude memory ranges.
208 * Returns 0 on success, negative errno on error.
210 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
211 u64 buf_min, u64 buf_max,
212 const struct crash_mem *emem)
214 int i, ret = 0, err = -EADDRNOTAVAIL;
215 u64 start, end, tmin, tmax;
218 for (i = 0; i < emem->nr_ranges; i++) {
219 start = emem->ranges[i].start;
220 end = emem->ranges[i].end;
226 tmax = (start > buf_max ? buf_max : start - 1);
227 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
234 if (tmin > buf_max) {
243 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
248 #ifdef CONFIG_CRASH_DUMP
250 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
251 * @um_info: Usable memory buffer and ranges info.
252 * @cnt: No. of entries to accommodate.
254 * Frees up the old buffer if memory reallocation fails.
256 * Returns buffer on success, NULL on error.
258 static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
263 if ((um_info->idx + cnt) <= um_info->max_entries)
266 new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
267 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
270 um_info->size = new_size;
271 um_info->max_entries = (um_info->size / sizeof(u64));
278 * add_usable_mem - Add the usable memory ranges within the given memory range
280 * @um_info: Usable memory buffer and ranges info.
281 * @base: Base address of memory range to look for.
282 * @end: End address of memory range to look for.
284 * Returns 0 on success, negative errno on error.
286 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
288 u64 loc_base, loc_end;
292 for (i = 0; i < um_info->nr_ranges; i++) {
294 loc_base = um_info->ranges[i].start;
295 loc_end = um_info->ranges[i].end;
296 if (loc_base >= base && loc_end <= end)
298 else if (base < loc_end && end > loc_base) {
307 if (!check_realloc_usable_mem(um_info, 2))
310 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
311 um_info->buf[um_info->idx++] =
312 cpu_to_be64(loc_end - loc_base + 1);
320 * kdump_setup_usable_lmb - This is a callback function that gets called by
321 * walk_drmem_lmbs for every LMB to set its
322 * usable memory ranges.
324 * @usm: linux,drconf-usable-memory property value.
325 * @data: Pointer to usable memory buffer and ranges info.
327 * Returns 0 on success, negative errno on error.
329 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
332 struct umem_info *um_info;
337 * kdump load isn't supported on kernels already booted with
338 * linux,drconf-usable-memory property.
341 pr_err("linux,drconf-usable-memory property already exists!");
346 tmp_idx = um_info->idx;
347 if (!check_realloc_usable_mem(um_info, 1))
351 base = lmb->base_addr;
352 end = base + drmem_lmb_size() - 1;
353 ret = add_usable_mem(um_info, base, end);
356 * Update the no. of ranges added. Two entries (base & size)
357 * for every range added.
359 um_info->buf[tmp_idx] =
360 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
366 #define NODE_PATH_LEN 256
368 * add_usable_mem_property - Add usable memory property for the given
370 * @fdt: Flattened device tree for the kdump kernel.
372 * @um_info: Usable memory buffer and ranges info.
374 * Returns 0 on success, negative errno on error.
376 static int add_usable_mem_property(void *fdt, struct device_node *dn,
377 struct umem_info *um_info)
379 int n_mem_addr_cells, n_mem_size_cells, node;
380 char path[NODE_PATH_LEN];
381 int i, len, ranges, ret;
387 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
388 pr_err("Buffer (%d) too small for memory node: %pOF\n",
392 kexec_dprintk("Memory node path: %s\n", path);
394 /* Now that we know the path, find its offset in kdump kernel's fdt */
395 node = fdt_path_offset(fdt, path);
397 pr_err("Malformed device tree: error reading %s\n", path);
402 /* Get the address & size cells */
403 n_mem_addr_cells = of_n_addr_cells(dn);
404 n_mem_size_cells = of_n_size_cells(dn);
405 kexec_dprintk("address cells: %d, size cells: %d\n", n_mem_addr_cells,
409 if (!check_realloc_usable_mem(um_info, 2)) {
414 prop = of_get_property(dn, "reg", &len);
415 if (!prop || len <= 0) {
421 * "reg" property represents sequence of (addr,size) tuples
422 * each representing a memory range.
424 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
426 for (i = 0; i < ranges; i++) {
427 base = of_read_number(prop, n_mem_addr_cells);
428 prop += n_mem_addr_cells;
429 end = base + of_read_number(prop, n_mem_size_cells) - 1;
430 prop += n_mem_size_cells;
432 ret = add_usable_mem(um_info, base, end);
438 * No kdump kernel usable memory found in this memory node.
439 * Write (0,0) tuple in linux,usable-memory property for
440 * this region to be ignored.
442 if (um_info->idx == 0) {
448 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
449 (um_info->idx * sizeof(u64)));
458 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
459 * and linux,drconf-usable-memory DT properties as
460 * appropriate to restrict its memory usage.
461 * @fdt: Flattened device tree for the kdump kernel.
462 * @usable_mem: Usable memory ranges for kdump kernel.
464 * Returns 0 on success, negative errno on error.
466 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
468 struct umem_info um_info;
469 struct device_node *dn;
473 pr_err("Usable memory ranges for kdump kernel not found\n");
477 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
478 if (node == -FDT_ERR_NOTFOUND)
479 kexec_dprintk("No dynamic reconfiguration memory found\n");
481 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
487 um_info.max_entries = 0;
489 /* Memory ranges to look up */
490 um_info.ranges = &(usable_mem->ranges[0]);
491 um_info.nr_ranges = usable_mem->nr_ranges;
493 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
495 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
499 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
503 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
504 um_info.buf, (um_info.idx * sizeof(u64)));
506 pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s",
513 * Walk through each memory node and set linux,usable-memory property
514 * for the corresponding node in kdump kernel's fdt.
516 for_each_node_by_type(dn, "memory") {
517 ret = add_usable_mem_property(fdt, dn, &um_info);
519 pr_err("Failed to set linux,usable-memory property for %s node",
532 * load_backup_segment - Locate a memory hole to place the backup region.
533 * @image: Kexec image.
534 * @kbuf: Buffer contents and memory parameters.
536 * Returns 0 on success, negative errno on error.
538 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
544 * Setup a source buffer for backup segment.
546 * A source buffer has no meaning for backup region as data will
547 * be copied from backup source, after crash, in the purgatory.
548 * But as load segment code doesn't recognize such segments,
549 * setup a dummy source buffer to keep it happy for now.
551 buf = vzalloc(BACKUP_SRC_SIZE);
556 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
557 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
558 kbuf->top_down = false;
560 ret = kexec_add_buffer(kbuf);
566 image->arch.backup_buf = buf;
567 image->arch.backup_start = kbuf->mem;
572 * update_backup_region_phdr - Update backup region's offset for the core to
573 * export the region appropriately.
574 * @image: Kexec image.
575 * @ehdr: ELF core header.
577 * Assumes an exclusive program header is setup for the backup region
582 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
587 phdr = (Elf64_Phdr *)(ehdr + 1);
588 for (i = 0; i < ehdr->e_phnum; i++) {
589 if (phdr->p_paddr == BACKUP_SRC_START) {
590 phdr->p_offset = image->arch.backup_start;
591 kexec_dprintk("Backup region offset updated to 0x%lx\n",
592 image->arch.backup_start);
598 static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem)
600 #if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG)
601 unsigned int extra_sz = 0;
603 if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM)
604 pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANGES);
605 else if (cmem->nr_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES)
606 pr_warn("Configured crash mem ranges may not be enough\n");
608 extra_sz = (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64_Phdr);
616 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
617 * segment needed to load kdump kernel.
618 * @image: Kexec image.
619 * @kbuf: Buffer contents and memory parameters.
621 * Returns 0 on success, negative errno on error.
623 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
625 struct crash_mem *cmem = NULL;
626 unsigned long headers_sz;
627 void *headers = NULL;
630 ret = get_crash_memory_ranges(&cmem);
634 /* Setup elfcorehdr segment */
635 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
637 pr_err("Failed to prepare elf headers for the core\n");
641 /* Fix the offset for backup region in the ELF header */
642 update_backup_region_phdr(image, headers);
644 kbuf->buffer = headers;
645 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
646 kbuf->bufsz = headers_sz;
647 kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem);
648 kbuf->top_down = false;
650 ret = kexec_add_buffer(kbuf);
656 image->elf_load_addr = kbuf->mem;
657 image->elf_headers_sz = headers_sz;
658 image->elf_headers = headers;
665 * load_crashdump_segments_ppc64 - Initialize the additional segements needed
666 * to load kdump kernel.
667 * @image: Kexec image.
668 * @kbuf: Buffer contents and memory parameters.
670 * Returns 0 on success, negative errno on error.
672 int load_crashdump_segments_ppc64(struct kimage *image,
673 struct kexec_buf *kbuf)
677 /* Load backup segment - first 64K bytes of the crashing kernel */
678 ret = load_backup_segment(image, kbuf);
680 pr_err("Failed to load backup segment\n");
683 kexec_dprintk("Loaded the backup region at 0x%lx\n", kbuf->mem);
685 /* Load elfcorehdr segment - to export crashing kernel's vmcore */
686 ret = load_elfcorehdr_segment(image, kbuf);
688 pr_err("Failed to load elfcorehdr segment\n");
691 kexec_dprintk("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
692 image->elf_load_addr, kbuf->bufsz, kbuf->memsz);
699 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
700 * variables and call setup_purgatory() to initialize
701 * common global variable.
702 * @image: kexec image.
703 * @slave_code: Slave code for the purgatory.
704 * @fdt: Flattened device tree for the next kernel.
705 * @kernel_load_addr: Address where the kernel is loaded.
706 * @fdt_load_addr: Address where the flattened device tree is loaded.
708 * Returns 0 on success, negative errno on error.
710 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
711 const void *fdt, unsigned long kernel_load_addr,
712 unsigned long fdt_load_addr)
714 struct device_node *dn = NULL;
717 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
722 if (image->type == KEXEC_TYPE_CRASH) {
723 u32 my_run_at_load = 1;
726 * Tell relocatable kernel to run at load address
727 * via the word meant for that at 0x5c.
729 ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
731 sizeof(my_run_at_load),
737 /* Tell purgatory where to look for backup region */
738 ret = kexec_purgatory_get_set_symbol(image, "backup_start",
739 &image->arch.backup_start,
740 sizeof(image->arch.backup_start),
745 /* Setup OPAL base & entry values */
746 dn = of_find_node_by_path("/ibm,opal");
750 of_property_read_u64(dn, "opal-base-address", &val);
751 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
756 of_property_read_u64(dn, "opal-entry-address", &val);
757 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
762 pr_err("Failed to setup purgatory symbols");
768 * cpu_node_size - Compute the size of a CPU node in the FDT.
769 * This should be done only once and the value is stored in
771 * Returns the max size of a CPU node in the FDT.
773 static unsigned int cpu_node_size(void)
775 static unsigned int size;
776 struct device_node *dn;
780 * Don't compute it twice, we are assuming that the per CPU node size
781 * doesn't change during the system's life.
786 dn = of_find_node_by_type(NULL, "cpu");
787 if (WARN_ON_ONCE(!dn)) {
788 // Unlikely to happen
793 * We compute the sub node size for a CPU node, assuming it
794 * will be the same for all.
796 size += strlen(dn->name) + 5;
797 for_each_property_of_node(dn, pp) {
798 size += strlen(pp->name);
806 static unsigned int kdump_extra_fdt_size_ppc64(struct kimage *image)
808 unsigned int cpu_nodes, extra_size = 0;
809 struct device_node *dn;
811 #ifdef CONFIG_CRASH_HOTPLUG
812 unsigned int possible_cpu_nodes;
815 if (!IS_ENABLED(CONFIG_CRASH_DUMP) || image->type != KEXEC_TYPE_CRASH)
819 * For kdump kernel, account for linux,usable-memory and
820 * linux,drconf-usable-memory properties. Get an approximate on the
821 * number of usable memory entries and use for FDT size estimation.
823 if (drmem_lmb_size()) {
824 usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) +
825 (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
826 extra_size += (unsigned int)(usm_entries * sizeof(u64));
830 * Get the number of CPU nodes in the current DT. This allows to
831 * reserve places for CPU nodes added since the boot time.
834 for_each_node_by_type(dn, "cpu") {
838 if (cpu_nodes > boot_cpu_node_count)
839 extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size();
841 #ifdef CONFIG_CRASH_HOTPLUG
843 * Make sure enough space is reserved to accommodate possible CPU nodes
844 * in the crash FDT. This allows packing possible CPU nodes which are
845 * not yet present in the system without regenerating the entire FDT.
847 if (image->type == KEXEC_TYPE_CRASH) {
848 possible_cpu_nodes = num_possible_cpus() / threads_per_core;
849 if (possible_cpu_nodes > cpu_nodes)
850 extra_size += (possible_cpu_nodes - cpu_nodes) * cpu_node_size();
858 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to
859 * setup FDT for kexec/kdump kernel.
860 * @image: kexec image being loaded.
862 * Returns the estimated extra size needed for kexec/kdump kernel FDT.
864 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
866 unsigned int extra_size = 0;
868 // Budget some space for the password blob. There's already extra space
870 if (plpks_is_available())
871 extra_size += (unsigned int)plpks_get_passwordlen();
873 return extra_size + kdump_extra_fdt_size_ppc64(image);
876 static int copy_property(void *fdt, int node_offset, const struct device_node *dn,
877 const char *propname)
879 const void *prop, *fdtprop;
880 int len = 0, fdtlen = 0;
882 prop = of_get_property(dn, propname, &len);
883 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen);
885 if (fdtprop && !prop)
886 return fdt_delprop(fdt, node_offset, propname);
888 return fdt_setprop(fdt, node_offset, propname, prop, len);
890 return -FDT_ERR_NOTFOUND;
893 static int update_pci_dma_nodes(void *fdt, const char *dmapropname)
895 struct device_node *dn;
896 int pci_offset, root_offset, ret = 0;
898 if (!firmware_has_feature(FW_FEATURE_LPAR))
901 root_offset = fdt_path_offset(fdt, "/");
902 for_each_node_with_property(dn, dmapropname) {
903 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn));
907 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window");
912 ret = copy_property(fdt, pci_offset, dn, dmapropname);
923 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
925 * @image: kexec image being loaded.
926 * @fdt: Flattened device tree for the next kernel.
927 * @initrd_load_addr: Address where the next initrd will be loaded.
928 * @initrd_len: Size of the next initrd, or 0 if there will be none.
929 * @cmdline: Command line for the next kernel, or NULL if there will
932 * Returns 0 on success, negative errno on error.
934 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
935 unsigned long initrd_load_addr,
936 unsigned long initrd_len, const char *cmdline)
938 struct crash_mem *umem = NULL, *rmem = NULL;
939 int i, nr_ranges, ret;
941 #ifdef CONFIG_CRASH_DUMP
943 * Restrict memory usage for kdump kernel by setting up
944 * usable memory ranges and memory reserve map.
946 if (image->type == KEXEC_TYPE_CRASH) {
947 ret = get_usable_memory_ranges(&umem);
951 ret = update_usable_mem_fdt(fdt, umem);
953 pr_err("Error setting up usable-memory property for kdump kernel\n");
958 * Ensure we don't touch crashed kernel's memory except the
959 * first 64K of RAM, which will be backed up.
961 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
962 crashk_res.start - BACKUP_SRC_SIZE);
964 pr_err("Error reserving crash memory: %s\n",
969 /* Ensure backup region is not used by kdump/capture kernel */
970 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
973 pr_err("Error reserving memory for backup: %s\n",
980 /* Update cpus nodes information to account hotplug CPUs. */
981 ret = update_cpus_node(fdt);
985 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME);
989 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME);
993 /* Update memory reserve map */
994 ret = get_reserved_memory_ranges(&rmem);
998 nr_ranges = rmem ? rmem->nr_ranges : 0;
999 for (i = 0; i < nr_ranges; i++) {
1002 base = rmem->ranges[i].start;
1003 size = rmem->ranges[i].end - base + 1;
1004 ret = fdt_add_mem_rsv(fdt, base, size);
1006 pr_err("Error updating memory reserve map: %s\n",
1012 // If we have PLPKS active, we need to provide the password to the new kernel
1013 if (plpks_is_available())
1014 ret = plpks_populate_fdt(fdt);
1023 * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
1024 * tce-table, reserved-ranges & such (exclude
1025 * memory ranges) as they can't be used for kexec
1026 * segment buffer. Sets kbuf->mem when a suitable
1027 * memory hole is found.
1028 * @kbuf: Buffer contents and memory parameters.
1030 * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
1032 * Returns 0 on success, negative errno on error.
1034 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
1036 struct crash_mem **emem;
1037 u64 buf_min, buf_max;
1040 /* Look up the exclude ranges list while locating the memory hole */
1041 emem = &(kbuf->image->arch.exclude_ranges);
1042 if (!(*emem) || ((*emem)->nr_ranges == 0)) {
1043 pr_warn("No exclude range list. Using the default locate mem hole method\n");
1044 return kexec_locate_mem_hole(kbuf);
1047 buf_min = kbuf->buf_min;
1048 buf_max = kbuf->buf_max;
1049 /* Segments for kdump kernel should be within crashkernel region */
1050 if (IS_ENABLED(CONFIG_CRASH_DUMP) && kbuf->image->type == KEXEC_TYPE_CRASH) {
1051 buf_min = (buf_min < crashk_res.start ?
1052 crashk_res.start : buf_min);
1053 buf_max = (buf_max > crashk_res.end ?
1054 crashk_res.end : buf_max);
1057 if (buf_min > buf_max) {
1058 pr_err("Invalid buffer min and/or max values\n");
1063 ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
1066 ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
1069 /* Add the buffer allocated to the exclude list for the next lookup */
1071 add_mem_range(emem, kbuf->mem, kbuf->memsz);
1072 sort_memory_ranges(*emem, true);
1074 pr_err("Failed to locate memory buffer of size %lu\n",
1081 * arch_kexec_kernel_image_probe - Does additional handling needed to setup
1083 * @image: kexec image being loaded.
1084 * @buf: Buffer pointing to elf data.
1085 * @buf_len: Length of the buffer.
1087 * Returns 0 on success, negative errno on error.
1089 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
1090 unsigned long buf_len)
1094 /* Get exclude memory ranges needed for setting up kexec segments */
1095 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
1097 pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
1101 return kexec_image_probe_default(image, buf, buf_len);
1105 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done
1106 * while loading the image.
1107 * @image: kexec image being loaded.
1109 * Returns 0 on success, negative errno on error.
1111 int arch_kimage_file_post_load_cleanup(struct kimage *image)
1113 kfree(image->arch.exclude_ranges);
1114 image->arch.exclude_ranges = NULL;
1116 vfree(image->arch.backup_buf);
1117 image->arch.backup_buf = NULL;
1119 vfree(image->elf_headers);
1120 image->elf_headers = NULL;
1121 image->elf_headers_sz = 0;
1123 kvfree(image->arch.fdt);
1124 image->arch.fdt = NULL;
1126 return kexec_image_post_load_cleanup_default(image);