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
24 #define TRACE(c, ...) \
26 fprintf(stderr, "=== %s: ", (c)->name); \
27 fprintf(stderr, __VA_ARGS__); \
28 fprintf(stderr, "\n"); \
31 #define TRACE(c, fmt, ...) do { } while (0)
43 typedef void (*check_fn
)(struct check
*c
, struct dt_info
*dti
, struct node
*node
);
50 enum checkstatus status
;
53 struct check
**prereq
;
56 #define CHECK_ENTRY(_nm, _fn, _d, _w, _e, ...) \
57 static struct check *_nm##_prereqs[] = { __VA_ARGS__ }; \
58 static struct check _nm = { \
64 .status = UNCHECKED, \
65 .num_prereqs = ARRAY_SIZE(_nm##_prereqs), \
66 .prereq = _nm##_prereqs, \
68 #define WARNING(_nm, _fn, _d, ...) \
69 CHECK_ENTRY(_nm, _fn, _d, true, false, __VA_ARGS__)
70 #define ERROR(_nm, _fn, _d, ...) \
71 CHECK_ENTRY(_nm, _fn, _d, false, true, __VA_ARGS__)
72 #define CHECK(_nm, _fn, _d, ...) \
73 CHECK_ENTRY(_nm, _fn, _d, false, false, __VA_ARGS__)
75 static inline void PRINTF(3, 4) check_msg(struct check
*c
, struct dt_info
*dti
,
81 if ((c
->warn
&& (quiet
< 1))
82 || (c
->error
&& (quiet
< 2))) {
83 fprintf(stderr
, "%s: %s (%s): ",
84 strcmp(dti
->outname
, "-") ? dti
->outname
: "<stdout>",
85 (c
->error
) ? "ERROR" : "Warning", c
->name
);
86 vfprintf(stderr
, fmt
, ap
);
87 fprintf(stderr
, "\n");
92 #define FAIL(c, dti, ...) \
94 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
95 (c)->status = FAILED; \
96 check_msg((c), dti, __VA_ARGS__); \
99 static void check_nodes_props(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
103 TRACE(c
, "%s", node
->fullpath
);
107 for_each_child(node
, child
)
108 check_nodes_props(c
, dti
, child
);
111 static bool run_check(struct check
*c
, struct dt_info
*dti
)
113 struct node
*dt
= dti
->dt
;
117 assert(!c
->inprogress
);
119 if (c
->status
!= UNCHECKED
)
122 c
->inprogress
= true;
124 for (i
= 0; i
< c
->num_prereqs
; i
++) {
125 struct check
*prq
= c
->prereq
[i
];
126 error
= error
|| run_check(prq
, dti
);
127 if (prq
->status
!= PASSED
) {
129 check_msg(c
, dti
, "Failed prerequisite '%s'",
134 if (c
->status
!= UNCHECKED
)
137 check_nodes_props(c
, dti
, dt
);
139 if (c
->status
== UNCHECKED
)
142 TRACE(c
, "\tCompleted, status %d", c
->status
);
145 c
->inprogress
= false;
146 if ((c
->status
!= PASSED
) && (c
->error
))
152 * Utility check functions
155 /* A check which always fails, for testing purposes only */
156 static inline void check_always_fail(struct check
*c
, struct dt_info
*dti
,
159 FAIL(c
, dti
, "always_fail check");
161 CHECK(always_fail
, check_always_fail
, NULL
);
163 static void check_is_string(struct check
*c
, struct dt_info
*dti
,
166 struct property
*prop
;
167 char *propname
= c
->data
;
169 prop
= get_property(node
, propname
);
171 return; /* Not present, assumed ok */
173 if (!data_is_one_string(prop
->val
))
174 FAIL(c
, dti
, "\"%s\" property in %s is not a string",
175 propname
, node
->fullpath
);
177 #define WARNING_IF_NOT_STRING(nm, propname) \
178 WARNING(nm, check_is_string, (propname))
179 #define ERROR_IF_NOT_STRING(nm, propname) \
180 ERROR(nm, check_is_string, (propname))
182 static void check_is_cell(struct check
*c
, struct dt_info
*dti
,
185 struct property
*prop
;
186 char *propname
= c
->data
;
188 prop
= get_property(node
, propname
);
190 return; /* Not present, assumed ok */
192 if (prop
->val
.len
!= sizeof(cell_t
))
193 FAIL(c
, dti
, "\"%s\" property in %s is not a single cell",
194 propname
, node
->fullpath
);
196 #define WARNING_IF_NOT_CELL(nm, propname) \
197 WARNING(nm, check_is_cell, (propname))
198 #define ERROR_IF_NOT_CELL(nm, propname) \
199 ERROR(nm, check_is_cell, (propname))
202 * Structural check functions
205 static void check_duplicate_node_names(struct check
*c
, struct dt_info
*dti
,
208 struct node
*child
, *child2
;
210 for_each_child(node
, child
)
211 for (child2
= child
->next_sibling
;
213 child2
= child2
->next_sibling
)
214 if (streq(child
->name
, child2
->name
))
215 FAIL(c
, dti
, "Duplicate node name %s",
218 ERROR(duplicate_node_names
, check_duplicate_node_names
, NULL
);
220 static void check_duplicate_property_names(struct check
*c
, struct dt_info
*dti
,
223 struct property
*prop
, *prop2
;
225 for_each_property(node
, prop
) {
226 for (prop2
= prop
->next
; prop2
; prop2
= prop2
->next
) {
229 if (streq(prop
->name
, prop2
->name
))
230 FAIL(c
, dti
, "Duplicate property name %s in %s",
231 prop
->name
, node
->fullpath
);
235 ERROR(duplicate_property_names
, check_duplicate_property_names
, NULL
);
237 #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
238 #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
239 #define DIGITS "0123456789"
240 #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
241 #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
243 static void check_node_name_chars(struct check
*c
, struct dt_info
*dti
,
246 int n
= strspn(node
->name
, c
->data
);
248 if (n
< strlen(node
->name
))
249 FAIL(c
, dti
, "Bad character '%c' in node %s",
250 node
->name
[n
], node
->fullpath
);
252 ERROR(node_name_chars
, check_node_name_chars
, PROPNODECHARS
"@");
254 static void check_node_name_chars_strict(struct check
*c
, struct dt_info
*dti
,
257 int n
= strspn(node
->name
, c
->data
);
259 if (n
< node
->basenamelen
)
260 FAIL(c
, dti
, "Character '%c' not recommended in node %s",
261 node
->name
[n
], node
->fullpath
);
263 CHECK(node_name_chars_strict
, check_node_name_chars_strict
, PROPNODECHARSSTRICT
);
265 static void check_node_name_format(struct check
*c
, struct dt_info
*dti
,
268 if (strchr(get_unitname(node
), '@'))
269 FAIL(c
, dti
, "Node %s has multiple '@' characters in name",
272 ERROR(node_name_format
, check_node_name_format
, NULL
, &node_name_chars
);
274 static void check_unit_address_vs_reg(struct check
*c
, struct dt_info
*dti
,
277 const char *unitname
= get_unitname(node
);
278 struct property
*prop
= get_property(node
, "reg");
281 prop
= get_property(node
, "ranges");
282 if (prop
&& !prop
->val
.len
)
288 FAIL(c
, dti
, "Node %s has a reg or ranges property, but no unit name",
292 FAIL(c
, dti
, "Node %s has a unit name, but no reg property",
296 WARNING(unit_address_vs_reg
, check_unit_address_vs_reg
, NULL
);
298 static void check_property_name_chars(struct check
*c
, struct dt_info
*dti
,
301 struct property
*prop
;
303 for_each_property(node
, prop
) {
304 int n
= strspn(prop
->name
, c
->data
);
306 if (n
< strlen(prop
->name
))
307 FAIL(c
, dti
, "Bad character '%c' in property name \"%s\", node %s",
308 prop
->name
[n
], prop
->name
, node
->fullpath
);
311 ERROR(property_name_chars
, check_property_name_chars
, PROPNODECHARS
);
313 static void check_property_name_chars_strict(struct check
*c
,
317 struct property
*prop
;
319 for_each_property(node
, prop
) {
320 const char *name
= prop
->name
;
321 int n
= strspn(name
, c
->data
);
323 if (n
== strlen(prop
->name
))
326 /* Certain names are whitelisted */
327 if (streq(name
, "device_type"))
331 * # is only allowed at the beginning of property names not counting
334 if (name
[n
] == '#' && ((n
== 0) || (name
[n
-1] == ','))) {
336 n
= strspn(name
, c
->data
);
338 if (n
< strlen(name
))
339 FAIL(c
, dti
, "Character '%c' not recommended in property name \"%s\", node %s",
340 name
[n
], prop
->name
, node
->fullpath
);
343 CHECK(property_name_chars_strict
, check_property_name_chars_strict
, PROPNODECHARSSTRICT
);
345 #define DESCLABEL_FMT "%s%s%s%s%s"
346 #define DESCLABEL_ARGS(node,prop,mark) \
347 ((mark) ? "value of " : ""), \
348 ((prop) ? "'" : ""), \
349 ((prop) ? (prop)->name : ""), \
350 ((prop) ? "' in " : ""), (node)->fullpath
352 static void check_duplicate_label(struct check
*c
, struct dt_info
*dti
,
353 const char *label
, struct node
*node
,
354 struct property
*prop
, struct marker
*mark
)
356 struct node
*dt
= dti
->dt
;
357 struct node
*othernode
= NULL
;
358 struct property
*otherprop
= NULL
;
359 struct marker
*othermark
= NULL
;
361 othernode
= get_node_by_label(dt
, label
);
364 otherprop
= get_property_by_label(dt
, label
, &othernode
);
366 othermark
= get_marker_label(dt
, label
, &othernode
,
372 if ((othernode
!= node
) || (otherprop
!= prop
) || (othermark
!= mark
))
373 FAIL(c
, dti
, "Duplicate label '%s' on " DESCLABEL_FMT
374 " and " DESCLABEL_FMT
,
375 label
, DESCLABEL_ARGS(node
, prop
, mark
),
376 DESCLABEL_ARGS(othernode
, otherprop
, othermark
));
379 static void check_duplicate_label_node(struct check
*c
, struct dt_info
*dti
,
383 struct property
*prop
;
385 for_each_label(node
->labels
, l
)
386 check_duplicate_label(c
, dti
, l
->label
, node
, NULL
, NULL
);
388 for_each_property(node
, prop
) {
389 struct marker
*m
= prop
->val
.markers
;
391 for_each_label(prop
->labels
, l
)
392 check_duplicate_label(c
, dti
, l
->label
, node
, prop
, NULL
);
394 for_each_marker_of_type(m
, LABEL
)
395 check_duplicate_label(c
, dti
, m
->ref
, node
, prop
, m
);
398 ERROR(duplicate_label
, check_duplicate_label_node
, NULL
);
400 static cell_t
check_phandle_prop(struct check
*c
, struct dt_info
*dti
,
401 struct node
*node
, const char *propname
)
403 struct node
*root
= dti
->dt
;
404 struct property
*prop
;
408 prop
= get_property(node
, propname
);
412 if (prop
->val
.len
!= sizeof(cell_t
)) {
413 FAIL(c
, dti
, "%s has bad length (%d) %s property",
414 node
->fullpath
, prop
->val
.len
, prop
->name
);
418 m
= prop
->val
.markers
;
419 for_each_marker_of_type(m
, REF_PHANDLE
) {
420 assert(m
->offset
== 0);
421 if (node
!= get_node_by_ref(root
, m
->ref
))
422 /* "Set this node's phandle equal to some
423 * other node's phandle". That's nonsensical
424 * by construction. */ {
425 FAIL(c
, dti
, "%s in %s is a reference to another node",
426 prop
->name
, node
->fullpath
);
428 /* But setting this node's phandle equal to its own
429 * phandle is allowed - that means allocate a unique
430 * phandle for this node, even if it's not otherwise
431 * referenced. The value will be filled in later, so
432 * we treat it as having no phandle data for now. */
436 phandle
= propval_cell(prop
);
438 if ((phandle
== 0) || (phandle
== -1)) {
439 FAIL(c
, dti
, "%s has bad value (0x%x) in %s property",
440 node
->fullpath
, phandle
, prop
->name
);
447 static void check_explicit_phandles(struct check
*c
, struct dt_info
*dti
,
450 struct node
*root
= dti
->dt
;
452 cell_t phandle
, linux_phandle
;
454 /* Nothing should have assigned phandles yet */
455 assert(!node
->phandle
);
457 phandle
= check_phandle_prop(c
, dti
, node
, "phandle");
459 linux_phandle
= check_phandle_prop(c
, dti
, node
, "linux,phandle");
461 if (!phandle
&& !linux_phandle
)
462 /* No valid phandles; nothing further to check */
465 if (linux_phandle
&& phandle
&& (phandle
!= linux_phandle
))
466 FAIL(c
, dti
, "%s has mismatching 'phandle' and 'linux,phandle'"
467 " properties", node
->fullpath
);
469 if (linux_phandle
&& !phandle
)
470 phandle
= linux_phandle
;
472 other
= get_node_by_phandle(root
, phandle
);
473 if (other
&& (other
!= node
)) {
474 FAIL(c
, dti
, "%s has duplicated phandle 0x%x (seen before at %s)",
475 node
->fullpath
, phandle
, other
->fullpath
);
479 node
->phandle
= phandle
;
481 ERROR(explicit_phandles
, check_explicit_phandles
, NULL
);
483 static void check_name_properties(struct check
*c
, struct dt_info
*dti
,
486 struct property
**pp
, *prop
= NULL
;
488 for (pp
= &node
->proplist
; *pp
; pp
= &((*pp
)->next
))
489 if (streq((*pp
)->name
, "name")) {
495 return; /* No name property, that's fine */
497 if ((prop
->val
.len
!= node
->basenamelen
+1)
498 || (memcmp(prop
->val
.val
, node
->name
, node
->basenamelen
) != 0)) {
499 FAIL(c
, dti
, "\"name\" property in %s is incorrect (\"%s\" instead"
500 " of base node name)", node
->fullpath
, prop
->val
.val
);
502 /* The name property is correct, and therefore redundant.
506 data_free(prop
->val
);
510 ERROR_IF_NOT_STRING(name_is_string
, "name");
511 ERROR(name_properties
, check_name_properties
, NULL
, &name_is_string
);
514 * Reference fixup functions
517 static void fixup_phandle_references(struct check
*c
, struct dt_info
*dti
,
520 struct node
*dt
= dti
->dt
;
521 struct property
*prop
;
523 for_each_property(node
, prop
) {
524 struct marker
*m
= prop
->val
.markers
;
525 struct node
*refnode
;
528 for_each_marker_of_type(m
, REF_PHANDLE
) {
529 assert(m
->offset
+ sizeof(cell_t
) <= prop
->val
.len
);
531 refnode
= get_node_by_ref(dt
, m
->ref
);
533 if (!(dti
->dtsflags
& DTSF_PLUGIN
))
534 FAIL(c
, dti
, "Reference to non-existent node or "
535 "label \"%s\"\n", m
->ref
);
536 else /* mark the entry as unresolved */
537 *((fdt32_t
*)(prop
->val
.val
+ m
->offset
)) =
538 cpu_to_fdt32(0xffffffff);
542 phandle
= get_node_phandle(dt
, refnode
);
543 *((fdt32_t
*)(prop
->val
.val
+ m
->offset
)) = cpu_to_fdt32(phandle
);
547 ERROR(phandle_references
, fixup_phandle_references
, NULL
,
548 &duplicate_node_names
, &explicit_phandles
);
550 static void fixup_path_references(struct check
*c
, struct dt_info
*dti
,
553 struct node
*dt
= dti
->dt
;
554 struct property
*prop
;
556 for_each_property(node
, prop
) {
557 struct marker
*m
= prop
->val
.markers
;
558 struct node
*refnode
;
561 for_each_marker_of_type(m
, REF_PATH
) {
562 assert(m
->offset
<= prop
->val
.len
);
564 refnode
= get_node_by_ref(dt
, m
->ref
);
566 FAIL(c
, dti
, "Reference to non-existent node or label \"%s\"\n",
571 path
= refnode
->fullpath
;
572 prop
->val
= data_insert_at_marker(prop
->val
, m
, path
,
577 ERROR(path_references
, fixup_path_references
, NULL
, &duplicate_node_names
);
582 WARNING_IF_NOT_CELL(address_cells_is_cell
, "#address-cells");
583 WARNING_IF_NOT_CELL(size_cells_is_cell
, "#size-cells");
584 WARNING_IF_NOT_CELL(interrupt_cells_is_cell
, "#interrupt-cells");
586 WARNING_IF_NOT_STRING(device_type_is_string
, "device_type");
587 WARNING_IF_NOT_STRING(model_is_string
, "model");
588 WARNING_IF_NOT_STRING(status_is_string
, "status");
590 static void fixup_addr_size_cells(struct check
*c
, struct dt_info
*dti
,
593 struct property
*prop
;
595 node
->addr_cells
= -1;
596 node
->size_cells
= -1;
598 prop
= get_property(node
, "#address-cells");
600 node
->addr_cells
= propval_cell(prop
);
602 prop
= get_property(node
, "#size-cells");
604 node
->size_cells
= propval_cell(prop
);
606 WARNING(addr_size_cells
, fixup_addr_size_cells
, NULL
,
607 &address_cells_is_cell
, &size_cells_is_cell
);
609 #define node_addr_cells(n) \
610 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
611 #define node_size_cells(n) \
612 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
614 static void check_reg_format(struct check
*c
, struct dt_info
*dti
,
617 struct property
*prop
;
618 int addr_cells
, size_cells
, entrylen
;
620 prop
= get_property(node
, "reg");
622 return; /* No "reg", that's fine */
625 FAIL(c
, dti
, "Root node has a \"reg\" property");
629 if (prop
->val
.len
== 0)
630 FAIL(c
, dti
, "\"reg\" property in %s is empty", node
->fullpath
);
632 addr_cells
= node_addr_cells(node
->parent
);
633 size_cells
= node_size_cells(node
->parent
);
634 entrylen
= (addr_cells
+ size_cells
) * sizeof(cell_t
);
636 if (!entrylen
|| (prop
->val
.len
% entrylen
) != 0)
637 FAIL(c
, dti
, "\"reg\" property in %s has invalid length (%d bytes) "
638 "(#address-cells == %d, #size-cells == %d)",
639 node
->fullpath
, prop
->val
.len
, addr_cells
, size_cells
);
641 WARNING(reg_format
, check_reg_format
, NULL
, &addr_size_cells
);
643 static void check_ranges_format(struct check
*c
, struct dt_info
*dti
,
646 struct property
*prop
;
647 int c_addr_cells
, p_addr_cells
, c_size_cells
, p_size_cells
, entrylen
;
649 prop
= get_property(node
, "ranges");
654 FAIL(c
, dti
, "Root node has a \"ranges\" property");
658 p_addr_cells
= node_addr_cells(node
->parent
);
659 p_size_cells
= node_size_cells(node
->parent
);
660 c_addr_cells
= node_addr_cells(node
);
661 c_size_cells
= node_size_cells(node
);
662 entrylen
= (p_addr_cells
+ c_addr_cells
+ c_size_cells
) * sizeof(cell_t
);
664 if (prop
->val
.len
== 0) {
665 if (p_addr_cells
!= c_addr_cells
)
666 FAIL(c
, dti
, "%s has empty \"ranges\" property but its "
667 "#address-cells (%d) differs from %s (%d)",
668 node
->fullpath
, c_addr_cells
, node
->parent
->fullpath
,
670 if (p_size_cells
!= c_size_cells
)
671 FAIL(c
, dti
, "%s has empty \"ranges\" property but its "
672 "#size-cells (%d) differs from %s (%d)",
673 node
->fullpath
, c_size_cells
, node
->parent
->fullpath
,
675 } else if ((prop
->val
.len
% entrylen
) != 0) {
676 FAIL(c
, dti
, "\"ranges\" property in %s has invalid length (%d bytes) "
677 "(parent #address-cells == %d, child #address-cells == %d, "
678 "#size-cells == %d)", node
->fullpath
, prop
->val
.len
,
679 p_addr_cells
, c_addr_cells
, c_size_cells
);
682 WARNING(ranges_format
, check_ranges_format
, NULL
, &addr_size_cells
);
684 static const struct bus_type pci_bus
= {
688 static void check_pci_bridge(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
690 struct property
*prop
;
693 prop
= get_property(node
, "device_type");
694 if (!prop
|| !streq(prop
->val
.val
, "pci"))
697 node
->bus
= &pci_bus
;
699 if (!strneq(node
->name
, "pci", node
->basenamelen
) &&
700 !strneq(node
->name
, "pcie", node
->basenamelen
))
701 FAIL(c
, dti
, "Node %s node name is not \"pci\" or \"pcie\"",
704 prop
= get_property(node
, "ranges");
706 FAIL(c
, dti
, "Node %s missing ranges for PCI bridge (or not a bridge)",
709 if (node_addr_cells(node
) != 3)
710 FAIL(c
, dti
, "Node %s incorrect #address-cells for PCI bridge",
712 if (node_size_cells(node
) != 2)
713 FAIL(c
, dti
, "Node %s incorrect #size-cells for PCI bridge",
716 prop
= get_property(node
, "bus-range");
718 FAIL(c
, dti
, "Node %s missing bus-range for PCI bridge",
722 if (prop
->val
.len
!= (sizeof(cell_t
) * 2)) {
723 FAIL(c
, dti
, "Node %s bus-range must be 2 cells",
727 cells
= (cell_t
*)prop
->val
.val
;
728 if (fdt32_to_cpu(cells
[0]) > fdt32_to_cpu(cells
[1]))
729 FAIL(c
, dti
, "Node %s bus-range 1st cell must be less than or equal to 2nd cell",
731 if (fdt32_to_cpu(cells
[1]) > 0xff)
732 FAIL(c
, dti
, "Node %s bus-range maximum bus number must be less than 256",
735 WARNING(pci_bridge
, check_pci_bridge
, NULL
,
736 &device_type_is_string
, &addr_size_cells
);
738 static void check_pci_device_bus_num(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
740 struct property
*prop
;
741 unsigned int bus_num
, min_bus
, max_bus
;
744 if (!node
->parent
|| (node
->parent
->bus
!= &pci_bus
))
747 prop
= get_property(node
, "reg");
751 cells
= (cell_t
*)prop
->val
.val
;
752 bus_num
= (fdt32_to_cpu(cells
[0]) & 0x00ff0000) >> 16;
754 prop
= get_property(node
->parent
, "bus-range");
756 min_bus
= max_bus
= 0;
758 cells
= (cell_t
*)prop
->val
.val
;
759 min_bus
= fdt32_to_cpu(cells
[0]);
760 max_bus
= fdt32_to_cpu(cells
[0]);
762 if ((bus_num
< min_bus
) || (bus_num
> max_bus
))
763 FAIL(c
, dti
, "Node %s PCI bus number %d out of range, expected (%d - %d)",
764 node
->fullpath
, bus_num
, min_bus
, max_bus
);
766 WARNING(pci_device_bus_num
, check_pci_device_bus_num
, NULL
, ®_format
, &pci_bridge
);
768 static void check_pci_device_reg(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
770 struct property
*prop
;
771 const char *unitname
= get_unitname(node
);
773 unsigned int dev
, func
, reg
;
776 if (!node
->parent
|| (node
->parent
->bus
!= &pci_bus
))
779 prop
= get_property(node
, "reg");
781 FAIL(c
, dti
, "Node %s missing PCI reg property", node
->fullpath
);
785 cells
= (cell_t
*)prop
->val
.val
;
786 if (cells
[1] || cells
[2])
787 FAIL(c
, dti
, "Node %s PCI reg config space address cells 2 and 3 must be 0",
790 reg
= fdt32_to_cpu(cells
[0]);
791 dev
= (reg
& 0xf800) >> 11;
792 func
= (reg
& 0x700) >> 8;
794 if (reg
& 0xff000000)
795 FAIL(c
, dti
, "Node %s PCI reg address is not configuration space",
797 if (reg
& 0x000000ff)
798 FAIL(c
, dti
, "Node %s PCI reg config space address register number must be 0",
802 snprintf(unit_addr
, sizeof(unit_addr
), "%x", dev
);
803 if (streq(unitname
, unit_addr
))
807 snprintf(unit_addr
, sizeof(unit_addr
), "%x,%x", dev
, func
);
808 if (streq(unitname
, unit_addr
))
811 FAIL(c
, dti
, "Node %s PCI unit address format error, expected \"%s\"",
812 node
->fullpath
, unit_addr
);
814 WARNING(pci_device_reg
, check_pci_device_reg
, NULL
, ®_format
, &pci_bridge
);
816 static const struct bus_type simple_bus
= {
817 .name
= "simple-bus",
820 static bool node_is_compatible(struct node
*node
, const char *compat
)
822 struct property
*prop
;
823 const char *str
, *end
;
825 prop
= get_property(node
, "compatible");
829 for (str
= prop
->val
.val
, end
= str
+ prop
->val
.len
; str
< end
;
830 str
+= strnlen(str
, end
- str
) + 1) {
831 if (strneq(str
, compat
, end
- str
))
837 static void check_simple_bus_bridge(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
839 if (node_is_compatible(node
, "simple-bus"))
840 node
->bus
= &simple_bus
;
842 WARNING(simple_bus_bridge
, check_simple_bus_bridge
, NULL
, &addr_size_cells
);
844 static void check_simple_bus_reg(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
846 struct property
*prop
;
847 const char *unitname
= get_unitname(node
);
851 cell_t
*cells
= NULL
;
853 if (!node
->parent
|| (node
->parent
->bus
!= &simple_bus
))
856 prop
= get_property(node
, "reg");
858 cells
= (cell_t
*)prop
->val
.val
;
860 prop
= get_property(node
, "ranges");
861 if (prop
&& prop
->val
.len
)
862 /* skip of child address */
863 cells
= ((cell_t
*)prop
->val
.val
) + node_addr_cells(node
);
867 if (node
->parent
->parent
&& !(node
->bus
== &simple_bus
))
868 FAIL(c
, dti
, "Node %s missing or empty reg/ranges property", node
->fullpath
);
872 size
= node_addr_cells(node
->parent
);
874 reg
= (reg
<< 32) | fdt32_to_cpu(*(cells
++));
876 snprintf(unit_addr
, sizeof(unit_addr
), "%"PRIx64
, reg
);
877 if (!streq(unitname
, unit_addr
))
878 FAIL(c
, dti
, "Node %s simple-bus unit address format error, expected \"%s\"",
879 node
->fullpath
, unit_addr
);
881 WARNING(simple_bus_reg
, check_simple_bus_reg
, NULL
, ®_format
, &simple_bus_bridge
);
883 static void check_unit_address_format(struct check
*c
, struct dt_info
*dti
,
886 const char *unitname
= get_unitname(node
);
888 if (node
->parent
&& node
->parent
->bus
)
894 if (!strncmp(unitname
, "0x", 2)) {
895 FAIL(c
, dti
, "Node %s unit name should not have leading \"0x\"",
897 /* skip over 0x for next test */
900 if (unitname
[0] == '0' && isxdigit(unitname
[1]))
901 FAIL(c
, dti
, "Node %s unit name should not have leading 0s",
904 WARNING(unit_address_format
, check_unit_address_format
, NULL
,
905 &node_name_format
, &pci_bridge
, &simple_bus_bridge
);
910 static void check_avoid_default_addr_size(struct check
*c
, struct dt_info
*dti
,
913 struct property
*reg
, *ranges
;
916 return; /* Ignore root node */
918 reg
= get_property(node
, "reg");
919 ranges
= get_property(node
, "ranges");
924 if (node
->parent
->addr_cells
== -1)
925 FAIL(c
, dti
, "Relying on default #address-cells value for %s",
928 if (node
->parent
->size_cells
== -1)
929 FAIL(c
, dti
, "Relying on default #size-cells value for %s",
932 WARNING(avoid_default_addr_size
, check_avoid_default_addr_size
, NULL
,
935 static void check_obsolete_chosen_interrupt_controller(struct check
*c
,
939 struct node
*dt
= dti
->dt
;
941 struct property
*prop
;
947 chosen
= get_node_by_path(dt
, "/chosen");
951 prop
= get_property(chosen
, "interrupt-controller");
953 FAIL(c
, dti
, "/chosen has obsolete \"interrupt-controller\" "
956 WARNING(obsolete_chosen_interrupt_controller
,
957 check_obsolete_chosen_interrupt_controller
, NULL
);
960 const char *prop_name
;
961 const char *cell_name
;
965 static void check_property_phandle_args(struct check
*c
,
968 struct property
*prop
,
969 const struct provider
*provider
)
971 struct node
*root
= dti
->dt
;
972 int cell
, cellsize
= 0;
974 if (prop
->val
.len
% sizeof(cell_t
)) {
975 FAIL(c
, dti
, "property '%s' size (%d) is invalid, expected multiple of %zu in node %s",
976 prop
->name
, prop
->val
.len
, sizeof(cell_t
), node
->fullpath
);
980 for (cell
= 0; cell
< prop
->val
.len
/ sizeof(cell_t
); cell
+= cellsize
+ 1) {
981 struct node
*provider_node
;
982 struct property
*cellprop
;
985 phandle
= propval_cell_n(prop
, cell
);
987 * Some bindings use a cell value 0 or -1 to skip over optional
988 * entries when each index position has a specific definition.
990 if (phandle
== 0 || phandle
== -1) {
991 /* Give up if this is an overlay with external references */
992 if (dti
->dtsflags
& DTSF_PLUGIN
)
999 /* If we have markers, verify the current cell is a phandle */
1000 if (prop
->val
.markers
) {
1001 struct marker
*m
= prop
->val
.markers
;
1002 for_each_marker_of_type(m
, REF_PHANDLE
) {
1003 if (m
->offset
== (cell
* sizeof(cell_t
)))
1007 FAIL(c
, dti
, "Property '%s', cell %d is not a phandle reference in %s",
1008 prop
->name
, cell
, node
->fullpath
);
1011 provider_node
= get_node_by_phandle(root
, phandle
);
1012 if (!provider_node
) {
1013 FAIL(c
, dti
, "Could not get phandle node for %s:%s(cell %d)",
1014 node
->fullpath
, prop
->name
, cell
);
1018 cellprop
= get_property(provider_node
, provider
->cell_name
);
1020 cellsize
= propval_cell(cellprop
);
1021 } else if (provider
->optional
) {
1024 FAIL(c
, dti
, "Missing property '%s' in node %s or bad phandle (referred from %s:%s[%d])",
1025 provider
->cell_name
,
1026 provider_node
->fullpath
,
1027 node
->fullpath
, prop
->name
, cell
);
1031 if (prop
->val
.len
< ((cell
+ cellsize
+ 1) * sizeof(cell_t
))) {
1032 FAIL(c
, dti
, "%s property size (%d) too small for cell size %d in %s",
1033 prop
->name
, prop
->val
.len
, cellsize
, node
->fullpath
);
1038 static void check_provider_cells_property(struct check
*c
,
1039 struct dt_info
*dti
,
1042 struct provider
*provider
= c
->data
;
1043 struct property
*prop
;
1045 prop
= get_property(node
, provider
->prop_name
);
1049 check_property_phandle_args(c
, dti
, node
, prop
, provider
);
1051 #define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1052 static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1053 WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
1055 WARNING_PROPERTY_PHANDLE_CELLS(clocks
, "clocks", "#clock-cells");
1056 WARNING_PROPERTY_PHANDLE_CELLS(cooling_device
, "cooling-device", "#cooling-cells");
1057 WARNING_PROPERTY_PHANDLE_CELLS(dmas
, "dmas", "#dma-cells");
1058 WARNING_PROPERTY_PHANDLE_CELLS(hwlocks
, "hwlocks", "#hwlock-cells");
1059 WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended
, "interrupts-extended", "#interrupt-cells");
1060 WARNING_PROPERTY_PHANDLE_CELLS(io_channels
, "io-channels", "#io-channel-cells");
1061 WARNING_PROPERTY_PHANDLE_CELLS(iommus
, "iommus", "#iommu-cells");
1062 WARNING_PROPERTY_PHANDLE_CELLS(mboxes
, "mboxes", "#mbox-cells");
1063 WARNING_PROPERTY_PHANDLE_CELLS(msi_parent
, "msi-parent", "#msi-cells", true);
1064 WARNING_PROPERTY_PHANDLE_CELLS(mux_controls
, "mux-controls", "#mux-control-cells");
1065 WARNING_PROPERTY_PHANDLE_CELLS(phys
, "phys", "#phy-cells");
1066 WARNING_PROPERTY_PHANDLE_CELLS(power_domains
, "power-domains", "#power-domain-cells");
1067 WARNING_PROPERTY_PHANDLE_CELLS(pwms
, "pwms", "#pwm-cells");
1068 WARNING_PROPERTY_PHANDLE_CELLS(resets
, "resets", "#reset-cells");
1069 WARNING_PROPERTY_PHANDLE_CELLS(sound_dais
, "sound-dais", "#sound-dai-cells");
1070 WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors
, "thermal-sensors", "#thermal-sensor-cells");
1072 static bool prop_is_gpio(struct property
*prop
)
1077 * *-gpios and *-gpio can appear in property names,
1078 * so skip over any false matches (only one known ATM)
1080 if (strstr(prop
->name
, "nr-gpio"))
1083 str
= strrchr(prop
->name
, '-');
1088 if (!(streq(str
, "gpios") || streq(str
, "gpio")))
1094 static void check_gpios_property(struct check
*c
,
1095 struct dt_info
*dti
,
1098 struct property
*prop
;
1100 /* Skip GPIO hog nodes which have 'gpios' property */
1101 if (get_property(node
, "gpio-hog"))
1104 for_each_property(node
, prop
) {
1105 struct provider provider
;
1107 if (!prop_is_gpio(prop
))
1110 provider
.prop_name
= prop
->name
;
1111 provider
.cell_name
= "#gpio-cells";
1112 provider
.optional
= false;
1113 check_property_phandle_args(c
, dti
, node
, prop
, &provider
);
1117 WARNING(gpios_property
, check_gpios_property
, NULL
, &phandle_references
);
1119 static void check_deprecated_gpio_property(struct check
*c
,
1120 struct dt_info
*dti
,
1123 struct property
*prop
;
1125 for_each_property(node
, prop
) {
1128 if (!prop_is_gpio(prop
))
1131 str
= strstr(prop
->name
, "gpio");
1132 if (!streq(str
, "gpio"))
1135 FAIL(c
, dti
, "'[*-]gpio' is deprecated, use '[*-]gpios' instead for %s:%s",
1136 node
->fullpath
, prop
->name
);
1140 CHECK(deprecated_gpio_property
, check_deprecated_gpio_property
, NULL
);
1142 static bool node_is_interrupt_provider(struct node
*node
)
1144 struct property
*prop
;
1146 prop
= get_property(node
, "interrupt-controller");
1150 prop
= get_property(node
, "interrupt-map");
1156 static void check_interrupts_property(struct check
*c
,
1157 struct dt_info
*dti
,
1160 struct node
*root
= dti
->dt
;
1161 struct node
*irq_node
= NULL
, *parent
= node
;
1162 struct property
*irq_prop
, *prop
= NULL
;
1163 int irq_cells
, phandle
;
1165 irq_prop
= get_property(node
, "interrupts");
1169 if (irq_prop
->val
.len
% sizeof(cell_t
))
1170 FAIL(c
, dti
, "property '%s' size (%d) is invalid, expected multiple of %zu in node %s",
1171 irq_prop
->name
, irq_prop
->val
.len
, sizeof(cell_t
),
1174 while (parent
&& !prop
) {
1175 if (parent
!= node
&& node_is_interrupt_provider(parent
)) {
1180 prop
= get_property(parent
, "interrupt-parent");
1182 phandle
= propval_cell(prop
);
1183 /* Give up if this is an overlay with external references */
1184 if ((phandle
== 0 || phandle
== -1) &&
1185 (dti
->dtsflags
& DTSF_PLUGIN
))
1188 irq_node
= get_node_by_phandle(root
, phandle
);
1190 FAIL(c
, dti
, "Bad interrupt-parent phandle for %s",
1194 if (!node_is_interrupt_provider(irq_node
))
1196 "Missing interrupt-controller or interrupt-map property in %s",
1197 irq_node
->fullpath
);
1202 parent
= parent
->parent
;
1206 FAIL(c
, dti
, "Missing interrupt-parent for %s", node
->fullpath
);
1210 prop
= get_property(irq_node
, "#interrupt-cells");
1212 FAIL(c
, dti
, "Missing #interrupt-cells in interrupt-parent %s",
1213 irq_node
->fullpath
);
1217 irq_cells
= propval_cell(prop
);
1218 if (irq_prop
->val
.len
% (irq_cells
* sizeof(cell_t
))) {
1220 "interrupts size is (%d), expected multiple of %d in %s",
1221 irq_prop
->val
.len
, (int)(irq_cells
* sizeof(cell_t
)),
1225 WARNING(interrupts_property
, check_interrupts_property
, &phandle_references
);
1227 static struct check
*check_table
[] = {
1228 &duplicate_node_names
, &duplicate_property_names
,
1229 &node_name_chars
, &node_name_format
, &property_name_chars
,
1230 &name_is_string
, &name_properties
,
1235 &phandle_references
, &path_references
,
1237 &address_cells_is_cell
, &size_cells_is_cell
, &interrupt_cells_is_cell
,
1238 &device_type_is_string
, &model_is_string
, &status_is_string
,
1240 &property_name_chars_strict
,
1241 &node_name_chars_strict
,
1243 &addr_size_cells
, ®_format
, &ranges_format
,
1245 &unit_address_vs_reg
,
1246 &unit_address_format
,
1250 &pci_device_bus_num
,
1255 &avoid_default_addr_size
,
1256 &obsolete_chosen_interrupt_controller
,
1259 &cooling_device_property
,
1262 &interrupts_extended_property
,
1263 &io_channels_property
,
1266 &msi_parent_property
,
1267 &mux_controls_property
,
1269 &power_domains_property
,
1272 &sound_dais_property
,
1273 &thermal_sensors_property
,
1275 &deprecated_gpio_property
,
1277 &interrupts_property
,
1282 static void enable_warning_error(struct check
*c
, bool warn
, bool error
)
1286 /* Raising level, also raise it for prereqs */
1287 if ((warn
&& !c
->warn
) || (error
&& !c
->error
))
1288 for (i
= 0; i
< c
->num_prereqs
; i
++)
1289 enable_warning_error(c
->prereq
[i
], warn
, error
);
1291 c
->warn
= c
->warn
|| warn
;
1292 c
->error
= c
->error
|| error
;
1295 static void disable_warning_error(struct check
*c
, bool warn
, bool error
)
1299 /* Lowering level, also lower it for things this is the prereq
1301 if ((warn
&& c
->warn
) || (error
&& c
->error
)) {
1302 for (i
= 0; i
< ARRAY_SIZE(check_table
); i
++) {
1303 struct check
*cc
= check_table
[i
];
1306 for (j
= 0; j
< cc
->num_prereqs
; j
++)
1307 if (cc
->prereq
[j
] == c
)
1308 disable_warning_error(cc
, warn
, error
);
1312 c
->warn
= c
->warn
&& !warn
;
1313 c
->error
= c
->error
&& !error
;
1316 void parse_checks_option(bool warn
, bool error
, const char *arg
)
1319 const char *name
= arg
;
1322 if ((strncmp(arg
, "no-", 3) == 0)
1323 || (strncmp(arg
, "no_", 3) == 0)) {
1328 for (i
= 0; i
< ARRAY_SIZE(check_table
); i
++) {
1329 struct check
*c
= check_table
[i
];
1331 if (streq(c
->name
, name
)) {
1333 enable_warning_error(c
, warn
, error
);
1335 disable_warning_error(c
, warn
, error
);
1340 die("Unrecognized check name \"%s\"\n", name
);
1343 void process_checks(bool force
, struct dt_info
*dti
)
1348 for (i
= 0; i
< ARRAY_SIZE(check_table
); i
++) {
1349 struct check
*c
= check_table
[i
];
1351 if (c
->warn
|| c
->error
)
1352 error
= error
|| run_check(c
, dti
);
1357 fprintf(stderr
, "ERROR: Input tree has errors, aborting "
1358 "(use -f to force output)\n");
1360 } else if (quiet
< 3) {
1361 fprintf(stderr
, "Warning: Input tree has errors, "