bus.c 14 KB

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