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