selftest.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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_string(void)
  291. {
  292. const char *strings[4];
  293. struct device_node *np;
  294. int rc;
  295. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  296. if (!np) {
  297. pr_err("No testcase data in device tree\n");
  298. return;
  299. }
  300. rc = of_property_match_string(np, "phandle-list-names", "first");
  301. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  302. rc = of_property_match_string(np, "phandle-list-names", "second");
  303. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  304. rc = of_property_match_string(np, "phandle-list-names", "third");
  305. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  306. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  307. selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  308. rc = of_property_match_string(np, "missing-property", "blah");
  309. selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  310. rc = of_property_match_string(np, "empty-property", "blah");
  311. selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  312. rc = of_property_match_string(np, "unterminated-string", "blah");
  313. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  314. /* of_property_count_strings() tests */
  315. rc = of_property_count_strings(np, "string-property");
  316. selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  317. rc = of_property_count_strings(np, "phandle-list-names");
  318. selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  319. rc = of_property_count_strings(np, "unterminated-string");
  320. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  321. rc = of_property_count_strings(np, "unterminated-string-list");
  322. selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  323. /* of_property_read_string_index() tests */
  324. rc = of_property_read_string_index(np, "string-property", 0, strings);
  325. selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  326. strings[0] = NULL;
  327. rc = of_property_read_string_index(np, "string-property", 1, strings);
  328. selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  329. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  330. selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  331. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  332. selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  333. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  334. selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  335. strings[0] = NULL;
  336. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  337. selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  338. strings[0] = NULL;
  339. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  340. selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  341. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  342. selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  343. strings[0] = NULL;
  344. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  345. selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  346. strings[1] = NULL;
  347. /* of_property_read_string_array() tests */
  348. rc = of_property_read_string_array(np, "string-property", strings, 4);
  349. selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  350. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  351. selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  352. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  353. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  354. /* -- An incorrectly formed string should cause a failure */
  355. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  356. selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  357. /* -- parsing the correctly formed strings should still work: */
  358. strings[2] = NULL;
  359. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  360. selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  361. strings[1] = NULL;
  362. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  363. selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  364. }
  365. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  366. (p1)->value && (p2)->value && \
  367. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  368. !strcmp((p1)->name, (p2)->name))
  369. static void __init of_selftest_property_copy(void)
  370. {
  371. #ifdef CONFIG_OF_DYNAMIC
  372. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  373. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  374. struct property *new;
  375. new = __of_prop_dup(&p1, GFP_KERNEL);
  376. selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  377. kfree(new->value);
  378. kfree(new->name);
  379. kfree(new);
  380. new = __of_prop_dup(&p2, GFP_KERNEL);
  381. selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  382. kfree(new->value);
  383. kfree(new->name);
  384. kfree(new);
  385. #endif
  386. }
  387. static void __init of_selftest_changeset(void)
  388. {
  389. #ifdef CONFIG_OF_DYNAMIC
  390. struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
  391. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  392. struct property *ppremove;
  393. struct device_node *n1, *n2, *n21, *nremove, *parent;
  394. struct of_changeset chgset;
  395. of_changeset_init(&chgset);
  396. n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
  397. selftest(n1, "testcase setup failure\n");
  398. n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
  399. selftest(n2, "testcase setup failure\n");
  400. n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
  401. selftest(n21, "testcase setup failure %p\n", n21);
  402. nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
  403. selftest(nremove, "testcase setup failure\n");
  404. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  405. selftest(ppadd, "testcase setup failure\n");
  406. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  407. selftest(ppupdate, "testcase setup failure\n");
  408. parent = nremove->parent;
  409. n1->parent = parent;
  410. n2->parent = parent;
  411. n21->parent = n2;
  412. n2->child = n21;
  413. ppremove = of_find_property(parent, "prop-remove", NULL);
  414. selftest(ppremove, "failed to find removal prop");
  415. of_changeset_init(&chgset);
  416. selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  417. selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  418. selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  419. selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  420. selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
  421. selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  422. selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  423. mutex_lock(&of_mutex);
  424. selftest(!of_changeset_apply(&chgset), "apply failed\n");
  425. mutex_unlock(&of_mutex);
  426. mutex_lock(&of_mutex);
  427. selftest(!of_changeset_revert(&chgset), "revert failed\n");
  428. mutex_unlock(&of_mutex);
  429. of_changeset_destroy(&chgset);
  430. #endif
  431. }
  432. static void __init of_selftest_parse_interrupts(void)
  433. {
  434. struct device_node *np;
  435. struct of_phandle_args args;
  436. int i, rc;
  437. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  438. if (!np) {
  439. pr_err("missing testcase data\n");
  440. return;
  441. }
  442. for (i = 0; i < 4; i++) {
  443. bool passed = true;
  444. args.args_count = 0;
  445. rc = of_irq_parse_one(np, i, &args);
  446. passed &= !rc;
  447. passed &= (args.args_count == 1);
  448. passed &= (args.args[0] == (i + 1));
  449. selftest(passed, "index %i - data error on node %s rc=%i\n",
  450. i, args.np->full_name, rc);
  451. }
  452. of_node_put(np);
  453. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  454. if (!np) {
  455. pr_err("missing testcase data\n");
  456. return;
  457. }
  458. for (i = 0; i < 4; i++) {
  459. bool passed = true;
  460. args.args_count = 0;
  461. rc = of_irq_parse_one(np, i, &args);
  462. /* Test the values from tests-phandle.dtsi */
  463. switch (i) {
  464. case 0:
  465. passed &= !rc;
  466. passed &= (args.args_count == 1);
  467. passed &= (args.args[0] == 9);
  468. break;
  469. case 1:
  470. passed &= !rc;
  471. passed &= (args.args_count == 3);
  472. passed &= (args.args[0] == 10);
  473. passed &= (args.args[1] == 11);
  474. passed &= (args.args[2] == 12);
  475. break;
  476. case 2:
  477. passed &= !rc;
  478. passed &= (args.args_count == 2);
  479. passed &= (args.args[0] == 13);
  480. passed &= (args.args[1] == 14);
  481. break;
  482. case 3:
  483. passed &= !rc;
  484. passed &= (args.args_count == 2);
  485. passed &= (args.args[0] == 15);
  486. passed &= (args.args[1] == 16);
  487. break;
  488. default:
  489. passed = false;
  490. }
  491. selftest(passed, "index %i - data error on node %s rc=%i\n",
  492. i, args.np->full_name, rc);
  493. }
  494. of_node_put(np);
  495. }
  496. static void __init of_selftest_parse_interrupts_extended(void)
  497. {
  498. struct device_node *np;
  499. struct of_phandle_args args;
  500. int i, rc;
  501. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  502. if (!np) {
  503. pr_err("missing testcase data\n");
  504. return;
  505. }
  506. for (i = 0; i < 7; i++) {
  507. bool passed = true;
  508. rc = of_irq_parse_one(np, i, &args);
  509. /* Test the values from tests-phandle.dtsi */
  510. switch (i) {
  511. case 0:
  512. passed &= !rc;
  513. passed &= (args.args_count == 1);
  514. passed &= (args.args[0] == 1);
  515. break;
  516. case 1:
  517. passed &= !rc;
  518. passed &= (args.args_count == 3);
  519. passed &= (args.args[0] == 2);
  520. passed &= (args.args[1] == 3);
  521. passed &= (args.args[2] == 4);
  522. break;
  523. case 2:
  524. passed &= !rc;
  525. passed &= (args.args_count == 2);
  526. passed &= (args.args[0] == 5);
  527. passed &= (args.args[1] == 6);
  528. break;
  529. case 3:
  530. passed &= !rc;
  531. passed &= (args.args_count == 1);
  532. passed &= (args.args[0] == 9);
  533. break;
  534. case 4:
  535. passed &= !rc;
  536. passed &= (args.args_count == 3);
  537. passed &= (args.args[0] == 10);
  538. passed &= (args.args[1] == 11);
  539. passed &= (args.args[2] == 12);
  540. break;
  541. case 5:
  542. passed &= !rc;
  543. passed &= (args.args_count == 2);
  544. passed &= (args.args[0] == 13);
  545. passed &= (args.args[1] == 14);
  546. break;
  547. case 6:
  548. passed &= !rc;
  549. passed &= (args.args_count == 1);
  550. passed &= (args.args[0] == 15);
  551. break;
  552. default:
  553. passed = false;
  554. }
  555. selftest(passed, "index %i - data error on node %s rc=%i\n",
  556. i, args.np->full_name, rc);
  557. }
  558. of_node_put(np);
  559. }
  560. static struct of_device_id match_node_table[] = {
  561. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  562. { .data = "B", .type = "type1", }, /* followed by type alone */
  563. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  564. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  565. { .data = "Cc", .name = "name2", .type = "type2", },
  566. { .data = "E", .compatible = "compat3" },
  567. { .data = "G", .compatible = "compat2", },
  568. { .data = "H", .compatible = "compat2", .name = "name5", },
  569. { .data = "I", .compatible = "compat2", .type = "type1", },
  570. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  571. { .data = "K", .compatible = "compat2", .name = "name9", },
  572. {}
  573. };
  574. static struct {
  575. const char *path;
  576. const char *data;
  577. } match_node_tests[] = {
  578. { .path = "/testcase-data/match-node/name0", .data = "A", },
  579. { .path = "/testcase-data/match-node/name1", .data = "B", },
  580. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  581. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  582. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  583. { .path = "/testcase-data/match-node/name3", .data = "E", },
  584. { .path = "/testcase-data/match-node/name4", .data = "G", },
  585. { .path = "/testcase-data/match-node/name5", .data = "H", },
  586. { .path = "/testcase-data/match-node/name6", .data = "G", },
  587. { .path = "/testcase-data/match-node/name7", .data = "I", },
  588. { .path = "/testcase-data/match-node/name8", .data = "J", },
  589. { .path = "/testcase-data/match-node/name9", .data = "K", },
  590. };
  591. static void __init of_selftest_match_node(void)
  592. {
  593. struct device_node *np;
  594. const struct of_device_id *match;
  595. int i;
  596. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  597. np = of_find_node_by_path(match_node_tests[i].path);
  598. if (!np) {
  599. selftest(0, "missing testcase node %s\n",
  600. match_node_tests[i].path);
  601. continue;
  602. }
  603. match = of_match_node(match_node_table, np);
  604. if (!match) {
  605. selftest(0, "%s didn't match anything\n",
  606. match_node_tests[i].path);
  607. continue;
  608. }
  609. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  610. selftest(0, "%s got wrong match. expected %s, got %s\n",
  611. match_node_tests[i].path, match_node_tests[i].data,
  612. (const char *)match->data);
  613. continue;
  614. }
  615. selftest(1, "passed");
  616. }
  617. }
  618. static void __init of_selftest_platform_populate(void)
  619. {
  620. int irq;
  621. struct device_node *np, *child;
  622. struct platform_device *pdev;
  623. struct of_device_id match[] = {
  624. { .compatible = "test-device", },
  625. {}
  626. };
  627. np = of_find_node_by_path("/testcase-data");
  628. of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
  629. /* Test that a missing irq domain returns -EPROBE_DEFER */
  630. np = of_find_node_by_path("/testcase-data/testcase-device1");
  631. pdev = of_find_device_by_node(np);
  632. selftest(pdev, "device 1 creation failed\n");
  633. irq = platform_get_irq(pdev, 0);
  634. selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
  635. /* Test that a parsing failure does not return -EPROBE_DEFER */
  636. np = of_find_node_by_path("/testcase-data/testcase-device2");
  637. pdev = of_find_device_by_node(np);
  638. selftest(pdev, "device 2 creation failed\n");
  639. irq = platform_get_irq(pdev, 0);
  640. selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
  641. np = of_find_node_by_path("/testcase-data/platform-tests");
  642. if (!np) {
  643. pr_err("No testcase data in device tree\n");
  644. return;
  645. }
  646. for_each_child_of_node(np, child) {
  647. struct device_node *grandchild;
  648. of_platform_populate(child, match, NULL, NULL);
  649. for_each_child_of_node(child, grandchild)
  650. selftest(of_find_device_by_node(grandchild),
  651. "Could not create device for node '%s'\n",
  652. grandchild->name);
  653. }
  654. }
  655. /**
  656. * update_node_properties - adds the properties
  657. * of np into dup node (present in live tree) and
  658. * updates parent of children of np to dup.
  659. *
  660. * @np: node already present in live tree
  661. * @dup: node present in live tree to be updated
  662. */
  663. static void update_node_properties(struct device_node *np,
  664. struct device_node *dup)
  665. {
  666. struct property *prop;
  667. struct device_node *child;
  668. for_each_property_of_node(np, prop)
  669. of_add_property(dup, prop);
  670. for_each_child_of_node(np, child)
  671. child->parent = dup;
  672. }
  673. /**
  674. * attach_node_and_children - attaches nodes
  675. * and its children to live tree
  676. *
  677. * @np: Node to attach to live tree
  678. */
  679. static int attach_node_and_children(struct device_node *np)
  680. {
  681. struct device_node *next, *root = np, *dup;
  682. /* skip root node */
  683. np = np->child;
  684. /* storing a copy in temporary node */
  685. dup = np;
  686. while (dup) {
  687. if (WARN_ON(last_node_index >= NO_OF_NODES))
  688. return -EINVAL;
  689. nodes[last_node_index++] = dup;
  690. dup = dup->sibling;
  691. }
  692. dup = NULL;
  693. while (np) {
  694. next = np->allnext;
  695. dup = of_find_node_by_path(np->full_name);
  696. if (dup)
  697. update_node_properties(np, dup);
  698. else {
  699. np->child = NULL;
  700. if (np->parent == root)
  701. np->parent = of_allnodes;
  702. of_attach_node(np);
  703. }
  704. np = next;
  705. }
  706. return 0;
  707. }
  708. /**
  709. * selftest_data_add - Reads, copies data from
  710. * linked tree and attaches it to the live tree
  711. */
  712. static int __init selftest_data_add(void)
  713. {
  714. void *selftest_data;
  715. struct device_node *selftest_data_node, *np;
  716. extern uint8_t __dtb_testcases_begin[];
  717. extern uint8_t __dtb_testcases_end[];
  718. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  719. int rc;
  720. if (!size) {
  721. pr_warn("%s: No testcase data to attach; not running tests\n",
  722. __func__);
  723. return -ENODATA;
  724. }
  725. /* creating copy */
  726. selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  727. if (!selftest_data) {
  728. pr_warn("%s: Failed to allocate memory for selftest_data; "
  729. "not running tests\n", __func__);
  730. return -ENOMEM;
  731. }
  732. of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
  733. if (!selftest_data_node) {
  734. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  735. return -ENODATA;
  736. }
  737. of_node_set_flag(selftest_data_node, OF_DETACHED);
  738. rc = of_resolve_phandles(selftest_data_node);
  739. if (rc) {
  740. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  741. return -EINVAL;
  742. }
  743. if (!of_allnodes) {
  744. /* enabling flag for removing nodes */
  745. selftest_live_tree = true;
  746. of_allnodes = selftest_data_node;
  747. for_each_of_allnodes(np)
  748. __of_attach_node_sysfs(np);
  749. of_aliases = of_find_node_by_path("/aliases");
  750. of_chosen = of_find_node_by_path("/chosen");
  751. return 0;
  752. }
  753. /* attach the sub-tree to live tree */
  754. return attach_node_and_children(selftest_data_node);
  755. }
  756. /**
  757. * detach_node_and_children - detaches node
  758. * and its children from live tree
  759. *
  760. * @np: Node to detach from live tree
  761. */
  762. static void detach_node_and_children(struct device_node *np)
  763. {
  764. while (np->child)
  765. detach_node_and_children(np->child);
  766. of_detach_node(np);
  767. }
  768. /**
  769. * selftest_data_remove - removes the selftest data
  770. * nodes from the live tree
  771. */
  772. static void selftest_data_remove(void)
  773. {
  774. struct device_node *np;
  775. struct property *prop;
  776. if (selftest_live_tree) {
  777. of_node_put(of_aliases);
  778. of_node_put(of_chosen);
  779. of_aliases = NULL;
  780. of_chosen = NULL;
  781. for_each_child_of_node(of_allnodes, np)
  782. detach_node_and_children(np);
  783. __of_detach_node_sysfs(of_allnodes);
  784. of_allnodes = NULL;
  785. return;
  786. }
  787. while (last_node_index >= 0) {
  788. if (nodes[last_node_index]) {
  789. np = of_find_node_by_path(nodes[last_node_index]->full_name);
  790. if (strcmp(np->full_name, "/aliases") != 0) {
  791. detach_node_and_children(np);
  792. } else {
  793. for_each_property_of_node(np, prop) {
  794. if (strcmp(prop->name, "testcase-alias") == 0)
  795. of_remove_property(np, prop);
  796. }
  797. }
  798. }
  799. last_node_index--;
  800. }
  801. }
  802. static int __init of_selftest(void)
  803. {
  804. struct device_node *np;
  805. int res;
  806. /* adding data for selftest */
  807. res = selftest_data_add();
  808. if (res)
  809. return res;
  810. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  811. if (!np) {
  812. pr_info("No testcase data in device tree; not running tests\n");
  813. return 0;
  814. }
  815. of_node_put(np);
  816. pr_info("start of selftest - you will see error messages\n");
  817. of_selftest_check_tree_linkage();
  818. of_selftest_check_phandles();
  819. of_selftest_find_node_by_name();
  820. of_selftest_dynamic();
  821. of_selftest_parse_phandle_with_args();
  822. of_selftest_property_string();
  823. of_selftest_property_copy();
  824. of_selftest_changeset();
  825. of_selftest_parse_interrupts();
  826. of_selftest_parse_interrupts_extended();
  827. of_selftest_match_node();
  828. of_selftest_platform_populate();
  829. /* removing selftest data from live tree */
  830. selftest_data_remove();
  831. /* Double check linkage after removing testcase data */
  832. of_selftest_check_tree_linkage();
  833. pr_info("end of selftest - %i passed, %i failed\n",
  834. selftest_results.passed, selftest_results.failed);
  835. return 0;
  836. }
  837. late_initcall(of_selftest);