unittest.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539
  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/pci.h>
  24. #include <linux/kernel.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-mux.h>
  27. #include <linux/gpio/driver.h>
  28. #include <linux/bitops.h>
  29. #include "of_private.h"
  30. static struct unittest_results {
  31. int passed;
  32. int failed;
  33. } unittest_results;
  34. #define unittest(result, fmt, ...) ({ \
  35. bool failed = !(result); \
  36. if (failed) { \
  37. unittest_results.failed++; \
  38. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  39. } else { \
  40. unittest_results.passed++; \
  41. pr_info("pass %s():%i\n", __func__, __LINE__); \
  42. } \
  43. failed; \
  44. })
  45. #ifdef CONFIG_OF_KOBJ
  46. #define OF_KREF_READ(NODE) kref_read(&(NODE)->kobj.kref)
  47. #else
  48. #define OF_KREF_READ(NODE) 1
  49. #endif
  50. /*
  51. * Expected message may have a message level other than KERN_INFO.
  52. * Print the expected message only if the current loglevel will allow
  53. * the actual message to print.
  54. *
  55. * Do not use EXPECT_BEGIN(), EXPECT_END(), EXPECT_NOT_BEGIN(), or
  56. * EXPECT_NOT_END() to report messages expected to be reported or not
  57. * reported by pr_debug().
  58. */
  59. #define EXPECT_BEGIN(level, fmt, ...) \
  60. printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
  61. #define EXPECT_END(level, fmt, ...) \
  62. printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
  63. #define EXPECT_NOT_BEGIN(level, fmt, ...) \
  64. printk(level pr_fmt("EXPECT_NOT \\ : ") fmt, ##__VA_ARGS__)
  65. #define EXPECT_NOT_END(level, fmt, ...) \
  66. printk(level pr_fmt("EXPECT_NOT / : ") fmt, ##__VA_ARGS__)
  67. static void __init of_unittest_find_node_by_name(void)
  68. {
  69. struct device_node *np;
  70. const char *options, *name;
  71. np = of_find_node_by_path("/testcase-data");
  72. name = kasprintf(GFP_KERNEL, "%pOF", np);
  73. unittest(np && name && !strcmp("/testcase-data", name),
  74. "find /testcase-data failed\n");
  75. of_node_put(np);
  76. kfree(name);
  77. /* Test if trailing '/' works */
  78. np = of_find_node_by_path("/testcase-data/");
  79. unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
  80. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  81. name = kasprintf(GFP_KERNEL, "%pOF", np);
  82. unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  83. "find /testcase-data/phandle-tests/consumer-a failed\n");
  84. of_node_put(np);
  85. kfree(name);
  86. np = of_find_node_by_path("testcase-alias");
  87. name = kasprintf(GFP_KERNEL, "%pOF", np);
  88. unittest(np && name && !strcmp("/testcase-data", name),
  89. "find testcase-alias failed\n");
  90. of_node_put(np);
  91. kfree(name);
  92. /* Test if trailing '/' works on aliases */
  93. np = of_find_node_by_path("testcase-alias/");
  94. unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
  95. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  96. name = kasprintf(GFP_KERNEL, "%pOF", np);
  97. unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  98. "find testcase-alias/phandle-tests/consumer-a failed\n");
  99. of_node_put(np);
  100. kfree(name);
  101. np = of_find_node_by_path("/testcase-data/missing-path");
  102. unittest(!np, "non-existent path returned node %pOF\n", np);
  103. of_node_put(np);
  104. np = of_find_node_by_path("missing-alias");
  105. unittest(!np, "non-existent alias returned node %pOF\n", np);
  106. of_node_put(np);
  107. np = of_find_node_by_path("testcase-alias/missing-path");
  108. unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
  109. of_node_put(np);
  110. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  111. unittest(np && !strcmp("testoption", options),
  112. "option path test failed\n");
  113. of_node_put(np);
  114. np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
  115. unittest(np && !strcmp("test/option", options),
  116. "option path test, subcase #1 failed\n");
  117. of_node_put(np);
  118. np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
  119. unittest(np && !strcmp("test/option", options),
  120. "option path test, subcase #2 failed\n");
  121. of_node_put(np);
  122. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  123. unittest(np, "NULL option path test failed\n");
  124. of_node_put(np);
  125. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  126. &options);
  127. unittest(np && !strcmp("testaliasoption", options),
  128. "option alias path test failed\n");
  129. of_node_put(np);
  130. np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
  131. &options);
  132. unittest(np && !strcmp("test/alias/option", options),
  133. "option alias path test, subcase #1 failed\n");
  134. of_node_put(np);
  135. np = of_find_node_opts_by_path("testcase-alias/phandle-tests/consumer-a:testaliasoption",
  136. &options);
  137. name = kasprintf(GFP_KERNEL, "%pOF", np);
  138. unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name) &&
  139. !strcmp("testaliasoption", options),
  140. "option alias path test, subcase #2 failed\n");
  141. of_node_put(np);
  142. kfree(name);
  143. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  144. unittest(np, "NULL option alias path test failed\n");
  145. of_node_put(np);
  146. options = "testoption";
  147. np = of_find_node_opts_by_path("testcase-alias", &options);
  148. unittest(np && !options, "option clearing test failed\n");
  149. of_node_put(np);
  150. options = "testoption";
  151. np = of_find_node_opts_by_path("/", &options);
  152. unittest(np && !options, "option clearing root node test failed\n");
  153. of_node_put(np);
  154. }
  155. static void __init of_unittest_dynamic(void)
  156. {
  157. struct device_node *np;
  158. struct property *prop;
  159. np = of_find_node_by_path("/testcase-data");
  160. if (!np) {
  161. pr_err("missing testcase data\n");
  162. return;
  163. }
  164. /* Array of 4 properties for the purpose of testing */
  165. prop = kzalloc_objs(*prop, 4);
  166. if (!prop) {
  167. unittest(0, "kzalloc() failed\n");
  168. return;
  169. }
  170. /* Add a new property - should pass*/
  171. prop->name = "new-property";
  172. prop->value = "new-property-data";
  173. prop->length = strlen(prop->value) + 1;
  174. unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  175. /* Try to add an existing property - should fail */
  176. prop++;
  177. prop->name = "new-property";
  178. prop->value = "new-property-data-should-fail";
  179. prop->length = strlen(prop->value) + 1;
  180. unittest(of_add_property(np, prop) != 0,
  181. "Adding an existing property should have failed\n");
  182. /* Try to modify an existing property - should pass */
  183. prop->value = "modify-property-data-should-pass";
  184. prop->length = strlen(prop->value) + 1;
  185. unittest(of_update_property(np, prop) == 0,
  186. "Updating an existing property should have passed\n");
  187. /* Try to modify non-existent property - should pass*/
  188. prop++;
  189. prop->name = "modify-property";
  190. prop->value = "modify-missing-property-data-should-pass";
  191. prop->length = strlen(prop->value) + 1;
  192. unittest(of_update_property(np, prop) == 0,
  193. "Updating a missing property should have passed\n");
  194. /* Remove property - should pass */
  195. unittest(of_remove_property(np, prop) == 0,
  196. "Removing a property should have passed\n");
  197. /* Adding very large property - should pass */
  198. prop++;
  199. prop->name = "large-property-PAGE_SIZEx8";
  200. prop->length = PAGE_SIZE * 8;
  201. prop->value = kzalloc(prop->length, GFP_KERNEL);
  202. unittest(prop->value != NULL, "Unable to allocate large buffer\n");
  203. if (prop->value)
  204. unittest(of_add_property(np, prop) == 0,
  205. "Adding a large property should have passed\n");
  206. }
  207. static int __init of_unittest_check_node_linkage(struct device_node *np)
  208. {
  209. int count = 0, rc;
  210. for_each_child_of_node_scoped(np, child) {
  211. if (child->parent != np) {
  212. pr_err("Child node %pOFn links to wrong parent %pOFn\n",
  213. child, np);
  214. return -EINVAL;
  215. }
  216. rc = of_unittest_check_node_linkage(child);
  217. if (rc < 0)
  218. return rc;
  219. count += rc;
  220. }
  221. return count + 1;
  222. }
  223. static void __init of_unittest_check_tree_linkage(void)
  224. {
  225. struct device_node *np;
  226. int allnode_count = 0, child_count;
  227. if (!of_root)
  228. return;
  229. for_each_of_allnodes(np)
  230. allnode_count++;
  231. child_count = of_unittest_check_node_linkage(of_root);
  232. unittest(child_count > 0, "Device node data structure is corrupted\n");
  233. unittest(child_count == allnode_count,
  234. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  235. allnode_count, child_count);
  236. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  237. }
  238. static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
  239. const char *expected)
  240. {
  241. unsigned char *buf;
  242. int buf_size;
  243. int size, i;
  244. buf_size = strlen(expected) + 10;
  245. buf = kmalloc(buf_size, GFP_KERNEL);
  246. if (!buf)
  247. return;
  248. /* Baseline; check conversion with a large size limit */
  249. memset(buf, 0xff, buf_size);
  250. size = snprintf(buf, buf_size - 2, fmt, np);
  251. /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
  252. unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
  253. "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
  254. fmt, expected, buf);
  255. /* Make sure length limits work */
  256. size++;
  257. for (i = 0; i < 2; i++, size--) {
  258. /* Clear the buffer, and make sure it works correctly still */
  259. memset(buf, 0xff, buf_size);
  260. snprintf(buf, size+1, fmt, np);
  261. unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
  262. "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
  263. size, fmt, expected, buf);
  264. }
  265. kfree(buf);
  266. }
  267. static void __init of_unittest_printf(void)
  268. {
  269. struct device_node *np;
  270. const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
  271. char phandle_str[16] = "";
  272. np = of_find_node_by_path(full_name);
  273. if (!np) {
  274. unittest(np, "testcase data missing\n");
  275. return;
  276. }
  277. num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
  278. of_unittest_printf_one(np, "%pOF", full_name);
  279. of_unittest_printf_one(np, "%pOFf", full_name);
  280. of_unittest_printf_one(np, "%pOFn", "dev");
  281. of_unittest_printf_one(np, "%2pOFn", "dev");
  282. of_unittest_printf_one(np, "%5pOFn", " dev");
  283. of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
  284. of_unittest_printf_one(np, "%pOFp", phandle_str);
  285. of_unittest_printf_one(np, "%pOFP", "dev@100");
  286. of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
  287. of_unittest_printf_one(np, "%10pOFP", " dev@100");
  288. of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
  289. of_unittest_printf_one(of_root, "%pOFP", "/");
  290. of_unittest_printf_one(np, "%pOFF", "----");
  291. of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
  292. of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
  293. of_unittest_printf_one(np, "%pOFc", "test-sub-device");
  294. of_unittest_printf_one(np, "%pOFC",
  295. "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
  296. }
  297. struct node_hash {
  298. struct hlist_node node;
  299. struct device_node *np;
  300. };
  301. static DEFINE_HASHTABLE(phandle_ht, 8);
  302. static void __init of_unittest_check_phandles(void)
  303. {
  304. struct device_node *np;
  305. struct node_hash *nh;
  306. struct hlist_node *tmp;
  307. int i, dup_count = 0, phandle_count = 0;
  308. for_each_of_allnodes(np) {
  309. if (!np->phandle)
  310. continue;
  311. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  312. if (nh->np->phandle == np->phandle) {
  313. pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
  314. np->phandle, nh->np, np);
  315. dup_count++;
  316. break;
  317. }
  318. }
  319. nh = kzalloc_obj(*nh);
  320. if (!nh)
  321. return;
  322. nh->np = np;
  323. hash_add(phandle_ht, &nh->node, np->phandle);
  324. phandle_count++;
  325. }
  326. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  327. dup_count, phandle_count);
  328. /* Clean up */
  329. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  330. hash_del(&nh->node);
  331. kfree(nh);
  332. }
  333. }
  334. static void __init of_unittest_parse_phandle_with_args(void)
  335. {
  336. struct device_node *np;
  337. struct of_phandle_args args;
  338. int i, rc;
  339. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  340. if (!np) {
  341. pr_err("missing testcase data\n");
  342. return;
  343. }
  344. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  345. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  346. for (i = 0; i < 8; i++) {
  347. bool passed = true;
  348. memset(&args, 0, sizeof(args));
  349. rc = of_parse_phandle_with_args(np, "phandle-list",
  350. "#phandle-cells", i, &args);
  351. /* Test the values from tests-phandle.dtsi */
  352. switch (i) {
  353. case 0:
  354. passed &= !rc;
  355. passed &= (args.args_count == 1);
  356. passed &= (args.args[0] == (i + 1));
  357. break;
  358. case 1:
  359. passed &= !rc;
  360. passed &= (args.args_count == 2);
  361. passed &= (args.args[0] == (i + 1));
  362. passed &= (args.args[1] == 0);
  363. break;
  364. case 2:
  365. passed &= (rc == -ENOENT);
  366. break;
  367. case 3:
  368. passed &= !rc;
  369. passed &= (args.args_count == 3);
  370. passed &= (args.args[0] == (i + 1));
  371. passed &= (args.args[1] == 4);
  372. passed &= (args.args[2] == 3);
  373. break;
  374. case 4:
  375. passed &= !rc;
  376. passed &= (args.args_count == 2);
  377. passed &= (args.args[0] == (i + 1));
  378. passed &= (args.args[1] == 100);
  379. break;
  380. case 5:
  381. passed &= !rc;
  382. passed &= (args.args_count == 0);
  383. break;
  384. case 6:
  385. passed &= !rc;
  386. passed &= (args.args_count == 1);
  387. passed &= (args.args[0] == (i + 1));
  388. break;
  389. case 7:
  390. passed &= (rc == -ENOENT);
  391. break;
  392. default:
  393. passed = false;
  394. }
  395. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  396. i, args.np, rc);
  397. if (rc == 0)
  398. of_node_put(args.np);
  399. }
  400. /* Check for missing list property */
  401. memset(&args, 0, sizeof(args));
  402. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  403. "#phandle-cells", 0, &args);
  404. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  405. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  406. "#phandle-cells");
  407. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  408. /* Check for missing cells property */
  409. memset(&args, 0, sizeof(args));
  410. EXPECT_BEGIN(KERN_INFO,
  411. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  412. rc = of_parse_phandle_with_args(np, "phandle-list",
  413. "#phandle-cells-missing", 0, &args);
  414. EXPECT_END(KERN_INFO,
  415. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  416. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  417. EXPECT_BEGIN(KERN_INFO,
  418. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  419. rc = of_count_phandle_with_args(np, "phandle-list",
  420. "#phandle-cells-missing");
  421. EXPECT_END(KERN_INFO,
  422. "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
  423. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  424. /* Check for bad phandle in list */
  425. memset(&args, 0, sizeof(args));
  426. EXPECT_BEGIN(KERN_INFO,
  427. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  428. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  429. "#phandle-cells", 0, &args);
  430. EXPECT_END(KERN_INFO,
  431. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  432. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  433. EXPECT_BEGIN(KERN_INFO,
  434. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  435. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  436. "#phandle-cells");
  437. EXPECT_END(KERN_INFO,
  438. "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
  439. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  440. /* Check for incorrectly formed argument list */
  441. memset(&args, 0, sizeof(args));
  442. EXPECT_BEGIN(KERN_INFO,
  443. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
  444. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  445. "#phandle-cells", 1, &args);
  446. EXPECT_END(KERN_INFO,
  447. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
  448. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  449. EXPECT_BEGIN(KERN_INFO,
  450. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
  451. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  452. "#phandle-cells");
  453. EXPECT_END(KERN_INFO,
  454. "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
  455. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  456. }
  457. static void __init of_unittest_parse_phandle_with_args_map(void)
  458. {
  459. struct device_node *np, *p[6] = {};
  460. struct of_phandle_args args;
  461. unsigned int prefs[6];
  462. int i, rc;
  463. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
  464. if (!np) {
  465. pr_err("missing testcase data\n");
  466. return;
  467. }
  468. p[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
  469. p[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
  470. p[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
  471. p[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
  472. p[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
  473. p[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
  474. for (i = 0; i < ARRAY_SIZE(p); ++i) {
  475. if (!p[i]) {
  476. pr_err("missing testcase data\n");
  477. return;
  478. }
  479. prefs[i] = OF_KREF_READ(p[i]);
  480. }
  481. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  482. unittest(rc == 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc);
  483. for (i = 0; i < 9; i++) {
  484. bool passed = true;
  485. memset(&args, 0, sizeof(args));
  486. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  487. "phandle", i, &args);
  488. /* Test the values from tests-phandle.dtsi */
  489. switch (i) {
  490. case 0:
  491. passed &= !rc;
  492. passed &= (args.np == p[1]);
  493. passed &= (args.args_count == 1);
  494. passed &= (args.args[0] == 1);
  495. break;
  496. case 1:
  497. passed &= !rc;
  498. passed &= (args.np == p[3]);
  499. passed &= (args.args_count == 3);
  500. passed &= (args.args[0] == 2);
  501. passed &= (args.args[1] == 5);
  502. passed &= (args.args[2] == 3);
  503. break;
  504. case 2:
  505. passed &= (rc == -ENOENT);
  506. break;
  507. case 3:
  508. passed &= !rc;
  509. passed &= (args.np == p[0]);
  510. passed &= (args.args_count == 0);
  511. break;
  512. case 4:
  513. passed &= !rc;
  514. passed &= (args.np == p[1]);
  515. passed &= (args.args_count == 1);
  516. passed &= (args.args[0] == 3);
  517. break;
  518. case 5:
  519. passed &= !rc;
  520. passed &= (args.np == p[0]);
  521. passed &= (args.args_count == 0);
  522. break;
  523. case 6:
  524. passed &= !rc;
  525. passed &= (args.np == p[2]);
  526. passed &= (args.args_count == 2);
  527. passed &= (args.args[0] == 15);
  528. passed &= (args.args[1] == 0x20);
  529. break;
  530. case 7:
  531. passed &= !rc;
  532. passed &= (args.np == p[3]);
  533. passed &= (args.args_count == 3);
  534. passed &= (args.args[0] == 2);
  535. passed &= (args.args[1] == 5);
  536. passed &= (args.args[2] == 3);
  537. break;
  538. case 8:
  539. passed &= (rc == -ENOENT);
  540. break;
  541. default:
  542. passed = false;
  543. }
  544. unittest(passed, "index %i - data error on node %s rc=%i\n",
  545. i, args.np->full_name, rc);
  546. if (rc == 0)
  547. of_node_put(args.np);
  548. }
  549. /* Check for missing list property */
  550. memset(&args, 0, sizeof(args));
  551. rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
  552. "phandle", 0, &args);
  553. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  554. /* Check for missing cells,map,mask property */
  555. memset(&args, 0, sizeof(args));
  556. EXPECT_BEGIN(KERN_INFO,
  557. "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
  558. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  559. "phandle-missing", 0, &args);
  560. EXPECT_END(KERN_INFO,
  561. "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
  562. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  563. /* Check for bad phandle in list */
  564. memset(&args, 0, sizeof(args));
  565. EXPECT_BEGIN(KERN_INFO,
  566. "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
  567. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
  568. "phandle", 0, &args);
  569. EXPECT_END(KERN_INFO,
  570. "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
  571. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  572. /* Check for incorrectly formed argument list */
  573. memset(&args, 0, sizeof(args));
  574. EXPECT_BEGIN(KERN_INFO,
  575. "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
  576. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
  577. "phandle", 1, &args);
  578. EXPECT_END(KERN_INFO,
  579. "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
  580. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  581. for (i = 0; i < ARRAY_SIZE(p); ++i) {
  582. unittest(prefs[i] == OF_KREF_READ(p[i]),
  583. "provider%d: expected:%d got:%d\n",
  584. i, prefs[i], OF_KREF_READ(p[i]));
  585. of_node_put(p[i]);
  586. }
  587. }
  588. static void __init of_unittest_property_string(void)
  589. {
  590. const char *strings[4];
  591. struct device_node *np;
  592. int rc;
  593. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  594. if (!np) {
  595. pr_err("No testcase data in device tree\n");
  596. return;
  597. }
  598. rc = of_property_match_string(np, "phandle-list-names", "first");
  599. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  600. rc = of_property_match_string(np, "phandle-list-names", "second");
  601. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  602. rc = of_property_match_string(np, "phandle-list-names", "third");
  603. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  604. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  605. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  606. rc = of_property_match_string(np, "missing-property", "blah");
  607. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  608. rc = of_property_match_string(np, "empty-property", "blah");
  609. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  610. rc = of_property_match_string(np, "unterminated-string", "blah");
  611. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  612. /* of_property_count_strings() tests */
  613. rc = of_property_count_strings(np, "string-property");
  614. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  615. rc = of_property_count_strings(np, "phandle-list-names");
  616. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  617. rc = of_property_count_strings(np, "unterminated-string");
  618. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  619. rc = of_property_count_strings(np, "unterminated-string-list");
  620. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  621. /* of_property_read_string_index() tests */
  622. rc = of_property_read_string_index(np, "string-property", 0, strings);
  623. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  624. strings[0] = NULL;
  625. rc = of_property_read_string_index(np, "string-property", 1, strings);
  626. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  627. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  628. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  629. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  630. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  631. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  632. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  633. strings[0] = NULL;
  634. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  635. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  636. strings[0] = NULL;
  637. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  638. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  639. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  640. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  641. strings[0] = NULL;
  642. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  643. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  644. strings[1] = NULL;
  645. /* of_property_read_string_array() tests */
  646. rc = of_property_read_string_array(np, "string-property", strings, 4);
  647. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  648. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  649. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  650. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  651. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  652. /* -- An incorrectly formed string should cause a failure */
  653. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  654. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  655. /* -- parsing the correctly formed strings should still work: */
  656. strings[2] = NULL;
  657. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  658. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  659. strings[1] = NULL;
  660. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  661. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  662. }
  663. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  664. (p1)->value && (p2)->value && \
  665. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  666. !strcmp((p1)->name, (p2)->name))
  667. static void __init of_unittest_property_copy(void)
  668. {
  669. #ifdef CONFIG_OF_DYNAMIC
  670. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  671. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  672. struct property *new;
  673. new = __of_prop_dup(&p1, GFP_KERNEL);
  674. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  675. if (new)
  676. __of_prop_free(new);
  677. new = __of_prop_dup(&p2, GFP_KERNEL);
  678. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  679. if (new)
  680. __of_prop_free(new);
  681. #endif
  682. }
  683. static void __init of_unittest_changeset(void)
  684. {
  685. #ifdef CONFIG_OF_DYNAMIC
  686. int ret;
  687. struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
  688. struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
  689. struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
  690. struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
  691. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  692. struct property *ppremove;
  693. struct device_node *n1, *n2, *n21, *n22, *nchangeset, *nremove, *parent, *np;
  694. static const char * const str_array[] = { "str1", "str2", "str3" };
  695. const u32 u32_array[] = { 1, 2, 3 };
  696. struct of_changeset chgset;
  697. const char *propstr = NULL;
  698. n1 = __of_node_dup(NULL, "n1");
  699. unittest(n1, "testcase setup failure\n");
  700. n2 = __of_node_dup(NULL, "n2");
  701. unittest(n2, "testcase setup failure\n");
  702. n21 = __of_node_dup(NULL, "n21");
  703. unittest(n21, "testcase setup failure %p\n", n21);
  704. nchangeset = of_find_node_by_path("/testcase-data/changeset");
  705. nremove = of_get_child_by_name(nchangeset, "node-remove");
  706. unittest(nremove, "testcase setup failure\n");
  707. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  708. unittest(ppadd, "testcase setup failure\n");
  709. ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
  710. unittest(ppname_n1, "testcase setup failure\n");
  711. ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
  712. unittest(ppname_n2, "testcase setup failure\n");
  713. ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
  714. unittest(ppname_n21, "testcase setup failure\n");
  715. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  716. unittest(ppupdate, "testcase setup failure\n");
  717. parent = nchangeset;
  718. n1->parent = parent;
  719. n2->parent = parent;
  720. n21->parent = n2;
  721. ppremove = of_find_property(parent, "prop-remove", NULL);
  722. unittest(ppremove, "failed to find removal prop");
  723. of_changeset_init(&chgset);
  724. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  725. unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
  726. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  727. unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
  728. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  729. unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
  730. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  731. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
  732. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  733. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  734. n22 = of_changeset_create_node(&chgset, n2, "n22");
  735. unittest(n22, "fail create n22\n");
  736. unittest(!of_changeset_add_prop_string(&chgset, n22, "prop-str", "abcd"),
  737. "fail add prop prop-str");
  738. unittest(!of_changeset_add_prop_string_array(&chgset, n22, "prop-str-array",
  739. (const char **)str_array,
  740. ARRAY_SIZE(str_array)),
  741. "fail add prop prop-str-array");
  742. unittest(!of_changeset_add_prop_u32_array(&chgset, n22, "prop-u32-array",
  743. u32_array, ARRAY_SIZE(u32_array)),
  744. "fail add prop prop-u32-array");
  745. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  746. of_node_put(nchangeset);
  747. /* Make sure node names are constructed correctly */
  748. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  749. "'%pOF' not added\n", n21);
  750. of_node_put(np);
  751. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n22")),
  752. "'%pOF' not added\n", n22);
  753. of_node_put(np);
  754. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  755. unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
  756. "'%pOF' still present after revert\n", n21);
  757. unittest(of_property_present(parent, "prop-remove"),
  758. "failed to find removed prop after revert\n");
  759. ret = of_property_read_string(parent, "prop-update", &propstr);
  760. unittest(!ret, "failed to find updated prop after revert\n");
  761. if (!ret)
  762. unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
  763. of_changeset_destroy(&chgset);
  764. of_node_put(n1);
  765. of_node_put(n2);
  766. of_node_put(n21);
  767. of_node_put(n22);
  768. #endif
  769. }
  770. static void __init __maybe_unused changeset_check_string(struct device_node *np,
  771. const char *prop_name,
  772. const char *expected_str)
  773. {
  774. const char *str;
  775. int ret;
  776. ret = of_property_read_string(np, prop_name, &str);
  777. if (unittest(ret == 0, "failed to read %s\n", prop_name))
  778. return;
  779. unittest(strcmp(str, expected_str) == 0,
  780. "%s value mismatch (read '%s', exp '%s')\n",
  781. prop_name, str, expected_str);
  782. }
  783. static void __init __maybe_unused changeset_check_string_array(struct device_node *np,
  784. const char *prop_name,
  785. const char * const *expected_array,
  786. unsigned int count)
  787. {
  788. const char *str;
  789. unsigned int i;
  790. int ret;
  791. int cnt;
  792. cnt = of_property_count_strings(np, prop_name);
  793. if (unittest(cnt >= 0, "failed to get %s count\n", prop_name))
  794. return;
  795. if (unittest(cnt == count,
  796. "%s count mismatch (read %d, exp %u)\n",
  797. prop_name, cnt, count))
  798. return;
  799. for (i = 0; i < count; i++) {
  800. ret = of_property_read_string_index(np, prop_name, i, &str);
  801. if (unittest(ret == 0, "failed to read %s[%d]\n", prop_name, i))
  802. continue;
  803. unittest(strcmp(str, expected_array[i]) == 0,
  804. "%s[%d] value mismatch (read '%s', exp '%s')\n",
  805. prop_name, i, str, expected_array[i]);
  806. }
  807. }
  808. static void __init __maybe_unused changeset_check_u32(struct device_node *np,
  809. const char *prop_name,
  810. u32 expected_u32)
  811. {
  812. u32 val32;
  813. int ret;
  814. ret = of_property_read_u32(np, prop_name, &val32);
  815. if (unittest(ret == 0, "failed to read %s\n", prop_name))
  816. return;
  817. unittest(val32 == expected_u32,
  818. "%s value mismatch (read '%u', exp '%u')\n",
  819. prop_name, val32, expected_u32);
  820. }
  821. static void __init __maybe_unused changeset_check_u32_array(struct device_node *np,
  822. const char *prop_name,
  823. const u32 *expected_array,
  824. unsigned int count)
  825. {
  826. unsigned int i;
  827. u32 val32;
  828. int ret;
  829. int cnt;
  830. cnt = of_property_count_u32_elems(np, prop_name);
  831. if (unittest(cnt >= 0, "failed to get %s count\n", prop_name))
  832. return;
  833. if (unittest(cnt == count,
  834. "%s count mismatch (read %d, exp %u)\n",
  835. prop_name, cnt, count))
  836. return;
  837. for (i = 0; i < count; i++) {
  838. ret = of_property_read_u32_index(np, prop_name, i, &val32);
  839. if (unittest(ret == 0, "failed to read %s[%d]\n", prop_name, i))
  840. continue;
  841. unittest(val32 == expected_array[i],
  842. "%s[%d] value mismatch (read '%u', exp '%u')\n",
  843. prop_name, i, val32, expected_array[i]);
  844. }
  845. }
  846. static void __init __maybe_unused changeset_check_bool(struct device_node *np,
  847. const char *prop_name)
  848. {
  849. unittest(of_property_read_bool(np, prop_name),
  850. "%s value mismatch (read 'false', exp 'true')\n", prop_name);
  851. }
  852. static void __init of_unittest_changeset_prop(void)
  853. {
  854. #ifdef CONFIG_OF_DYNAMIC
  855. static const char * const str_array[] = { "abc", "defg", "hij" };
  856. static const u32 u32_array[] = { 123, 4567, 89, 10, 11 };
  857. struct device_node *nchangeset, *np;
  858. struct of_changeset chgset;
  859. int ret;
  860. nchangeset = of_find_node_by_path("/testcase-data/changeset");
  861. if (!nchangeset) {
  862. pr_err("missing testcase data\n");
  863. return;
  864. }
  865. of_changeset_init(&chgset);
  866. np = of_changeset_create_node(&chgset, nchangeset, "test-prop");
  867. if (unittest(np, "failed to create test-prop node\n"))
  868. goto end_changeset_destroy;
  869. ret = of_changeset_add_prop_string(&chgset, np, "prop-string", "abcde");
  870. unittest(ret == 0, "failed to add prop-string\n");
  871. ret = of_changeset_add_prop_string_array(&chgset, np, "prop-string-array",
  872. str_array, ARRAY_SIZE(str_array));
  873. unittest(ret == 0, "failed to add prop-string-array\n");
  874. ret = of_changeset_add_prop_u32(&chgset, np, "prop-u32", 1234);
  875. unittest(ret == 0, "failed to add prop-u32\n");
  876. ret = of_changeset_add_prop_u32_array(&chgset, np, "prop-u32-array",
  877. u32_array, ARRAY_SIZE(u32_array));
  878. unittest(ret == 0, "failed to add prop-u32-array\n");
  879. ret = of_changeset_add_prop_bool(&chgset, np, "prop-bool");
  880. unittest(ret == 0, "failed to add prop-bool\n");
  881. of_node_put(np);
  882. ret = of_changeset_apply(&chgset);
  883. if (unittest(ret == 0, "failed to apply changeset\n"))
  884. goto end_changeset_destroy;
  885. np = of_find_node_by_path("/testcase-data/changeset/test-prop");
  886. if (unittest(np, "failed to find test-prop node\n"))
  887. goto end_revert_changeset;
  888. changeset_check_string(np, "prop-string", "abcde");
  889. changeset_check_string_array(np, "prop-string-array", str_array, ARRAY_SIZE(str_array));
  890. changeset_check_u32(np, "prop-u32", 1234);
  891. changeset_check_u32_array(np, "prop-u32-array", u32_array, ARRAY_SIZE(u32_array));
  892. changeset_check_bool(np, "prop-bool");
  893. of_node_put(np);
  894. end_revert_changeset:
  895. ret = of_changeset_revert(&chgset);
  896. unittest(ret == 0, "failed to revert changeset\n");
  897. end_changeset_destroy:
  898. of_changeset_destroy(&chgset);
  899. of_node_put(nchangeset);
  900. #endif
  901. }
  902. static void __init of_unittest_dma_get_max_cpu_address(void)
  903. {
  904. struct device_node *np;
  905. phys_addr_t cpu_addr;
  906. if (!IS_ENABLED(CONFIG_OF_ADDRESS))
  907. return;
  908. np = of_find_node_by_path("/testcase-data/address-tests");
  909. if (!np) {
  910. pr_err("missing testcase data\n");
  911. return;
  912. }
  913. cpu_addr = of_dma_get_max_cpu_address(np);
  914. unittest(cpu_addr == 0x4fffffff,
  915. "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
  916. &cpu_addr, 0x4fffffff);
  917. }
  918. static void __init of_unittest_dma_ranges_one(const char *path,
  919. u64 expect_dma_addr, u64 expect_paddr)
  920. {
  921. #ifdef CONFIG_HAS_DMA
  922. struct device_node *np;
  923. const struct bus_dma_region *map = NULL;
  924. int rc;
  925. np = of_find_node_by_path(path);
  926. if (!np) {
  927. pr_err("missing testcase data\n");
  928. return;
  929. }
  930. rc = of_dma_get_range(np, &map);
  931. unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
  932. if (!rc) {
  933. phys_addr_t paddr;
  934. dma_addr_t dma_addr;
  935. struct device *dev_bogus;
  936. dev_bogus = kzalloc_obj(struct device);
  937. if (!dev_bogus) {
  938. unittest(0, "kzalloc() failed\n");
  939. kfree(map);
  940. return;
  941. }
  942. dev_bogus->dma_range_map = map;
  943. paddr = dma_to_phys(dev_bogus, expect_dma_addr);
  944. dma_addr = phys_to_dma(dev_bogus, expect_paddr);
  945. unittest(paddr == expect_paddr,
  946. "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
  947. &paddr, expect_paddr, np);
  948. unittest(dma_addr == expect_dma_addr,
  949. "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
  950. &dma_addr, expect_dma_addr, np);
  951. kfree(map);
  952. kfree(dev_bogus);
  953. }
  954. of_node_put(np);
  955. #endif
  956. }
  957. static void __init of_unittest_parse_dma_ranges(void)
  958. {
  959. of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
  960. 0x0, 0x20000000);
  961. if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
  962. of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
  963. 0x100000000, 0x20000000);
  964. of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
  965. 0x80000000, 0x20000000);
  966. }
  967. static void __init of_unittest_pci_dma_ranges(void)
  968. {
  969. struct device_node *np;
  970. struct of_pci_range range;
  971. struct of_pci_range_parser parser;
  972. int i = 0;
  973. if (!IS_ENABLED(CONFIG_PCI))
  974. return;
  975. np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
  976. if (!np) {
  977. pr_err("missing testcase data\n");
  978. return;
  979. }
  980. if (of_pci_dma_range_parser_init(&parser, np)) {
  981. pr_err("missing dma-ranges property\n");
  982. return;
  983. }
  984. /*
  985. * Get the dma-ranges from the device tree
  986. */
  987. for_each_of_pci_range(&parser, &range) {
  988. if (!i) {
  989. unittest(range.size == 0x10000000,
  990. "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
  991. np, range.size);
  992. unittest(range.cpu_addr == 0x20000000,
  993. "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
  994. range.cpu_addr, np);
  995. unittest(range.pci_addr == 0x80000000,
  996. "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
  997. range.pci_addr, np);
  998. } else {
  999. unittest(range.size == 0x10000000,
  1000. "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
  1001. np, range.size);
  1002. unittest(range.cpu_addr == 0x40000000,
  1003. "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
  1004. range.cpu_addr, np);
  1005. unittest(range.pci_addr == 0xc0000000,
  1006. "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
  1007. range.pci_addr, np);
  1008. }
  1009. i++;
  1010. }
  1011. of_node_put(np);
  1012. }
  1013. static void __init of_unittest_pci_empty_dma_ranges(void)
  1014. {
  1015. struct device_node *np;
  1016. struct of_pci_range range;
  1017. struct of_pci_range_parser parser;
  1018. if (!IS_ENABLED(CONFIG_PCI))
  1019. return;
  1020. np = of_find_node_by_path("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0");
  1021. if (!np) {
  1022. pr_err("missing testcase data\n");
  1023. return;
  1024. }
  1025. if (of_pci_dma_range_parser_init(&parser, np)) {
  1026. pr_err("missing dma-ranges property\n");
  1027. return;
  1028. }
  1029. /*
  1030. * Get the dma-ranges from the device tree
  1031. */
  1032. for_each_of_pci_range(&parser, &range) {
  1033. unittest(range.size == 0x10000000,
  1034. "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
  1035. np, range.size);
  1036. unittest(range.cpu_addr == 0x00000000,
  1037. "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
  1038. range.cpu_addr, np);
  1039. unittest(range.pci_addr == 0xc0000000,
  1040. "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
  1041. range.pci_addr, np);
  1042. }
  1043. of_node_put(np);
  1044. }
  1045. static void __init of_unittest_bus_ranges(void)
  1046. {
  1047. struct device_node *np;
  1048. struct of_range range;
  1049. struct of_range_parser parser;
  1050. struct resource res;
  1051. int ret, count, i = 0;
  1052. np = of_find_node_by_path("/testcase-data/address-tests");
  1053. if (!np) {
  1054. pr_err("missing testcase data\n");
  1055. return;
  1056. }
  1057. if (of_range_parser_init(&parser, np)) {
  1058. pr_err("missing ranges property\n");
  1059. return;
  1060. }
  1061. ret = of_range_to_resource(np, 1, &res);
  1062. unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
  1063. ret, np);
  1064. unittest(resource_type(&res) == IORESOURCE_MEM,
  1065. "of_range_to_resource wrong resource type on node %pOF res=%pR\n",
  1066. np, &res);
  1067. unittest(res.start == 0xd0000000,
  1068. "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
  1069. np, &res);
  1070. unittest(resource_size(&res) == 0x20000000,
  1071. "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
  1072. np, &res);
  1073. count = of_range_count(&parser);
  1074. unittest(count == 2,
  1075. "of_range_count wrong size on node %pOF count=%d\n",
  1076. np, count);
  1077. /*
  1078. * Get the "ranges" from the device tree
  1079. */
  1080. for_each_of_range(&parser, &range) {
  1081. unittest(range.flags == IORESOURCE_MEM,
  1082. "for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
  1083. np, range.flags, IORESOURCE_MEM);
  1084. if (!i) {
  1085. unittest(range.size == 0x50000000,
  1086. "for_each_of_range wrong size on node %pOF size=%llx\n",
  1087. np, range.size);
  1088. unittest(range.cpu_addr == 0x70000000,
  1089. "for_each_of_range wrong CPU addr (%llx) on node %pOF",
  1090. range.cpu_addr, np);
  1091. unittest(range.bus_addr == 0x70000000,
  1092. "for_each_of_range wrong bus addr (%llx) on node %pOF",
  1093. range.pci_addr, np);
  1094. } else {
  1095. unittest(range.size == 0x20000000,
  1096. "for_each_of_range wrong size on node %pOF size=%llx\n",
  1097. np, range.size);
  1098. unittest(range.cpu_addr == 0xd0000000,
  1099. "for_each_of_range wrong CPU addr (%llx) on node %pOF",
  1100. range.cpu_addr, np);
  1101. unittest(range.bus_addr == 0x00000000,
  1102. "for_each_of_range wrong bus addr (%llx) on node %pOF",
  1103. range.pci_addr, np);
  1104. }
  1105. i++;
  1106. }
  1107. of_node_put(np);
  1108. }
  1109. static void __init of_unittest_bus_3cell_ranges(void)
  1110. {
  1111. struct device_node *np;
  1112. struct of_range range;
  1113. struct of_range_parser parser;
  1114. int i = 0;
  1115. np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
  1116. if (!np) {
  1117. pr_err("missing testcase data\n");
  1118. return;
  1119. }
  1120. if (of_range_parser_init(&parser, np)) {
  1121. pr_err("missing ranges property\n");
  1122. return;
  1123. }
  1124. /*
  1125. * Get the "ranges" from the device tree
  1126. */
  1127. for_each_of_range(&parser, &range) {
  1128. if (!i) {
  1129. unittest(range.flags == 0xf00baa,
  1130. "for_each_of_range wrong flags on node %pOF flags=%x\n",
  1131. np, range.flags);
  1132. unittest(range.size == 0x100000,
  1133. "for_each_of_range wrong size on node %pOF size=%llx\n",
  1134. np, range.size);
  1135. unittest(range.cpu_addr == 0xa0000000,
  1136. "for_each_of_range wrong CPU addr (%llx) on node %pOF",
  1137. range.cpu_addr, np);
  1138. unittest(range.bus_addr == 0x0,
  1139. "for_each_of_range wrong bus addr (%llx) on node %pOF",
  1140. range.pci_addr, np);
  1141. } else {
  1142. unittest(range.flags == 0xf00bee,
  1143. "for_each_of_range wrong flags on node %pOF flags=%x\n",
  1144. np, range.flags);
  1145. unittest(range.size == 0x200000,
  1146. "for_each_of_range wrong size on node %pOF size=%llx\n",
  1147. np, range.size);
  1148. unittest(range.cpu_addr == 0xb0000000,
  1149. "for_each_of_range wrong CPU addr (%llx) on node %pOF",
  1150. range.cpu_addr, np);
  1151. unittest(range.bus_addr == 0x100000000,
  1152. "for_each_of_range wrong bus addr (%llx) on node %pOF",
  1153. range.pci_addr, np);
  1154. }
  1155. i++;
  1156. }
  1157. of_node_put(np);
  1158. }
  1159. static void __init of_unittest_reg(void)
  1160. {
  1161. struct device_node *np;
  1162. struct resource res;
  1163. int ret;
  1164. u64 addr, size;
  1165. np = of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
  1166. if (!np) {
  1167. pr_err("missing testcase data\n");
  1168. return;
  1169. }
  1170. ret = of_property_read_reg(np, 0, &addr, &size);
  1171. unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
  1172. np, ret);
  1173. unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
  1174. np, addr);
  1175. of_node_put(np);
  1176. np = of_find_node_by_path("/testcase-data/platform-tests-2/node/test-device@100");
  1177. if (!np) {
  1178. pr_err("missing testcase data\n");
  1179. return;
  1180. }
  1181. ret = of_address_to_resource(np, 0, &res);
  1182. unittest(ret == -EINVAL, "of_address_to_resource(%pOF) expected error on untranslatable address\n",
  1183. np);
  1184. of_node_put(np);
  1185. }
  1186. struct of_unittest_expected_res {
  1187. int index;
  1188. struct resource res;
  1189. };
  1190. static void __init of_unittest_check_addr(const char *node_path,
  1191. const struct of_unittest_expected_res *tab_exp,
  1192. unsigned int tab_exp_count)
  1193. {
  1194. const struct of_unittest_expected_res *expected;
  1195. struct device_node *np;
  1196. struct resource res;
  1197. unsigned int count;
  1198. int ret;
  1199. if (!IS_ENABLED(CONFIG_OF_ADDRESS))
  1200. return;
  1201. np = of_find_node_by_path(node_path);
  1202. if (!np) {
  1203. pr_err("missing testcase data (%s)\n", node_path);
  1204. return;
  1205. }
  1206. expected = tab_exp;
  1207. count = tab_exp_count;
  1208. while (count--) {
  1209. ret = of_address_to_resource(np, expected->index, &res);
  1210. unittest(!ret, "of_address_to_resource(%pOF, %d) returned error %d\n",
  1211. np, expected->index, ret);
  1212. unittest(resource_type(&res) == resource_type(&expected->res) &&
  1213. res.start == expected->res.start &&
  1214. resource_size(&res) == resource_size(&expected->res),
  1215. "of_address_to_resource(%pOF, %d) wrong resource %pR, expected %pR\n",
  1216. np, expected->index, &res, &expected->res);
  1217. expected++;
  1218. }
  1219. of_node_put(np);
  1220. }
  1221. static const struct of_unittest_expected_res of_unittest_reg_2cell_expected_res[] = {
  1222. {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
  1223. {.index = 1, .res = DEFINE_RES_MEM(0xa0a02000, 0x100) },
  1224. {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
  1225. {.index = 3, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
  1226. };
  1227. static const struct of_unittest_expected_res of_unittest_reg_3cell_expected_res[] = {
  1228. {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
  1229. {.index = 1, .res = DEFINE_RES_MEM(0xa0b02000, 0x100) },
  1230. {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
  1231. {.index = 3, .res = DEFINE_RES_MEM(0xc0c09000, 0x100) },
  1232. {.index = 4, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
  1233. };
  1234. static const struct of_unittest_expected_res of_unittest_reg_pci_expected_res[] = {
  1235. {.index = 0, .res = DEFINE_RES_MEM(0xe8001000, 0x1000) },
  1236. {.index = 1, .res = DEFINE_RES_MEM(0xea002000, 0x2000) },
  1237. };
  1238. static void __init of_unittest_translate_addr(void)
  1239. {
  1240. of_unittest_check_addr("/testcase-data/address-tests2/bus-2cell@10000000/device@100000",
  1241. of_unittest_reg_2cell_expected_res,
  1242. ARRAY_SIZE(of_unittest_reg_2cell_expected_res));
  1243. of_unittest_check_addr("/testcase-data/address-tests2/bus-3cell@20000000/local-bus@100000/device@f1001000",
  1244. of_unittest_reg_3cell_expected_res,
  1245. ARRAY_SIZE(of_unittest_reg_3cell_expected_res));
  1246. of_unittest_check_addr("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0/dev@e0000000",
  1247. of_unittest_reg_pci_expected_res,
  1248. ARRAY_SIZE(of_unittest_reg_pci_expected_res));
  1249. }
  1250. static void __init of_unittest_parse_interrupts(void)
  1251. {
  1252. struct device_node *np;
  1253. struct of_phandle_args args;
  1254. int i, rc;
  1255. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  1256. return;
  1257. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  1258. if (!np) {
  1259. pr_err("missing testcase data\n");
  1260. return;
  1261. }
  1262. for (i = 0; i < 4; i++) {
  1263. bool passed = true;
  1264. memset(&args, 0, sizeof(args));
  1265. rc = of_irq_parse_one(np, i, &args);
  1266. passed &= !rc;
  1267. passed &= (args.args_count == 1);
  1268. passed &= (args.args[0] == (i + 1));
  1269. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  1270. i, args.np, rc);
  1271. }
  1272. of_node_put(np);
  1273. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  1274. if (!np) {
  1275. pr_err("missing testcase data\n");
  1276. return;
  1277. }
  1278. for (i = 0; i < 4; i++) {
  1279. bool passed = true;
  1280. memset(&args, 0, sizeof(args));
  1281. rc = of_irq_parse_one(np, i, &args);
  1282. /* Test the values from tests-phandle.dtsi */
  1283. switch (i) {
  1284. case 0:
  1285. passed &= !rc;
  1286. passed &= (args.args_count == 1);
  1287. passed &= (args.args[0] == 9);
  1288. break;
  1289. case 1:
  1290. passed &= !rc;
  1291. passed &= (args.args_count == 3);
  1292. passed &= (args.args[0] == 10);
  1293. passed &= (args.args[1] == 11);
  1294. passed &= (args.args[2] == 12);
  1295. break;
  1296. case 2:
  1297. passed &= !rc;
  1298. passed &= (args.args_count == 2);
  1299. passed &= (args.args[0] == 13);
  1300. passed &= (args.args[1] == 14);
  1301. break;
  1302. case 3:
  1303. passed &= !rc;
  1304. passed &= (args.args_count == 2);
  1305. passed &= (args.args[0] == 15);
  1306. passed &= (args.args[1] == 16);
  1307. break;
  1308. default:
  1309. passed = false;
  1310. }
  1311. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  1312. i, args.np, rc);
  1313. }
  1314. of_node_put(np);
  1315. }
  1316. static void __init of_unittest_parse_interrupts_extended(void)
  1317. {
  1318. struct device_node *np;
  1319. struct of_phandle_args args;
  1320. int i, rc;
  1321. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  1322. return;
  1323. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  1324. if (!np) {
  1325. pr_err("missing testcase data\n");
  1326. return;
  1327. }
  1328. for (i = 0; i < 7; i++) {
  1329. bool passed = true;
  1330. memset(&args, 0, sizeof(args));
  1331. rc = of_irq_parse_one(np, i, &args);
  1332. /* Test the values from tests-phandle.dtsi */
  1333. switch (i) {
  1334. case 0:
  1335. passed &= !rc;
  1336. passed &= (args.args_count == 1);
  1337. passed &= (args.args[0] == 1);
  1338. break;
  1339. case 1:
  1340. passed &= !rc;
  1341. passed &= (args.args_count == 3);
  1342. passed &= (args.args[0] == 2);
  1343. passed &= (args.args[1] == 3);
  1344. passed &= (args.args[2] == 4);
  1345. break;
  1346. case 2:
  1347. passed &= !rc;
  1348. passed &= (args.args_count == 2);
  1349. passed &= (args.args[0] == 5);
  1350. passed &= (args.args[1] == 6);
  1351. break;
  1352. case 3:
  1353. passed &= !rc;
  1354. passed &= (args.args_count == 1);
  1355. passed &= (args.args[0] == 9);
  1356. break;
  1357. case 4:
  1358. passed &= !rc;
  1359. passed &= (args.args_count == 3);
  1360. passed &= (args.args[0] == 10);
  1361. passed &= (args.args[1] == 11);
  1362. passed &= (args.args[2] == 12);
  1363. break;
  1364. case 5:
  1365. passed &= !rc;
  1366. passed &= (args.args_count == 2);
  1367. passed &= (args.args[0] == 13);
  1368. passed &= (args.args[1] == 14);
  1369. break;
  1370. case 6:
  1371. /*
  1372. * Tests child node that is missing property
  1373. * #address-cells. See the comments in
  1374. * drivers/of/unittest-data/tests-interrupts.dtsi
  1375. * nodes intmap1 and interrupts-extended0
  1376. */
  1377. passed &= !rc;
  1378. passed &= (args.args_count == 1);
  1379. passed &= (args.args[0] == 15);
  1380. break;
  1381. default:
  1382. passed = false;
  1383. }
  1384. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  1385. i, args.np, rc);
  1386. }
  1387. of_node_put(np);
  1388. }
  1389. struct of_unittest_expected_imap_item {
  1390. u32 child_imap_count;
  1391. u32 child_imap[2];
  1392. const char *parent_path;
  1393. int parent_args_count;
  1394. u32 parent_args[3];
  1395. };
  1396. static const struct of_unittest_expected_imap_item of_unittest_expected_imap_items[] = {
  1397. {
  1398. .child_imap_count = 2,
  1399. .child_imap = {1, 11},
  1400. .parent_path = "/testcase-data/interrupts/intc0",
  1401. .parent_args_count = 1,
  1402. .parent_args = {100},
  1403. }, {
  1404. .child_imap_count = 2,
  1405. .child_imap = {2, 22},
  1406. .parent_path = "/testcase-data/interrupts/intc1",
  1407. .parent_args_count = 3,
  1408. .parent_args = {200, 201, 202},
  1409. }, {
  1410. .child_imap_count = 2,
  1411. .child_imap = {3, 33},
  1412. .parent_path = "/testcase-data/interrupts/intc2",
  1413. .parent_args_count = 2,
  1414. .parent_args = {300, 301},
  1415. }, {
  1416. .child_imap_count = 2,
  1417. .child_imap = {4, 44},
  1418. .parent_path = "/testcase-data/interrupts/intc2",
  1419. .parent_args_count = 2,
  1420. .parent_args = {400, 401},
  1421. }
  1422. };
  1423. static void __init of_unittest_parse_interrupt_map(void)
  1424. {
  1425. const struct of_unittest_expected_imap_item *expected_item;
  1426. struct device_node *imap_np, *expected_parent_np;
  1427. struct of_imap_parser imap_parser;
  1428. struct of_imap_item imap_item;
  1429. int count, ret, i;
  1430. if (of_irq_workarounds & (OF_IMAP_NO_PHANDLE | OF_IMAP_OLDWORLD_MAC))
  1431. return;
  1432. imap_np = of_find_node_by_path("/testcase-data/interrupts/intmap2");
  1433. if (!imap_np) {
  1434. pr_err("missing testcase data\n");
  1435. return;
  1436. }
  1437. ret = of_imap_parser_init(&imap_parser, imap_np, &imap_item);
  1438. if (unittest(!ret, "of_imap_parser_init(%pOF) returned error %d\n",
  1439. imap_np, ret))
  1440. goto end;
  1441. expected_item = of_unittest_expected_imap_items;
  1442. count = 0;
  1443. for_each_of_imap_item(&imap_parser, &imap_item) {
  1444. if (unittest(count < ARRAY_SIZE(of_unittest_expected_imap_items),
  1445. "imap item number %d not expected. Max number %zu\n",
  1446. count, ARRAY_SIZE(of_unittest_expected_imap_items) - 1)) {
  1447. of_node_put(imap_item.parent_args.np);
  1448. goto end;
  1449. }
  1450. expected_parent_np = of_find_node_by_path(expected_item->parent_path);
  1451. if (unittest(expected_parent_np,
  1452. "missing dependent testcase data (%s)\n",
  1453. expected_item->parent_path)) {
  1454. of_node_put(imap_item.parent_args.np);
  1455. goto end;
  1456. }
  1457. unittest(imap_item.child_imap_count == expected_item->child_imap_count,
  1458. "imap[%d] child_imap_count = %u, expected %u\n",
  1459. count, imap_item.child_imap_count,
  1460. expected_item->child_imap_count);
  1461. for (i = 0; i < expected_item->child_imap_count; i++)
  1462. unittest(imap_item.child_imap[i] == expected_item->child_imap[i],
  1463. "imap[%d] child_imap[%d] = %u, expected %u\n",
  1464. count, i, imap_item.child_imap[i],
  1465. expected_item->child_imap[i]);
  1466. unittest(imap_item.parent_args.np == expected_parent_np,
  1467. "imap[%d] parent np = %pOF, expected %pOF\n",
  1468. count, imap_item.parent_args.np, expected_parent_np);
  1469. unittest(imap_item.parent_args.args_count == expected_item->parent_args_count,
  1470. "imap[%d] parent param_count = %d, expected %d\n",
  1471. count, imap_item.parent_args.args_count,
  1472. expected_item->parent_args_count);
  1473. for (i = 0; i < expected_item->parent_args_count; i++)
  1474. unittest(imap_item.parent_args.args[i] == expected_item->parent_args[i],
  1475. "imap[%d] parent param[%d] = %u, expected %u\n",
  1476. count, i, imap_item.parent_args.args[i],
  1477. expected_item->parent_args[i]);
  1478. of_node_put(expected_parent_np);
  1479. count++;
  1480. expected_item++;
  1481. }
  1482. unittest(count == ARRAY_SIZE(of_unittest_expected_imap_items),
  1483. "Missing items. %d parsed, expected %zu\n",
  1484. count, ARRAY_SIZE(of_unittest_expected_imap_items));
  1485. end:
  1486. of_node_put(imap_np);
  1487. }
  1488. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  1489. static void __init of_unittest_irq_refcount(void)
  1490. {
  1491. struct of_phandle_args args;
  1492. struct device_node *intc0, *int_ext0;
  1493. struct device_node *int2, *intc_intmap0;
  1494. unsigned int ref_c0, ref_c1, ref_c2;
  1495. int rc;
  1496. bool passed;
  1497. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  1498. return;
  1499. intc0 = of_find_node_by_path("/testcase-data/interrupts/intc0");
  1500. int_ext0 = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  1501. intc_intmap0 = of_find_node_by_path("/testcase-data/interrupts/intc-intmap0");
  1502. int2 = of_find_node_by_path("/testcase-data/interrupts/interrupts2");
  1503. if (!intc0 || !int_ext0 || !intc_intmap0 || !int2) {
  1504. pr_err("missing testcase data\n");
  1505. goto out;
  1506. }
  1507. /* Test refcount for API of_irq_parse_one() */
  1508. passed = true;
  1509. ref_c0 = OF_KREF_READ(intc0);
  1510. ref_c1 = ref_c0 + 1;
  1511. memset(&args, 0, sizeof(args));
  1512. rc = of_irq_parse_one(int_ext0, 0, &args);
  1513. ref_c2 = OF_KREF_READ(intc0);
  1514. of_node_put(args.np);
  1515. passed &= !rc;
  1516. passed &= (args.np == intc0);
  1517. passed &= (args.args_count == 1);
  1518. passed &= (args.args[0] == 1);
  1519. passed &= (ref_c1 == ref_c2);
  1520. unittest(passed, "IRQ refcount case #1 failed, original(%u) expected(%u) got(%u)\n",
  1521. ref_c0, ref_c1, ref_c2);
  1522. /* Test refcount for API of_irq_parse_raw() */
  1523. passed = true;
  1524. ref_c0 = OF_KREF_READ(intc_intmap0);
  1525. ref_c1 = ref_c0 + 1;
  1526. memset(&args, 0, sizeof(args));
  1527. rc = of_irq_parse_one(int2, 0, &args);
  1528. ref_c2 = OF_KREF_READ(intc_intmap0);
  1529. of_node_put(args.np);
  1530. passed &= !rc;
  1531. passed &= (args.np == intc_intmap0);
  1532. passed &= (args.args_count == 1);
  1533. passed &= (args.args[0] == 2);
  1534. passed &= (ref_c1 == ref_c2);
  1535. unittest(passed, "IRQ refcount case #2 failed, original(%u) expected(%u) got(%u)\n",
  1536. ref_c0, ref_c1, ref_c2);
  1537. out:
  1538. of_node_put(int2);
  1539. of_node_put(intc_intmap0);
  1540. of_node_put(int_ext0);
  1541. of_node_put(intc0);
  1542. }
  1543. #else
  1544. static inline void __init of_unittest_irq_refcount(void) { }
  1545. #endif
  1546. static const struct of_device_id match_node_table[] = {
  1547. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  1548. { .data = "B", .type = "type1", }, /* followed by type alone */
  1549. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  1550. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  1551. { .data = "Cc", .name = "name2", .type = "type2", },
  1552. { .data = "E", .compatible = "compat3" },
  1553. { .data = "G", .compatible = "compat2", },
  1554. { .data = "H", .compatible = "compat2", .name = "name5", },
  1555. { .data = "I", .compatible = "compat2", .type = "type1", },
  1556. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  1557. { .data = "K", .compatible = "compat2", .name = "name9", },
  1558. {}
  1559. };
  1560. static struct {
  1561. const char *path;
  1562. const char *data;
  1563. } match_node_tests[] = {
  1564. { .path = "/testcase-data/match-node/name0", .data = "A", },
  1565. { .path = "/testcase-data/match-node/name1", .data = "B", },
  1566. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  1567. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  1568. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  1569. { .path = "/testcase-data/match-node/name3", .data = "E", },
  1570. { .path = "/testcase-data/match-node/name4", .data = "G", },
  1571. { .path = "/testcase-data/match-node/name5", .data = "H", },
  1572. { .path = "/testcase-data/match-node/name6", .data = "G", },
  1573. { .path = "/testcase-data/match-node/name7", .data = "I", },
  1574. { .path = "/testcase-data/match-node/name8", .data = "J", },
  1575. { .path = "/testcase-data/match-node/name9", .data = "K", },
  1576. };
  1577. static void __init of_unittest_match_node(void)
  1578. {
  1579. struct device_node *np;
  1580. const struct of_device_id *match;
  1581. int i;
  1582. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  1583. np = of_find_node_by_path(match_node_tests[i].path);
  1584. if (!np) {
  1585. unittest(0, "missing testcase node %s\n",
  1586. match_node_tests[i].path);
  1587. continue;
  1588. }
  1589. match = of_match_node(match_node_table, np);
  1590. if (!match) {
  1591. unittest(0, "%s didn't match anything\n",
  1592. match_node_tests[i].path);
  1593. continue;
  1594. }
  1595. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  1596. unittest(0, "%s got wrong match. expected %s, got %s\n",
  1597. match_node_tests[i].path, match_node_tests[i].data,
  1598. (const char *)match->data);
  1599. continue;
  1600. }
  1601. unittest(1, "passed");
  1602. }
  1603. }
  1604. static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
  1605. static const struct platform_device_info test_bus_info = {
  1606. .name = "unittest-bus",
  1607. };
  1608. static void __init of_unittest_platform_populate(void)
  1609. {
  1610. int irq, rc;
  1611. struct device_node *np, *child, *grandchild;
  1612. struct platform_device *pdev, *test_bus;
  1613. const struct of_device_id match[] = {
  1614. { .compatible = "test-device", },
  1615. {}
  1616. };
  1617. np = of_find_node_by_path("/testcase-data");
  1618. of_platform_default_populate(np, NULL, NULL);
  1619. /* Test that a missing irq domain returns -EPROBE_DEFER */
  1620. np = of_find_node_by_path("/testcase-data/testcase-device1");
  1621. pdev = of_find_device_by_node(np);
  1622. unittest(pdev, "device 1 creation failed\n");
  1623. if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
  1624. irq = platform_get_irq(pdev, 0);
  1625. unittest(irq == -EPROBE_DEFER,
  1626. "device deferred probe failed - %d\n", irq);
  1627. /* Test that a parsing failure does not return -EPROBE_DEFER */
  1628. np = of_find_node_by_path("/testcase-data/testcase-device2");
  1629. pdev = of_find_device_by_node(np);
  1630. unittest(pdev, "device 2 creation failed\n");
  1631. EXPECT_BEGIN(KERN_INFO,
  1632. "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
  1633. irq = platform_get_irq(pdev, 0);
  1634. EXPECT_END(KERN_INFO,
  1635. "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
  1636. unittest(irq < 0 && irq != -EPROBE_DEFER,
  1637. "device parsing error failed - %d\n", irq);
  1638. }
  1639. np = of_find_node_by_path("/testcase-data/platform-tests");
  1640. unittest(np, "No testcase data in device tree\n");
  1641. if (!np)
  1642. return;
  1643. test_bus = platform_device_register_full(&test_bus_info);
  1644. rc = PTR_ERR_OR_ZERO(test_bus);
  1645. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  1646. if (rc) {
  1647. of_node_put(np);
  1648. return;
  1649. }
  1650. test_bus->dev.of_node = np;
  1651. /*
  1652. * Add a dummy resource to the test bus node after it is
  1653. * registered to catch problems with un-inserted resources. The
  1654. * DT code doesn't insert the resources, and it has caused the
  1655. * kernel to oops in the past. This makes sure the same bug
  1656. * doesn't crop up again.
  1657. */
  1658. platform_device_add_resources(test_bus, &test_bus_res, 1);
  1659. of_platform_populate(np, match, NULL, &test_bus->dev);
  1660. for_each_child_of_node(np, child) {
  1661. for_each_child_of_node(child, grandchild) {
  1662. if (!of_property_present(grandchild, "compatible"))
  1663. continue;
  1664. pdev = of_find_device_by_node(grandchild);
  1665. unittest(pdev,
  1666. "Could not create device for node '%pOFn'\n",
  1667. grandchild);
  1668. platform_device_put(pdev);
  1669. }
  1670. }
  1671. of_platform_depopulate(&test_bus->dev);
  1672. for_each_child_of_node(np, child) {
  1673. for_each_child_of_node(child, grandchild)
  1674. unittest(!of_find_device_by_node(grandchild),
  1675. "device didn't get destroyed '%pOFn'\n",
  1676. grandchild);
  1677. }
  1678. platform_device_unregister(test_bus);
  1679. of_node_put(np);
  1680. }
  1681. /**
  1682. * update_node_properties - adds the properties
  1683. * of np into dup node (present in live tree) and
  1684. * updates parent of children of np to dup.
  1685. *
  1686. * @np: node whose properties are being added to the live tree
  1687. * @dup: node present in live tree to be updated
  1688. */
  1689. static void update_node_properties(struct device_node *np,
  1690. struct device_node *dup)
  1691. {
  1692. struct property *prop;
  1693. struct property *save_next;
  1694. struct device_node *child;
  1695. int ret;
  1696. for_each_child_of_node(np, child)
  1697. child->parent = dup;
  1698. /*
  1699. * "unittest internal error: unable to add testdata property"
  1700. *
  1701. * If this message reports a property in node '/__symbols__' then
  1702. * the respective unittest overlay contains a label that has the
  1703. * same name as a label in the live devicetree. The label will
  1704. * be in the live devicetree only if the devicetree source was
  1705. * compiled with the '-@' option. If you encounter this error,
  1706. * please consider renaming __all__ of the labels in the unittest
  1707. * overlay dts files with an odd prefix that is unlikely to be
  1708. * used in a real devicetree.
  1709. */
  1710. /*
  1711. * open code for_each_property_of_node() because of_add_property()
  1712. * sets prop->next to NULL
  1713. */
  1714. for (prop = np->properties; prop != NULL; prop = save_next) {
  1715. save_next = prop->next;
  1716. ret = of_add_property(dup, prop);
  1717. if (ret) {
  1718. if (ret == -EEXIST && !strcmp(prop->name, "name"))
  1719. continue;
  1720. pr_err("unittest internal error: unable to add testdata property %pOF/%s",
  1721. np, prop->name);
  1722. }
  1723. }
  1724. }
  1725. /**
  1726. * attach_node_and_children - attaches nodes
  1727. * and its children to live tree.
  1728. * CAUTION: misleading function name - if node @np already exists in
  1729. * the live tree then children of @np are *not* attached to the live
  1730. * tree. This works for the current test devicetree nodes because such
  1731. * nodes do not have child nodes.
  1732. *
  1733. * @np: Node to attach to live tree
  1734. */
  1735. static void attach_node_and_children(struct device_node *np)
  1736. {
  1737. struct device_node *next, *dup, *child;
  1738. unsigned long flags;
  1739. const char *full_name;
  1740. full_name = kasprintf(GFP_KERNEL, "%pOF", np);
  1741. if (!full_name)
  1742. return;
  1743. if (!strcmp(full_name, "/__local_fixups__") ||
  1744. !strcmp(full_name, "/__fixups__")) {
  1745. kfree(full_name);
  1746. return;
  1747. }
  1748. dup = of_find_node_by_path(full_name);
  1749. kfree(full_name);
  1750. if (dup) {
  1751. update_node_properties(np, dup);
  1752. return;
  1753. }
  1754. child = np->child;
  1755. np->child = NULL;
  1756. mutex_lock(&of_mutex);
  1757. raw_spin_lock_irqsave(&devtree_lock, flags);
  1758. np->sibling = np->parent->child;
  1759. np->parent->child = np;
  1760. of_node_clear_flag(np, OF_DETACHED);
  1761. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  1762. __of_attach_node_sysfs(np);
  1763. mutex_unlock(&of_mutex);
  1764. while (child) {
  1765. next = child->sibling;
  1766. attach_node_and_children(child);
  1767. child = next;
  1768. }
  1769. }
  1770. /**
  1771. * unittest_data_add - Reads, copies data from
  1772. * linked tree and attaches it to the live tree
  1773. */
  1774. static int __init unittest_data_add(void)
  1775. {
  1776. void *unittest_data_align;
  1777. struct device_node *unittest_data_node = NULL, *np;
  1778. /*
  1779. * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
  1780. * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
  1781. */
  1782. extern uint8_t __dtbo_testcases_begin[];
  1783. extern uint8_t __dtbo_testcases_end[];
  1784. const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
  1785. int rc;
  1786. void *ret;
  1787. if (!size) {
  1788. pr_warn("%s: testcases is empty\n", __func__);
  1789. return -ENODATA;
  1790. }
  1791. /* creating copy */
  1792. void *unittest_data __free(kfree) = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
  1793. if (!unittest_data)
  1794. return -ENOMEM;
  1795. unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
  1796. memcpy(unittest_data_align, __dtbo_testcases_begin, size);
  1797. ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
  1798. if (!ret) {
  1799. pr_warn("%s: unflatten testcases tree failed\n", __func__);
  1800. return -ENODATA;
  1801. }
  1802. if (!unittest_data_node) {
  1803. pr_warn("%s: testcases tree is empty\n", __func__);
  1804. return -ENODATA;
  1805. }
  1806. /*
  1807. * This lock normally encloses of_resolve_phandles()
  1808. */
  1809. of_overlay_mutex_lock();
  1810. rc = of_resolve_phandles(unittest_data_node);
  1811. if (rc) {
  1812. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  1813. rc = -EINVAL;
  1814. goto unlock;
  1815. }
  1816. /* attach the sub-tree to live tree */
  1817. if (!of_root) {
  1818. pr_warn("%s: no live tree to attach sub-tree\n", __func__);
  1819. rc = -ENODEV;
  1820. goto unlock;
  1821. }
  1822. EXPECT_BEGIN(KERN_INFO,
  1823. "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
  1824. np = unittest_data_node->child;
  1825. while (np) {
  1826. struct device_node *next = np->sibling;
  1827. np->parent = of_root;
  1828. /* this will clear OF_DETACHED in np and children */
  1829. attach_node_and_children(np);
  1830. np = next;
  1831. }
  1832. EXPECT_END(KERN_INFO,
  1833. "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
  1834. retain_and_null_ptr(unittest_data);
  1835. unlock:
  1836. of_overlay_mutex_unlock();
  1837. return rc;
  1838. }
  1839. #ifdef CONFIG_OF_OVERLAY
  1840. static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
  1841. static int unittest_probe(struct platform_device *pdev)
  1842. {
  1843. struct device *dev = &pdev->dev;
  1844. struct device_node *np = dev->of_node;
  1845. if (np == NULL) {
  1846. dev_err(dev, "No OF data for device\n");
  1847. return -EINVAL;
  1848. }
  1849. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1850. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1851. return 0;
  1852. }
  1853. static void unittest_remove(struct platform_device *pdev)
  1854. {
  1855. struct device *dev = &pdev->dev;
  1856. struct device_node *np = dev->of_node;
  1857. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1858. }
  1859. static const struct of_device_id unittest_match[] = {
  1860. { .compatible = "unittest", },
  1861. {},
  1862. };
  1863. static struct platform_driver unittest_driver = {
  1864. .probe = unittest_probe,
  1865. .remove = unittest_remove,
  1866. .driver = {
  1867. .name = "unittest",
  1868. .of_match_table = unittest_match,
  1869. },
  1870. };
  1871. /* get the platform device instantiated at the path */
  1872. static struct platform_device *of_path_to_platform_device(const char *path)
  1873. {
  1874. struct device_node *np;
  1875. struct platform_device *pdev;
  1876. np = of_find_node_by_path(path);
  1877. if (np == NULL)
  1878. return NULL;
  1879. pdev = of_find_device_by_node(np);
  1880. of_node_put(np);
  1881. return pdev;
  1882. }
  1883. /* find out if a platform device exists at that path */
  1884. static int of_path_platform_device_exists(const char *path)
  1885. {
  1886. struct platform_device *pdev;
  1887. pdev = of_path_to_platform_device(path);
  1888. platform_device_put(pdev);
  1889. return pdev != NULL;
  1890. }
  1891. #ifdef CONFIG_OF_GPIO
  1892. struct unittest_gpio_dev {
  1893. struct gpio_chip chip;
  1894. };
  1895. static int unittest_gpio_chip_request_count;
  1896. static int unittest_gpio_probe_count;
  1897. static int unittest_gpio_probe_pass_count;
  1898. static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
  1899. {
  1900. unittest_gpio_chip_request_count++;
  1901. pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
  1902. unittest_gpio_chip_request_count);
  1903. return 0;
  1904. }
  1905. static int unittest_gpio_probe(struct platform_device *pdev)
  1906. {
  1907. struct unittest_gpio_dev *devptr;
  1908. int ret;
  1909. unittest_gpio_probe_count++;
  1910. devptr = kzalloc_obj(*devptr);
  1911. if (!devptr)
  1912. return -ENOMEM;
  1913. platform_set_drvdata(pdev, devptr);
  1914. devptr->chip.fwnode = dev_fwnode(&pdev->dev);
  1915. devptr->chip.label = "of-unittest-gpio";
  1916. devptr->chip.base = -1; /* dynamic allocation */
  1917. devptr->chip.ngpio = 5;
  1918. devptr->chip.request = unittest_gpio_chip_request;
  1919. ret = gpiochip_add_data(&devptr->chip, NULL);
  1920. unittest(!ret,
  1921. "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
  1922. if (!ret)
  1923. unittest_gpio_probe_pass_count++;
  1924. return ret;
  1925. }
  1926. static void unittest_gpio_remove(struct platform_device *pdev)
  1927. {
  1928. struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
  1929. struct device *dev = &pdev->dev;
  1930. dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
  1931. if (devptr->chip.base != -1)
  1932. gpiochip_remove(&devptr->chip);
  1933. kfree(devptr);
  1934. }
  1935. static const struct of_device_id unittest_gpio_id[] = {
  1936. { .compatible = "unittest-gpio", },
  1937. {}
  1938. };
  1939. static struct platform_driver unittest_gpio_driver = {
  1940. .probe = unittest_gpio_probe,
  1941. .remove = unittest_gpio_remove,
  1942. .driver = {
  1943. .name = "unittest-gpio",
  1944. .of_match_table = unittest_gpio_id,
  1945. },
  1946. };
  1947. static void __init of_unittest_overlay_gpio(void)
  1948. {
  1949. int chip_request_count;
  1950. int probe_pass_count;
  1951. int ret;
  1952. /*
  1953. * tests: apply overlays before registering driver
  1954. * Similar to installing a driver as a module, the
  1955. * driver is registered after applying the overlays.
  1956. *
  1957. * The overlays are applied by overlay_data_apply()
  1958. * instead of of_unittest_apply_overlay() so that they
  1959. * will not be tracked. Thus they will not be removed
  1960. * by of_unittest_remove_tracked_overlays().
  1961. *
  1962. * - apply overlay_gpio_01
  1963. * - apply overlay_gpio_02a
  1964. * - apply overlay_gpio_02b
  1965. * - register driver
  1966. *
  1967. * register driver will result in
  1968. * - probe and processing gpio hog for overlay_gpio_01
  1969. * - probe for overlay_gpio_02a
  1970. * - processing gpio for overlay_gpio_02b
  1971. */
  1972. probe_pass_count = unittest_gpio_probe_pass_count;
  1973. chip_request_count = unittest_gpio_chip_request_count;
  1974. /*
  1975. * overlay_gpio_01 contains gpio node and child gpio hog node
  1976. * overlay_gpio_02a contains gpio node
  1977. * overlay_gpio_02b contains child gpio hog node
  1978. */
  1979. unittest(overlay_data_apply("overlay_gpio_01", NULL),
  1980. "Adding overlay 'overlay_gpio_01' failed\n");
  1981. unittest(overlay_data_apply("overlay_gpio_02a", NULL),
  1982. "Adding overlay 'overlay_gpio_02a' failed\n");
  1983. unittest(overlay_data_apply("overlay_gpio_02b", NULL),
  1984. "Adding overlay 'overlay_gpio_02b' failed\n");
  1985. ret = platform_driver_register(&unittest_gpio_driver);
  1986. if (unittest(ret == 0, "could not register unittest gpio driver\n"))
  1987. return;
  1988. unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
  1989. "unittest_gpio_probe() failed or not called\n");
  1990. unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
  1991. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1992. unittest_gpio_chip_request_count - chip_request_count);
  1993. /*
  1994. * tests: apply overlays after registering driver
  1995. *
  1996. * Similar to a driver built-in to the kernel, the
  1997. * driver is registered before applying the overlays.
  1998. *
  1999. * overlay_gpio_03 contains gpio node and child gpio hog node
  2000. *
  2001. * - apply overlay_gpio_03
  2002. *
  2003. * apply overlay will result in
  2004. * - probe and processing gpio hog.
  2005. */
  2006. probe_pass_count = unittest_gpio_probe_pass_count;
  2007. chip_request_count = unittest_gpio_chip_request_count;
  2008. /* overlay_gpio_03 contains gpio node and child gpio hog node */
  2009. unittest(overlay_data_apply("overlay_gpio_03", NULL),
  2010. "Adding overlay 'overlay_gpio_03' failed\n");
  2011. unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
  2012. "unittest_gpio_probe() failed or not called\n");
  2013. unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
  2014. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  2015. unittest_gpio_chip_request_count - chip_request_count);
  2016. /*
  2017. * overlay_gpio_04a contains gpio node
  2018. *
  2019. * - apply overlay_gpio_04a
  2020. *
  2021. * apply the overlay will result in
  2022. * - probe for overlay_gpio_04a
  2023. */
  2024. probe_pass_count = unittest_gpio_probe_pass_count;
  2025. chip_request_count = unittest_gpio_chip_request_count;
  2026. /* overlay_gpio_04a contains gpio node */
  2027. unittest(overlay_data_apply("overlay_gpio_04a", NULL),
  2028. "Adding overlay 'overlay_gpio_04a' failed\n");
  2029. unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
  2030. "unittest_gpio_probe() failed or not called\n");
  2031. /*
  2032. * overlay_gpio_04b contains child gpio hog node
  2033. *
  2034. * - apply overlay_gpio_04b
  2035. *
  2036. * apply the overlay will result in
  2037. * - processing gpio for overlay_gpio_04b
  2038. */
  2039. /* overlay_gpio_04b contains child gpio hog node */
  2040. unittest(overlay_data_apply("overlay_gpio_04b", NULL),
  2041. "Adding overlay 'overlay_gpio_04b' failed\n");
  2042. unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
  2043. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  2044. unittest_gpio_chip_request_count - chip_request_count);
  2045. }
  2046. #else
  2047. static void __init of_unittest_overlay_gpio(void)
  2048. {
  2049. /* skip tests */
  2050. }
  2051. #endif
  2052. #if IS_BUILTIN(CONFIG_I2C)
  2053. /* get the i2c client device instantiated at the path */
  2054. static struct i2c_client *of_path_to_i2c_client(const char *path)
  2055. {
  2056. struct device_node *np;
  2057. struct i2c_client *client;
  2058. np = of_find_node_by_path(path);
  2059. if (np == NULL)
  2060. return NULL;
  2061. client = of_find_i2c_device_by_node(np);
  2062. of_node_put(np);
  2063. return client;
  2064. }
  2065. /* find out if a i2c client device exists at that path */
  2066. static int of_path_i2c_client_exists(const char *path)
  2067. {
  2068. struct i2c_client *client;
  2069. client = of_path_to_i2c_client(path);
  2070. if (client)
  2071. put_device(&client->dev);
  2072. return client != NULL;
  2073. }
  2074. #else
  2075. static int of_path_i2c_client_exists(const char *path)
  2076. {
  2077. return 0;
  2078. }
  2079. #endif
  2080. enum overlay_type {
  2081. PDEV_OVERLAY,
  2082. I2C_OVERLAY
  2083. };
  2084. static int of_path_device_type_exists(const char *path,
  2085. enum overlay_type ovtype)
  2086. {
  2087. switch (ovtype) {
  2088. case PDEV_OVERLAY:
  2089. return of_path_platform_device_exists(path);
  2090. case I2C_OVERLAY:
  2091. return of_path_i2c_client_exists(path);
  2092. }
  2093. return 0;
  2094. }
  2095. static const char *unittest_path(int nr, enum overlay_type ovtype)
  2096. {
  2097. const char *base;
  2098. static char buf[256];
  2099. switch (ovtype) {
  2100. case PDEV_OVERLAY:
  2101. base = "/testcase-data/overlay-node/test-bus";
  2102. break;
  2103. case I2C_OVERLAY:
  2104. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  2105. break;
  2106. default:
  2107. buf[0] = '\0';
  2108. return buf;
  2109. }
  2110. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  2111. buf[sizeof(buf) - 1] = '\0';
  2112. return buf;
  2113. }
  2114. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  2115. {
  2116. const char *path;
  2117. path = unittest_path(unittest_nr, ovtype);
  2118. switch (ovtype) {
  2119. case PDEV_OVERLAY:
  2120. return of_path_platform_device_exists(path);
  2121. case I2C_OVERLAY:
  2122. return of_path_i2c_client_exists(path);
  2123. }
  2124. return 0;
  2125. }
  2126. static const char *overlay_name_from_nr(int nr)
  2127. {
  2128. static char buf[256];
  2129. snprintf(buf, sizeof(buf) - 1,
  2130. "overlay_%d", nr);
  2131. buf[sizeof(buf) - 1] = '\0';
  2132. return buf;
  2133. }
  2134. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  2135. #define MAX_TRACK_OVCS_IDS 256
  2136. static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
  2137. static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
  2138. static int track_ovcs_id_cnt;
  2139. static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
  2140. {
  2141. if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
  2142. return;
  2143. track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
  2144. track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
  2145. track_ovcs_id_cnt++;
  2146. }
  2147. static void of_unittest_untrack_overlay(int ovcs_id)
  2148. {
  2149. if (WARN_ON(track_ovcs_id_cnt < 1))
  2150. return;
  2151. track_ovcs_id_cnt--;
  2152. /* If out of synch then test is broken. Do not try to recover. */
  2153. WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
  2154. }
  2155. static void of_unittest_remove_tracked_overlays(void)
  2156. {
  2157. int ret, ovcs_id, overlay_nr, save_ovcs_id;
  2158. const char *overlay_name;
  2159. while (track_ovcs_id_cnt > 0) {
  2160. ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
  2161. overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
  2162. save_ovcs_id = ovcs_id;
  2163. ret = of_overlay_remove(&ovcs_id);
  2164. if (ret == -ENODEV) {
  2165. overlay_name = overlay_name_from_nr(overlay_nr);
  2166. pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
  2167. __func__, overlay_name, ret);
  2168. }
  2169. of_unittest_untrack_overlay(save_ovcs_id);
  2170. }
  2171. }
  2172. static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
  2173. {
  2174. /*
  2175. * The overlay will be tracked, thus it will be removed
  2176. * by of_unittest_remove_tracked_overlays().
  2177. */
  2178. const char *overlay_name;
  2179. overlay_name = overlay_name_from_nr(overlay_nr);
  2180. if (!overlay_data_apply(overlay_name, ovcs_id)) {
  2181. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2182. return -EFAULT;
  2183. }
  2184. of_unittest_track_overlay(*ovcs_id, overlay_nr);
  2185. return 0;
  2186. }
  2187. static int __init __of_unittest_apply_overlay_check(int overlay_nr,
  2188. int unittest_nr, int before, int after,
  2189. enum overlay_type ovtype)
  2190. {
  2191. int ret, ovcs_id;
  2192. /* unittest device must be in before state */
  2193. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  2194. unittest(0, "%s with device @\"%s\" %s\n",
  2195. overlay_name_from_nr(overlay_nr),
  2196. unittest_path(unittest_nr, ovtype),
  2197. !before ? "enabled" : "disabled");
  2198. return -EINVAL;
  2199. }
  2200. /* apply the overlay */
  2201. ovcs_id = 0;
  2202. ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
  2203. if (ret != 0) {
  2204. /* of_unittest_apply_overlay already called unittest() */
  2205. return ret;
  2206. }
  2207. /* unittest device must be in after state */
  2208. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  2209. unittest(0, "%s with device @\"%s\" %s\n",
  2210. overlay_name_from_nr(overlay_nr),
  2211. unittest_path(unittest_nr, ovtype),
  2212. !after ? "enabled" : "disabled");
  2213. return -EINVAL;
  2214. }
  2215. return ovcs_id;
  2216. }
  2217. /* apply an overlay while checking before and after states */
  2218. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  2219. int unittest_nr, int before, int after,
  2220. enum overlay_type ovtype)
  2221. {
  2222. int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
  2223. unittest_nr, before, after, ovtype);
  2224. if (ovcs_id < 0)
  2225. return ovcs_id;
  2226. return 0;
  2227. }
  2228. /* apply an overlay and then revert it while checking before, after states */
  2229. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  2230. int unittest_nr, int before, int after,
  2231. enum overlay_type ovtype)
  2232. {
  2233. int ret, ovcs_id, save_ovcs_id;
  2234. ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
  2235. before, after, ovtype);
  2236. if (ovcs_id < 0)
  2237. return ovcs_id;
  2238. /* remove the overlay */
  2239. save_ovcs_id = ovcs_id;
  2240. ret = of_overlay_remove(&ovcs_id);
  2241. if (ret != 0) {
  2242. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  2243. overlay_name_from_nr(overlay_nr),
  2244. unittest_path(unittest_nr, ovtype));
  2245. return ret;
  2246. }
  2247. of_unittest_untrack_overlay(save_ovcs_id);
  2248. /* unittest device must be again in before state */
  2249. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  2250. unittest(0, "%s with device @\"%s\" %s\n",
  2251. overlay_name_from_nr(overlay_nr),
  2252. unittest_path(unittest_nr, ovtype),
  2253. !before ? "enabled" : "disabled");
  2254. return -EINVAL;
  2255. }
  2256. return 0;
  2257. }
  2258. /* test activation of device */
  2259. static void __init of_unittest_overlay_0(void)
  2260. {
  2261. int ret;
  2262. EXPECT_BEGIN(KERN_INFO,
  2263. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
  2264. /* device should enable */
  2265. ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
  2266. EXPECT_END(KERN_INFO,
  2267. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
  2268. if (ret)
  2269. return;
  2270. unittest(1, "overlay test %d passed\n", 0);
  2271. }
  2272. /* test deactivation of device */
  2273. static void __init of_unittest_overlay_1(void)
  2274. {
  2275. int ret;
  2276. EXPECT_BEGIN(KERN_INFO,
  2277. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
  2278. /* device should disable */
  2279. ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
  2280. EXPECT_END(KERN_INFO,
  2281. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
  2282. if (ret)
  2283. return;
  2284. unittest(1, "overlay test %d passed\n", 1);
  2285. }
  2286. /* test activation of device */
  2287. static void __init of_unittest_overlay_2(void)
  2288. {
  2289. int ret;
  2290. EXPECT_BEGIN(KERN_INFO,
  2291. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
  2292. /* device should enable */
  2293. ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
  2294. EXPECT_END(KERN_INFO,
  2295. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
  2296. if (ret)
  2297. return;
  2298. unittest(1, "overlay test %d passed\n", 2);
  2299. }
  2300. /* test deactivation of device */
  2301. static void __init of_unittest_overlay_3(void)
  2302. {
  2303. int ret;
  2304. EXPECT_BEGIN(KERN_INFO,
  2305. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
  2306. /* device should disable */
  2307. ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
  2308. EXPECT_END(KERN_INFO,
  2309. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
  2310. if (ret)
  2311. return;
  2312. unittest(1, "overlay test %d passed\n", 3);
  2313. }
  2314. /* test activation of a full device node */
  2315. static void __init of_unittest_overlay_4(void)
  2316. {
  2317. /* device should disable */
  2318. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  2319. return;
  2320. unittest(1, "overlay test %d passed\n", 4);
  2321. }
  2322. /* test overlay apply/revert sequence */
  2323. static void __init of_unittest_overlay_5(void)
  2324. {
  2325. int ret;
  2326. EXPECT_BEGIN(KERN_INFO,
  2327. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
  2328. /* device should disable */
  2329. ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
  2330. EXPECT_END(KERN_INFO,
  2331. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
  2332. if (ret)
  2333. return;
  2334. unittest(1, "overlay test %d passed\n", 5);
  2335. }
  2336. /* test overlay application in sequence */
  2337. static void __init of_unittest_overlay_6(void)
  2338. {
  2339. int i, save_ovcs_id[2], ovcs_id;
  2340. int overlay_nr = 6, unittest_nr = 6;
  2341. int before = 0, after = 1;
  2342. const char *overlay_name;
  2343. int ret;
  2344. /* unittest device must be in before state */
  2345. for (i = 0; i < 2; i++) {
  2346. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  2347. != before) {
  2348. unittest(0, "%s with device @\"%s\" %s\n",
  2349. overlay_name_from_nr(overlay_nr + i),
  2350. unittest_path(unittest_nr + i,
  2351. PDEV_OVERLAY),
  2352. !before ? "enabled" : "disabled");
  2353. return;
  2354. }
  2355. }
  2356. /* apply the overlays */
  2357. EXPECT_BEGIN(KERN_INFO,
  2358. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
  2359. overlay_name = overlay_name_from_nr(overlay_nr + 0);
  2360. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2361. if (!ret) {
  2362. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2363. return;
  2364. }
  2365. save_ovcs_id[0] = ovcs_id;
  2366. of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
  2367. EXPECT_END(KERN_INFO,
  2368. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
  2369. EXPECT_BEGIN(KERN_INFO,
  2370. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
  2371. overlay_name = overlay_name_from_nr(overlay_nr + 1);
  2372. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2373. if (!ret) {
  2374. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2375. return;
  2376. }
  2377. save_ovcs_id[1] = ovcs_id;
  2378. of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
  2379. EXPECT_END(KERN_INFO,
  2380. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
  2381. for (i = 0; i < 2; i++) {
  2382. /* unittest device must be in after state */
  2383. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  2384. != after) {
  2385. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  2386. overlay_name_from_nr(overlay_nr + i),
  2387. unittest_path(unittest_nr + i,
  2388. PDEV_OVERLAY),
  2389. !after ? "enabled" : "disabled");
  2390. return;
  2391. }
  2392. }
  2393. for (i = 1; i >= 0; i--) {
  2394. ovcs_id = save_ovcs_id[i];
  2395. if (of_overlay_remove(&ovcs_id)) {
  2396. unittest(0, "%s failed destroy @\"%s\"\n",
  2397. overlay_name_from_nr(overlay_nr + i),
  2398. unittest_path(unittest_nr + i,
  2399. PDEV_OVERLAY));
  2400. return;
  2401. }
  2402. of_unittest_untrack_overlay(save_ovcs_id[i]);
  2403. }
  2404. for (i = 0; i < 2; i++) {
  2405. /* unittest device must be again in before state */
  2406. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  2407. != before) {
  2408. unittest(0, "%s with device @\"%s\" %s\n",
  2409. overlay_name_from_nr(overlay_nr + i),
  2410. unittest_path(unittest_nr + i,
  2411. PDEV_OVERLAY),
  2412. !before ? "enabled" : "disabled");
  2413. return;
  2414. }
  2415. }
  2416. unittest(1, "overlay test %d passed\n", 6);
  2417. }
  2418. /* test overlay application in sequence */
  2419. static void __init of_unittest_overlay_8(void)
  2420. {
  2421. int i, save_ovcs_id[2], ovcs_id;
  2422. int overlay_nr = 8, unittest_nr = 8;
  2423. const char *overlay_name;
  2424. int ret;
  2425. /* we don't care about device state in this test */
  2426. EXPECT_BEGIN(KERN_INFO,
  2427. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
  2428. overlay_name = overlay_name_from_nr(overlay_nr + 0);
  2429. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2430. if (!ret)
  2431. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2432. EXPECT_END(KERN_INFO,
  2433. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
  2434. if (!ret)
  2435. return;
  2436. save_ovcs_id[0] = ovcs_id;
  2437. of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
  2438. overlay_name = overlay_name_from_nr(overlay_nr + 1);
  2439. EXPECT_BEGIN(KERN_INFO,
  2440. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
  2441. /* apply the overlays */
  2442. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2443. EXPECT_END(KERN_INFO,
  2444. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
  2445. if (!ret) {
  2446. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2447. return;
  2448. }
  2449. save_ovcs_id[1] = ovcs_id;
  2450. of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
  2451. /* now try to remove first overlay (it should fail) */
  2452. ovcs_id = save_ovcs_id[0];
  2453. EXPECT_BEGIN(KERN_INFO,
  2454. "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
  2455. EXPECT_BEGIN(KERN_INFO,
  2456. "OF: overlay: overlay #6 is not topmost");
  2457. ret = of_overlay_remove(&ovcs_id);
  2458. EXPECT_END(KERN_INFO,
  2459. "OF: overlay: overlay #6 is not topmost");
  2460. EXPECT_END(KERN_INFO,
  2461. "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
  2462. if (!ret) {
  2463. /*
  2464. * Should never get here. If we do, expect a lot of
  2465. * subsequent tracking and overlay removal related errors.
  2466. */
  2467. unittest(0, "%s was destroyed @\"%s\"\n",
  2468. overlay_name_from_nr(overlay_nr + 0),
  2469. unittest_path(unittest_nr,
  2470. PDEV_OVERLAY));
  2471. return;
  2472. }
  2473. /* removing them in order should work */
  2474. for (i = 1; i >= 0; i--) {
  2475. ovcs_id = save_ovcs_id[i];
  2476. if (of_overlay_remove(&ovcs_id)) {
  2477. unittest(0, "%s not destroyed @\"%s\"\n",
  2478. overlay_name_from_nr(overlay_nr + i),
  2479. unittest_path(unittest_nr,
  2480. PDEV_OVERLAY));
  2481. return;
  2482. }
  2483. of_unittest_untrack_overlay(save_ovcs_id[i]);
  2484. }
  2485. unittest(1, "overlay test %d passed\n", 8);
  2486. }
  2487. /* test insertion of a bus with parent devices */
  2488. static void __init of_unittest_overlay_10(void)
  2489. {
  2490. int ret;
  2491. char *child_path;
  2492. /* device should disable */
  2493. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  2494. if (unittest(ret == 0,
  2495. "overlay test %d failed; overlay application\n", 10))
  2496. return;
  2497. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  2498. unittest_path(10, PDEV_OVERLAY));
  2499. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  2500. return;
  2501. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  2502. kfree(child_path);
  2503. unittest(ret, "overlay test %d failed; no child device\n", 10);
  2504. }
  2505. /* test insertion of a bus with parent devices (and revert) */
  2506. static void __init of_unittest_overlay_11(void)
  2507. {
  2508. int ret;
  2509. /* device should disable */
  2510. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  2511. PDEV_OVERLAY);
  2512. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  2513. }
  2514. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  2515. struct unittest_i2c_bus_data {
  2516. struct platform_device *pdev;
  2517. struct i2c_adapter adap;
  2518. };
  2519. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  2520. struct i2c_msg *msgs, int num)
  2521. {
  2522. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  2523. (void)std;
  2524. return num;
  2525. }
  2526. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  2527. {
  2528. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  2529. }
  2530. static const struct i2c_algorithm unittest_i2c_algo = {
  2531. .master_xfer = unittest_i2c_master_xfer,
  2532. .functionality = unittest_i2c_functionality,
  2533. };
  2534. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  2535. {
  2536. struct device *dev = &pdev->dev;
  2537. struct device_node *np = dev->of_node;
  2538. struct unittest_i2c_bus_data *std;
  2539. struct i2c_adapter *adap;
  2540. int ret;
  2541. if (np == NULL) {
  2542. dev_err(dev, "No OF data for device\n");
  2543. return -EINVAL;
  2544. }
  2545. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2546. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  2547. if (!std)
  2548. return -ENOMEM;
  2549. /* link them together */
  2550. std->pdev = pdev;
  2551. platform_set_drvdata(pdev, std);
  2552. adap = &std->adap;
  2553. i2c_set_adapdata(adap, std);
  2554. adap->nr = -1;
  2555. strscpy(adap->name, pdev->name, sizeof(adap->name));
  2556. adap->class = I2C_CLASS_DEPRECATED;
  2557. adap->algo = &unittest_i2c_algo;
  2558. adap->dev.parent = dev;
  2559. adap->dev.of_node = dev->of_node;
  2560. adap->timeout = 5 * HZ;
  2561. adap->retries = 3;
  2562. ret = i2c_add_numbered_adapter(adap);
  2563. if (ret != 0) {
  2564. dev_err(dev, "Failed to add I2C adapter\n");
  2565. return ret;
  2566. }
  2567. return 0;
  2568. }
  2569. static void unittest_i2c_bus_remove(struct platform_device *pdev)
  2570. {
  2571. struct device *dev = &pdev->dev;
  2572. struct device_node *np = dev->of_node;
  2573. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  2574. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2575. i2c_del_adapter(&std->adap);
  2576. }
  2577. static const struct of_device_id unittest_i2c_bus_match[] = {
  2578. { .compatible = "unittest-i2c-bus", },
  2579. {},
  2580. };
  2581. static struct platform_driver unittest_i2c_bus_driver = {
  2582. .probe = unittest_i2c_bus_probe,
  2583. .remove = unittest_i2c_bus_remove,
  2584. .driver = {
  2585. .name = "unittest-i2c-bus",
  2586. .of_match_table = unittest_i2c_bus_match,
  2587. },
  2588. };
  2589. static int unittest_i2c_dev_probe(struct i2c_client *client)
  2590. {
  2591. struct device *dev = &client->dev;
  2592. struct device_node *np = client->dev.of_node;
  2593. if (!np) {
  2594. dev_err(dev, "No OF node\n");
  2595. return -EINVAL;
  2596. }
  2597. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2598. return 0;
  2599. };
  2600. static void unittest_i2c_dev_remove(struct i2c_client *client)
  2601. {
  2602. struct device *dev = &client->dev;
  2603. struct device_node *np = client->dev.of_node;
  2604. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2605. }
  2606. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  2607. { .name = "unittest-i2c-dev" },
  2608. { }
  2609. };
  2610. static struct i2c_driver unittest_i2c_dev_driver = {
  2611. .driver = {
  2612. .name = "unittest-i2c-dev",
  2613. },
  2614. .probe = unittest_i2c_dev_probe,
  2615. .remove = unittest_i2c_dev_remove,
  2616. .id_table = unittest_i2c_dev_id,
  2617. };
  2618. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2619. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  2620. {
  2621. return 0;
  2622. }
  2623. static int unittest_i2c_mux_probe(struct i2c_client *client)
  2624. {
  2625. int i, nchans;
  2626. struct device *dev = &client->dev;
  2627. struct i2c_adapter *adap = client->adapter;
  2628. struct device_node *np = client->dev.of_node, *child;
  2629. struct i2c_mux_core *muxc;
  2630. u32 reg, max_reg;
  2631. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2632. if (!np) {
  2633. dev_err(dev, "No OF node\n");
  2634. return -EINVAL;
  2635. }
  2636. max_reg = (u32)-1;
  2637. for_each_child_of_node(np, child) {
  2638. if (of_property_read_u32(child, "reg", &reg))
  2639. continue;
  2640. if (max_reg == (u32)-1 || reg > max_reg)
  2641. max_reg = reg;
  2642. }
  2643. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  2644. if (nchans == 0) {
  2645. dev_err(dev, "No channels\n");
  2646. return -EINVAL;
  2647. }
  2648. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  2649. unittest_i2c_mux_select_chan, NULL);
  2650. if (!muxc)
  2651. return -ENOMEM;
  2652. for (i = 0; i < nchans; i++) {
  2653. if (i2c_mux_add_adapter(muxc, 0, i)) {
  2654. dev_err(dev, "Failed to register mux #%d\n", i);
  2655. i2c_mux_del_adapters(muxc);
  2656. return -ENODEV;
  2657. }
  2658. }
  2659. i2c_set_clientdata(client, muxc);
  2660. return 0;
  2661. };
  2662. static void unittest_i2c_mux_remove(struct i2c_client *client)
  2663. {
  2664. struct device *dev = &client->dev;
  2665. struct device_node *np = client->dev.of_node;
  2666. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  2667. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2668. i2c_mux_del_adapters(muxc);
  2669. }
  2670. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  2671. { .name = "unittest-i2c-mux" },
  2672. { }
  2673. };
  2674. static struct i2c_driver unittest_i2c_mux_driver = {
  2675. .driver = {
  2676. .name = "unittest-i2c-mux",
  2677. },
  2678. .probe = unittest_i2c_mux_probe,
  2679. .remove = unittest_i2c_mux_remove,
  2680. .id_table = unittest_i2c_mux_id,
  2681. };
  2682. #endif
  2683. static int of_unittest_overlay_i2c_init(void)
  2684. {
  2685. int ret;
  2686. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  2687. if (unittest(ret == 0,
  2688. "could not register unittest i2c device driver\n"))
  2689. return ret;
  2690. ret = platform_driver_register(&unittest_i2c_bus_driver);
  2691. if (unittest(ret == 0,
  2692. "could not register unittest i2c bus driver\n"))
  2693. return ret;
  2694. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2695. EXPECT_BEGIN(KERN_INFO,
  2696. "i2c i2c-1: Added multiplexed i2c bus 2");
  2697. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  2698. EXPECT_END(KERN_INFO,
  2699. "i2c i2c-1: Added multiplexed i2c bus 2");
  2700. if (unittest(ret == 0,
  2701. "could not register unittest i2c mux driver\n"))
  2702. return ret;
  2703. #endif
  2704. return 0;
  2705. }
  2706. static void of_unittest_overlay_i2c_cleanup(void)
  2707. {
  2708. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2709. i2c_del_driver(&unittest_i2c_mux_driver);
  2710. #endif
  2711. platform_driver_unregister(&unittest_i2c_bus_driver);
  2712. i2c_del_driver(&unittest_i2c_dev_driver);
  2713. }
  2714. static void __init of_unittest_overlay_i2c_12(void)
  2715. {
  2716. int ret;
  2717. /* device should enable */
  2718. EXPECT_BEGIN(KERN_INFO,
  2719. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
  2720. ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
  2721. EXPECT_END(KERN_INFO,
  2722. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
  2723. if (ret)
  2724. return;
  2725. unittest(1, "overlay test %d passed\n", 12);
  2726. }
  2727. /* test deactivation of device */
  2728. static void __init of_unittest_overlay_i2c_13(void)
  2729. {
  2730. int ret;
  2731. EXPECT_BEGIN(KERN_INFO,
  2732. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
  2733. /* device should disable */
  2734. ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
  2735. EXPECT_END(KERN_INFO,
  2736. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
  2737. if (ret)
  2738. return;
  2739. unittest(1, "overlay test %d passed\n", 13);
  2740. }
  2741. /* just check for i2c mux existence */
  2742. static void of_unittest_overlay_i2c_14(void)
  2743. {
  2744. }
  2745. static void __init of_unittest_overlay_i2c_15(void)
  2746. {
  2747. int ret;
  2748. /* device should enable */
  2749. EXPECT_BEGIN(KERN_INFO,
  2750. "i2c i2c-1: Added multiplexed i2c bus 3");
  2751. ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
  2752. EXPECT_END(KERN_INFO,
  2753. "i2c i2c-1: Added multiplexed i2c bus 3");
  2754. if (ret)
  2755. return;
  2756. unittest(1, "overlay test %d passed\n", 15);
  2757. }
  2758. #else
  2759. static inline void of_unittest_overlay_i2c_14(void) { }
  2760. static inline void of_unittest_overlay_i2c_15(void) { }
  2761. #endif
  2762. static int of_notify(struct notifier_block *nb, unsigned long action,
  2763. void *arg)
  2764. {
  2765. struct of_overlay_notify_data *nd = arg;
  2766. struct device_node *found;
  2767. int ret;
  2768. /*
  2769. * For overlay_16 .. overlay_19, check that returning an error
  2770. * works for each of the actions by setting an arbitrary return
  2771. * error number that matches the test number. e.g. for unittest16,
  2772. * ret = -EBUSY which is -16.
  2773. *
  2774. * OVERLAY_INFO() for the overlays is declared to expect the same
  2775. * error number, so overlay_data_apply() will return no error.
  2776. *
  2777. * overlay_20 will return NOTIFY_DONE
  2778. */
  2779. ret = 0;
  2780. of_node_get(nd->overlay);
  2781. switch (action) {
  2782. case OF_OVERLAY_PRE_APPLY:
  2783. found = of_find_node_by_name(nd->overlay, "test-unittest16");
  2784. if (found) {
  2785. of_node_put(found);
  2786. ret = -EBUSY;
  2787. }
  2788. break;
  2789. case OF_OVERLAY_POST_APPLY:
  2790. found = of_find_node_by_name(nd->overlay, "test-unittest17");
  2791. if (found) {
  2792. of_node_put(found);
  2793. ret = -EEXIST;
  2794. }
  2795. break;
  2796. case OF_OVERLAY_PRE_REMOVE:
  2797. found = of_find_node_by_name(nd->overlay, "test-unittest18");
  2798. if (found) {
  2799. of_node_put(found);
  2800. ret = -EXDEV;
  2801. }
  2802. break;
  2803. case OF_OVERLAY_POST_REMOVE:
  2804. found = of_find_node_by_name(nd->overlay, "test-unittest19");
  2805. if (found) {
  2806. of_node_put(found);
  2807. ret = -ENODEV;
  2808. }
  2809. break;
  2810. default: /* should not happen */
  2811. of_node_put(nd->overlay);
  2812. ret = -EINVAL;
  2813. break;
  2814. }
  2815. if (ret)
  2816. return notifier_from_errno(ret);
  2817. return NOTIFY_DONE;
  2818. }
  2819. static struct notifier_block of_nb = {
  2820. .notifier_call = of_notify,
  2821. };
  2822. static void __init of_unittest_overlay_notify(void)
  2823. {
  2824. int ovcs_id;
  2825. int ret;
  2826. ret = of_overlay_notifier_register(&of_nb);
  2827. unittest(!ret,
  2828. "of_overlay_notifier_register() failed, ret = %d\n", ret);
  2829. if (ret)
  2830. return;
  2831. /*
  2832. * The overlays are applied by overlay_data_apply()
  2833. * instead of of_unittest_apply_overlay() so that they
  2834. * will not be tracked. Thus they will not be removed
  2835. * by of_unittest_remove_tracked_overlays().
  2836. *
  2837. * Applying overlays 16 - 19 will each trigger an error for a
  2838. * different action in of_notify().
  2839. *
  2840. * Applying overlay 20 will not trigger any error in of_notify().
  2841. */
  2842. /* --- overlay 16 --- */
  2843. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
  2844. unittest(overlay_data_apply("overlay_16", &ovcs_id),
  2845. "test OF_OVERLAY_PRE_APPLY notify injected error\n");
  2846. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
  2847. unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
  2848. /* --- overlay 17 --- */
  2849. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
  2850. unittest(overlay_data_apply("overlay_17", &ovcs_id),
  2851. "test OF_OVERLAY_POST_APPLY notify injected error\n");
  2852. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
  2853. unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
  2854. /* --- overlay 18 --- */
  2855. unittest(overlay_data_apply("overlay_18", &ovcs_id),
  2856. "OF_OVERLAY_PRE_REMOVE notify injected error\n");
  2857. unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
  2858. if (ovcs_id) {
  2859. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
  2860. ret = of_overlay_remove(&ovcs_id);
  2861. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
  2862. if (ret == -EXDEV) {
  2863. /*
  2864. * change set ovcs_id should still exist
  2865. */
  2866. unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
  2867. } else {
  2868. unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
  2869. }
  2870. } else {
  2871. unittest(1, "ovcs_id not created for overlay_18\n");
  2872. }
  2873. unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
  2874. /* --- overlay 19 --- */
  2875. unittest(overlay_data_apply("overlay_19", &ovcs_id),
  2876. "OF_OVERLAY_POST_REMOVE notify injected error\n");
  2877. unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
  2878. if (ovcs_id) {
  2879. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
  2880. ret = of_overlay_remove(&ovcs_id);
  2881. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
  2882. if (ret == -ENODEV)
  2883. unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
  2884. else
  2885. unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
  2886. } else {
  2887. unittest(1, "ovcs_id removed for overlay_19\n");
  2888. }
  2889. unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
  2890. ovcs_id);
  2891. /* --- overlay 20 --- */
  2892. unittest(overlay_data_apply("overlay_20", &ovcs_id),
  2893. "overlay notify no injected error\n");
  2894. if (ovcs_id) {
  2895. ret = of_overlay_remove(&ovcs_id);
  2896. if (ret)
  2897. unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
  2898. ret);
  2899. } else {
  2900. unittest(1, "ovcs_id not created for overlay_20\n");
  2901. }
  2902. unittest(!of_overlay_notifier_unregister(&of_nb),
  2903. "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
  2904. }
  2905. static void __init of_unittest_overlay(void)
  2906. {
  2907. struct device_node *bus_np = NULL;
  2908. unsigned int i;
  2909. if (platform_driver_register(&unittest_driver)) {
  2910. unittest(0, "could not register unittest driver\n");
  2911. goto out;
  2912. }
  2913. bus_np = of_find_node_by_path(bus_path);
  2914. if (bus_np == NULL) {
  2915. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  2916. goto out;
  2917. }
  2918. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  2919. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  2920. goto out;
  2921. }
  2922. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  2923. unittest(0, "could not find unittest0 @ \"%s\"\n",
  2924. unittest_path(100, PDEV_OVERLAY));
  2925. goto out;
  2926. }
  2927. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  2928. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  2929. unittest_path(101, PDEV_OVERLAY));
  2930. goto out;
  2931. }
  2932. unittest(1, "basic infrastructure of overlays passed");
  2933. /* tests in sequence */
  2934. of_unittest_overlay_0();
  2935. of_unittest_overlay_1();
  2936. of_unittest_overlay_2();
  2937. of_unittest_overlay_3();
  2938. of_unittest_overlay_4();
  2939. for (i = 0; i < 3; i++)
  2940. of_unittest_overlay_5();
  2941. of_unittest_overlay_6();
  2942. of_unittest_overlay_8();
  2943. of_unittest_overlay_10();
  2944. of_unittest_overlay_11();
  2945. #if IS_BUILTIN(CONFIG_I2C)
  2946. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  2947. goto out;
  2948. of_unittest_overlay_i2c_12();
  2949. of_unittest_overlay_i2c_13();
  2950. of_unittest_overlay_i2c_14();
  2951. of_unittest_overlay_i2c_15();
  2952. of_unittest_overlay_i2c_cleanup();
  2953. #endif
  2954. of_unittest_overlay_gpio();
  2955. of_unittest_remove_tracked_overlays();
  2956. of_unittest_overlay_notify();
  2957. out:
  2958. of_node_put(bus_np);
  2959. }
  2960. #else
  2961. static inline void __init of_unittest_overlay(void) { }
  2962. #endif
  2963. static void __init of_unittest_lifecycle(void)
  2964. {
  2965. #ifdef CONFIG_OF_DYNAMIC
  2966. unsigned int refcount;
  2967. int found_refcount_one = 0;
  2968. int put_count = 0;
  2969. struct device_node *np;
  2970. struct device_node *prev_sibling, *next_sibling;
  2971. const char *refcount_path = "/testcase-data/refcount-node";
  2972. const char *refcount_parent_path = "/testcase-data";
  2973. /*
  2974. * Node lifecycle tests, non-dynamic node:
  2975. *
  2976. * - Decrementing refcount to zero via of_node_put() should cause the
  2977. * attempt to free the node memory by of_node_release() to fail
  2978. * because the node is not a dynamic node.
  2979. *
  2980. * - Decrementing refcount past zero should result in additional
  2981. * errors reported.
  2982. */
  2983. np = of_find_node_by_path(refcount_path);
  2984. unittest(np, "find refcount_path \"%s\"\n", refcount_path);
  2985. if (np == NULL)
  2986. goto out_skip_tests;
  2987. while (!found_refcount_one) {
  2988. if (put_count++ > 10) {
  2989. unittest(0, "guardrail to avoid infinite loop\n");
  2990. goto out_skip_tests;
  2991. }
  2992. refcount = kref_read(&np->kobj.kref);
  2993. if (refcount == 1)
  2994. found_refcount_one = 1;
  2995. else
  2996. of_node_put(np);
  2997. }
  2998. EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
  2999. /*
  3000. * refcount is now one, decrementing to zero will result in a call to
  3001. * of_node_release() to free the node's memory, which should result
  3002. * in an error
  3003. */
  3004. unittest(1, "/testcase-data/refcount-node is one");
  3005. of_node_put(np);
  3006. EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
  3007. /*
  3008. * expect stack trace for subsequent of_node_put():
  3009. * __refcount_sub_and_test() calls:
  3010. * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
  3011. *
  3012. * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
  3013. * the first three lines, and the last line.
  3014. */
  3015. EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
  3016. EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
  3017. EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
  3018. EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
  3019. /* refcount is now zero, this should fail */
  3020. unittest(1, "/testcase-data/refcount-node is zero");
  3021. of_node_put(np);
  3022. EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
  3023. EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
  3024. EXPECT_END(KERN_INFO, "WARNING: <<all>>");
  3025. EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
  3026. /*
  3027. * Q. do we expect to get yet another warning?
  3028. * A. no, the WARNING is from WARN_ONCE()
  3029. */
  3030. EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
  3031. EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
  3032. EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
  3033. EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
  3034. unittest(1, "/testcase-data/refcount-node is zero, second time");
  3035. of_node_put(np);
  3036. EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
  3037. EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
  3038. EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
  3039. EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
  3040. /*
  3041. * refcount of zero will trigger stack traces from any further
  3042. * attempt to of_node_get() node "refcount-node". One example of
  3043. * this is where of_unittest_check_node_linkage() will recursively
  3044. * scan the tree, with 'for_each_child_of_node()' doing an
  3045. * of_node_get() of the children of a node.
  3046. *
  3047. * Prevent the stack trace by removing node "refcount-node" from
  3048. * its parent's child list.
  3049. *
  3050. * WARNING: EVIL, EVIL, EVIL:
  3051. *
  3052. * Directly manipulate the child list of node /testcase-data to
  3053. * remove child refcount-node. This is ignoring all proper methods
  3054. * of removing a child and will leak a small amount of memory.
  3055. */
  3056. np = of_find_node_by_path(refcount_parent_path);
  3057. unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
  3058. unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
  3059. if (np == NULL)
  3060. return;
  3061. prev_sibling = np->child;
  3062. next_sibling = prev_sibling->sibling;
  3063. if (!strcmp(prev_sibling->full_name, "refcount-node")) {
  3064. np->child = next_sibling;
  3065. next_sibling = next_sibling->sibling;
  3066. }
  3067. while (next_sibling) {
  3068. if (!strcmp(next_sibling->full_name, "refcount-node"))
  3069. prev_sibling->sibling = next_sibling->sibling;
  3070. prev_sibling = next_sibling;
  3071. next_sibling = next_sibling->sibling;
  3072. }
  3073. of_node_put(np);
  3074. return;
  3075. out_skip_tests:
  3076. #endif
  3077. unittest(0, "One or more lifecycle tests skipped\n");
  3078. }
  3079. #ifdef CONFIG_OF_OVERLAY
  3080. /*
  3081. * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
  3082. * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
  3083. */
  3084. #define OVERLAY_INFO_EXTERN(overlay_name) \
  3085. extern uint8_t __dtbo_##overlay_name##_begin[]; \
  3086. extern uint8_t __dtbo_##overlay_name##_end[]
  3087. #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
  3088. { .dtbo_begin = __dtbo_##overlay_name##_begin, \
  3089. .dtbo_end = __dtbo_##overlay_name##_end, \
  3090. .expected_result = expected, \
  3091. .expected_result_remove = expected_remove, \
  3092. .name = #overlay_name, \
  3093. }
  3094. struct overlay_info {
  3095. uint8_t *dtbo_begin;
  3096. uint8_t *dtbo_end;
  3097. int expected_result;
  3098. int expected_result_remove; /* if apply failed */
  3099. int ovcs_id;
  3100. char *name;
  3101. };
  3102. OVERLAY_INFO_EXTERN(overlay_base);
  3103. OVERLAY_INFO_EXTERN(overlay);
  3104. OVERLAY_INFO_EXTERN(overlay_0);
  3105. OVERLAY_INFO_EXTERN(overlay_1);
  3106. OVERLAY_INFO_EXTERN(overlay_2);
  3107. OVERLAY_INFO_EXTERN(overlay_3);
  3108. OVERLAY_INFO_EXTERN(overlay_4);
  3109. OVERLAY_INFO_EXTERN(overlay_5);
  3110. OVERLAY_INFO_EXTERN(overlay_6);
  3111. OVERLAY_INFO_EXTERN(overlay_7);
  3112. OVERLAY_INFO_EXTERN(overlay_8);
  3113. OVERLAY_INFO_EXTERN(overlay_9);
  3114. OVERLAY_INFO_EXTERN(overlay_10);
  3115. OVERLAY_INFO_EXTERN(overlay_11);
  3116. OVERLAY_INFO_EXTERN(overlay_12);
  3117. OVERLAY_INFO_EXTERN(overlay_13);
  3118. OVERLAY_INFO_EXTERN(overlay_15);
  3119. OVERLAY_INFO_EXTERN(overlay_16);
  3120. OVERLAY_INFO_EXTERN(overlay_17);
  3121. OVERLAY_INFO_EXTERN(overlay_18);
  3122. OVERLAY_INFO_EXTERN(overlay_19);
  3123. OVERLAY_INFO_EXTERN(overlay_20);
  3124. OVERLAY_INFO_EXTERN(overlay_gpio_01);
  3125. OVERLAY_INFO_EXTERN(overlay_gpio_02a);
  3126. OVERLAY_INFO_EXTERN(overlay_gpio_02b);
  3127. OVERLAY_INFO_EXTERN(overlay_gpio_03);
  3128. OVERLAY_INFO_EXTERN(overlay_gpio_04a);
  3129. OVERLAY_INFO_EXTERN(overlay_gpio_04b);
  3130. OVERLAY_INFO_EXTERN(overlay_pci_node);
  3131. OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
  3132. OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
  3133. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  3134. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  3135. OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
  3136. /* entries found by name */
  3137. static struct overlay_info overlays[] = {
  3138. OVERLAY_INFO(overlay_base, -9999, 0),
  3139. OVERLAY_INFO(overlay, 0, 0),
  3140. OVERLAY_INFO(overlay_0, 0, 0),
  3141. OVERLAY_INFO(overlay_1, 0, 0),
  3142. OVERLAY_INFO(overlay_2, 0, 0),
  3143. OVERLAY_INFO(overlay_3, 0, 0),
  3144. OVERLAY_INFO(overlay_4, 0, 0),
  3145. OVERLAY_INFO(overlay_5, 0, 0),
  3146. OVERLAY_INFO(overlay_6, 0, 0),
  3147. OVERLAY_INFO(overlay_7, 0, 0),
  3148. OVERLAY_INFO(overlay_8, 0, 0),
  3149. OVERLAY_INFO(overlay_9, 0, 0),
  3150. OVERLAY_INFO(overlay_10, 0, 0),
  3151. OVERLAY_INFO(overlay_11, 0, 0),
  3152. OVERLAY_INFO(overlay_12, 0, 0),
  3153. OVERLAY_INFO(overlay_13, 0, 0),
  3154. OVERLAY_INFO(overlay_15, 0, 0),
  3155. OVERLAY_INFO(overlay_16, -EBUSY, 0),
  3156. OVERLAY_INFO(overlay_17, -EEXIST, 0),
  3157. OVERLAY_INFO(overlay_18, 0, 0),
  3158. OVERLAY_INFO(overlay_19, 0, 0),
  3159. OVERLAY_INFO(overlay_20, 0, 0),
  3160. OVERLAY_INFO(overlay_gpio_01, 0, 0),
  3161. OVERLAY_INFO(overlay_gpio_02a, 0, 0),
  3162. OVERLAY_INFO(overlay_gpio_02b, 0, 0),
  3163. OVERLAY_INFO(overlay_gpio_03, 0, 0),
  3164. OVERLAY_INFO(overlay_gpio_04a, 0, 0),
  3165. OVERLAY_INFO(overlay_gpio_04b, 0, 0),
  3166. OVERLAY_INFO(overlay_pci_node, 0, 0),
  3167. OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
  3168. OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
  3169. OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
  3170. OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
  3171. OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
  3172. /* end marker */
  3173. { }
  3174. };
  3175. static struct device_node *overlay_base_root;
  3176. static void * __init dt_alloc_memory(u64 size, u64 align)
  3177. {
  3178. return memblock_alloc_or_panic(size, align);
  3179. }
  3180. /*
  3181. * Create base device tree for the overlay unittest.
  3182. *
  3183. * This is called from very early boot code.
  3184. *
  3185. * Do as much as possible the same way as done in __unflatten_device_tree
  3186. * and other early boot steps for the normal FDT so that the overlay base
  3187. * unflattened tree will have the same characteristics as the real tree
  3188. * (such as having memory allocated by the early allocator). The goal
  3189. * is to test "the real thing" as much as possible, and test "test setup
  3190. * code" as little as possible.
  3191. *
  3192. * Have to stop before resolving phandles, because that uses kmalloc.
  3193. */
  3194. void __init unittest_unflatten_overlay_base(void)
  3195. {
  3196. struct overlay_info *info;
  3197. u32 data_size;
  3198. void *new_fdt;
  3199. u32 size;
  3200. int found = 0;
  3201. const char *overlay_name = "overlay_base";
  3202. for (info = overlays; info && info->name; info++) {
  3203. if (!strcmp(overlay_name, info->name)) {
  3204. found = 1;
  3205. break;
  3206. }
  3207. }
  3208. if (!found) {
  3209. pr_err("no overlay data for %s\n", overlay_name);
  3210. return;
  3211. }
  3212. info = &overlays[0];
  3213. if (info->expected_result != -9999) {
  3214. pr_err("No dtb 'overlay_base' to attach\n");
  3215. return;
  3216. }
  3217. data_size = info->dtbo_end - info->dtbo_begin;
  3218. if (!data_size) {
  3219. pr_err("No dtb 'overlay_base' to attach\n");
  3220. return;
  3221. }
  3222. size = fdt_totalsize(info->dtbo_begin);
  3223. if (size != data_size) {
  3224. pr_err("dtb 'overlay_base' header totalsize != actual size");
  3225. return;
  3226. }
  3227. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  3228. if (!new_fdt) {
  3229. pr_err("alloc for dtb 'overlay_base' failed");
  3230. return;
  3231. }
  3232. memcpy(new_fdt, info->dtbo_begin, size);
  3233. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  3234. dt_alloc_memory, true);
  3235. }
  3236. /*
  3237. * The purpose of of_unittest_overlay_data_add is to add an
  3238. * overlay in the normal fashion. This is a test of the whole
  3239. * picture, instead of testing individual elements.
  3240. *
  3241. * A secondary purpose is to be able to verify that the contents of
  3242. * /proc/device-tree/ contains the updated structure and values from
  3243. * the overlay. That must be verified separately in user space.
  3244. *
  3245. * Return 0 on unexpected error.
  3246. */
  3247. static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
  3248. {
  3249. struct overlay_info *info;
  3250. int passed = 1;
  3251. int found = 0;
  3252. int ret, ret2;
  3253. u32 size;
  3254. for (info = overlays; info && info->name; info++) {
  3255. if (!strcmp(overlay_name, info->name)) {
  3256. found = 1;
  3257. break;
  3258. }
  3259. }
  3260. if (!found) {
  3261. pr_err("no overlay data for %s\n", overlay_name);
  3262. return 0;
  3263. }
  3264. size = info->dtbo_end - info->dtbo_begin;
  3265. if (!size)
  3266. pr_err("no overlay data for %s\n", overlay_name);
  3267. ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
  3268. NULL);
  3269. if (ovcs_id)
  3270. *ovcs_id = info->ovcs_id;
  3271. if (ret < 0)
  3272. goto out;
  3273. pr_debug("%s applied\n", overlay_name);
  3274. out:
  3275. if (ret != info->expected_result) {
  3276. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  3277. info->expected_result, ret, overlay_name);
  3278. passed = 0;
  3279. }
  3280. if (ret < 0) {
  3281. /* changeset may be partially applied */
  3282. ret2 = of_overlay_remove(&info->ovcs_id);
  3283. if (ret2 != info->expected_result_remove) {
  3284. pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
  3285. info->expected_result_remove, ret2,
  3286. overlay_name);
  3287. passed = 0;
  3288. }
  3289. }
  3290. return passed;
  3291. }
  3292. /*
  3293. * The purpose of of_unittest_overlay_high_level is to add an overlay
  3294. * in the normal fashion. This is a test of the whole picture,
  3295. * instead of individual elements.
  3296. *
  3297. * The first part of the function is _not_ normal overlay usage; it is
  3298. * finishing splicing the base overlay device tree into the live tree.
  3299. */
  3300. static __init void of_unittest_overlay_high_level(void)
  3301. {
  3302. struct device_node *last_sibling;
  3303. struct device_node *np;
  3304. struct device_node *of_symbols;
  3305. struct device_node *overlay_base_symbols;
  3306. struct device_node **pprev;
  3307. struct property *prop;
  3308. int ret;
  3309. if (!overlay_base_root) {
  3310. unittest(0, "overlay_base_root not initialized\n");
  3311. return;
  3312. }
  3313. /*
  3314. * Could not fixup phandles in unittest_unflatten_overlay_base()
  3315. * because kmalloc() was not yet available.
  3316. */
  3317. of_overlay_mutex_lock();
  3318. of_resolve_phandles(overlay_base_root);
  3319. of_overlay_mutex_unlock();
  3320. /*
  3321. * do not allow overlay_base to duplicate any node already in
  3322. * tree, this greatly simplifies the code
  3323. */
  3324. /*
  3325. * remove overlay_base_root node "__local_fixups", after
  3326. * being used by of_resolve_phandles()
  3327. */
  3328. pprev = &overlay_base_root->child;
  3329. for (np = overlay_base_root->child; np; np = np->sibling) {
  3330. if (of_node_name_eq(np, "__local_fixups__")) {
  3331. *pprev = np->sibling;
  3332. break;
  3333. }
  3334. pprev = &np->sibling;
  3335. }
  3336. /* remove overlay_base_root node "__symbols__" if in live tree */
  3337. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  3338. if (of_symbols) {
  3339. /* will have to graft properties from node into live tree */
  3340. pprev = &overlay_base_root->child;
  3341. for (np = overlay_base_root->child; np; np = np->sibling) {
  3342. if (of_node_name_eq(np, "__symbols__")) {
  3343. overlay_base_symbols = np;
  3344. *pprev = np->sibling;
  3345. break;
  3346. }
  3347. pprev = &np->sibling;
  3348. }
  3349. }
  3350. for_each_child_of_node(overlay_base_root, np) {
  3351. struct device_node *base_child;
  3352. for_each_child_of_node(of_root, base_child) {
  3353. if (!strcmp(np->full_name, base_child->full_name)) {
  3354. unittest(0, "illegal node name in overlay_base %pOFn",
  3355. np);
  3356. of_node_put(np);
  3357. of_node_put(base_child);
  3358. return;
  3359. }
  3360. }
  3361. }
  3362. /*
  3363. * overlay 'overlay_base' is not allowed to have root
  3364. * properties, so only need to splice nodes into main device tree.
  3365. *
  3366. * root node of *overlay_base_root will not be freed, it is lost
  3367. * memory.
  3368. */
  3369. for (np = overlay_base_root->child; np; np = np->sibling)
  3370. np->parent = of_root;
  3371. mutex_lock(&of_mutex);
  3372. for (last_sibling = np = of_root->child; np; np = np->sibling)
  3373. last_sibling = np;
  3374. if (last_sibling)
  3375. last_sibling->sibling = overlay_base_root->child;
  3376. else
  3377. of_root->child = overlay_base_root->child;
  3378. for_each_of_allnodes_from(overlay_base_root, np)
  3379. __of_attach_node_sysfs(np);
  3380. if (of_symbols) {
  3381. struct property *new_prop;
  3382. for_each_property_of_node(overlay_base_symbols, prop) {
  3383. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  3384. if (!new_prop) {
  3385. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  3386. prop->name);
  3387. goto err_unlock;
  3388. }
  3389. if (__of_add_property(of_symbols, new_prop)) {
  3390. __of_prop_free(new_prop);
  3391. /* "name" auto-generated by unflatten */
  3392. if (!strcmp(prop->name, "name"))
  3393. continue;
  3394. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  3395. prop->name);
  3396. goto err_unlock;
  3397. }
  3398. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  3399. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  3400. prop->name);
  3401. goto err_unlock;
  3402. }
  3403. }
  3404. }
  3405. mutex_unlock(&of_mutex);
  3406. /* now do the normal overlay usage test */
  3407. /* --- overlay --- */
  3408. EXPECT_BEGIN(KERN_ERR,
  3409. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
  3410. EXPECT_BEGIN(KERN_ERR,
  3411. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
  3412. EXPECT_BEGIN(KERN_ERR,
  3413. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
  3414. EXPECT_BEGIN(KERN_ERR,
  3415. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
  3416. EXPECT_BEGIN(KERN_ERR,
  3417. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
  3418. EXPECT_BEGIN(KERN_ERR,
  3419. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
  3420. EXPECT_BEGIN(KERN_ERR,
  3421. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
  3422. EXPECT_BEGIN(KERN_ERR,
  3423. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
  3424. EXPECT_BEGIN(KERN_ERR,
  3425. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
  3426. EXPECT_BEGIN(KERN_ERR,
  3427. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
  3428. EXPECT_BEGIN(KERN_ERR,
  3429. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
  3430. ret = overlay_data_apply("overlay", NULL);
  3431. EXPECT_END(KERN_ERR,
  3432. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
  3433. EXPECT_END(KERN_ERR,
  3434. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
  3435. EXPECT_END(KERN_ERR,
  3436. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
  3437. EXPECT_END(KERN_ERR,
  3438. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
  3439. EXPECT_END(KERN_ERR,
  3440. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
  3441. EXPECT_END(KERN_ERR,
  3442. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
  3443. EXPECT_END(KERN_ERR,
  3444. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
  3445. EXPECT_END(KERN_ERR,
  3446. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
  3447. EXPECT_END(KERN_ERR,
  3448. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
  3449. EXPECT_END(KERN_ERR,
  3450. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
  3451. EXPECT_END(KERN_ERR,
  3452. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
  3453. unittest(ret, "Adding overlay 'overlay' failed\n");
  3454. /* --- overlay_bad_add_dup_node --- */
  3455. EXPECT_BEGIN(KERN_ERR,
  3456. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
  3457. EXPECT_BEGIN(KERN_ERR,
  3458. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
  3459. EXPECT_BEGIN(KERN_ERR,
  3460. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
  3461. EXPECT_BEGIN(KERN_ERR,
  3462. "OF: Error reverting changeset (-19)");
  3463. unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
  3464. "Adding overlay 'overlay_bad_add_dup_node' failed\n");
  3465. EXPECT_END(KERN_ERR,
  3466. "OF: Error reverting changeset (-19)");
  3467. EXPECT_END(KERN_ERR,
  3468. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
  3469. EXPECT_END(KERN_ERR,
  3470. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
  3471. EXPECT_END(KERN_ERR,
  3472. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
  3473. /* --- overlay_bad_add_dup_prop --- */
  3474. EXPECT_BEGIN(KERN_ERR,
  3475. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
  3476. EXPECT_BEGIN(KERN_ERR,
  3477. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
  3478. EXPECT_BEGIN(KERN_ERR,
  3479. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
  3480. EXPECT_BEGIN(KERN_ERR,
  3481. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
  3482. EXPECT_BEGIN(KERN_ERR,
  3483. "OF: Error reverting changeset (-19)");
  3484. unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
  3485. "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
  3486. EXPECT_END(KERN_ERR,
  3487. "OF: Error reverting changeset (-19)");
  3488. EXPECT_END(KERN_ERR,
  3489. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
  3490. EXPECT_END(KERN_ERR,
  3491. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
  3492. EXPECT_END(KERN_ERR,
  3493. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
  3494. EXPECT_END(KERN_ERR,
  3495. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
  3496. /* --- overlay_bad_phandle --- */
  3497. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  3498. "Adding overlay 'overlay_bad_phandle' failed\n");
  3499. /* --- overlay_bad_symbol --- */
  3500. EXPECT_BEGIN(KERN_ERR,
  3501. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
  3502. EXPECT_BEGIN(KERN_ERR,
  3503. "OF: Error reverting changeset (-19)");
  3504. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  3505. "Adding overlay 'overlay_bad_symbol' failed\n");
  3506. EXPECT_END(KERN_ERR,
  3507. "OF: Error reverting changeset (-19)");
  3508. EXPECT_END(KERN_ERR,
  3509. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
  3510. /* --- overlay_bad_unresolved --- */
  3511. EXPECT_BEGIN(KERN_ERR,
  3512. "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
  3513. EXPECT_BEGIN(KERN_ERR,
  3514. "OF: resolver: overlay phandle fixup failed: -22");
  3515. unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
  3516. "Adding overlay 'overlay_bad_unresolved' failed\n");
  3517. EXPECT_END(KERN_ERR,
  3518. "OF: resolver: overlay phandle fixup failed: -22");
  3519. EXPECT_END(KERN_ERR,
  3520. "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
  3521. return;
  3522. err_unlock:
  3523. mutex_unlock(&of_mutex);
  3524. }
  3525. static int of_unittest_pci_dev_num;
  3526. static int of_unittest_pci_child_num;
  3527. /*
  3528. * PCI device tree node test driver
  3529. */
  3530. static const struct pci_device_id testdrv_pci_ids[] = {
  3531. { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
  3532. { 0, }
  3533. };
  3534. static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  3535. {
  3536. struct overlay_info *info;
  3537. struct device_node *dn;
  3538. int ret, ovcs_id;
  3539. u32 size;
  3540. dn = pdev->dev.of_node;
  3541. if (!dn) {
  3542. dev_err(&pdev->dev, "does not find bus endpoint");
  3543. return -EINVAL;
  3544. }
  3545. for (info = overlays; info && info->name; info++) {
  3546. if (!strcmp(info->name, "overlay_pci_node"))
  3547. break;
  3548. }
  3549. if (!info || !info->name) {
  3550. dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
  3551. return -ENODEV;
  3552. }
  3553. size = info->dtbo_end - info->dtbo_begin;
  3554. ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
  3555. of_node_put(dn);
  3556. if (ret)
  3557. return ret;
  3558. of_platform_default_populate(dn, NULL, &pdev->dev);
  3559. pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
  3560. return 0;
  3561. }
  3562. static void testdrv_remove(struct pci_dev *pdev)
  3563. {
  3564. int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
  3565. of_platform_depopulate(&pdev->dev);
  3566. of_overlay_remove(&ovcs_id);
  3567. }
  3568. static struct pci_driver testdrv_driver = {
  3569. .name = "pci_dt_testdrv",
  3570. .id_table = testdrv_pci_ids,
  3571. .probe = testdrv_probe,
  3572. .remove = testdrv_remove,
  3573. };
  3574. static int unittest_pci_probe(struct platform_device *pdev)
  3575. {
  3576. struct resource *res;
  3577. struct device *dev;
  3578. u64 exp_addr;
  3579. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3580. if (!res)
  3581. return -ENODEV;
  3582. dev = &pdev->dev;
  3583. while (dev && !dev_is_pci(dev))
  3584. dev = dev->parent;
  3585. if (!dev) {
  3586. pr_err("unable to find parent device\n");
  3587. return -ENODEV;
  3588. }
  3589. exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
  3590. unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
  3591. (u64)res->start, exp_addr);
  3592. of_unittest_pci_child_num++;
  3593. return 0;
  3594. }
  3595. static const struct of_device_id unittest_pci_of_match[] = {
  3596. { .compatible = "unittest-pci" },
  3597. { }
  3598. };
  3599. static struct platform_driver unittest_pci_driver = {
  3600. .probe = unittest_pci_probe,
  3601. .driver = {
  3602. .name = "unittest-pci",
  3603. .of_match_table = unittest_pci_of_match,
  3604. },
  3605. };
  3606. static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
  3607. {
  3608. struct device_node *pnp, *np = NULL;
  3609. struct device *child_dev;
  3610. char *path = NULL;
  3611. const __be32 *reg;
  3612. int rc = 0;
  3613. pnp = pdev->dev.of_node;
  3614. unittest(pnp, "Failed creating PCI dt node\n");
  3615. if (!pnp)
  3616. return -ENODEV;
  3617. if (add) {
  3618. path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
  3619. np = of_find_node_by_path(path);
  3620. unittest(np, "Failed to get unittest-pci node under PCI node\n");
  3621. if (!np) {
  3622. rc = -ENODEV;
  3623. goto failed;
  3624. }
  3625. reg = of_get_property(np, "reg", NULL);
  3626. unittest(reg, "Failed to get reg property\n");
  3627. if (!reg)
  3628. rc = -ENODEV;
  3629. } else {
  3630. path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
  3631. np = of_find_node_by_path(path);
  3632. unittest(!np, "Child device tree node is not removed\n");
  3633. child_dev = device_find_any_child(&pdev->dev);
  3634. unittest(!child_dev, "Child device is not removed\n");
  3635. put_device(child_dev);
  3636. }
  3637. failed:
  3638. kfree(path);
  3639. if (np)
  3640. of_node_put(np);
  3641. return rc;
  3642. }
  3643. static void __init of_unittest_pci_node(void)
  3644. {
  3645. struct pci_dev *pdev = NULL;
  3646. int rc;
  3647. if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
  3648. return;
  3649. rc = pci_register_driver(&testdrv_driver);
  3650. unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
  3651. if (rc)
  3652. return;
  3653. rc = platform_driver_register(&unittest_pci_driver);
  3654. if (unittest(!rc, "Failed to register unittest pci driver\n")) {
  3655. pci_unregister_driver(&testdrv_driver);
  3656. return;
  3657. }
  3658. while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
  3659. of_unittest_pci_node_verify(pdev, true);
  3660. of_unittest_pci_dev_num++;
  3661. }
  3662. if (pdev)
  3663. pci_dev_put(pdev);
  3664. unittest(of_unittest_pci_dev_num,
  3665. "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
  3666. unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
  3667. "Child device number %d is not expected %d", of_unittest_pci_child_num,
  3668. of_unittest_pci_dev_num);
  3669. platform_driver_unregister(&unittest_pci_driver);
  3670. pci_unregister_driver(&testdrv_driver);
  3671. while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
  3672. of_unittest_pci_node_verify(pdev, false);
  3673. if (pdev)
  3674. pci_dev_put(pdev);
  3675. }
  3676. #else
  3677. static inline __init void of_unittest_overlay_high_level(void) {}
  3678. static inline __init void of_unittest_pci_node(void) { }
  3679. #endif
  3680. static int __init of_unittest(void)
  3681. {
  3682. struct device_node *np;
  3683. int res;
  3684. pr_info("start of unittest - you will see error messages\n");
  3685. /* Taint the kernel so we know we've run tests. */
  3686. add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
  3687. /* adding data for unittest */
  3688. res = unittest_data_add();
  3689. if (res)
  3690. return res;
  3691. if (!of_aliases)
  3692. of_aliases = of_find_node_by_path("/aliases");
  3693. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  3694. if (!np) {
  3695. pr_info("No testcase data in device tree; not running tests\n");
  3696. return 0;
  3697. }
  3698. of_node_put(np);
  3699. of_unittest_check_tree_linkage();
  3700. of_unittest_check_phandles();
  3701. of_unittest_find_node_by_name();
  3702. of_unittest_dynamic();
  3703. of_unittest_parse_phandle_with_args();
  3704. of_unittest_parse_phandle_with_args_map();
  3705. of_unittest_printf();
  3706. of_unittest_property_string();
  3707. of_unittest_property_copy();
  3708. of_unittest_changeset();
  3709. of_unittest_changeset_prop();
  3710. of_unittest_parse_interrupts();
  3711. of_unittest_parse_interrupts_extended();
  3712. of_unittest_parse_interrupt_map();
  3713. of_unittest_irq_refcount();
  3714. of_unittest_dma_get_max_cpu_address();
  3715. of_unittest_parse_dma_ranges();
  3716. of_unittest_pci_dma_ranges();
  3717. of_unittest_pci_empty_dma_ranges();
  3718. of_unittest_bus_ranges();
  3719. of_unittest_bus_3cell_ranges();
  3720. of_unittest_reg();
  3721. of_unittest_translate_addr();
  3722. of_unittest_match_node();
  3723. of_unittest_platform_populate();
  3724. of_unittest_overlay();
  3725. of_unittest_lifecycle();
  3726. of_unittest_pci_node();
  3727. /* Double check linkage after removing testcase data */
  3728. of_unittest_check_tree_linkage();
  3729. of_unittest_overlay_high_level();
  3730. pr_info("end of unittest - %i passed, %i failed\n",
  3731. unittest_results.passed, unittest_results.failed);
  3732. return 0;
  3733. }
  3734. late_initcall(of_unittest);