unittest.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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 <linux/platform_device.h>
  19. #include <linux/of_platform.h>
  20. #include "of_private.h"
  21. static struct selftest_results {
  22. int passed;
  23. int failed;
  24. } selftest_results;
  25. #define NO_OF_NODES 3
  26. static struct device_node *nodes[NO_OF_NODES];
  27. static int last_node_index;
  28. static bool selftest_live_tree;
  29. #define selftest(result, fmt, ...) ({ \
  30. bool failed = !(result); \
  31. if (failed) { \
  32. selftest_results.failed++; \
  33. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  34. } else { \
  35. selftest_results.passed++; \
  36. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  37. } \
  38. failed; \
  39. })
  40. static void __init of_selftest_find_node_by_name(void)
  41. {
  42. struct device_node *np;
  43. const char *options;
  44. np = of_find_node_by_path("/testcase-data");
  45. selftest(np && !strcmp("/testcase-data", np->full_name),
  46. "find /testcase-data failed\n");
  47. of_node_put(np);
  48. /* Test if trailing '/' works */
  49. np = of_find_node_by_path("/testcase-data/");
  50. selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
  51. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  52. selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  53. "find /testcase-data/phandle-tests/consumer-a failed\n");
  54. of_node_put(np);
  55. np = of_find_node_by_path("testcase-alias");
  56. selftest(np && !strcmp("/testcase-data", np->full_name),
  57. "find testcase-alias failed\n");
  58. of_node_put(np);
  59. /* Test if trailing '/' works on aliases */
  60. np = of_find_node_by_path("testcase-alias/");
  61. selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
  62. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  63. selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
  64. "find testcase-alias/phandle-tests/consumer-a failed\n");
  65. of_node_put(np);
  66. np = of_find_node_by_path("/testcase-data/missing-path");
  67. selftest(!np, "non-existent path returned node %s\n", np->full_name);
  68. of_node_put(np);
  69. np = of_find_node_by_path("missing-alias");
  70. selftest(!np, "non-existent alias returned node %s\n", np->full_name);
  71. of_node_put(np);
  72. np = of_find_node_by_path("testcase-alias/missing-path");
  73. selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
  74. of_node_put(np);
  75. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  76. selftest(np && !strcmp("testoption", options),
  77. "option path test failed\n");
  78. of_node_put(np);
  79. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  80. selftest(np, "NULL option path test failed\n");
  81. of_node_put(np);
  82. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  83. &options);
  84. selftest(np && !strcmp("testaliasoption", options),
  85. "option alias path test failed\n");
  86. of_node_put(np);
  87. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  88. selftest(np, "NULL option alias path test failed\n");
  89. of_node_put(np);
  90. options = "testoption";
  91. np = of_find_node_opts_by_path("testcase-alias", &options);
  92. selftest(np && !options, "option clearing test failed\n");
  93. of_node_put(np);
  94. options = "testoption";
  95. np = of_find_node_opts_by_path("/", &options);
  96. selftest(np && !options, "option clearing root node test failed\n");
  97. of_node_put(np);
  98. }
  99. static void __init of_selftest_dynamic(void)
  100. {
  101. struct device_node *np;
  102. struct property *prop;
  103. np = of_find_node_by_path("/testcase-data");
  104. if (!np) {
  105. pr_err("missing testcase data\n");
  106. return;
  107. }
  108. /* Array of 4 properties for the purpose of testing */
  109. prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
  110. if (!prop) {
  111. selftest(0, "kzalloc() failed\n");
  112. return;
  113. }
  114. /* Add a new property - should pass*/
  115. prop->name = "new-property";
  116. prop->value = "new-property-data";
  117. prop->length = strlen(prop->value);
  118. selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  119. /* Try to add an existing property - should fail */
  120. prop++;
  121. prop->name = "new-property";
  122. prop->value = "new-property-data-should-fail";
  123. prop->length = strlen(prop->value);
  124. selftest(of_add_property(np, prop) != 0,
  125. "Adding an existing property should have failed\n");
  126. /* Try to modify an existing property - should pass */
  127. prop->value = "modify-property-data-should-pass";
  128. prop->length = strlen(prop->value);
  129. selftest(of_update_property(np, prop) == 0,
  130. "Updating an existing property should have passed\n");
  131. /* Try to modify non-existent property - should pass*/
  132. prop++;
  133. prop->name = "modify-property";
  134. prop->value = "modify-missing-property-data-should-pass";
  135. prop->length = strlen(prop->value);
  136. selftest(of_update_property(np, prop) == 0,
  137. "Updating a missing property should have passed\n");
  138. /* Remove property - should pass */
  139. selftest(of_remove_property(np, prop) == 0,
  140. "Removing a property should have passed\n");
  141. /* Adding very large property - should pass */
  142. prop++;
  143. prop->name = "large-property-PAGE_SIZEx8";
  144. prop->length = PAGE_SIZE * 8;
  145. prop->value = kzalloc(prop->length, GFP_KERNEL);
  146. selftest(prop->value != NULL, "Unable to allocate large buffer\n");
  147. if (prop->value)
  148. selftest(of_add_property(np, prop) == 0,
  149. "Adding a large property should have passed\n");
  150. }
  151. static int __init of_selftest_check_node_linkage(struct device_node *np)
  152. {
  153. struct device_node *child;
  154. int count = 0, rc;
  155. for_each_child_of_node(np, child) {
  156. if (child->parent != np) {
  157. pr_err("Child node %s links to wrong parent %s\n",
  158. child->name, np->name);
  159. return -EINVAL;
  160. }
  161. rc = of_selftest_check_node_linkage(child);
  162. if (rc < 0)
  163. return rc;
  164. count += rc;
  165. }
  166. return count + 1;
  167. }
  168. static void __init of_selftest_check_tree_linkage(void)
  169. {
  170. struct device_node *np;
  171. int allnode_count = 0, child_count;
  172. if (!of_root)
  173. return;
  174. for_each_of_allnodes(np)
  175. allnode_count++;
  176. child_count = of_selftest_check_node_linkage(of_root);
  177. selftest(child_count > 0, "Device node data structure is corrupted\n");
  178. selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
  179. "sibling lists size (%i)\n", allnode_count, child_count);
  180. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  181. }
  182. struct node_hash {
  183. struct hlist_node node;
  184. struct device_node *np;
  185. };
  186. static DEFINE_HASHTABLE(phandle_ht, 8);
  187. static void __init of_selftest_check_phandles(void)
  188. {
  189. struct device_node *np;
  190. struct node_hash *nh;
  191. struct hlist_node *tmp;
  192. int i, dup_count = 0, phandle_count = 0;
  193. for_each_of_allnodes(np) {
  194. if (!np->phandle)
  195. continue;
  196. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  197. if (nh->np->phandle == np->phandle) {
  198. pr_info("Duplicate phandle! %i used by %s and %s\n",
  199. np->phandle, nh->np->full_name, np->full_name);
  200. dup_count++;
  201. break;
  202. }
  203. }
  204. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  205. if (WARN_ON(!nh))
  206. return;
  207. nh->np = np;
  208. hash_add(phandle_ht, &nh->node, np->phandle);
  209. phandle_count++;
  210. }
  211. selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  212. dup_count, phandle_count);
  213. /* Clean up */
  214. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  215. hash_del(&nh->node);
  216. kfree(nh);
  217. }
  218. }
  219. static void __init of_selftest_parse_phandle_with_args(void)
  220. {
  221. struct device_node *np;
  222. struct of_phandle_args args;
  223. int i, rc;
  224. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  225. if (!np) {
  226. pr_err("missing testcase data\n");
  227. return;
  228. }
  229. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  230. selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  231. for (i = 0; i < 8; i++) {
  232. bool passed = true;
  233. rc = of_parse_phandle_with_args(np, "phandle-list",
  234. "#phandle-cells", i, &args);
  235. /* Test the values from tests-phandle.dtsi */
  236. switch (i) {
  237. case 0:
  238. passed &= !rc;
  239. passed &= (args.args_count == 1);
  240. passed &= (args.args[0] == (i + 1));
  241. break;
  242. case 1:
  243. passed &= !rc;
  244. passed &= (args.args_count == 2);
  245. passed &= (args.args[0] == (i + 1));
  246. passed &= (args.args[1] == 0);
  247. break;
  248. case 2:
  249. passed &= (rc == -ENOENT);
  250. break;
  251. case 3:
  252. passed &= !rc;
  253. passed &= (args.args_count == 3);
  254. passed &= (args.args[0] == (i + 1));
  255. passed &= (args.args[1] == 4);
  256. passed &= (args.args[2] == 3);
  257. break;
  258. case 4:
  259. passed &= !rc;
  260. passed &= (args.args_count == 2);
  261. passed &= (args.args[0] == (i + 1));
  262. passed &= (args.args[1] == 100);
  263. break;
  264. case 5:
  265. passed &= !rc;
  266. passed &= (args.args_count == 0);
  267. break;
  268. case 6:
  269. passed &= !rc;
  270. passed &= (args.args_count == 1);
  271. passed &= (args.args[0] == (i + 1));
  272. break;
  273. case 7:
  274. passed &= (rc == -ENOENT);
  275. break;
  276. default:
  277. passed = false;
  278. }
  279. selftest(passed, "index %i - data error on node %s rc=%i\n",
  280. i, args.np->full_name, rc);
  281. }
  282. /* Check for missing list property */
  283. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  284. "#phandle-cells", 0, &args);
  285. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  286. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  287. "#phandle-cells");
  288. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  289. /* Check for missing cells property */
  290. rc = of_parse_phandle_with_args(np, "phandle-list",
  291. "#phandle-cells-missing", 0, &args);
  292. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  293. rc = of_count_phandle_with_args(np, "phandle-list",
  294. "#phandle-cells-missing");
  295. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  296. /* Check for bad phandle in list */
  297. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  298. "#phandle-cells", 0, &args);
  299. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  300. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  301. "#phandle-cells");
  302. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  303. /* Check for incorrectly formed argument list */
  304. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  305. "#phandle-cells", 1, &args);
  306. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  307. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  308. "#phandle-cells");
  309. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  310. }
  311. static void __init of_selftest_property_string(void)
  312. {
  313. const char *strings[4];
  314. struct device_node *np;
  315. int rc;
  316. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  317. if (!np) {
  318. pr_err("No testcase data in device tree\n");
  319. return;
  320. }
  321. rc = of_property_match_string(np, "phandle-list-names", "first");
  322. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  323. rc = of_property_match_string(np, "phandle-list-names", "second");
  324. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  325. rc = of_property_match_string(np, "phandle-list-names", "third");
  326. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  327. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  328. selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  329. rc = of_property_match_string(np, "missing-property", "blah");
  330. selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  331. rc = of_property_match_string(np, "empty-property", "blah");
  332. selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  333. rc = of_property_match_string(np, "unterminated-string", "blah");
  334. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  335. /* of_property_count_strings() tests */
  336. rc = of_property_count_strings(np, "string-property");
  337. selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  338. rc = of_property_count_strings(np, "phandle-list-names");
  339. selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  340. rc = of_property_count_strings(np, "unterminated-string");
  341. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  342. rc = of_property_count_strings(np, "unterminated-string-list");
  343. selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  344. /* of_property_read_string_index() tests */
  345. rc = of_property_read_string_index(np, "string-property", 0, strings);
  346. selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  347. strings[0] = NULL;
  348. rc = of_property_read_string_index(np, "string-property", 1, strings);
  349. selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  350. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  351. selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  352. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  353. selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  354. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  355. selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  356. strings[0] = NULL;
  357. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  358. selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  359. strings[0] = NULL;
  360. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  361. selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  362. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  363. selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  364. strings[0] = NULL;
  365. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  366. selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  367. strings[1] = NULL;
  368. /* of_property_read_string_array() tests */
  369. rc = of_property_read_string_array(np, "string-property", strings, 4);
  370. selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  371. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  372. selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  373. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  374. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  375. /* -- An incorrectly formed string should cause a failure */
  376. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  377. selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  378. /* -- parsing the correctly formed strings should still work: */
  379. strings[2] = NULL;
  380. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  381. selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  382. strings[1] = NULL;
  383. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  384. selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  385. }
  386. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  387. (p1)->value && (p2)->value && \
  388. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  389. !strcmp((p1)->name, (p2)->name))
  390. static void __init of_selftest_property_copy(void)
  391. {
  392. #ifdef CONFIG_OF_DYNAMIC
  393. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  394. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  395. struct property *new;
  396. new = __of_prop_dup(&p1, GFP_KERNEL);
  397. selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  398. kfree(new->value);
  399. kfree(new->name);
  400. kfree(new);
  401. new = __of_prop_dup(&p2, GFP_KERNEL);
  402. selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  403. kfree(new->value);
  404. kfree(new->name);
  405. kfree(new);
  406. #endif
  407. }
  408. static void __init of_selftest_changeset(void)
  409. {
  410. #ifdef CONFIG_OF_DYNAMIC
  411. struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
  412. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  413. struct property *ppremove;
  414. struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
  415. struct of_changeset chgset;
  416. of_changeset_init(&chgset);
  417. n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
  418. selftest(n1, "testcase setup failure\n");
  419. n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
  420. selftest(n2, "testcase setup failure\n");
  421. n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
  422. selftest(n21, "testcase setup failure %p\n", n21);
  423. nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
  424. selftest(nremove, "testcase setup failure\n");
  425. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  426. selftest(ppadd, "testcase setup failure\n");
  427. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  428. selftest(ppupdate, "testcase setup failure\n");
  429. parent = nremove->parent;
  430. n1->parent = parent;
  431. n2->parent = parent;
  432. n21->parent = n2;
  433. n2->child = n21;
  434. ppremove = of_find_property(parent, "prop-remove", NULL);
  435. selftest(ppremove, "failed to find removal prop");
  436. of_changeset_init(&chgset);
  437. selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  438. selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  439. selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  440. selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  441. selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
  442. selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  443. selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  444. mutex_lock(&of_mutex);
  445. selftest(!of_changeset_apply(&chgset), "apply failed\n");
  446. mutex_unlock(&of_mutex);
  447. /* Make sure node names are constructed correctly */
  448. selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  449. "'%s' not added\n", n21->full_name);
  450. of_node_put(np);
  451. mutex_lock(&of_mutex);
  452. selftest(!of_changeset_revert(&chgset), "revert failed\n");
  453. mutex_unlock(&of_mutex);
  454. of_changeset_destroy(&chgset);
  455. #endif
  456. }
  457. static void __init of_selftest_parse_interrupts(void)
  458. {
  459. struct device_node *np;
  460. struct of_phandle_args args;
  461. int i, rc;
  462. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  463. if (!np) {
  464. pr_err("missing testcase data\n");
  465. return;
  466. }
  467. for (i = 0; i < 4; i++) {
  468. bool passed = true;
  469. args.args_count = 0;
  470. rc = of_irq_parse_one(np, i, &args);
  471. passed &= !rc;
  472. passed &= (args.args_count == 1);
  473. passed &= (args.args[0] == (i + 1));
  474. selftest(passed, "index %i - data error on node %s rc=%i\n",
  475. i, args.np->full_name, rc);
  476. }
  477. of_node_put(np);
  478. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  479. if (!np) {
  480. pr_err("missing testcase data\n");
  481. return;
  482. }
  483. for (i = 0; i < 4; i++) {
  484. bool passed = true;
  485. args.args_count = 0;
  486. rc = of_irq_parse_one(np, i, &args);
  487. /* Test the values from tests-phandle.dtsi */
  488. switch (i) {
  489. case 0:
  490. passed &= !rc;
  491. passed &= (args.args_count == 1);
  492. passed &= (args.args[0] == 9);
  493. break;
  494. case 1:
  495. passed &= !rc;
  496. passed &= (args.args_count == 3);
  497. passed &= (args.args[0] == 10);
  498. passed &= (args.args[1] == 11);
  499. passed &= (args.args[2] == 12);
  500. break;
  501. case 2:
  502. passed &= !rc;
  503. passed &= (args.args_count == 2);
  504. passed &= (args.args[0] == 13);
  505. passed &= (args.args[1] == 14);
  506. break;
  507. case 3:
  508. passed &= !rc;
  509. passed &= (args.args_count == 2);
  510. passed &= (args.args[0] == 15);
  511. passed &= (args.args[1] == 16);
  512. break;
  513. default:
  514. passed = false;
  515. }
  516. selftest(passed, "index %i - data error on node %s rc=%i\n",
  517. i, args.np->full_name, rc);
  518. }
  519. of_node_put(np);
  520. }
  521. static void __init of_selftest_parse_interrupts_extended(void)
  522. {
  523. struct device_node *np;
  524. struct of_phandle_args args;
  525. int i, rc;
  526. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  527. if (!np) {
  528. pr_err("missing testcase data\n");
  529. return;
  530. }
  531. for (i = 0; i < 7; i++) {
  532. bool passed = true;
  533. rc = of_irq_parse_one(np, i, &args);
  534. /* Test the values from tests-phandle.dtsi */
  535. switch (i) {
  536. case 0:
  537. passed &= !rc;
  538. passed &= (args.args_count == 1);
  539. passed &= (args.args[0] == 1);
  540. break;
  541. case 1:
  542. passed &= !rc;
  543. passed &= (args.args_count == 3);
  544. passed &= (args.args[0] == 2);
  545. passed &= (args.args[1] == 3);
  546. passed &= (args.args[2] == 4);
  547. break;
  548. case 2:
  549. passed &= !rc;
  550. passed &= (args.args_count == 2);
  551. passed &= (args.args[0] == 5);
  552. passed &= (args.args[1] == 6);
  553. break;
  554. case 3:
  555. passed &= !rc;
  556. passed &= (args.args_count == 1);
  557. passed &= (args.args[0] == 9);
  558. break;
  559. case 4:
  560. passed &= !rc;
  561. passed &= (args.args_count == 3);
  562. passed &= (args.args[0] == 10);
  563. passed &= (args.args[1] == 11);
  564. passed &= (args.args[2] == 12);
  565. break;
  566. case 5:
  567. passed &= !rc;
  568. passed &= (args.args_count == 2);
  569. passed &= (args.args[0] == 13);
  570. passed &= (args.args[1] == 14);
  571. break;
  572. case 6:
  573. passed &= !rc;
  574. passed &= (args.args_count == 1);
  575. passed &= (args.args[0] == 15);
  576. break;
  577. default:
  578. passed = false;
  579. }
  580. selftest(passed, "index %i - data error on node %s rc=%i\n",
  581. i, args.np->full_name, rc);
  582. }
  583. of_node_put(np);
  584. }
  585. static struct of_device_id match_node_table[] = {
  586. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  587. { .data = "B", .type = "type1", }, /* followed by type alone */
  588. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  589. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  590. { .data = "Cc", .name = "name2", .type = "type2", },
  591. { .data = "E", .compatible = "compat3" },
  592. { .data = "G", .compatible = "compat2", },
  593. { .data = "H", .compatible = "compat2", .name = "name5", },
  594. { .data = "I", .compatible = "compat2", .type = "type1", },
  595. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  596. { .data = "K", .compatible = "compat2", .name = "name9", },
  597. {}
  598. };
  599. static struct {
  600. const char *path;
  601. const char *data;
  602. } match_node_tests[] = {
  603. { .path = "/testcase-data/match-node/name0", .data = "A", },
  604. { .path = "/testcase-data/match-node/name1", .data = "B", },
  605. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  606. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  607. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  608. { .path = "/testcase-data/match-node/name3", .data = "E", },
  609. { .path = "/testcase-data/match-node/name4", .data = "G", },
  610. { .path = "/testcase-data/match-node/name5", .data = "H", },
  611. { .path = "/testcase-data/match-node/name6", .data = "G", },
  612. { .path = "/testcase-data/match-node/name7", .data = "I", },
  613. { .path = "/testcase-data/match-node/name8", .data = "J", },
  614. { .path = "/testcase-data/match-node/name9", .data = "K", },
  615. };
  616. static void __init of_selftest_match_node(void)
  617. {
  618. struct device_node *np;
  619. const struct of_device_id *match;
  620. int i;
  621. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  622. np = of_find_node_by_path(match_node_tests[i].path);
  623. if (!np) {
  624. selftest(0, "missing testcase node %s\n",
  625. match_node_tests[i].path);
  626. continue;
  627. }
  628. match = of_match_node(match_node_table, np);
  629. if (!match) {
  630. selftest(0, "%s didn't match anything\n",
  631. match_node_tests[i].path);
  632. continue;
  633. }
  634. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  635. selftest(0, "%s got wrong match. expected %s, got %s\n",
  636. match_node_tests[i].path, match_node_tests[i].data,
  637. (const char *)match->data);
  638. continue;
  639. }
  640. selftest(1, "passed");
  641. }
  642. }
  643. struct device test_bus = {
  644. .init_name = "unittest-bus",
  645. };
  646. static void __init of_selftest_platform_populate(void)
  647. {
  648. int irq, rc;
  649. struct device_node *np, *child, *grandchild;
  650. struct platform_device *pdev;
  651. struct of_device_id match[] = {
  652. { .compatible = "test-device", },
  653. {}
  654. };
  655. np = of_find_node_by_path("/testcase-data");
  656. of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
  657. /* Test that a missing irq domain returns -EPROBE_DEFER */
  658. np = of_find_node_by_path("/testcase-data/testcase-device1");
  659. pdev = of_find_device_by_node(np);
  660. selftest(pdev, "device 1 creation failed\n");
  661. irq = platform_get_irq(pdev, 0);
  662. selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
  663. /* Test that a parsing failure does not return -EPROBE_DEFER */
  664. np = of_find_node_by_path("/testcase-data/testcase-device2");
  665. pdev = of_find_device_by_node(np);
  666. selftest(pdev, "device 2 creation failed\n");
  667. irq = platform_get_irq(pdev, 0);
  668. selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
  669. if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
  670. "No testcase data in device tree\n"));
  671. return;
  672. if (selftest(!(rc = device_register(&test_bus)),
  673. "testbus registration failed; rc=%i\n", rc));
  674. return;
  675. for_each_child_of_node(np, child) {
  676. of_platform_populate(child, match, NULL, &test_bus);
  677. for_each_child_of_node(child, grandchild)
  678. selftest(of_find_device_by_node(grandchild),
  679. "Could not create device for node '%s'\n",
  680. grandchild->name);
  681. }
  682. of_platform_depopulate(&test_bus);
  683. for_each_child_of_node(np, child) {
  684. for_each_child_of_node(child, grandchild)
  685. selftest(!of_find_device_by_node(grandchild),
  686. "device didn't get destroyed '%s'\n",
  687. grandchild->name);
  688. }
  689. device_unregister(&test_bus);
  690. of_node_put(np);
  691. }
  692. /**
  693. * update_node_properties - adds the properties
  694. * of np into dup node (present in live tree) and
  695. * updates parent of children of np to dup.
  696. *
  697. * @np: node already present in live tree
  698. * @dup: node present in live tree to be updated
  699. */
  700. static void update_node_properties(struct device_node *np,
  701. struct device_node *dup)
  702. {
  703. struct property *prop;
  704. struct device_node *child;
  705. for_each_property_of_node(np, prop)
  706. of_add_property(dup, prop);
  707. for_each_child_of_node(np, child)
  708. child->parent = dup;
  709. }
  710. /**
  711. * attach_node_and_children - attaches nodes
  712. * and its children to live tree
  713. *
  714. * @np: Node to attach to live tree
  715. */
  716. static int attach_node_and_children(struct device_node *np)
  717. {
  718. struct device_node *next, *dup, *child;
  719. dup = of_find_node_by_path(np->full_name);
  720. if (dup) {
  721. update_node_properties(np, dup);
  722. return 0;
  723. }
  724. /* Children of the root need to be remembered for removal */
  725. if (np->parent == of_root) {
  726. if (WARN_ON(last_node_index >= NO_OF_NODES))
  727. return -EINVAL;
  728. nodes[last_node_index++] = np;
  729. }
  730. child = np->child;
  731. np->child = NULL;
  732. np->sibling = NULL;
  733. of_attach_node(np);
  734. while (child) {
  735. next = child->sibling;
  736. attach_node_and_children(child);
  737. child = next;
  738. }
  739. return 0;
  740. }
  741. /**
  742. * selftest_data_add - Reads, copies data from
  743. * linked tree and attaches it to the live tree
  744. */
  745. static int __init selftest_data_add(void)
  746. {
  747. void *selftest_data;
  748. struct device_node *selftest_data_node, *np;
  749. extern uint8_t __dtb_testcases_begin[];
  750. extern uint8_t __dtb_testcases_end[];
  751. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  752. int rc;
  753. if (!size) {
  754. pr_warn("%s: No testcase data to attach; not running tests\n",
  755. __func__);
  756. return -ENODATA;
  757. }
  758. /* creating copy */
  759. selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  760. if (!selftest_data) {
  761. pr_warn("%s: Failed to allocate memory for selftest_data; "
  762. "not running tests\n", __func__);
  763. return -ENOMEM;
  764. }
  765. of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
  766. if (!selftest_data_node) {
  767. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  768. return -ENODATA;
  769. }
  770. of_node_set_flag(selftest_data_node, OF_DETACHED);
  771. rc = of_resolve_phandles(selftest_data_node);
  772. if (rc) {
  773. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  774. return -EINVAL;
  775. }
  776. if (!of_root) {
  777. /* enabling flag for removing nodes */
  778. selftest_live_tree = true;
  779. of_root = selftest_data_node;
  780. for_each_of_allnodes(np)
  781. __of_attach_node_sysfs(np);
  782. of_aliases = of_find_node_by_path("/aliases");
  783. of_chosen = of_find_node_by_path("/chosen");
  784. return 0;
  785. }
  786. /* attach the sub-tree to live tree */
  787. np = selftest_data_node->child;
  788. while (np) {
  789. struct device_node *next = np->sibling;
  790. np->parent = of_root;
  791. attach_node_and_children(np);
  792. np = next;
  793. }
  794. return 0;
  795. }
  796. /**
  797. * detach_node_and_children - detaches node
  798. * and its children from live tree
  799. *
  800. * @np: Node to detach from live tree
  801. */
  802. static void detach_node_and_children(struct device_node *np)
  803. {
  804. while (np->child)
  805. detach_node_and_children(np->child);
  806. of_detach_node(np);
  807. }
  808. /**
  809. * selftest_data_remove - removes the selftest data
  810. * nodes from the live tree
  811. */
  812. static void selftest_data_remove(void)
  813. {
  814. struct device_node *np;
  815. struct property *prop;
  816. if (selftest_live_tree) {
  817. of_node_put(of_aliases);
  818. of_node_put(of_chosen);
  819. of_aliases = NULL;
  820. of_chosen = NULL;
  821. for_each_child_of_node(of_root, np)
  822. detach_node_and_children(np);
  823. __of_detach_node_sysfs(of_root);
  824. of_root = NULL;
  825. return;
  826. }
  827. while (last_node_index-- > 0) {
  828. if (nodes[last_node_index]) {
  829. np = of_find_node_by_path(nodes[last_node_index]->full_name);
  830. if (np == nodes[last_node_index]) {
  831. if (of_aliases == np) {
  832. of_node_put(of_aliases);
  833. of_aliases = NULL;
  834. }
  835. detach_node_and_children(np);
  836. } else {
  837. for_each_property_of_node(np, prop) {
  838. if (strcmp(prop->name, "testcase-alias") == 0)
  839. of_remove_property(np, prop);
  840. }
  841. }
  842. }
  843. }
  844. }
  845. #ifdef CONFIG_OF_OVERLAY
  846. static int selftest_probe(struct platform_device *pdev)
  847. {
  848. struct device *dev = &pdev->dev;
  849. struct device_node *np = dev->of_node;
  850. if (np == NULL) {
  851. dev_err(dev, "No OF data for device\n");
  852. return -EINVAL;
  853. }
  854. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  855. of_platform_populate(np, NULL, NULL, &pdev->dev);
  856. return 0;
  857. }
  858. static int selftest_remove(struct platform_device *pdev)
  859. {
  860. struct device *dev = &pdev->dev;
  861. struct device_node *np = dev->of_node;
  862. dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
  863. return 0;
  864. }
  865. static struct of_device_id selftest_match[] = {
  866. { .compatible = "selftest", },
  867. {},
  868. };
  869. static struct platform_driver selftest_driver = {
  870. .probe = selftest_probe,
  871. .remove = selftest_remove,
  872. .driver = {
  873. .name = "selftest",
  874. .owner = THIS_MODULE,
  875. .of_match_table = of_match_ptr(selftest_match),
  876. },
  877. };
  878. /* get the platform device instantiated at the path */
  879. static struct platform_device *of_path_to_platform_device(const char *path)
  880. {
  881. struct device_node *np;
  882. struct platform_device *pdev;
  883. np = of_find_node_by_path(path);
  884. if (np == NULL)
  885. return NULL;
  886. pdev = of_find_device_by_node(np);
  887. of_node_put(np);
  888. return pdev;
  889. }
  890. /* find out if a platform device exists at that path */
  891. static int of_path_platform_device_exists(const char *path)
  892. {
  893. struct platform_device *pdev;
  894. pdev = of_path_to_platform_device(path);
  895. platform_device_put(pdev);
  896. return pdev != NULL;
  897. }
  898. static const char *selftest_path(int nr)
  899. {
  900. static char buf[256];
  901. snprintf(buf, sizeof(buf) - 1,
  902. "/testcase-data/overlay-node/test-bus/test-selftest%d", nr);
  903. buf[sizeof(buf) - 1] = '\0';
  904. return buf;
  905. }
  906. static const char *overlay_path(int nr)
  907. {
  908. static char buf[256];
  909. snprintf(buf, sizeof(buf) - 1,
  910. "/testcase-data/overlay%d", nr);
  911. buf[sizeof(buf) - 1] = '\0';
  912. return buf;
  913. }
  914. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  915. static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
  916. int *overlay_id)
  917. {
  918. struct device_node *np = NULL;
  919. int ret, id = -1;
  920. np = of_find_node_by_path(overlay_path(overlay_nr));
  921. if (np == NULL) {
  922. selftest(0, "could not find overlay node @\"%s\"\n",
  923. overlay_path(overlay_nr));
  924. ret = -EINVAL;
  925. goto out;
  926. }
  927. ret = of_overlay_create(np);
  928. if (ret < 0) {
  929. selftest(0, "could not create overlay from \"%s\"\n",
  930. overlay_path(overlay_nr));
  931. goto out;
  932. }
  933. id = ret;
  934. ret = 0;
  935. out:
  936. of_node_put(np);
  937. if (overlay_id)
  938. *overlay_id = id;
  939. return ret;
  940. }
  941. /* apply an overlay while checking before and after states */
  942. static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
  943. int before, int after)
  944. {
  945. int ret;
  946. /* selftest device must not be in before state */
  947. if (of_path_platform_device_exists(selftest_path(selftest_nr))
  948. != before) {
  949. selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  950. overlay_path(overlay_nr),
  951. selftest_path(selftest_nr),
  952. !before ? "enabled" : "disabled");
  953. return -EINVAL;
  954. }
  955. ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
  956. if (ret != 0) {
  957. /* of_selftest_apply_overlay already called selftest() */
  958. return ret;
  959. }
  960. /* selftest device must be to set to after state */
  961. if (of_path_platform_device_exists(selftest_path(selftest_nr))
  962. != after) {
  963. selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
  964. overlay_path(overlay_nr),
  965. selftest_path(selftest_nr),
  966. !after ? "enabled" : "disabled");
  967. return -EINVAL;
  968. }
  969. return 0;
  970. }
  971. /* apply an overlay and then revert it while checking before, after states */
  972. static int of_selftest_apply_revert_overlay_check(int overlay_nr,
  973. int selftest_nr, int before, int after)
  974. {
  975. int ret, ov_id;
  976. /* selftest device must be in before state */
  977. if (of_path_platform_device_exists(selftest_path(selftest_nr))
  978. != before) {
  979. selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  980. overlay_path(overlay_nr),
  981. selftest_path(selftest_nr),
  982. !before ? "enabled" : "disabled");
  983. return -EINVAL;
  984. }
  985. /* apply the overlay */
  986. ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
  987. if (ret != 0) {
  988. /* of_selftest_apply_overlay already called selftest() */
  989. return ret;
  990. }
  991. /* selftest device must be in after state */
  992. if (of_path_platform_device_exists(selftest_path(selftest_nr))
  993. != after) {
  994. selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
  995. overlay_path(overlay_nr),
  996. selftest_path(selftest_nr),
  997. !after ? "enabled" : "disabled");
  998. return -EINVAL;
  999. }
  1000. ret = of_overlay_destroy(ov_id);
  1001. if (ret != 0) {
  1002. selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
  1003. overlay_path(overlay_nr),
  1004. selftest_path(selftest_nr));
  1005. return ret;
  1006. }
  1007. /* selftest device must be again in before state */
  1008. if (of_path_platform_device_exists(selftest_path(selftest_nr))
  1009. != before) {
  1010. selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1011. overlay_path(overlay_nr),
  1012. selftest_path(selftest_nr),
  1013. !before ? "enabled" : "disabled");
  1014. return -EINVAL;
  1015. }
  1016. return 0;
  1017. }
  1018. /* test activation of device */
  1019. static void of_selftest_overlay_0(void)
  1020. {
  1021. int ret;
  1022. /* device should enable */
  1023. ret = of_selftest_apply_overlay_check(0, 0, 0, 1);
  1024. if (ret != 0)
  1025. return;
  1026. selftest(1, "overlay test %d passed\n", 0);
  1027. }
  1028. /* test deactivation of device */
  1029. static void of_selftest_overlay_1(void)
  1030. {
  1031. int ret;
  1032. /* device should disable */
  1033. ret = of_selftest_apply_overlay_check(1, 1, 1, 0);
  1034. if (ret != 0)
  1035. return;
  1036. selftest(1, "overlay test %d passed\n", 1);
  1037. }
  1038. /* test activation of device */
  1039. static void of_selftest_overlay_2(void)
  1040. {
  1041. int ret;
  1042. /* device should enable */
  1043. ret = of_selftest_apply_overlay_check(2, 2, 0, 1);
  1044. if (ret != 0)
  1045. return;
  1046. selftest(1, "overlay test %d passed\n", 2);
  1047. }
  1048. /* test deactivation of device */
  1049. static void of_selftest_overlay_3(void)
  1050. {
  1051. int ret;
  1052. /* device should disable */
  1053. ret = of_selftest_apply_overlay_check(3, 3, 1, 0);
  1054. if (ret != 0)
  1055. return;
  1056. selftest(1, "overlay test %d passed\n", 3);
  1057. }
  1058. /* test activation of a full device node */
  1059. static void of_selftest_overlay_4(void)
  1060. {
  1061. int ret;
  1062. /* device should disable */
  1063. ret = of_selftest_apply_overlay_check(4, 4, 0, 1);
  1064. if (ret != 0)
  1065. return;
  1066. selftest(1, "overlay test %d passed\n", 4);
  1067. }
  1068. /* test overlay apply/revert sequence */
  1069. static void of_selftest_overlay_5(void)
  1070. {
  1071. int ret;
  1072. /* device should disable */
  1073. ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
  1074. if (ret != 0)
  1075. return;
  1076. selftest(1, "overlay test %d passed\n", 5);
  1077. }
  1078. /* test overlay application in sequence */
  1079. static void of_selftest_overlay_6(void)
  1080. {
  1081. struct device_node *np;
  1082. int ret, i, ov_id[2];
  1083. int overlay_nr = 6, selftest_nr = 6;
  1084. int before = 0, after = 1;
  1085. /* selftest device must be in before state */
  1086. for (i = 0; i < 2; i++) {
  1087. if (of_path_platform_device_exists(
  1088. selftest_path(selftest_nr + i))
  1089. != before) {
  1090. selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1091. overlay_path(overlay_nr + i),
  1092. selftest_path(selftest_nr + i),
  1093. !before ? "enabled" : "disabled");
  1094. return;
  1095. }
  1096. }
  1097. /* apply the overlays */
  1098. for (i = 0; i < 2; i++) {
  1099. np = of_find_node_by_path(overlay_path(overlay_nr + i));
  1100. if (np == NULL) {
  1101. selftest(0, "could not find overlay node @\"%s\"\n",
  1102. overlay_path(overlay_nr + i));
  1103. return;
  1104. }
  1105. ret = of_overlay_create(np);
  1106. if (ret < 0) {
  1107. selftest(0, "could not create overlay from \"%s\"\n",
  1108. overlay_path(overlay_nr + i));
  1109. return;
  1110. }
  1111. ov_id[i] = ret;
  1112. }
  1113. for (i = 0; i < 2; i++) {
  1114. /* selftest device must be in after state */
  1115. if (of_path_platform_device_exists(
  1116. selftest_path(selftest_nr + i))
  1117. != after) {
  1118. selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1119. overlay_path(overlay_nr + i),
  1120. selftest_path(selftest_nr + i),
  1121. !after ? "enabled" : "disabled");
  1122. return;
  1123. }
  1124. }
  1125. for (i = 1; i >= 0; i--) {
  1126. ret = of_overlay_destroy(ov_id[i]);
  1127. if (ret != 0) {
  1128. selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
  1129. overlay_path(overlay_nr + i),
  1130. selftest_path(selftest_nr + i));
  1131. return;
  1132. }
  1133. }
  1134. for (i = 0; i < 2; i++) {
  1135. /* selftest device must be again in before state */
  1136. if (of_path_platform_device_exists(
  1137. selftest_path(selftest_nr + i))
  1138. != before) {
  1139. selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
  1140. overlay_path(overlay_nr + i),
  1141. selftest_path(selftest_nr + i),
  1142. !before ? "enabled" : "disabled");
  1143. return;
  1144. }
  1145. }
  1146. selftest(1, "overlay test %d passed\n", 6);
  1147. }
  1148. /* test overlay application in sequence */
  1149. static void of_selftest_overlay_8(void)
  1150. {
  1151. struct device_node *np;
  1152. int ret, i, ov_id[2];
  1153. int overlay_nr = 8, selftest_nr = 8;
  1154. /* we don't care about device state in this test */
  1155. /* apply the overlays */
  1156. for (i = 0; i < 2; i++) {
  1157. np = of_find_node_by_path(overlay_path(overlay_nr + i));
  1158. if (np == NULL) {
  1159. selftest(0, "could not find overlay node @\"%s\"\n",
  1160. overlay_path(overlay_nr + i));
  1161. return;
  1162. }
  1163. ret = of_overlay_create(np);
  1164. if (ret < 0) {
  1165. selftest(0, "could not create overlay from \"%s\"\n",
  1166. overlay_path(overlay_nr + i));
  1167. return;
  1168. }
  1169. ov_id[i] = ret;
  1170. }
  1171. /* now try to remove first overlay (it should fail) */
  1172. ret = of_overlay_destroy(ov_id[0]);
  1173. if (ret == 0) {
  1174. selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
  1175. overlay_path(overlay_nr + 0),
  1176. selftest_path(selftest_nr));
  1177. return;
  1178. }
  1179. /* removing them in order should work */
  1180. for (i = 1; i >= 0; i--) {
  1181. ret = of_overlay_destroy(ov_id[i]);
  1182. if (ret != 0) {
  1183. selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
  1184. overlay_path(overlay_nr + i),
  1185. selftest_path(selftest_nr));
  1186. return;
  1187. }
  1188. }
  1189. selftest(1, "overlay test %d passed\n", 8);
  1190. }
  1191. /* test insertion of a bus with parent devices */
  1192. static void of_selftest_overlay_10(void)
  1193. {
  1194. int ret;
  1195. char *child_path;
  1196. /* device should disable */
  1197. ret = of_selftest_apply_overlay_check(10, 10, 0, 1);
  1198. if (selftest(ret == 0, "overlay test %d failed; overlay application\n", 10))
  1199. return;
  1200. child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
  1201. selftest_path(10));
  1202. if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1203. return;
  1204. ret = of_path_platform_device_exists(child_path);
  1205. kfree(child_path);
  1206. if (selftest(ret, "overlay test %d failed; no child device\n", 10))
  1207. return;
  1208. }
  1209. /* test insertion of a bus with parent devices (and revert) */
  1210. static void of_selftest_overlay_11(void)
  1211. {
  1212. int ret;
  1213. /* device should disable */
  1214. ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1);
  1215. if (selftest(ret == 0, "overlay test %d failed; overlay application\n", 11))
  1216. return;
  1217. }
  1218. static void __init of_selftest_overlay(void)
  1219. {
  1220. struct device_node *bus_np = NULL;
  1221. int ret;
  1222. ret = platform_driver_register(&selftest_driver);
  1223. if (ret != 0) {
  1224. selftest(0, "could not register selftest driver\n");
  1225. goto out;
  1226. }
  1227. bus_np = of_find_node_by_path(bus_path);
  1228. if (bus_np == NULL) {
  1229. selftest(0, "could not find bus_path \"%s\"\n", bus_path);
  1230. goto out;
  1231. }
  1232. ret = of_platform_populate(bus_np, of_default_bus_match_table,
  1233. NULL, NULL);
  1234. if (ret != 0) {
  1235. selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1236. goto out;
  1237. }
  1238. if (!of_path_platform_device_exists(selftest_path(100))) {
  1239. selftest(0, "could not find selftest0 @ \"%s\"\n",
  1240. selftest_path(100));
  1241. goto out;
  1242. }
  1243. if (of_path_platform_device_exists(selftest_path(101))) {
  1244. selftest(0, "selftest1 @ \"%s\" should not exist\n",
  1245. selftest_path(101));
  1246. goto out;
  1247. }
  1248. selftest(1, "basic infrastructure of overlays passed");
  1249. /* tests in sequence */
  1250. of_selftest_overlay_0();
  1251. of_selftest_overlay_1();
  1252. of_selftest_overlay_2();
  1253. of_selftest_overlay_3();
  1254. of_selftest_overlay_4();
  1255. of_selftest_overlay_5();
  1256. of_selftest_overlay_6();
  1257. of_selftest_overlay_8();
  1258. of_selftest_overlay_10();
  1259. of_selftest_overlay_11();
  1260. out:
  1261. of_node_put(bus_np);
  1262. }
  1263. #else
  1264. static inline void __init of_selftest_overlay(void) { }
  1265. #endif
  1266. static int __init of_selftest(void)
  1267. {
  1268. struct device_node *np;
  1269. int res;
  1270. /* adding data for selftest */
  1271. res = selftest_data_add();
  1272. if (res)
  1273. return res;
  1274. if (!of_aliases)
  1275. of_aliases = of_find_node_by_path("/aliases");
  1276. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  1277. if (!np) {
  1278. pr_info("No testcase data in device tree; not running tests\n");
  1279. return 0;
  1280. }
  1281. of_node_put(np);
  1282. pr_info("start of selftest - you will see error messages\n");
  1283. of_selftest_check_tree_linkage();
  1284. of_selftest_check_phandles();
  1285. of_selftest_find_node_by_name();
  1286. of_selftest_dynamic();
  1287. of_selftest_parse_phandle_with_args();
  1288. of_selftest_property_string();
  1289. of_selftest_property_copy();
  1290. of_selftest_changeset();
  1291. of_selftest_parse_interrupts();
  1292. of_selftest_parse_interrupts_extended();
  1293. of_selftest_match_node();
  1294. of_selftest_platform_populate();
  1295. of_selftest_overlay();
  1296. /* removing selftest data from live tree */
  1297. selftest_data_remove();
  1298. /* Double check linkage after removing testcase data */
  1299. of_selftest_check_tree_linkage();
  1300. pr_info("end of selftest - %i passed, %i failed\n",
  1301. selftest_results.passed, selftest_results.failed);
  1302. return 0;
  1303. }
  1304. late_initcall(of_selftest);