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>
29 #include <asm/mmzone.h>
31 #include <asm/plpks.h>
34 u64 *buf; /* data buffer for usable-memory property */
35 u32 size; /* size allocated for the data buffer */
36 u32 max_entries; /* maximum no. of entries */
37 u32 idx; /* index of current entry */
39 /* usable memory ranges to look up */
40 unsigned int nr_ranges;
41 const struct range *ranges;
44 const struct kexec_file_ops * const kexec_file_loaders[] = {
50 * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
51 * regions like opal/rtas, tce-table, initrd,
52 * kernel, htab which should be avoided while
53 * setting up kexec load segments.
54 * @mem_ranges: Range list to add the memory ranges to.
56 * Returns 0 on success, negative errno on error.
58 static int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
62 ret = add_tce_mem_ranges(mem_ranges);
66 ret = add_initrd_mem_range(mem_ranges);
70 ret = add_htab_mem_range(mem_ranges);
74 ret = add_kernel_mem_range(mem_ranges);
78 ret = add_rtas_mem_range(mem_ranges);
82 ret = add_opal_mem_range(mem_ranges);
86 ret = add_reserved_mem_ranges(mem_ranges);
90 /* exclude memory ranges should be sorted for easy lookup */
91 sort_memory_ranges(*mem_ranges, true);
94 pr_err("Failed to setup exclude memory ranges\n");
99 * get_usable_memory_ranges - Get usable memory ranges. This list includes
100 * regions like crashkernel, opal/rtas & tce-table,
101 * that kdump kernel could use.
102 * @mem_ranges: Range list to add the memory ranges to.
104 * Returns 0 on success, negative errno on error.
106 static int get_usable_memory_ranges(struct crash_mem **mem_ranges)
111 * Early boot failure observed on guests when low memory (first memory
112 * block?) is not added to usable memory. So, add [0, crashk_res.end]
113 * instead of [crashk_res.start, crashk_res.end] to workaround it.
114 * Also, crashed kernel's memory must be added to reserve map to
115 * avoid kdump kernel from using it.
117 ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
121 ret = add_rtas_mem_range(mem_ranges);
125 ret = add_opal_mem_range(mem_ranges);
129 ret = add_tce_mem_ranges(mem_ranges);
132 pr_err("Failed to setup usable memory ranges\n");
137 * get_crash_memory_ranges - Get crash memory ranges. This list includes
138 * first/crashing kernel's memory regions that
139 * would be exported via an elfcore.
140 * @mem_ranges: Range list to add the memory ranges to.
142 * Returns 0 on success, negative errno on error.
144 static int get_crash_memory_ranges(struct crash_mem **mem_ranges)
146 phys_addr_t base, end;
147 struct crash_mem *tmem;
151 for_each_mem_range(i, &base, &end) {
152 u64 size = end - base;
154 /* Skip backup memory region, which needs a separate entry */
155 if (base == BACKUP_SRC_START) {
156 if (size > BACKUP_SRC_SIZE) {
157 base = BACKUP_SRC_END + 1;
158 size -= BACKUP_SRC_SIZE;
163 ret = add_mem_range(mem_ranges, base, size);
167 /* Try merging adjacent ranges before reallocation attempt */
168 if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
169 sort_memory_ranges(*mem_ranges, true);
172 /* Reallocate memory ranges if there is no space to split ranges */
174 if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
175 tmem = realloc_mem_ranges(mem_ranges);
180 /* Exclude crashkernel region */
181 ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
186 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
187 * regions are exported to save their context at the time of
188 * crash, they should actually be backed up just like the
189 * first 64K bytes of memory.
191 ret = add_rtas_mem_range(mem_ranges);
195 ret = add_opal_mem_range(mem_ranges);
199 /* create a separate program header for the backup region */
200 ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
204 sort_memory_ranges(*mem_ranges, false);
207 pr_err("Failed to setup crash memory ranges\n");
212 * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
213 * memory regions that should be added to the
214 * memory reserve map to ensure the region is
215 * protected from any mischief.
216 * @mem_ranges: Range list to add the memory ranges to.
218 * Returns 0 on success, negative errno on error.
220 static int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
224 ret = add_rtas_mem_range(mem_ranges);
228 ret = add_tce_mem_ranges(mem_ranges);
232 ret = add_reserved_mem_ranges(mem_ranges);
235 pr_err("Failed to setup reserved memory ranges\n");
240 * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
241 * in the memory regions between buf_min & buf_max
242 * for the buffer. If found, sets kbuf->mem.
243 * @kbuf: Buffer contents and memory parameters.
244 * @buf_min: Minimum address for the buffer.
245 * @buf_max: Maximum address for the buffer.
247 * Returns 0 on success, negative errno on error.
249 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
250 u64 buf_min, u64 buf_max)
252 int ret = -EADDRNOTAVAIL;
253 phys_addr_t start, end;
256 for_each_mem_range_rev(i, &start, &end) {
258 * memblock uses [start, end) convention while it is
259 * [start, end] here. Fix the off-by-one to have the
267 /* Memory hole not found */
271 /* Adjust memory region based on the given range */
277 start = ALIGN(start, kbuf->buf_align);
278 if (start < end && (end - start + 1) >= kbuf->memsz) {
279 /* Suitable memory range found. Set kbuf->mem */
280 kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
291 * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
292 * suitable buffer with top down approach.
293 * @kbuf: Buffer contents and memory parameters.
294 * @buf_min: Minimum address for the buffer.
295 * @buf_max: Maximum address for the buffer.
296 * @emem: Exclude memory ranges.
298 * Returns 0 on success, negative errno on error.
300 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
301 u64 buf_min, u64 buf_max,
302 const struct crash_mem *emem)
304 int i, ret = 0, err = -EADDRNOTAVAIL;
305 u64 start, end, tmin, tmax;
308 for (i = (emem->nr_ranges - 1); i >= 0; i--) {
309 start = emem->ranges[i].start;
310 end = emem->ranges[i].end;
316 tmin = (end < buf_min ? buf_min : end + 1);
317 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
324 if (tmax < buf_min) {
333 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
339 * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
340 * in the memory regions between buf_min & buf_max
341 * for the buffer. If found, sets kbuf->mem.
342 * @kbuf: Buffer contents and memory parameters.
343 * @buf_min: Minimum address for the buffer.
344 * @buf_max: Maximum address for the buffer.
346 * Returns 0 on success, negative errno on error.
348 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
349 u64 buf_min, u64 buf_max)
351 int ret = -EADDRNOTAVAIL;
352 phys_addr_t start, end;
355 for_each_mem_range(i, &start, &end) {
357 * memblock uses [start, end) convention while it is
358 * [start, end] here. Fix the off-by-one to have the
366 /* Memory hole not found */
370 /* Adjust memory region based on the given range */
376 start = ALIGN(start, kbuf->buf_align);
377 if (start < end && (end - start + 1) >= kbuf->memsz) {
378 /* Suitable memory range found. Set kbuf->mem */
389 * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
390 * suitable buffer with bottom up approach.
391 * @kbuf: Buffer contents and memory parameters.
392 * @buf_min: Minimum address for the buffer.
393 * @buf_max: Maximum address for the buffer.
394 * @emem: Exclude memory ranges.
396 * Returns 0 on success, negative errno on error.
398 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
399 u64 buf_min, u64 buf_max,
400 const struct crash_mem *emem)
402 int i, ret = 0, err = -EADDRNOTAVAIL;
403 u64 start, end, tmin, tmax;
406 for (i = 0; i < emem->nr_ranges; i++) {
407 start = emem->ranges[i].start;
408 end = emem->ranges[i].end;
414 tmax = (start > buf_max ? buf_max : start - 1);
415 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
422 if (tmin > buf_max) {
431 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
437 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
438 * @um_info: Usable memory buffer and ranges info.
439 * @cnt: No. of entries to accommodate.
441 * Frees up the old buffer if memory reallocation fails.
443 * Returns buffer on success, NULL on error.
445 static u64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
450 if ((um_info->idx + cnt) <= um_info->max_entries)
453 new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
454 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
457 um_info->size = new_size;
458 um_info->max_entries = (um_info->size / sizeof(u64));
465 * add_usable_mem - Add the usable memory ranges within the given memory range
467 * @um_info: Usable memory buffer and ranges info.
468 * @base: Base address of memory range to look for.
469 * @end: End address of memory range to look for.
471 * Returns 0 on success, negative errno on error.
473 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
475 u64 loc_base, loc_end;
479 for (i = 0; i < um_info->nr_ranges; i++) {
481 loc_base = um_info->ranges[i].start;
482 loc_end = um_info->ranges[i].end;
483 if (loc_base >= base && loc_end <= end)
485 else if (base < loc_end && end > loc_base) {
494 if (!check_realloc_usable_mem(um_info, 2))
497 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
498 um_info->buf[um_info->idx++] =
499 cpu_to_be64(loc_end - loc_base + 1);
507 * kdump_setup_usable_lmb - This is a callback function that gets called by
508 * walk_drmem_lmbs for every LMB to set its
509 * usable memory ranges.
511 * @usm: linux,drconf-usable-memory property value.
512 * @data: Pointer to usable memory buffer and ranges info.
514 * Returns 0 on success, negative errno on error.
516 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
519 struct umem_info *um_info;
524 * kdump load isn't supported on kernels already booted with
525 * linux,drconf-usable-memory property.
528 pr_err("linux,drconf-usable-memory property already exists!");
533 tmp_idx = um_info->idx;
534 if (!check_realloc_usable_mem(um_info, 1))
538 base = lmb->base_addr;
539 end = base + drmem_lmb_size() - 1;
540 ret = add_usable_mem(um_info, base, end);
543 * Update the no. of ranges added. Two entries (base & size)
544 * for every range added.
546 um_info->buf[tmp_idx] =
547 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
553 #define NODE_PATH_LEN 256
555 * add_usable_mem_property - Add usable memory property for the given
557 * @fdt: Flattened device tree for the kdump kernel.
559 * @um_info: Usable memory buffer and ranges info.
561 * Returns 0 on success, negative errno on error.
563 static int add_usable_mem_property(void *fdt, struct device_node *dn,
564 struct umem_info *um_info)
566 int n_mem_addr_cells, n_mem_size_cells, node;
567 char path[NODE_PATH_LEN];
568 int i, len, ranges, ret;
574 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
575 pr_err("Buffer (%d) too small for memory node: %pOF\n",
579 pr_debug("Memory node path: %s\n", path);
581 /* Now that we know the path, find its offset in kdump kernel's fdt */
582 node = fdt_path_offset(fdt, path);
584 pr_err("Malformed device tree: error reading %s\n", path);
589 /* Get the address & size cells */
590 n_mem_addr_cells = of_n_addr_cells(dn);
591 n_mem_size_cells = of_n_size_cells(dn);
592 pr_debug("address cells: %d, size cells: %d\n", n_mem_addr_cells,
596 if (!check_realloc_usable_mem(um_info, 2)) {
601 prop = of_get_property(dn, "reg", &len);
602 if (!prop || len <= 0) {
608 * "reg" property represents sequence of (addr,size) tuples
609 * each representing a memory range.
611 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
613 for (i = 0; i < ranges; i++) {
614 base = of_read_number(prop, n_mem_addr_cells);
615 prop += n_mem_addr_cells;
616 end = base + of_read_number(prop, n_mem_size_cells) - 1;
617 prop += n_mem_size_cells;
619 ret = add_usable_mem(um_info, base, end);
625 * No kdump kernel usable memory found in this memory node.
626 * Write (0,0) tuple in linux,usable-memory property for
627 * this region to be ignored.
629 if (um_info->idx == 0) {
635 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
636 (um_info->idx * sizeof(u64)));
645 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
646 * and linux,drconf-usable-memory DT properties as
647 * appropriate to restrict its memory usage.
648 * @fdt: Flattened device tree for the kdump kernel.
649 * @usable_mem: Usable memory ranges for kdump kernel.
651 * Returns 0 on success, negative errno on error.
653 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
655 struct umem_info um_info;
656 struct device_node *dn;
660 pr_err("Usable memory ranges for kdump kernel not found\n");
664 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
665 if (node == -FDT_ERR_NOTFOUND)
666 pr_debug("No dynamic reconfiguration memory found\n");
668 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
674 um_info.max_entries = 0;
676 /* Memory ranges to look up */
677 um_info.ranges = &(usable_mem->ranges[0]);
678 um_info.nr_ranges = usable_mem->nr_ranges;
680 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
682 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
686 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
690 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
691 um_info.buf, (um_info.idx * sizeof(u64)));
693 pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s",
700 * Walk through each memory node and set linux,usable-memory property
701 * for the corresponding node in kdump kernel's fdt.
703 for_each_node_by_type(dn, "memory") {
704 ret = add_usable_mem_property(fdt, dn, &um_info);
706 pr_err("Failed to set linux,usable-memory property for %s node",
719 * load_backup_segment - Locate a memory hole to place the backup region.
720 * @image: Kexec image.
721 * @kbuf: Buffer contents and memory parameters.
723 * Returns 0 on success, negative errno on error.
725 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
731 * Setup a source buffer for backup segment.
733 * A source buffer has no meaning for backup region as data will
734 * be copied from backup source, after crash, in the purgatory.
735 * But as load segment code doesn't recognize such segments,
736 * setup a dummy source buffer to keep it happy for now.
738 buf = vzalloc(BACKUP_SRC_SIZE);
743 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
744 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
745 kbuf->top_down = false;
747 ret = kexec_add_buffer(kbuf);
753 image->arch.backup_buf = buf;
754 image->arch.backup_start = kbuf->mem;
759 * update_backup_region_phdr - Update backup region's offset for the core to
760 * export the region appropriately.
761 * @image: Kexec image.
762 * @ehdr: ELF core header.
764 * Assumes an exclusive program header is setup for the backup region
769 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
774 phdr = (Elf64_Phdr *)(ehdr + 1);
775 for (i = 0; i < ehdr->e_phnum; i++) {
776 if (phdr->p_paddr == BACKUP_SRC_START) {
777 phdr->p_offset = image->arch.backup_start;
778 pr_debug("Backup region offset updated to 0x%lx\n",
779 image->arch.backup_start);
786 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
787 * segment needed to load kdump kernel.
788 * @image: Kexec image.
789 * @kbuf: Buffer contents and memory parameters.
791 * Returns 0 on success, negative errno on error.
793 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
795 struct crash_mem *cmem = NULL;
796 unsigned long headers_sz;
797 void *headers = NULL;
800 ret = get_crash_memory_ranges(&cmem);
804 /* Setup elfcorehdr segment */
805 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
807 pr_err("Failed to prepare elf headers for the core\n");
811 /* Fix the offset for backup region in the ELF header */
812 update_backup_region_phdr(image, headers);
814 kbuf->buffer = headers;
815 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
816 kbuf->bufsz = kbuf->memsz = headers_sz;
817 kbuf->top_down = false;
819 ret = kexec_add_buffer(kbuf);
825 image->elf_load_addr = kbuf->mem;
826 image->elf_headers_sz = headers_sz;
827 image->elf_headers = headers;
834 * load_crashdump_segments_ppc64 - Initialize the additional segements needed
835 * to load kdump kernel.
836 * @image: Kexec image.
837 * @kbuf: Buffer contents and memory parameters.
839 * Returns 0 on success, negative errno on error.
841 int load_crashdump_segments_ppc64(struct kimage *image,
842 struct kexec_buf *kbuf)
846 /* Load backup segment - first 64K bytes of the crashing kernel */
847 ret = load_backup_segment(image, kbuf);
849 pr_err("Failed to load backup segment\n");
852 pr_debug("Loaded the backup region at 0x%lx\n", kbuf->mem);
854 /* Load elfcorehdr segment - to export crashing kernel's vmcore */
855 ret = load_elfcorehdr_segment(image, kbuf);
857 pr_err("Failed to load elfcorehdr segment\n");
860 pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
861 image->elf_load_addr, kbuf->bufsz, kbuf->memsz);
867 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
868 * variables and call setup_purgatory() to initialize
869 * common global variable.
870 * @image: kexec image.
871 * @slave_code: Slave code for the purgatory.
872 * @fdt: Flattened device tree for the next kernel.
873 * @kernel_load_addr: Address where the kernel is loaded.
874 * @fdt_load_addr: Address where the flattened device tree is loaded.
876 * Returns 0 on success, negative errno on error.
878 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
879 const void *fdt, unsigned long kernel_load_addr,
880 unsigned long fdt_load_addr)
882 struct device_node *dn = NULL;
885 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
890 if (image->type == KEXEC_TYPE_CRASH) {
891 u32 my_run_at_load = 1;
894 * Tell relocatable kernel to run at load address
895 * via the word meant for that at 0x5c.
897 ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
899 sizeof(my_run_at_load),
905 /* Tell purgatory where to look for backup region */
906 ret = kexec_purgatory_get_set_symbol(image, "backup_start",
907 &image->arch.backup_start,
908 sizeof(image->arch.backup_start),
913 /* Setup OPAL base & entry values */
914 dn = of_find_node_by_path("/ibm,opal");
918 of_property_read_u64(dn, "opal-base-address", &val);
919 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
924 of_property_read_u64(dn, "opal-entry-address", &val);
925 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
930 pr_err("Failed to setup purgatory symbols");
936 * get_cpu_node_size - Compute the size of a CPU node in the FDT.
937 * This should be done only once and the value is stored in
939 * Returns the max size of a CPU node in the FDT.
941 static unsigned int cpu_node_size(void)
943 static unsigned int size;
944 struct device_node *dn;
948 * Don't compute it twice, we are assuming that the per CPU node size
949 * doesn't change during the system's life.
954 dn = of_find_node_by_type(NULL, "cpu");
955 if (WARN_ON_ONCE(!dn)) {
956 // Unlikely to happen
961 * We compute the sub node size for a CPU node, assuming it
962 * will be the same for all.
964 size += strlen(dn->name) + 5;
965 for_each_property_of_node(dn, pp) {
966 size += strlen(pp->name);
975 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to
976 * setup FDT for kexec/kdump kernel.
977 * @image: kexec image being loaded.
979 * Returns the estimated extra size needed for kexec/kdump kernel FDT.
981 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
983 unsigned int cpu_nodes, extra_size = 0;
984 struct device_node *dn;
987 // Budget some space for the password blob. There's already extra space
989 if (plpks_is_available())
990 extra_size += (unsigned int)plpks_get_passwordlen();
992 if (image->type != KEXEC_TYPE_CRASH)
996 * For kdump kernel, account for linux,usable-memory and
997 * linux,drconf-usable-memory properties. Get an approximate on the
998 * number of usable memory entries and use for FDT size estimation.
1000 if (drmem_lmb_size()) {
1001 usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) +
1002 (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
1003 extra_size += (unsigned int)(usm_entries * sizeof(u64));
1007 * Get the number of CPU nodes in the current DT. This allows to
1008 * reserve places for CPU nodes added since the boot time.
1011 for_each_node_by_type(dn, "cpu") {
1015 if (cpu_nodes > boot_cpu_node_count)
1016 extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size();
1022 * add_node_props - Reads node properties from device node structure and add
1024 * @fdt: Flattened device tree of the kernel
1025 * @node_offset: offset of the node to add a property at
1026 * @dn: device node pointer
1028 * Returns 0 on success, negative errno on error.
1030 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)
1033 struct property *pp;
1038 for_each_property_of_node(dn, pp) {
1039 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);
1041 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));
1049 * update_cpus_node - Update cpus node of flattened device tree using of_root
1051 * @fdt: Flattened device tree of the kernel.
1053 * Returns 0 on success, negative errno on error.
1055 static int update_cpus_node(void *fdt)
1057 struct device_node *cpus_node, *dn;
1058 int cpus_offset, cpus_subnode_offset, ret = 0;
1060 cpus_offset = fdt_path_offset(fdt, "/cpus");
1061 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {
1062 pr_err("Malformed device tree: error reading /cpus node: %s\n",
1063 fdt_strerror(cpus_offset));
1067 if (cpus_offset > 0) {
1068 ret = fdt_del_node(fdt, cpus_offset);
1070 pr_err("Error deleting /cpus node: %s\n", fdt_strerror(ret));
1075 /* Add cpus node to fdt */
1076 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "cpus");
1077 if (cpus_offset < 0) {
1078 pr_err("Error creating /cpus node: %s\n", fdt_strerror(cpus_offset));
1082 /* Add cpus node properties */
1083 cpus_node = of_find_node_by_path("/cpus");
1084 ret = add_node_props(fdt, cpus_offset, cpus_node);
1085 of_node_put(cpus_node);
1089 /* Loop through all subnodes of cpus and add them to fdt */
1090 for_each_node_by_type(dn, "cpu") {
1091 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);
1092 if (cpus_subnode_offset < 0) {
1093 pr_err("Unable to add %s subnode: %s\n", dn->full_name,
1094 fdt_strerror(cpus_subnode_offset));
1095 ret = cpus_subnode_offset;
1099 ret = add_node_props(fdt, cpus_subnode_offset, dn);
1108 static int copy_property(void *fdt, int node_offset, const struct device_node *dn,
1109 const char *propname)
1111 const void *prop, *fdtprop;
1112 int len = 0, fdtlen = 0;
1114 prop = of_get_property(dn, propname, &len);
1115 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen);
1117 if (fdtprop && !prop)
1118 return fdt_delprop(fdt, node_offset, propname);
1120 return fdt_setprop(fdt, node_offset, propname, prop, len);
1122 return -FDT_ERR_NOTFOUND;
1125 static int update_pci_dma_nodes(void *fdt, const char *dmapropname)
1127 struct device_node *dn;
1128 int pci_offset, root_offset, ret = 0;
1130 if (!firmware_has_feature(FW_FEATURE_LPAR))
1133 root_offset = fdt_path_offset(fdt, "/");
1134 for_each_node_with_property(dn, dmapropname) {
1135 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn));
1139 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window");
1142 ret = copy_property(fdt, pci_offset, dn, dmapropname);
1151 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
1153 * @image: kexec image being loaded.
1154 * @fdt: Flattened device tree for the next kernel.
1155 * @initrd_load_addr: Address where the next initrd will be loaded.
1156 * @initrd_len: Size of the next initrd, or 0 if there will be none.
1157 * @cmdline: Command line for the next kernel, or NULL if there will
1160 * Returns 0 on success, negative errno on error.
1162 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
1163 unsigned long initrd_load_addr,
1164 unsigned long initrd_len, const char *cmdline)
1166 struct crash_mem *umem = NULL, *rmem = NULL;
1167 int i, nr_ranges, ret;
1170 * Restrict memory usage for kdump kernel by setting up
1171 * usable memory ranges and memory reserve map.
1173 if (image->type == KEXEC_TYPE_CRASH) {
1174 ret = get_usable_memory_ranges(&umem);
1178 ret = update_usable_mem_fdt(fdt, umem);
1180 pr_err("Error setting up usable-memory property for kdump kernel\n");
1185 * Ensure we don't touch crashed kernel's memory except the
1186 * first 64K of RAM, which will be backed up.
1188 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
1189 crashk_res.start - BACKUP_SRC_SIZE);
1191 pr_err("Error reserving crash memory: %s\n",
1196 /* Ensure backup region is not used by kdump/capture kernel */
1197 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
1200 pr_err("Error reserving memory for backup: %s\n",
1206 /* Update cpus nodes information to account hotplug CPUs. */
1207 ret = update_cpus_node(fdt);
1211 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
1212 #define DMA64_PROPNAME "linux,dma64-ddr-window-info"
1213 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME);
1217 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME);
1220 #undef DMA64_PROPNAME
1221 #undef DIRECT64_PROPNAME
1223 /* Update memory reserve map */
1224 ret = get_reserved_memory_ranges(&rmem);
1228 nr_ranges = rmem ? rmem->nr_ranges : 0;
1229 for (i = 0; i < nr_ranges; i++) {
1232 base = rmem->ranges[i].start;
1233 size = rmem->ranges[i].end - base + 1;
1234 ret = fdt_add_mem_rsv(fdt, base, size);
1236 pr_err("Error updating memory reserve map: %s\n",
1242 // If we have PLPKS active, we need to provide the password to the new kernel
1243 if (plpks_is_available())
1244 ret = plpks_populate_fdt(fdt);
1253 * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
1254 * tce-table, reserved-ranges & such (exclude
1255 * memory ranges) as they can't be used for kexec
1256 * segment buffer. Sets kbuf->mem when a suitable
1257 * memory hole is found.
1258 * @kbuf: Buffer contents and memory parameters.
1260 * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
1262 * Returns 0 on success, negative errno on error.
1264 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
1266 struct crash_mem **emem;
1267 u64 buf_min, buf_max;
1270 /* Look up the exclude ranges list while locating the memory hole */
1271 emem = &(kbuf->image->arch.exclude_ranges);
1272 if (!(*emem) || ((*emem)->nr_ranges == 0)) {
1273 pr_warn("No exclude range list. Using the default locate mem hole method\n");
1274 return kexec_locate_mem_hole(kbuf);
1277 buf_min = kbuf->buf_min;
1278 buf_max = kbuf->buf_max;
1279 /* Segments for kdump kernel should be within crashkernel region */
1280 if (kbuf->image->type == KEXEC_TYPE_CRASH) {
1281 buf_min = (buf_min < crashk_res.start ?
1282 crashk_res.start : buf_min);
1283 buf_max = (buf_max > crashk_res.end ?
1284 crashk_res.end : buf_max);
1287 if (buf_min > buf_max) {
1288 pr_err("Invalid buffer min and/or max values\n");
1293 ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
1296 ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
1299 /* Add the buffer allocated to the exclude list for the next lookup */
1301 add_mem_range(emem, kbuf->mem, kbuf->memsz);
1302 sort_memory_ranges(*emem, true);
1304 pr_err("Failed to locate memory buffer of size %lu\n",
1311 * arch_kexec_kernel_image_probe - Does additional handling needed to setup
1313 * @image: kexec image being loaded.
1314 * @buf: Buffer pointing to elf data.
1315 * @buf_len: Length of the buffer.
1317 * Returns 0 on success, negative errno on error.
1319 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
1320 unsigned long buf_len)
1324 /* Get exclude memory ranges needed for setting up kexec segments */
1325 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
1327 pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
1331 return kexec_image_probe_default(image, buf, buf_len);
1335 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done
1336 * while loading the image.
1337 * @image: kexec image being loaded.
1339 * Returns 0 on success, negative errno on error.
1341 int arch_kimage_file_post_load_cleanup(struct kimage *image)
1343 kfree(image->arch.exclude_ranges);
1344 image->arch.exclude_ranges = NULL;
1346 vfree(image->arch.backup_buf);
1347 image->arch.backup_buf = NULL;
1349 vfree(image->elf_headers);
1350 image->elf_headers = NULL;
1351 image->elf_headers_sz = 0;
1353 kvfree(image->arch.fdt);
1354 image->arch.fdt = NULL;
1356 return kexec_image_post_load_cleanup_default(image);