core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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/ut.h>
  16. #include <dm/util.h>
  17. #include <dm/test.h>
  18. #include <dm/uclass-internal.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. };
  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. U_BOOT_DEVICE(dm_test_info1) = {
  35. .name = "test_drv",
  36. .platdata = &test_pdata[0],
  37. };
  38. U_BOOT_DEVICE(dm_test_info2) = {
  39. .name = "test_drv",
  40. .platdata = &test_pdata[1],
  41. };
  42. U_BOOT_DEVICE(dm_test_info3) = {
  43. .name = "test_drv",
  44. .platdata = &test_pdata[2],
  45. };
  46. static struct driver_info driver_info_manual = {
  47. .name = "test_manual_drv",
  48. .platdata = &test_pdata_manual,
  49. };
  50. /* Test that binding with platdata occurs correctly */
  51. static int dm_test_autobind(struct dm_test_state *dms)
  52. {
  53. struct udevice *dev;
  54. /*
  55. * We should have a single class (UCLASS_ROOT) and a single root
  56. * device with no children.
  57. */
  58. ut_assert(dms->root);
  59. ut_asserteq(1, list_count_items(&gd->uclass_root));
  60. ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
  61. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
  62. ut_assertok(dm_scan_platdata());
  63. /* We should have our test class now at least, plus more children */
  64. ut_assert(1 < list_count_items(&gd->uclass_root));
  65. ut_assert(0 < list_count_items(&gd->dm_root->child_head));
  66. /* Our 3 dm_test_infox children should be bound to the test uclass */
  67. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
  68. /* No devices should be probed */
  69. list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
  70. ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
  71. /* Our test driver should have been bound 3 times */
  72. ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
  73. return 0;
  74. }
  75. DM_TEST(dm_test_autobind, 0);
  76. /* Test that autoprobe finds all the expected devices */
  77. static int dm_test_autoprobe(struct dm_test_state *dms)
  78. {
  79. int expected_base_add;
  80. struct udevice *dev;
  81. struct uclass *uc;
  82. int i;
  83. ut_assertok(uclass_get(UCLASS_TEST, &uc));
  84. ut_assert(uc);
  85. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
  86. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
  87. /* The root device should not be activated until needed */
  88. ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
  89. /*
  90. * We should be able to find the three test devices, and they should
  91. * all be activated as they are used (lazy activation, required by
  92. * U-Boot)
  93. */
  94. for (i = 0; i < 3; i++) {
  95. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  96. ut_assert(dev);
  97. ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
  98. "Driver %d/%s already activated", i, dev->name);
  99. /* This should activate it */
  100. ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
  101. ut_assert(dev);
  102. ut_assert(dev->flags & DM_FLAG_ACTIVATED);
  103. /* Activating a device should activate the root device */
  104. if (!i)
  105. ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
  106. }
  107. /* Our 3 dm_test_infox children should be passed to post_probe */
  108. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
  109. /* Also we can check the per-device data */
  110. expected_base_add = 0;
  111. for (i = 0; i < 3; i++) {
  112. struct dm_test_uclass_perdev_priv *priv;
  113. struct dm_test_pdata *pdata;
  114. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  115. ut_assert(dev);
  116. priv = dev->uclass_priv;
  117. ut_assert(priv);
  118. ut_asserteq(expected_base_add, priv->base_add);
  119. pdata = dev->platdata;
  120. expected_base_add += pdata->ping_add;
  121. }
  122. return 0;
  123. }
  124. DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
  125. /* Check that we see the correct platdata in each device */
  126. static int dm_test_platdata(struct dm_test_state *dms)
  127. {
  128. const struct dm_test_pdata *pdata;
  129. struct udevice *dev;
  130. int i;
  131. for (i = 0; i < 3; i++) {
  132. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  133. ut_assert(dev);
  134. pdata = dev->platdata;
  135. ut_assert(pdata->ping_add == test_pdata[i].ping_add);
  136. }
  137. return 0;
  138. }
  139. DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
  140. /* Test that we can bind, probe, remove, unbind a driver */
  141. static int dm_test_lifecycle(struct dm_test_state *dms)
  142. {
  143. int op_count[DM_TEST_OP_COUNT];
  144. struct udevice *dev, *test_dev;
  145. int pingret;
  146. int ret;
  147. memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
  148. ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
  149. &dev));
  150. ut_assert(dev);
  151. ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
  152. == op_count[DM_TEST_OP_BIND] + 1);
  153. ut_assert(!dev->priv);
  154. /* Probe the device - it should fail allocating private data */
  155. dms->force_fail_alloc = 1;
  156. ret = device_probe(dev);
  157. ut_assert(ret == -ENOMEM);
  158. ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
  159. == op_count[DM_TEST_OP_PROBE] + 1);
  160. ut_assert(!dev->priv);
  161. /* Try again without the alloc failure */
  162. dms->force_fail_alloc = 0;
  163. ut_assertok(device_probe(dev));
  164. ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
  165. == op_count[DM_TEST_OP_PROBE] + 2);
  166. ut_assert(dev->priv);
  167. /* This should be device 3 in the uclass */
  168. ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
  169. ut_assert(dev == test_dev);
  170. /* Try ping */
  171. ut_assertok(test_ping(dev, 100, &pingret));
  172. ut_assert(pingret == 102);
  173. /* Now remove device 3 */
  174. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
  175. ut_assertok(device_remove(dev));
  176. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
  177. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  178. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
  179. ut_assertok(device_unbind(dev));
  180. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  181. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
  182. return 0;
  183. }
  184. DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
  185. /* Test that we can bind/unbind and the lists update correctly */
  186. static int dm_test_ordering(struct dm_test_state *dms)
  187. {
  188. struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
  189. int pingret;
  190. ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
  191. &dev));
  192. ut_assert(dev);
  193. /* Bind two new devices (numbers 4 and 5) */
  194. ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
  195. &dev_penultimate));
  196. ut_assert(dev_penultimate);
  197. ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
  198. &dev_last));
  199. ut_assert(dev_last);
  200. /* Now remove device 3 */
  201. ut_assertok(device_remove(dev));
  202. ut_assertok(device_unbind(dev));
  203. /* The device numbering should have shifted down one */
  204. ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
  205. ut_assert(dev_penultimate == test_dev);
  206. ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
  207. ut_assert(dev_last == test_dev);
  208. /* Add back the original device 3, now in position 5 */
  209. ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, &dev));
  210. ut_assert(dev);
  211. /* Try ping */
  212. ut_assertok(test_ping(dev, 100, &pingret));
  213. ut_assert(pingret == 102);
  214. /* Remove 3 and 4 */
  215. ut_assertok(device_remove(dev_penultimate));
  216. ut_assertok(device_unbind(dev_penultimate));
  217. ut_assertok(device_remove(dev_last));
  218. ut_assertok(device_unbind(dev_last));
  219. /* Our device should now be in position 3 */
  220. ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
  221. ut_assert(dev == test_dev);
  222. /* Now remove device 3 */
  223. ut_assertok(device_remove(dev));
  224. ut_assertok(device_unbind(dev));
  225. return 0;
  226. }
  227. DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
  228. /* Check that we can perform operations on a device (do a ping) */
  229. int dm_check_operations(struct dm_test_state *dms, struct udevice *dev,
  230. uint32_t base, struct dm_test_priv *priv)
  231. {
  232. int expected;
  233. int pingret;
  234. /* Getting the child device should allocate platdata / priv */
  235. ut_assertok(testfdt_ping(dev, 10, &pingret));
  236. ut_assert(dev->priv);
  237. ut_assert(dev->platdata);
  238. expected = 10 + base;
  239. ut_asserteq(expected, pingret);
  240. /* Do another ping */
  241. ut_assertok(testfdt_ping(dev, 20, &pingret));
  242. expected = 20 + base;
  243. ut_asserteq(expected, pingret);
  244. /* Now check the ping_total */
  245. priv = dev->priv;
  246. ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
  247. priv->ping_total);
  248. return 0;
  249. }
  250. /* Check that we can perform operations on devices */
  251. static int dm_test_operations(struct dm_test_state *dms)
  252. {
  253. struct udevice *dev;
  254. int i;
  255. /*
  256. * Now check that the ping adds are what we expect. This is using the
  257. * ping-add property in each node.
  258. */
  259. for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
  260. uint32_t base;
  261. ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
  262. /*
  263. * Get the 'reg' property, which tells us what the ping add
  264. * should be. We don't use the platdata because we want
  265. * to test the code that sets that up (testfdt_drv_probe()).
  266. */
  267. base = test_pdata[i].ping_add;
  268. debug("dev=%d, base=%d\n", i, base);
  269. ut_assert(!dm_check_operations(dms, dev, base, dev->priv));
  270. }
  271. return 0;
  272. }
  273. DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
  274. /* Remove all drivers and check that things work */
  275. static int dm_test_remove(struct dm_test_state *dms)
  276. {
  277. struct udevice *dev;
  278. int i;
  279. for (i = 0; i < 3; i++) {
  280. ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
  281. ut_assert(dev);
  282. ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
  283. "Driver %d/%s not activated", i, dev->name);
  284. ut_assertok(device_remove(dev));
  285. ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
  286. "Driver %d/%s should have deactivated", i,
  287. dev->name);
  288. ut_assert(!dev->priv);
  289. }
  290. return 0;
  291. }
  292. DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
  293. /* Remove and recreate everything, check for memory leaks */
  294. static int dm_test_leak(struct dm_test_state *dms)
  295. {
  296. int i;
  297. for (i = 0; i < 2; i++) {
  298. struct mallinfo start, end;
  299. struct udevice *dev;
  300. int ret;
  301. int id;
  302. start = mallinfo();
  303. if (!start.uordblks)
  304. puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
  305. ut_assertok(dm_scan_platdata());
  306. ut_assertok(dm_scan_fdt(gd->fdt_blob));
  307. /* Scanning the uclass is enough to probe all the devices */
  308. for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
  309. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  310. dev;
  311. ret = uclass_next_device(&dev))
  312. ;
  313. ut_assertok(ret);
  314. }
  315. /* Don't delete the root class, since we started with that */
  316. for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
  317. struct uclass *uc;
  318. uc = uclass_find(id);
  319. if (!uc)
  320. continue;
  321. ut_assertok(uclass_destroy(uc));
  322. }
  323. end = mallinfo();
  324. ut_asserteq(start.uordblks, end.uordblks);
  325. }
  326. return 0;
  327. }
  328. DM_TEST(dm_test_leak, 0);
  329. /* Test uclass init/destroy methods */
  330. static int dm_test_uclass(struct dm_test_state *dms)
  331. {
  332. struct uclass *uc;
  333. ut_assertok(uclass_get(UCLASS_TEST, &uc));
  334. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
  335. ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
  336. ut_assert(uc->priv);
  337. ut_assertok(uclass_destroy(uc));
  338. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
  339. ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
  340. return 0;
  341. }
  342. DM_TEST(dm_test_uclass, 0);
  343. /**
  344. * create_children() - Create children of a parent node
  345. *
  346. * @dms: Test system state
  347. * @parent: Parent device
  348. * @count: Number of children to create
  349. * @key: Key value to put in first child. Subsequence children
  350. * receive an incrementing value
  351. * @child: If not NULL, then the child device pointers are written into
  352. * this array.
  353. * @return 0 if OK, -ve on error
  354. */
  355. static int create_children(struct dm_test_state *dms, struct udevice *parent,
  356. int count, int key, struct udevice *child[])
  357. {
  358. struct udevice *dev;
  359. int i;
  360. for (i = 0; i < count; i++) {
  361. struct dm_test_pdata *pdata;
  362. ut_assertok(device_bind_by_name(parent, &driver_info_manual,
  363. &dev));
  364. pdata = calloc(1, sizeof(*pdata));
  365. pdata->ping_add = key + i;
  366. dev->platdata = pdata;
  367. if (child)
  368. child[i] = dev;
  369. }
  370. return 0;
  371. }
  372. #define NODE_COUNT 10
  373. static int dm_test_children(struct dm_test_state *dms)
  374. {
  375. struct udevice *top[NODE_COUNT];
  376. struct udevice *child[NODE_COUNT];
  377. struct udevice *grandchild[NODE_COUNT];
  378. struct udevice *dev;
  379. int total;
  380. int ret;
  381. int i;
  382. /* We don't care about the numbering for this test */
  383. dms->skip_post_probe = 1;
  384. ut_assert(NODE_COUNT > 5);
  385. /* First create 10 top-level children */
  386. ut_assertok(create_children(dms, dms->root, NODE_COUNT, 0, top));
  387. /* Now a few have their own children */
  388. ut_assertok(create_children(dms, top[2], NODE_COUNT, 2, NULL));
  389. ut_assertok(create_children(dms, top[5], NODE_COUNT, 5, child));
  390. /* And grandchildren */
  391. for (i = 0; i < NODE_COUNT; i++)
  392. ut_assertok(create_children(dms, child[i], NODE_COUNT, 50 * i,
  393. i == 2 ? grandchild : NULL));
  394. /* Check total number of devices */
  395. total = NODE_COUNT * (3 + NODE_COUNT);
  396. ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
  397. /* Try probing one of the grandchildren */
  398. ut_assertok(uclass_get_device(UCLASS_TEST,
  399. NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
  400. ut_asserteq_ptr(grandchild[0], dev);
  401. /*
  402. * This should have probed the child and top node also, for a total
  403. * of 3 nodes.
  404. */
  405. ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
  406. /* Probe the other grandchildren */
  407. for (i = 1; i < NODE_COUNT; i++)
  408. ut_assertok(device_probe(grandchild[i]));
  409. ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
  410. /* Probe everything */
  411. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  412. dev;
  413. ret = uclass_next_device(&dev))
  414. ;
  415. ut_assertok(ret);
  416. ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
  417. /* Remove a top-level child and check that the children are removed */
  418. ut_assertok(device_remove(top[2]));
  419. ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
  420. dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
  421. /* Try one with grandchildren */
  422. ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
  423. ut_asserteq_ptr(dev, top[5]);
  424. ut_assertok(device_remove(dev));
  425. ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
  426. dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
  427. /* Try the same with unbind */
  428. ut_assertok(device_unbind(top[2]));
  429. ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  430. dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
  431. /* Try one with grandchildren */
  432. ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
  433. ut_asserteq_ptr(dev, top[6]);
  434. ut_assertok(device_unbind(top[5]));
  435. ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
  436. dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
  437. return 0;
  438. }
  439. DM_TEST(dm_test_children, 0);