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