unittest.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315
  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/memblock.h>
  7. #include <linux/clk.h>
  8. #include <linux/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
  9. #include <linux/err.h>
  10. #include <linux/errno.h>
  11. #include <linux/hashtable.h>
  12. #include <linux/libfdt.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/list.h>
  19. #include <linux/mutex.h>
  20. #include <linux/slab.h>
  21. #include <linux/device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-mux.h>
  25. #include <linux/gpio/driver.h>
  26. #include <linux/bitops.h>
  27. #include "of_private.h"
  28. static struct unittest_results {
  29. int passed;
  30. int failed;
  31. } unittest_results;
  32. #define unittest(result, fmt, ...) ({ \
  33. bool failed = !(result); \
  34. if (failed) { \
  35. unittest_results.failed++; \
  36. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  37. } else { \
  38. unittest_results.passed++; \
  39. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  40. } \
  41. failed; \
  42. })
  43. /*
  44. * Expected message may have a message level other than KERN_INFO.
  45. * Print the expected message only if the current loglevel will allow
  46. * the actual message to print.
  47. *
  48. * Do not use EXPECT_BEGIN() or EXPECT_END() for messages generated by
  49. * pr_debug().
  50. */
  51. #define EXPECT_BEGIN(level, fmt, ...) \
  52. printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
  53. #define EXPECT_END(level, fmt, ...) \
  54. printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
  55. static void __init of_unittest_find_node_by_name(void)
  56. {
  57. struct device_node *np;
  58. const char *options, *name;
  59. np = of_find_node_by_path("/testcase-data");
  60. name = kasprintf(GFP_KERNEL, "%pOF", np);
  61. unittest(np && !strcmp("/testcase-data", name),
  62. "find /testcase-data failed\n");
  63. of_node_put(np);
  64. kfree(name);
  65. /* Test if trailing '/' works */
  66. np = of_find_node_by_path("/testcase-data/");
  67. unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
  68. np = of_find_node_by_path("/testcase-data/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-data/phandle-tests/consumer-a failed\n");
  72. of_node_put(np);
  73. kfree(name);
  74. np = of_find_node_by_path("testcase-alias");
  75. name = kasprintf(GFP_KERNEL, "%pOF", np);
  76. unittest(np && !strcmp("/testcase-data", name),
  77. "find testcase-alias failed\n");
  78. of_node_put(np);
  79. kfree(name);
  80. /* Test if trailing '/' works on aliases */
  81. np = of_find_node_by_path("testcase-alias/");
  82. unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
  83. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  84. name = kasprintf(GFP_KERNEL, "%pOF", np);
  85. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  86. "find testcase-alias/phandle-tests/consumer-a failed\n");
  87. of_node_put(np);
  88. kfree(name);
  89. np = of_find_node_by_path("/testcase-data/missing-path");
  90. unittest(!np, "non-existent path returned node %pOF\n", np);
  91. of_node_put(np);
  92. np = of_find_node_by_path("missing-alias");
  93. unittest(!np, "non-existent alias returned node %pOF\n", np);
  94. of_node_put(np);
  95. np = of_find_node_by_path("testcase-alias/missing-path");
  96. unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
  97. of_node_put(np);
  98. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  99. unittest(np && !strcmp("testoption", options),
  100. "option path test failed\n");
  101. of_node_put(np);
  102. np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
  103. unittest(np && !strcmp("test/option", options),
  104. "option path test, subcase #1 failed\n");
  105. of_node_put(np);
  106. np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
  107. unittest(np && !strcmp("test/option", options),
  108. "option path test, subcase #2 failed\n");
  109. of_node_put(np);
  110. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  111. unittest(np, "NULL option path test failed\n");
  112. of_node_put(np);
  113. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  114. &options);
  115. unittest(np && !strcmp("testaliasoption", options),
  116. "option alias path test failed\n");
  117. of_node_put(np);
  118. np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
  119. &options);
  120. unittest(np && !strcmp("test/alias/option", options),
  121. "option alias path test, subcase #1 failed\n");
  122. of_node_put(np);
  123. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  124. unittest(np, "NULL option alias path test failed\n");
  125. of_node_put(np);
  126. options = "testoption";
  127. np = of_find_node_opts_by_path("testcase-alias", &options);
  128. unittest(np && !options, "option clearing test failed\n");
  129. of_node_put(np);
  130. options = "testoption";
  131. np = of_find_node_opts_by_path("/", &options);
  132. unittest(np && !options, "option clearing root node test failed\n");
  133. of_node_put(np);
  134. }
  135. static void __init of_unittest_dynamic(void)
  136. {
  137. struct device_node *np;
  138. struct property *prop;
  139. np = of_find_node_by_path("/testcase-data");
  140. if (!np) {
  141. pr_err("missing testcase data\n");
  142. return;
  143. }
  144. /* Array of 4 properties for the purpose of testing */
  145. prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
  146. if (!prop) {
  147. unittest(0, "kzalloc() failed\n");
  148. return;
  149. }
  150. /* Add a new property - should pass*/
  151. prop->name = "new-property";
  152. prop->value = "new-property-data";
  153. prop->length = strlen(prop->value) + 1;
  154. unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  155. /* Try to add an existing property - should fail */
  156. prop++;
  157. prop->name = "new-property";
  158. prop->value = "new-property-data-should-fail";
  159. prop->length = strlen(prop->value) + 1;
  160. unittest(of_add_property(np, prop) != 0,
  161. "Adding an existing property should have failed\n");
  162. /* Try to modify an existing property - should pass */
  163. prop->value = "modify-property-data-should-pass";
  164. prop->length = strlen(prop->value) + 1;
  165. unittest(of_update_property(np, prop) == 0,
  166. "Updating an existing property should have passed\n");
  167. /* Try to modify non-existent property - should pass*/
  168. prop++;
  169. prop->name = "modify-property";
  170. prop->value = "modify-missing-property-data-should-pass";
  171. prop->length = strlen(prop->value) + 1;
  172. unittest(of_update_property(np, prop) == 0,
  173. "Updating a missing property should have passed\n");
  174. /* Remove property - should pass */
  175. unittest(of_remove_property(np, prop) == 0,
  176. "Removing a property should have passed\n");
  177. /* Adding very large property - should pass */
  178. prop++;
  179. prop->name = "large-property-PAGE_SIZEx8";
  180. prop->length = PAGE_SIZE * 8;
  181. prop->value = kzalloc(prop->length, GFP_KERNEL);
  182. unittest(prop->value != NULL, "Unable to allocate large buffer\n");
  183. if (prop->value)
  184. unittest(of_add_property(np, prop) == 0,
  185. "Adding a large property should have passed\n");
  186. }
  187. static int __init of_unittest_check_node_linkage(struct device_node *np)
  188. {
  189. struct device_node *child;
  190. int count = 0, rc;
  191. for_each_child_of_node(np, child) {
  192. if (child->parent != np) {
  193. pr_err("Child node %pOFn links to wrong parent %pOFn\n",
  194. child, np);
  195. rc = -EINVAL;
  196. goto put_child;
  197. }
  198. rc = of_unittest_check_node_linkage(child);
  199. if (rc < 0)
  200. goto put_child;
  201. count += rc;
  202. }
  203. return count + 1;
  204. put_child:
  205. of_node_put(child);
  206. return rc;
  207. }
  208. static void __init of_unittest_check_tree_linkage(void)
  209. {
  210. struct device_node *np;
  211. int allnode_count = 0, child_count;
  212. if (!of_root)
  213. return;
  214. for_each_of_allnodes(np)
  215. allnode_count++;
  216. child_count = of_unittest_check_node_linkage(of_root);
  217. unittest(child_count > 0, "Device node data structure is corrupted\n");
  218. unittest(child_count == allnode_count,
  219. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  220. allnode_count, child_count);
  221. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  222. }
  223. static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
  224. const char *expected)
  225. {
  226. unsigned char *buf;
  227. int buf_size;
  228. int size, i;
  229. buf_size = strlen(expected) + 10;
  230. buf = kmalloc(buf_size, GFP_KERNEL);
  231. if (!buf)
  232. return;
  233. /* Baseline; check conversion with a large size limit */
  234. memset(buf, 0xff, buf_size);
  235. size = snprintf(buf, buf_size - 2, fmt, np);
  236. /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
  237. unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
  238. "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
  239. fmt, expected, buf);
  240. /* Make sure length limits work */
  241. size++;
  242. for (i = 0; i < 2; i++, size--) {
  243. /* Clear the buffer, and make sure it works correctly still */
  244. memset(buf, 0xff, buf_size);
  245. snprintf(buf, size+1, fmt, np);
  246. unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
  247. "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
  248. size, fmt, expected, buf);
  249. }
  250. kfree(buf);
  251. }
  252. static void __init of_unittest_printf(void)
  253. {
  254. struct device_node *np;
  255. const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
  256. char phandle_str[16] = "";
  257. np = of_find_node_by_path(full_name);
  258. if (!np) {
  259. unittest(np, "testcase data missing\n");
  260. return;
  261. }
  262. num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
  263. of_unittest_printf_one(np, "%pOF", full_name);
  264. of_unittest_printf_one(np, "%pOFf", full_name);
  265. of_unittest_printf_one(np, "%pOFn", "dev");
  266. of_unittest_printf_one(np, "%2pOFn", "dev");
  267. of_unittest_printf_one(np, "%5pOFn", " dev");
  268. of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
  269. of_unittest_printf_one(np, "%pOFp", phandle_str);
  270. of_unittest_printf_one(np, "%pOFP", "dev@100");
  271. of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
  272. of_unittest_printf_one(np, "%10pOFP", " dev@100");
  273. of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
  274. of_unittest_printf_one(of_root, "%pOFP", "/");
  275. of_unittest_printf_one(np, "%pOFF", "----");
  276. of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
  277. of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
  278. of_unittest_printf_one(np, "%pOFc", "test-sub-device");
  279. of_unittest_printf_one(np, "%pOFC",
  280. "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
  281. }
  282. struct node_hash {
  283. struct hlist_node node;
  284. struct device_node *np;
  285. };
  286. static DEFINE_HASHTABLE(phandle_ht, 8);
  287. static void __init of_unittest_check_phandles(void)
  288. {
  289. struct device_node *np;
  290. struct node_hash *nh;
  291. struct hlist_node *tmp;
  292. int i, dup_count = 0, phandle_count = 0;
  293. for_each_of_allnodes(np) {
  294. if (!np->phandle)
  295. continue;
  296. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  297. if (nh->np->phandle == np->phandle) {
  298. pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
  299. np->phandle, nh->np, np);
  300. dup_count++;
  301. break;
  302. }
  303. }
  304. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  305. if (!nh)
  306. return;
  307. nh->np = np;
  308. hash_add(phandle_ht, &nh->node, np->phandle);
  309. phandle_count++;
  310. }
  311. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  312. dup_count, phandle_count);
  313. /* Clean up */
  314. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  315. hash_del(&nh->node);
  316. kfree(nh);
  317. }
  318. }
  319. static void __init of_unittest_parse_phandle_with_args(void)
  320. {
  321. struct device_node *np;
  322. struct of_phandle_args args;
  323. int i, rc;
  324. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  325. if (!np) {
  326. pr_err("missing testcase data\n");
  327. return;
  328. }
  329. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  330. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  331. for (i = 0; i < 8; i++) {
  332. bool passed = true;
  333. memset(&args, 0, sizeof(args));
  334. rc = of_parse_phandle_with_args(np, "phandle-list",
  335. "#phandle-cells", i, &args);
  336. /* Test the values from tests-phandle.dtsi */
  337. switch (i) {
  338. case 0:
  339. passed &= !rc;
  340. passed &= (args.args_count == 1);
  341. passed &= (args.args[0] == (i + 1));
  342. break;
  343. case 1:
  344. passed &= !rc;
  345. passed &= (args.args_count == 2);
  346. passed &= (args.args[0] == (i + 1));
  347. passed &= (args.args[1] == 0);
  348. break;
  349. case 2:
  350. passed &= (rc == -ENOENT);
  351. break;
  352. case 3:
  353. passed &= !rc;
  354. passed &= (args.args_count == 3);
  355. passed &= (args.args[0] == (i + 1));
  356. passed &= (args.args[1] == 4);
  357. passed &= (args.args[2] == 3);
  358. break;
  359. case 4:
  360. passed &= !rc;
  361. passed &= (args.args_count == 2);
  362. passed &= (args.args[0] == (i + 1));
  363. passed &= (args.args[1] == 100);
  364. break;
  365. case 5:
  366. passed &= !rc;
  367. passed &= (args.args_count == 0);
  368. break;
  369. case 6:
  370. passed &= !rc;
  371. passed &= (args.args_count == 1);
  372. passed &= (args.args[0] == (i + 1));
  373. break;
  374. case 7:
  375. passed &= (rc == -ENOENT);
  376. break;
  377. default:
  378. passed = false;
  379. }
  380. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  381. i, args.np, rc);
  382. }
  383. /* Check for missing list property */
  384. memset(&args, 0, sizeof(args));
  385. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  386. "#phandle-cells", 0, &args);
  387. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  388. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  389. "#phandle-cells");
  390. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  391. /* Check for missing cells property */
  392. memset(&args, 0, sizeof(args));
  393. EXPECT_BEGIN(KERN_INFO,
  394. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  395. rc = of_parse_phandle_with_args(np, "phandle-list",
  396. "#phandle-cells-missing", 0, &args);
  397. EXPECT_END(KERN_INFO,
  398. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  399. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  400. EXPECT_BEGIN(KERN_INFO,
  401. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  402. rc = of_count_phandle_with_args(np, "phandle-list",
  403. "#phandle-cells-missing");
  404. EXPECT_END(KERN_INFO,
  405. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  406. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  407. /* Check for bad phandle in list */
  408. memset(&args, 0, sizeof(args));
  409. EXPECT_BEGIN(KERN_INFO,
  410. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  411. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  412. "#phandle-cells", 0, &args);
  413. EXPECT_END(KERN_INFO,
  414. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  415. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  416. EXPECT_BEGIN(KERN_INFO,
  417. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  418. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  419. "#phandle-cells");
  420. EXPECT_END(KERN_INFO,
  421. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  422. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  423. /* Check for incorrectly formed argument list */
  424. memset(&args, 0, sizeof(args));
  425. EXPECT_BEGIN(KERN_INFO,
  426. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
  427. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  428. "#phandle-cells", 1, &args);
  429. EXPECT_END(KERN_INFO,
  430. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
  431. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  432. EXPECT_BEGIN(KERN_INFO,
  433. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
  434. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  435. "#phandle-cells");
  436. EXPECT_END(KERN_INFO,
  437. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
  438. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  439. }
  440. static void __init of_unittest_parse_phandle_with_args_map(void)
  441. {
  442. struct device_node *np, *p0, *p1, *p2, *p3;
  443. struct of_phandle_args args;
  444. int i, rc;
  445. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
  446. if (!np) {
  447. pr_err("missing testcase data\n");
  448. return;
  449. }
  450. p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
  451. if (!p0) {
  452. pr_err("missing testcase data\n");
  453. return;
  454. }
  455. p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
  456. if (!p1) {
  457. pr_err("missing testcase data\n");
  458. return;
  459. }
  460. p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
  461. if (!p2) {
  462. pr_err("missing testcase data\n");
  463. return;
  464. }
  465. p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
  466. if (!p3) {
  467. pr_err("missing testcase data\n");
  468. return;
  469. }
  470. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  471. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  472. for (i = 0; i < 8; i++) {
  473. bool passed = true;
  474. memset(&args, 0, sizeof(args));
  475. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  476. "phandle", i, &args);
  477. /* Test the values from tests-phandle.dtsi */
  478. switch (i) {
  479. case 0:
  480. passed &= !rc;
  481. passed &= (args.np == p1);
  482. passed &= (args.args_count == 1);
  483. passed &= (args.args[0] == 1);
  484. break;
  485. case 1:
  486. passed &= !rc;
  487. passed &= (args.np == p3);
  488. passed &= (args.args_count == 3);
  489. passed &= (args.args[0] == 2);
  490. passed &= (args.args[1] == 5);
  491. passed &= (args.args[2] == 3);
  492. break;
  493. case 2:
  494. passed &= (rc == -ENOENT);
  495. break;
  496. case 3:
  497. passed &= !rc;
  498. passed &= (args.np == p0);
  499. passed &= (args.args_count == 0);
  500. break;
  501. case 4:
  502. passed &= !rc;
  503. passed &= (args.np == p1);
  504. passed &= (args.args_count == 1);
  505. passed &= (args.args[0] == 3);
  506. break;
  507. case 5:
  508. passed &= !rc;
  509. passed &= (args.np == p0);
  510. passed &= (args.args_count == 0);
  511. break;
  512. case 6:
  513. passed &= !rc;
  514. passed &= (args.np == p2);
  515. passed &= (args.args_count == 2);
  516. passed &= (args.args[0] == 15);
  517. passed &= (args.args[1] == 0x20);
  518. break;
  519. case 7:
  520. passed &= (rc == -ENOENT);
  521. break;
  522. default:
  523. passed = false;
  524. }
  525. unittest(passed, "index %i - data error on node %s rc=%i\n",
  526. i, args.np->full_name, rc);
  527. }
  528. /* Check for missing list property */
  529. memset(&args, 0, sizeof(args));
  530. rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
  531. "phandle", 0, &args);
  532. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  533. /* Check for missing cells,map,mask property */
  534. memset(&args, 0, sizeof(args));
  535. EXPECT_BEGIN(KERN_INFO,
  536. "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
  537. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  538. "phandle-missing", 0, &args);
  539. EXPECT_END(KERN_INFO,
  540. "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
  541. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  542. /* Check for bad phandle in list */
  543. memset(&args, 0, sizeof(args));
  544. EXPECT_BEGIN(KERN_INFO,
  545. "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
  546. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
  547. "phandle", 0, &args);
  548. EXPECT_END(KERN_INFO,
  549. "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
  550. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  551. /* Check for incorrectly formed argument list */
  552. memset(&args, 0, sizeof(args));
  553. EXPECT_BEGIN(KERN_INFO,
  554. "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
  555. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
  556. "phandle", 1, &args);
  557. EXPECT_END(KERN_INFO,
  558. "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
  559. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  560. }
  561. static void __init of_unittest_property_string(void)
  562. {
  563. const char *strings[4];
  564. struct device_node *np;
  565. int rc;
  566. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  567. if (!np) {
  568. pr_err("No testcase data in device tree\n");
  569. return;
  570. }
  571. rc = of_property_match_string(np, "phandle-list-names", "first");
  572. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  573. rc = of_property_match_string(np, "phandle-list-names", "second");
  574. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  575. rc = of_property_match_string(np, "phandle-list-names", "third");
  576. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  577. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  578. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  579. rc = of_property_match_string(np, "missing-property", "blah");
  580. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  581. rc = of_property_match_string(np, "empty-property", "blah");
  582. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  583. rc = of_property_match_string(np, "unterminated-string", "blah");
  584. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  585. /* of_property_count_strings() tests */
  586. rc = of_property_count_strings(np, "string-property");
  587. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  588. rc = of_property_count_strings(np, "phandle-list-names");
  589. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  590. rc = of_property_count_strings(np, "unterminated-string");
  591. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  592. rc = of_property_count_strings(np, "unterminated-string-list");
  593. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  594. /* of_property_read_string_index() tests */
  595. rc = of_property_read_string_index(np, "string-property", 0, strings);
  596. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  597. strings[0] = NULL;
  598. rc = of_property_read_string_index(np, "string-property", 1, strings);
  599. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  600. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  601. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  602. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  603. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  604. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  605. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  606. strings[0] = NULL;
  607. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  608. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  609. strings[0] = NULL;
  610. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  611. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  612. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  613. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  614. strings[0] = NULL;
  615. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  616. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  617. strings[1] = NULL;
  618. /* of_property_read_string_array() tests */
  619. rc = of_property_read_string_array(np, "string-property", strings, 4);
  620. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  621. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  622. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  623. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  624. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  625. /* -- An incorrectly formed string should cause a failure */
  626. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  627. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  628. /* -- parsing the correctly formed strings should still work: */
  629. strings[2] = NULL;
  630. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  631. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  632. strings[1] = NULL;
  633. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  634. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  635. }
  636. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  637. (p1)->value && (p2)->value && \
  638. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  639. !strcmp((p1)->name, (p2)->name))
  640. static void __init of_unittest_property_copy(void)
  641. {
  642. #ifdef CONFIG_OF_DYNAMIC
  643. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  644. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  645. struct property *new;
  646. new = __of_prop_dup(&p1, GFP_KERNEL);
  647. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  648. kfree(new->value);
  649. kfree(new->name);
  650. kfree(new);
  651. new = __of_prop_dup(&p2, GFP_KERNEL);
  652. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  653. kfree(new->value);
  654. kfree(new->name);
  655. kfree(new);
  656. #endif
  657. }
  658. static void __init of_unittest_changeset(void)
  659. {
  660. #ifdef CONFIG_OF_DYNAMIC
  661. struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
  662. struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
  663. struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
  664. struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
  665. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  666. struct property *ppremove;
  667. struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
  668. struct of_changeset chgset;
  669. n1 = __of_node_dup(NULL, "n1");
  670. unittest(n1, "testcase setup failure\n");
  671. n2 = __of_node_dup(NULL, "n2");
  672. unittest(n2, "testcase setup failure\n");
  673. n21 = __of_node_dup(NULL, "n21");
  674. unittest(n21, "testcase setup failure %p\n", n21);
  675. nchangeset = of_find_node_by_path("/testcase-data/changeset");
  676. nremove = of_get_child_by_name(nchangeset, "node-remove");
  677. unittest(nremove, "testcase setup failure\n");
  678. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  679. unittest(ppadd, "testcase setup failure\n");
  680. ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
  681. unittest(ppname_n1, "testcase setup failure\n");
  682. ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
  683. unittest(ppname_n2, "testcase setup failure\n");
  684. ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
  685. unittest(ppname_n21, "testcase setup failure\n");
  686. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  687. unittest(ppupdate, "testcase setup failure\n");
  688. parent = nchangeset;
  689. n1->parent = parent;
  690. n2->parent = parent;
  691. n21->parent = n2;
  692. ppremove = of_find_property(parent, "prop-remove", NULL);
  693. unittest(ppremove, "failed to find removal prop");
  694. of_changeset_init(&chgset);
  695. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  696. unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
  697. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  698. unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
  699. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  700. unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
  701. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  702. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
  703. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  704. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  705. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  706. of_node_put(nchangeset);
  707. /* Make sure node names are constructed correctly */
  708. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  709. "'%pOF' not added\n", n21);
  710. of_node_put(np);
  711. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  712. of_changeset_destroy(&chgset);
  713. of_node_put(n1);
  714. of_node_put(n2);
  715. of_node_put(n21);
  716. #endif
  717. }
  718. static void __init of_unittest_dma_get_max_cpu_address(void)
  719. {
  720. struct device_node *np;
  721. phys_addr_t cpu_addr;
  722. if (!IS_ENABLED(CONFIG_OF_ADDRESS))
  723. return;
  724. np = of_find_node_by_path("/testcase-data/address-tests");
  725. if (!np) {
  726. pr_err("missing testcase data\n");
  727. return;
  728. }
  729. cpu_addr = of_dma_get_max_cpu_address(np);
  730. unittest(cpu_addr == 0x4fffffff,
  731. "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
  732. &cpu_addr, 0x4fffffff);
  733. }
  734. static void __init of_unittest_dma_ranges_one(const char *path,
  735. u64 expect_dma_addr, u64 expect_paddr)
  736. {
  737. #ifdef CONFIG_HAS_DMA
  738. struct device_node *np;
  739. const struct bus_dma_region *map = NULL;
  740. int rc;
  741. np = of_find_node_by_path(path);
  742. if (!np) {
  743. pr_err("missing testcase data\n");
  744. return;
  745. }
  746. rc = of_dma_get_range(np, &map);
  747. unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
  748. if (!rc) {
  749. phys_addr_t paddr;
  750. dma_addr_t dma_addr;
  751. struct device *dev_bogus;
  752. dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
  753. if (!dev_bogus) {
  754. unittest(0, "kzalloc() failed\n");
  755. kfree(map);
  756. return;
  757. }
  758. dev_bogus->dma_range_map = map;
  759. paddr = dma_to_phys(dev_bogus, expect_dma_addr);
  760. dma_addr = phys_to_dma(dev_bogus, expect_paddr);
  761. unittest(paddr == expect_paddr,
  762. "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
  763. &paddr, expect_paddr, np);
  764. unittest(dma_addr == expect_dma_addr,
  765. "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
  766. &dma_addr, expect_dma_addr, np);
  767. kfree(map);
  768. kfree(dev_bogus);
  769. }
  770. of_node_put(np);
  771. #endif
  772. }
  773. static void __init of_unittest_parse_dma_ranges(void)
  774. {
  775. of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
  776. 0x0, 0x20000000);
  777. if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
  778. of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
  779. 0x100000000, 0x20000000);
  780. of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
  781. 0x80000000, 0x20000000);
  782. }
  783. static void __init of_unittest_pci_dma_ranges(void)
  784. {
  785. struct device_node *np;
  786. struct of_pci_range range;
  787. struct of_pci_range_parser parser;
  788. int i = 0;
  789. if (!IS_ENABLED(CONFIG_PCI))
  790. return;
  791. np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
  792. if (!np) {
  793. pr_err("missing testcase data\n");
  794. return;
  795. }
  796. if (of_pci_dma_range_parser_init(&parser, np)) {
  797. pr_err("missing dma-ranges property\n");
  798. return;
  799. }
  800. /*
  801. * Get the dma-ranges from the device tree
  802. */
  803. for_each_of_pci_range(&parser, &range) {
  804. if (!i) {
  805. unittest(range.size == 0x10000000,
  806. "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
  807. np, range.size);
  808. unittest(range.cpu_addr == 0x20000000,
  809. "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
  810. range.cpu_addr, np);
  811. unittest(range.pci_addr == 0x80000000,
  812. "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
  813. range.pci_addr, np);
  814. } else {
  815. unittest(range.size == 0x10000000,
  816. "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
  817. np, range.size);
  818. unittest(range.cpu_addr == 0x40000000,
  819. "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
  820. range.cpu_addr, np);
  821. unittest(range.pci_addr == 0xc0000000,
  822. "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
  823. range.pci_addr, np);
  824. }
  825. i++;
  826. }
  827. of_node_put(np);
  828. }
  829. static void __init of_unittest_parse_interrupts(void)
  830. {
  831. struct device_node *np;
  832. struct of_phandle_args args;
  833. int i, rc;
  834. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  835. return;
  836. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  837. if (!np) {
  838. pr_err("missing testcase data\n");
  839. return;
  840. }
  841. for (i = 0; i < 4; i++) {
  842. bool passed = true;
  843. memset(&args, 0, sizeof(args));
  844. rc = of_irq_parse_one(np, i, &args);
  845. passed &= !rc;
  846. passed &= (args.args_count == 1);
  847. passed &= (args.args[0] == (i + 1));
  848. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  849. i, args.np, rc);
  850. }
  851. of_node_put(np);
  852. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  853. if (!np) {
  854. pr_err("missing testcase data\n");
  855. return;
  856. }
  857. for (i = 0; i < 4; i++) {
  858. bool passed = true;
  859. memset(&args, 0, sizeof(args));
  860. rc = of_irq_parse_one(np, i, &args);
  861. /* Test the values from tests-phandle.dtsi */
  862. switch (i) {
  863. case 0:
  864. passed &= !rc;
  865. passed &= (args.args_count == 1);
  866. passed &= (args.args[0] == 9);
  867. break;
  868. case 1:
  869. passed &= !rc;
  870. passed &= (args.args_count == 3);
  871. passed &= (args.args[0] == 10);
  872. passed &= (args.args[1] == 11);
  873. passed &= (args.args[2] == 12);
  874. break;
  875. case 2:
  876. passed &= !rc;
  877. passed &= (args.args_count == 2);
  878. passed &= (args.args[0] == 13);
  879. passed &= (args.args[1] == 14);
  880. break;
  881. case 3:
  882. passed &= !rc;
  883. passed &= (args.args_count == 2);
  884. passed &= (args.args[0] == 15);
  885. passed &= (args.args[1] == 16);
  886. break;
  887. default:
  888. passed = false;
  889. }
  890. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  891. i, args.np, rc);
  892. }
  893. of_node_put(np);
  894. }
  895. static void __init of_unittest_parse_interrupts_extended(void)
  896. {
  897. struct device_node *np;
  898. struct of_phandle_args args;
  899. int i, rc;
  900. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  901. return;
  902. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  903. if (!np) {
  904. pr_err("missing testcase data\n");
  905. return;
  906. }
  907. for (i = 0; i < 7; i++) {
  908. bool passed = true;
  909. memset(&args, 0, sizeof(args));
  910. rc = of_irq_parse_one(np, i, &args);
  911. /* Test the values from tests-phandle.dtsi */
  912. switch (i) {
  913. case 0:
  914. passed &= !rc;
  915. passed &= (args.args_count == 1);
  916. passed &= (args.args[0] == 1);
  917. break;
  918. case 1:
  919. passed &= !rc;
  920. passed &= (args.args_count == 3);
  921. passed &= (args.args[0] == 2);
  922. passed &= (args.args[1] == 3);
  923. passed &= (args.args[2] == 4);
  924. break;
  925. case 2:
  926. passed &= !rc;
  927. passed &= (args.args_count == 2);
  928. passed &= (args.args[0] == 5);
  929. passed &= (args.args[1] == 6);
  930. break;
  931. case 3:
  932. passed &= !rc;
  933. passed &= (args.args_count == 1);
  934. passed &= (args.args[0] == 9);
  935. break;
  936. case 4:
  937. passed &= !rc;
  938. passed &= (args.args_count == 3);
  939. passed &= (args.args[0] == 10);
  940. passed &= (args.args[1] == 11);
  941. passed &= (args.args[2] == 12);
  942. break;
  943. case 5:
  944. passed &= !rc;
  945. passed &= (args.args_count == 2);
  946. passed &= (args.args[0] == 13);
  947. passed &= (args.args[1] == 14);
  948. break;
  949. case 6:
  950. passed &= !rc;
  951. passed &= (args.args_count == 1);
  952. passed &= (args.args[0] == 15);
  953. break;
  954. default:
  955. passed = false;
  956. }
  957. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  958. i, args.np, rc);
  959. }
  960. of_node_put(np);
  961. }
  962. static const struct of_device_id match_node_table[] = {
  963. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  964. { .data = "B", .type = "type1", }, /* followed by type alone */
  965. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  966. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  967. { .data = "Cc", .name = "name2", .type = "type2", },
  968. { .data = "E", .compatible = "compat3" },
  969. { .data = "G", .compatible = "compat2", },
  970. { .data = "H", .compatible = "compat2", .name = "name5", },
  971. { .data = "I", .compatible = "compat2", .type = "type1", },
  972. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  973. { .data = "K", .compatible = "compat2", .name = "name9", },
  974. {}
  975. };
  976. static struct {
  977. const char *path;
  978. const char *data;
  979. } match_node_tests[] = {
  980. { .path = "/testcase-data/match-node/name0", .data = "A", },
  981. { .path = "/testcase-data/match-node/name1", .data = "B", },
  982. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  983. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  984. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  985. { .path = "/testcase-data/match-node/name3", .data = "E", },
  986. { .path = "/testcase-data/match-node/name4", .data = "G", },
  987. { .path = "/testcase-data/match-node/name5", .data = "H", },
  988. { .path = "/testcase-data/match-node/name6", .data = "G", },
  989. { .path = "/testcase-data/match-node/name7", .data = "I", },
  990. { .path = "/testcase-data/match-node/name8", .data = "J", },
  991. { .path = "/testcase-data/match-node/name9", .data = "K", },
  992. };
  993. static void __init of_unittest_match_node(void)
  994. {
  995. struct device_node *np;
  996. const struct of_device_id *match;
  997. int i;
  998. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  999. np = of_find_node_by_path(match_node_tests[i].path);
  1000. if (!np) {
  1001. unittest(0, "missing testcase node %s\n",
  1002. match_node_tests[i].path);
  1003. continue;
  1004. }
  1005. match = of_match_node(match_node_table, np);
  1006. if (!match) {
  1007. unittest(0, "%s didn't match anything\n",
  1008. match_node_tests[i].path);
  1009. continue;
  1010. }
  1011. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  1012. unittest(0, "%s got wrong match. expected %s, got %s\n",
  1013. match_node_tests[i].path, match_node_tests[i].data,
  1014. (const char *)match->data);
  1015. continue;
  1016. }
  1017. unittest(1, "passed");
  1018. }
  1019. }
  1020. static struct resource test_bus_res = {
  1021. .start = 0xfffffff8,
  1022. .end = 0xfffffff9,
  1023. .flags = IORESOURCE_MEM,
  1024. };
  1025. static const struct platform_device_info test_bus_info = {
  1026. .name = "unittest-bus",
  1027. };
  1028. static void __init of_unittest_platform_populate(void)
  1029. {
  1030. int irq, rc;
  1031. struct device_node *np, *child, *grandchild;
  1032. struct platform_device *pdev, *test_bus;
  1033. const struct of_device_id match[] = {
  1034. { .compatible = "test-device", },
  1035. {}
  1036. };
  1037. np = of_find_node_by_path("/testcase-data");
  1038. of_platform_default_populate(np, NULL, NULL);
  1039. /* Test that a missing irq domain returns -EPROBE_DEFER */
  1040. np = of_find_node_by_path("/testcase-data/testcase-device1");
  1041. pdev = of_find_device_by_node(np);
  1042. unittest(pdev, "device 1 creation failed\n");
  1043. if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
  1044. irq = platform_get_irq(pdev, 0);
  1045. unittest(irq == -EPROBE_DEFER,
  1046. "device deferred probe failed - %d\n", irq);
  1047. /* Test that a parsing failure does not return -EPROBE_DEFER */
  1048. np = of_find_node_by_path("/testcase-data/testcase-device2");
  1049. pdev = of_find_device_by_node(np);
  1050. unittest(pdev, "device 2 creation failed\n");
  1051. EXPECT_BEGIN(KERN_INFO,
  1052. "platform testcase-data:testcase-device2: IRQ index 0 not found");
  1053. irq = platform_get_irq(pdev, 0);
  1054. EXPECT_END(KERN_INFO,
  1055. "platform testcase-data:testcase-device2: IRQ index 0 not found");
  1056. unittest(irq < 0 && irq != -EPROBE_DEFER,
  1057. "device parsing error failed - %d\n", irq);
  1058. }
  1059. np = of_find_node_by_path("/testcase-data/platform-tests");
  1060. unittest(np, "No testcase data in device tree\n");
  1061. if (!np)
  1062. return;
  1063. test_bus = platform_device_register_full(&test_bus_info);
  1064. rc = PTR_ERR_OR_ZERO(test_bus);
  1065. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  1066. if (rc) {
  1067. of_node_put(np);
  1068. return;
  1069. }
  1070. test_bus->dev.of_node = np;
  1071. /*
  1072. * Add a dummy resource to the test bus node after it is
  1073. * registered to catch problems with un-inserted resources. The
  1074. * DT code doesn't insert the resources, and it has caused the
  1075. * kernel to oops in the past. This makes sure the same bug
  1076. * doesn't crop up again.
  1077. */
  1078. platform_device_add_resources(test_bus, &test_bus_res, 1);
  1079. of_platform_populate(np, match, NULL, &test_bus->dev);
  1080. for_each_child_of_node(np, child) {
  1081. for_each_child_of_node(child, grandchild) {
  1082. pdev = of_find_device_by_node(grandchild);
  1083. unittest(pdev,
  1084. "Could not create device for node '%pOFn'\n",
  1085. grandchild);
  1086. of_dev_put(pdev);
  1087. }
  1088. }
  1089. of_platform_depopulate(&test_bus->dev);
  1090. for_each_child_of_node(np, child) {
  1091. for_each_child_of_node(child, grandchild)
  1092. unittest(!of_find_device_by_node(grandchild),
  1093. "device didn't get destroyed '%pOFn'\n",
  1094. grandchild);
  1095. }
  1096. platform_device_unregister(test_bus);
  1097. of_node_put(np);
  1098. }
  1099. /**
  1100. * update_node_properties - adds the properties
  1101. * of np into dup node (present in live tree) and
  1102. * updates parent of children of np to dup.
  1103. *
  1104. * @np: node whose properties are being added to the live tree
  1105. * @dup: node present in live tree to be updated
  1106. */
  1107. static void update_node_properties(struct device_node *np,
  1108. struct device_node *dup)
  1109. {
  1110. struct property *prop;
  1111. struct property *save_next;
  1112. struct device_node *child;
  1113. int ret;
  1114. for_each_child_of_node(np, child)
  1115. child->parent = dup;
  1116. /*
  1117. * "unittest internal error: unable to add testdata property"
  1118. *
  1119. * If this message reports a property in node '/__symbols__' then
  1120. * the respective unittest overlay contains a label that has the
  1121. * same name as a label in the live devicetree. The label will
  1122. * be in the live devicetree only if the devicetree source was
  1123. * compiled with the '-@' option. If you encounter this error,
  1124. * please consider renaming __all__ of the labels in the unittest
  1125. * overlay dts files with an odd prefix that is unlikely to be
  1126. * used in a real devicetree.
  1127. */
  1128. /*
  1129. * open code for_each_property_of_node() because of_add_property()
  1130. * sets prop->next to NULL
  1131. */
  1132. for (prop = np->properties; prop != NULL; prop = save_next) {
  1133. save_next = prop->next;
  1134. ret = of_add_property(dup, prop);
  1135. if (ret) {
  1136. if (ret == -EEXIST && !strcmp(prop->name, "name"))
  1137. continue;
  1138. pr_err("unittest internal error: unable to add testdata property %pOF/%s",
  1139. np, prop->name);
  1140. }
  1141. }
  1142. }
  1143. /**
  1144. * attach_node_and_children - attaches nodes
  1145. * and its children to live tree.
  1146. * CAUTION: misleading function name - if node @np already exists in
  1147. * the live tree then children of @np are *not* attached to the live
  1148. * tree. This works for the current test devicetree nodes because such
  1149. * nodes do not have child nodes.
  1150. *
  1151. * @np: Node to attach to live tree
  1152. */
  1153. static void attach_node_and_children(struct device_node *np)
  1154. {
  1155. struct device_node *next, *dup, *child;
  1156. unsigned long flags;
  1157. const char *full_name;
  1158. full_name = kasprintf(GFP_KERNEL, "%pOF", np);
  1159. if (!strcmp(full_name, "/__local_fixups__") ||
  1160. !strcmp(full_name, "/__fixups__")) {
  1161. kfree(full_name);
  1162. return;
  1163. }
  1164. dup = of_find_node_by_path(full_name);
  1165. kfree(full_name);
  1166. if (dup) {
  1167. update_node_properties(np, dup);
  1168. return;
  1169. }
  1170. child = np->child;
  1171. np->child = NULL;
  1172. mutex_lock(&of_mutex);
  1173. raw_spin_lock_irqsave(&devtree_lock, flags);
  1174. np->sibling = np->parent->child;
  1175. np->parent->child = np;
  1176. of_node_clear_flag(np, OF_DETACHED);
  1177. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1178. __of_attach_node_sysfs(np);
  1179. mutex_unlock(&of_mutex);
  1180. while (child) {
  1181. next = child->sibling;
  1182. attach_node_and_children(child);
  1183. child = next;
  1184. }
  1185. }
  1186. /**
  1187. * unittest_data_add - Reads, copies data from
  1188. * linked tree and attaches it to the live tree
  1189. */
  1190. static int __init unittest_data_add(void)
  1191. {
  1192. void *unittest_data;
  1193. struct device_node *unittest_data_node, *np;
  1194. /*
  1195. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  1196. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  1197. */
  1198. extern uint8_t __dtb_testcases_begin[];
  1199. extern uint8_t __dtb_testcases_end[];
  1200. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  1201. int rc;
  1202. if (!size) {
  1203. pr_warn("%s: No testcase data to attach; not running tests\n",
  1204. __func__);
  1205. return -ENODATA;
  1206. }
  1207. /* creating copy */
  1208. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  1209. if (!unittest_data)
  1210. return -ENOMEM;
  1211. of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
  1212. if (!unittest_data_node) {
  1213. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  1214. kfree(unittest_data);
  1215. return -ENODATA;
  1216. }
  1217. /*
  1218. * This lock normally encloses of_resolve_phandles()
  1219. */
  1220. of_overlay_mutex_lock();
  1221. rc = of_resolve_phandles(unittest_data_node);
  1222. if (rc) {
  1223. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  1224. of_overlay_mutex_unlock();
  1225. return -EINVAL;
  1226. }
  1227. if (!of_root) {
  1228. of_root = unittest_data_node;
  1229. for_each_of_allnodes(np)
  1230. __of_attach_node_sysfs(np);
  1231. of_aliases = of_find_node_by_path("/aliases");
  1232. of_chosen = of_find_node_by_path("/chosen");
  1233. of_overlay_mutex_unlock();
  1234. return 0;
  1235. }
  1236. EXPECT_BEGIN(KERN_INFO,
  1237. "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
  1238. /* attach the sub-tree to live tree */
  1239. np = unittest_data_node->child;
  1240. while (np) {
  1241. struct device_node *next = np->sibling;
  1242. np->parent = of_root;
  1243. attach_node_and_children(np);
  1244. np = next;
  1245. }
  1246. EXPECT_END(KERN_INFO,
  1247. "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
  1248. of_overlay_mutex_unlock();
  1249. return 0;
  1250. }
  1251. #ifdef CONFIG_OF_OVERLAY
  1252. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
  1253. static int unittest_probe(struct platform_device *pdev)
  1254. {
  1255. struct device *dev = &pdev->dev;
  1256. struct device_node *np = dev->of_node;
  1257. if (np == NULL) {
  1258. dev_err(dev, "No OF data for device\n");
  1259. return -EINVAL;
  1260. }
  1261. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1262. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1263. return 0;
  1264. }
  1265. static int unittest_remove(struct platform_device *pdev)
  1266. {
  1267. struct device *dev = &pdev->dev;
  1268. struct device_node *np = dev->of_node;
  1269. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1270. return 0;
  1271. }
  1272. static const struct of_device_id unittest_match[] = {
  1273. { .compatible = "unittest", },
  1274. {},
  1275. };
  1276. static struct platform_driver unittest_driver = {
  1277. .probe = unittest_probe,
  1278. .remove = unittest_remove,
  1279. .driver = {
  1280. .name = "unittest",
  1281. .of_match_table = of_match_ptr(unittest_match),
  1282. },
  1283. };
  1284. /* get the platform device instantiated at the path */
  1285. static struct platform_device *of_path_to_platform_device(const char *path)
  1286. {
  1287. struct device_node *np;
  1288. struct platform_device *pdev;
  1289. np = of_find_node_by_path(path);
  1290. if (np == NULL)
  1291. return NULL;
  1292. pdev = of_find_device_by_node(np);
  1293. of_node_put(np);
  1294. return pdev;
  1295. }
  1296. /* find out if a platform device exists at that path */
  1297. static int of_path_platform_device_exists(const char *path)
  1298. {
  1299. struct platform_device *pdev;
  1300. pdev = of_path_to_platform_device(path);
  1301. platform_device_put(pdev);
  1302. return pdev != NULL;
  1303. }
  1304. #ifdef CONFIG_OF_GPIO
  1305. struct unittest_gpio_dev {
  1306. struct gpio_chip chip;
  1307. };
  1308. static int unittest_gpio_chip_request_count;
  1309. static int unittest_gpio_probe_count;
  1310. static int unittest_gpio_probe_pass_count;
  1311. static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
  1312. {
  1313. unittest_gpio_chip_request_count++;
  1314. pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
  1315. unittest_gpio_chip_request_count);
  1316. return 0;
  1317. }
  1318. static int unittest_gpio_probe(struct platform_device *pdev)
  1319. {
  1320. struct unittest_gpio_dev *devptr;
  1321. int ret;
  1322. unittest_gpio_probe_count++;
  1323. devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
  1324. if (!devptr)
  1325. return -ENOMEM;
  1326. platform_set_drvdata(pdev, devptr);
  1327. devptr->chip.of_node = pdev->dev.of_node;
  1328. devptr->chip.label = "of-unittest-gpio";
  1329. devptr->chip.base = -1; /* dynamic allocation */
  1330. devptr->chip.ngpio = 5;
  1331. devptr->chip.request = unittest_gpio_chip_request;
  1332. ret = gpiochip_add_data(&devptr->chip, NULL);
  1333. unittest(!ret,
  1334. "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr->chip.of_node, ret);
  1335. if (!ret)
  1336. unittest_gpio_probe_pass_count++;
  1337. return ret;
  1338. }
  1339. static int unittest_gpio_remove(struct platform_device *pdev)
  1340. {
  1341. struct unittest_gpio_dev *gdev = platform_get_drvdata(pdev);
  1342. struct device *dev = &pdev->dev;
  1343. struct device_node *np = pdev->dev.of_node;
  1344. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1345. if (!gdev)
  1346. return -EINVAL;
  1347. if (gdev->chip.base != -1)
  1348. gpiochip_remove(&gdev->chip);
  1349. platform_set_drvdata(pdev, NULL);
  1350. kfree(gdev);
  1351. return 0;
  1352. }
  1353. static const struct of_device_id unittest_gpio_id[] = {
  1354. { .compatible = "unittest-gpio", },
  1355. {}
  1356. };
  1357. static struct platform_driver unittest_gpio_driver = {
  1358. .probe = unittest_gpio_probe,
  1359. .remove = unittest_gpio_remove,
  1360. .driver = {
  1361. .name = "unittest-gpio",
  1362. .of_match_table = of_match_ptr(unittest_gpio_id),
  1363. },
  1364. };
  1365. static void __init of_unittest_overlay_gpio(void)
  1366. {
  1367. int chip_request_count;
  1368. int probe_pass_count;
  1369. int ret;
  1370. /*
  1371. * tests: apply overlays before registering driver
  1372. * Similar to installing a driver as a module, the
  1373. * driver is registered after applying the overlays.
  1374. *
  1375. * The overlays are applied by overlay_data_apply()
  1376. * instead of of_unittest_apply_overlay() so that they
  1377. * will not be tracked. Thus they will not be removed
  1378. * by of_unittest_destroy_tracked_overlays().
  1379. *
  1380. * - apply overlay_gpio_01
  1381. * - apply overlay_gpio_02a
  1382. * - apply overlay_gpio_02b
  1383. * - register driver
  1384. *
  1385. * register driver will result in
  1386. * - probe and processing gpio hog for overlay_gpio_01
  1387. * - probe for overlay_gpio_02a
  1388. * - processing gpio for overlay_gpio_02b
  1389. */
  1390. probe_pass_count = unittest_gpio_probe_pass_count;
  1391. chip_request_count = unittest_gpio_chip_request_count;
  1392. /*
  1393. * overlay_gpio_01 contains gpio node and child gpio hog node
  1394. * overlay_gpio_02a contains gpio node
  1395. * overlay_gpio_02b contains child gpio hog node
  1396. */
  1397. unittest(overlay_data_apply("overlay_gpio_01", NULL),
  1398. "Adding overlay 'overlay_gpio_01' failed\n");
  1399. unittest(overlay_data_apply("overlay_gpio_02a", NULL),
  1400. "Adding overlay 'overlay_gpio_02a' failed\n");
  1401. unittest(overlay_data_apply("overlay_gpio_02b", NULL),
  1402. "Adding overlay 'overlay_gpio_02b' failed\n");
  1403. /*
  1404. * messages are the result of the probes, after the
  1405. * driver is registered
  1406. */
  1407. EXPECT_BEGIN(KERN_INFO,
  1408. "gpio-<<int>> (line-B-input): hogged as input\n");
  1409. EXPECT_BEGIN(KERN_INFO,
  1410. "gpio-<<int>> (line-A-input): hogged as input\n");
  1411. ret = platform_driver_register(&unittest_gpio_driver);
  1412. if (unittest(ret == 0, "could not register unittest gpio driver\n"))
  1413. return;
  1414. EXPECT_END(KERN_INFO,
  1415. "gpio-<<int>> (line-A-input): hogged as input\n");
  1416. EXPECT_END(KERN_INFO,
  1417. "gpio-<<int>> (line-B-input): hogged as input\n");
  1418. unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
  1419. "unittest_gpio_probe() failed or not called\n");
  1420. unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
  1421. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1422. unittest_gpio_chip_request_count - chip_request_count);
  1423. /*
  1424. * tests: apply overlays after registering driver
  1425. *
  1426. * Similar to a driver built-in to the kernel, the
  1427. * driver is registered before applying the overlays.
  1428. *
  1429. * overlay_gpio_03 contains gpio node and child gpio hog node
  1430. *
  1431. * - apply overlay_gpio_03
  1432. *
  1433. * apply overlay will result in
  1434. * - probe and processing gpio hog.
  1435. */
  1436. probe_pass_count = unittest_gpio_probe_pass_count;
  1437. chip_request_count = unittest_gpio_chip_request_count;
  1438. EXPECT_BEGIN(KERN_INFO,
  1439. "gpio-<<int>> (line-D-input): hogged as input\n");
  1440. /* overlay_gpio_03 contains gpio node and child gpio hog node */
  1441. unittest(overlay_data_apply("overlay_gpio_03", NULL),
  1442. "Adding overlay 'overlay_gpio_03' failed\n");
  1443. EXPECT_END(KERN_INFO,
  1444. "gpio-<<int>> (line-D-input): hogged as input\n");
  1445. unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
  1446. "unittest_gpio_probe() failed or not called\n");
  1447. unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
  1448. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1449. unittest_gpio_chip_request_count - chip_request_count);
  1450. /*
  1451. * overlay_gpio_04a contains gpio node
  1452. *
  1453. * - apply overlay_gpio_04a
  1454. *
  1455. * apply the overlay will result in
  1456. * - probe for overlay_gpio_04a
  1457. */
  1458. probe_pass_count = unittest_gpio_probe_pass_count;
  1459. chip_request_count = unittest_gpio_chip_request_count;
  1460. /* overlay_gpio_04a contains gpio node */
  1461. unittest(overlay_data_apply("overlay_gpio_04a", NULL),
  1462. "Adding overlay 'overlay_gpio_04a' failed\n");
  1463. unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
  1464. "unittest_gpio_probe() failed or not called\n");
  1465. /*
  1466. * overlay_gpio_04b contains child gpio hog node
  1467. *
  1468. * - apply overlay_gpio_04b
  1469. *
  1470. * apply the overlay will result in
  1471. * - processing gpio for overlay_gpio_04b
  1472. */
  1473. EXPECT_BEGIN(KERN_INFO,
  1474. "gpio-<<int>> (line-C-input): hogged as input\n");
  1475. /* overlay_gpio_04b contains child gpio hog node */
  1476. unittest(overlay_data_apply("overlay_gpio_04b", NULL),
  1477. "Adding overlay 'overlay_gpio_04b' failed\n");
  1478. EXPECT_END(KERN_INFO,
  1479. "gpio-<<int>> (line-C-input): hogged as input\n");
  1480. unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
  1481. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1482. unittest_gpio_chip_request_count - chip_request_count);
  1483. }
  1484. #else
  1485. static void __init of_unittest_overlay_gpio(void)
  1486. {
  1487. /* skip tests */
  1488. }
  1489. #endif
  1490. #if IS_BUILTIN(CONFIG_I2C)
  1491. /* get the i2c client device instantiated at the path */
  1492. static struct i2c_client *of_path_to_i2c_client(const char *path)
  1493. {
  1494. struct device_node *np;
  1495. struct i2c_client *client;
  1496. np = of_find_node_by_path(path);
  1497. if (np == NULL)
  1498. return NULL;
  1499. client = of_find_i2c_device_by_node(np);
  1500. of_node_put(np);
  1501. return client;
  1502. }
  1503. /* find out if a i2c client device exists at that path */
  1504. static int of_path_i2c_client_exists(const char *path)
  1505. {
  1506. struct i2c_client *client;
  1507. client = of_path_to_i2c_client(path);
  1508. if (client)
  1509. put_device(&client->dev);
  1510. return client != NULL;
  1511. }
  1512. #else
  1513. static int of_path_i2c_client_exists(const char *path)
  1514. {
  1515. return 0;
  1516. }
  1517. #endif
  1518. enum overlay_type {
  1519. PDEV_OVERLAY,
  1520. I2C_OVERLAY
  1521. };
  1522. static int of_path_device_type_exists(const char *path,
  1523. enum overlay_type ovtype)
  1524. {
  1525. switch (ovtype) {
  1526. case PDEV_OVERLAY:
  1527. return of_path_platform_device_exists(path);
  1528. case I2C_OVERLAY:
  1529. return of_path_i2c_client_exists(path);
  1530. }
  1531. return 0;
  1532. }
  1533. static const char *unittest_path(int nr, enum overlay_type ovtype)
  1534. {
  1535. const char *base;
  1536. static char buf[256];
  1537. switch (ovtype) {
  1538. case PDEV_OVERLAY:
  1539. base = "/testcase-data/overlay-node/test-bus";
  1540. break;
  1541. case I2C_OVERLAY:
  1542. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  1543. break;
  1544. default:
  1545. buf[0] = '\0';
  1546. return buf;
  1547. }
  1548. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  1549. buf[sizeof(buf) - 1] = '\0';
  1550. return buf;
  1551. }
  1552. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  1553. {
  1554. const char *path;
  1555. path = unittest_path(unittest_nr, ovtype);
  1556. switch (ovtype) {
  1557. case PDEV_OVERLAY:
  1558. return of_path_platform_device_exists(path);
  1559. case I2C_OVERLAY:
  1560. return of_path_i2c_client_exists(path);
  1561. }
  1562. return 0;
  1563. }
  1564. static const char *overlay_name_from_nr(int nr)
  1565. {
  1566. static char buf[256];
  1567. snprintf(buf, sizeof(buf) - 1,
  1568. "overlay_%d", nr);
  1569. buf[sizeof(buf) - 1] = '\0';
  1570. return buf;
  1571. }
  1572. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  1573. /* FIXME: it is NOT guaranteed that overlay ids are assigned in sequence */
  1574. #define MAX_UNITTEST_OVERLAYS 256
  1575. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  1576. static int overlay_first_id = -1;
  1577. static long of_unittest_overlay_tracked(int id)
  1578. {
  1579. if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
  1580. return 0;
  1581. return overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id);
  1582. }
  1583. static void of_unittest_track_overlay(int id)
  1584. {
  1585. if (overlay_first_id < 0)
  1586. overlay_first_id = id;
  1587. id -= overlay_first_id;
  1588. if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
  1589. return;
  1590. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  1591. }
  1592. static void of_unittest_untrack_overlay(int id)
  1593. {
  1594. if (overlay_first_id < 0)
  1595. return;
  1596. id -= overlay_first_id;
  1597. if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
  1598. return;
  1599. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1600. }
  1601. static void of_unittest_destroy_tracked_overlays(void)
  1602. {
  1603. int id, ret, defers, ovcs_id;
  1604. if (overlay_first_id < 0)
  1605. return;
  1606. /* try until no defers */
  1607. do {
  1608. defers = 0;
  1609. /* remove in reverse order */
  1610. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  1611. if (!of_unittest_overlay_tracked(id))
  1612. continue;
  1613. ovcs_id = id + overlay_first_id;
  1614. ret = of_overlay_remove(&ovcs_id);
  1615. if (ret == -ENODEV) {
  1616. pr_warn("%s: no overlay to destroy for #%d\n",
  1617. __func__, id + overlay_first_id);
  1618. continue;
  1619. }
  1620. if (ret != 0) {
  1621. defers++;
  1622. pr_warn("%s: overlay destroy failed for #%d\n",
  1623. __func__, id + overlay_first_id);
  1624. continue;
  1625. }
  1626. of_unittest_untrack_overlay(id);
  1627. }
  1628. } while (defers > 0);
  1629. }
  1630. static int __init of_unittest_apply_overlay(int overlay_nr, int *overlay_id)
  1631. {
  1632. const char *overlay_name;
  1633. overlay_name = overlay_name_from_nr(overlay_nr);
  1634. if (!overlay_data_apply(overlay_name, overlay_id)) {
  1635. unittest(0, "could not apply overlay \"%s\"\n",
  1636. overlay_name);
  1637. return -EFAULT;
  1638. }
  1639. of_unittest_track_overlay(*overlay_id);
  1640. return 0;
  1641. }
  1642. /* apply an overlay while checking before and after states */
  1643. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  1644. int unittest_nr, int before, int after,
  1645. enum overlay_type ovtype)
  1646. {
  1647. int ret, ovcs_id;
  1648. /* unittest device must not be in before state */
  1649. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1650. unittest(0, "%s with device @\"%s\" %s\n",
  1651. overlay_name_from_nr(overlay_nr),
  1652. unittest_path(unittest_nr, ovtype),
  1653. !before ? "enabled" : "disabled");
  1654. return -EINVAL;
  1655. }
  1656. ovcs_id = 0;
  1657. ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
  1658. if (ret != 0) {
  1659. /* of_unittest_apply_overlay already called unittest() */
  1660. return ret;
  1661. }
  1662. /* unittest device must be to set to after state */
  1663. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1664. unittest(0, "%s failed to create @\"%s\" %s\n",
  1665. overlay_name_from_nr(overlay_nr),
  1666. unittest_path(unittest_nr, ovtype),
  1667. !after ? "enabled" : "disabled");
  1668. return -EINVAL;
  1669. }
  1670. return 0;
  1671. }
  1672. /* apply an overlay and then revert it while checking before, after states */
  1673. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  1674. int unittest_nr, int before, int after,
  1675. enum overlay_type ovtype)
  1676. {
  1677. int ret, ovcs_id, save_id;
  1678. /* unittest device must be in before state */
  1679. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1680. unittest(0, "%s with device @\"%s\" %s\n",
  1681. overlay_name_from_nr(overlay_nr),
  1682. unittest_path(unittest_nr, ovtype),
  1683. !before ? "enabled" : "disabled");
  1684. return -EINVAL;
  1685. }
  1686. /* apply the overlay */
  1687. ovcs_id = 0;
  1688. ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
  1689. if (ret != 0) {
  1690. /* of_unittest_apply_overlay already called unittest() */
  1691. return ret;
  1692. }
  1693. /* unittest device must be in after state */
  1694. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1695. unittest(0, "%s failed to create @\"%s\" %s\n",
  1696. overlay_name_from_nr(overlay_nr),
  1697. unittest_path(unittest_nr, ovtype),
  1698. !after ? "enabled" : "disabled");
  1699. return -EINVAL;
  1700. }
  1701. save_id = ovcs_id;
  1702. ret = of_overlay_remove(&ovcs_id);
  1703. if (ret != 0) {
  1704. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  1705. overlay_name_from_nr(overlay_nr),
  1706. unittest_path(unittest_nr, ovtype));
  1707. return ret;
  1708. }
  1709. of_unittest_untrack_overlay(save_id);
  1710. /* unittest device must be again in before state */
  1711. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1712. unittest(0, "%s with device @\"%s\" %s\n",
  1713. overlay_name_from_nr(overlay_nr),
  1714. unittest_path(unittest_nr, ovtype),
  1715. !before ? "enabled" : "disabled");
  1716. return -EINVAL;
  1717. }
  1718. return 0;
  1719. }
  1720. /* test activation of device */
  1721. static void __init of_unittest_overlay_0(void)
  1722. {
  1723. int ret;
  1724. EXPECT_BEGIN(KERN_INFO,
  1725. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
  1726. /* device should enable */
  1727. ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
  1728. EXPECT_END(KERN_INFO,
  1729. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
  1730. if (ret)
  1731. return;
  1732. unittest(1, "overlay test %d passed\n", 0);
  1733. }
  1734. /* test deactivation of device */
  1735. static void __init of_unittest_overlay_1(void)
  1736. {
  1737. int ret;
  1738. EXPECT_BEGIN(KERN_INFO,
  1739. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
  1740. /* device should disable */
  1741. ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
  1742. EXPECT_END(KERN_INFO,
  1743. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
  1744. if (ret)
  1745. return;
  1746. unittest(1, "overlay test %d passed\n", 1);
  1747. }
  1748. /* test activation of device */
  1749. static void __init of_unittest_overlay_2(void)
  1750. {
  1751. int ret;
  1752. EXPECT_BEGIN(KERN_INFO,
  1753. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
  1754. /* device should enable */
  1755. ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
  1756. EXPECT_END(KERN_INFO,
  1757. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
  1758. if (ret)
  1759. return;
  1760. unittest(1, "overlay test %d passed\n", 2);
  1761. }
  1762. /* test deactivation of device */
  1763. static void __init of_unittest_overlay_3(void)
  1764. {
  1765. int ret;
  1766. EXPECT_BEGIN(KERN_INFO,
  1767. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
  1768. /* device should disable */
  1769. ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
  1770. EXPECT_END(KERN_INFO,
  1771. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
  1772. if (ret)
  1773. return;
  1774. unittest(1, "overlay test %d passed\n", 3);
  1775. }
  1776. /* test activation of a full device node */
  1777. static void __init of_unittest_overlay_4(void)
  1778. {
  1779. /* device should disable */
  1780. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  1781. return;
  1782. unittest(1, "overlay test %d passed\n", 4);
  1783. }
  1784. /* test overlay apply/revert sequence */
  1785. static void __init of_unittest_overlay_5(void)
  1786. {
  1787. int ret;
  1788. EXPECT_BEGIN(KERN_INFO,
  1789. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
  1790. /* device should disable */
  1791. ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
  1792. EXPECT_END(KERN_INFO,
  1793. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
  1794. if (ret)
  1795. return;
  1796. unittest(1, "overlay test %d passed\n", 5);
  1797. }
  1798. /* test overlay application in sequence */
  1799. static void __init of_unittest_overlay_6(void)
  1800. {
  1801. int i, ov_id[2], ovcs_id;
  1802. int overlay_nr = 6, unittest_nr = 6;
  1803. int before = 0, after = 1;
  1804. const char *overlay_name;
  1805. int ret;
  1806. /* unittest device must be in before state */
  1807. for (i = 0; i < 2; i++) {
  1808. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1809. != before) {
  1810. unittest(0, "%s with device @\"%s\" %s\n",
  1811. overlay_name_from_nr(overlay_nr + i),
  1812. unittest_path(unittest_nr + i,
  1813. PDEV_OVERLAY),
  1814. !before ? "enabled" : "disabled");
  1815. return;
  1816. }
  1817. }
  1818. /* apply the overlays */
  1819. EXPECT_BEGIN(KERN_INFO,
  1820. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
  1821. overlay_name = overlay_name_from_nr(overlay_nr + 0);
  1822. ret = overlay_data_apply(overlay_name, &ovcs_id);
  1823. if (!ret) {
  1824. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  1825. return;
  1826. }
  1827. ov_id[0] = ovcs_id;
  1828. of_unittest_track_overlay(ov_id[0]);
  1829. EXPECT_END(KERN_INFO,
  1830. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
  1831. EXPECT_BEGIN(KERN_INFO,
  1832. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
  1833. overlay_name = overlay_name_from_nr(overlay_nr + 1);
  1834. ret = overlay_data_apply(overlay_name, &ovcs_id);
  1835. if (!ret) {
  1836. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  1837. return;
  1838. }
  1839. ov_id[1] = ovcs_id;
  1840. of_unittest_track_overlay(ov_id[1]);
  1841. EXPECT_END(KERN_INFO,
  1842. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
  1843. for (i = 0; i < 2; i++) {
  1844. /* unittest device must be in after state */
  1845. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1846. != after) {
  1847. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1848. overlay_name_from_nr(overlay_nr + i),
  1849. unittest_path(unittest_nr + i,
  1850. PDEV_OVERLAY),
  1851. !after ? "enabled" : "disabled");
  1852. return;
  1853. }
  1854. }
  1855. for (i = 1; i >= 0; i--) {
  1856. ovcs_id = ov_id[i];
  1857. if (of_overlay_remove(&ovcs_id)) {
  1858. unittest(0, "%s failed destroy @\"%s\"\n",
  1859. overlay_name_from_nr(overlay_nr + i),
  1860. unittest_path(unittest_nr + i,
  1861. PDEV_OVERLAY));
  1862. return;
  1863. }
  1864. of_unittest_untrack_overlay(ov_id[i]);
  1865. }
  1866. for (i = 0; i < 2; i++) {
  1867. /* unittest device must be again in before state */
  1868. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1869. != before) {
  1870. unittest(0, "%s with device @\"%s\" %s\n",
  1871. overlay_name_from_nr(overlay_nr + i),
  1872. unittest_path(unittest_nr + i,
  1873. PDEV_OVERLAY),
  1874. !before ? "enabled" : "disabled");
  1875. return;
  1876. }
  1877. }
  1878. unittest(1, "overlay test %d passed\n", 6);
  1879. }
  1880. /* test overlay application in sequence */
  1881. static void __init of_unittest_overlay_8(void)
  1882. {
  1883. int i, ov_id[2], ovcs_id;
  1884. int overlay_nr = 8, unittest_nr = 8;
  1885. const char *overlay_name;
  1886. int ret;
  1887. /* we don't care about device state in this test */
  1888. EXPECT_BEGIN(KERN_INFO,
  1889. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
  1890. overlay_name = overlay_name_from_nr(overlay_nr + 0);
  1891. ret = overlay_data_apply(overlay_name, &ovcs_id);
  1892. if (!ret)
  1893. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  1894. EXPECT_END(KERN_INFO,
  1895. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
  1896. if (!ret)
  1897. return;
  1898. ov_id[0] = ovcs_id;
  1899. of_unittest_track_overlay(ov_id[0]);
  1900. overlay_name = overlay_name_from_nr(overlay_nr + 1);
  1901. EXPECT_BEGIN(KERN_INFO,
  1902. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
  1903. /* apply the overlays */
  1904. ret = overlay_data_apply(overlay_name, &ovcs_id);
  1905. EXPECT_END(KERN_INFO,
  1906. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
  1907. if (!ret) {
  1908. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  1909. return;
  1910. }
  1911. ov_id[1] = ovcs_id;
  1912. of_unittest_track_overlay(ov_id[1]);
  1913. /* now try to remove first overlay (it should fail) */
  1914. ovcs_id = ov_id[0];
  1915. EXPECT_BEGIN(KERN_INFO,
  1916. "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
  1917. EXPECT_BEGIN(KERN_INFO,
  1918. "OF: overlay: overlay #6 is not topmost");
  1919. ret = of_overlay_remove(&ovcs_id);
  1920. EXPECT_END(KERN_INFO,
  1921. "OF: overlay: overlay #6 is not topmost");
  1922. EXPECT_END(KERN_INFO,
  1923. "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
  1924. if (!ret) {
  1925. unittest(0, "%s was destroyed @\"%s\"\n",
  1926. overlay_name_from_nr(overlay_nr + 0),
  1927. unittest_path(unittest_nr,
  1928. PDEV_OVERLAY));
  1929. return;
  1930. }
  1931. /* removing them in order should work */
  1932. for (i = 1; i >= 0; i--) {
  1933. ovcs_id = ov_id[i];
  1934. if (of_overlay_remove(&ovcs_id)) {
  1935. unittest(0, "%s not destroyed @\"%s\"\n",
  1936. overlay_name_from_nr(overlay_nr + i),
  1937. unittest_path(unittest_nr,
  1938. PDEV_OVERLAY));
  1939. return;
  1940. }
  1941. of_unittest_untrack_overlay(ov_id[i]);
  1942. }
  1943. unittest(1, "overlay test %d passed\n", 8);
  1944. }
  1945. /* test insertion of a bus with parent devices */
  1946. static void __init of_unittest_overlay_10(void)
  1947. {
  1948. int ret;
  1949. char *child_path;
  1950. /* device should disable */
  1951. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1952. if (unittest(ret == 0,
  1953. "overlay test %d failed; overlay application\n", 10))
  1954. return;
  1955. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1956. unittest_path(10, PDEV_OVERLAY));
  1957. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1958. return;
  1959. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1960. kfree(child_path);
  1961. unittest(ret, "overlay test %d failed; no child device\n", 10);
  1962. }
  1963. /* test insertion of a bus with parent devices (and revert) */
  1964. static void __init of_unittest_overlay_11(void)
  1965. {
  1966. int ret;
  1967. /* device should disable */
  1968. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1969. PDEV_OVERLAY);
  1970. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  1971. }
  1972. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1973. struct unittest_i2c_bus_data {
  1974. struct platform_device *pdev;
  1975. struct i2c_adapter adap;
  1976. };
  1977. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1978. struct i2c_msg *msgs, int num)
  1979. {
  1980. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1981. (void)std;
  1982. return num;
  1983. }
  1984. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1985. {
  1986. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1987. }
  1988. static const struct i2c_algorithm unittest_i2c_algo = {
  1989. .master_xfer = unittest_i2c_master_xfer,
  1990. .functionality = unittest_i2c_functionality,
  1991. };
  1992. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1993. {
  1994. struct device *dev = &pdev->dev;
  1995. struct device_node *np = dev->of_node;
  1996. struct unittest_i2c_bus_data *std;
  1997. struct i2c_adapter *adap;
  1998. int ret;
  1999. if (np == NULL) {
  2000. dev_err(dev, "No OF data for device\n");
  2001. return -EINVAL;
  2002. }
  2003. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2004. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  2005. if (!std)
  2006. return -ENOMEM;
  2007. /* link them together */
  2008. std->pdev = pdev;
  2009. platform_set_drvdata(pdev, std);
  2010. adap = &std->adap;
  2011. i2c_set_adapdata(adap, std);
  2012. adap->nr = -1;
  2013. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  2014. adap->class = I2C_CLASS_DEPRECATED;
  2015. adap->algo = &unittest_i2c_algo;
  2016. adap->dev.parent = dev;
  2017. adap->dev.of_node = dev->of_node;
  2018. adap->timeout = 5 * HZ;
  2019. adap->retries = 3;
  2020. ret = i2c_add_numbered_adapter(adap);
  2021. if (ret != 0) {
  2022. dev_err(dev, "Failed to add I2C adapter\n");
  2023. return ret;
  2024. }
  2025. return 0;
  2026. }
  2027. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  2028. {
  2029. struct device *dev = &pdev->dev;
  2030. struct device_node *np = dev->of_node;
  2031. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  2032. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2033. i2c_del_adapter(&std->adap);
  2034. return 0;
  2035. }
  2036. static const struct of_device_id unittest_i2c_bus_match[] = {
  2037. { .compatible = "unittest-i2c-bus", },
  2038. {},
  2039. };
  2040. static struct platform_driver unittest_i2c_bus_driver = {
  2041. .probe = unittest_i2c_bus_probe,
  2042. .remove = unittest_i2c_bus_remove,
  2043. .driver = {
  2044. .name = "unittest-i2c-bus",
  2045. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  2046. },
  2047. };
  2048. static int unittest_i2c_dev_probe(struct i2c_client *client,
  2049. const struct i2c_device_id *id)
  2050. {
  2051. struct device *dev = &client->dev;
  2052. struct device_node *np = client->dev.of_node;
  2053. if (!np) {
  2054. dev_err(dev, "No OF node\n");
  2055. return -EINVAL;
  2056. }
  2057. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2058. return 0;
  2059. };
  2060. static int unittest_i2c_dev_remove(struct i2c_client *client)
  2061. {
  2062. struct device *dev = &client->dev;
  2063. struct device_node *np = client->dev.of_node;
  2064. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2065. return 0;
  2066. }
  2067. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  2068. { .name = "unittest-i2c-dev" },
  2069. { }
  2070. };
  2071. static struct i2c_driver unittest_i2c_dev_driver = {
  2072. .driver = {
  2073. .name = "unittest-i2c-dev",
  2074. },
  2075. .probe = unittest_i2c_dev_probe,
  2076. .remove = unittest_i2c_dev_remove,
  2077. .id_table = unittest_i2c_dev_id,
  2078. };
  2079. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2080. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  2081. {
  2082. return 0;
  2083. }
  2084. static int unittest_i2c_mux_probe(struct i2c_client *client,
  2085. const struct i2c_device_id *id)
  2086. {
  2087. int i, nchans;
  2088. struct device *dev = &client->dev;
  2089. struct i2c_adapter *adap = client->adapter;
  2090. struct device_node *np = client->dev.of_node, *child;
  2091. struct i2c_mux_core *muxc;
  2092. u32 reg, max_reg;
  2093. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2094. if (!np) {
  2095. dev_err(dev, "No OF node\n");
  2096. return -EINVAL;
  2097. }
  2098. max_reg = (u32)-1;
  2099. for_each_child_of_node(np, child) {
  2100. if (of_property_read_u32(child, "reg", &reg))
  2101. continue;
  2102. if (max_reg == (u32)-1 || reg > max_reg)
  2103. max_reg = reg;
  2104. }
  2105. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  2106. if (nchans == 0) {
  2107. dev_err(dev, "No channels\n");
  2108. return -EINVAL;
  2109. }
  2110. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  2111. unittest_i2c_mux_select_chan, NULL);
  2112. if (!muxc)
  2113. return -ENOMEM;
  2114. for (i = 0; i < nchans; i++) {
  2115. if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
  2116. dev_err(dev, "Failed to register mux #%d\n", i);
  2117. i2c_mux_del_adapters(muxc);
  2118. return -ENODEV;
  2119. }
  2120. }
  2121. i2c_set_clientdata(client, muxc);
  2122. return 0;
  2123. };
  2124. static int unittest_i2c_mux_remove(struct i2c_client *client)
  2125. {
  2126. struct device *dev = &client->dev;
  2127. struct device_node *np = client->dev.of_node;
  2128. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  2129. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2130. i2c_mux_del_adapters(muxc);
  2131. return 0;
  2132. }
  2133. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  2134. { .name = "unittest-i2c-mux" },
  2135. { }
  2136. };
  2137. static struct i2c_driver unittest_i2c_mux_driver = {
  2138. .driver = {
  2139. .name = "unittest-i2c-mux",
  2140. },
  2141. .probe = unittest_i2c_mux_probe,
  2142. .remove = unittest_i2c_mux_remove,
  2143. .id_table = unittest_i2c_mux_id,
  2144. };
  2145. #endif
  2146. static int of_unittest_overlay_i2c_init(void)
  2147. {
  2148. int ret;
  2149. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  2150. if (unittest(ret == 0,
  2151. "could not register unittest i2c device driver\n"))
  2152. return ret;
  2153. ret = platform_driver_register(&unittest_i2c_bus_driver);
  2154. if (unittest(ret == 0,
  2155. "could not register unittest i2c bus driver\n"))
  2156. return ret;
  2157. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2158. EXPECT_BEGIN(KERN_INFO,
  2159. "i2c i2c-1: Added multiplexed i2c bus 2");
  2160. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  2161. EXPECT_END(KERN_INFO,
  2162. "i2c i2c-1: Added multiplexed i2c bus 2");
  2163. if (unittest(ret == 0,
  2164. "could not register unittest i2c mux driver\n"))
  2165. return ret;
  2166. #endif
  2167. return 0;
  2168. }
  2169. static void of_unittest_overlay_i2c_cleanup(void)
  2170. {
  2171. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2172. i2c_del_driver(&unittest_i2c_mux_driver);
  2173. #endif
  2174. platform_driver_unregister(&unittest_i2c_bus_driver);
  2175. i2c_del_driver(&unittest_i2c_dev_driver);
  2176. }
  2177. static void __init of_unittest_overlay_i2c_12(void)
  2178. {
  2179. int ret;
  2180. /* device should enable */
  2181. EXPECT_BEGIN(KERN_INFO,
  2182. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
  2183. ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
  2184. EXPECT_END(KERN_INFO,
  2185. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
  2186. if (ret)
  2187. return;
  2188. unittest(1, "overlay test %d passed\n", 12);
  2189. }
  2190. /* test deactivation of device */
  2191. static void __init of_unittest_overlay_i2c_13(void)
  2192. {
  2193. int ret;
  2194. EXPECT_BEGIN(KERN_INFO,
  2195. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
  2196. /* device should disable */
  2197. ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
  2198. EXPECT_END(KERN_INFO,
  2199. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
  2200. if (ret)
  2201. return;
  2202. unittest(1, "overlay test %d passed\n", 13);
  2203. }
  2204. /* just check for i2c mux existence */
  2205. static void of_unittest_overlay_i2c_14(void)
  2206. {
  2207. }
  2208. static void __init of_unittest_overlay_i2c_15(void)
  2209. {
  2210. int ret;
  2211. /* device should enable */
  2212. EXPECT_BEGIN(KERN_INFO,
  2213. "i2c i2c-1: Added multiplexed i2c bus 3");
  2214. ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
  2215. EXPECT_END(KERN_INFO,
  2216. "i2c i2c-1: Added multiplexed i2c bus 3");
  2217. if (ret)
  2218. return;
  2219. unittest(1, "overlay test %d passed\n", 15);
  2220. }
  2221. #else
  2222. static inline void of_unittest_overlay_i2c_14(void) { }
  2223. static inline void of_unittest_overlay_i2c_15(void) { }
  2224. #endif
  2225. static void __init of_unittest_overlay(void)
  2226. {
  2227. struct device_node *bus_np = NULL;
  2228. if (platform_driver_register(&unittest_driver)) {
  2229. unittest(0, "could not register unittest driver\n");
  2230. goto out;
  2231. }
  2232. bus_np = of_find_node_by_path(bus_path);
  2233. if (bus_np == NULL) {
  2234. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  2235. goto out;
  2236. }
  2237. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  2238. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  2239. goto out;
  2240. }
  2241. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  2242. unittest(0, "could not find unittest0 @ \"%s\"\n",
  2243. unittest_path(100, PDEV_OVERLAY));
  2244. goto out;
  2245. }
  2246. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  2247. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  2248. unittest_path(101, PDEV_OVERLAY));
  2249. goto out;
  2250. }
  2251. unittest(1, "basic infrastructure of overlays passed");
  2252. /* tests in sequence */
  2253. of_unittest_overlay_0();
  2254. of_unittest_overlay_1();
  2255. of_unittest_overlay_2();
  2256. of_unittest_overlay_3();
  2257. of_unittest_overlay_4();
  2258. of_unittest_overlay_5();
  2259. of_unittest_overlay_6();
  2260. of_unittest_overlay_8();
  2261. of_unittest_overlay_10();
  2262. of_unittest_overlay_11();
  2263. #if IS_BUILTIN(CONFIG_I2C)
  2264. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  2265. goto out;
  2266. of_unittest_overlay_i2c_12();
  2267. of_unittest_overlay_i2c_13();
  2268. of_unittest_overlay_i2c_14();
  2269. of_unittest_overlay_i2c_15();
  2270. of_unittest_overlay_i2c_cleanup();
  2271. #endif
  2272. of_unittest_overlay_gpio();
  2273. of_unittest_destroy_tracked_overlays();
  2274. out:
  2275. of_node_put(bus_np);
  2276. }
  2277. #else
  2278. static inline void __init of_unittest_overlay(void) { }
  2279. #endif
  2280. #ifdef CONFIG_OF_OVERLAY
  2281. /*
  2282. * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
  2283. * in scripts/Makefile.lib
  2284. */
  2285. #define OVERLAY_INFO_EXTERN(name) \
  2286. extern uint8_t __dtb_##name##_begin[]; \
  2287. extern uint8_t __dtb_##name##_end[]
  2288. #define OVERLAY_INFO(overlay_name, expected) \
  2289. { .dtb_begin = __dtb_##overlay_name##_begin, \
  2290. .dtb_end = __dtb_##overlay_name##_end, \
  2291. .expected_result = expected, \
  2292. .name = #overlay_name, \
  2293. }
  2294. struct overlay_info {
  2295. uint8_t *dtb_begin;
  2296. uint8_t *dtb_end;
  2297. int expected_result;
  2298. int overlay_id;
  2299. char *name;
  2300. };
  2301. OVERLAY_INFO_EXTERN(overlay_base);
  2302. OVERLAY_INFO_EXTERN(overlay);
  2303. OVERLAY_INFO_EXTERN(overlay_0);
  2304. OVERLAY_INFO_EXTERN(overlay_1);
  2305. OVERLAY_INFO_EXTERN(overlay_2);
  2306. OVERLAY_INFO_EXTERN(overlay_3);
  2307. OVERLAY_INFO_EXTERN(overlay_4);
  2308. OVERLAY_INFO_EXTERN(overlay_5);
  2309. OVERLAY_INFO_EXTERN(overlay_6);
  2310. OVERLAY_INFO_EXTERN(overlay_7);
  2311. OVERLAY_INFO_EXTERN(overlay_8);
  2312. OVERLAY_INFO_EXTERN(overlay_9);
  2313. OVERLAY_INFO_EXTERN(overlay_10);
  2314. OVERLAY_INFO_EXTERN(overlay_11);
  2315. OVERLAY_INFO_EXTERN(overlay_12);
  2316. OVERLAY_INFO_EXTERN(overlay_13);
  2317. OVERLAY_INFO_EXTERN(overlay_15);
  2318. OVERLAY_INFO_EXTERN(overlay_gpio_01);
  2319. OVERLAY_INFO_EXTERN(overlay_gpio_02a);
  2320. OVERLAY_INFO_EXTERN(overlay_gpio_02b);
  2321. OVERLAY_INFO_EXTERN(overlay_gpio_03);
  2322. OVERLAY_INFO_EXTERN(overlay_gpio_04a);
  2323. OVERLAY_INFO_EXTERN(overlay_gpio_04b);
  2324. OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
  2325. OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
  2326. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  2327. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  2328. /* entries found by name */
  2329. static struct overlay_info overlays[] = {
  2330. OVERLAY_INFO(overlay_base, -9999),
  2331. OVERLAY_INFO(overlay, 0),
  2332. OVERLAY_INFO(overlay_0, 0),
  2333. OVERLAY_INFO(overlay_1, 0),
  2334. OVERLAY_INFO(overlay_2, 0),
  2335. OVERLAY_INFO(overlay_3, 0),
  2336. OVERLAY_INFO(overlay_4, 0),
  2337. OVERLAY_INFO(overlay_5, 0),
  2338. OVERLAY_INFO(overlay_6, 0),
  2339. OVERLAY_INFO(overlay_7, 0),
  2340. OVERLAY_INFO(overlay_8, 0),
  2341. OVERLAY_INFO(overlay_9, 0),
  2342. OVERLAY_INFO(overlay_10, 0),
  2343. OVERLAY_INFO(overlay_11, 0),
  2344. OVERLAY_INFO(overlay_12, 0),
  2345. OVERLAY_INFO(overlay_13, 0),
  2346. OVERLAY_INFO(overlay_15, 0),
  2347. OVERLAY_INFO(overlay_gpio_01, 0),
  2348. OVERLAY_INFO(overlay_gpio_02a, 0),
  2349. OVERLAY_INFO(overlay_gpio_02b, 0),
  2350. OVERLAY_INFO(overlay_gpio_03, 0),
  2351. OVERLAY_INFO(overlay_gpio_04a, 0),
  2352. OVERLAY_INFO(overlay_gpio_04b, 0),
  2353. OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
  2354. OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
  2355. OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
  2356. OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
  2357. /* end marker */
  2358. {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
  2359. };
  2360. static struct device_node *overlay_base_root;
  2361. static void * __init dt_alloc_memory(u64 size, u64 align)
  2362. {
  2363. void *ptr = memblock_alloc(size, align);
  2364. if (!ptr)
  2365. panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
  2366. __func__, size, align);
  2367. return ptr;
  2368. }
  2369. /*
  2370. * Create base device tree for the overlay unittest.
  2371. *
  2372. * This is called from very early boot code.
  2373. *
  2374. * Do as much as possible the same way as done in __unflatten_device_tree
  2375. * and other early boot steps for the normal FDT so that the overlay base
  2376. * unflattened tree will have the same characteristics as the real tree
  2377. * (such as having memory allocated by the early allocator). The goal
  2378. * is to test "the real thing" as much as possible, and test "test setup
  2379. * code" as little as possible.
  2380. *
  2381. * Have to stop before resolving phandles, because that uses kmalloc.
  2382. */
  2383. void __init unittest_unflatten_overlay_base(void)
  2384. {
  2385. struct overlay_info *info;
  2386. u32 data_size;
  2387. void *new_fdt;
  2388. u32 size;
  2389. int found = 0;
  2390. const char *overlay_name = "overlay_base";
  2391. for (info = overlays; info && info->name; info++) {
  2392. if (!strcmp(overlay_name, info->name)) {
  2393. found = 1;
  2394. break;
  2395. }
  2396. }
  2397. if (!found) {
  2398. pr_err("no overlay data for %s\n", overlay_name);
  2399. return;
  2400. }
  2401. info = &overlays[0];
  2402. if (info->expected_result != -9999) {
  2403. pr_err("No dtb 'overlay_base' to attach\n");
  2404. return;
  2405. }
  2406. data_size = info->dtb_end - info->dtb_begin;
  2407. if (!data_size) {
  2408. pr_err("No dtb 'overlay_base' to attach\n");
  2409. return;
  2410. }
  2411. size = fdt_totalsize(info->dtb_begin);
  2412. if (size != data_size) {
  2413. pr_err("dtb 'overlay_base' header totalsize != actual size");
  2414. return;
  2415. }
  2416. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  2417. if (!new_fdt) {
  2418. pr_err("alloc for dtb 'overlay_base' failed");
  2419. return;
  2420. }
  2421. memcpy(new_fdt, info->dtb_begin, size);
  2422. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  2423. dt_alloc_memory, true);
  2424. }
  2425. /*
  2426. * The purpose of of_unittest_overlay_data_add is to add an
  2427. * overlay in the normal fashion. This is a test of the whole
  2428. * picture, instead of testing individual elements.
  2429. *
  2430. * A secondary purpose is to be able to verify that the contents of
  2431. * /proc/device-tree/ contains the updated structure and values from
  2432. * the overlay. That must be verified separately in user space.
  2433. *
  2434. * Return 0 on unexpected error.
  2435. */
  2436. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
  2437. {
  2438. struct overlay_info *info;
  2439. int found = 0;
  2440. int ret;
  2441. u32 size;
  2442. for (info = overlays; info && info->name; info++) {
  2443. if (!strcmp(overlay_name, info->name)) {
  2444. found = 1;
  2445. break;
  2446. }
  2447. }
  2448. if (!found) {
  2449. pr_err("no overlay data for %s\n", overlay_name);
  2450. return 0;
  2451. }
  2452. size = info->dtb_end - info->dtb_begin;
  2453. if (!size)
  2454. pr_err("no overlay data for %s\n", overlay_name);
  2455. ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
  2456. if (overlay_id)
  2457. *overlay_id = info->overlay_id;
  2458. if (ret < 0)
  2459. goto out;
  2460. pr_debug("%s applied\n", overlay_name);
  2461. out:
  2462. if (ret != info->expected_result)
  2463. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  2464. info->expected_result, ret, overlay_name);
  2465. return (ret == info->expected_result);
  2466. }
  2467. /*
  2468. * The purpose of of_unittest_overlay_high_level is to add an overlay
  2469. * in the normal fashion. This is a test of the whole picture,
  2470. * instead of individual elements.
  2471. *
  2472. * The first part of the function is _not_ normal overlay usage; it is
  2473. * finishing splicing the base overlay device tree into the live tree.
  2474. */
  2475. static __init void of_unittest_overlay_high_level(void)
  2476. {
  2477. struct device_node *last_sibling;
  2478. struct device_node *np;
  2479. struct device_node *of_symbols;
  2480. struct device_node *overlay_base_symbols;
  2481. struct device_node **pprev;
  2482. struct property *prop;
  2483. int ret;
  2484. if (!overlay_base_root) {
  2485. unittest(0, "overlay_base_root not initialized\n");
  2486. return;
  2487. }
  2488. /*
  2489. * Could not fixup phandles in unittest_unflatten_overlay_base()
  2490. * because kmalloc() was not yet available.
  2491. */
  2492. of_overlay_mutex_lock();
  2493. of_resolve_phandles(overlay_base_root);
  2494. of_overlay_mutex_unlock();
  2495. /*
  2496. * do not allow overlay_base to duplicate any node already in
  2497. * tree, this greatly simplifies the code
  2498. */
  2499. /*
  2500. * remove overlay_base_root node "__local_fixups", after
  2501. * being used by of_resolve_phandles()
  2502. */
  2503. pprev = &overlay_base_root->child;
  2504. for (np = overlay_base_root->child; np; np = np->sibling) {
  2505. if (of_node_name_eq(np, "__local_fixups__")) {
  2506. *pprev = np->sibling;
  2507. break;
  2508. }
  2509. pprev = &np->sibling;
  2510. }
  2511. /* remove overlay_base_root node "__symbols__" if in live tree */
  2512. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  2513. if (of_symbols) {
  2514. /* will have to graft properties from node into live tree */
  2515. pprev = &overlay_base_root->child;
  2516. for (np = overlay_base_root->child; np; np = np->sibling) {
  2517. if (of_node_name_eq(np, "__symbols__")) {
  2518. overlay_base_symbols = np;
  2519. *pprev = np->sibling;
  2520. break;
  2521. }
  2522. pprev = &np->sibling;
  2523. }
  2524. }
  2525. for_each_child_of_node(overlay_base_root, np) {
  2526. struct device_node *base_child;
  2527. for_each_child_of_node(of_root, base_child) {
  2528. if (!strcmp(np->full_name, base_child->full_name)) {
  2529. unittest(0, "illegal node name in overlay_base %pOFn",
  2530. np);
  2531. return;
  2532. }
  2533. }
  2534. }
  2535. /*
  2536. * overlay 'overlay_base' is not allowed to have root
  2537. * properties, so only need to splice nodes into main device tree.
  2538. *
  2539. * root node of *overlay_base_root will not be freed, it is lost
  2540. * memory.
  2541. */
  2542. for (np = overlay_base_root->child; np; np = np->sibling)
  2543. np->parent = of_root;
  2544. mutex_lock(&of_mutex);
  2545. for (last_sibling = np = of_root->child; np; np = np->sibling)
  2546. last_sibling = np;
  2547. if (last_sibling)
  2548. last_sibling->sibling = overlay_base_root->child;
  2549. else
  2550. of_root->child = overlay_base_root->child;
  2551. for_each_of_allnodes_from(overlay_base_root, np)
  2552. __of_attach_node_sysfs(np);
  2553. if (of_symbols) {
  2554. struct property *new_prop;
  2555. for_each_property_of_node(overlay_base_symbols, prop) {
  2556. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  2557. if (!new_prop) {
  2558. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  2559. prop->name);
  2560. goto err_unlock;
  2561. }
  2562. if (__of_add_property(of_symbols, new_prop)) {
  2563. kfree(new_prop->name);
  2564. kfree(new_prop->value);
  2565. kfree(new_prop);
  2566. /* "name" auto-generated by unflatten */
  2567. if (!strcmp(prop->name, "name"))
  2568. continue;
  2569. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  2570. prop->name);
  2571. goto err_unlock;
  2572. }
  2573. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  2574. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  2575. prop->name);
  2576. goto err_unlock;
  2577. }
  2578. }
  2579. }
  2580. mutex_unlock(&of_mutex);
  2581. /* now do the normal overlay usage test */
  2582. EXPECT_BEGIN(KERN_ERR,
  2583. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
  2584. EXPECT_BEGIN(KERN_ERR,
  2585. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
  2586. EXPECT_BEGIN(KERN_ERR,
  2587. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
  2588. EXPECT_BEGIN(KERN_ERR,
  2589. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
  2590. EXPECT_BEGIN(KERN_ERR,
  2591. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
  2592. EXPECT_BEGIN(KERN_ERR,
  2593. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
  2594. EXPECT_BEGIN(KERN_ERR,
  2595. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
  2596. EXPECT_BEGIN(KERN_ERR,
  2597. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
  2598. EXPECT_BEGIN(KERN_ERR,
  2599. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
  2600. EXPECT_BEGIN(KERN_ERR,
  2601. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
  2602. EXPECT_BEGIN(KERN_ERR,
  2603. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
  2604. ret = overlay_data_apply("overlay", NULL);
  2605. EXPECT_END(KERN_ERR,
  2606. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
  2607. EXPECT_END(KERN_ERR,
  2608. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
  2609. EXPECT_END(KERN_ERR,
  2610. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
  2611. EXPECT_END(KERN_ERR,
  2612. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
  2613. EXPECT_END(KERN_ERR,
  2614. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
  2615. EXPECT_END(KERN_ERR,
  2616. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
  2617. EXPECT_END(KERN_ERR,
  2618. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
  2619. EXPECT_END(KERN_ERR,
  2620. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
  2621. EXPECT_END(KERN_ERR,
  2622. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
  2623. EXPECT_END(KERN_ERR,
  2624. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
  2625. EXPECT_END(KERN_ERR,
  2626. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
  2627. unittest(ret, "Adding overlay 'overlay' failed\n");
  2628. EXPECT_BEGIN(KERN_ERR,
  2629. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
  2630. EXPECT_BEGIN(KERN_ERR,
  2631. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
  2632. unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
  2633. "Adding overlay 'overlay_bad_add_dup_node' failed\n");
  2634. EXPECT_END(KERN_ERR,
  2635. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
  2636. EXPECT_END(KERN_ERR,
  2637. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
  2638. EXPECT_BEGIN(KERN_ERR,
  2639. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
  2640. EXPECT_BEGIN(KERN_ERR,
  2641. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
  2642. EXPECT_BEGIN(KERN_ERR,
  2643. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
  2644. unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
  2645. "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
  2646. EXPECT_END(KERN_ERR,
  2647. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
  2648. EXPECT_END(KERN_ERR,
  2649. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
  2650. EXPECT_END(KERN_ERR,
  2651. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
  2652. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  2653. "Adding overlay 'overlay_bad_phandle' failed\n");
  2654. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  2655. "Adding overlay 'overlay_bad_symbol' failed\n");
  2656. return;
  2657. err_unlock:
  2658. mutex_unlock(&of_mutex);
  2659. }
  2660. #else
  2661. static inline __init void of_unittest_overlay_high_level(void) {}
  2662. #endif
  2663. static int __init of_unittest(void)
  2664. {
  2665. struct device_node *np;
  2666. int res;
  2667. pr_info("start of unittest - you will see error messages\n");
  2668. /* adding data for unittest */
  2669. if (IS_ENABLED(CONFIG_UML))
  2670. unittest_unflatten_overlay_base();
  2671. res = unittest_data_add();
  2672. if (res)
  2673. return res;
  2674. if (!of_aliases)
  2675. of_aliases = of_find_node_by_path("/aliases");
  2676. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  2677. if (!np) {
  2678. pr_info("No testcase data in device tree; not running tests\n");
  2679. return 0;
  2680. }
  2681. of_node_put(np);
  2682. of_unittest_check_tree_linkage();
  2683. of_unittest_check_phandles();
  2684. of_unittest_find_node_by_name();
  2685. of_unittest_dynamic();
  2686. of_unittest_parse_phandle_with_args();
  2687. of_unittest_parse_phandle_with_args_map();
  2688. of_unittest_printf();
  2689. of_unittest_property_string();
  2690. of_unittest_property_copy();
  2691. of_unittest_changeset();
  2692. of_unittest_parse_interrupts();
  2693. of_unittest_parse_interrupts_extended();
  2694. of_unittest_dma_get_max_cpu_address();
  2695. of_unittest_parse_dma_ranges();
  2696. of_unittest_pci_dma_ranges();
  2697. of_unittest_match_node();
  2698. of_unittest_platform_populate();
  2699. of_unittest_overlay();
  2700. /* Double check linkage after removing testcase data */
  2701. of_unittest_check_tree_linkage();
  2702. of_unittest_overlay_high_level();
  2703. pr_info("end of unittest - %i passed, %i failed\n",
  2704. unittest_results.passed, unittest_results.failed);
  2705. return 0;
  2706. }
  2707. late_initcall(of_unittest);