unittest.c 69 KB

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