core.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Tests for the core driver model code
  4. *
  5. * Copyright (c) 2013 Google, Inc
  6. */
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <dm.h>
  10. #include <fdtdec.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <asm/global_data.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/root.h>
  16. #include <dm/util.h>
  17. #include <dm/test.h>
  18. #include <dm/uclass-internal.h>
  19. #include <test/test.h>
  20. #include <test/ut.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. enum {
  23. TEST_INTVAL1 = 0,
  24. TEST_INTVAL2 = 3,
  25. TEST_INTVAL3 = 6,
  26. TEST_INTVAL_MANUAL = 101112,
  27. TEST_INTVAL_PRE_RELOC = 7,
  28. };
  29. static const struct dm_test_pdata test_pdata[] = {
  30. { .ping_add = TEST_INTVAL1, },
  31. { .ping_add = TEST_INTVAL2, },
  32. { .ping_add = TEST_INTVAL3, },
  33. };
  34. static const struct dm_test_pdata test_pdata_manual = {
  35. .ping_add = TEST_INTVAL_MANUAL,
  36. };
  37. static const struct dm_test_pdata test_pdata_pre_reloc = {
  38. .ping_add = TEST_INTVAL_PRE_RELOC,
  39. };
  40. U_BOOT_DRVINFO(dm_test_info1) = {
  41. .name = "test_drv",
  42. .plat = &test_pdata[0],
  43. };
  44. U_BOOT_DRVINFO(dm_test_info2) = {
  45. .name = "test_drv",
  46. .plat = &test_pdata[1],
  47. };
  48. U_BOOT_DRVINFO(dm_test_info3) = {
  49. .name = "test_drv",
  50. .plat = &test_pdata[2],
  51. };
  52. static struct driver_info driver_info_manual = {
  53. .name = "test_manual_drv",
  54. .plat = &test_pdata_manual,
  55. };
  56. static struct driver_info driver_info_pre_reloc = {
  57. .name = "test_pre_reloc_drv",
  58. .plat = &test_pdata_pre_reloc,
  59. };
  60. static struct driver_info driver_info_act_dma = {
  61. .name = "test_act_dma_drv",
  62. };
  63. static struct driver_info driver_info_vital_clk = {
  64. .name = "test_vital_clk_drv",
  65. };
  66. static struct driver_info driver_info_act_dma_vital_clk = {
  67. .name = "test_act_dma_vital_clk_drv",
  68. };
  69. void dm_leak_check_start(struct unit_test_state *uts)
  70. {
  71. uts->start = mallinfo();
  72. if (!uts->start.uordblks)
  73. puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
  74. }
  75. int dm_leak_check_end(struct unit_test_state *uts)
  76. {
  77. struct mallinfo end;
  78. int id, diff;
  79. /* Don't delete the root class, since we started with that */
  80. for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
  81. struct uclass *uc;
  82. uc = uclass_find(id);
  83. if (!uc)
  84. continue;
  85. ut_assertok(uclass_destroy(uc));
  86. }
  87. end = mallinfo();
  88. diff = end.uordblks - uts->start.uordblks;
  89. if (diff > 0)
  90. printf("Leak: lost %#xd bytes\n", diff);
  91. else if (diff < 0)
  92. printf("Leak: gained %#xd bytes\n", -diff);
  93. ut_asserteq(uts->start.uordblks, end.uordblks);
  94. return 0;
  95. }
  96. /* Test that binding with plat occurs correctly */
  97. static int dm_test_autobind(struct unit_test_state *uts)
  98. {
  99. struct udevice *dev;
  100. /*
  101. * We should have a single class (UCLASS_ROOT) and a single root
  102. * device with no children.
  103. */
  104. ut_assert(uts->root);
  105. ut_asserteq(1, list_count_items(gd->uclass_root));
  106. ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
  107. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
  108. ut_assertok(dm_scan_plat(false));
  109. /* We should have our test class now at least, plus more children */
  110. ut_assert(1 < list_count_items(gd->uclass_root));
  111. ut_assert(0 < list_count_items(&gd->dm_root->child_head));
  112. /* Our 3 dm_test_infox children should be bound to the test uclass */
  113. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
  114. /* No devices should be probed */
  115. list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
  116. ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
  117. /* Our test driver should have been bound 3 times */
  118. ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
  119. return 0;
  120. }
  121. DM_TEST(dm_test_autobind, 0);
  122. /* Test that binding with uclass plat allocation occurs correctly */
  123. static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
  124. {
  125. struct dm_test_perdev_uc_pdata *uc_pdata;
  126. struct udevice *dev;
  127. struct uclass *uc;
  128. ut_assertok(uclass_get(UCLASS_TEST, &uc));
  129. ut_assert(uc);
  130. /**
  131. * Test if test uclass driver requires allocation for the uclass
  132. * platform data and then check the dev->uclass_plat pointer.
  133. */
  134. ut_assert(uc->uc_drv->per_device_plat_auto);
  135. for (uclass_find_first_device(UCLASS_TEST, &dev);
  136. dev;
  137. uclass_find_next_device(&dev)) {
  138. ut_assertnonnull(dev);
  139. uc_pdata = dev_get_uclass_plat(dev);
  140. ut_assert(uc_pdata);
  141. }
  142. return 0;
  143. }
  144. DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA);
  145. /* compare node names ignoring the unit address */
  146. static int dm_test_compare_node_name(struct unit_test_state *uts)
  147. {
  148. ofnode node;
  149. node = ofnode_path("/mmio-bus@0");
  150. ut_assert(ofnode_valid(node));
  151. ut_assert(ofnode_name_eq(node, "mmio-bus"));
  152. return 0;
  153. }
  154. DM_TEST(dm_test_compare_node_name, UT_TESTF_SCAN_PDATA);
  155. /* Test that binding with uclass plat setting occurs correctly */
  156. static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
  157. {
  158. struct dm_test_perdev_uc_pdata *uc_pdata;
  159. struct udevice *dev;
  160. /**
  161. * In the test_postbind() method of test uclass driver, the uclass
  162. * platform data should be set to three test int values - test it.
  163. */
  164. for (uclass_find_first_device(UCLASS_TEST, &dev);
  165. dev;
  166. uclass_find_next_device(&dev)) {
  167. ut_assertnonnull(dev);
  168. uc_pdata = dev_get_uclass_plat(dev);
  169. ut_assert(uc_pdata);
  170. ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
  171. ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
  172. ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
  173. }
  174. return 0;
  175. }
  176. DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
  177. /* Test that autoprobe finds all the expected devices */
  178. static int dm_test_autoprobe(struct unit_test_state *uts)
  179. {
  180. int expected_base_add;
  181. struct udevice *dev;
  182. struct uclass *uc;
  183. int i;
  184. ut_assertok(uclass_get(UCLASS_TEST, &uc));
  185. ut_assert(uc);
  186. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
  187. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
  188. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
  189. /* The root device should not be activated until needed */
  190. ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
  191. /*
  192. * We should be able to find the three test devices, and they should
  193. * all be activated as they are used (lazy activation, required by
  194. * U-Boot)
  195. */
  196. for (i = 0; i < 3; i++) {
  197. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  198. ut_assert(dev);
  199. ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
  200. "Driver %d/%s already activated", i, dev->name);
  201. /* This should activate it */
  202. ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
  203. ut_assert(dev);
  204. ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
  205. /* Activating a device should activate the root device */
  206. if (!i)
  207. ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
  208. }
  209. /*
  210. * Our 3 dm_test_info children should be passed to pre_probe and
  211. * post_probe
  212. */
  213. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
  214. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
  215. /* Also we can check the per-device data */
  216. expected_base_add = 0;
  217. for (i = 0; i < 3; i++) {
  218. struct dm_test_uclass_perdev_priv *priv;
  219. struct dm_test_pdata *pdata;
  220. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  221. ut_assert(dev);
  222. priv = dev_get_uclass_priv(dev);
  223. ut_assert(priv);
  224. ut_asserteq(expected_base_add, priv->base_add);
  225. pdata = dev_get_plat(dev);
  226. expected_base_add += pdata->ping_add;
  227. }
  228. return 0;
  229. }
  230. DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
  231. /* Check that we see the correct plat in each device */
  232. static int dm_test_plat(struct unit_test_state *uts)
  233. {
  234. const struct dm_test_pdata *pdata;
  235. struct udevice *dev;
  236. int i;
  237. for (i = 0; i < 3; i++) {
  238. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  239. ut_assert(dev);
  240. pdata = dev_get_plat(dev);
  241. ut_assert(pdata->ping_add == test_pdata[i].ping_add);
  242. }
  243. return 0;
  244. }
  245. DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
  246. /* Test that we can bind, probe, remove, unbind a driver */
  247. static int dm_test_lifecycle(struct unit_test_state *uts)
  248. {
  249. int op_count[DM_TEST_OP_COUNT];
  250. struct udevice *dev, *test_dev;
  251. int start_dev_count, start_uc_count;
  252. int dev_count, uc_count;
  253. int pingret;
  254. int ret;
  255. memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
  256. dm_get_stats(&start_dev_count, &start_uc_count);
  257. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  258. &dev));
  259. ut_assert(dev);
  260. ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
  261. == op_count[DM_TEST_OP_BIND] + 1);
  262. ut_assert(!dev_get_priv(dev));
  263. /* We should have one more device */
  264. dm_get_stats(&dev_count, &uc_count);
  265. ut_asserteq(start_dev_count + 1, dev_count);
  266. ut_asserteq(start_uc_count, uc_count);
  267. /* Probe the device - it should fail allocating private data */
  268. uts->force_fail_alloc = 1;
  269. ret = device_probe(dev);
  270. ut_assert(ret == -ENOMEM);
  271. ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
  272. == op_count[DM_TEST_OP_PROBE] + 1);
  273. ut_assert(!dev_get_priv(dev));
  274. /* Try again without the alloc failure */
  275. uts->force_fail_alloc = 0;
  276. ut_assertok(device_probe(dev));
  277. ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
  278. == op_count[DM_TEST_OP_PROBE] + 2);
  279. ut_assert(dev_get_priv(dev));
  280. /* This should be device 3 in the uclass */
  281. ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
  282. ut_assert(dev == test_dev);
  283. /* Try ping */
  284. ut_assertok(test_ping(dev, 100, &pingret));
  285. ut_assert(pingret == 102);
  286. /* Now remove device 3 */
  287. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
  288. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  289. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
  290. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  291. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
  292. ut_assertok(device_unbind(dev));
  293. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  294. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
  295. /* We should have one less device */
  296. dm_get_stats(&dev_count, &uc_count);
  297. ut_asserteq(start_dev_count, dev_count);
  298. ut_asserteq(start_uc_count, uc_count);
  299. return 0;
  300. }
  301. DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
  302. /* Test that we can bind/unbind and the lists update correctly */
  303. static int dm_test_ordering(struct unit_test_state *uts)
  304. {
  305. struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
  306. int pingret;
  307. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  308. &dev));
  309. ut_assert(dev);
  310. /* Bind two new devices (numbers 4 and 5) */
  311. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  312. &dev_penultimate));
  313. ut_assert(dev_penultimate);
  314. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  315. &dev_last));
  316. ut_assert(dev_last);
  317. /* Now remove device 3 */
  318. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  319. ut_assertok(device_unbind(dev));
  320. /* The device numbering should have shifted down one */
  321. ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
  322. ut_assert(dev_penultimate == test_dev);
  323. ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
  324. ut_assert(dev_last == test_dev);
  325. /* Add back the original device 3, now in position 5 */
  326. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  327. &dev));
  328. ut_assert(dev);
  329. /* Try ping */
  330. ut_assertok(test_ping(dev, 100, &pingret));
  331. ut_assert(pingret == 102);
  332. /* Remove 3 and 4 */
  333. ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
  334. ut_assertok(device_unbind(dev_penultimate));
  335. ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
  336. ut_assertok(device_unbind(dev_last));
  337. /* Our device should now be in position 3 */
  338. ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
  339. ut_assert(dev == test_dev);
  340. /* Now remove device 3 */
  341. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  342. ut_assertok(device_unbind(dev));
  343. return 0;
  344. }
  345. DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
  346. /* Check that we can perform operations on a device (do a ping) */
  347. int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
  348. uint32_t base, struct dm_test_priv *priv)
  349. {
  350. int expected;
  351. int pingret;
  352. /* Getting the child device should allocate plat / priv */
  353. ut_assertok(testfdt_ping(dev, 10, &pingret));
  354. ut_assert(dev_get_priv(dev));
  355. ut_assert(dev_get_plat(dev));
  356. expected = 10 + base;
  357. ut_asserteq(expected, pingret);
  358. /* Do another ping */
  359. ut_assertok(testfdt_ping(dev, 20, &pingret));
  360. expected = 20 + base;
  361. ut_asserteq(expected, pingret);
  362. /* Now check the ping_total */
  363. priv = dev_get_priv(dev);
  364. ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
  365. priv->ping_total);
  366. return 0;
  367. }
  368. /* Check that we can perform operations on devices */
  369. static int dm_test_operations(struct unit_test_state *uts)
  370. {
  371. struct udevice *dev;
  372. int i;
  373. /*
  374. * Now check that the ping adds are what we expect. This is using the
  375. * ping-add property in each node.
  376. */
  377. for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
  378. uint32_t base;
  379. ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
  380. /*
  381. * Get the 'reg' property, which tells us what the ping add
  382. * should be. We don't use the plat because we want
  383. * to test the code that sets that up (testfdt_drv_probe()).
  384. */
  385. base = test_pdata[i].ping_add;
  386. debug("dev=%d, base=%d\n", i, base);
  387. ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
  388. }
  389. return 0;
  390. }
  391. DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
  392. /* Remove all drivers and check that things work */
  393. static int dm_test_remove(struct unit_test_state *uts)
  394. {
  395. struct udevice *dev;
  396. int i;
  397. for (i = 0; i < 3; i++) {
  398. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  399. ut_assert(dev);
  400. ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
  401. "Driver %d/%s not activated", i, dev->name);
  402. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  403. ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
  404. "Driver %d/%s should have deactivated", i,
  405. dev->name);
  406. ut_assert(!dev_get_priv(dev));
  407. }
  408. return 0;
  409. }
  410. DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
  411. /* Remove and recreate everything, check for memory leaks */
  412. static int dm_test_leak(struct unit_test_state *uts)
  413. {
  414. int i;
  415. for (i = 0; i < 2; i++) {
  416. int ret;
  417. dm_leak_check_start(uts);
  418. ut_assertok(dm_scan_plat(false));
  419. ut_assertok(dm_scan_fdt(false));
  420. ret = uclass_probe_all(UCLASS_TEST);
  421. ut_assertok(ret);
  422. ut_assertok(dm_leak_check_end(uts));
  423. }
  424. return 0;
  425. }
  426. DM_TEST(dm_test_leak, 0);
  427. /* Test uclass init/destroy methods */
  428. static int dm_test_uclass(struct unit_test_state *uts)
  429. {
  430. int dev_count, uc_count;
  431. struct uclass *uc;
  432. /* We should have just the root device and uclass */
  433. dm_get_stats(&dev_count, &uc_count);
  434. ut_asserteq(1, dev_count);
  435. ut_asserteq(1, uc_count);
  436. ut_assertok(uclass_get(UCLASS_TEST, &uc));
  437. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
  438. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
  439. ut_assert(uclass_get_priv(uc));
  440. dm_get_stats(&dev_count, &uc_count);
  441. ut_asserteq(1, dev_count);
  442. ut_asserteq(2, uc_count);
  443. ut_assertok(uclass_destroy(uc));
  444. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
  445. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
  446. dm_get_stats(&dev_count, &uc_count);
  447. ut_asserteq(1, dev_count);
  448. ut_asserteq(1, uc_count);
  449. return 0;
  450. }
  451. DM_TEST(dm_test_uclass, 0);
  452. /**
  453. * create_children() - Create children of a parent node
  454. *
  455. * @dms: Test system state
  456. * @parent: Parent device
  457. * @count: Number of children to create
  458. * @key: Key value to put in first child. Subsequence children
  459. * receive an incrementing value
  460. * @child: If not NULL, then the child device pointers are written into
  461. * this array.
  462. * Return: 0 if OK, -ve on error
  463. */
  464. static int create_children(struct unit_test_state *uts, struct udevice *parent,
  465. int count, int key, struct udevice *child[])
  466. {
  467. struct udevice *dev;
  468. int i;
  469. for (i = 0; i < count; i++) {
  470. struct dm_test_pdata *pdata;
  471. ut_assertok(device_bind_by_name(parent, false,
  472. &driver_info_manual, &dev));
  473. pdata = calloc(1, sizeof(*pdata));
  474. pdata->ping_add = key + i;
  475. dev_set_plat(dev, pdata);
  476. if (child)
  477. child[i] = dev;
  478. }
  479. return 0;
  480. }
  481. #define NODE_COUNT 10
  482. static int dm_test_children(struct unit_test_state *uts)
  483. {
  484. struct udevice *top[NODE_COUNT];
  485. struct udevice *child[NODE_COUNT];
  486. struct udevice *grandchild[NODE_COUNT];
  487. struct udevice *dev;
  488. int total;
  489. int ret;
  490. int i;
  491. /* We don't care about the numbering for this test */
  492. uts->skip_post_probe = 1;
  493. ut_assert(NODE_COUNT > 5);
  494. /* First create 10 top-level children */
  495. ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
  496. /* Now a few have their own children */
  497. ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
  498. ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
  499. /* And grandchildren */
  500. for (i = 0; i < NODE_COUNT; i++)
  501. ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
  502. i == 2 ? grandchild : NULL));
  503. /* Check total number of devices */
  504. total = NODE_COUNT * (3 + NODE_COUNT);
  505. ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
  506. /* Try probing one of the grandchildren */
  507. ut_assertok(uclass_get_device(UCLASS_TEST,
  508. NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
  509. ut_asserteq_ptr(grandchild[0], dev);
  510. /*
  511. * This should have probed the child and top node also, for a total
  512. * of 3 nodes.
  513. */
  514. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
  515. /* Probe the other grandchildren */
  516. for (i = 1; i < NODE_COUNT; i++)
  517. ut_assertok(device_probe(grandchild[i]));
  518. ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
  519. /* Probe everything */
  520. ret = uclass_probe_all(UCLASS_TEST);
  521. ut_assertok(ret);
  522. ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
  523. /* Remove a top-level child and check that the children are removed */
  524. ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
  525. ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
  526. dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
  527. /* Try one with grandchildren */
  528. ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
  529. ut_asserteq_ptr(dev, top[5]);
  530. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  531. ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
  532. dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
  533. /* Try the same with unbind */
  534. ut_assertok(device_unbind(top[2]));
  535. ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  536. dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
  537. /* Try one with grandchildren */
  538. ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
  539. ut_asserteq_ptr(dev, top[6]);
  540. ut_assertok(device_unbind(top[5]));
  541. ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
  542. dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  543. return 0;
  544. }
  545. DM_TEST(dm_test_children, 0);
  546. static int dm_test_device_reparent(struct unit_test_state *uts)
  547. {
  548. struct udevice *top[NODE_COUNT];
  549. struct udevice *child[NODE_COUNT];
  550. struct udevice *grandchild[NODE_COUNT];
  551. struct udevice *dev;
  552. int total;
  553. int ret;
  554. int i;
  555. /* We don't care about the numbering for this test */
  556. uts->skip_post_probe = 1;
  557. ut_assert(NODE_COUNT > 5);
  558. /* First create 10 top-level children */
  559. ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
  560. /* Now a few have their own children */
  561. ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
  562. ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
  563. /* And grandchildren */
  564. for (i = 0; i < NODE_COUNT; i++)
  565. ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
  566. i == 2 ? grandchild : NULL));
  567. /* Check total number of devices */
  568. total = NODE_COUNT * (3 + NODE_COUNT);
  569. ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
  570. /* Probe everything */
  571. for (i = 0; i < total; i++)
  572. ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
  573. /* Re-parent top-level children with no grandchildren. */
  574. ut_assertok(device_reparent(top[3], top[0]));
  575. /* try to get devices */
  576. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  577. dev;
  578. ret = uclass_find_next_device(&dev)) {
  579. ut_assert(!ret);
  580. ut_assertnonnull(dev);
  581. }
  582. ut_assertok(device_reparent(top[4], top[0]));
  583. /* try to get devices */
  584. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  585. dev;
  586. ret = uclass_find_next_device(&dev)) {
  587. ut_assert(!ret);
  588. ut_assertnonnull(dev);
  589. }
  590. /* Re-parent top-level children with grandchildren. */
  591. ut_assertok(device_reparent(top[2], top[0]));
  592. /* try to get devices */
  593. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  594. dev;
  595. ret = uclass_find_next_device(&dev)) {
  596. ut_assert(!ret);
  597. ut_assertnonnull(dev);
  598. }
  599. ut_assertok(device_reparent(top[5], top[2]));
  600. /* try to get devices */
  601. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  602. dev;
  603. ret = uclass_find_next_device(&dev)) {
  604. ut_assert(!ret);
  605. ut_assertnonnull(dev);
  606. }
  607. /* Re-parent grandchildren. */
  608. ut_assertok(device_reparent(grandchild[0], top[1]));
  609. /* try to get devices */
  610. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  611. dev;
  612. ret = uclass_find_next_device(&dev)) {
  613. ut_assert(!ret);
  614. ut_assertnonnull(dev);
  615. }
  616. ut_assertok(device_reparent(grandchild[1], top[1]));
  617. /* try to get devices */
  618. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  619. dev;
  620. ret = uclass_find_next_device(&dev)) {
  621. ut_assert(!ret);
  622. ut_assertnonnull(dev);
  623. }
  624. /* Remove re-pareneted devices. */
  625. ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
  626. /* try to get devices */
  627. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  628. dev;
  629. ret = uclass_find_next_device(&dev)) {
  630. ut_assert(!ret);
  631. ut_assertnonnull(dev);
  632. }
  633. ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
  634. /* try to get devices */
  635. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  636. dev;
  637. ret = uclass_find_next_device(&dev)) {
  638. ut_assert(!ret);
  639. ut_assertnonnull(dev);
  640. }
  641. ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
  642. /* try to get devices */
  643. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  644. dev;
  645. ret = uclass_find_next_device(&dev)) {
  646. ut_assert(!ret);
  647. ut_assertnonnull(dev);
  648. }
  649. ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
  650. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  651. dev;
  652. ret = uclass_find_next_device(&dev)) {
  653. ut_assert(!ret);
  654. ut_assertnonnull(dev);
  655. }
  656. ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
  657. /* try to get devices */
  658. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  659. dev;
  660. ret = uclass_find_next_device(&dev)) {
  661. ut_assert(!ret);
  662. ut_assertnonnull(dev);
  663. }
  664. ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
  665. /* try to get devices */
  666. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  667. dev;
  668. ret = uclass_find_next_device(&dev)) {
  669. ut_assert(!ret);
  670. ut_assertnonnull(dev);
  671. }
  672. /* Try the same with unbind */
  673. ut_assertok(device_unbind(top[3]));
  674. ut_assertok(device_unbind(top[4]));
  675. ut_assertok(device_unbind(top[5]));
  676. ut_assertok(device_unbind(top[2]));
  677. ut_assertok(device_unbind(grandchild[0]));
  678. ut_assertok(device_unbind(grandchild[1]));
  679. return 0;
  680. }
  681. DM_TEST(dm_test_device_reparent, 0);
  682. /* Test that pre-relocation devices work as expected */
  683. static int dm_test_pre_reloc(struct unit_test_state *uts)
  684. {
  685. struct udevice *dev;
  686. /* The normal driver should refuse to bind before relocation */
  687. ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
  688. &driver_info_manual, &dev));
  689. /* But this one is marked pre-reloc */
  690. ut_assertok(device_bind_by_name(uts->root, true,
  691. &driver_info_pre_reloc, &dev));
  692. return 0;
  693. }
  694. DM_TEST(dm_test_pre_reloc, 0);
  695. /*
  696. * Test that removal of devices, either via the "normal" device_remove()
  697. * API or via the device driver selective flag works as expected
  698. */
  699. static int dm_test_remove_active_dma(struct unit_test_state *uts)
  700. {
  701. struct udevice *dev;
  702. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
  703. &dev));
  704. ut_assert(dev);
  705. /* Probe the device */
  706. ut_assertok(device_probe(dev));
  707. /* Test if device is active right now */
  708. ut_asserteq(true, device_active(dev));
  709. /* Remove the device via selective remove flag */
  710. dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
  711. /* Test if device is inactive right now */
  712. ut_asserteq(false, device_active(dev));
  713. /* Probe the device again */
  714. ut_assertok(device_probe(dev));
  715. /* Test if device is active right now */
  716. ut_asserteq(true, device_active(dev));
  717. /* Remove the device via "normal" remove API */
  718. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  719. /* Test if device is inactive right now */
  720. ut_asserteq(false, device_active(dev));
  721. /*
  722. * Test if a device without the active DMA flags is not removed upon
  723. * the active DMA remove call
  724. */
  725. ut_assertok(device_unbind(dev));
  726. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  727. &dev));
  728. ut_assert(dev);
  729. /* Probe the device */
  730. ut_assertok(device_probe(dev));
  731. /* Test if device is active right now */
  732. ut_asserteq(true, device_active(dev));
  733. /* Remove the device via selective remove flag */
  734. dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
  735. /* Test if device is still active right now */
  736. ut_asserteq(true, device_active(dev));
  737. return 0;
  738. }
  739. DM_TEST(dm_test_remove_active_dma, 0);
  740. /* Test removal of 'vital' devices */
  741. static int dm_test_remove_vital(struct unit_test_state *uts)
  742. {
  743. struct udevice *normal, *dma, *vital, *dma_vital;
  744. /* Skip the behaviour in test_post_probe() */
  745. uts->skip_post_probe = 1;
  746. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
  747. &normal));
  748. ut_assertnonnull(normal);
  749. ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
  750. &dma));
  751. ut_assertnonnull(dma);
  752. ut_assertok(device_bind_by_name(uts->root, false,
  753. &driver_info_vital_clk, &vital));
  754. ut_assertnonnull(vital);
  755. ut_assertok(device_bind_by_name(uts->root, false,
  756. &driver_info_act_dma_vital_clk,
  757. &dma_vital));
  758. ut_assertnonnull(dma_vital);
  759. /* Probe the devices */
  760. ut_assertok(device_probe(normal));
  761. ut_assertok(device_probe(dma));
  762. ut_assertok(device_probe(vital));
  763. ut_assertok(device_probe(dma_vital));
  764. /* Check that devices are active right now */
  765. ut_asserteq(true, device_active(normal));
  766. ut_asserteq(true, device_active(dma));
  767. ut_asserteq(true, device_active(vital));
  768. ut_asserteq(true, device_active(dma_vital));
  769. /* Remove active devices via selective remove flag */
  770. dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
  771. /*
  772. * Check that this only has an effect on the dma device, since two
  773. * devices are vital and the third does not have active DMA
  774. */
  775. ut_asserteq(true, device_active(normal));
  776. ut_asserteq(false, device_active(dma));
  777. ut_asserteq(true, device_active(vital));
  778. ut_asserteq(true, device_active(dma_vital));
  779. /* Remove active devices via selective remove flag */
  780. ut_assertok(device_probe(dma));
  781. dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
  782. /* This should have affected both active-dma devices */
  783. ut_asserteq(true, device_active(normal));
  784. ut_asserteq(false, device_active(dma));
  785. ut_asserteq(true, device_active(vital));
  786. ut_asserteq(false, device_active(dma_vital));
  787. /* Remove non-vital devices */
  788. ut_assertok(device_probe(dma));
  789. ut_assertok(device_probe(dma_vital));
  790. dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
  791. /* This should have affected only non-vital devices */
  792. ut_asserteq(false, device_active(normal));
  793. ut_asserteq(false, device_active(dma));
  794. ut_asserteq(true, device_active(vital));
  795. ut_asserteq(true, device_active(dma_vital));
  796. /* Remove vital devices via normal remove flag */
  797. ut_assertok(device_probe(normal));
  798. ut_assertok(device_probe(dma));
  799. dm_remove_devices_flags(DM_REMOVE_NORMAL);
  800. /* Check that all devices are inactive right now */
  801. ut_asserteq(false, device_active(normal));
  802. ut_asserteq(false, device_active(dma));
  803. ut_asserteq(false, device_active(vital));
  804. ut_asserteq(false, device_active(dma_vital));
  805. return 0;
  806. }
  807. DM_TEST(dm_test_remove_vital, 0);
  808. static int dm_test_uclass_before_ready(struct unit_test_state *uts)
  809. {
  810. struct uclass *uc;
  811. ut_assertok(uclass_get(UCLASS_TEST, &uc));
  812. gd->dm_root = NULL;
  813. gd->dm_root_f = NULL;
  814. memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
  815. ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
  816. ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc));
  817. return 0;
  818. }
  819. DM_TEST(dm_test_uclass_before_ready, 0);
  820. static int dm_test_uclass_devices_find(struct unit_test_state *uts)
  821. {
  822. struct udevice *dev;
  823. int ret;
  824. for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
  825. dev;
  826. ret = uclass_find_next_device(&dev)) {
  827. ut_assert(!ret);
  828. ut_assertnonnull(dev);
  829. }
  830. ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
  831. ut_assertnull(dev);
  832. return 0;
  833. }
  834. DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
  835. static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
  836. {
  837. struct udevice *finddev;
  838. struct udevice *testdev;
  839. int findret, ret;
  840. /*
  841. * For each test device found in fdt like: "a-test", "b-test", etc.,
  842. * use its name and try to find it by uclass_find_device_by_name().
  843. * Then, on success check if:
  844. * - current 'testdev' name is equal to the returned 'finddev' name
  845. * - current 'testdev' pointer is equal to the returned 'finddev'
  846. *
  847. * We assume that, each uclass's device name is unique, so if not, then
  848. * this will fail on checking condition: testdev == finddev, since the
  849. * uclass_find_device_by_name(), returns the first device by given name.
  850. */
  851. for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
  852. testdev;
  853. ret = uclass_find_next_device(&testdev)) {
  854. ut_assertok(ret);
  855. ut_assertnonnull(testdev);
  856. findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
  857. testdev->name,
  858. &finddev);
  859. ut_assertok(findret);
  860. ut_assert(testdev);
  861. ut_asserteq_str(testdev->name, finddev->name);
  862. ut_asserteq_ptr(testdev, finddev);
  863. }
  864. return 0;
  865. }
  866. DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
  867. static int dm_test_uclass_devices_get(struct unit_test_state *uts)
  868. {
  869. struct udevice *dev;
  870. int ret;
  871. for (ret = uclass_first_device_check(UCLASS_TEST, &dev);
  872. dev;
  873. ret = uclass_next_device_check(&dev)) {
  874. ut_assert(!ret);
  875. ut_assert(device_active(dev));
  876. }
  877. return 0;
  878. }
  879. DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
  880. static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
  881. {
  882. struct udevice *finddev;
  883. struct udevice *testdev;
  884. int ret, findret;
  885. /*
  886. * For each test device found in fdt like: "a-test", "b-test", etc.,
  887. * use its name and try to get it by uclass_get_device_by_name().
  888. * On success check if:
  889. * - returned finddev' is active
  890. * - current 'testdev' name is equal to the returned 'finddev' name
  891. * - current 'testdev' pointer is equal to the returned 'finddev'
  892. *
  893. * We asserts that the 'testdev' is active on each loop entry, so we
  894. * could be sure that the 'finddev' is activated too, but for sure
  895. * we check it again.
  896. *
  897. * We assume that, each uclass's device name is unique, so if not, then
  898. * this will fail on checking condition: testdev == finddev, since the
  899. * uclass_get_device_by_name(), returns the first device by given name.
  900. */
  901. for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev);
  902. testdev;
  903. ret = uclass_next_device_check(&testdev)) {
  904. ut_assertok(ret);
  905. ut_assert(device_active(testdev));
  906. findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
  907. testdev->name,
  908. &finddev);
  909. ut_assertok(findret);
  910. ut_assert(finddev);
  911. ut_assert(device_active(finddev));
  912. ut_asserteq_str(testdev->name, finddev->name);
  913. ut_asserteq_ptr(testdev, finddev);
  914. }
  915. return 0;
  916. }
  917. DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
  918. static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
  919. {
  920. struct udevice *dev;
  921. ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
  922. ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
  923. return 0;
  924. }
  925. DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
  926. static int dm_test_uclass_names(struct unit_test_state *uts)
  927. {
  928. ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
  929. ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
  930. ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi"));
  931. return 0;
  932. }
  933. DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
  934. static int dm_test_inactive_child(struct unit_test_state *uts)
  935. {
  936. struct udevice *parent, *dev1, *dev2;
  937. /* Skip the behaviour in test_post_probe() */
  938. uts->skip_post_probe = 1;
  939. ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
  940. /*
  941. * Create a child but do not activate it. Calling the function again
  942. * should return the same child.
  943. */
  944. ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
  945. UCLASS_TEST, &dev1));
  946. ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
  947. "test_child", 0, ofnode_null(), &dev1));
  948. ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
  949. &dev2));
  950. ut_asserteq_ptr(dev1, dev2);
  951. ut_assertok(device_probe(dev1));
  952. ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
  953. UCLASS_TEST, &dev2));
  954. return 0;
  955. }
  956. DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
  957. /* Make sure all bound devices have a sequence number */
  958. static int dm_test_all_have_seq(struct unit_test_state *uts)
  959. {
  960. struct udevice *dev;
  961. struct uclass *uc;
  962. list_for_each_entry(uc, gd->uclass_root, sibling_node) {
  963. list_for_each_entry(dev, &uc->dev_head, uclass_node) {
  964. if (dev->seq_ == -1)
  965. printf("Device '%s' has no seq (%d)\n",
  966. dev->name, dev->seq_);
  967. ut_assert(dev->seq_ != -1);
  968. }
  969. }
  970. return 0;
  971. }
  972. DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
  973. #if CONFIG_IS_ENABLED(DM_DMA)
  974. static int dm_test_dma_offset(struct unit_test_state *uts)
  975. {
  976. struct udevice *dev;
  977. ofnode node;
  978. /* Make sure the bus's dma-ranges aren't taken into account here */
  979. node = ofnode_path("/mmio-bus@0");
  980. ut_assert(ofnode_valid(node));
  981. ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
  982. ut_asserteq_64(0, dev->dma_offset);
  983. /* Device behind a bus with dma-ranges */
  984. node = ofnode_path("/mmio-bus@0/subnode@0");
  985. ut_assert(ofnode_valid(node));
  986. ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
  987. ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
  988. /* This one has no dma-ranges */
  989. node = ofnode_path("/mmio-bus@1");
  990. ut_assert(ofnode_valid(node));
  991. ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
  992. node = ofnode_path("/mmio-bus@1/subnode@0");
  993. ut_assert(ofnode_valid(node));
  994. ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
  995. ut_asserteq_64(0, dev->dma_offset);
  996. return 0;
  997. }
  998. DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  999. #endif
  1000. /* Test dm_get_stats() */
  1001. static int dm_test_get_stats(struct unit_test_state *uts)
  1002. {
  1003. int dev_count, uc_count;
  1004. dm_get_stats(&dev_count, &uc_count);
  1005. ut_assert(dev_count > 50);
  1006. ut_assert(uc_count > 30);
  1007. return 0;
  1008. }
  1009. DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT);
  1010. /* Test uclass_find_device_by_name() */
  1011. static int dm_test_uclass_find_device(struct unit_test_state *uts)
  1012. {
  1013. struct udevice *dev;
  1014. ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
  1015. ut_asserteq(-ENODEV,
  1016. uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
  1017. ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
  1018. &dev));
  1019. return 0;
  1020. }
  1021. DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT);
  1022. /* Test getting information about tags attached to devices */
  1023. static int dm_test_dev_get_attach(struct unit_test_state *uts)
  1024. {
  1025. struct udevice *dev;
  1026. ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
  1027. ut_asserteq_str("a-test", dev->name);
  1028. ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
  1029. ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
  1030. ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
  1031. ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
  1032. ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
  1033. ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
  1034. ut_asserteq(sizeof(struct dm_test_pdata),
  1035. dev_get_attach_size(dev, DM_TAG_PLAT));
  1036. ut_asserteq(sizeof(struct dm_test_priv),
  1037. dev_get_attach_size(dev, DM_TAG_PRIV));
  1038. ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV));
  1039. ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT));
  1040. ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
  1041. ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
  1042. return 0;
  1043. }
  1044. DM_TEST(dm_test_dev_get_attach, UT_TESTF_SCAN_FDT);
  1045. /* Test getting information about tags attached to bus devices */
  1046. static int dm_test_dev_get_attach_bus(struct unit_test_state *uts)
  1047. {
  1048. struct udevice *dev, *child;
  1049. ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev));
  1050. ut_asserteq_str("some-bus", dev->name);
  1051. ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
  1052. ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
  1053. ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
  1054. ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
  1055. ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
  1056. ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
  1057. ut_asserteq(sizeof(struct dm_test_pdata),
  1058. dev_get_attach_size(dev, DM_TAG_PLAT));
  1059. ut_asserteq(sizeof(struct dm_test_priv),
  1060. dev_get_attach_size(dev, DM_TAG_PRIV));
  1061. ut_asserteq(sizeof(struct dm_test_uclass_priv),
  1062. dev_get_attach_size(dev, DM_TAG_UC_PRIV));
  1063. ut_asserteq(sizeof(struct dm_test_uclass_plat),
  1064. dev_get_attach_size(dev, DM_TAG_UC_PLAT));
  1065. ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
  1066. ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
  1067. /* Now try the child of the bus */
  1068. ut_assertok(device_first_child_err(dev, &child));
  1069. ut_asserteq_str("c-test@5", child->name);
  1070. ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT));
  1071. ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV));
  1072. ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV));
  1073. ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT));
  1074. ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT));
  1075. ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV));
  1076. ut_asserteq(sizeof(struct dm_test_pdata),
  1077. dev_get_attach_size(child, DM_TAG_PLAT));
  1078. ut_asserteq(sizeof(struct dm_test_priv),
  1079. dev_get_attach_size(child, DM_TAG_PRIV));
  1080. ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV));
  1081. ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT));
  1082. ut_asserteq(sizeof(struct dm_test_parent_plat),
  1083. dev_get_attach_size(child, DM_TAG_PARENT_PLAT));
  1084. ut_asserteq(sizeof(struct dm_test_parent_data),
  1085. dev_get_attach_size(child, DM_TAG_PARENT_PRIV));
  1086. return 0;
  1087. }
  1088. DM_TEST(dm_test_dev_get_attach_bus, UT_TESTF_SCAN_FDT);
  1089. /* Test getting information about tags attached to bus devices */
  1090. static int dm_test_dev_get_mem(struct unit_test_state *uts)
  1091. {
  1092. struct dm_stats stats;
  1093. dm_get_mem(&stats);
  1094. return 0;
  1095. }
  1096. DM_TEST(dm_test_dev_get_mem, UT_TESTF_SCAN_FDT);