Rob Herring | af6074f | 2017-12-27 12:55:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Grant Likely | e169cfb | 2009-11-23 14:53:09 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Functions for working with the Flattened Device Tree data format |
| 4 | * |
| 5 | * Copyright 2009 Benjamin Herrenschmidt, IBM Corp |
| 6 | * benh@kernel.crashing.org |
Grant Likely | e169cfb | 2009-11-23 14:53:09 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Frank Rowand | bd0096d | 2016-10-17 12:21:23 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) "OF: fdt: " fmt |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 10 | |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 11 | #include <linux/crc32.h> |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 13 | #include <linux/initrd.h> |
Rob Herring | 0fa1c57 | 2018-01-05 15:32:33 -0600 | [diff] [blame] | 14 | #include <linux/bootmem.h> |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 15 | #include <linux/memblock.h> |
Guenter Roeck | f806238 | 2015-12-05 16:13:53 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Grant Likely | e169cfb | 2009-11-23 14:53:09 -0700 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_fdt.h> |
Marek Szyprowski | 3f0c820 | 2014-02-28 14:42:48 +0100 | [diff] [blame] | 19 | #include <linux/of_reserved_mem.h> |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 20 | #include <linux/sizes.h> |
Jeremy Kerr | 4ef7b37 | 2010-02-14 07:13:47 -0700 | [diff] [blame] | 21 | #include <linux/string.h> |
| 22 | #include <linux/errno.h> |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 23 | #include <linux/slab.h> |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 24 | #include <linux/libfdt.h> |
Rob Herring | b0a6fb3 | 2014-04-02 16:56:48 -0500 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 26 | #include <linux/serial_core.h> |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 27 | #include <linux/sysfs.h> |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 28 | |
Fabio Estevam | c89810a | 2012-01-02 14:19:03 -0200 | [diff] [blame] | 29 | #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ |
Jeremy Kerr | 4ef7b37 | 2010-02-14 07:13:47 -0700 | [diff] [blame] | 30 | #include <asm/page.h> |
| 31 | |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 32 | #include "of_private.h" |
| 33 | |
Laura Abbott | 704033c | 2014-07-15 10:03:35 -0700 | [diff] [blame] | 34 | /* |
| 35 | * of_fdt_limit_memory - limit the number of regions in the /memory node |
| 36 | * @limit: maximum entries |
| 37 | * |
| 38 | * Adjust the flattened device tree to have at most 'limit' number of |
| 39 | * memory entries in the /memory node. This function may be called |
| 40 | * any time after initial_boot_param is set. |
| 41 | */ |
| 42 | void of_fdt_limit_memory(int limit) |
| 43 | { |
| 44 | int memory; |
| 45 | int len; |
| 46 | const void *val; |
| 47 | int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 48 | int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
Rob Herring | 17a7035 | 2017-05-04 12:56:12 -0500 | [diff] [blame] | 49 | const __be32 *addr_prop; |
| 50 | const __be32 *size_prop; |
Laura Abbott | 704033c | 2014-07-15 10:03:35 -0700 | [diff] [blame] | 51 | int root_offset; |
| 52 | int cell_size; |
| 53 | |
| 54 | root_offset = fdt_path_offset(initial_boot_params, "/"); |
| 55 | if (root_offset < 0) |
| 56 | return; |
| 57 | |
| 58 | addr_prop = fdt_getprop(initial_boot_params, root_offset, |
| 59 | "#address-cells", NULL); |
| 60 | if (addr_prop) |
| 61 | nr_address_cells = fdt32_to_cpu(*addr_prop); |
| 62 | |
| 63 | size_prop = fdt_getprop(initial_boot_params, root_offset, |
| 64 | "#size-cells", NULL); |
| 65 | if (size_prop) |
| 66 | nr_size_cells = fdt32_to_cpu(*size_prop); |
| 67 | |
| 68 | cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells); |
| 69 | |
| 70 | memory = fdt_path_offset(initial_boot_params, "/memory"); |
| 71 | if (memory > 0) { |
| 72 | val = fdt_getprop(initial_boot_params, memory, "reg", &len); |
| 73 | if (len > limit*cell_size) { |
| 74 | len = limit*cell_size; |
| 75 | pr_debug("Limiting number of entries to %d\n", limit); |
| 76 | fdt_setprop(initial_boot_params, memory, "reg", val, |
| 77 | len); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 82 | /** |
| 83 | * of_fdt_is_compatible - Return true if given node from the given blob has |
| 84 | * compat in its compatible list |
| 85 | * @blob: A device tree blob |
| 86 | * @node: node to test |
| 87 | * @compat: compatible string to compare with compatible list. |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 88 | * |
| 89 | * On match, returns a non-zero value with smaller values returned for more |
| 90 | * specific compatible values. |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 91 | */ |
Frank Rowand | 92af089 | 2017-06-20 16:38:28 -0700 | [diff] [blame] | 92 | static int of_fdt_is_compatible(const void *blob, |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 93 | unsigned long node, const char *compat) |
| 94 | { |
| 95 | const char *cp; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 96 | int cplen; |
| 97 | unsigned long l, score = 0; |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 98 | |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 99 | cp = fdt_getprop(blob, node, "compatible", &cplen); |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 100 | if (cp == NULL) |
| 101 | return 0; |
| 102 | while (cplen > 0) { |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 103 | score++; |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 104 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 105 | return score; |
Stephen Neuendorffer | 9706a36 | 2010-11-18 15:54:59 -0800 | [diff] [blame] | 106 | l = strlen(cp) + 1; |
| 107 | cp += l; |
| 108 | cplen -= l; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 114 | /** |
Kevin Cernekee | cc78378 | 2015-04-09 13:05:15 -0700 | [diff] [blame] | 115 | * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses |
| 116 | * @blob: A device tree blob |
| 117 | * @node: node to test |
| 118 | * |
| 119 | * Returns true if the node has a "big-endian" property, or if the kernel |
| 120 | * was compiled for BE *and* the node has a "native-endian" property. |
| 121 | * Returns false otherwise. |
| 122 | */ |
| 123 | bool of_fdt_is_big_endian(const void *blob, unsigned long node) |
| 124 | { |
| 125 | if (fdt_getprop(blob, node, "big-endian", NULL)) |
| 126 | return true; |
| 127 | if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && |
| 128 | fdt_getprop(blob, node, "native-endian", NULL)) |
| 129 | return true; |
| 130 | return false; |
| 131 | } |
| 132 | |
Rob Herring | ecc8a96 | 2017-09-28 19:20:32 -0500 | [diff] [blame] | 133 | static bool of_fdt_device_is_available(const void *blob, unsigned long node) |
| 134 | { |
| 135 | const char *status = fdt_getprop(blob, node, "status", NULL); |
| 136 | |
| 137 | if (!status) |
| 138 | return true; |
| 139 | |
| 140 | if (!strcmp(status, "ok") || !strcmp(status, "okay")) |
| 141 | return true; |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
Kevin Cernekee | cc78378 | 2015-04-09 13:05:15 -0700 | [diff] [blame] | 146 | /** |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 147 | * of_fdt_match - Return true if node matches a list of compatible values |
| 148 | */ |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 149 | int of_fdt_match(const void *blob, unsigned long node, |
Uwe Kleine-König | 7b482c8 | 2011-12-20 22:56:45 +0100 | [diff] [blame] | 150 | const char *const *compat) |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 151 | { |
| 152 | unsigned int tmp, score = 0; |
| 153 | |
| 154 | if (!compat) |
| 155 | return 0; |
| 156 | |
| 157 | while (*compat) { |
| 158 | tmp = of_fdt_is_compatible(blob, node, *compat); |
| 159 | if (tmp && (score == 0 || (tmp < score))) |
| 160 | score = tmp; |
| 161 | compat++; |
| 162 | } |
| 163 | |
| 164 | return score; |
| 165 | } |
| 166 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 167 | static void *unflatten_dt_alloc(void **mem, unsigned long size, |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 168 | unsigned long align) |
| 169 | { |
| 170 | void *res; |
| 171 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 172 | *mem = PTR_ALIGN(*mem, align); |
| 173 | res = *mem; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 174 | *mem += size; |
| 175 | |
| 176 | return res; |
| 177 | } |
| 178 | |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 179 | static void populate_properties(const void *blob, |
| 180 | int offset, |
| 181 | void **mem, |
| 182 | struct device_node *np, |
| 183 | const char *nodename, |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 184 | bool dryrun) |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 185 | { |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 186 | struct property *pp, **pprev = NULL; |
| 187 | int cur; |
| 188 | bool has_name = false; |
| 189 | |
| 190 | pprev = &np->properties; |
| 191 | for (cur = fdt_first_property_offset(blob, offset); |
| 192 | cur >= 0; |
| 193 | cur = fdt_next_property_offset(blob, cur)) { |
| 194 | const __be32 *val; |
| 195 | const char *pname; |
| 196 | u32 sz; |
| 197 | |
| 198 | val = fdt_getprop_by_offset(blob, cur, &pname, &sz); |
| 199 | if (!val) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 200 | pr_warn("Cannot locate property at 0x%x\n", cur); |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 201 | continue; |
| 202 | } |
| 203 | |
| 204 | if (!pname) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 205 | pr_warn("Cannot find property name at 0x%x\n", cur); |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 206 | continue; |
| 207 | } |
| 208 | |
| 209 | if (!strcmp(pname, "name")) |
| 210 | has_name = true; |
| 211 | |
| 212 | pp = unflatten_dt_alloc(mem, sizeof(struct property), |
| 213 | __alignof__(struct property)); |
| 214 | if (dryrun) |
| 215 | continue; |
| 216 | |
| 217 | /* We accept flattened tree phandles either in |
| 218 | * ePAPR-style "phandle" properties, or the |
| 219 | * legacy "linux,phandle" properties. If both |
| 220 | * appear and have different values, things |
| 221 | * will get weird. Don't do that. |
| 222 | */ |
| 223 | if (!strcmp(pname, "phandle") || |
| 224 | !strcmp(pname, "linux,phandle")) { |
| 225 | if (!np->phandle) |
| 226 | np->phandle = be32_to_cpup(val); |
| 227 | } |
| 228 | |
| 229 | /* And we process the "ibm,phandle" property |
| 230 | * used in pSeries dynamic device tree |
| 231 | * stuff |
| 232 | */ |
| 233 | if (!strcmp(pname, "ibm,phandle")) |
| 234 | np->phandle = be32_to_cpup(val); |
| 235 | |
| 236 | pp->name = (char *)pname; |
| 237 | pp->length = sz; |
| 238 | pp->value = (__be32 *)val; |
| 239 | *pprev = pp; |
| 240 | pprev = &pp->next; |
| 241 | } |
| 242 | |
| 243 | /* With version 0x10 we may not have the name property, |
| 244 | * recreate it here from the unit name if absent |
| 245 | */ |
| 246 | if (!has_name) { |
| 247 | const char *p = nodename, *ps = p, *pa = NULL; |
| 248 | int len; |
| 249 | |
| 250 | while (*p) { |
| 251 | if ((*p) == '@') |
| 252 | pa = p; |
| 253 | else if ((*p) == '/') |
| 254 | ps = p + 1; |
| 255 | p++; |
| 256 | } |
| 257 | |
| 258 | if (pa < ps) |
| 259 | pa = p; |
| 260 | len = (pa - ps) + 1; |
| 261 | pp = unflatten_dt_alloc(mem, sizeof(struct property) + len, |
| 262 | __alignof__(struct property)); |
| 263 | if (!dryrun) { |
| 264 | pp->name = "name"; |
| 265 | pp->length = len; |
| 266 | pp->value = pp + 1; |
| 267 | *pprev = pp; |
| 268 | pprev = &pp->next; |
| 269 | memcpy(pp->value, ps, len - 1); |
| 270 | ((char *)pp->value)[len - 1] = 0; |
| 271 | pr_debug("fixed up name for %s -> %s\n", |
| 272 | nodename, (char *)pp->value); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (!dryrun) |
| 277 | *pprev = NULL; |
| 278 | } |
| 279 | |
Rob Herring | a7e4cfb | 2017-06-01 18:01:47 -0500 | [diff] [blame] | 280 | static bool populate_node(const void *blob, |
| 281 | int offset, |
| 282 | void **mem, |
| 283 | struct device_node *dad, |
| 284 | struct device_node **pnp, |
| 285 | bool dryrun) |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 286 | { |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 287 | struct device_node *np; |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 288 | const char *pathp; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 289 | unsigned int l, allocl; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 290 | |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 291 | pathp = fdt_get_name(blob, offset, &l); |
| 292 | if (!pathp) { |
| 293 | *pnp = NULL; |
Rob Herring | a7e4cfb | 2017-06-01 18:01:47 -0500 | [diff] [blame] | 294 | return false; |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 295 | } |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 296 | |
Ricky Liang | 05f4647 | 2015-04-14 12:36:05 +0800 | [diff] [blame] | 297 | allocl = ++l; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 298 | |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 299 | np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl, |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 300 | __alignof__(struct device_node)); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 301 | if (!dryrun) { |
Grant Likely | c22618a | 2012-11-14 22:37:12 +0000 | [diff] [blame] | 302 | char *fn; |
Pantelis Antoniou | 0829f6d | 2013-12-13 20:08:59 +0200 | [diff] [blame] | 303 | of_node_init(np); |
Grant Likely | c22618a | 2012-11-14 22:37:12 +0000 | [diff] [blame] | 304 | np->full_name = fn = ((char *)np) + sizeof(*np); |
Rob Herring | a7e4cfb | 2017-06-01 18:01:47 -0500 | [diff] [blame] | 305 | |
Grant Likely | c22618a | 2012-11-14 22:37:12 +0000 | [diff] [blame] | 306 | memcpy(fn, pathp, l); |
| 307 | |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 308 | if (dad != NULL) { |
| 309 | np->parent = dad; |
Grant Likely | 70161ff | 2014-11-28 16:03:33 +0000 | [diff] [blame] | 310 | np->sibling = dad->child; |
| 311 | dad->child = np; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 312 | } |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 313 | } |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 314 | |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 315 | populate_properties(blob, offset, mem, np, pathp, dryrun); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 316 | if (!dryrun) { |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 317 | np->name = of_get_property(np, "name", NULL); |
| 318 | np->type = of_get_property(np, "device_type", NULL); |
| 319 | |
| 320 | if (!np->name) |
| 321 | np->name = "<NULL>"; |
| 322 | if (!np->type) |
| 323 | np->type = "<NULL>"; |
| 324 | } |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 325 | |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 326 | *pnp = np; |
Rob Herring | a7e4cfb | 2017-06-01 18:01:47 -0500 | [diff] [blame] | 327 | return true; |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 328 | } |
| 329 | |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 330 | static void reverse_nodes(struct device_node *parent) |
| 331 | { |
| 332 | struct device_node *child, *next; |
| 333 | |
| 334 | /* In-depth first */ |
| 335 | child = parent->child; |
| 336 | while (child) { |
| 337 | reverse_nodes(child); |
| 338 | |
| 339 | child = child->sibling; |
| 340 | } |
| 341 | |
| 342 | /* Reverse the nodes in the child list */ |
| 343 | child = parent->child; |
| 344 | parent->child = NULL; |
| 345 | while (child) { |
| 346 | next = child->sibling; |
| 347 | |
| 348 | child->sibling = parent->child; |
| 349 | parent->child = child; |
| 350 | child = next; |
| 351 | } |
| 352 | } |
| 353 | |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 354 | /** |
Gavin Shan | 947c82c | 2016-05-03 23:22:49 +1000 | [diff] [blame] | 355 | * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 356 | * @blob: The parent device tree blob |
| 357 | * @mem: Memory chunk to use for allocating device nodes and properties |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 358 | * @dad: Parent struct device_node |
| 359 | * @nodepp: The device_node tree created by the call |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 360 | * |
| 361 | * It returns the size of unflattened device tree or error code |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 362 | */ |
Gavin Shan | 947c82c | 2016-05-03 23:22:49 +1000 | [diff] [blame] | 363 | static int unflatten_dt_nodes(const void *blob, |
| 364 | void *mem, |
| 365 | struct device_node *dad, |
| 366 | struct device_node **nodepp) |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 367 | { |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 368 | struct device_node *root; |
Gavin Shan | 8c237cd | 2016-06-09 15:50:49 +1000 | [diff] [blame] | 369 | int offset = 0, depth = 0, initial_depth = 0; |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 370 | #define FDT_MAX_DEPTH 64 |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 371 | struct device_node *nps[FDT_MAX_DEPTH]; |
| 372 | void *base = mem; |
| 373 | bool dryrun = !base; |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 374 | |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 375 | if (nodepp) |
| 376 | *nodepp = NULL; |
Gavin Shan | dfbd4c6 | 2016-05-03 23:22:47 +1000 | [diff] [blame] | 377 | |
Gavin Shan | 8c237cd | 2016-06-09 15:50:49 +1000 | [diff] [blame] | 378 | /* |
| 379 | * We're unflattening device sub-tree if @dad is valid. There are |
| 380 | * possibly multiple nodes in the first level of depth. We need |
| 381 | * set @depth to 1 to make fdt_next_node() happy as it bails |
| 382 | * immediately when negative @depth is found. Otherwise, the device |
| 383 | * nodes except the first one won't be unflattened successfully. |
| 384 | */ |
| 385 | if (dad) |
| 386 | depth = initial_depth = 1; |
| 387 | |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 388 | root = dad; |
Rhyland Klein | 78c44d9 | 2016-05-11 13:36:57 -0400 | [diff] [blame] | 389 | nps[depth] = dad; |
Gavin Shan | 8c237cd | 2016-06-09 15:50:49 +1000 | [diff] [blame] | 390 | |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 391 | for (offset = 0; |
Gavin Shan | 8c237cd | 2016-06-09 15:50:49 +1000 | [diff] [blame] | 392 | offset >= 0 && depth >= initial_depth; |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 393 | offset = fdt_next_node(blob, offset, &depth)) { |
| 394 | if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH)) |
| 395 | continue; |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 396 | |
Rob Herring | 77ea8a6 | 2017-10-03 11:07:55 -0500 | [diff] [blame] | 397 | if (!IS_ENABLED(CONFIG_OF_KOBJ) && |
| 398 | !of_fdt_device_is_available(blob, offset)) |
| 399 | continue; |
| 400 | |
Rob Herring | a7e4cfb | 2017-06-01 18:01:47 -0500 | [diff] [blame] | 401 | if (!populate_node(blob, offset, &mem, nps[depth], |
| 402 | &nps[depth+1], dryrun)) |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 403 | return mem - base; |
| 404 | |
| 405 | if (!dryrun && nodepp && !*nodepp) |
Rhyland Klein | 78c44d9 | 2016-05-11 13:36:57 -0400 | [diff] [blame] | 406 | *nodepp = nps[depth+1]; |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 407 | if (!dryrun && !root) |
Rhyland Klein | 78c44d9 | 2016-05-11 13:36:57 -0400 | [diff] [blame] | 408 | root = nps[depth+1]; |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (offset < 0 && offset != -FDT_ERR_NOTFOUND) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 412 | pr_err("Error %d processing FDT\n", offset); |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 413 | return -EINVAL; |
| 414 | } |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 415 | |
Grant Likely | 70161ff | 2014-11-28 16:03:33 +0000 | [diff] [blame] | 416 | /* |
| 417 | * Reverse the child list. Some drivers assumes node order matches .dts |
| 418 | * node order |
| 419 | */ |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 420 | if (!dryrun) |
| 421 | reverse_nodes(root); |
Grant Likely | 70161ff | 2014-11-28 16:03:33 +0000 | [diff] [blame] | 422 | |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 423 | return mem - base; |
Grant Likely | bbd3393 | 2009-11-23 20:07:00 -0700 | [diff] [blame] | 424 | } |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 425 | |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 426 | /** |
| 427 | * __unflatten_device_tree - create tree of device_nodes from flat blob |
| 428 | * |
| 429 | * unflattens a device-tree, creating the |
| 430 | * tree of struct device_node. It also fills the "name" and "type" |
| 431 | * pointers of the nodes so the normal device-tree walking functions |
| 432 | * can be used. |
| 433 | * @blob: The blob to expand |
Gavin Shan | c426323 | 2016-05-03 23:22:50 +1000 | [diff] [blame] | 434 | * @dad: Parent device node |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 435 | * @mynodes: The device_node tree created by the call |
| 436 | * @dt_alloc: An allocator that provides a virtual address to memory |
| 437 | * for the resulting tree |
Stephen Boyd | f5d2da6 | 2017-10-13 00:41:29 -0700 | [diff] [blame] | 438 | * @detached: if true set OF_DETACHED on @mynodes |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 439 | * |
| 440 | * Returns NULL on failure or the memory chunk containing the unflattened |
| 441 | * device tree on success. |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 442 | */ |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 443 | void *__unflatten_device_tree(const void *blob, |
| 444 | struct device_node *dad, |
| 445 | struct device_node **mynodes, |
| 446 | void *(*dt_alloc)(u64 size, u64 align), |
| 447 | bool detached) |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 448 | { |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 449 | int size; |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 450 | void *mem; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 451 | |
| 452 | pr_debug(" -> unflatten_device_tree()\n"); |
| 453 | |
| 454 | if (!blob) { |
| 455 | pr_debug("No device tree pointer\n"); |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 456 | return NULL; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | pr_debug("Unflattening device tree:\n"); |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 460 | pr_debug("magic: %08x\n", fdt_magic(blob)); |
| 461 | pr_debug("size: %08x\n", fdt_totalsize(blob)); |
| 462 | pr_debug("version: %08x\n", fdt_version(blob)); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 463 | |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 464 | if (fdt_check_header(blob)) { |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 465 | pr_err("Invalid device tree blob header\n"); |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 466 | return NULL; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* First pass, scan for size */ |
Gavin Shan | c426323 | 2016-05-03 23:22:50 +1000 | [diff] [blame] | 470 | size = unflatten_dt_nodes(blob, NULL, dad, NULL); |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 471 | if (size < 0) |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 472 | return NULL; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 473 | |
Gavin Shan | 5080008 | 2016-05-03 23:22:48 +1000 | [diff] [blame] | 474 | size = ALIGN(size, 4); |
| 475 | pr_debug(" size is %d, allocating...\n", size); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 476 | |
| 477 | /* Allocate memory for the expanded device tree */ |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 478 | mem = dt_alloc(size + 4, __alignof__(struct device_node)); |
Johan Hovold | 49e67dd | 2017-05-17 17:29:09 +0200 | [diff] [blame] | 479 | if (!mem) |
| 480 | return NULL; |
| 481 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 482 | memset(mem, 0, size); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 483 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 484 | *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef); |
Wladislav Wiebe | 9e40127 | 2013-08-12 13:06:53 +0200 | [diff] [blame] | 485 | |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 486 | pr_debug(" unflattening %p...\n", mem); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 487 | |
| 488 | /* Second pass, do actual unflattening */ |
Gavin Shan | c426323 | 2016-05-03 23:22:50 +1000 | [diff] [blame] | 489 | unflatten_dt_nodes(blob, mem, dad, mynodes); |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 490 | if (be32_to_cpup(mem + size) != 0xdeadbeef) |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 491 | pr_warning("End of tree marker overwritten: %08x\n", |
Grant Likely | 4485681 | 2013-08-29 13:30:35 +0100 | [diff] [blame] | 492 | be32_to_cpup(mem + size)); |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 493 | |
Gavin Shan | 89c6775 | 2016-08-01 17:17:53 +1000 | [diff] [blame] | 494 | if (detached && mynodes) { |
Michal Suchanek | 1d1bde5 | 2016-07-19 00:01:12 +0200 | [diff] [blame] | 495 | of_node_set_flag(*mynodes, OF_DETACHED); |
| 496 | pr_debug("unflattened tree is detached\n"); |
| 497 | } |
| 498 | |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 499 | pr_debug(" <- unflatten_device_tree()\n"); |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 500 | return mem; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static void *kernel_tree_alloc(u64 size, u64 align) |
| 504 | { |
| 505 | return kzalloc(size, GFP_KERNEL); |
| 506 | } |
| 507 | |
Guenter Roeck | f806238 | 2015-12-05 16:13:53 -0800 | [diff] [blame] | 508 | static DEFINE_MUTEX(of_fdt_unflatten_mutex); |
| 509 | |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 510 | /** |
| 511 | * of_fdt_unflatten_tree - create tree of device_nodes from flat blob |
Gavin Shan | c426323 | 2016-05-03 23:22:50 +1000 | [diff] [blame] | 512 | * @blob: Flat device tree blob |
| 513 | * @dad: Parent device node |
| 514 | * @mynodes: The device tree created by the call |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 515 | * |
| 516 | * unflattens the device-tree passed by the firmware, creating the |
| 517 | * tree of struct device_node. It also fills the "name" and "type" |
| 518 | * pointers of the nodes so the normal device-tree walking functions |
| 519 | * can be used. |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 520 | * |
| 521 | * Returns NULL on failure or the memory chunk containing the unflattened |
| 522 | * device tree on success. |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 523 | */ |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 524 | void *of_fdt_unflatten_tree(const unsigned long *blob, |
| 525 | struct device_node *dad, |
| 526 | struct device_node **mynodes) |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 527 | { |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 528 | void *mem; |
| 529 | |
Guenter Roeck | f806238 | 2015-12-05 16:13:53 -0800 | [diff] [blame] | 530 | mutex_lock(&of_fdt_unflatten_mutex); |
Michal Suchanek | 1d1bde5 | 2016-07-19 00:01:12 +0200 | [diff] [blame] | 531 | mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc, |
| 532 | true); |
Guenter Roeck | f806238 | 2015-12-05 16:13:53 -0800 | [diff] [blame] | 533 | mutex_unlock(&of_fdt_unflatten_mutex); |
Gavin Shan | 8326241 | 2016-05-03 23:22:51 +1000 | [diff] [blame] | 534 | |
| 535 | return mem; |
Stephen Neuendorffer | fe14042 | 2010-11-18 15:55:02 -0800 | [diff] [blame] | 536 | } |
| 537 | EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree); |
| 538 | |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 539 | /* Everything below here references initial_boot_params directly. */ |
| 540 | int __initdata dt_root_addr_cells; |
| 541 | int __initdata dt_root_size_cells; |
| 542 | |
Rob Herring | 1daa0c4 | 2014-03-31 15:25:04 -0500 | [diff] [blame] | 543 | void *initial_boot_params; |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 544 | |
| 545 | #ifdef CONFIG_OF_EARLY_FLATTREE |
| 546 | |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 547 | static u32 of_fdt_crc32; |
| 548 | |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 549 | /** |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 550 | * res_mem_reserve_reg() - reserve all memory described in 'reg' property |
| 551 | */ |
| 552 | static int __init __reserved_mem_reserve_reg(unsigned long node, |
| 553 | const char *uname) |
| 554 | { |
| 555 | int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); |
| 556 | phys_addr_t base, size; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 557 | int len; |
| 558 | const __be32 *prop; |
Marek Szyprowski | 3f0c820 | 2014-02-28 14:42:48 +0100 | [diff] [blame] | 559 | int nomap, first = 1; |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 560 | |
| 561 | prop = of_get_flat_dt_prop(node, "reg", &len); |
| 562 | if (!prop) |
| 563 | return -ENOENT; |
| 564 | |
| 565 | if (len && len % t_len != 0) { |
| 566 | pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n", |
| 567 | uname); |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
| 571 | nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; |
| 572 | |
| 573 | while (len >= t_len) { |
| 574 | base = dt_mem_next_cell(dt_root_addr_cells, &prop); |
| 575 | size = dt_mem_next_cell(dt_root_size_cells, &prop); |
| 576 | |
Al Cooper | b5f2a8c | 2014-08-06 16:30:04 -0400 | [diff] [blame] | 577 | if (size && |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 578 | early_init_dt_reserve_memory_arch(base, size, nomap) == 0) |
| 579 | pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n", |
| 580 | uname, &base, (unsigned long)size / SZ_1M); |
| 581 | else |
| 582 | pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n", |
| 583 | uname, &base, (unsigned long)size / SZ_1M); |
| 584 | |
| 585 | len -= t_len; |
Marek Szyprowski | 3f0c820 | 2014-02-28 14:42:48 +0100 | [diff] [blame] | 586 | if (first) { |
| 587 | fdt_reserved_mem_save_node(node, uname, base, size); |
| 588 | first = 0; |
| 589 | } |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * __reserved_mem_check_root() - check if #size-cells, #address-cells provided |
| 596 | * in /reserved-memory matches the values supported by the current implementation, |
| 597 | * also check if ranges property has been provided |
| 598 | */ |
Xiubo Li | 5b62411 | 2014-04-08 13:48:07 +0800 | [diff] [blame] | 599 | static int __init __reserved_mem_check_root(unsigned long node) |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 600 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 601 | const __be32 *prop; |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 602 | |
| 603 | prop = of_get_flat_dt_prop(node, "#size-cells", NULL); |
| 604 | if (!prop || be32_to_cpup(prop) != dt_root_size_cells) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | prop = of_get_flat_dt_prop(node, "#address-cells", NULL); |
| 608 | if (!prop || be32_to_cpup(prop) != dt_root_addr_cells) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | prop = of_get_flat_dt_prop(node, "ranges", NULL); |
| 612 | if (!prop) |
| 613 | return -EINVAL; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory |
| 619 | */ |
| 620 | static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname, |
| 621 | int depth, void *data) |
| 622 | { |
| 623 | static int found; |
Marek Szyprowski | 3f0c820 | 2014-02-28 14:42:48 +0100 | [diff] [blame] | 624 | int err; |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 625 | |
| 626 | if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) { |
| 627 | if (__reserved_mem_check_root(node) != 0) { |
| 628 | pr_err("Reserved memory: unsupported node format, ignoring\n"); |
| 629 | /* break scan */ |
| 630 | return 1; |
| 631 | } |
| 632 | found = 1; |
| 633 | /* scan next node */ |
| 634 | return 0; |
| 635 | } else if (!found) { |
| 636 | /* scan next node */ |
| 637 | return 0; |
| 638 | } else if (found && depth < 2) { |
| 639 | /* scanning of /reserved-memory has been finished */ |
| 640 | return 1; |
| 641 | } |
| 642 | |
Rob Herring | ecc8a96 | 2017-09-28 19:20:32 -0500 | [diff] [blame] | 643 | if (!of_fdt_device_is_available(initial_boot_params, node)) |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 644 | return 0; |
| 645 | |
Marek Szyprowski | 3f0c820 | 2014-02-28 14:42:48 +0100 | [diff] [blame] | 646 | err = __reserved_mem_reserve_reg(node, uname); |
| 647 | if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL)) |
| 648 | fdt_reserved_mem_save_node(node, uname, 0, 0); |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 649 | |
| 650 | /* scan next node */ |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * early_init_fdt_scan_reserved_mem() - create reserved memory regions |
| 656 | * |
| 657 | * This function grabs memory from early allocator for device exclusive use |
| 658 | * defined in device tree structures. It should be called by arch specific code |
| 659 | * once the early allocator (i.e. memblock) has been fully activated. |
| 660 | */ |
| 661 | void __init early_init_fdt_scan_reserved_mem(void) |
| 662 | { |
Rob Herring | d1552ce | 2014-04-01 22:46:48 -0500 | [diff] [blame] | 663 | int n; |
| 664 | u64 base, size; |
| 665 | |
Josh Cartwright | 2040b52 | 2014-03-13 16:36:36 -0500 | [diff] [blame] | 666 | if (!initial_boot_params) |
| 667 | return; |
| 668 | |
Rob Herring | d1552ce | 2014-04-01 22:46:48 -0500 | [diff] [blame] | 669 | /* Process header /memreserve/ fields */ |
| 670 | for (n = 0; ; n++) { |
| 671 | fdt_get_mem_rsv(initial_boot_params, n, &base, &size); |
| 672 | if (!size) |
| 673 | break; |
| 674 | early_init_dt_reserve_memory_arch(base, size, 0); |
| 675 | } |
| 676 | |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 677 | of_scan_flat_dt(__fdt_scan_reserved_mem, NULL); |
Marek Szyprowski | 3f0c820 | 2014-02-28 14:42:48 +0100 | [diff] [blame] | 678 | fdt_init_reserved_mem(); |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /** |
Ard Biesheuvel | 24bbd92 | 2015-06-01 13:40:31 +0200 | [diff] [blame] | 682 | * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob |
| 683 | */ |
| 684 | void __init early_init_fdt_reserve_self(void) |
| 685 | { |
| 686 | if (!initial_boot_params) |
| 687 | return; |
| 688 | |
| 689 | /* Reserve the dtb region */ |
| 690 | early_init_dt_reserve_memory_arch(__pa(initial_boot_params), |
| 691 | fdt_totalsize(initial_boot_params), |
| 692 | 0); |
| 693 | } |
| 694 | |
| 695 | /** |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 696 | * of_scan_flat_dt - scan flattened tree blob and call callback on each. |
| 697 | * @it: callback function |
| 698 | * @data: context data pointer |
| 699 | * |
| 700 | * This function is used to scan the flattened device-tree, it is |
| 701 | * used to extract the memory information at boot before we can |
| 702 | * unflatten the tree |
| 703 | */ |
| 704 | int __init of_scan_flat_dt(int (*it)(unsigned long node, |
| 705 | const char *uname, int depth, |
| 706 | void *data), |
| 707 | void *data) |
| 708 | { |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 709 | const void *blob = initial_boot_params; |
| 710 | const char *pathp; |
| 711 | int offset, rc = 0, depth = -1; |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 712 | |
Tobias Wolf | 3ec7544 | 2016-11-23 10:40:07 +0100 | [diff] [blame] | 713 | if (!blob) |
| 714 | return 0; |
| 715 | |
| 716 | for (offset = fdt_next_node(blob, -1, &depth); |
| 717 | offset >= 0 && depth >= 0 && !rc; |
| 718 | offset = fdt_next_node(blob, offset, &depth)) { |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 719 | |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 720 | pathp = fdt_get_name(blob, offset, NULL); |
Andy Shevchenko | 375da3a | 2012-12-17 16:01:28 -0800 | [diff] [blame] | 721 | if (*pathp == '/') |
| 722 | pathp = kbasename(pathp); |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 723 | rc = it(offset, pathp, depth, data); |
| 724 | } |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 725 | return rc; |
| 726 | } |
| 727 | |
| 728 | /** |
Nicholas Piggin | ea47dd1 | 2017-04-19 05:12:18 +1000 | [diff] [blame] | 729 | * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each. |
| 730 | * @it: callback function |
| 731 | * @data: context data pointer |
| 732 | * |
| 733 | * This function is used to scan sub-nodes of a node. |
| 734 | */ |
| 735 | int __init of_scan_flat_dt_subnodes(unsigned long parent, |
| 736 | int (*it)(unsigned long node, |
| 737 | const char *uname, |
| 738 | void *data), |
| 739 | void *data) |
| 740 | { |
| 741 | const void *blob = initial_boot_params; |
| 742 | int node; |
| 743 | |
| 744 | fdt_for_each_subnode(node, blob, parent) { |
| 745 | const char *pathp; |
| 746 | int rc; |
| 747 | |
| 748 | pathp = fdt_get_name(blob, node, NULL); |
| 749 | if (*pathp == '/') |
| 750 | pathp = kbasename(pathp); |
| 751 | rc = it(node, pathp, data); |
| 752 | if (rc) |
| 753 | return rc; |
| 754 | } |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /** |
Shannon Zhao | 9c60986 | 2016-04-07 20:03:33 +0800 | [diff] [blame] | 759 | * of_get_flat_dt_subnode_by_name - get the subnode by given name |
| 760 | * |
| 761 | * @node: the parent node |
| 762 | * @uname: the name of subnode |
| 763 | * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none |
| 764 | */ |
| 765 | |
| 766 | int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname) |
| 767 | { |
| 768 | return fdt_subnode_offset(initial_boot_params, node, uname); |
| 769 | } |
| 770 | |
| 771 | /** |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 772 | * of_get_flat_dt_root - find the root node in the flat blob |
| 773 | */ |
| 774 | unsigned long __init of_get_flat_dt_root(void) |
| 775 | { |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 776 | return 0; |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
Rob Herring | c0556d3 | 2014-04-22 12:55:10 -0500 | [diff] [blame] | 780 | * of_get_flat_dt_size - Return the total size of the FDT |
| 781 | */ |
| 782 | int __init of_get_flat_dt_size(void) |
| 783 | { |
| 784 | return fdt_totalsize(initial_boot_params); |
| 785 | } |
| 786 | |
| 787 | /** |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 788 | * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr |
| 789 | * |
| 790 | * This function can be used within scan_flattened_dt callback to get |
| 791 | * access to properties |
| 792 | */ |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 793 | const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, |
| 794 | int *size) |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 795 | { |
Rob Herring | e6a6928 | 2014-04-02 15:10:14 -0500 | [diff] [blame] | 796 | return fdt_getprop(initial_boot_params, node, name, size); |
Stephen Neuendorffer | 57d00ec | 2010-11-18 15:55:01 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /** |
| 800 | * of_flat_dt_is_compatible - Return true if given node has compat in compatible list |
| 801 | * @node: node to test |
| 802 | * @compat: compatible string to compare with compatible list. |
| 803 | */ |
| 804 | int __init of_flat_dt_is_compatible(unsigned long node, const char *compat) |
| 805 | { |
| 806 | return of_fdt_is_compatible(initial_boot_params, node, compat); |
| 807 | } |
| 808 | |
Grant Likely | a4f740c | 2010-10-30 11:49:09 -0400 | [diff] [blame] | 809 | /** |
| 810 | * of_flat_dt_match - Return true if node matches a list of compatible values |
| 811 | */ |
Uwe Kleine-König | 7b482c8 | 2011-12-20 22:56:45 +0100 | [diff] [blame] | 812 | 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] | 813 | { |
| 814 | return of_fdt_match(initial_boot_params, node, compat); |
| 815 | } |
| 816 | |
Nicholas Piggin | ea47dd1 | 2017-04-19 05:12:18 +1000 | [diff] [blame] | 817 | /** |
| 818 | * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle |
| 819 | */ |
| 820 | uint32_t __init of_get_flat_dt_phandle(unsigned long node) |
| 821 | { |
| 822 | return fdt_get_phandle(initial_boot_params, node); |
| 823 | } |
| 824 | |
Marek Szyprowski | 57d74bc | 2013-08-26 14:41:56 +0200 | [diff] [blame] | 825 | struct fdt_scan_status { |
| 826 | const char *name; |
| 827 | int namelen; |
| 828 | int depth; |
| 829 | int found; |
| 830 | int (*iterator)(unsigned long node, const char *uname, int depth, void *data); |
| 831 | void *data; |
| 832 | }; |
| 833 | |
Rob Herring | 6a903a2 | 2013-08-27 21:41:56 -0500 | [diff] [blame] | 834 | const char * __init of_flat_dt_get_machine_name(void) |
| 835 | { |
| 836 | const char *name; |
| 837 | unsigned long dt_root = of_get_flat_dt_root(); |
| 838 | |
| 839 | name = of_get_flat_dt_prop(dt_root, "model", NULL); |
| 840 | if (!name) |
| 841 | name = of_get_flat_dt_prop(dt_root, "compatible", NULL); |
| 842 | return name; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * of_flat_dt_match_machine - Iterate match tables to find matching machine. |
| 847 | * |
| 848 | * @default_match: A machine specific ptr to return in case of no match. |
| 849 | * @get_next_compat: callback function to return next compatible match table. |
| 850 | * |
| 851 | * Iterate through machine match tables to find the best match for the machine |
| 852 | * compatible string in the FDT. |
| 853 | */ |
| 854 | const void * __init of_flat_dt_match_machine(const void *default_match, |
| 855 | const void * (*get_next_compat)(const char * const**)) |
| 856 | { |
| 857 | const void *data = NULL; |
| 858 | const void *best_data = default_match; |
| 859 | const char *const *compat; |
| 860 | unsigned long dt_root; |
| 861 | unsigned int best_score = ~1, score = 0; |
| 862 | |
| 863 | dt_root = of_get_flat_dt_root(); |
| 864 | while ((data = get_next_compat(&compat))) { |
| 865 | score = of_flat_dt_match(dt_root, compat); |
| 866 | if (score > 0 && score < best_score) { |
| 867 | best_data = data; |
| 868 | best_score = score; |
| 869 | } |
| 870 | } |
| 871 | if (!best_data) { |
| 872 | const char *prop; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 873 | int size; |
Rob Herring | 6a903a2 | 2013-08-27 21:41:56 -0500 | [diff] [blame] | 874 | |
| 875 | pr_err("\n unrecognized device tree list:\n[ "); |
| 876 | |
| 877 | prop = of_get_flat_dt_prop(dt_root, "compatible", &size); |
| 878 | if (prop) { |
| 879 | while (size > 0) { |
| 880 | printk("'%s' ", prop); |
| 881 | size -= strlen(prop) + 1; |
| 882 | prop += strlen(prop) + 1; |
| 883 | } |
| 884 | } |
| 885 | printk("]\n\n"); |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | pr_info("Machine model: %s\n", of_flat_dt_get_machine_name()); |
| 890 | |
| 891 | return best_data; |
| 892 | } |
| 893 | |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 894 | #ifdef CONFIG_BLK_DEV_INITRD |
Ard Biesheuvel | 369bc9a | 2016-02-16 13:52:33 +0100 | [diff] [blame] | 895 | #ifndef __early_init_dt_declare_initrd |
| 896 | static void __early_init_dt_declare_initrd(unsigned long start, |
| 897 | unsigned long end) |
| 898 | { |
| 899 | initrd_start = (unsigned long)__va(start); |
| 900 | initrd_end = (unsigned long)__va(end); |
| 901 | initrd_below_start_ok = 1; |
| 902 | } |
| 903 | #endif |
| 904 | |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 905 | /** |
| 906 | * early_init_dt_check_for_initrd - Decode initrd location from flat tree |
| 907 | * @node: reference to node containing initrd location ('chosen') |
| 908 | */ |
Rob Herring | 29eb45a | 2013-08-30 17:06:53 -0500 | [diff] [blame] | 909 | static void __init early_init_dt_check_for_initrd(unsigned long node) |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 910 | { |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 911 | u64 start, end; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 912 | int len; |
| 913 | const __be32 *prop; |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 914 | |
| 915 | pr_debug("Looking for initrd properties... "); |
| 916 | |
| 917 | prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len); |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 918 | if (!prop) |
| 919 | return; |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 920 | start = of_read_number(prop, len/4); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 921 | |
Jeremy Kerr | 1406bc2 | 2010-01-30 01:31:21 -0700 | [diff] [blame] | 922 | prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len); |
| 923 | if (!prop) |
| 924 | return; |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 925 | end = of_read_number(prop, len/4); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 926 | |
Ard Biesheuvel | 369bc9a | 2016-02-16 13:52:33 +0100 | [diff] [blame] | 927 | __early_init_dt_declare_initrd(start, end); |
Rob Herring | 29eb45a | 2013-08-30 17:06:53 -0500 | [diff] [blame] | 928 | |
Santosh Shilimkar | 374d5c9 | 2013-07-01 14:20:35 -0400 | [diff] [blame] | 929 | pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n", |
| 930 | (unsigned long long)start, (unsigned long long)end); |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 931 | } |
| 932 | #else |
Rob Herring | 29eb45a | 2013-08-30 17:06:53 -0500 | [diff] [blame] | 933 | static inline void early_init_dt_check_for_initrd(unsigned long node) |
Grant Likely | f7b3a83 | 2009-11-24 03:26:58 -0700 | [diff] [blame] | 934 | { |
| 935 | } |
| 936 | #endif /* CONFIG_BLK_DEV_INITRD */ |
| 937 | |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 938 | #ifdef CONFIG_SERIAL_EARLYCON |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 939 | |
Leif Lindholm | d503187 | 2016-09-27 23:54:12 +0300 | [diff] [blame] | 940 | int __init early_init_dt_scan_chosen_stdout(void) |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 941 | { |
| 942 | int offset; |
Peter Hurley | 4d118c9 | 2016-01-16 15:23:42 -0800 | [diff] [blame] | 943 | const char *p, *q, *options = NULL; |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 944 | int l; |
Peter Hurley | 2eaa790 | 2016-01-16 15:23:39 -0800 | [diff] [blame] | 945 | const struct earlycon_id *match; |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 946 | const void *fdt = initial_boot_params; |
| 947 | |
| 948 | offset = fdt_path_offset(fdt, "/chosen"); |
| 949 | if (offset < 0) |
| 950 | offset = fdt_path_offset(fdt, "/chosen@0"); |
| 951 | if (offset < 0) |
| 952 | return -ENOENT; |
| 953 | |
| 954 | p = fdt_getprop(fdt, offset, "stdout-path", &l); |
| 955 | if (!p) |
| 956 | p = fdt_getprop(fdt, offset, "linux,stdout-path", &l); |
| 957 | if (!p || !l) |
| 958 | return -ENOENT; |
| 959 | |
Peter Hurley | 4d118c9 | 2016-01-16 15:23:42 -0800 | [diff] [blame] | 960 | q = strchrnul(p, ':'); |
| 961 | if (*q != '\0') |
| 962 | options = q + 1; |
Peter Hurley | 0fcc286 | 2016-01-16 15:23:48 -0800 | [diff] [blame] | 963 | l = q - p; |
Stefan Agner | 6296ad9 | 2015-10-10 01:29:30 -0700 | [diff] [blame] | 964 | |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 965 | /* Get the node specified by stdout-path */ |
Peter Hurley | 0fcc286 | 2016-01-16 15:23:48 -0800 | [diff] [blame] | 966 | offset = fdt_path_offset_namelen(fdt, p, l); |
| 967 | if (offset < 0) { |
| 968 | pr_warn("earlycon: stdout-path %.*s not found\n", l, p); |
| 969 | return 0; |
| 970 | } |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 971 | |
Peter Hurley | 2eaa790 | 2016-01-16 15:23:39 -0800 | [diff] [blame] | 972 | for (match = __earlycon_table; match < __earlycon_table_end; match++) { |
Peter Hurley | 2eaa790 | 2016-01-16 15:23:39 -0800 | [diff] [blame] | 973 | if (!match->compatible[0]) |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 974 | continue; |
Peter Hurley | 2eaa790 | 2016-01-16 15:23:39 -0800 | [diff] [blame] | 975 | |
| 976 | if (fdt_node_check_compatible(fdt, offset, match->compatible)) |
| 977 | continue; |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 978 | |
Peter Hurley | c90fe9c | 2016-01-16 15:23:44 -0800 | [diff] [blame] | 979 | of_setup_earlycon(match, offset, options); |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 980 | return 0; |
| 981 | } |
| 982 | return -ENODEV; |
| 983 | } |
Rob Herring | fb11ffe | 2014-03-27 08:07:01 -0500 | [diff] [blame] | 984 | #endif |
| 985 | |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 986 | /** |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 987 | * early_init_dt_scan_root - fetch the top level address and size cells |
| 988 | */ |
| 989 | int __init early_init_dt_scan_root(unsigned long node, const char *uname, |
| 990 | int depth, void *data) |
| 991 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 992 | const __be32 *prop; |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 993 | |
| 994 | if (depth != 0) |
| 995 | return 0; |
| 996 | |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 997 | dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 998 | dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 999 | |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 1000 | prop = of_get_flat_dt_prop(node, "#size-cells", NULL); |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 1001 | if (prop) |
| 1002 | dt_root_size_cells = be32_to_cpup(prop); |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 1003 | pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells); |
| 1004 | |
| 1005 | prop = of_get_flat_dt_prop(node, "#address-cells", NULL); |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 1006 | if (prop) |
| 1007 | dt_root_addr_cells = be32_to_cpup(prop); |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 1008 | pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells); |
| 1009 | |
| 1010 | /* break now */ |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 1014 | u64 __init dt_mem_next_cell(int s, const __be32 **cellp) |
Grant Likely | 83f7a06 | 2009-11-24 03:37:56 -0700 | [diff] [blame] | 1015 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 1016 | const __be32 *p = *cellp; |
Grant Likely | 83f7a06 | 2009-11-24 03:37:56 -0700 | [diff] [blame] | 1017 | |
| 1018 | *cellp = p + s; |
| 1019 | return of_read_number(p, s); |
| 1020 | } |
| 1021 | |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1022 | /** |
Frank Rowand | 0ef5adc | 2017-06-20 16:46:28 -0700 | [diff] [blame] | 1023 | * early_init_dt_scan_memory - Look for and parse memory nodes |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1024 | */ |
| 1025 | int __init early_init_dt_scan_memory(unsigned long node, const char *uname, |
| 1026 | int depth, void *data) |
| 1027 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 1028 | const char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 1029 | const __be32 *reg, *endp; |
| 1030 | int l; |
Reza Arbab | 41a9ada | 2016-12-12 16:43:02 -0800 | [diff] [blame] | 1031 | bool hotpluggable; |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1032 | |
| 1033 | /* We are scanning "memory" nodes only */ |
| 1034 | if (type == NULL) { |
| 1035 | /* |
| 1036 | * The longtrail doesn't have a device_type on the |
| 1037 | * /memory node, so look for the node called /memory@0. |
| 1038 | */ |
Leif Lindholm | b44aa25 | 2014-04-17 18:42:01 +0100 | [diff] [blame] | 1039 | if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0) |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1040 | return 0; |
| 1041 | } else if (strcmp(type, "memory") != 0) |
| 1042 | return 0; |
| 1043 | |
| 1044 | reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l); |
| 1045 | if (reg == NULL) |
| 1046 | reg = of_get_flat_dt_prop(node, "reg", &l); |
| 1047 | if (reg == NULL) |
| 1048 | return 0; |
| 1049 | |
| 1050 | endp = reg + (l / sizeof(__be32)); |
Reza Arbab | 41a9ada | 2016-12-12 16:43:02 -0800 | [diff] [blame] | 1051 | hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL); |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1052 | |
Florian Fainelli | c954b36 | 2015-04-12 13:16:26 -0700 | [diff] [blame] | 1053 | pr_debug("memory scan node %s, reg size %d,\n", uname, l); |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1054 | |
| 1055 | while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { |
| 1056 | u64 base, size; |
| 1057 | |
| 1058 | base = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 1059 | size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 1060 | |
| 1061 | if (size == 0) |
| 1062 | continue; |
| 1063 | pr_debug(" - %llx , %llx\n", (unsigned long long)base, |
| 1064 | (unsigned long long)size); |
| 1065 | |
| 1066 | early_init_dt_add_memory_arch(base, size); |
Reza Arbab | 41a9ada | 2016-12-12 16:43:02 -0800 | [diff] [blame] | 1067 | |
| 1068 | if (!hotpluggable) |
| 1069 | continue; |
| 1070 | |
| 1071 | if (early_init_dt_mark_hotplug_memory_arch(base, size)) |
| 1072 | pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n", |
| 1073 | base, base + size); |
Grant Likely | 51975db | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1079 | int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, |
| 1080 | int depth, void *data) |
| 1081 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 1082 | int l; |
| 1083 | const char *p; |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1084 | |
| 1085 | pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
| 1086 | |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 1087 | if (depth != 1 || !data || |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1088 | (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) |
| 1089 | return 0; |
| 1090 | |
| 1091 | early_init_dt_check_for_initrd(node); |
| 1092 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1093 | /* Retrieve command line */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1094 | p = of_get_flat_dt_prop(node, "bootargs", &l); |
| 1095 | if (p != NULL && l > 0) |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 1096 | strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE)); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1097 | |
Benjamin Herrenschmidt | 78b782cb | 2011-09-19 18:50:15 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | * CONFIG_CMDLINE is meant to be a default in case nothing else |
| 1100 | * managed to set the command line, unless CONFIG_CMDLINE_FORCE |
| 1101 | * is set in which case we override whatever was found earlier. |
| 1102 | */ |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1103 | #ifdef CONFIG_CMDLINE |
Max Uvarov | 34b8202 | 2016-04-13 12:52:16 +0300 | [diff] [blame] | 1104 | #if defined(CONFIG_CMDLINE_EXTEND) |
| 1105 | strlcat(data, " ", COMMAND_LINE_SIZE); |
| 1106 | strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
| 1107 | #elif defined(CONFIG_CMDLINE_FORCE) |
| 1108 | strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
| 1109 | #else |
| 1110 | /* No arguments from boot loader, use kernel's cmdl*/ |
Benjamin Herrenschmidt | 78b782cb | 2011-09-19 18:50:15 +0000 | [diff] [blame] | 1111 | if (!((char *)data)[0]) |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 1112 | strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
Max Uvarov | 34b8202 | 2016-04-13 12:52:16 +0300 | [diff] [blame] | 1113 | #endif |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1114 | #endif /* CONFIG_CMDLINE */ |
| 1115 | |
Grant Likely | 85f60ae | 2011-04-29 00:18:16 -0600 | [diff] [blame] | 1116 | pr_debug("Command line is: %s\n", (char*)data); |
Grant Likely | 86e0322 | 2009-12-10 23:42:21 -0700 | [diff] [blame] | 1117 | |
| 1118 | /* break now */ |
| 1119 | return 1; |
| 1120 | } |
| 1121 | |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 1122 | #ifdef CONFIG_HAVE_MEMBLOCK |
Ard Biesheuvel | 270522a | 2016-02-16 13:52:32 +0100 | [diff] [blame] | 1123 | #ifndef MIN_MEMBLOCK_ADDR |
| 1124 | #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET) |
| 1125 | #endif |
Ard Biesheuvel | 8eafeb4 | 2015-08-18 10:34:41 +0100 | [diff] [blame] | 1126 | #ifndef MAX_MEMBLOCK_ADDR |
| 1127 | #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0) |
| 1128 | #endif |
Laura Abbott | 3069f0c | 2014-07-07 17:45:43 -0700 | [diff] [blame] | 1129 | |
Rob Herring | 068f631 | 2013-09-24 22:20:01 -0500 | [diff] [blame] | 1130 | void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) |
| 1131 | { |
Ard Biesheuvel | 270522a | 2016-02-16 13:52:32 +0100 | [diff] [blame] | 1132 | const u64 phys_offset = MIN_MEMBLOCK_ADDR; |
Geert Uytterhoeven | 8f73d4b | 2014-08-20 17:10:31 +0200 | [diff] [blame] | 1133 | |
| 1134 | if (!PAGE_ALIGNED(base)) { |
Ard Biesheuvel | 8cccffc | 2014-10-29 17:09:32 +0100 | [diff] [blame] | 1135 | if (size < PAGE_SIZE - (base & ~PAGE_MASK)) { |
| 1136 | pr_warn("Ignoring memory block 0x%llx - 0x%llx\n", |
| 1137 | base, base + size); |
| 1138 | return; |
| 1139 | } |
Geert Uytterhoeven | 8f73d4b | 2014-08-20 17:10:31 +0200 | [diff] [blame] | 1140 | size -= PAGE_SIZE - (base & ~PAGE_MASK); |
| 1141 | base = PAGE_ALIGN(base); |
| 1142 | } |
Rob Herring | 068f631 | 2013-09-24 22:20:01 -0500 | [diff] [blame] | 1143 | size &= PAGE_MASK; |
Laura Abbott | a67a6ed | 2014-06-19 20:13:38 -0700 | [diff] [blame] | 1144 | |
Ard Biesheuvel | 8eafeb4 | 2015-08-18 10:34:41 +0100 | [diff] [blame] | 1145 | if (base > MAX_MEMBLOCK_ADDR) { |
Laura Abbott | 3069f0c | 2014-07-07 17:45:43 -0700 | [diff] [blame] | 1146 | pr_warning("Ignoring memory block 0x%llx - 0x%llx\n", |
| 1147 | base, base + size); |
| 1148 | return; |
| 1149 | } |
Laura Abbott | a67a6ed | 2014-06-19 20:13:38 -0700 | [diff] [blame] | 1150 | |
Ard Biesheuvel | 8eafeb4 | 2015-08-18 10:34:41 +0100 | [diff] [blame] | 1151 | if (base + size - 1 > MAX_MEMBLOCK_ADDR) { |
Srinivas Kandagatla | 9aacd60 | 2014-09-23 10:59:09 +0100 | [diff] [blame] | 1152 | pr_warning("Ignoring memory range 0x%llx - 0x%llx\n", |
Ard Biesheuvel | 8eafeb4 | 2015-08-18 10:34:41 +0100 | [diff] [blame] | 1153 | ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size); |
| 1154 | size = MAX_MEMBLOCK_ADDR - base + 1; |
Laura Abbott | a67a6ed | 2014-06-19 20:13:38 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Rob Herring | 068f631 | 2013-09-24 22:20:01 -0500 | [diff] [blame] | 1157 | if (base + size < phys_offset) { |
| 1158 | pr_warning("Ignoring memory block 0x%llx - 0x%llx\n", |
| 1159 | base, base + size); |
| 1160 | return; |
| 1161 | } |
| 1162 | if (base < phys_offset) { |
| 1163 | pr_warning("Ignoring memory range 0x%llx - 0x%llx\n", |
| 1164 | base, phys_offset); |
| 1165 | size -= phys_offset - base; |
| 1166 | base = phys_offset; |
| 1167 | } |
| 1168 | memblock_add(base, size); |
| 1169 | } |
| 1170 | |
Reza Arbab | 41a9ada | 2016-12-12 16:43:02 -0800 | [diff] [blame] | 1171 | int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size) |
| 1172 | { |
| 1173 | return memblock_mark_hotplug(base, size); |
| 1174 | } |
| 1175 | |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 1176 | int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, |
| 1177 | phys_addr_t size, bool nomap) |
| 1178 | { |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 1179 | if (nomap) |
| 1180 | return memblock_remove(base, size); |
| 1181 | return memblock_reserve(base, size); |
| 1182 | } |
| 1183 | |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 1184 | #else |
Rob Herring | aefc7ec | 2015-06-18 20:35:46 -0500 | [diff] [blame] | 1185 | void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) |
| 1186 | { |
| 1187 | WARN_ON(1); |
| 1188 | } |
| 1189 | |
Reza Arbab | 41a9ada | 2016-12-12 16:43:02 -0800 | [diff] [blame] | 1190 | int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size) |
| 1191 | { |
| 1192 | return -ENOSYS; |
| 1193 | } |
| 1194 | |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 1195 | int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, |
| 1196 | phys_addr_t size, bool nomap) |
| 1197 | { |
Dmitry V. Krivenok | 78bb2ab | 2015-11-30 23:45:48 +0300 | [diff] [blame] | 1198 | pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n", |
Rob Herring | 1d1a661 | 2014-04-22 12:50:24 -0500 | [diff] [blame] | 1199 | &base, &size, nomap ? " (nomap)" : ""); |
Marek Szyprowski | e8d9d1f | 2014-02-28 14:42:47 +0100 | [diff] [blame] | 1200 | return -ENOSYS; |
| 1201 | } |
Grant Likely | a1727da | 2013-08-28 21:18:32 +0100 | [diff] [blame] | 1202 | #endif |
| 1203 | |
Rob Herring | 0fa1c57 | 2018-01-05 15:32:33 -0600 | [diff] [blame] | 1204 | static void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align) |
| 1205 | { |
| 1206 | return memblock_virt_alloc(size, align); |
| 1207 | } |
| 1208 | |
Laura Abbott | 4972a74 | 2014-07-15 10:03:34 -0700 | [diff] [blame] | 1209 | bool __init early_init_dt_verify(void *params) |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 1210 | { |
| 1211 | if (!params) |
| 1212 | return false; |
| 1213 | |
Bjorn Helgaas | 50ba08f | 2014-10-29 12:15:00 -0600 | [diff] [blame] | 1214 | /* check device tree validity */ |
| 1215 | if (fdt_check_header(params)) |
| 1216 | return false; |
| 1217 | |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 1218 | /* Setup flat device-tree pointer */ |
| 1219 | initial_boot_params = params; |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 1220 | of_fdt_crc32 = crc32_be(~0, initial_boot_params, |
| 1221 | fdt_totalsize(initial_boot_params)); |
Laura Abbott | 4972a74 | 2014-07-15 10:03:34 -0700 | [diff] [blame] | 1222 | return true; |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | void __init early_init_dt_scan_nodes(void) |
| 1227 | { |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 1228 | /* Retrieve various information from the /chosen node */ |
| 1229 | of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line); |
| 1230 | |
| 1231 | /* Initialize {size,address}-cells info */ |
| 1232 | of_scan_flat_dt(early_init_dt_scan_root, NULL); |
| 1233 | |
| 1234 | /* Setup memory, calling early_init_dt_add_memory_arch */ |
| 1235 | of_scan_flat_dt(early_init_dt_scan_memory, NULL); |
Laura Abbott | 4972a74 | 2014-07-15 10:03:34 -0700 | [diff] [blame] | 1236 | } |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 1237 | |
Laura Abbott | 4972a74 | 2014-07-15 10:03:34 -0700 | [diff] [blame] | 1238 | bool __init early_init_dt_scan(void *params) |
| 1239 | { |
| 1240 | bool status; |
| 1241 | |
| 1242 | status = early_init_dt_verify(params); |
| 1243 | if (!status) |
| 1244 | return false; |
| 1245 | |
| 1246 | early_init_dt_scan_nodes(); |
Rob Herring | 0288ffcb | 2013-08-26 09:47:40 -0500 | [diff] [blame] | 1247 | return true; |
| 1248 | } |
| 1249 | |
Grant Likely | f00abd9 | 2009-11-24 03:27:10 -0700 | [diff] [blame] | 1250 | /** |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 1251 | * unflatten_device_tree - create tree of device_nodes from flat blob |
| 1252 | * |
| 1253 | * unflattens the device-tree passed by the firmware, creating the |
| 1254 | * tree of struct device_node. It also fills the "name" and "type" |
| 1255 | * pointers of the nodes so the normal device-tree walking functions |
| 1256 | * can be used. |
| 1257 | */ |
| 1258 | void __init unflatten_device_tree(void) |
| 1259 | { |
Gavin Shan | c426323 | 2016-05-03 23:22:50 +1000 | [diff] [blame] | 1260 | __unflatten_device_tree(initial_boot_params, NULL, &of_root, |
Michal Suchanek | 1d1bde5 | 2016-07-19 00:01:12 +0200 | [diff] [blame] | 1261 | early_init_dt_alloc_memory_arch, false); |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 1262 | |
Robert P. J. Day | 4c7d636 | 2013-05-30 05:38:08 -0400 | [diff] [blame] | 1263 | /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */ |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1264 | of_alias_scan(early_init_dt_alloc_memory_arch); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 1265 | |
| 1266 | unittest_unflatten_overlay_base(); |
Grant Likely | 41f8800 | 2009-11-23 20:07:01 -0700 | [diff] [blame] | 1267 | } |
Stephen Neuendorffer | e6ce132 | 2010-11-18 15:54:56 -0800 | [diff] [blame] | 1268 | |
Rob Herring | a8bf752 | 2013-08-26 11:22:45 -0500 | [diff] [blame] | 1269 | /** |
| 1270 | * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob |
| 1271 | * |
| 1272 | * Copies and unflattens the device-tree passed by the firmware, creating the |
| 1273 | * tree of struct device_node. It also fills the "name" and "type" |
| 1274 | * pointers of the nodes so the normal device-tree walking functions |
| 1275 | * can be used. This should only be used when the FDT memory has not been |
| 1276 | * reserved such is the case when the FDT is built-in to the kernel init |
| 1277 | * section. If the FDT memory is reserved already then unflatten_device_tree |
| 1278 | * should be used instead. |
| 1279 | */ |
| 1280 | void __init unflatten_and_copy_device_tree(void) |
| 1281 | { |
James Hogan | 6f041e9 | 2013-11-21 13:44:14 +0000 | [diff] [blame] | 1282 | int size; |
| 1283 | void *dt; |
| 1284 | |
| 1285 | if (!initial_boot_params) { |
| 1286 | pr_warn("No valid device tree found, continuing without\n"); |
| 1287 | return; |
| 1288 | } |
| 1289 | |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 1290 | size = fdt_totalsize(initial_boot_params); |
James Hogan | 6f041e9 | 2013-11-21 13:44:14 +0000 | [diff] [blame] | 1291 | dt = early_init_dt_alloc_memory_arch(size, |
Rob Herring | c972de1 | 2014-04-01 22:48:01 -0500 | [diff] [blame] | 1292 | roundup_pow_of_two(FDT_V17_SIZE)); |
Rob Herring | a8bf752 | 2013-08-26 11:22:45 -0500 | [diff] [blame] | 1293 | |
| 1294 | if (dt) { |
| 1295 | memcpy(dt, initial_boot_params, size); |
| 1296 | initial_boot_params = dt; |
| 1297 | } |
| 1298 | unflatten_device_tree(); |
| 1299 | } |
| 1300 | |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 1301 | #ifdef CONFIG_SYSFS |
| 1302 | static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj, |
| 1303 | struct bin_attribute *bin_attr, |
| 1304 | char *buf, loff_t off, size_t count) |
Rob Herring | b0a6fb3 | 2014-04-02 16:56:48 -0500 | [diff] [blame] | 1305 | { |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 1306 | memcpy(buf, initial_boot_params + off, count); |
| 1307 | return count; |
Rob Herring | b0a6fb3 | 2014-04-02 16:56:48 -0500 | [diff] [blame] | 1308 | } |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 1309 | |
| 1310 | static int __init of_fdt_raw_init(void) |
| 1311 | { |
| 1312 | static struct bin_attribute of_fdt_raw_attr = |
| 1313 | __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0); |
| 1314 | |
| 1315 | if (!initial_boot_params) |
| 1316 | return 0; |
| 1317 | |
| 1318 | if (of_fdt_crc32 != crc32_be(~0, initial_boot_params, |
| 1319 | fdt_totalsize(initial_boot_params))) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 1320 | pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n"); |
Ard Biesheuvel | 08d53aa | 2014-11-14 18:05:35 +0100 | [diff] [blame] | 1321 | return 0; |
| 1322 | } |
| 1323 | of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params); |
| 1324 | return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr); |
| 1325 | } |
| 1326 | late_initcall(of_fdt_raw_init); |
Rob Herring | b0a6fb3 | 2014-04-02 16:56:48 -0500 | [diff] [blame] | 1327 | #endif |
| 1328 | |
Stephen Neuendorffer | e6ce132 | 2010-11-18 15:54:56 -0800 | [diff] [blame] | 1329 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |