unittest.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  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/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/hashtable.h>
  11. #include <linux/libfdt.h>
  12. #include <linux/of.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/list.h>
  17. #include <linux/mutex.h>
  18. #include <linux/slab.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-mux.h>
  23. #include <linux/bitops.h>
  24. #include "of_private.h"
  25. static struct unittest_results {
  26. int passed;
  27. int failed;
  28. } unittest_results;
  29. #define unittest(result, fmt, ...) ({ \
  30. bool failed = !(result); \
  31. if (failed) { \
  32. unittest_results.failed++; \
  33. pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
  34. } else { \
  35. unittest_results.passed++; \
  36. pr_debug("pass %s():%i\n", __func__, __LINE__); \
  37. } \
  38. failed; \
  39. })
  40. static void __init of_unittest_find_node_by_name(void)
  41. {
  42. struct device_node *np;
  43. const char *options, *name;
  44. np = of_find_node_by_path("/testcase-data");
  45. name = kasprintf(GFP_KERNEL, "%pOF", np);
  46. unittest(np && !strcmp("/testcase-data", name),
  47. "find /testcase-data failed\n");
  48. of_node_put(np);
  49. kfree(name);
  50. /* Test if trailing '/' works */
  51. np = of_find_node_by_path("/testcase-data/");
  52. unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
  53. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  54. name = kasprintf(GFP_KERNEL, "%pOF", np);
  55. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  56. "find /testcase-data/phandle-tests/consumer-a failed\n");
  57. of_node_put(np);
  58. kfree(name);
  59. np = of_find_node_by_path("testcase-alias");
  60. name = kasprintf(GFP_KERNEL, "%pOF", np);
  61. unittest(np && !strcmp("/testcase-data", name),
  62. "find testcase-alias failed\n");
  63. of_node_put(np);
  64. kfree(name);
  65. /* Test if trailing '/' works on aliases */
  66. np = of_find_node_by_path("testcase-alias/");
  67. unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
  68. np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
  69. name = kasprintf(GFP_KERNEL, "%pOF", np);
  70. unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
  71. "find testcase-alias/phandle-tests/consumer-a failed\n");
  72. of_node_put(np);
  73. kfree(name);
  74. np = of_find_node_by_path("/testcase-data/missing-path");
  75. unittest(!np, "non-existent path returned node %pOF\n", np);
  76. of_node_put(np);
  77. np = of_find_node_by_path("missing-alias");
  78. unittest(!np, "non-existent alias returned node %pOF\n", np);
  79. of_node_put(np);
  80. np = of_find_node_by_path("testcase-alias/missing-path");
  81. unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
  82. of_node_put(np);
  83. np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
  84. unittest(np && !strcmp("testoption", options),
  85. "option path test failed\n");
  86. of_node_put(np);
  87. np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
  88. unittest(np && !strcmp("test/option", options),
  89. "option path test, subcase #1 failed\n");
  90. of_node_put(np);
  91. np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
  92. unittest(np && !strcmp("test/option", options),
  93. "option path test, subcase #2 failed\n");
  94. of_node_put(np);
  95. np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
  96. unittest(np, "NULL option path test failed\n");
  97. of_node_put(np);
  98. np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
  99. &options);
  100. unittest(np && !strcmp("testaliasoption", options),
  101. "option alias path test failed\n");
  102. of_node_put(np);
  103. np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
  104. &options);
  105. unittest(np && !strcmp("test/alias/option", options),
  106. "option alias path test, subcase #1 failed\n");
  107. of_node_put(np);
  108. np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
  109. unittest(np, "NULL option alias path test failed\n");
  110. of_node_put(np);
  111. options = "testoption";
  112. np = of_find_node_opts_by_path("testcase-alias", &options);
  113. unittest(np && !options, "option clearing test failed\n");
  114. of_node_put(np);
  115. options = "testoption";
  116. np = of_find_node_opts_by_path("/", &options);
  117. unittest(np && !options, "option clearing root node test failed\n");
  118. of_node_put(np);
  119. }
  120. static void __init of_unittest_dynamic(void)
  121. {
  122. struct device_node *np;
  123. struct property *prop;
  124. np = of_find_node_by_path("/testcase-data");
  125. if (!np) {
  126. pr_err("missing testcase data\n");
  127. return;
  128. }
  129. /* Array of 4 properties for the purpose of testing */
  130. prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
  131. if (!prop) {
  132. unittest(0, "kzalloc() failed\n");
  133. return;
  134. }
  135. /* Add a new property - should pass*/
  136. prop->name = "new-property";
  137. prop->value = "new-property-data";
  138. prop->length = strlen(prop->value) + 1;
  139. unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
  140. /* Try to add an existing property - should fail */
  141. prop++;
  142. prop->name = "new-property";
  143. prop->value = "new-property-data-should-fail";
  144. prop->length = strlen(prop->value) + 1;
  145. unittest(of_add_property(np, prop) != 0,
  146. "Adding an existing property should have failed\n");
  147. /* Try to modify an existing property - should pass */
  148. prop->value = "modify-property-data-should-pass";
  149. prop->length = strlen(prop->value) + 1;
  150. unittest(of_update_property(np, prop) == 0,
  151. "Updating an existing property should have passed\n");
  152. /* Try to modify non-existent property - should pass*/
  153. prop++;
  154. prop->name = "modify-property";
  155. prop->value = "modify-missing-property-data-should-pass";
  156. prop->length = strlen(prop->value) + 1;
  157. unittest(of_update_property(np, prop) == 0,
  158. "Updating a missing property should have passed\n");
  159. /* Remove property - should pass */
  160. unittest(of_remove_property(np, prop) == 0,
  161. "Removing a property should have passed\n");
  162. /* Adding very large property - should pass */
  163. prop++;
  164. prop->name = "large-property-PAGE_SIZEx8";
  165. prop->length = PAGE_SIZE * 8;
  166. prop->value = kzalloc(prop->length, GFP_KERNEL);
  167. unittest(prop->value != NULL, "Unable to allocate large buffer\n");
  168. if (prop->value)
  169. unittest(of_add_property(np, prop) == 0,
  170. "Adding a large property should have passed\n");
  171. }
  172. static int __init of_unittest_check_node_linkage(struct device_node *np)
  173. {
  174. struct device_node *child;
  175. int count = 0, rc;
  176. for_each_child_of_node(np, child) {
  177. if (child->parent != np) {
  178. pr_err("Child node %pOFn links to wrong parent %pOFn\n",
  179. child, np);
  180. rc = -EINVAL;
  181. goto put_child;
  182. }
  183. rc = of_unittest_check_node_linkage(child);
  184. if (rc < 0)
  185. goto put_child;
  186. count += rc;
  187. }
  188. return count + 1;
  189. put_child:
  190. of_node_put(child);
  191. return rc;
  192. }
  193. static void __init of_unittest_check_tree_linkage(void)
  194. {
  195. struct device_node *np;
  196. int allnode_count = 0, child_count;
  197. if (!of_root)
  198. return;
  199. for_each_of_allnodes(np)
  200. allnode_count++;
  201. child_count = of_unittest_check_node_linkage(of_root);
  202. unittest(child_count > 0, "Device node data structure is corrupted\n");
  203. unittest(child_count == allnode_count,
  204. "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
  205. allnode_count, child_count);
  206. pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
  207. }
  208. static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
  209. const char *expected)
  210. {
  211. unsigned char *buf;
  212. int buf_size;
  213. int size, i;
  214. buf_size = strlen(expected) + 10;
  215. buf = kmalloc(buf_size, GFP_KERNEL);
  216. if (!buf)
  217. return;
  218. /* Baseline; check conversion with a large size limit */
  219. memset(buf, 0xff, buf_size);
  220. size = snprintf(buf, buf_size - 2, fmt, np);
  221. /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
  222. unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
  223. "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
  224. fmt, expected, buf);
  225. /* Make sure length limits work */
  226. size++;
  227. for (i = 0; i < 2; i++, size--) {
  228. /* Clear the buffer, and make sure it works correctly still */
  229. memset(buf, 0xff, buf_size);
  230. snprintf(buf, size+1, fmt, np);
  231. unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
  232. "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
  233. size, fmt, expected, buf);
  234. }
  235. kfree(buf);
  236. }
  237. static void __init of_unittest_printf(void)
  238. {
  239. struct device_node *np;
  240. const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
  241. char phandle_str[16] = "";
  242. np = of_find_node_by_path(full_name);
  243. if (!np) {
  244. unittest(np, "testcase data missing\n");
  245. return;
  246. }
  247. num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
  248. of_unittest_printf_one(np, "%pOF", full_name);
  249. of_unittest_printf_one(np, "%pOFf", full_name);
  250. of_unittest_printf_one(np, "%pOFn", "dev");
  251. of_unittest_printf_one(np, "%2pOFn", "dev");
  252. of_unittest_printf_one(np, "%5pOFn", " dev");
  253. of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
  254. of_unittest_printf_one(np, "%pOFp", phandle_str);
  255. of_unittest_printf_one(np, "%pOFP", "dev@100");
  256. of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
  257. of_unittest_printf_one(np, "%10pOFP", " dev@100");
  258. of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
  259. of_unittest_printf_one(of_root, "%pOFP", "/");
  260. of_unittest_printf_one(np, "%pOFF", "----");
  261. of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
  262. of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
  263. of_unittest_printf_one(np, "%pOFc", "test-sub-device");
  264. of_unittest_printf_one(np, "%pOFC",
  265. "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
  266. }
  267. struct node_hash {
  268. struct hlist_node node;
  269. struct device_node *np;
  270. };
  271. static DEFINE_HASHTABLE(phandle_ht, 8);
  272. static void __init of_unittest_check_phandles(void)
  273. {
  274. struct device_node *np;
  275. struct node_hash *nh;
  276. struct hlist_node *tmp;
  277. int i, dup_count = 0, phandle_count = 0;
  278. for_each_of_allnodes(np) {
  279. if (!np->phandle)
  280. continue;
  281. hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
  282. if (nh->np->phandle == np->phandle) {
  283. pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
  284. np->phandle, nh->np, np);
  285. dup_count++;
  286. break;
  287. }
  288. }
  289. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  290. if (WARN_ON(!nh))
  291. return;
  292. nh->np = np;
  293. hash_add(phandle_ht, &nh->node, np->phandle);
  294. phandle_count++;
  295. }
  296. unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
  297. dup_count, phandle_count);
  298. /* Clean up */
  299. hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
  300. hash_del(&nh->node);
  301. kfree(nh);
  302. }
  303. }
  304. static void __init of_unittest_parse_phandle_with_args(void)
  305. {
  306. struct device_node *np;
  307. struct of_phandle_args args;
  308. int i, rc;
  309. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  310. if (!np) {
  311. pr_err("missing testcase data\n");
  312. return;
  313. }
  314. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  315. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  316. for (i = 0; i < 8; i++) {
  317. bool passed = true;
  318. rc = of_parse_phandle_with_args(np, "phandle-list",
  319. "#phandle-cells", i, &args);
  320. /* Test the values from tests-phandle.dtsi */
  321. switch (i) {
  322. case 0:
  323. passed &= !rc;
  324. passed &= (args.args_count == 1);
  325. passed &= (args.args[0] == (i + 1));
  326. break;
  327. case 1:
  328. passed &= !rc;
  329. passed &= (args.args_count == 2);
  330. passed &= (args.args[0] == (i + 1));
  331. passed &= (args.args[1] == 0);
  332. break;
  333. case 2:
  334. passed &= (rc == -ENOENT);
  335. break;
  336. case 3:
  337. passed &= !rc;
  338. passed &= (args.args_count == 3);
  339. passed &= (args.args[0] == (i + 1));
  340. passed &= (args.args[1] == 4);
  341. passed &= (args.args[2] == 3);
  342. break;
  343. case 4:
  344. passed &= !rc;
  345. passed &= (args.args_count == 2);
  346. passed &= (args.args[0] == (i + 1));
  347. passed &= (args.args[1] == 100);
  348. break;
  349. case 5:
  350. passed &= !rc;
  351. passed &= (args.args_count == 0);
  352. break;
  353. case 6:
  354. passed &= !rc;
  355. passed &= (args.args_count == 1);
  356. passed &= (args.args[0] == (i + 1));
  357. break;
  358. case 7:
  359. passed &= (rc == -ENOENT);
  360. break;
  361. default:
  362. passed = false;
  363. }
  364. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  365. i, args.np, rc);
  366. }
  367. /* Check for missing list property */
  368. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  369. "#phandle-cells", 0, &args);
  370. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  371. rc = of_count_phandle_with_args(np, "phandle-list-missing",
  372. "#phandle-cells");
  373. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  374. /* Check for missing cells property */
  375. rc = of_parse_phandle_with_args(np, "phandle-list",
  376. "#phandle-cells-missing", 0, &args);
  377. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  378. rc = of_count_phandle_with_args(np, "phandle-list",
  379. "#phandle-cells-missing");
  380. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  381. /* Check for bad phandle in list */
  382. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  383. "#phandle-cells", 0, &args);
  384. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  385. rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
  386. "#phandle-cells");
  387. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  388. /* Check for incorrectly formed argument list */
  389. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  390. "#phandle-cells", 1, &args);
  391. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  392. rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
  393. "#phandle-cells");
  394. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  395. }
  396. static void __init of_unittest_parse_phandle_with_args_map(void)
  397. {
  398. struct device_node *np, *p0, *p1, *p2, *p3;
  399. struct of_phandle_args args;
  400. int i, rc;
  401. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
  402. if (!np) {
  403. pr_err("missing testcase data\n");
  404. return;
  405. }
  406. p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
  407. if (!p0) {
  408. pr_err("missing testcase data\n");
  409. return;
  410. }
  411. p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
  412. if (!p1) {
  413. pr_err("missing testcase data\n");
  414. return;
  415. }
  416. p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
  417. if (!p2) {
  418. pr_err("missing testcase data\n");
  419. return;
  420. }
  421. p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
  422. if (!p3) {
  423. pr_err("missing testcase data\n");
  424. return;
  425. }
  426. rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
  427. unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
  428. for (i = 0; i < 8; i++) {
  429. bool passed = true;
  430. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  431. "phandle", i, &args);
  432. /* Test the values from tests-phandle.dtsi */
  433. switch (i) {
  434. case 0:
  435. passed &= !rc;
  436. passed &= (args.np == p1);
  437. passed &= (args.args_count == 1);
  438. passed &= (args.args[0] == 1);
  439. break;
  440. case 1:
  441. passed &= !rc;
  442. passed &= (args.np == p3);
  443. passed &= (args.args_count == 3);
  444. passed &= (args.args[0] == 2);
  445. passed &= (args.args[1] == 5);
  446. passed &= (args.args[2] == 3);
  447. break;
  448. case 2:
  449. passed &= (rc == -ENOENT);
  450. break;
  451. case 3:
  452. passed &= !rc;
  453. passed &= (args.np == p0);
  454. passed &= (args.args_count == 0);
  455. break;
  456. case 4:
  457. passed &= !rc;
  458. passed &= (args.np == p1);
  459. passed &= (args.args_count == 1);
  460. passed &= (args.args[0] == 3);
  461. break;
  462. case 5:
  463. passed &= !rc;
  464. passed &= (args.np == p0);
  465. passed &= (args.args_count == 0);
  466. break;
  467. case 6:
  468. passed &= !rc;
  469. passed &= (args.np == p2);
  470. passed &= (args.args_count == 2);
  471. passed &= (args.args[0] == 15);
  472. passed &= (args.args[1] == 0x20);
  473. break;
  474. case 7:
  475. passed &= (rc == -ENOENT);
  476. break;
  477. default:
  478. passed = false;
  479. }
  480. unittest(passed, "index %i - data error on node %s rc=%i\n",
  481. i, args.np->full_name, rc);
  482. }
  483. /* Check for missing list property */
  484. rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
  485. "phandle", 0, &args);
  486. unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  487. /* Check for missing cells,map,mask property */
  488. rc = of_parse_phandle_with_args_map(np, "phandle-list",
  489. "phandle-missing", 0, &args);
  490. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  491. /* Check for bad phandle in list */
  492. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
  493. "phandle", 0, &args);
  494. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  495. /* Check for incorrectly formed argument list */
  496. rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
  497. "phandle", 1, &args);
  498. unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  499. }
  500. static void __init of_unittest_property_string(void)
  501. {
  502. const char *strings[4];
  503. struct device_node *np;
  504. int rc;
  505. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  506. if (!np) {
  507. pr_err("No testcase data in device tree\n");
  508. return;
  509. }
  510. rc = of_property_match_string(np, "phandle-list-names", "first");
  511. unittest(rc == 0, "first expected:0 got:%i\n", rc);
  512. rc = of_property_match_string(np, "phandle-list-names", "second");
  513. unittest(rc == 1, "second expected:1 got:%i\n", rc);
  514. rc = of_property_match_string(np, "phandle-list-names", "third");
  515. unittest(rc == 2, "third expected:2 got:%i\n", rc);
  516. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  517. unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  518. rc = of_property_match_string(np, "missing-property", "blah");
  519. unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  520. rc = of_property_match_string(np, "empty-property", "blah");
  521. unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  522. rc = of_property_match_string(np, "unterminated-string", "blah");
  523. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  524. /* of_property_count_strings() tests */
  525. rc = of_property_count_strings(np, "string-property");
  526. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  527. rc = of_property_count_strings(np, "phandle-list-names");
  528. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  529. rc = of_property_count_strings(np, "unterminated-string");
  530. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  531. rc = of_property_count_strings(np, "unterminated-string-list");
  532. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  533. /* of_property_read_string_index() tests */
  534. rc = of_property_read_string_index(np, "string-property", 0, strings);
  535. unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  536. strings[0] = NULL;
  537. rc = of_property_read_string_index(np, "string-property", 1, strings);
  538. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  539. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  540. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  541. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  542. unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  543. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  544. unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  545. strings[0] = NULL;
  546. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  547. unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  548. strings[0] = NULL;
  549. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  550. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  551. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  552. unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  553. strings[0] = NULL;
  554. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  555. unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  556. strings[1] = NULL;
  557. /* of_property_read_string_array() tests */
  558. rc = of_property_read_string_array(np, "string-property", strings, 4);
  559. unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  560. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  561. unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  562. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  563. unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  564. /* -- An incorrectly formed string should cause a failure */
  565. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  566. unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  567. /* -- parsing the correctly formed strings should still work: */
  568. strings[2] = NULL;
  569. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  570. unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  571. strings[1] = NULL;
  572. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  573. unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  574. }
  575. #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
  576. (p1)->value && (p2)->value && \
  577. !memcmp((p1)->value, (p2)->value, (p1)->length) && \
  578. !strcmp((p1)->name, (p2)->name))
  579. static void __init of_unittest_property_copy(void)
  580. {
  581. #ifdef CONFIG_OF_DYNAMIC
  582. struct property p1 = { .name = "p1", .length = 0, .value = "" };
  583. struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
  584. struct property *new;
  585. new = __of_prop_dup(&p1, GFP_KERNEL);
  586. unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
  587. kfree(new->value);
  588. kfree(new->name);
  589. kfree(new);
  590. new = __of_prop_dup(&p2, GFP_KERNEL);
  591. unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
  592. kfree(new->value);
  593. kfree(new->name);
  594. kfree(new);
  595. #endif
  596. }
  597. static void __init of_unittest_changeset(void)
  598. {
  599. #ifdef CONFIG_OF_DYNAMIC
  600. struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
  601. struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
  602. struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
  603. struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
  604. struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
  605. struct property *ppremove;
  606. struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
  607. struct of_changeset chgset;
  608. n1 = __of_node_dup(NULL, "n1");
  609. unittest(n1, "testcase setup failure\n");
  610. n2 = __of_node_dup(NULL, "n2");
  611. unittest(n2, "testcase setup failure\n");
  612. n21 = __of_node_dup(NULL, "n21");
  613. unittest(n21, "testcase setup failure %p\n", n21);
  614. nchangeset = of_find_node_by_path("/testcase-data/changeset");
  615. nremove = of_get_child_by_name(nchangeset, "node-remove");
  616. unittest(nremove, "testcase setup failure\n");
  617. ppadd = __of_prop_dup(&padd, GFP_KERNEL);
  618. unittest(ppadd, "testcase setup failure\n");
  619. ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
  620. unittest(ppname_n1, "testcase setup failure\n");
  621. ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
  622. unittest(ppname_n2, "testcase setup failure\n");
  623. ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
  624. unittest(ppname_n21, "testcase setup failure\n");
  625. ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
  626. unittest(ppupdate, "testcase setup failure\n");
  627. parent = nchangeset;
  628. n1->parent = parent;
  629. n2->parent = parent;
  630. n21->parent = n2;
  631. ppremove = of_find_property(parent, "prop-remove", NULL);
  632. unittest(ppremove, "failed to find removal prop");
  633. of_changeset_init(&chgset);
  634. unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
  635. unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
  636. unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
  637. unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
  638. unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
  639. unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
  640. unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
  641. unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
  642. unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
  643. unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
  644. unittest(!of_changeset_apply(&chgset), "apply failed\n");
  645. of_node_put(nchangeset);
  646. /* Make sure node names are constructed correctly */
  647. unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
  648. "'%pOF' not added\n", n21);
  649. of_node_put(np);
  650. unittest(!of_changeset_revert(&chgset), "revert failed\n");
  651. of_changeset_destroy(&chgset);
  652. #endif
  653. }
  654. static void __init of_unittest_parse_interrupts(void)
  655. {
  656. struct device_node *np;
  657. struct of_phandle_args args;
  658. int i, rc;
  659. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  660. return;
  661. np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
  662. if (!np) {
  663. pr_err("missing testcase data\n");
  664. return;
  665. }
  666. for (i = 0; i < 4; i++) {
  667. bool passed = true;
  668. args.args_count = 0;
  669. rc = of_irq_parse_one(np, i, &args);
  670. passed &= !rc;
  671. passed &= (args.args_count == 1);
  672. passed &= (args.args[0] == (i + 1));
  673. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  674. i, args.np, rc);
  675. }
  676. of_node_put(np);
  677. np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
  678. if (!np) {
  679. pr_err("missing testcase data\n");
  680. return;
  681. }
  682. for (i = 0; i < 4; i++) {
  683. bool passed = true;
  684. args.args_count = 0;
  685. rc = of_irq_parse_one(np, i, &args);
  686. /* Test the values from tests-phandle.dtsi */
  687. switch (i) {
  688. case 0:
  689. passed &= !rc;
  690. passed &= (args.args_count == 1);
  691. passed &= (args.args[0] == 9);
  692. break;
  693. case 1:
  694. passed &= !rc;
  695. passed &= (args.args_count == 3);
  696. passed &= (args.args[0] == 10);
  697. passed &= (args.args[1] == 11);
  698. passed &= (args.args[2] == 12);
  699. break;
  700. case 2:
  701. passed &= !rc;
  702. passed &= (args.args_count == 2);
  703. passed &= (args.args[0] == 13);
  704. passed &= (args.args[1] == 14);
  705. break;
  706. case 3:
  707. passed &= !rc;
  708. passed &= (args.args_count == 2);
  709. passed &= (args.args[0] == 15);
  710. passed &= (args.args[1] == 16);
  711. break;
  712. default:
  713. passed = false;
  714. }
  715. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  716. i, args.np, rc);
  717. }
  718. of_node_put(np);
  719. }
  720. static void __init of_unittest_parse_interrupts_extended(void)
  721. {
  722. struct device_node *np;
  723. struct of_phandle_args args;
  724. int i, rc;
  725. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  726. return;
  727. np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
  728. if (!np) {
  729. pr_err("missing testcase data\n");
  730. return;
  731. }
  732. for (i = 0; i < 7; i++) {
  733. bool passed = true;
  734. rc = of_irq_parse_one(np, i, &args);
  735. /* Test the values from tests-phandle.dtsi */
  736. switch (i) {
  737. case 0:
  738. passed &= !rc;
  739. passed &= (args.args_count == 1);
  740. passed &= (args.args[0] == 1);
  741. break;
  742. case 1:
  743. passed &= !rc;
  744. passed &= (args.args_count == 3);
  745. passed &= (args.args[0] == 2);
  746. passed &= (args.args[1] == 3);
  747. passed &= (args.args[2] == 4);
  748. break;
  749. case 2:
  750. passed &= !rc;
  751. passed &= (args.args_count == 2);
  752. passed &= (args.args[0] == 5);
  753. passed &= (args.args[1] == 6);
  754. break;
  755. case 3:
  756. passed &= !rc;
  757. passed &= (args.args_count == 1);
  758. passed &= (args.args[0] == 9);
  759. break;
  760. case 4:
  761. passed &= !rc;
  762. passed &= (args.args_count == 3);
  763. passed &= (args.args[0] == 10);
  764. passed &= (args.args[1] == 11);
  765. passed &= (args.args[2] == 12);
  766. break;
  767. case 5:
  768. passed &= !rc;
  769. passed &= (args.args_count == 2);
  770. passed &= (args.args[0] == 13);
  771. passed &= (args.args[1] == 14);
  772. break;
  773. case 6:
  774. passed &= !rc;
  775. passed &= (args.args_count == 1);
  776. passed &= (args.args[0] == 15);
  777. break;
  778. default:
  779. passed = false;
  780. }
  781. unittest(passed, "index %i - data error on node %pOF rc=%i\n",
  782. i, args.np, rc);
  783. }
  784. of_node_put(np);
  785. }
  786. static const struct of_device_id match_node_table[] = {
  787. { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
  788. { .data = "B", .type = "type1", }, /* followed by type alone */
  789. { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
  790. { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
  791. { .data = "Cc", .name = "name2", .type = "type2", },
  792. { .data = "E", .compatible = "compat3" },
  793. { .data = "G", .compatible = "compat2", },
  794. { .data = "H", .compatible = "compat2", .name = "name5", },
  795. { .data = "I", .compatible = "compat2", .type = "type1", },
  796. { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
  797. { .data = "K", .compatible = "compat2", .name = "name9", },
  798. {}
  799. };
  800. static struct {
  801. const char *path;
  802. const char *data;
  803. } match_node_tests[] = {
  804. { .path = "/testcase-data/match-node/name0", .data = "A", },
  805. { .path = "/testcase-data/match-node/name1", .data = "B", },
  806. { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
  807. { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
  808. { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
  809. { .path = "/testcase-data/match-node/name3", .data = "E", },
  810. { .path = "/testcase-data/match-node/name4", .data = "G", },
  811. { .path = "/testcase-data/match-node/name5", .data = "H", },
  812. { .path = "/testcase-data/match-node/name6", .data = "G", },
  813. { .path = "/testcase-data/match-node/name7", .data = "I", },
  814. { .path = "/testcase-data/match-node/name8", .data = "J", },
  815. { .path = "/testcase-data/match-node/name9", .data = "K", },
  816. };
  817. static void __init of_unittest_match_node(void)
  818. {
  819. struct device_node *np;
  820. const struct of_device_id *match;
  821. int i;
  822. for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
  823. np = of_find_node_by_path(match_node_tests[i].path);
  824. if (!np) {
  825. unittest(0, "missing testcase node %s\n",
  826. match_node_tests[i].path);
  827. continue;
  828. }
  829. match = of_match_node(match_node_table, np);
  830. if (!match) {
  831. unittest(0, "%s didn't match anything\n",
  832. match_node_tests[i].path);
  833. continue;
  834. }
  835. if (strcmp(match->data, match_node_tests[i].data) != 0) {
  836. unittest(0, "%s got wrong match. expected %s, got %s\n",
  837. match_node_tests[i].path, match_node_tests[i].data,
  838. (const char *)match->data);
  839. continue;
  840. }
  841. unittest(1, "passed");
  842. }
  843. }
  844. static struct resource test_bus_res = {
  845. .start = 0xfffffff8,
  846. .end = 0xfffffff9,
  847. .flags = IORESOURCE_MEM,
  848. };
  849. static const struct platform_device_info test_bus_info = {
  850. .name = "unittest-bus",
  851. };
  852. static void __init of_unittest_platform_populate(void)
  853. {
  854. int irq, rc;
  855. struct device_node *np, *child, *grandchild;
  856. struct platform_device *pdev, *test_bus;
  857. const struct of_device_id match[] = {
  858. { .compatible = "test-device", },
  859. {}
  860. };
  861. np = of_find_node_by_path("/testcase-data");
  862. of_platform_default_populate(np, NULL, NULL);
  863. /* Test that a missing irq domain returns -EPROBE_DEFER */
  864. np = of_find_node_by_path("/testcase-data/testcase-device1");
  865. pdev = of_find_device_by_node(np);
  866. unittest(pdev, "device 1 creation failed\n");
  867. if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
  868. irq = platform_get_irq(pdev, 0);
  869. unittest(irq == -EPROBE_DEFER,
  870. "device deferred probe failed - %d\n", irq);
  871. /* Test that a parsing failure does not return -EPROBE_DEFER */
  872. np = of_find_node_by_path("/testcase-data/testcase-device2");
  873. pdev = of_find_device_by_node(np);
  874. unittest(pdev, "device 2 creation failed\n");
  875. irq = platform_get_irq(pdev, 0);
  876. unittest(irq < 0 && irq != -EPROBE_DEFER,
  877. "device parsing error failed - %d\n", irq);
  878. }
  879. np = of_find_node_by_path("/testcase-data/platform-tests");
  880. unittest(np, "No testcase data in device tree\n");
  881. if (!np)
  882. return;
  883. test_bus = platform_device_register_full(&test_bus_info);
  884. rc = PTR_ERR_OR_ZERO(test_bus);
  885. unittest(!rc, "testbus registration failed; rc=%i\n", rc);
  886. if (rc)
  887. return;
  888. test_bus->dev.of_node = np;
  889. /*
  890. * Add a dummy resource to the test bus node after it is
  891. * registered to catch problems with un-inserted resources. The
  892. * DT code doesn't insert the resources, and it has caused the
  893. * kernel to oops in the past. This makes sure the same bug
  894. * doesn't crop up again.
  895. */
  896. platform_device_add_resources(test_bus, &test_bus_res, 1);
  897. of_platform_populate(np, match, NULL, &test_bus->dev);
  898. for_each_child_of_node(np, child) {
  899. for_each_child_of_node(child, grandchild)
  900. unittest(of_find_device_by_node(grandchild),
  901. "Could not create device for node '%pOFn'\n",
  902. grandchild);
  903. }
  904. of_platform_depopulate(&test_bus->dev);
  905. for_each_child_of_node(np, child) {
  906. for_each_child_of_node(child, grandchild)
  907. unittest(!of_find_device_by_node(grandchild),
  908. "device didn't get destroyed '%pOFn'\n",
  909. grandchild);
  910. }
  911. platform_device_unregister(test_bus);
  912. of_node_put(np);
  913. }
  914. /**
  915. * update_node_properties - adds the properties
  916. * of np into dup node (present in live tree) and
  917. * updates parent of children of np to dup.
  918. *
  919. * @np: node already present in live tree
  920. * @dup: node present in live tree to be updated
  921. */
  922. static void update_node_properties(struct device_node *np,
  923. struct device_node *dup)
  924. {
  925. struct property *prop;
  926. struct device_node *child;
  927. for_each_property_of_node(np, prop)
  928. of_add_property(dup, prop);
  929. for_each_child_of_node(np, child)
  930. child->parent = dup;
  931. }
  932. /**
  933. * attach_node_and_children - attaches nodes
  934. * and its children to live tree
  935. *
  936. * @np: Node to attach to live tree
  937. */
  938. static int attach_node_and_children(struct device_node *np)
  939. {
  940. struct device_node *next, *dup, *child;
  941. unsigned long flags;
  942. const char *full_name;
  943. full_name = kasprintf(GFP_KERNEL, "%pOF", np);
  944. dup = of_find_node_by_path(full_name);
  945. kfree(full_name);
  946. if (dup) {
  947. update_node_properties(np, dup);
  948. return 0;
  949. }
  950. child = np->child;
  951. np->child = NULL;
  952. mutex_lock(&of_mutex);
  953. raw_spin_lock_irqsave(&devtree_lock, flags);
  954. np->sibling = np->parent->child;
  955. np->parent->child = np;
  956. of_node_clear_flag(np, OF_DETACHED);
  957. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  958. __of_attach_node_sysfs(np);
  959. mutex_unlock(&of_mutex);
  960. while (child) {
  961. next = child->sibling;
  962. attach_node_and_children(child);
  963. child = next;
  964. }
  965. return 0;
  966. }
  967. /**
  968. * unittest_data_add - Reads, copies data from
  969. * linked tree and attaches it to the live tree
  970. */
  971. static int __init unittest_data_add(void)
  972. {
  973. void *unittest_data;
  974. struct device_node *unittest_data_node, *np;
  975. /*
  976. * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
  977. * created by cmd_dt_S_dtb in scripts/Makefile.lib
  978. */
  979. extern uint8_t __dtb_testcases_begin[];
  980. extern uint8_t __dtb_testcases_end[];
  981. const int size = __dtb_testcases_end - __dtb_testcases_begin;
  982. int rc;
  983. if (!size) {
  984. pr_warn("%s: No testcase data to attach; not running tests\n",
  985. __func__);
  986. return -ENODATA;
  987. }
  988. /* creating copy */
  989. unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
  990. if (!unittest_data) {
  991. pr_warn("%s: Failed to allocate memory for unittest_data; "
  992. "not running tests\n", __func__);
  993. return -ENOMEM;
  994. }
  995. of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
  996. if (!unittest_data_node) {
  997. pr_warn("%s: No tree to attach; not running tests\n", __func__);
  998. return -ENODATA;
  999. }
  1000. /*
  1001. * This lock normally encloses of_resolve_phandles()
  1002. */
  1003. of_overlay_mutex_lock();
  1004. rc = of_resolve_phandles(unittest_data_node);
  1005. if (rc) {
  1006. pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
  1007. of_overlay_mutex_unlock();
  1008. return -EINVAL;
  1009. }
  1010. if (!of_root) {
  1011. of_root = unittest_data_node;
  1012. for_each_of_allnodes(np)
  1013. __of_attach_node_sysfs(np);
  1014. of_aliases = of_find_node_by_path("/aliases");
  1015. of_chosen = of_find_node_by_path("/chosen");
  1016. of_overlay_mutex_unlock();
  1017. return 0;
  1018. }
  1019. /* attach the sub-tree to live tree */
  1020. np = unittest_data_node->child;
  1021. while (np) {
  1022. struct device_node *next = np->sibling;
  1023. np->parent = of_root;
  1024. attach_node_and_children(np);
  1025. np = next;
  1026. }
  1027. of_overlay_mutex_unlock();
  1028. return 0;
  1029. }
  1030. #ifdef CONFIG_OF_OVERLAY
  1031. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
  1032. static int unittest_probe(struct platform_device *pdev)
  1033. {
  1034. struct device *dev = &pdev->dev;
  1035. struct device_node *np = dev->of_node;
  1036. if (np == NULL) {
  1037. dev_err(dev, "No OF data for device\n");
  1038. return -EINVAL;
  1039. }
  1040. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1041. of_platform_populate(np, NULL, NULL, &pdev->dev);
  1042. return 0;
  1043. }
  1044. static int unittest_remove(struct platform_device *pdev)
  1045. {
  1046. struct device *dev = &pdev->dev;
  1047. struct device_node *np = dev->of_node;
  1048. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1049. return 0;
  1050. }
  1051. static const struct of_device_id unittest_match[] = {
  1052. { .compatible = "unittest", },
  1053. {},
  1054. };
  1055. static struct platform_driver unittest_driver = {
  1056. .probe = unittest_probe,
  1057. .remove = unittest_remove,
  1058. .driver = {
  1059. .name = "unittest",
  1060. .of_match_table = of_match_ptr(unittest_match),
  1061. },
  1062. };
  1063. /* get the platform device instantiated at the path */
  1064. static struct platform_device *of_path_to_platform_device(const char *path)
  1065. {
  1066. struct device_node *np;
  1067. struct platform_device *pdev;
  1068. np = of_find_node_by_path(path);
  1069. if (np == NULL)
  1070. return NULL;
  1071. pdev = of_find_device_by_node(np);
  1072. of_node_put(np);
  1073. return pdev;
  1074. }
  1075. /* find out if a platform device exists at that path */
  1076. static int of_path_platform_device_exists(const char *path)
  1077. {
  1078. struct platform_device *pdev;
  1079. pdev = of_path_to_platform_device(path);
  1080. platform_device_put(pdev);
  1081. return pdev != NULL;
  1082. }
  1083. #if IS_BUILTIN(CONFIG_I2C)
  1084. /* get the i2c client device instantiated at the path */
  1085. static struct i2c_client *of_path_to_i2c_client(const char *path)
  1086. {
  1087. struct device_node *np;
  1088. struct i2c_client *client;
  1089. np = of_find_node_by_path(path);
  1090. if (np == NULL)
  1091. return NULL;
  1092. client = of_find_i2c_device_by_node(np);
  1093. of_node_put(np);
  1094. return client;
  1095. }
  1096. /* find out if a i2c client device exists at that path */
  1097. static int of_path_i2c_client_exists(const char *path)
  1098. {
  1099. struct i2c_client *client;
  1100. client = of_path_to_i2c_client(path);
  1101. if (client)
  1102. put_device(&client->dev);
  1103. return client != NULL;
  1104. }
  1105. #else
  1106. static int of_path_i2c_client_exists(const char *path)
  1107. {
  1108. return 0;
  1109. }
  1110. #endif
  1111. enum overlay_type {
  1112. PDEV_OVERLAY,
  1113. I2C_OVERLAY
  1114. };
  1115. static int of_path_device_type_exists(const char *path,
  1116. enum overlay_type ovtype)
  1117. {
  1118. switch (ovtype) {
  1119. case PDEV_OVERLAY:
  1120. return of_path_platform_device_exists(path);
  1121. case I2C_OVERLAY:
  1122. return of_path_i2c_client_exists(path);
  1123. }
  1124. return 0;
  1125. }
  1126. static const char *unittest_path(int nr, enum overlay_type ovtype)
  1127. {
  1128. const char *base;
  1129. static char buf[256];
  1130. switch (ovtype) {
  1131. case PDEV_OVERLAY:
  1132. base = "/testcase-data/overlay-node/test-bus";
  1133. break;
  1134. case I2C_OVERLAY:
  1135. base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
  1136. break;
  1137. default:
  1138. buf[0] = '\0';
  1139. return buf;
  1140. }
  1141. snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
  1142. buf[sizeof(buf) - 1] = '\0';
  1143. return buf;
  1144. }
  1145. static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
  1146. {
  1147. const char *path;
  1148. path = unittest_path(unittest_nr, ovtype);
  1149. switch (ovtype) {
  1150. case PDEV_OVERLAY:
  1151. return of_path_platform_device_exists(path);
  1152. case I2C_OVERLAY:
  1153. return of_path_i2c_client_exists(path);
  1154. }
  1155. return 0;
  1156. }
  1157. static const char *overlay_name_from_nr(int nr)
  1158. {
  1159. static char buf[256];
  1160. snprintf(buf, sizeof(buf) - 1,
  1161. "overlay_%d", nr);
  1162. buf[sizeof(buf) - 1] = '\0';
  1163. return buf;
  1164. }
  1165. static const char *bus_path = "/testcase-data/overlay-node/test-bus";
  1166. /* it is guaranteed that overlay ids are assigned in sequence */
  1167. #define MAX_UNITTEST_OVERLAYS 256
  1168. static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
  1169. static int overlay_first_id = -1;
  1170. static void of_unittest_track_overlay(int id)
  1171. {
  1172. if (overlay_first_id < 0)
  1173. overlay_first_id = id;
  1174. id -= overlay_first_id;
  1175. /* we shouldn't need that many */
  1176. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1177. overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
  1178. }
  1179. static void of_unittest_untrack_overlay(int id)
  1180. {
  1181. if (overlay_first_id < 0)
  1182. return;
  1183. id -= overlay_first_id;
  1184. BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
  1185. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1186. }
  1187. static void of_unittest_destroy_tracked_overlays(void)
  1188. {
  1189. int id, ret, defers, ovcs_id;
  1190. if (overlay_first_id < 0)
  1191. return;
  1192. /* try until no defers */
  1193. do {
  1194. defers = 0;
  1195. /* remove in reverse order */
  1196. for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
  1197. if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
  1198. continue;
  1199. ovcs_id = id + overlay_first_id;
  1200. ret = of_overlay_remove(&ovcs_id);
  1201. if (ret == -ENODEV) {
  1202. pr_warn("%s: no overlay to destroy for #%d\n",
  1203. __func__, id + overlay_first_id);
  1204. continue;
  1205. }
  1206. if (ret != 0) {
  1207. defers++;
  1208. pr_warn("%s: overlay destroy failed for #%d\n",
  1209. __func__, id + overlay_first_id);
  1210. continue;
  1211. }
  1212. overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
  1213. }
  1214. } while (defers > 0);
  1215. }
  1216. static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
  1217. int *overlay_id)
  1218. {
  1219. const char *overlay_name;
  1220. overlay_name = overlay_name_from_nr(overlay_nr);
  1221. if (!overlay_data_apply(overlay_name, overlay_id)) {
  1222. unittest(0, "could not apply overlay \"%s\"\n",
  1223. overlay_name);
  1224. return -EFAULT;
  1225. }
  1226. of_unittest_track_overlay(*overlay_id);
  1227. return 0;
  1228. }
  1229. /* apply an overlay while checking before and after states */
  1230. static int __init of_unittest_apply_overlay_check(int overlay_nr,
  1231. int unittest_nr, int before, int after,
  1232. enum overlay_type ovtype)
  1233. {
  1234. int ret, ovcs_id;
  1235. /* unittest device must not be in before state */
  1236. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1237. unittest(0, "%s with device @\"%s\" %s\n",
  1238. overlay_name_from_nr(overlay_nr),
  1239. unittest_path(unittest_nr, ovtype),
  1240. !before ? "enabled" : "disabled");
  1241. return -EINVAL;
  1242. }
  1243. ovcs_id = 0;
  1244. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1245. if (ret != 0) {
  1246. /* of_unittest_apply_overlay already called unittest() */
  1247. return ret;
  1248. }
  1249. /* unittest device must be to set to after state */
  1250. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1251. unittest(0, "%s failed to create @\"%s\" %s\n",
  1252. overlay_name_from_nr(overlay_nr),
  1253. unittest_path(unittest_nr, ovtype),
  1254. !after ? "enabled" : "disabled");
  1255. return -EINVAL;
  1256. }
  1257. return 0;
  1258. }
  1259. /* apply an overlay and then revert it while checking before, after states */
  1260. static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
  1261. int unittest_nr, int before, int after,
  1262. enum overlay_type ovtype)
  1263. {
  1264. int ret, ovcs_id;
  1265. /* unittest device must be in before state */
  1266. if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
  1267. unittest(0, "%s with device @\"%s\" %s\n",
  1268. overlay_name_from_nr(overlay_nr),
  1269. unittest_path(unittest_nr, ovtype),
  1270. !before ? "enabled" : "disabled");
  1271. return -EINVAL;
  1272. }
  1273. /* apply the overlay */
  1274. ovcs_id = 0;
  1275. ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
  1276. if (ret != 0) {
  1277. /* of_unittest_apply_overlay already called unittest() */
  1278. return ret;
  1279. }
  1280. /* unittest device must be in after state */
  1281. if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
  1282. unittest(0, "%s failed to create @\"%s\" %s\n",
  1283. overlay_name_from_nr(overlay_nr),
  1284. unittest_path(unittest_nr, ovtype),
  1285. !after ? "enabled" : "disabled");
  1286. return -EINVAL;
  1287. }
  1288. ret = of_overlay_remove(&ovcs_id);
  1289. if (ret != 0) {
  1290. unittest(0, "%s failed to be destroyed @\"%s\"\n",
  1291. overlay_name_from_nr(overlay_nr),
  1292. unittest_path(unittest_nr, ovtype));
  1293. return ret;
  1294. }
  1295. /* unittest device must be again in before state */
  1296. if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
  1297. unittest(0, "%s with device @\"%s\" %s\n",
  1298. overlay_name_from_nr(overlay_nr),
  1299. unittest_path(unittest_nr, ovtype),
  1300. !before ? "enabled" : "disabled");
  1301. return -EINVAL;
  1302. }
  1303. return 0;
  1304. }
  1305. /* test activation of device */
  1306. static void __init of_unittest_overlay_0(void)
  1307. {
  1308. /* device should enable */
  1309. if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
  1310. return;
  1311. unittest(1, "overlay test %d passed\n", 0);
  1312. }
  1313. /* test deactivation of device */
  1314. static void __init of_unittest_overlay_1(void)
  1315. {
  1316. /* device should disable */
  1317. if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
  1318. return;
  1319. unittest(1, "overlay test %d passed\n", 1);
  1320. }
  1321. /* test activation of device */
  1322. static void __init of_unittest_overlay_2(void)
  1323. {
  1324. /* device should enable */
  1325. if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
  1326. return;
  1327. unittest(1, "overlay test %d passed\n", 2);
  1328. }
  1329. /* test deactivation of device */
  1330. static void __init of_unittest_overlay_3(void)
  1331. {
  1332. /* device should disable */
  1333. if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
  1334. return;
  1335. unittest(1, "overlay test %d passed\n", 3);
  1336. }
  1337. /* test activation of a full device node */
  1338. static void __init of_unittest_overlay_4(void)
  1339. {
  1340. /* device should disable */
  1341. if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
  1342. return;
  1343. unittest(1, "overlay test %d passed\n", 4);
  1344. }
  1345. /* test overlay apply/revert sequence */
  1346. static void __init of_unittest_overlay_5(void)
  1347. {
  1348. /* device should disable */
  1349. if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
  1350. return;
  1351. unittest(1, "overlay test %d passed\n", 5);
  1352. }
  1353. /* test overlay application in sequence */
  1354. static void __init of_unittest_overlay_6(void)
  1355. {
  1356. int i, ov_id[2], ovcs_id;
  1357. int overlay_nr = 6, unittest_nr = 6;
  1358. int before = 0, after = 1;
  1359. const char *overlay_name;
  1360. /* unittest device must be in before state */
  1361. for (i = 0; i < 2; i++) {
  1362. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1363. != before) {
  1364. unittest(0, "%s with device @\"%s\" %s\n",
  1365. overlay_name_from_nr(overlay_nr + i),
  1366. unittest_path(unittest_nr + i,
  1367. PDEV_OVERLAY),
  1368. !before ? "enabled" : "disabled");
  1369. return;
  1370. }
  1371. }
  1372. /* apply the overlays */
  1373. for (i = 0; i < 2; i++) {
  1374. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1375. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1376. unittest(0, "could not apply overlay \"%s\"\n",
  1377. overlay_name);
  1378. return;
  1379. }
  1380. ov_id[i] = ovcs_id;
  1381. of_unittest_track_overlay(ov_id[i]);
  1382. }
  1383. for (i = 0; i < 2; i++) {
  1384. /* unittest device must be in after state */
  1385. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1386. != after) {
  1387. unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
  1388. overlay_name_from_nr(overlay_nr + i),
  1389. unittest_path(unittest_nr + i,
  1390. PDEV_OVERLAY),
  1391. !after ? "enabled" : "disabled");
  1392. return;
  1393. }
  1394. }
  1395. for (i = 1; i >= 0; i--) {
  1396. ovcs_id = ov_id[i];
  1397. if (of_overlay_remove(&ovcs_id)) {
  1398. unittest(0, "%s failed destroy @\"%s\"\n",
  1399. overlay_name_from_nr(overlay_nr + i),
  1400. unittest_path(unittest_nr + i,
  1401. PDEV_OVERLAY));
  1402. return;
  1403. }
  1404. of_unittest_untrack_overlay(ov_id[i]);
  1405. }
  1406. for (i = 0; i < 2; i++) {
  1407. /* unittest device must be again in before state */
  1408. if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
  1409. != before) {
  1410. unittest(0, "%s with device @\"%s\" %s\n",
  1411. overlay_name_from_nr(overlay_nr + i),
  1412. unittest_path(unittest_nr + i,
  1413. PDEV_OVERLAY),
  1414. !before ? "enabled" : "disabled");
  1415. return;
  1416. }
  1417. }
  1418. unittest(1, "overlay test %d passed\n", 6);
  1419. }
  1420. /* test overlay application in sequence */
  1421. static void __init of_unittest_overlay_8(void)
  1422. {
  1423. int i, ov_id[2], ovcs_id;
  1424. int overlay_nr = 8, unittest_nr = 8;
  1425. const char *overlay_name;
  1426. /* we don't care about device state in this test */
  1427. /* apply the overlays */
  1428. for (i = 0; i < 2; i++) {
  1429. overlay_name = overlay_name_from_nr(overlay_nr + i);
  1430. if (!overlay_data_apply(overlay_name, &ovcs_id)) {
  1431. unittest(0, "could not apply overlay \"%s\"\n",
  1432. overlay_name);
  1433. return;
  1434. }
  1435. ov_id[i] = ovcs_id;
  1436. of_unittest_track_overlay(ov_id[i]);
  1437. }
  1438. /* now try to remove first overlay (it should fail) */
  1439. ovcs_id = ov_id[0];
  1440. if (!of_overlay_remove(&ovcs_id)) {
  1441. unittest(0, "%s was destroyed @\"%s\"\n",
  1442. overlay_name_from_nr(overlay_nr + 0),
  1443. unittest_path(unittest_nr,
  1444. PDEV_OVERLAY));
  1445. return;
  1446. }
  1447. /* removing them in order should work */
  1448. for (i = 1; i >= 0; i--) {
  1449. ovcs_id = ov_id[i];
  1450. if (of_overlay_remove(&ovcs_id)) {
  1451. unittest(0, "%s not destroyed @\"%s\"\n",
  1452. overlay_name_from_nr(overlay_nr + i),
  1453. unittest_path(unittest_nr,
  1454. PDEV_OVERLAY));
  1455. return;
  1456. }
  1457. of_unittest_untrack_overlay(ov_id[i]);
  1458. }
  1459. unittest(1, "overlay test %d passed\n", 8);
  1460. }
  1461. /* test insertion of a bus with parent devices */
  1462. static void __init of_unittest_overlay_10(void)
  1463. {
  1464. int ret;
  1465. char *child_path;
  1466. /* device should disable */
  1467. ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
  1468. if (unittest(ret == 0,
  1469. "overlay test %d failed; overlay application\n", 10))
  1470. return;
  1471. child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
  1472. unittest_path(10, PDEV_OVERLAY));
  1473. if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
  1474. return;
  1475. ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
  1476. kfree(child_path);
  1477. unittest(ret, "overlay test %d failed; no child device\n", 10);
  1478. }
  1479. /* test insertion of a bus with parent devices (and revert) */
  1480. static void __init of_unittest_overlay_11(void)
  1481. {
  1482. int ret;
  1483. /* device should disable */
  1484. ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
  1485. PDEV_OVERLAY);
  1486. unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
  1487. }
  1488. #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
  1489. struct unittest_i2c_bus_data {
  1490. struct platform_device *pdev;
  1491. struct i2c_adapter adap;
  1492. };
  1493. static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
  1494. struct i2c_msg *msgs, int num)
  1495. {
  1496. struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
  1497. (void)std;
  1498. return num;
  1499. }
  1500. static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
  1501. {
  1502. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1503. }
  1504. static const struct i2c_algorithm unittest_i2c_algo = {
  1505. .master_xfer = unittest_i2c_master_xfer,
  1506. .functionality = unittest_i2c_functionality,
  1507. };
  1508. static int unittest_i2c_bus_probe(struct platform_device *pdev)
  1509. {
  1510. struct device *dev = &pdev->dev;
  1511. struct device_node *np = dev->of_node;
  1512. struct unittest_i2c_bus_data *std;
  1513. struct i2c_adapter *adap;
  1514. int ret;
  1515. if (np == NULL) {
  1516. dev_err(dev, "No OF data for device\n");
  1517. return -EINVAL;
  1518. }
  1519. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1520. std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
  1521. if (!std) {
  1522. dev_err(dev, "Failed to allocate unittest i2c data\n");
  1523. return -ENOMEM;
  1524. }
  1525. /* link them together */
  1526. std->pdev = pdev;
  1527. platform_set_drvdata(pdev, std);
  1528. adap = &std->adap;
  1529. i2c_set_adapdata(adap, std);
  1530. adap->nr = -1;
  1531. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  1532. adap->class = I2C_CLASS_DEPRECATED;
  1533. adap->algo = &unittest_i2c_algo;
  1534. adap->dev.parent = dev;
  1535. adap->dev.of_node = dev->of_node;
  1536. adap->timeout = 5 * HZ;
  1537. adap->retries = 3;
  1538. ret = i2c_add_numbered_adapter(adap);
  1539. if (ret != 0) {
  1540. dev_err(dev, "Failed to add I2C adapter\n");
  1541. return ret;
  1542. }
  1543. return 0;
  1544. }
  1545. static int unittest_i2c_bus_remove(struct platform_device *pdev)
  1546. {
  1547. struct device *dev = &pdev->dev;
  1548. struct device_node *np = dev->of_node;
  1549. struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
  1550. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1551. i2c_del_adapter(&std->adap);
  1552. return 0;
  1553. }
  1554. static const struct of_device_id unittest_i2c_bus_match[] = {
  1555. { .compatible = "unittest-i2c-bus", },
  1556. {},
  1557. };
  1558. static struct platform_driver unittest_i2c_bus_driver = {
  1559. .probe = unittest_i2c_bus_probe,
  1560. .remove = unittest_i2c_bus_remove,
  1561. .driver = {
  1562. .name = "unittest-i2c-bus",
  1563. .of_match_table = of_match_ptr(unittest_i2c_bus_match),
  1564. },
  1565. };
  1566. static int unittest_i2c_dev_probe(struct i2c_client *client,
  1567. const struct i2c_device_id *id)
  1568. {
  1569. struct device *dev = &client->dev;
  1570. struct device_node *np = client->dev.of_node;
  1571. if (!np) {
  1572. dev_err(dev, "No OF node\n");
  1573. return -EINVAL;
  1574. }
  1575. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1576. return 0;
  1577. };
  1578. static int unittest_i2c_dev_remove(struct i2c_client *client)
  1579. {
  1580. struct device *dev = &client->dev;
  1581. struct device_node *np = client->dev.of_node;
  1582. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1583. return 0;
  1584. }
  1585. static const struct i2c_device_id unittest_i2c_dev_id[] = {
  1586. { .name = "unittest-i2c-dev" },
  1587. { }
  1588. };
  1589. static struct i2c_driver unittest_i2c_dev_driver = {
  1590. .driver = {
  1591. .name = "unittest-i2c-dev",
  1592. },
  1593. .probe = unittest_i2c_dev_probe,
  1594. .remove = unittest_i2c_dev_remove,
  1595. .id_table = unittest_i2c_dev_id,
  1596. };
  1597. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1598. static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
  1599. {
  1600. return 0;
  1601. }
  1602. static int unittest_i2c_mux_probe(struct i2c_client *client,
  1603. const struct i2c_device_id *id)
  1604. {
  1605. int i, nchans;
  1606. struct device *dev = &client->dev;
  1607. struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
  1608. struct device_node *np = client->dev.of_node, *child;
  1609. struct i2c_mux_core *muxc;
  1610. u32 reg, max_reg;
  1611. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1612. if (!np) {
  1613. dev_err(dev, "No OF node\n");
  1614. return -EINVAL;
  1615. }
  1616. max_reg = (u32)-1;
  1617. for_each_child_of_node(np, child) {
  1618. if (of_property_read_u32(child, "reg", &reg))
  1619. continue;
  1620. if (max_reg == (u32)-1 || reg > max_reg)
  1621. max_reg = reg;
  1622. }
  1623. nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
  1624. if (nchans == 0) {
  1625. dev_err(dev, "No channels\n");
  1626. return -EINVAL;
  1627. }
  1628. muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
  1629. unittest_i2c_mux_select_chan, NULL);
  1630. if (!muxc)
  1631. return -ENOMEM;
  1632. for (i = 0; i < nchans; i++) {
  1633. if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
  1634. dev_err(dev, "Failed to register mux #%d\n", i);
  1635. i2c_mux_del_adapters(muxc);
  1636. return -ENODEV;
  1637. }
  1638. }
  1639. i2c_set_clientdata(client, muxc);
  1640. return 0;
  1641. };
  1642. static int unittest_i2c_mux_remove(struct i2c_client *client)
  1643. {
  1644. struct device *dev = &client->dev;
  1645. struct device_node *np = client->dev.of_node;
  1646. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  1647. dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
  1648. i2c_mux_del_adapters(muxc);
  1649. return 0;
  1650. }
  1651. static const struct i2c_device_id unittest_i2c_mux_id[] = {
  1652. { .name = "unittest-i2c-mux" },
  1653. { }
  1654. };
  1655. static struct i2c_driver unittest_i2c_mux_driver = {
  1656. .driver = {
  1657. .name = "unittest-i2c-mux",
  1658. },
  1659. .probe = unittest_i2c_mux_probe,
  1660. .remove = unittest_i2c_mux_remove,
  1661. .id_table = unittest_i2c_mux_id,
  1662. };
  1663. #endif
  1664. static int of_unittest_overlay_i2c_init(void)
  1665. {
  1666. int ret;
  1667. ret = i2c_add_driver(&unittest_i2c_dev_driver);
  1668. if (unittest(ret == 0,
  1669. "could not register unittest i2c device driver\n"))
  1670. return ret;
  1671. ret = platform_driver_register(&unittest_i2c_bus_driver);
  1672. if (unittest(ret == 0,
  1673. "could not register unittest i2c bus driver\n"))
  1674. return ret;
  1675. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1676. ret = i2c_add_driver(&unittest_i2c_mux_driver);
  1677. if (unittest(ret == 0,
  1678. "could not register unittest i2c mux driver\n"))
  1679. return ret;
  1680. #endif
  1681. return 0;
  1682. }
  1683. static void of_unittest_overlay_i2c_cleanup(void)
  1684. {
  1685. #if IS_BUILTIN(CONFIG_I2C_MUX)
  1686. i2c_del_driver(&unittest_i2c_mux_driver);
  1687. #endif
  1688. platform_driver_unregister(&unittest_i2c_bus_driver);
  1689. i2c_del_driver(&unittest_i2c_dev_driver);
  1690. }
  1691. static void __init of_unittest_overlay_i2c_12(void)
  1692. {
  1693. /* device should enable */
  1694. if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
  1695. return;
  1696. unittest(1, "overlay test %d passed\n", 12);
  1697. }
  1698. /* test deactivation of device */
  1699. static void __init of_unittest_overlay_i2c_13(void)
  1700. {
  1701. /* device should disable */
  1702. if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
  1703. return;
  1704. unittest(1, "overlay test %d passed\n", 13);
  1705. }
  1706. /* just check for i2c mux existence */
  1707. static void of_unittest_overlay_i2c_14(void)
  1708. {
  1709. }
  1710. static void __init of_unittest_overlay_i2c_15(void)
  1711. {
  1712. /* device should enable */
  1713. if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
  1714. return;
  1715. unittest(1, "overlay test %d passed\n", 15);
  1716. }
  1717. #else
  1718. static inline void of_unittest_overlay_i2c_14(void) { }
  1719. static inline void of_unittest_overlay_i2c_15(void) { }
  1720. #endif
  1721. static void __init of_unittest_overlay(void)
  1722. {
  1723. struct device_node *bus_np = NULL;
  1724. if (platform_driver_register(&unittest_driver)) {
  1725. unittest(0, "could not register unittest driver\n");
  1726. goto out;
  1727. }
  1728. bus_np = of_find_node_by_path(bus_path);
  1729. if (bus_np == NULL) {
  1730. unittest(0, "could not find bus_path \"%s\"\n", bus_path);
  1731. goto out;
  1732. }
  1733. if (of_platform_default_populate(bus_np, NULL, NULL)) {
  1734. unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
  1735. goto out;
  1736. }
  1737. if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
  1738. unittest(0, "could not find unittest0 @ \"%s\"\n",
  1739. unittest_path(100, PDEV_OVERLAY));
  1740. goto out;
  1741. }
  1742. if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
  1743. unittest(0, "unittest1 @ \"%s\" should not exist\n",
  1744. unittest_path(101, PDEV_OVERLAY));
  1745. goto out;
  1746. }
  1747. unittest(1, "basic infrastructure of overlays passed");
  1748. /* tests in sequence */
  1749. of_unittest_overlay_0();
  1750. of_unittest_overlay_1();
  1751. of_unittest_overlay_2();
  1752. of_unittest_overlay_3();
  1753. of_unittest_overlay_4();
  1754. of_unittest_overlay_5();
  1755. of_unittest_overlay_6();
  1756. of_unittest_overlay_8();
  1757. of_unittest_overlay_10();
  1758. of_unittest_overlay_11();
  1759. #if IS_BUILTIN(CONFIG_I2C)
  1760. if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
  1761. goto out;
  1762. of_unittest_overlay_i2c_12();
  1763. of_unittest_overlay_i2c_13();
  1764. of_unittest_overlay_i2c_14();
  1765. of_unittest_overlay_i2c_15();
  1766. of_unittest_overlay_i2c_cleanup();
  1767. #endif
  1768. of_unittest_destroy_tracked_overlays();
  1769. out:
  1770. of_node_put(bus_np);
  1771. }
  1772. #else
  1773. static inline void __init of_unittest_overlay(void) { }
  1774. #endif
  1775. #ifdef CONFIG_OF_OVERLAY
  1776. /*
  1777. * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
  1778. * in scripts/Makefile.lib
  1779. */
  1780. #define OVERLAY_INFO_EXTERN(name) \
  1781. extern uint8_t __dtb_##name##_begin[]; \
  1782. extern uint8_t __dtb_##name##_end[]
  1783. #define OVERLAY_INFO(overlay_name, expected) \
  1784. { .dtb_begin = __dtb_##overlay_name##_begin, \
  1785. .dtb_end = __dtb_##overlay_name##_end, \
  1786. .expected_result = expected, \
  1787. .name = #overlay_name, \
  1788. }
  1789. struct overlay_info {
  1790. uint8_t *dtb_begin;
  1791. uint8_t *dtb_end;
  1792. int expected_result;
  1793. int overlay_id;
  1794. char *name;
  1795. };
  1796. OVERLAY_INFO_EXTERN(overlay_base);
  1797. OVERLAY_INFO_EXTERN(overlay);
  1798. OVERLAY_INFO_EXTERN(overlay_0);
  1799. OVERLAY_INFO_EXTERN(overlay_1);
  1800. OVERLAY_INFO_EXTERN(overlay_2);
  1801. OVERLAY_INFO_EXTERN(overlay_3);
  1802. OVERLAY_INFO_EXTERN(overlay_4);
  1803. OVERLAY_INFO_EXTERN(overlay_5);
  1804. OVERLAY_INFO_EXTERN(overlay_6);
  1805. OVERLAY_INFO_EXTERN(overlay_7);
  1806. OVERLAY_INFO_EXTERN(overlay_8);
  1807. OVERLAY_INFO_EXTERN(overlay_9);
  1808. OVERLAY_INFO_EXTERN(overlay_10);
  1809. OVERLAY_INFO_EXTERN(overlay_11);
  1810. OVERLAY_INFO_EXTERN(overlay_12);
  1811. OVERLAY_INFO_EXTERN(overlay_13);
  1812. OVERLAY_INFO_EXTERN(overlay_15);
  1813. OVERLAY_INFO_EXTERN(overlay_bad_phandle);
  1814. OVERLAY_INFO_EXTERN(overlay_bad_symbol);
  1815. /* order of entries is hard-coded into users of overlays[] */
  1816. static struct overlay_info overlays[] = {
  1817. OVERLAY_INFO(overlay_base, -9999),
  1818. OVERLAY_INFO(overlay, 0),
  1819. OVERLAY_INFO(overlay_0, 0),
  1820. OVERLAY_INFO(overlay_1, 0),
  1821. OVERLAY_INFO(overlay_2, 0),
  1822. OVERLAY_INFO(overlay_3, 0),
  1823. OVERLAY_INFO(overlay_4, 0),
  1824. OVERLAY_INFO(overlay_5, 0),
  1825. OVERLAY_INFO(overlay_6, 0),
  1826. OVERLAY_INFO(overlay_7, 0),
  1827. OVERLAY_INFO(overlay_8, 0),
  1828. OVERLAY_INFO(overlay_9, 0),
  1829. OVERLAY_INFO(overlay_10, 0),
  1830. OVERLAY_INFO(overlay_11, 0),
  1831. OVERLAY_INFO(overlay_12, 0),
  1832. OVERLAY_INFO(overlay_13, 0),
  1833. OVERLAY_INFO(overlay_15, 0),
  1834. OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
  1835. OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
  1836. {}
  1837. };
  1838. static struct device_node *overlay_base_root;
  1839. static void * __init dt_alloc_memory(u64 size, u64 align)
  1840. {
  1841. return memblock_alloc(size, align);
  1842. }
  1843. /*
  1844. * Create base device tree for the overlay unittest.
  1845. *
  1846. * This is called from very early boot code.
  1847. *
  1848. * Do as much as possible the same way as done in __unflatten_device_tree
  1849. * and other early boot steps for the normal FDT so that the overlay base
  1850. * unflattened tree will have the same characteristics as the real tree
  1851. * (such as having memory allocated by the early allocator). The goal
  1852. * is to test "the real thing" as much as possible, and test "test setup
  1853. * code" as little as possible.
  1854. *
  1855. * Have to stop before resolving phandles, because that uses kmalloc.
  1856. */
  1857. void __init unittest_unflatten_overlay_base(void)
  1858. {
  1859. struct overlay_info *info;
  1860. u32 data_size;
  1861. void *new_fdt;
  1862. u32 size;
  1863. info = &overlays[0];
  1864. if (info->expected_result != -9999) {
  1865. pr_err("No dtb 'overlay_base' to attach\n");
  1866. return;
  1867. }
  1868. data_size = info->dtb_end - info->dtb_begin;
  1869. if (!data_size) {
  1870. pr_err("No dtb 'overlay_base' to attach\n");
  1871. return;
  1872. }
  1873. size = fdt_totalsize(info->dtb_begin);
  1874. if (size != data_size) {
  1875. pr_err("dtb 'overlay_base' header totalsize != actual size");
  1876. return;
  1877. }
  1878. new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
  1879. if (!new_fdt) {
  1880. pr_err("alloc for dtb 'overlay_base' failed");
  1881. return;
  1882. }
  1883. memcpy(new_fdt, info->dtb_begin, size);
  1884. __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
  1885. dt_alloc_memory, true);
  1886. }
  1887. /*
  1888. * The purpose of of_unittest_overlay_data_add is to add an
  1889. * overlay in the normal fashion. This is a test of the whole
  1890. * picture, instead of testing individual elements.
  1891. *
  1892. * A secondary purpose is to be able to verify that the contents of
  1893. * /proc/device-tree/ contains the updated structure and values from
  1894. * the overlay. That must be verified separately in user space.
  1895. *
  1896. * Return 0 on unexpected error.
  1897. */
  1898. static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
  1899. {
  1900. struct overlay_info *info;
  1901. int found = 0;
  1902. int k;
  1903. int ret;
  1904. u32 size;
  1905. for (k = 0, info = overlays; info && info->name; info++, k++) {
  1906. if (!strcmp(overlay_name, info->name)) {
  1907. found = 1;
  1908. break;
  1909. }
  1910. }
  1911. if (!found) {
  1912. pr_err("no overlay data for %s\n", overlay_name);
  1913. return 0;
  1914. }
  1915. size = info->dtb_end - info->dtb_begin;
  1916. if (!size)
  1917. pr_err("no overlay data for %s\n", overlay_name);
  1918. ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
  1919. if (overlay_id)
  1920. *overlay_id = info->overlay_id;
  1921. if (ret < 0)
  1922. goto out;
  1923. pr_debug("%s applied\n", overlay_name);
  1924. out:
  1925. if (ret != info->expected_result)
  1926. pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
  1927. info->expected_result, ret, overlay_name);
  1928. return (ret == info->expected_result);
  1929. }
  1930. /*
  1931. * The purpose of of_unittest_overlay_high_level is to add an overlay
  1932. * in the normal fashion. This is a test of the whole picture,
  1933. * instead of individual elements.
  1934. *
  1935. * The first part of the function is _not_ normal overlay usage; it is
  1936. * finishing splicing the base overlay device tree into the live tree.
  1937. */
  1938. static __init void of_unittest_overlay_high_level(void)
  1939. {
  1940. struct device_node *last_sibling;
  1941. struct device_node *np;
  1942. struct device_node *of_symbols;
  1943. struct device_node *overlay_base_symbols;
  1944. struct device_node **pprev;
  1945. struct property *prop;
  1946. if (!overlay_base_root) {
  1947. unittest(0, "overlay_base_root not initialized\n");
  1948. return;
  1949. }
  1950. /*
  1951. * Could not fixup phandles in unittest_unflatten_overlay_base()
  1952. * because kmalloc() was not yet available.
  1953. */
  1954. of_overlay_mutex_lock();
  1955. of_resolve_phandles(overlay_base_root);
  1956. of_overlay_mutex_unlock();
  1957. /*
  1958. * do not allow overlay_base to duplicate any node already in
  1959. * tree, this greatly simplifies the code
  1960. */
  1961. /*
  1962. * remove overlay_base_root node "__local_fixups", after
  1963. * being used by of_resolve_phandles()
  1964. */
  1965. pprev = &overlay_base_root->child;
  1966. for (np = overlay_base_root->child; np; np = np->sibling) {
  1967. if (!of_node_cmp(np->name, "__local_fixups__")) {
  1968. *pprev = np->sibling;
  1969. break;
  1970. }
  1971. pprev = &np->sibling;
  1972. }
  1973. /* remove overlay_base_root node "__symbols__" if in live tree */
  1974. of_symbols = of_get_child_by_name(of_root, "__symbols__");
  1975. if (of_symbols) {
  1976. /* will have to graft properties from node into live tree */
  1977. pprev = &overlay_base_root->child;
  1978. for (np = overlay_base_root->child; np; np = np->sibling) {
  1979. if (!of_node_cmp(np->name, "__symbols__")) {
  1980. overlay_base_symbols = np;
  1981. *pprev = np->sibling;
  1982. break;
  1983. }
  1984. pprev = &np->sibling;
  1985. }
  1986. }
  1987. for_each_child_of_node(overlay_base_root, np) {
  1988. struct device_node *base_child;
  1989. for_each_child_of_node(of_root, base_child) {
  1990. if (!strcmp(np->full_name, base_child->full_name)) {
  1991. unittest(0, "illegal node name in overlay_base %pOFn",
  1992. np);
  1993. return;
  1994. }
  1995. }
  1996. }
  1997. /*
  1998. * overlay 'overlay_base' is not allowed to have root
  1999. * properties, so only need to splice nodes into main device tree.
  2000. *
  2001. * root node of *overlay_base_root will not be freed, it is lost
  2002. * memory.
  2003. */
  2004. for (np = overlay_base_root->child; np; np = np->sibling)
  2005. np->parent = of_root;
  2006. mutex_lock(&of_mutex);
  2007. for (last_sibling = np = of_root->child; np; np = np->sibling)
  2008. last_sibling = np;
  2009. if (last_sibling)
  2010. last_sibling->sibling = overlay_base_root->child;
  2011. else
  2012. of_root->child = overlay_base_root->child;
  2013. for_each_of_allnodes_from(overlay_base_root, np)
  2014. __of_attach_node_sysfs(np);
  2015. if (of_symbols) {
  2016. struct property *new_prop;
  2017. for_each_property_of_node(overlay_base_symbols, prop) {
  2018. new_prop = __of_prop_dup(prop, GFP_KERNEL);
  2019. if (!new_prop) {
  2020. unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
  2021. prop->name);
  2022. goto err_unlock;
  2023. }
  2024. if (__of_add_property(of_symbols, new_prop)) {
  2025. /* "name" auto-generated by unflatten */
  2026. if (!strcmp(new_prop->name, "name"))
  2027. continue;
  2028. unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
  2029. prop->name);
  2030. goto err_unlock;
  2031. }
  2032. if (__of_add_property_sysfs(of_symbols, new_prop)) {
  2033. unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
  2034. prop->name);
  2035. goto err_unlock;
  2036. }
  2037. }
  2038. }
  2039. mutex_unlock(&of_mutex);
  2040. /* now do the normal overlay usage test */
  2041. unittest(overlay_data_apply("overlay", NULL),
  2042. "Adding overlay 'overlay' failed\n");
  2043. unittest(overlay_data_apply("overlay_bad_phandle", NULL),
  2044. "Adding overlay 'overlay_bad_phandle' failed\n");
  2045. unittest(overlay_data_apply("overlay_bad_symbol", NULL),
  2046. "Adding overlay 'overlay_bad_symbol' failed\n");
  2047. return;
  2048. err_unlock:
  2049. mutex_unlock(&of_mutex);
  2050. }
  2051. #else
  2052. static inline __init void of_unittest_overlay_high_level(void) {}
  2053. #endif
  2054. static int __init of_unittest(void)
  2055. {
  2056. struct device_node *np;
  2057. int res;
  2058. /* adding data for unittest */
  2059. res = unittest_data_add();
  2060. if (res)
  2061. return res;
  2062. if (!of_aliases)
  2063. of_aliases = of_find_node_by_path("/aliases");
  2064. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  2065. if (!np) {
  2066. pr_info("No testcase data in device tree; not running tests\n");
  2067. return 0;
  2068. }
  2069. of_node_put(np);
  2070. pr_info("start of unittest - you will see error messages\n");
  2071. of_unittest_check_tree_linkage();
  2072. of_unittest_check_phandles();
  2073. of_unittest_find_node_by_name();
  2074. of_unittest_dynamic();
  2075. of_unittest_parse_phandle_with_args();
  2076. of_unittest_parse_phandle_with_args_map();
  2077. of_unittest_printf();
  2078. of_unittest_property_string();
  2079. of_unittest_property_copy();
  2080. of_unittest_changeset();
  2081. of_unittest_parse_interrupts();
  2082. of_unittest_parse_interrupts_extended();
  2083. of_unittest_match_node();
  2084. of_unittest_platform_populate();
  2085. of_unittest_overlay();
  2086. /* Double check linkage after removing testcase data */
  2087. of_unittest_check_tree_linkage();
  2088. of_unittest_overlay_high_level();
  2089. pr_info("end of unittest - %i passed, %i failed\n",
  2090. unittest_results.passed, unittest_results.failed);
  2091. return 0;
  2092. }
  2093. late_initcall(of_unittest);