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