unittest.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Self tests for device tree subsystem
  4. */
  5. #define pr_fmt(fmt) "### dt-test ### " fmt
  6. #include <linux/bootmem.h>
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/hashtable.h>
  11. #include <linux/libfdt.h>
  12. #include <linux/of.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/list.h>
  17. #include <linux/mutex.h>
  18. #include <linux/slab.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-mux.h>
  23. #include <linux/bitops.h>
  24. #include "of_private.h"
  25. static struct unittest_results {
  26. int passed;
  27. int failed;
  28. } unittest_results;
  29. #define unittest(result, fmt, ...) ({ \
  30. bool failed = !(result); \
  31. if (failed) { \
  32. unittest_results.failed++; \
  33. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  34. } else { \
  35. unittest_results.passed++; \
  36. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  37. } \
  38. failed; \
  39. })
  40. static void __init of_unittest_find_node_by_name(void)
  41. {
  42. struct device_node *np;
  43. const char *options, *name;
  44. np = of_find_node_by_path("/testcase-data");
  45. name = kasprintf(GFP_KERNEL, "%pOF", np);
  46. unittest(np && !strcmp("/testcase-data", name),
  47. "find /testcase-data failed\n");
  48. of_node_put(np);
  49. kfree(name);
  50. /* Test if trailing '/' works */
  51. np = of_find_node_by_path("/testcase-data/");
  52. unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
  53. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  54. name = kasprintf(GFP_KERNEL, "%pOF", np);
  55. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  56. "find /testcase-data/phandle-tests/consumer-a failed\n");
  57. of_node_put(np);
  58. kfree(name);
  59. np = of_find_node_by_path("testcase-alias");
  60. name = kasprintf(GFP_KERNEL, "%pOF", np);
  61. unittest(np && !strcmp("/testcase-data", name),
  62. "find testcase-alias failed\n");
  63. of_node_put(np);
  64. kfree(name);
  65. /* Test if trailing '/' works on aliases */
  66. np = of_find_node_by_path("testcase-alias/");
  67. unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
  68. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  69. name = kasprintf(GFP_KERNEL, "%pOF", np);
  70. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  71. "find testcase-alias/phandle-tests/consumer-a failed\n");
  72. of_node_put(np);
  73. kfree(name);
  74. np = of_find_node_by_path("/testcase-data/missing-path");
  75. unittest(!np, "non-existent path returned node %pOF\n", np);
  76. of_node_put(np);
  77. np = of_find_node_by_path("missing-alias");
  78. unittest(!np, "non-existent alias returned node %pOF\n", np);
  79. of_node_put(np);
  80. np = of_find_node_by_path("testcase-alias/missing-path");
  81. unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
  82. of_node_put(np);
  83. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  84. unittest(np && !strcmp("testoption", options),
  85. "option path test failed\n");
  86. of_node_put(np);
  87. np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
  88. unittest(np && !strcmp("test/option", options),
  89. "option path test, subcase #1 failed\n");
  90. of_node_put(np);
  91. np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
  92. unittest(np && !strcmp("test/option", options),
  93. "option path test, subcase #2 failed\n");
  94. of_node_put(np);
  95. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  96. unittest(np, "NULL option path test failed\n");
  97. of_node_put(np);
  98. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  99. &options);
  100. unittest(np && !strcmp("testaliasoption", options),
  101. "option alias path test failed\n");
  102. of_node_put(np);
  103. np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
  104. &options);
  105. unittest(np && !strcmp("test/alias/option", options),
  106. "option alias path test, subcase #1 failed\n");
  107. of_node_put(np);
  108. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  109. unittest(np, "NULL option alias path test failed\n");
  110. of_node_put(np);
  111. options = "testoption";
  112. np = of_find_node_opts_by_path("testcase-alias", &options);
  113. unittest(np && !options, "option clearing test failed\n");
  114. of_node_put(np);
  115. options = "testoption";
  116. np = of_find_node_opts_by_path("/", &options);
  117. unittest(np && !options, "option clearing root node test failed\n");
  118. of_node_put(np);
  119. }
  120. static void __init of_unittest_dynamic(void)
  121. {
  122. struct device_node *np;
  123. struct property *prop;
  124. np = of_find_node_by_path("/testcase-data");
  125. if (!np) {
  126. pr_err("missing testcase data\n");
  127. return;
  128. }
  129. /* Array of 4 properties for the purpose of testing */
  130. prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
  131. if (!prop) {
  132. unittest(0, "kzalloc() failed\n");
  133. return;
  134. }
  135. /* Add a new property - should pass*/
  136. prop->name = "new-property";
  137. prop->value = "new-property-data";
  138. prop->length = strlen(prop->value) + 1;
  139. unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  140. /* Try to add an existing property - should fail */
  141. prop++;
  142. prop->name = "new-property";
  143. prop->value = "new-property-data-should-fail";
  144. prop->length = strlen(prop->value) + 1;
  145. unittest(of_add_property(np, prop) != 0,
  146. "Adding an existing property should have failed\n");
  147. /* Try to modify an existing property - should pass */
  148. prop->value = "modify-property-data-should-pass";
  149. prop->length = strlen(prop->value) + 1;
  150. unittest(of_update_property(np, prop) == 0,
  151. "Updating an existing property should have passed\n");
  152. /* Try to modify non-existent property - should pass*/
  153. prop++;
  154. prop->name = "modify-property";
  155. prop->value = "modify-missing-property-data-should-pass";
  156. prop->length = strlen(prop->value) + 1;
  157. unittest(of_update_property(np, prop) == 0,
  158. "Updating a missing property should have passed\n");
  159. /* Remove property - should pass */
  160. unittest(of_remove_property(np, prop) == 0,
  161. "Removing a property should have passed\n");
  162. /* Adding very large property - should pass */
  163. prop++;
  164. prop->name = "large-property-PAGE_SIZEx8";
  165. prop->length = PAGE_SIZE * 8;
  166. prop->value = kzalloc(prop->length, GFP_KERNEL);
  167. unittest(prop->value != NULL, "Unable to allocate large buffer\n");
  168. if (prop->value)
  169. unittest(of_add_property(np, prop) == 0,
  170. "Adding a large property should have passed\n");
  171. }
  172. static int __init of_unittest_check_node_linkage(struct device_node *np)
  173. {
  174. struct device_node *child;
  175. int count = 0, rc;
  176. for_each_child_of_node(np, child) {
  177. if (child->parent != np) {
  178. pr_err("Child node %pOFn links to wrong parent %pOFn\n",
  179. child, np);
  180. rc = -EINVAL;
  181. goto put_child;
  182. }
  183. rc = of_unittest_check_node_linkage(child);
  184. if (rc < 0)
  185. goto put_child;
  186. count += rc;
  187. }
  188. return count + 1;
  189. put_child:
  190. of_node_put(child);
  191. return rc;
  192. }
  193. static void __init of_unittest_check_tree_linkage(void)
  194. {
  195. struct device_node *np;
  196. int allnode_count = 0, child_count;
  197. if (!of_root)
  198. return;
  199. for_each_of_allnodes(np)
  200. allnode_count++;
  201. child_count = of_unittest_check_node_linkage(of_root);
  202. unittest(child_count > 0, "Device node data structure is corrupted\n");
  203. unittest(child_count == allnode_count,
  204. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  205. allnode_count, child_count);
  206. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  207. }
  208. static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
  209. const char *expected)
  210. {
  211. unsigned char *buf;
  212. int buf_size;
  213. int size, i;
  214. buf_size = strlen(expected) + 10;
  215. buf = kmalloc(buf_size, GFP_KERNEL);
  216. if (!buf)
  217. return;
  218. /* Baseline; check conversion with a large size limit */
  219. memset(buf, 0xff, buf_size);
  220. size = snprintf(buf, buf_size - 2, fmt, np);
  221. /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
  222. unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
  223. "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
  224. fmt, expected, buf);
  225. /* Make sure length limits work */
  226. size++;
  227. for (i = 0; i < 2; i++, size--) {
  228. /* Clear the buffer, and make sure it works correctly still */
  229. memset(buf, 0xff, buf_size);
  230. snprintf(buf, size+1, fmt, np);
  231. unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
  232. "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
  233. size, fmt, expected, buf);
  234. }
  235. kfree(buf);
  236. }
  237. static void __init of_unittest_printf(void)
  238. {
  239. struct device_node *np;
  240. const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
  241. char phandle_str[16] = "";
  242. np = of_find_node_by_path(full_name);
  243. if (!np) {
  244. unittest(np, "testcase data missing\n");
  245. return;
  246. }
  247. num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
  248. of_unittest_printf_one(np, "%pOF", full_name);
  249. of_unittest_printf_one(np, "%pOFf", full_name);
  250. of_unittest_printf_one(np, "%pOFp", phandle_str);
  251. of_unittest_printf_one(np, "%pOFP", "dev@100");
  252. of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
  253. of_unittest_printf_one(np, "%10pOFP", " dev@100");
  254. of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
  255. of_unittest_printf_one(of_root, "%pOFP", "/");
  256. of_unittest_printf_one(np, "%pOFF", "----");
  257. of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
  258. of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
  259. of_unittest_printf_one(np, "%pOFc", "test-sub-device");
  260. of_unittest_printf_one(np, "%pOFC",
  261. "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
  262. }
  263. struct node_hash {
  264. struct hlist_node node;
  265. struct device_node *np;
  266. };
  267. static DEFINE_HASHTABLE(phandle_ht, 8);
  268. static void __init of_unittest_check_phandles(void)
  269. {
  270. struct device_node *np;
  271. struct node_hash *nh;
  272. struct hlist_node *tmp;
  273. int i, dup_count = 0, phandle_count = 0;
  274. for_each_of_allnodes(np) {
  275. if (!np->phandle)
  276. continue;
  277. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  278. if (nh->np->phandle == np->phandle) {
  279. pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
  280. np->phandle, nh->np, np);
  281. dup_count++;
  282. break;
  283. }
  284. }
  285. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  286. if (WARN_ON(!nh))
  287. return;
  288. nh->np = np;
  289. hash_add(phandle_ht, &nh->node, np->phandle);
  290. phandle_count++;
  291. }
  292. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  293. dup_count, phandle_count);
  294. /* Clean up */
  295. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  296. hash_del(&nh->node);
  297. kfree(nh);
  298. }
  299. }
  300. static void __init of_unittest_parse_phandle_with_args(void)
  301. {
  302. struct device_node *np;
  303. struct of_phandle_args args;
  304. int i, rc;
  305. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  306. if (!np) {
  307. pr_err("missing testcase data\n");
  308. return;
  309. }
  310. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  311. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  312. for (i = 0; i < 8; i++) {
  313. bool passed = true;
  314. memset(&args, 0, sizeof(args));
  315. rc = of_parse_phandle_with_args(np, "phandle-list",
  316. "#phandle-cells", i, &args);
  317. /* Test the values from tests-phandle.dtsi */
  318. switch (i) {
  319. case 0:
  320. passed &= !rc;
  321. passed &= (args.args_count == 1);
  322. passed &= (args.args[0] == (i + 1));
  323. break;
  324. case 1:
  325. passed &= !rc;
  326. passed &= (args.args_count == 2);
  327. passed &= (args.args[0] == (i + 1));
  328. passed &= (args.args[1] == 0);
  329. break;
  330. case 2:
  331. passed &= (rc == -ENOENT);
  332. break;
  333. case 3:
  334. passed &= !rc;
  335. passed &= (args.args_count == 3);
  336. passed &= (args.args[0] == (i + 1));
  337. passed &= (args.args[1] == 4);
  338. passed &= (args.args[2] == 3);
  339. break;
  340. case 4:
  341. passed &= !rc;
  342. passed &= (args.args_count == 2);
  343. passed &= (args.args[0] == (i + 1));
  344. passed &= (args.args[1] == 100);
  345. break;
  346. case 5:
  347. passed &= !rc;
  348. passed &= (args.args_count == 0);
  349. break;
  350. case 6:
  351. passed &= !rc;
  352. passed &= (args.args_count == 1);
  353. passed &= (args.args[0] == (i + 1));
  354. break;
  355. case 7:
  356. passed &= (rc == -ENOENT);
  357. break;
  358. default:
  359. passed = false;
  360. }
  361. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  362. i, args.np, rc);
  363. }
  364. /* Check for missing list property */
  365. memset(&args, 0, sizeof(args));
  366. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  367. "#phandle-cells", 0, &args);
  368. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  369. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  370. "#phandle-cells");
  371. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  372. /* Check for missing cells property */
  373. memset(&args, 0, sizeof(args));
  374. rc = of_parse_phandle_with_args(np, "phandle-list",
  375. "#phandle-cells-missing", 0, &args);
  376. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  377. rc = of_count_phandle_with_args(np, "phandle-list",
  378. "#phandle-cells-missing");
  379. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  380. /* Check for bad phandle in list */
  381. memset(&args, 0, sizeof(args));
  382. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  383. "#phandle-cells", 0, &args);
  384. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  385. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  386. "#phandle-cells");
  387. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  388. /* Check for incorrectly formed argument list */
  389. memset(&args, 0, sizeof(args));
  390. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  391. "#phandle-cells", 1, &args);
  392. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  393. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  394. "#phandle-cells");
  395. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  396. }
  397. static void __init of_unittest_parse_phandle_with_args_map(void)
  398. {
  399. struct device_node *np, *p0, *p1, *p2, *p3;
  400. struct of_phandle_args args;
  401. int i, rc;
  402. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
  403. if (!np) {
  404. pr_err("missing testcase data\n");
  405. return;
  406. }
  407. p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
  408. if (!p0) {
  409. pr_err("missing testcase data\n");
  410. return;
  411. }
  412. p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
  413. if (!p1) {
  414. pr_err("missing testcase data\n");
  415. return;
  416. }
  417. p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
  418. if (!p2) {
  419. pr_err("missing testcase data\n");
  420. return;
  421. }
  422. p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
  423. if (!p3) {
  424. pr_err("missing testcase data\n");
  425. return;
  426. }
  427. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  428. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  429. for (i = 0; i < 8; i++) {
  430. bool passed = true;
  431. memset(&args, 0, sizeof(args));
  432. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  433. "phandle", i, &args);
  434. /* Test the values from tests-phandle.dtsi */
  435. switch (i) {
  436. case 0:
  437. passed &= !rc;
  438. passed &= (args.np == p1);
  439. passed &= (args.args_count == 1);
  440. passed &= (args.args[0] == 1);
  441. break;
  442. case 1:
  443. passed &= !rc;
  444. passed &= (args.np == p3);
  445. passed &= (args.args_count == 3);
  446. passed &= (args.args[0] == 2);
  447. passed &= (args.args[1] == 5);
  448. passed &= (args.args[2] == 3);
  449. break;
  450. case 2:
  451. passed &= (rc == -ENOENT);
  452. break;
  453. case 3:
  454. passed &= !rc;
  455. passed &= (args.np == p0);
  456. passed &= (args.args_count == 0);
  457. break;
  458. case 4:
  459. passed &= !rc;
  460. passed &= (args.np == p1);
  461. passed &= (args.args_count == 1);
  462. passed &= (args.args[0] == 3);
  463. break;
  464. case 5:
  465. passed &= !rc;
  466. passed &= (args.np == p0);
  467. passed &= (args.args_count == 0);
  468. break;
  469. case 6:
  470. passed &= !rc;
  471. passed &= (args.np == p2);
  472. passed &= (args.args_count == 2);
  473. passed &= (args.args[0] == 15);
  474. passed &= (args.args[1] == 0x20);
  475. break;
  476. case 7:
  477. passed &= (rc == -ENOENT);
  478. break;
  479. default:
  480. passed = false;
  481. }
  482. unittest(passed, "index %i - data error on node %s rc=%i\n",
  483. i, args.np->full_name, rc);
  484. }
  485. /* Check for missing list property */
  486. memset(&args, 0, sizeof(args));
  487. rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
  488. "phandle", 0, &args);
  489. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  490. /* Check for missing cells,map,mask property */
  491. memset(&args, 0, sizeof(args));
  492. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  493. "phandle-missing", 0, &args);
  494. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  495. /* Check for bad phandle in list */
  496. memset(&args, 0, sizeof(args));
  497. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
  498. "phandle", 0, &args);
  499. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  500. /* Check for incorrectly formed argument list */
  501. memset(&args, 0, sizeof(args));
  502. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
  503. "phandle", 1, &args);
  504. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  505. }
  506. static void __init of_unittest_property_string(void)
  507. {
  508. const char *strings[4];
  509. struct device_node *np;
  510. int rc;
  511. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  512. if (!np) {
  513. pr_err("No testcase data in device tree\n");
  514. return;
  515. }
  516. rc = of_property_match_string(np, "phandle-list-names", "first");
  517. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  518. rc = of_property_match_string(np, "phandle-list-names", "second");
  519. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  520. rc = of_property_match_string(np, "phandle-list-names", "third");
  521. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  522. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  523. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  524. rc = of_property_match_string(np, "missing-property", "blah");
  525. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  526. rc = of_property_match_string(np, "empty-property", "blah");
  527. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  528. rc = of_property_match_string(np, "unterminated-string", "blah");
  529. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  530. /* of_property_count_strings() tests */
  531. rc = of_property_count_strings(np, "string-property");
  532. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  533. rc = of_property_count_strings(np, "phandle-list-names");
  534. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  535. rc = of_property_count_strings(np, "unterminated-string");
  536. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  537. rc = of_property_count_strings(np, "unterminated-string-list");
  538. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  539. /* of_property_read_string_index() tests */
  540. rc = of_property_read_string_index(np, "string-property", 0, strings);
  541. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  542. strings[0] = NULL;
  543. rc = of_property_read_string_index(np, "string-property", 1, strings);
  544. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  545. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  546. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  547. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  548. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  549. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  550. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  551. strings[0] = NULL;
  552. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  553. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  554. strings[0] = NULL;
  555. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  556. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  557. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  558. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  559. strings[0] = NULL;
  560. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  561. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  562. strings[1] = NULL;
  563. /* of_property_read_string_array() tests */
  564. rc = of_property_read_string_array(np, "string-property", strings, 4);
  565. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  566. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  567. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  568. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  569. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  570. /* -- An incorrectly formed string should cause a failure */
  571. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  572. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  573. /* -- parsing the correctly formed strings should still work: */
  574. strings[2] = NULL;
  575. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  576. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  577. strings[1] = NULL;
  578. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  579. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  580. }
  581. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  582. (p1)->value && (p2)->value && \
  583. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  584. !strcmp((p1)->name, (p2)->name))
  585. static void __init of_unittest_property_copy(void)
  586. {
  587. #ifdef CONFIG_OF_DYNAMIC
  588. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  589. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  590. struct property *new;
  591. new = __of_prop_dup(&p1, GFP_KERNEL);
  592. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  593. kfree(new->value);
  594. kfree(new->name);
  595. kfree(new);
  596. new = __of_prop_dup(&p2, GFP_KERNEL);
  597. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  598. kfree(new->value);
  599. kfree(new->name);
  600. kfree(new);
  601. #endif
  602. }
  603. static void __init of_unittest_changeset(void)
  604. {
  605. #ifdef CONFIG_OF_DYNAMIC
  606. struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
  607. struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
  608. struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
  609. struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
  610. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  611. struct property *ppremove;
  612. struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
  613. struct of_changeset chgset;
  614. n1 = __of_node_dup(NULL, "n1");
  615. unittest(n1, "testcase setup failure\n");
  616. n2 = __of_node_dup(NULL, "n2");
  617. unittest(n2, "testcase setup failure\n");
  618. n21 = __of_node_dup(NULL, "n21");
  619. unittest(n21, "testcase setup failure %p\n", n21);
  620. nchangeset = of_find_node_by_path("/testcase-data/changeset");
  621. nremove = of_get_child_by_name(nchangeset, "node-remove");
  622. unittest(nremove, "testcase setup failure\n");
  623. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  624. unittest(ppadd, "testcase setup failure\n");
  625. ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
  626. unittest(ppname_n1, "testcase setup failure\n");
  627. ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
  628. unittest(ppname_n2, "testcase setup failure\n");
  629. ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
  630. unittest(ppname_n21, "testcase setup failure\n");
  631. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  632. unittest(ppupdate, "testcase setup failure\n");
  633. parent = nchangeset;
  634. n1->parent = parent;
  635. n2->parent = parent;
  636. n21->parent = n2;
  637. ppremove = of_find_property(parent, "prop-remove", NULL);
  638. unittest(ppremove, "failed to find removal prop");
  639. of_changeset_init(&chgset);
  640. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  641. unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
  642. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  643. unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
  644. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  645. unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
  646. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  647. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
  648. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  649. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  650. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  651. of_node_put(nchangeset);
  652. /* Make sure node names are constructed correctly */
  653. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  654. "'%pOF' not added\n", n21);
  655. of_node_put(np);
  656. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  657. of_changeset_destroy(&chgset);
  658. of_node_put(n1);
  659. of_node_put(n2);
  660. of_node_put(n21);
  661. #endif
  662. }
  663. static void __init of_unittest_parse_interrupts(void)
  664. {
  665. struct device_node *np;
  666. struct of_phandle_args args;
  667. int i, rc;
  668. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  669. return;
  670. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  671. if (!np) {
  672. pr_err("missing testcase data\n");
  673. return;
  674. }
  675. for (i = 0; i < 4; i++) {
  676. bool passed = true;
  677. memset(&args, 0, sizeof(args));
  678. rc = of_irq_parse_one(np, i, &args);
  679. passed &= !rc;
  680. passed &= (args.args_count == 1);
  681. passed &= (args.args[0] == (i + 1));
  682. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  683. i, args.np, rc);
  684. }
  685. of_node_put(np);
  686. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  687. if (!np) {
  688. pr_err("missing testcase data\n");
  689. return;
  690. }
  691. for (i = 0; i < 4; i++) {
  692. bool passed = true;
  693. memset(&args, 0, sizeof(args));
  694. rc = of_irq_parse_one(np, i, &args);
  695. /* Test the values from tests-phandle.dtsi */
  696. switch (i) {
  697. case 0:
  698. passed &= !rc;
  699. passed &= (args.args_count == 1);
  700. passed &= (args.args[0] == 9);
  701. break;
  702. case 1:
  703. passed &= !rc;
  704. passed &= (args.args_count == 3);
  705. passed &= (args.args[0] == 10);
  706. passed &= (args.args[1] == 11);
  707. passed &= (args.args[2] == 12);
  708. break;
  709. case 2:
  710. passed &= !rc;
  711. passed &= (args.args_count == 2);
  712. passed &= (args.args[0] == 13);
  713. passed &= (args.args[1] == 14);
  714. break;
  715. case 3:
  716. passed &= !rc;
  717. passed &= (args.args_count == 2);
  718. passed &= (args.args[0] == 15);
  719. passed &= (args.args[1] == 16);
  720. break;
  721. default:
  722. passed = false;
  723. }
  724. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  725. i, args.np, rc);
  726. }
  727. of_node_put(np);
  728. }
  729. static void __init of_unittest_parse_interrupts_extended(void)
  730. {
  731. struct device_node *np;
  732. struct of_phandle_args args;
  733. int i, rc;
  734. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  735. return;
  736. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  737. if (!np) {
  738. pr_err("missing testcase data\n");
  739. return;
  740. }
  741. for (i = 0; i < 7; i++) {
  742. bool passed = true;
  743. memset(&args, 0, sizeof(args));
  744. rc = of_irq_parse_one(np, i, &args);
  745. /* Test the values from tests-phandle.dtsi */
  746. switch (i) {
  747. case 0:
  748. passed &= !rc;
  749. passed &= (args.args_count == 1);
  750. passed &= (args.args[0] == 1);
  751. break;
  752. case 1:
  753. passed &= !rc;
  754. passed &= (args.args_count == 3);
  755. passed &= (args.args[0] == 2);
  756. passed &= (args.args[1] == 3);
  757. passed &= (args.args[2] == 4);
  758. break;
  759. case 2:
  760. passed &= !rc;
  761. passed &= (args.args_count == 2);
  762. passed &= (args.args[0] == 5);
  763. passed &= (args.args[1] == 6);
  764. break;
  765. case 3:
  766. passed &= !rc;
  767. passed &= (args.args_count == 1);
  768. passed &= (args.args[0] == 9);
  769. break;
  770. case 4:
  771. passed &= !rc;
  772. passed &= (args.args_count == 3);
  773. passed &= (args.args[0] == 10);
  774. passed &= (args.args[1] == 11);
  775. passed &= (args.args[2] == 12);
  776. break;
  777. case 5:
  778. passed &= !rc;
  779. passed &= (args.args_count == 2);
  780. passed &= (args.args[0] == 13);
  781. passed &= (args.args[1] == 14);
  782. break;
  783. case 6:
  784. passed &= !rc;
  785. passed &= (args.args_count == 1);
  786. passed &= (args.args[0] == 15);
  787. break;
  788. default:
  789. passed = false;
  790. }
  791. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  792. i, args.np, rc);
  793. }
  794. of_node_put(np);
  795. }
  796. static const struct of_device_id match_node_table[] = {
  797. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  798. { .data = "B", .type = "type1", }, /* followed by type alone */
  799. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  800. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  801. { .data = "Cc", .name = "name2", .type = "type2", },
  802. { .data = "E", .compatible = "compat3" },
  803. { .data = "G", .compatible = "compat2", },
  804. { .data = "H", .compatible = "compat2", .name = "name5", },
  805. { .data = "I", .compatible = "compat2", .type = "type1", },
  806. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  807. { .data = "K", .compatible = "compat2", .name = "name9", },
  808. {}
  809. };
  810. static struct {
  811. const char *path;
  812. const char *data;
  813. } match_node_tests[] = {
  814. { .path = "/testcase-data/match-node/name0", .data = "A", },
  815. { .path = "/testcase-data/match-node/name1", .data = "B", },
  816. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  817. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  818. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  819. { .path = "/testcase-data/match-node/name3", .data = "E", },
  820. { .path = "/testcase-data/match-node/name4", .data = "G", },
  821. { .path = "/testcase-data/match-node/name5", .data = "H", },
  822. { .path = "/testcase-data/match-node/name6", .data = "G", },
  823. { .path = "/testcase-data/match-node/name7", .data = "I", },
  824. { .path = "/testcase-data/match-node/name8", .data = "J", },
  825. { .path = "/testcase-data/match-node/name9", .data = "K", },
  826. };
  827. static void __init of_unittest_match_node(void)
  828. {
  829. struct device_node *np;
  830. const struct of_device_id *match;
  831. int i;
  832. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  833. np = of_find_node_by_path(match_node_tests[i].path);
  834. if (!np) {
  835. unittest(0, "missing testcase node %s\n",
  836. match_node_tests[i].path);
  837. continue;
  838. }
  839. match = of_match_node(match_node_table, np);
  840. if (!match) {
  841. unittest(0, "%s didn't match anything\n",
  842. match_node_tests[i].path);
  843. continue;
  844. }
  845. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  846. unittest(0, "%s got wrong match. expected %s, got %s\n",
  847. match_node_tests[i].path, match_node_tests[i].data,
  848. (const char *)match->data);
  849. continue;
  850. }
  851. unittest(1, "passed");
  852. }
  853. }
  854. static struct resource test_bus_res = {
  855. .start = 0xfffffff8,
  856. .end = 0xfffffff9,
  857. .flags = IORESOURCE_MEM,
  858. };
  859. static const struct platform_device_info test_bus_info = {
  860. .name = "unittest-bus",
  861. };
  862. static void __init of_unittest_platform_populate(void)
  863. {
  864. int irq, rc;
  865. struct device_node *np, *child, *grandchild;
  866. struct platform_device *pdev, *test_bus;
  867. const struct of_device_id match[] = {
  868. { .compatible = "test-device", },
  869. {}
  870. };
  871. np = of_find_node_by_path("/testcase-data");
  872. of_platform_default_populate(np, NULL, NULL);
  873. /* Test that a missing irq domain returns -EPROBE_DEFER */
  874. np = of_find_node_by_path("/testcase-data/testcase-device1");
  875. pdev = of_find_device_by_node(np);
  876. unittest(pdev, "device 1 creation failed\n");
  877. if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
  878. irq = platform_get_irq(pdev, 0);
  879. unittest(irq == -EPROBE_DEFER,
  880. "device deferred probe failed - %d\n", irq);
  881. /* Test that a parsing failure does not return -EPROBE_DEFER */
  882. np = of_find_node_by_path("/testcase-data/testcase-device2");
  883. pdev = of_find_device_by_node(np);
  884. unittest(pdev, "device 2 creation failed\n");
  885. irq = platform_get_irq(pdev, 0);
  886. unittest(irq < 0 && irq != -EPROBE_DEFER,
  887. "device parsing error failed - %d\n", irq);
  888. }
  889. np = of_find_node_by_path("/testcase-data/platform-tests");
  890. unittest(np, "No testcase data in device tree\n");
  891. if (!np)
  892. return;
  893. test_bus = platform_device_register_full(&test_bus_info);
  894. rc = PTR_ERR_OR_ZERO(test_bus);
  895. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  896. if (rc)
  897. return;
  898. test_bus->dev.of_node = np;
  899. /*
  900. * Add a dummy resource to the test bus node after it is
  901. * registered to catch problems with un-inserted resources. The
  902. * DT code doesn't insert the resources, and it has caused the
  903. * kernel to oops in the past. This makes sure the same bug
  904. * doesn't crop up again.
  905. */
  906. platform_device_add_resources(test_bus, &test_bus_res, 1);
  907. of_platform_populate(np, match, NULL, &test_bus->dev);
  908. for_each_child_of_node(np, child) {
  909. for_each_child_of_node(child, grandchild) {
  910. pdev = of_find_device_by_node(grandchild);
  911. unittest(pdev,
  912. "Could not create device for node '%pOFn'\n",
  913. grandchild);
  914. of_dev_put(pdev);
  915. }
  916. }
  917. of_platform_depopulate(&test_bus->dev);
  918. for_each_child_of_node(np, child) {
  919. for_each_child_of_node(child, grandchild)
  920. unittest(!of_find_device_by_node(grandchild),
  921. "device didn't get destroyed '%pOFn'\n",
  922. grandchild);
  923. }
  924. platform_device_unregister(test_bus);
  925. of_node_put(np);
  926. }
  927. /**
  928. * update_node_properties - adds the properties
  929. * of np into dup node (present in live tree) and
  930. * updates parent of children of np to dup.
  931. *
  932. * @np: node whose properties are being added to the live tree
  933. * @dup: node present in live tree to be updated
  934. */
  935. static void update_node_properties(struct device_node *np,
  936. struct device_node *dup)
  937. {
  938. struct property *prop;
  939. struct property *save_next;
  940. struct device_node *child;
  941. int ret;
  942. for_each_child_of_node(np, child)
  943. child->parent = dup;
  944. /*
  945. * "unittest internal error: unable to add testdata property"
  946. *
  947. * If this message reports a property in node '/__symbols__' then
  948. * the respective unittest overlay contains a label that has the
  949. * same name as a label in the live devicetree. The label will
  950. * be in the live devicetree only if the devicetree source was
  951. * compiled with the '-@' option. If you encounter this error,
  952. * please consider renaming __all__ of the labels in the unittest
  953. * overlay dts files with an odd prefix that is unlikely to be
  954. * used in a real devicetree.
  955. */
  956. /*
  957. * open code for_each_property_of_node() because of_add_property()
  958. * sets prop->next to NULL
  959. */
  960. for (prop = np->properties; prop != NULL; prop = save_next) {
  961. save_next = prop->next;
  962. ret = of_add_property(dup, prop);
  963. if (ret)
  964. pr_err("unittest internal error: unable to add testdata property %pOF/%s",
  965. np, prop->name);
  966. }
  967. }
  968. /**
  969. * attach_node_and_children - attaches nodes
  970. * and its children to live tree
  971. *
  972. * @np: Node to attach to live tree
  973. */
  974. static void attach_node_and_children(struct device_node *np)
  975. {
  976. struct device_node *next, *dup, *child;
  977. unsigned long flags;
  978. const char *full_name;
  979. full_name = kasprintf(GFP_KERNEL, "%pOF", np);
  980. if (!strcmp(full_name, "/__local_fixups__") ||
  981. !strcmp(full_name, "/__fixups__")) {
  982. kfree(full_name);
  983. return;
  984. }
  985. dup = of_find_node_by_path(full_name);
  986. kfree(full_name);
  987. if (dup) {
  988. update_node_properties(np, dup);
  989. return;
  990. }
  991. child = np->child;
  992. np->child = NULL;
  993. mutex_lock(&of_mutex);
  994. raw_spin_lock_irqsave(&devtree_lock, flags);
  995. np->sibling = np->parent->child;
  996. np->parent->child = np;
  997. of_node_clear_flag(np, OF_DETACHED);
  998. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  999. __of_attach_node_sysfs(np);
  1000. mutex_unlock(&of_mutex);
  1001. while (child) {
  1002. next = child->sibling;
  1003. attach_node_and_children(child);
  1004. child = next;
  1005. }
  1006. }
  1007. /**
  1008. * unittest_data_add - Reads, copies data from
  1009. * linked tree and attaches it to the live tree
  1010. */
  1011. static int __init unittest_data_add(void)
  1012. {
  1013. void *unittest_data;
  1014. struct device_node *unittest_data_node, *np;
  1015. /*
  1016. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  1017. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  1018. */
  1019. extern uint8_t __dtb_testcases_begin[];
  1020. extern uint8_t __dtb_testcases_end[];
  1021. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  1022. int rc;
  1023. if (!size) {
  1024. pr_warn("%s: No testcase data to attach; not running tests\n",
  1025. __func__);
  1026. return -ENODATA;
  1027. }
  1028. /* creating copy */
  1029. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  1030. if (!unittest_data) {
  1031. pr_warn("%s: Failed to allocate memory for unittest_data; "
  1032. "not running tests\n", __func__);
  1033. return -ENOMEM;
  1034. }
  1035. of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
  1036. if (!unittest_data_node) {
  1037. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  1038. kfree(unittest_data);
  1039. return -ENODATA;
  1040. }
  1041. /*
  1042. * This lock normally encloses of_resolve_phandles()
  1043. */
  1044. of_overlay_mutex_lock();
  1045. rc = of_resolve_phandles(unittest_data_node);
  1046. if (rc) {
  1047. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  1048. of_overlay_mutex_unlock();
  1049. return -EINVAL;
  1050. }
  1051. if (!of_root) {
  1052. of_root = unittest_data_node;
  1053. for_each_of_allnodes(np)
  1054. __of_attach_node_sysfs(np);
  1055. of_aliases = of_find_node_by_path("/aliases");
  1056. of_chosen = of_find_node_by_path("/chosen");
  1057. of_overlay_mutex_unlock();
  1058. return 0;
  1059. }
  1060. /* attach the sub-tree to live tree */
  1061. np = unittest_data_node->child;
  1062. while (np) {
  1063. struct device_node *next = np->sibling;
  1064. np->parent = of_root;
  1065. attach_node_and_children(np);
  1066. np = next;
  1067. }
  1068. of_overlay_mutex_unlock();
  1069. return 0;
  1070. }
  1071. #ifdef CONFIG_OF_OVERLAY
  1072. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
  1073. static int unittest_probe(struct platform_device *pdev)
  1074. {
  1075. struct device *dev = &pdev->dev;
  1076. struct device_node *np = dev->of_node;
  1077. if (np == NULL) {
  1078. dev_err(dev, "No OF data for device\n");
  1079. return -EINVAL;
  1080. }
  1081. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1082. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1083. return 0;
  1084. }
  1085. static int unittest_remove(struct platform_device *pdev)
  1086. {
  1087. struct device *dev = &pdev->dev;
  1088. struct device_node *np = dev->of_node;
  1089. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1090. return 0;
  1091. }
  1092. static const struct of_device_id unittest_match[] = {
  1093. { .compatible = "unittest", },
  1094. {},
  1095. };
  1096. static struct platform_driver unittest_driver = {
  1097. .probe = unittest_probe,
  1098. .remove = unittest_remove,
  1099. .driver = {
  1100. .name = "unittest",
  1101. .of_match_table = of_match_ptr(unittest_match),
  1102. },
  1103. };
  1104. /* get the platform device instantiated at the path */
  1105. static struct platform_device *of_path_to_platform_device(const char *path)
  1106. {
  1107. struct device_node *np;
  1108. struct platform_device *pdev;
  1109. np = of_find_node_by_path(path);
  1110. if (np == NULL)
  1111. return NULL;
  1112. pdev = of_find_device_by_node(np);
  1113. of_node_put(np);
  1114. return pdev;
  1115. }
  1116. /* find out if a platform device exists at that path */
  1117. static int of_path_platform_device_exists(const char *path)
  1118. {
  1119. struct platform_device *pdev;
  1120. pdev = of_path_to_platform_device(path);
  1121. platform_device_put(pdev);
  1122. return pdev != NULL;
  1123. }
  1124. #if IS_BUILTIN(CONFIG_I2C)
  1125. /* get the i2c client device instantiated at the path */
  1126. static struct i2c_client *of_path_to_i2c_client(const char *path)
  1127. {
  1128. struct device_node *np;
  1129. struct i2c_client *client;
  1130. np = of_find_node_by_path(path);
  1131. if (np == NULL)
  1132. return NULL;
  1133. client = of_find_i2c_device_by_node(np);
  1134. of_node_put(np);
  1135. return client;
  1136. }
  1137. /* find out if a i2c client device exists at that path */
  1138. static int of_path_i2c_client_exists(const char *path)
  1139. {
  1140. struct i2c_client *client;
  1141. client = of_path_to_i2c_client(path);
  1142. if (client)
  1143. put_device(&client->dev);
  1144. return client != NULL;
  1145. }
  1146. #else
  1147. static int of_path_i2c_client_exists(const char *path)
  1148. {
  1149. return 0;
  1150. }
  1151. #endif
  1152. enum overlay_type {
  1153. PDEV_OVERLAY,
  1154. I2C_OVERLAY
  1155. };
  1156. static int of_path_device_type_exists(const char *path,
  1157. enum overlay_type ovtype)
  1158. {
  1159. switch (ovtype) {
  1160. case PDEV_OVERLAY:
  1161. return of_path_platform_device_exists(path);
  1162. case I2C_OVERLAY:
  1163. return of_path_i2c_client_exists(path);
  1164. }
  1165. return 0;
  1166. }
  1167. static const char *unittest_path(int nr, enum overlay_type ovtype)
  1168. {
  1169. const char *base;
  1170. static char buf[256];
  1171. switch (ovtype) {
  1172. case PDEV_OVERLAY:
  1173. base = "/testcase-data/overlay-node/test-bus";
  1174. break;
  1175. case I2C_OVERLAY:
  1176. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  1177. break;
  1178. default:
  1179. buf[0] = '\0';
  1180. return buf;
  1181. }
  1182. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  1183. buf[sizeof(buf) - 1] = '\0';
  1184. return buf;
  1185. }
  1186. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  1187. {
  1188. const char *path;
  1189. path = unittest_path(unittest_nr, ovtype);
  1190. switch (ovtype) {
  1191. case PDEV_OVERLAY:
  1192. return of_path_platform_device_exists(path);
  1193. case I2C_OVERLAY:
  1194. return of_path_i2c_client_exists(path);
  1195. }
  1196. return 0;
  1197. }
  1198. static const char *overlay_name_from_nr(int nr)
  1199. {
  1200. static char buf[256];
  1201. snprintf(buf, sizeof(buf) - 1,
  1202. "overlay_%d", nr);
  1203. buf[sizeof(buf) - 1] = '\0';
  1204. return buf;
  1205. }
  1206. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  1207. /* it is guaranteed that overlay ids are assigned in sequence */
  1208. #define MAX_UNITTEST_OVERLAYS 256
  1209. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  1210. static int overlay_first_id = -1;
  1211. static void of_unittest_track_overlay(int id)
  1212. {
  1213. if (overlay_first_id < 0)
  1214. overlay_first_id = id;
  1215. id -= overlay_first_id;
  1216. /* we shouldn't need that many */
  1217. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1218. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  1219. }
  1220. static void of_unittest_untrack_overlay(int id)
  1221. {
  1222. if (overlay_first_id < 0)
  1223. return;
  1224. id -= overlay_first_id;
  1225. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1226. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1227. }
  1228. static void of_unittest_destroy_tracked_overlays(void)
  1229. {
  1230. int id, ret, defers, ovcs_id;
  1231. if (overlay_first_id < 0)
  1232. return;
  1233. /* try until no defers */
  1234. do {
  1235. defers = 0;
  1236. /* remove in reverse order */
  1237. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  1238. if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
  1239. continue;
  1240. ovcs_id = id + overlay_first_id;
  1241. ret = of_overlay_remove(&ovcs_id);
  1242. if (ret == -ENODEV) {
  1243. pr_warn("%s: no overlay to destroy for #%d\n",
  1244. __func__, id + overlay_first_id);
  1245. continue;
  1246. }
  1247. if (ret != 0) {
  1248. defers++;
  1249. pr_warn("%s: overlay destroy failed for #%d\n",
  1250. __func__, id + overlay_first_id);
  1251. continue;
  1252. }
  1253. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1254. }
  1255. } while (defers > 0);
  1256. }
  1257. static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
  1258. int *overlay_id)
  1259. {
  1260. const char *overlay_name;
  1261. overlay_name = overlay_name_from_nr(overlay_nr);
  1262. if (!overlay_data_apply(overlay_name, overlay_id)) {
  1263. unittest(0, "could not apply overlay \"%s\"\n",
  1264. overlay_name);
  1265. return -EFAULT;
  1266. }
  1267. of_unittest_track_overlay(*overlay_id);
  1268. return 0;
  1269. }
  1270. /* apply an overlay while checking before and after states */
  1271. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  1272. int unittest_nr, int before, int after,
  1273. enum overlay_type ovtype)
  1274. {
  1275. int ret, ovcs_id;
  1276. /* unittest device must not be in before state */
  1277. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1278. unittest(0, "%s with device @\"%s\" %s\n",
  1279. overlay_name_from_nr(overlay_nr),
  1280. unittest_path(unittest_nr, ovtype),
  1281. !before ? "enabled" : "disabled");
  1282. return -EINVAL;
  1283. }
  1284. ovcs_id = 0;
  1285. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1286. if (ret != 0) {
  1287. /* of_unittest_apply_overlay already called unittest() */
  1288. return ret;
  1289. }
  1290. /* unittest device must be to set to after state */
  1291. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1292. unittest(0, "%s failed to create @\"%s\" %s\n",
  1293. overlay_name_from_nr(overlay_nr),
  1294. unittest_path(unittest_nr, ovtype),
  1295. !after ? "enabled" : "disabled");
  1296. return -EINVAL;
  1297. }
  1298. return 0;
  1299. }
  1300. /* apply an overlay and then revert it while checking before, after states */
  1301. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  1302. int unittest_nr, int before, int after,
  1303. enum overlay_type ovtype)
  1304. {
  1305. int ret, ovcs_id;
  1306. /* unittest device must be in before state */
  1307. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1308. unittest(0, "%s with device @\"%s\" %s\n",
  1309. overlay_name_from_nr(overlay_nr),
  1310. unittest_path(unittest_nr, ovtype),
  1311. !before ? "enabled" : "disabled");
  1312. return -EINVAL;
  1313. }
  1314. /* apply the overlay */
  1315. ovcs_id = 0;
  1316. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1317. if (ret != 0) {
  1318. /* of_unittest_apply_overlay already called unittest() */
  1319. return ret;
  1320. }
  1321. /* unittest device must be in after state */
  1322. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1323. unittest(0, "%s failed to create @\"%s\" %s\n",
  1324. overlay_name_from_nr(overlay_nr),
  1325. unittest_path(unittest_nr, ovtype),
  1326. !after ? "enabled" : "disabled");
  1327. return -EINVAL;
  1328. }
  1329. ret = of_overlay_remove(&ovcs_id);
  1330. if (ret != 0) {
  1331. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  1332. overlay_name_from_nr(overlay_nr),
  1333. unittest_path(unittest_nr, ovtype));
  1334. return ret;
  1335. }
  1336. /* unittest device must be again in before state */
  1337. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1338. unittest(0, "%s with device @\"%s\" %s\n",
  1339. overlay_name_from_nr(overlay_nr),
  1340. unittest_path(unittest_nr, ovtype),
  1341. !before ? "enabled" : "disabled");
  1342. return -EINVAL;
  1343. }
  1344. return 0;
  1345. }
  1346. /* test activation of device */
  1347. static void __init of_unittest_overlay_0(void)
  1348. {
  1349. /* device should enable */
  1350. if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
  1351. return;
  1352. unittest(1, "overlay test %d passed\n", 0);
  1353. }
  1354. /* test deactivation of device */
  1355. static void __init of_unittest_overlay_1(void)
  1356. {
  1357. /* device should disable */
  1358. if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
  1359. return;
  1360. unittest(1, "overlay test %d passed\n", 1);
  1361. }
  1362. /* test activation of device */
  1363. static void __init of_unittest_overlay_2(void)
  1364. {
  1365. /* device should enable */
  1366. if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
  1367. return;
  1368. unittest(1, "overlay test %d passed\n", 2);
  1369. }
  1370. /* test deactivation of device */
  1371. static void __init of_unittest_overlay_3(void)
  1372. {
  1373. /* device should disable */
  1374. if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
  1375. return;
  1376. unittest(1, "overlay test %d passed\n", 3);
  1377. }
  1378. /* test activation of a full device node */
  1379. static void __init of_unittest_overlay_4(void)
  1380. {
  1381. /* device should disable */
  1382. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  1383. return;
  1384. unittest(1, "overlay test %d passed\n", 4);
  1385. }
  1386. /* test overlay apply/revert sequence */
  1387. static void __init of_unittest_overlay_5(void)
  1388. {
  1389. /* device should disable */
  1390. if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
  1391. return;
  1392. unittest(1, "overlay test %d passed\n", 5);
  1393. }
  1394. /* test overlay application in sequence */
  1395. static void __init of_unittest_overlay_6(void)
  1396. {
  1397. int i, ov_id[2], ovcs_id;
  1398. int overlay_nr = 6, unittest_nr = 6;
  1399. int before = 0, after = 1;
  1400. const char *overlay_name;
  1401. /* unittest device must be in before state */
  1402. for (i = 0; i < 2; i++) {
  1403. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1404. != before) {
  1405. unittest(0, "%s with device @\"%s\" %s\n",
  1406. overlay_name_from_nr(overlay_nr + i),
  1407. unittest_path(unittest_nr + i,
  1408. PDEV_OVERLAY),
  1409. !before ? "enabled" : "disabled");
  1410. return;
  1411. }
  1412. }
  1413. /* apply the overlays */
  1414. for (i = 0; i < 2; i++) {
  1415. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1416. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1417. unittest(0, "could not apply overlay \"%s\"\n",
  1418. overlay_name);
  1419. return;
  1420. }
  1421. ov_id[i] = ovcs_id;
  1422. of_unittest_track_overlay(ov_id[i]);
  1423. }
  1424. for (i = 0; i < 2; i++) {
  1425. /* unittest device must be in after state */
  1426. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1427. != after) {
  1428. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1429. overlay_name_from_nr(overlay_nr + i),
  1430. unittest_path(unittest_nr + i,
  1431. PDEV_OVERLAY),
  1432. !after ? "enabled" : "disabled");
  1433. return;
  1434. }
  1435. }
  1436. for (i = 1; i >= 0; i--) {
  1437. ovcs_id = ov_id[i];
  1438. if (of_overlay_remove(&ovcs_id)) {
  1439. unittest(0, "%s failed destroy @\"%s\"\n",
  1440. overlay_name_from_nr(overlay_nr + i),
  1441. unittest_path(unittest_nr + i,
  1442. PDEV_OVERLAY));
  1443. return;
  1444. }
  1445. of_unittest_untrack_overlay(ov_id[i]);
  1446. }
  1447. for (i = 0; i < 2; i++) {
  1448. /* unittest device must be again in before state */
  1449. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1450. != before) {
  1451. unittest(0, "%s with device @\"%s\" %s\n",
  1452. overlay_name_from_nr(overlay_nr + i),
  1453. unittest_path(unittest_nr + i,
  1454. PDEV_OVERLAY),
  1455. !before ? "enabled" : "disabled");
  1456. return;
  1457. }
  1458. }
  1459. unittest(1, "overlay test %d passed\n", 6);
  1460. }
  1461. /* test overlay application in sequence */
  1462. static void __init of_unittest_overlay_8(void)
  1463. {
  1464. int i, ov_id[2], ovcs_id;
  1465. int overlay_nr = 8, unittest_nr = 8;
  1466. const char *overlay_name;
  1467. /* we don't care about device state in this test */
  1468. /* apply the overlays */
  1469. for (i = 0; i < 2; i++) {
  1470. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1471. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1472. unittest(0, "could not apply overlay \"%s\"\n",
  1473. overlay_name);
  1474. return;
  1475. }
  1476. ov_id[i] = ovcs_id;
  1477. of_unittest_track_overlay(ov_id[i]);
  1478. }
  1479. /* now try to remove first overlay (it should fail) */
  1480. ovcs_id = ov_id[0];
  1481. if (!of_overlay_remove(&ovcs_id)) {
  1482. unittest(0, "%s was destroyed @\"%s\"\n",
  1483. overlay_name_from_nr(overlay_nr + 0),
  1484. unittest_path(unittest_nr,
  1485. PDEV_OVERLAY));
  1486. return;
  1487. }
  1488. /* removing them in order should work */
  1489. for (i = 1; i >= 0; i--) {
  1490. ovcs_id = ov_id[i];
  1491. if (of_overlay_remove(&ovcs_id)) {
  1492. unittest(0, "%s not destroyed @\"%s\"\n",
  1493. overlay_name_from_nr(overlay_nr + i),
  1494. unittest_path(unittest_nr,
  1495. PDEV_OVERLAY));
  1496. return;
  1497. }
  1498. of_unittest_untrack_overlay(ov_id[i]);
  1499. }
  1500. unittest(1, "overlay test %d passed\n", 8);
  1501. }
  1502. /* test insertion of a bus with parent devices */
  1503. static void __init of_unittest_overlay_10(void)
  1504. {
  1505. int ret;
  1506. char *child_path;
  1507. /* device should disable */
  1508. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1509. if (unittest(ret == 0,
  1510. "overlay test %d failed; overlay application\n", 10))
  1511. return;
  1512. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1513. unittest_path(10, PDEV_OVERLAY));
  1514. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1515. return;
  1516. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1517. kfree(child_path);
  1518. unittest(ret, "overlay test %d failed; no child device\n", 10);
  1519. }
  1520. /* test insertion of a bus with parent devices (and revert) */
  1521. static void __init of_unittest_overlay_11(void)
  1522. {
  1523. int ret;
  1524. /* device should disable */
  1525. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1526. PDEV_OVERLAY);
  1527. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  1528. }
  1529. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1530. struct unittest_i2c_bus_data {
  1531. struct platform_device *pdev;
  1532. struct i2c_adapter adap;
  1533. };
  1534. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1535. struct i2c_msg *msgs, int num)
  1536. {
  1537. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1538. (void)std;
  1539. return num;
  1540. }
  1541. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1542. {
  1543. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1544. }
  1545. static const struct i2c_algorithm unittest_i2c_algo = {
  1546. .master_xfer = unittest_i2c_master_xfer,
  1547. .functionality = unittest_i2c_functionality,
  1548. };
  1549. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1550. {
  1551. struct device *dev = &pdev->dev;
  1552. struct device_node *np = dev->of_node;
  1553. struct unittest_i2c_bus_data *std;
  1554. struct i2c_adapter *adap;
  1555. int ret;
  1556. if (np == NULL) {
  1557. dev_err(dev, "No OF data for device\n");
  1558. return -EINVAL;
  1559. }
  1560. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1561. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  1562. if (!std) {
  1563. dev_err(dev, "Failed to allocate unittest i2c data\n");
  1564. return -ENOMEM;
  1565. }
  1566. /* link them together */
  1567. std->pdev = pdev;
  1568. platform_set_drvdata(pdev, std);
  1569. adap = &std->adap;
  1570. i2c_set_adapdata(adap, std);
  1571. adap->nr = -1;
  1572. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  1573. adap->class = I2C_CLASS_DEPRECATED;
  1574. adap->algo = &unittest_i2c_algo;
  1575. adap->dev.parent = dev;
  1576. adap->dev.of_node = dev->of_node;
  1577. adap->timeout = 5 * HZ;
  1578. adap->retries = 3;
  1579. ret = i2c_add_numbered_adapter(adap);
  1580. if (ret != 0) {
  1581. dev_err(dev, "Failed to add I2C adapter\n");
  1582. return ret;
  1583. }
  1584. return 0;
  1585. }
  1586. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  1587. {
  1588. struct device *dev = &pdev->dev;
  1589. struct device_node *np = dev->of_node;
  1590. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  1591. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1592. i2c_del_adapter(&std->adap);
  1593. return 0;
  1594. }
  1595. static const struct of_device_id unittest_i2c_bus_match[] = {
  1596. { .compatible = "unittest-i2c-bus", },
  1597. {},
  1598. };
  1599. static struct platform_driver unittest_i2c_bus_driver = {
  1600. .probe = unittest_i2c_bus_probe,
  1601. .remove = unittest_i2c_bus_remove,
  1602. .driver = {
  1603. .name = "unittest-i2c-bus",
  1604. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  1605. },
  1606. };
  1607. static int unittest_i2c_dev_probe(struct i2c_client *client,
  1608. const struct i2c_device_id *id)
  1609. {
  1610. struct device *dev = &client->dev;
  1611. struct device_node *np = client->dev.of_node;
  1612. if (!np) {
  1613. dev_err(dev, "No OF node\n");
  1614. return -EINVAL;
  1615. }
  1616. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1617. return 0;
  1618. };
  1619. static int unittest_i2c_dev_remove(struct i2c_client *client)
  1620. {
  1621. struct device *dev = &client->dev;
  1622. struct device_node *np = client->dev.of_node;
  1623. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1624. return 0;
  1625. }
  1626. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  1627. { .name = "unittest-i2c-dev" },
  1628. { }
  1629. };
  1630. static struct i2c_driver unittest_i2c_dev_driver = {
  1631. .driver = {
  1632. .name = "unittest-i2c-dev",
  1633. },
  1634. .probe = unittest_i2c_dev_probe,
  1635. .remove = unittest_i2c_dev_remove,
  1636. .id_table = unittest_i2c_dev_id,
  1637. };
  1638. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1639. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  1640. {
  1641. return 0;
  1642. }
  1643. static int unittest_i2c_mux_probe(struct i2c_client *client,
  1644. const struct i2c_device_id *id)
  1645. {
  1646. int i, nchans;
  1647. struct device *dev = &client->dev;
  1648. struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
  1649. struct device_node *np = client->dev.of_node, *child;
  1650. struct i2c_mux_core *muxc;
  1651. u32 reg, max_reg;
  1652. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1653. if (!np) {
  1654. dev_err(dev, "No OF node\n");
  1655. return -EINVAL;
  1656. }
  1657. max_reg = (u32)-1;
  1658. for_each_child_of_node(np, child) {
  1659. if (of_property_read_u32(child, "reg", &reg))
  1660. continue;
  1661. if (max_reg == (u32)-1 || reg > max_reg)
  1662. max_reg = reg;
  1663. }
  1664. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  1665. if (nchans == 0) {
  1666. dev_err(dev, "No channels\n");
  1667. return -EINVAL;
  1668. }
  1669. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  1670. unittest_i2c_mux_select_chan, NULL);
  1671. if (!muxc)
  1672. return -ENOMEM;
  1673. for (i = 0; i < nchans; i++) {
  1674. if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
  1675. dev_err(dev, "Failed to register mux #%d\n", i);
  1676. i2c_mux_del_adapters(muxc);
  1677. return -ENODEV;
  1678. }
  1679. }
  1680. i2c_set_clientdata(client, muxc);
  1681. return 0;
  1682. };
  1683. static int unittest_i2c_mux_remove(struct i2c_client *client)
  1684. {
  1685. struct device *dev = &client->dev;
  1686. struct device_node *np = client->dev.of_node;
  1687. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  1688. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1689. i2c_mux_del_adapters(muxc);
  1690. return 0;
  1691. }
  1692. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  1693. { .name = "unittest-i2c-mux" },
  1694. { }
  1695. };
  1696. static struct i2c_driver unittest_i2c_mux_driver = {
  1697. .driver = {
  1698. .name = "unittest-i2c-mux",
  1699. },
  1700. .probe = unittest_i2c_mux_probe,
  1701. .remove = unittest_i2c_mux_remove,
  1702. .id_table = unittest_i2c_mux_id,
  1703. };
  1704. #endif
  1705. static int of_unittest_overlay_i2c_init(void)
  1706. {
  1707. int ret;
  1708. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  1709. if (unittest(ret == 0,
  1710. "could not register unittest i2c device driver\n"))
  1711. return ret;
  1712. ret = platform_driver_register(&unittest_i2c_bus_driver);
  1713. if (unittest(ret == 0,
  1714. "could not register unittest i2c bus driver\n"))
  1715. return ret;
  1716. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1717. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  1718. if (unittest(ret == 0,
  1719. "could not register unittest i2c mux driver\n"))
  1720. return ret;
  1721. #endif
  1722. return 0;
  1723. }
  1724. static void of_unittest_overlay_i2c_cleanup(void)
  1725. {
  1726. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1727. i2c_del_driver(&unittest_i2c_mux_driver);
  1728. #endif
  1729. platform_driver_unregister(&unittest_i2c_bus_driver);
  1730. i2c_del_driver(&unittest_i2c_dev_driver);
  1731. }
  1732. static void __init of_unittest_overlay_i2c_12(void)
  1733. {
  1734. /* device should enable */
  1735. if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
  1736. return;
  1737. unittest(1, "overlay test %d passed\n", 12);
  1738. }
  1739. /* test deactivation of device */
  1740. static void __init of_unittest_overlay_i2c_13(void)
  1741. {
  1742. /* device should disable */
  1743. if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
  1744. return;
  1745. unittest(1, "overlay test %d passed\n", 13);
  1746. }
  1747. /* just check for i2c mux existence */
  1748. static void of_unittest_overlay_i2c_14(void)
  1749. {
  1750. }
  1751. static void __init of_unittest_overlay_i2c_15(void)
  1752. {
  1753. /* device should enable */
  1754. if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
  1755. return;
  1756. unittest(1, "overlay test %d passed\n", 15);
  1757. }
  1758. #else
  1759. static inline void of_unittest_overlay_i2c_14(void) { }
  1760. static inline void of_unittest_overlay_i2c_15(void) { }
  1761. #endif
  1762. static void __init of_unittest_overlay(void)
  1763. {
  1764. struct device_node *bus_np = NULL;
  1765. if (platform_driver_register(&unittest_driver)) {
  1766. unittest(0, "could not register unittest driver\n");
  1767. goto out;
  1768. }
  1769. bus_np = of_find_node_by_path(bus_path);
  1770. if (bus_np == NULL) {
  1771. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  1772. goto out;
  1773. }
  1774. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  1775. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1776. goto out;
  1777. }
  1778. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  1779. unittest(0, "could not find unittest0 @ \"%s\"\n",
  1780. unittest_path(100, PDEV_OVERLAY));
  1781. goto out;
  1782. }
  1783. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  1784. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  1785. unittest_path(101, PDEV_OVERLAY));
  1786. goto out;
  1787. }
  1788. unittest(1, "basic infrastructure of overlays passed");
  1789. /* tests in sequence */
  1790. of_unittest_overlay_0();
  1791. of_unittest_overlay_1();
  1792. of_unittest_overlay_2();
  1793. of_unittest_overlay_3();
  1794. of_unittest_overlay_4();
  1795. of_unittest_overlay_5();
  1796. of_unittest_overlay_6();
  1797. of_unittest_overlay_8();
  1798. of_unittest_overlay_10();
  1799. of_unittest_overlay_11();
  1800. #if IS_BUILTIN(CONFIG_I2C)
  1801. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  1802. goto out;
  1803. of_unittest_overlay_i2c_12();
  1804. of_unittest_overlay_i2c_13();
  1805. of_unittest_overlay_i2c_14();
  1806. of_unittest_overlay_i2c_15();
  1807. of_unittest_overlay_i2c_cleanup();
  1808. #endif
  1809. of_unittest_destroy_tracked_overlays();
  1810. out:
  1811. of_node_put(bus_np);
  1812. }
  1813. #else
  1814. static inline void __init of_unittest_overlay(void) { }
  1815. #endif
  1816. #ifdef CONFIG_OF_OVERLAY
  1817. /*
  1818. * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
  1819. * in scripts/Makefile.lib
  1820. */
  1821. #define OVERLAY_INFO_EXTERN(name) \
  1822. extern uint8_t __dtb_##name##_begin[]; \
  1823. extern uint8_t __dtb_##name##_end[]
  1824. #define OVERLAY_INFO(overlay_name, expected) \
  1825. { .dtb_begin = __dtb_##overlay_name##_begin, \
  1826. .dtb_end = __dtb_##overlay_name##_end, \
  1827. .expected_result = expected, \
  1828. .name = #overlay_name, \
  1829. }
  1830. struct overlay_info {
  1831. uint8_t *dtb_begin;
  1832. uint8_t *dtb_end;
  1833. int expected_result;
  1834. int overlay_id;
  1835. char *name;
  1836. };
  1837. OVERLAY_INFO_EXTERN(overlay_base);
  1838. OVERLAY_INFO_EXTERN(overlay);
  1839. OVERLAY_INFO_EXTERN(overlay_0);
  1840. OVERLAY_INFO_EXTERN(overlay_1);
  1841. OVERLAY_INFO_EXTERN(overlay_2);
  1842. OVERLAY_INFO_EXTERN(overlay_3);
  1843. OVERLAY_INFO_EXTERN(overlay_4);
  1844. OVERLAY_INFO_EXTERN(overlay_5);
  1845. OVERLAY_INFO_EXTERN(overlay_6);
  1846. OVERLAY_INFO_EXTERN(overlay_7);
  1847. OVERLAY_INFO_EXTERN(overlay_8);
  1848. OVERLAY_INFO_EXTERN(overlay_9);
  1849. OVERLAY_INFO_EXTERN(overlay_10);
  1850. OVERLAY_INFO_EXTERN(overlay_11);
  1851. OVERLAY_INFO_EXTERN(overlay_12);
  1852. OVERLAY_INFO_EXTERN(overlay_13);
  1853. OVERLAY_INFO_EXTERN(overlay_15);
  1854. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  1855. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  1856. /* order of entries is hard-coded into users of overlays[] */
  1857. static struct overlay_info overlays[] = {
  1858. OVERLAY_INFO(overlay_base, -9999),
  1859. OVERLAY_INFO(overlay, 0),
  1860. OVERLAY_INFO(overlay_0, 0),
  1861. OVERLAY_INFO(overlay_1, 0),
  1862. OVERLAY_INFO(overlay_2, 0),
  1863. OVERLAY_INFO(overlay_3, 0),
  1864. OVERLAY_INFO(overlay_4, 0),
  1865. OVERLAY_INFO(overlay_5, 0),
  1866. OVERLAY_INFO(overlay_6, 0),
  1867. OVERLAY_INFO(overlay_7, 0),
  1868. OVERLAY_INFO(overlay_8, 0),
  1869. OVERLAY_INFO(overlay_9, 0),
  1870. OVERLAY_INFO(overlay_10, 0),
  1871. OVERLAY_INFO(overlay_11, 0),
  1872. OVERLAY_INFO(overlay_12, 0),
  1873. OVERLAY_INFO(overlay_13, 0),
  1874. OVERLAY_INFO(overlay_15, 0),
  1875. OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
  1876. OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
  1877. {}
  1878. };
  1879. static struct device_node *overlay_base_root;
  1880. static void * __init dt_alloc_memory(u64 size, u64 align)
  1881. {
  1882. return memblock_virt_alloc(size, align);
  1883. }
  1884. /*
  1885. * Create base device tree for the overlay unittest.
  1886. *
  1887. * This is called from very early boot code.
  1888. *
  1889. * Do as much as possible the same way as done in __unflatten_device_tree
  1890. * and other early boot steps for the normal FDT so that the overlay base
  1891. * unflattened tree will have the same characteristics as the real tree
  1892. * (such as having memory allocated by the early allocator). The goal
  1893. * is to test "the real thing" as much as possible, and test "test setup
  1894. * code" as little as possible.
  1895. *
  1896. * Have to stop before resolving phandles, because that uses kmalloc.
  1897. */
  1898. void __init unittest_unflatten_overlay_base(void)
  1899. {
  1900. struct overlay_info *info;
  1901. u32 data_size;
  1902. void *new_fdt;
  1903. u32 size;
  1904. info = &overlays[0];
  1905. if (info->expected_result != -9999) {
  1906. pr_err("No dtb 'overlay_base' to attach\n");
  1907. return;
  1908. }
  1909. data_size = info->dtb_end - info->dtb_begin;
  1910. if (!data_size) {
  1911. pr_err("No dtb 'overlay_base' to attach\n");
  1912. return;
  1913. }
  1914. size = fdt_totalsize(info->dtb_begin);
  1915. if (size != data_size) {
  1916. pr_err("dtb 'overlay_base' header totalsize != actual size");
  1917. return;
  1918. }
  1919. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  1920. if (!new_fdt) {
  1921. pr_err("alloc for dtb 'overlay_base' failed");
  1922. return;
  1923. }
  1924. memcpy(new_fdt, info->dtb_begin, size);
  1925. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  1926. dt_alloc_memory, true);
  1927. }
  1928. /*
  1929. * The purpose of of_unittest_overlay_data_add is to add an
  1930. * overlay in the normal fashion. This is a test of the whole
  1931. * picture, instead of testing individual elements.
  1932. *
  1933. * A secondary purpose is to be able to verify that the contents of
  1934. * /proc/device-tree/ contains the updated structure and values from
  1935. * the overlay. That must be verified separately in user space.
  1936. *
  1937. * Return 0 on unexpected error.
  1938. */
  1939. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
  1940. {
  1941. struct overlay_info *info;
  1942. int found = 0;
  1943. int k;
  1944. int ret;
  1945. u32 size;
  1946. for (k = 0, info = overlays; info && info->name; info++, k++) {
  1947. if (!strcmp(overlay_name, info->name)) {
  1948. found = 1;
  1949. break;
  1950. }
  1951. }
  1952. if (!found) {
  1953. pr_err("no overlay data for %s\n", overlay_name);
  1954. return 0;
  1955. }
  1956. size = info->dtb_end - info->dtb_begin;
  1957. if (!size)
  1958. pr_err("no overlay data for %s\n", overlay_name);
  1959. ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
  1960. if (overlay_id)
  1961. *overlay_id = info->overlay_id;
  1962. if (ret < 0)
  1963. goto out;
  1964. pr_debug("%s applied\n", overlay_name);
  1965. out:
  1966. if (ret != info->expected_result)
  1967. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  1968. info->expected_result, ret, overlay_name);
  1969. return (ret == info->expected_result);
  1970. }
  1971. /*
  1972. * The purpose of of_unittest_overlay_high_level is to add an overlay
  1973. * in the normal fashion. This is a test of the whole picture,
  1974. * instead of individual elements.
  1975. *
  1976. * The first part of the function is _not_ normal overlay usage; it is
  1977. * finishing splicing the base overlay device tree into the live tree.
  1978. */
  1979. static __init void of_unittest_overlay_high_level(void)
  1980. {
  1981. struct device_node *last_sibling;
  1982. struct device_node *np;
  1983. struct device_node *of_symbols;
  1984. struct device_node *overlay_base_symbols;
  1985. struct device_node **pprev;
  1986. struct property *prop;
  1987. if (!overlay_base_root) {
  1988. unittest(0, "overlay_base_root not initialized\n");
  1989. return;
  1990. }
  1991. /*
  1992. * Could not fixup phandles in unittest_unflatten_overlay_base()
  1993. * because kmalloc() was not yet available.
  1994. */
  1995. of_overlay_mutex_lock();
  1996. of_resolve_phandles(overlay_base_root);
  1997. of_overlay_mutex_unlock();
  1998. /*
  1999. * do not allow overlay_base to duplicate any node already in
  2000. * tree, this greatly simplifies the code
  2001. */
  2002. /*
  2003. * remove overlay_base_root node "__local_fixups", after
  2004. * being used by of_resolve_phandles()
  2005. */
  2006. pprev = &overlay_base_root->child;
  2007. for (np = overlay_base_root->child; np; np = np->sibling) {
  2008. if (!of_node_cmp(np->name, "__local_fixups__")) {
  2009. *pprev = np->sibling;
  2010. break;
  2011. }
  2012. pprev = &np->sibling;
  2013. }
  2014. /* remove overlay_base_root node "__symbols__" if in live tree */
  2015. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  2016. if (of_symbols) {
  2017. /* will have to graft properties from node into live tree */
  2018. pprev = &overlay_base_root->child;
  2019. for (np = overlay_base_root->child; np; np = np->sibling) {
  2020. if (!of_node_cmp(np->name, "__symbols__")) {
  2021. overlay_base_symbols = np;
  2022. *pprev = np->sibling;
  2023. break;
  2024. }
  2025. pprev = &np->sibling;
  2026. }
  2027. }
  2028. for (np = overlay_base_root->child; np; np = np->sibling) {
  2029. if (of_get_child_by_name(of_root, np->name)) {
  2030. unittest(0, "illegal node name in overlay_base %s",
  2031. np->name);
  2032. return;
  2033. }
  2034. }
  2035. /*
  2036. * overlay 'overlay_base' is not allowed to have root
  2037. * properties, so only need to splice nodes into main device tree.
  2038. *
  2039. * root node of *overlay_base_root will not be freed, it is lost
  2040. * memory.
  2041. */
  2042. for (np = overlay_base_root->child; np; np = np->sibling)
  2043. np->parent = of_root;
  2044. mutex_lock(&of_mutex);
  2045. for (last_sibling = np = of_root->child; np; np = np->sibling)
  2046. last_sibling = np;
  2047. if (last_sibling)
  2048. last_sibling->sibling = overlay_base_root->child;
  2049. else
  2050. of_root->child = overlay_base_root->child;
  2051. for_each_of_allnodes_from(overlay_base_root, np)
  2052. __of_attach_node_sysfs(np);
  2053. if (of_symbols) {
  2054. struct property *new_prop;
  2055. for_each_property_of_node(overlay_base_symbols, prop) {
  2056. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  2057. if (!new_prop) {
  2058. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  2059. prop->name);
  2060. goto err_unlock;
  2061. }
  2062. if (__of_add_property(of_symbols, new_prop)) {
  2063. kfree(new_prop->name);
  2064. kfree(new_prop->value);
  2065. kfree(new_prop);
  2066. /* "name" auto-generated by unflatten */
  2067. if (!strcmp(prop->name, "name"))
  2068. continue;
  2069. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  2070. prop->name);
  2071. goto err_unlock;
  2072. }
  2073. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  2074. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  2075. prop->name);
  2076. goto err_unlock;
  2077. }
  2078. }
  2079. }
  2080. mutex_unlock(&of_mutex);
  2081. /* now do the normal overlay usage test */
  2082. unittest(overlay_data_apply("overlay", NULL),
  2083. "Adding overlay 'overlay' failed\n");
  2084. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  2085. "Adding overlay 'overlay_bad_phandle' failed\n");
  2086. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  2087. "Adding overlay 'overlay_bad_symbol' failed\n");
  2088. return;
  2089. err_unlock:
  2090. mutex_unlock(&of_mutex);
  2091. }
  2092. #else
  2093. static inline __init void of_unittest_overlay_high_level(void) {}
  2094. #endif
  2095. static int __init of_unittest(void)
  2096. {
  2097. struct device_node *np;
  2098. int res;
  2099. /* adding data for unittest */
  2100. res = unittest_data_add();
  2101. if (res)
  2102. return res;
  2103. if (!of_aliases)
  2104. of_aliases = of_find_node_by_path("/aliases");
  2105. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  2106. if (!np) {
  2107. pr_info("No testcase data in device tree; not running tests\n");
  2108. return 0;
  2109. }
  2110. of_node_put(np);
  2111. pr_info("start of unittest - you will see error messages\n");
  2112. of_unittest_check_tree_linkage();
  2113. of_unittest_check_phandles();
  2114. of_unittest_find_node_by_name();
  2115. of_unittest_dynamic();
  2116. of_unittest_parse_phandle_with_args();
  2117. of_unittest_parse_phandle_with_args_map();
  2118. of_unittest_printf();
  2119. of_unittest_property_string();
  2120. of_unittest_property_copy();
  2121. of_unittest_changeset();
  2122. of_unittest_parse_interrupts();
  2123. of_unittest_parse_interrupts_extended();
  2124. of_unittest_match_node();
  2125. of_unittest_platform_populate();
  2126. of_unittest_overlay();
  2127. /* Double check linkage after removing testcase data */
  2128. of_unittest_check_tree_linkage();
  2129. of_unittest_overlay_high_level();
  2130. pr_info("end of unittest - %i passed, %i failed\n",
  2131. unittest_results.passed, unittest_results.failed);
  2132. return 0;
  2133. }
  2134. late_initcall(of_unittest);