selftest.c 14 KB

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