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