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; |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 327 | size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0); |
| 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 | |
| 375 | struct boot_param_header *initial_boot_params; |
| 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 | /** |
| 557 | * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr |
| 558 | * |
| 559 | * This function can be used within scan_flattened_dt callback to get |
| 560 | * access to properties |
| 561 | */ |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 562 | const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, |
| 563 | int *size) |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 564 | { |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 565 | return fdt_getprop(initial_boot_params, node, name, size); |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
| 569 | * of_flat_dt_is_compatible - Return true if given node has compat in compatible list |
| 570 | * @node: node to test |
| 571 | * @compat: compatible string to compare with compatible list. |
| 572 | */ |
| 573 | int __init of_flat_dt_is_compatible(unsigned long node, const char *compat) |
| 574 | { |
| 575 | return of_fdt_is_compatible(initial_boot_params, node, compat); |
| 576 | } |
| 577 | |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 578 | /** |
| 579 | * of_flat_dt_match - Return true if node matches a list of compatible values |
| 580 | */ |
Uwe Kleine-König | 7b482c8 | 2011-12-20 22:56:45 +0100 | [diff] [blame] | 581 | 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] | 582 | { |
| 583 | return of_fdt_match(initial_boot_params, node, compat); |
| 584 | } |
| 585 | |
Marek Szyprowski | 57d74bc | 2013-08-26 14:41:56 +0200 | [diff] [blame] | 586 | struct fdt_scan_status { |
| 587 | const char *name; |
| 588 | int namelen; |
| 589 | int depth; |
| 590 | int found; |
| 591 | int (*iterator)(unsigned long node, const char *uname, int depth, void *data); |
| 592 | void *data; |
| 593 | }; |
| 594 | |
Rob Herring | 6a903a2 | 2013-08-27 21:41:56 -0500 | [diff] [blame] | 595 | const char * __init of_flat_dt_get_machine_name(void) |
| 596 | { |
| 597 | const char *name; |
| 598 | unsigned long dt_root = of_get_flat_dt_root(); |
| 599 | |
| 600 | name = of_get_flat_dt_prop(dt_root, "model", NULL); |
| 601 | if (!name) |
| 602 | name = of_get_flat_dt_prop(dt_root, "compatible", NULL); |
| 603 | return name; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * of_flat_dt_match_machine - Iterate match tables to find matching machine. |
| 608 | * |
| 609 | * @default_match: A machine specific ptr to return in case of no match. |
| 610 | * @get_next_compat: callback function to return next compatible match table. |
| 611 | * |
| 612 | * Iterate through machine match tables to find the best match for the machine |
| 613 | * compatible string in the FDT. |
| 614 | */ |
| 615 | const void * __init of_flat_dt_match_machine(const void *default_match, |
| 616 | const void * (*get_next_compat)(const char * const**)) |
| 617 | { |
| 618 | const void *data = NULL; |
| 619 | const void *best_data = default_match; |
| 620 | const char *const *compat; |
| 621 | unsigned long dt_root; |
| 622 | unsigned int best_score = ~1, score = 0; |
| 623 | |
| 624 | dt_root = of_get_flat_dt_root(); |
| 625 | while ((data = get_next_compat(&compat))) { |
| 626 | score = of_flat_dt_match(dt_root, compat); |
| 627 | if (score > 0 && score < best_score) { |
| 628 | best_data = data; |
| 629 | best_score = score; |
| 630 | } |
| 631 | } |
| 632 | if (!best_data) { |
| 633 | const char *prop; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 634 | int size; |
Rob Herring | 6a903a2 | 2013-08-27 21:41:56 -0500 | [diff] [blame] | 635 | |
| 636 | pr_err("\n unrecognized device tree list:\n[ "); |
| 637 | |
| 638 | prop = of_get_flat_dt_prop(dt_root, "compatible", &size); |
| 639 | if (prop) { |
| 640 | while (size > 0) { |
| 641 | printk("'%s' ", prop); |
| 642 | size -= strlen(prop) + 1; |
| 643 | prop += strlen(prop) + 1; |
| 644 | } |
| 645 | } |
| 646 | printk("]\n\n"); |
| 647 | return NULL; |
| 648 | } |
| 649 | |
| 650 | pr_info("Machine model: %s\n", of_flat_dt_get_machine_name()); |
| 651 | |
| 652 | return best_data; |
| 653 | } |
| 654 | |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 655 | #ifdef CONFIG_BLK_DEV_INITRD |
| 656 | /** |
| 657 | * early_init_dt_check_for_initrd - Decode initrd location from flat tree |
| 658 | * @node: reference to node containing initrd location ('chosen') |
| 659 | */ |
Rob Herring | 29eb45a | 2013-08-30 17:06:53 -0500 | [diff] [blame] | 660 | static void __init early_init_dt_check_for_initrd(unsigned long node) |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 661 | { |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 662 | u64 start, end; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 663 | int len; |
| 664 | const __be32 *prop; |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 665 | |
| 666 | pr_debug("Looking for initrd properties... "); |
| 667 | |
| 668 | prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len); |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 669 | if (!prop) |
| 670 | return; |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 671 | start = of_read_number(prop, len/4); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 672 | |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 673 | prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len); |
| 674 | if (!prop) |
| 675 | return; |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 676 | end = of_read_number(prop, len/4); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 677 | |
Rob Herring | 29eb45a | 2013-08-30 17:06:53 -0500 | [diff] [blame] | 678 | initrd_start = (unsigned long)__va(start); |
| 679 | initrd_end = (unsigned long)__va(end); |
| 680 | initrd_below_start_ok = 1; |
| 681 | |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 682 | pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n", |
| 683 | (unsigned long long)start, (unsigned long long)end); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 684 | } |
| 685 | #else |
Rob Herring | 29eb45a | 2013-08-30 17:06:53 -0500 | [diff] [blame] | 686 | static inline void early_init_dt_check_for_initrd(unsigned long node) |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 687 | { |
| 688 | } |
| 689 | #endif /* CONFIG_BLK_DEV_INITRD */ |
| 690 | |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 691 | /** |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 692 | * early_init_dt_scan_root - fetch the top level address and size cells |
| 693 | */ |
| 694 | int __init early_init_dt_scan_root(unsigned long node, const char *uname, |
| 695 | int depth, void *data) |
| 696 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 697 | const __be32 *prop; |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 698 | |
| 699 | if (depth != 0) |
| 700 | return 0; |
| 701 | |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 702 | dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 703 | dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 704 | |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 705 | prop = of_get_flat_dt_prop(node, "#size-cells", NULL); |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 706 | if (prop) |
| 707 | dt_root_size_cells = be32_to_cpup(prop); |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 708 | pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells); |
| 709 | |
| 710 | prop = of_get_flat_dt_prop(node, "#address-cells", NULL); |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 711 | if (prop) |
| 712 | dt_root_addr_cells = be32_to_cpup(prop); |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 713 | pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells); |
| 714 | |
| 715 | /* break now */ |
| 716 | return 1; |
| 717 | } |
| 718 | |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 719 | u64 __init dt_mem_next_cell(int s, const __be32 **cellp) |
Grant Likely | 83f7a06 | 2009-11-24 03:37:56 -0700 | [diff] [blame] | 720 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 721 | const __be32 *p = *cellp; |
Grant Likely | 83f7a06 | 2009-11-24 03:37:56 -0700 | [diff] [blame] | 722 | |
| 723 | *cellp = p + s; |
| 724 | return of_read_number(p, s); |
| 725 | } |
| 726 | |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 727 | /** |
| 728 | * early_init_dt_scan_memory - Look for an parse memory nodes |
| 729 | */ |
| 730 | int __init early_init_dt_scan_memory(unsigned long node, const char *uname, |
| 731 | int depth, void *data) |
| 732 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 733 | const char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 734 | const __be32 *reg, *endp; |
| 735 | int l; |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 736 | |
| 737 | /* We are scanning "memory" nodes only */ |
| 738 | if (type == NULL) { |
| 739 | /* |
| 740 | * The longtrail doesn't have a device_type on the |
| 741 | * /memory node, so look for the node called /memory@0. |
| 742 | */ |
| 743 | if (depth != 1 || strcmp(uname, "memory@0") != 0) |
| 744 | return 0; |
| 745 | } else if (strcmp(type, "memory") != 0) |
| 746 | return 0; |
| 747 | |
| 748 | reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l); |
| 749 | if (reg == NULL) |
| 750 | reg = of_get_flat_dt_prop(node, "reg", &l); |
| 751 | if (reg == NULL) |
| 752 | return 0; |
| 753 | |
| 754 | endp = reg + (l / sizeof(__be32)); |
| 755 | |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 756 | 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] | 757 | uname, l, reg[0], reg[1], reg[2], reg[3]); |
| 758 | |
| 759 | while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { |
| 760 | u64 base, size; |
| 761 | |
| 762 | base = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 763 | size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 764 | |
| 765 | if (size == 0) |
| 766 | continue; |
| 767 | pr_debug(" - %llx , %llx\n", (unsigned long long)base, |
| 768 | (unsigned long long)size); |
| 769 | |
| 770 | early_init_dt_add_memory_arch(base, size); |
| 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 776 | int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, |
| 777 | int depth, void *data) |
| 778 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 779 | int l; |
| 780 | const char *p; |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 781 | |
| 782 | pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
| 783 | |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 784 | if (depth != 1 || !data || |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 785 | (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) |
| 786 | return 0; |
| 787 | |
| 788 | early_init_dt_check_for_initrd(node); |
| 789 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 790 | /* Retrieve command line */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 791 | p = of_get_flat_dt_prop(node, "bootargs", &l); |
| 792 | if (p != NULL && l > 0) |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 793 | strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE)); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 794 | |
Benjamin Herrenschmidt | 78b782cb | 2011-09-19 18:50:15 +0000 | [diff] [blame] | 795 | /* |
| 796 | * CONFIG_CMDLINE is meant to be a default in case nothing else |
| 797 | * managed to set the command line, unless CONFIG_CMDLINE_FORCE |
| 798 | * is set in which case we override whatever was found earlier. |
| 799 | */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 800 | #ifdef CONFIG_CMDLINE |
| 801 | #ifndef CONFIG_CMDLINE_FORCE |
Benjamin Herrenschmidt | 78b782cb | 2011-09-19 18:50:15 +0000 | [diff] [blame] | 802 | if (!((char *)data)[0]) |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 803 | #endif |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 804 | strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 805 | #endif /* CONFIG_CMDLINE */ |
| 806 | |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 807 | pr_debug("Command line is: %s\n", (char*)data); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 808 | |
| 809 | /* break now */ |
| 810 | return 1; |
| 811 | } |
| 812 | |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 813 | #ifdef CONFIG_HAVE_MEMBLOCK |
Rob Herring | 068f631 | 2013-09-24 22:20:01 -0500 | [diff] [blame] | 814 | void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) |
| 815 | { |
| 816 | const u64 phys_offset = __pa(PAGE_OFFSET); |
| 817 | base &= PAGE_MASK; |
| 818 | size &= PAGE_MASK; |
| 819 | if (base + size < phys_offset) { |
| 820 | pr_warning("Ignoring memory block 0x%llx - 0x%llx\n", |
| 821 | base, base + size); |
| 822 | return; |
| 823 | } |
| 824 | if (base < phys_offset) { |
| 825 | pr_warning("Ignoring memory range 0x%llx - 0x%llx\n", |
| 826 | base, phys_offset); |
| 827 | size -= phys_offset - base; |
| 828 | base = phys_offset; |
| 829 | } |
| 830 | memblock_add(base, size); |
| 831 | } |
| 832 | |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 833 | int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, |
| 834 | phys_addr_t size, bool nomap) |
| 835 | { |
| 836 | if (memblock_is_region_reserved(base, size)) |
| 837 | return -EBUSY; |
| 838 | if (nomap) |
| 839 | return memblock_remove(base, size); |
| 840 | return memblock_reserve(base, size); |
| 841 | } |
| 842 | |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 843 | /* |
| 844 | * called from unflatten_device_tree() to bootstrap devicetree itself |
| 845 | * Architectures can override this definition if memblock isn't used |
| 846 | */ |
| 847 | void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align) |
| 848 | { |
| 849 | return __va(memblock_alloc(size, align)); |
| 850 | } |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 851 | #else |
| 852 | int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, |
| 853 | phys_addr_t size, bool nomap) |
| 854 | { |
| 855 | pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n", |
| 856 | base, size, nomap ? " (nomap)" : ""); |
| 857 | return -ENOSYS; |
| 858 | } |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 859 | #endif |
| 860 | |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 861 | bool __init early_init_dt_scan(void *params) |
| 862 | { |
| 863 | if (!params) |
| 864 | return false; |
| 865 | |
| 866 | /* Setup flat device-tree pointer */ |
| 867 | initial_boot_params = params; |
| 868 | |
| 869 | /* check device tree validity */ |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 870 | if (fdt_check_header(params)) { |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 871 | initial_boot_params = NULL; |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | /* Retrieve various information from the /chosen node */ |
| 876 | of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line); |
| 877 | |
| 878 | /* Initialize {size,address}-cells info */ |
| 879 | of_scan_flat_dt(early_init_dt_scan_root, NULL); |
| 880 | |
| 881 | /* Setup memory, calling early_init_dt_add_memory_arch */ |
| 882 | of_scan_flat_dt(early_init_dt_scan_memory, NULL); |
| 883 | |
| 884 | return true; |
| 885 | } |
| 886 | |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 887 | /** |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 888 | * unflatten_device_tree - create tree of device_nodes from flat blob |
| 889 | * |
| 890 | * unflattens the device-tree passed by the firmware, creating the |
| 891 | * tree of struct device_node. It also fills the "name" and "type" |
| 892 | * pointers of the nodes so the normal device-tree walking functions |
| 893 | * can be used. |
| 894 | */ |
| 895 | void __init unflatten_device_tree(void) |
| 896 | { |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 897 | __unflatten_device_tree(initial_boot_params, &of_allnodes, |
Grant Likely | 672c544 | 2011-01-13 15:36:09 -0700 | [diff] [blame] | 898 | early_init_dt_alloc_memory_arch); |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 899 | |
Robert P. J. Day | 4c7d636 | 2013-05-30 05:38:08 -0400 | [diff] [blame] | 900 | /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */ |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 901 | of_alias_scan(early_init_dt_alloc_memory_arch); |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 902 | } |
Stephen Neuendorffer | e6ce132 | 2010-11-18 15:54:56 -0800 | [diff] [blame] | 903 | |
Rob Herring | a8bf752 | 2013-08-26 11:22:45 -0500 | [diff] [blame] | 904 | /** |
| 905 | * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob |
| 906 | * |
| 907 | * Copies and unflattens the device-tree passed by the firmware, creating the |
| 908 | * tree of struct device_node. It also fills the "name" and "type" |
| 909 | * pointers of the nodes so the normal device-tree walking functions |
| 910 | * can be used. This should only be used when the FDT memory has not been |
| 911 | * reserved such is the case when the FDT is built-in to the kernel init |
| 912 | * section. If the FDT memory is reserved already then unflatten_device_tree |
| 913 | * should be used instead. |
| 914 | */ |
| 915 | void __init unflatten_and_copy_device_tree(void) |
| 916 | { |
James Hogan | 6f041e9 | 2013-11-21 13:44:14 +0000 | [diff] [blame] | 917 | int size; |
| 918 | void *dt; |
| 919 | |
| 920 | if (!initial_boot_params) { |
| 921 | pr_warn("No valid device tree found, continuing without\n"); |
| 922 | return; |
| 923 | } |
| 924 | |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 925 | size = fdt_totalsize(initial_boot_params); |
James Hogan | 6f041e9 | 2013-11-21 13:44:14 +0000 | [diff] [blame] | 926 | dt = early_init_dt_alloc_memory_arch(size, |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 927 | roundup_pow_of_two(FDT_V17_SIZE)); |
Rob Herring | a8bf752 | 2013-08-26 11:22:45 -0500 | [diff] [blame] | 928 | |
| 929 | if (dt) { |
| 930 | memcpy(dt, initial_boot_params, size); |
| 931 | initial_boot_params = dt; |
| 932 | } |
| 933 | unflatten_device_tree(); |
| 934 | } |
| 935 | |
Rob Herring | b0a6fb3 | 2014-04-02 16:56:48 -0500 | [diff] [blame] | 936 | #if defined(CONFIG_DEBUG_FS) && defined(DEBUG) |
| 937 | static struct debugfs_blob_wrapper flat_dt_blob; |
| 938 | |
| 939 | static int __init of_flat_dt_debugfs_export_fdt(void) |
| 940 | { |
| 941 | struct dentry *d = debugfs_create_dir("device-tree", NULL); |
| 942 | |
| 943 | if (!d) |
| 944 | return -ENOENT; |
| 945 | |
| 946 | flat_dt_blob.data = initial_boot_params; |
| 947 | flat_dt_blob.size = fdt_totalsize(initial_boot_params); |
| 948 | |
| 949 | d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR, |
| 950 | d, &flat_dt_blob); |
| 951 | if (!d) |
| 952 | return -ENOENT; |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | module_init(of_flat_dt_debugfs_export_fdt); |
| 957 | #endif |
| 958 | |
Stephen Neuendorffer | e6ce132 | 2010-11-18 15:54:56 -0800 | [diff] [blame] | 959 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |