Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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