xref: /openbmc/linux/scripts/dtc/checks.c (revision 6d99a79c)
1 /*
2  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2007.
3  *
4  *
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.
9  *
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.
14  *
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
19  */
20 
21 #include "dtc.h"
22 
23 #ifdef TRACE_CHECKS
24 #define TRACE(c, ...) \
25 	do { \
26 		fprintf(stderr, "=== %s: ", (c)->name); \
27 		fprintf(stderr, __VA_ARGS__); \
28 		fprintf(stderr, "\n"); \
29 	} while (0)
30 #else
31 #define TRACE(c, fmt, ...)	do { } while (0)
32 #endif
33 
34 enum checkstatus {
35 	UNCHECKED = 0,
36 	PREREQ,
37 	PASSED,
38 	FAILED,
39 };
40 
41 struct check;
42 
43 typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
44 
45 struct check {
46 	const char *name;
47 	check_fn fn;
48 	void *data;
49 	bool warn, error;
50 	enum checkstatus status;
51 	bool inprogress;
52 	int num_prereqs;
53 	struct check **prereq;
54 };
55 
56 #define CHECK_ENTRY(nm_, fn_, d_, w_, e_, ...)	       \
57 	static struct check *nm_##_prereqs[] = { __VA_ARGS__ }; \
58 	static struct check nm_ = { \
59 		.name = #nm_, \
60 		.fn = (fn_), \
61 		.data = (d_), \
62 		.warn = (w_), \
63 		.error = (e_), \
64 		.status = UNCHECKED, \
65 		.num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
66 		.prereq = nm_##_prereqs, \
67 	};
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__)
74 
75 static inline void  PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
76 					   struct node *node,
77 					   struct property *prop,
78 					   const char *fmt, ...)
79 {
80 	va_list ap;
81 	va_start(ap, fmt);
82 
83 	if ((c->warn && (quiet < 1))
84 	    || (c->error && (quiet < 2))) {
85 		fprintf(stderr, "%s: %s (%s): ",
86 			strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
87 			(c->error) ? "ERROR" : "Warning", c->name);
88 		if (node) {
89 			fprintf(stderr, "%s", node->fullpath);
90 			if (prop)
91 				fprintf(stderr, ":%s", prop->name);
92 			fputs(": ", stderr);
93 		}
94 		vfprintf(stderr, fmt, ap);
95 		fprintf(stderr, "\n");
96 	}
97 	va_end(ap);
98 }
99 
100 #define FAIL(c, dti, node, ...)						\
101 	do {								\
102 		TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__);	\
103 		(c)->status = FAILED;					\
104 		check_msg((c), dti, node, NULL, __VA_ARGS__);		\
105 	} while (0)
106 
107 #define FAIL_PROP(c, dti, node, prop, ...)				\
108 	do {								\
109 		TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__);	\
110 		(c)->status = FAILED;					\
111 		check_msg((c), dti, node, prop, __VA_ARGS__);		\
112 	} while (0)
113 
114 
115 static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
116 {
117 	struct node *child;
118 
119 	TRACE(c, "%s", node->fullpath);
120 	if (c->fn)
121 		c->fn(c, dti, node);
122 
123 	for_each_child(node, child)
124 		check_nodes_props(c, dti, child);
125 }
126 
127 static bool run_check(struct check *c, struct dt_info *dti)
128 {
129 	struct node *dt = dti->dt;
130 	bool error = false;
131 	int i;
132 
133 	assert(!c->inprogress);
134 
135 	if (c->status != UNCHECKED)
136 		goto out;
137 
138 	c->inprogress = true;
139 
140 	for (i = 0; i < c->num_prereqs; i++) {
141 		struct check *prq = c->prereq[i];
142 		error = error || run_check(prq, dti);
143 		if (prq->status != PASSED) {
144 			c->status = PREREQ;
145 			check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
146 				  c->prereq[i]->name);
147 		}
148 	}
149 
150 	if (c->status != UNCHECKED)
151 		goto out;
152 
153 	check_nodes_props(c, dti, dt);
154 
155 	if (c->status == UNCHECKED)
156 		c->status = PASSED;
157 
158 	TRACE(c, "\tCompleted, status %d", c->status);
159 
160 out:
161 	c->inprogress = false;
162 	if ((c->status != PASSED) && (c->error))
163 		error = true;
164 	return error;
165 }
166 
167 /*
168  * Utility check functions
169  */
170 
171 /* A check which always fails, for testing purposes only */
172 static inline void check_always_fail(struct check *c, struct dt_info *dti,
173 				     struct node *node)
174 {
175 	FAIL(c, dti, node, "always_fail check");
176 }
177 CHECK(always_fail, check_always_fail, NULL);
178 
179 static void check_is_string(struct check *c, struct dt_info *dti,
180 			    struct node *node)
181 {
182 	struct property *prop;
183 	char *propname = c->data;
184 
185 	prop = get_property(node, propname);
186 	if (!prop)
187 		return; /* Not present, assumed ok */
188 
189 	if (!data_is_one_string(prop->val))
190 		FAIL_PROP(c, dti, node, prop, "property is not a string");
191 }
192 #define WARNING_IF_NOT_STRING(nm, propname) \
193 	WARNING(nm, check_is_string, (propname))
194 #define ERROR_IF_NOT_STRING(nm, propname) \
195 	ERROR(nm, check_is_string, (propname))
196 
197 static void check_is_string_list(struct check *c, struct dt_info *dti,
198 				 struct node *node)
199 {
200 	int rem, l;
201 	struct property *prop;
202 	char *propname = c->data;
203 	char *str;
204 
205 	prop = get_property(node, propname);
206 	if (!prop)
207 		return; /* Not present, assumed ok */
208 
209 	str = prop->val.val;
210 	rem = prop->val.len;
211 	while (rem > 0) {
212 		l = strnlen(str, rem);
213 		if (l == rem) {
214 			FAIL_PROP(c, dti, node, prop, "property is not a string list");
215 			break;
216 		}
217 		rem -= l + 1;
218 		str += l + 1;
219 	}
220 }
221 #define WARNING_IF_NOT_STRING_LIST(nm, propname) \
222 	WARNING(nm, check_is_string_list, (propname))
223 #define ERROR_IF_NOT_STRING_LIST(nm, propname) \
224 	ERROR(nm, check_is_string_list, (propname))
225 
226 static void check_is_cell(struct check *c, struct dt_info *dti,
227 			  struct node *node)
228 {
229 	struct property *prop;
230 	char *propname = c->data;
231 
232 	prop = get_property(node, propname);
233 	if (!prop)
234 		return; /* Not present, assumed ok */
235 
236 	if (prop->val.len != sizeof(cell_t))
237 		FAIL_PROP(c, dti, node, prop, "property is not a single cell");
238 }
239 #define WARNING_IF_NOT_CELL(nm, propname) \
240 	WARNING(nm, check_is_cell, (propname))
241 #define ERROR_IF_NOT_CELL(nm, propname) \
242 	ERROR(nm, check_is_cell, (propname))
243 
244 /*
245  * Structural check functions
246  */
247 
248 static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
249 				       struct node *node)
250 {
251 	struct node *child, *child2;
252 
253 	for_each_child(node, child)
254 		for (child2 = child->next_sibling;
255 		     child2;
256 		     child2 = child2->next_sibling)
257 			if (streq(child->name, child2->name))
258 				FAIL(c, dti, child2, "Duplicate node name");
259 }
260 ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
261 
262 static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
263 					   struct node *node)
264 {
265 	struct property *prop, *prop2;
266 
267 	for_each_property(node, prop) {
268 		for (prop2 = prop->next; prop2; prop2 = prop2->next) {
269 			if (prop2->deleted)
270 				continue;
271 			if (streq(prop->name, prop2->name))
272 				FAIL_PROP(c, dti, node, prop, "Duplicate property name");
273 		}
274 	}
275 }
276 ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
277 
278 #define LOWERCASE	"abcdefghijklmnopqrstuvwxyz"
279 #define UPPERCASE	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
280 #define DIGITS		"0123456789"
281 #define PROPNODECHARS	LOWERCASE UPPERCASE DIGITS ",._+*#?-"
282 #define PROPNODECHARSSTRICT	LOWERCASE UPPERCASE DIGITS ",-"
283 
284 static void check_node_name_chars(struct check *c, struct dt_info *dti,
285 				  struct node *node)
286 {
287 	int n = strspn(node->name, c->data);
288 
289 	if (n < strlen(node->name))
290 		FAIL(c, dti, node, "Bad character '%c' in node name",
291 		     node->name[n]);
292 }
293 ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
294 
295 static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
296 					 struct node *node)
297 {
298 	int n = strspn(node->name, c->data);
299 
300 	if (n < node->basenamelen)
301 		FAIL(c, dti, node, "Character '%c' not recommended in node name",
302 		     node->name[n]);
303 }
304 CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
305 
306 static void check_node_name_format(struct check *c, struct dt_info *dti,
307 				   struct node *node)
308 {
309 	if (strchr(get_unitname(node), '@'))
310 		FAIL(c, dti, node, "multiple '@' characters in node name");
311 }
312 ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
313 
314 static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
315 				      struct node *node)
316 {
317 	const char *unitname = get_unitname(node);
318 	struct property *prop = get_property(node, "reg");
319 
320 	if (get_subnode(node, "__overlay__")) {
321 		/* HACK: Overlay fragments are a special case */
322 		return;
323 	}
324 
325 	if (!prop) {
326 		prop = get_property(node, "ranges");
327 		if (prop && !prop->val.len)
328 			prop = NULL;
329 	}
330 
331 	if (prop) {
332 		if (!unitname[0])
333 			FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
334 	} else {
335 		if (unitname[0])
336 			FAIL(c, dti, node, "node has a unit name, but no reg property");
337 	}
338 }
339 WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
340 
341 static void check_property_name_chars(struct check *c, struct dt_info *dti,
342 				      struct node *node)
343 {
344 	struct property *prop;
345 
346 	for_each_property(node, prop) {
347 		int n = strspn(prop->name, c->data);
348 
349 		if (n < strlen(prop->name))
350 			FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
351 				  prop->name[n]);
352 	}
353 }
354 ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
355 
356 static void check_property_name_chars_strict(struct check *c,
357 					     struct dt_info *dti,
358 					     struct node *node)
359 {
360 	struct property *prop;
361 
362 	for_each_property(node, prop) {
363 		const char *name = prop->name;
364 		int n = strspn(name, c->data);
365 
366 		if (n == strlen(prop->name))
367 			continue;
368 
369 		/* Certain names are whitelisted */
370 		if (streq(name, "device_type"))
371 			continue;
372 
373 		/*
374 		 * # is only allowed at the beginning of property names not counting
375 		 * the vendor prefix.
376 		 */
377 		if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
378 			name += n + 1;
379 			n = strspn(name, c->data);
380 		}
381 		if (n < strlen(name))
382 			FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
383 				  name[n]);
384 	}
385 }
386 CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
387 
388 #define DESCLABEL_FMT	"%s%s%s%s%s"
389 #define DESCLABEL_ARGS(node,prop,mark)		\
390 	((mark) ? "value of " : ""),		\
391 	((prop) ? "'" : ""), \
392 	((prop) ? (prop)->name : ""), \
393 	((prop) ? "' in " : ""), (node)->fullpath
394 
395 static void check_duplicate_label(struct check *c, struct dt_info *dti,
396 				  const char *label, struct node *node,
397 				  struct property *prop, struct marker *mark)
398 {
399 	struct node *dt = dti->dt;
400 	struct node *othernode = NULL;
401 	struct property *otherprop = NULL;
402 	struct marker *othermark = NULL;
403 
404 	othernode = get_node_by_label(dt, label);
405 
406 	if (!othernode)
407 		otherprop = get_property_by_label(dt, label, &othernode);
408 	if (!othernode)
409 		othermark = get_marker_label(dt, label, &othernode,
410 					       &otherprop);
411 
412 	if (!othernode)
413 		return;
414 
415 	if ((othernode != node) || (otherprop != prop) || (othermark != mark))
416 		FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
417 		     " and " DESCLABEL_FMT,
418 		     label, DESCLABEL_ARGS(node, prop, mark),
419 		     DESCLABEL_ARGS(othernode, otherprop, othermark));
420 }
421 
422 static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
423 				       struct node *node)
424 {
425 	struct label *l;
426 	struct property *prop;
427 
428 	for_each_label(node->labels, l)
429 		check_duplicate_label(c, dti, l->label, node, NULL, NULL);
430 
431 	for_each_property(node, prop) {
432 		struct marker *m = prop->val.markers;
433 
434 		for_each_label(prop->labels, l)
435 			check_duplicate_label(c, dti, l->label, node, prop, NULL);
436 
437 		for_each_marker_of_type(m, LABEL)
438 			check_duplicate_label(c, dti, m->ref, node, prop, m);
439 	}
440 }
441 ERROR(duplicate_label, check_duplicate_label_node, NULL);
442 
443 static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
444 				 struct node *node, const char *propname)
445 {
446 	struct node *root = dti->dt;
447 	struct property *prop;
448 	struct marker *m;
449 	cell_t phandle;
450 
451 	prop = get_property(node, propname);
452 	if (!prop)
453 		return 0;
454 
455 	if (prop->val.len != sizeof(cell_t)) {
456 		FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
457 			  prop->val.len, prop->name);
458 		return 0;
459 	}
460 
461 	m = prop->val.markers;
462 	for_each_marker_of_type(m, REF_PHANDLE) {
463 		assert(m->offset == 0);
464 		if (node != get_node_by_ref(root, m->ref))
465 			/* "Set this node's phandle equal to some
466 			 * other node's phandle".  That's nonsensical
467 			 * by construction. */ {
468 			FAIL(c, dti, node, "%s is a reference to another node",
469 			     prop->name);
470 		}
471 		/* But setting this node's phandle equal to its own
472 		 * phandle is allowed - that means allocate a unique
473 		 * phandle for this node, even if it's not otherwise
474 		 * referenced.  The value will be filled in later, so
475 		 * we treat it as having no phandle data for now. */
476 		return 0;
477 	}
478 
479 	phandle = propval_cell(prop);
480 
481 	if ((phandle == 0) || (phandle == -1)) {
482 		FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
483 		     phandle, prop->name);
484 		return 0;
485 	}
486 
487 	return phandle;
488 }
489 
490 static void check_explicit_phandles(struct check *c, struct dt_info *dti,
491 				    struct node *node)
492 {
493 	struct node *root = dti->dt;
494 	struct node *other;
495 	cell_t phandle, linux_phandle;
496 
497 	/* Nothing should have assigned phandles yet */
498 	assert(!node->phandle);
499 
500 	phandle = check_phandle_prop(c, dti, node, "phandle");
501 
502 	linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
503 
504 	if (!phandle && !linux_phandle)
505 		/* No valid phandles; nothing further to check */
506 		return;
507 
508 	if (linux_phandle && phandle && (phandle != linux_phandle))
509 		FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
510 		     " properties");
511 
512 	if (linux_phandle && !phandle)
513 		phandle = linux_phandle;
514 
515 	other = get_node_by_phandle(root, phandle);
516 	if (other && (other != node)) {
517 		FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
518 		     phandle, other->fullpath);
519 		return;
520 	}
521 
522 	node->phandle = phandle;
523 }
524 ERROR(explicit_phandles, check_explicit_phandles, NULL);
525 
526 static void check_name_properties(struct check *c, struct dt_info *dti,
527 				  struct node *node)
528 {
529 	struct property **pp, *prop = NULL;
530 
531 	for (pp = &node->proplist; *pp; pp = &((*pp)->next))
532 		if (streq((*pp)->name, "name")) {
533 			prop = *pp;
534 			break;
535 		}
536 
537 	if (!prop)
538 		return; /* No name property, that's fine */
539 
540 	if ((prop->val.len != node->basenamelen+1)
541 	    || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
542 		FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
543 		     " of base node name)", prop->val.val);
544 	} else {
545 		/* The name property is correct, and therefore redundant.
546 		 * Delete it */
547 		*pp = prop->next;
548 		free(prop->name);
549 		data_free(prop->val);
550 		free(prop);
551 	}
552 }
553 ERROR_IF_NOT_STRING(name_is_string, "name");
554 ERROR(name_properties, check_name_properties, NULL, &name_is_string);
555 
556 /*
557  * Reference fixup functions
558  */
559 
560 static void fixup_phandle_references(struct check *c, struct dt_info *dti,
561 				     struct node *node)
562 {
563 	struct node *dt = dti->dt;
564 	struct property *prop;
565 
566 	for_each_property(node, prop) {
567 		struct marker *m = prop->val.markers;
568 		struct node *refnode;
569 		cell_t phandle;
570 
571 		for_each_marker_of_type(m, REF_PHANDLE) {
572 			assert(m->offset + sizeof(cell_t) <= prop->val.len);
573 
574 			refnode = get_node_by_ref(dt, m->ref);
575 			if (! refnode) {
576 				if (!(dti->dtsflags & DTSF_PLUGIN))
577 					FAIL(c, dti, node, "Reference to non-existent node or "
578 							"label \"%s\"\n", m->ref);
579 				else /* mark the entry as unresolved */
580 					*((fdt32_t *)(prop->val.val + m->offset)) =
581 						cpu_to_fdt32(0xffffffff);
582 				continue;
583 			}
584 
585 			phandle = get_node_phandle(dt, refnode);
586 			*((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
587 
588 			reference_node(refnode);
589 		}
590 	}
591 }
592 ERROR(phandle_references, fixup_phandle_references, NULL,
593       &duplicate_node_names, &explicit_phandles);
594 
595 static void fixup_path_references(struct check *c, struct dt_info *dti,
596 				  struct node *node)
597 {
598 	struct node *dt = dti->dt;
599 	struct property *prop;
600 
601 	for_each_property(node, prop) {
602 		struct marker *m = prop->val.markers;
603 		struct node *refnode;
604 		char *path;
605 
606 		for_each_marker_of_type(m, REF_PATH) {
607 			assert(m->offset <= prop->val.len);
608 
609 			refnode = get_node_by_ref(dt, m->ref);
610 			if (!refnode) {
611 				FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
612 				     m->ref);
613 				continue;
614 			}
615 
616 			path = refnode->fullpath;
617 			prop->val = data_insert_at_marker(prop->val, m, path,
618 							  strlen(path) + 1);
619 
620 			reference_node(refnode);
621 		}
622 	}
623 }
624 ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
625 
626 static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
627 				    struct node *node)
628 {
629 	if (node->omit_if_unused && !node->is_referenced)
630 		delete_node(node);
631 }
632 ERROR(omit_unused_nodes, fixup_omit_unused_nodes, NULL, &phandle_references, &path_references);
633 
634 /*
635  * Semantic checks
636  */
637 WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
638 WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
639 WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
640 
641 WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
642 WARNING_IF_NOT_STRING(model_is_string, "model");
643 WARNING_IF_NOT_STRING(status_is_string, "status");
644 WARNING_IF_NOT_STRING(label_is_string, "label");
645 
646 WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
647 
648 static void check_names_is_string_list(struct check *c, struct dt_info *dti,
649 				       struct node *node)
650 {
651 	struct property *prop;
652 
653 	for_each_property(node, prop) {
654 		const char *s = strrchr(prop->name, '-');
655 		if (!s || !streq(s, "-names"))
656 			continue;
657 
658 		c->data = prop->name;
659 		check_is_string_list(c, dti, node);
660 	}
661 }
662 WARNING(names_is_string_list, check_names_is_string_list, NULL);
663 
664 static void check_alias_paths(struct check *c, struct dt_info *dti,
665 				    struct node *node)
666 {
667 	struct property *prop;
668 
669 	if (!streq(node->name, "aliases"))
670 		return;
671 
672 	for_each_property(node, prop) {
673 		if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
674 			FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
675 				  prop->val.val);
676 			continue;
677 		}
678 		if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
679 			FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
680 	}
681 }
682 WARNING(alias_paths, check_alias_paths, NULL);
683 
684 static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
685 				  struct node *node)
686 {
687 	struct property *prop;
688 
689 	node->addr_cells = -1;
690 	node->size_cells = -1;
691 
692 	prop = get_property(node, "#address-cells");
693 	if (prop)
694 		node->addr_cells = propval_cell(prop);
695 
696 	prop = get_property(node, "#size-cells");
697 	if (prop)
698 		node->size_cells = propval_cell(prop);
699 }
700 WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
701 	&address_cells_is_cell, &size_cells_is_cell);
702 
703 #define node_addr_cells(n) \
704 	(((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
705 #define node_size_cells(n) \
706 	(((n)->size_cells == -1) ? 1 : (n)->size_cells)
707 
708 static void check_reg_format(struct check *c, struct dt_info *dti,
709 			     struct node *node)
710 {
711 	struct property *prop;
712 	int addr_cells, size_cells, entrylen;
713 
714 	prop = get_property(node, "reg");
715 	if (!prop)
716 		return; /* No "reg", that's fine */
717 
718 	if (!node->parent) {
719 		FAIL(c, dti, node, "Root node has a \"reg\" property");
720 		return;
721 	}
722 
723 	if (prop->val.len == 0)
724 		FAIL_PROP(c, dti, node, prop, "property is empty");
725 
726 	addr_cells = node_addr_cells(node->parent);
727 	size_cells = node_size_cells(node->parent);
728 	entrylen = (addr_cells + size_cells) * sizeof(cell_t);
729 
730 	if (!entrylen || (prop->val.len % entrylen) != 0)
731 		FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
732 			  "(#address-cells == %d, #size-cells == %d)",
733 			  prop->val.len, addr_cells, size_cells);
734 }
735 WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
736 
737 static void check_ranges_format(struct check *c, struct dt_info *dti,
738 				struct node *node)
739 {
740 	struct property *prop;
741 	int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
742 
743 	prop = get_property(node, "ranges");
744 	if (!prop)
745 		return;
746 
747 	if (!node->parent) {
748 		FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
749 		return;
750 	}
751 
752 	p_addr_cells = node_addr_cells(node->parent);
753 	p_size_cells = node_size_cells(node->parent);
754 	c_addr_cells = node_addr_cells(node);
755 	c_size_cells = node_size_cells(node);
756 	entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
757 
758 	if (prop->val.len == 0) {
759 		if (p_addr_cells != c_addr_cells)
760 			FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
761 				  "#address-cells (%d) differs from %s (%d)",
762 				  c_addr_cells, node->parent->fullpath,
763 				  p_addr_cells);
764 		if (p_size_cells != c_size_cells)
765 			FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
766 				  "#size-cells (%d) differs from %s (%d)",
767 				  c_size_cells, node->parent->fullpath,
768 				  p_size_cells);
769 	} else if ((prop->val.len % entrylen) != 0) {
770 		FAIL_PROP(c, dti, node, prop, "\"ranges\" property has invalid length (%d bytes) "
771 			  "(parent #address-cells == %d, child #address-cells == %d, "
772 			  "#size-cells == %d)", prop->val.len,
773 			  p_addr_cells, c_addr_cells, c_size_cells);
774 	}
775 }
776 WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
777 
778 static const struct bus_type pci_bus = {
779 	.name = "PCI",
780 };
781 
782 static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
783 {
784 	struct property *prop;
785 	cell_t *cells;
786 
787 	prop = get_property(node, "device_type");
788 	if (!prop || !streq(prop->val.val, "pci"))
789 		return;
790 
791 	node->bus = &pci_bus;
792 
793 	if (!strprefixeq(node->name, node->basenamelen, "pci") &&
794 	    !strprefixeq(node->name, node->basenamelen, "pcie"))
795 		FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
796 
797 	prop = get_property(node, "ranges");
798 	if (!prop)
799 		FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
800 
801 	if (node_addr_cells(node) != 3)
802 		FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
803 	if (node_size_cells(node) != 2)
804 		FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
805 
806 	prop = get_property(node, "bus-range");
807 	if (!prop)
808 		return;
809 
810 	if (prop->val.len != (sizeof(cell_t) * 2)) {
811 		FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
812 		return;
813 	}
814 	cells = (cell_t *)prop->val.val;
815 	if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
816 		FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
817 	if (fdt32_to_cpu(cells[1]) > 0xff)
818 		FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
819 }
820 WARNING(pci_bridge, check_pci_bridge, NULL,
821 	&device_type_is_string, &addr_size_cells);
822 
823 static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
824 {
825 	struct property *prop;
826 	unsigned int bus_num, min_bus, max_bus;
827 	cell_t *cells;
828 
829 	if (!node->parent || (node->parent->bus != &pci_bus))
830 		return;
831 
832 	prop = get_property(node, "reg");
833 	if (!prop)
834 		return;
835 
836 	cells = (cell_t *)prop->val.val;
837 	bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
838 
839 	prop = get_property(node->parent, "bus-range");
840 	if (!prop) {
841 		min_bus = max_bus = 0;
842 	} else {
843 		cells = (cell_t *)prop->val.val;
844 		min_bus = fdt32_to_cpu(cells[0]);
845 		max_bus = fdt32_to_cpu(cells[0]);
846 	}
847 	if ((bus_num < min_bus) || (bus_num > max_bus))
848 		FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
849 			  bus_num, min_bus, max_bus);
850 }
851 WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
852 
853 static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
854 {
855 	struct property *prop;
856 	const char *unitname = get_unitname(node);
857 	char unit_addr[5];
858 	unsigned int dev, func, reg;
859 	cell_t *cells;
860 
861 	if (!node->parent || (node->parent->bus != &pci_bus))
862 		return;
863 
864 	prop = get_property(node, "reg");
865 	if (!prop) {
866 		FAIL(c, dti, node, "missing PCI reg property");
867 		return;
868 	}
869 
870 	cells = (cell_t *)prop->val.val;
871 	if (cells[1] || cells[2])
872 		FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
873 
874 	reg = fdt32_to_cpu(cells[0]);
875 	dev = (reg & 0xf800) >> 11;
876 	func = (reg & 0x700) >> 8;
877 
878 	if (reg & 0xff000000)
879 		FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
880 	if (reg & 0x000000ff)
881 		FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
882 
883 	if (func == 0) {
884 		snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
885 		if (streq(unitname, unit_addr))
886 			return;
887 	}
888 
889 	snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
890 	if (streq(unitname, unit_addr))
891 		return;
892 
893 	FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
894 	     unit_addr);
895 }
896 WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
897 
898 static const struct bus_type simple_bus = {
899 	.name = "simple-bus",
900 };
901 
902 static bool node_is_compatible(struct node *node, const char *compat)
903 {
904 	struct property *prop;
905 	const char *str, *end;
906 
907 	prop = get_property(node, "compatible");
908 	if (!prop)
909 		return false;
910 
911 	for (str = prop->val.val, end = str + prop->val.len; str < end;
912 	     str += strnlen(str, end - str) + 1) {
913 		if (strprefixeq(str, end - str, compat))
914 			return true;
915 	}
916 	return false;
917 }
918 
919 static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
920 {
921 	if (node_is_compatible(node, "simple-bus"))
922 		node->bus = &simple_bus;
923 }
924 WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL, &addr_size_cells);
925 
926 static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
927 {
928 	struct property *prop;
929 	const char *unitname = get_unitname(node);
930 	char unit_addr[17];
931 	unsigned int size;
932 	uint64_t reg = 0;
933 	cell_t *cells = NULL;
934 
935 	if (!node->parent || (node->parent->bus != &simple_bus))
936 		return;
937 
938 	prop = get_property(node, "reg");
939 	if (prop)
940 		cells = (cell_t *)prop->val.val;
941 	else {
942 		prop = get_property(node, "ranges");
943 		if (prop && prop->val.len)
944 			/* skip of child address */
945 			cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
946 	}
947 
948 	if (!cells) {
949 		if (node->parent->parent && !(node->bus == &simple_bus))
950 			FAIL(c, dti, node, "missing or empty reg/ranges property");
951 		return;
952 	}
953 
954 	size = node_addr_cells(node->parent);
955 	while (size--)
956 		reg = (reg << 32) | fdt32_to_cpu(*(cells++));
957 
958 	snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
959 	if (!streq(unitname, unit_addr))
960 		FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
961 		     unit_addr);
962 }
963 WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
964 
965 static const struct bus_type i2c_bus = {
966 	.name = "i2c-bus",
967 };
968 
969 static void check_i2c_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
970 {
971 	if (strprefixeq(node->name, node->basenamelen, "i2c-bus") ||
972 	    strprefixeq(node->name, node->basenamelen, "i2c-arb")) {
973 		node->bus = &i2c_bus;
974 	} else if (strprefixeq(node->name, node->basenamelen, "i2c")) {
975 		struct node *child;
976 		for_each_child(node, child) {
977 			if (strprefixeq(child->name, node->basenamelen, "i2c-bus"))
978 				return;
979 		}
980 		node->bus = &i2c_bus;
981 	} else
982 		return;
983 
984 	if (!node->children)
985 		return;
986 
987 	if (node_addr_cells(node) != 1)
988 		FAIL(c, dti, node, "incorrect #address-cells for I2C bus");
989 	if (node_size_cells(node) != 0)
990 		FAIL(c, dti, node, "incorrect #size-cells for I2C bus");
991 
992 }
993 WARNING(i2c_bus_bridge, check_i2c_bus_bridge, NULL, &addr_size_cells);
994 
995 static void check_i2c_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
996 {
997 	struct property *prop;
998 	const char *unitname = get_unitname(node);
999 	char unit_addr[17];
1000 	uint32_t reg = 0;
1001 	int len;
1002 	cell_t *cells = NULL;
1003 
1004 	if (!node->parent || (node->parent->bus != &i2c_bus))
1005 		return;
1006 
1007 	prop = get_property(node, "reg");
1008 	if (prop)
1009 		cells = (cell_t *)prop->val.val;
1010 
1011 	if (!cells) {
1012 		FAIL(c, dti, node, "missing or empty reg property");
1013 		return;
1014 	}
1015 
1016 	reg = fdt32_to_cpu(*cells);
1017 	snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1018 	if (!streq(unitname, unit_addr))
1019 		FAIL(c, dti, node, "I2C bus unit address format error, expected \"%s\"",
1020 		     unit_addr);
1021 
1022 	for (len = prop->val.len; len > 0; len -= 4) {
1023 		reg = fdt32_to_cpu(*(cells++));
1024 		if (reg > 0x3ff)
1025 			FAIL_PROP(c, dti, node, prop, "I2C address must be less than 10-bits, got \"0x%x\"",
1026 				  reg);
1027 
1028 	}
1029 }
1030 WARNING(i2c_bus_reg, check_i2c_bus_reg, NULL, &reg_format, &i2c_bus_bridge);
1031 
1032 static const struct bus_type spi_bus = {
1033 	.name = "spi-bus",
1034 };
1035 
1036 static void check_spi_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1037 {
1038 
1039 	if (strprefixeq(node->name, node->basenamelen, "spi")) {
1040 		node->bus = &spi_bus;
1041 	} else {
1042 		/* Try to detect SPI buses which don't have proper node name */
1043 		struct node *child;
1044 
1045 		if (node_addr_cells(node) != 1 || node_size_cells(node) != 0)
1046 			return;
1047 
1048 		for_each_child(node, child) {
1049 			struct property *prop;
1050 			for_each_property(child, prop) {
1051 				if (strprefixeq(prop->name, 4, "spi-")) {
1052 					node->bus = &spi_bus;
1053 					break;
1054 				}
1055 			}
1056 			if (node->bus == &spi_bus)
1057 				break;
1058 		}
1059 
1060 		if (node->bus == &spi_bus && get_property(node, "reg"))
1061 			FAIL(c, dti, node, "node name for SPI buses should be 'spi'");
1062 	}
1063 	if (node->bus != &spi_bus || !node->children)
1064 		return;
1065 
1066 	if (node_addr_cells(node) != 1)
1067 		FAIL(c, dti, node, "incorrect #address-cells for SPI bus");
1068 	if (node_size_cells(node) != 0)
1069 		FAIL(c, dti, node, "incorrect #size-cells for SPI bus");
1070 
1071 }
1072 WARNING(spi_bus_bridge, check_spi_bus_bridge, NULL, &addr_size_cells);
1073 
1074 static void check_spi_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1075 {
1076 	struct property *prop;
1077 	const char *unitname = get_unitname(node);
1078 	char unit_addr[9];
1079 	uint32_t reg = 0;
1080 	cell_t *cells = NULL;
1081 
1082 	if (!node->parent || (node->parent->bus != &spi_bus))
1083 		return;
1084 
1085 	prop = get_property(node, "reg");
1086 	if (prop)
1087 		cells = (cell_t *)prop->val.val;
1088 
1089 	if (!cells) {
1090 		FAIL(c, dti, node, "missing or empty reg property");
1091 		return;
1092 	}
1093 
1094 	reg = fdt32_to_cpu(*cells);
1095 	snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1096 	if (!streq(unitname, unit_addr))
1097 		FAIL(c, dti, node, "SPI bus unit address format error, expected \"%s\"",
1098 		     unit_addr);
1099 }
1100 WARNING(spi_bus_reg, check_spi_bus_reg, NULL, &reg_format, &spi_bus_bridge);
1101 
1102 static void check_unit_address_format(struct check *c, struct dt_info *dti,
1103 				      struct node *node)
1104 {
1105 	const char *unitname = get_unitname(node);
1106 
1107 	if (node->parent && node->parent->bus)
1108 		return;
1109 
1110 	if (!unitname[0])
1111 		return;
1112 
1113 	if (!strncmp(unitname, "0x", 2)) {
1114 		FAIL(c, dti, node, "unit name should not have leading \"0x\"");
1115 		/* skip over 0x for next test */
1116 		unitname += 2;
1117 	}
1118 	if (unitname[0] == '0' && isxdigit(unitname[1]))
1119 		FAIL(c, dti, node, "unit name should not have leading 0s");
1120 }
1121 WARNING(unit_address_format, check_unit_address_format, NULL,
1122 	&node_name_format, &pci_bridge, &simple_bus_bridge);
1123 
1124 /*
1125  * Style checks
1126  */
1127 static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
1128 					  struct node *node)
1129 {
1130 	struct property *reg, *ranges;
1131 
1132 	if (!node->parent)
1133 		return; /* Ignore root node */
1134 
1135 	reg = get_property(node, "reg");
1136 	ranges = get_property(node, "ranges");
1137 
1138 	if (!reg && !ranges)
1139 		return;
1140 
1141 	if (node->parent->addr_cells == -1)
1142 		FAIL(c, dti, node, "Relying on default #address-cells value");
1143 
1144 	if (node->parent->size_cells == -1)
1145 		FAIL(c, dti, node, "Relying on default #size-cells value");
1146 }
1147 WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1148 	&addr_size_cells);
1149 
1150 static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
1151 					      struct node *node)
1152 {
1153 	struct property *prop;
1154 	struct node *child;
1155 	bool has_reg = false;
1156 
1157 	if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
1158 		return;
1159 
1160 	if (get_property(node, "ranges") || !node->children)
1161 		return;
1162 
1163 	for_each_child(node, child) {
1164 		prop = get_property(child, "reg");
1165 		if (prop)
1166 			has_reg = true;
1167 	}
1168 
1169 	if (!has_reg)
1170 		FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
1171 }
1172 WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
1173 
1174 static void check_unique_unit_address(struct check *c, struct dt_info *dti,
1175 					      struct node *node)
1176 {
1177 	struct node *childa;
1178 
1179 	if (node->addr_cells < 0 || node->size_cells < 0)
1180 		return;
1181 
1182 	if (!node->children)
1183 		return;
1184 
1185 	for_each_child(node, childa) {
1186 		struct node *childb;
1187 		const char *addr_a = get_unitname(childa);
1188 
1189 		if (!strlen(addr_a))
1190 			continue;
1191 
1192 		for_each_child(node, childb) {
1193 			const char *addr_b = get_unitname(childb);
1194 			if (childa == childb)
1195 				break;
1196 
1197 			if (streq(addr_a, addr_b))
1198 				FAIL(c, dti, childb, "duplicate unit-address (also used in node %s)", childa->fullpath);
1199 		}
1200 	}
1201 }
1202 WARNING(unique_unit_address, check_unique_unit_address, NULL, &avoid_default_addr_size);
1203 
1204 static void check_obsolete_chosen_interrupt_controller(struct check *c,
1205 						       struct dt_info *dti,
1206 						       struct node *node)
1207 {
1208 	struct node *dt = dti->dt;
1209 	struct node *chosen;
1210 	struct property *prop;
1211 
1212 	if (node != dt)
1213 		return;
1214 
1215 
1216 	chosen = get_node_by_path(dt, "/chosen");
1217 	if (!chosen)
1218 		return;
1219 
1220 	prop = get_property(chosen, "interrupt-controller");
1221 	if (prop)
1222 		FAIL_PROP(c, dti, node, prop,
1223 			  "/chosen has obsolete \"interrupt-controller\" property");
1224 }
1225 WARNING(obsolete_chosen_interrupt_controller,
1226 	check_obsolete_chosen_interrupt_controller, NULL);
1227 
1228 static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
1229 				      struct node *node)
1230 {
1231 	if (!streq(node->name, "chosen"))
1232 		return;
1233 
1234 	if (node->parent != dti->dt)
1235 		FAIL(c, dti, node, "chosen node must be at root node");
1236 }
1237 WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
1238 
1239 static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
1240 				       struct node *node)
1241 {
1242 	struct property *prop;
1243 
1244 	if (!streq(node->name, "chosen"))
1245 		return;
1246 
1247 	prop = get_property(node, "bootargs");
1248 	if (!prop)
1249 		return;
1250 
1251 	c->data = prop->name;
1252 	check_is_string(c, dti, node);
1253 }
1254 WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
1255 
1256 static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
1257 					  struct node *node)
1258 {
1259 	struct property *prop;
1260 
1261 	if (!streq(node->name, "chosen"))
1262 		return;
1263 
1264 	prop = get_property(node, "stdout-path");
1265 	if (!prop) {
1266 		prop = get_property(node, "linux,stdout-path");
1267 		if (!prop)
1268 			return;
1269 		FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
1270 	}
1271 
1272 	c->data = prop->name;
1273 	check_is_string(c, dti, node);
1274 }
1275 WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
1276 
1277 struct provider {
1278 	const char *prop_name;
1279 	const char *cell_name;
1280 	bool optional;
1281 };
1282 
1283 static void check_property_phandle_args(struct check *c,
1284 					  struct dt_info *dti,
1285 				          struct node *node,
1286 				          struct property *prop,
1287 				          const struct provider *provider)
1288 {
1289 	struct node *root = dti->dt;
1290 	int cell, cellsize = 0;
1291 
1292 	if (prop->val.len % sizeof(cell_t)) {
1293 		FAIL_PROP(c, dti, node, prop,
1294 			  "property size (%d) is invalid, expected multiple of %zu",
1295 			  prop->val.len, sizeof(cell_t));
1296 		return;
1297 	}
1298 
1299 	for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
1300 		struct node *provider_node;
1301 		struct property *cellprop;
1302 		int phandle;
1303 
1304 		phandle = propval_cell_n(prop, cell);
1305 		/*
1306 		 * Some bindings use a cell value 0 or -1 to skip over optional
1307 		 * entries when each index position has a specific definition.
1308 		 */
1309 		if (phandle == 0 || phandle == -1) {
1310 			/* Give up if this is an overlay with external references */
1311 			if (dti->dtsflags & DTSF_PLUGIN)
1312 				break;
1313 
1314 			cellsize = 0;
1315 			continue;
1316 		}
1317 
1318 		/* If we have markers, verify the current cell is a phandle */
1319 		if (prop->val.markers) {
1320 			struct marker *m = prop->val.markers;
1321 			for_each_marker_of_type(m, REF_PHANDLE) {
1322 				if (m->offset == (cell * sizeof(cell_t)))
1323 					break;
1324 			}
1325 			if (!m)
1326 				FAIL_PROP(c, dti, node, prop,
1327 					  "cell %d is not a phandle reference",
1328 					  cell);
1329 		}
1330 
1331 		provider_node = get_node_by_phandle(root, phandle);
1332 		if (!provider_node) {
1333 			FAIL_PROP(c, dti, node, prop,
1334 				  "Could not get phandle node for (cell %d)",
1335 				  cell);
1336 			break;
1337 		}
1338 
1339 		cellprop = get_property(provider_node, provider->cell_name);
1340 		if (cellprop) {
1341 			cellsize = propval_cell(cellprop);
1342 		} else if (provider->optional) {
1343 			cellsize = 0;
1344 		} else {
1345 			FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
1346 			     provider->cell_name,
1347 			     provider_node->fullpath,
1348 			     prop->name, cell);
1349 			break;
1350 		}
1351 
1352 		if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
1353 			FAIL_PROP(c, dti, node, prop,
1354 				  "property size (%d) too small for cell size %d",
1355 				  prop->val.len, cellsize);
1356 		}
1357 	}
1358 }
1359 
1360 static void check_provider_cells_property(struct check *c,
1361 					  struct dt_info *dti,
1362 				          struct node *node)
1363 {
1364 	struct provider *provider = c->data;
1365 	struct property *prop;
1366 
1367 	prop = get_property(node, provider->prop_name);
1368 	if (!prop)
1369 		return;
1370 
1371 	check_property_phandle_args(c, dti, node, prop, provider);
1372 }
1373 #define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1374 	static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1375 	WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
1376 
1377 WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
1378 WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
1379 WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
1380 WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
1381 WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
1382 WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
1383 WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
1384 WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
1385 WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
1386 WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
1387 WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
1388 WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
1389 WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
1390 WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
1391 WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
1392 WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
1393 
1394 static bool prop_is_gpio(struct property *prop)
1395 {
1396 	char *str;
1397 
1398 	/*
1399 	 * *-gpios and *-gpio can appear in property names,
1400 	 * so skip over any false matches (only one known ATM)
1401 	 */
1402 	if (strstr(prop->name, "nr-gpio"))
1403 		return false;
1404 
1405 	str = strrchr(prop->name, '-');
1406 	if (str)
1407 		str++;
1408 	else
1409 		str = prop->name;
1410 	if (!(streq(str, "gpios") || streq(str, "gpio")))
1411 		return false;
1412 
1413 	return true;
1414 }
1415 
1416 static void check_gpios_property(struct check *c,
1417 					  struct dt_info *dti,
1418 				          struct node *node)
1419 {
1420 	struct property *prop;
1421 
1422 	/* Skip GPIO hog nodes which have 'gpios' property */
1423 	if (get_property(node, "gpio-hog"))
1424 		return;
1425 
1426 	for_each_property(node, prop) {
1427 		struct provider provider;
1428 
1429 		if (!prop_is_gpio(prop))
1430 			continue;
1431 
1432 		provider.prop_name = prop->name;
1433 		provider.cell_name = "#gpio-cells";
1434 		provider.optional = false;
1435 		check_property_phandle_args(c, dti, node, prop, &provider);
1436 	}
1437 
1438 }
1439 WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
1440 
1441 static void check_deprecated_gpio_property(struct check *c,
1442 					   struct dt_info *dti,
1443 				           struct node *node)
1444 {
1445 	struct property *prop;
1446 
1447 	for_each_property(node, prop) {
1448 		char *str;
1449 
1450 		if (!prop_is_gpio(prop))
1451 			continue;
1452 
1453 		str = strstr(prop->name, "gpio");
1454 		if (!streq(str, "gpio"))
1455 			continue;
1456 
1457 		FAIL_PROP(c, dti, node, prop,
1458 			  "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
1459 	}
1460 
1461 }
1462 CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
1463 
1464 static bool node_is_interrupt_provider(struct node *node)
1465 {
1466 	struct property *prop;
1467 
1468 	prop = get_property(node, "interrupt-controller");
1469 	if (prop)
1470 		return true;
1471 
1472 	prop = get_property(node, "interrupt-map");
1473 	if (prop)
1474 		return true;
1475 
1476 	return false;
1477 }
1478 static void check_interrupts_property(struct check *c,
1479 				      struct dt_info *dti,
1480 				      struct node *node)
1481 {
1482 	struct node *root = dti->dt;
1483 	struct node *irq_node = NULL, *parent = node;
1484 	struct property *irq_prop, *prop = NULL;
1485 	int irq_cells, phandle;
1486 
1487 	irq_prop = get_property(node, "interrupts");
1488 	if (!irq_prop)
1489 		return;
1490 
1491 	if (irq_prop->val.len % sizeof(cell_t))
1492 		FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1493 		     irq_prop->val.len, sizeof(cell_t));
1494 
1495 	while (parent && !prop) {
1496 		if (parent != node && node_is_interrupt_provider(parent)) {
1497 			irq_node = parent;
1498 			break;
1499 		}
1500 
1501 		prop = get_property(parent, "interrupt-parent");
1502 		if (prop) {
1503 			phandle = propval_cell(prop);
1504 			/* Give up if this is an overlay with external references */
1505 			if ((phandle == 0 || phandle == -1) &&
1506 			    (dti->dtsflags & DTSF_PLUGIN))
1507 					return;
1508 
1509 			irq_node = get_node_by_phandle(root, phandle);
1510 			if (!irq_node) {
1511 				FAIL_PROP(c, dti, parent, prop, "Bad phandle");
1512 				return;
1513 			}
1514 			if (!node_is_interrupt_provider(irq_node))
1515 				FAIL(c, dti, irq_node,
1516 				     "Missing interrupt-controller or interrupt-map property");
1517 
1518 			break;
1519 		}
1520 
1521 		parent = parent->parent;
1522 	}
1523 
1524 	if (!irq_node) {
1525 		FAIL(c, dti, node, "Missing interrupt-parent");
1526 		return;
1527 	}
1528 
1529 	prop = get_property(irq_node, "#interrupt-cells");
1530 	if (!prop) {
1531 		FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
1532 		return;
1533 	}
1534 
1535 	irq_cells = propval_cell(prop);
1536 	if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
1537 		FAIL_PROP(c, dti, node, prop,
1538 			  "size is (%d), expected multiple of %d",
1539 			  irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
1540 	}
1541 }
1542 WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1543 
1544 static const struct bus_type graph_port_bus = {
1545 	.name = "graph-port",
1546 };
1547 
1548 static const struct bus_type graph_ports_bus = {
1549 	.name = "graph-ports",
1550 };
1551 
1552 static void check_graph_nodes(struct check *c, struct dt_info *dti,
1553 			      struct node *node)
1554 {
1555 	struct node *child;
1556 
1557 	for_each_child(node, child) {
1558 		if (!(strprefixeq(child->name, child->basenamelen, "endpoint") ||
1559 		      get_property(child, "remote-endpoint")))
1560 			continue;
1561 
1562 		node->bus = &graph_port_bus;
1563 
1564 		/* The parent of 'port' nodes can be either 'ports' or a device */
1565 		if (!node->parent->bus &&
1566 		    (streq(node->parent->name, "ports") || get_property(node, "reg")))
1567 			node->parent->bus = &graph_ports_bus;
1568 
1569 		break;
1570 	}
1571 
1572 }
1573 WARNING(graph_nodes, check_graph_nodes, NULL);
1574 
1575 static void check_graph_child_address(struct check *c, struct dt_info *dti,
1576 				      struct node *node)
1577 {
1578 	int cnt = 0;
1579 	struct node *child;
1580 
1581 	if (node->bus != &graph_ports_bus && node->bus != &graph_port_bus)
1582 		return;
1583 
1584 	for_each_child(node, child) {
1585 		struct property *prop = get_property(child, "reg");
1586 
1587 		/* No error if we have any non-zero unit address */
1588 		if (prop && propval_cell(prop) != 0)
1589 			return;
1590 
1591 		cnt++;
1592 	}
1593 
1594 	if (cnt == 1 && node->addr_cells != -1)
1595 		FAIL(c, dti, node, "graph node has single child node '%s', #address-cells/#size-cells are not necessary",
1596 		     node->children->name);
1597 }
1598 WARNING(graph_child_address, check_graph_child_address, NULL, &graph_nodes);
1599 
1600 static void check_graph_reg(struct check *c, struct dt_info *dti,
1601 			    struct node *node)
1602 {
1603 	char unit_addr[9];
1604 	const char *unitname = get_unitname(node);
1605 	struct property *prop;
1606 
1607 	prop = get_property(node, "reg");
1608 	if (!prop || !unitname)
1609 		return;
1610 
1611 	if (!(prop->val.val && prop->val.len == sizeof(cell_t))) {
1612 		FAIL(c, dti, node, "graph node malformed 'reg' property");
1613 		return;
1614 	}
1615 
1616 	snprintf(unit_addr, sizeof(unit_addr), "%x", propval_cell(prop));
1617 	if (!streq(unitname, unit_addr))
1618 		FAIL(c, dti, node, "graph node unit address error, expected \"%s\"",
1619 		     unit_addr);
1620 
1621 	if (node->parent->addr_cells != 1)
1622 		FAIL_PROP(c, dti, node, get_property(node, "#address-cells"),
1623 			  "graph node '#address-cells' is %d, must be 1",
1624 			  node->parent->addr_cells);
1625 	if (node->parent->size_cells != 0)
1626 		FAIL_PROP(c, dti, node, get_property(node, "#size-cells"),
1627 			  "graph node '#size-cells' is %d, must be 0",
1628 			  node->parent->size_cells);
1629 }
1630 
1631 static void check_graph_port(struct check *c, struct dt_info *dti,
1632 			     struct node *node)
1633 {
1634 	if (node->bus != &graph_port_bus)
1635 		return;
1636 
1637 	if (!strprefixeq(node->name, node->basenamelen, "port"))
1638 		FAIL(c, dti, node, "graph port node name should be 'port'");
1639 
1640 	check_graph_reg(c, dti, node);
1641 }
1642 WARNING(graph_port, check_graph_port, NULL, &graph_nodes);
1643 
1644 static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,
1645 					struct node *endpoint)
1646 {
1647 	int phandle;
1648 	struct node *node;
1649 	struct property *prop;
1650 
1651 	prop = get_property(endpoint, "remote-endpoint");
1652 	if (!prop)
1653 		return NULL;
1654 
1655 	phandle = propval_cell(prop);
1656 	/* Give up if this is an overlay with external references */
1657 	if (phandle == 0 || phandle == -1)
1658 		return NULL;
1659 
1660 	node = get_node_by_phandle(dti->dt, phandle);
1661 	if (!node)
1662 		FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
1663 
1664 	return node;
1665 }
1666 
1667 static void check_graph_endpoint(struct check *c, struct dt_info *dti,
1668 				 struct node *node)
1669 {
1670 	struct node *remote_node;
1671 
1672 	if (!node->parent || node->parent->bus != &graph_port_bus)
1673 		return;
1674 
1675 	if (!strprefixeq(node->name, node->basenamelen, "endpoint"))
1676 		FAIL(c, dti, node, "graph endpont node name should be 'endpoint'");
1677 
1678 	check_graph_reg(c, dti, node);
1679 
1680 	remote_node = get_remote_endpoint(c, dti, node);
1681 	if (!remote_node)
1682 		return;
1683 
1684 	if (get_remote_endpoint(c, dti, remote_node) != node)
1685 		FAIL(c, dti, node, "graph connection to node '%s' is not bidirectional",
1686 		     remote_node->fullpath);
1687 }
1688 WARNING(graph_endpoint, check_graph_endpoint, NULL, &graph_nodes);
1689 
1690 static struct check *check_table[] = {
1691 	&duplicate_node_names, &duplicate_property_names,
1692 	&node_name_chars, &node_name_format, &property_name_chars,
1693 	&name_is_string, &name_properties,
1694 
1695 	&duplicate_label,
1696 
1697 	&explicit_phandles,
1698 	&phandle_references, &path_references,
1699 	&omit_unused_nodes,
1700 
1701 	&address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
1702 	&device_type_is_string, &model_is_string, &status_is_string,
1703 	&label_is_string,
1704 
1705 	&compatible_is_string_list, &names_is_string_list,
1706 
1707 	&property_name_chars_strict,
1708 	&node_name_chars_strict,
1709 
1710 	&addr_size_cells, &reg_format, &ranges_format,
1711 
1712 	&unit_address_vs_reg,
1713 	&unit_address_format,
1714 
1715 	&pci_bridge,
1716 	&pci_device_reg,
1717 	&pci_device_bus_num,
1718 
1719 	&simple_bus_bridge,
1720 	&simple_bus_reg,
1721 
1722 	&i2c_bus_bridge,
1723 	&i2c_bus_reg,
1724 
1725 	&spi_bus_bridge,
1726 	&spi_bus_reg,
1727 
1728 	&avoid_default_addr_size,
1729 	&avoid_unnecessary_addr_size,
1730 	&unique_unit_address,
1731 	&obsolete_chosen_interrupt_controller,
1732 	&chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
1733 
1734 	&clocks_property,
1735 	&cooling_device_property,
1736 	&dmas_property,
1737 	&hwlocks_property,
1738 	&interrupts_extended_property,
1739 	&io_channels_property,
1740 	&iommus_property,
1741 	&mboxes_property,
1742 	&msi_parent_property,
1743 	&mux_controls_property,
1744 	&phys_property,
1745 	&power_domains_property,
1746 	&pwms_property,
1747 	&resets_property,
1748 	&sound_dai_property,
1749 	&thermal_sensors_property,
1750 
1751 	&deprecated_gpio_property,
1752 	&gpios_property,
1753 	&interrupts_property,
1754 
1755 	&alias_paths,
1756 
1757 	&graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
1758 
1759 	&always_fail,
1760 };
1761 
1762 static void enable_warning_error(struct check *c, bool warn, bool error)
1763 {
1764 	int i;
1765 
1766 	/* Raising level, also raise it for prereqs */
1767 	if ((warn && !c->warn) || (error && !c->error))
1768 		for (i = 0; i < c->num_prereqs; i++)
1769 			enable_warning_error(c->prereq[i], warn, error);
1770 
1771 	c->warn = c->warn || warn;
1772 	c->error = c->error || error;
1773 }
1774 
1775 static void disable_warning_error(struct check *c, bool warn, bool error)
1776 {
1777 	int i;
1778 
1779 	/* Lowering level, also lower it for things this is the prereq
1780 	 * for */
1781 	if ((warn && c->warn) || (error && c->error)) {
1782 		for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1783 			struct check *cc = check_table[i];
1784 			int j;
1785 
1786 			for (j = 0; j < cc->num_prereqs; j++)
1787 				if (cc->prereq[j] == c)
1788 					disable_warning_error(cc, warn, error);
1789 		}
1790 	}
1791 
1792 	c->warn = c->warn && !warn;
1793 	c->error = c->error && !error;
1794 }
1795 
1796 void parse_checks_option(bool warn, bool error, const char *arg)
1797 {
1798 	int i;
1799 	const char *name = arg;
1800 	bool enable = true;
1801 
1802 	if ((strncmp(arg, "no-", 3) == 0)
1803 	    || (strncmp(arg, "no_", 3) == 0)) {
1804 		name = arg + 3;
1805 		enable = false;
1806 	}
1807 
1808 	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1809 		struct check *c = check_table[i];
1810 
1811 		if (streq(c->name, name)) {
1812 			if (enable)
1813 				enable_warning_error(c, warn, error);
1814 			else
1815 				disable_warning_error(c, warn, error);
1816 			return;
1817 		}
1818 	}
1819 
1820 	die("Unrecognized check name \"%s\"\n", name);
1821 }
1822 
1823 void process_checks(bool force, struct dt_info *dti)
1824 {
1825 	int i;
1826 	int error = 0;
1827 
1828 	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1829 		struct check *c = check_table[i];
1830 
1831 		if (c->warn || c->error)
1832 			error = error || run_check(c, dti);
1833 	}
1834 
1835 	if (error) {
1836 		if (!force) {
1837 			fprintf(stderr, "ERROR: Input tree has errors, aborting "
1838 				"(use -f to force output)\n");
1839 			exit(2);
1840 		} else if (quiet < 3) {
1841 			fprintf(stderr, "Warning: Input tree has errors, "
1842 				"output forced\n");
1843 		}
1844 	}
1845 }
1846