core.c 24 KB

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