bus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <dm/device-internal.h>
  9. #include <dm/test.h>
  10. #include <dm/uclass-internal.h>
  11. #include <dm/util.h>
  12. #include <test/ut.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. struct dm_test_parent_platdata {
  15. int count;
  16. int bind_flag;
  17. int uclass_bind_flag;
  18. };
  19. enum {
  20. FLAG_CHILD_PROBED = 10,
  21. FLAG_CHILD_REMOVED = -7,
  22. };
  23. static struct dm_test_state *test_state;
  24. static int testbus_drv_probe(struct udevice *dev)
  25. {
  26. return dm_scan_fdt_dev(dev);
  27. }
  28. static int testbus_child_post_bind(struct udevice *dev)
  29. {
  30. struct dm_test_parent_platdata *plat;
  31. plat = dev_get_parent_platdata(dev);
  32. plat->bind_flag = 1;
  33. plat->uclass_bind_flag = 2;
  34. return 0;
  35. }
  36. static int testbus_child_pre_probe(struct udevice *dev)
  37. {
  38. struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
  39. parent_data->flag += FLAG_CHILD_PROBED;
  40. return 0;
  41. }
  42. static int testbus_child_pre_probe_uclass(struct udevice *dev)
  43. {
  44. struct dm_test_priv *priv = dev_get_priv(dev);
  45. priv->uclass_flag++;
  46. return 0;
  47. }
  48. static int testbus_child_post_remove(struct udevice *dev)
  49. {
  50. struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
  51. struct dm_test_state *dms = test_state;
  52. parent_data->flag += FLAG_CHILD_REMOVED;
  53. if (dms)
  54. dms->removed = dev;
  55. return 0;
  56. }
  57. static const struct udevice_id testbus_ids[] = {
  58. {
  59. .compatible = "denx,u-boot-test-bus",
  60. .data = DM_TEST_TYPE_FIRST },
  61. { }
  62. };
  63. U_BOOT_DRIVER(testbus_drv) = {
  64. .name = "testbus_drv",
  65. .of_match = testbus_ids,
  66. .id = UCLASS_TEST_BUS,
  67. .probe = testbus_drv_probe,
  68. .child_post_bind = testbus_child_post_bind,
  69. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  70. .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
  71. .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
  72. .per_child_platdata_auto_alloc_size =
  73. sizeof(struct dm_test_parent_platdata),
  74. .child_pre_probe = testbus_child_pre_probe,
  75. .child_post_remove = testbus_child_post_remove,
  76. };
  77. UCLASS_DRIVER(testbus) = {
  78. .name = "testbus",
  79. .id = UCLASS_TEST_BUS,
  80. .flags = DM_UC_FLAG_SEQ_ALIAS,
  81. .child_pre_probe = testbus_child_pre_probe_uclass,
  82. };
  83. /* Test that we can probe for children */
  84. static int dm_test_bus_children(struct unit_test_state *uts)
  85. {
  86. int num_devices = 6;
  87. struct udevice *bus;
  88. struct uclass *uc;
  89. ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
  90. ut_asserteq(num_devices, list_count_items(&uc->dev_head));
  91. /* Probe the bus, which should yield 3 more devices */
  92. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  93. num_devices += 3;
  94. ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
  95. ut_asserteq(num_devices, list_count_items(&uc->dev_head));
  96. ut_assert(!dm_check_devices(uts, num_devices));
  97. return 0;
  98. }
  99. DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  100. /* Test our functions for accessing children */
  101. static int dm_test_bus_children_funcs(struct unit_test_state *uts)
  102. {
  103. const void *blob = gd->fdt_blob;
  104. struct udevice *bus, *dev;
  105. int node;
  106. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  107. /* device_get_child() */
  108. ut_assertok(device_get_child(bus, 0, &dev));
  109. ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
  110. ut_assertok(device_get_child_by_seq(bus, 5, &dev));
  111. ut_assert(dev->flags & DM_FLAG_ACTIVATED);
  112. ut_asserteq_str("c-test@5", dev->name);
  113. /* Device with sequence number 0 should be accessible */
  114. ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
  115. ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
  116. ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
  117. ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
  118. ut_assertok(device_get_child_by_seq(bus, 0, &dev));
  119. ut_assert(dev->flags & DM_FLAG_ACTIVATED);
  120. /* There is no device with sequence number 2 */
  121. ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
  122. ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
  123. ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
  124. /* Looking for something that is not a child */
  125. node = fdt_path_offset(blob, "/junk");
  126. ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
  127. node = fdt_path_offset(blob, "/d-test");
  128. ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
  129. return 0;
  130. }
  131. DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  132. static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
  133. {
  134. const void *blob = gd->fdt_blob;
  135. struct udevice *bus, *dev;
  136. int node;
  137. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  138. ut_assertnonnull(bus);
  139. /* Find a valid child */
  140. node = fdt_path_offset(blob, "/some-bus/c-test@1");
  141. ut_assert(node > 0);
  142. ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
  143. ut_assertnonnull(dev);
  144. ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
  145. ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
  146. ut_assertnonnull(dev);
  147. ut_assert(dev->flags & DM_FLAG_ACTIVATED);
  148. return 0;
  149. }
  150. DM_TEST(dm_test_bus_children_of_offset,
  151. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
  152. /* Test that we can iterate through children */
  153. static int dm_test_bus_children_iterators(struct unit_test_state *uts)
  154. {
  155. struct udevice *bus, *dev, *child;
  156. /* Walk through the children one by one */
  157. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  158. ut_assertok(device_find_first_child(bus, &dev));
  159. ut_asserteq_str("c-test@5", dev->name);
  160. ut_assertok(device_find_next_child(&dev));
  161. ut_asserteq_str("c-test@0", dev->name);
  162. ut_assertok(device_find_next_child(&dev));
  163. ut_asserteq_str("c-test@1", dev->name);
  164. ut_assertok(device_find_next_child(&dev));
  165. ut_asserteq_ptr(dev, NULL);
  166. /* Move to the next child without using device_find_first_child() */
  167. ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
  168. ut_asserteq_str("c-test@5", dev->name);
  169. ut_assertok(device_find_next_child(&dev));
  170. ut_asserteq_str("c-test@0", dev->name);
  171. /* Try a device with no children */
  172. ut_assertok(device_find_first_child(dev, &child));
  173. ut_asserteq_ptr(child, NULL);
  174. return 0;
  175. }
  176. DM_TEST(dm_test_bus_children_iterators,
  177. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  178. /* Test that the bus can store data about each child */
  179. static int test_bus_parent_data(struct unit_test_state *uts)
  180. {
  181. struct dm_test_parent_data *parent_data;
  182. struct udevice *bus, *dev;
  183. struct uclass *uc;
  184. int value;
  185. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  186. /* Check that parent data is allocated */
  187. ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
  188. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  189. ut_assertok(device_get_child_by_seq(bus, 0, &dev));
  190. parent_data = dev_get_parent_priv(dev);
  191. ut_assert(NULL != parent_data);
  192. /* Check that it starts at 0 and goes away when device is removed */
  193. parent_data->sum += 5;
  194. ut_asserteq(5, parent_data->sum);
  195. device_remove(dev, DM_REMOVE_NORMAL);
  196. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  197. /* Check that we can do this twice */
  198. ut_assertok(device_get_child_by_seq(bus, 0, &dev));
  199. parent_data = dev_get_parent_priv(dev);
  200. ut_assert(NULL != parent_data);
  201. parent_data->sum += 5;
  202. ut_asserteq(5, parent_data->sum);
  203. /* Add parent data to all children */
  204. ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
  205. value = 5;
  206. uclass_foreach_dev(dev, uc) {
  207. /* Ignore these if they are not on this bus */
  208. if (dev->parent != bus) {
  209. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  210. continue;
  211. }
  212. ut_assertok(device_probe(dev));
  213. parent_data = dev_get_parent_priv(dev);
  214. parent_data->sum = value;
  215. value += 5;
  216. }
  217. /* Check it is still there */
  218. value = 5;
  219. uclass_foreach_dev(dev, uc) {
  220. /* Ignore these if they are not on this bus */
  221. if (dev->parent != bus)
  222. continue;
  223. parent_data = dev_get_parent_priv(dev);
  224. ut_asserteq(value, parent_data->sum);
  225. value += 5;
  226. }
  227. return 0;
  228. }
  229. /* Test that the bus can store data about each child */
  230. static int dm_test_bus_parent_data(struct unit_test_state *uts)
  231. {
  232. return test_bus_parent_data(uts);
  233. }
  234. DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  235. /* As above but the size is controlled by the uclass */
  236. static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
  237. {
  238. struct driver *drv;
  239. struct udevice *bus;
  240. int size;
  241. int ret;
  242. /* Set the driver size to 0 so that the uclass size is used */
  243. ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
  244. drv = (struct driver *)bus->driver;
  245. size = drv->per_child_auto_alloc_size;
  246. bus->uclass->uc_drv->per_child_auto_alloc_size = size;
  247. drv->per_child_auto_alloc_size = 0;
  248. ret = test_bus_parent_data(uts);
  249. if (ret)
  250. return ret;
  251. bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
  252. drv->per_child_auto_alloc_size = size;
  253. return 0;
  254. }
  255. DM_TEST(dm_test_bus_parent_data_uclass,
  256. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  257. /* Test that the bus ops are called when a child is probed/removed */
  258. static int dm_test_bus_parent_ops(struct unit_test_state *uts)
  259. {
  260. struct dm_test_parent_data *parent_data;
  261. struct dm_test_state *dms = uts->priv;
  262. struct udevice *bus, *dev;
  263. struct uclass *uc;
  264. test_state = dms;
  265. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  266. ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
  267. uclass_foreach_dev(dev, uc) {
  268. /* Ignore these if they are not on this bus */
  269. if (dev->parent != bus)
  270. continue;
  271. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  272. ut_assertok(device_probe(dev));
  273. parent_data = dev_get_parent_priv(dev);
  274. ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
  275. }
  276. uclass_foreach_dev(dev, uc) {
  277. /* Ignore these if they are not on this bus */
  278. if (dev->parent != bus)
  279. continue;
  280. parent_data = dev_get_parent_priv(dev);
  281. ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
  282. ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
  283. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  284. ut_asserteq_ptr(dms->removed, dev);
  285. }
  286. test_state = NULL;
  287. return 0;
  288. }
  289. DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  290. static int test_bus_parent_platdata(struct unit_test_state *uts)
  291. {
  292. struct dm_test_parent_platdata *plat;
  293. struct udevice *bus, *dev;
  294. int child_count;
  295. /* Check that the bus has no children */
  296. ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
  297. device_find_first_child(bus, &dev);
  298. ut_asserteq_ptr(NULL, dev);
  299. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  300. for (device_find_first_child(bus, &dev), child_count = 0;
  301. dev;
  302. device_find_next_child(&dev)) {
  303. /* Check that platform data is allocated */
  304. plat = dev_get_parent_platdata(dev);
  305. ut_assert(plat != NULL);
  306. /*
  307. * Check that it is not affected by the device being
  308. * probed/removed
  309. */
  310. plat->count++;
  311. ut_asserteq(1, plat->count);
  312. device_probe(dev);
  313. device_remove(dev, DM_REMOVE_NORMAL);
  314. ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
  315. ut_asserteq(1, plat->count);
  316. ut_assertok(device_probe(dev));
  317. child_count++;
  318. }
  319. ut_asserteq(3, child_count);
  320. /* Removing the bus should also have no effect (it is still bound) */
  321. device_remove(bus, DM_REMOVE_NORMAL);
  322. for (device_find_first_child(bus, &dev), child_count = 0;
  323. dev;
  324. device_find_next_child(&dev)) {
  325. /* Check that platform data is allocated */
  326. plat = dev_get_parent_platdata(dev);
  327. ut_assert(plat != NULL);
  328. ut_asserteq(1, plat->count);
  329. child_count++;
  330. }
  331. ut_asserteq(3, child_count);
  332. /* Unbind all the children */
  333. do {
  334. device_find_first_child(bus, &dev);
  335. if (dev)
  336. device_unbind(dev);
  337. } while (dev);
  338. /* Now the child platdata should be removed and re-added */
  339. device_probe(bus);
  340. for (device_find_first_child(bus, &dev), child_count = 0;
  341. dev;
  342. device_find_next_child(&dev)) {
  343. /* Check that platform data is allocated */
  344. plat = dev_get_parent_platdata(dev);
  345. ut_assert(plat != NULL);
  346. ut_asserteq(0, plat->count);
  347. child_count++;
  348. }
  349. ut_asserteq(3, child_count);
  350. return 0;
  351. }
  352. /* Test that the bus can store platform data about each child */
  353. static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
  354. {
  355. return test_bus_parent_platdata(uts);
  356. }
  357. DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  358. /* As above but the size is controlled by the uclass */
  359. static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
  360. {
  361. struct udevice *bus;
  362. struct driver *drv;
  363. int size;
  364. int ret;
  365. /* Set the driver size to 0 so that the uclass size is used */
  366. ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
  367. drv = (struct driver *)bus->driver;
  368. size = drv->per_child_platdata_auto_alloc_size;
  369. bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
  370. drv->per_child_platdata_auto_alloc_size = 0;
  371. ret = test_bus_parent_platdata(uts);
  372. if (ret)
  373. return ret;
  374. bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
  375. drv->per_child_platdata_auto_alloc_size = size;
  376. return 0;
  377. }
  378. DM_TEST(dm_test_bus_parent_platdata_uclass,
  379. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  380. /* Test that the child post_bind method is called */
  381. static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
  382. {
  383. struct dm_test_parent_platdata *plat;
  384. struct udevice *bus, *dev;
  385. int child_count;
  386. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  387. for (device_find_first_child(bus, &dev), child_count = 0;
  388. dev;
  389. device_find_next_child(&dev)) {
  390. /* Check that platform data is allocated */
  391. plat = dev_get_parent_platdata(dev);
  392. ut_assert(plat != NULL);
  393. ut_asserteq(1, plat->bind_flag);
  394. child_count++;
  395. }
  396. ut_asserteq(3, child_count);
  397. return 0;
  398. }
  399. DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  400. /* Test that the child post_bind method is called */
  401. static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
  402. {
  403. struct dm_test_parent_platdata *plat;
  404. struct udevice *bus, *dev;
  405. int child_count;
  406. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  407. for (device_find_first_child(bus, &dev), child_count = 0;
  408. dev;
  409. device_find_next_child(&dev)) {
  410. /* Check that platform data is allocated */
  411. plat = dev_get_parent_platdata(dev);
  412. ut_assert(plat != NULL);
  413. ut_asserteq(2, plat->uclass_bind_flag);
  414. child_count++;
  415. }
  416. ut_asserteq(3, child_count);
  417. return 0;
  418. }
  419. DM_TEST(dm_test_bus_child_post_bind_uclass,
  420. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  421. /*
  422. * Test that the bus' uclass' child_pre_probe() is called before the
  423. * device's probe() method
  424. */
  425. static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
  426. {
  427. struct udevice *bus, *dev;
  428. int child_count;
  429. /*
  430. * See testfdt_drv_probe() which effectively checks that the uclass
  431. * flag is set before that method is called
  432. */
  433. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  434. for (device_find_first_child(bus, &dev), child_count = 0;
  435. dev;
  436. device_find_next_child(&dev)) {
  437. struct dm_test_priv *priv = dev_get_priv(dev);
  438. /* Check that things happened in the right order */
  439. ut_asserteq_ptr(NULL, priv);
  440. ut_assertok(device_probe(dev));
  441. priv = dev_get_priv(dev);
  442. ut_assert(priv != NULL);
  443. ut_asserteq(1, priv->uclass_flag);
  444. ut_asserteq(1, priv->uclass_total);
  445. child_count++;
  446. }
  447. ut_asserteq(3, child_count);
  448. return 0;
  449. }
  450. DM_TEST(dm_test_bus_child_pre_probe_uclass,
  451. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);