blob: 4834e44b37b2dc6635a103ae4e6deba10c660874 [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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"
Rob Herringc2e70752018-11-28 18:37:35 -060022#include "srcpos.h"
David Gibsona4da2e32007-12-18 15:06:42 +110023
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
David Gibsona4da2e32007-12-18 15:06:42 +110035enum checkstatus {
36 UNCHECKED = 0,
37 PREREQ,
38 PASSED,
39 FAILED,
40};
41
42struct check;
43
Rob Herring6f05afc2017-01-04 10:45:20 -060044typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
David Gibsona4da2e32007-12-18 15:06:42 +110045
46struct check {
47 const char *name;
Rob Herring6f05afc2017-01-04 10:45:20 -060048 check_fn fn;
David Gibsona4da2e32007-12-18 15:06:42 +110049 void *data;
Stephen Warrencd296722012-09-28 21:25:59 +000050 bool warn, error;
David Gibsona4da2e32007-12-18 15:06:42 +110051 enum checkstatus status;
Rob Herring47605972015-04-29 16:00:05 -050052 bool inprogress;
David Gibsona4da2e32007-12-18 15:06:42 +110053 int num_prereqs;
54 struct check **prereq;
55};
56
Rob Herring9130ba82018-02-27 17:40:38 -060057#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_), \
David Gibsona4da2e32007-12-18 15:06:42 +110065 .status = UNCHECKED, \
Rob Herring9130ba82018-02-27 17:40:38 -060066 .num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
67 .prereq = nm_##_prereqs, \
David Gibsona4da2e32007-12-18 15:06:42 +110068 };
Rob Herring9130ba82018-02-27 17:40:38 -060069#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__)
David Gibsona4da2e32007-12-18 15:06:42 +110075
Rob Herring9130ba82018-02-27 17:40:38 -060076static inline void PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
77 struct node *node,
78 struct property *prop,
Rob Herring89d12312017-03-21 09:01:08 -050079 const char *fmt, ...)
David Gibsona4da2e32007-12-18 15:06:42 +110080{
81 va_list ap;
Rob Herringc2e70752018-11-28 18:37:35 -060082 char *str = NULL;
83 struct srcpos *pos = NULL;
84 char *file_str;
David Gibsona4da2e32007-12-18 15:06:42 +110085
Rob Herringc2e70752018-11-28 18:37:35 -060086 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);
Stephen Warrencd296722012-09-28 21:25:59 +0000102 }
Rob Herringc2e70752018-11-28 18:37:35 -0600103
104 xasprintf_append(&str, ": %s (%s): ",
105 (c->error) ? "ERROR" : "Warning", c->name);
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);
112 }
113
114 va_start(ap, fmt);
115 xavsprintf_append(&str, fmt, ap);
Rob Herring47605972015-04-29 16:00:05 -0500116 va_end(ap);
Rob Herringc2e70752018-11-28 18:37:35 -0600117
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);
David Gibsona4da2e32007-12-18 15:06:42 +1100132}
133
Rob Herring9130ba82018-02-27 17:40:38 -0600134#define FAIL(c, dti, node, ...) \
Rob Herring89d12312017-03-21 09:01:08 -0500135 do { \
136 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
137 (c)->status = FAILED; \
Rob Herring9130ba82018-02-27 17:40:38 -0600138 check_msg((c), dti, node, NULL, __VA_ARGS__); \
David Gibsona4da2e32007-12-18 15:06:42 +1100139 } while (0)
140
Rob Herring9130ba82018-02-27 17:40:38 -0600141#define FAIL_PROP(c, dti, node, prop, ...) \
142 do { \
143 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
144 (c)->status = FAILED; \
145 check_msg((c), dti, node, prop, __VA_ARGS__); \
146 } while (0)
147
148
Rob Herring6f05afc2017-01-04 10:45:20 -0600149static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +1100150{
151 struct node *child;
David Gibsona4da2e32007-12-18 15:06:42 +1100152
153 TRACE(c, "%s", node->fullpath);
Rob Herring6f05afc2017-01-04 10:45:20 -0600154 if (c->fn)
155 c->fn(c, dti, node);
David Gibsona4da2e32007-12-18 15:06:42 +1100156
157 for_each_child(node, child)
Rob Herring6f05afc2017-01-04 10:45:20 -0600158 check_nodes_props(c, dti, child);
David Gibsona4da2e32007-12-18 15:06:42 +1100159}
160
Rob Herring6f05afc2017-01-04 10:45:20 -0600161static bool run_check(struct check *c, struct dt_info *dti)
David Gibsona4da2e32007-12-18 15:06:42 +1100162{
Rob Herring6f05afc2017-01-04 10:45:20 -0600163 struct node *dt = dti->dt;
Rob Herring47605972015-04-29 16:00:05 -0500164 bool error = false;
David Gibsona4da2e32007-12-18 15:06:42 +1100165 int i;
166
167 assert(!c->inprogress);
168
169 if (c->status != UNCHECKED)
170 goto out;
171
Rob Herring47605972015-04-29 16:00:05 -0500172 c->inprogress = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100173
174 for (i = 0; i < c->num_prereqs; i++) {
175 struct check *prq = c->prereq[i];
Rob Herring6f05afc2017-01-04 10:45:20 -0600176 error = error || run_check(prq, dti);
David Gibsona4da2e32007-12-18 15:06:42 +1100177 if (prq->status != PASSED) {
178 c->status = PREREQ;
Rob Herring9130ba82018-02-27 17:40:38 -0600179 check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
David Gibsona4da2e32007-12-18 15:06:42 +1100180 c->prereq[i]->name);
181 }
182 }
183
184 if (c->status != UNCHECKED)
185 goto out;
186
Rob Herring6f05afc2017-01-04 10:45:20 -0600187 check_nodes_props(c, dti, dt);
David Gibsona4da2e32007-12-18 15:06:42 +1100188
David Gibsona4da2e32007-12-18 15:06:42 +1100189 if (c->status == UNCHECKED)
190 c->status = PASSED;
191
192 TRACE(c, "\tCompleted, status %d", c->status);
193
194out:
Rob Herring47605972015-04-29 16:00:05 -0500195 c->inprogress = false;
Stephen Warrencd296722012-09-28 21:25:59 +0000196 if ((c->status != PASSED) && (c->error))
Rob Herring47605972015-04-29 16:00:05 -0500197 error = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100198 return error;
199}
200
201/*
202 * Utility check functions
203 */
204
Stephen Warrencd296722012-09-28 21:25:59 +0000205/* A check which always fails, for testing purposes only */
Rob Herring6f05afc2017-01-04 10:45:20 -0600206static inline void check_always_fail(struct check *c, struct dt_info *dti,
207 struct node *node)
Stephen Warrencd296722012-09-28 21:25:59 +0000208{
Rob Herring9130ba82018-02-27 17:40:38 -0600209 FAIL(c, dti, node, "always_fail check");
Stephen Warrencd296722012-09-28 21:25:59 +0000210}
Rob Herring6f05afc2017-01-04 10:45:20 -0600211CHECK(always_fail, check_always_fail, NULL);
Stephen Warrencd296722012-09-28 21:25:59 +0000212
Rob Herring6f05afc2017-01-04 10:45:20 -0600213static void check_is_string(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100214 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600224 FAIL_PROP(c, dti, node, prop, "property is not a string");
David Gibsona4da2e32007-12-18 15:06:42 +1100225}
Stephen Warrencd296722012-09-28 21:25:59 +0000226#define WARNING_IF_NOT_STRING(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600227 WARNING(nm, check_is_string, (propname))
Stephen Warrencd296722012-09-28 21:25:59 +0000228#define ERROR_IF_NOT_STRING(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600229 ERROR(nm, check_is_string, (propname))
David Gibsona4da2e32007-12-18 15:06:42 +1100230
Rob Herring9130ba82018-02-27 17:40:38 -0600231static 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
Rob Herring6f05afc2017-01-04 10:45:20 -0600260static void check_is_cell(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100261 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600271 FAIL_PROP(c, dti, node, prop, "property is not a single cell");
David Gibsona4da2e32007-12-18 15:06:42 +1100272}
Stephen Warrencd296722012-09-28 21:25:59 +0000273#define WARNING_IF_NOT_CELL(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600274 WARNING(nm, check_is_cell, (propname))
Stephen Warrencd296722012-09-28 21:25:59 +0000275#define ERROR_IF_NOT_CELL(nm, propname) \
Rob Herring6f05afc2017-01-04 10:45:20 -0600276 ERROR(nm, check_is_cell, (propname))
David Gibsona4da2e32007-12-18 15:06:42 +1100277
278/*
279 * Structural check functions
280 */
281
Rob Herring6f05afc2017-01-04 10:45:20 -0600282static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100283 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))
Rob Herring50aafd62018-05-08 13:07:49 -0500292 FAIL(c, dti, child2, "Duplicate node name");
David Gibsona4da2e32007-12-18 15:06:42 +1100293}
Rob Herring6f05afc2017-01-04 10:45:20 -0600294ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100295
Rob Herring6f05afc2017-01-04 10:45:20 -0600296static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100297 struct node *node)
298{
299 struct property *prop, *prop2;
300
Stephen Warrencd296722012-09-28 21:25:59 +0000301 for_each_property(node, prop) {
302 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
303 if (prop2->deleted)
304 continue;
David Gibsona4da2e32007-12-18 15:06:42 +1100305 if (streq(prop->name, prop2->name))
Rob Herring9130ba82018-02-27 17:40:38 -0600306 FAIL_PROP(c, dti, node, prop, "Duplicate property name");
Stephen Warrencd296722012-09-28 21:25:59 +0000307 }
308 }
David Gibsona4da2e32007-12-18 15:06:42 +1100309}
Rob Herring6f05afc2017-01-04 10:45:20 -0600310ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100311
David Gibsoned95d742008-08-07 12:24:17 +1000312#define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
313#define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
314#define DIGITS "0123456789"
315#define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
Rob Herring89d12312017-03-21 09:01:08 -0500316#define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
David Gibsoned95d742008-08-07 12:24:17 +1000317
Rob Herring6f05afc2017-01-04 10:45:20 -0600318static void check_node_name_chars(struct check *c, struct dt_info *dti,
David Gibsoned95d742008-08-07 12:24:17 +1000319 struct node *node)
320{
321 int n = strspn(node->name, c->data);
322
323 if (n < strlen(node->name))
Rob Herring9130ba82018-02-27 17:40:38 -0600324 FAIL(c, dti, node, "Bad character '%c' in node name",
325 node->name[n]);
David Gibsoned95d742008-08-07 12:24:17 +1000326}
Rob Herring6f05afc2017-01-04 10:45:20 -0600327ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
David Gibsoned95d742008-08-07 12:24:17 +1000328
Rob Herring89d12312017-03-21 09:01:08 -0500329static 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)
Rob Herring9130ba82018-02-27 17:40:38 -0600335 FAIL(c, dti, node, "Character '%c' not recommended in node name",
336 node->name[n]);
Rob Herring89d12312017-03-21 09:01:08 -0500337}
338CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
339
Rob Herring6f05afc2017-01-04 10:45:20 -0600340static void check_node_name_format(struct check *c, struct dt_info *dti,
David Gibsoned95d742008-08-07 12:24:17 +1000341 struct node *node)
342{
343 if (strchr(get_unitname(node), '@'))
Rob Herring9130ba82018-02-27 17:40:38 -0600344 FAIL(c, dti, node, "multiple '@' characters in node name");
David Gibsoned95d742008-08-07 12:24:17 +1000345}
Rob Herring6f05afc2017-01-04 10:45:20 -0600346ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
David Gibsoned95d742008-08-07 12:24:17 +1000347
Rob Herring6f05afc2017-01-04 10:45:20 -0600348static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
349 struct node *node)
Rob Herringb9937342016-03-04 08:56:58 -0600350{
351 const char *unitname = get_unitname(node);
352 struct property *prop = get_property(node, "reg");
353
Rob Herring50aafd62018-05-08 13:07:49 -0500354 if (get_subnode(node, "__overlay__")) {
355 /* HACK: Overlay fragments are a special case */
356 return;
357 }
358
Rob Herringb9937342016-03-04 08:56:58 -0600359 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])
Rob Herring9130ba82018-02-27 17:40:38 -0600367 FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
Rob Herringb9937342016-03-04 08:56:58 -0600368 } else {
369 if (unitname[0])
Rob Herring9130ba82018-02-27 17:40:38 -0600370 FAIL(c, dti, node, "node has a unit name, but no reg property");
Rob Herringb9937342016-03-04 08:56:58 -0600371 }
372}
Rob Herring6f05afc2017-01-04 10:45:20 -0600373WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
Rob Herringb9937342016-03-04 08:56:58 -0600374
Rob Herring6f05afc2017-01-04 10:45:20 -0600375static void check_property_name_chars(struct check *c, struct dt_info *dti,
376 struct node *node)
David Gibsoned95d742008-08-07 12:24:17 +1000377{
Rob Herring6f05afc2017-01-04 10:45:20 -0600378 struct property *prop;
David Gibsoned95d742008-08-07 12:24:17 +1000379
Rob Herring6f05afc2017-01-04 10:45:20 -0600380 for_each_property(node, prop) {
381 int n = strspn(prop->name, c->data);
382
383 if (n < strlen(prop->name))
Rob Herring9130ba82018-02-27 17:40:38 -0600384 FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
385 prop->name[n]);
Rob Herring6f05afc2017-01-04 10:45:20 -0600386 }
David Gibsoned95d742008-08-07 12:24:17 +1000387}
Rob Herring6f05afc2017-01-04 10:45:20 -0600388ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
David Gibsoned95d742008-08-07 12:24:17 +1000389
Rob Herring89d12312017-03-21 09:01:08 -0500390static 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600416 FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
417 name[n]);
Rob Herring89d12312017-03-21 09:01:08 -0500418 }
419}
420CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
421
John Bonesio658f29a2010-11-17 15:28:20 -0800422#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
Rob Herring6f05afc2017-01-04 10:45:20 -0600429static void check_duplicate_label(struct check *c, struct dt_info *dti,
John Bonesio658f29a2010-11-17 15:28:20 -0800430 const char *label, struct node *node,
431 struct property *prop, struct marker *mark)
David Gibsona4da2e32007-12-18 15:06:42 +1100432{
Rob Herring6f05afc2017-01-04 10:45:20 -0600433 struct node *dt = dti->dt;
John Bonesio658f29a2010-11-17 15:28:20 -0800434 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600450 FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
John Bonesio658f29a2010-11-17 15:28:20 -0800451 " and " DESCLABEL_FMT,
452 label, DESCLABEL_ARGS(node, prop, mark),
453 DESCLABEL_ARGS(othernode, otherprop, othermark));
454}
455
Rob Herring6f05afc2017-01-04 10:45:20 -0600456static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
John Bonesio658f29a2010-11-17 15:28:20 -0800457 struct node *node)
458{
459 struct label *l;
Rob Herring6f05afc2017-01-04 10:45:20 -0600460 struct property *prop;
John Bonesio658f29a2010-11-17 15:28:20 -0800461
462 for_each_label(node->labels, l)
Rob Herring6f05afc2017-01-04 10:45:20 -0600463 check_duplicate_label(c, dti, l->label, node, NULL, NULL);
464
465 for_each_property(node, prop) {
466 struct marker *m = prop->val.markers;
467
468 for_each_label(prop->labels, l)
469 check_duplicate_label(c, dti, l->label, node, prop, NULL);
470
471 for_each_marker_of_type(m, LABEL)
472 check_duplicate_label(c, dti, m->ref, node, prop, m);
473 }
John Bonesio658f29a2010-11-17 15:28:20 -0800474}
Rob Herring6f05afc2017-01-04 10:45:20 -0600475ERROR(duplicate_label, check_duplicate_label_node, NULL);
476
477static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
478 struct node *node, const char *propname)
John Bonesio658f29a2010-11-17 15:28:20 -0800479{
Rob Herring6f05afc2017-01-04 10:45:20 -0600480 struct node *root = dti->dt;
481 struct property *prop;
John Bonesio658f29a2010-11-17 15:28:20 -0800482 struct marker *m;
David Gibsona4da2e32007-12-18 15:06:42 +1100483 cell_t phandle;
484
Rob Herring6f05afc2017-01-04 10:45:20 -0600485 prop = get_property(node, propname);
486 if (!prop)
487 return 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100488
489 if (prop->val.len != sizeof(cell_t)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600490 FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
491 prop->val.len, prop->name);
Rob Herring6f05afc2017-01-04 10:45:20 -0600492 return 0;
John Bonesio658f29a2010-11-17 15:28:20 -0800493 }
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. */ {
Rob Herring9130ba82018-02-27 17:40:38 -0600502 FAIL(c, dti, node, "%s is a reference to another node",
503 prop->name);
John Bonesio658f29a2010-11-17 15:28:20 -0800504 }
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
Rob Herring6f05afc2017-01-04 10:45:20 -0600509 * we treat it as having no phandle data for now. */
510 return 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100511 }
512
513 phandle = propval_cell(prop);
John Bonesio658f29a2010-11-17 15:28:20 -0800514
David Gibsona4da2e32007-12-18 15:06:42 +1100515 if ((phandle == 0) || (phandle == -1)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600516 FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
517 phandle, prop->name);
Rob Herring6f05afc2017-01-04 10:45:20 -0600518 return 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100519 }
520
Rob Herring6f05afc2017-01-04 10:45:20 -0600521 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600543 FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
544 " properties");
Rob Herring6f05afc2017-01-04 10:45:20 -0600545
546 if (linux_phandle && !phandle)
547 phandle = linux_phandle;
John Bonesio658f29a2010-11-17 15:28:20 -0800548
David Gibsona4da2e32007-12-18 15:06:42 +1100549 other = get_node_by_phandle(root, phandle);
John Bonesio658f29a2010-11-17 15:28:20 -0800550 if (other && (other != node)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600551 FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
552 phandle, other->fullpath);
David Gibsona4da2e32007-12-18 15:06:42 +1100553 return;
554 }
555
556 node->phandle = phandle;
557}
Rob Herring6f05afc2017-01-04 10:45:20 -0600558ERROR(explicit_phandles, check_explicit_phandles, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100559
Rob Herring6f05afc2017-01-04 10:45:20 -0600560static void check_name_properties(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100561 struct node *node)
562{
David Gibsoned95d742008-08-07 12:24:17 +1000563 struct property **pp, *prop = NULL;
David Gibsona4da2e32007-12-18 15:06:42 +1100564
David Gibsoned95d742008-08-07 12:24:17 +1000565 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
566 if (streq((*pp)->name, "name")) {
567 prop = *pp;
568 break;
569 }
570
David Gibsona4da2e32007-12-18 15:06:42 +1100571 if (!prop)
572 return; /* No name property, that's fine */
573
574 if ((prop->val.len != node->basenamelen+1)
David Gibsoned95d742008-08-07 12:24:17 +1000575 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600576 FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
577 " of base node name)", prop->val.val);
David Gibsoned95d742008-08-07 12:24:17 +1000578 } 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 }
David Gibsona4da2e32007-12-18 15:06:42 +1100586}
Stephen Warrencd296722012-09-28 21:25:59 +0000587ERROR_IF_NOT_STRING(name_is_string, "name");
Rob Herring6f05afc2017-01-04 10:45:20 -0600588ERROR(name_properties, check_name_properties, NULL, &name_is_string);
David Gibsona4da2e32007-12-18 15:06:42 +1100589
590/*
591 * Reference fixup functions
592 */
593
Rob Herring6f05afc2017-01-04 10:45:20 -0600594static void fixup_phandle_references(struct check *c, struct dt_info *dti,
595 struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +1100596{
Rob Herring6f05afc2017-01-04 10:45:20 -0600597 struct node *dt = dti->dt;
598 struct property *prop;
David Gibsona4da2e32007-12-18 15:06:42 +1100599
Rob Herring6f05afc2017-01-04 10:45:20 -0600600 for_each_property(node, prop) {
601 struct marker *m = prop->val.markers;
602 struct node *refnode;
603 cell_t phandle;
David Gibsona4da2e32007-12-18 15:06:42 +1100604
Rob Herring6f05afc2017-01-04 10:45:20 -0600605 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))
Rob Herring9130ba82018-02-27 17:40:38 -0600611 FAIL(c, dti, node, "Reference to non-existent node or "
Rob Herring6f05afc2017-01-04 10:45:20 -0600612 "label \"%s\"\n", m->ref);
613 else /* mark the entry as unresolved */
Rob Herring89d12312017-03-21 09:01:08 -0500614 *((fdt32_t *)(prop->val.val + m->offset)) =
Rob Herring6f05afc2017-01-04 10:45:20 -0600615 cpu_to_fdt32(0xffffffff);
616 continue;
617 }
618
619 phandle = get_node_phandle(dt, refnode);
Rob Herring89d12312017-03-21 09:01:08 -0500620 *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
Rob Herring50aafd62018-05-08 13:07:49 -0500621
622 reference_node(refnode);
David Gibsoned95d742008-08-07 12:24:17 +1000623 }
David Gibsoned95d742008-08-07 12:24:17 +1000624 }
David Gibsona4da2e32007-12-18 15:06:42 +1100625}
Rob Herring6f05afc2017-01-04 10:45:20 -0600626ERROR(phandle_references, fixup_phandle_references, NULL,
David Gibsona4da2e32007-12-18 15:06:42 +1100627 &duplicate_node_names, &explicit_phandles);
628
Rob Herring6f05afc2017-01-04 10:45:20 -0600629static void fixup_path_references(struct check *c, struct dt_info *dti,
630 struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +1100631{
Rob Herring6f05afc2017-01-04 10:45:20 -0600632 struct node *dt = dti->dt;
633 struct property *prop;
David Gibsona4da2e32007-12-18 15:06:42 +1100634
Rob Herring6f05afc2017-01-04 10:45:20 -0600635 for_each_property(node, prop) {
636 struct marker *m = prop->val.markers;
637 struct node *refnode;
638 char *path;
David Gibsona4da2e32007-12-18 15:06:42 +1100639
Rob Herring6f05afc2017-01-04 10:45:20 -0600640 for_each_marker_of_type(m, REF_PATH) {
641 assert(m->offset <= prop->val.len);
642
643 refnode = get_node_by_ref(dt, m->ref);
644 if (!refnode) {
Rob Herring9130ba82018-02-27 17:40:38 -0600645 FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
Rob Herring6f05afc2017-01-04 10:45:20 -0600646 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);
Rob Herring50aafd62018-05-08 13:07:49 -0500653
654 reference_node(refnode);
David Gibsona4da2e32007-12-18 15:06:42 +1100655 }
David Gibsona4da2e32007-12-18 15:06:42 +1100656 }
657}
Rob Herring6f05afc2017-01-04 10:45:20 -0600658ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
David Gibsona4da2e32007-12-18 15:06:42 +1100659
Rob Herring50aafd62018-05-08 13:07:49 -0500660static 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
David Gibsona4da2e32007-12-18 15:06:42 +1100668/*
669 * Semantic checks
670 */
Stephen Warrencd296722012-09-28 21:25:59 +0000671WARNING_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");
David Gibsona4da2e32007-12-18 15:06:42 +1100674
Stephen Warrencd296722012-09-28 21:25:59 +0000675WARNING_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");
Rob Herring9130ba82018-02-27 17:40:38 -0600678WARNING_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);
David Gibsona4da2e32007-12-18 15:06:42 +1100717
Rob Herring6f05afc2017-01-04 10:45:20 -0600718static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100719 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}
Rob Herring6f05afc2017-01-04 10:45:20 -0600734WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
Stephen Warrencd296722012-09-28 21:25:59 +0000735 &address_cells_is_cell, &size_cells_is_cell);
David Gibsona4da2e32007-12-18 15:06:42 +1100736
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
Rob Herring6f05afc2017-01-04 10:45:20 -0600742static void check_reg_format(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100743 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) {
Rob Herring9130ba82018-02-27 17:40:38 -0600753 FAIL(c, dti, node, "Root node has a \"reg\" property");
David Gibsona4da2e32007-12-18 15:06:42 +1100754 return;
755 }
756
757 if (prop->val.len == 0)
Rob Herring9130ba82018-02-27 17:40:38 -0600758 FAIL_PROP(c, dti, node, prop, "property is empty");
David Gibsona4da2e32007-12-18 15:06:42 +1100759
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
Rob Herring91feabc2016-01-26 09:04:11 -0600764 if (!entrylen || (prop->val.len % entrylen) != 0)
Rob Herring9130ba82018-02-27 17:40:38 -0600765 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);
David Gibsona4da2e32007-12-18 15:06:42 +1100768}
Rob Herring6f05afc2017-01-04 10:45:20 -0600769WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100770
Rob Herring6f05afc2017-01-04 10:45:20 -0600771static void check_ranges_format(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +1100772 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) {
Rob Herring9130ba82018-02-27 17:40:38 -0600782 FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
David Gibsona4da2e32007-12-18 15:06:42 +1100783 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)
Rob Herring9130ba82018-02-27 17:40:38 -0600794 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);
David Gibsona4da2e32007-12-18 15:06:42 +1100798 if (p_size_cells != c_size_cells)
Rob Herring9130ba82018-02-27 17:40:38 -0600799 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);
David Gibsona4da2e32007-12-18 15:06:42 +1100803 } else if ((prop->val.len % entrylen) != 0) {
Rob Herring9130ba82018-02-27 17:40:38 -0600804 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);
David Gibsona4da2e32007-12-18 15:06:42 +1100808 }
809}
Rob Herring6f05afc2017-01-04 10:45:20 -0600810WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +1100811
Rob Herring89d12312017-03-21 09:01:08 -0500812static 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
Rob Herring9130ba82018-02-27 17:40:38 -0600827 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\"");
Rob Herring89d12312017-03-21 09:01:08 -0500830
831 prop = get_property(node, "ranges");
832 if (!prop)
Rob Herring9130ba82018-02-27 17:40:38 -0600833 FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
Rob Herring89d12312017-03-21 09:01:08 -0500834
835 if (node_addr_cells(node) != 3)
Rob Herring9130ba82018-02-27 17:40:38 -0600836 FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
Rob Herring89d12312017-03-21 09:01:08 -0500837 if (node_size_cells(node) != 2)
Rob Herring9130ba82018-02-27 17:40:38 -0600838 FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
Rob Herring89d12312017-03-21 09:01:08 -0500839
840 prop = get_property(node, "bus-range");
Rob Herring970f04c2018-04-20 08:08:23 -0500841 if (!prop)
Rob Herring89d12312017-03-21 09:01:08 -0500842 return;
Rob Herring970f04c2018-04-20 08:08:23 -0500843
Rob Herring89d12312017-03-21 09:01:08 -0500844 if (prop->val.len != (sizeof(cell_t) * 2)) {
Rob Herring9130ba82018-02-27 17:40:38 -0600845 FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
Rob Herring89d12312017-03-21 09:01:08 -0500846 return;
847 }
848 cells = (cell_t *)prop->val.val;
849 if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
Rob Herring9130ba82018-02-27 17:40:38 -0600850 FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
Rob Herring89d12312017-03-21 09:01:08 -0500851 if (fdt32_to_cpu(cells[1]) > 0xff)
Rob Herring9130ba82018-02-27 17:40:38 -0600852 FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
Rob Herring89d12312017-03-21 09:01:08 -0500853}
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))
Rob Herring9130ba82018-02-27 17:40:38 -0600882 FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
883 bus_num, min_bus, max_bus);
Rob Herring89d12312017-03-21 09:01:08 -0500884}
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) {
Rob Herring9130ba82018-02-27 17:40:38 -0600900 FAIL(c, dti, node, "missing PCI reg property");
Rob Herring89d12312017-03-21 09:01:08 -0500901 return;
902 }
903
904 cells = (cell_t *)prop->val.val;
905 if (cells[1] || cells[2])
Rob Herring9130ba82018-02-27 17:40:38 -0600906 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 -0500907
908 reg = fdt32_to_cpu(cells[0]);
909 dev = (reg & 0xf800) >> 11;
910 func = (reg & 0x700) >> 8;
911
912 if (reg & 0xff000000)
Rob Herring9130ba82018-02-27 17:40:38 -0600913 FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
Rob Herring89d12312017-03-21 09:01:08 -0500914 if (reg & 0x000000ff)
Rob Herring9130ba82018-02-27 17:40:38 -0600915 FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
Rob Herring89d12312017-03-21 09:01:08 -0500916
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
Rob Herring9130ba82018-02-27 17:40:38 -0600927 FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
928 unit_addr);
Rob Herring89d12312017-03-21 09:01:08 -0500929}
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) {
Rob Herringc2e70752018-11-28 18:37:35 -0600947 if (streq(str, compat))
Rob Herring89d12312017-03-21 09:01:08 -0500948 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}
Rob Herringc2e70752018-11-28 18:37:35 -0600958WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL,
959 &addr_size_cells, &compatible_is_string_list);
Rob Herring89d12312017-03-21 09:01:08 -0500960
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))
Rob Herring9130ba82018-02-27 17:40:38 -0600985 FAIL(c, dti, node, "missing or empty reg/ranges property");
Rob Herring89d12312017-03-21 09:01:08 -0500986 return;
987 }
988
989 size = node_addr_cells(node->parent);
990 while (size--)
991 reg = (reg << 32) | fdt32_to_cpu(*(cells++));
992
Rob Herring4201d052017-10-03 11:37:04 -0500993 snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
Rob Herring89d12312017-03-21 09:01:08 -0500994 if (!streq(unitname, unit_addr))
Rob Herring9130ba82018-02-27 17:40:38 -0600995 FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
996 unit_addr);
Rob Herring89d12312017-03-21 09:01:08 -0500997}
998WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
999
Rob Herringf8589272018-09-13 08:59:25 -05001000static 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{
Rob Herringc2e70752018-11-28 18:37:35 -06001073 int spi_addr_cells = 1;
Rob Herringf8589272018-09-13 08:59:25 -05001074
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
Rob Herringc2e70752018-11-28 18:37:35 -06001102 if (get_property(node, "spi-slave"))
1103 spi_addr_cells = 0;
1104 if (node_addr_cells(node) != spi_addr_cells)
Rob Herringf8589272018-09-13 08:59:25 -05001105 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
Rob Herringc2e70752018-11-28 18:37:35 -06001123 if (get_property(node->parent, "spi-slave"))
1124 return;
1125
Rob Herringf8589272018-09-13 08:59:25 -05001126 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
Rob Herring89d12312017-03-21 09:01:08 -05001143static 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)) {
Rob Herring9130ba82018-02-27 17:40:38 -06001155 FAIL(c, dti, node, "unit name should not have leading \"0x\"");
Rob Herring89d12312017-03-21 09:01:08 -05001156 /* skip over 0x for next test */
1157 unitname += 2;
1158 }
1159 if (unitname[0] == '0' && isxdigit(unitname[1]))
Rob Herring9130ba82018-02-27 17:40:38 -06001160 FAIL(c, dti, node, "unit name should not have leading 0s");
Rob Herring89d12312017-03-21 09:01:08 -05001161}
1162WARNING(unit_address_format, check_unit_address_format, NULL,
1163 &node_name_format, &pci_bridge, &simple_bus_bridge);
1164
David Gibsona4da2e32007-12-18 15:06:42 +11001165/*
1166 * Style checks
1167 */
Rob Herring6f05afc2017-01-04 10:45:20 -06001168static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
David Gibsona4da2e32007-12-18 15:06:42 +11001169 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
Rob Herring47605972015-04-29 16:00:05 -05001182 if (node->parent->addr_cells == -1)
Rob Herring9130ba82018-02-27 17:40:38 -06001183 FAIL(c, dti, node, "Relying on default #address-cells value");
David Gibsona4da2e32007-12-18 15:06:42 +11001184
Rob Herring47605972015-04-29 16:00:05 -05001185 if (node->parent->size_cells == -1)
Rob Herring9130ba82018-02-27 17:40:38 -06001186 FAIL(c, dti, node, "Relying on default #size-cells value");
David Gibsona4da2e32007-12-18 15:06:42 +11001187}
Rob Herring6f05afc2017-01-04 10:45:20 -06001188WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1189 &addr_size_cells);
David Gibsona4da2e32007-12-18 15:06:42 +11001190
Rob Herring9130ba82018-02-27 17:40:38 -06001191static 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
Rob Herring50aafd62018-05-08 13:07:49 -05001215static 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
David Gibsona4da2e32007-12-18 15:06:42 +11001245static void check_obsolete_chosen_interrupt_controller(struct check *c,
Rob Herring6f05afc2017-01-04 10:45:20 -06001246 struct dt_info *dti,
1247 struct node *node)
David Gibsona4da2e32007-12-18 15:06:42 +11001248{
Rob Herring6f05afc2017-01-04 10:45:20 -06001249 struct node *dt = dti->dt;
David Gibsona4da2e32007-12-18 15:06:42 +11001250 struct node *chosen;
1251 struct property *prop;
1252
Rob Herring6f05afc2017-01-04 10:45:20 -06001253 if (node != dt)
1254 return;
1255
1256
David Gibsona4da2e32007-12-18 15:06:42 +11001257 chosen = get_node_by_path(dt, "/chosen");
1258 if (!chosen)
1259 return;
1260
1261 prop = get_property(chosen, "interrupt-controller");
1262 if (prop)
Rob Herring9130ba82018-02-27 17:40:38 -06001263 FAIL_PROP(c, dti, node, prop,
1264 "/chosen has obsolete \"interrupt-controller\" property");
David Gibsona4da2e32007-12-18 15:06:42 +11001265}
Rob Herring6f05afc2017-01-04 10:45:20 -06001266WARNING(obsolete_chosen_interrupt_controller,
1267 check_obsolete_chosen_interrupt_controller, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +11001268
Rob Herring9130ba82018-02-27 17:40:38 -06001269static 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
Rob Herring4201d052017-10-03 11:37:04 -05001318struct 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)) {
Rob Herring9130ba82018-02-27 17:40:38 -06001334 FAIL_PROP(c, dti, node, prop,
1335 "property size (%d) is invalid, expected multiple of %zu",
1336 prop->val.len, sizeof(cell_t));
Rob Herring4201d052017-10-03 11:37:04 -05001337 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) {
Rob Herringe45fe7f2017-10-25 10:59:13 -05001351 /* Give up if this is an overlay with external references */
1352 if (dti->dtsflags & DTSF_PLUGIN)
1353 break;
1354
Rob Herring4201d052017-10-03 11:37:04 -05001355 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)
Rob Herring9130ba82018-02-27 17:40:38 -06001367 FAIL_PROP(c, dti, node, prop,
1368 "cell %d is not a phandle reference",
1369 cell);
Rob Herring4201d052017-10-03 11:37:04 -05001370 }
1371
1372 provider_node = get_node_by_phandle(root, phandle);
1373 if (!provider_node) {
Rob Herring9130ba82018-02-27 17:40:38 -06001374 FAIL_PROP(c, dti, node, prop,
1375 "Could not get phandle node for (cell %d)",
1376 cell);
Rob Herring4201d052017-10-03 11:37:04 -05001377 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 {
Rob Herring9130ba82018-02-27 17:40:38 -06001386 FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
Rob Herring4201d052017-10-03 11:37:04 -05001387 provider->cell_name,
1388 provider_node->fullpath,
Rob Herring9130ba82018-02-27 17:40:38 -06001389 prop->name, cell);
Rob Herring4201d052017-10-03 11:37:04 -05001390 break;
1391 }
1392
1393 if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
Rob Herring9130ba82018-02-27 17:40:38 -06001394 FAIL_PROP(c, dti, node, prop,
1395 "property size (%d) too small for cell size %d",
1396 prop->val.len, cellsize);
Rob Herring4201d052017-10-03 11:37:04 -05001397 }
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");
Rob Herring9130ba82018-02-27 17:40:38 -06001432WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
Rob Herring4201d052017-10-03 11:37:04 -05001433WARNING_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
Rob Herring9130ba82018-02-27 17:40:38 -06001498 FAIL_PROP(c, dti, node, prop,
1499 "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
Rob Herring4201d052017-10-03 11:37:04 -05001500 }
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))
Rob Herring9130ba82018-02-27 17:40:38 -06001533 FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1534 irq_prop->val.len, sizeof(cell_t));
Rob Herring4201d052017-10-03 11:37:04 -05001535
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);
Rob Herringe45fe7f2017-10-25 10:59:13 -05001545 /* Give up if this is an overlay with external references */
1546 if ((phandle == 0 || phandle == -1) &&
1547 (dti->dtsflags & DTSF_PLUGIN))
1548 return;
1549
Rob Herring4201d052017-10-03 11:37:04 -05001550 irq_node = get_node_by_phandle(root, phandle);
1551 if (!irq_node) {
Rob Herring9130ba82018-02-27 17:40:38 -06001552 FAIL_PROP(c, dti, parent, prop, "Bad phandle");
Rob Herring4201d052017-10-03 11:37:04 -05001553 return;
1554 }
1555 if (!node_is_interrupt_provider(irq_node))
Rob Herring9130ba82018-02-27 17:40:38 -06001556 FAIL(c, dti, irq_node,
1557 "Missing interrupt-controller or interrupt-map property");
Rob Herring4201d052017-10-03 11:37:04 -05001558
1559 break;
1560 }
1561
1562 parent = parent->parent;
1563 }
1564
1565 if (!irq_node) {
Rob Herring9130ba82018-02-27 17:40:38 -06001566 FAIL(c, dti, node, "Missing interrupt-parent");
Rob Herring4201d052017-10-03 11:37:04 -05001567 return;
1568 }
1569
1570 prop = get_property(irq_node, "#interrupt-cells");
1571 if (!prop) {
Rob Herring9130ba82018-02-27 17:40:38 -06001572 FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
Rob Herring4201d052017-10-03 11:37:04 -05001573 return;
1574 }
1575
1576 irq_cells = propval_cell(prop);
1577 if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
Rob Herring9130ba82018-02-27 17:40:38 -06001578 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)));
Rob Herring4201d052017-10-03 11:37:04 -05001581 }
1582}
1583WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1584
Rob Herring50aafd62018-05-08 13:07:49 -05001585static 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
David Gibsona4da2e32007-12-18 15:06:42 +11001731static struct check *check_table[] = {
1732 &duplicate_node_names, &duplicate_property_names,
David Gibsoned95d742008-08-07 12:24:17 +10001733 &node_name_chars, &node_name_format, &property_name_chars,
David Gibsona4da2e32007-12-18 15:06:42 +11001734 &name_is_string, &name_properties,
John Bonesio658f29a2010-11-17 15:28:20 -08001735
1736 &duplicate_label,
1737
David Gibsona4da2e32007-12-18 15:06:42 +11001738 &explicit_phandles,
1739 &phandle_references, &path_references,
Rob Herring50aafd62018-05-08 13:07:49 -05001740 &omit_unused_nodes,
David Gibsona4da2e32007-12-18 15:06:42 +11001741
1742 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
1743 &device_type_is_string, &model_is_string, &status_is_string,
Rob Herring9130ba82018-02-27 17:40:38 -06001744 &label_is_string,
1745
1746 &compatible_is_string_list, &names_is_string_list,
David Gibsona4da2e32007-12-18 15:06:42 +11001747
Rob Herring89d12312017-03-21 09:01:08 -05001748 &property_name_chars_strict,
1749 &node_name_chars_strict,
1750
David Gibsona4da2e32007-12-18 15:06:42 +11001751 &addr_size_cells, &reg_format, &ranges_format,
1752
Rob Herringb9937342016-03-04 08:56:58 -06001753 &unit_address_vs_reg,
Rob Herring89d12312017-03-21 09:01:08 -05001754 &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,
Rob Herringb9937342016-03-04 08:56:58 -06001762
Rob Herringf8589272018-09-13 08:59:25 -05001763 &i2c_bus_bridge,
1764 &i2c_bus_reg,
1765
1766 &spi_bus_bridge,
1767 &spi_bus_reg,
1768
David Gibsona4da2e32007-12-18 15:06:42 +11001769 &avoid_default_addr_size,
Rob Herring9130ba82018-02-27 17:40:38 -06001770 &avoid_unnecessary_addr_size,
Rob Herring50aafd62018-05-08 13:07:49 -05001771 &unique_unit_address,
David Gibsona4da2e32007-12-18 15:06:42 +11001772 &obsolete_chosen_interrupt_controller,
Rob Herring9130ba82018-02-27 17:40:38 -06001773 &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
Stephen Warrencd296722012-09-28 21:25:59 +00001774
Rob Herring4201d052017-10-03 11:37:04 -05001775 &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,
Rob Herring9130ba82018-02-27 17:40:38 -06001789 &sound_dai_property,
Rob Herring4201d052017-10-03 11:37:04 -05001790 &thermal_sensors_property,
1791
1792 &deprecated_gpio_property,
1793 &gpios_property,
1794 &interrupts_property,
1795
Rob Herring9130ba82018-02-27 17:40:38 -06001796 &alias_paths,
1797
Rob Herring50aafd62018-05-08 13:07:49 -05001798 &graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
1799
Stephen Warrencd296722012-09-28 21:25:59 +00001800 &always_fail,
David Gibsona4da2e32007-12-18 15:06:42 +11001801};
1802
Stephen Warrencd296722012-09-28 21:25:59 +00001803static 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
Rob Herring47605972015-04-29 16:00:05 -05001837void parse_checks_option(bool warn, bool error, const char *arg)
Stephen Warrencd296722012-09-28 21:25:59 +00001838{
1839 int i;
Rob Herring47605972015-04-29 16:00:05 -05001840 const char *name = arg;
Stephen Warrencd296722012-09-28 21:25:59 +00001841 bool enable = true;
1842
Rob Herring47605972015-04-29 16:00:05 -05001843 if ((strncmp(arg, "no-", 3) == 0)
1844 || (strncmp(arg, "no_", 3) == 0)) {
1845 name = arg + 3;
Stephen Warrencd296722012-09-28 21:25:59 +00001846 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
Rob Herring6f05afc2017-01-04 10:45:20 -06001864void process_checks(bool force, struct dt_info *dti)
David Gibsona4da2e32007-12-18 15:06:42 +11001865{
David Gibsona4da2e32007-12-18 15:06:42 +11001866 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
Stephen Warrencd296722012-09-28 21:25:59 +00001872 if (c->warn || c->error)
Rob Herring6f05afc2017-01-04 10:45:20 -06001873 error = error || run_check(c, dti);
David Gibsona4da2e32007-12-18 15:06:42 +11001874 }
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 }
David Gibsona4da2e32007-12-18 15:06:42 +11001886}