selftest.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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_irq.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/list.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/device.h>
  16. static struct selftest_results {
  17. int passed;
  18. int failed;
  19. } selftest_results;
  20. #define selftest(result, fmt, ...) { \
  21. if (!(result)) { \
  22. selftest_results.failed++; \
  23. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  24. } else { \
  25. selftest_results.passed++; \
  26. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  27. } \
  28. }
  29. static void __init of_selftest_find_node_by_name(void)
  30. {
  31. struct device_node *np;
  32. np = of_find_node_by_path("/testcase-data");
  33. selftest(np && !strcmp("/testcase-data", np->full_name),
  34. "find /testcase-data failed\n");
  35. of_node_put(np);
  36. /* Test if trailing '/' works */
  37. np = of_find_node_by_path("/testcase-data/");
  38. selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
  39. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  40. selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  41. "find /testcase-data/phandle-tests/consumer-a failed\n");
  42. of_node_put(np);
  43. np = of_find_node_by_path("testcase-alias");
  44. selftest(np && !strcmp("/testcase-data", np->full_name),
  45. "find testcase-alias failed\n");
  46. of_node_put(np);
  47. /* Test if trailing '/' works on aliases */
  48. np = of_find_node_by_path("testcase-alias/");
  49. selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
  50. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  51. selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  52. "find testcase-alias/phandle-tests/consumer-a failed\n");
  53. of_node_put(np);
  54. np = of_find_node_by_path("/testcase-data/missing-path");
  55. selftest(!np, "non-existent path returned node %s\n", np->full_name);
  56. of_node_put(np);
  57. np = of_find_node_by_path("missing-alias");
  58. selftest(!np, "non-existent alias returned node %s\n", np->full_name);
  59. of_node_put(np);
  60. np = of_find_node_by_path("testcase-alias/missing-path");
  61. selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
  62. of_node_put(np);
  63. }
  64. static void __init of_selftest_dynamic(void)
  65. {
  66. struct device_node *np;
  67. struct property *prop;
  68. np = of_find_node_by_path("/testcase-data");
  69. if (!np) {
  70. pr_err("missing testcase data\n");
  71. return;
  72. }
  73. /* Array of 4 properties for the purpose of testing */
  74. prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
  75. if (!prop) {
  76. selftest(0, "kzalloc() failed\n");
  77. return;
  78. }
  79. /* Add a new property - should pass*/
  80. prop->name = "new-property";
  81. prop->value = "new-property-data";
  82. prop->length = strlen(prop->value);
  83. selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  84. /* Try to add an existing property - should fail */
  85. prop++;
  86. prop->name = "new-property";
  87. prop->value = "new-property-data-should-fail";
  88. prop->length = strlen(prop->value);
  89. selftest(of_add_property(np, prop) != 0,
  90. "Adding an existing property should have failed\n");
  91. /* Try to modify an existing property - should pass */
  92. prop->value = "modify-property-data-should-pass";
  93. prop->length = strlen(prop->value);
  94. selftest(of_update_property(np, prop) == 0,
  95. "Updating an existing property should have passed\n");
  96. /* Try to modify non-existent property - should pass*/
  97. prop++;
  98. prop->name = "modify-property";
  99. prop->value = "modify-missing-property-data-should-pass";
  100. prop->length = strlen(prop->value);
  101. selftest(of_update_property(np, prop) == 0,
  102. "Updating a missing property should have passed\n");
  103. /* Remove property - should pass */
  104. selftest(of_remove_property(np, prop) == 0,
  105. "Removing a property should have passed\n");
  106. /* Adding very large property - should pass */
  107. prop++;
  108. prop->name = "large-property-PAGE_SIZEx8";
  109. prop->length = PAGE_SIZE * 8;
  110. prop->value = kzalloc(prop->length, GFP_KERNEL);
  111. selftest(prop->value != NULL, "Unable to allocate large buffer\n");
  112. if (prop->value)
  113. selftest(of_add_property(np, prop) == 0,
  114. "Adding a large property should have passed\n");
  115. }
  116. static void __init of_selftest_parse_phandle_with_args(void)
  117. {
  118. struct device_node *np;
  119. struct of_phandle_args args;
  120. int i, rc;
  121. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  122. if (!np) {
  123. pr_err("missing testcase data\n");
  124. return;
  125. }
  126. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  127. selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  128. for (i = 0; i < 8; i++) {
  129. bool passed = true;
  130. rc = of_parse_phandle_with_args(np, "phandle-list",
  131. "#phandle-cells", i, &args);
  132. /* Test the values from tests-phandle.dtsi */
  133. switch (i) {
  134. case 0:
  135. passed &= !rc;
  136. passed &= (args.args_count == 1);
  137. passed &= (args.args[0] == (i + 1));
  138. break;
  139. case 1:
  140. passed &= !rc;
  141. passed &= (args.args_count == 2);
  142. passed &= (args.args[0] == (i + 1));
  143. passed &= (args.args[1] == 0);
  144. break;
  145. case 2:
  146. passed &= (rc == -ENOENT);
  147. break;
  148. case 3:
  149. passed &= !rc;
  150. passed &= (args.args_count == 3);
  151. passed &= (args.args[0] == (i + 1));
  152. passed &= (args.args[1] == 4);
  153. passed &= (args.args[2] == 3);
  154. break;
  155. case 4:
  156. passed &= !rc;
  157. passed &= (args.args_count == 2);
  158. passed &= (args.args[0] == (i + 1));
  159. passed &= (args.args[1] == 100);
  160. break;
  161. case 5:
  162. passed &= !rc;
  163. passed &= (args.args_count == 0);
  164. break;
  165. case 6:
  166. passed &= !rc;
  167. passed &= (args.args_count == 1);
  168. passed &= (args.args[0] == (i + 1));
  169. break;
  170. case 7:
  171. passed &= (rc == -ENOENT);
  172. break;
  173. default:
  174. passed = false;
  175. }
  176. selftest(passed, "index %i - data error on node %s rc=%i\n",
  177. i, args.np->full_name, rc);
  178. }
  179. /* Check for missing list property */
  180. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  181. "#phandle-cells", 0, &args);
  182. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  183. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  184. "#phandle-cells");
  185. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  186. /* Check for missing cells property */
  187. rc = of_parse_phandle_with_args(np, "phandle-list",
  188. "#phandle-cells-missing", 0, &args);
  189. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  190. rc = of_count_phandle_with_args(np, "phandle-list",
  191. "#phandle-cells-missing");
  192. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  193. /* Check for bad phandle in list */
  194. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  195. "#phandle-cells", 0, &args);
  196. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  197. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  198. "#phandle-cells");
  199. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  200. /* Check for incorrectly formed argument list */
  201. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  202. "#phandle-cells", 1, &args);
  203. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  204. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  205. "#phandle-cells");
  206. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  207. }
  208. static void __init of_selftest_property_match_string(void)
  209. {
  210. struct device_node *np;
  211. int rc;
  212. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  213. if (!np) {
  214. pr_err("No testcase data in device tree\n");
  215. return;
  216. }
  217. rc = of_property_match_string(np, "phandle-list-names", "first");
  218. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  219. rc = of_property_match_string(np, "phandle-list-names", "second");
  220. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  221. rc = of_property_match_string(np, "phandle-list-names", "third");
  222. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  223. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  224. selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
  225. rc = of_property_match_string(np, "missing-property", "blah");
  226. selftest(rc == -EINVAL, "missing property; rc=%i", rc);
  227. rc = of_property_match_string(np, "empty-property", "blah");
  228. selftest(rc == -ENODATA, "empty property; rc=%i", rc);
  229. rc = of_property_match_string(np, "unterminated-string", "blah");
  230. selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
  231. }
  232. static void __init of_selftest_parse_interrupts(void)
  233. {
  234. struct device_node *np;
  235. struct of_phandle_args args;
  236. int i, rc;
  237. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  238. if (!np) {
  239. pr_err("missing testcase data\n");
  240. return;
  241. }
  242. for (i = 0; i < 4; i++) {
  243. bool passed = true;
  244. args.args_count = 0;
  245. rc = of_irq_parse_one(np, i, &args);
  246. passed &= !rc;
  247. passed &= (args.args_count == 1);
  248. passed &= (args.args[0] == (i + 1));
  249. selftest(passed, "index %i - data error on node %s rc=%i\n",
  250. i, args.np->full_name, rc);
  251. }
  252. of_node_put(np);
  253. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  254. if (!np) {
  255. pr_err("missing testcase data\n");
  256. return;
  257. }
  258. for (i = 0; i < 4; i++) {
  259. bool passed = true;
  260. args.args_count = 0;
  261. rc = of_irq_parse_one(np, i, &args);
  262. /* Test the values from tests-phandle.dtsi */
  263. switch (i) {
  264. case 0:
  265. passed &= !rc;
  266. passed &= (args.args_count == 1);
  267. passed &= (args.args[0] == 9);
  268. break;
  269. case 1:
  270. passed &= !rc;
  271. passed &= (args.args_count == 3);
  272. passed &= (args.args[0] == 10);
  273. passed &= (args.args[1] == 11);
  274. passed &= (args.args[2] == 12);
  275. break;
  276. case 2:
  277. passed &= !rc;
  278. passed &= (args.args_count == 2);
  279. passed &= (args.args[0] == 13);
  280. passed &= (args.args[1] == 14);
  281. break;
  282. case 3:
  283. passed &= !rc;
  284. passed &= (args.args_count == 2);
  285. passed &= (args.args[0] == 15);
  286. passed &= (args.args[1] == 16);
  287. break;
  288. default:
  289. passed = false;
  290. }
  291. selftest(passed, "index %i - data error on node %s rc=%i\n",
  292. i, args.np->full_name, rc);
  293. }
  294. of_node_put(np);
  295. }
  296. static void __init of_selftest_parse_interrupts_extended(void)
  297. {
  298. struct device_node *np;
  299. struct of_phandle_args args;
  300. int i, rc;
  301. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  302. if (!np) {
  303. pr_err("missing testcase data\n");
  304. return;
  305. }
  306. for (i = 0; i < 7; i++) {
  307. bool passed = true;
  308. rc = of_irq_parse_one(np, i, &args);
  309. /* Test the values from tests-phandle.dtsi */
  310. switch (i) {
  311. case 0:
  312. passed &= !rc;
  313. passed &= (args.args_count == 1);
  314. passed &= (args.args[0] == 1);
  315. break;
  316. case 1:
  317. passed &= !rc;
  318. passed &= (args.args_count == 3);
  319. passed &= (args.args[0] == 2);
  320. passed &= (args.args[1] == 3);
  321. passed &= (args.args[2] == 4);
  322. break;
  323. case 2:
  324. passed &= !rc;
  325. passed &= (args.args_count == 2);
  326. passed &= (args.args[0] == 5);
  327. passed &= (args.args[1] == 6);
  328. break;
  329. case 3:
  330. passed &= !rc;
  331. passed &= (args.args_count == 1);
  332. passed &= (args.args[0] == 9);
  333. break;
  334. case 4:
  335. passed &= !rc;
  336. passed &= (args.args_count == 3);
  337. passed &= (args.args[0] == 10);
  338. passed &= (args.args[1] == 11);
  339. passed &= (args.args[2] == 12);
  340. break;
  341. case 5:
  342. passed &= !rc;
  343. passed &= (args.args_count == 2);
  344. passed &= (args.args[0] == 13);
  345. passed &= (args.args[1] == 14);
  346. break;
  347. case 6:
  348. passed &= !rc;
  349. passed &= (args.args_count == 1);
  350. passed &= (args.args[0] == 15);
  351. break;
  352. default:
  353. passed = false;
  354. }
  355. selftest(passed, "index %i - data error on node %s rc=%i\n",
  356. i, args.np->full_name, rc);
  357. }
  358. of_node_put(np);
  359. }
  360. static struct of_device_id match_node_table[] = {
  361. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  362. { .data = "B", .type = "type1", }, /* followed by type alone */
  363. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  364. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  365. { .data = "Cc", .name = "name2", .type = "type2", },
  366. { .data = "E", .compatible = "compat3" },
  367. { .data = "G", .compatible = "compat2", },
  368. { .data = "H", .compatible = "compat2", .name = "name5", },
  369. { .data = "I", .compatible = "compat2", .type = "type1", },
  370. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  371. { .data = "K", .compatible = "compat2", .name = "name9", },
  372. {}
  373. };
  374. static struct {
  375. const char *path;
  376. const char *data;
  377. } match_node_tests[] = {
  378. { .path = "/testcase-data/match-node/name0", .data = "A", },
  379. { .path = "/testcase-data/match-node/name1", .data = "B", },
  380. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  381. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  382. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  383. { .path = "/testcase-data/match-node/name3", .data = "E", },
  384. { .path = "/testcase-data/match-node/name4", .data = "G", },
  385. { .path = "/testcase-data/match-node/name5", .data = "H", },
  386. { .path = "/testcase-data/match-node/name6", .data = "G", },
  387. { .path = "/testcase-data/match-node/name7", .data = "I", },
  388. { .path = "/testcase-data/match-node/name8", .data = "J", },
  389. { .path = "/testcase-data/match-node/name9", .data = "K", },
  390. };
  391. static void __init of_selftest_match_node(void)
  392. {
  393. struct device_node *np;
  394. const struct of_device_id *match;
  395. int i;
  396. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  397. np = of_find_node_by_path(match_node_tests[i].path);
  398. if (!np) {
  399. selftest(0, "missing testcase node %s\n",
  400. match_node_tests[i].path);
  401. continue;
  402. }
  403. match = of_match_node(match_node_table, np);
  404. if (!match) {
  405. selftest(0, "%s didn't match anything\n",
  406. match_node_tests[i].path);
  407. continue;
  408. }
  409. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  410. selftest(0, "%s got wrong match. expected %s, got %s\n",
  411. match_node_tests[i].path, match_node_tests[i].data,
  412. (const char *)match->data);
  413. continue;
  414. }
  415. selftest(1, "passed");
  416. }
  417. }
  418. static void __init of_selftest_platform_populate(void)
  419. {
  420. int irq;
  421. struct device_node *np, *child;
  422. struct platform_device *pdev;
  423. struct of_device_id match[] = {
  424. { .compatible = "test-device", },
  425. {}
  426. };
  427. np = of_find_node_by_path("/testcase-data");
  428. of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
  429. /* Test that a missing irq domain returns -EPROBE_DEFER */
  430. np = of_find_node_by_path("/testcase-data/testcase-device1");
  431. pdev = of_find_device_by_node(np);
  432. selftest(pdev, "device 1 creation failed\n");
  433. irq = platform_get_irq(pdev, 0);
  434. selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
  435. /* Test that a parsing failure does not return -EPROBE_DEFER */
  436. np = of_find_node_by_path("/testcase-data/testcase-device2");
  437. pdev = of_find_device_by_node(np);
  438. selftest(pdev, "device 2 creation failed\n");
  439. irq = platform_get_irq(pdev, 0);
  440. selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
  441. np = of_find_node_by_path("/testcase-data/platform-tests");
  442. if (!np) {
  443. pr_err("No testcase data in device tree\n");
  444. return;
  445. }
  446. for_each_child_of_node(np, child) {
  447. struct device_node *grandchild;
  448. of_platform_populate(child, match, NULL, NULL);
  449. for_each_child_of_node(child, grandchild)
  450. selftest(of_find_device_by_node(grandchild),
  451. "Could not create device for node '%s'\n",
  452. grandchild->name);
  453. }
  454. }
  455. static int __init of_selftest(void)
  456. {
  457. struct device_node *np;
  458. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  459. if (!np) {
  460. pr_info("No testcase data in device tree; not running tests\n");
  461. return 0;
  462. }
  463. of_node_put(np);
  464. pr_info("start of selftest - you will see error messages\n");
  465. of_selftest_find_node_by_name();
  466. of_selftest_dynamic();
  467. of_selftest_parse_phandle_with_args();
  468. of_selftest_property_match_string();
  469. of_selftest_parse_interrupts();
  470. of_selftest_parse_interrupts_extended();
  471. of_selftest_match_node();
  472. of_selftest_platform_populate();
  473. pr_info("end of selftest - %i passed, %i failed\n",
  474. selftest_results.passed, selftest_results.failed);
  475. return 0;
  476. }
  477. late_initcall(of_selftest);