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