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>
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>
35 u64 *buf; /* data buffer for usable-memory property */
36 u32 size; /* size allocated for the data buffer */
37 u32 max_entries; /* maximum no. of entries */
38 u32 idx; /* index of current entry */
40 /* usable memory ranges to look up */
41 unsigned int nr_ranges;
42 const struct range *ranges;
45 const struct kexec_file_ops * const kexec_file_loaders[] = {
51 * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
52 * regions like opal/rtas, tce-table, initrd,
53 * kernel, htab which should be avoided while
54 * setting up kexec load segments.
55 * @mem_ranges: Range list to add the memory ranges to.
57 * Returns 0 on success, negative errno on error.
59 static int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
63 ret = add_tce_mem_ranges(mem_ranges);
67 ret = add_initrd_mem_range(mem_ranges);
71 ret = add_htab_mem_range(mem_ranges);
75 ret = add_kernel_mem_range(mem_ranges);
79 ret = add_rtas_mem_range(mem_ranges);
83 ret = add_opal_mem_range(mem_ranges);
87 ret = add_reserved_mem_ranges(mem_ranges);
91 /* exclude memory ranges should be sorted for easy lookup */
92 sort_memory_ranges(*mem_ranges, true);
95 pr_err("Failed to setup exclude memory ranges\n");
100 * get_usable_memory_ranges - Get usable memory ranges. This list includes
101 * regions like crashkernel, opal/rtas & tce-table,
102 * that kdump kernel could use.
103 * @mem_ranges: Range list to add the memory ranges to.
105 * Returns 0 on success, negative errno on error.
107 static int get_usable_memory_ranges(struct crash_mem **mem_ranges)
112 * Early boot failure observed on guests when low memory (first memory
113 * block?) is not added to usable memory. So, add [0, crashk_res.end]
114 * instead of [crashk_res.start, crashk_res.end] to workaround it.
115 * Also, crashed kernel's memory must be added to reserve map to
116 * avoid kdump kernel from using it.
118 ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
122 ret = add_rtas_mem_range(mem_ranges);
126 ret = add_opal_mem_range(mem_ranges);
130 ret = add_tce_mem_ranges(mem_ranges);
133 pr_err("Failed to setup usable memory ranges\n");
138 * get_crash_memory_ranges - Get crash memory ranges. This list includes
139 * first/crashing kernel's memory regions that
140 * would be exported via an elfcore.
141 * @mem_ranges: Range list to add the memory ranges to.
143 * Returns 0 on success, negative errno on error.
145 static int get_crash_memory_ranges(struct crash_mem **mem_ranges)
147 phys_addr_t base, end;
148 struct crash_mem *tmem;
152 for_each_mem_range(i, &base, &end) {
153 u64 size = end - base;
155 /* Skip backup memory region, which needs a separate entry */
156 if (base == BACKUP_SRC_START) {
157 if (size > BACKUP_SRC_SIZE) {
158 base = BACKUP_SRC_END + 1;
159 size -= BACKUP_SRC_SIZE;
164 ret = add_mem_range(mem_ranges, base, size);
168 /* Try merging adjacent ranges before reallocation attempt */
169 if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
170 sort_memory_ranges(*mem_ranges, true);
173 /* Reallocate memory ranges if there is no space to split ranges */
175 if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
176 tmem = realloc_mem_ranges(mem_ranges);
181 /* Exclude crashkernel region */
182 ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
187 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
188 * regions are exported to save their context at the time of
189 * crash, they should actually be backed up just like the
190 * first 64K bytes of memory.
192 ret = add_rtas_mem_range(mem_ranges);
196 ret = add_opal_mem_range(mem_ranges);
200 /* create a separate program header for the backup region */
201 ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
205 sort_memory_ranges(*mem_ranges, false);
208 pr_err("Failed to setup crash memory ranges\n");
213 * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
214 * memory regions that should be added to the
215 * memory reserve map to ensure the region is
216 * protected from any mischief.
217 * @mem_ranges: Range list to add the memory ranges to.
219 * Returns 0 on success, negative errno on error.
221 static int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
225 ret = add_rtas_mem_range(mem_ranges);
229 ret = add_tce_mem_ranges(mem_ranges);
233 ret = add_reserved_mem_ranges(mem_ranges);
236 pr_err("Failed to setup reserved memory ranges\n");
241 * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
242 * in the memory regions between buf_min & buf_max
243 * for the buffer. If found, sets kbuf->mem.
244 * @kbuf: Buffer contents and memory parameters.
245 * @buf_min: Minimum address for the buffer.
246 * @buf_max: Maximum address for the buffer.
248 * Returns 0 on success, negative errno on error.
250 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
251 u64 buf_min, u64 buf_max)
253 int ret = -EADDRNOTAVAIL;
254 phys_addr_t start, end;
257 for_each_mem_range_rev(i, &start, &end) {
259 * memblock uses [start, end) convention while it is
260 * [start, end] here. Fix the off-by-one to have the
268 /* Memory hole not found */
272 /* Adjust memory region based on the given range */
278 start = ALIGN(start, kbuf->buf_align);
279 if (start < end && (end - start + 1) >= kbuf->memsz) {
280 /* Suitable memory range found. Set kbuf->mem */
281 kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
292 * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
293 * suitable buffer with top down approach.
294 * @kbuf: Buffer contents and memory parameters.
295 * @buf_min: Minimum address for the buffer.
296 * @buf_max: Maximum address for the buffer.
297 * @emem: Exclude memory ranges.
299 * Returns 0 on success, negative errno on error.
301 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
302 u64 buf_min, u64 buf_max,
303 const struct crash_mem *emem)
305 int i, ret = 0, err = -EADDRNOTAVAIL;
306 u64 start, end, tmin, tmax;
309 for (i = (emem->nr_ranges - 1); i >= 0; i--) {
310 start = emem->ranges[i].start;
311 end = emem->ranges[i].end;
317 tmin = (end < buf_min ? buf_min : end + 1);
318 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
325 if (tmax < buf_min) {
334 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
340 * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
341 * in the memory regions between buf_min & buf_max
342 * for the buffer. If found, sets kbuf->mem.
343 * @kbuf: Buffer contents and memory parameters.
344 * @buf_min: Minimum address for the buffer.
345 * @buf_max: Maximum address for the buffer.
347 * Returns 0 on success, negative errno on error.
349 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
350 u64 buf_min, u64 buf_max)
352 int ret = -EADDRNOTAVAIL;
353 phys_addr_t start, end;
356 for_each_mem_range(i, &start, &end) {
358 * memblock uses [start, end) convention while it is
359 * [start, end] here. Fix the off-by-one to have the
367 /* Memory hole not found */
371 /* Adjust memory region based on the given range */
377 start = ALIGN(start, kbuf->buf_align);
378 if (start < end && (end - start + 1) >= kbuf->memsz) {
379 /* Suitable memory range found. Set kbuf->mem */
390 * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
391 * suitable buffer with bottom up approach.
392 * @kbuf: Buffer contents and memory parameters.
393 * @buf_min: Minimum address for the buffer.
394 * @buf_max: Maximum address for the buffer.
395 * @emem: Exclude memory ranges.
397 * Returns 0 on success, negative errno on error.
399 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
400 u64 buf_min, u64 buf_max,
401 const struct crash_mem *emem)
403 int i, ret = 0, err = -EADDRNOTAVAIL;
404 u64 start, end, tmin, tmax;
407 for (i = 0; i < emem->nr_ranges; i++) {
408 start = emem->ranges[i].start;
409 end = emem->ranges[i].end;
415 tmax = (start > buf_max ? buf_max : start - 1);
416 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
423 if (tmin > buf_max) {
432 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
438 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
439 * @um_info: Usable memory buffer and ranges info.
440 * @cnt: No. of entries to accommodate.
442 * Frees up the old buffer if memory reallocation fails.
444 * Returns buffer on success, NULL on error.
446 static u64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
451 if ((um_info->idx + cnt) <= um_info->max_entries)
454 new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
455 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
458 um_info->size = new_size;
459 um_info->max_entries = (um_info->size / sizeof(u64));
466 * add_usable_mem - Add the usable memory ranges within the given memory range
468 * @um_info: Usable memory buffer and ranges info.
469 * @base: Base address of memory range to look for.
470 * @end: End address of memory range to look for.
472 * Returns 0 on success, negative errno on error.
474 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
476 u64 loc_base, loc_end;
480 for (i = 0; i < um_info->nr_ranges; i++) {
482 loc_base = um_info->ranges[i].start;
483 loc_end = um_info->ranges[i].end;
484 if (loc_base >= base && loc_end <= end)
486 else if (base < loc_end && end > loc_base) {
495 if (!check_realloc_usable_mem(um_info, 2))
498 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
499 um_info->buf[um_info->idx++] =
500 cpu_to_be64(loc_end - loc_base + 1);
508 * kdump_setup_usable_lmb - This is a callback function that gets called by
509 * walk_drmem_lmbs for every LMB to set its
510 * usable memory ranges.
512 * @usm: linux,drconf-usable-memory property value.
513 * @data: Pointer to usable memory buffer and ranges info.
515 * Returns 0 on success, negative errno on error.
517 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
520 struct umem_info *um_info;
525 * kdump load isn't supported on kernels already booted with
526 * linux,drconf-usable-memory property.
529 pr_err("linux,drconf-usable-memory property already exists!");
534 tmp_idx = um_info->idx;
535 if (!check_realloc_usable_mem(um_info, 1))
539 base = lmb->base_addr;
540 end = base + drmem_lmb_size() - 1;
541 ret = add_usable_mem(um_info, base, end);
544 * Update the no. of ranges added. Two entries (base & size)
545 * for every range added.
547 um_info->buf[tmp_idx] =
548 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
554 #define NODE_PATH_LEN 256
556 * add_usable_mem_property - Add usable memory property for the given
558 * @fdt: Flattened device tree for the kdump kernel.
560 * @um_info: Usable memory buffer and ranges info.
562 * Returns 0 on success, negative errno on error.
564 static int add_usable_mem_property(void *fdt, struct device_node *dn,
565 struct umem_info *um_info)
567 int n_mem_addr_cells, n_mem_size_cells, node;
568 char path[NODE_PATH_LEN];
569 int i, len, ranges, ret;
575 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
576 pr_err("Buffer (%d) too small for memory node: %pOF\n",
580 pr_debug("Memory node path: %s\n", path);
582 /* Now that we know the path, find its offset in kdump kernel's fdt */
583 node = fdt_path_offset(fdt, path);
585 pr_err("Malformed device tree: error reading %s\n", path);
590 /* Get the address & size cells */
591 n_mem_addr_cells = of_n_addr_cells(dn);
592 n_mem_size_cells = of_n_size_cells(dn);
593 pr_debug("address cells: %d, size cells: %d\n", n_mem_addr_cells,
597 if (!check_realloc_usable_mem(um_info, 2)) {
602 prop = of_get_property(dn, "reg", &len);
603 if (!prop || len <= 0) {
609 * "reg" property represents sequence of (addr,size) tuples
610 * each representing a memory range.
612 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
614 for (i = 0; i < ranges; i++) {
615 base = of_read_number(prop, n_mem_addr_cells);
616 prop += n_mem_addr_cells;
617 end = base + of_read_number(prop, n_mem_size_cells) - 1;
618 prop += n_mem_size_cells;
620 ret = add_usable_mem(um_info, base, end);
626 * No kdump kernel usable memory found in this memory node.
627 * Write (0,0) tuple in linux,usable-memory property for
628 * this region to be ignored.
630 if (um_info->idx == 0) {
636 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
637 (um_info->idx * sizeof(u64)));
646 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
647 * and linux,drconf-usable-memory DT properties as
648 * appropriate to restrict its memory usage.
649 * @fdt: Flattened device tree for the kdump kernel.
650 * @usable_mem: Usable memory ranges for kdump kernel.
652 * Returns 0 on success, negative errno on error.
654 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
656 struct umem_info um_info;
657 struct device_node *dn;
661 pr_err("Usable memory ranges for kdump kernel not found\n");
665 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
666 if (node == -FDT_ERR_NOTFOUND)
667 pr_debug("No dynamic reconfiguration memory found\n");
669 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
675 um_info.max_entries = 0;
677 /* Memory ranges to look up */
678 um_info.ranges = &(usable_mem->ranges[0]);
679 um_info.nr_ranges = usable_mem->nr_ranges;
681 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
683 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
687 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
691 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
692 um_info.buf, (um_info.idx * sizeof(u64)));
694 pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s",
701 * Walk through each memory node and set linux,usable-memory property
702 * for the corresponding node in kdump kernel's fdt.
704 for_each_node_by_type(dn, "memory") {
705 ret = add_usable_mem_property(fdt, dn, &um_info);
707 pr_err("Failed to set linux,usable-memory property for %s node",
720 * load_backup_segment - Locate a memory hole to place the backup region.
721 * @image: Kexec image.
722 * @kbuf: Buffer contents and memory parameters.
724 * Returns 0 on success, negative errno on error.
726 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
732 * Setup a source buffer for backup segment.
734 * A source buffer has no meaning for backup region as data will
735 * be copied from backup source, after crash, in the purgatory.
736 * But as load segment code doesn't recognize such segments,
737 * setup a dummy source buffer to keep it happy for now.
739 buf = vzalloc(BACKUP_SRC_SIZE);
744 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
745 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
746 kbuf->top_down = false;
748 ret = kexec_add_buffer(kbuf);
754 image->arch.backup_buf = buf;
755 image->arch.backup_start = kbuf->mem;
760 * update_backup_region_phdr - Update backup region's offset for the core to
761 * export the region appropriately.
762 * @image: Kexec image.
763 * @ehdr: ELF core header.
765 * Assumes an exclusive program header is setup for the backup region
770 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
775 phdr = (Elf64_Phdr *)(ehdr + 1);
776 for (i = 0; i < ehdr->e_phnum; i++) {
777 if (phdr->p_paddr == BACKUP_SRC_START) {
778 phdr->p_offset = image->arch.backup_start;
779 pr_debug("Backup region offset updated to 0x%lx\n",
780 image->arch.backup_start);
787 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
788 * segment needed to load kdump kernel.
789 * @image: Kexec image.
790 * @kbuf: Buffer contents and memory parameters.
792 * Returns 0 on success, negative errno on error.
794 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
796 struct crash_mem *cmem = NULL;
797 unsigned long headers_sz;
798 void *headers = NULL;
801 ret = get_crash_memory_ranges(&cmem);
805 /* Setup elfcorehdr segment */
806 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
808 pr_err("Failed to prepare elf headers for the core\n");
812 /* Fix the offset for backup region in the ELF header */
813 update_backup_region_phdr(image, headers);
815 kbuf->buffer = headers;
816 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
817 kbuf->bufsz = kbuf->memsz = headers_sz;
818 kbuf->top_down = false;
820 ret = kexec_add_buffer(kbuf);
826 image->elf_load_addr = kbuf->mem;
827 image->elf_headers_sz = headers_sz;
828 image->elf_headers = headers;
835 * load_crashdump_segments_ppc64 - Initialize the additional segements needed
836 * to load kdump kernel.
837 * @image: Kexec image.
838 * @kbuf: Buffer contents and memory parameters.
840 * Returns 0 on success, negative errno on error.
842 int load_crashdump_segments_ppc64(struct kimage *image,
843 struct kexec_buf *kbuf)
847 /* Load backup segment - first 64K bytes of the crashing kernel */
848 ret = load_backup_segment(image, kbuf);
850 pr_err("Failed to load backup segment\n");
853 pr_debug("Loaded the backup region at 0x%lx\n", kbuf->mem);
855 /* Load elfcorehdr segment - to export crashing kernel's vmcore */
856 ret = load_elfcorehdr_segment(image, kbuf);
858 pr_err("Failed to load elfcorehdr segment\n");
861 pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
862 image->elf_load_addr, kbuf->bufsz, kbuf->memsz);
868 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
869 * variables and call setup_purgatory() to initialize
870 * common global variable.
871 * @image: kexec image.
872 * @slave_code: Slave code for the purgatory.
873 * @fdt: Flattened device tree for the next kernel.
874 * @kernel_load_addr: Address where the kernel is loaded.
875 * @fdt_load_addr: Address where the flattened device tree is loaded.
877 * Returns 0 on success, negative errno on error.
879 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
880 const void *fdt, unsigned long kernel_load_addr,
881 unsigned long fdt_load_addr)
883 struct device_node *dn = NULL;
886 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
891 if (image->type == KEXEC_TYPE_CRASH) {
892 u32 my_run_at_load = 1;
895 * Tell relocatable kernel to run at load address
896 * via the word meant for that at 0x5c.
898 ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
900 sizeof(my_run_at_load),
906 /* Tell purgatory where to look for backup region */
907 ret = kexec_purgatory_get_set_symbol(image, "backup_start",
908 &image->arch.backup_start,
909 sizeof(image->arch.backup_start),
914 /* Setup OPAL base & entry values */
915 dn = of_find_node_by_path("/ibm,opal");
919 of_property_read_u64(dn, "opal-base-address", &val);
920 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
925 of_property_read_u64(dn, "opal-entry-address", &val);
926 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
931 pr_err("Failed to setup purgatory symbols");
937 * cpu_node_size - Compute the size of a CPU node in the FDT.
938 * This should be done only once and the value is stored in
940 * Returns the max size of a CPU node in the FDT.
942 static unsigned int cpu_node_size(void)
944 static unsigned int size;
945 struct device_node *dn;
949 * Don't compute it twice, we are assuming that the per CPU node size
950 * doesn't change during the system's life.
955 dn = of_find_node_by_type(NULL, "cpu");
956 if (WARN_ON_ONCE(!dn)) {
957 // Unlikely to happen
962 * We compute the sub node size for a CPU node, assuming it
963 * will be the same for all.
965 size += strlen(dn->name) + 5;
966 for_each_property_of_node(dn, pp) {
967 size += strlen(pp->name);
976 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to
977 * setup FDT for kexec/kdump kernel.
978 * @image: kexec image being loaded.
980 * Returns the estimated extra size needed for kexec/kdump kernel FDT.
982 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
984 unsigned int cpu_nodes, extra_size = 0;
985 struct device_node *dn;
988 // Budget some space for the password blob. There's already extra space
990 if (plpks_is_available())
991 extra_size += (unsigned int)plpks_get_passwordlen();
993 if (image->type != KEXEC_TYPE_CRASH)
997 * For kdump kernel, account for linux,usable-memory and
998 * linux,drconf-usable-memory properties. Get an approximate on the
999 * number of usable memory entries and use for FDT size estimation.
1001 if (drmem_lmb_size()) {
1002 usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) +
1003 (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
1004 extra_size += (unsigned int)(usm_entries * sizeof(u64));
1008 * Get the number of CPU nodes in the current DT. This allows to
1009 * reserve places for CPU nodes added since the boot time.
1012 for_each_node_by_type(dn, "cpu") {
1016 if (cpu_nodes > boot_cpu_node_count)
1017 extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size();
1023 * add_node_props - Reads node properties from device node structure and add
1025 * @fdt: Flattened device tree of the kernel
1026 * @node_offset: offset of the node to add a property at
1027 * @dn: device node pointer
1029 * Returns 0 on success, negative errno on error.
1031 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)
1034 struct property *pp;
1039 for_each_property_of_node(dn, pp) {
1040 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);
1042 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));
1050 * update_cpus_node - Update cpus node of flattened device tree using of_root
1052 * @fdt: Flattened device tree of the kernel.
1054 * Returns 0 on success, negative errno on error.
1056 static int update_cpus_node(void *fdt)
1058 struct device_node *cpus_node, *dn;
1059 int cpus_offset, cpus_subnode_offset, ret = 0;
1061 cpus_offset = fdt_path_offset(fdt, "/cpus");
1062 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {
1063 pr_err("Malformed device tree: error reading /cpus node: %s\n",
1064 fdt_strerror(cpus_offset));
1068 if (cpus_offset > 0) {
1069 ret = fdt_del_node(fdt, cpus_offset);
1071 pr_err("Error deleting /cpus node: %s\n", fdt_strerror(ret));
1076 /* Add cpus node to fdt */
1077 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "cpus");
1078 if (cpus_offset < 0) {
1079 pr_err("Error creating /cpus node: %s\n", fdt_strerror(cpus_offset));
1083 /* Add cpus node properties */
1084 cpus_node = of_find_node_by_path("/cpus");
1085 ret = add_node_props(fdt, cpus_offset, cpus_node);
1086 of_node_put(cpus_node);
1090 /* Loop through all subnodes of cpus and add them to fdt */
1091 for_each_node_by_type(dn, "cpu") {
1092 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);
1093 if (cpus_subnode_offset < 0) {
1094 pr_err("Unable to add %s subnode: %s\n", dn->full_name,
1095 fdt_strerror(cpus_subnode_offset));
1096 ret = cpus_subnode_offset;
1100 ret = add_node_props(fdt, cpus_subnode_offset, dn);
1109 static int copy_property(void *fdt, int node_offset, const struct device_node *dn,
1110 const char *propname)
1112 const void *prop, *fdtprop;
1113 int len = 0, fdtlen = 0;
1115 prop = of_get_property(dn, propname, &len);
1116 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen);
1118 if (fdtprop && !prop)
1119 return fdt_delprop(fdt, node_offset, propname);
1121 return fdt_setprop(fdt, node_offset, propname, prop, len);
1123 return -FDT_ERR_NOTFOUND;
1126 static int update_pci_dma_nodes(void *fdt, const char *dmapropname)
1128 struct device_node *dn;
1129 int pci_offset, root_offset, ret = 0;
1131 if (!firmware_has_feature(FW_FEATURE_LPAR))
1134 root_offset = fdt_path_offset(fdt, "/");
1135 for_each_node_with_property(dn, dmapropname) {
1136 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn));
1140 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window");
1143 ret = copy_property(fdt, pci_offset, dn, dmapropname);
1152 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
1154 * @image: kexec image being loaded.
1155 * @fdt: Flattened device tree for the next kernel.
1156 * @initrd_load_addr: Address where the next initrd will be loaded.
1157 * @initrd_len: Size of the next initrd, or 0 if there will be none.
1158 * @cmdline: Command line for the next kernel, or NULL if there will
1161 * Returns 0 on success, negative errno on error.
1163 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
1164 unsigned long initrd_load_addr,
1165 unsigned long initrd_len, const char *cmdline)
1167 struct crash_mem *umem = NULL, *rmem = NULL;
1168 int i, nr_ranges, ret;
1171 * Restrict memory usage for kdump kernel by setting up
1172 * usable memory ranges and memory reserve map.
1174 if (image->type == KEXEC_TYPE_CRASH) {
1175 ret = get_usable_memory_ranges(&umem);
1179 ret = update_usable_mem_fdt(fdt, umem);
1181 pr_err("Error setting up usable-memory property for kdump kernel\n");
1186 * Ensure we don't touch crashed kernel's memory except the
1187 * first 64K of RAM, which will be backed up.
1189 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
1190 crashk_res.start - BACKUP_SRC_SIZE);
1192 pr_err("Error reserving crash memory: %s\n",
1197 /* Ensure backup region is not used by kdump/capture kernel */
1198 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
1201 pr_err("Error reserving memory for backup: %s\n",
1207 /* Update cpus nodes information to account hotplug CPUs. */
1208 ret = update_cpus_node(fdt);
1212 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME);
1216 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME);
1220 /* Update memory reserve map */
1221 ret = get_reserved_memory_ranges(&rmem);
1225 nr_ranges = rmem ? rmem->nr_ranges : 0;
1226 for (i = 0; i < nr_ranges; i++) {
1229 base = rmem->ranges[i].start;
1230 size = rmem->ranges[i].end - base + 1;
1231 ret = fdt_add_mem_rsv(fdt, base, size);
1233 pr_err("Error updating memory reserve map: %s\n",
1239 // If we have PLPKS active, we need to provide the password to the new kernel
1240 if (plpks_is_available())
1241 ret = plpks_populate_fdt(fdt);
1250 * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
1251 * tce-table, reserved-ranges & such (exclude
1252 * memory ranges) as they can't be used for kexec
1253 * segment buffer. Sets kbuf->mem when a suitable
1254 * memory hole is found.
1255 * @kbuf: Buffer contents and memory parameters.
1257 * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
1259 * Returns 0 on success, negative errno on error.
1261 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
1263 struct crash_mem **emem;
1264 u64 buf_min, buf_max;
1267 /* Look up the exclude ranges list while locating the memory hole */
1268 emem = &(kbuf->image->arch.exclude_ranges);
1269 if (!(*emem) || ((*emem)->nr_ranges == 0)) {
1270 pr_warn("No exclude range list. Using the default locate mem hole method\n");
1271 return kexec_locate_mem_hole(kbuf);
1274 buf_min = kbuf->buf_min;
1275 buf_max = kbuf->buf_max;
1276 /* Segments for kdump kernel should be within crashkernel region */
1277 if (kbuf->image->type == KEXEC_TYPE_CRASH) {
1278 buf_min = (buf_min < crashk_res.start ?
1279 crashk_res.start : buf_min);
1280 buf_max = (buf_max > crashk_res.end ?
1281 crashk_res.end : buf_max);
1284 if (buf_min > buf_max) {
1285 pr_err("Invalid buffer min and/or max values\n");
1290 ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
1293 ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
1296 /* Add the buffer allocated to the exclude list for the next lookup */
1298 add_mem_range(emem, kbuf->mem, kbuf->memsz);
1299 sort_memory_ranges(*emem, true);
1301 pr_err("Failed to locate memory buffer of size %lu\n",
1308 * arch_kexec_kernel_image_probe - Does additional handling needed to setup
1310 * @image: kexec image being loaded.
1311 * @buf: Buffer pointing to elf data.
1312 * @buf_len: Length of the buffer.
1314 * Returns 0 on success, negative errno on error.
1316 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
1317 unsigned long buf_len)
1321 /* Get exclude memory ranges needed for setting up kexec segments */
1322 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
1324 pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
1328 return kexec_image_probe_default(image, buf, buf_len);
1332 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done
1333 * while loading the image.
1334 * @image: kexec image being loaded.
1336 * Returns 0 on success, negative errno on error.
1338 int arch_kimage_file_post_load_cleanup(struct kimage *image)
1340 kfree(image->arch.exclude_ranges);
1341 image->arch.exclude_ranges = NULL;
1343 vfree(image->arch.backup_buf);
1344 image->arch.backup_buf = NULL;
1346 vfree(image->elf_headers);
1347 image->elf_headers = NULL;
1348 image->elf_headers_sz = 0;
1350 kvfree(image->arch.fdt);
1351 image->arch.fdt = NULL;
1353 return kexec_image_post_load_cleanup_default(image);