]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/of/fdt.c
of/flattree: Add non-boottime device tree functions
[mirror_ubuntu-jammy-kernel.git] / drivers / of / fdt.c
CommitLineData
e169cfbe
GL
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
41f88009 12#include <linux/kernel.h>
f7b3a835 13#include <linux/initrd.h>
e169cfbe
GL
14#include <linux/of.h>
15#include <linux/of_fdt.h>
4ef7b373
JK
16#include <linux/string.h>
17#include <linux/errno.h>
51975db0 18
86e03221
GL
19#ifdef CONFIG_PPC
20#include <asm/machdep.h>
21#endif /* CONFIG_PPC */
22
4ef7b373
JK
23#include <asm/page.h>
24
9706a36e
SN
25char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
26{
27 return ((char *)blob) +
28 be32_to_cpu(blob->off_dt_strings) + offset;
29}
30
31/**
32 * of_fdt_get_property - Given a node in the given flat blob, return
33 * the property ptr
34 */
35void *of_fdt_get_property(struct boot_param_header *blob,
36 unsigned long node, const char *name,
37 unsigned long *size)
38{
39 unsigned long p = node;
40
41 do {
42 u32 tag = be32_to_cpup((__be32 *)p);
43 u32 sz, noff;
44 const char *nstr;
45
46 p += 4;
47 if (tag == OF_DT_NOP)
48 continue;
49 if (tag != OF_DT_PROP)
50 return NULL;
51
52 sz = be32_to_cpup((__be32 *)p);
53 noff = be32_to_cpup((__be32 *)(p + 4));
54 p += 8;
55 if (be32_to_cpu(blob->version) < 0x10)
56 p = ALIGN(p, sz >= 8 ? 8 : 4);
57
58 nstr = of_fdt_get_string(blob, noff);
59 if (nstr == NULL) {
60 pr_warning("Can't find property index name !\n");
61 return NULL;
62 }
63 if (strcmp(name, nstr) == 0) {
64 if (size)
65 *size = sz;
66 return (void *)p;
67 }
68 p += sz;
69 p = ALIGN(p, 4);
70 } while (1);
71}
72
73/**
74 * of_fdt_is_compatible - Return true if given node from the given blob has
75 * compat in its compatible list
76 * @blob: A device tree blob
77 * @node: node to test
78 * @compat: compatible string to compare with compatible list.
79 */
80int of_fdt_is_compatible(struct boot_param_header *blob,
81 unsigned long node, const char *compat)
82{
83 const char *cp;
84 unsigned long cplen, l;
85
86 cp = of_fdt_get_property(blob, node, "compatible", &cplen);
87 if (cp == NULL)
88 return 0;
89 while (cplen > 0) {
90 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
91 return 1;
92 l = strlen(cp) + 1;
93 cp += l;
94 cplen -= l;
95 }
96
97 return 0;
98}
99
100/* Everything below here references initial_boot_params directly. */
f00abd94
GL
101int __initdata dt_root_addr_cells;
102int __initdata dt_root_size_cells;
103
e169cfbe
GL
104struct boot_param_header *initial_boot_params;
105
e6ce1324
SN
106#ifdef CONFIG_OF_EARLY_FLATTREE
107
c8cb7a59
GL
108/**
109 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
110 * @it: callback function
111 * @data: context data pointer
112 *
113 * This function is used to scan the flattened device-tree, it is
114 * used to extract the memory information at boot before we can
115 * unflatten the tree
116 */
117int __init of_scan_flat_dt(int (*it)(unsigned long node,
118 const char *uname, int depth,
119 void *data),
120 void *data)
121{
122 unsigned long p = ((unsigned long)initial_boot_params) +
087f79c4 123 be32_to_cpu(initial_boot_params->off_dt_struct);
c8cb7a59
GL
124 int rc = 0;
125 int depth = -1;
126
127 do {
33714881 128 u32 tag = be32_to_cpup((__be32 *)p);
c8cb7a59
GL
129 char *pathp;
130
131 p += 4;
132 if (tag == OF_DT_END_NODE) {
133 depth--;
134 continue;
135 }
136 if (tag == OF_DT_NOP)
137 continue;
138 if (tag == OF_DT_END)
139 break;
140 if (tag == OF_DT_PROP) {
33714881 141 u32 sz = be32_to_cpup((__be32 *)p);
c8cb7a59 142 p += 8;
087f79c4 143 if (be32_to_cpu(initial_boot_params->version) < 0x10)
f1d4c3a7 144 p = ALIGN(p, sz >= 8 ? 8 : 4);
c8cb7a59 145 p += sz;
f1d4c3a7 146 p = ALIGN(p, 4);
c8cb7a59
GL
147 continue;
148 }
149 if (tag != OF_DT_BEGIN_NODE) {
150 pr_err("Invalid tag %x in flat device tree!\n", tag);
151 return -EINVAL;
152 }
153 depth++;
154 pathp = (char *)p;
f1d4c3a7 155 p = ALIGN(p + strlen(pathp) + 1, 4);
c8cb7a59
GL
156 if ((*pathp) == '/') {
157 char *lp, *np;
158 for (lp = NULL, np = pathp; *np; np++)
159 if ((*np) == '/')
160 lp = np+1;
161 if (lp != NULL)
162 pathp = lp;
163 }
164 rc = it(p, pathp, depth, data);
165 if (rc != 0)
166 break;
167 } while (1);
168
169 return rc;
170}
819d2819
GL
171
172/**
173 * of_get_flat_dt_root - find the root node in the flat blob
174 */
175unsigned long __init of_get_flat_dt_root(void)
176{
177 unsigned long p = ((unsigned long)initial_boot_params) +
087f79c4 178 be32_to_cpu(initial_boot_params->off_dt_struct);
819d2819 179
33714881 180 while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
819d2819 181 p += 4;
33714881 182 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
819d2819 183 p += 4;
f1d4c3a7 184 return ALIGN(p + strlen((char *)p) + 1, 4);
819d2819
GL
185}
186
ca900cfa
GL
187/**
188 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
189 *
190 * This function can be used within scan_flattened_dt callback to get
191 * access to properties
192 */
193void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
194 unsigned long *size)
195{
9706a36e 196 return of_fdt_get_property(initial_boot_params, node, name, size);
ca900cfa
GL
197}
198
00e38efd
GL
199/**
200 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
201 * @node: node to test
202 * @compat: compatible string to compare with compatible list.
203 */
204int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
205{
9706a36e 206 return of_fdt_is_compatible(initial_boot_params, node, compat);
00e38efd
GL
207}
208
bbd33931
GL
209static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
210 unsigned long align)
211{
212 void *res;
213
f1d4c3a7 214 *mem = ALIGN(*mem, align);
bbd33931
GL
215 res = (void *)*mem;
216 *mem += size;
217
218 return res;
219}
220
221/**
222 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
223 * @p: pointer to node in flat tree
224 * @dad: Parent struct device_node
225 * @allnextpp: pointer to ->allnext from last allocated device_node
226 * @fpsize: Size of the node path up at the current depth.
227 */
228unsigned long __init unflatten_dt_node(unsigned long mem,
229 unsigned long *p,
230 struct device_node *dad,
231 struct device_node ***allnextpp,
232 unsigned long fpsize)
233{
234 struct device_node *np;
235 struct property *pp, **prev_pp = NULL;
236 char *pathp;
237 u32 tag;
238 unsigned int l, allocl;
239 int has_name = 0;
240 int new_format = 0;
241
33714881 242 tag = be32_to_cpup((__be32 *)(*p));
bbd33931
GL
243 if (tag != OF_DT_BEGIN_NODE) {
244 pr_err("Weird tag at start of node: %x\n", tag);
245 return mem;
246 }
247 *p += 4;
248 pathp = (char *)*p;
249 l = allocl = strlen(pathp) + 1;
f1d4c3a7 250 *p = ALIGN(*p + l, 4);
bbd33931
GL
251
252 /* version 0x10 has a more compact unit name here instead of the full
253 * path. we accumulate the full path size using "fpsize", we'll rebuild
254 * it later. We detect this because the first character of the name is
255 * not '/'.
256 */
257 if ((*pathp) != '/') {
258 new_format = 1;
259 if (fpsize == 0) {
260 /* root node: special case. fpsize accounts for path
261 * plus terminating zero. root node only has '/', so
262 * fpsize should be 2, but we want to avoid the first
263 * level nodes to have two '/' so we use fpsize 1 here
264 */
265 fpsize = 1;
266 allocl = 2;
267 } else {
268 /* account for '/' and path size minus terminal 0
269 * already in 'l'
270 */
271 fpsize += l;
272 allocl = fpsize;
273 }
274 }
275
276 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
277 __alignof__(struct device_node));
278 if (allnextpp) {
279 memset(np, 0, sizeof(*np));
280 np->full_name = ((char *)np) + sizeof(struct device_node);
281 if (new_format) {
282 char *fn = np->full_name;
283 /* rebuild full path for new format */
284 if (dad && dad->parent) {
285 strcpy(fn, dad->full_name);
286#ifdef DEBUG
287 if ((strlen(fn) + l + 1) != allocl) {
288 pr_debug("%s: p: %d, l: %d, a: %d\n",
289 pathp, (int)strlen(fn),
290 l, allocl);
291 }
292#endif
293 fn += strlen(fn);
294 }
295 *(fn++) = '/';
296 memcpy(fn, pathp, l);
297 } else
298 memcpy(np->full_name, pathp, l);
299 prev_pp = &np->properties;
300 **allnextpp = np;
301 *allnextpp = &np->allnext;
302 if (dad != NULL) {
303 np->parent = dad;
304 /* we temporarily use the next field as `last_child'*/
305 if (dad->next == NULL)
306 dad->child = np;
307 else
308 dad->next->sibling = np;
309 dad->next = np;
310 }
311 kref_init(&np->kref);
312 }
313 while (1) {
314 u32 sz, noff;
315 char *pname;
316
33714881 317 tag = be32_to_cpup((__be32 *)(*p));
bbd33931
GL
318 if (tag == OF_DT_NOP) {
319 *p += 4;
320 continue;
321 }
322 if (tag != OF_DT_PROP)
323 break;
324 *p += 4;
33714881
JK
325 sz = be32_to_cpup((__be32 *)(*p));
326 noff = be32_to_cpup((__be32 *)((*p) + 4));
bbd33931 327 *p += 8;
087f79c4 328 if (be32_to_cpu(initial_boot_params->version) < 0x10)
f1d4c3a7 329 *p = ALIGN(*p, sz >= 8 ? 8 : 4);
bbd33931 330
9706a36e 331 pname = of_fdt_get_string(initial_boot_params, noff);
bbd33931
GL
332 if (pname == NULL) {
333 pr_info("Can't find property name in list !\n");
334 break;
335 }
336 if (strcmp(pname, "name") == 0)
337 has_name = 1;
338 l = strlen(pname) + 1;
339 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
340 __alignof__(struct property));
341 if (allnextpp) {
04b954a6
DG
342 /* We accept flattened tree phandles either in
343 * ePAPR-style "phandle" properties, or the
344 * legacy "linux,phandle" properties. If both
345 * appear and have different values, things
346 * will get weird. Don't do that. */
347 if ((strcmp(pname, "phandle") == 0) ||
348 (strcmp(pname, "linux,phandle") == 0)) {
6016a363 349 if (np->phandle == 0)
9a6b2e58 350 np->phandle = be32_to_cpup((__be32*)*p);
bbd33931 351 }
04b954a6
DG
352 /* And we process the "ibm,phandle" property
353 * used in pSeries dynamic device tree
354 * stuff */
bbd33931 355 if (strcmp(pname, "ibm,phandle") == 0)
9a6b2e58 356 np->phandle = be32_to_cpup((__be32 *)*p);
bbd33931
GL
357 pp->name = pname;
358 pp->length = sz;
359 pp->value = (void *)*p;
360 *prev_pp = pp;
361 prev_pp = &pp->next;
362 }
f1d4c3a7 363 *p = ALIGN((*p) + sz, 4);
bbd33931
GL
364 }
365 /* with version 0x10 we may not have the name property, recreate
366 * it here from the unit name if absent
367 */
368 if (!has_name) {
369 char *p1 = pathp, *ps = pathp, *pa = NULL;
370 int sz;
371
372 while (*p1) {
373 if ((*p1) == '@')
374 pa = p1;
375 if ((*p1) == '/')
376 ps = p1 + 1;
377 p1++;
378 }
379 if (pa < ps)
380 pa = p1;
381 sz = (pa - ps) + 1;
382 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
383 __alignof__(struct property));
384 if (allnextpp) {
385 pp->name = "name";
386 pp->length = sz;
387 pp->value = pp + 1;
388 *prev_pp = pp;
389 prev_pp = &pp->next;
390 memcpy(pp->value, ps, sz - 1);
391 ((char *)pp->value)[sz - 1] = 0;
392 pr_debug("fixed up name for %s -> %s\n", pathp,
393 (char *)pp->value);
394 }
395 }
396 if (allnextpp) {
397 *prev_pp = NULL;
398 np->name = of_get_property(np, "name", NULL);
399 np->type = of_get_property(np, "device_type", NULL);
400
401 if (!np->name)
402 np->name = "<NULL>";
403 if (!np->type)
404 np->type = "<NULL>";
405 }
7f809e1f
JG
406 while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
407 if (tag == OF_DT_NOP)
408 *p += 4;
409 else
410 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
33714881 411 tag = be32_to_cpup((__be32 *)(*p));
bbd33931
GL
412 }
413 if (tag != OF_DT_END_NODE) {
414 pr_err("Weird tag at end of node: %x\n", tag);
415 return mem;
416 }
417 *p += 4;
418 return mem;
419}
41f88009 420
f7b3a835
GL
421#ifdef CONFIG_BLK_DEV_INITRD
422/**
423 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
424 * @node: reference to node containing initrd location ('chosen')
425 */
426void __init early_init_dt_check_for_initrd(unsigned long node)
427{
1406bc2f 428 unsigned long start, end, len;
33714881 429 __be32 *prop;
f7b3a835
GL
430
431 pr_debug("Looking for initrd properties... ");
432
433 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
1406bc2f
JK
434 if (!prop)
435 return;
436 start = of_read_ulong(prop, len/4);
437
438 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
439 if (!prop)
440 return;
441 end = of_read_ulong(prop, len/4);
f7b3a835 442
1406bc2f
JK
443 early_init_dt_setup_initrd_arch(start, end);
444 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", start, end);
f7b3a835
GL
445}
446#else
447inline void early_init_dt_check_for_initrd(unsigned long node)
448{
449}
450#endif /* CONFIG_BLK_DEV_INITRD */
451
f00abd94
GL
452/**
453 * early_init_dt_scan_root - fetch the top level address and size cells
454 */
455int __init early_init_dt_scan_root(unsigned long node, const char *uname,
456 int depth, void *data)
457{
33714881 458 __be32 *prop;
f00abd94
GL
459
460 if (depth != 0)
461 return 0;
462
33714881
JK
463 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
464 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
465
f00abd94 466 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
33714881
JK
467 if (prop)
468 dt_root_size_cells = be32_to_cpup(prop);
f00abd94
GL
469 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
470
471 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
33714881
JK
472 if (prop)
473 dt_root_addr_cells = be32_to_cpup(prop);
f00abd94
GL
474 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
475
476 /* break now */
477 return 1;
478}
479
2e89e685 480u64 __init dt_mem_next_cell(int s, __be32 **cellp)
83f7a06e 481{
2e89e685 482 __be32 *p = *cellp;
83f7a06e
GL
483
484 *cellp = p + s;
485 return of_read_number(p, s);
486}
487
51975db0
GL
488/**
489 * early_init_dt_scan_memory - Look for an parse memory nodes
490 */
491int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
492 int depth, void *data)
493{
494 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
495 __be32 *reg, *endp;
496 unsigned long l;
497
498 /* We are scanning "memory" nodes only */
499 if (type == NULL) {
500 /*
501 * The longtrail doesn't have a device_type on the
502 * /memory node, so look for the node called /memory@0.
503 */
504 if (depth != 1 || strcmp(uname, "memory@0") != 0)
505 return 0;
506 } else if (strcmp(type, "memory") != 0)
507 return 0;
508
509 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
510 if (reg == NULL)
511 reg = of_get_flat_dt_prop(node, "reg", &l);
512 if (reg == NULL)
513 return 0;
514
515 endp = reg + (l / sizeof(__be32));
516
517 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
518 uname, l, reg[0], reg[1], reg[2], reg[3]);
519
520 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
521 u64 base, size;
522
523 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
524 size = dt_mem_next_cell(dt_root_size_cells, &reg);
525
526 if (size == 0)
527 continue;
528 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
529 (unsigned long long)size);
530
531 early_init_dt_add_memory_arch(base, size);
532 }
533
534 return 0;
535}
536
86e03221
GL
537int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
538 int depth, void *data)
539{
540 unsigned long l;
541 char *p;
542
543 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
544
545 if (depth != 1 ||
546 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
547 return 0;
548
549 early_init_dt_check_for_initrd(node);
550
551 /* Retreive command line */
552 p = of_get_flat_dt_prop(node, "bootargs", &l);
553 if (p != NULL && l > 0)
554 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
555
556#ifdef CONFIG_CMDLINE
557#ifndef CONFIG_CMDLINE_FORCE
558 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
559#endif
560 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
561#endif /* CONFIG_CMDLINE */
562
86e03221
GL
563 pr_debug("Command line is: %s\n", cmd_line);
564
565 /* break now */
566 return 1;
567}
568
41f88009
GL
569/**
570 * unflatten_device_tree - create tree of device_nodes from flat blob
571 *
572 * unflattens the device-tree passed by the firmware, creating the
573 * tree of struct device_node. It also fills the "name" and "type"
574 * pointers of the nodes so the normal device-tree walking functions
575 * can be used.
576 */
577void __init unflatten_device_tree(void)
578{
579 unsigned long start, mem, size;
580 struct device_node **allnextp = &allnodes;
581
582 pr_debug(" -> unflatten_device_tree()\n");
583
8bfe9b5c
GL
584 if (!initial_boot_params) {
585 pr_debug("No device tree pointer\n");
586 return;
587 }
588
589 pr_debug("Unflattening device tree:\n");
590 pr_debug("magic: %08x\n", be32_to_cpu(initial_boot_params->magic));
591 pr_debug("size: %08x\n", be32_to_cpu(initial_boot_params->totalsize));
592 pr_debug("version: %08x\n", be32_to_cpu(initial_boot_params->version));
593
594 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
595 pr_err("Invalid device tree blob header\n");
596 return;
597 }
598
41f88009
GL
599 /* First pass, scan for size */
600 start = ((unsigned long)initial_boot_params) +
087f79c4 601 be32_to_cpu(initial_boot_params->off_dt_struct);
41f88009
GL
602 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
603 size = (size | 3) + 1;
604
605 pr_debug(" size is %lx, allocating...\n", size);
606
607 /* Allocate memory for the expanded device tree */
4ef7b373
JK
608 mem = early_init_dt_alloc_memory_arch(size + 4,
609 __alignof__(struct device_node));
41f88009
GL
610 mem = (unsigned long) __va(mem);
611
33714881 612 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
41f88009
GL
613
614 pr_debug(" unflattening %lx...\n", mem);
615
616 /* Second pass, do actual unflattening */
617 start = ((unsigned long)initial_boot_params) +
087f79c4 618 be32_to_cpu(initial_boot_params->off_dt_struct);
41f88009 619 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
33714881 620 if (be32_to_cpup((__be32 *)start) != OF_DT_END)
41f88009 621 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
33714881 622 if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
41f88009 623 pr_warning("End of tree marker overwritten: %08x\n",
33714881 624 be32_to_cpu(((__be32 *)mem)[size / 4]));
41f88009
GL
625 *allnextp = NULL;
626
627 /* Get pointer to OF "/chosen" node for use everywhere */
628 of_chosen = of_find_node_by_path("/chosen");
629 if (of_chosen == NULL)
630 of_chosen = of_find_node_by_path("/chosen@0");
631
632 pr_debug(" <- unflatten_device_tree()\n");
633}
e6ce1324
SN
634
635#endif /* CONFIG_OF_EARLY_FLATTREE */