unittest.c 123 KB

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