unittest.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  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. #endif
  659. }
  660. static void __init of_unittest_parse_interrupts(void)
  661. {
  662. struct device_node *np;
  663. struct of_phandle_args args;
  664. int i, rc;
  665. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  666. return;
  667. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  668. if (!np) {
  669. pr_err("missing testcase data\n");
  670. return;
  671. }
  672. for (i = 0; i < 4; i++) {
  673. bool passed = true;
  674. memset(&args, 0, sizeof(args));
  675. rc = of_irq_parse_one(np, i, &args);
  676. passed &= !rc;
  677. passed &= (args.args_count == 1);
  678. passed &= (args.args[0] == (i + 1));
  679. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  680. i, args.np, rc);
  681. }
  682. of_node_put(np);
  683. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  684. if (!np) {
  685. pr_err("missing testcase data\n");
  686. return;
  687. }
  688. for (i = 0; i < 4; i++) {
  689. bool passed = true;
  690. memset(&args, 0, sizeof(args));
  691. rc = of_irq_parse_one(np, i, &args);
  692. /* Test the values from tests-phandle.dtsi */
  693. switch (i) {
  694. case 0:
  695. passed &= !rc;
  696. passed &= (args.args_count == 1);
  697. passed &= (args.args[0] == 9);
  698. break;
  699. case 1:
  700. passed &= !rc;
  701. passed &= (args.args_count == 3);
  702. passed &= (args.args[0] == 10);
  703. passed &= (args.args[1] == 11);
  704. passed &= (args.args[2] == 12);
  705. break;
  706. case 2:
  707. passed &= !rc;
  708. passed &= (args.args_count == 2);
  709. passed &= (args.args[0] == 13);
  710. passed &= (args.args[1] == 14);
  711. break;
  712. case 3:
  713. passed &= !rc;
  714. passed &= (args.args_count == 2);
  715. passed &= (args.args[0] == 15);
  716. passed &= (args.args[1] == 16);
  717. break;
  718. default:
  719. passed = false;
  720. }
  721. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  722. i, args.np, rc);
  723. }
  724. of_node_put(np);
  725. }
  726. static void __init of_unittest_parse_interrupts_extended(void)
  727. {
  728. struct device_node *np;
  729. struct of_phandle_args args;
  730. int i, rc;
  731. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  732. return;
  733. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  734. if (!np) {
  735. pr_err("missing testcase data\n");
  736. return;
  737. }
  738. for (i = 0; i < 7; i++) {
  739. bool passed = true;
  740. memset(&args, 0, sizeof(args));
  741. rc = of_irq_parse_one(np, i, &args);
  742. /* Test the values from tests-phandle.dtsi */
  743. switch (i) {
  744. case 0:
  745. passed &= !rc;
  746. passed &= (args.args_count == 1);
  747. passed &= (args.args[0] == 1);
  748. break;
  749. case 1:
  750. passed &= !rc;
  751. passed &= (args.args_count == 3);
  752. passed &= (args.args[0] == 2);
  753. passed &= (args.args[1] == 3);
  754. passed &= (args.args[2] == 4);
  755. break;
  756. case 2:
  757. passed &= !rc;
  758. passed &= (args.args_count == 2);
  759. passed &= (args.args[0] == 5);
  760. passed &= (args.args[1] == 6);
  761. break;
  762. case 3:
  763. passed &= !rc;
  764. passed &= (args.args_count == 1);
  765. passed &= (args.args[0] == 9);
  766. break;
  767. case 4:
  768. passed &= !rc;
  769. passed &= (args.args_count == 3);
  770. passed &= (args.args[0] == 10);
  771. passed &= (args.args[1] == 11);
  772. passed &= (args.args[2] == 12);
  773. break;
  774. case 5:
  775. passed &= !rc;
  776. passed &= (args.args_count == 2);
  777. passed &= (args.args[0] == 13);
  778. passed &= (args.args[1] == 14);
  779. break;
  780. case 6:
  781. passed &= !rc;
  782. passed &= (args.args_count == 1);
  783. passed &= (args.args[0] == 15);
  784. break;
  785. default:
  786. passed = false;
  787. }
  788. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  789. i, args.np, rc);
  790. }
  791. of_node_put(np);
  792. }
  793. static const struct of_device_id match_node_table[] = {
  794. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  795. { .data = "B", .type = "type1", }, /* followed by type alone */
  796. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  797. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  798. { .data = "Cc", .name = "name2", .type = "type2", },
  799. { .data = "E", .compatible = "compat3" },
  800. { .data = "G", .compatible = "compat2", },
  801. { .data = "H", .compatible = "compat2", .name = "name5", },
  802. { .data = "I", .compatible = "compat2", .type = "type1", },
  803. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  804. { .data = "K", .compatible = "compat2", .name = "name9", },
  805. {}
  806. };
  807. static struct {
  808. const char *path;
  809. const char *data;
  810. } match_node_tests[] = {
  811. { .path = "/testcase-data/match-node/name0", .data = "A", },
  812. { .path = "/testcase-data/match-node/name1", .data = "B", },
  813. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  814. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  815. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  816. { .path = "/testcase-data/match-node/name3", .data = "E", },
  817. { .path = "/testcase-data/match-node/name4", .data = "G", },
  818. { .path = "/testcase-data/match-node/name5", .data = "H", },
  819. { .path = "/testcase-data/match-node/name6", .data = "G", },
  820. { .path = "/testcase-data/match-node/name7", .data = "I", },
  821. { .path = "/testcase-data/match-node/name8", .data = "J", },
  822. { .path = "/testcase-data/match-node/name9", .data = "K", },
  823. };
  824. static void __init of_unittest_match_node(void)
  825. {
  826. struct device_node *np;
  827. const struct of_device_id *match;
  828. int i;
  829. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  830. np = of_find_node_by_path(match_node_tests[i].path);
  831. if (!np) {
  832. unittest(0, "missing testcase node %s\n",
  833. match_node_tests[i].path);
  834. continue;
  835. }
  836. match = of_match_node(match_node_table, np);
  837. if (!match) {
  838. unittest(0, "%s didn't match anything\n",
  839. match_node_tests[i].path);
  840. continue;
  841. }
  842. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  843. unittest(0, "%s got wrong match. expected %s, got %s\n",
  844. match_node_tests[i].path, match_node_tests[i].data,
  845. (const char *)match->data);
  846. continue;
  847. }
  848. unittest(1, "passed");
  849. }
  850. }
  851. static struct resource test_bus_res = {
  852. .start = 0xfffffff8,
  853. .end = 0xfffffff9,
  854. .flags = IORESOURCE_MEM,
  855. };
  856. static const struct platform_device_info test_bus_info = {
  857. .name = "unittest-bus",
  858. };
  859. static void __init of_unittest_platform_populate(void)
  860. {
  861. int irq, rc;
  862. struct device_node *np, *child, *grandchild;
  863. struct platform_device *pdev, *test_bus;
  864. const struct of_device_id match[] = {
  865. { .compatible = "test-device", },
  866. {}
  867. };
  868. np = of_find_node_by_path("/testcase-data");
  869. of_platform_default_populate(np, NULL, NULL);
  870. /* Test that a missing irq domain returns -EPROBE_DEFER */
  871. np = of_find_node_by_path("/testcase-data/testcase-device1");
  872. pdev = of_find_device_by_node(np);
  873. unittest(pdev, "device 1 creation failed\n");
  874. if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
  875. irq = platform_get_irq(pdev, 0);
  876. unittest(irq == -EPROBE_DEFER,
  877. "device deferred probe failed - %d\n", irq);
  878. /* Test that a parsing failure does not return -EPROBE_DEFER */
  879. np = of_find_node_by_path("/testcase-data/testcase-device2");
  880. pdev = of_find_device_by_node(np);
  881. unittest(pdev, "device 2 creation failed\n");
  882. irq = platform_get_irq(pdev, 0);
  883. unittest(irq < 0 && irq != -EPROBE_DEFER,
  884. "device parsing error failed - %d\n", irq);
  885. }
  886. np = of_find_node_by_path("/testcase-data/platform-tests");
  887. unittest(np, "No testcase data in device tree\n");
  888. if (!np)
  889. return;
  890. test_bus = platform_device_register_full(&test_bus_info);
  891. rc = PTR_ERR_OR_ZERO(test_bus);
  892. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  893. if (rc)
  894. return;
  895. test_bus->dev.of_node = np;
  896. /*
  897. * Add a dummy resource to the test bus node after it is
  898. * registered to catch problems with un-inserted resources. The
  899. * DT code doesn't insert the resources, and it has caused the
  900. * kernel to oops in the past. This makes sure the same bug
  901. * doesn't crop up again.
  902. */
  903. platform_device_add_resources(test_bus, &test_bus_res, 1);
  904. of_platform_populate(np, match, NULL, &test_bus->dev);
  905. for_each_child_of_node(np, child) {
  906. for_each_child_of_node(child, grandchild)
  907. unittest(of_find_device_by_node(grandchild),
  908. "Could not create device for node '%pOFn'\n",
  909. grandchild);
  910. }
  911. of_platform_depopulate(&test_bus->dev);
  912. for_each_child_of_node(np, child) {
  913. for_each_child_of_node(child, grandchild)
  914. unittest(!of_find_device_by_node(grandchild),
  915. "device didn't get destroyed '%pOFn'\n",
  916. grandchild);
  917. }
  918. platform_device_unregister(test_bus);
  919. of_node_put(np);
  920. }
  921. /**
  922. * update_node_properties - adds the properties
  923. * of np into dup node (present in live tree) and
  924. * updates parent of children of np to dup.
  925. *
  926. * @np: node whose properties are being added to the live tree
  927. * @dup: node present in live tree to be updated
  928. */
  929. static void update_node_properties(struct device_node *np,
  930. struct device_node *dup)
  931. {
  932. struct property *prop;
  933. struct property *save_next;
  934. struct device_node *child;
  935. int ret;
  936. for_each_child_of_node(np, child)
  937. child->parent = dup;
  938. /*
  939. * "unittest internal error: unable to add testdata property"
  940. *
  941. * If this message reports a property in node '/__symbols__' then
  942. * the respective unittest overlay contains a label that has the
  943. * same name as a label in the live devicetree. The label will
  944. * be in the live devicetree only if the devicetree source was
  945. * compiled with the '-@' option. If you encounter this error,
  946. * please consider renaming __all__ of the labels in the unittest
  947. * overlay dts files with an odd prefix that is unlikely to be
  948. * used in a real devicetree.
  949. */
  950. /*
  951. * open code for_each_property_of_node() because of_add_property()
  952. * sets prop->next to NULL
  953. */
  954. for (prop = np->properties; prop != NULL; prop = save_next) {
  955. save_next = prop->next;
  956. ret = of_add_property(dup, prop);
  957. if (ret)
  958. pr_err("unittest internal error: unable to add testdata property %pOF/%s",
  959. np, prop->name);
  960. }
  961. }
  962. /**
  963. * attach_node_and_children - attaches nodes
  964. * and its children to live tree
  965. *
  966. * @np: Node to attach to live tree
  967. */
  968. static void attach_node_and_children(struct device_node *np)
  969. {
  970. struct device_node *next, *dup, *child;
  971. unsigned long flags;
  972. const char *full_name;
  973. full_name = kasprintf(GFP_KERNEL, "%pOF", np);
  974. if (!strcmp(full_name, "/__local_fixups__") ||
  975. !strcmp(full_name, "/__fixups__")) {
  976. kfree(full_name);
  977. return;
  978. }
  979. dup = of_find_node_by_path(full_name);
  980. kfree(full_name);
  981. if (dup) {
  982. update_node_properties(np, dup);
  983. return;
  984. }
  985. child = np->child;
  986. np->child = NULL;
  987. mutex_lock(&of_mutex);
  988. raw_spin_lock_irqsave(&devtree_lock, flags);
  989. np->sibling = np->parent->child;
  990. np->parent->child = np;
  991. of_node_clear_flag(np, OF_DETACHED);
  992. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  993. __of_attach_node_sysfs(np);
  994. mutex_unlock(&of_mutex);
  995. while (child) {
  996. next = child->sibling;
  997. attach_node_and_children(child);
  998. child = next;
  999. }
  1000. }
  1001. /**
  1002. * unittest_data_add - Reads, copies data from
  1003. * linked tree and attaches it to the live tree
  1004. */
  1005. static int __init unittest_data_add(void)
  1006. {
  1007. void *unittest_data;
  1008. struct device_node *unittest_data_node, *np;
  1009. /*
  1010. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  1011. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  1012. */
  1013. extern uint8_t __dtb_testcases_begin[];
  1014. extern uint8_t __dtb_testcases_end[];
  1015. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  1016. int rc;
  1017. if (!size) {
  1018. pr_warn("%s: No testcase data to attach; not running tests\n",
  1019. __func__);
  1020. return -ENODATA;
  1021. }
  1022. /* creating copy */
  1023. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  1024. if (!unittest_data) {
  1025. pr_warn("%s: Failed to allocate memory for unittest_data; "
  1026. "not running tests\n", __func__);
  1027. return -ENOMEM;
  1028. }
  1029. of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
  1030. if (!unittest_data_node) {
  1031. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  1032. kfree(unittest_data);
  1033. return -ENODATA;
  1034. }
  1035. /*
  1036. * This lock normally encloses of_resolve_phandles()
  1037. */
  1038. of_overlay_mutex_lock();
  1039. rc = of_resolve_phandles(unittest_data_node);
  1040. if (rc) {
  1041. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  1042. of_overlay_mutex_unlock();
  1043. return -EINVAL;
  1044. }
  1045. if (!of_root) {
  1046. of_root = unittest_data_node;
  1047. for_each_of_allnodes(np)
  1048. __of_attach_node_sysfs(np);
  1049. of_aliases = of_find_node_by_path("/aliases");
  1050. of_chosen = of_find_node_by_path("/chosen");
  1051. of_overlay_mutex_unlock();
  1052. return 0;
  1053. }
  1054. /* attach the sub-tree to live tree */
  1055. np = unittest_data_node->child;
  1056. while (np) {
  1057. struct device_node *next = np->sibling;
  1058. np->parent = of_root;
  1059. attach_node_and_children(np);
  1060. np = next;
  1061. }
  1062. of_overlay_mutex_unlock();
  1063. return 0;
  1064. }
  1065. #ifdef CONFIG_OF_OVERLAY
  1066. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
  1067. static int unittest_probe(struct platform_device *pdev)
  1068. {
  1069. struct device *dev = &pdev->dev;
  1070. struct device_node *np = dev->of_node;
  1071. if (np == NULL) {
  1072. dev_err(dev, "No OF data for device\n");
  1073. return -EINVAL;
  1074. }
  1075. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1076. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1077. return 0;
  1078. }
  1079. static int unittest_remove(struct platform_device *pdev)
  1080. {
  1081. struct device *dev = &pdev->dev;
  1082. struct device_node *np = dev->of_node;
  1083. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1084. return 0;
  1085. }
  1086. static const struct of_device_id unittest_match[] = {
  1087. { .compatible = "unittest", },
  1088. {},
  1089. };
  1090. static struct platform_driver unittest_driver = {
  1091. .probe = unittest_probe,
  1092. .remove = unittest_remove,
  1093. .driver = {
  1094. .name = "unittest",
  1095. .of_match_table = of_match_ptr(unittest_match),
  1096. },
  1097. };
  1098. /* get the platform device instantiated at the path */
  1099. static struct platform_device *of_path_to_platform_device(const char *path)
  1100. {
  1101. struct device_node *np;
  1102. struct platform_device *pdev;
  1103. np = of_find_node_by_path(path);
  1104. if (np == NULL)
  1105. return NULL;
  1106. pdev = of_find_device_by_node(np);
  1107. of_node_put(np);
  1108. return pdev;
  1109. }
  1110. /* find out if a platform device exists at that path */
  1111. static int of_path_platform_device_exists(const char *path)
  1112. {
  1113. struct platform_device *pdev;
  1114. pdev = of_path_to_platform_device(path);
  1115. platform_device_put(pdev);
  1116. return pdev != NULL;
  1117. }
  1118. #if IS_BUILTIN(CONFIG_I2C)
  1119. /* get the i2c client device instantiated at the path */
  1120. static struct i2c_client *of_path_to_i2c_client(const char *path)
  1121. {
  1122. struct device_node *np;
  1123. struct i2c_client *client;
  1124. np = of_find_node_by_path(path);
  1125. if (np == NULL)
  1126. return NULL;
  1127. client = of_find_i2c_device_by_node(np);
  1128. of_node_put(np);
  1129. return client;
  1130. }
  1131. /* find out if a i2c client device exists at that path */
  1132. static int of_path_i2c_client_exists(const char *path)
  1133. {
  1134. struct i2c_client *client;
  1135. client = of_path_to_i2c_client(path);
  1136. if (client)
  1137. put_device(&client->dev);
  1138. return client != NULL;
  1139. }
  1140. #else
  1141. static int of_path_i2c_client_exists(const char *path)
  1142. {
  1143. return 0;
  1144. }
  1145. #endif
  1146. enum overlay_type {
  1147. PDEV_OVERLAY,
  1148. I2C_OVERLAY
  1149. };
  1150. static int of_path_device_type_exists(const char *path,
  1151. enum overlay_type ovtype)
  1152. {
  1153. switch (ovtype) {
  1154. case PDEV_OVERLAY:
  1155. return of_path_platform_device_exists(path);
  1156. case I2C_OVERLAY:
  1157. return of_path_i2c_client_exists(path);
  1158. }
  1159. return 0;
  1160. }
  1161. static const char *unittest_path(int nr, enum overlay_type ovtype)
  1162. {
  1163. const char *base;
  1164. static char buf[256];
  1165. switch (ovtype) {
  1166. case PDEV_OVERLAY:
  1167. base = "/testcase-data/overlay-node/test-bus";
  1168. break;
  1169. case I2C_OVERLAY:
  1170. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  1171. break;
  1172. default:
  1173. buf[0] = '\0';
  1174. return buf;
  1175. }
  1176. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  1177. buf[sizeof(buf) - 1] = '\0';
  1178. return buf;
  1179. }
  1180. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  1181. {
  1182. const char *path;
  1183. path = unittest_path(unittest_nr, ovtype);
  1184. switch (ovtype) {
  1185. case PDEV_OVERLAY:
  1186. return of_path_platform_device_exists(path);
  1187. case I2C_OVERLAY:
  1188. return of_path_i2c_client_exists(path);
  1189. }
  1190. return 0;
  1191. }
  1192. static const char *overlay_name_from_nr(int nr)
  1193. {
  1194. static char buf[256];
  1195. snprintf(buf, sizeof(buf) - 1,
  1196. "overlay_%d", nr);
  1197. buf[sizeof(buf) - 1] = '\0';
  1198. return buf;
  1199. }
  1200. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  1201. /* it is guaranteed that overlay ids are assigned in sequence */
  1202. #define MAX_UNITTEST_OVERLAYS 256
  1203. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  1204. static int overlay_first_id = -1;
  1205. static void of_unittest_track_overlay(int id)
  1206. {
  1207. if (overlay_first_id < 0)
  1208. overlay_first_id = id;
  1209. id -= overlay_first_id;
  1210. /* we shouldn't need that many */
  1211. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1212. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  1213. }
  1214. static void of_unittest_untrack_overlay(int id)
  1215. {
  1216. if (overlay_first_id < 0)
  1217. return;
  1218. id -= overlay_first_id;
  1219. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1220. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1221. }
  1222. static void of_unittest_destroy_tracked_overlays(void)
  1223. {
  1224. int id, ret, defers, ovcs_id;
  1225. if (overlay_first_id < 0)
  1226. return;
  1227. /* try until no defers */
  1228. do {
  1229. defers = 0;
  1230. /* remove in reverse order */
  1231. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  1232. if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
  1233. continue;
  1234. ovcs_id = id + overlay_first_id;
  1235. ret = of_overlay_remove(&ovcs_id);
  1236. if (ret == -ENODEV) {
  1237. pr_warn("%s: no overlay to destroy for #%d\n",
  1238. __func__, id + overlay_first_id);
  1239. continue;
  1240. }
  1241. if (ret != 0) {
  1242. defers++;
  1243. pr_warn("%s: overlay destroy failed for #%d\n",
  1244. __func__, id + overlay_first_id);
  1245. continue;
  1246. }
  1247. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1248. }
  1249. } while (defers > 0);
  1250. }
  1251. static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
  1252. int *overlay_id)
  1253. {
  1254. const char *overlay_name;
  1255. overlay_name = overlay_name_from_nr(overlay_nr);
  1256. if (!overlay_data_apply(overlay_name, overlay_id)) {
  1257. unittest(0, "could not apply overlay \"%s\"\n",
  1258. overlay_name);
  1259. return -EFAULT;
  1260. }
  1261. of_unittest_track_overlay(*overlay_id);
  1262. return 0;
  1263. }
  1264. /* apply an overlay while checking before and after states */
  1265. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  1266. int unittest_nr, int before, int after,
  1267. enum overlay_type ovtype)
  1268. {
  1269. int ret, ovcs_id;
  1270. /* unittest device must not be in before state */
  1271. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1272. unittest(0, "%s with device @\"%s\" %s\n",
  1273. overlay_name_from_nr(overlay_nr),
  1274. unittest_path(unittest_nr, ovtype),
  1275. !before ? "enabled" : "disabled");
  1276. return -EINVAL;
  1277. }
  1278. ovcs_id = 0;
  1279. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1280. if (ret != 0) {
  1281. /* of_unittest_apply_overlay already called unittest() */
  1282. return ret;
  1283. }
  1284. /* unittest device must be to set to after state */
  1285. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1286. unittest(0, "%s failed to create @\"%s\" %s\n",
  1287. overlay_name_from_nr(overlay_nr),
  1288. unittest_path(unittest_nr, ovtype),
  1289. !after ? "enabled" : "disabled");
  1290. return -EINVAL;
  1291. }
  1292. return 0;
  1293. }
  1294. /* apply an overlay and then revert it while checking before, after states */
  1295. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  1296. int unittest_nr, int before, int after,
  1297. enum overlay_type ovtype)
  1298. {
  1299. int ret, ovcs_id;
  1300. /* unittest device must be in before state */
  1301. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1302. unittest(0, "%s with device @\"%s\" %s\n",
  1303. overlay_name_from_nr(overlay_nr),
  1304. unittest_path(unittest_nr, ovtype),
  1305. !before ? "enabled" : "disabled");
  1306. return -EINVAL;
  1307. }
  1308. /* apply the overlay */
  1309. ovcs_id = 0;
  1310. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1311. if (ret != 0) {
  1312. /* of_unittest_apply_overlay already called unittest() */
  1313. return ret;
  1314. }
  1315. /* unittest device must be in after state */
  1316. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1317. unittest(0, "%s failed to create @\"%s\" %s\n",
  1318. overlay_name_from_nr(overlay_nr),
  1319. unittest_path(unittest_nr, ovtype),
  1320. !after ? "enabled" : "disabled");
  1321. return -EINVAL;
  1322. }
  1323. ret = of_overlay_remove(&ovcs_id);
  1324. if (ret != 0) {
  1325. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  1326. overlay_name_from_nr(overlay_nr),
  1327. unittest_path(unittest_nr, ovtype));
  1328. return ret;
  1329. }
  1330. /* unittest device must be again in before state */
  1331. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1332. unittest(0, "%s with device @\"%s\" %s\n",
  1333. overlay_name_from_nr(overlay_nr),
  1334. unittest_path(unittest_nr, ovtype),
  1335. !before ? "enabled" : "disabled");
  1336. return -EINVAL;
  1337. }
  1338. return 0;
  1339. }
  1340. /* test activation of device */
  1341. static void __init of_unittest_overlay_0(void)
  1342. {
  1343. /* device should enable */
  1344. if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
  1345. return;
  1346. unittest(1, "overlay test %d passed\n", 0);
  1347. }
  1348. /* test deactivation of device */
  1349. static void __init of_unittest_overlay_1(void)
  1350. {
  1351. /* device should disable */
  1352. if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
  1353. return;
  1354. unittest(1, "overlay test %d passed\n", 1);
  1355. }
  1356. /* test activation of device */
  1357. static void __init of_unittest_overlay_2(void)
  1358. {
  1359. /* device should enable */
  1360. if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
  1361. return;
  1362. unittest(1, "overlay test %d passed\n", 2);
  1363. }
  1364. /* test deactivation of device */
  1365. static void __init of_unittest_overlay_3(void)
  1366. {
  1367. /* device should disable */
  1368. if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
  1369. return;
  1370. unittest(1, "overlay test %d passed\n", 3);
  1371. }
  1372. /* test activation of a full device node */
  1373. static void __init of_unittest_overlay_4(void)
  1374. {
  1375. /* device should disable */
  1376. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  1377. return;
  1378. unittest(1, "overlay test %d passed\n", 4);
  1379. }
  1380. /* test overlay apply/revert sequence */
  1381. static void __init of_unittest_overlay_5(void)
  1382. {
  1383. /* device should disable */
  1384. if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
  1385. return;
  1386. unittest(1, "overlay test %d passed\n", 5);
  1387. }
  1388. /* test overlay application in sequence */
  1389. static void __init of_unittest_overlay_6(void)
  1390. {
  1391. int i, ov_id[2], ovcs_id;
  1392. int overlay_nr = 6, unittest_nr = 6;
  1393. int before = 0, after = 1;
  1394. const char *overlay_name;
  1395. /* unittest device must be in before state */
  1396. for (i = 0; i < 2; i++) {
  1397. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1398. != before) {
  1399. unittest(0, "%s with device @\"%s\" %s\n",
  1400. overlay_name_from_nr(overlay_nr + i),
  1401. unittest_path(unittest_nr + i,
  1402. PDEV_OVERLAY),
  1403. !before ? "enabled" : "disabled");
  1404. return;
  1405. }
  1406. }
  1407. /* apply the overlays */
  1408. for (i = 0; i < 2; i++) {
  1409. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1410. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1411. unittest(0, "could not apply overlay \"%s\"\n",
  1412. overlay_name);
  1413. return;
  1414. }
  1415. ov_id[i] = ovcs_id;
  1416. of_unittest_track_overlay(ov_id[i]);
  1417. }
  1418. for (i = 0; i < 2; i++) {
  1419. /* unittest device must be in after state */
  1420. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1421. != after) {
  1422. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1423. overlay_name_from_nr(overlay_nr + i),
  1424. unittest_path(unittest_nr + i,
  1425. PDEV_OVERLAY),
  1426. !after ? "enabled" : "disabled");
  1427. return;
  1428. }
  1429. }
  1430. for (i = 1; i >= 0; i--) {
  1431. ovcs_id = ov_id[i];
  1432. if (of_overlay_remove(&ovcs_id)) {
  1433. unittest(0, "%s failed destroy @\"%s\"\n",
  1434. overlay_name_from_nr(overlay_nr + i),
  1435. unittest_path(unittest_nr + i,
  1436. PDEV_OVERLAY));
  1437. return;
  1438. }
  1439. of_unittest_untrack_overlay(ov_id[i]);
  1440. }
  1441. for (i = 0; i < 2; i++) {
  1442. /* unittest device must be again in before state */
  1443. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1444. != before) {
  1445. unittest(0, "%s with device @\"%s\" %s\n",
  1446. overlay_name_from_nr(overlay_nr + i),
  1447. unittest_path(unittest_nr + i,
  1448. PDEV_OVERLAY),
  1449. !before ? "enabled" : "disabled");
  1450. return;
  1451. }
  1452. }
  1453. unittest(1, "overlay test %d passed\n", 6);
  1454. }
  1455. /* test overlay application in sequence */
  1456. static void __init of_unittest_overlay_8(void)
  1457. {
  1458. int i, ov_id[2], ovcs_id;
  1459. int overlay_nr = 8, unittest_nr = 8;
  1460. const char *overlay_name;
  1461. /* we don't care about device state in this test */
  1462. /* apply the overlays */
  1463. for (i = 0; i < 2; i++) {
  1464. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1465. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1466. unittest(0, "could not apply overlay \"%s\"\n",
  1467. overlay_name);
  1468. return;
  1469. }
  1470. ov_id[i] = ovcs_id;
  1471. of_unittest_track_overlay(ov_id[i]);
  1472. }
  1473. /* now try to remove first overlay (it should fail) */
  1474. ovcs_id = ov_id[0];
  1475. if (!of_overlay_remove(&ovcs_id)) {
  1476. unittest(0, "%s was destroyed @\"%s\"\n",
  1477. overlay_name_from_nr(overlay_nr + 0),
  1478. unittest_path(unittest_nr,
  1479. PDEV_OVERLAY));
  1480. return;
  1481. }
  1482. /* removing them in order should work */
  1483. for (i = 1; i >= 0; i--) {
  1484. ovcs_id = ov_id[i];
  1485. if (of_overlay_remove(&ovcs_id)) {
  1486. unittest(0, "%s not destroyed @\"%s\"\n",
  1487. overlay_name_from_nr(overlay_nr + i),
  1488. unittest_path(unittest_nr,
  1489. PDEV_OVERLAY));
  1490. return;
  1491. }
  1492. of_unittest_untrack_overlay(ov_id[i]);
  1493. }
  1494. unittest(1, "overlay test %d passed\n", 8);
  1495. }
  1496. /* test insertion of a bus with parent devices */
  1497. static void __init of_unittest_overlay_10(void)
  1498. {
  1499. int ret;
  1500. char *child_path;
  1501. /* device should disable */
  1502. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1503. if (unittest(ret == 0,
  1504. "overlay test %d failed; overlay application\n", 10))
  1505. return;
  1506. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1507. unittest_path(10, PDEV_OVERLAY));
  1508. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1509. return;
  1510. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1511. kfree(child_path);
  1512. unittest(ret, "overlay test %d failed; no child device\n", 10);
  1513. }
  1514. /* test insertion of a bus with parent devices (and revert) */
  1515. static void __init of_unittest_overlay_11(void)
  1516. {
  1517. int ret;
  1518. /* device should disable */
  1519. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1520. PDEV_OVERLAY);
  1521. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  1522. }
  1523. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1524. struct unittest_i2c_bus_data {
  1525. struct platform_device *pdev;
  1526. struct i2c_adapter adap;
  1527. };
  1528. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1529. struct i2c_msg *msgs, int num)
  1530. {
  1531. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1532. (void)std;
  1533. return num;
  1534. }
  1535. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1536. {
  1537. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1538. }
  1539. static const struct i2c_algorithm unittest_i2c_algo = {
  1540. .master_xfer = unittest_i2c_master_xfer,
  1541. .functionality = unittest_i2c_functionality,
  1542. };
  1543. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1544. {
  1545. struct device *dev = &pdev->dev;
  1546. struct device_node *np = dev->of_node;
  1547. struct unittest_i2c_bus_data *std;
  1548. struct i2c_adapter *adap;
  1549. int ret;
  1550. if (np == NULL) {
  1551. dev_err(dev, "No OF data for device\n");
  1552. return -EINVAL;
  1553. }
  1554. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1555. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  1556. if (!std) {
  1557. dev_err(dev, "Failed to allocate unittest i2c data\n");
  1558. return -ENOMEM;
  1559. }
  1560. /* link them together */
  1561. std->pdev = pdev;
  1562. platform_set_drvdata(pdev, std);
  1563. adap = &std->adap;
  1564. i2c_set_adapdata(adap, std);
  1565. adap->nr = -1;
  1566. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  1567. adap->class = I2C_CLASS_DEPRECATED;
  1568. adap->algo = &unittest_i2c_algo;
  1569. adap->dev.parent = dev;
  1570. adap->dev.of_node = dev->of_node;
  1571. adap->timeout = 5 * HZ;
  1572. adap->retries = 3;
  1573. ret = i2c_add_numbered_adapter(adap);
  1574. if (ret != 0) {
  1575. dev_err(dev, "Failed to add I2C adapter\n");
  1576. return ret;
  1577. }
  1578. return 0;
  1579. }
  1580. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  1581. {
  1582. struct device *dev = &pdev->dev;
  1583. struct device_node *np = dev->of_node;
  1584. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  1585. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1586. i2c_del_adapter(&std->adap);
  1587. return 0;
  1588. }
  1589. static const struct of_device_id unittest_i2c_bus_match[] = {
  1590. { .compatible = "unittest-i2c-bus", },
  1591. {},
  1592. };
  1593. static struct platform_driver unittest_i2c_bus_driver = {
  1594. .probe = unittest_i2c_bus_probe,
  1595. .remove = unittest_i2c_bus_remove,
  1596. .driver = {
  1597. .name = "unittest-i2c-bus",
  1598. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  1599. },
  1600. };
  1601. static int unittest_i2c_dev_probe(struct i2c_client *client,
  1602. const struct i2c_device_id *id)
  1603. {
  1604. struct device *dev = &client->dev;
  1605. struct device_node *np = client->dev.of_node;
  1606. if (!np) {
  1607. dev_err(dev, "No OF node\n");
  1608. return -EINVAL;
  1609. }
  1610. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1611. return 0;
  1612. };
  1613. static int unittest_i2c_dev_remove(struct i2c_client *client)
  1614. {
  1615. struct device *dev = &client->dev;
  1616. struct device_node *np = client->dev.of_node;
  1617. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1618. return 0;
  1619. }
  1620. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  1621. { .name = "unittest-i2c-dev" },
  1622. { }
  1623. };
  1624. static struct i2c_driver unittest_i2c_dev_driver = {
  1625. .driver = {
  1626. .name = "unittest-i2c-dev",
  1627. },
  1628. .probe = unittest_i2c_dev_probe,
  1629. .remove = unittest_i2c_dev_remove,
  1630. .id_table = unittest_i2c_dev_id,
  1631. };
  1632. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1633. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  1634. {
  1635. return 0;
  1636. }
  1637. static int unittest_i2c_mux_probe(struct i2c_client *client,
  1638. const struct i2c_device_id *id)
  1639. {
  1640. int i, nchans;
  1641. struct device *dev = &client->dev;
  1642. struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
  1643. struct device_node *np = client->dev.of_node, *child;
  1644. struct i2c_mux_core *muxc;
  1645. u32 reg, max_reg;
  1646. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1647. if (!np) {
  1648. dev_err(dev, "No OF node\n");
  1649. return -EINVAL;
  1650. }
  1651. max_reg = (u32)-1;
  1652. for_each_child_of_node(np, child) {
  1653. if (of_property_read_u32(child, "reg", &reg))
  1654. continue;
  1655. if (max_reg == (u32)-1 || reg > max_reg)
  1656. max_reg = reg;
  1657. }
  1658. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  1659. if (nchans == 0) {
  1660. dev_err(dev, "No channels\n");
  1661. return -EINVAL;
  1662. }
  1663. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  1664. unittest_i2c_mux_select_chan, NULL);
  1665. if (!muxc)
  1666. return -ENOMEM;
  1667. for (i = 0; i < nchans; i++) {
  1668. if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
  1669. dev_err(dev, "Failed to register mux #%d\n", i);
  1670. i2c_mux_del_adapters(muxc);
  1671. return -ENODEV;
  1672. }
  1673. }
  1674. i2c_set_clientdata(client, muxc);
  1675. return 0;
  1676. };
  1677. static int unittest_i2c_mux_remove(struct i2c_client *client)
  1678. {
  1679. struct device *dev = &client->dev;
  1680. struct device_node *np = client->dev.of_node;
  1681. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  1682. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1683. i2c_mux_del_adapters(muxc);
  1684. return 0;
  1685. }
  1686. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  1687. { .name = "unittest-i2c-mux" },
  1688. { }
  1689. };
  1690. static struct i2c_driver unittest_i2c_mux_driver = {
  1691. .driver = {
  1692. .name = "unittest-i2c-mux",
  1693. },
  1694. .probe = unittest_i2c_mux_probe,
  1695. .remove = unittest_i2c_mux_remove,
  1696. .id_table = unittest_i2c_mux_id,
  1697. };
  1698. #endif
  1699. static int of_unittest_overlay_i2c_init(void)
  1700. {
  1701. int ret;
  1702. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  1703. if (unittest(ret == 0,
  1704. "could not register unittest i2c device driver\n"))
  1705. return ret;
  1706. ret = platform_driver_register(&unittest_i2c_bus_driver);
  1707. if (unittest(ret == 0,
  1708. "could not register unittest i2c bus driver\n"))
  1709. return ret;
  1710. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1711. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  1712. if (unittest(ret == 0,
  1713. "could not register unittest i2c mux driver\n"))
  1714. return ret;
  1715. #endif
  1716. return 0;
  1717. }
  1718. static void of_unittest_overlay_i2c_cleanup(void)
  1719. {
  1720. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1721. i2c_del_driver(&unittest_i2c_mux_driver);
  1722. #endif
  1723. platform_driver_unregister(&unittest_i2c_bus_driver);
  1724. i2c_del_driver(&unittest_i2c_dev_driver);
  1725. }
  1726. static void __init of_unittest_overlay_i2c_12(void)
  1727. {
  1728. /* device should enable */
  1729. if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
  1730. return;
  1731. unittest(1, "overlay test %d passed\n", 12);
  1732. }
  1733. /* test deactivation of device */
  1734. static void __init of_unittest_overlay_i2c_13(void)
  1735. {
  1736. /* device should disable */
  1737. if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
  1738. return;
  1739. unittest(1, "overlay test %d passed\n", 13);
  1740. }
  1741. /* just check for i2c mux existence */
  1742. static void of_unittest_overlay_i2c_14(void)
  1743. {
  1744. }
  1745. static void __init of_unittest_overlay_i2c_15(void)
  1746. {
  1747. /* device should enable */
  1748. if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
  1749. return;
  1750. unittest(1, "overlay test %d passed\n", 15);
  1751. }
  1752. #else
  1753. static inline void of_unittest_overlay_i2c_14(void) { }
  1754. static inline void of_unittest_overlay_i2c_15(void) { }
  1755. #endif
  1756. static void __init of_unittest_overlay(void)
  1757. {
  1758. struct device_node *bus_np = NULL;
  1759. if (platform_driver_register(&unittest_driver)) {
  1760. unittest(0, "could not register unittest driver\n");
  1761. goto out;
  1762. }
  1763. bus_np = of_find_node_by_path(bus_path);
  1764. if (bus_np == NULL) {
  1765. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  1766. goto out;
  1767. }
  1768. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  1769. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1770. goto out;
  1771. }
  1772. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  1773. unittest(0, "could not find unittest0 @ \"%s\"\n",
  1774. unittest_path(100, PDEV_OVERLAY));
  1775. goto out;
  1776. }
  1777. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  1778. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  1779. unittest_path(101, PDEV_OVERLAY));
  1780. goto out;
  1781. }
  1782. unittest(1, "basic infrastructure of overlays passed");
  1783. /* tests in sequence */
  1784. of_unittest_overlay_0();
  1785. of_unittest_overlay_1();
  1786. of_unittest_overlay_2();
  1787. of_unittest_overlay_3();
  1788. of_unittest_overlay_4();
  1789. of_unittest_overlay_5();
  1790. of_unittest_overlay_6();
  1791. of_unittest_overlay_8();
  1792. of_unittest_overlay_10();
  1793. of_unittest_overlay_11();
  1794. #if IS_BUILTIN(CONFIG_I2C)
  1795. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  1796. goto out;
  1797. of_unittest_overlay_i2c_12();
  1798. of_unittest_overlay_i2c_13();
  1799. of_unittest_overlay_i2c_14();
  1800. of_unittest_overlay_i2c_15();
  1801. of_unittest_overlay_i2c_cleanup();
  1802. #endif
  1803. of_unittest_destroy_tracked_overlays();
  1804. out:
  1805. of_node_put(bus_np);
  1806. }
  1807. #else
  1808. static inline void __init of_unittest_overlay(void) { }
  1809. #endif
  1810. #ifdef CONFIG_OF_OVERLAY
  1811. /*
  1812. * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
  1813. * in scripts/Makefile.lib
  1814. */
  1815. #define OVERLAY_INFO_EXTERN(name) \
  1816. extern uint8_t __dtb_##name##_begin[]; \
  1817. extern uint8_t __dtb_##name##_end[]
  1818. #define OVERLAY_INFO(overlay_name, expected) \
  1819. { .dtb_begin = __dtb_##overlay_name##_begin, \
  1820. .dtb_end = __dtb_##overlay_name##_end, \
  1821. .expected_result = expected, \
  1822. .name = #overlay_name, \
  1823. }
  1824. struct overlay_info {
  1825. uint8_t *dtb_begin;
  1826. uint8_t *dtb_end;
  1827. int expected_result;
  1828. int overlay_id;
  1829. char *name;
  1830. };
  1831. OVERLAY_INFO_EXTERN(overlay_base);
  1832. OVERLAY_INFO_EXTERN(overlay);
  1833. OVERLAY_INFO_EXTERN(overlay_0);
  1834. OVERLAY_INFO_EXTERN(overlay_1);
  1835. OVERLAY_INFO_EXTERN(overlay_2);
  1836. OVERLAY_INFO_EXTERN(overlay_3);
  1837. OVERLAY_INFO_EXTERN(overlay_4);
  1838. OVERLAY_INFO_EXTERN(overlay_5);
  1839. OVERLAY_INFO_EXTERN(overlay_6);
  1840. OVERLAY_INFO_EXTERN(overlay_7);
  1841. OVERLAY_INFO_EXTERN(overlay_8);
  1842. OVERLAY_INFO_EXTERN(overlay_9);
  1843. OVERLAY_INFO_EXTERN(overlay_10);
  1844. OVERLAY_INFO_EXTERN(overlay_11);
  1845. OVERLAY_INFO_EXTERN(overlay_12);
  1846. OVERLAY_INFO_EXTERN(overlay_13);
  1847. OVERLAY_INFO_EXTERN(overlay_15);
  1848. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  1849. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  1850. /* order of entries is hard-coded into users of overlays[] */
  1851. static struct overlay_info overlays[] = {
  1852. OVERLAY_INFO(overlay_base, -9999),
  1853. OVERLAY_INFO(overlay, 0),
  1854. OVERLAY_INFO(overlay_0, 0),
  1855. OVERLAY_INFO(overlay_1, 0),
  1856. OVERLAY_INFO(overlay_2, 0),
  1857. OVERLAY_INFO(overlay_3, 0),
  1858. OVERLAY_INFO(overlay_4, 0),
  1859. OVERLAY_INFO(overlay_5, 0),
  1860. OVERLAY_INFO(overlay_6, 0),
  1861. OVERLAY_INFO(overlay_7, 0),
  1862. OVERLAY_INFO(overlay_8, 0),
  1863. OVERLAY_INFO(overlay_9, 0),
  1864. OVERLAY_INFO(overlay_10, 0),
  1865. OVERLAY_INFO(overlay_11, 0),
  1866. OVERLAY_INFO(overlay_12, 0),
  1867. OVERLAY_INFO(overlay_13, 0),
  1868. OVERLAY_INFO(overlay_15, 0),
  1869. OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
  1870. OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
  1871. {}
  1872. };
  1873. static struct device_node *overlay_base_root;
  1874. static void * __init dt_alloc_memory(u64 size, u64 align)
  1875. {
  1876. return memblock_virt_alloc(size, align);
  1877. }
  1878. /*
  1879. * Create base device tree for the overlay unittest.
  1880. *
  1881. * This is called from very early boot code.
  1882. *
  1883. * Do as much as possible the same way as done in __unflatten_device_tree
  1884. * and other early boot steps for the normal FDT so that the overlay base
  1885. * unflattened tree will have the same characteristics as the real tree
  1886. * (such as having memory allocated by the early allocator). The goal
  1887. * is to test "the real thing" as much as possible, and test "test setup
  1888. * code" as little as possible.
  1889. *
  1890. * Have to stop before resolving phandles, because that uses kmalloc.
  1891. */
  1892. void __init unittest_unflatten_overlay_base(void)
  1893. {
  1894. struct overlay_info *info;
  1895. u32 data_size;
  1896. void *new_fdt;
  1897. u32 size;
  1898. info = &overlays[0];
  1899. if (info->expected_result != -9999) {
  1900. pr_err("No dtb 'overlay_base' to attach\n");
  1901. return;
  1902. }
  1903. data_size = info->dtb_end - info->dtb_begin;
  1904. if (!data_size) {
  1905. pr_err("No dtb 'overlay_base' to attach\n");
  1906. return;
  1907. }
  1908. size = fdt_totalsize(info->dtb_begin);
  1909. if (size != data_size) {
  1910. pr_err("dtb 'overlay_base' header totalsize != actual size");
  1911. return;
  1912. }
  1913. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  1914. if (!new_fdt) {
  1915. pr_err("alloc for dtb 'overlay_base' failed");
  1916. return;
  1917. }
  1918. memcpy(new_fdt, info->dtb_begin, size);
  1919. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  1920. dt_alloc_memory, true);
  1921. }
  1922. /*
  1923. * The purpose of of_unittest_overlay_data_add is to add an
  1924. * overlay in the normal fashion. This is a test of the whole
  1925. * picture, instead of testing individual elements.
  1926. *
  1927. * A secondary purpose is to be able to verify that the contents of
  1928. * /proc/device-tree/ contains the updated structure and values from
  1929. * the overlay. That must be verified separately in user space.
  1930. *
  1931. * Return 0 on unexpected error.
  1932. */
  1933. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
  1934. {
  1935. struct overlay_info *info;
  1936. int found = 0;
  1937. int k;
  1938. int ret;
  1939. u32 size;
  1940. for (k = 0, info = overlays; info && info->name; info++, k++) {
  1941. if (!strcmp(overlay_name, info->name)) {
  1942. found = 1;
  1943. break;
  1944. }
  1945. }
  1946. if (!found) {
  1947. pr_err("no overlay data for %s\n", overlay_name);
  1948. return 0;
  1949. }
  1950. size = info->dtb_end - info->dtb_begin;
  1951. if (!size)
  1952. pr_err("no overlay data for %s\n", overlay_name);
  1953. ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
  1954. if (overlay_id)
  1955. *overlay_id = info->overlay_id;
  1956. if (ret < 0)
  1957. goto out;
  1958. pr_debug("%s applied\n", overlay_name);
  1959. out:
  1960. if (ret != info->expected_result)
  1961. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  1962. info->expected_result, ret, overlay_name);
  1963. return (ret == info->expected_result);
  1964. }
  1965. /*
  1966. * The purpose of of_unittest_overlay_high_level is to add an overlay
  1967. * in the normal fashion. This is a test of the whole picture,
  1968. * instead of individual elements.
  1969. *
  1970. * The first part of the function is _not_ normal overlay usage; it is
  1971. * finishing splicing the base overlay device tree into the live tree.
  1972. */
  1973. static __init void of_unittest_overlay_high_level(void)
  1974. {
  1975. struct device_node *last_sibling;
  1976. struct device_node *np;
  1977. struct device_node *of_symbols;
  1978. struct device_node *overlay_base_symbols;
  1979. struct device_node **pprev;
  1980. struct property *prop;
  1981. if (!overlay_base_root) {
  1982. unittest(0, "overlay_base_root not initialized\n");
  1983. return;
  1984. }
  1985. /*
  1986. * Could not fixup phandles in unittest_unflatten_overlay_base()
  1987. * because kmalloc() was not yet available.
  1988. */
  1989. of_overlay_mutex_lock();
  1990. of_resolve_phandles(overlay_base_root);
  1991. of_overlay_mutex_unlock();
  1992. /*
  1993. * do not allow overlay_base to duplicate any node already in
  1994. * tree, this greatly simplifies the code
  1995. */
  1996. /*
  1997. * remove overlay_base_root node "__local_fixups", after
  1998. * being used by of_resolve_phandles()
  1999. */
  2000. pprev = &overlay_base_root->child;
  2001. for (np = overlay_base_root->child; np; np = np->sibling) {
  2002. if (!of_node_cmp(np->name, "__local_fixups__")) {
  2003. *pprev = np->sibling;
  2004. break;
  2005. }
  2006. pprev = &np->sibling;
  2007. }
  2008. /* remove overlay_base_root node "__symbols__" if in live tree */
  2009. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  2010. if (of_symbols) {
  2011. /* will have to graft properties from node into live tree */
  2012. pprev = &overlay_base_root->child;
  2013. for (np = overlay_base_root->child; np; np = np->sibling) {
  2014. if (!of_node_cmp(np->name, "__symbols__")) {
  2015. overlay_base_symbols = np;
  2016. *pprev = np->sibling;
  2017. break;
  2018. }
  2019. pprev = &np->sibling;
  2020. }
  2021. }
  2022. for (np = overlay_base_root->child; np; np = np->sibling) {
  2023. if (of_get_child_by_name(of_root, np->name)) {
  2024. unittest(0, "illegal node name in overlay_base %s",
  2025. np->name);
  2026. return;
  2027. }
  2028. }
  2029. /*
  2030. * overlay 'overlay_base' is not allowed to have root
  2031. * properties, so only need to splice nodes into main device tree.
  2032. *
  2033. * root node of *overlay_base_root will not be freed, it is lost
  2034. * memory.
  2035. */
  2036. for (np = overlay_base_root->child; np; np = np->sibling)
  2037. np->parent = of_root;
  2038. mutex_lock(&of_mutex);
  2039. for (last_sibling = np = of_root->child; np; np = np->sibling)
  2040. last_sibling = np;
  2041. if (last_sibling)
  2042. last_sibling->sibling = overlay_base_root->child;
  2043. else
  2044. of_root->child = overlay_base_root->child;
  2045. for_each_of_allnodes_from(overlay_base_root, np)
  2046. __of_attach_node_sysfs(np);
  2047. if (of_symbols) {
  2048. struct property *new_prop;
  2049. for_each_property_of_node(overlay_base_symbols, prop) {
  2050. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  2051. if (!new_prop) {
  2052. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  2053. prop->name);
  2054. goto err_unlock;
  2055. }
  2056. if (__of_add_property(of_symbols, new_prop)) {
  2057. /* "name" auto-generated by unflatten */
  2058. if (!strcmp(new_prop->name, "name"))
  2059. continue;
  2060. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  2061. prop->name);
  2062. goto err_unlock;
  2063. }
  2064. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  2065. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  2066. prop->name);
  2067. goto err_unlock;
  2068. }
  2069. }
  2070. }
  2071. mutex_unlock(&of_mutex);
  2072. /* now do the normal overlay usage test */
  2073. unittest(overlay_data_apply("overlay", NULL),
  2074. "Adding overlay 'overlay' failed\n");
  2075. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  2076. "Adding overlay 'overlay_bad_phandle' failed\n");
  2077. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  2078. "Adding overlay 'overlay_bad_symbol' failed\n");
  2079. return;
  2080. err_unlock:
  2081. mutex_unlock(&of_mutex);
  2082. }
  2083. #else
  2084. static inline __init void of_unittest_overlay_high_level(void) {}
  2085. #endif
  2086. static int __init of_unittest(void)
  2087. {
  2088. struct device_node *np;
  2089. int res;
  2090. /* adding data for unittest */
  2091. res = unittest_data_add();
  2092. if (res)
  2093. return res;
  2094. if (!of_aliases)
  2095. of_aliases = of_find_node_by_path("/aliases");
  2096. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  2097. if (!np) {
  2098. pr_info("No testcase data in device tree; not running tests\n");
  2099. return 0;
  2100. }
  2101. of_node_put(np);
  2102. pr_info("start of unittest - you will see error messages\n");
  2103. of_unittest_check_tree_linkage();
  2104. of_unittest_check_phandles();
  2105. of_unittest_find_node_by_name();
  2106. of_unittest_dynamic();
  2107. of_unittest_parse_phandle_with_args();
  2108. of_unittest_parse_phandle_with_args_map();
  2109. of_unittest_printf();
  2110. of_unittest_property_string();
  2111. of_unittest_property_copy();
  2112. of_unittest_changeset();
  2113. of_unittest_parse_interrupts();
  2114. of_unittest_parse_interrupts_extended();
  2115. of_unittest_match_node();
  2116. of_unittest_platform_populate();
  2117. of_unittest_overlay();
  2118. /* Double check linkage after removing testcase data */
  2119. of_unittest_check_tree_linkage();
  2120. of_unittest_overlay_high_level();
  2121. pr_info("end of unittest - %i passed, %i failed\n",
  2122. unittest_results.passed, unittest_results.failed);
  2123. return 0;
  2124. }
  2125. late_initcall(of_unittest);