unittest.c 63 KB

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