spi: efm32: Convert to use GPIO descriptors
[linux/fpc-iii.git] / scripts / dtc / checks.c
blob756f0fa9203fda0db14a9905d97255811a1a4467
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2007.
4 */
6 #include "dtc.h"
7 #include "srcpos.h"
9 #ifdef TRACE_CHECKS
10 #define TRACE(c, ...) \
11 do { \
12 fprintf(stderr, "=== %s: ", (c)->name); \
13 fprintf(stderr, __VA_ARGS__); \
14 fprintf(stderr, "\n"); \
15 } while (0)
16 #else
17 #define TRACE(c, fmt, ...) do { } while (0)
18 #endif
20 enum checkstatus {
21 UNCHECKED = 0,
22 PREREQ,
23 PASSED,
24 FAILED,
27 struct check;
29 typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
31 struct check {
32 const char *name;
33 check_fn fn;
34 void *data;
35 bool warn, error;
36 enum checkstatus status;
37 bool inprogress;
38 int num_prereqs;
39 struct check **prereq;
42 #define CHECK_ENTRY(nm_, fn_, d_, w_, e_, ...) \
43 static struct check *nm_##_prereqs[] = { __VA_ARGS__ }; \
44 static struct check nm_ = { \
45 .name = #nm_, \
46 .fn = (fn_), \
47 .data = (d_), \
48 .warn = (w_), \
49 .error = (e_), \
50 .status = UNCHECKED, \
51 .num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
52 .prereq = nm_##_prereqs, \
54 #define WARNING(nm_, fn_, d_, ...) \
55 CHECK_ENTRY(nm_, fn_, d_, true, false, __VA_ARGS__)
56 #define ERROR(nm_, fn_, d_, ...) \
57 CHECK_ENTRY(nm_, fn_, d_, false, true, __VA_ARGS__)
58 #define CHECK(nm_, fn_, d_, ...) \
59 CHECK_ENTRY(nm_, fn_, d_, false, false, __VA_ARGS__)
61 static inline void PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
62 struct node *node,
63 struct property *prop,
64 const char *fmt, ...)
66 va_list ap;
67 char *str = NULL;
68 struct srcpos *pos = NULL;
69 char *file_str;
71 if (!(c->warn && (quiet < 1)) && !(c->error && (quiet < 2)))
72 return;
74 if (prop && prop->srcpos)
75 pos = prop->srcpos;
76 else if (node && node->srcpos)
77 pos = node->srcpos;
79 if (pos) {
80 file_str = srcpos_string(pos);
81 xasprintf(&str, "%s", file_str);
82 free(file_str);
83 } else if (streq(dti->outname, "-")) {
84 xasprintf(&str, "<stdout>");
85 } else {
86 xasprintf(&str, "%s", dti->outname);
89 xasprintf_append(&str, ": %s (%s): ",
90 (c->error) ? "ERROR" : "Warning", c->name);
92 if (node) {
93 if (prop)
94 xasprintf_append(&str, "%s:%s: ", node->fullpath, prop->name);
95 else
96 xasprintf_append(&str, "%s: ", node->fullpath);
99 va_start(ap, fmt);
100 xavsprintf_append(&str, fmt, ap);
101 va_end(ap);
103 xasprintf_append(&str, "\n");
105 if (!prop && pos) {
106 pos = node->srcpos;
107 while (pos->next) {
108 pos = pos->next;
110 file_str = srcpos_string(pos);
111 xasprintf_append(&str, " also defined at %s\n", file_str);
112 free(file_str);
116 fputs(str, stderr);
119 #define FAIL(c, dti, node, ...) \
120 do { \
121 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
122 (c)->status = FAILED; \
123 check_msg((c), dti, node, NULL, __VA_ARGS__); \
124 } while (0)
126 #define FAIL_PROP(c, dti, node, prop, ...) \
127 do { \
128 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
129 (c)->status = FAILED; \
130 check_msg((c), dti, node, prop, __VA_ARGS__); \
131 } while (0)
134 static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
136 struct node *child;
138 TRACE(c, "%s", node->fullpath);
139 if (c->fn)
140 c->fn(c, dti, node);
142 for_each_child(node, child)
143 check_nodes_props(c, dti, child);
146 static bool run_check(struct check *c, struct dt_info *dti)
148 struct node *dt = dti->dt;
149 bool error = false;
150 int i;
152 assert(!c->inprogress);
154 if (c->status != UNCHECKED)
155 goto out;
157 c->inprogress = true;
159 for (i = 0; i < c->num_prereqs; i++) {
160 struct check *prq = c->prereq[i];
161 error = error || run_check(prq, dti);
162 if (prq->status != PASSED) {
163 c->status = PREREQ;
164 check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
165 c->prereq[i]->name);
169 if (c->status != UNCHECKED)
170 goto out;
172 check_nodes_props(c, dti, dt);
174 if (c->status == UNCHECKED)
175 c->status = PASSED;
177 TRACE(c, "\tCompleted, status %d", c->status);
179 out:
180 c->inprogress = false;
181 if ((c->status != PASSED) && (c->error))
182 error = true;
183 return error;
187 * Utility check functions
190 /* A check which always fails, for testing purposes only */
191 static inline void check_always_fail(struct check *c, struct dt_info *dti,
192 struct node *node)
194 FAIL(c, dti, node, "always_fail check");
196 CHECK(always_fail, check_always_fail, NULL);
198 static void check_is_string(struct check *c, struct dt_info *dti,
199 struct node *node)
201 struct property *prop;
202 char *propname = c->data;
204 prop = get_property(node, propname);
205 if (!prop)
206 return; /* Not present, assumed ok */
208 if (!data_is_one_string(prop->val))
209 FAIL_PROP(c, dti, node, prop, "property is not a string");
211 #define WARNING_IF_NOT_STRING(nm, propname) \
212 WARNING(nm, check_is_string, (propname))
213 #define ERROR_IF_NOT_STRING(nm, propname) \
214 ERROR(nm, check_is_string, (propname))
216 static void check_is_string_list(struct check *c, struct dt_info *dti,
217 struct node *node)
219 int rem, l;
220 struct property *prop;
221 char *propname = c->data;
222 char *str;
224 prop = get_property(node, propname);
225 if (!prop)
226 return; /* Not present, assumed ok */
228 str = prop->val.val;
229 rem = prop->val.len;
230 while (rem > 0) {
231 l = strnlen(str, rem);
232 if (l == rem) {
233 FAIL_PROP(c, dti, node, prop, "property is not a string list");
234 break;
236 rem -= l + 1;
237 str += l + 1;
240 #define WARNING_IF_NOT_STRING_LIST(nm, propname) \
241 WARNING(nm, check_is_string_list, (propname))
242 #define ERROR_IF_NOT_STRING_LIST(nm, propname) \
243 ERROR(nm, check_is_string_list, (propname))
245 static void check_is_cell(struct check *c, struct dt_info *dti,
246 struct node *node)
248 struct property *prop;
249 char *propname = c->data;
251 prop = get_property(node, propname);
252 if (!prop)
253 return; /* Not present, assumed ok */
255 if (prop->val.len != sizeof(cell_t))
256 FAIL_PROP(c, dti, node, prop, "property is not a single cell");
258 #define WARNING_IF_NOT_CELL(nm, propname) \
259 WARNING(nm, check_is_cell, (propname))
260 #define ERROR_IF_NOT_CELL(nm, propname) \
261 ERROR(nm, check_is_cell, (propname))
264 * Structural check functions
267 static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
268 struct node *node)
270 struct node *child, *child2;
272 for_each_child(node, child)
273 for (child2 = child->next_sibling;
274 child2;
275 child2 = child2->next_sibling)
276 if (streq(child->name, child2->name))
277 FAIL(c, dti, child2, "Duplicate node name");
279 ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
281 static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
282 struct node *node)
284 struct property *prop, *prop2;
286 for_each_property(node, prop) {
287 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
288 if (prop2->deleted)
289 continue;
290 if (streq(prop->name, prop2->name))
291 FAIL_PROP(c, dti, node, prop, "Duplicate property name");
295 ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
297 #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
298 #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
299 #define DIGITS "0123456789"
300 #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
301 #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
303 static void check_node_name_chars(struct check *c, struct dt_info *dti,
304 struct node *node)
306 int n = strspn(node->name, c->data);
308 if (n < strlen(node->name))
309 FAIL(c, dti, node, "Bad character '%c' in node name",
310 node->name[n]);
312 ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
314 static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
315 struct node *node)
317 int n = strspn(node->name, c->data);
319 if (n < node->basenamelen)
320 FAIL(c, dti, node, "Character '%c' not recommended in node name",
321 node->name[n]);
323 CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
325 static void check_node_name_format(struct check *c, struct dt_info *dti,
326 struct node *node)
328 if (strchr(get_unitname(node), '@'))
329 FAIL(c, dti, node, "multiple '@' characters in node name");
331 ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
333 static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
334 struct node *node)
336 const char *unitname = get_unitname(node);
337 struct property *prop = get_property(node, "reg");
339 if (get_subnode(node, "__overlay__")) {
340 /* HACK: Overlay fragments are a special case */
341 return;
344 if (!prop) {
345 prop = get_property(node, "ranges");
346 if (prop && !prop->val.len)
347 prop = NULL;
350 if (prop) {
351 if (!unitname[0])
352 FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
353 } else {
354 if (unitname[0])
355 FAIL(c, dti, node, "node has a unit name, but no reg property");
358 WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
360 static void check_property_name_chars(struct check *c, struct dt_info *dti,
361 struct node *node)
363 struct property *prop;
365 for_each_property(node, prop) {
366 int n = strspn(prop->name, c->data);
368 if (n < strlen(prop->name))
369 FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
370 prop->name[n]);
373 ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
375 static void check_property_name_chars_strict(struct check *c,
376 struct dt_info *dti,
377 struct node *node)
379 struct property *prop;
381 for_each_property(node, prop) {
382 const char *name = prop->name;
383 int n = strspn(name, c->data);
385 if (n == strlen(prop->name))
386 continue;
388 /* Certain names are whitelisted */
389 if (streq(name, "device_type"))
390 continue;
393 * # is only allowed at the beginning of property names not counting
394 * the vendor prefix.
396 if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
397 name += n + 1;
398 n = strspn(name, c->data);
400 if (n < strlen(name))
401 FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
402 name[n]);
405 CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
407 #define DESCLABEL_FMT "%s%s%s%s%s"
408 #define DESCLABEL_ARGS(node,prop,mark) \
409 ((mark) ? "value of " : ""), \
410 ((prop) ? "'" : ""), \
411 ((prop) ? (prop)->name : ""), \
412 ((prop) ? "' in " : ""), (node)->fullpath
414 static void check_duplicate_label(struct check *c, struct dt_info *dti,
415 const char *label, struct node *node,
416 struct property *prop, struct marker *mark)
418 struct node *dt = dti->dt;
419 struct node *othernode = NULL;
420 struct property *otherprop = NULL;
421 struct marker *othermark = NULL;
423 othernode = get_node_by_label(dt, label);
425 if (!othernode)
426 otherprop = get_property_by_label(dt, label, &othernode);
427 if (!othernode)
428 othermark = get_marker_label(dt, label, &othernode,
429 &otherprop);
431 if (!othernode)
432 return;
434 if ((othernode != node) || (otherprop != prop) || (othermark != mark))
435 FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
436 " and " DESCLABEL_FMT,
437 label, DESCLABEL_ARGS(node, prop, mark),
438 DESCLABEL_ARGS(othernode, otherprop, othermark));
441 static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
442 struct node *node)
444 struct label *l;
445 struct property *prop;
447 for_each_label(node->labels, l)
448 check_duplicate_label(c, dti, l->label, node, NULL, NULL);
450 for_each_property(node, prop) {
451 struct marker *m = prop->val.markers;
453 for_each_label(prop->labels, l)
454 check_duplicate_label(c, dti, l->label, node, prop, NULL);
456 for_each_marker_of_type(m, LABEL)
457 check_duplicate_label(c, dti, m->ref, node, prop, m);
460 ERROR(duplicate_label, check_duplicate_label_node, NULL);
462 static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
463 struct node *node, const char *propname)
465 struct node *root = dti->dt;
466 struct property *prop;
467 struct marker *m;
468 cell_t phandle;
470 prop = get_property(node, propname);
471 if (!prop)
472 return 0;
474 if (prop->val.len != sizeof(cell_t)) {
475 FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
476 prop->val.len, prop->name);
477 return 0;
480 m = prop->val.markers;
481 for_each_marker_of_type(m, REF_PHANDLE) {
482 assert(m->offset == 0);
483 if (node != get_node_by_ref(root, m->ref))
484 /* "Set this node's phandle equal to some
485 * other node's phandle". That's nonsensical
486 * by construction. */ {
487 FAIL(c, dti, node, "%s is a reference to another node",
488 prop->name);
490 /* But setting this node's phandle equal to its own
491 * phandle is allowed - that means allocate a unique
492 * phandle for this node, even if it's not otherwise
493 * referenced. The value will be filled in later, so
494 * we treat it as having no phandle data for now. */
495 return 0;
498 phandle = propval_cell(prop);
500 if ((phandle == 0) || (phandle == -1)) {
501 FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
502 phandle, prop->name);
503 return 0;
506 return phandle;
509 static void check_explicit_phandles(struct check *c, struct dt_info *dti,
510 struct node *node)
512 struct node *root = dti->dt;
513 struct node *other;
514 cell_t phandle, linux_phandle;
516 /* Nothing should have assigned phandles yet */
517 assert(!node->phandle);
519 phandle = check_phandle_prop(c, dti, node, "phandle");
521 linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
523 if (!phandle && !linux_phandle)
524 /* No valid phandles; nothing further to check */
525 return;
527 if (linux_phandle && phandle && (phandle != linux_phandle))
528 FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
529 " properties");
531 if (linux_phandle && !phandle)
532 phandle = linux_phandle;
534 other = get_node_by_phandle(root, phandle);
535 if (other && (other != node)) {
536 FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
537 phandle, other->fullpath);
538 return;
541 node->phandle = phandle;
543 ERROR(explicit_phandles, check_explicit_phandles, NULL);
545 static void check_name_properties(struct check *c, struct dt_info *dti,
546 struct node *node)
548 struct property **pp, *prop = NULL;
550 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
551 if (streq((*pp)->name, "name")) {
552 prop = *pp;
553 break;
556 if (!prop)
557 return; /* No name property, that's fine */
559 if ((prop->val.len != node->basenamelen+1)
560 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
561 FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
562 " of base node name)", prop->val.val);
563 } else {
564 /* The name property is correct, and therefore redundant.
565 * Delete it */
566 *pp = prop->next;
567 free(prop->name);
568 data_free(prop->val);
569 free(prop);
572 ERROR_IF_NOT_STRING(name_is_string, "name");
573 ERROR(name_properties, check_name_properties, NULL, &name_is_string);
576 * Reference fixup functions
579 static void fixup_phandle_references(struct check *c, struct dt_info *dti,
580 struct node *node)
582 struct node *dt = dti->dt;
583 struct property *prop;
585 for_each_property(node, prop) {
586 struct marker *m = prop->val.markers;
587 struct node *refnode;
588 cell_t phandle;
590 for_each_marker_of_type(m, REF_PHANDLE) {
591 assert(m->offset + sizeof(cell_t) <= prop->val.len);
593 refnode = get_node_by_ref(dt, m->ref);
594 if (! refnode) {
595 if (!(dti->dtsflags & DTSF_PLUGIN))
596 FAIL(c, dti, node, "Reference to non-existent node or "
597 "label \"%s\"\n", m->ref);
598 else /* mark the entry as unresolved */
599 *((fdt32_t *)(prop->val.val + m->offset)) =
600 cpu_to_fdt32(0xffffffff);
601 continue;
604 phandle = get_node_phandle(dt, refnode);
605 *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
607 reference_node(refnode);
611 ERROR(phandle_references, fixup_phandle_references, NULL,
612 &duplicate_node_names, &explicit_phandles);
614 static void fixup_path_references(struct check *c, struct dt_info *dti,
615 struct node *node)
617 struct node *dt = dti->dt;
618 struct property *prop;
620 for_each_property(node, prop) {
621 struct marker *m = prop->val.markers;
622 struct node *refnode;
623 char *path;
625 for_each_marker_of_type(m, REF_PATH) {
626 assert(m->offset <= prop->val.len);
628 refnode = get_node_by_ref(dt, m->ref);
629 if (!refnode) {
630 FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
631 m->ref);
632 continue;
635 path = refnode->fullpath;
636 prop->val = data_insert_at_marker(prop->val, m, path,
637 strlen(path) + 1);
639 reference_node(refnode);
643 ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
645 static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
646 struct node *node)
648 if (generate_symbols && node->labels)
649 return;
650 if (node->omit_if_unused && !node->is_referenced)
651 delete_node(node);
653 ERROR(omit_unused_nodes, fixup_omit_unused_nodes, NULL, &phandle_references, &path_references);
656 * Semantic checks
658 WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
659 WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
660 WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
662 WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
663 WARNING_IF_NOT_STRING(model_is_string, "model");
664 WARNING_IF_NOT_STRING(status_is_string, "status");
665 WARNING_IF_NOT_STRING(label_is_string, "label");
667 WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
669 static void check_names_is_string_list(struct check *c, struct dt_info *dti,
670 struct node *node)
672 struct property *prop;
674 for_each_property(node, prop) {
675 const char *s = strrchr(prop->name, '-');
676 if (!s || !streq(s, "-names"))
677 continue;
679 c->data = prop->name;
680 check_is_string_list(c, dti, node);
683 WARNING(names_is_string_list, check_names_is_string_list, NULL);
685 static void check_alias_paths(struct check *c, struct dt_info *dti,
686 struct node *node)
688 struct property *prop;
690 if (!streq(node->name, "aliases"))
691 return;
693 for_each_property(node, prop) {
694 if (streq(prop->name, "phandle")
695 || streq(prop->name, "linux,phandle")) {
696 continue;
699 if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
700 FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
701 prop->val.val);
702 continue;
704 if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
705 FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
708 WARNING(alias_paths, check_alias_paths, NULL);
710 static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
711 struct node *node)
713 struct property *prop;
715 node->addr_cells = -1;
716 node->size_cells = -1;
718 prop = get_property(node, "#address-cells");
719 if (prop)
720 node->addr_cells = propval_cell(prop);
722 prop = get_property(node, "#size-cells");
723 if (prop)
724 node->size_cells = propval_cell(prop);
726 WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
727 &address_cells_is_cell, &size_cells_is_cell);
729 #define node_addr_cells(n) \
730 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
731 #define node_size_cells(n) \
732 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
734 static void check_reg_format(struct check *c, struct dt_info *dti,
735 struct node *node)
737 struct property *prop;
738 int addr_cells, size_cells, entrylen;
740 prop = get_property(node, "reg");
741 if (!prop)
742 return; /* No "reg", that's fine */
744 if (!node->parent) {
745 FAIL(c, dti, node, "Root node has a \"reg\" property");
746 return;
749 if (prop->val.len == 0)
750 FAIL_PROP(c, dti, node, prop, "property is empty");
752 addr_cells = node_addr_cells(node->parent);
753 size_cells = node_size_cells(node->parent);
754 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
756 if (!entrylen || (prop->val.len % entrylen) != 0)
757 FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
758 "(#address-cells == %d, #size-cells == %d)",
759 prop->val.len, addr_cells, size_cells);
761 WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
763 static void check_ranges_format(struct check *c, struct dt_info *dti,
764 struct node *node)
766 struct property *prop;
767 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
769 prop = get_property(node, "ranges");
770 if (!prop)
771 return;
773 if (!node->parent) {
774 FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
775 return;
778 p_addr_cells = node_addr_cells(node->parent);
779 p_size_cells = node_size_cells(node->parent);
780 c_addr_cells = node_addr_cells(node);
781 c_size_cells = node_size_cells(node);
782 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
784 if (prop->val.len == 0) {
785 if (p_addr_cells != c_addr_cells)
786 FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
787 "#address-cells (%d) differs from %s (%d)",
788 c_addr_cells, node->parent->fullpath,
789 p_addr_cells);
790 if (p_size_cells != c_size_cells)
791 FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
792 "#size-cells (%d) differs from %s (%d)",
793 c_size_cells, node->parent->fullpath,
794 p_size_cells);
795 } else if ((prop->val.len % entrylen) != 0) {
796 FAIL_PROP(c, dti, node, prop, "\"ranges\" property has invalid length (%d bytes) "
797 "(parent #address-cells == %d, child #address-cells == %d, "
798 "#size-cells == %d)", prop->val.len,
799 p_addr_cells, c_addr_cells, c_size_cells);
802 WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
804 static const struct bus_type pci_bus = {
805 .name = "PCI",
808 static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
810 struct property *prop;
811 cell_t *cells;
813 prop = get_property(node, "device_type");
814 if (!prop || !streq(prop->val.val, "pci"))
815 return;
817 node->bus = &pci_bus;
819 if (!strprefixeq(node->name, node->basenamelen, "pci") &&
820 !strprefixeq(node->name, node->basenamelen, "pcie"))
821 FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
823 prop = get_property(node, "ranges");
824 if (!prop)
825 FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
827 if (node_addr_cells(node) != 3)
828 FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
829 if (node_size_cells(node) != 2)
830 FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
832 prop = get_property(node, "bus-range");
833 if (!prop)
834 return;
836 if (prop->val.len != (sizeof(cell_t) * 2)) {
837 FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
838 return;
840 cells = (cell_t *)prop->val.val;
841 if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
842 FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
843 if (fdt32_to_cpu(cells[1]) > 0xff)
844 FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
846 WARNING(pci_bridge, check_pci_bridge, NULL,
847 &device_type_is_string, &addr_size_cells);
849 static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
851 struct property *prop;
852 unsigned int bus_num, min_bus, max_bus;
853 cell_t *cells;
855 if (!node->parent || (node->parent->bus != &pci_bus))
856 return;
858 prop = get_property(node, "reg");
859 if (!prop)
860 return;
862 cells = (cell_t *)prop->val.val;
863 bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
865 prop = get_property(node->parent, "bus-range");
866 if (!prop) {
867 min_bus = max_bus = 0;
868 } else {
869 cells = (cell_t *)prop->val.val;
870 min_bus = fdt32_to_cpu(cells[0]);
871 max_bus = fdt32_to_cpu(cells[0]);
873 if ((bus_num < min_bus) || (bus_num > max_bus))
874 FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
875 bus_num, min_bus, max_bus);
877 WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
879 static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
881 struct property *prop;
882 const char *unitname = get_unitname(node);
883 char unit_addr[5];
884 unsigned int dev, func, reg;
885 cell_t *cells;
887 if (!node->parent || (node->parent->bus != &pci_bus))
888 return;
890 prop = get_property(node, "reg");
891 if (!prop) {
892 FAIL(c, dti, node, "missing PCI reg property");
893 return;
896 cells = (cell_t *)prop->val.val;
897 if (cells[1] || cells[2])
898 FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
900 reg = fdt32_to_cpu(cells[0]);
901 dev = (reg & 0xf800) >> 11;
902 func = (reg & 0x700) >> 8;
904 if (reg & 0xff000000)
905 FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
906 if (reg & 0x000000ff)
907 FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
909 if (func == 0) {
910 snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
911 if (streq(unitname, unit_addr))
912 return;
915 snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
916 if (streq(unitname, unit_addr))
917 return;
919 FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
920 unit_addr);
922 WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
924 static const struct bus_type simple_bus = {
925 .name = "simple-bus",
928 static bool node_is_compatible(struct node *node, const char *compat)
930 struct property *prop;
931 const char *str, *end;
933 prop = get_property(node, "compatible");
934 if (!prop)
935 return false;
937 for (str = prop->val.val, end = str + prop->val.len; str < end;
938 str += strnlen(str, end - str) + 1) {
939 if (streq(str, compat))
940 return true;
942 return false;
945 static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
947 if (node_is_compatible(node, "simple-bus"))
948 node->bus = &simple_bus;
950 WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL,
951 &addr_size_cells, &compatible_is_string_list);
953 static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
955 struct property *prop;
956 const char *unitname = get_unitname(node);
957 char unit_addr[17];
958 unsigned int size;
959 uint64_t reg = 0;
960 cell_t *cells = NULL;
962 if (!node->parent || (node->parent->bus != &simple_bus))
963 return;
965 prop = get_property(node, "reg");
966 if (prop)
967 cells = (cell_t *)prop->val.val;
968 else {
969 prop = get_property(node, "ranges");
970 if (prop && prop->val.len)
971 /* skip of child address */
972 cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
975 if (!cells) {
976 if (node->parent->parent && !(node->bus == &simple_bus))
977 FAIL(c, dti, node, "missing or empty reg/ranges property");
978 return;
981 size = node_addr_cells(node->parent);
982 while (size--)
983 reg = (reg << 32) | fdt32_to_cpu(*(cells++));
985 snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
986 if (!streq(unitname, unit_addr))
987 FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
988 unit_addr);
990 WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
992 static const struct bus_type i2c_bus = {
993 .name = "i2c-bus",
996 static void check_i2c_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
998 if (strprefixeq(node->name, node->basenamelen, "i2c-bus") ||
999 strprefixeq(node->name, node->basenamelen, "i2c-arb")) {
1000 node->bus = &i2c_bus;
1001 } else if (strprefixeq(node->name, node->basenamelen, "i2c")) {
1002 struct node *child;
1003 for_each_child(node, child) {
1004 if (strprefixeq(child->name, node->basenamelen, "i2c-bus"))
1005 return;
1007 node->bus = &i2c_bus;
1008 } else
1009 return;
1011 if (!node->children)
1012 return;
1014 if (node_addr_cells(node) != 1)
1015 FAIL(c, dti, node, "incorrect #address-cells for I2C bus");
1016 if (node_size_cells(node) != 0)
1017 FAIL(c, dti, node, "incorrect #size-cells for I2C bus");
1020 WARNING(i2c_bus_bridge, check_i2c_bus_bridge, NULL, &addr_size_cells);
1022 static void check_i2c_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1024 struct property *prop;
1025 const char *unitname = get_unitname(node);
1026 char unit_addr[17];
1027 uint32_t reg = 0;
1028 int len;
1029 cell_t *cells = NULL;
1031 if (!node->parent || (node->parent->bus != &i2c_bus))
1032 return;
1034 prop = get_property(node, "reg");
1035 if (prop)
1036 cells = (cell_t *)prop->val.val;
1038 if (!cells) {
1039 FAIL(c, dti, node, "missing or empty reg property");
1040 return;
1043 reg = fdt32_to_cpu(*cells);
1044 snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1045 if (!streq(unitname, unit_addr))
1046 FAIL(c, dti, node, "I2C bus unit address format error, expected \"%s\"",
1047 unit_addr);
1049 for (len = prop->val.len; len > 0; len -= 4) {
1050 reg = fdt32_to_cpu(*(cells++));
1051 if (reg > 0x3ff)
1052 FAIL_PROP(c, dti, node, prop, "I2C address must be less than 10-bits, got \"0x%x\"",
1053 reg);
1057 WARNING(i2c_bus_reg, check_i2c_bus_reg, NULL, &reg_format, &i2c_bus_bridge);
1059 static const struct bus_type spi_bus = {
1060 .name = "spi-bus",
1063 static void check_spi_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1065 int spi_addr_cells = 1;
1067 if (strprefixeq(node->name, node->basenamelen, "spi")) {
1068 node->bus = &spi_bus;
1069 } else {
1070 /* Try to detect SPI buses which don't have proper node name */
1071 struct node *child;
1073 if (node_addr_cells(node) != 1 || node_size_cells(node) != 0)
1074 return;
1076 for_each_child(node, child) {
1077 struct property *prop;
1078 for_each_property(child, prop) {
1079 if (strprefixeq(prop->name, 4, "spi-")) {
1080 node->bus = &spi_bus;
1081 break;
1084 if (node->bus == &spi_bus)
1085 break;
1088 if (node->bus == &spi_bus && get_property(node, "reg"))
1089 FAIL(c, dti, node, "node name for SPI buses should be 'spi'");
1091 if (node->bus != &spi_bus || !node->children)
1092 return;
1094 if (get_property(node, "spi-slave"))
1095 spi_addr_cells = 0;
1096 if (node_addr_cells(node) != spi_addr_cells)
1097 FAIL(c, dti, node, "incorrect #address-cells for SPI bus");
1098 if (node_size_cells(node) != 0)
1099 FAIL(c, dti, node, "incorrect #size-cells for SPI bus");
1102 WARNING(spi_bus_bridge, check_spi_bus_bridge, NULL, &addr_size_cells);
1104 static void check_spi_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1106 struct property *prop;
1107 const char *unitname = get_unitname(node);
1108 char unit_addr[9];
1109 uint32_t reg = 0;
1110 cell_t *cells = NULL;
1112 if (!node->parent || (node->parent->bus != &spi_bus))
1113 return;
1115 if (get_property(node->parent, "spi-slave"))
1116 return;
1118 prop = get_property(node, "reg");
1119 if (prop)
1120 cells = (cell_t *)prop->val.val;
1122 if (!cells) {
1123 FAIL(c, dti, node, "missing or empty reg property");
1124 return;
1127 reg = fdt32_to_cpu(*cells);
1128 snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1129 if (!streq(unitname, unit_addr))
1130 FAIL(c, dti, node, "SPI bus unit address format error, expected \"%s\"",
1131 unit_addr);
1133 WARNING(spi_bus_reg, check_spi_bus_reg, NULL, &reg_format, &spi_bus_bridge);
1135 static void check_unit_address_format(struct check *c, struct dt_info *dti,
1136 struct node *node)
1138 const char *unitname = get_unitname(node);
1140 if (node->parent && node->parent->bus)
1141 return;
1143 if (!unitname[0])
1144 return;
1146 if (!strncmp(unitname, "0x", 2)) {
1147 FAIL(c, dti, node, "unit name should not have leading \"0x\"");
1148 /* skip over 0x for next test */
1149 unitname += 2;
1151 if (unitname[0] == '0' && isxdigit(unitname[1]))
1152 FAIL(c, dti, node, "unit name should not have leading 0s");
1154 WARNING(unit_address_format, check_unit_address_format, NULL,
1155 &node_name_format, &pci_bridge, &simple_bus_bridge);
1158 * Style checks
1160 static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
1161 struct node *node)
1163 struct property *reg, *ranges;
1165 if (!node->parent)
1166 return; /* Ignore root node */
1168 reg = get_property(node, "reg");
1169 ranges = get_property(node, "ranges");
1171 if (!reg && !ranges)
1172 return;
1174 if (node->parent->addr_cells == -1)
1175 FAIL(c, dti, node, "Relying on default #address-cells value");
1177 if (node->parent->size_cells == -1)
1178 FAIL(c, dti, node, "Relying on default #size-cells value");
1180 WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1181 &addr_size_cells);
1183 static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
1184 struct node *node)
1186 struct property *prop;
1187 struct node *child;
1188 bool has_reg = false;
1190 if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
1191 return;
1193 if (get_property(node, "ranges") || !node->children)
1194 return;
1196 for_each_child(node, child) {
1197 prop = get_property(child, "reg");
1198 if (prop)
1199 has_reg = true;
1202 if (!has_reg)
1203 FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
1205 WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
1207 static bool node_is_disabled(struct node *node)
1209 struct property *prop;
1211 prop = get_property(node, "status");
1212 if (prop) {
1213 char *str = prop->val.val;
1214 if (streq("disabled", str))
1215 return true;
1218 return false;
1221 static void check_unique_unit_address_common(struct check *c,
1222 struct dt_info *dti,
1223 struct node *node,
1224 bool disable_check)
1226 struct node *childa;
1228 if (node->addr_cells < 0 || node->size_cells < 0)
1229 return;
1231 if (!node->children)
1232 return;
1234 for_each_child(node, childa) {
1235 struct node *childb;
1236 const char *addr_a = get_unitname(childa);
1238 if (!strlen(addr_a))
1239 continue;
1241 if (disable_check && node_is_disabled(childa))
1242 continue;
1244 for_each_child(node, childb) {
1245 const char *addr_b = get_unitname(childb);
1246 if (childa == childb)
1247 break;
1249 if (disable_check && node_is_disabled(childb))
1250 continue;
1252 if (streq(addr_a, addr_b))
1253 FAIL(c, dti, childb, "duplicate unit-address (also used in node %s)", childa->fullpath);
1258 static void check_unique_unit_address(struct check *c, struct dt_info *dti,
1259 struct node *node)
1261 check_unique_unit_address_common(c, dti, node, false);
1263 WARNING(unique_unit_address, check_unique_unit_address, NULL, &avoid_default_addr_size);
1265 static void check_unique_unit_address_if_enabled(struct check *c, struct dt_info *dti,
1266 struct node *node)
1268 check_unique_unit_address_common(c, dti, node, true);
1270 CHECK_ENTRY(unique_unit_address_if_enabled, check_unique_unit_address_if_enabled,
1271 NULL, false, false, &avoid_default_addr_size);
1273 static void check_obsolete_chosen_interrupt_controller(struct check *c,
1274 struct dt_info *dti,
1275 struct node *node)
1277 struct node *dt = dti->dt;
1278 struct node *chosen;
1279 struct property *prop;
1281 if (node != dt)
1282 return;
1285 chosen = get_node_by_path(dt, "/chosen");
1286 if (!chosen)
1287 return;
1289 prop = get_property(chosen, "interrupt-controller");
1290 if (prop)
1291 FAIL_PROP(c, dti, node, prop,
1292 "/chosen has obsolete \"interrupt-controller\" property");
1294 WARNING(obsolete_chosen_interrupt_controller,
1295 check_obsolete_chosen_interrupt_controller, NULL);
1297 static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
1298 struct node *node)
1300 if (!streq(node->name, "chosen"))
1301 return;
1303 if (node->parent != dti->dt)
1304 FAIL(c, dti, node, "chosen node must be at root node");
1306 WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
1308 static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
1309 struct node *node)
1311 struct property *prop;
1313 if (!streq(node->name, "chosen"))
1314 return;
1316 prop = get_property(node, "bootargs");
1317 if (!prop)
1318 return;
1320 c->data = prop->name;
1321 check_is_string(c, dti, node);
1323 WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
1325 static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
1326 struct node *node)
1328 struct property *prop;
1330 if (!streq(node->name, "chosen"))
1331 return;
1333 prop = get_property(node, "stdout-path");
1334 if (!prop) {
1335 prop = get_property(node, "linux,stdout-path");
1336 if (!prop)
1337 return;
1338 FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
1341 c->data = prop->name;
1342 check_is_string(c, dti, node);
1344 WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
1346 struct provider {
1347 const char *prop_name;
1348 const char *cell_name;
1349 bool optional;
1352 static void check_property_phandle_args(struct check *c,
1353 struct dt_info *dti,
1354 struct node *node,
1355 struct property *prop,
1356 const struct provider *provider)
1358 struct node *root = dti->dt;
1359 int cell, cellsize = 0;
1361 if (prop->val.len % sizeof(cell_t)) {
1362 FAIL_PROP(c, dti, node, prop,
1363 "property size (%d) is invalid, expected multiple of %zu",
1364 prop->val.len, sizeof(cell_t));
1365 return;
1368 for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
1369 struct node *provider_node;
1370 struct property *cellprop;
1371 int phandle;
1373 phandle = propval_cell_n(prop, cell);
1375 * Some bindings use a cell value 0 or -1 to skip over optional
1376 * entries when each index position has a specific definition.
1378 if (phandle == 0 || phandle == -1) {
1379 /* Give up if this is an overlay with external references */
1380 if (dti->dtsflags & DTSF_PLUGIN)
1381 break;
1383 cellsize = 0;
1384 continue;
1387 /* If we have markers, verify the current cell is a phandle */
1388 if (prop->val.markers) {
1389 struct marker *m = prop->val.markers;
1390 for_each_marker_of_type(m, REF_PHANDLE) {
1391 if (m->offset == (cell * sizeof(cell_t)))
1392 break;
1394 if (!m)
1395 FAIL_PROP(c, dti, node, prop,
1396 "cell %d is not a phandle reference",
1397 cell);
1400 provider_node = get_node_by_phandle(root, phandle);
1401 if (!provider_node) {
1402 FAIL_PROP(c, dti, node, prop,
1403 "Could not get phandle node for (cell %d)",
1404 cell);
1405 break;
1408 cellprop = get_property(provider_node, provider->cell_name);
1409 if (cellprop) {
1410 cellsize = propval_cell(cellprop);
1411 } else if (provider->optional) {
1412 cellsize = 0;
1413 } else {
1414 FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
1415 provider->cell_name,
1416 provider_node->fullpath,
1417 prop->name, cell);
1418 break;
1421 if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
1422 FAIL_PROP(c, dti, node, prop,
1423 "property size (%d) too small for cell size %d",
1424 prop->val.len, cellsize);
1429 static void check_provider_cells_property(struct check *c,
1430 struct dt_info *dti,
1431 struct node *node)
1433 struct provider *provider = c->data;
1434 struct property *prop;
1436 prop = get_property(node, provider->prop_name);
1437 if (!prop)
1438 return;
1440 check_property_phandle_args(c, dti, node, prop, provider);
1442 #define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1443 static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1444 WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
1446 WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
1447 WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
1448 WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
1449 WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
1450 WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
1451 WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
1452 WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
1453 WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
1454 WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
1455 WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
1456 WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
1457 WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
1458 WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
1459 WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
1460 WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
1461 WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
1463 static bool prop_is_gpio(struct property *prop)
1465 char *str;
1468 * *-gpios and *-gpio can appear in property names,
1469 * so skip over any false matches (only one known ATM)
1471 if (strstr(prop->name, "nr-gpio"))
1472 return false;
1474 str = strrchr(prop->name, '-');
1475 if (str)
1476 str++;
1477 else
1478 str = prop->name;
1479 if (!(streq(str, "gpios") || streq(str, "gpio")))
1480 return false;
1482 return true;
1485 static void check_gpios_property(struct check *c,
1486 struct dt_info *dti,
1487 struct node *node)
1489 struct property *prop;
1491 /* Skip GPIO hog nodes which have 'gpios' property */
1492 if (get_property(node, "gpio-hog"))
1493 return;
1495 for_each_property(node, prop) {
1496 struct provider provider;
1498 if (!prop_is_gpio(prop))
1499 continue;
1501 provider.prop_name = prop->name;
1502 provider.cell_name = "#gpio-cells";
1503 provider.optional = false;
1504 check_property_phandle_args(c, dti, node, prop, &provider);
1508 WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
1510 static void check_deprecated_gpio_property(struct check *c,
1511 struct dt_info *dti,
1512 struct node *node)
1514 struct property *prop;
1516 for_each_property(node, prop) {
1517 char *str;
1519 if (!prop_is_gpio(prop))
1520 continue;
1522 str = strstr(prop->name, "gpio");
1523 if (!streq(str, "gpio"))
1524 continue;
1526 FAIL_PROP(c, dti, node, prop,
1527 "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
1531 CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
1533 static bool node_is_interrupt_provider(struct node *node)
1535 struct property *prop;
1537 prop = get_property(node, "interrupt-controller");
1538 if (prop)
1539 return true;
1541 prop = get_property(node, "interrupt-map");
1542 if (prop)
1543 return true;
1545 return false;
1547 static void check_interrupts_property(struct check *c,
1548 struct dt_info *dti,
1549 struct node *node)
1551 struct node *root = dti->dt;
1552 struct node *irq_node = NULL, *parent = node;
1553 struct property *irq_prop, *prop = NULL;
1554 int irq_cells, phandle;
1556 irq_prop = get_property(node, "interrupts");
1557 if (!irq_prop)
1558 return;
1560 if (irq_prop->val.len % sizeof(cell_t))
1561 FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1562 irq_prop->val.len, sizeof(cell_t));
1564 while (parent && !prop) {
1565 if (parent != node && node_is_interrupt_provider(parent)) {
1566 irq_node = parent;
1567 break;
1570 prop = get_property(parent, "interrupt-parent");
1571 if (prop) {
1572 phandle = propval_cell(prop);
1573 if ((phandle == 0) || (phandle == -1)) {
1574 /* Give up if this is an overlay with
1575 * external references */
1576 if (dti->dtsflags & DTSF_PLUGIN)
1577 return;
1578 FAIL_PROP(c, dti, parent, prop, "Invalid phandle");
1579 continue;
1582 irq_node = get_node_by_phandle(root, phandle);
1583 if (!irq_node) {
1584 FAIL_PROP(c, dti, parent, prop, "Bad phandle");
1585 return;
1587 if (!node_is_interrupt_provider(irq_node))
1588 FAIL(c, dti, irq_node,
1589 "Missing interrupt-controller or interrupt-map property");
1591 break;
1594 parent = parent->parent;
1597 if (!irq_node) {
1598 FAIL(c, dti, node, "Missing interrupt-parent");
1599 return;
1602 prop = get_property(irq_node, "#interrupt-cells");
1603 if (!prop) {
1604 FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
1605 return;
1608 irq_cells = propval_cell(prop);
1609 if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
1610 FAIL_PROP(c, dti, node, prop,
1611 "size is (%d), expected multiple of %d",
1612 irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
1615 WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1617 static const struct bus_type graph_port_bus = {
1618 .name = "graph-port",
1621 static const struct bus_type graph_ports_bus = {
1622 .name = "graph-ports",
1625 static void check_graph_nodes(struct check *c, struct dt_info *dti,
1626 struct node *node)
1628 struct node *child;
1630 for_each_child(node, child) {
1631 if (!(strprefixeq(child->name, child->basenamelen, "endpoint") ||
1632 get_property(child, "remote-endpoint")))
1633 continue;
1635 node->bus = &graph_port_bus;
1637 /* The parent of 'port' nodes can be either 'ports' or a device */
1638 if (!node->parent->bus &&
1639 (streq(node->parent->name, "ports") || get_property(node, "reg")))
1640 node->parent->bus = &graph_ports_bus;
1642 break;
1646 WARNING(graph_nodes, check_graph_nodes, NULL);
1648 static void check_graph_child_address(struct check *c, struct dt_info *dti,
1649 struct node *node)
1651 int cnt = 0;
1652 struct node *child;
1654 if (node->bus != &graph_ports_bus && node->bus != &graph_port_bus)
1655 return;
1657 for_each_child(node, child) {
1658 struct property *prop = get_property(child, "reg");
1660 /* No error if we have any non-zero unit address */
1661 if (prop && propval_cell(prop) != 0)
1662 return;
1664 cnt++;
1667 if (cnt == 1 && node->addr_cells != -1)
1668 FAIL(c, dti, node, "graph node has single child node '%s', #address-cells/#size-cells are not necessary",
1669 node->children->name);
1671 WARNING(graph_child_address, check_graph_child_address, NULL, &graph_nodes);
1673 static void check_graph_reg(struct check *c, struct dt_info *dti,
1674 struct node *node)
1676 char unit_addr[9];
1677 const char *unitname = get_unitname(node);
1678 struct property *prop;
1680 prop = get_property(node, "reg");
1681 if (!prop || !unitname)
1682 return;
1684 if (!(prop->val.val && prop->val.len == sizeof(cell_t))) {
1685 FAIL(c, dti, node, "graph node malformed 'reg' property");
1686 return;
1689 snprintf(unit_addr, sizeof(unit_addr), "%x", propval_cell(prop));
1690 if (!streq(unitname, unit_addr))
1691 FAIL(c, dti, node, "graph node unit address error, expected \"%s\"",
1692 unit_addr);
1694 if (node->parent->addr_cells != 1)
1695 FAIL_PROP(c, dti, node, get_property(node, "#address-cells"),
1696 "graph node '#address-cells' is %d, must be 1",
1697 node->parent->addr_cells);
1698 if (node->parent->size_cells != 0)
1699 FAIL_PROP(c, dti, node, get_property(node, "#size-cells"),
1700 "graph node '#size-cells' is %d, must be 0",
1701 node->parent->size_cells);
1704 static void check_graph_port(struct check *c, struct dt_info *dti,
1705 struct node *node)
1707 if (node->bus != &graph_port_bus)
1708 return;
1710 if (!strprefixeq(node->name, node->basenamelen, "port"))
1711 FAIL(c, dti, node, "graph port node name should be 'port'");
1713 check_graph_reg(c, dti, node);
1715 WARNING(graph_port, check_graph_port, NULL, &graph_nodes);
1717 static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,
1718 struct node *endpoint)
1720 int phandle;
1721 struct node *node;
1722 struct property *prop;
1724 prop = get_property(endpoint, "remote-endpoint");
1725 if (!prop)
1726 return NULL;
1728 phandle = propval_cell(prop);
1729 /* Give up if this is an overlay with external references */
1730 if (phandle == 0 || phandle == -1)
1731 return NULL;
1733 node = get_node_by_phandle(dti->dt, phandle);
1734 if (!node)
1735 FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
1737 return node;
1740 static void check_graph_endpoint(struct check *c, struct dt_info *dti,
1741 struct node *node)
1743 struct node *remote_node;
1745 if (!node->parent || node->parent->bus != &graph_port_bus)
1746 return;
1748 if (!strprefixeq(node->name, node->basenamelen, "endpoint"))
1749 FAIL(c, dti, node, "graph endpoint node name should be 'endpoint'");
1751 check_graph_reg(c, dti, node);
1753 remote_node = get_remote_endpoint(c, dti, node);
1754 if (!remote_node)
1755 return;
1757 if (get_remote_endpoint(c, dti, remote_node) != node)
1758 FAIL(c, dti, node, "graph connection to node '%s' is not bidirectional",
1759 remote_node->fullpath);
1761 WARNING(graph_endpoint, check_graph_endpoint, NULL, &graph_nodes);
1763 static struct check *check_table[] = {
1764 &duplicate_node_names, &duplicate_property_names,
1765 &node_name_chars, &node_name_format, &property_name_chars,
1766 &name_is_string, &name_properties,
1768 &duplicate_label,
1770 &explicit_phandles,
1771 &phandle_references, &path_references,
1772 &omit_unused_nodes,
1774 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
1775 &device_type_is_string, &model_is_string, &status_is_string,
1776 &label_is_string,
1778 &compatible_is_string_list, &names_is_string_list,
1780 &property_name_chars_strict,
1781 &node_name_chars_strict,
1783 &addr_size_cells, &reg_format, &ranges_format,
1785 &unit_address_vs_reg,
1786 &unit_address_format,
1788 &pci_bridge,
1789 &pci_device_reg,
1790 &pci_device_bus_num,
1792 &simple_bus_bridge,
1793 &simple_bus_reg,
1795 &i2c_bus_bridge,
1796 &i2c_bus_reg,
1798 &spi_bus_bridge,
1799 &spi_bus_reg,
1801 &avoid_default_addr_size,
1802 &avoid_unnecessary_addr_size,
1803 &unique_unit_address,
1804 &unique_unit_address_if_enabled,
1805 &obsolete_chosen_interrupt_controller,
1806 &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
1808 &clocks_property,
1809 &cooling_device_property,
1810 &dmas_property,
1811 &hwlocks_property,
1812 &interrupts_extended_property,
1813 &io_channels_property,
1814 &iommus_property,
1815 &mboxes_property,
1816 &msi_parent_property,
1817 &mux_controls_property,
1818 &phys_property,
1819 &power_domains_property,
1820 &pwms_property,
1821 &resets_property,
1822 &sound_dai_property,
1823 &thermal_sensors_property,
1825 &deprecated_gpio_property,
1826 &gpios_property,
1827 &interrupts_property,
1829 &alias_paths,
1831 &graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
1833 &always_fail,
1836 static void enable_warning_error(struct check *c, bool warn, bool error)
1838 int i;
1840 /* Raising level, also raise it for prereqs */
1841 if ((warn && !c->warn) || (error && !c->error))
1842 for (i = 0; i < c->num_prereqs; i++)
1843 enable_warning_error(c->prereq[i], warn, error);
1845 c->warn = c->warn || warn;
1846 c->error = c->error || error;
1849 static void disable_warning_error(struct check *c, bool warn, bool error)
1851 int i;
1853 /* Lowering level, also lower it for things this is the prereq
1854 * for */
1855 if ((warn && c->warn) || (error && c->error)) {
1856 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1857 struct check *cc = check_table[i];
1858 int j;
1860 for (j = 0; j < cc->num_prereqs; j++)
1861 if (cc->prereq[j] == c)
1862 disable_warning_error(cc, warn, error);
1866 c->warn = c->warn && !warn;
1867 c->error = c->error && !error;
1870 void parse_checks_option(bool warn, bool error, const char *arg)
1872 int i;
1873 const char *name = arg;
1874 bool enable = true;
1876 if ((strncmp(arg, "no-", 3) == 0)
1877 || (strncmp(arg, "no_", 3) == 0)) {
1878 name = arg + 3;
1879 enable = false;
1882 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1883 struct check *c = check_table[i];
1885 if (streq(c->name, name)) {
1886 if (enable)
1887 enable_warning_error(c, warn, error);
1888 else
1889 disable_warning_error(c, warn, error);
1890 return;
1894 die("Unrecognized check name \"%s\"\n", name);
1897 void process_checks(bool force, struct dt_info *dti)
1899 int i;
1900 int error = 0;
1902 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1903 struct check *c = check_table[i];
1905 if (c->warn || c->error)
1906 error = error || run_check(c, dti);
1909 if (error) {
1910 if (!force) {
1911 fprintf(stderr, "ERROR: Input tree has errors, aborting "
1912 "(use -f to force output)\n");
1913 exit(2);
1914 } else if (quiet < 3) {
1915 fprintf(stderr, "Warning: Input tree has errors, "
1916 "output forced\n");