selftest.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Self tests for device tree subsystem
  3. */
  4. #define pr_fmt(fmt) "### dt-test ### " fmt
  5. #include <linux/clk.h>
  6. #include <linux/err.h>
  7. #include <linux/errno.h>
  8. #include <linux/hashtable.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_fdt.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include "of_private.h"
  19. static struct selftest_results {
  20. int passed;
  21. int failed;
  22. } selftest_results;
  23. #define NO_OF_NODES 3
  24. static struct device_node *nodes[NO_OF_NODES];
  25. static int last_node_index;
  26. static bool selftest_live_tree;
  27. #define selftest(result, fmt, ...) { \
  28. if (!(result)) { \
  29. selftest_results.failed++; \
  30. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  31. } else { \
  32. selftest_results.passed++; \
  33. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  34. } \
  35. }
  36. static void __init of_selftest_find_node_by_name(void)
  37. {
  38. struct device_node *np;
  39. np = of_find_node_by_path("/testcase-data");
  40. selftest(np && !strcmp("/testcase-data", np->full_name),
  41. "find /testcase-data failed\n");
  42. of_node_put(np);
  43. /* Test if trailing '/' works */
  44. np = of_find_node_by_path("/testcase-data/");
  45. selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
  46. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  47. selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  48. "find /testcase-data/phandle-tests/consumer-a failed\n");
  49. of_node_put(np);
  50. np = of_find_node_by_path("testcase-alias");
  51. selftest(np && !strcmp("/testcase-data", np->full_name),
  52. "find testcase-alias failed\n");
  53. of_node_put(np);
  54. /* Test if trailing '/' works on aliases */
  55. np = of_find_node_by_path("testcase-alias/");
  56. selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
  57. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  58. selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  59. "find testcase-alias/phandle-tests/consumer-a failed\n");
  60. of_node_put(np);
  61. np = of_find_node_by_path("/testcase-data/missing-path");
  62. selftest(!np, "non-existent path returned node %s\n", np->full_name);
  63. of_node_put(np);
  64. np = of_find_node_by_path("missing-alias");
  65. selftest(!np, "non-existent alias returned node %s\n", np->full_name);
  66. of_node_put(np);
  67. np = of_find_node_by_path("testcase-alias/missing-path");
  68. selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
  69. of_node_put(np);
  70. }
  71. static void __init of_selftest_dynamic(void)
  72. {
  73. struct device_node *np;
  74. struct property *prop;
  75. np = of_find_node_by_path("/testcase-data");
  76. if (!np) {
  77. pr_err("missing testcase data\n");
  78. return;
  79. }
  80. /* Array of 4 properties for the purpose of testing */
  81. prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
  82. if (!prop) {
  83. selftest(0, "kzalloc() failed\n");
  84. return;
  85. }
  86. /* Add a new property - should pass*/
  87. prop->name = "new-property";
  88. prop->value = "new-property-data";
  89. prop->length = strlen(prop->value);
  90. selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  91. /* Try to add an existing property - should fail */
  92. prop++;
  93. prop->name = "new-property";
  94. prop->value = "new-property-data-should-fail";
  95. prop->length = strlen(prop->value);
  96. selftest(of_add_property(np, prop) != 0,
  97. "Adding an existing property should have failed\n");
  98. /* Try to modify an existing property - should pass */
  99. prop->value = "modify-property-data-should-pass";
  100. prop->length = strlen(prop->value);
  101. selftest(of_update_property(np, prop) == 0,
  102. "Updating an existing property should have passed\n");
  103. /* Try to modify non-existent property - should pass*/
  104. prop++;
  105. prop->name = "modify-property";
  106. prop->value = "modify-missing-property-data-should-pass";
  107. prop->length = strlen(prop->value);
  108. selftest(of_update_property(np, prop) == 0,
  109. "Updating a missing property should have passed\n");
  110. /* Remove property - should pass */
  111. selftest(of_remove_property(np, prop) == 0,
  112. "Removing a property should have passed\n");
  113. /* Adding very large property - should pass */
  114. prop++;
  115. prop->name = "large-property-PAGE_SIZEx8";
  116. prop->length = PAGE_SIZE * 8;
  117. prop->value = kzalloc(prop->length, GFP_KERNEL);
  118. selftest(prop->value != NULL, "Unable to allocate large buffer\n");
  119. if (prop->value)
  120. selftest(of_add_property(np, prop) == 0,
  121. "Adding a large property should have passed\n");
  122. }
  123. static int __init of_selftest_check_node_linkage(struct device_node *np)
  124. {
  125. struct device_node *child, *allnext_index = np;
  126. int count = 0, rc;
  127. for_each_child_of_node(np, child) {
  128. if (child->parent != np) {
  129. pr_err("Child node %s links to wrong parent %s\n",
  130. child->name, np->name);
  131. return -EINVAL;
  132. }
  133. while (allnext_index && allnext_index != child)
  134. allnext_index = allnext_index->allnext;
  135. if (allnext_index != child) {
  136. pr_err("Node %s is ordered differently in sibling and allnode lists\n",
  137. child->name);
  138. return -EINVAL;
  139. }
  140. rc = of_selftest_check_node_linkage(child);
  141. if (rc < 0)
  142. return rc;
  143. count += rc;
  144. }
  145. return count + 1;
  146. }
  147. static void __init of_selftest_check_tree_linkage(void)
  148. {
  149. struct device_node *np;
  150. int allnode_count = 0, child_count;
  151. if (!of_allnodes)
  152. return;
  153. for_each_of_allnodes(np)
  154. allnode_count++;
  155. child_count = of_selftest_check_node_linkage(of_allnodes);
  156. selftest(child_count > 0, "Device node data structure is corrupted\n");
  157. selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
  158. "sibling lists size (%i)\n", allnode_count, child_count);
  159. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  160. }
  161. struct node_hash {
  162. struct hlist_node node;
  163. struct device_node *np;
  164. };
  165. static DEFINE_HASHTABLE(phandle_ht, 8);
  166. static void __init of_selftest_check_phandles(void)
  167. {
  168. struct device_node *np;
  169. struct node_hash *nh;
  170. struct hlist_node *tmp;
  171. int i, dup_count = 0, phandle_count = 0;
  172. for_each_of_allnodes(np) {
  173. if (!np->phandle)
  174. continue;
  175. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  176. if (nh->np->phandle == np->phandle) {
  177. pr_info("Duplicate phandle! %i used by %s and %s\n",
  178. np->phandle, nh->np->full_name, np->full_name);
  179. dup_count++;
  180. break;
  181. }
  182. }
  183. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  184. if (WARN_ON(!nh))
  185. return;
  186. nh->np = np;
  187. hash_add(phandle_ht, &nh->node, np->phandle);
  188. phandle_count++;
  189. }
  190. selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  191. dup_count, phandle_count);
  192. /* Clean up */
  193. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  194. hash_del(&nh->node);
  195. kfree(nh);
  196. }
  197. }
  198. static void __init of_selftest_parse_phandle_with_args(void)
  199. {
  200. struct device_node *np;
  201. struct of_phandle_args args;
  202. int i, rc;
  203. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  204. if (!np) {
  205. pr_err("missing testcase data\n");
  206. return;
  207. }
  208. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  209. selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  210. for (i = 0; i < 8; i++) {
  211. bool passed = true;
  212. rc = of_parse_phandle_with_args(np, "phandle-list",
  213. "#phandle-cells", i, &args);
  214. /* Test the values from tests-phandle.dtsi */
  215. switch (i) {
  216. case 0:
  217. passed &= !rc;
  218. passed &= (args.args_count == 1);
  219. passed &= (args.args[0] == (i + 1));
  220. break;
  221. case 1:
  222. passed &= !rc;
  223. passed &= (args.args_count == 2);
  224. passed &= (args.args[0] == (i + 1));
  225. passed &= (args.args[1] == 0);
  226. break;
  227. case 2:
  228. passed &= (rc == -ENOENT);
  229. break;
  230. case 3:
  231. passed &= !rc;
  232. passed &= (args.args_count == 3);
  233. passed &= (args.args[0] == (i + 1));
  234. passed &= (args.args[1] == 4);
  235. passed &= (args.args[2] == 3);
  236. break;
  237. case 4:
  238. passed &= !rc;
  239. passed &= (args.args_count == 2);
  240. passed &= (args.args[0] == (i + 1));
  241. passed &= (args.args[1] == 100);
  242. break;
  243. case 5:
  244. passed &= !rc;
  245. passed &= (args.args_count == 0);
  246. break;
  247. case 6:
  248. passed &= !rc;
  249. passed &= (args.args_count == 1);
  250. passed &= (args.args[0] == (i + 1));
  251. break;
  252. case 7:
  253. passed &= (rc == -ENOENT);
  254. break;
  255. default:
  256. passed = false;
  257. }
  258. selftest(passed, "index %i - data error on node %s rc=%i\n",
  259. i, args.np->full_name, rc);
  260. }
  261. /* Check for missing list property */
  262. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  263. "#phandle-cells", 0, &args);
  264. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  265. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  266. "#phandle-cells");
  267. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  268. /* Check for missing cells property */
  269. rc = of_parse_phandle_with_args(np, "phandle-list",
  270. "#phandle-cells-missing", 0, &args);
  271. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  272. rc = of_count_phandle_with_args(np, "phandle-list",
  273. "#phandle-cells-missing");
  274. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  275. /* Check for bad phandle in list */
  276. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  277. "#phandle-cells", 0, &args);
  278. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  279. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  280. "#phandle-cells");
  281. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  282. /* Check for incorrectly formed argument list */
  283. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  284. "#phandle-cells", 1, &args);
  285. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  286. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  287. "#phandle-cells");
  288. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  289. }
  290. static void __init of_selftest_property_match_string(void)
  291. {
  292. struct device_node *np;
  293. int rc;
  294. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  295. if (!np) {
  296. pr_err("No testcase data in device tree\n");
  297. return;
  298. }
  299. rc = of_property_match_string(np, "phandle-list-names", "first");
  300. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  301. rc = of_property_match_string(np, "phandle-list-names", "second");
  302. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  303. rc = of_property_match_string(np, "phandle-list-names", "third");
  304. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  305. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  306. selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
  307. rc = of_property_match_string(np, "missing-property", "blah");
  308. selftest(rc == -EINVAL, "missing property; rc=%i", rc);
  309. rc = of_property_match_string(np, "empty-property", "blah");
  310. selftest(rc == -ENODATA, "empty property; rc=%i", rc);
  311. rc = of_property_match_string(np, "unterminated-string", "blah");
  312. selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
  313. }
  314. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  315. (p1)->value && (p2)->value && \
  316. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  317. !strcmp((p1)->name, (p2)->name))
  318. static void __init of_selftest_property_copy(void)
  319. {
  320. #ifdef CONFIG_OF_DYNAMIC
  321. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  322. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  323. struct property *new;
  324. new = __of_prop_dup(&p1, GFP_KERNEL);
  325. selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  326. kfree(new->value);
  327. kfree(new->name);
  328. kfree(new);
  329. new = __of_prop_dup(&p2, GFP_KERNEL);
  330. selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  331. kfree(new->value);
  332. kfree(new->name);
  333. kfree(new);
  334. #endif
  335. }
  336. static void __init of_selftest_changeset(void)
  337. {
  338. #ifdef CONFIG_OF_DYNAMIC
  339. struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
  340. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  341. struct property *ppremove;
  342. struct device_node *n1, *n2, *n21, *nremove, *parent;
  343. struct of_changeset chgset;
  344. of_changeset_init(&chgset);
  345. n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
  346. selftest(n1, "testcase setup failure\n");
  347. n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
  348. selftest(n2, "testcase setup failure\n");
  349. n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
  350. selftest(n21, "testcase setup failure %p\n", n21);
  351. nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
  352. selftest(nremove, "testcase setup failure\n");
  353. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  354. selftest(ppadd, "testcase setup failure\n");
  355. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  356. selftest(ppupdate, "testcase setup failure\n");
  357. parent = nremove->parent;
  358. n1->parent = parent;
  359. n2->parent = parent;
  360. n21->parent = n2;
  361. n2->child = n21;
  362. ppremove = of_find_property(parent, "prop-remove", NULL);
  363. selftest(ppremove, "failed to find removal prop");
  364. of_changeset_init(&chgset);
  365. selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  366. selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  367. selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  368. selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  369. selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
  370. selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  371. selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  372. mutex_lock(&of_mutex);
  373. selftest(!of_changeset_apply(&chgset), "apply failed\n");
  374. mutex_unlock(&of_mutex);
  375. mutex_lock(&of_mutex);
  376. selftest(!of_changeset_revert(&chgset), "revert failed\n");
  377. mutex_unlock(&of_mutex);
  378. of_changeset_destroy(&chgset);
  379. #endif
  380. }
  381. static void __init of_selftest_parse_interrupts(void)
  382. {
  383. struct device_node *np;
  384. struct of_phandle_args args;
  385. int i, rc;
  386. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  387. if (!np) {
  388. pr_err("missing testcase data\n");
  389. return;
  390. }
  391. for (i = 0; i < 4; i++) {
  392. bool passed = true;
  393. args.args_count = 0;
  394. rc = of_irq_parse_one(np, i, &args);
  395. passed &= !rc;
  396. passed &= (args.args_count == 1);
  397. passed &= (args.args[0] == (i + 1));
  398. selftest(passed, "index %i - data error on node %s rc=%i\n",
  399. i, args.np->full_name, rc);
  400. }
  401. of_node_put(np);
  402. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  403. if (!np) {
  404. pr_err("missing testcase data\n");
  405. return;
  406. }
  407. for (i = 0; i < 4; i++) {
  408. bool passed = true;
  409. args.args_count = 0;
  410. rc = of_irq_parse_one(np, i, &args);
  411. /* Test the values from tests-phandle.dtsi */
  412. switch (i) {
  413. case 0:
  414. passed &= !rc;
  415. passed &= (args.args_count == 1);
  416. passed &= (args.args[0] == 9);
  417. break;
  418. case 1:
  419. passed &= !rc;
  420. passed &= (args.args_count == 3);
  421. passed &= (args.args[0] == 10);
  422. passed &= (args.args[1] == 11);
  423. passed &= (args.args[2] == 12);
  424. break;
  425. case 2:
  426. passed &= !rc;
  427. passed &= (args.args_count == 2);
  428. passed &= (args.args[0] == 13);
  429. passed &= (args.args[1] == 14);
  430. break;
  431. case 3:
  432. passed &= !rc;
  433. passed &= (args.args_count == 2);
  434. passed &= (args.args[0] == 15);
  435. passed &= (args.args[1] == 16);
  436. break;
  437. default:
  438. passed = false;
  439. }
  440. selftest(passed, "index %i - data error on node %s rc=%i\n",
  441. i, args.np->full_name, rc);
  442. }
  443. of_node_put(np);
  444. }
  445. static void __init of_selftest_parse_interrupts_extended(void)
  446. {
  447. struct device_node *np;
  448. struct of_phandle_args args;
  449. int i, rc;
  450. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  451. if (!np) {
  452. pr_err("missing testcase data\n");
  453. return;
  454. }
  455. for (i = 0; i < 7; i++) {
  456. bool passed = true;
  457. rc = of_irq_parse_one(np, i, &args);
  458. /* Test the values from tests-phandle.dtsi */
  459. switch (i) {
  460. case 0:
  461. passed &= !rc;
  462. passed &= (args.args_count == 1);
  463. passed &= (args.args[0] == 1);
  464. break;
  465. case 1:
  466. passed &= !rc;
  467. passed &= (args.args_count == 3);
  468. passed &= (args.args[0] == 2);
  469. passed &= (args.args[1] == 3);
  470. passed &= (args.args[2] == 4);
  471. break;
  472. case 2:
  473. passed &= !rc;
  474. passed &= (args.args_count == 2);
  475. passed &= (args.args[0] == 5);
  476. passed &= (args.args[1] == 6);
  477. break;
  478. case 3:
  479. passed &= !rc;
  480. passed &= (args.args_count == 1);
  481. passed &= (args.args[0] == 9);
  482. break;
  483. case 4:
  484. passed &= !rc;
  485. passed &= (args.args_count == 3);
  486. passed &= (args.args[0] == 10);
  487. passed &= (args.args[1] == 11);
  488. passed &= (args.args[2] == 12);
  489. break;
  490. case 5:
  491. passed &= !rc;
  492. passed &= (args.args_count == 2);
  493. passed &= (args.args[0] == 13);
  494. passed &= (args.args[1] == 14);
  495. break;
  496. case 6:
  497. passed &= !rc;
  498. passed &= (args.args_count == 1);
  499. passed &= (args.args[0] == 15);
  500. break;
  501. default:
  502. passed = false;
  503. }
  504. selftest(passed, "index %i - data error on node %s rc=%i\n",
  505. i, args.np->full_name, rc);
  506. }
  507. of_node_put(np);
  508. }
  509. static struct of_device_id match_node_table[] = {
  510. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  511. { .data = "B", .type = "type1", }, /* followed by type alone */
  512. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  513. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  514. { .data = "Cc", .name = "name2", .type = "type2", },
  515. { .data = "E", .compatible = "compat3" },
  516. { .data = "G", .compatible = "compat2", },
  517. { .data = "H", .compatible = "compat2", .name = "name5", },
  518. { .data = "I", .compatible = "compat2", .type = "type1", },
  519. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  520. { .data = "K", .compatible = "compat2", .name = "name9", },
  521. {}
  522. };
  523. static struct {
  524. const char *path;
  525. const char *data;
  526. } match_node_tests[] = {
  527. { .path = "/testcase-data/match-node/name0", .data = "A", },
  528. { .path = "/testcase-data/match-node/name1", .data = "B", },
  529. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  530. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  531. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  532. { .path = "/testcase-data/match-node/name3", .data = "E", },
  533. { .path = "/testcase-data/match-node/name4", .data = "G", },
  534. { .path = "/testcase-data/match-node/name5", .data = "H", },
  535. { .path = "/testcase-data/match-node/name6", .data = "G", },
  536. { .path = "/testcase-data/match-node/name7", .data = "I", },
  537. { .path = "/testcase-data/match-node/name8", .data = "J", },
  538. { .path = "/testcase-data/match-node/name9", .data = "K", },
  539. };
  540. static void __init of_selftest_match_node(void)
  541. {
  542. struct device_node *np;
  543. const struct of_device_id *match;
  544. int i;
  545. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  546. np = of_find_node_by_path(match_node_tests[i].path);
  547. if (!np) {
  548. selftest(0, "missing testcase node %s\n",
  549. match_node_tests[i].path);
  550. continue;
  551. }
  552. match = of_match_node(match_node_table, np);
  553. if (!match) {
  554. selftest(0, "%s didn't match anything\n",
  555. match_node_tests[i].path);
  556. continue;
  557. }
  558. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  559. selftest(0, "%s got wrong match. expected %s, got %s\n",
  560. match_node_tests[i].path, match_node_tests[i].data,
  561. (const char *)match->data);
  562. continue;
  563. }
  564. selftest(1, "passed");
  565. }
  566. }
  567. static void __init of_selftest_platform_populate(void)
  568. {
  569. int irq;
  570. struct device_node *np, *child;
  571. struct platform_device *pdev;
  572. struct of_device_id match[] = {
  573. { .compatible = "test-device", },
  574. {}
  575. };
  576. np = of_find_node_by_path("/testcase-data");
  577. of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
  578. /* Test that a missing irq domain returns -EPROBE_DEFER */
  579. np = of_find_node_by_path("/testcase-data/testcase-device1");
  580. pdev = of_find_device_by_node(np);
  581. selftest(pdev, "device 1 creation failed\n");
  582. irq = platform_get_irq(pdev, 0);
  583. selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
  584. /* Test that a parsing failure does not return -EPROBE_DEFER */
  585. np = of_find_node_by_path("/testcase-data/testcase-device2");
  586. pdev = of_find_device_by_node(np);
  587. selftest(pdev, "device 2 creation failed\n");
  588. irq = platform_get_irq(pdev, 0);
  589. selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
  590. np = of_find_node_by_path("/testcase-data/platform-tests");
  591. if (!np) {
  592. pr_err("No testcase data in device tree\n");
  593. return;
  594. }
  595. for_each_child_of_node(np, child) {
  596. struct device_node *grandchild;
  597. of_platform_populate(child, match, NULL, NULL);
  598. for_each_child_of_node(child, grandchild)
  599. selftest(of_find_device_by_node(grandchild),
  600. "Could not create device for node '%s'\n",
  601. grandchild->name);
  602. }
  603. }
  604. /**
  605. * update_node_properties - adds the properties
  606. * of np into dup node (present in live tree) and
  607. * updates parent of children of np to dup.
  608. *
  609. * @np: node already present in live tree
  610. * @dup: node present in live tree to be updated
  611. */
  612. static void update_node_properties(struct device_node *np,
  613. struct device_node *dup)
  614. {
  615. struct property *prop;
  616. struct device_node *child;
  617. for_each_property_of_node(np, prop)
  618. of_add_property(dup, prop);
  619. for_each_child_of_node(np, child)
  620. child->parent = dup;
  621. }
  622. /**
  623. * attach_node_and_children - attaches nodes
  624. * and its children to live tree
  625. *
  626. * @np: Node to attach to live tree
  627. */
  628. static int attach_node_and_children(struct device_node *np)
  629. {
  630. struct device_node *next, *root = np, *dup;
  631. /* skip root node */
  632. np = np->child;
  633. /* storing a copy in temporary node */
  634. dup = np;
  635. while (dup) {
  636. if (WARN_ON(last_node_index >= NO_OF_NODES))
  637. return -EINVAL;
  638. nodes[last_node_index++] = dup;
  639. dup = dup->sibling;
  640. }
  641. dup = NULL;
  642. while (np) {
  643. next = np->allnext;
  644. dup = of_find_node_by_path(np->full_name);
  645. if (dup)
  646. update_node_properties(np, dup);
  647. else {
  648. np->child = NULL;
  649. if (np->parent == root)
  650. np->parent = of_allnodes;
  651. of_attach_node(np);
  652. }
  653. np = next;
  654. }
  655. return 0;
  656. }
  657. /**
  658. * selftest_data_add - Reads, copies data from
  659. * linked tree and attaches it to the live tree
  660. */
  661. static int __init selftest_data_add(void)
  662. {
  663. void *selftest_data;
  664. struct device_node *selftest_data_node, *np;
  665. extern uint8_t __dtb_testcases_begin[];
  666. extern uint8_t __dtb_testcases_end[];
  667. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  668. int rc;
  669. if (!size) {
  670. pr_warn("%s: No testcase data to attach; not running tests\n",
  671. __func__);
  672. return -ENODATA;
  673. }
  674. /* creating copy */
  675. selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  676. if (!selftest_data) {
  677. pr_warn("%s: Failed to allocate memory for selftest_data; "
  678. "not running tests\n", __func__);
  679. return -ENOMEM;
  680. }
  681. of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
  682. if (!selftest_data_node) {
  683. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  684. return -ENODATA;
  685. }
  686. of_node_set_flag(selftest_data_node, OF_DETACHED);
  687. rc = of_resolve_phandles(selftest_data_node);
  688. if (rc) {
  689. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  690. return -EINVAL;
  691. }
  692. if (!of_allnodes) {
  693. /* enabling flag for removing nodes */
  694. selftest_live_tree = true;
  695. of_allnodes = selftest_data_node;
  696. for_each_of_allnodes(np)
  697. __of_attach_node_sysfs(np);
  698. of_aliases = of_find_node_by_path("/aliases");
  699. of_chosen = of_find_node_by_path("/chosen");
  700. return 0;
  701. }
  702. /* attach the sub-tree to live tree */
  703. return attach_node_and_children(selftest_data_node);
  704. }
  705. /**
  706. * detach_node_and_children - detaches node
  707. * and its children from live tree
  708. *
  709. * @np: Node to detach from live tree
  710. */
  711. static void detach_node_and_children(struct device_node *np)
  712. {
  713. while (np->child)
  714. detach_node_and_children(np->child);
  715. of_detach_node(np);
  716. }
  717. /**
  718. * selftest_data_remove - removes the selftest data
  719. * nodes from the live tree
  720. */
  721. static void selftest_data_remove(void)
  722. {
  723. struct device_node *np;
  724. struct property *prop;
  725. if (selftest_live_tree) {
  726. of_node_put(of_aliases);
  727. of_node_put(of_chosen);
  728. of_aliases = NULL;
  729. of_chosen = NULL;
  730. for_each_child_of_node(of_allnodes, np)
  731. detach_node_and_children(np);
  732. __of_detach_node_sysfs(of_allnodes);
  733. of_allnodes = NULL;
  734. return;
  735. }
  736. while (last_node_index >= 0) {
  737. if (nodes[last_node_index]) {
  738. np = of_find_node_by_path(nodes[last_node_index]->full_name);
  739. if (strcmp(np->full_name, "/aliases") != 0) {
  740. detach_node_and_children(np);
  741. } else {
  742. for_each_property_of_node(np, prop) {
  743. if (strcmp(prop->name, "testcase-alias") == 0)
  744. of_remove_property(np, prop);
  745. }
  746. }
  747. }
  748. last_node_index--;
  749. }
  750. }
  751. static int __init of_selftest(void)
  752. {
  753. struct device_node *np;
  754. int res;
  755. /* adding data for selftest */
  756. res = selftest_data_add();
  757. if (res)
  758. return res;
  759. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  760. if (!np) {
  761. pr_info("No testcase data in device tree; not running tests\n");
  762. return 0;
  763. }
  764. of_node_put(np);
  765. pr_info("start of selftest - you will see error messages\n");
  766. of_selftest_check_tree_linkage();
  767. of_selftest_check_phandles();
  768. of_selftest_find_node_by_name();
  769. of_selftest_dynamic();
  770. of_selftest_parse_phandle_with_args();
  771. of_selftest_property_match_string();
  772. of_selftest_property_copy();
  773. of_selftest_changeset();
  774. of_selftest_parse_interrupts();
  775. of_selftest_parse_interrupts_extended();
  776. of_selftest_match_node();
  777. of_selftest_platform_populate();
  778. /* removing selftest data from live tree */
  779. selftest_data_remove();
  780. /* Double check linkage after removing testcase data */
  781. of_selftest_check_tree_linkage();
  782. pr_info("end of selftest - %i passed, %i failed\n",
  783. selftest_results.passed, selftest_results.failed);
  784. return 0;
  785. }
  786. late_initcall(of_selftest);