^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Functions for working with the Flattened Device Tree data format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * benh@kernel.crashing.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "OF: fdt: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/initrd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of_fdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_reserved_mem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/serial_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "of_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * of_fdt_limit_memory - limit the number of regions in the /memory node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @limit: maximum entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Adjust the flattened device tree to have at most 'limit' number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * memory entries in the /memory node. This function may be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * any time after initial_boot_param is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void __init of_fdt_limit_memory(int limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const void *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const __be32 *addr_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const __be32 *size_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int root_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int cell_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) root_offset = fdt_path_offset(initial_boot_params, "/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (root_offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) addr_prop = fdt_getprop(initial_boot_params, root_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "#address-cells", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (addr_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) nr_address_cells = fdt32_to_cpu(*addr_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) size_prop = fdt_getprop(initial_boot_params, root_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) "#size-cells", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (size_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) nr_size_cells = fdt32_to_cpu(*size_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) memory = fdt_path_offset(initial_boot_params, "/memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (memory > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) val = fdt_getprop(initial_boot_params, memory, "reg", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (len > limit*cell_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) len = limit*cell_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) pr_debug("Limiting number of entries to %d\n", limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fdt_setprop(initial_boot_params, memory, "reg", val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static bool of_fdt_device_is_available(const void *blob, unsigned long node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) const char *status = fdt_getprop(blob, node, "status", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!strcmp(status, "ok") || !strcmp(status, "okay"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static void *unflatten_dt_alloc(void **mem, unsigned long size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned long align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *mem = PTR_ALIGN(*mem, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) res = *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *mem += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void populate_properties(const void *blob,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void **mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const char *nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) bool dryrun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct property *pp, **pprev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) bool has_name = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pprev = &np->properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) for (cur = fdt_first_property_offset(blob, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) cur >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) cur = fdt_next_property_offset(blob, cur)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const __be32 *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) const char *pname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u32 sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pr_warn("Cannot locate property at 0x%x\n", cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!pname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) pr_warn("Cannot find property name at 0x%x\n", cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!strcmp(pname, "name"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) has_name = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pp = unflatten_dt_alloc(mem, sizeof(struct property),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) __alignof__(struct property));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (dryrun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* We accept flattened tree phandles either in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * ePAPR-style "phandle" properties, or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * legacy "linux,phandle" properties. If both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * appear and have different values, things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * will get weird. Don't do that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!strcmp(pname, "phandle") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) !strcmp(pname, "linux,phandle")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!np->phandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) np->phandle = be32_to_cpup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* And we process the "ibm,phandle" property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * used in pSeries dynamic device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!strcmp(pname, "ibm,phandle"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) np->phandle = be32_to_cpup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pp->name = (char *)pname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pp->length = sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) pp->value = (__be32 *)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *pprev = pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pprev = &pp->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* With version 0x10 we may not have the name property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * recreate it here from the unit name if absent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!has_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) const char *p = nodename, *ps = p, *pa = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if ((*p) == '@')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) pa = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) else if ((*p) == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ps = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (pa < ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) pa = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) len = (pa - ps) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) __alignof__(struct property));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!dryrun) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pp->name = "name";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) pp->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pp->value = pp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *pprev = pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pprev = &pp->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) memcpy(pp->value, ps, len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ((char *)pp->value)[len - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pr_debug("fixed up name for %s -> %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) nodename, (char *)pp->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (!dryrun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) *pprev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static bool populate_node(const void *blob,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) void **mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct device_node *dad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct device_node **pnp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) bool dryrun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) const char *pathp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) unsigned int l, allocl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) pathp = fdt_get_name(blob, offset, &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!pathp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *pnp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) allocl = ++l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) __alignof__(struct device_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!dryrun) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) char *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) of_node_init(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) np->full_name = fn = ((char *)np) + sizeof(*np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) memcpy(fn, pathp, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (dad != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) np->parent = dad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) np->sibling = dad->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dad->child = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) populate_properties(blob, offset, mem, np, pathp, dryrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!dryrun) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) np->name = of_get_property(np, "name", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!np->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) np->name = "<NULL>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *pnp = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void reverse_nodes(struct device_node *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct device_node *child, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* In-depth first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) child = parent->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) while (child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) reverse_nodes(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) child = child->sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* Reverse the nodes in the child list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) child = parent->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) parent->child = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) while (child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) next = child->sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) child->sibling = parent->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) parent->child = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) child = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * @blob: The parent device tree blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * @mem: Memory chunk to use for allocating device nodes and properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * @dad: Parent struct device_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @nodepp: The device_node tree created by the call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * It returns the size of unflattened device tree or error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static int unflatten_dt_nodes(const void *blob,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) void *mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct device_node *dad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct device_node **nodepp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct device_node *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int offset = 0, depth = 0, initial_depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #define FDT_MAX_DEPTH 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct device_node *nps[FDT_MAX_DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) void *base = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) bool dryrun = !base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (nodepp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) *nodepp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * We're unflattening device sub-tree if @dad is valid. There are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * possibly multiple nodes in the first level of depth. We need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * set @depth to 1 to make fdt_next_node() happy as it bails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * immediately when negative @depth is found. Otherwise, the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * nodes except the first one won't be unflattened successfully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (dad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) depth = initial_depth = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) root = dad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) nps[depth] = dad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) for (offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) offset >= 0 && depth >= initial_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) offset = fdt_next_node(blob, offset, &depth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!IS_ENABLED(CONFIG_OF_KOBJ) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) !of_fdt_device_is_available(blob, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!populate_node(blob, offset, &mem, nps[depth],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) &nps[depth+1], dryrun))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return mem - base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!dryrun && nodepp && !*nodepp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) *nodepp = nps[depth+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!dryrun && !root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) root = nps[depth+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) pr_err("Error %d processing FDT\n", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * Reverse the child list. Some drivers assumes node order matches .dts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * node order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!dryrun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) reverse_nodes(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return mem - base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * __unflatten_device_tree - create tree of device_nodes from flat blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * unflattens a device-tree, creating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * tree of struct device_node. It also fills the "name" and "type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * pointers of the nodes so the normal device-tree walking functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * can be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * @blob: The blob to expand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * @dad: Parent device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * @mynodes: The device_node tree created by the call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * @dt_alloc: An allocator that provides a virtual address to memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * for the resulting tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * @detached: if true set OF_DETACHED on @mynodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * Returns NULL on failure or the memory chunk containing the unflattened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * device tree on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) void *__unflatten_device_tree(const void *blob,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct device_node *dad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct device_node **mynodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) void *(*dt_alloc)(u64 size, u64 align),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) bool detached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) void *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) pr_debug(" -> unflatten_device_tree()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (!blob) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) pr_debug("No device tree pointer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) pr_debug("Unflattening device tree:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) pr_debug("magic: %08x\n", fdt_magic(blob));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) pr_debug("size: %08x\n", fdt_totalsize(blob));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) pr_debug("version: %08x\n", fdt_version(blob));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (fdt_check_header(blob)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) pr_err("Invalid device tree blob header\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* First pass, scan for size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) size = unflatten_dt_nodes(blob, NULL, dad, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) size = ALIGN(size, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) pr_debug(" size is %d, allocating...\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /* Allocate memory for the expanded device tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) mem = dt_alloc(size + 4, __alignof__(struct device_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) memset(mem, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) pr_debug(" unflattening %p...\n", mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /* Second pass, do actual unflattening */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) unflatten_dt_nodes(blob, mem, dad, mynodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (be32_to_cpup(mem + size) != 0xdeadbeef)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) pr_warn("End of tree marker overwritten: %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) be32_to_cpup(mem + size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (detached && mynodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) of_node_set_flag(*mynodes, OF_DETACHED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) pr_debug("unflattened tree is detached\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) pr_debug(" <- unflatten_device_tree()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static void *kernel_tree_alloc(u64 size, u64 align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static DEFINE_MUTEX(of_fdt_unflatten_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * @blob: Flat device tree blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * @dad: Parent device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * @mynodes: The device tree created by the call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * unflattens the device-tree passed by the firmware, creating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * tree of struct device_node. It also fills the "name" and "type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * pointers of the nodes so the normal device-tree walking functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * can be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * Returns NULL on failure or the memory chunk containing the unflattened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * device tree on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) void *of_fdt_unflatten_tree(const unsigned long *blob,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct device_node *dad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct device_node **mynodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) void *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) mutex_lock(&of_fdt_unflatten_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) mutex_unlock(&of_fdt_unflatten_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* Everything below here references initial_boot_params directly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int __initdata dt_root_addr_cells;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) int __initdata dt_root_size_cells;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) void *initial_boot_params __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) #ifdef CONFIG_OF_EARLY_FLATTREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static u32 of_fdt_crc32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int __init __reserved_mem_reserve_reg(unsigned long node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) const char *uname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) phys_addr_t base, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) int first = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) bool nomap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) prop = of_get_flat_dt_prop(node, "reg", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (len && len % t_len != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) uname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) while (len >= t_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) base = dt_mem_next_cell(dt_root_addr_cells, &prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) size = dt_mem_next_cell(dt_root_size_cells, &prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) uname, &base, (unsigned long)(size / SZ_1M));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) uname, &base, (unsigned long)(size / SZ_1M));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) len -= t_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (first) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) fdt_reserved_mem_save_node(node, uname, base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * in /reserved-memory matches the values supported by the current implementation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * also check if ranges property has been provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static int __init __reserved_mem_check_root(unsigned long node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) prop = of_get_flat_dt_prop(node, "ranges", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int depth, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static int found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (__reserved_mem_check_root(node) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) pr_err("Reserved memory: unsupported node format, ignoring\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /* break scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /* scan next node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) } else if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* scan next node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) } else if (found && depth < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /* scanning of /reserved-memory has been finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (!of_fdt_device_is_available(initial_boot_params, node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) err = __reserved_mem_reserve_reg(node, uname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) fdt_reserved_mem_save_node(node, uname, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /* scan next node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * early_init_fdt_scan_reserved_mem() - create reserved memory regions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * This function grabs memory from early allocator for device exclusive use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * defined in device tree structures. It should be called by arch specific code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * once the early allocator (i.e. memblock) has been fully activated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) void __init early_init_fdt_scan_reserved_mem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) u64 base, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (!initial_boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /* Process header /memreserve/ fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) for (n = 0; ; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) early_init_dt_reserve_memory_arch(base, size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) fdt_init_reserved_mem();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) void __init early_init_fdt_reserve_self(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (!initial_boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /* Reserve the dtb region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) fdt_totalsize(initial_boot_params),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * of_scan_flat_dt - scan flattened tree blob and call callback on each.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * @it: callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * @data: context data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * This function is used to scan the flattened device-tree, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * used to extract the memory information at boot before we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * unflatten the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) int __init of_scan_flat_dt(int (*it)(unsigned long node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) const char *uname, int depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) const void *blob = initial_boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) const char *pathp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) int offset, rc = 0, depth = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (!blob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) for (offset = fdt_next_node(blob, -1, &depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) offset >= 0 && depth >= 0 && !rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) offset = fdt_next_node(blob, offset, &depth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) pathp = fdt_get_name(blob, offset, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) rc = it(offset, pathp, depth, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * @it: callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * @data: context data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * This function is used to scan sub-nodes of a node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) int __init of_scan_flat_dt_subnodes(unsigned long parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int (*it)(unsigned long node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) const char *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) const void *blob = initial_boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) fdt_for_each_subnode(node, blob, parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) const char *pathp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) pathp = fdt_get_name(blob, node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) rc = it(node, pathp, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * of_get_flat_dt_subnode_by_name - get the subnode by given name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * @node: the parent node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * @uname: the name of subnode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) int __init of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return fdt_subnode_offset(initial_boot_params, node, uname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * of_get_flat_dt_root - find the root node in the flat blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) unsigned long __init of_get_flat_dt_root(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * This function can be used within scan_flattened_dt callback to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * access to properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) int *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return fdt_getprop(initial_boot_params, node, name, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * of_fdt_is_compatible - Return true if given node from the given blob has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * compat in its compatible list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * @blob: A device tree blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * @node: node to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * @compat: compatible string to compare with compatible list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * On match, returns a non-zero value with smaller values returned for more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * specific compatible values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) static int of_fdt_is_compatible(const void *blob,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) unsigned long node, const char *compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int cplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) unsigned long l, score = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) cp = fdt_getprop(blob, node, "compatible", &cplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (cp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) while (cplen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) score++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return score;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) l = strlen(cp) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) cp += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) cplen -= l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) * @node: node to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * @compat: compatible string to compare with compatible list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) return of_fdt_is_compatible(initial_boot_params, node, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * of_flat_dt_match - Return true if node matches a list of compatible values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) static int __init of_flat_dt_match(unsigned long node, const char *const *compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) unsigned int tmp, score = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (!compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) while (*compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) tmp = of_fdt_is_compatible(initial_boot_params, node, *compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) if (tmp && (score == 0 || (tmp < score)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) score = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) compat++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) return score;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) uint32_t __init of_get_flat_dt_phandle(unsigned long node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) return fdt_get_phandle(initial_boot_params, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct fdt_scan_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) int found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) const char * __init of_flat_dt_get_machine_name(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) unsigned long dt_root = of_get_flat_dt_root();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) name = of_get_flat_dt_prop(dt_root, "model", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * of_flat_dt_match_machine - Iterate match tables to find matching machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * @default_match: A machine specific ptr to return in case of no match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * @get_next_compat: callback function to return next compatible match table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * Iterate through machine match tables to find the best match for the machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * compatible string in the FDT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) const void * __init of_flat_dt_match_machine(const void *default_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) const void * (*get_next_compat)(const char * const**))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) const void *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) const void *best_data = default_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) const char *const *compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) unsigned long dt_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) unsigned int best_score = ~1, score = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) dt_root = of_get_flat_dt_root();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) while ((data = get_next_compat(&compat))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) score = of_flat_dt_match(dt_root, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (score > 0 && score < best_score) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) best_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) best_score = score;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (!best_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) const char *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) pr_err("\n unrecognized device tree list:\n[ ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) while (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) printk("'%s' ", prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) size -= strlen(prop) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) prop += strlen(prop) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) printk("]\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) return best_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) #ifdef CONFIG_BLK_DEV_INITRD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) static void __early_init_dt_declare_initrd(unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) /* ARM64 would cause a BUG to occur here when CONFIG_DEBUG_VM is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * enabled since __va() is called too early. ARM64 does make use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * of phys_initrd_start/phys_initrd_size so we can skip this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (!IS_ENABLED(CONFIG_ARM64)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) initrd_start = (unsigned long)__va(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) initrd_end = (unsigned long)__va(end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) initrd_below_start_ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * early_init_dt_check_for_initrd - Decode initrd location from flat tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * @node: reference to node containing initrd location ('chosen')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) static void __init early_init_dt_check_for_initrd(unsigned long node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) pr_debug("Looking for initrd properties... ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) start = of_read_number(prop, len/4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) end = of_read_number(prop, len/4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) __early_init_dt_declare_initrd(start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) phys_initrd_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) phys_initrd_size = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) (unsigned long long)start, (unsigned long long)end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static inline void early_init_dt_check_for_initrd(unsigned long node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) #endif /* CONFIG_BLK_DEV_INITRD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) #ifdef CONFIG_SERIAL_EARLYCON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) int __init early_init_dt_scan_chosen_stdout(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) const char *p, *q, *options = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) const struct earlycon_id **p_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) const void *fdt = initial_boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) offset = fdt_path_offset(fdt, "/chosen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) offset = fdt_path_offset(fdt, "/chosen@0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) p = fdt_getprop(fdt, offset, "stdout-path", &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (!p || !l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) q = strchrnul(p, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (*q != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) options = q + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) l = q - p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) /* Get the node specified by stdout-path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) offset = fdt_path_offset_namelen(fdt, p, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (offset < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) for (p_match = __earlycon_table; p_match < __earlycon_table_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) p_match++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) const struct earlycon_id *match = *p_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (!match->compatible[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (fdt_node_check_compatible(fdt, offset, match->compatible))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (of_setup_earlycon(match, offset, options) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * early_init_dt_scan_root - fetch the top level address and size cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) int __init early_init_dt_scan_root(unsigned long node, const char *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) int depth, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if (depth != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) dt_root_size_cells = be32_to_cpup(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) dt_root_addr_cells = be32_to_cpup(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) /* break now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) const __be32 *p = *cellp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) *cellp = p + s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) return of_read_number(p, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * early_init_dt_scan_memory - Look for and parse memory nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) int depth, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) const __be32 *reg, *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) bool hotpluggable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) /* We are scanning "memory" nodes only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (type == NULL || strcmp(type, "memory") != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (reg == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) reg = of_get_flat_dt_prop(node, "reg", &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) if (reg == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) endp = reg + (l / sizeof(__be32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) pr_debug("memory scan node %s, reg size %d,\n", uname, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) u64 base, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) base = dt_mem_next_cell(dt_root_addr_cells, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) size = dt_mem_next_cell(dt_root_size_cells, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) pr_debug(" - %llx , %llx\n", (unsigned long long)base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) (unsigned long long)size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) early_init_dt_add_memory_arch(base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (!hotpluggable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (early_init_dt_mark_hotplug_memory_arch(base, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) base, base + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) * Convert configs to something easy to use in C code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) #if defined(CONFIG_CMDLINE_FORCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) static const int overwrite_incoming_cmdline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static const int read_dt_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static const int concat_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) #elif defined(CONFIG_CMDLINE_EXTEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) static const int overwrite_incoming_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static const int read_dt_cmdline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) static const int concat_cmdline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) #else /* CMDLINE_FROM_BOOTLOADER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) static const int overwrite_incoming_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) static const int read_dt_cmdline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) static const int concat_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) #ifdef CONFIG_CMDLINE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) static const char *config_cmdline = CONFIG_CMDLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) static const char *config_cmdline = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) int depth, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) int l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) const char *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) const void *rng_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) char *cmdline = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (depth != 1 || !cmdline ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) early_init_dt_check_for_initrd(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) /* Put CONFIG_CMDLINE in if forced or if data had nothing in it to start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) if (overwrite_incoming_cmdline || !cmdline[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) strlcpy(cmdline, config_cmdline, COMMAND_LINE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) /* Retrieve command line unless forcing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) if (read_dt_cmdline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) p = of_get_flat_dt_prop(node, "bootargs", &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (p != NULL && l > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (concat_cmdline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) int cmdline_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) int copy_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) strlcat(cmdline, " ", COMMAND_LINE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) cmdline_len = strlen(cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) copy_len = COMMAND_LINE_SIZE - cmdline_len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) copy_len = min((int)l, copy_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) strncpy(cmdline + cmdline_len, p, copy_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) cmdline[cmdline_len + copy_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) strlcpy(cmdline, p, min(l, COMMAND_LINE_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) pr_debug("Command line is: %s\n", (char *)data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (rng_seed && l > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) add_bootloader_randomness(rng_seed, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) /* try to clear seed so it won't be found. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) fdt_nop_property(initial_boot_params, node, "rng-seed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) /* update CRC check value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) of_fdt_crc32 = crc32_be(~0, initial_boot_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) fdt_totalsize(initial_boot_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) /* break now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) #ifndef MIN_MEMBLOCK_ADDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) #ifndef MAX_MEMBLOCK_ADDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) const u64 phys_offset = MIN_MEMBLOCK_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) base, base + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) if (!PAGE_ALIGNED(base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) size -= PAGE_SIZE - (base & ~PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) base = PAGE_ALIGN(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) size &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) if (base > MAX_MEMBLOCK_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) base, base + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) pr_warn("Ignoring memory range 0x%llx - 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) size = MAX_MEMBLOCK_ADDR - base + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (base + size < phys_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) base, base + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) if (base < phys_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) pr_warn("Ignoring memory range 0x%llx - 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) base, phys_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) size -= phys_offset - base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) base = phys_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) memblock_add(base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) return memblock_mark_hotplug(base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) phys_addr_t size, bool nomap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) if (nomap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) * If the memory is already reserved (by another region), we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) * should not allow it to be marked nomap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) if (memblock_is_region_reserved(base, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) return memblock_mark_nomap(base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) return memblock_reserve(base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) static void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) void *ptr = memblock_alloc(size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) __func__, size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) bool __init early_init_dt_verify(void *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) if (!params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) /* check device tree validity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (fdt_check_header(params))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) /* Setup flat device-tree pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) initial_boot_params = params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) of_fdt_crc32 = crc32_be(~0, initial_boot_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) fdt_totalsize(initial_boot_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) void __init early_init_dt_scan_nodes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) /* Retrieve various information from the /chosen node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) rc = of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) pr_warn("No chosen node found, continuing without\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /* Initialize {size,address}-cells info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) of_scan_flat_dt(early_init_dt_scan_root, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) /* Setup memory, calling early_init_dt_add_memory_arch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) of_scan_flat_dt(early_init_dt_scan_memory, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) bool __init early_init_dt_scan(void *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) bool status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) status = early_init_dt_verify(params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) early_init_dt_scan_nodes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) * unflatten_device_tree - create tree of device_nodes from flat blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) * unflattens the device-tree passed by the firmware, creating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) * tree of struct device_node. It also fills the "name" and "type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * pointers of the nodes so the normal device-tree walking functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) * can be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) void __init unflatten_device_tree(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) __unflatten_device_tree(initial_boot_params, NULL, &of_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) early_init_dt_alloc_memory_arch, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) of_alias_scan(early_init_dt_alloc_memory_arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) unittest_unflatten_overlay_base();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) * Copies and unflattens the device-tree passed by the firmware, creating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) * tree of struct device_node. It also fills the "name" and "type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) * pointers of the nodes so the normal device-tree walking functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) * can be used. This should only be used when the FDT memory has not been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) * reserved such is the case when the FDT is built-in to the kernel init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) * section. If the FDT memory is reserved already then unflatten_device_tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) * should be used instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) void __init unflatten_and_copy_device_tree(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) void *dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) if (!initial_boot_params) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) pr_warn("No valid device tree found, continuing without\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) size = fdt_totalsize(initial_boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) dt = early_init_dt_alloc_memory_arch(size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) roundup_pow_of_two(FDT_V17_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) if (dt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) memcpy(dt, initial_boot_params, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) initial_boot_params = dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) unflatten_device_tree();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) char *buf, loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) memcpy(buf, initial_boot_params + off, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) static int __init of_fdt_raw_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) static struct bin_attribute of_fdt_raw_attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (!initial_boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) fdt_totalsize(initial_boot_params))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) late_initcall(of_fdt_raw_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) #endif /* CONFIG_OF_EARLY_FLATTREE */