Linux 4.19.133
[linux/fpc-iii.git] / scripts / dtc / livetree.c
blob6e4c367f54b3a8de0289d9677519a81b63ef5aa4
1 /*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
21 #include "dtc.h"
24 * Tree building functions
27 void add_label(struct label **labels, char *label)
29 struct label *new;
31 /* Make sure the label isn't already there */
32 for_each_label_withdel(*labels, new)
33 if (streq(new->label, label)) {
34 new->deleted = 0;
35 return;
38 new = xmalloc(sizeof(*new));
39 memset(new, 0, sizeof(*new));
40 new->label = label;
41 new->next = *labels;
42 *labels = new;
45 void delete_labels(struct label **labels)
47 struct label *label;
49 for_each_label(*labels, label)
50 label->deleted = 1;
53 struct property *build_property(char *name, struct data val)
55 struct property *new = xmalloc(sizeof(*new));
57 memset(new, 0, sizeof(*new));
59 new->name = name;
60 new->val = val;
62 return new;
65 struct property *build_property_delete(char *name)
67 struct property *new = xmalloc(sizeof(*new));
69 memset(new, 0, sizeof(*new));
71 new->name = name;
72 new->deleted = 1;
74 return new;
77 struct property *chain_property(struct property *first, struct property *list)
79 assert(first->next == NULL);
81 first->next = list;
82 return first;
85 struct property *reverse_properties(struct property *first)
87 struct property *p = first;
88 struct property *head = NULL;
89 struct property *next;
91 while (p) {
92 next = p->next;
93 p->next = head;
94 head = p;
95 p = next;
97 return head;
100 struct node *build_node(struct property *proplist, struct node *children)
102 struct node *new = xmalloc(sizeof(*new));
103 struct node *child;
105 memset(new, 0, sizeof(*new));
107 new->proplist = reverse_properties(proplist);
108 new->children = children;
110 for_each_child(new, child) {
111 child->parent = new;
114 return new;
117 struct node *build_node_delete(void)
119 struct node *new = xmalloc(sizeof(*new));
121 memset(new, 0, sizeof(*new));
123 new->deleted = 1;
125 return new;
128 struct node *name_node(struct node *node, char *name)
130 assert(node->name == NULL);
132 node->name = name;
134 return node;
137 struct node *omit_node_if_unused(struct node *node)
139 node->omit_if_unused = 1;
141 return node;
144 struct node *reference_node(struct node *node)
146 node->is_referenced = 1;
148 return node;
151 struct node *merge_nodes(struct node *old_node, struct node *new_node)
153 struct property *new_prop, *old_prop;
154 struct node *new_child, *old_child;
155 struct label *l;
157 old_node->deleted = 0;
159 /* Add new node labels to old node */
160 for_each_label_withdel(new_node->labels, l)
161 add_label(&old_node->labels, l->label);
163 /* Move properties from the new node to the old node. If there
164 * is a collision, replace the old value with the new */
165 while (new_node->proplist) {
166 /* Pop the property off the list */
167 new_prop = new_node->proplist;
168 new_node->proplist = new_prop->next;
169 new_prop->next = NULL;
171 if (new_prop->deleted) {
172 delete_property_by_name(old_node, new_prop->name);
173 free(new_prop);
174 continue;
177 /* Look for a collision, set new value if there is */
178 for_each_property_withdel(old_node, old_prop) {
179 if (streq(old_prop->name, new_prop->name)) {
180 /* Add new labels to old property */
181 for_each_label_withdel(new_prop->labels, l)
182 add_label(&old_prop->labels, l->label);
184 old_prop->val = new_prop->val;
185 old_prop->deleted = 0;
186 free(new_prop);
187 new_prop = NULL;
188 break;
192 /* if no collision occurred, add property to the old node. */
193 if (new_prop)
194 add_property(old_node, new_prop);
197 /* Move the override child nodes into the primary node. If
198 * there is a collision, then merge the nodes. */
199 while (new_node->children) {
200 /* Pop the child node off the list */
201 new_child = new_node->children;
202 new_node->children = new_child->next_sibling;
203 new_child->parent = NULL;
204 new_child->next_sibling = NULL;
206 if (new_child->deleted) {
207 delete_node_by_name(old_node, new_child->name);
208 free(new_child);
209 continue;
212 /* Search for a collision. Merge if there is */
213 for_each_child_withdel(old_node, old_child) {
214 if (streq(old_child->name, new_child->name)) {
215 merge_nodes(old_child, new_child);
216 new_child = NULL;
217 break;
221 /* if no collision occurred, add child to the old node. */
222 if (new_child)
223 add_child(old_node, new_child);
226 /* The new node contents are now merged into the old node. Free
227 * the new node. */
228 free(new_node);
230 return old_node;
233 struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
235 static unsigned int next_orphan_fragment = 0;
236 struct node *node;
237 struct property *p;
238 struct data d = empty_data;
239 char *name;
241 if (ref[0] == '/') {
242 d = data_append_data(d, ref, strlen(ref) + 1);
244 p = build_property("target-path", d);
245 } else {
246 d = data_add_marker(d, REF_PHANDLE, ref);
247 d = data_append_integer(d, 0xffffffff, 32);
249 p = build_property("target", d);
252 xasprintf(&name, "fragment@%u",
253 next_orphan_fragment++);
254 name_node(new_node, "__overlay__");
255 node = build_node(p, new_node);
256 name_node(node, name);
258 add_child(dt, node);
259 return dt;
262 struct node *chain_node(struct node *first, struct node *list)
264 assert(first->next_sibling == NULL);
266 first->next_sibling = list;
267 return first;
270 void add_property(struct node *node, struct property *prop)
272 struct property **p;
274 prop->next = NULL;
276 p = &node->proplist;
277 while (*p)
278 p = &((*p)->next);
280 *p = prop;
283 void delete_property_by_name(struct node *node, char *name)
285 struct property *prop = node->proplist;
287 while (prop) {
288 if (streq(prop->name, name)) {
289 delete_property(prop);
290 return;
292 prop = prop->next;
296 void delete_property(struct property *prop)
298 prop->deleted = 1;
299 delete_labels(&prop->labels);
302 void add_child(struct node *parent, struct node *child)
304 struct node **p;
306 child->next_sibling = NULL;
307 child->parent = parent;
309 p = &parent->children;
310 while (*p)
311 p = &((*p)->next_sibling);
313 *p = child;
316 void delete_node_by_name(struct node *parent, char *name)
318 struct node *node = parent->children;
320 while (node) {
321 if (streq(node->name, name)) {
322 delete_node(node);
323 return;
325 node = node->next_sibling;
329 void delete_node(struct node *node)
331 struct property *prop;
332 struct node *child;
334 node->deleted = 1;
335 for_each_child(node, child)
336 delete_node(child);
337 for_each_property(node, prop)
338 delete_property(prop);
339 delete_labels(&node->labels);
342 void append_to_property(struct node *node,
343 char *name, const void *data, int len)
345 struct data d;
346 struct property *p;
348 p = get_property(node, name);
349 if (p) {
350 d = data_append_data(p->val, data, len);
351 p->val = d;
352 } else {
353 d = data_append_data(empty_data, data, len);
354 p = build_property(name, d);
355 add_property(node, p);
359 struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
361 struct reserve_info *new = xmalloc(sizeof(*new));
363 memset(new, 0, sizeof(*new));
365 new->address = address;
366 new->size = size;
368 return new;
371 struct reserve_info *chain_reserve_entry(struct reserve_info *first,
372 struct reserve_info *list)
374 assert(first->next == NULL);
376 first->next = list;
377 return first;
380 struct reserve_info *add_reserve_entry(struct reserve_info *list,
381 struct reserve_info *new)
383 struct reserve_info *last;
385 new->next = NULL;
387 if (! list)
388 return new;
390 for (last = list; last->next; last = last->next)
393 last->next = new;
395 return list;
398 struct dt_info *build_dt_info(unsigned int dtsflags,
399 struct reserve_info *reservelist,
400 struct node *tree, uint32_t boot_cpuid_phys)
402 struct dt_info *dti;
404 dti = xmalloc(sizeof(*dti));
405 dti->dtsflags = dtsflags;
406 dti->reservelist = reservelist;
407 dti->dt = tree;
408 dti->boot_cpuid_phys = boot_cpuid_phys;
410 return dti;
414 * Tree accessor functions
417 const char *get_unitname(struct node *node)
419 if (node->name[node->basenamelen] == '\0')
420 return "";
421 else
422 return node->name + node->basenamelen + 1;
425 struct property *get_property(struct node *node, const char *propname)
427 struct property *prop;
429 for_each_property(node, prop)
430 if (streq(prop->name, propname))
431 return prop;
433 return NULL;
436 cell_t propval_cell(struct property *prop)
438 assert(prop->val.len == sizeof(cell_t));
439 return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
442 cell_t propval_cell_n(struct property *prop, int n)
444 assert(prop->val.len / sizeof(cell_t) >= n);
445 return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
448 struct property *get_property_by_label(struct node *tree, const char *label,
449 struct node **node)
451 struct property *prop;
452 struct node *c;
454 *node = tree;
456 for_each_property(tree, prop) {
457 struct label *l;
459 for_each_label(prop->labels, l)
460 if (streq(l->label, label))
461 return prop;
464 for_each_child(tree, c) {
465 prop = get_property_by_label(c, label, node);
466 if (prop)
467 return prop;
470 *node = NULL;
471 return NULL;
474 struct marker *get_marker_label(struct node *tree, const char *label,
475 struct node **node, struct property **prop)
477 struct marker *m;
478 struct property *p;
479 struct node *c;
481 *node = tree;
483 for_each_property(tree, p) {
484 *prop = p;
485 m = p->val.markers;
486 for_each_marker_of_type(m, LABEL)
487 if (streq(m->ref, label))
488 return m;
491 for_each_child(tree, c) {
492 m = get_marker_label(c, label, node, prop);
493 if (m)
494 return m;
497 *prop = NULL;
498 *node = NULL;
499 return NULL;
502 struct node *get_subnode(struct node *node, const char *nodename)
504 struct node *child;
506 for_each_child(node, child)
507 if (streq(child->name, nodename))
508 return child;
510 return NULL;
513 struct node *get_node_by_path(struct node *tree, const char *path)
515 const char *p;
516 struct node *child;
518 if (!path || ! (*path)) {
519 if (tree->deleted)
520 return NULL;
521 return tree;
524 while (path[0] == '/')
525 path++;
527 p = strchr(path, '/');
529 for_each_child(tree, child) {
530 if (p && (strlen(child->name) == p-path) &&
531 strprefixeq(path, p - path, child->name))
532 return get_node_by_path(child, p+1);
533 else if (!p && streq(path, child->name))
534 return child;
537 return NULL;
540 struct node *get_node_by_label(struct node *tree, const char *label)
542 struct node *child, *node;
543 struct label *l;
545 assert(label && (strlen(label) > 0));
547 for_each_label(tree->labels, l)
548 if (streq(l->label, label))
549 return tree;
551 for_each_child(tree, child) {
552 node = get_node_by_label(child, label);
553 if (node)
554 return node;
557 return NULL;
560 struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
562 struct node *child, *node;
564 if ((phandle == 0) || (phandle == -1)) {
565 assert(generate_fixups);
566 return NULL;
569 if (tree->phandle == phandle) {
570 if (tree->deleted)
571 return NULL;
572 return tree;
575 for_each_child(tree, child) {
576 node = get_node_by_phandle(child, phandle);
577 if (node)
578 return node;
581 return NULL;
584 struct node *get_node_by_ref(struct node *tree, const char *ref)
586 if (streq(ref, "/"))
587 return tree;
588 else if (ref[0] == '/')
589 return get_node_by_path(tree, ref);
590 else
591 return get_node_by_label(tree, ref);
594 cell_t get_node_phandle(struct node *root, struct node *node)
596 static cell_t phandle = 1; /* FIXME: ick, static local */
598 if ((node->phandle != 0) && (node->phandle != -1))
599 return node->phandle;
601 while (get_node_by_phandle(root, phandle))
602 phandle++;
604 node->phandle = phandle;
606 if (!get_property(node, "linux,phandle")
607 && (phandle_format & PHANDLE_LEGACY))
608 add_property(node,
609 build_property("linux,phandle",
610 data_append_cell(empty_data, phandle)));
612 if (!get_property(node, "phandle")
613 && (phandle_format & PHANDLE_EPAPR))
614 add_property(node,
615 build_property("phandle",
616 data_append_cell(empty_data, phandle)));
618 /* If the node *does* have a phandle property, we must
619 * be dealing with a self-referencing phandle, which will be
620 * fixed up momentarily in the caller */
622 return node->phandle;
625 uint32_t guess_boot_cpuid(struct node *tree)
627 struct node *cpus, *bootcpu;
628 struct property *reg;
630 cpus = get_node_by_path(tree, "/cpus");
631 if (!cpus)
632 return 0;
635 bootcpu = cpus->children;
636 if (!bootcpu)
637 return 0;
639 reg = get_property(bootcpu, "reg");
640 if (!reg || (reg->val.len != sizeof(uint32_t)))
641 return 0;
643 /* FIXME: Sanity check node? */
645 return propval_cell(reg);
648 static int cmp_reserve_info(const void *ax, const void *bx)
650 const struct reserve_info *a, *b;
652 a = *((const struct reserve_info * const *)ax);
653 b = *((const struct reserve_info * const *)bx);
655 if (a->address < b->address)
656 return -1;
657 else if (a->address > b->address)
658 return 1;
659 else if (a->size < b->size)
660 return -1;
661 else if (a->size > b->size)
662 return 1;
663 else
664 return 0;
667 static void sort_reserve_entries(struct dt_info *dti)
669 struct reserve_info *ri, **tbl;
670 int n = 0, i = 0;
672 for (ri = dti->reservelist;
674 ri = ri->next)
675 n++;
677 if (n == 0)
678 return;
680 tbl = xmalloc(n * sizeof(*tbl));
682 for (ri = dti->reservelist;
684 ri = ri->next)
685 tbl[i++] = ri;
687 qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
689 dti->reservelist = tbl[0];
690 for (i = 0; i < (n-1); i++)
691 tbl[i]->next = tbl[i+1];
692 tbl[n-1]->next = NULL;
694 free(tbl);
697 static int cmp_prop(const void *ax, const void *bx)
699 const struct property *a, *b;
701 a = *((const struct property * const *)ax);
702 b = *((const struct property * const *)bx);
704 return strcmp(a->name, b->name);
707 static void sort_properties(struct node *node)
709 int n = 0, i = 0;
710 struct property *prop, **tbl;
712 for_each_property_withdel(node, prop)
713 n++;
715 if (n == 0)
716 return;
718 tbl = xmalloc(n * sizeof(*tbl));
720 for_each_property_withdel(node, prop)
721 tbl[i++] = prop;
723 qsort(tbl, n, sizeof(*tbl), cmp_prop);
725 node->proplist = tbl[0];
726 for (i = 0; i < (n-1); i++)
727 tbl[i]->next = tbl[i+1];
728 tbl[n-1]->next = NULL;
730 free(tbl);
733 static int cmp_subnode(const void *ax, const void *bx)
735 const struct node *a, *b;
737 a = *((const struct node * const *)ax);
738 b = *((const struct node * const *)bx);
740 return strcmp(a->name, b->name);
743 static void sort_subnodes(struct node *node)
745 int n = 0, i = 0;
746 struct node *subnode, **tbl;
748 for_each_child_withdel(node, subnode)
749 n++;
751 if (n == 0)
752 return;
754 tbl = xmalloc(n * sizeof(*tbl));
756 for_each_child_withdel(node, subnode)
757 tbl[i++] = subnode;
759 qsort(tbl, n, sizeof(*tbl), cmp_subnode);
761 node->children = tbl[0];
762 for (i = 0; i < (n-1); i++)
763 tbl[i]->next_sibling = tbl[i+1];
764 tbl[n-1]->next_sibling = NULL;
766 free(tbl);
769 static void sort_node(struct node *node)
771 struct node *c;
773 sort_properties(node);
774 sort_subnodes(node);
775 for_each_child_withdel(node, c)
776 sort_node(c);
779 void sort_tree(struct dt_info *dti)
781 sort_reserve_entries(dti);
782 sort_node(dti->dt);
785 /* utility helper to avoid code duplication */
786 static struct node *build_and_name_child_node(struct node *parent, char *name)
788 struct node *node;
790 node = build_node(NULL, NULL);
791 name_node(node, xstrdup(name));
792 add_child(parent, node);
794 return node;
797 static struct node *build_root_node(struct node *dt, char *name)
799 struct node *an;
801 an = get_subnode(dt, name);
802 if (!an)
803 an = build_and_name_child_node(dt, name);
805 if (!an)
806 die("Could not build root node /%s\n", name);
808 return an;
811 static bool any_label_tree(struct dt_info *dti, struct node *node)
813 struct node *c;
815 if (node->labels)
816 return true;
818 for_each_child(node, c)
819 if (any_label_tree(dti, c))
820 return true;
822 return false;
825 static void generate_label_tree_internal(struct dt_info *dti,
826 struct node *an, struct node *node,
827 bool allocph)
829 struct node *dt = dti->dt;
830 struct node *c;
831 struct property *p;
832 struct label *l;
834 /* if there are labels */
835 if (node->labels) {
837 /* now add the label in the node */
838 for_each_label(node->labels, l) {
840 /* check whether the label already exists */
841 p = get_property(an, l->label);
842 if (p) {
843 fprintf(stderr, "WARNING: label %s already"
844 " exists in /%s", l->label,
845 an->name);
846 continue;
849 /* insert it */
850 p = build_property(l->label,
851 data_copy_mem(node->fullpath,
852 strlen(node->fullpath) + 1));
853 add_property(an, p);
856 /* force allocation of a phandle for this node */
857 if (allocph)
858 (void)get_node_phandle(dt, node);
861 for_each_child(node, c)
862 generate_label_tree_internal(dti, an, c, allocph);
865 static bool any_fixup_tree(struct dt_info *dti, struct node *node)
867 struct node *c;
868 struct property *prop;
869 struct marker *m;
871 for_each_property(node, prop) {
872 m = prop->val.markers;
873 for_each_marker_of_type(m, REF_PHANDLE) {
874 if (!get_node_by_ref(dti->dt, m->ref))
875 return true;
879 for_each_child(node, c) {
880 if (any_fixup_tree(dti, c))
881 return true;
884 return false;
887 static void add_fixup_entry(struct dt_info *dti, struct node *fn,
888 struct node *node, struct property *prop,
889 struct marker *m)
891 char *entry;
893 /* m->ref can only be a REF_PHANDLE, but check anyway */
894 assert(m->type == REF_PHANDLE);
896 /* there shouldn't be any ':' in the arguments */
897 if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
898 die("arguments should not contain ':'\n");
900 xasprintf(&entry, "%s:%s:%u",
901 node->fullpath, prop->name, m->offset);
902 append_to_property(fn, m->ref, entry, strlen(entry) + 1);
904 free(entry);
907 static void generate_fixups_tree_internal(struct dt_info *dti,
908 struct node *fn,
909 struct node *node)
911 struct node *dt = dti->dt;
912 struct node *c;
913 struct property *prop;
914 struct marker *m;
915 struct node *refnode;
917 for_each_property(node, prop) {
918 m = prop->val.markers;
919 for_each_marker_of_type(m, REF_PHANDLE) {
920 refnode = get_node_by_ref(dt, m->ref);
921 if (!refnode)
922 add_fixup_entry(dti, fn, node, prop, m);
926 for_each_child(node, c)
927 generate_fixups_tree_internal(dti, fn, c);
930 static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
932 struct node *c;
933 struct property *prop;
934 struct marker *m;
936 for_each_property(node, prop) {
937 m = prop->val.markers;
938 for_each_marker_of_type(m, REF_PHANDLE) {
939 if (get_node_by_ref(dti->dt, m->ref))
940 return true;
944 for_each_child(node, c) {
945 if (any_local_fixup_tree(dti, c))
946 return true;
949 return false;
952 static void add_local_fixup_entry(struct dt_info *dti,
953 struct node *lfn, struct node *node,
954 struct property *prop, struct marker *m,
955 struct node *refnode)
957 struct node *wn, *nwn; /* local fixup node, walk node, new */
958 fdt32_t value_32;
959 char **compp;
960 int i, depth;
962 /* walk back retreiving depth */
963 depth = 0;
964 for (wn = node; wn; wn = wn->parent)
965 depth++;
967 /* allocate name array */
968 compp = xmalloc(sizeof(*compp) * depth);
970 /* store names in the array */
971 for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
972 compp[i] = wn->name;
974 /* walk the path components creating nodes if they don't exist */
975 for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
976 /* if no node exists, create it */
977 nwn = get_subnode(wn, compp[i]);
978 if (!nwn)
979 nwn = build_and_name_child_node(wn, compp[i]);
982 free(compp);
984 value_32 = cpu_to_fdt32(m->offset);
985 append_to_property(wn, prop->name, &value_32, sizeof(value_32));
988 static void generate_local_fixups_tree_internal(struct dt_info *dti,
989 struct node *lfn,
990 struct node *node)
992 struct node *dt = dti->dt;
993 struct node *c;
994 struct property *prop;
995 struct marker *m;
996 struct node *refnode;
998 for_each_property(node, prop) {
999 m = prop->val.markers;
1000 for_each_marker_of_type(m, REF_PHANDLE) {
1001 refnode = get_node_by_ref(dt, m->ref);
1002 if (refnode)
1003 add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
1007 for_each_child(node, c)
1008 generate_local_fixups_tree_internal(dti, lfn, c);
1011 void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
1013 if (!any_label_tree(dti, dti->dt))
1014 return;
1015 generate_label_tree_internal(dti, build_root_node(dti->dt, name),
1016 dti->dt, allocph);
1019 void generate_fixups_tree(struct dt_info *dti, char *name)
1021 if (!any_fixup_tree(dti, dti->dt))
1022 return;
1023 generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1024 dti->dt);
1027 void generate_local_fixups_tree(struct dt_info *dti, char *name)
1029 if (!any_local_fixup_tree(dti, dti->dt))
1030 return;
1031 generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1032 dti->dt);