unittest.c 49 KB

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