core.c 21 KB

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