checks.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. #include "dtc.h"
  21. #ifdef TRACE_CHECKS
  22. #define TRACE(c, ...) \
  23. do { \
  24. fprintf(stderr, "=== %s: ", (c)->name); \
  25. fprintf(stderr, __VA_ARGS__); \
  26. fprintf(stderr, "\n"); \
  27. } while (0)
  28. #else
  29. #define TRACE(c, fmt, ...) do { } while (0)
  30. #endif
  31. enum checkstatus {
  32. UNCHECKED = 0,
  33. PREREQ,
  34. PASSED,
  35. FAILED,
  36. };
  37. struct check;
  38. typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
  39. struct check {
  40. const char *name;
  41. check_fn fn;
  42. void *data;
  43. bool warn, error;
  44. enum checkstatus status;
  45. bool inprogress;
  46. int num_prereqs;
  47. struct check **prereq;
  48. };
  49. #define CHECK_ENTRY(nm_, fn_, d_, w_, e_, ...) \
  50. static struct check *nm_##_prereqs[] = { __VA_ARGS__ }; \
  51. static struct check nm_ = { \
  52. .name = #nm_, \
  53. .fn = (fn_), \
  54. .data = (d_), \
  55. .warn = (w_), \
  56. .error = (e_), \
  57. .status = UNCHECKED, \
  58. .num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
  59. .prereq = nm_##_prereqs, \
  60. };
  61. #define WARNING(nm_, fn_, d_, ...) \
  62. CHECK_ENTRY(nm_, fn_, d_, true, false, __VA_ARGS__)
  63. #define ERROR(nm_, fn_, d_, ...) \
  64. CHECK_ENTRY(nm_, fn_, d_, false, true, __VA_ARGS__)
  65. #define CHECK(nm_, fn_, d_, ...) \
  66. CHECK_ENTRY(nm_, fn_, d_, false, false, __VA_ARGS__)
  67. static inline void PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
  68. struct node *node,
  69. struct property *prop,
  70. const char *fmt, ...)
  71. {
  72. va_list ap;
  73. va_start(ap, fmt);
  74. if ((c->warn && (quiet < 1))
  75. || (c->error && (quiet < 2))) {
  76. fprintf(stderr, "%s: %s (%s): ",
  77. strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
  78. (c->error) ? "ERROR" : "Warning", c->name);
  79. if (node) {
  80. fprintf(stderr, "%s", node->fullpath);
  81. if (prop)
  82. fprintf(stderr, ":%s", prop->name);
  83. fputs(": ", stderr);
  84. }
  85. vfprintf(stderr, fmt, ap);
  86. fprintf(stderr, "\n");
  87. }
  88. va_end(ap);
  89. }
  90. #define FAIL(c, dti, node, ...) \
  91. do { \
  92. TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
  93. (c)->status = FAILED; \
  94. check_msg((c), dti, node, NULL, __VA_ARGS__); \
  95. } while (0)
  96. #define FAIL_PROP(c, dti, node, prop, ...) \
  97. do { \
  98. TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
  99. (c)->status = FAILED; \
  100. check_msg((c), dti, node, prop, __VA_ARGS__); \
  101. } while (0)
  102. static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
  103. {
  104. struct node *child;
  105. TRACE(c, "%s", node->fullpath);
  106. if (c->fn)
  107. c->fn(c, dti, node);
  108. for_each_child(node, child)
  109. check_nodes_props(c, dti, child);
  110. }
  111. static bool run_check(struct check *c, struct dt_info *dti)
  112. {
  113. struct node *dt = dti->dt;
  114. bool error = false;
  115. int i;
  116. assert(!c->inprogress);
  117. if (c->status != UNCHECKED)
  118. goto out;
  119. c->inprogress = true;
  120. for (i = 0; i < c->num_prereqs; i++) {
  121. struct check *prq = c->prereq[i];
  122. error = error || run_check(prq, dti);
  123. if (prq->status != PASSED) {
  124. c->status = PREREQ;
  125. check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
  126. c->prereq[i]->name);
  127. }
  128. }
  129. if (c->status != UNCHECKED)
  130. goto out;
  131. check_nodes_props(c, dti, dt);
  132. if (c->status == UNCHECKED)
  133. c->status = PASSED;
  134. TRACE(c, "\tCompleted, status %d", c->status);
  135. out:
  136. c->inprogress = false;
  137. if ((c->status != PASSED) && (c->error))
  138. error = true;
  139. return error;
  140. }
  141. /*
  142. * Utility check functions
  143. */
  144. /* A check which always fails, for testing purposes only */
  145. static inline void check_always_fail(struct check *c, struct dt_info *dti,
  146. struct node *node)
  147. {
  148. FAIL(c, dti, node, "always_fail check");
  149. }
  150. CHECK(always_fail, check_always_fail, NULL);
  151. static void check_is_string(struct check *c, struct dt_info *dti,
  152. struct node *node)
  153. {
  154. struct property *prop;
  155. char *propname = c->data;
  156. prop = get_property(node, propname);
  157. if (!prop)
  158. return; /* Not present, assumed ok */
  159. if (!data_is_one_string(prop->val))
  160. FAIL_PROP(c, dti, node, prop, "property is not a string");
  161. }
  162. #define WARNING_IF_NOT_STRING(nm, propname) \
  163. WARNING(nm, check_is_string, (propname))
  164. #define ERROR_IF_NOT_STRING(nm, propname) \
  165. ERROR(nm, check_is_string, (propname))
  166. static void check_is_string_list(struct check *c, struct dt_info *dti,
  167. struct node *node)
  168. {
  169. int rem, l;
  170. struct property *prop;
  171. char *propname = c->data;
  172. char *str;
  173. prop = get_property(node, propname);
  174. if (!prop)
  175. return; /* Not present, assumed ok */
  176. str = prop->val.val;
  177. rem = prop->val.len;
  178. while (rem > 0) {
  179. l = strnlen(str, rem);
  180. if (l == rem) {
  181. FAIL_PROP(c, dti, node, prop, "property is not a string list");
  182. break;
  183. }
  184. rem -= l + 1;
  185. str += l + 1;
  186. }
  187. }
  188. #define WARNING_IF_NOT_STRING_LIST(nm, propname) \
  189. WARNING(nm, check_is_string_list, (propname))
  190. #define ERROR_IF_NOT_STRING_LIST(nm, propname) \
  191. ERROR(nm, check_is_string_list, (propname))
  192. static void check_is_cell(struct check *c, struct dt_info *dti,
  193. struct node *node)
  194. {
  195. struct property *prop;
  196. char *propname = c->data;
  197. prop = get_property(node, propname);
  198. if (!prop)
  199. return; /* Not present, assumed ok */
  200. if (prop->val.len != sizeof(cell_t))
  201. FAIL_PROP(c, dti, node, prop, "property is not a single cell");
  202. }
  203. #define WARNING_IF_NOT_CELL(nm, propname) \
  204. WARNING(nm, check_is_cell, (propname))
  205. #define ERROR_IF_NOT_CELL(nm, propname) \
  206. ERROR(nm, check_is_cell, (propname))
  207. /*
  208. * Structural check functions
  209. */
  210. static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
  211. struct node *node)
  212. {
  213. struct node *child, *child2;
  214. for_each_child(node, child)
  215. for (child2 = child->next_sibling;
  216. child2;
  217. child2 = child2->next_sibling)
  218. if (streq(child->name, child2->name))
  219. FAIL(c, dti, child2, "Duplicate node name");
  220. }
  221. ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
  222. static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
  223. struct node *node)
  224. {
  225. struct property *prop, *prop2;
  226. for_each_property(node, prop) {
  227. for (prop2 = prop->next; prop2; prop2 = prop2->next) {
  228. if (prop2->deleted)
  229. continue;
  230. if (streq(prop->name, prop2->name))
  231. FAIL_PROP(c, dti, node, prop, "Duplicate property name");
  232. }
  233. }
  234. }
  235. ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
  236. #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
  237. #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  238. #define DIGITS "0123456789"
  239. #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
  240. #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
  241. static void check_node_name_chars(struct check *c, struct dt_info *dti,
  242. struct node *node)
  243. {
  244. int n = strspn(node->name, c->data);
  245. if (n < strlen(node->name))
  246. FAIL(c, dti, node, "Bad character '%c' in node name",
  247. node->name[n]);
  248. }
  249. ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
  250. static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
  251. struct node *node)
  252. {
  253. int n = strspn(node->name, c->data);
  254. if (n < node->basenamelen)
  255. FAIL(c, dti, node, "Character '%c' not recommended in node name",
  256. node->name[n]);
  257. }
  258. CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
  259. static void check_node_name_format(struct check *c, struct dt_info *dti,
  260. struct node *node)
  261. {
  262. if (strchr(get_unitname(node), '@'))
  263. FAIL(c, dti, node, "multiple '@' characters in node name");
  264. }
  265. ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
  266. static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
  267. struct node *node)
  268. {
  269. const char *unitname = get_unitname(node);
  270. struct property *prop = get_property(node, "reg");
  271. if (get_subnode(node, "__overlay__")) {
  272. /* HACK: Overlay fragments are a special case */
  273. return;
  274. }
  275. if (!prop) {
  276. prop = get_property(node, "ranges");
  277. if (prop && !prop->val.len)
  278. prop = NULL;
  279. }
  280. if (prop) {
  281. if (!unitname[0])
  282. FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
  283. } else {
  284. if (unitname[0])
  285. FAIL(c, dti, node, "node has a unit name, but no reg property");
  286. }
  287. }
  288. WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
  289. static void check_property_name_chars(struct check *c, struct dt_info *dti,
  290. struct node *node)
  291. {
  292. struct property *prop;
  293. for_each_property(node, prop) {
  294. int n = strspn(prop->name, c->data);
  295. if (n < strlen(prop->name))
  296. FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
  297. prop->name[n]);
  298. }
  299. }
  300. ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
  301. static void check_property_name_chars_strict(struct check *c,
  302. struct dt_info *dti,
  303. struct node *node)
  304. {
  305. struct property *prop;
  306. for_each_property(node, prop) {
  307. const char *name = prop->name;
  308. int n = strspn(name, c->data);
  309. if (n == strlen(prop->name))
  310. continue;
  311. /* Certain names are whitelisted */
  312. if (streq(name, "device_type"))
  313. continue;
  314. /*
  315. * # is only allowed at the beginning of property names not counting
  316. * the vendor prefix.
  317. */
  318. if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
  319. name += n + 1;
  320. n = strspn(name, c->data);
  321. }
  322. if (n < strlen(name))
  323. FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
  324. name[n]);
  325. }
  326. }
  327. CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
  328. #define DESCLABEL_FMT "%s%s%s%s%s"
  329. #define DESCLABEL_ARGS(node,prop,mark) \
  330. ((mark) ? "value of " : ""), \
  331. ((prop) ? "'" : ""), \
  332. ((prop) ? (prop)->name : ""), \
  333. ((prop) ? "' in " : ""), (node)->fullpath
  334. static void check_duplicate_label(struct check *c, struct dt_info *dti,
  335. const char *label, struct node *node,
  336. struct property *prop, struct marker *mark)
  337. {
  338. struct node *dt = dti->dt;
  339. struct node *othernode = NULL;
  340. struct property *otherprop = NULL;
  341. struct marker *othermark = NULL;
  342. othernode = get_node_by_label(dt, label);
  343. if (!othernode)
  344. otherprop = get_property_by_label(dt, label, &othernode);
  345. if (!othernode)
  346. othermark = get_marker_label(dt, label, &othernode,
  347. &otherprop);
  348. if (!othernode)
  349. return;
  350. if ((othernode != node) || (otherprop != prop) || (othermark != mark))
  351. FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
  352. " and " DESCLABEL_FMT,
  353. label, DESCLABEL_ARGS(node, prop, mark),
  354. DESCLABEL_ARGS(othernode, otherprop, othermark));
  355. }
  356. static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
  357. struct node *node)
  358. {
  359. struct label *l;
  360. struct property *prop;
  361. for_each_label(node->labels, l)
  362. check_duplicate_label(c, dti, l->label, node, NULL, NULL);
  363. for_each_property(node, prop) {
  364. struct marker *m = prop->val.markers;
  365. for_each_label(prop->labels, l)
  366. check_duplicate_label(c, dti, l->label, node, prop, NULL);
  367. for_each_marker_of_type(m, LABEL)
  368. check_duplicate_label(c, dti, m->ref, node, prop, m);
  369. }
  370. }
  371. ERROR(duplicate_label, check_duplicate_label_node, NULL);
  372. static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
  373. struct node *node, const char *propname)
  374. {
  375. struct node *root = dti->dt;
  376. struct property *prop;
  377. struct marker *m;
  378. cell_t phandle;
  379. prop = get_property(node, propname);
  380. if (!prop)
  381. return 0;
  382. if (prop->val.len != sizeof(cell_t)) {
  383. FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
  384. prop->val.len, prop->name);
  385. return 0;
  386. }
  387. m = prop->val.markers;
  388. for_each_marker_of_type(m, REF_PHANDLE) {
  389. assert(m->offset == 0);
  390. if (node != get_node_by_ref(root, m->ref))
  391. /* "Set this node's phandle equal to some
  392. * other node's phandle". That's nonsensical
  393. * by construction. */ {
  394. FAIL(c, dti, node, "%s is a reference to another node",
  395. prop->name);
  396. }
  397. /* But setting this node's phandle equal to its own
  398. * phandle is allowed - that means allocate a unique
  399. * phandle for this node, even if it's not otherwise
  400. * referenced. The value will be filled in later, so
  401. * we treat it as having no phandle data for now. */
  402. return 0;
  403. }
  404. phandle = propval_cell(prop);
  405. if ((phandle == 0) || (phandle == -1)) {
  406. FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
  407. phandle, prop->name);
  408. return 0;
  409. }
  410. return phandle;
  411. }
  412. static void check_explicit_phandles(struct check *c, struct dt_info *dti,
  413. struct node *node)
  414. {
  415. struct node *root = dti->dt;
  416. struct node *other;
  417. cell_t phandle, linux_phandle;
  418. /* Nothing should have assigned phandles yet */
  419. assert(!node->phandle);
  420. phandle = check_phandle_prop(c, dti, node, "phandle");
  421. linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
  422. if (!phandle && !linux_phandle)
  423. /* No valid phandles; nothing further to check */
  424. return;
  425. if (linux_phandle && phandle && (phandle != linux_phandle))
  426. FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
  427. " properties");
  428. if (linux_phandle && !phandle)
  429. phandle = linux_phandle;
  430. other = get_node_by_phandle(root, phandle);
  431. if (other && (other != node)) {
  432. FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
  433. phandle, other->fullpath);
  434. return;
  435. }
  436. node->phandle = phandle;
  437. }
  438. ERROR(explicit_phandles, check_explicit_phandles, NULL);
  439. static void check_name_properties(struct check *c, struct dt_info *dti,
  440. struct node *node)
  441. {
  442. struct property **pp, *prop = NULL;
  443. for (pp = &node->proplist; *pp; pp = &((*pp)->next))
  444. if (streq((*pp)->name, "name")) {
  445. prop = *pp;
  446. break;
  447. }
  448. if (!prop)
  449. return; /* No name property, that's fine */
  450. if ((prop->val.len != node->basenamelen+1)
  451. || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
  452. FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
  453. " of base node name)", prop->val.val);
  454. } else {
  455. /* The name property is correct, and therefore redundant.
  456. * Delete it */
  457. *pp = prop->next;
  458. free(prop->name);
  459. data_free(prop->val);
  460. free(prop);
  461. }
  462. }
  463. ERROR_IF_NOT_STRING(name_is_string, "name");
  464. ERROR(name_properties, check_name_properties, NULL, &name_is_string);
  465. /*
  466. * Reference fixup functions
  467. */
  468. static void fixup_phandle_references(struct check *c, struct dt_info *dti,
  469. struct node *node)
  470. {
  471. struct node *dt = dti->dt;
  472. struct property *prop;
  473. for_each_property(node, prop) {
  474. struct marker *m = prop->val.markers;
  475. struct node *refnode;
  476. cell_t phandle;
  477. for_each_marker_of_type(m, REF_PHANDLE) {
  478. assert(m->offset + sizeof(cell_t) <= prop->val.len);
  479. refnode = get_node_by_ref(dt, m->ref);
  480. if (! refnode) {
  481. if (!(dti->dtsflags & DTSF_PLUGIN))
  482. FAIL(c, dti, node, "Reference to non-existent node or "
  483. "label \"%s\"\n", m->ref);
  484. else /* mark the entry as unresolved */
  485. *((fdt32_t *)(prop->val.val + m->offset)) =
  486. cpu_to_fdt32(0xffffffff);
  487. continue;
  488. }
  489. phandle = get_node_phandle(dt, refnode);
  490. *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
  491. reference_node(refnode);
  492. }
  493. }
  494. }
  495. ERROR(phandle_references, fixup_phandle_references, NULL,
  496. &duplicate_node_names, &explicit_phandles);
  497. static void fixup_path_references(struct check *c, struct dt_info *dti,
  498. struct node *node)
  499. {
  500. struct node *dt = dti->dt;
  501. struct property *prop;
  502. for_each_property(node, prop) {
  503. struct marker *m = prop->val.markers;
  504. struct node *refnode;
  505. char *path;
  506. for_each_marker_of_type(m, REF_PATH) {
  507. assert(m->offset <= prop->val.len);
  508. refnode = get_node_by_ref(dt, m->ref);
  509. if (!refnode) {
  510. FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
  511. m->ref);
  512. continue;
  513. }
  514. path = refnode->fullpath;
  515. prop->val = data_insert_at_marker(prop->val, m, path,
  516. strlen(path) + 1);
  517. reference_node(refnode);
  518. }
  519. }
  520. }
  521. ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
  522. static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
  523. struct node *node)
  524. {
  525. if (node->omit_if_unused && !node->is_referenced)
  526. delete_node(node);
  527. }
  528. ERROR(omit_unused_nodes, fixup_omit_unused_nodes, NULL, &phandle_references, &path_references);
  529. /*
  530. * Semantic checks
  531. */
  532. WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
  533. WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
  534. WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
  535. WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
  536. WARNING_IF_NOT_STRING(model_is_string, "model");
  537. WARNING_IF_NOT_STRING(status_is_string, "status");
  538. WARNING_IF_NOT_STRING(label_is_string, "label");
  539. WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
  540. static void check_names_is_string_list(struct check *c, struct dt_info *dti,
  541. struct node *node)
  542. {
  543. struct property *prop;
  544. for_each_property(node, prop) {
  545. const char *s = strrchr(prop->name, '-');
  546. if (!s || !streq(s, "-names"))
  547. continue;
  548. c->data = prop->name;
  549. check_is_string_list(c, dti, node);
  550. }
  551. }
  552. WARNING(names_is_string_list, check_names_is_string_list, NULL);
  553. static void check_alias_paths(struct check *c, struct dt_info *dti,
  554. struct node *node)
  555. {
  556. struct property *prop;
  557. if (!streq(node->name, "aliases"))
  558. return;
  559. for_each_property(node, prop) {
  560. if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
  561. FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
  562. prop->val.val);
  563. continue;
  564. }
  565. if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
  566. FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
  567. }
  568. }
  569. WARNING(alias_paths, check_alias_paths, NULL);
  570. static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
  571. struct node *node)
  572. {
  573. struct property *prop;
  574. node->addr_cells = -1;
  575. node->size_cells = -1;
  576. prop = get_property(node, "#address-cells");
  577. if (prop)
  578. node->addr_cells = propval_cell(prop);
  579. prop = get_property(node, "#size-cells");
  580. if (prop)
  581. node->size_cells = propval_cell(prop);
  582. }
  583. WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
  584. &address_cells_is_cell, &size_cells_is_cell);
  585. #define node_addr_cells(n) \
  586. (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
  587. #define node_size_cells(n) \
  588. (((n)->size_cells == -1) ? 1 : (n)->size_cells)
  589. static void check_reg_format(struct check *c, struct dt_info *dti,
  590. struct node *node)
  591. {
  592. struct property *prop;
  593. int addr_cells, size_cells, entrylen;
  594. prop = get_property(node, "reg");
  595. if (!prop)
  596. return; /* No "reg", that's fine */
  597. if (!node->parent) {
  598. FAIL(c, dti, node, "Root node has a \"reg\" property");
  599. return;
  600. }
  601. if (prop->val.len == 0)
  602. FAIL_PROP(c, dti, node, prop, "property is empty");
  603. addr_cells = node_addr_cells(node->parent);
  604. size_cells = node_size_cells(node->parent);
  605. entrylen = (addr_cells + size_cells) * sizeof(cell_t);
  606. if (!entrylen || (prop->val.len % entrylen) != 0)
  607. FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
  608. "(#address-cells == %d, #size-cells == %d)",
  609. prop->val.len, addr_cells, size_cells);
  610. }
  611. WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
  612. static void check_ranges_format(struct check *c, struct dt_info *dti,
  613. struct node *node)
  614. {
  615. struct property *prop;
  616. int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
  617. prop = get_property(node, "ranges");
  618. if (!prop)
  619. return;
  620. if (!node->parent) {
  621. FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
  622. return;
  623. }
  624. p_addr_cells = node_addr_cells(node->parent);
  625. p_size_cells = node_size_cells(node->parent);
  626. c_addr_cells = node_addr_cells(node);
  627. c_size_cells = node_size_cells(node);
  628. entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
  629. if (prop->val.len == 0) {
  630. if (p_addr_cells != c_addr_cells)
  631. FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
  632. "#address-cells (%d) differs from %s (%d)",
  633. c_addr_cells, node->parent->fullpath,
  634. p_addr_cells);
  635. if (p_size_cells != c_size_cells)
  636. FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
  637. "#size-cells (%d) differs from %s (%d)",
  638. c_size_cells, node->parent->fullpath,
  639. p_size_cells);
  640. } else if ((prop->val.len % entrylen) != 0) {
  641. FAIL_PROP(c, dti, node, prop, "\"ranges\" property has invalid length (%d bytes) "
  642. "(parent #address-cells == %d, child #address-cells == %d, "
  643. "#size-cells == %d)", prop->val.len,
  644. p_addr_cells, c_addr_cells, c_size_cells);
  645. }
  646. }
  647. WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
  648. static const struct bus_type pci_bus = {
  649. .name = "PCI",
  650. };
  651. static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
  652. {
  653. struct property *prop;
  654. cell_t *cells;
  655. prop = get_property(node, "device_type");
  656. if (!prop || !streq(prop->val.val, "pci"))
  657. return;
  658. node->bus = &pci_bus;
  659. if (!strprefixeq(node->name, node->basenamelen, "pci") &&
  660. !strprefixeq(node->name, node->basenamelen, "pcie"))
  661. FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
  662. prop = get_property(node, "ranges");
  663. if (!prop)
  664. FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
  665. if (node_addr_cells(node) != 3)
  666. FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
  667. if (node_size_cells(node) != 2)
  668. FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
  669. prop = get_property(node, "bus-range");
  670. if (!prop) {
  671. FAIL(c, dti, node, "missing bus-range for PCI bridge");
  672. return;
  673. }
  674. if (prop->val.len != (sizeof(cell_t) * 2)) {
  675. FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
  676. return;
  677. }
  678. cells = (cell_t *)prop->val.val;
  679. if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
  680. FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
  681. if (fdt32_to_cpu(cells[1]) > 0xff)
  682. FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
  683. }
  684. WARNING(pci_bridge, check_pci_bridge, NULL,
  685. &device_type_is_string, &addr_size_cells);
  686. static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
  687. {
  688. struct property *prop;
  689. unsigned int bus_num, min_bus, max_bus;
  690. cell_t *cells;
  691. if (!node->parent || (node->parent->bus != &pci_bus))
  692. return;
  693. prop = get_property(node, "reg");
  694. if (!prop)
  695. return;
  696. cells = (cell_t *)prop->val.val;
  697. bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
  698. prop = get_property(node->parent, "bus-range");
  699. if (!prop) {
  700. min_bus = max_bus = 0;
  701. } else {
  702. cells = (cell_t *)prop->val.val;
  703. min_bus = fdt32_to_cpu(cells[0]);
  704. max_bus = fdt32_to_cpu(cells[0]);
  705. }
  706. if ((bus_num < min_bus) || (bus_num > max_bus))
  707. FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
  708. bus_num, min_bus, max_bus);
  709. }
  710. WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
  711. static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
  712. {
  713. struct property *prop;
  714. const char *unitname = get_unitname(node);
  715. char unit_addr[5];
  716. unsigned int dev, func, reg;
  717. cell_t *cells;
  718. if (!node->parent || (node->parent->bus != &pci_bus))
  719. return;
  720. prop = get_property(node, "reg");
  721. if (!prop) {
  722. FAIL(c, dti, node, "missing PCI reg property");
  723. return;
  724. }
  725. cells = (cell_t *)prop->val.val;
  726. if (cells[1] || cells[2])
  727. FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
  728. reg = fdt32_to_cpu(cells[0]);
  729. dev = (reg & 0xf800) >> 11;
  730. func = (reg & 0x700) >> 8;
  731. if (reg & 0xff000000)
  732. FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
  733. if (reg & 0x000000ff)
  734. FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
  735. if (func == 0) {
  736. snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
  737. if (streq(unitname, unit_addr))
  738. return;
  739. }
  740. snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
  741. if (streq(unitname, unit_addr))
  742. return;
  743. FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
  744. unit_addr);
  745. }
  746. WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
  747. static const struct bus_type simple_bus = {
  748. .name = "simple-bus",
  749. };
  750. static bool node_is_compatible(struct node *node, const char *compat)
  751. {
  752. struct property *prop;
  753. const char *str, *end;
  754. prop = get_property(node, "compatible");
  755. if (!prop)
  756. return false;
  757. for (str = prop->val.val, end = str + prop->val.len; str < end;
  758. str += strnlen(str, end - str) + 1) {
  759. if (strprefixeq(str, end - str, compat))
  760. return true;
  761. }
  762. return false;
  763. }
  764. static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
  765. {
  766. if (node_is_compatible(node, "simple-bus"))
  767. node->bus = &simple_bus;
  768. }
  769. WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL, &addr_size_cells);
  770. static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
  771. {
  772. struct property *prop;
  773. const char *unitname = get_unitname(node);
  774. char unit_addr[17];
  775. unsigned int size;
  776. uint64_t reg = 0;
  777. cell_t *cells = NULL;
  778. if (!node->parent || (node->parent->bus != &simple_bus))
  779. return;
  780. prop = get_property(node, "reg");
  781. if (prop)
  782. cells = (cell_t *)prop->val.val;
  783. else {
  784. prop = get_property(node, "ranges");
  785. if (prop && prop->val.len)
  786. /* skip of child address */
  787. cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
  788. }
  789. if (!cells) {
  790. if (node->parent->parent && !(node->bus == &simple_bus))
  791. FAIL(c, dti, node, "missing or empty reg/ranges property");
  792. return;
  793. }
  794. size = node_addr_cells(node->parent);
  795. while (size--)
  796. reg = (reg << 32) | fdt32_to_cpu(*(cells++));
  797. snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
  798. if (!streq(unitname, unit_addr))
  799. FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
  800. unit_addr);
  801. }
  802. WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
  803. static void check_unit_address_format(struct check *c, struct dt_info *dti,
  804. struct node *node)
  805. {
  806. const char *unitname = get_unitname(node);
  807. if (node->parent && node->parent->bus)
  808. return;
  809. if (!unitname[0])
  810. return;
  811. if (!strncmp(unitname, "0x", 2)) {
  812. FAIL(c, dti, node, "unit name should not have leading \"0x\"");
  813. /* skip over 0x for next test */
  814. unitname += 2;
  815. }
  816. if (unitname[0] == '0' && isxdigit(unitname[1]))
  817. FAIL(c, dti, node, "unit name should not have leading 0s");
  818. }
  819. WARNING(unit_address_format, check_unit_address_format, NULL,
  820. &node_name_format, &pci_bridge, &simple_bus_bridge);
  821. /*
  822. * Style checks
  823. */
  824. static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
  825. struct node *node)
  826. {
  827. struct property *reg, *ranges;
  828. if (!node->parent)
  829. return; /* Ignore root node */
  830. reg = get_property(node, "reg");
  831. ranges = get_property(node, "ranges");
  832. if (!reg && !ranges)
  833. return;
  834. if (node->parent->addr_cells == -1)
  835. FAIL(c, dti, node, "Relying on default #address-cells value");
  836. if (node->parent->size_cells == -1)
  837. FAIL(c, dti, node, "Relying on default #size-cells value");
  838. }
  839. WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
  840. &addr_size_cells);
  841. static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
  842. struct node *node)
  843. {
  844. struct property *prop;
  845. struct node *child;
  846. bool has_reg = false;
  847. if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
  848. return;
  849. if (get_property(node, "ranges") || !node->children)
  850. return;
  851. for_each_child(node, child) {
  852. prop = get_property(child, "reg");
  853. if (prop)
  854. has_reg = true;
  855. }
  856. if (!has_reg)
  857. FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
  858. }
  859. WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
  860. static void check_unique_unit_address(struct check *c, struct dt_info *dti,
  861. struct node *node)
  862. {
  863. struct node *childa;
  864. if (node->addr_cells < 0 || node->size_cells < 0)
  865. return;
  866. if (!node->children)
  867. return;
  868. for_each_child(node, childa) {
  869. struct node *childb;
  870. const char *addr_a = get_unitname(childa);
  871. if (!strlen(addr_a))
  872. continue;
  873. for_each_child(node, childb) {
  874. const char *addr_b = get_unitname(childb);
  875. if (childa == childb)
  876. break;
  877. if (streq(addr_a, addr_b))
  878. FAIL(c, dti, childb, "duplicate unit-address (also used in node %s)", childa->fullpath);
  879. }
  880. }
  881. }
  882. WARNING(unique_unit_address, check_unique_unit_address, NULL, &avoid_default_addr_size);
  883. static void check_obsolete_chosen_interrupt_controller(struct check *c,
  884. struct dt_info *dti,
  885. struct node *node)
  886. {
  887. struct node *dt = dti->dt;
  888. struct node *chosen;
  889. struct property *prop;
  890. if (node != dt)
  891. return;
  892. chosen = get_node_by_path(dt, "/chosen");
  893. if (!chosen)
  894. return;
  895. prop = get_property(chosen, "interrupt-controller");
  896. if (prop)
  897. FAIL_PROP(c, dti, node, prop,
  898. "/chosen has obsolete \"interrupt-controller\" property");
  899. }
  900. WARNING(obsolete_chosen_interrupt_controller,
  901. check_obsolete_chosen_interrupt_controller, NULL);
  902. static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
  903. struct node *node)
  904. {
  905. if (!streq(node->name, "chosen"))
  906. return;
  907. if (node->parent != dti->dt)
  908. FAIL(c, dti, node, "chosen node must be at root node");
  909. }
  910. WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
  911. static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
  912. struct node *node)
  913. {
  914. struct property *prop;
  915. if (!streq(node->name, "chosen"))
  916. return;
  917. prop = get_property(node, "bootargs");
  918. if (!prop)
  919. return;
  920. c->data = prop->name;
  921. check_is_string(c, dti, node);
  922. }
  923. WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
  924. static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
  925. struct node *node)
  926. {
  927. struct property *prop;
  928. if (!streq(node->name, "chosen"))
  929. return;
  930. prop = get_property(node, "stdout-path");
  931. if (!prop) {
  932. prop = get_property(node, "linux,stdout-path");
  933. if (!prop)
  934. return;
  935. FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
  936. }
  937. c->data = prop->name;
  938. check_is_string(c, dti, node);
  939. }
  940. WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
  941. struct provider {
  942. const char *prop_name;
  943. const char *cell_name;
  944. bool optional;
  945. };
  946. static void check_property_phandle_args(struct check *c,
  947. struct dt_info *dti,
  948. struct node *node,
  949. struct property *prop,
  950. const struct provider *provider)
  951. {
  952. struct node *root = dti->dt;
  953. int cell, cellsize = 0;
  954. if (prop->val.len % sizeof(cell_t)) {
  955. FAIL_PROP(c, dti, node, prop,
  956. "property size (%d) is invalid, expected multiple of %zu",
  957. prop->val.len, sizeof(cell_t));
  958. return;
  959. }
  960. for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
  961. struct node *provider_node;
  962. struct property *cellprop;
  963. int phandle;
  964. phandle = propval_cell_n(prop, cell);
  965. /*
  966. * Some bindings use a cell value 0 or -1 to skip over optional
  967. * entries when each index position has a specific definition.
  968. */
  969. if (phandle == 0 || phandle == -1) {
  970. /* Give up if this is an overlay with external references */
  971. if (dti->dtsflags & DTSF_PLUGIN)
  972. break;
  973. cellsize = 0;
  974. continue;
  975. }
  976. /* If we have markers, verify the current cell is a phandle */
  977. if (prop->val.markers) {
  978. struct marker *m = prop->val.markers;
  979. for_each_marker_of_type(m, REF_PHANDLE) {
  980. if (m->offset == (cell * sizeof(cell_t)))
  981. break;
  982. }
  983. if (!m)
  984. FAIL_PROP(c, dti, node, prop,
  985. "cell %d is not a phandle reference",
  986. cell);
  987. }
  988. provider_node = get_node_by_phandle(root, phandle);
  989. if (!provider_node) {
  990. FAIL_PROP(c, dti, node, prop,
  991. "Could not get phandle node for (cell %d)",
  992. cell);
  993. break;
  994. }
  995. cellprop = get_property(provider_node, provider->cell_name);
  996. if (cellprop) {
  997. cellsize = propval_cell(cellprop);
  998. } else if (provider->optional) {
  999. cellsize = 0;
  1000. } else {
  1001. FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
  1002. provider->cell_name,
  1003. provider_node->fullpath,
  1004. prop->name, cell);
  1005. break;
  1006. }
  1007. if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
  1008. FAIL_PROP(c, dti, node, prop,
  1009. "property size (%d) too small for cell size %d",
  1010. prop->val.len, cellsize);
  1011. }
  1012. }
  1013. }
  1014. static void check_provider_cells_property(struct check *c,
  1015. struct dt_info *dti,
  1016. struct node *node)
  1017. {
  1018. struct provider *provider = c->data;
  1019. struct property *prop;
  1020. prop = get_property(node, provider->prop_name);
  1021. if (!prop)
  1022. return;
  1023. check_property_phandle_args(c, dti, node, prop, provider);
  1024. }
  1025. #define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
  1026. static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
  1027. WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
  1028. WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
  1029. WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
  1030. WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
  1031. WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
  1032. WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
  1033. WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
  1034. WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
  1035. WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
  1036. WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
  1037. WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
  1038. WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
  1039. WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
  1040. WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
  1041. WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
  1042. WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
  1043. WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
  1044. static bool prop_is_gpio(struct property *prop)
  1045. {
  1046. char *str;
  1047. /*
  1048. * *-gpios and *-gpio can appear in property names,
  1049. * so skip over any false matches (only one known ATM)
  1050. */
  1051. if (strstr(prop->name, "nr-gpio"))
  1052. return false;
  1053. str = strrchr(prop->name, '-');
  1054. if (str)
  1055. str++;
  1056. else
  1057. str = prop->name;
  1058. if (!(streq(str, "gpios") || streq(str, "gpio")))
  1059. return false;
  1060. return true;
  1061. }
  1062. static void check_gpios_property(struct check *c,
  1063. struct dt_info *dti,
  1064. struct node *node)
  1065. {
  1066. struct property *prop;
  1067. /* Skip GPIO hog nodes which have 'gpios' property */
  1068. if (get_property(node, "gpio-hog"))
  1069. return;
  1070. for_each_property(node, prop) {
  1071. struct provider provider;
  1072. if (!prop_is_gpio(prop))
  1073. continue;
  1074. provider.prop_name = prop->name;
  1075. provider.cell_name = "#gpio-cells";
  1076. provider.optional = false;
  1077. check_property_phandle_args(c, dti, node, prop, &provider);
  1078. }
  1079. }
  1080. WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
  1081. static void check_deprecated_gpio_property(struct check *c,
  1082. struct dt_info *dti,
  1083. struct node *node)
  1084. {
  1085. struct property *prop;
  1086. for_each_property(node, prop) {
  1087. char *str;
  1088. if (!prop_is_gpio(prop))
  1089. continue;
  1090. str = strstr(prop->name, "gpio");
  1091. if (!streq(str, "gpio"))
  1092. continue;
  1093. FAIL_PROP(c, dti, node, prop,
  1094. "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
  1095. }
  1096. }
  1097. CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
  1098. static bool node_is_interrupt_provider(struct node *node)
  1099. {
  1100. struct property *prop;
  1101. prop = get_property(node, "interrupt-controller");
  1102. if (prop)
  1103. return true;
  1104. prop = get_property(node, "interrupt-map");
  1105. if (prop)
  1106. return true;
  1107. return false;
  1108. }
  1109. static void check_interrupts_property(struct check *c,
  1110. struct dt_info *dti,
  1111. struct node *node)
  1112. {
  1113. struct node *root = dti->dt;
  1114. struct node *irq_node = NULL, *parent = node;
  1115. struct property *irq_prop, *prop = NULL;
  1116. int irq_cells, phandle;
  1117. irq_prop = get_property(node, "interrupts");
  1118. if (!irq_prop)
  1119. return;
  1120. if (irq_prop->val.len % sizeof(cell_t))
  1121. FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
  1122. irq_prop->val.len, sizeof(cell_t));
  1123. while (parent && !prop) {
  1124. if (parent != node && node_is_interrupt_provider(parent)) {
  1125. irq_node = parent;
  1126. break;
  1127. }
  1128. prop = get_property(parent, "interrupt-parent");
  1129. if (prop) {
  1130. phandle = propval_cell(prop);
  1131. /* Give up if this is an overlay with external references */
  1132. if ((phandle == 0 || phandle == -1) &&
  1133. (dti->dtsflags & DTSF_PLUGIN))
  1134. return;
  1135. irq_node = get_node_by_phandle(root, phandle);
  1136. if (!irq_node) {
  1137. FAIL_PROP(c, dti, parent, prop, "Bad phandle");
  1138. return;
  1139. }
  1140. if (!node_is_interrupt_provider(irq_node))
  1141. FAIL(c, dti, irq_node,
  1142. "Missing interrupt-controller or interrupt-map property");
  1143. break;
  1144. }
  1145. parent = parent->parent;
  1146. }
  1147. if (!irq_node) {
  1148. FAIL(c, dti, node, "Missing interrupt-parent");
  1149. return;
  1150. }
  1151. prop = get_property(irq_node, "#interrupt-cells");
  1152. if (!prop) {
  1153. FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
  1154. return;
  1155. }
  1156. irq_cells = propval_cell(prop);
  1157. if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
  1158. FAIL_PROP(c, dti, node, prop,
  1159. "size is (%d), expected multiple of %d",
  1160. irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
  1161. }
  1162. }
  1163. WARNING(interrupts_property, check_interrupts_property, &phandle_references);
  1164. static const struct bus_type graph_port_bus = {
  1165. .name = "graph-port",
  1166. };
  1167. static const struct bus_type graph_ports_bus = {
  1168. .name = "graph-ports",
  1169. };
  1170. static void check_graph_nodes(struct check *c, struct dt_info *dti,
  1171. struct node *node)
  1172. {
  1173. struct node *child;
  1174. for_each_child(node, child) {
  1175. if (!(strprefixeq(child->name, child->basenamelen, "endpoint") ||
  1176. get_property(child, "remote-endpoint")))
  1177. continue;
  1178. node->bus = &graph_port_bus;
  1179. /* The parent of 'port' nodes can be either 'ports' or a device */
  1180. if (!node->parent->bus &&
  1181. (streq(node->parent->name, "ports") || get_property(node, "reg")))
  1182. node->parent->bus = &graph_ports_bus;
  1183. break;
  1184. }
  1185. }
  1186. WARNING(graph_nodes, check_graph_nodes, NULL);
  1187. static void check_graph_child_address(struct check *c, struct dt_info *dti,
  1188. struct node *node)
  1189. {
  1190. int cnt = 0;
  1191. struct node *child;
  1192. if (node->bus != &graph_ports_bus && node->bus != &graph_port_bus)
  1193. return;
  1194. for_each_child(node, child) {
  1195. struct property *prop = get_property(child, "reg");
  1196. /* No error if we have any non-zero unit address */
  1197. if (prop && propval_cell(prop) != 0)
  1198. return;
  1199. cnt++;
  1200. }
  1201. if (cnt == 1 && node->addr_cells != -1)
  1202. FAIL(c, dti, node, "graph node has single child node '%s', #address-cells/#size-cells are not necessary",
  1203. node->children->name);
  1204. }
  1205. WARNING(graph_child_address, check_graph_child_address, NULL, &graph_nodes);
  1206. static void check_graph_reg(struct check *c, struct dt_info *dti,
  1207. struct node *node)
  1208. {
  1209. char unit_addr[9];
  1210. const char *unitname = get_unitname(node);
  1211. struct property *prop;
  1212. prop = get_property(node, "reg");
  1213. if (!prop || !unitname)
  1214. return;
  1215. if (!(prop->val.val && prop->val.len == sizeof(cell_t))) {
  1216. FAIL(c, dti, node, "graph node malformed 'reg' property");
  1217. return;
  1218. }
  1219. snprintf(unit_addr, sizeof(unit_addr), "%x", propval_cell(prop));
  1220. if (!streq(unitname, unit_addr))
  1221. FAIL(c, dti, node, "graph node unit address error, expected \"%s\"",
  1222. unit_addr);
  1223. if (node->parent->addr_cells != 1)
  1224. FAIL_PROP(c, dti, node, get_property(node, "#address-cells"),
  1225. "graph node '#address-cells' is %d, must be 1",
  1226. node->parent->addr_cells);
  1227. if (node->parent->size_cells != 0)
  1228. FAIL_PROP(c, dti, node, get_property(node, "#size-cells"),
  1229. "graph node '#size-cells' is %d, must be 0",
  1230. node->parent->size_cells);
  1231. }
  1232. static void check_graph_port(struct check *c, struct dt_info *dti,
  1233. struct node *node)
  1234. {
  1235. if (node->bus != &graph_port_bus)
  1236. return;
  1237. if (!strprefixeq(node->name, node->basenamelen, "port"))
  1238. FAIL(c, dti, node, "graph port node name should be 'port'");
  1239. check_graph_reg(c, dti, node);
  1240. }
  1241. WARNING(graph_port, check_graph_port, NULL, &graph_nodes);
  1242. static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,
  1243. struct node *endpoint)
  1244. {
  1245. int phandle;
  1246. struct node *node;
  1247. struct property *prop;
  1248. prop = get_property(endpoint, "remote-endpoint");
  1249. if (!prop)
  1250. return NULL;
  1251. phandle = propval_cell(prop);
  1252. /* Give up if this is an overlay with external references */
  1253. if (phandle == 0 || phandle == -1)
  1254. return NULL;
  1255. node = get_node_by_phandle(dti->dt, phandle);
  1256. if (!node)
  1257. FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
  1258. return node;
  1259. }
  1260. static void check_graph_endpoint(struct check *c, struct dt_info *dti,
  1261. struct node *node)
  1262. {
  1263. struct node *remote_node;
  1264. if (!node->parent || node->parent->bus != &graph_port_bus)
  1265. return;
  1266. if (!strprefixeq(node->name, node->basenamelen, "endpoint"))
  1267. FAIL(c, dti, node, "graph endpont node name should be 'endpoint'");
  1268. check_graph_reg(c, dti, node);
  1269. remote_node = get_remote_endpoint(c, dti, node);
  1270. if (!remote_node)
  1271. return;
  1272. if (get_remote_endpoint(c, dti, remote_node) != node)
  1273. FAIL(c, dti, node, "graph connection to node '%s' is not bidirectional",
  1274. remote_node->fullpath);
  1275. }
  1276. WARNING(graph_endpoint, check_graph_endpoint, NULL, &graph_nodes);
  1277. static struct check *check_table[] = {
  1278. &duplicate_node_names, &duplicate_property_names,
  1279. &node_name_chars, &node_name_format, &property_name_chars,
  1280. &name_is_string, &name_properties,
  1281. &duplicate_label,
  1282. &explicit_phandles,
  1283. &phandle_references, &path_references,
  1284. &omit_unused_nodes,
  1285. &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
  1286. &device_type_is_string, &model_is_string, &status_is_string,
  1287. &label_is_string,
  1288. &compatible_is_string_list, &names_is_string_list,
  1289. &property_name_chars_strict,
  1290. &node_name_chars_strict,
  1291. &addr_size_cells, &reg_format, &ranges_format,
  1292. &unit_address_vs_reg,
  1293. &unit_address_format,
  1294. &pci_bridge,
  1295. &pci_device_reg,
  1296. &pci_device_bus_num,
  1297. &simple_bus_bridge,
  1298. &simple_bus_reg,
  1299. &avoid_default_addr_size,
  1300. &avoid_unnecessary_addr_size,
  1301. &unique_unit_address,
  1302. &obsolete_chosen_interrupt_controller,
  1303. &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
  1304. &clocks_property,
  1305. &cooling_device_property,
  1306. &dmas_property,
  1307. &hwlocks_property,
  1308. &interrupts_extended_property,
  1309. &io_channels_property,
  1310. &iommus_property,
  1311. &mboxes_property,
  1312. &msi_parent_property,
  1313. &mux_controls_property,
  1314. &phys_property,
  1315. &power_domains_property,
  1316. &pwms_property,
  1317. &resets_property,
  1318. &sound_dai_property,
  1319. &thermal_sensors_property,
  1320. &deprecated_gpio_property,
  1321. &gpios_property,
  1322. &interrupts_property,
  1323. &alias_paths,
  1324. &graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
  1325. &always_fail,
  1326. };
  1327. static void enable_warning_error(struct check *c, bool warn, bool error)
  1328. {
  1329. int i;
  1330. /* Raising level, also raise it for prereqs */
  1331. if ((warn && !c->warn) || (error && !c->error))
  1332. for (i = 0; i < c->num_prereqs; i++)
  1333. enable_warning_error(c->prereq[i], warn, error);
  1334. c->warn = c->warn || warn;
  1335. c->error = c->error || error;
  1336. }
  1337. static void disable_warning_error(struct check *c, bool warn, bool error)
  1338. {
  1339. int i;
  1340. /* Lowering level, also lower it for things this is the prereq
  1341. * for */
  1342. if ((warn && c->warn) || (error && c->error)) {
  1343. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  1344. struct check *cc = check_table[i];
  1345. int j;
  1346. for (j = 0; j < cc->num_prereqs; j++)
  1347. if (cc->prereq[j] == c)
  1348. disable_warning_error(cc, warn, error);
  1349. }
  1350. }
  1351. c->warn = c->warn && !warn;
  1352. c->error = c->error && !error;
  1353. }
  1354. void parse_checks_option(bool warn, bool error, const char *arg)
  1355. {
  1356. int i;
  1357. const char *name = arg;
  1358. bool enable = true;
  1359. if ((strncmp(arg, "no-", 3) == 0)
  1360. || (strncmp(arg, "no_", 3) == 0)) {
  1361. name = arg + 3;
  1362. enable = false;
  1363. }
  1364. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  1365. struct check *c = check_table[i];
  1366. if (streq(c->name, name)) {
  1367. if (enable)
  1368. enable_warning_error(c, warn, error);
  1369. else
  1370. disable_warning_error(c, warn, error);
  1371. return;
  1372. }
  1373. }
  1374. die("Unrecognized check name \"%s\"\n", name);
  1375. }
  1376. void process_checks(bool force, struct dt_info *dti)
  1377. {
  1378. int i;
  1379. int error = 0;
  1380. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  1381. struct check *c = check_table[i];
  1382. if (c->warn || c->error)
  1383. error = error || run_check(c, dti);
  1384. }
  1385. if (error) {
  1386. if (!force) {
  1387. fprintf(stderr, "ERROR: Input tree has errors, aborting "
  1388. "(use -f to force output)\n");
  1389. exit(2);
  1390. } else if (quiet < 3) {
  1391. fprintf(stderr, "Warning: Input tree has errors, "
  1392. "output forced\n");
  1393. }
  1394. }
  1395. }