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 | |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 13 | #include <linux/initrd.h> |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 14 | #include <linux/memblock.h> |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 15 | #include <linux/module.h> |
Grant Likely | e169cfb | 2009-11-23 14:53:09 -0700 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_fdt.h> |
Jeremy Kerr | 4ef7b37 | 2010-02-14 07:13:47 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/errno.h> |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 20 | #include <linux/slab.h> |
Anton Blanchard | 109b623 | 2013-07-29 13:11:50 +1000 | [diff] [blame] | 21 | #include <linux/random.h> |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 22 | |
Fabio Estevam | c89810a | 2012-01-02 14:19:03 -0200 | [diff] [blame] | 23 | #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 24 | #ifdef CONFIG_PPC |
| 25 | #include <asm/machdep.h> |
| 26 | #endif /* CONFIG_PPC */ |
| 27 | |
Jeremy Kerr | 4ef7b37 | 2010-02-14 07:13:47 -0700 | [diff] [blame] | 28 | #include <asm/page.h> |
| 29 | |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 30 | char *of_fdt_get_string(struct boot_param_header *blob, u32 offset) |
| 31 | { |
| 32 | return ((char *)blob) + |
| 33 | be32_to_cpu(blob->off_dt_strings) + offset; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * of_fdt_get_property - Given a node in the given flat blob, return |
| 38 | * the property ptr |
| 39 | */ |
| 40 | void *of_fdt_get_property(struct boot_param_header *blob, |
| 41 | unsigned long node, const char *name, |
| 42 | unsigned long *size) |
| 43 | { |
| 44 | unsigned long p = node; |
| 45 | |
| 46 | do { |
| 47 | u32 tag = be32_to_cpup((__be32 *)p); |
| 48 | u32 sz, noff; |
| 49 | const char *nstr; |
| 50 | |
| 51 | p += 4; |
| 52 | if (tag == OF_DT_NOP) |
| 53 | continue; |
| 54 | if (tag != OF_DT_PROP) |
| 55 | return NULL; |
| 56 | |
| 57 | sz = be32_to_cpup((__be32 *)p); |
| 58 | noff = be32_to_cpup((__be32 *)(p + 4)); |
| 59 | p += 8; |
| 60 | if (be32_to_cpu(blob->version) < 0x10) |
| 61 | p = ALIGN(p, sz >= 8 ? 8 : 4); |
| 62 | |
| 63 | nstr = of_fdt_get_string(blob, noff); |
| 64 | if (nstr == NULL) { |
| 65 | pr_warning("Can't find property index name !\n"); |
| 66 | return NULL; |
| 67 | } |
| 68 | if (strcmp(name, nstr) == 0) { |
| 69 | if (size) |
| 70 | *size = sz; |
| 71 | return (void *)p; |
| 72 | } |
| 73 | p += sz; |
| 74 | p = ALIGN(p, 4); |
| 75 | } while (1); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * of_fdt_is_compatible - Return true if given node from the given blob has |
| 80 | * compat in its compatible list |
| 81 | * @blob: A device tree blob |
| 82 | * @node: node to test |
| 83 | * @compat: compatible string to compare with compatible list. |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 84 | * |
| 85 | * On match, returns a non-zero value with smaller values returned for more |
| 86 | * specific compatible values. |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 87 | */ |
| 88 | int of_fdt_is_compatible(struct boot_param_header *blob, |
| 89 | unsigned long node, const char *compat) |
| 90 | { |
| 91 | const char *cp; |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 92 | unsigned long cplen, l, score = 0; |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 93 | |
| 94 | cp = of_fdt_get_property(blob, node, "compatible", &cplen); |
| 95 | if (cp == NULL) |
| 96 | return 0; |
| 97 | while (cplen > 0) { |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 98 | score++; |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 99 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 100 | return score; |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 101 | l = strlen(cp) + 1; |
| 102 | cp += l; |
| 103 | cplen -= l; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 109 | /** |
| 110 | * of_fdt_match - Return true if node matches a list of compatible values |
| 111 | */ |
| 112 | int of_fdt_match(struct boot_param_header *blob, unsigned long node, |
Uwe Kleine-König | 7b482c8 | 2011-12-20 22:56:45 +0100 | [diff] [blame] | 113 | const char *const *compat) |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 114 | { |
| 115 | unsigned int tmp, score = 0; |
| 116 | |
| 117 | if (!compat) |
| 118 | return 0; |
| 119 | |
| 120 | while (*compat) { |
| 121 | tmp = of_fdt_is_compatible(blob, node, *compat); |
| 122 | if (tmp && (score == 0 || (tmp < score))) |
| 123 | score = tmp; |
| 124 | compat++; |
| 125 | } |
| 126 | |
| 127 | return score; |
| 128 | } |
| 129 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 130 | static void *unflatten_dt_alloc(void **mem, unsigned long size, |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 131 | unsigned long align) |
| 132 | { |
| 133 | void *res; |
| 134 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 135 | *mem = PTR_ALIGN(*mem, align); |
| 136 | res = *mem; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 137 | *mem += size; |
| 138 | |
| 139 | return res; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * unflatten_dt_node - Alloc and populate a device_node from the flat tree |
Stephen Neuendorffer | a40d6c4 | 2010-11-18 15:55:00 -0800 | [diff] [blame] | 144 | * @blob: The parent device tree blob |
Andres Salomon | a7006c9 | 2011-03-17 17:32:35 -0700 | [diff] [blame] | 145 | * @mem: Memory chunk to use for allocating device nodes and properties |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 146 | * @p: pointer to node in flat tree |
| 147 | * @dad: Parent struct device_node |
| 148 | * @allnextpp: pointer to ->allnext from last allocated device_node |
| 149 | * @fpsize: Size of the node path up at the current depth. |
| 150 | */ |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 151 | static void * unflatten_dt_node(struct boot_param_header *blob, |
| 152 | void *mem, |
| 153 | void **p, |
Stephen Neuendorffer | a40d6c4 | 2010-11-18 15:55:00 -0800 | [diff] [blame] | 154 | struct device_node *dad, |
| 155 | struct device_node ***allnextpp, |
| 156 | unsigned long fpsize) |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 157 | { |
| 158 | struct device_node *np; |
| 159 | struct property *pp, **prev_pp = NULL; |
| 160 | char *pathp; |
| 161 | u32 tag; |
| 162 | unsigned int l, allocl; |
| 163 | int has_name = 0; |
| 164 | int new_format = 0; |
| 165 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 166 | tag = be32_to_cpup(*p); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 167 | if (tag != OF_DT_BEGIN_NODE) { |
| 168 | pr_err("Weird tag at start of node: %x\n", tag); |
| 169 | return mem; |
| 170 | } |
| 171 | *p += 4; |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 172 | pathp = *p; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 173 | l = allocl = strlen(pathp) + 1; |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 174 | *p = PTR_ALIGN(*p + l, 4); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 175 | |
| 176 | /* version 0x10 has a more compact unit name here instead of the full |
| 177 | * path. we accumulate the full path size using "fpsize", we'll rebuild |
| 178 | * it later. We detect this because the first character of the name is |
| 179 | * not '/'. |
| 180 | */ |
| 181 | if ((*pathp) != '/') { |
| 182 | new_format = 1; |
| 183 | if (fpsize == 0) { |
| 184 | /* root node: special case. fpsize accounts for path |
| 185 | * plus terminating zero. root node only has '/', so |
| 186 | * fpsize should be 2, but we want to avoid the first |
| 187 | * level nodes to have two '/' so we use fpsize 1 here |
| 188 | */ |
| 189 | fpsize = 1; |
| 190 | allocl = 2; |
Catalin Marinas | 0fca5de | 2012-11-16 15:14:38 +0000 | [diff] [blame] | 191 | l = 1; |
| 192 | *pathp = '\0'; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 193 | } else { |
| 194 | /* account for '/' and path size minus terminal 0 |
| 195 | * already in 'l' |
| 196 | */ |
| 197 | fpsize += l; |
| 198 | allocl = fpsize; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl, |
| 203 | __alignof__(struct device_node)); |
| 204 | if (allnextpp) { |
Grant Likely | c22618a | 2012-11-14 22:37:12 +0000 | [diff] [blame] | 205 | char *fn; |
Grant Likely | c22618a | 2012-11-14 22:37:12 +0000 | [diff] [blame] | 206 | np->full_name = fn = ((char *)np) + sizeof(*np); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 207 | if (new_format) { |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 208 | /* rebuild full path for new format */ |
| 209 | if (dad && dad->parent) { |
| 210 | strcpy(fn, dad->full_name); |
| 211 | #ifdef DEBUG |
| 212 | if ((strlen(fn) + l + 1) != allocl) { |
| 213 | pr_debug("%s: p: %d, l: %d, a: %d\n", |
| 214 | pathp, (int)strlen(fn), |
| 215 | l, allocl); |
| 216 | } |
| 217 | #endif |
| 218 | fn += strlen(fn); |
| 219 | } |
| 220 | *(fn++) = '/'; |
Grant Likely | c22618a | 2012-11-14 22:37:12 +0000 | [diff] [blame] | 221 | } |
| 222 | memcpy(fn, pathp, l); |
| 223 | |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 224 | prev_pp = &np->properties; |
| 225 | **allnextpp = np; |
| 226 | *allnextpp = &np->allnext; |
| 227 | if (dad != NULL) { |
| 228 | np->parent = dad; |
| 229 | /* we temporarily use the next field as `last_child'*/ |
| 230 | if (dad->next == NULL) |
| 231 | dad->child = np; |
| 232 | else |
| 233 | dad->next->sibling = np; |
| 234 | dad->next = np; |
| 235 | } |
| 236 | kref_init(&np->kref); |
| 237 | } |
Andres Salomon | a7006c9 | 2011-03-17 17:32:35 -0700 | [diff] [blame] | 238 | /* process properties */ |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 239 | while (1) { |
| 240 | u32 sz, noff; |
| 241 | char *pname; |
| 242 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 243 | tag = be32_to_cpup(*p); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 244 | if (tag == OF_DT_NOP) { |
| 245 | *p += 4; |
| 246 | continue; |
| 247 | } |
| 248 | if (tag != OF_DT_PROP) |
| 249 | break; |
| 250 | *p += 4; |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 251 | sz = be32_to_cpup(*p); |
| 252 | noff = be32_to_cpup(*p + 4); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 253 | *p += 8; |
Stephen Neuendorffer | a40d6c4 | 2010-11-18 15:55:00 -0800 | [diff] [blame] | 254 | if (be32_to_cpu(blob->version) < 0x10) |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 255 | *p = PTR_ALIGN(*p, sz >= 8 ? 8 : 4); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 256 | |
Stephen Neuendorffer | a40d6c4 | 2010-11-18 15:55:00 -0800 | [diff] [blame] | 257 | pname = of_fdt_get_string(blob, noff); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 258 | if (pname == NULL) { |
| 259 | pr_info("Can't find property name in list !\n"); |
| 260 | break; |
| 261 | } |
| 262 | if (strcmp(pname, "name") == 0) |
| 263 | has_name = 1; |
| 264 | l = strlen(pname) + 1; |
| 265 | pp = unflatten_dt_alloc(&mem, sizeof(struct property), |
| 266 | __alignof__(struct property)); |
| 267 | if (allnextpp) { |
David Gibson | 04b954a | 2010-02-01 21:34:15 -0700 | [diff] [blame] | 268 | /* We accept flattened tree phandles either in |
| 269 | * ePAPR-style "phandle" properties, or the |
| 270 | * legacy "linux,phandle" properties. If both |
| 271 | * appear and have different values, things |
| 272 | * will get weird. Don't do that. */ |
| 273 | if ((strcmp(pname, "phandle") == 0) || |
| 274 | (strcmp(pname, "linux,phandle") == 0)) { |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 275 | if (np->phandle == 0) |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 276 | np->phandle = be32_to_cpup((__be32*)*p); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 277 | } |
David Gibson | 04b954a | 2010-02-01 21:34:15 -0700 | [diff] [blame] | 278 | /* And we process the "ibm,phandle" property |
| 279 | * used in pSeries dynamic device tree |
| 280 | * stuff */ |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 281 | if (strcmp(pname, "ibm,phandle") == 0) |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 282 | np->phandle = be32_to_cpup((__be32 *)*p); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 283 | pp->name = pname; |
| 284 | pp->length = sz; |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 285 | pp->value = *p; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 286 | *prev_pp = pp; |
| 287 | prev_pp = &pp->next; |
| 288 | } |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 289 | *p = PTR_ALIGN((*p) + sz, 4); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 290 | } |
| 291 | /* with version 0x10 we may not have the name property, recreate |
| 292 | * it here from the unit name if absent |
| 293 | */ |
| 294 | if (!has_name) { |
| 295 | char *p1 = pathp, *ps = pathp, *pa = NULL; |
| 296 | int sz; |
| 297 | |
| 298 | while (*p1) { |
| 299 | if ((*p1) == '@') |
| 300 | pa = p1; |
| 301 | if ((*p1) == '/') |
| 302 | ps = p1 + 1; |
| 303 | p1++; |
| 304 | } |
| 305 | if (pa < ps) |
| 306 | pa = p1; |
| 307 | sz = (pa - ps) + 1; |
| 308 | pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz, |
| 309 | __alignof__(struct property)); |
| 310 | if (allnextpp) { |
| 311 | pp->name = "name"; |
| 312 | pp->length = sz; |
| 313 | pp->value = pp + 1; |
| 314 | *prev_pp = pp; |
| 315 | prev_pp = &pp->next; |
| 316 | memcpy(pp->value, ps, sz - 1); |
| 317 | ((char *)pp->value)[sz - 1] = 0; |
| 318 | pr_debug("fixed up name for %s -> %s\n", pathp, |
| 319 | (char *)pp->value); |
| 320 | } |
| 321 | } |
| 322 | if (allnextpp) { |
| 323 | *prev_pp = NULL; |
| 324 | np->name = of_get_property(np, "name", NULL); |
| 325 | np->type = of_get_property(np, "device_type", NULL); |
| 326 | |
| 327 | if (!np->name) |
| 328 | np->name = "<NULL>"; |
| 329 | if (!np->type) |
| 330 | np->type = "<NULL>"; |
| 331 | } |
Jason Gunthorpe | 7f809e1 | 2010-03-26 22:09:56 -0600 | [diff] [blame] | 332 | while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) { |
| 333 | if (tag == OF_DT_NOP) |
| 334 | *p += 4; |
| 335 | else |
Stephen Neuendorffer | a40d6c4 | 2010-11-18 15:55:00 -0800 | [diff] [blame] | 336 | mem = unflatten_dt_node(blob, mem, p, np, allnextpp, |
| 337 | fpsize); |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 338 | tag = be32_to_cpup(*p); |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 339 | } |
| 340 | if (tag != OF_DT_END_NODE) { |
| 341 | pr_err("Weird tag at end of node: %x\n", tag); |
| 342 | return mem; |
| 343 | } |
| 344 | *p += 4; |
| 345 | return mem; |
| 346 | } |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 347 | |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 348 | /** |
| 349 | * __unflatten_device_tree - create tree of device_nodes from flat blob |
| 350 | * |
| 351 | * unflattens a device-tree, creating the |
| 352 | * tree of struct device_node. It also fills the "name" and "type" |
| 353 | * pointers of the nodes so the normal device-tree walking functions |
| 354 | * can be used. |
| 355 | * @blob: The blob to expand |
| 356 | * @mynodes: The device_node tree created by the call |
| 357 | * @dt_alloc: An allocator that provides a virtual address to memory |
| 358 | * for the resulting tree |
| 359 | */ |
Andres Salomon | a7006c9 | 2011-03-17 17:32:35 -0700 | [diff] [blame] | 360 | static void __unflatten_device_tree(struct boot_param_header *blob, |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 361 | struct device_node **mynodes, |
| 362 | void * (*dt_alloc)(u64 size, u64 align)) |
| 363 | { |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 364 | unsigned long size; |
| 365 | void *start, *mem; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 366 | struct device_node **allnextp = mynodes; |
| 367 | |
| 368 | pr_debug(" -> unflatten_device_tree()\n"); |
| 369 | |
| 370 | if (!blob) { |
| 371 | pr_debug("No device tree pointer\n"); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | pr_debug("Unflattening device tree:\n"); |
| 376 | pr_debug("magic: %08x\n", be32_to_cpu(blob->magic)); |
| 377 | pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize)); |
| 378 | pr_debug("version: %08x\n", be32_to_cpu(blob->version)); |
| 379 | |
| 380 | if (be32_to_cpu(blob->magic) != OF_DT_HEADER) { |
| 381 | pr_err("Invalid device tree blob header\n"); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | /* First pass, scan for size */ |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 386 | start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct); |
| 387 | size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0); |
| 388 | size = ALIGN(size, 4); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 389 | |
| 390 | pr_debug(" size is %lx, allocating...\n", size); |
| 391 | |
| 392 | /* Allocate memory for the expanded device tree */ |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 393 | mem = dt_alloc(size + 4, __alignof__(struct device_node)); |
| 394 | memset(mem, 0, size); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 395 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 396 | *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef); |
Wladislav Wiebe | 9e40127 | 2013-08-12 13:06:53 +0200 | [diff] [blame] | 397 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 398 | pr_debug(" unflattening %p...\n", mem); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 399 | |
| 400 | /* Second pass, do actual unflattening */ |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 401 | start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 402 | unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0); |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 403 | if (be32_to_cpup(start) != OF_DT_END) |
| 404 | pr_warning("Weird tag at end of tree: %08x\n", be32_to_cpup(start)); |
| 405 | if (be32_to_cpup(mem + size) != 0xdeadbeef) |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 406 | pr_warning("End of tree marker overwritten: %08x\n", |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 407 | be32_to_cpup(mem + size)); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 408 | *allnextp = NULL; |
| 409 | |
| 410 | pr_debug(" <- unflatten_device_tree()\n"); |
| 411 | } |
| 412 | |
| 413 | static void *kernel_tree_alloc(u64 size, u64 align) |
| 414 | { |
| 415 | return kzalloc(size, GFP_KERNEL); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * of_fdt_unflatten_tree - create tree of device_nodes from flat blob |
| 420 | * |
| 421 | * unflattens the device-tree passed by the firmware, creating the |
| 422 | * tree of struct device_node. It also fills the "name" and "type" |
| 423 | * pointers of the nodes so the normal device-tree walking functions |
| 424 | * can be used. |
| 425 | */ |
| 426 | void of_fdt_unflatten_tree(unsigned long *blob, |
| 427 | struct device_node **mynodes) |
| 428 | { |
| 429 | struct boot_param_header *device_tree = |
| 430 | (struct boot_param_header *)blob; |
| 431 | __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc); |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree); |
| 434 | |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 435 | /* Everything below here references initial_boot_params directly. */ |
| 436 | int __initdata dt_root_addr_cells; |
| 437 | int __initdata dt_root_size_cells; |
| 438 | |
| 439 | struct boot_param_header *initial_boot_params; |
| 440 | |
| 441 | #ifdef CONFIG_OF_EARLY_FLATTREE |
| 442 | |
| 443 | /** |
| 444 | * of_scan_flat_dt - scan flattened tree blob and call callback on each. |
| 445 | * @it: callback function |
| 446 | * @data: context data pointer |
| 447 | * |
| 448 | * This function is used to scan the flattened device-tree, it is |
| 449 | * used to extract the memory information at boot before we can |
| 450 | * unflatten the tree |
| 451 | */ |
| 452 | int __init of_scan_flat_dt(int (*it)(unsigned long node, |
| 453 | const char *uname, int depth, |
| 454 | void *data), |
| 455 | void *data) |
| 456 | { |
| 457 | unsigned long p = ((unsigned long)initial_boot_params) + |
| 458 | be32_to_cpu(initial_boot_params->off_dt_struct); |
| 459 | int rc = 0; |
| 460 | int depth = -1; |
| 461 | |
| 462 | do { |
| 463 | u32 tag = be32_to_cpup((__be32 *)p); |
Fabio Estevam | e55b082 | 2012-11-12 18:30:49 -0200 | [diff] [blame] | 464 | const char *pathp; |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 465 | |
| 466 | p += 4; |
| 467 | if (tag == OF_DT_END_NODE) { |
| 468 | depth--; |
| 469 | continue; |
| 470 | } |
| 471 | if (tag == OF_DT_NOP) |
| 472 | continue; |
| 473 | if (tag == OF_DT_END) |
| 474 | break; |
| 475 | if (tag == OF_DT_PROP) { |
| 476 | u32 sz = be32_to_cpup((__be32 *)p); |
| 477 | p += 8; |
| 478 | if (be32_to_cpu(initial_boot_params->version) < 0x10) |
| 479 | p = ALIGN(p, sz >= 8 ? 8 : 4); |
| 480 | p += sz; |
| 481 | p = ALIGN(p, 4); |
| 482 | continue; |
| 483 | } |
| 484 | if (tag != OF_DT_BEGIN_NODE) { |
| 485 | pr_err("Invalid tag %x in flat device tree!\n", tag); |
| 486 | return -EINVAL; |
| 487 | } |
| 488 | depth++; |
| 489 | pathp = (char *)p; |
| 490 | p = ALIGN(p + strlen(pathp) + 1, 4); |
Andy Shevchenko | 375da3a | 2012-12-17 16:01:28 -0800 | [diff] [blame] | 491 | if (*pathp == '/') |
| 492 | pathp = kbasename(pathp); |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 493 | rc = it(p, pathp, depth, data); |
| 494 | if (rc != 0) |
| 495 | break; |
| 496 | } while (1); |
| 497 | |
| 498 | return rc; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * of_get_flat_dt_root - find the root node in the flat blob |
| 503 | */ |
| 504 | unsigned long __init of_get_flat_dt_root(void) |
| 505 | { |
| 506 | unsigned long p = ((unsigned long)initial_boot_params) + |
| 507 | be32_to_cpu(initial_boot_params->off_dt_struct); |
| 508 | |
| 509 | while (be32_to_cpup((__be32 *)p) == OF_DT_NOP) |
| 510 | p += 4; |
| 511 | BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE); |
| 512 | p += 4; |
| 513 | return ALIGN(p + strlen((char *)p) + 1, 4); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr |
| 518 | * |
| 519 | * This function can be used within scan_flattened_dt callback to get |
| 520 | * access to properties |
| 521 | */ |
| 522 | void *__init of_get_flat_dt_prop(unsigned long node, const char *name, |
| 523 | unsigned long *size) |
| 524 | { |
| 525 | return of_fdt_get_property(initial_boot_params, node, name, size); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * of_flat_dt_is_compatible - Return true if given node has compat in compatible list |
| 530 | * @node: node to test |
| 531 | * @compat: compatible string to compare with compatible list. |
| 532 | */ |
| 533 | int __init of_flat_dt_is_compatible(unsigned long node, const char *compat) |
| 534 | { |
| 535 | return of_fdt_is_compatible(initial_boot_params, node, compat); |
| 536 | } |
| 537 | |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 538 | /** |
| 539 | * of_flat_dt_match - Return true if node matches a list of compatible values |
| 540 | */ |
Uwe Kleine-König | 7b482c8 | 2011-12-20 22:56:45 +0100 | [diff] [blame] | 541 | 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] | 542 | { |
| 543 | return of_fdt_match(initial_boot_params, node, compat); |
| 544 | } |
| 545 | |
Marek Szyprowski | 57d74bc | 2013-08-26 14:41:56 +0200 | [diff] [blame] | 546 | struct fdt_scan_status { |
| 547 | const char *name; |
| 548 | int namelen; |
| 549 | int depth; |
| 550 | int found; |
| 551 | int (*iterator)(unsigned long node, const char *uname, int depth, void *data); |
| 552 | void *data; |
| 553 | }; |
| 554 | |
| 555 | /** |
| 556 | * fdt_scan_node_by_path - iterator for of_scan_flat_dt_by_path function |
| 557 | */ |
| 558 | static int __init fdt_scan_node_by_path(unsigned long node, const char *uname, |
| 559 | int depth, void *data) |
| 560 | { |
| 561 | struct fdt_scan_status *st = data; |
| 562 | |
| 563 | /* |
| 564 | * if scan at the requested fdt node has been completed, |
| 565 | * return -ENXIO to abort further scanning |
| 566 | */ |
| 567 | if (depth <= st->depth) |
| 568 | return -ENXIO; |
| 569 | |
| 570 | /* requested fdt node has been found, so call iterator function */ |
| 571 | if (st->found) |
| 572 | return st->iterator(node, uname, depth, st->data); |
| 573 | |
| 574 | /* check if scanning automata is entering next level of fdt nodes */ |
| 575 | if (depth == st->depth + 1 && |
| 576 | strncmp(st->name, uname, st->namelen) == 0 && |
| 577 | uname[st->namelen] == 0) { |
| 578 | st->depth += 1; |
| 579 | if (st->name[st->namelen] == 0) { |
| 580 | st->found = 1; |
| 581 | } else { |
| 582 | const char *next = st->name + st->namelen + 1; |
| 583 | st->name = next; |
| 584 | st->namelen = strcspn(next, "/"); |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /* scan next fdt node */ |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * of_scan_flat_dt_by_path - scan flattened tree blob and call callback on each |
| 595 | * child of the given path. |
| 596 | * @path: path to start searching for children |
| 597 | * @it: callback function |
| 598 | * @data: context data pointer |
| 599 | * |
| 600 | * This function is used to scan the flattened device-tree starting from the |
| 601 | * node given by path. It is used to extract information (like reserved |
| 602 | * memory), which is required on ealy boot before we can unflatten the tree. |
| 603 | */ |
| 604 | int __init of_scan_flat_dt_by_path(const char *path, |
| 605 | int (*it)(unsigned long node, const char *name, int depth, void *data), |
| 606 | void *data) |
| 607 | { |
| 608 | struct fdt_scan_status st = {path, 0, -1, 0, it, data}; |
| 609 | int ret = 0; |
| 610 | |
| 611 | if (initial_boot_params) |
| 612 | ret = of_scan_flat_dt(fdt_scan_node_by_path, &st); |
| 613 | |
| 614 | if (!st.found) |
| 615 | return -ENOENT; |
| 616 | else if (ret == -ENXIO) /* scan has been completed */ |
| 617 | return 0; |
| 618 | else |
| 619 | return ret; |
| 620 | } |
| 621 | |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 622 | #ifdef CONFIG_BLK_DEV_INITRD |
| 623 | /** |
| 624 | * early_init_dt_check_for_initrd - Decode initrd location from flat tree |
| 625 | * @node: reference to node containing initrd location ('chosen') |
| 626 | */ |
| 627 | void __init early_init_dt_check_for_initrd(unsigned long node) |
| 628 | { |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 629 | u64 start, end; |
| 630 | unsigned long len; |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 631 | __be32 *prop; |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 632 | |
| 633 | pr_debug("Looking for initrd properties... "); |
| 634 | |
| 635 | prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len); |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 636 | if (!prop) |
| 637 | return; |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 638 | start = of_read_number(prop, len/4); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 639 | |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 640 | prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len); |
| 641 | if (!prop) |
| 642 | return; |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 643 | end = of_read_number(prop, len/4); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 644 | |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 645 | early_init_dt_setup_initrd_arch(start, end); |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 646 | pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n", |
| 647 | (unsigned long long)start, (unsigned long long)end); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 648 | } |
| 649 | #else |
| 650 | inline void early_init_dt_check_for_initrd(unsigned long node) |
| 651 | { |
| 652 | } |
| 653 | #endif /* CONFIG_BLK_DEV_INITRD */ |
| 654 | |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 655 | /** |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 656 | * early_init_dt_scan_root - fetch the top level address and size cells |
| 657 | */ |
| 658 | int __init early_init_dt_scan_root(unsigned long node, const char *uname, |
| 659 | int depth, void *data) |
| 660 | { |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 661 | __be32 *prop; |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 662 | |
| 663 | if (depth != 0) |
| 664 | return 0; |
| 665 | |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 666 | dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 667 | dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 668 | |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 669 | prop = of_get_flat_dt_prop(node, "#size-cells", NULL); |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 670 | if (prop) |
| 671 | dt_root_size_cells = be32_to_cpup(prop); |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 672 | pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells); |
| 673 | |
| 674 | prop = of_get_flat_dt_prop(node, "#address-cells", NULL); |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 675 | if (prop) |
| 676 | dt_root_addr_cells = be32_to_cpup(prop); |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 677 | pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells); |
| 678 | |
| 679 | /* break now */ |
| 680 | return 1; |
| 681 | } |
| 682 | |
Jeremy Kerr | 2e89e68 | 2010-01-30 01:41:49 -0700 | [diff] [blame] | 683 | u64 __init dt_mem_next_cell(int s, __be32 **cellp) |
Grant Likely | 83f7a06 | 2009-11-24 03:37:56 -0700 | [diff] [blame] | 684 | { |
Jeremy Kerr | 2e89e68 | 2010-01-30 01:41:49 -0700 | [diff] [blame] | 685 | __be32 *p = *cellp; |
Grant Likely | 83f7a06 | 2009-11-24 03:37:56 -0700 | [diff] [blame] | 686 | |
| 687 | *cellp = p + s; |
| 688 | return of_read_number(p, s); |
| 689 | } |
| 690 | |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 691 | /** |
| 692 | * early_init_dt_scan_memory - Look for an parse memory nodes |
| 693 | */ |
| 694 | int __init early_init_dt_scan_memory(unsigned long node, const char *uname, |
| 695 | int depth, void *data) |
| 696 | { |
| 697 | char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 698 | __be32 *reg, *endp; |
| 699 | unsigned long l; |
| 700 | |
| 701 | /* We are scanning "memory" nodes only */ |
| 702 | if (type == NULL) { |
| 703 | /* |
| 704 | * The longtrail doesn't have a device_type on the |
| 705 | * /memory node, so look for the node called /memory@0. |
| 706 | */ |
| 707 | if (depth != 1 || strcmp(uname, "memory@0") != 0) |
| 708 | return 0; |
| 709 | } else if (strcmp(type, "memory") != 0) |
| 710 | return 0; |
| 711 | |
| 712 | reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l); |
| 713 | if (reg == NULL) |
| 714 | reg = of_get_flat_dt_prop(node, "reg", &l); |
| 715 | if (reg == NULL) |
| 716 | return 0; |
| 717 | |
| 718 | endp = reg + (l / sizeof(__be32)); |
| 719 | |
| 720 | pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n", |
| 721 | uname, l, reg[0], reg[1], reg[2], reg[3]); |
| 722 | |
| 723 | while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { |
| 724 | u64 base, size; |
| 725 | |
| 726 | base = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 727 | size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 728 | |
| 729 | if (size == 0) |
| 730 | continue; |
| 731 | pr_debug(" - %llx , %llx\n", (unsigned long long)base, |
| 732 | (unsigned long long)size); |
| 733 | |
| 734 | early_init_dt_add_memory_arch(base, size); |
| 735 | } |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 740 | int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, |
| 741 | int depth, void *data) |
| 742 | { |
| 743 | unsigned long l; |
| 744 | char *p; |
| 745 | |
| 746 | pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
| 747 | |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 748 | if (depth != 1 || !data || |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 749 | (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) |
| 750 | return 0; |
| 751 | |
| 752 | early_init_dt_check_for_initrd(node); |
| 753 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 754 | /* Retrieve command line */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 755 | p = of_get_flat_dt_prop(node, "bootargs", &l); |
| 756 | if (p != NULL && l > 0) |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 757 | strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE)); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 758 | |
Benjamin Herrenschmidt | 78b782cb | 2011-09-19 18:50:15 +0000 | [diff] [blame] | 759 | /* |
| 760 | * CONFIG_CMDLINE is meant to be a default in case nothing else |
| 761 | * managed to set the command line, unless CONFIG_CMDLINE_FORCE |
| 762 | * is set in which case we override whatever was found earlier. |
| 763 | */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 764 | #ifdef CONFIG_CMDLINE |
| 765 | #ifndef CONFIG_CMDLINE_FORCE |
Benjamin Herrenschmidt | 78b782cb | 2011-09-19 18:50:15 +0000 | [diff] [blame] | 766 | if (!((char *)data)[0]) |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 767 | #endif |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 768 | strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 769 | #endif /* CONFIG_CMDLINE */ |
| 770 | |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 771 | pr_debug("Command line is: %s\n", (char*)data); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 772 | |
| 773 | /* break now */ |
| 774 | return 1; |
| 775 | } |
| 776 | |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 777 | #ifdef CONFIG_HAVE_MEMBLOCK |
| 778 | /* |
| 779 | * called from unflatten_device_tree() to bootstrap devicetree itself |
| 780 | * Architectures can override this definition if memblock isn't used |
| 781 | */ |
| 782 | void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align) |
| 783 | { |
| 784 | return __va(memblock_alloc(size, align)); |
| 785 | } |
| 786 | #endif |
| 787 | |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 788 | /** |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 789 | * unflatten_device_tree - create tree of device_nodes from flat blob |
| 790 | * |
| 791 | * unflattens the device-tree passed by the firmware, creating the |
| 792 | * tree of struct device_node. It also fills the "name" and "type" |
| 793 | * pointers of the nodes so the normal device-tree walking functions |
| 794 | * can be used. |
| 795 | */ |
| 796 | void __init unflatten_device_tree(void) |
| 797 | { |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 798 | __unflatten_device_tree(initial_boot_params, &of_allnodes, |
Grant Likely | 672c544 | 2011-01-13 15:36:09 -0700 | [diff] [blame] | 799 | early_init_dt_alloc_memory_arch); |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 800 | |
Robert P. J. Day | 4c7d636 | 2013-05-30 05:38:08 -0400 | [diff] [blame] | 801 | /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */ |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 802 | of_alias_scan(early_init_dt_alloc_memory_arch); |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 803 | } |
Stephen Neuendorffer | e6ce132 | 2010-11-18 15:54:56 -0800 | [diff] [blame] | 804 | |
Rob Herring | a8bf752 | 2013-08-26 11:22:45 -0500 | [diff] [blame^] | 805 | /** |
| 806 | * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob |
| 807 | * |
| 808 | * Copies and unflattens the device-tree passed by the firmware, creating the |
| 809 | * tree of struct device_node. It also fills the "name" and "type" |
| 810 | * pointers of the nodes so the normal device-tree walking functions |
| 811 | * can be used. This should only be used when the FDT memory has not been |
| 812 | * reserved such is the case when the FDT is built-in to the kernel init |
| 813 | * section. If the FDT memory is reserved already then unflatten_device_tree |
| 814 | * should be used instead. |
| 815 | */ |
| 816 | void __init unflatten_and_copy_device_tree(void) |
| 817 | { |
| 818 | int size = __be32_to_cpu(initial_boot_params->totalsize); |
| 819 | void *dt = early_init_dt_alloc_memory_arch(size, |
| 820 | __alignof__(struct boot_param_header)); |
| 821 | |
| 822 | if (dt) { |
| 823 | memcpy(dt, initial_boot_params, size); |
| 824 | initial_boot_params = dt; |
| 825 | } |
| 826 | unflatten_device_tree(); |
| 827 | } |
| 828 | |
Stephen Neuendorffer | e6ce132 | 2010-11-18 15:54:56 -0800 | [diff] [blame] | 829 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |
Anton Blanchard | 109b623 | 2013-07-29 13:11:50 +1000 | [diff] [blame] | 830 | |
| 831 | /* Feed entire flattened device tree into the random pool */ |
| 832 | static int __init add_fdt_randomness(void) |
| 833 | { |
| 834 | if (initial_boot_params) |
| 835 | add_device_randomness(initial_boot_params, |
| 836 | be32_to_cpu(initial_boot_params->totalsize)); |
| 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | core_initcall(add_fdt_randomness); |