unittest.c 60 KB

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