Merge tag 'linux-kselftest-kunit-fixes-5.11-rc3' of git://git.kernel.org/pub/scm...
[linux/fpc-iii.git] / arch / powerpc / kexec / ranges.c
blob6b81c852feaba1759d8851d5d7dfc0ad7c6b8c62
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
26 /**
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));
39 /**
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)
48 size_t size;
50 if (!mem_rngs)
51 return 0;
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);
63 /**
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);
79 if (!mem_rngs)
80 return -ENOMEM;
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++;
88 return 0;
91 /**
92 * __merge_memory_ranges - Merges the given memory ranges list.
93 * @mem_rngs: Range list to merge.
95 * Assumes a sorted range list.
97 * Returns nothing.
99 static void __merge_memory_ranges(struct crash_mem *mem_rngs)
101 struct crash_mem_range *ranges;
102 int i, idx;
104 if (!mem_rngs)
105 return;
107 idx = 0;
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;
112 else {
113 idx++;
114 if (i == idx)
115 continue;
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)
129 return 1;
130 if (x->start < y->start)
131 return -1;
132 return 0;
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.
140 * Returns nothing.
142 void sort_memory_ranges(struct crash_mem *mem_rngs, bool merge)
144 int i;
146 if (!mem_rngs)
147 return;
149 /* Sort the ranges in-place */
150 sort(&(mem_rngs->ranges[0]), mem_rngs->nr_ranges,
151 sizeof(mem_rngs->ranges[0]), rngcmp, NULL);
153 if (merge)
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;
177 size_t size;
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);
184 if (!mem_rngs) {
185 kfree(*mem_ranges);
186 *mem_ranges = NULL;
187 return NULL;
190 mem_rngs->nr_ranges = nr_ranges;
191 mem_rngs->max_nr_ranges = get_max_nr_ranges(size);
192 *mem_ranges = mem_rngs;
194 return 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;
212 unsigned int i;
214 if (!size)
215 return 0;
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) {
226 if (base < mstart)
227 mem_rngs->ranges[i].start = base;
228 if (end > mend)
229 mem_rngs->ranges[i].end = end;
230 return 0;
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;
246 int ret = 0;
248 for_each_node_by_type(dn, "pci") {
249 u64 base;
250 u32 size;
252 ret = of_property_read_u64(dn, "linux,tce-base", &base);
253 ret |= of_property_read_u32(dn, "linux,tce-size", &size);
254 if (ret) {
256 * It is ok to have pci nodes without tce. So, ignore
257 * property does not exist error.
259 if (ret == -EINVAL) {
260 ret = 0;
261 continue;
263 break;
266 ret = add_mem_range(mem_ranges, base, size);
267 if (ret)
268 break;
271 of_node_put(dn);
272 return ret;
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)
284 u64 base, end;
285 int ret;
287 /* This range means something, only if initrd was retained */
288 if (!strstr(saved_command_line, "retain_initrd"))
289 return 0;
291 ret = of_property_read_u64(of_chosen, "linux,initrd-start", &base);
292 ret |= of_property_read_u64(of_chosen, "linux,initrd-end", &end);
293 if (!ret)
294 ret = add_mem_range(mem_ranges, base, end - base + 1);
296 return ret;
299 #ifdef CONFIG_PPC_BOOK3S_64
301 * add_htab_mem_range - Adds htab range to the given memory ranges list,
302 * if it exists
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)
309 if (!htab_address)
310 return 0;
312 return add_mem_range(mem_ranges, __pa(htab_address), htab_size_bytes);
314 #endif
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;
337 u32 base, size;
338 int ret = 0;
340 dn = of_find_node_by_path("/rtas");
341 if (!dn)
342 return 0;
344 ret = of_property_read_u32(dn, "linux,rtas-base", &base);
345 ret |= of_property_read_u32(dn, "rtas-size", &size);
346 if (!ret)
347 ret = add_mem_range(mem_ranges, base, size);
349 of_node_put(dn);
350 return ret;
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;
362 u64 base, size;
363 int ret;
365 dn = of_find_node_by_path("/ibm,opal");
366 if (!dn)
367 return 0;
369 ret = of_property_read_u64(dn, "opal-base-address", &base);
370 ret |= of_property_read_u64(dn, "opal-runtime-size", &size);
371 if (!ret)
372 ret = add_mem_range(mem_ranges, base, size);
374 of_node_put(dn);
375 return ret;
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;
388 const __be32 *prop;
390 prop = of_get_property(of_root, "reserved-ranges", &len);
391 if (!prop)
392 return 0;
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++) {
400 u64 base, size;
402 base = of_read_number(prop + (i * cells), n_mem_addr_cells);
403 size = of_read_number(prop + (i * cells) + n_mem_addr_cells,
404 n_mem_size_cells);
406 ret = add_mem_range(mem_ranges, base, size);
407 if (ret)
408 break;
411 return ret;