unittest.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309
  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. of_overlay_mutex_unlock();
  1616. return -EINVAL;
  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. return -ENODEV;
  1623. }
  1624. EXPECT_BEGIN(KERN_INFO,
  1625. "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
  1626. np = unittest_data_node->child;
  1627. while (np) {
  1628. struct device_node *next = np->sibling;
  1629. np->parent = of_root;
  1630. /* this will clear OF_DETACHED in np and children */
  1631. attach_node_and_children(np);
  1632. np = next;
  1633. }
  1634. EXPECT_END(KERN_INFO,
  1635. "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
  1636. of_overlay_mutex_unlock();
  1637. return 0;
  1638. }
  1639. #ifdef CONFIG_OF_OVERLAY
  1640. static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
  1641. static int unittest_probe(struct platform_device *pdev)
  1642. {
  1643. struct device *dev = &pdev->dev;
  1644. struct device_node *np = dev->of_node;
  1645. if (np == NULL) {
  1646. dev_err(dev, "No OF data for device\n");
  1647. return -EINVAL;
  1648. }
  1649. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1650. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1651. return 0;
  1652. }
  1653. static void unittest_remove(struct platform_device *pdev)
  1654. {
  1655. struct device *dev = &pdev->dev;
  1656. struct device_node *np = dev->of_node;
  1657. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1658. }
  1659. static const struct of_device_id unittest_match[] = {
  1660. { .compatible = "unittest", },
  1661. {},
  1662. };
  1663. static struct platform_driver unittest_driver = {
  1664. .probe = unittest_probe,
  1665. .remove_new = unittest_remove,
  1666. .driver = {
  1667. .name = "unittest",
  1668. .of_match_table = unittest_match,
  1669. },
  1670. };
  1671. /* get the platform device instantiated at the path */
  1672. static struct platform_device *of_path_to_platform_device(const char *path)
  1673. {
  1674. struct device_node *np;
  1675. struct platform_device *pdev;
  1676. np = of_find_node_by_path(path);
  1677. if (np == NULL)
  1678. return NULL;
  1679. pdev = of_find_device_by_node(np);
  1680. of_node_put(np);
  1681. return pdev;
  1682. }
  1683. /* find out if a platform device exists at that path */
  1684. static int of_path_platform_device_exists(const char *path)
  1685. {
  1686. struct platform_device *pdev;
  1687. pdev = of_path_to_platform_device(path);
  1688. platform_device_put(pdev);
  1689. return pdev != NULL;
  1690. }
  1691. #ifdef CONFIG_OF_GPIO
  1692. struct unittest_gpio_dev {
  1693. struct gpio_chip chip;
  1694. };
  1695. static int unittest_gpio_chip_request_count;
  1696. static int unittest_gpio_probe_count;
  1697. static int unittest_gpio_probe_pass_count;
  1698. static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
  1699. {
  1700. unittest_gpio_chip_request_count++;
  1701. pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
  1702. unittest_gpio_chip_request_count);
  1703. return 0;
  1704. }
  1705. static int unittest_gpio_probe(struct platform_device *pdev)
  1706. {
  1707. struct unittest_gpio_dev *devptr;
  1708. int ret;
  1709. unittest_gpio_probe_count++;
  1710. devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
  1711. if (!devptr)
  1712. return -ENOMEM;
  1713. platform_set_drvdata(pdev, devptr);
  1714. devptr->chip.fwnode = dev_fwnode(&pdev->dev);
  1715. devptr->chip.label = "of-unittest-gpio";
  1716. devptr->chip.base = -1; /* dynamic allocation */
  1717. devptr->chip.ngpio = 5;
  1718. devptr->chip.request = unittest_gpio_chip_request;
  1719. ret = gpiochip_add_data(&devptr->chip, NULL);
  1720. unittest(!ret,
  1721. "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
  1722. if (!ret)
  1723. unittest_gpio_probe_pass_count++;
  1724. return ret;
  1725. }
  1726. static void unittest_gpio_remove(struct platform_device *pdev)
  1727. {
  1728. struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
  1729. struct device *dev = &pdev->dev;
  1730. dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
  1731. if (devptr->chip.base != -1)
  1732. gpiochip_remove(&devptr->chip);
  1733. kfree(devptr);
  1734. }
  1735. static const struct of_device_id unittest_gpio_id[] = {
  1736. { .compatible = "unittest-gpio", },
  1737. {}
  1738. };
  1739. static struct platform_driver unittest_gpio_driver = {
  1740. .probe = unittest_gpio_probe,
  1741. .remove_new = unittest_gpio_remove,
  1742. .driver = {
  1743. .name = "unittest-gpio",
  1744. .of_match_table = unittest_gpio_id,
  1745. },
  1746. };
  1747. static void __init of_unittest_overlay_gpio(void)
  1748. {
  1749. int chip_request_count;
  1750. int probe_pass_count;
  1751. int ret;
  1752. /*
  1753. * tests: apply overlays before registering driver
  1754. * Similar to installing a driver as a module, the
  1755. * driver is registered after applying the overlays.
  1756. *
  1757. * The overlays are applied by overlay_data_apply()
  1758. * instead of of_unittest_apply_overlay() so that they
  1759. * will not be tracked. Thus they will not be removed
  1760. * by of_unittest_remove_tracked_overlays().
  1761. *
  1762. * - apply overlay_gpio_01
  1763. * - apply overlay_gpio_02a
  1764. * - apply overlay_gpio_02b
  1765. * - register driver
  1766. *
  1767. * register driver will result in
  1768. * - probe and processing gpio hog for overlay_gpio_01
  1769. * - probe for overlay_gpio_02a
  1770. * - processing gpio for overlay_gpio_02b
  1771. */
  1772. probe_pass_count = unittest_gpio_probe_pass_count;
  1773. chip_request_count = unittest_gpio_chip_request_count;
  1774. /*
  1775. * overlay_gpio_01 contains gpio node and child gpio hog node
  1776. * overlay_gpio_02a contains gpio node
  1777. * overlay_gpio_02b contains child gpio hog node
  1778. */
  1779. unittest(overlay_data_apply("overlay_gpio_01", NULL),
  1780. "Adding overlay 'overlay_gpio_01' failed\n");
  1781. unittest(overlay_data_apply("overlay_gpio_02a", NULL),
  1782. "Adding overlay 'overlay_gpio_02a' failed\n");
  1783. unittest(overlay_data_apply("overlay_gpio_02b", NULL),
  1784. "Adding overlay 'overlay_gpio_02b' failed\n");
  1785. ret = platform_driver_register(&unittest_gpio_driver);
  1786. if (unittest(ret == 0, "could not register unittest gpio driver\n"))
  1787. return;
  1788. unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
  1789. "unittest_gpio_probe() failed or not called\n");
  1790. unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
  1791. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1792. unittest_gpio_chip_request_count - chip_request_count);
  1793. /*
  1794. * tests: apply overlays after registering driver
  1795. *
  1796. * Similar to a driver built-in to the kernel, the
  1797. * driver is registered before applying the overlays.
  1798. *
  1799. * overlay_gpio_03 contains gpio node and child gpio hog node
  1800. *
  1801. * - apply overlay_gpio_03
  1802. *
  1803. * apply overlay will result in
  1804. * - probe and processing gpio hog.
  1805. */
  1806. probe_pass_count = unittest_gpio_probe_pass_count;
  1807. chip_request_count = unittest_gpio_chip_request_count;
  1808. /* overlay_gpio_03 contains gpio node and child gpio hog node */
  1809. unittest(overlay_data_apply("overlay_gpio_03", NULL),
  1810. "Adding overlay 'overlay_gpio_03' failed\n");
  1811. unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
  1812. "unittest_gpio_probe() failed or not called\n");
  1813. unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
  1814. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1815. unittest_gpio_chip_request_count - chip_request_count);
  1816. /*
  1817. * overlay_gpio_04a contains gpio node
  1818. *
  1819. * - apply overlay_gpio_04a
  1820. *
  1821. * apply the overlay will result in
  1822. * - probe for overlay_gpio_04a
  1823. */
  1824. probe_pass_count = unittest_gpio_probe_pass_count;
  1825. chip_request_count = unittest_gpio_chip_request_count;
  1826. /* overlay_gpio_04a contains gpio node */
  1827. unittest(overlay_data_apply("overlay_gpio_04a", NULL),
  1828. "Adding overlay 'overlay_gpio_04a' failed\n");
  1829. unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
  1830. "unittest_gpio_probe() failed or not called\n");
  1831. /*
  1832. * overlay_gpio_04b contains child gpio hog node
  1833. *
  1834. * - apply overlay_gpio_04b
  1835. *
  1836. * apply the overlay will result in
  1837. * - processing gpio for overlay_gpio_04b
  1838. */
  1839. /* overlay_gpio_04b contains child gpio hog node */
  1840. unittest(overlay_data_apply("overlay_gpio_04b", NULL),
  1841. "Adding overlay 'overlay_gpio_04b' failed\n");
  1842. unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
  1843. "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
  1844. unittest_gpio_chip_request_count - chip_request_count);
  1845. }
  1846. #else
  1847. static void __init of_unittest_overlay_gpio(void)
  1848. {
  1849. /* skip tests */
  1850. }
  1851. #endif
  1852. #if IS_BUILTIN(CONFIG_I2C)
  1853. /* get the i2c client device instantiated at the path */
  1854. static struct i2c_client *of_path_to_i2c_client(const char *path)
  1855. {
  1856. struct device_node *np;
  1857. struct i2c_client *client;
  1858. np = of_find_node_by_path(path);
  1859. if (np == NULL)
  1860. return NULL;
  1861. client = of_find_i2c_device_by_node(np);
  1862. of_node_put(np);
  1863. return client;
  1864. }
  1865. /* find out if a i2c client device exists at that path */
  1866. static int of_path_i2c_client_exists(const char *path)
  1867. {
  1868. struct i2c_client *client;
  1869. client = of_path_to_i2c_client(path);
  1870. if (client)
  1871. put_device(&client->dev);
  1872. return client != NULL;
  1873. }
  1874. #else
  1875. static int of_path_i2c_client_exists(const char *path)
  1876. {
  1877. return 0;
  1878. }
  1879. #endif
  1880. enum overlay_type {
  1881. PDEV_OVERLAY,
  1882. I2C_OVERLAY
  1883. };
  1884. static int of_path_device_type_exists(const char *path,
  1885. enum overlay_type ovtype)
  1886. {
  1887. switch (ovtype) {
  1888. case PDEV_OVERLAY:
  1889. return of_path_platform_device_exists(path);
  1890. case I2C_OVERLAY:
  1891. return of_path_i2c_client_exists(path);
  1892. }
  1893. return 0;
  1894. }
  1895. static const char *unittest_path(int nr, enum overlay_type ovtype)
  1896. {
  1897. const char *base;
  1898. static char buf[256];
  1899. switch (ovtype) {
  1900. case PDEV_OVERLAY:
  1901. base = "/testcase-data/overlay-node/test-bus";
  1902. break;
  1903. case I2C_OVERLAY:
  1904. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  1905. break;
  1906. default:
  1907. buf[0] = '\0';
  1908. return buf;
  1909. }
  1910. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  1911. buf[sizeof(buf) - 1] = '\0';
  1912. return buf;
  1913. }
  1914. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  1915. {
  1916. const char *path;
  1917. path = unittest_path(unittest_nr, ovtype);
  1918. switch (ovtype) {
  1919. case PDEV_OVERLAY:
  1920. return of_path_platform_device_exists(path);
  1921. case I2C_OVERLAY:
  1922. return of_path_i2c_client_exists(path);
  1923. }
  1924. return 0;
  1925. }
  1926. static const char *overlay_name_from_nr(int nr)
  1927. {
  1928. static char buf[256];
  1929. snprintf(buf, sizeof(buf) - 1,
  1930. "overlay_%d", nr);
  1931. buf[sizeof(buf) - 1] = '\0';
  1932. return buf;
  1933. }
  1934. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  1935. #define MAX_TRACK_OVCS_IDS 256
  1936. static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
  1937. static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
  1938. static int track_ovcs_id_cnt;
  1939. static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
  1940. {
  1941. if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
  1942. return;
  1943. track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
  1944. track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
  1945. track_ovcs_id_cnt++;
  1946. }
  1947. static void of_unittest_untrack_overlay(int ovcs_id)
  1948. {
  1949. if (WARN_ON(track_ovcs_id_cnt < 1))
  1950. return;
  1951. track_ovcs_id_cnt--;
  1952. /* If out of synch then test is broken. Do not try to recover. */
  1953. WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
  1954. }
  1955. static void of_unittest_remove_tracked_overlays(void)
  1956. {
  1957. int ret, ovcs_id, overlay_nr, save_ovcs_id;
  1958. const char *overlay_name;
  1959. while (track_ovcs_id_cnt > 0) {
  1960. ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
  1961. overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
  1962. save_ovcs_id = ovcs_id;
  1963. ret = of_overlay_remove(&ovcs_id);
  1964. if (ret == -ENODEV) {
  1965. overlay_name = overlay_name_from_nr(overlay_nr);
  1966. pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
  1967. __func__, overlay_name, ret);
  1968. }
  1969. of_unittest_untrack_overlay(save_ovcs_id);
  1970. }
  1971. }
  1972. static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
  1973. {
  1974. /*
  1975. * The overlay will be tracked, thus it will be removed
  1976. * by of_unittest_remove_tracked_overlays().
  1977. */
  1978. const char *overlay_name;
  1979. overlay_name = overlay_name_from_nr(overlay_nr);
  1980. if (!overlay_data_apply(overlay_name, ovcs_id)) {
  1981. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  1982. return -EFAULT;
  1983. }
  1984. of_unittest_track_overlay(*ovcs_id, overlay_nr);
  1985. return 0;
  1986. }
  1987. static int __init __of_unittest_apply_overlay_check(int overlay_nr,
  1988. int unittest_nr, int before, int after,
  1989. enum overlay_type ovtype)
  1990. {
  1991. int ret, ovcs_id;
  1992. /* unittest device must be in before state */
  1993. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1994. unittest(0, "%s with device @\"%s\" %s\n",
  1995. overlay_name_from_nr(overlay_nr),
  1996. unittest_path(unittest_nr, ovtype),
  1997. !before ? "enabled" : "disabled");
  1998. return -EINVAL;
  1999. }
  2000. /* apply the overlay */
  2001. ovcs_id = 0;
  2002. ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
  2003. if (ret != 0) {
  2004. /* of_unittest_apply_overlay already called unittest() */
  2005. return ret;
  2006. }
  2007. /* unittest device must be in after state */
  2008. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  2009. unittest(0, "%s with device @\"%s\" %s\n",
  2010. overlay_name_from_nr(overlay_nr),
  2011. unittest_path(unittest_nr, ovtype),
  2012. !after ? "enabled" : "disabled");
  2013. return -EINVAL;
  2014. }
  2015. return ovcs_id;
  2016. }
  2017. /* apply an overlay while checking before and after states */
  2018. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  2019. int unittest_nr, int before, int after,
  2020. enum overlay_type ovtype)
  2021. {
  2022. int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
  2023. unittest_nr, before, after, ovtype);
  2024. if (ovcs_id < 0)
  2025. return ovcs_id;
  2026. return 0;
  2027. }
  2028. /* apply an overlay and then revert it while checking before, after states */
  2029. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  2030. int unittest_nr, int before, int after,
  2031. enum overlay_type ovtype)
  2032. {
  2033. int ret, ovcs_id, save_ovcs_id;
  2034. ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
  2035. before, after, ovtype);
  2036. if (ovcs_id < 0)
  2037. return ovcs_id;
  2038. /* remove the overlay */
  2039. save_ovcs_id = ovcs_id;
  2040. ret = of_overlay_remove(&ovcs_id);
  2041. if (ret != 0) {
  2042. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  2043. overlay_name_from_nr(overlay_nr),
  2044. unittest_path(unittest_nr, ovtype));
  2045. return ret;
  2046. }
  2047. of_unittest_untrack_overlay(save_ovcs_id);
  2048. /* unittest device must be again in before state */
  2049. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  2050. unittest(0, "%s with device @\"%s\" %s\n",
  2051. overlay_name_from_nr(overlay_nr),
  2052. unittest_path(unittest_nr, ovtype),
  2053. !before ? "enabled" : "disabled");
  2054. return -EINVAL;
  2055. }
  2056. return 0;
  2057. }
  2058. /* test activation of device */
  2059. static void __init of_unittest_overlay_0(void)
  2060. {
  2061. int ret;
  2062. EXPECT_BEGIN(KERN_INFO,
  2063. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
  2064. /* device should enable */
  2065. ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
  2066. EXPECT_END(KERN_INFO,
  2067. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
  2068. if (ret)
  2069. return;
  2070. unittest(1, "overlay test %d passed\n", 0);
  2071. }
  2072. /* test deactivation of device */
  2073. static void __init of_unittest_overlay_1(void)
  2074. {
  2075. int ret;
  2076. EXPECT_BEGIN(KERN_INFO,
  2077. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
  2078. /* device should disable */
  2079. ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
  2080. EXPECT_END(KERN_INFO,
  2081. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
  2082. if (ret)
  2083. return;
  2084. unittest(1, "overlay test %d passed\n", 1);
  2085. }
  2086. /* test activation of device */
  2087. static void __init of_unittest_overlay_2(void)
  2088. {
  2089. int ret;
  2090. EXPECT_BEGIN(KERN_INFO,
  2091. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
  2092. /* device should enable */
  2093. ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
  2094. EXPECT_END(KERN_INFO,
  2095. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
  2096. if (ret)
  2097. return;
  2098. unittest(1, "overlay test %d passed\n", 2);
  2099. }
  2100. /* test deactivation of device */
  2101. static void __init of_unittest_overlay_3(void)
  2102. {
  2103. int ret;
  2104. EXPECT_BEGIN(KERN_INFO,
  2105. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
  2106. /* device should disable */
  2107. ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
  2108. EXPECT_END(KERN_INFO,
  2109. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
  2110. if (ret)
  2111. return;
  2112. unittest(1, "overlay test %d passed\n", 3);
  2113. }
  2114. /* test activation of a full device node */
  2115. static void __init of_unittest_overlay_4(void)
  2116. {
  2117. /* device should disable */
  2118. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  2119. return;
  2120. unittest(1, "overlay test %d passed\n", 4);
  2121. }
  2122. /* test overlay apply/revert sequence */
  2123. static void __init of_unittest_overlay_5(void)
  2124. {
  2125. int ret;
  2126. EXPECT_BEGIN(KERN_INFO,
  2127. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
  2128. /* device should disable */
  2129. ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
  2130. EXPECT_END(KERN_INFO,
  2131. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
  2132. if (ret)
  2133. return;
  2134. unittest(1, "overlay test %d passed\n", 5);
  2135. }
  2136. /* test overlay application in sequence */
  2137. static void __init of_unittest_overlay_6(void)
  2138. {
  2139. int i, save_ovcs_id[2], ovcs_id;
  2140. int overlay_nr = 6, unittest_nr = 6;
  2141. int before = 0, after = 1;
  2142. const char *overlay_name;
  2143. int ret;
  2144. /* unittest device must be in before state */
  2145. for (i = 0; i < 2; i++) {
  2146. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  2147. != before) {
  2148. unittest(0, "%s with device @\"%s\" %s\n",
  2149. overlay_name_from_nr(overlay_nr + i),
  2150. unittest_path(unittest_nr + i,
  2151. PDEV_OVERLAY),
  2152. !before ? "enabled" : "disabled");
  2153. return;
  2154. }
  2155. }
  2156. /* apply the overlays */
  2157. EXPECT_BEGIN(KERN_INFO,
  2158. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
  2159. overlay_name = overlay_name_from_nr(overlay_nr + 0);
  2160. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2161. if (!ret) {
  2162. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2163. return;
  2164. }
  2165. save_ovcs_id[0] = ovcs_id;
  2166. of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
  2167. EXPECT_END(KERN_INFO,
  2168. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
  2169. EXPECT_BEGIN(KERN_INFO,
  2170. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
  2171. overlay_name = overlay_name_from_nr(overlay_nr + 1);
  2172. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2173. if (!ret) {
  2174. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2175. return;
  2176. }
  2177. save_ovcs_id[1] = ovcs_id;
  2178. of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
  2179. EXPECT_END(KERN_INFO,
  2180. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
  2181. for (i = 0; i < 2; i++) {
  2182. /* unittest device must be in after state */
  2183. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  2184. != after) {
  2185. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  2186. overlay_name_from_nr(overlay_nr + i),
  2187. unittest_path(unittest_nr + i,
  2188. PDEV_OVERLAY),
  2189. !after ? "enabled" : "disabled");
  2190. return;
  2191. }
  2192. }
  2193. for (i = 1; i >= 0; i--) {
  2194. ovcs_id = save_ovcs_id[i];
  2195. if (of_overlay_remove(&ovcs_id)) {
  2196. unittest(0, "%s failed destroy @\"%s\"\n",
  2197. overlay_name_from_nr(overlay_nr + i),
  2198. unittest_path(unittest_nr + i,
  2199. PDEV_OVERLAY));
  2200. return;
  2201. }
  2202. of_unittest_untrack_overlay(save_ovcs_id[i]);
  2203. }
  2204. for (i = 0; i < 2; i++) {
  2205. /* unittest device must be again in before state */
  2206. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  2207. != before) {
  2208. unittest(0, "%s with device @\"%s\" %s\n",
  2209. overlay_name_from_nr(overlay_nr + i),
  2210. unittest_path(unittest_nr + i,
  2211. PDEV_OVERLAY),
  2212. !before ? "enabled" : "disabled");
  2213. return;
  2214. }
  2215. }
  2216. unittest(1, "overlay test %d passed\n", 6);
  2217. }
  2218. /* test overlay application in sequence */
  2219. static void __init of_unittest_overlay_8(void)
  2220. {
  2221. int i, save_ovcs_id[2], ovcs_id;
  2222. int overlay_nr = 8, unittest_nr = 8;
  2223. const char *overlay_name;
  2224. int ret;
  2225. /* we don't care about device state in this test */
  2226. EXPECT_BEGIN(KERN_INFO,
  2227. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
  2228. overlay_name = overlay_name_from_nr(overlay_nr + 0);
  2229. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2230. if (!ret)
  2231. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2232. EXPECT_END(KERN_INFO,
  2233. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
  2234. if (!ret)
  2235. return;
  2236. save_ovcs_id[0] = ovcs_id;
  2237. of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
  2238. overlay_name = overlay_name_from_nr(overlay_nr + 1);
  2239. EXPECT_BEGIN(KERN_INFO,
  2240. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
  2241. /* apply the overlays */
  2242. ret = overlay_data_apply(overlay_name, &ovcs_id);
  2243. EXPECT_END(KERN_INFO,
  2244. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
  2245. if (!ret) {
  2246. unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
  2247. return;
  2248. }
  2249. save_ovcs_id[1] = ovcs_id;
  2250. of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
  2251. /* now try to remove first overlay (it should fail) */
  2252. ovcs_id = save_ovcs_id[0];
  2253. EXPECT_BEGIN(KERN_INFO,
  2254. "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
  2255. EXPECT_BEGIN(KERN_INFO,
  2256. "OF: overlay: overlay #6 is not topmost");
  2257. ret = of_overlay_remove(&ovcs_id);
  2258. EXPECT_END(KERN_INFO,
  2259. "OF: overlay: overlay #6 is not topmost");
  2260. EXPECT_END(KERN_INFO,
  2261. "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
  2262. if (!ret) {
  2263. /*
  2264. * Should never get here. If we do, expect a lot of
  2265. * subsequent tracking and overlay removal related errors.
  2266. */
  2267. unittest(0, "%s was destroyed @\"%s\"\n",
  2268. overlay_name_from_nr(overlay_nr + 0),
  2269. unittest_path(unittest_nr,
  2270. PDEV_OVERLAY));
  2271. return;
  2272. }
  2273. /* removing them in order should work */
  2274. for (i = 1; i >= 0; i--) {
  2275. ovcs_id = save_ovcs_id[i];
  2276. if (of_overlay_remove(&ovcs_id)) {
  2277. unittest(0, "%s not destroyed @\"%s\"\n",
  2278. overlay_name_from_nr(overlay_nr + i),
  2279. unittest_path(unittest_nr,
  2280. PDEV_OVERLAY));
  2281. return;
  2282. }
  2283. of_unittest_untrack_overlay(save_ovcs_id[i]);
  2284. }
  2285. unittest(1, "overlay test %d passed\n", 8);
  2286. }
  2287. /* test insertion of a bus with parent devices */
  2288. static void __init of_unittest_overlay_10(void)
  2289. {
  2290. int ret;
  2291. char *child_path;
  2292. /* device should disable */
  2293. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  2294. if (unittest(ret == 0,
  2295. "overlay test %d failed; overlay application\n", 10))
  2296. return;
  2297. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  2298. unittest_path(10, PDEV_OVERLAY));
  2299. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  2300. return;
  2301. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  2302. kfree(child_path);
  2303. unittest(ret, "overlay test %d failed; no child device\n", 10);
  2304. }
  2305. /* test insertion of a bus with parent devices (and revert) */
  2306. static void __init of_unittest_overlay_11(void)
  2307. {
  2308. int ret;
  2309. /* device should disable */
  2310. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  2311. PDEV_OVERLAY);
  2312. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  2313. }
  2314. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  2315. struct unittest_i2c_bus_data {
  2316. struct platform_device *pdev;
  2317. struct i2c_adapter adap;
  2318. };
  2319. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  2320. struct i2c_msg *msgs, int num)
  2321. {
  2322. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  2323. (void)std;
  2324. return num;
  2325. }
  2326. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  2327. {
  2328. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  2329. }
  2330. static const struct i2c_algorithm unittest_i2c_algo = {
  2331. .master_xfer = unittest_i2c_master_xfer,
  2332. .functionality = unittest_i2c_functionality,
  2333. };
  2334. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  2335. {
  2336. struct device *dev = &pdev->dev;
  2337. struct device_node *np = dev->of_node;
  2338. struct unittest_i2c_bus_data *std;
  2339. struct i2c_adapter *adap;
  2340. int ret;
  2341. if (np == NULL) {
  2342. dev_err(dev, "No OF data for device\n");
  2343. return -EINVAL;
  2344. }
  2345. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2346. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  2347. if (!std)
  2348. return -ENOMEM;
  2349. /* link them together */
  2350. std->pdev = pdev;
  2351. platform_set_drvdata(pdev, std);
  2352. adap = &std->adap;
  2353. i2c_set_adapdata(adap, std);
  2354. adap->nr = -1;
  2355. strscpy(adap->name, pdev->name, sizeof(adap->name));
  2356. adap->class = I2C_CLASS_DEPRECATED;
  2357. adap->algo = &unittest_i2c_algo;
  2358. adap->dev.parent = dev;
  2359. adap->dev.of_node = dev->of_node;
  2360. adap->timeout = 5 * HZ;
  2361. adap->retries = 3;
  2362. ret = i2c_add_numbered_adapter(adap);
  2363. if (ret != 0) {
  2364. dev_err(dev, "Failed to add I2C adapter\n");
  2365. return ret;
  2366. }
  2367. return 0;
  2368. }
  2369. static void unittest_i2c_bus_remove(struct platform_device *pdev)
  2370. {
  2371. struct device *dev = &pdev->dev;
  2372. struct device_node *np = dev->of_node;
  2373. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  2374. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2375. i2c_del_adapter(&std->adap);
  2376. }
  2377. static const struct of_device_id unittest_i2c_bus_match[] = {
  2378. { .compatible = "unittest-i2c-bus", },
  2379. {},
  2380. };
  2381. static struct platform_driver unittest_i2c_bus_driver = {
  2382. .probe = unittest_i2c_bus_probe,
  2383. .remove_new = unittest_i2c_bus_remove,
  2384. .driver = {
  2385. .name = "unittest-i2c-bus",
  2386. .of_match_table = unittest_i2c_bus_match,
  2387. },
  2388. };
  2389. static int unittest_i2c_dev_probe(struct i2c_client *client)
  2390. {
  2391. struct device *dev = &client->dev;
  2392. struct device_node *np = client->dev.of_node;
  2393. if (!np) {
  2394. dev_err(dev, "No OF node\n");
  2395. return -EINVAL;
  2396. }
  2397. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2398. return 0;
  2399. };
  2400. static void unittest_i2c_dev_remove(struct i2c_client *client)
  2401. {
  2402. struct device *dev = &client->dev;
  2403. struct device_node *np = client->dev.of_node;
  2404. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2405. }
  2406. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  2407. { .name = "unittest-i2c-dev" },
  2408. { }
  2409. };
  2410. static struct i2c_driver unittest_i2c_dev_driver = {
  2411. .driver = {
  2412. .name = "unittest-i2c-dev",
  2413. },
  2414. .probe = unittest_i2c_dev_probe,
  2415. .remove = unittest_i2c_dev_remove,
  2416. .id_table = unittest_i2c_dev_id,
  2417. };
  2418. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2419. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  2420. {
  2421. return 0;
  2422. }
  2423. static int unittest_i2c_mux_probe(struct i2c_client *client)
  2424. {
  2425. int i, nchans;
  2426. struct device *dev = &client->dev;
  2427. struct i2c_adapter *adap = client->adapter;
  2428. struct device_node *np = client->dev.of_node, *child;
  2429. struct i2c_mux_core *muxc;
  2430. u32 reg, max_reg;
  2431. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2432. if (!np) {
  2433. dev_err(dev, "No OF node\n");
  2434. return -EINVAL;
  2435. }
  2436. max_reg = (u32)-1;
  2437. for_each_child_of_node(np, child) {
  2438. if (of_property_read_u32(child, "reg", &reg))
  2439. continue;
  2440. if (max_reg == (u32)-1 || reg > max_reg)
  2441. max_reg = reg;
  2442. }
  2443. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  2444. if (nchans == 0) {
  2445. dev_err(dev, "No channels\n");
  2446. return -EINVAL;
  2447. }
  2448. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  2449. unittest_i2c_mux_select_chan, NULL);
  2450. if (!muxc)
  2451. return -ENOMEM;
  2452. for (i = 0; i < nchans; i++) {
  2453. if (i2c_mux_add_adapter(muxc, 0, i)) {
  2454. dev_err(dev, "Failed to register mux #%d\n", i);
  2455. i2c_mux_del_adapters(muxc);
  2456. return -ENODEV;
  2457. }
  2458. }
  2459. i2c_set_clientdata(client, muxc);
  2460. return 0;
  2461. };
  2462. static void unittest_i2c_mux_remove(struct i2c_client *client)
  2463. {
  2464. struct device *dev = &client->dev;
  2465. struct device_node *np = client->dev.of_node;
  2466. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  2467. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  2468. i2c_mux_del_adapters(muxc);
  2469. }
  2470. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  2471. { .name = "unittest-i2c-mux" },
  2472. { }
  2473. };
  2474. static struct i2c_driver unittest_i2c_mux_driver = {
  2475. .driver = {
  2476. .name = "unittest-i2c-mux",
  2477. },
  2478. .probe = unittest_i2c_mux_probe,
  2479. .remove = unittest_i2c_mux_remove,
  2480. .id_table = unittest_i2c_mux_id,
  2481. };
  2482. #endif
  2483. static int of_unittest_overlay_i2c_init(void)
  2484. {
  2485. int ret;
  2486. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  2487. if (unittest(ret == 0,
  2488. "could not register unittest i2c device driver\n"))
  2489. return ret;
  2490. ret = platform_driver_register(&unittest_i2c_bus_driver);
  2491. if (unittest(ret == 0,
  2492. "could not register unittest i2c bus driver\n"))
  2493. return ret;
  2494. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2495. EXPECT_BEGIN(KERN_INFO,
  2496. "i2c i2c-1: Added multiplexed i2c bus 2");
  2497. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  2498. EXPECT_END(KERN_INFO,
  2499. "i2c i2c-1: Added multiplexed i2c bus 2");
  2500. if (unittest(ret == 0,
  2501. "could not register unittest i2c mux driver\n"))
  2502. return ret;
  2503. #endif
  2504. return 0;
  2505. }
  2506. static void of_unittest_overlay_i2c_cleanup(void)
  2507. {
  2508. #if IS_BUILTIN(CONFIG_I2C_MUX)
  2509. i2c_del_driver(&unittest_i2c_mux_driver);
  2510. #endif
  2511. platform_driver_unregister(&unittest_i2c_bus_driver);
  2512. i2c_del_driver(&unittest_i2c_dev_driver);
  2513. }
  2514. static void __init of_unittest_overlay_i2c_12(void)
  2515. {
  2516. int ret;
  2517. /* device should enable */
  2518. EXPECT_BEGIN(KERN_INFO,
  2519. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
  2520. ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
  2521. EXPECT_END(KERN_INFO,
  2522. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
  2523. if (ret)
  2524. return;
  2525. unittest(1, "overlay test %d passed\n", 12);
  2526. }
  2527. /* test deactivation of device */
  2528. static void __init of_unittest_overlay_i2c_13(void)
  2529. {
  2530. int ret;
  2531. EXPECT_BEGIN(KERN_INFO,
  2532. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
  2533. /* device should disable */
  2534. ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
  2535. EXPECT_END(KERN_INFO,
  2536. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
  2537. if (ret)
  2538. return;
  2539. unittest(1, "overlay test %d passed\n", 13);
  2540. }
  2541. /* just check for i2c mux existence */
  2542. static void of_unittest_overlay_i2c_14(void)
  2543. {
  2544. }
  2545. static void __init of_unittest_overlay_i2c_15(void)
  2546. {
  2547. int ret;
  2548. /* device should enable */
  2549. EXPECT_BEGIN(KERN_INFO,
  2550. "i2c i2c-1: Added multiplexed i2c bus 3");
  2551. ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
  2552. EXPECT_END(KERN_INFO,
  2553. "i2c i2c-1: Added multiplexed i2c bus 3");
  2554. if (ret)
  2555. return;
  2556. unittest(1, "overlay test %d passed\n", 15);
  2557. }
  2558. #else
  2559. static inline void of_unittest_overlay_i2c_14(void) { }
  2560. static inline void of_unittest_overlay_i2c_15(void) { }
  2561. #endif
  2562. static int of_notify(struct notifier_block *nb, unsigned long action,
  2563. void *arg)
  2564. {
  2565. struct of_overlay_notify_data *nd = arg;
  2566. struct device_node *found;
  2567. int ret;
  2568. /*
  2569. * For overlay_16 .. overlay_19, check that returning an error
  2570. * works for each of the actions by setting an arbitrary return
  2571. * error number that matches the test number. e.g. for unittest16,
  2572. * ret = -EBUSY which is -16.
  2573. *
  2574. * OVERLAY_INFO() for the overlays is declared to expect the same
  2575. * error number, so overlay_data_apply() will return no error.
  2576. *
  2577. * overlay_20 will return NOTIFY_DONE
  2578. */
  2579. ret = 0;
  2580. of_node_get(nd->overlay);
  2581. switch (action) {
  2582. case OF_OVERLAY_PRE_APPLY:
  2583. found = of_find_node_by_name(nd->overlay, "test-unittest16");
  2584. if (found) {
  2585. of_node_put(found);
  2586. ret = -EBUSY;
  2587. }
  2588. break;
  2589. case OF_OVERLAY_POST_APPLY:
  2590. found = of_find_node_by_name(nd->overlay, "test-unittest17");
  2591. if (found) {
  2592. of_node_put(found);
  2593. ret = -EEXIST;
  2594. }
  2595. break;
  2596. case OF_OVERLAY_PRE_REMOVE:
  2597. found = of_find_node_by_name(nd->overlay, "test-unittest18");
  2598. if (found) {
  2599. of_node_put(found);
  2600. ret = -EXDEV;
  2601. }
  2602. break;
  2603. case OF_OVERLAY_POST_REMOVE:
  2604. found = of_find_node_by_name(nd->overlay, "test-unittest19");
  2605. if (found) {
  2606. of_node_put(found);
  2607. ret = -ENODEV;
  2608. }
  2609. break;
  2610. default: /* should not happen */
  2611. of_node_put(nd->overlay);
  2612. ret = -EINVAL;
  2613. break;
  2614. }
  2615. if (ret)
  2616. return notifier_from_errno(ret);
  2617. return NOTIFY_DONE;
  2618. }
  2619. static struct notifier_block of_nb = {
  2620. .notifier_call = of_notify,
  2621. };
  2622. static void __init of_unittest_overlay_notify(void)
  2623. {
  2624. int ovcs_id;
  2625. int ret;
  2626. ret = of_overlay_notifier_register(&of_nb);
  2627. unittest(!ret,
  2628. "of_overlay_notifier_register() failed, ret = %d\n", ret);
  2629. if (ret)
  2630. return;
  2631. /*
  2632. * The overlays are applied by overlay_data_apply()
  2633. * instead of of_unittest_apply_overlay() so that they
  2634. * will not be tracked. Thus they will not be removed
  2635. * by of_unittest_remove_tracked_overlays().
  2636. *
  2637. * Applying overlays 16 - 19 will each trigger an error for a
  2638. * different action in of_notify().
  2639. *
  2640. * Applying overlay 20 will not trigger any error in of_notify().
  2641. */
  2642. /* --- overlay 16 --- */
  2643. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
  2644. unittest(overlay_data_apply("overlay_16", &ovcs_id),
  2645. "test OF_OVERLAY_PRE_APPLY notify injected error\n");
  2646. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
  2647. unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
  2648. /* --- overlay 17 --- */
  2649. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
  2650. unittest(overlay_data_apply("overlay_17", &ovcs_id),
  2651. "test OF_OVERLAY_POST_APPLY notify injected error\n");
  2652. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
  2653. unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
  2654. /* --- overlay 18 --- */
  2655. unittest(overlay_data_apply("overlay_18", &ovcs_id),
  2656. "OF_OVERLAY_PRE_REMOVE notify injected error\n");
  2657. unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
  2658. if (ovcs_id) {
  2659. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
  2660. ret = of_overlay_remove(&ovcs_id);
  2661. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
  2662. if (ret == -EXDEV) {
  2663. /*
  2664. * change set ovcs_id should still exist
  2665. */
  2666. unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
  2667. } else {
  2668. unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
  2669. }
  2670. } else {
  2671. unittest(1, "ovcs_id not created for overlay_18\n");
  2672. }
  2673. unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
  2674. /* --- overlay 19 --- */
  2675. unittest(overlay_data_apply("overlay_19", &ovcs_id),
  2676. "OF_OVERLAY_POST_REMOVE notify injected error\n");
  2677. unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
  2678. if (ovcs_id) {
  2679. EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
  2680. ret = of_overlay_remove(&ovcs_id);
  2681. EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
  2682. if (ret == -ENODEV)
  2683. unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
  2684. else
  2685. unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
  2686. } else {
  2687. unittest(1, "ovcs_id removed for overlay_19\n");
  2688. }
  2689. unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
  2690. ovcs_id);
  2691. /* --- overlay 20 --- */
  2692. unittest(overlay_data_apply("overlay_20", &ovcs_id),
  2693. "overlay notify no injected error\n");
  2694. if (ovcs_id) {
  2695. ret = of_overlay_remove(&ovcs_id);
  2696. if (ret)
  2697. unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
  2698. ret);
  2699. } else {
  2700. unittest(1, "ovcs_id not created for overlay_20\n");
  2701. }
  2702. unittest(!of_overlay_notifier_unregister(&of_nb),
  2703. "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
  2704. }
  2705. static void __init of_unittest_overlay(void)
  2706. {
  2707. struct device_node *bus_np = NULL;
  2708. unsigned int i;
  2709. if (platform_driver_register(&unittest_driver)) {
  2710. unittest(0, "could not register unittest driver\n");
  2711. goto out;
  2712. }
  2713. bus_np = of_find_node_by_path(bus_path);
  2714. if (bus_np == NULL) {
  2715. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  2716. goto out;
  2717. }
  2718. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  2719. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  2720. goto out;
  2721. }
  2722. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  2723. unittest(0, "could not find unittest0 @ \"%s\"\n",
  2724. unittest_path(100, PDEV_OVERLAY));
  2725. goto out;
  2726. }
  2727. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  2728. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  2729. unittest_path(101, PDEV_OVERLAY));
  2730. goto out;
  2731. }
  2732. unittest(1, "basic infrastructure of overlays passed");
  2733. /* tests in sequence */
  2734. of_unittest_overlay_0();
  2735. of_unittest_overlay_1();
  2736. of_unittest_overlay_2();
  2737. of_unittest_overlay_3();
  2738. of_unittest_overlay_4();
  2739. for (i = 0; i < 3; i++)
  2740. of_unittest_overlay_5();
  2741. of_unittest_overlay_6();
  2742. of_unittest_overlay_8();
  2743. of_unittest_overlay_10();
  2744. of_unittest_overlay_11();
  2745. #if IS_BUILTIN(CONFIG_I2C)
  2746. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  2747. goto out;
  2748. of_unittest_overlay_i2c_12();
  2749. of_unittest_overlay_i2c_13();
  2750. of_unittest_overlay_i2c_14();
  2751. of_unittest_overlay_i2c_15();
  2752. of_unittest_overlay_i2c_cleanup();
  2753. #endif
  2754. of_unittest_overlay_gpio();
  2755. of_unittest_remove_tracked_overlays();
  2756. of_unittest_overlay_notify();
  2757. out:
  2758. of_node_put(bus_np);
  2759. }
  2760. #else
  2761. static inline void __init of_unittest_overlay(void) { }
  2762. #endif
  2763. static void __init of_unittest_lifecycle(void)
  2764. {
  2765. #ifdef CONFIG_OF_DYNAMIC
  2766. unsigned int refcount;
  2767. int found_refcount_one = 0;
  2768. int put_count = 0;
  2769. struct device_node *np;
  2770. struct device_node *prev_sibling, *next_sibling;
  2771. const char *refcount_path = "/testcase-data/refcount-node";
  2772. const char *refcount_parent_path = "/testcase-data";
  2773. /*
  2774. * Node lifecycle tests, non-dynamic node:
  2775. *
  2776. * - Decrementing refcount to zero via of_node_put() should cause the
  2777. * attempt to free the node memory by of_node_release() to fail
  2778. * because the node is not a dynamic node.
  2779. *
  2780. * - Decrementing refcount past zero should result in additional
  2781. * errors reported.
  2782. */
  2783. np = of_find_node_by_path(refcount_path);
  2784. unittest(np, "find refcount_path \"%s\"\n", refcount_path);
  2785. if (np == NULL)
  2786. goto out_skip_tests;
  2787. while (!found_refcount_one) {
  2788. if (put_count++ > 10) {
  2789. unittest(0, "guardrail to avoid infinite loop\n");
  2790. goto out_skip_tests;
  2791. }
  2792. refcount = kref_read(&np->kobj.kref);
  2793. if (refcount == 1)
  2794. found_refcount_one = 1;
  2795. else
  2796. of_node_put(np);
  2797. }
  2798. EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
  2799. /*
  2800. * refcount is now one, decrementing to zero will result in a call to
  2801. * of_node_release() to free the node's memory, which should result
  2802. * in an error
  2803. */
  2804. unittest(1, "/testcase-data/refcount-node is one");
  2805. of_node_put(np);
  2806. EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
  2807. /*
  2808. * expect stack trace for subsequent of_node_put():
  2809. * __refcount_sub_and_test() calls:
  2810. * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
  2811. *
  2812. * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
  2813. * the first three lines, and the last line.
  2814. */
  2815. EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
  2816. EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
  2817. EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
  2818. EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
  2819. /* refcount is now zero, this should fail */
  2820. unittest(1, "/testcase-data/refcount-node is zero");
  2821. of_node_put(np);
  2822. EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
  2823. EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
  2824. EXPECT_END(KERN_INFO, "WARNING: <<all>>");
  2825. EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
  2826. /*
  2827. * Q. do we expect to get yet another warning?
  2828. * A. no, the WARNING is from WARN_ONCE()
  2829. */
  2830. EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
  2831. EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
  2832. EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
  2833. EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
  2834. unittest(1, "/testcase-data/refcount-node is zero, second time");
  2835. of_node_put(np);
  2836. EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
  2837. EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
  2838. EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
  2839. EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
  2840. /*
  2841. * refcount of zero will trigger stack traces from any further
  2842. * attempt to of_node_get() node "refcount-node". One example of
  2843. * this is where of_unittest_check_node_linkage() will recursively
  2844. * scan the tree, with 'for_each_child_of_node()' doing an
  2845. * of_node_get() of the children of a node.
  2846. *
  2847. * Prevent the stack trace by removing node "refcount-node" from
  2848. * its parent's child list.
  2849. *
  2850. * WARNING: EVIL, EVIL, EVIL:
  2851. *
  2852. * Directly manipulate the child list of node /testcase-data to
  2853. * remove child refcount-node. This is ignoring all proper methods
  2854. * of removing a child and will leak a small amount of memory.
  2855. */
  2856. np = of_find_node_by_path(refcount_parent_path);
  2857. unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
  2858. unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
  2859. if (np == NULL)
  2860. return;
  2861. prev_sibling = np->child;
  2862. next_sibling = prev_sibling->sibling;
  2863. if (!strcmp(prev_sibling->full_name, "refcount-node")) {
  2864. np->child = next_sibling;
  2865. next_sibling = next_sibling->sibling;
  2866. }
  2867. while (next_sibling) {
  2868. if (!strcmp(next_sibling->full_name, "refcount-node"))
  2869. prev_sibling->sibling = next_sibling->sibling;
  2870. prev_sibling = next_sibling;
  2871. next_sibling = next_sibling->sibling;
  2872. }
  2873. of_node_put(np);
  2874. return;
  2875. out_skip_tests:
  2876. #endif
  2877. unittest(0, "One or more lifecycle tests skipped\n");
  2878. }
  2879. #ifdef CONFIG_OF_OVERLAY
  2880. /*
  2881. * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
  2882. * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
  2883. */
  2884. #define OVERLAY_INFO_EXTERN(overlay_name) \
  2885. extern uint8_t __dtbo_##overlay_name##_begin[]; \
  2886. extern uint8_t __dtbo_##overlay_name##_end[]
  2887. #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
  2888. { .dtbo_begin = __dtbo_##overlay_name##_begin, \
  2889. .dtbo_end = __dtbo_##overlay_name##_end, \
  2890. .expected_result = expected, \
  2891. .expected_result_remove = expected_remove, \
  2892. .name = #overlay_name, \
  2893. }
  2894. struct overlay_info {
  2895. uint8_t *dtbo_begin;
  2896. uint8_t *dtbo_end;
  2897. int expected_result;
  2898. int expected_result_remove; /* if apply failed */
  2899. int ovcs_id;
  2900. char *name;
  2901. };
  2902. OVERLAY_INFO_EXTERN(overlay_base);
  2903. OVERLAY_INFO_EXTERN(overlay);
  2904. OVERLAY_INFO_EXTERN(overlay_0);
  2905. OVERLAY_INFO_EXTERN(overlay_1);
  2906. OVERLAY_INFO_EXTERN(overlay_2);
  2907. OVERLAY_INFO_EXTERN(overlay_3);
  2908. OVERLAY_INFO_EXTERN(overlay_4);
  2909. OVERLAY_INFO_EXTERN(overlay_5);
  2910. OVERLAY_INFO_EXTERN(overlay_6);
  2911. OVERLAY_INFO_EXTERN(overlay_7);
  2912. OVERLAY_INFO_EXTERN(overlay_8);
  2913. OVERLAY_INFO_EXTERN(overlay_9);
  2914. OVERLAY_INFO_EXTERN(overlay_10);
  2915. OVERLAY_INFO_EXTERN(overlay_11);
  2916. OVERLAY_INFO_EXTERN(overlay_12);
  2917. OVERLAY_INFO_EXTERN(overlay_13);
  2918. OVERLAY_INFO_EXTERN(overlay_15);
  2919. OVERLAY_INFO_EXTERN(overlay_16);
  2920. OVERLAY_INFO_EXTERN(overlay_17);
  2921. OVERLAY_INFO_EXTERN(overlay_18);
  2922. OVERLAY_INFO_EXTERN(overlay_19);
  2923. OVERLAY_INFO_EXTERN(overlay_20);
  2924. OVERLAY_INFO_EXTERN(overlay_gpio_01);
  2925. OVERLAY_INFO_EXTERN(overlay_gpio_02a);
  2926. OVERLAY_INFO_EXTERN(overlay_gpio_02b);
  2927. OVERLAY_INFO_EXTERN(overlay_gpio_03);
  2928. OVERLAY_INFO_EXTERN(overlay_gpio_04a);
  2929. OVERLAY_INFO_EXTERN(overlay_gpio_04b);
  2930. OVERLAY_INFO_EXTERN(overlay_pci_node);
  2931. OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
  2932. OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
  2933. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  2934. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  2935. OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
  2936. /* entries found by name */
  2937. static struct overlay_info overlays[] = {
  2938. OVERLAY_INFO(overlay_base, -9999, 0),
  2939. OVERLAY_INFO(overlay, 0, 0),
  2940. OVERLAY_INFO(overlay_0, 0, 0),
  2941. OVERLAY_INFO(overlay_1, 0, 0),
  2942. OVERLAY_INFO(overlay_2, 0, 0),
  2943. OVERLAY_INFO(overlay_3, 0, 0),
  2944. OVERLAY_INFO(overlay_4, 0, 0),
  2945. OVERLAY_INFO(overlay_5, 0, 0),
  2946. OVERLAY_INFO(overlay_6, 0, 0),
  2947. OVERLAY_INFO(overlay_7, 0, 0),
  2948. OVERLAY_INFO(overlay_8, 0, 0),
  2949. OVERLAY_INFO(overlay_9, 0, 0),
  2950. OVERLAY_INFO(overlay_10, 0, 0),
  2951. OVERLAY_INFO(overlay_11, 0, 0),
  2952. OVERLAY_INFO(overlay_12, 0, 0),
  2953. OVERLAY_INFO(overlay_13, 0, 0),
  2954. OVERLAY_INFO(overlay_15, 0, 0),
  2955. OVERLAY_INFO(overlay_16, -EBUSY, 0),
  2956. OVERLAY_INFO(overlay_17, -EEXIST, 0),
  2957. OVERLAY_INFO(overlay_18, 0, 0),
  2958. OVERLAY_INFO(overlay_19, 0, 0),
  2959. OVERLAY_INFO(overlay_20, 0, 0),
  2960. OVERLAY_INFO(overlay_gpio_01, 0, 0),
  2961. OVERLAY_INFO(overlay_gpio_02a, 0, 0),
  2962. OVERLAY_INFO(overlay_gpio_02b, 0, 0),
  2963. OVERLAY_INFO(overlay_gpio_03, 0, 0),
  2964. OVERLAY_INFO(overlay_gpio_04a, 0, 0),
  2965. OVERLAY_INFO(overlay_gpio_04b, 0, 0),
  2966. OVERLAY_INFO(overlay_pci_node, 0, 0),
  2967. OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
  2968. OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
  2969. OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
  2970. OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
  2971. OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
  2972. /* end marker */
  2973. { }
  2974. };
  2975. static struct device_node *overlay_base_root;
  2976. static void * __init dt_alloc_memory(u64 size, u64 align)
  2977. {
  2978. void *ptr = memblock_alloc(size, align);
  2979. if (!ptr)
  2980. panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
  2981. __func__, size, align);
  2982. return ptr;
  2983. }
  2984. /*
  2985. * Create base device tree for the overlay unittest.
  2986. *
  2987. * This is called from very early boot code.
  2988. *
  2989. * Do as much as possible the same way as done in __unflatten_device_tree
  2990. * and other early boot steps for the normal FDT so that the overlay base
  2991. * unflattened tree will have the same characteristics as the real tree
  2992. * (such as having memory allocated by the early allocator). The goal
  2993. * is to test "the real thing" as much as possible, and test "test setup
  2994. * code" as little as possible.
  2995. *
  2996. * Have to stop before resolving phandles, because that uses kmalloc.
  2997. */
  2998. void __init unittest_unflatten_overlay_base(void)
  2999. {
  3000. struct overlay_info *info;
  3001. u32 data_size;
  3002. void *new_fdt;
  3003. u32 size;
  3004. int found = 0;
  3005. const char *overlay_name = "overlay_base";
  3006. for (info = overlays; info && info->name; info++) {
  3007. if (!strcmp(overlay_name, info->name)) {
  3008. found = 1;
  3009. break;
  3010. }
  3011. }
  3012. if (!found) {
  3013. pr_err("no overlay data for %s\n", overlay_name);
  3014. return;
  3015. }
  3016. info = &overlays[0];
  3017. if (info->expected_result != -9999) {
  3018. pr_err("No dtb 'overlay_base' to attach\n");
  3019. return;
  3020. }
  3021. data_size = info->dtbo_end - info->dtbo_begin;
  3022. if (!data_size) {
  3023. pr_err("No dtb 'overlay_base' to attach\n");
  3024. return;
  3025. }
  3026. size = fdt_totalsize(info->dtbo_begin);
  3027. if (size != data_size) {
  3028. pr_err("dtb 'overlay_base' header totalsize != actual size");
  3029. return;
  3030. }
  3031. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  3032. if (!new_fdt) {
  3033. pr_err("alloc for dtb 'overlay_base' failed");
  3034. return;
  3035. }
  3036. memcpy(new_fdt, info->dtbo_begin, size);
  3037. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  3038. dt_alloc_memory, true);
  3039. }
  3040. /*
  3041. * The purpose of of_unittest_overlay_data_add is to add an
  3042. * overlay in the normal fashion. This is a test of the whole
  3043. * picture, instead of testing individual elements.
  3044. *
  3045. * A secondary purpose is to be able to verify that the contents of
  3046. * /proc/device-tree/ contains the updated structure and values from
  3047. * the overlay. That must be verified separately in user space.
  3048. *
  3049. * Return 0 on unexpected error.
  3050. */
  3051. static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
  3052. {
  3053. struct overlay_info *info;
  3054. int passed = 1;
  3055. int found = 0;
  3056. int ret, ret2;
  3057. u32 size;
  3058. for (info = overlays; info && info->name; info++) {
  3059. if (!strcmp(overlay_name, info->name)) {
  3060. found = 1;
  3061. break;
  3062. }
  3063. }
  3064. if (!found) {
  3065. pr_err("no overlay data for %s\n", overlay_name);
  3066. return 0;
  3067. }
  3068. size = info->dtbo_end - info->dtbo_begin;
  3069. if (!size)
  3070. pr_err("no overlay data for %s\n", overlay_name);
  3071. ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
  3072. NULL);
  3073. if (ovcs_id)
  3074. *ovcs_id = info->ovcs_id;
  3075. if (ret < 0)
  3076. goto out;
  3077. pr_debug("%s applied\n", overlay_name);
  3078. out:
  3079. if (ret != info->expected_result) {
  3080. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  3081. info->expected_result, ret, overlay_name);
  3082. passed = 0;
  3083. }
  3084. if (ret < 0) {
  3085. /* changeset may be partially applied */
  3086. ret2 = of_overlay_remove(&info->ovcs_id);
  3087. if (ret2 != info->expected_result_remove) {
  3088. pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
  3089. info->expected_result_remove, ret2,
  3090. overlay_name);
  3091. passed = 0;
  3092. }
  3093. }
  3094. return passed;
  3095. }
  3096. /*
  3097. * The purpose of of_unittest_overlay_high_level is to add an overlay
  3098. * in the normal fashion. This is a test of the whole picture,
  3099. * instead of individual elements.
  3100. *
  3101. * The first part of the function is _not_ normal overlay usage; it is
  3102. * finishing splicing the base overlay device tree into the live tree.
  3103. */
  3104. static __init void of_unittest_overlay_high_level(void)
  3105. {
  3106. struct device_node *last_sibling;
  3107. struct device_node *np;
  3108. struct device_node *of_symbols;
  3109. struct device_node *overlay_base_symbols;
  3110. struct device_node **pprev;
  3111. struct property *prop;
  3112. int ret;
  3113. if (!overlay_base_root) {
  3114. unittest(0, "overlay_base_root not initialized\n");
  3115. return;
  3116. }
  3117. /*
  3118. * Could not fixup phandles in unittest_unflatten_overlay_base()
  3119. * because kmalloc() was not yet available.
  3120. */
  3121. of_overlay_mutex_lock();
  3122. of_resolve_phandles(overlay_base_root);
  3123. of_overlay_mutex_unlock();
  3124. /*
  3125. * do not allow overlay_base to duplicate any node already in
  3126. * tree, this greatly simplifies the code
  3127. */
  3128. /*
  3129. * remove overlay_base_root node "__local_fixups", after
  3130. * being used by of_resolve_phandles()
  3131. */
  3132. pprev = &overlay_base_root->child;
  3133. for (np = overlay_base_root->child; np; np = np->sibling) {
  3134. if (of_node_name_eq(np, "__local_fixups__")) {
  3135. *pprev = np->sibling;
  3136. break;
  3137. }
  3138. pprev = &np->sibling;
  3139. }
  3140. /* remove overlay_base_root node "__symbols__" if in live tree */
  3141. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  3142. if (of_symbols) {
  3143. /* will have to graft properties from node into live tree */
  3144. pprev = &overlay_base_root->child;
  3145. for (np = overlay_base_root->child; np; np = np->sibling) {
  3146. if (of_node_name_eq(np, "__symbols__")) {
  3147. overlay_base_symbols = np;
  3148. *pprev = np->sibling;
  3149. break;
  3150. }
  3151. pprev = &np->sibling;
  3152. }
  3153. }
  3154. for_each_child_of_node(overlay_base_root, np) {
  3155. struct device_node *base_child;
  3156. for_each_child_of_node(of_root, base_child) {
  3157. if (!strcmp(np->full_name, base_child->full_name)) {
  3158. unittest(0, "illegal node name in overlay_base %pOFn",
  3159. np);
  3160. of_node_put(np);
  3161. of_node_put(base_child);
  3162. return;
  3163. }
  3164. }
  3165. }
  3166. /*
  3167. * overlay 'overlay_base' is not allowed to have root
  3168. * properties, so only need to splice nodes into main device tree.
  3169. *
  3170. * root node of *overlay_base_root will not be freed, it is lost
  3171. * memory.
  3172. */
  3173. for (np = overlay_base_root->child; np; np = np->sibling)
  3174. np->parent = of_root;
  3175. mutex_lock(&of_mutex);
  3176. for (last_sibling = np = of_root->child; np; np = np->sibling)
  3177. last_sibling = np;
  3178. if (last_sibling)
  3179. last_sibling->sibling = overlay_base_root->child;
  3180. else
  3181. of_root->child = overlay_base_root->child;
  3182. for_each_of_allnodes_from(overlay_base_root, np)
  3183. __of_attach_node_sysfs(np);
  3184. if (of_symbols) {
  3185. struct property *new_prop;
  3186. for_each_property_of_node(overlay_base_symbols, prop) {
  3187. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  3188. if (!new_prop) {
  3189. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  3190. prop->name);
  3191. goto err_unlock;
  3192. }
  3193. if (__of_add_property(of_symbols, new_prop)) {
  3194. __of_prop_free(new_prop);
  3195. /* "name" auto-generated by unflatten */
  3196. if (!strcmp(prop->name, "name"))
  3197. continue;
  3198. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  3199. prop->name);
  3200. goto err_unlock;
  3201. }
  3202. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  3203. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  3204. prop->name);
  3205. goto err_unlock;
  3206. }
  3207. }
  3208. }
  3209. mutex_unlock(&of_mutex);
  3210. /* now do the normal overlay usage test */
  3211. /* --- overlay --- */
  3212. EXPECT_BEGIN(KERN_ERR,
  3213. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
  3214. EXPECT_BEGIN(KERN_ERR,
  3215. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
  3216. EXPECT_BEGIN(KERN_ERR,
  3217. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
  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@40/incline-up");
  3220. EXPECT_BEGIN(KERN_ERR,
  3221. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
  3222. EXPECT_BEGIN(KERN_ERR,
  3223. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
  3224. EXPECT_BEGIN(KERN_ERR,
  3225. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
  3226. EXPECT_BEGIN(KERN_ERR,
  3227. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
  3228. EXPECT_BEGIN(KERN_ERR,
  3229. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
  3230. EXPECT_BEGIN(KERN_ERR,
  3231. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
  3232. EXPECT_BEGIN(KERN_ERR,
  3233. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
  3234. ret = overlay_data_apply("overlay", NULL);
  3235. EXPECT_END(KERN_ERR,
  3236. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
  3237. EXPECT_END(KERN_ERR,
  3238. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
  3239. EXPECT_END(KERN_ERR,
  3240. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
  3241. EXPECT_END(KERN_ERR,
  3242. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
  3243. EXPECT_END(KERN_ERR,
  3244. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
  3245. EXPECT_END(KERN_ERR,
  3246. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
  3247. EXPECT_END(KERN_ERR,
  3248. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
  3249. EXPECT_END(KERN_ERR,
  3250. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
  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@30/incline-up");
  3253. EXPECT_END(KERN_ERR,
  3254. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
  3255. EXPECT_END(KERN_ERR,
  3256. "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
  3257. unittest(ret, "Adding overlay 'overlay' failed\n");
  3258. /* --- overlay_bad_add_dup_node --- */
  3259. EXPECT_BEGIN(KERN_ERR,
  3260. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
  3261. EXPECT_BEGIN(KERN_ERR,
  3262. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
  3263. EXPECT_BEGIN(KERN_ERR,
  3264. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
  3265. EXPECT_BEGIN(KERN_ERR,
  3266. "OF: Error reverting changeset (-19)");
  3267. unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
  3268. "Adding overlay 'overlay_bad_add_dup_node' failed\n");
  3269. EXPECT_END(KERN_ERR,
  3270. "OF: Error reverting changeset (-19)");
  3271. EXPECT_END(KERN_ERR,
  3272. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
  3273. EXPECT_END(KERN_ERR,
  3274. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
  3275. EXPECT_END(KERN_ERR,
  3276. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
  3277. /* --- overlay_bad_add_dup_prop --- */
  3278. EXPECT_BEGIN(KERN_ERR,
  3279. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
  3280. EXPECT_BEGIN(KERN_ERR,
  3281. "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
  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/name");
  3284. EXPECT_BEGIN(KERN_ERR,
  3285. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
  3286. EXPECT_BEGIN(KERN_ERR,
  3287. "OF: Error reverting changeset (-19)");
  3288. unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
  3289. "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
  3290. EXPECT_END(KERN_ERR,
  3291. "OF: Error reverting changeset (-19)");
  3292. EXPECT_END(KERN_ERR,
  3293. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
  3294. EXPECT_END(KERN_ERR,
  3295. "OF: overlay: ERROR: multiple fragments add, update, and/or delete 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/rpm_avail");
  3298. EXPECT_END(KERN_ERR,
  3299. "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
  3300. /* --- overlay_bad_phandle --- */
  3301. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  3302. "Adding overlay 'overlay_bad_phandle' failed\n");
  3303. /* --- overlay_bad_symbol --- */
  3304. EXPECT_BEGIN(KERN_ERR,
  3305. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
  3306. EXPECT_BEGIN(KERN_ERR,
  3307. "OF: Error reverting changeset (-19)");
  3308. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  3309. "Adding overlay 'overlay_bad_symbol' failed\n");
  3310. EXPECT_END(KERN_ERR,
  3311. "OF: Error reverting changeset (-19)");
  3312. EXPECT_END(KERN_ERR,
  3313. "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
  3314. /* --- overlay_bad_unresolved --- */
  3315. EXPECT_BEGIN(KERN_ERR,
  3316. "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
  3317. EXPECT_BEGIN(KERN_ERR,
  3318. "OF: resolver: overlay phandle fixup failed: -22");
  3319. unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
  3320. "Adding overlay 'overlay_bad_unresolved' failed\n");
  3321. EXPECT_END(KERN_ERR,
  3322. "OF: resolver: overlay phandle fixup failed: -22");
  3323. EXPECT_END(KERN_ERR,
  3324. "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
  3325. return;
  3326. err_unlock:
  3327. mutex_unlock(&of_mutex);
  3328. }
  3329. static int of_unittest_pci_dev_num;
  3330. static int of_unittest_pci_child_num;
  3331. /*
  3332. * PCI device tree node test driver
  3333. */
  3334. static const struct pci_device_id testdrv_pci_ids[] = {
  3335. { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
  3336. { 0, }
  3337. };
  3338. static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  3339. {
  3340. struct overlay_info *info;
  3341. struct device_node *dn;
  3342. int ret, ovcs_id;
  3343. u32 size;
  3344. dn = pdev->dev.of_node;
  3345. if (!dn) {
  3346. dev_err(&pdev->dev, "does not find bus endpoint");
  3347. return -EINVAL;
  3348. }
  3349. for (info = overlays; info && info->name; info++) {
  3350. if (!strcmp(info->name, "overlay_pci_node"))
  3351. break;
  3352. }
  3353. if (!info || !info->name) {
  3354. dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
  3355. return -ENODEV;
  3356. }
  3357. size = info->dtbo_end - info->dtbo_begin;
  3358. ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
  3359. of_node_put(dn);
  3360. if (ret)
  3361. return ret;
  3362. of_platform_default_populate(dn, NULL, &pdev->dev);
  3363. pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
  3364. return 0;
  3365. }
  3366. static void testdrv_remove(struct pci_dev *pdev)
  3367. {
  3368. int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
  3369. of_platform_depopulate(&pdev->dev);
  3370. of_overlay_remove(&ovcs_id);
  3371. }
  3372. static struct pci_driver testdrv_driver = {
  3373. .name = "pci_dt_testdrv",
  3374. .id_table = testdrv_pci_ids,
  3375. .probe = testdrv_probe,
  3376. .remove = testdrv_remove,
  3377. };
  3378. static int unittest_pci_probe(struct platform_device *pdev)
  3379. {
  3380. struct resource *res;
  3381. struct device *dev;
  3382. u64 exp_addr;
  3383. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3384. if (!res)
  3385. return -ENODEV;
  3386. dev = &pdev->dev;
  3387. while (dev && !dev_is_pci(dev))
  3388. dev = dev->parent;
  3389. if (!dev) {
  3390. pr_err("unable to find parent device\n");
  3391. return -ENODEV;
  3392. }
  3393. exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
  3394. unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
  3395. (u64)res->start, exp_addr);
  3396. of_unittest_pci_child_num++;
  3397. return 0;
  3398. }
  3399. static const struct of_device_id unittest_pci_of_match[] = {
  3400. { .compatible = "unittest-pci" },
  3401. { }
  3402. };
  3403. static struct platform_driver unittest_pci_driver = {
  3404. .probe = unittest_pci_probe,
  3405. .driver = {
  3406. .name = "unittest-pci",
  3407. .of_match_table = unittest_pci_of_match,
  3408. },
  3409. };
  3410. static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
  3411. {
  3412. struct device_node *pnp, *np = NULL;
  3413. struct device *child_dev;
  3414. char *path = NULL;
  3415. const __be32 *reg;
  3416. int rc = 0;
  3417. pnp = pdev->dev.of_node;
  3418. unittest(pnp, "Failed creating PCI dt node\n");
  3419. if (!pnp)
  3420. return -ENODEV;
  3421. if (add) {
  3422. path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
  3423. np = of_find_node_by_path(path);
  3424. unittest(np, "Failed to get unittest-pci node under PCI node\n");
  3425. if (!np) {
  3426. rc = -ENODEV;
  3427. goto failed;
  3428. }
  3429. reg = of_get_property(np, "reg", NULL);
  3430. unittest(reg, "Failed to get reg property\n");
  3431. if (!reg)
  3432. rc = -ENODEV;
  3433. } else {
  3434. path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
  3435. np = of_find_node_by_path(path);
  3436. unittest(!np, "Child device tree node is not removed\n");
  3437. child_dev = device_find_any_child(&pdev->dev);
  3438. unittest(!child_dev, "Child device is not removed\n");
  3439. }
  3440. failed:
  3441. kfree(path);
  3442. if (np)
  3443. of_node_put(np);
  3444. return rc;
  3445. }
  3446. static void __init of_unittest_pci_node(void)
  3447. {
  3448. struct pci_dev *pdev = NULL;
  3449. int rc;
  3450. if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
  3451. return;
  3452. rc = pci_register_driver(&testdrv_driver);
  3453. unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
  3454. if (rc)
  3455. return;
  3456. rc = platform_driver_register(&unittest_pci_driver);
  3457. if (unittest(!rc, "Failed to register unittest pci driver\n")) {
  3458. pci_unregister_driver(&testdrv_driver);
  3459. return;
  3460. }
  3461. while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
  3462. of_unittest_pci_node_verify(pdev, true);
  3463. of_unittest_pci_dev_num++;
  3464. }
  3465. if (pdev)
  3466. pci_dev_put(pdev);
  3467. unittest(of_unittest_pci_dev_num,
  3468. "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
  3469. unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
  3470. "Child device number %d is not expected %d", of_unittest_pci_child_num,
  3471. of_unittest_pci_dev_num);
  3472. platform_driver_unregister(&unittest_pci_driver);
  3473. pci_unregister_driver(&testdrv_driver);
  3474. while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
  3475. of_unittest_pci_node_verify(pdev, false);
  3476. if (pdev)
  3477. pci_dev_put(pdev);
  3478. }
  3479. #else
  3480. static inline __init void of_unittest_overlay_high_level(void) {}
  3481. static inline __init void of_unittest_pci_node(void) { }
  3482. #endif
  3483. static int __init of_unittest(void)
  3484. {
  3485. struct device_node *np;
  3486. int res;
  3487. pr_info("start of unittest - you will see error messages\n");
  3488. /* Taint the kernel so we know we've run tests. */
  3489. add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
  3490. /* adding data for unittest */
  3491. res = unittest_data_add();
  3492. if (res)
  3493. return res;
  3494. if (!of_aliases)
  3495. of_aliases = of_find_node_by_path("/aliases");
  3496. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  3497. if (!np) {
  3498. pr_info("No testcase data in device tree; not running tests\n");
  3499. return 0;
  3500. }
  3501. of_node_put(np);
  3502. of_unittest_check_tree_linkage();
  3503. of_unittest_check_phandles();
  3504. of_unittest_find_node_by_name();
  3505. of_unittest_dynamic();
  3506. of_unittest_parse_phandle_with_args();
  3507. of_unittest_parse_phandle_with_args_map();
  3508. of_unittest_printf();
  3509. of_unittest_property_string();
  3510. of_unittest_property_copy();
  3511. of_unittest_changeset();
  3512. of_unittest_changeset_prop();
  3513. of_unittest_parse_interrupts();
  3514. of_unittest_parse_interrupts_extended();
  3515. of_unittest_dma_get_max_cpu_address();
  3516. of_unittest_parse_dma_ranges();
  3517. of_unittest_pci_dma_ranges();
  3518. of_unittest_bus_ranges();
  3519. of_unittest_bus_3cell_ranges();
  3520. of_unittest_reg();
  3521. of_unittest_translate_addr();
  3522. of_unittest_match_node();
  3523. of_unittest_platform_populate();
  3524. of_unittest_overlay();
  3525. of_unittest_lifecycle();
  3526. of_unittest_pci_node();
  3527. /* Double check linkage after removing testcase data */
  3528. of_unittest_check_tree_linkage();
  3529. of_unittest_overlay_high_level();
  3530. pr_info("end of unittest - %i passed, %i failed\n",
  3531. unittest_results.passed, unittest_results.failed);
  3532. return 0;
  3533. }
  3534. late_initcall(of_unittest);