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