selftest.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/list.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. static struct selftest_results {
  16. int passed;
  17. int failed;
  18. } selftest_results;
  19. #define selftest(result, fmt, ...) { \
  20. if (!(result)) { \
  21. selftest_results.failed++; \
  22. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  23. } else { \
  24. selftest_results.passed++; \
  25. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  26. } \
  27. }
  28. static void __init of_selftest_parse_phandle_with_args(void)
  29. {
  30. struct device_node *np;
  31. struct of_phandle_args args;
  32. int i, rc;
  33. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  34. if (!np) {
  35. pr_err("missing testcase data\n");
  36. return;
  37. }
  38. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  39. selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  40. for (i = 0; i < 8; i++) {
  41. bool passed = true;
  42. rc = of_parse_phandle_with_args(np, "phandle-list",
  43. "#phandle-cells", i, &args);
  44. /* Test the values from tests-phandle.dtsi */
  45. switch (i) {
  46. case 0:
  47. passed &= !rc;
  48. passed &= (args.args_count == 1);
  49. passed &= (args.args[0] == (i + 1));
  50. break;
  51. case 1:
  52. passed &= !rc;
  53. passed &= (args.args_count == 2);
  54. passed &= (args.args[0] == (i + 1));
  55. passed &= (args.args[1] == 0);
  56. break;
  57. case 2:
  58. passed &= (rc == -ENOENT);
  59. break;
  60. case 3:
  61. passed &= !rc;
  62. passed &= (args.args_count == 3);
  63. passed &= (args.args[0] == (i + 1));
  64. passed &= (args.args[1] == 4);
  65. passed &= (args.args[2] == 3);
  66. break;
  67. case 4:
  68. passed &= !rc;
  69. passed &= (args.args_count == 2);
  70. passed &= (args.args[0] == (i + 1));
  71. passed &= (args.args[1] == 100);
  72. break;
  73. case 5:
  74. passed &= !rc;
  75. passed &= (args.args_count == 0);
  76. break;
  77. case 6:
  78. passed &= !rc;
  79. passed &= (args.args_count == 1);
  80. passed &= (args.args[0] == (i + 1));
  81. break;
  82. case 7:
  83. passed &= (rc == -ENOENT);
  84. break;
  85. default:
  86. passed = false;
  87. }
  88. selftest(passed, "index %i - data error on node %s rc=%i\n",
  89. i, args.np->full_name, rc);
  90. }
  91. /* Check for missing list property */
  92. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  93. "#phandle-cells", 0, &args);
  94. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  95. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  96. "#phandle-cells");
  97. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  98. /* Check for missing cells property */
  99. rc = of_parse_phandle_with_args(np, "phandle-list",
  100. "#phandle-cells-missing", 0, &args);
  101. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  102. rc = of_count_phandle_with_args(np, "phandle-list",
  103. "#phandle-cells-missing");
  104. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  105. /* Check for bad phandle in list */
  106. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  107. "#phandle-cells", 0, &args);
  108. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  109. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  110. "#phandle-cells");
  111. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  112. /* Check for incorrectly formed argument list */
  113. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  114. "#phandle-cells", 1, &args);
  115. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  116. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  117. "#phandle-cells");
  118. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  119. }
  120. static void __init of_selftest_property_match_string(void)
  121. {
  122. struct device_node *np;
  123. int rc;
  124. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  125. if (!np) {
  126. pr_err("No testcase data in device tree\n");
  127. return;
  128. }
  129. rc = of_property_match_string(np, "phandle-list-names", "first");
  130. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  131. rc = of_property_match_string(np, "phandle-list-names", "second");
  132. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  133. rc = of_property_match_string(np, "phandle-list-names", "third");
  134. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  135. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  136. selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
  137. rc = of_property_match_string(np, "missing-property", "blah");
  138. selftest(rc == -EINVAL, "missing property; rc=%i", rc);
  139. rc = of_property_match_string(np, "empty-property", "blah");
  140. selftest(rc == -ENODATA, "empty property; rc=%i", rc);
  141. rc = of_property_match_string(np, "unterminated-string", "blah");
  142. selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
  143. }
  144. static void __init of_selftest_parse_interrupts(void)
  145. {
  146. struct device_node *np;
  147. struct of_phandle_args args;
  148. int i, rc;
  149. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  150. if (!np) {
  151. pr_err("missing testcase data\n");
  152. return;
  153. }
  154. for (i = 0; i < 4; i++) {
  155. bool passed = true;
  156. args.args_count = 0;
  157. rc = of_irq_parse_one(np, i, &args);
  158. passed &= !rc;
  159. passed &= (args.args_count == 1);
  160. passed &= (args.args[0] == (i + 1));
  161. selftest(passed, "index %i - data error on node %s rc=%i\n",
  162. i, args.np->full_name, rc);
  163. }
  164. of_node_put(np);
  165. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  166. if (!np) {
  167. pr_err("missing testcase data\n");
  168. return;
  169. }
  170. for (i = 0; i < 4; i++) {
  171. bool passed = true;
  172. args.args_count = 0;
  173. rc = of_irq_parse_one(np, i, &args);
  174. /* Test the values from tests-phandle.dtsi */
  175. switch (i) {
  176. case 0:
  177. passed &= !rc;
  178. passed &= (args.args_count == 1);
  179. passed &= (args.args[0] == 9);
  180. break;
  181. case 1:
  182. passed &= !rc;
  183. passed &= (args.args_count == 3);
  184. passed &= (args.args[0] == 10);
  185. passed &= (args.args[1] == 11);
  186. passed &= (args.args[2] == 12);
  187. break;
  188. case 2:
  189. passed &= !rc;
  190. passed &= (args.args_count == 2);
  191. passed &= (args.args[0] == 13);
  192. passed &= (args.args[1] == 14);
  193. break;
  194. case 3:
  195. passed &= !rc;
  196. passed &= (args.args_count == 2);
  197. passed &= (args.args[0] == 15);
  198. passed &= (args.args[1] == 16);
  199. break;
  200. default:
  201. passed = false;
  202. }
  203. selftest(passed, "index %i - data error on node %s rc=%i\n",
  204. i, args.np->full_name, rc);
  205. }
  206. of_node_put(np);
  207. }
  208. static int __init of_selftest(void)
  209. {
  210. struct device_node *np;
  211. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  212. if (!np) {
  213. pr_info("No testcase data in device tree; not running tests\n");
  214. return 0;
  215. }
  216. of_node_put(np);
  217. pr_info("start of selftest - you will see error messages\n");
  218. of_selftest_parse_phandle_with_args();
  219. of_selftest_property_match_string();
  220. of_selftest_parse_interrupts();
  221. pr_info("end of selftest - %i passed, %i failed\n",
  222. selftest_results.passed, selftest_results.failed);
  223. return 0;
  224. }
  225. late_initcall(of_selftest);