core.c 29 KB

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