Linux Audio

Check our new training course

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