core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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_DEVICE(dm_test_info1) = {
  40. .name = "test_drv",
  41. .platdata = &test_pdata[0],
  42. };
  43. U_BOOT_DEVICE(dm_test_info2) = {
  44. .name = "test_drv",
  45. .platdata = &test_pdata[1],
  46. };
  47. U_BOOT_DEVICE(dm_test_info3) = {
  48. .name = "test_drv",
  49. .platdata = &test_pdata[2],
  50. };
  51. static struct driver_info driver_info_manual = {
  52. .name = "test_manual_drv",
  53. .platdata = &test_pdata_manual,
  54. };
  55. static struct driver_info driver_info_pre_reloc = {
  56. .name = "test_pre_reloc_drv",
  57. .platdata = &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 platdata 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_platdata(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->flags & 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 platdata 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_platdata pointer.
  127. */
  128. ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
  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_platdata(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 platdata 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_platdata(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(dms->root->flags & 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->flags & 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->flags & DM_FLAG_ACTIVATED);
  190. /* Activating a device should activate the root device */
  191. if (!i)
  192. ut_assert(dms->root->flags & 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->platdata;
  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 platdata in each device */
  217. static int dm_test_platdata(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->platdata;
  226. ut_assert(pdata->ping_add == test_pdata[i].ping_add);
  227. }
  228. return 0;
  229. }
  230. DM_TEST(dm_test_platdata, 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->priv);
  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->priv);
  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->priv);
  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 platdata / priv */
  329. ut_assertok(testfdt_ping(dev, 10, &pingret));
  330. ut_assert(dev->priv);
  331. ut_assert(dev->platdata);
  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->priv;
  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 platdata 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->priv));
  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->flags & 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->flags & DM_FLAG_ACTIVATED),
  380. "Driver %d/%s should have deactivated", i,
  381. dev->name);
  382. ut_assert(!dev->priv);
  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_platdata(false));
  397. ut_assertok(dm_scan_fdt(gd->fdt_blob, 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(uc->priv);
  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->platdata = 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_ofnode(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);