core.c 23 KB

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