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