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