staging: comedi: ni_usb6501: Fix use of uninitialized mutex
[linux/fpc-iii.git] / scripts / dtc / checks.c
blob4834e44b37b2dc6635a103ae4e6deba10c660874
1 /*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2007.
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"
22 #include "srcpos.h"
24 #ifdef TRACE_CHECKS
25 #define TRACE(c, ...) \
26 do { \
27 fprintf(stderr, "=== %s: ", (c)->name); \
28 fprintf(stderr, __VA_ARGS__); \
29 fprintf(stderr, "\n"); \
30 } while (0)
31 #else
32 #define TRACE(c, fmt, ...) do { } while (0)
33 #endif
35 enum checkstatus {
36 UNCHECKED = 0,
37 PREREQ,
38 PASSED,
39 FAILED,
42 struct check;
44 typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
46 struct check {
47 const char *name;
48 check_fn fn;
49 void *data;
50 bool warn, error;
51 enum checkstatus status;
52 bool inprogress;
53 int num_prereqs;
54 struct check **prereq;
57 #define CHECK_ENTRY(nm_, fn_, d_, w_, e_, ...) \
58 static struct check *nm_##_prereqs[] = { __VA_ARGS__ }; \
59 static struct check nm_ = { \
60 .name = #nm_, \
61 .fn = (fn_), \
62 .data = (d_), \
63 .warn = (w_), \
64 .error = (e_), \
65 .status = UNCHECKED, \
66 .num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
67 .prereq = nm_##_prereqs, \
69 #define WARNING(nm_, fn_, d_, ...) \
70 CHECK_ENTRY(nm_, fn_, d_, true, false, __VA_ARGS__)
71 #define ERROR(nm_, fn_, d_, ...) \
72 CHECK_ENTRY(nm_, fn_, d_, false, true, __VA_ARGS__)
73 #define CHECK(nm_, fn_, d_, ...) \
74 CHECK_ENTRY(nm_, fn_, d_, false, false, __VA_ARGS__)
76 static inline void PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
77 struct node *node,
78 struct property *prop,
79 const char *fmt, ...)
81 va_list ap;
82 char *str = NULL;
83 struct srcpos *pos = NULL;
84 char *file_str;
86 if (!(c->warn && (quiet < 1)) && !(c->error && (quiet < 2)))
87 return;
89 if (prop && prop->srcpos)
90 pos = prop->srcpos;
91 else if (node && node->srcpos)
92 pos = node->srcpos;
94 if (pos) {
95 file_str = srcpos_string(pos);
96 xasprintf(&str, "%s", file_str);
97 free(file_str);
98 } else if (streq(dti->outname, "-")) {
99 xasprintf(&str, "<stdout>");
100 } else {
101 xasprintf(&str, "%s", dti->outname);
104 xasprintf_append(&str, ": %s (%s): ",
105 (c->error) ? "ERROR" : "Warning", c->name);
107 if (node) {
108 if (prop)
109 xasprintf_append(&str, "%s:%s: ", node->fullpath, prop->name);
110 else
111 xasprintf_append(&str, "%s: ", node->fullpath);
114 va_start(ap, fmt);
115 xavsprintf_append(&str, fmt, ap);
116 va_end(ap);
118 xasprintf_append(&str, "\n");
120 if (!prop && pos) {
121 pos = node->srcpos;
122 while (pos->next) {
123 pos = pos->next;
125 file_str = srcpos_string(pos);
126 xasprintf_append(&str, " also defined at %s\n", file_str);
127 free(file_str);
131 fputs(str, stderr);
134 #define FAIL(c, dti, node, ...) \
135 do { \
136 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
137 (c)->status = FAILED; \
138 check_msg((c), dti, node, NULL, __VA_ARGS__); \
139 } while (0)
141 #define FAIL_PROP(c, dti, node, prop, ...) \
142 do { \
143 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
144 (c)->status = FAILED; \
145 check_msg((c), dti, node, prop, __VA_ARGS__); \
146 } while (0)
149 static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
151 struct node *child;
153 TRACE(c, "%s", node->fullpath);
154 if (c->fn)
155 c->fn(c, dti, node);
157 for_each_child(node, child)
158 check_nodes_props(c, dti, child);
161 static bool run_check(struct check *c, struct dt_info *dti)
163 struct node *dt = dti->dt;
164 bool error = false;
165 int i;
167 assert(!c->inprogress);
169 if (c->status != UNCHECKED)
170 goto out;
172 c->inprogress = true;
174 for (i = 0; i < c->num_prereqs; i++) {
175 struct check *prq = c->prereq[i];
176 error = error || run_check(prq, dti);
177 if (prq->status != PASSED) {
178 c->status = PREREQ;
179 check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
180 c->prereq[i]->name);
184 if (c->status != UNCHECKED)
185 goto out;
187 check_nodes_props(c, dti, dt);
189 if (c->status == UNCHECKED)
190 c->status = PASSED;
192 TRACE(c, "\tCompleted, status %d", c->status);
194 out:
195 c->inprogress = false;
196 if ((c->status != PASSED) && (c->error))
197 error = true;
198 return error;
202 * Utility check functions
205 /* A check which always fails, for testing purposes only */
206 static inline void check_always_fail(struct check *c, struct dt_info *dti,
207 struct node *node)
209 FAIL(c, dti, node, "always_fail check");
211 CHECK(always_fail, check_always_fail, NULL);
213 static void check_is_string(struct check *c, struct dt_info *dti,
214 struct node *node)
216 struct property *prop;
217 char *propname = c->data;
219 prop = get_property(node, propname);
220 if (!prop)
221 return; /* Not present, assumed ok */
223 if (!data_is_one_string(prop->val))
224 FAIL_PROP(c, dti, node, prop, "property is not a string");
226 #define WARNING_IF_NOT_STRING(nm, propname) \
227 WARNING(nm, check_is_string, (propname))
228 #define ERROR_IF_NOT_STRING(nm, propname) \
229 ERROR(nm, check_is_string, (propname))
231 static void check_is_string_list(struct check *c, struct dt_info *dti,
232 struct node *node)
234 int rem, l;
235 struct property *prop;
236 char *propname = c->data;
237 char *str;
239 prop = get_property(node, propname);
240 if (!prop)
241 return; /* Not present, assumed ok */
243 str = prop->val.val;
244 rem = prop->val.len;
245 while (rem > 0) {
246 l = strnlen(str, rem);
247 if (l == rem) {
248 FAIL_PROP(c, dti, node, prop, "property is not a string list");
249 break;
251 rem -= l + 1;
252 str += l + 1;
255 #define WARNING_IF_NOT_STRING_LIST(nm, propname) \
256 WARNING(nm, check_is_string_list, (propname))
257 #define ERROR_IF_NOT_STRING_LIST(nm, propname) \
258 ERROR(nm, check_is_string_list, (propname))
260 static void check_is_cell(struct check *c, struct dt_info *dti,
261 struct node *node)
263 struct property *prop;
264 char *propname = c->data;
266 prop = get_property(node, propname);
267 if (!prop)
268 return; /* Not present, assumed ok */
270 if (prop->val.len != sizeof(cell_t))
271 FAIL_PROP(c, dti, node, prop, "property is not a single cell");
273 #define WARNING_IF_NOT_CELL(nm, propname) \
274 WARNING(nm, check_is_cell, (propname))
275 #define ERROR_IF_NOT_CELL(nm, propname) \
276 ERROR(nm, check_is_cell, (propname))
279 * Structural check functions
282 static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
283 struct node *node)
285 struct node *child, *child2;
287 for_each_child(node, child)
288 for (child2 = child->next_sibling;
289 child2;
290 child2 = child2->next_sibling)
291 if (streq(child->name, child2->name))
292 FAIL(c, dti, child2, "Duplicate node name");
294 ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
296 static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
297 struct node *node)
299 struct property *prop, *prop2;
301 for_each_property(node, prop) {
302 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
303 if (prop2->deleted)
304 continue;
305 if (streq(prop->name, prop2->name))
306 FAIL_PROP(c, dti, node, prop, "Duplicate property name");
310 ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
312 #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
313 #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
314 #define DIGITS "0123456789"
315 #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
316 #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
318 static void check_node_name_chars(struct check *c, struct dt_info *dti,
319 struct node *node)
321 int n = strspn(node->name, c->data);
323 if (n < strlen(node->name))
324 FAIL(c, dti, node, "Bad character '%c' in node name",
325 node->name[n]);
327 ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
329 static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
330 struct node *node)
332 int n = strspn(node->name, c->data);
334 if (n < node->basenamelen)
335 FAIL(c, dti, node, "Character '%c' not recommended in node name",
336 node->name[n]);
338 CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
340 static void check_node_name_format(struct check *c, struct dt_info *dti,
341 struct node *node)
343 if (strchr(get_unitname(node), '@'))
344 FAIL(c, dti, node, "multiple '@' characters in node name");
346 ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
348 static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
349 struct node *node)
351 const char *unitname = get_unitname(node);
352 struct property *prop = get_property(node, "reg");
354 if (get_subnode(node, "__overlay__")) {
355 /* HACK: Overlay fragments are a special case */
356 return;
359 if (!prop) {
360 prop = get_property(node, "ranges");
361 if (prop && !prop->val.len)
362 prop = NULL;
365 if (prop) {
366 if (!unitname[0])
367 FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
368 } else {
369 if (unitname[0])
370 FAIL(c, dti, node, "node has a unit name, but no reg property");
373 WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
375 static void check_property_name_chars(struct check *c, struct dt_info *dti,
376 struct node *node)
378 struct property *prop;
380 for_each_property(node, prop) {
381 int n = strspn(prop->name, c->data);
383 if (n < strlen(prop->name))
384 FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
385 prop->name[n]);
388 ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
390 static void check_property_name_chars_strict(struct check *c,
391 struct dt_info *dti,
392 struct node *node)
394 struct property *prop;
396 for_each_property(node, prop) {
397 const char *name = prop->name;
398 int n = strspn(name, c->data);
400 if (n == strlen(prop->name))
401 continue;
403 /* Certain names are whitelisted */
404 if (streq(name, "device_type"))
405 continue;
408 * # is only allowed at the beginning of property names not counting
409 * the vendor prefix.
411 if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
412 name += n + 1;
413 n = strspn(name, c->data);
415 if (n < strlen(name))
416 FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
417 name[n]);
420 CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
422 #define DESCLABEL_FMT "%s%s%s%s%s"
423 #define DESCLABEL_ARGS(node,prop,mark) \
424 ((mark) ? "value of " : ""), \
425 ((prop) ? "'" : ""), \
426 ((prop) ? (prop)->name : ""), \
427 ((prop) ? "' in " : ""), (node)->fullpath
429 static void check_duplicate_label(struct check *c, struct dt_info *dti,
430 const char *label, struct node *node,
431 struct property *prop, struct marker *mark)
433 struct node *dt = dti->dt;
434 struct node *othernode = NULL;
435 struct property *otherprop = NULL;
436 struct marker *othermark = NULL;
438 othernode = get_node_by_label(dt, label);
440 if (!othernode)
441 otherprop = get_property_by_label(dt, label, &othernode);
442 if (!othernode)
443 othermark = get_marker_label(dt, label, &othernode,
444 &otherprop);
446 if (!othernode)
447 return;
449 if ((othernode != node) || (otherprop != prop) || (othermark != mark))
450 FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
451 " and " DESCLABEL_FMT,
452 label, DESCLABEL_ARGS(node, prop, mark),
453 DESCLABEL_ARGS(othernode, otherprop, othermark));
456 static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
457 struct node *node)
459 struct label *l;
460 struct property *prop;
462 for_each_label(node->labels, l)
463 check_duplicate_label(c, dti, l->label, node, NULL, NULL);
465 for_each_property(node, prop) {
466 struct marker *m = prop->val.markers;
468 for_each_label(prop->labels, l)
469 check_duplicate_label(c, dti, l->label, node, prop, NULL);
471 for_each_marker_of_type(m, LABEL)
472 check_duplicate_label(c, dti, m->ref, node, prop, m);
475 ERROR(duplicate_label, check_duplicate_label_node, NULL);
477 static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
478 struct node *node, const char *propname)
480 struct node *root = dti->dt;
481 struct property *prop;
482 struct marker *m;
483 cell_t phandle;
485 prop = get_property(node, propname);
486 if (!prop)
487 return 0;
489 if (prop->val.len != sizeof(cell_t)) {
490 FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
491 prop->val.len, prop->name);
492 return 0;
495 m = prop->val.markers;
496 for_each_marker_of_type(m, REF_PHANDLE) {
497 assert(m->offset == 0);
498 if (node != get_node_by_ref(root, m->ref))
499 /* "Set this node's phandle equal to some
500 * other node's phandle". That's nonsensical
501 * by construction. */ {
502 FAIL(c, dti, node, "%s is a reference to another node",
503 prop->name);
505 /* But setting this node's phandle equal to its own
506 * phandle is allowed - that means allocate a unique
507 * phandle for this node, even if it's not otherwise
508 * referenced. The value will be filled in later, so
509 * we treat it as having no phandle data for now. */
510 return 0;
513 phandle = propval_cell(prop);
515 if ((phandle == 0) || (phandle == -1)) {
516 FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
517 phandle, prop->name);
518 return 0;
521 return phandle;
524 static void check_explicit_phandles(struct check *c, struct dt_info *dti,
525 struct node *node)
527 struct node *root = dti->dt;
528 struct node *other;
529 cell_t phandle, linux_phandle;
531 /* Nothing should have assigned phandles yet */
532 assert(!node->phandle);
534 phandle = check_phandle_prop(c, dti, node, "phandle");
536 linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
538 if (!phandle && !linux_phandle)
539 /* No valid phandles; nothing further to check */
540 return;
542 if (linux_phandle && phandle && (phandle != linux_phandle))
543 FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
544 " properties");
546 if (linux_phandle && !phandle)
547 phandle = linux_phandle;
549 other = get_node_by_phandle(root, phandle);
550 if (other && (other != node)) {
551 FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
552 phandle, other->fullpath);
553 return;
556 node->phandle = phandle;
558 ERROR(explicit_phandles, check_explicit_phandles, NULL);
560 static void check_name_properties(struct check *c, struct dt_info *dti,
561 struct node *node)
563 struct property **pp, *prop = NULL;
565 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
566 if (streq((*pp)->name, "name")) {
567 prop = *pp;
568 break;
571 if (!prop)
572 return; /* No name property, that's fine */
574 if ((prop->val.len != node->basenamelen+1)
575 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
576 FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
577 " of base node name)", prop->val.val);
578 } else {
579 /* The name property is correct, and therefore redundant.
580 * Delete it */
581 *pp = prop->next;
582 free(prop->name);
583 data_free(prop->val);
584 free(prop);
587 ERROR_IF_NOT_STRING(name_is_string, "name");
588 ERROR(name_properties, check_name_properties, NULL, &name_is_string);
591 * Reference fixup functions
594 static void fixup_phandle_references(struct check *c, struct dt_info *dti,
595 struct node *node)
597 struct node *dt = dti->dt;
598 struct property *prop;
600 for_each_property(node, prop) {
601 struct marker *m = prop->val.markers;
602 struct node *refnode;
603 cell_t phandle;
605 for_each_marker_of_type(m, REF_PHANDLE) {
606 assert(m->offset + sizeof(cell_t) <= prop->val.len);
608 refnode = get_node_by_ref(dt, m->ref);
609 if (! refnode) {
610 if (!(dti->dtsflags & DTSF_PLUGIN))
611 FAIL(c, dti, node, "Reference to non-existent node or "
612 "label \"%s\"\n", m->ref);
613 else /* mark the entry as unresolved */
614 *((fdt32_t *)(prop->val.val + m->offset)) =
615 cpu_to_fdt32(0xffffffff);
616 continue;
619 phandle = get_node_phandle(dt, refnode);
620 *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
622 reference_node(refnode);
626 ERROR(phandle_references, fixup_phandle_references, NULL,
627 &duplicate_node_names, &explicit_phandles);
629 static void fixup_path_references(struct check *c, struct dt_info *dti,
630 struct node *node)
632 struct node *dt = dti->dt;
633 struct property *prop;
635 for_each_property(node, prop) {
636 struct marker *m = prop->val.markers;
637 struct node *refnode;
638 char *path;
640 for_each_marker_of_type(m, REF_PATH) {
641 assert(m->offset <= prop->val.len);
643 refnode = get_node_by_ref(dt, m->ref);
644 if (!refnode) {
645 FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
646 m->ref);
647 continue;
650 path = refnode->fullpath;
651 prop->val = data_insert_at_marker(prop->val, m, path,
652 strlen(path) + 1);
654 reference_node(refnode);
658 ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
660 static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
661 struct node *node)
663 if (node->omit_if_unused && !node->is_referenced)
664 delete_node(node);
666 ERROR(omit_unused_nodes, fixup_omit_unused_nodes, NULL, &phandle_references, &path_references);
669 * Semantic checks
671 WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
672 WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
673 WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
675 WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
676 WARNING_IF_NOT_STRING(model_is_string, "model");
677 WARNING_IF_NOT_STRING(status_is_string, "status");
678 WARNING_IF_NOT_STRING(label_is_string, "label");
680 WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
682 static void check_names_is_string_list(struct check *c, struct dt_info *dti,
683 struct node *node)
685 struct property *prop;
687 for_each_property(node, prop) {
688 const char *s = strrchr(prop->name, '-');
689 if (!s || !streq(s, "-names"))
690 continue;
692 c->data = prop->name;
693 check_is_string_list(c, dti, node);
696 WARNING(names_is_string_list, check_names_is_string_list, NULL);
698 static void check_alias_paths(struct check *c, struct dt_info *dti,
699 struct node *node)
701 struct property *prop;
703 if (!streq(node->name, "aliases"))
704 return;
706 for_each_property(node, prop) {
707 if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
708 FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
709 prop->val.val);
710 continue;
712 if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
713 FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
716 WARNING(alias_paths, check_alias_paths, NULL);
718 static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
719 struct node *node)
721 struct property *prop;
723 node->addr_cells = -1;
724 node->size_cells = -1;
726 prop = get_property(node, "#address-cells");
727 if (prop)
728 node->addr_cells = propval_cell(prop);
730 prop = get_property(node, "#size-cells");
731 if (prop)
732 node->size_cells = propval_cell(prop);
734 WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
735 &address_cells_is_cell, &size_cells_is_cell);
737 #define node_addr_cells(n) \
738 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
739 #define node_size_cells(n) \
740 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
742 static void check_reg_format(struct check *c, struct dt_info *dti,
743 struct node *node)
745 struct property *prop;
746 int addr_cells, size_cells, entrylen;
748 prop = get_property(node, "reg");
749 if (!prop)
750 return; /* No "reg", that's fine */
752 if (!node->parent) {
753 FAIL(c, dti, node, "Root node has a \"reg\" property");
754 return;
757 if (prop->val.len == 0)
758 FAIL_PROP(c, dti, node, prop, "property is empty");
760 addr_cells = node_addr_cells(node->parent);
761 size_cells = node_size_cells(node->parent);
762 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
764 if (!entrylen || (prop->val.len % entrylen) != 0)
765 FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
766 "(#address-cells == %d, #size-cells == %d)",
767 prop->val.len, addr_cells, size_cells);
769 WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
771 static void check_ranges_format(struct check *c, struct dt_info *dti,
772 struct node *node)
774 struct property *prop;
775 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
777 prop = get_property(node, "ranges");
778 if (!prop)
779 return;
781 if (!node->parent) {
782 FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
783 return;
786 p_addr_cells = node_addr_cells(node->parent);
787 p_size_cells = node_size_cells(node->parent);
788 c_addr_cells = node_addr_cells(node);
789 c_size_cells = node_size_cells(node);
790 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
792 if (prop->val.len == 0) {
793 if (p_addr_cells != c_addr_cells)
794 FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
795 "#address-cells (%d) differs from %s (%d)",
796 c_addr_cells, node->parent->fullpath,
797 p_addr_cells);
798 if (p_size_cells != c_size_cells)
799 FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
800 "#size-cells (%d) differs from %s (%d)",
801 c_size_cells, node->parent->fullpath,
802 p_size_cells);
803 } else if ((prop->val.len % entrylen) != 0) {
804 FAIL_PROP(c, dti, node, prop, "\"ranges\" property has invalid length (%d bytes) "
805 "(parent #address-cells == %d, child #address-cells == %d, "
806 "#size-cells == %d)", prop->val.len,
807 p_addr_cells, c_addr_cells, c_size_cells);
810 WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
812 static const struct bus_type pci_bus = {
813 .name = "PCI",
816 static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
818 struct property *prop;
819 cell_t *cells;
821 prop = get_property(node, "device_type");
822 if (!prop || !streq(prop->val.val, "pci"))
823 return;
825 node->bus = &pci_bus;
827 if (!strprefixeq(node->name, node->basenamelen, "pci") &&
828 !strprefixeq(node->name, node->basenamelen, "pcie"))
829 FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
831 prop = get_property(node, "ranges");
832 if (!prop)
833 FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
835 if (node_addr_cells(node) != 3)
836 FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
837 if (node_size_cells(node) != 2)
838 FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
840 prop = get_property(node, "bus-range");
841 if (!prop)
842 return;
844 if (prop->val.len != (sizeof(cell_t) * 2)) {
845 FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
846 return;
848 cells = (cell_t *)prop->val.val;
849 if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
850 FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
851 if (fdt32_to_cpu(cells[1]) > 0xff)
852 FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
854 WARNING(pci_bridge, check_pci_bridge, NULL,
855 &device_type_is_string, &addr_size_cells);
857 static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
859 struct property *prop;
860 unsigned int bus_num, min_bus, max_bus;
861 cell_t *cells;
863 if (!node->parent || (node->parent->bus != &pci_bus))
864 return;
866 prop = get_property(node, "reg");
867 if (!prop)
868 return;
870 cells = (cell_t *)prop->val.val;
871 bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
873 prop = get_property(node->parent, "bus-range");
874 if (!prop) {
875 min_bus = max_bus = 0;
876 } else {
877 cells = (cell_t *)prop->val.val;
878 min_bus = fdt32_to_cpu(cells[0]);
879 max_bus = fdt32_to_cpu(cells[0]);
881 if ((bus_num < min_bus) || (bus_num > max_bus))
882 FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
883 bus_num, min_bus, max_bus);
885 WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
887 static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
889 struct property *prop;
890 const char *unitname = get_unitname(node);
891 char unit_addr[5];
892 unsigned int dev, func, reg;
893 cell_t *cells;
895 if (!node->parent || (node->parent->bus != &pci_bus))
896 return;
898 prop = get_property(node, "reg");
899 if (!prop) {
900 FAIL(c, dti, node, "missing PCI reg property");
901 return;
904 cells = (cell_t *)prop->val.val;
905 if (cells[1] || cells[2])
906 FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
908 reg = fdt32_to_cpu(cells[0]);
909 dev = (reg & 0xf800) >> 11;
910 func = (reg & 0x700) >> 8;
912 if (reg & 0xff000000)
913 FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
914 if (reg & 0x000000ff)
915 FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
917 if (func == 0) {
918 snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
919 if (streq(unitname, unit_addr))
920 return;
923 snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
924 if (streq(unitname, unit_addr))
925 return;
927 FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
928 unit_addr);
930 WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
932 static const struct bus_type simple_bus = {
933 .name = "simple-bus",
936 static bool node_is_compatible(struct node *node, const char *compat)
938 struct property *prop;
939 const char *str, *end;
941 prop = get_property(node, "compatible");
942 if (!prop)
943 return false;
945 for (str = prop->val.val, end = str + prop->val.len; str < end;
946 str += strnlen(str, end - str) + 1) {
947 if (streq(str, compat))
948 return true;
950 return false;
953 static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
955 if (node_is_compatible(node, "simple-bus"))
956 node->bus = &simple_bus;
958 WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL,
959 &addr_size_cells, &compatible_is_string_list);
961 static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
963 struct property *prop;
964 const char *unitname = get_unitname(node);
965 char unit_addr[17];
966 unsigned int size;
967 uint64_t reg = 0;
968 cell_t *cells = NULL;
970 if (!node->parent || (node->parent->bus != &simple_bus))
971 return;
973 prop = get_property(node, "reg");
974 if (prop)
975 cells = (cell_t *)prop->val.val;
976 else {
977 prop = get_property(node, "ranges");
978 if (prop && prop->val.len)
979 /* skip of child address */
980 cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
983 if (!cells) {
984 if (node->parent->parent && !(node->bus == &simple_bus))
985 FAIL(c, dti, node, "missing or empty reg/ranges property");
986 return;
989 size = node_addr_cells(node->parent);
990 while (size--)
991 reg = (reg << 32) | fdt32_to_cpu(*(cells++));
993 snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
994 if (!streq(unitname, unit_addr))
995 FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
996 unit_addr);
998 WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
1000 static const struct bus_type i2c_bus = {
1001 .name = "i2c-bus",
1004 static void check_i2c_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1006 if (strprefixeq(node->name, node->basenamelen, "i2c-bus") ||
1007 strprefixeq(node->name, node->basenamelen, "i2c-arb")) {
1008 node->bus = &i2c_bus;
1009 } else if (strprefixeq(node->name, node->basenamelen, "i2c")) {
1010 struct node *child;
1011 for_each_child(node, child) {
1012 if (strprefixeq(child->name, node->basenamelen, "i2c-bus"))
1013 return;
1015 node->bus = &i2c_bus;
1016 } else
1017 return;
1019 if (!node->children)
1020 return;
1022 if (node_addr_cells(node) != 1)
1023 FAIL(c, dti, node, "incorrect #address-cells for I2C bus");
1024 if (node_size_cells(node) != 0)
1025 FAIL(c, dti, node, "incorrect #size-cells for I2C bus");
1028 WARNING(i2c_bus_bridge, check_i2c_bus_bridge, NULL, &addr_size_cells);
1030 static void check_i2c_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1032 struct property *prop;
1033 const char *unitname = get_unitname(node);
1034 char unit_addr[17];
1035 uint32_t reg = 0;
1036 int len;
1037 cell_t *cells = NULL;
1039 if (!node->parent || (node->parent->bus != &i2c_bus))
1040 return;
1042 prop = get_property(node, "reg");
1043 if (prop)
1044 cells = (cell_t *)prop->val.val;
1046 if (!cells) {
1047 FAIL(c, dti, node, "missing or empty reg property");
1048 return;
1051 reg = fdt32_to_cpu(*cells);
1052 snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1053 if (!streq(unitname, unit_addr))
1054 FAIL(c, dti, node, "I2C bus unit address format error, expected \"%s\"",
1055 unit_addr);
1057 for (len = prop->val.len; len > 0; len -= 4) {
1058 reg = fdt32_to_cpu(*(cells++));
1059 if (reg > 0x3ff)
1060 FAIL_PROP(c, dti, node, prop, "I2C address must be less than 10-bits, got \"0x%x\"",
1061 reg);
1065 WARNING(i2c_bus_reg, check_i2c_bus_reg, NULL, &reg_format, &i2c_bus_bridge);
1067 static const struct bus_type spi_bus = {
1068 .name = "spi-bus",
1071 static void check_spi_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1073 int spi_addr_cells = 1;
1075 if (strprefixeq(node->name, node->basenamelen, "spi")) {
1076 node->bus = &spi_bus;
1077 } else {
1078 /* Try to detect SPI buses which don't have proper node name */
1079 struct node *child;
1081 if (node_addr_cells(node) != 1 || node_size_cells(node) != 0)
1082 return;
1084 for_each_child(node, child) {
1085 struct property *prop;
1086 for_each_property(child, prop) {
1087 if (strprefixeq(prop->name, 4, "spi-")) {
1088 node->bus = &spi_bus;
1089 break;
1092 if (node->bus == &spi_bus)
1093 break;
1096 if (node->bus == &spi_bus && get_property(node, "reg"))
1097 FAIL(c, dti, node, "node name for SPI buses should be 'spi'");
1099 if (node->bus != &spi_bus || !node->children)
1100 return;
1102 if (get_property(node, "spi-slave"))
1103 spi_addr_cells = 0;
1104 if (node_addr_cells(node) != spi_addr_cells)
1105 FAIL(c, dti, node, "incorrect #address-cells for SPI bus");
1106 if (node_size_cells(node) != 0)
1107 FAIL(c, dti, node, "incorrect #size-cells for SPI bus");
1110 WARNING(spi_bus_bridge, check_spi_bus_bridge, NULL, &addr_size_cells);
1112 static void check_spi_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1114 struct property *prop;
1115 const char *unitname = get_unitname(node);
1116 char unit_addr[9];
1117 uint32_t reg = 0;
1118 cell_t *cells = NULL;
1120 if (!node->parent || (node->parent->bus != &spi_bus))
1121 return;
1123 if (get_property(node->parent, "spi-slave"))
1124 return;
1126 prop = get_property(node, "reg");
1127 if (prop)
1128 cells = (cell_t *)prop->val.val;
1130 if (!cells) {
1131 FAIL(c, dti, node, "missing or empty reg property");
1132 return;
1135 reg = fdt32_to_cpu(*cells);
1136 snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1137 if (!streq(unitname, unit_addr))
1138 FAIL(c, dti, node, "SPI bus unit address format error, expected \"%s\"",
1139 unit_addr);
1141 WARNING(spi_bus_reg, check_spi_bus_reg, NULL, &reg_format, &spi_bus_bridge);
1143 static void check_unit_address_format(struct check *c, struct dt_info *dti,
1144 struct node *node)
1146 const char *unitname = get_unitname(node);
1148 if (node->parent && node->parent->bus)
1149 return;
1151 if (!unitname[0])
1152 return;
1154 if (!strncmp(unitname, "0x", 2)) {
1155 FAIL(c, dti, node, "unit name should not have leading \"0x\"");
1156 /* skip over 0x for next test */
1157 unitname += 2;
1159 if (unitname[0] == '0' && isxdigit(unitname[1]))
1160 FAIL(c, dti, node, "unit name should not have leading 0s");
1162 WARNING(unit_address_format, check_unit_address_format, NULL,
1163 &node_name_format, &pci_bridge, &simple_bus_bridge);
1166 * Style checks
1168 static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
1169 struct node *node)
1171 struct property *reg, *ranges;
1173 if (!node->parent)
1174 return; /* Ignore root node */
1176 reg = get_property(node, "reg");
1177 ranges = get_property(node, "ranges");
1179 if (!reg && !ranges)
1180 return;
1182 if (node->parent->addr_cells == -1)
1183 FAIL(c, dti, node, "Relying on default #address-cells value");
1185 if (node->parent->size_cells == -1)
1186 FAIL(c, dti, node, "Relying on default #size-cells value");
1188 WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1189 &addr_size_cells);
1191 static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
1192 struct node *node)
1194 struct property *prop;
1195 struct node *child;
1196 bool has_reg = false;
1198 if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
1199 return;
1201 if (get_property(node, "ranges") || !node->children)
1202 return;
1204 for_each_child(node, child) {
1205 prop = get_property(child, "reg");
1206 if (prop)
1207 has_reg = true;
1210 if (!has_reg)
1211 FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
1213 WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
1215 static void check_unique_unit_address(struct check *c, struct dt_info *dti,
1216 struct node *node)
1218 struct node *childa;
1220 if (node->addr_cells < 0 || node->size_cells < 0)
1221 return;
1223 if (!node->children)
1224 return;
1226 for_each_child(node, childa) {
1227 struct node *childb;
1228 const char *addr_a = get_unitname(childa);
1230 if (!strlen(addr_a))
1231 continue;
1233 for_each_child(node, childb) {
1234 const char *addr_b = get_unitname(childb);
1235 if (childa == childb)
1236 break;
1238 if (streq(addr_a, addr_b))
1239 FAIL(c, dti, childb, "duplicate unit-address (also used in node %s)", childa->fullpath);
1243 WARNING(unique_unit_address, check_unique_unit_address, NULL, &avoid_default_addr_size);
1245 static void check_obsolete_chosen_interrupt_controller(struct check *c,
1246 struct dt_info *dti,
1247 struct node *node)
1249 struct node *dt = dti->dt;
1250 struct node *chosen;
1251 struct property *prop;
1253 if (node != dt)
1254 return;
1257 chosen = get_node_by_path(dt, "/chosen");
1258 if (!chosen)
1259 return;
1261 prop = get_property(chosen, "interrupt-controller");
1262 if (prop)
1263 FAIL_PROP(c, dti, node, prop,
1264 "/chosen has obsolete \"interrupt-controller\" property");
1266 WARNING(obsolete_chosen_interrupt_controller,
1267 check_obsolete_chosen_interrupt_controller, NULL);
1269 static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
1270 struct node *node)
1272 if (!streq(node->name, "chosen"))
1273 return;
1275 if (node->parent != dti->dt)
1276 FAIL(c, dti, node, "chosen node must be at root node");
1278 WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
1280 static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
1281 struct node *node)
1283 struct property *prop;
1285 if (!streq(node->name, "chosen"))
1286 return;
1288 prop = get_property(node, "bootargs");
1289 if (!prop)
1290 return;
1292 c->data = prop->name;
1293 check_is_string(c, dti, node);
1295 WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
1297 static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
1298 struct node *node)
1300 struct property *prop;
1302 if (!streq(node->name, "chosen"))
1303 return;
1305 prop = get_property(node, "stdout-path");
1306 if (!prop) {
1307 prop = get_property(node, "linux,stdout-path");
1308 if (!prop)
1309 return;
1310 FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
1313 c->data = prop->name;
1314 check_is_string(c, dti, node);
1316 WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
1318 struct provider {
1319 const char *prop_name;
1320 const char *cell_name;
1321 bool optional;
1324 static void check_property_phandle_args(struct check *c,
1325 struct dt_info *dti,
1326 struct node *node,
1327 struct property *prop,
1328 const struct provider *provider)
1330 struct node *root = dti->dt;
1331 int cell, cellsize = 0;
1333 if (prop->val.len % sizeof(cell_t)) {
1334 FAIL_PROP(c, dti, node, prop,
1335 "property size (%d) is invalid, expected multiple of %zu",
1336 prop->val.len, sizeof(cell_t));
1337 return;
1340 for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
1341 struct node *provider_node;
1342 struct property *cellprop;
1343 int phandle;
1345 phandle = propval_cell_n(prop, cell);
1347 * Some bindings use a cell value 0 or -1 to skip over optional
1348 * entries when each index position has a specific definition.
1350 if (phandle == 0 || phandle == -1) {
1351 /* Give up if this is an overlay with external references */
1352 if (dti->dtsflags & DTSF_PLUGIN)
1353 break;
1355 cellsize = 0;
1356 continue;
1359 /* If we have markers, verify the current cell is a phandle */
1360 if (prop->val.markers) {
1361 struct marker *m = prop->val.markers;
1362 for_each_marker_of_type(m, REF_PHANDLE) {
1363 if (m->offset == (cell * sizeof(cell_t)))
1364 break;
1366 if (!m)
1367 FAIL_PROP(c, dti, node, prop,
1368 "cell %d is not a phandle reference",
1369 cell);
1372 provider_node = get_node_by_phandle(root, phandle);
1373 if (!provider_node) {
1374 FAIL_PROP(c, dti, node, prop,
1375 "Could not get phandle node for (cell %d)",
1376 cell);
1377 break;
1380 cellprop = get_property(provider_node, provider->cell_name);
1381 if (cellprop) {
1382 cellsize = propval_cell(cellprop);
1383 } else if (provider->optional) {
1384 cellsize = 0;
1385 } else {
1386 FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
1387 provider->cell_name,
1388 provider_node->fullpath,
1389 prop->name, cell);
1390 break;
1393 if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
1394 FAIL_PROP(c, dti, node, prop,
1395 "property size (%d) too small for cell size %d",
1396 prop->val.len, cellsize);
1401 static void check_provider_cells_property(struct check *c,
1402 struct dt_info *dti,
1403 struct node *node)
1405 struct provider *provider = c->data;
1406 struct property *prop;
1408 prop = get_property(node, provider->prop_name);
1409 if (!prop)
1410 return;
1412 check_property_phandle_args(c, dti, node, prop, provider);
1414 #define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1415 static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1416 WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
1418 WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
1419 WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
1420 WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
1421 WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
1422 WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
1423 WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
1424 WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
1425 WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
1426 WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
1427 WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
1428 WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
1429 WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
1430 WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
1431 WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
1432 WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
1433 WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
1435 static bool prop_is_gpio(struct property *prop)
1437 char *str;
1440 * *-gpios and *-gpio can appear in property names,
1441 * so skip over any false matches (only one known ATM)
1443 if (strstr(prop->name, "nr-gpio"))
1444 return false;
1446 str = strrchr(prop->name, '-');
1447 if (str)
1448 str++;
1449 else
1450 str = prop->name;
1451 if (!(streq(str, "gpios") || streq(str, "gpio")))
1452 return false;
1454 return true;
1457 static void check_gpios_property(struct check *c,
1458 struct dt_info *dti,
1459 struct node *node)
1461 struct property *prop;
1463 /* Skip GPIO hog nodes which have 'gpios' property */
1464 if (get_property(node, "gpio-hog"))
1465 return;
1467 for_each_property(node, prop) {
1468 struct provider provider;
1470 if (!prop_is_gpio(prop))
1471 continue;
1473 provider.prop_name = prop->name;
1474 provider.cell_name = "#gpio-cells";
1475 provider.optional = false;
1476 check_property_phandle_args(c, dti, node, prop, &provider);
1480 WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
1482 static void check_deprecated_gpio_property(struct check *c,
1483 struct dt_info *dti,
1484 struct node *node)
1486 struct property *prop;
1488 for_each_property(node, prop) {
1489 char *str;
1491 if (!prop_is_gpio(prop))
1492 continue;
1494 str = strstr(prop->name, "gpio");
1495 if (!streq(str, "gpio"))
1496 continue;
1498 FAIL_PROP(c, dti, node, prop,
1499 "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
1503 CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
1505 static bool node_is_interrupt_provider(struct node *node)
1507 struct property *prop;
1509 prop = get_property(node, "interrupt-controller");
1510 if (prop)
1511 return true;
1513 prop = get_property(node, "interrupt-map");
1514 if (prop)
1515 return true;
1517 return false;
1519 static void check_interrupts_property(struct check *c,
1520 struct dt_info *dti,
1521 struct node *node)
1523 struct node *root = dti->dt;
1524 struct node *irq_node = NULL, *parent = node;
1525 struct property *irq_prop, *prop = NULL;
1526 int irq_cells, phandle;
1528 irq_prop = get_property(node, "interrupts");
1529 if (!irq_prop)
1530 return;
1532 if (irq_prop->val.len % sizeof(cell_t))
1533 FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1534 irq_prop->val.len, sizeof(cell_t));
1536 while (parent && !prop) {
1537 if (parent != node && node_is_interrupt_provider(parent)) {
1538 irq_node = parent;
1539 break;
1542 prop = get_property(parent, "interrupt-parent");
1543 if (prop) {
1544 phandle = propval_cell(prop);
1545 /* Give up if this is an overlay with external references */
1546 if ((phandle == 0 || phandle == -1) &&
1547 (dti->dtsflags & DTSF_PLUGIN))
1548 return;
1550 irq_node = get_node_by_phandle(root, phandle);
1551 if (!irq_node) {
1552 FAIL_PROP(c, dti, parent, prop, "Bad phandle");
1553 return;
1555 if (!node_is_interrupt_provider(irq_node))
1556 FAIL(c, dti, irq_node,
1557 "Missing interrupt-controller or interrupt-map property");
1559 break;
1562 parent = parent->parent;
1565 if (!irq_node) {
1566 FAIL(c, dti, node, "Missing interrupt-parent");
1567 return;
1570 prop = get_property(irq_node, "#interrupt-cells");
1571 if (!prop) {
1572 FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
1573 return;
1576 irq_cells = propval_cell(prop);
1577 if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
1578 FAIL_PROP(c, dti, node, prop,
1579 "size is (%d), expected multiple of %d",
1580 irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
1583 WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1585 static const struct bus_type graph_port_bus = {
1586 .name = "graph-port",
1589 static const struct bus_type graph_ports_bus = {
1590 .name = "graph-ports",
1593 static void check_graph_nodes(struct check *c, struct dt_info *dti,
1594 struct node *node)
1596 struct node *child;
1598 for_each_child(node, child) {
1599 if (!(strprefixeq(child->name, child->basenamelen, "endpoint") ||
1600 get_property(child, "remote-endpoint")))
1601 continue;
1603 node->bus = &graph_port_bus;
1605 /* The parent of 'port' nodes can be either 'ports' or a device */
1606 if (!node->parent->bus &&
1607 (streq(node->parent->name, "ports") || get_property(node, "reg")))
1608 node->parent->bus = &graph_ports_bus;
1610 break;
1614 WARNING(graph_nodes, check_graph_nodes, NULL);
1616 static void check_graph_child_address(struct check *c, struct dt_info *dti,
1617 struct node *node)
1619 int cnt = 0;
1620 struct node *child;
1622 if (node->bus != &graph_ports_bus && node->bus != &graph_port_bus)
1623 return;
1625 for_each_child(node, child) {
1626 struct property *prop = get_property(child, "reg");
1628 /* No error if we have any non-zero unit address */
1629 if (prop && propval_cell(prop) != 0)
1630 return;
1632 cnt++;
1635 if (cnt == 1 && node->addr_cells != -1)
1636 FAIL(c, dti, node, "graph node has single child node '%s', #address-cells/#size-cells are not necessary",
1637 node->children->name);
1639 WARNING(graph_child_address, check_graph_child_address, NULL, &graph_nodes);
1641 static void check_graph_reg(struct check *c, struct dt_info *dti,
1642 struct node *node)
1644 char unit_addr[9];
1645 const char *unitname = get_unitname(node);
1646 struct property *prop;
1648 prop = get_property(node, "reg");
1649 if (!prop || !unitname)
1650 return;
1652 if (!(prop->val.val && prop->val.len == sizeof(cell_t))) {
1653 FAIL(c, dti, node, "graph node malformed 'reg' property");
1654 return;
1657 snprintf(unit_addr, sizeof(unit_addr), "%x", propval_cell(prop));
1658 if (!streq(unitname, unit_addr))
1659 FAIL(c, dti, node, "graph node unit address error, expected \"%s\"",
1660 unit_addr);
1662 if (node->parent->addr_cells != 1)
1663 FAIL_PROP(c, dti, node, get_property(node, "#address-cells"),
1664 "graph node '#address-cells' is %d, must be 1",
1665 node->parent->addr_cells);
1666 if (node->parent->size_cells != 0)
1667 FAIL_PROP(c, dti, node, get_property(node, "#size-cells"),
1668 "graph node '#size-cells' is %d, must be 0",
1669 node->parent->size_cells);
1672 static void check_graph_port(struct check *c, struct dt_info *dti,
1673 struct node *node)
1675 if (node->bus != &graph_port_bus)
1676 return;
1678 if (!strprefixeq(node->name, node->basenamelen, "port"))
1679 FAIL(c, dti, node, "graph port node name should be 'port'");
1681 check_graph_reg(c, dti, node);
1683 WARNING(graph_port, check_graph_port, NULL, &graph_nodes);
1685 static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,
1686 struct node *endpoint)
1688 int phandle;
1689 struct node *node;
1690 struct property *prop;
1692 prop = get_property(endpoint, "remote-endpoint");
1693 if (!prop)
1694 return NULL;
1696 phandle = propval_cell(prop);
1697 /* Give up if this is an overlay with external references */
1698 if (phandle == 0 || phandle == -1)
1699 return NULL;
1701 node = get_node_by_phandle(dti->dt, phandle);
1702 if (!node)
1703 FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
1705 return node;
1708 static void check_graph_endpoint(struct check *c, struct dt_info *dti,
1709 struct node *node)
1711 struct node *remote_node;
1713 if (!node->parent || node->parent->bus != &graph_port_bus)
1714 return;
1716 if (!strprefixeq(node->name, node->basenamelen, "endpoint"))
1717 FAIL(c, dti, node, "graph endpont node name should be 'endpoint'");
1719 check_graph_reg(c, dti, node);
1721 remote_node = get_remote_endpoint(c, dti, node);
1722 if (!remote_node)
1723 return;
1725 if (get_remote_endpoint(c, dti, remote_node) != node)
1726 FAIL(c, dti, node, "graph connection to node '%s' is not bidirectional",
1727 remote_node->fullpath);
1729 WARNING(graph_endpoint, check_graph_endpoint, NULL, &graph_nodes);
1731 static struct check *check_table[] = {
1732 &duplicate_node_names, &duplicate_property_names,
1733 &node_name_chars, &node_name_format, &property_name_chars,
1734 &name_is_string, &name_properties,
1736 &duplicate_label,
1738 &explicit_phandles,
1739 &phandle_references, &path_references,
1740 &omit_unused_nodes,
1742 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
1743 &device_type_is_string, &model_is_string, &status_is_string,
1744 &label_is_string,
1746 &compatible_is_string_list, &names_is_string_list,
1748 &property_name_chars_strict,
1749 &node_name_chars_strict,
1751 &addr_size_cells, &reg_format, &ranges_format,
1753 &unit_address_vs_reg,
1754 &unit_address_format,
1756 &pci_bridge,
1757 &pci_device_reg,
1758 &pci_device_bus_num,
1760 &simple_bus_bridge,
1761 &simple_bus_reg,
1763 &i2c_bus_bridge,
1764 &i2c_bus_reg,
1766 &spi_bus_bridge,
1767 &spi_bus_reg,
1769 &avoid_default_addr_size,
1770 &avoid_unnecessary_addr_size,
1771 &unique_unit_address,
1772 &obsolete_chosen_interrupt_controller,
1773 &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
1775 &clocks_property,
1776 &cooling_device_property,
1777 &dmas_property,
1778 &hwlocks_property,
1779 &interrupts_extended_property,
1780 &io_channels_property,
1781 &iommus_property,
1782 &mboxes_property,
1783 &msi_parent_property,
1784 &mux_controls_property,
1785 &phys_property,
1786 &power_domains_property,
1787 &pwms_property,
1788 &resets_property,
1789 &sound_dai_property,
1790 &thermal_sensors_property,
1792 &deprecated_gpio_property,
1793 &gpios_property,
1794 &interrupts_property,
1796 &alias_paths,
1798 &graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
1800 &always_fail,
1803 static void enable_warning_error(struct check *c, bool warn, bool error)
1805 int i;
1807 /* Raising level, also raise it for prereqs */
1808 if ((warn && !c->warn) || (error && !c->error))
1809 for (i = 0; i < c->num_prereqs; i++)
1810 enable_warning_error(c->prereq[i], warn, error);
1812 c->warn = c->warn || warn;
1813 c->error = c->error || error;
1816 static void disable_warning_error(struct check *c, bool warn, bool error)
1818 int i;
1820 /* Lowering level, also lower it for things this is the prereq
1821 * for */
1822 if ((warn && c->warn) || (error && c->error)) {
1823 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1824 struct check *cc = check_table[i];
1825 int j;
1827 for (j = 0; j < cc->num_prereqs; j++)
1828 if (cc->prereq[j] == c)
1829 disable_warning_error(cc, warn, error);
1833 c->warn = c->warn && !warn;
1834 c->error = c->error && !error;
1837 void parse_checks_option(bool warn, bool error, const char *arg)
1839 int i;
1840 const char *name = arg;
1841 bool enable = true;
1843 if ((strncmp(arg, "no-", 3) == 0)
1844 || (strncmp(arg, "no_", 3) == 0)) {
1845 name = arg + 3;
1846 enable = false;
1849 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1850 struct check *c = check_table[i];
1852 if (streq(c->name, name)) {
1853 if (enable)
1854 enable_warning_error(c, warn, error);
1855 else
1856 disable_warning_error(c, warn, error);
1857 return;
1861 die("Unrecognized check name \"%s\"\n", name);
1864 void process_checks(bool force, struct dt_info *dti)
1866 int i;
1867 int error = 0;
1869 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1870 struct check *c = check_table[i];
1872 if (c->warn || c->error)
1873 error = error || run_check(c, dti);
1876 if (error) {
1877 if (!force) {
1878 fprintf(stderr, "ERROR: Input tree has errors, aborting "
1879 "(use -f to force output)\n");
1880 exit(2);
1881 } else if (quiet < 3) {
1882 fprintf(stderr, "Warning: Input tree has errors, "
1883 "output forced\n");