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