bus.c 14 KB

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