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