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