blob: 4b3c486f1399f809c0ccc250cf1419120628e822 [file] [log] [blame]
Rob Herring12869ec2019-06-21 08:18:32 -06001// SPDX-License-Identifier: GPL-2.0-or-later
David Gibsona4da2e32007-12-18 15:06:42 +11002/*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2007.
David Gibsona4da2e32007-12-18 15:06:42 +11004 */
5
6#include "dtc.h"
Rob Herringc2e70752018-11-28 18:37:35 -06007#include "srcpos.h"
David Gibsona4da2e32007-12-18 15:06:42 +11008
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
David Gibsona4da2e32007-12-18 15:06:42 +110020enum checkstatus {
21 UNCHECKED = 0,
22 PREREQ,
23 PASSED,
24 FAILED,
25};
26
27struct check;
28
Rob Herring6f05afc2017-01-04 10:45:20 -060029typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
David Gibsona4da2e32007-12-18 15:06:42 +110030
31struct check {
32 const char *name;
Rob Herring6f05afc2017-01-04 10:45:20 -060033 check_fn fn;
David Gibsona4da2e32007-12-18 15:06:42 +110034 void *data;
Stephen Warrencd296722012-09-28 21:25:59 +000035 bool warn, error;
David Gibsona4da2e32007-12-18 15:06:42 +110036 enum checkstatus status;
Rob Herring47605972015-04-29 16:00:05 -050037 bool inprogress;
David Gibsona4da2e32007-12-18 15:06:42 +110038 int num_prereqs;
39 struct check **prereq;
40};
41
Rob Herring9130ba82018-02-27 17:40:38 -060042#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_), \
David Gibsona4da2e32007-12-18 15:06:42 +110050 .status = UNCHECKED, \
Rob Herring9130ba82018-02-27 17:40:38 -060051 .num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
52 .prereq = nm_##_prereqs, \
David Gibsona4da2e32007-12-18 15:06:42 +110053 };
Rob Herring9130ba82018-02-27 17:40:38 -060054#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__)
David Gibsona4da2e32007-12-18 15:06:42 +110060
Rob Herring9130ba82018-02-27 17:40:38 -060061static inline void PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
62 struct node *node,
63 struct property *prop,
Rob Herring89d12312017-03-21 09:01:08 -050064 const char *fmt, ...)
David Gibsona4da2e32007-12-18 15:06:42 +110065{
66 va_list ap;
Rob Herringc2e70752018-11-28 18:37:35 -060067 char *str = NULL;
68 struct srcpos *pos = NULL;
69 char *file_str;
David Gibsona4da2e32007-12-18 15:06:42 +110070
Rob Herringc2e70752018-11-28 18:37:35 -060071 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);
Stephen Warrencd296722012-09-28 21:25:59 +000087 }
Rob Herringc2e70752018-11-28 18:37:35 -060088
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);
Rob Herring47605972015-04-29 16:00:05 -0500101 va_end(ap);
Rob Herringc2e70752018-11-28 18:37:35 -0600102
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);
David Gibsona4da2e32007-12-18 15:06:42 +1100117}
118
Rob Herring9130ba82018-02-27 17:40:38 -0600119#define FAIL(c, dti, node, ...) \
Rob Herring89d12312017-03-21 09:01:08 -0500120 do { \
121 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
122 (c)->status = FAILED; \
Rob Herring9130ba82018-02-27 17:40:38 -0600123 check_msg((c), dti, node, NULL, __VA_ARGS__); \
David Gibsona4da2e32007-12-18 15:06:42 +1100124 } while (0)
125
Rob Herring9130ba82018-02-27 17:40:38 -0600126#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
Rob Herring6f05afc2017-01-04 10:45:20 -0600134static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +1100135{
136 struct node *child;
David Gibsona4da2e32007-12-18 15:06:42 +1100137
138 TRACE(c, "%s", node->fullpath);
Rob Herring6f05afc2017-01-04 10:45:20 -0600139 if (c->fn)
140 c->fn(c, dti, node);
David Gibsona4da2e32007-12-18 15:06:42 +1100141
142 for_each_child(node, child)
Rob Herring6f05afc2017-01-04 10:45:20 -0600143 check_nodes_props(c, dti, child);
David Gibsona4da2e32007-12-18 15:06:42 +1100144}
145
Rob Herring6f05afc2017-01-04 10:45:20 -0600146static bool run_check(struct check *c, struct dt_info *dti)
David Gibsona4da2e32007-12-18 15:06:42 +1100147{
Rob Herring6f05afc2017-01-04 10:45:20 -0600148 struct node *dt = dti->dt;
Rob Herring47605972015-04-29 16:00:05 -0500149 bool error = false;
David Gibsona4da2e32007-12-18 15:06:42 +1100150 int i;
151
152 assert(!c->inprogress);
153
154 if (c->status != UNCHECKED)
155 goto out;
156
Rob Herring47605972015-04-29 16:00:05 -0500157 c->inprogress = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100158
159 for (i = 0; i < c->num_prereqs; i++) {
160 struct check *prq = c->prereq[i];
Rob Herring6f05afc2017-01-04 10:45:20 -0600161 error = error || run_check(prq, dti);
David Gibsona4da2e32007-12-18 15:06:42 +1100162 if (prq->status != PASSED) {
163 c->status = PREREQ;
Rob Herring9130ba82018-02-27 17:40:38 -0600164 check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
David Gibsona4da2e32007-12-18 15:06:42 +1100165 c->prereq[i]->name);
166 }
167 }
168
169 if (c->status != UNCHECKED)
170 goto out;
171
Rob Herring6f05afc2017-01-04 10:45:20 -0600172 check_nodes_props(c, dti, dt);
David Gibsona4da2e32007-12-18 15:06:42 +1100173
David Gibsona4da2e32007-12-18 15:06:42 +1100174 if (c->status == UNCHECKED)
175 c->status = PASSED;
176
177 TRACE(c, "\tCompleted, status %d", c->status);
178
179out:
Rob Herring47605972015-04-29 16:00:05 -0500180 c->inprogress = false;
Stephen Warrencd296722012-09-28 21:25:59 +0000181 if ((c->status != PASSED) && (c->error))
Rob Herring47605972015-04-29 16:00:05 -0500182 error = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100183 return error;
184}
185
186/*
187 * Utility check functions
188 */
189
Stephen Warrencd296722012-09-28 21:25:59 +0000190/* A check which always fails, for testing purposes only */
Rob Herring6f05afc2017-01-04 10:45:20 -0600191static inline void check_always_fail(struct check *c, struct dt_info *dti,
192 struct node *node)
Stephen Warrencd296722012-09-28 21:25:59 +0000193{
Rob Herring9130ba82018-02-27 17:40:38 -0600194 FAIL(c, dti, node, "always_fail check");
Stephen Warrencd296722012-09-28 21:25:59 +0000195}
Rob Herring6f05afc2017-01-04 10:45:20 -0600196CHECK(always_fail, check_always_fail, NULL);
Stephen Warrencd296722012-09-28 21:25:59 +0000197
Rob Herring6f05afc2017-01-04 10:45:20 -0600198static void check_is_string(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100199 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600209 FAIL_PROP(c, dti, node, prop, "property is not a string");
David Gibsona4da2e32007-12-18 15:06:42 +1100210}
Stephen Warrencd296722012-09-28 21:25:59 +0000211#define WARNING_IF_NOT_STRING(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600212 WARNING(nm, check_is_string, (propname))
Stephen Warrencd296722012-09-28 21:25:59 +0000213#define ERROR_IF_NOT_STRING(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600214 ERROR(nm, check_is_string, (propname))
David Gibsona4da2e32007-12-18 15:06:42 +1100215
Rob Herring9130ba82018-02-27 17:40:38 -0600216static 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
Rob Herring6f05afc2017-01-04 10:45:20 -0600245static void check_is_cell(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100246 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600256 FAIL_PROP(c, dti, node, prop, "property is not a single cell");
David Gibsona4da2e32007-12-18 15:06:42 +1100257}
Stephen Warrencd296722012-09-28 21:25:59 +0000258#define WARNING_IF_NOT_CELL(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600259 WARNING(nm, check_is_cell, (propname))
Stephen Warrencd296722012-09-28 21:25:59 +0000260#define ERROR_IF_NOT_CELL(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600261 ERROR(nm, check_is_cell, (propname))
David Gibsona4da2e32007-12-18 15:06:42 +1100262
263/*
264 * Structural check functions
265 */
266
Rob Herring6f05afc2017-01-04 10:45:20 -0600267static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100268 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))
Rob Herring50aafd62018-05-08 13:07:49 -0500277 FAIL(c, dti, child2, "Duplicate node name");
David Gibsona4da2e32007-12-18 15:06:42 +1100278}
Rob Herring6f05afc2017-01-04 10:45:20 -0600279ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100280
Rob Herring6f05afc2017-01-04 10:45:20 -0600281static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100282 struct node *node)
283{
284 struct property *prop, *prop2;
285
Stephen Warrencd296722012-09-28 21:25:59 +0000286 for_each_property(node, prop) {
287 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
288 if (prop2->deleted)
289 continue;
David Gibsona4da2e32007-12-18 15:06:42 +1100290 if (streq(prop->name, prop2->name))
Rob Herring9130ba82018-02-27 17:40:38 -0600291 FAIL_PROP(c, dti, node, prop, "Duplicate property name");
Stephen Warrencd296722012-09-28 21:25:59 +0000292 }
293 }
David Gibsona4da2e32007-12-18 15:06:42 +1100294}
Rob Herring6f05afc2017-01-04 10:45:20 -0600295ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100296
David Gibsoned95d742008-08-07 12:24:17 +1000297#define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
298#define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
299#define DIGITS "0123456789"
300#define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
Rob Herring89d12312017-03-21 09:01:08 -0500301#define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
David Gibsoned95d742008-08-07 12:24:17 +1000302
Rob Herring6f05afc2017-01-04 10:45:20 -0600303static void check_node_name_chars(struct check *c, struct dt_info *dti,
David Gibsoned95d742008-08-07 12:24:17 +1000304 struct node *node)
305{
306 int n = strspn(node->name, c->data);
307
308 if (n < strlen(node->name))
Rob Herring9130ba82018-02-27 17:40:38 -0600309 FAIL(c, dti, node, "Bad character '%c' in node name",
310 node->name[n]);
David Gibsoned95d742008-08-07 12:24:17 +1000311}
Rob Herring6f05afc2017-01-04 10:45:20 -0600312ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
David Gibsoned95d742008-08-07 12:24:17 +1000313
Rob Herring89d12312017-03-21 09:01:08 -0500314static 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)
Rob Herring9130ba82018-02-27 17:40:38 -0600320 FAIL(c, dti, node, "Character '%c' not recommended in node name",
321 node->name[n]);
Rob Herring89d12312017-03-21 09:01:08 -0500322}
323CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
324
Rob Herring6f05afc2017-01-04 10:45:20 -0600325static void check_node_name_format(struct check *c, struct dt_info *dti,
David Gibsoned95d742008-08-07 12:24:17 +1000326 struct node *node)
327{
328 if (strchr(get_unitname(node), '@'))
Rob Herring9130ba82018-02-27 17:40:38 -0600329 FAIL(c, dti, node, "multiple '@' characters in node name");
David Gibsoned95d742008-08-07 12:24:17 +1000330}
Rob Herring6f05afc2017-01-04 10:45:20 -0600331ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
David Gibsoned95d742008-08-07 12:24:17 +1000332
Rob Herring6f05afc2017-01-04 10:45:20 -0600333static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
334 struct node *node)
Rob Herringb9937342016-03-04 08:56:58 -0600335{
336 const char *unitname = get_unitname(node);
337 struct property *prop = get_property(node, "reg");
338
Rob Herring50aafd62018-05-08 13:07:49 -0500339 if (get_subnode(node, "__overlay__")) {
340 /* HACK: Overlay fragments are a special case */
341 return;
342 }
343
Rob Herringb9937342016-03-04 08:56:58 -0600344 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])
Rob Herring9130ba82018-02-27 17:40:38 -0600352 FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
Rob Herringb9937342016-03-04 08:56:58 -0600353 } else {
354 if (unitname[0])
Rob Herringd047cd82020-03-13 08:56:58 -0500355 FAIL(c, dti, node, "node has a unit name, but no reg or ranges property");
Rob Herringb9937342016-03-04 08:56:58 -0600356 }
357}
Rob Herring6f05afc2017-01-04 10:45:20 -0600358WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
Rob Herringb9937342016-03-04 08:56:58 -0600359
Rob Herring6f05afc2017-01-04 10:45:20 -0600360static void check_property_name_chars(struct check *c, struct dt_info *dti,
361 struct node *node)
David Gibsoned95d742008-08-07 12:24:17 +1000362{
Rob Herring6f05afc2017-01-04 10:45:20 -0600363 struct property *prop;
David Gibsoned95d742008-08-07 12:24:17 +1000364
Rob Herring6f05afc2017-01-04 10:45:20 -0600365 for_each_property(node, prop) {
366 int n = strspn(prop->name, c->data);
367
368 if (n < strlen(prop->name))
Rob Herring9130ba82018-02-27 17:40:38 -0600369 FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
370 prop->name[n]);
Rob Herring6f05afc2017-01-04 10:45:20 -0600371 }
David Gibsoned95d742008-08-07 12:24:17 +1000372}
Rob Herring6f05afc2017-01-04 10:45:20 -0600373ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
David Gibsoned95d742008-08-07 12:24:17 +1000374
Rob Herring89d12312017-03-21 09:01:08 -0500375static 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600401 FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
402 name[n]);
Rob Herring89d12312017-03-21 09:01:08 -0500403 }
404}
405CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
406
John Bonesio658f29a2010-11-17 15:28:20 -0800407#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
Rob Herring6f05afc2017-01-04 10:45:20 -0600414static void check_duplicate_label(struct check *c, struct dt_info *dti,
John Bonesio658f29a2010-11-17 15:28:20 -0800415 const char *label, struct node *node,
416 struct property *prop, struct marker *mark)
David Gibsona4da2e32007-12-18 15:06:42 +1100417{
Rob Herring6f05afc2017-01-04 10:45:20 -0600418 struct node *dt = dti->dt;
John Bonesio658f29a2010-11-17 15:28:20 -0800419 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600435 FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
John Bonesio658f29a2010-11-17 15:28:20 -0800436 " and " DESCLABEL_FMT,
437 label, DESCLABEL_ARGS(node, prop, mark),
438 DESCLABEL_ARGS(othernode, otherprop, othermark));
439}
440
Rob Herring6f05afc2017-01-04 10:45:20 -0600441static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
John Bonesio658f29a2010-11-17 15:28:20 -0800442 struct node *node)
443{
444 struct label *l;
Rob Herring6f05afc2017-01-04 10:45:20 -0600445 struct property *prop;
John Bonesio658f29a2010-11-17 15:28:20 -0800446
447 for_each_label(node->labels, l)
Rob Herring6f05afc2017-01-04 10:45:20 -0600448 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 }
John Bonesio658f29a2010-11-17 15:28:20 -0800459}
Rob Herring6f05afc2017-01-04 10:45:20 -0600460ERROR(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)
John Bonesio658f29a2010-11-17 15:28:20 -0800464{
Rob Herring6f05afc2017-01-04 10:45:20 -0600465 struct node *root = dti->dt;
466 struct property *prop;
John Bonesio658f29a2010-11-17 15:28:20 -0800467 struct marker *m;
David Gibsona4da2e32007-12-18 15:06:42 +1100468 cell_t phandle;
469
Rob Herring6f05afc2017-01-04 10:45:20 -0600470 prop = get_property(node, propname);
471 if (!prop)
472 return 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100473
474 if (prop->val.len != sizeof(cell_t)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600475 FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
476 prop->val.len, prop->name);
Rob Herring6f05afc2017-01-04 10:45:20 -0600477 return 0;
John Bonesio658f29a2010-11-17 15:28:20 -0800478 }
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. */ {
Rob Herring9130ba82018-02-27 17:40:38 -0600487 FAIL(c, dti, node, "%s is a reference to another node",
488 prop->name);
John Bonesio658f29a2010-11-17 15:28:20 -0800489 }
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
Rob Herring6f05afc2017-01-04 10:45:20 -0600494 * we treat it as having no phandle data for now. */
495 return 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100496 }
497
498 phandle = propval_cell(prop);
John Bonesio658f29a2010-11-17 15:28:20 -0800499
David Gibsona4da2e32007-12-18 15:06:42 +1100500 if ((phandle == 0) || (phandle == -1)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600501 FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
502 phandle, prop->name);
Rob Herring6f05afc2017-01-04 10:45:20 -0600503 return 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100504 }
505
Rob Herring6f05afc2017-01-04 10:45:20 -0600506 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600528 FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
529 " properties");
Rob Herring6f05afc2017-01-04 10:45:20 -0600530
531 if (linux_phandle && !phandle)
532 phandle = linux_phandle;
John Bonesio658f29a2010-11-17 15:28:20 -0800533
David Gibsona4da2e32007-12-18 15:06:42 +1100534 other = get_node_by_phandle(root, phandle);
John Bonesio658f29a2010-11-17 15:28:20 -0800535 if (other && (other != node)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600536 FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
537 phandle, other->fullpath);
David Gibsona4da2e32007-12-18 15:06:42 +1100538 return;
539 }
540
541 node->phandle = phandle;
542}
Rob Herring6f05afc2017-01-04 10:45:20 -0600543ERROR(explicit_phandles, check_explicit_phandles, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100544
Rob Herring6f05afc2017-01-04 10:45:20 -0600545static void check_name_properties(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100546 struct node *node)
547{
David Gibsoned95d742008-08-07 12:24:17 +1000548 struct property **pp, *prop = NULL;
David Gibsona4da2e32007-12-18 15:06:42 +1100549
David Gibsoned95d742008-08-07 12:24:17 +1000550 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
551 if (streq((*pp)->name, "name")) {
552 prop = *pp;
553 break;
554 }
555
David Gibsona4da2e32007-12-18 15:06:42 +1100556 if (!prop)
557 return; /* No name property, that's fine */
558
559 if ((prop->val.len != node->basenamelen+1)
David Gibsoned95d742008-08-07 12:24:17 +1000560 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600561 FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
562 " of base node name)", prop->val.val);
David Gibsoned95d742008-08-07 12:24:17 +1000563 } 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 }
David Gibsona4da2e32007-12-18 15:06:42 +1100571}
Stephen Warrencd296722012-09-28 21:25:59 +0000572ERROR_IF_NOT_STRING(name_is_string, "name");
Rob Herring6f05afc2017-01-04 10:45:20 -0600573ERROR(name_properties, check_name_properties, NULL, &name_is_string);
David Gibsona4da2e32007-12-18 15:06:42 +1100574
575/*
576 * Reference fixup functions
577 */
578
Rob Herring6f05afc2017-01-04 10:45:20 -0600579static void fixup_phandle_references(struct check *c, struct dt_info *dti,
580 struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +1100581{
Rob Herring6f05afc2017-01-04 10:45:20 -0600582 struct node *dt = dti->dt;
583 struct property *prop;
David Gibsona4da2e32007-12-18 15:06:42 +1100584
Rob Herring6f05afc2017-01-04 10:45:20 -0600585 for_each_property(node, prop) {
586 struct marker *m = prop->val.markers;
587 struct node *refnode;
588 cell_t phandle;
David Gibsona4da2e32007-12-18 15:06:42 +1100589
Rob Herring6f05afc2017-01-04 10:45:20 -0600590 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600596 FAIL(c, dti, node, "Reference to non-existent node or "
Rob Herring6f05afc2017-01-04 10:45:20 -0600597 "label \"%s\"\n", m->ref);
598 else /* mark the entry as unresolved */
Rob Herring89d12312017-03-21 09:01:08 -0500599 *((fdt32_t *)(prop->val.val + m->offset)) =
Rob Herring6f05afc2017-01-04 10:45:20 -0600600 cpu_to_fdt32(0xffffffff);
601 continue;
602 }
603
604 phandle = get_node_phandle(dt, refnode);
Rob Herring89d12312017-03-21 09:01:08 -0500605 *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
Rob Herring50aafd62018-05-08 13:07:49 -0500606
607 reference_node(refnode);
David Gibsoned95d742008-08-07 12:24:17 +1000608 }
David Gibsoned95d742008-08-07 12:24:17 +1000609 }
David Gibsona4da2e32007-12-18 15:06:42 +1100610}
Rob Herring6f05afc2017-01-04 10:45:20 -0600611ERROR(phandle_references, fixup_phandle_references, NULL,
David Gibsona4da2e32007-12-18 15:06:42 +1100612 &duplicate_node_names, &explicit_phandles);
613
Rob Herring6f05afc2017-01-04 10:45:20 -0600614static void fixup_path_references(struct check *c, struct dt_info *dti,
615 struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +1100616{
Rob Herring6f05afc2017-01-04 10:45:20 -0600617 struct node *dt = dti->dt;
618 struct property *prop;
David Gibsona4da2e32007-12-18 15:06:42 +1100619
Rob Herring6f05afc2017-01-04 10:45:20 -0600620 for_each_property(node, prop) {
621 struct marker *m = prop->val.markers;
622 struct node *refnode;
623 char *path;
David Gibsona4da2e32007-12-18 15:06:42 +1100624
Rob Herring6f05afc2017-01-04 10:45:20 -0600625 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) {
Rob Herring9130ba82018-02-27 17:40:38 -0600630 FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
Rob Herring6f05afc2017-01-04 10:45:20 -0600631 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);
Rob Herring50aafd62018-05-08 13:07:49 -0500638
639 reference_node(refnode);
David Gibsona4da2e32007-12-18 15:06:42 +1100640 }
David Gibsona4da2e32007-12-18 15:06:42 +1100641 }
642}
Rob Herring6f05afc2017-01-04 10:45:20 -0600643ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
David Gibsona4da2e32007-12-18 15:06:42 +1100644
Rob Herring50aafd62018-05-08 13:07:49 -0500645static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
646 struct node *node)
647{
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600648 if (generate_symbols && node->labels)
649 return;
Rob Herring50aafd62018-05-08 13:07:49 -0500650 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
David Gibsona4da2e32007-12-18 15:06:42 +1100655/*
656 * Semantic checks
657 */
Stephen Warrencd296722012-09-28 21:25:59 +0000658WARNING_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");
David Gibsona4da2e32007-12-18 15:06:42 +1100661
Stephen Warrencd296722012-09-28 21:25:59 +0000662WARNING_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");
Rob Herring9130ba82018-02-27 17:40:38 -0600665WARNING_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) {
Rob Herring0cec1142019-12-26 15:36:47 -0700694 if (streq(prop->name, "phandle")
695 || streq(prop->name, "linux,phandle")) {
696 continue;
697 }
698
Rob Herring9130ba82018-02-27 17:40:38 -0600699 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);
David Gibsona4da2e32007-12-18 15:06:42 +1100709
Rob Herring6f05afc2017-01-04 10:45:20 -0600710static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100711 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}
Rob Herring6f05afc2017-01-04 10:45:20 -0600726WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
Stephen Warrencd296722012-09-28 21:25:59 +0000727 &address_cells_is_cell, &size_cells_is_cell);
David Gibsona4da2e32007-12-18 15:06:42 +1100728
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
Rob Herring6f05afc2017-01-04 10:45:20 -0600734static void check_reg_format(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100735 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) {
Rob Herring9130ba82018-02-27 17:40:38 -0600745 FAIL(c, dti, node, "Root node has a \"reg\" property");
David Gibsona4da2e32007-12-18 15:06:42 +1100746 return;
747 }
748
749 if (prop->val.len == 0)
Rob Herring9130ba82018-02-27 17:40:38 -0600750 FAIL_PROP(c, dti, node, prop, "property is empty");
David Gibsona4da2e32007-12-18 15:06:42 +1100751
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
Rob Herring91feabc2016-01-26 09:04:11 -0600756 if (!entrylen || (prop->val.len % entrylen) != 0)
Rob Herring9130ba82018-02-27 17:40:38 -0600757 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);
David Gibsona4da2e32007-12-18 15:06:42 +1100760}
Rob Herring6f05afc2017-01-04 10:45:20 -0600761WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100762
Rob Herring6f05afc2017-01-04 10:45:20 -0600763static void check_ranges_format(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100764 struct node *node)
765{
766 struct property *prop;
767 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
Rob Herringd047cd82020-03-13 08:56:58 -0500768 const char *ranges = c->data;
David Gibsona4da2e32007-12-18 15:06:42 +1100769
Rob Herringd047cd82020-03-13 08:56:58 -0500770 prop = get_property(node, ranges);
David Gibsona4da2e32007-12-18 15:06:42 +1100771 if (!prop)
772 return;
773
774 if (!node->parent) {
Rob Herringd047cd82020-03-13 08:56:58 -0500775 FAIL_PROP(c, dti, node, prop, "Root node has a \"%s\" property",
776 ranges);
David Gibsona4da2e32007-12-18 15:06:42 +1100777 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)
Rob Herringd047cd82020-03-13 08:56:58 -0500788 FAIL_PROP(c, dti, node, prop, "empty \"%s\" property but its "
Rob Herring9130ba82018-02-27 17:40:38 -0600789 "#address-cells (%d) differs from %s (%d)",
Rob Herringd047cd82020-03-13 08:56:58 -0500790 ranges, c_addr_cells, node->parent->fullpath,
Rob Herring9130ba82018-02-27 17:40:38 -0600791 p_addr_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100792 if (p_size_cells != c_size_cells)
Rob Herringd047cd82020-03-13 08:56:58 -0500793 FAIL_PROP(c, dti, node, prop, "empty \"%s\" property but its "
Rob Herring9130ba82018-02-27 17:40:38 -0600794 "#size-cells (%d) differs from %s (%d)",
Rob Herringd047cd82020-03-13 08:56:58 -0500795 ranges, c_size_cells, node->parent->fullpath,
Rob Herring9130ba82018-02-27 17:40:38 -0600796 p_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100797 } else if ((prop->val.len % entrylen) != 0) {
Rob Herringd047cd82020-03-13 08:56:58 -0500798 FAIL_PROP(c, dti, node, prop, "\"%s\" property has invalid length (%d bytes) "
Rob Herring9130ba82018-02-27 17:40:38 -0600799 "(parent #address-cells == %d, child #address-cells == %d, "
Rob Herringd047cd82020-03-13 08:56:58 -0500800 "#size-cells == %d)", ranges, prop->val.len,
Rob Herring9130ba82018-02-27 17:40:38 -0600801 p_addr_cells, c_addr_cells, c_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100802 }
803}
Rob Herringd047cd82020-03-13 08:56:58 -0500804WARNING(ranges_format, check_ranges_format, "ranges", &addr_size_cells);
805WARNING(dma_ranges_format, check_ranges_format, "dma-ranges", &addr_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100806
Rob Herring89d12312017-03-21 09:01:08 -0500807static 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
Rob Herring9130ba82018-02-27 17:40:38 -0600822 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\"");
Rob Herring89d12312017-03-21 09:01:08 -0500825
826 prop = get_property(node, "ranges");
827 if (!prop)
Rob Herring9130ba82018-02-27 17:40:38 -0600828 FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
Rob Herring89d12312017-03-21 09:01:08 -0500829
830 if (node_addr_cells(node) != 3)
Rob Herring9130ba82018-02-27 17:40:38 -0600831 FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
Rob Herring89d12312017-03-21 09:01:08 -0500832 if (node_size_cells(node) != 2)
Rob Herring9130ba82018-02-27 17:40:38 -0600833 FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
Rob Herring89d12312017-03-21 09:01:08 -0500834
835 prop = get_property(node, "bus-range");
Rob Herring970f04c2018-04-20 08:08:23 -0500836 if (!prop)
Rob Herring89d12312017-03-21 09:01:08 -0500837 return;
Rob Herring970f04c2018-04-20 08:08:23 -0500838
Rob Herring89d12312017-03-21 09:01:08 -0500839 if (prop->val.len != (sizeof(cell_t) * 2)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600840 FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
Rob Herring89d12312017-03-21 09:01:08 -0500841 return;
842 }
843 cells = (cell_t *)prop->val.val;
844 if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
Rob Herring9130ba82018-02-27 17:40:38 -0600845 FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
Rob Herring89d12312017-03-21 09:01:08 -0500846 if (fdt32_to_cpu(cells[1]) > 0xff)
Rob Herring9130ba82018-02-27 17:40:38 -0600847 FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
Rob Herring89d12312017-03-21 09:01:08 -0500848}
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))
Rob Herring9130ba82018-02-27 17:40:38 -0600877 FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
878 bus_num, min_bus, max_bus);
Rob Herring89d12312017-03-21 09:01:08 -0500879}
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) {
Rob Herring9130ba82018-02-27 17:40:38 -0600895 FAIL(c, dti, node, "missing PCI reg property");
Rob Herring89d12312017-03-21 09:01:08 -0500896 return;
897 }
898
899 cells = (cell_t *)prop->val.val;
900 if (cells[1] || cells[2])
Rob Herring9130ba82018-02-27 17:40:38 -0600901 FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
Rob Herring89d12312017-03-21 09:01:08 -0500902
903 reg = fdt32_to_cpu(cells[0]);
904 dev = (reg & 0xf800) >> 11;
905 func = (reg & 0x700) >> 8;
906
907 if (reg & 0xff000000)
Rob Herring9130ba82018-02-27 17:40:38 -0600908 FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
Rob Herring89d12312017-03-21 09:01:08 -0500909 if (reg & 0x000000ff)
Rob Herring9130ba82018-02-27 17:40:38 -0600910 FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
Rob Herring89d12312017-03-21 09:01:08 -0500911
912 if (func == 0) {
913 snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
914 if (streq(unitname, unit_addr))
915 return;
916 }
917
918 snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
919 if (streq(unitname, unit_addr))
920 return;
921
Rob Herring9130ba82018-02-27 17:40:38 -0600922 FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
923 unit_addr);
Rob Herring89d12312017-03-21 09:01:08 -0500924}
925WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
926
927static const struct bus_type simple_bus = {
928 .name = "simple-bus",
929};
930
931static bool node_is_compatible(struct node *node, const char *compat)
932{
933 struct property *prop;
934 const char *str, *end;
935
936 prop = get_property(node, "compatible");
937 if (!prop)
938 return false;
939
940 for (str = prop->val.val, end = str + prop->val.len; str < end;
941 str += strnlen(str, end - str) + 1) {
Rob Herringc2e70752018-11-28 18:37:35 -0600942 if (streq(str, compat))
Rob Herring89d12312017-03-21 09:01:08 -0500943 return true;
944 }
945 return false;
946}
947
948static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
949{
950 if (node_is_compatible(node, "simple-bus"))
951 node->bus = &simple_bus;
952}
Rob Herringc2e70752018-11-28 18:37:35 -0600953WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL,
954 &addr_size_cells, &compatible_is_string_list);
Rob Herring89d12312017-03-21 09:01:08 -0500955
956static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
957{
958 struct property *prop;
959 const char *unitname = get_unitname(node);
960 char unit_addr[17];
961 unsigned int size;
962 uint64_t reg = 0;
963 cell_t *cells = NULL;
964
965 if (!node->parent || (node->parent->bus != &simple_bus))
966 return;
967
968 prop = get_property(node, "reg");
969 if (prop)
970 cells = (cell_t *)prop->val.val;
971 else {
972 prop = get_property(node, "ranges");
973 if (prop && prop->val.len)
974 /* skip of child address */
975 cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
976 }
977
978 if (!cells) {
979 if (node->parent->parent && !(node->bus == &simple_bus))
Rob Herring9130ba82018-02-27 17:40:38 -0600980 FAIL(c, dti, node, "missing or empty reg/ranges property");
Rob Herring89d12312017-03-21 09:01:08 -0500981 return;
982 }
983
984 size = node_addr_cells(node->parent);
985 while (size--)
986 reg = (reg << 32) | fdt32_to_cpu(*(cells++));
987
Rob Herring4201d052017-10-03 11:37:04 -0500988 snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
Rob Herring89d12312017-03-21 09:01:08 -0500989 if (!streq(unitname, unit_addr))
Rob Herring9130ba82018-02-27 17:40:38 -0600990 FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
991 unit_addr);
Rob Herring89d12312017-03-21 09:01:08 -0500992}
993WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
994
Rob Herringf8589272018-09-13 08:59:25 -0500995static const struct bus_type i2c_bus = {
996 .name = "i2c-bus",
997};
998
999static void check_i2c_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1000{
1001 if (strprefixeq(node->name, node->basenamelen, "i2c-bus") ||
1002 strprefixeq(node->name, node->basenamelen, "i2c-arb")) {
1003 node->bus = &i2c_bus;
1004 } else if (strprefixeq(node->name, node->basenamelen, "i2c")) {
1005 struct node *child;
1006 for_each_child(node, child) {
1007 if (strprefixeq(child->name, node->basenamelen, "i2c-bus"))
1008 return;
1009 }
1010 node->bus = &i2c_bus;
1011 } else
1012 return;
1013
1014 if (!node->children)
1015 return;
1016
1017 if (node_addr_cells(node) != 1)
1018 FAIL(c, dti, node, "incorrect #address-cells for I2C bus");
1019 if (node_size_cells(node) != 0)
1020 FAIL(c, dti, node, "incorrect #size-cells for I2C bus");
1021
1022}
1023WARNING(i2c_bus_bridge, check_i2c_bus_bridge, NULL, &addr_size_cells);
1024
1025static void check_i2c_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1026{
1027 struct property *prop;
1028 const char *unitname = get_unitname(node);
1029 char unit_addr[17];
1030 uint32_t reg = 0;
1031 int len;
1032 cell_t *cells = NULL;
1033
1034 if (!node->parent || (node->parent->bus != &i2c_bus))
1035 return;
1036
1037 prop = get_property(node, "reg");
1038 if (prop)
1039 cells = (cell_t *)prop->val.val;
1040
1041 if (!cells) {
1042 FAIL(c, dti, node, "missing or empty reg property");
1043 return;
1044 }
1045
1046 reg = fdt32_to_cpu(*cells);
1047 snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1048 if (!streq(unitname, unit_addr))
1049 FAIL(c, dti, node, "I2C bus unit address format error, expected \"%s\"",
1050 unit_addr);
1051
1052 for (len = prop->val.len; len > 0; len -= 4) {
1053 reg = fdt32_to_cpu(*(cells++));
1054 if (reg > 0x3ff)
1055 FAIL_PROP(c, dti, node, prop, "I2C address must be less than 10-bits, got \"0x%x\"",
1056 reg);
1057
1058 }
1059}
1060WARNING(i2c_bus_reg, check_i2c_bus_reg, NULL, &reg_format, &i2c_bus_bridge);
1061
1062static const struct bus_type spi_bus = {
1063 .name = "spi-bus",
1064};
1065
1066static void check_spi_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1067{
Rob Herringc2e70752018-11-28 18:37:35 -06001068 int spi_addr_cells = 1;
Rob Herringf8589272018-09-13 08:59:25 -05001069
1070 if (strprefixeq(node->name, node->basenamelen, "spi")) {
1071 node->bus = &spi_bus;
1072 } else {
1073 /* Try to detect SPI buses which don't have proper node name */
1074 struct node *child;
1075
1076 if (node_addr_cells(node) != 1 || node_size_cells(node) != 0)
1077 return;
1078
1079 for_each_child(node, child) {
1080 struct property *prop;
1081 for_each_property(child, prop) {
1082 if (strprefixeq(prop->name, 4, "spi-")) {
1083 node->bus = &spi_bus;
1084 break;
1085 }
1086 }
1087 if (node->bus == &spi_bus)
1088 break;
1089 }
1090
1091 if (node->bus == &spi_bus && get_property(node, "reg"))
1092 FAIL(c, dti, node, "node name for SPI buses should be 'spi'");
1093 }
1094 if (node->bus != &spi_bus || !node->children)
1095 return;
1096
Rob Herringc2e70752018-11-28 18:37:35 -06001097 if (get_property(node, "spi-slave"))
1098 spi_addr_cells = 0;
1099 if (node_addr_cells(node) != spi_addr_cells)
Rob Herringf8589272018-09-13 08:59:25 -05001100 FAIL(c, dti, node, "incorrect #address-cells for SPI bus");
1101 if (node_size_cells(node) != 0)
1102 FAIL(c, dti, node, "incorrect #size-cells for SPI bus");
1103
1104}
1105WARNING(spi_bus_bridge, check_spi_bus_bridge, NULL, &addr_size_cells);
1106
1107static void check_spi_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1108{
1109 struct property *prop;
1110 const char *unitname = get_unitname(node);
1111 char unit_addr[9];
1112 uint32_t reg = 0;
1113 cell_t *cells = NULL;
1114
1115 if (!node->parent || (node->parent->bus != &spi_bus))
1116 return;
1117
Rob Herringc2e70752018-11-28 18:37:35 -06001118 if (get_property(node->parent, "spi-slave"))
1119 return;
1120
Rob Herringf8589272018-09-13 08:59:25 -05001121 prop = get_property(node, "reg");
1122 if (prop)
1123 cells = (cell_t *)prop->val.val;
1124
1125 if (!cells) {
1126 FAIL(c, dti, node, "missing or empty reg property");
1127 return;
1128 }
1129
1130 reg = fdt32_to_cpu(*cells);
1131 snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1132 if (!streq(unitname, unit_addr))
1133 FAIL(c, dti, node, "SPI bus unit address format error, expected \"%s\"",
1134 unit_addr);
1135}
1136WARNING(spi_bus_reg, check_spi_bus_reg, NULL, &reg_format, &spi_bus_bridge);
1137
Rob Herring89d12312017-03-21 09:01:08 -05001138static void check_unit_address_format(struct check *c, struct dt_info *dti,
1139 struct node *node)
1140{
1141 const char *unitname = get_unitname(node);
1142
1143 if (node->parent && node->parent->bus)
1144 return;
1145
1146 if (!unitname[0])
1147 return;
1148
1149 if (!strncmp(unitname, "0x", 2)) {
Rob Herring9130ba82018-02-27 17:40:38 -06001150 FAIL(c, dti, node, "unit name should not have leading \"0x\"");
Rob Herring89d12312017-03-21 09:01:08 -05001151 /* skip over 0x for next test */
1152 unitname += 2;
1153 }
1154 if (unitname[0] == '0' && isxdigit(unitname[1]))
Rob Herring9130ba82018-02-27 17:40:38 -06001155 FAIL(c, dti, node, "unit name should not have leading 0s");
Rob Herring89d12312017-03-21 09:01:08 -05001156}
1157WARNING(unit_address_format, check_unit_address_format, NULL,
1158 &node_name_format, &pci_bridge, &simple_bus_bridge);
1159
David Gibsona4da2e32007-12-18 15:06:42 +11001160/*
1161 * Style checks
1162 */
Rob Herring6f05afc2017-01-04 10:45:20 -06001163static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +11001164 struct node *node)
1165{
1166 struct property *reg, *ranges;
1167
1168 if (!node->parent)
1169 return; /* Ignore root node */
1170
1171 reg = get_property(node, "reg");
1172 ranges = get_property(node, "ranges");
1173
1174 if (!reg && !ranges)
1175 return;
1176
Rob Herring47605972015-04-29 16:00:05 -05001177 if (node->parent->addr_cells == -1)
Rob Herring9130ba82018-02-27 17:40:38 -06001178 FAIL(c, dti, node, "Relying on default #address-cells value");
David Gibsona4da2e32007-12-18 15:06:42 +11001179
Rob Herring47605972015-04-29 16:00:05 -05001180 if (node->parent->size_cells == -1)
Rob Herring9130ba82018-02-27 17:40:38 -06001181 FAIL(c, dti, node, "Relying on default #size-cells value");
David Gibsona4da2e32007-12-18 15:06:42 +11001182}
Rob Herring6f05afc2017-01-04 10:45:20 -06001183WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1184 &addr_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +11001185
Rob Herring9130ba82018-02-27 17:40:38 -06001186static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
1187 struct node *node)
1188{
1189 struct property *prop;
1190 struct node *child;
1191 bool has_reg = false;
1192
1193 if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
1194 return;
1195
1196 if (get_property(node, "ranges") || !node->children)
1197 return;
1198
1199 for_each_child(node, child) {
1200 prop = get_property(child, "reg");
1201 if (prop)
1202 has_reg = true;
1203 }
1204
1205 if (!has_reg)
1206 FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
1207}
1208WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
1209
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001210static bool node_is_disabled(struct node *node)
1211{
1212 struct property *prop;
1213
1214 prop = get_property(node, "status");
1215 if (prop) {
1216 char *str = prop->val.val;
1217 if (streq("disabled", str))
1218 return true;
1219 }
1220
1221 return false;
1222}
1223
1224static void check_unique_unit_address_common(struct check *c,
1225 struct dt_info *dti,
1226 struct node *node,
1227 bool disable_check)
Rob Herring50aafd62018-05-08 13:07:49 -05001228{
1229 struct node *childa;
1230
1231 if (node->addr_cells < 0 || node->size_cells < 0)
1232 return;
1233
1234 if (!node->children)
1235 return;
1236
1237 for_each_child(node, childa) {
1238 struct node *childb;
1239 const char *addr_a = get_unitname(childa);
1240
1241 if (!strlen(addr_a))
1242 continue;
1243
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001244 if (disable_check && node_is_disabled(childa))
1245 continue;
1246
Rob Herring50aafd62018-05-08 13:07:49 -05001247 for_each_child(node, childb) {
1248 const char *addr_b = get_unitname(childb);
1249 if (childa == childb)
1250 break;
1251
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001252 if (disable_check && node_is_disabled(childb))
1253 continue;
1254
Rob Herring50aafd62018-05-08 13:07:49 -05001255 if (streq(addr_a, addr_b))
1256 FAIL(c, dti, childb, "duplicate unit-address (also used in node %s)", childa->fullpath);
1257 }
1258 }
1259}
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001260
1261static void check_unique_unit_address(struct check *c, struct dt_info *dti,
1262 struct node *node)
1263{
1264 check_unique_unit_address_common(c, dti, node, false);
1265}
Rob Herring50aafd62018-05-08 13:07:49 -05001266WARNING(unique_unit_address, check_unique_unit_address, NULL, &avoid_default_addr_size);
1267
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001268static void check_unique_unit_address_if_enabled(struct check *c, struct dt_info *dti,
1269 struct node *node)
1270{
1271 check_unique_unit_address_common(c, dti, node, true);
1272}
1273CHECK_ENTRY(unique_unit_address_if_enabled, check_unique_unit_address_if_enabled,
1274 NULL, false, false, &avoid_default_addr_size);
1275
David Gibsona4da2e32007-12-18 15:06:42 +11001276static void check_obsolete_chosen_interrupt_controller(struct check *c,
Rob Herring6f05afc2017-01-04 10:45:20 -06001277 struct dt_info *dti,
1278 struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +11001279{
Rob Herring6f05afc2017-01-04 10:45:20 -06001280 struct node *dt = dti->dt;
David Gibsona4da2e32007-12-18 15:06:42 +11001281 struct node *chosen;
1282 struct property *prop;
1283
Rob Herring6f05afc2017-01-04 10:45:20 -06001284 if (node != dt)
1285 return;
1286
1287
David Gibsona4da2e32007-12-18 15:06:42 +11001288 chosen = get_node_by_path(dt, "/chosen");
1289 if (!chosen)
1290 return;
1291
1292 prop = get_property(chosen, "interrupt-controller");
1293 if (prop)
Rob Herring9130ba82018-02-27 17:40:38 -06001294 FAIL_PROP(c, dti, node, prop,
1295 "/chosen has obsolete \"interrupt-controller\" property");
David Gibsona4da2e32007-12-18 15:06:42 +11001296}
Rob Herring6f05afc2017-01-04 10:45:20 -06001297WARNING(obsolete_chosen_interrupt_controller,
1298 check_obsolete_chosen_interrupt_controller, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +11001299
Rob Herring9130ba82018-02-27 17:40:38 -06001300static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
1301 struct node *node)
1302{
1303 if (!streq(node->name, "chosen"))
1304 return;
1305
1306 if (node->parent != dti->dt)
1307 FAIL(c, dti, node, "chosen node must be at root node");
1308}
1309WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
1310
1311static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
1312 struct node *node)
1313{
1314 struct property *prop;
1315
1316 if (!streq(node->name, "chosen"))
1317 return;
1318
1319 prop = get_property(node, "bootargs");
1320 if (!prop)
1321 return;
1322
1323 c->data = prop->name;
1324 check_is_string(c, dti, node);
1325}
1326WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
1327
1328static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
1329 struct node *node)
1330{
1331 struct property *prop;
1332
1333 if (!streq(node->name, "chosen"))
1334 return;
1335
1336 prop = get_property(node, "stdout-path");
1337 if (!prop) {
1338 prop = get_property(node, "linux,stdout-path");
1339 if (!prop)
1340 return;
1341 FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
1342 }
1343
1344 c->data = prop->name;
1345 check_is_string(c, dti, node);
1346}
1347WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
1348
Rob Herring4201d052017-10-03 11:37:04 -05001349struct provider {
1350 const char *prop_name;
1351 const char *cell_name;
1352 bool optional;
1353};
1354
1355static void check_property_phandle_args(struct check *c,
1356 struct dt_info *dti,
1357 struct node *node,
1358 struct property *prop,
1359 const struct provider *provider)
1360{
1361 struct node *root = dti->dt;
1362 int cell, cellsize = 0;
1363
1364 if (prop->val.len % sizeof(cell_t)) {
Rob Herring9130ba82018-02-27 17:40:38 -06001365 FAIL_PROP(c, dti, node, prop,
1366 "property size (%d) is invalid, expected multiple of %zu",
1367 prop->val.len, sizeof(cell_t));
Rob Herring4201d052017-10-03 11:37:04 -05001368 return;
1369 }
1370
1371 for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
1372 struct node *provider_node;
1373 struct property *cellprop;
1374 int phandle;
1375
1376 phandle = propval_cell_n(prop, cell);
1377 /*
1378 * Some bindings use a cell value 0 or -1 to skip over optional
1379 * entries when each index position has a specific definition.
1380 */
1381 if (phandle == 0 || phandle == -1) {
Rob Herringe45fe7f2017-10-25 10:59:13 -05001382 /* Give up if this is an overlay with external references */
1383 if (dti->dtsflags & DTSF_PLUGIN)
1384 break;
1385
Rob Herring4201d052017-10-03 11:37:04 -05001386 cellsize = 0;
1387 continue;
1388 }
1389
1390 /* If we have markers, verify the current cell is a phandle */
1391 if (prop->val.markers) {
1392 struct marker *m = prop->val.markers;
1393 for_each_marker_of_type(m, REF_PHANDLE) {
1394 if (m->offset == (cell * sizeof(cell_t)))
1395 break;
1396 }
1397 if (!m)
Rob Herring9130ba82018-02-27 17:40:38 -06001398 FAIL_PROP(c, dti, node, prop,
1399 "cell %d is not a phandle reference",
1400 cell);
Rob Herring4201d052017-10-03 11:37:04 -05001401 }
1402
1403 provider_node = get_node_by_phandle(root, phandle);
1404 if (!provider_node) {
Rob Herring9130ba82018-02-27 17:40:38 -06001405 FAIL_PROP(c, dti, node, prop,
1406 "Could not get phandle node for (cell %d)",
1407 cell);
Rob Herring4201d052017-10-03 11:37:04 -05001408 break;
1409 }
1410
1411 cellprop = get_property(provider_node, provider->cell_name);
1412 if (cellprop) {
1413 cellsize = propval_cell(cellprop);
1414 } else if (provider->optional) {
1415 cellsize = 0;
1416 } else {
Rob Herring9130ba82018-02-27 17:40:38 -06001417 FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
Rob Herring4201d052017-10-03 11:37:04 -05001418 provider->cell_name,
1419 provider_node->fullpath,
Rob Herring9130ba82018-02-27 17:40:38 -06001420 prop->name, cell);
Rob Herring4201d052017-10-03 11:37:04 -05001421 break;
1422 }
1423
1424 if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
Rob Herring9130ba82018-02-27 17:40:38 -06001425 FAIL_PROP(c, dti, node, prop,
1426 "property size (%d) too small for cell size %d",
1427 prop->val.len, cellsize);
Rob Herring4201d052017-10-03 11:37:04 -05001428 }
1429 }
1430}
1431
1432static void check_provider_cells_property(struct check *c,
1433 struct dt_info *dti,
1434 struct node *node)
1435{
1436 struct provider *provider = c->data;
1437 struct property *prop;
1438
1439 prop = get_property(node, provider->prop_name);
1440 if (!prop)
1441 return;
1442
1443 check_property_phandle_args(c, dti, node, prop, provider);
1444}
1445#define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1446 static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1447 WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
1448
1449WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
1450WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
1451WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
1452WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
1453WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
1454WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
1455WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
1456WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
1457WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
1458WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
1459WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
1460WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
1461WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
1462WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
Rob Herring9130ba82018-02-27 17:40:38 -06001463WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
Rob Herring4201d052017-10-03 11:37:04 -05001464WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
1465
1466static bool prop_is_gpio(struct property *prop)
1467{
1468 char *str;
1469
1470 /*
1471 * *-gpios and *-gpio can appear in property names,
1472 * so skip over any false matches (only one known ATM)
1473 */
1474 if (strstr(prop->name, "nr-gpio"))
1475 return false;
1476
1477 str = strrchr(prop->name, '-');
1478 if (str)
1479 str++;
1480 else
1481 str = prop->name;
1482 if (!(streq(str, "gpios") || streq(str, "gpio")))
1483 return false;
1484
1485 return true;
1486}
1487
1488static void check_gpios_property(struct check *c,
1489 struct dt_info *dti,
1490 struct node *node)
1491{
1492 struct property *prop;
1493
1494 /* Skip GPIO hog nodes which have 'gpios' property */
1495 if (get_property(node, "gpio-hog"))
1496 return;
1497
1498 for_each_property(node, prop) {
1499 struct provider provider;
1500
1501 if (!prop_is_gpio(prop))
1502 continue;
1503
1504 provider.prop_name = prop->name;
1505 provider.cell_name = "#gpio-cells";
1506 provider.optional = false;
1507 check_property_phandle_args(c, dti, node, prop, &provider);
1508 }
1509
1510}
1511WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
1512
1513static void check_deprecated_gpio_property(struct check *c,
1514 struct dt_info *dti,
1515 struct node *node)
1516{
1517 struct property *prop;
1518
1519 for_each_property(node, prop) {
1520 char *str;
1521
1522 if (!prop_is_gpio(prop))
1523 continue;
1524
1525 str = strstr(prop->name, "gpio");
1526 if (!streq(str, "gpio"))
1527 continue;
1528
Rob Herring9130ba82018-02-27 17:40:38 -06001529 FAIL_PROP(c, dti, node, prop,
1530 "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
Rob Herring4201d052017-10-03 11:37:04 -05001531 }
1532
1533}
1534CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
1535
1536static bool node_is_interrupt_provider(struct node *node)
1537{
1538 struct property *prop;
1539
1540 prop = get_property(node, "interrupt-controller");
1541 if (prop)
1542 return true;
1543
1544 prop = get_property(node, "interrupt-map");
1545 if (prop)
1546 return true;
1547
1548 return false;
1549}
1550static void check_interrupts_property(struct check *c,
1551 struct dt_info *dti,
1552 struct node *node)
1553{
1554 struct node *root = dti->dt;
1555 struct node *irq_node = NULL, *parent = node;
1556 struct property *irq_prop, *prop = NULL;
1557 int irq_cells, phandle;
1558
1559 irq_prop = get_property(node, "interrupts");
1560 if (!irq_prop)
1561 return;
1562
1563 if (irq_prop->val.len % sizeof(cell_t))
Rob Herring9130ba82018-02-27 17:40:38 -06001564 FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1565 irq_prop->val.len, sizeof(cell_t));
Rob Herring4201d052017-10-03 11:37:04 -05001566
1567 while (parent && !prop) {
1568 if (parent != node && node_is_interrupt_provider(parent)) {
1569 irq_node = parent;
1570 break;
1571 }
1572
1573 prop = get_property(parent, "interrupt-parent");
1574 if (prop) {
1575 phandle = propval_cell(prop);
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001576 if ((phandle == 0) || (phandle == -1)) {
1577 /* Give up if this is an overlay with
1578 * external references */
1579 if (dti->dtsflags & DTSF_PLUGIN)
Rob Herringe45fe7f2017-10-25 10:59:13 -05001580 return;
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001581 FAIL_PROP(c, dti, parent, prop, "Invalid phandle");
1582 continue;
1583 }
Rob Herringe45fe7f2017-10-25 10:59:13 -05001584
Rob Herring4201d052017-10-03 11:37:04 -05001585 irq_node = get_node_by_phandle(root, phandle);
1586 if (!irq_node) {
Rob Herring9130ba82018-02-27 17:40:38 -06001587 FAIL_PROP(c, dti, parent, prop, "Bad phandle");
Rob Herring4201d052017-10-03 11:37:04 -05001588 return;
1589 }
1590 if (!node_is_interrupt_provider(irq_node))
Rob Herring9130ba82018-02-27 17:40:38 -06001591 FAIL(c, dti, irq_node,
1592 "Missing interrupt-controller or interrupt-map property");
Rob Herring4201d052017-10-03 11:37:04 -05001593
1594 break;
1595 }
1596
1597 parent = parent->parent;
1598 }
1599
1600 if (!irq_node) {
Rob Herring9130ba82018-02-27 17:40:38 -06001601 FAIL(c, dti, node, "Missing interrupt-parent");
Rob Herring4201d052017-10-03 11:37:04 -05001602 return;
1603 }
1604
1605 prop = get_property(irq_node, "#interrupt-cells");
1606 if (!prop) {
Rob Herring9130ba82018-02-27 17:40:38 -06001607 FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
Rob Herring4201d052017-10-03 11:37:04 -05001608 return;
1609 }
1610
1611 irq_cells = propval_cell(prop);
1612 if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
Rob Herring9130ba82018-02-27 17:40:38 -06001613 FAIL_PROP(c, dti, node, prop,
1614 "size is (%d), expected multiple of %d",
1615 irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
Rob Herring4201d052017-10-03 11:37:04 -05001616 }
1617}
1618WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1619
Rob Herring50aafd62018-05-08 13:07:49 -05001620static const struct bus_type graph_port_bus = {
1621 .name = "graph-port",
1622};
1623
1624static const struct bus_type graph_ports_bus = {
1625 .name = "graph-ports",
1626};
1627
1628static void check_graph_nodes(struct check *c, struct dt_info *dti,
1629 struct node *node)
1630{
1631 struct node *child;
1632
1633 for_each_child(node, child) {
1634 if (!(strprefixeq(child->name, child->basenamelen, "endpoint") ||
1635 get_property(child, "remote-endpoint")))
1636 continue;
1637
1638 node->bus = &graph_port_bus;
1639
1640 /* The parent of 'port' nodes can be either 'ports' or a device */
1641 if (!node->parent->bus &&
1642 (streq(node->parent->name, "ports") || get_property(node, "reg")))
1643 node->parent->bus = &graph_ports_bus;
1644
1645 break;
1646 }
1647
1648}
1649WARNING(graph_nodes, check_graph_nodes, NULL);
1650
1651static void check_graph_child_address(struct check *c, struct dt_info *dti,
1652 struct node *node)
1653{
1654 int cnt = 0;
1655 struct node *child;
1656
1657 if (node->bus != &graph_ports_bus && node->bus != &graph_port_bus)
1658 return;
1659
1660 for_each_child(node, child) {
1661 struct property *prop = get_property(child, "reg");
1662
1663 /* No error if we have any non-zero unit address */
1664 if (prop && propval_cell(prop) != 0)
1665 return;
1666
1667 cnt++;
1668 }
1669
1670 if (cnt == 1 && node->addr_cells != -1)
1671 FAIL(c, dti, node, "graph node has single child node '%s', #address-cells/#size-cells are not necessary",
1672 node->children->name);
1673}
1674WARNING(graph_child_address, check_graph_child_address, NULL, &graph_nodes);
1675
1676static void check_graph_reg(struct check *c, struct dt_info *dti,
1677 struct node *node)
1678{
1679 char unit_addr[9];
1680 const char *unitname = get_unitname(node);
1681 struct property *prop;
1682
1683 prop = get_property(node, "reg");
1684 if (!prop || !unitname)
1685 return;
1686
1687 if (!(prop->val.val && prop->val.len == sizeof(cell_t))) {
1688 FAIL(c, dti, node, "graph node malformed 'reg' property");
1689 return;
1690 }
1691
1692 snprintf(unit_addr, sizeof(unit_addr), "%x", propval_cell(prop));
1693 if (!streq(unitname, unit_addr))
1694 FAIL(c, dti, node, "graph node unit address error, expected \"%s\"",
1695 unit_addr);
1696
1697 if (node->parent->addr_cells != 1)
1698 FAIL_PROP(c, dti, node, get_property(node, "#address-cells"),
1699 "graph node '#address-cells' is %d, must be 1",
1700 node->parent->addr_cells);
1701 if (node->parent->size_cells != 0)
1702 FAIL_PROP(c, dti, node, get_property(node, "#size-cells"),
1703 "graph node '#size-cells' is %d, must be 0",
1704 node->parent->size_cells);
1705}
1706
1707static void check_graph_port(struct check *c, struct dt_info *dti,
1708 struct node *node)
1709{
1710 if (node->bus != &graph_port_bus)
1711 return;
1712
1713 if (!strprefixeq(node->name, node->basenamelen, "port"))
1714 FAIL(c, dti, node, "graph port node name should be 'port'");
1715
1716 check_graph_reg(c, dti, node);
1717}
1718WARNING(graph_port, check_graph_port, NULL, &graph_nodes);
1719
1720static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,
1721 struct node *endpoint)
1722{
1723 int phandle;
1724 struct node *node;
1725 struct property *prop;
1726
1727 prop = get_property(endpoint, "remote-endpoint");
1728 if (!prop)
1729 return NULL;
1730
1731 phandle = propval_cell(prop);
1732 /* Give up if this is an overlay with external references */
1733 if (phandle == 0 || phandle == -1)
1734 return NULL;
1735
1736 node = get_node_by_phandle(dti->dt, phandle);
1737 if (!node)
1738 FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
1739
1740 return node;
1741}
1742
1743static void check_graph_endpoint(struct check *c, struct dt_info *dti,
1744 struct node *node)
1745{
1746 struct node *remote_node;
1747
1748 if (!node->parent || node->parent->bus != &graph_port_bus)
1749 return;
1750
1751 if (!strprefixeq(node->name, node->basenamelen, "endpoint"))
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001752 FAIL(c, dti, node, "graph endpoint node name should be 'endpoint'");
Rob Herring50aafd62018-05-08 13:07:49 -05001753
1754 check_graph_reg(c, dti, node);
1755
1756 remote_node = get_remote_endpoint(c, dti, node);
1757 if (!remote_node)
1758 return;
1759
1760 if (get_remote_endpoint(c, dti, remote_node) != node)
1761 FAIL(c, dti, node, "graph connection to node '%s' is not bidirectional",
1762 remote_node->fullpath);
1763}
1764WARNING(graph_endpoint, check_graph_endpoint, NULL, &graph_nodes);
1765
David Gibsona4da2e32007-12-18 15:06:42 +11001766static struct check *check_table[] = {
1767 &duplicate_node_names, &duplicate_property_names,
David Gibsoned95d742008-08-07 12:24:17 +10001768 &node_name_chars, &node_name_format, &property_name_chars,
David Gibsona4da2e32007-12-18 15:06:42 +11001769 &name_is_string, &name_properties,
John Bonesio658f29a2010-11-17 15:28:20 -08001770
1771 &duplicate_label,
1772
David Gibsona4da2e32007-12-18 15:06:42 +11001773 &explicit_phandles,
1774 &phandle_references, &path_references,
Rob Herring50aafd62018-05-08 13:07:49 -05001775 &omit_unused_nodes,
David Gibsona4da2e32007-12-18 15:06:42 +11001776
1777 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
1778 &device_type_is_string, &model_is_string, &status_is_string,
Rob Herring9130ba82018-02-27 17:40:38 -06001779 &label_is_string,
1780
1781 &compatible_is_string_list, &names_is_string_list,
David Gibsona4da2e32007-12-18 15:06:42 +11001782
Rob Herring89d12312017-03-21 09:01:08 -05001783 &property_name_chars_strict,
1784 &node_name_chars_strict,
1785
Rob Herringd047cd82020-03-13 08:56:58 -05001786 &addr_size_cells, &reg_format, &ranges_format, &dma_ranges_format,
David Gibsona4da2e32007-12-18 15:06:42 +11001787
Rob Herringb9937342016-03-04 08:56:58 -06001788 &unit_address_vs_reg,
Rob Herring89d12312017-03-21 09:01:08 -05001789 &unit_address_format,
1790
1791 &pci_bridge,
1792 &pci_device_reg,
1793 &pci_device_bus_num,
1794
1795 &simple_bus_bridge,
1796 &simple_bus_reg,
Rob Herringb9937342016-03-04 08:56:58 -06001797
Rob Herringf8589272018-09-13 08:59:25 -05001798 &i2c_bus_bridge,
1799 &i2c_bus_reg,
1800
1801 &spi_bus_bridge,
1802 &spi_bus_reg,
1803
David Gibsona4da2e32007-12-18 15:06:42 +11001804 &avoid_default_addr_size,
Rob Herring9130ba82018-02-27 17:40:38 -06001805 &avoid_unnecessary_addr_size,
Rob Herring50aafd62018-05-08 13:07:49 -05001806 &unique_unit_address,
Rob Herring9bb9c6a2019-06-12 07:05:52 -06001807 &unique_unit_address_if_enabled,
David Gibsona4da2e32007-12-18 15:06:42 +11001808 &obsolete_chosen_interrupt_controller,
Rob Herring9130ba82018-02-27 17:40:38 -06001809 &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
Stephen Warrencd296722012-09-28 21:25:59 +00001810
Rob Herring4201d052017-10-03 11:37:04 -05001811 &clocks_property,
1812 &cooling_device_property,
1813 &dmas_property,
1814 &hwlocks_property,
1815 &interrupts_extended_property,
1816 &io_channels_property,
1817 &iommus_property,
1818 &mboxes_property,
1819 &msi_parent_property,
1820 &mux_controls_property,
1821 &phys_property,
1822 &power_domains_property,
1823 &pwms_property,
1824 &resets_property,
Rob Herring9130ba82018-02-27 17:40:38 -06001825 &sound_dai_property,
Rob Herring4201d052017-10-03 11:37:04 -05001826 &thermal_sensors_property,
1827
1828 &deprecated_gpio_property,
1829 &gpios_property,
1830 &interrupts_property,
1831
Rob Herring9130ba82018-02-27 17:40:38 -06001832 &alias_paths,
1833
Rob Herring50aafd62018-05-08 13:07:49 -05001834 &graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
1835
Stephen Warrencd296722012-09-28 21:25:59 +00001836 &always_fail,
David Gibsona4da2e32007-12-18 15:06:42 +11001837};
1838
Stephen Warrencd296722012-09-28 21:25:59 +00001839static void enable_warning_error(struct check *c, bool warn, bool error)
1840{
1841 int i;
1842
1843 /* Raising level, also raise it for prereqs */
1844 if ((warn && !c->warn) || (error && !c->error))
1845 for (i = 0; i < c->num_prereqs; i++)
1846 enable_warning_error(c->prereq[i], warn, error);
1847
1848 c->warn = c->warn || warn;
1849 c->error = c->error || error;
1850}
1851
1852static void disable_warning_error(struct check *c, bool warn, bool error)
1853{
1854 int i;
1855
1856 /* Lowering level, also lower it for things this is the prereq
1857 * for */
1858 if ((warn && c->warn) || (error && c->error)) {
1859 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1860 struct check *cc = check_table[i];
1861 int j;
1862
1863 for (j = 0; j < cc->num_prereqs; j++)
1864 if (cc->prereq[j] == c)
1865 disable_warning_error(cc, warn, error);
1866 }
1867 }
1868
1869 c->warn = c->warn && !warn;
1870 c->error = c->error && !error;
1871}
1872
Rob Herring47605972015-04-29 16:00:05 -05001873void parse_checks_option(bool warn, bool error, const char *arg)
Stephen Warrencd296722012-09-28 21:25:59 +00001874{
1875 int i;
Rob Herring47605972015-04-29 16:00:05 -05001876 const char *name = arg;
Stephen Warrencd296722012-09-28 21:25:59 +00001877 bool enable = true;
1878
Rob Herring47605972015-04-29 16:00:05 -05001879 if ((strncmp(arg, "no-", 3) == 0)
1880 || (strncmp(arg, "no_", 3) == 0)) {
1881 name = arg + 3;
Stephen Warrencd296722012-09-28 21:25:59 +00001882 enable = false;
1883 }
1884
1885 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1886 struct check *c = check_table[i];
1887
1888 if (streq(c->name, name)) {
1889 if (enable)
1890 enable_warning_error(c, warn, error);
1891 else
1892 disable_warning_error(c, warn, error);
1893 return;
1894 }
1895 }
1896
1897 die("Unrecognized check name \"%s\"\n", name);
1898}
1899
Rob Herring6f05afc2017-01-04 10:45:20 -06001900void process_checks(bool force, struct dt_info *dti)
David Gibsona4da2e32007-12-18 15:06:42 +11001901{
David Gibsona4da2e32007-12-18 15:06:42 +11001902 int i;
1903 int error = 0;
1904
1905 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1906 struct check *c = check_table[i];
1907
Stephen Warrencd296722012-09-28 21:25:59 +00001908 if (c->warn || c->error)
Rob Herring6f05afc2017-01-04 10:45:20 -06001909 error = error || run_check(c, dti);
David Gibsona4da2e32007-12-18 15:06:42 +11001910 }
1911
1912 if (error) {
1913 if (!force) {
1914 fprintf(stderr, "ERROR: Input tree has errors, aborting "
1915 "(use -f to force output)\n");
1916 exit(2);
1917 } else if (quiet < 3) {
1918 fprintf(stderr, "Warning: Input tree has errors, "
1919 "output forced\n");
1920 }
1921 }
David Gibsona4da2e32007-12-18 15:06:42 +11001922}