1 // SPDX-License-Identifier: GPL-2.0-only
3 * powerpc 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, fs2dt.c.
13 * Heavily modified for the kernel by
14 * Hari Bathini, IBM Corporation.
17 #define pr_fmt(fmt) "kexec ranges: " fmt
19 #include <linux/sort.h>
20 #include <linux/kexec.h>
21 #include <linux/of_device.h>
22 #include <linux/slab.h>
23 #include <asm/sections.h>
24 #include <asm/kexec_ranges.h>
27 * get_max_nr_ranges - Get the max no. of ranges crash_mem structure
28 * could hold, given the size allocated for it.
29 * @size: Allocation size of crash_mem structure.
31 * Returns the maximum no. of ranges.
33 static inline unsigned int get_max_nr_ranges(size_t size
)
35 return ((size
- sizeof(struct crash_mem
)) /
36 sizeof(struct crash_mem_range
));
40 * get_mem_rngs_size - Get the allocated size of mem_rngs based on
41 * max_nr_ranges and chunk size.
42 * @mem_rngs: Memory ranges.
44 * Returns the maximum size of @mem_rngs.
46 static inline size_t get_mem_rngs_size(struct crash_mem
*mem_rngs
)
53 size
= (sizeof(struct crash_mem
) +
54 (mem_rngs
->max_nr_ranges
* sizeof(struct crash_mem_range
)));
57 * Memory is allocated in size multiple of MEM_RANGE_CHUNK_SZ.
58 * So, align to get the actual length.
60 return ALIGN(size
, MEM_RANGE_CHUNK_SZ
);
64 * __add_mem_range - add a memory range to memory ranges list.
65 * @mem_ranges: Range list to add the memory range to.
66 * @base: Base address of the range to add.
67 * @size: Size of the memory range to add.
69 * (Re)allocates memory, if needed.
71 * Returns 0 on success, negative errno on error.
73 static int __add_mem_range(struct crash_mem
**mem_ranges
, u64 base
, u64 size
)
75 struct crash_mem
*mem_rngs
= *mem_ranges
;
77 if (!mem_rngs
|| (mem_rngs
->nr_ranges
== mem_rngs
->max_nr_ranges
)) {
78 mem_rngs
= realloc_mem_ranges(mem_ranges
);
83 mem_rngs
->ranges
[mem_rngs
->nr_ranges
].start
= base
;
84 mem_rngs
->ranges
[mem_rngs
->nr_ranges
].end
= base
+ size
- 1;
85 pr_debug("Added memory range [%#016llx - %#016llx] at index %d\n",
86 base
, base
+ size
- 1, mem_rngs
->nr_ranges
);
87 mem_rngs
->nr_ranges
++;
92 * __merge_memory_ranges - Merges the given memory ranges list.
93 * @mem_rngs: Range list to merge.
95 * Assumes a sorted range list.
99 static void __merge_memory_ranges(struct crash_mem
*mem_rngs
)
101 struct crash_mem_range
*ranges
;
108 ranges
= &(mem_rngs
->ranges
[0]);
109 for (i
= 1; i
< mem_rngs
->nr_ranges
; i
++) {
110 if (ranges
[i
].start
<= (ranges
[i
-1].end
+ 1))
111 ranges
[idx
].end
= ranges
[i
].end
;
117 ranges
[idx
] = ranges
[i
];
120 mem_rngs
->nr_ranges
= idx
+ 1;
123 /* cmp_func_t callback to sort ranges with sort() */
124 static int rngcmp(const void *_x
, const void *_y
)
126 const struct crash_mem_range
*x
= _x
, *y
= _y
;
128 if (x
->start
> y
->start
)
130 if (x
->start
< y
->start
)
136 * sort_memory_ranges - Sorts the given memory ranges list.
137 * @mem_rngs: Range list to sort.
138 * @merge: If true, merge the list after sorting.
142 void sort_memory_ranges(struct crash_mem
*mem_rngs
, bool merge
)
149 /* Sort the ranges in-place */
150 sort(&(mem_rngs
->ranges
[0]), mem_rngs
->nr_ranges
,
151 sizeof(mem_rngs
->ranges
[0]), rngcmp
, NULL
);
154 __merge_memory_ranges(mem_rngs
);
156 /* For debugging purpose */
157 pr_debug("Memory ranges:\n");
158 for (i
= 0; i
< mem_rngs
->nr_ranges
; i
++) {
159 pr_debug("\t[%03d][%#016llx - %#016llx]\n", i
,
160 mem_rngs
->ranges
[i
].start
,
161 mem_rngs
->ranges
[i
].end
);
166 * realloc_mem_ranges - reallocate mem_ranges with size incremented
167 * by MEM_RANGE_CHUNK_SZ. Frees up the old memory,
168 * if memory allocation fails.
169 * @mem_ranges: Memory ranges to reallocate.
171 * Returns pointer to reallocated memory on success, NULL otherwise.
173 struct crash_mem
*realloc_mem_ranges(struct crash_mem
**mem_ranges
)
175 struct crash_mem
*mem_rngs
= *mem_ranges
;
176 unsigned int nr_ranges
;
179 size
= get_mem_rngs_size(mem_rngs
);
180 nr_ranges
= mem_rngs
? mem_rngs
->nr_ranges
: 0;
182 size
+= MEM_RANGE_CHUNK_SZ
;
183 mem_rngs
= krealloc(*mem_ranges
, size
, GFP_KERNEL
);
190 mem_rngs
->nr_ranges
= nr_ranges
;
191 mem_rngs
->max_nr_ranges
= get_max_nr_ranges(size
);
192 *mem_ranges
= mem_rngs
;
198 * add_mem_range - Updates existing memory range, if there is an overlap.
199 * Else, adds a new memory range.
200 * @mem_ranges: Range list to add the memory range to.
201 * @base: Base address of the range to add.
202 * @size: Size of the memory range to add.
204 * (Re)allocates memory, if needed.
206 * Returns 0 on success, negative errno on error.
208 int add_mem_range(struct crash_mem
**mem_ranges
, u64 base
, u64 size
)
210 struct crash_mem
*mem_rngs
= *mem_ranges
;
211 u64 mstart
, mend
, end
;
217 end
= base
+ size
- 1;
219 if (!mem_rngs
|| !(mem_rngs
->nr_ranges
))
220 return __add_mem_range(mem_ranges
, base
, size
);
222 for (i
= 0; i
< mem_rngs
->nr_ranges
; i
++) {
223 mstart
= mem_rngs
->ranges
[i
].start
;
224 mend
= mem_rngs
->ranges
[i
].end
;
225 if (base
< mend
&& end
> mstart
) {
227 mem_rngs
->ranges
[i
].start
= base
;
229 mem_rngs
->ranges
[i
].end
= end
;
234 return __add_mem_range(mem_ranges
, base
, size
);
238 * add_tce_mem_ranges - Adds tce-table range to the given memory ranges list.
239 * @mem_ranges: Range list to add the memory range(s) to.
241 * Returns 0 on success, negative errno on error.
243 int add_tce_mem_ranges(struct crash_mem
**mem_ranges
)
245 struct device_node
*dn
= NULL
;
248 for_each_node_by_type(dn
, "pci") {
252 ret
= of_property_read_u64(dn
, "linux,tce-base", &base
);
253 ret
|= of_property_read_u32(dn
, "linux,tce-size", &size
);
256 * It is ok to have pci nodes without tce. So, ignore
257 * property does not exist error.
259 if (ret
== -EINVAL
) {
266 ret
= add_mem_range(mem_ranges
, base
, size
);
276 * add_initrd_mem_range - Adds initrd range to the given memory ranges list,
277 * if the initrd was retained.
278 * @mem_ranges: Range list to add the memory range to.
280 * Returns 0 on success, negative errno on error.
282 int add_initrd_mem_range(struct crash_mem
**mem_ranges
)
287 /* This range means something, only if initrd was retained */
288 if (!strstr(saved_command_line
, "retain_initrd"))
291 ret
= of_property_read_u64(of_chosen
, "linux,initrd-start", &base
);
292 ret
|= of_property_read_u64(of_chosen
, "linux,initrd-end", &end
);
294 ret
= add_mem_range(mem_ranges
, base
, end
- base
+ 1);
299 #ifdef CONFIG_PPC_BOOK3S_64
301 * add_htab_mem_range - Adds htab range to the given memory ranges list,
303 * @mem_ranges: Range list to add the memory range to.
305 * Returns 0 on success, negative errno on error.
307 int add_htab_mem_range(struct crash_mem
**mem_ranges
)
312 return add_mem_range(mem_ranges
, __pa(htab_address
), htab_size_bytes
);
317 * add_kernel_mem_range - Adds kernel text region to the given
318 * memory ranges list.
319 * @mem_ranges: Range list to add the memory range to.
321 * Returns 0 on success, negative errno on error.
323 int add_kernel_mem_range(struct crash_mem
**mem_ranges
)
325 return add_mem_range(mem_ranges
, 0, __pa(_end
));
329 * add_rtas_mem_range - Adds RTAS region to the given memory ranges list.
330 * @mem_ranges: Range list to add the memory range to.
332 * Returns 0 on success, negative errno on error.
334 int add_rtas_mem_range(struct crash_mem
**mem_ranges
)
336 struct device_node
*dn
;
340 dn
= of_find_node_by_path("/rtas");
344 ret
= of_property_read_u32(dn
, "linux,rtas-base", &base
);
345 ret
|= of_property_read_u32(dn
, "rtas-size", &size
);
347 ret
= add_mem_range(mem_ranges
, base
, size
);
354 * add_opal_mem_range - Adds OPAL region to the given memory ranges list.
355 * @mem_ranges: Range list to add the memory range to.
357 * Returns 0 on success, negative errno on error.
359 int add_opal_mem_range(struct crash_mem
**mem_ranges
)
361 struct device_node
*dn
;
365 dn
= of_find_node_by_path("/ibm,opal");
369 ret
= of_property_read_u64(dn
, "opal-base-address", &base
);
370 ret
|= of_property_read_u64(dn
, "opal-runtime-size", &size
);
372 ret
= add_mem_range(mem_ranges
, base
, size
);
379 * add_reserved_mem_ranges - Adds "/reserved-ranges" regions exported by f/w
380 * to the given memory ranges list.
381 * @mem_ranges: Range list to add the memory ranges to.
383 * Returns 0 on success, negative errno on error.
385 int add_reserved_mem_ranges(struct crash_mem
**mem_ranges
)
387 int n_mem_addr_cells
, n_mem_size_cells
, i
, len
, cells
, ret
= 0;
390 prop
= of_get_property(of_root
, "reserved-ranges", &len
);
394 n_mem_addr_cells
= of_n_addr_cells(of_root
);
395 n_mem_size_cells
= of_n_size_cells(of_root
);
396 cells
= n_mem_addr_cells
+ n_mem_size_cells
;
398 /* Each reserved range is an (address,size) pair */
399 for (i
= 0; i
< (len
/ (sizeof(u32
) * cells
)); i
++) {
402 base
= of_read_number(prop
+ (i
* cells
), n_mem_addr_cells
);
403 size
= of_read_number(prop
+ (i
* cells
) + n_mem_addr_cells
,
406 ret
= add_mem_range(mem_ranges
, base
, size
);