selftest.c 22 KB

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