unittest.c 99 KB

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