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