Linux Audio

Check our new training course

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