bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /* Find a valid child */
  130. node = fdt_path_offset(blob, "/some-bus/c-test@1");
  131. ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
  132. ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
  133. ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
  134. ut_assert(dev->flags & DM_FLAG_ACTIVATED);
  135. return 0;
  136. }
  137. DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  138. /* Test that we can iterate through children */
  139. static int dm_test_bus_children_iterators(struct unit_test_state *uts)
  140. {
  141. struct udevice *bus, *dev, *child;
  142. /* Walk through the children one by one */
  143. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  144. ut_assertok(device_find_first_child(bus, &dev));
  145. ut_asserteq_str("c-test@5", dev->name);
  146. ut_assertok(device_find_next_child(&dev));
  147. ut_asserteq_str("c-test@0", dev->name);
  148. ut_assertok(device_find_next_child(&dev));
  149. ut_asserteq_str("c-test@1", dev->name);
  150. ut_assertok(device_find_next_child(&dev));
  151. ut_asserteq_ptr(dev, NULL);
  152. /* Move to the next child without using device_find_first_child() */
  153. ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
  154. ut_asserteq_str("c-test@5", dev->name);
  155. ut_assertok(device_find_next_child(&dev));
  156. ut_asserteq_str("c-test@0", dev->name);
  157. /* Try a device with no children */
  158. ut_assertok(device_find_first_child(dev, &child));
  159. ut_asserteq_ptr(child, NULL);
  160. return 0;
  161. }
  162. DM_TEST(dm_test_bus_children_iterators,
  163. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  164. /* Test that the bus can store data about each child */
  165. static int test_bus_parent_data(struct unit_test_state *uts)
  166. {
  167. struct dm_test_parent_data *parent_data;
  168. struct udevice *bus, *dev;
  169. struct uclass *uc;
  170. int value;
  171. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  172. /* Check that parent data is allocated */
  173. ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
  174. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  175. ut_assertok(device_get_child_by_seq(bus, 0, &dev));
  176. parent_data = dev_get_parent_priv(dev);
  177. ut_assert(NULL != parent_data);
  178. /* Check that it starts at 0 and goes away when device is removed */
  179. parent_data->sum += 5;
  180. ut_asserteq(5, parent_data->sum);
  181. device_remove(dev);
  182. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  183. /* Check that we can do this twice */
  184. ut_assertok(device_get_child_by_seq(bus, 0, &dev));
  185. parent_data = dev_get_parent_priv(dev);
  186. ut_assert(NULL != parent_data);
  187. parent_data->sum += 5;
  188. ut_asserteq(5, parent_data->sum);
  189. /* Add parent data to all children */
  190. ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
  191. value = 5;
  192. uclass_foreach_dev(dev, uc) {
  193. /* Ignore these if they are not on this bus */
  194. if (dev->parent != bus) {
  195. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  196. continue;
  197. }
  198. ut_assertok(device_probe(dev));
  199. parent_data = dev_get_parent_priv(dev);
  200. parent_data->sum = value;
  201. value += 5;
  202. }
  203. /* Check it is still there */
  204. value = 5;
  205. uclass_foreach_dev(dev, uc) {
  206. /* Ignore these if they are not on this bus */
  207. if (dev->parent != bus)
  208. continue;
  209. parent_data = dev_get_parent_priv(dev);
  210. ut_asserteq(value, parent_data->sum);
  211. value += 5;
  212. }
  213. return 0;
  214. }
  215. /* Test that the bus can store data about each child */
  216. static int dm_test_bus_parent_data(struct unit_test_state *uts)
  217. {
  218. return test_bus_parent_data(uts);
  219. }
  220. DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  221. /* As above but the size is controlled by the uclass */
  222. static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
  223. {
  224. struct driver *drv;
  225. struct udevice *bus;
  226. int size;
  227. int ret;
  228. /* Set the driver size to 0 so that the uclass size is used */
  229. ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
  230. drv = (struct driver *)bus->driver;
  231. size = drv->per_child_auto_alloc_size;
  232. bus->uclass->uc_drv->per_child_auto_alloc_size = size;
  233. drv->per_child_auto_alloc_size = 0;
  234. ret = test_bus_parent_data(uts);
  235. if (ret)
  236. return ret;
  237. bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
  238. drv->per_child_auto_alloc_size = size;
  239. return 0;
  240. }
  241. DM_TEST(dm_test_bus_parent_data_uclass,
  242. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  243. /* Test that the bus ops are called when a child is probed/removed */
  244. static int dm_test_bus_parent_ops(struct unit_test_state *uts)
  245. {
  246. struct dm_test_parent_data *parent_data;
  247. struct dm_test_state *dms = uts->priv;
  248. struct udevice *bus, *dev;
  249. struct uclass *uc;
  250. test_state = dms;
  251. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  252. ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
  253. uclass_foreach_dev(dev, uc) {
  254. /* Ignore these if they are not on this bus */
  255. if (dev->parent != bus)
  256. continue;
  257. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  258. ut_assertok(device_probe(dev));
  259. parent_data = dev_get_parent_priv(dev);
  260. ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
  261. }
  262. uclass_foreach_dev(dev, uc) {
  263. /* Ignore these if they are not on this bus */
  264. if (dev->parent != bus)
  265. continue;
  266. parent_data = dev_get_parent_priv(dev);
  267. ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
  268. ut_assertok(device_remove(dev));
  269. ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
  270. ut_asserteq_ptr(dms->removed, dev);
  271. }
  272. test_state = NULL;
  273. return 0;
  274. }
  275. DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  276. static int test_bus_parent_platdata(struct unit_test_state *uts)
  277. {
  278. struct dm_test_parent_platdata *plat;
  279. struct udevice *bus, *dev;
  280. int child_count;
  281. /* Check that the bus has no children */
  282. ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
  283. device_find_first_child(bus, &dev);
  284. ut_asserteq_ptr(NULL, dev);
  285. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  286. for (device_find_first_child(bus, &dev), child_count = 0;
  287. dev;
  288. device_find_next_child(&dev)) {
  289. /* Check that platform data is allocated */
  290. plat = dev_get_parent_platdata(dev);
  291. ut_assert(plat != NULL);
  292. /*
  293. * Check that it is not affected by the device being
  294. * probed/removed
  295. */
  296. plat->count++;
  297. ut_asserteq(1, plat->count);
  298. device_probe(dev);
  299. device_remove(dev);
  300. ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
  301. ut_asserteq(1, plat->count);
  302. ut_assertok(device_probe(dev));
  303. child_count++;
  304. }
  305. ut_asserteq(3, child_count);
  306. /* Removing the bus should also have no effect (it is still bound) */
  307. device_remove(bus);
  308. for (device_find_first_child(bus, &dev), child_count = 0;
  309. dev;
  310. device_find_next_child(&dev)) {
  311. /* Check that platform data is allocated */
  312. plat = dev_get_parent_platdata(dev);
  313. ut_assert(plat != NULL);
  314. ut_asserteq(1, plat->count);
  315. child_count++;
  316. }
  317. ut_asserteq(3, child_count);
  318. /* Unbind all the children */
  319. do {
  320. device_find_first_child(bus, &dev);
  321. if (dev)
  322. device_unbind(dev);
  323. } while (dev);
  324. /* Now the child platdata should be removed and re-added */
  325. device_probe(bus);
  326. for (device_find_first_child(bus, &dev), child_count = 0;
  327. dev;
  328. device_find_next_child(&dev)) {
  329. /* Check that platform data is allocated */
  330. plat = dev_get_parent_platdata(dev);
  331. ut_assert(plat != NULL);
  332. ut_asserteq(0, plat->count);
  333. child_count++;
  334. }
  335. ut_asserteq(3, child_count);
  336. return 0;
  337. }
  338. /* Test that the bus can store platform data about each child */
  339. static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
  340. {
  341. return test_bus_parent_platdata(uts);
  342. }
  343. DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  344. /* As above but the size is controlled by the uclass */
  345. static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
  346. {
  347. struct udevice *bus;
  348. struct driver *drv;
  349. int size;
  350. int ret;
  351. /* Set the driver size to 0 so that the uclass size is used */
  352. ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
  353. drv = (struct driver *)bus->driver;
  354. size = drv->per_child_platdata_auto_alloc_size;
  355. bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
  356. drv->per_child_platdata_auto_alloc_size = 0;
  357. ret = test_bus_parent_platdata(uts);
  358. if (ret)
  359. return ret;
  360. bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
  361. drv->per_child_platdata_auto_alloc_size = size;
  362. return 0;
  363. }
  364. DM_TEST(dm_test_bus_parent_platdata_uclass,
  365. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  366. /* Test that the child post_bind method is called */
  367. static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
  368. {
  369. struct dm_test_parent_platdata *plat;
  370. struct udevice *bus, *dev;
  371. int child_count;
  372. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  373. for (device_find_first_child(bus, &dev), child_count = 0;
  374. dev;
  375. device_find_next_child(&dev)) {
  376. /* Check that platform data is allocated */
  377. plat = dev_get_parent_platdata(dev);
  378. ut_assert(plat != NULL);
  379. ut_asserteq(1, plat->bind_flag);
  380. child_count++;
  381. }
  382. ut_asserteq(3, child_count);
  383. return 0;
  384. }
  385. DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  386. /* Test that the child post_bind method is called */
  387. static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
  388. {
  389. struct dm_test_parent_platdata *plat;
  390. struct udevice *bus, *dev;
  391. int child_count;
  392. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  393. for (device_find_first_child(bus, &dev), child_count = 0;
  394. dev;
  395. device_find_next_child(&dev)) {
  396. /* Check that platform data is allocated */
  397. plat = dev_get_parent_platdata(dev);
  398. ut_assert(plat != NULL);
  399. ut_asserteq(2, plat->uclass_bind_flag);
  400. child_count++;
  401. }
  402. ut_asserteq(3, child_count);
  403. return 0;
  404. }
  405. DM_TEST(dm_test_bus_child_post_bind_uclass,
  406. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  407. /*
  408. * Test that the bus' uclass' child_pre_probe() is called before the
  409. * device's probe() method
  410. */
  411. static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
  412. {
  413. struct udevice *bus, *dev;
  414. int child_count;
  415. /*
  416. * See testfdt_drv_probe() which effectively checks that the uclass
  417. * flag is set before that method is called
  418. */
  419. ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
  420. for (device_find_first_child(bus, &dev), child_count = 0;
  421. dev;
  422. device_find_next_child(&dev)) {
  423. struct dm_test_priv *priv = dev_get_priv(dev);
  424. /* Check that things happened in the right order */
  425. ut_asserteq_ptr(NULL, priv);
  426. ut_assertok(device_probe(dev));
  427. priv = dev_get_priv(dev);
  428. ut_assert(priv != NULL);
  429. ut_asserteq(1, priv->uclass_flag);
  430. ut_asserteq(1, priv->uclass_total);
  431. child_count++;
  432. }
  433. ut_asserteq(3, child_count);
  434. return 0;
  435. }
  436. DM_TEST(dm_test_bus_child_pre_probe_uclass,
  437. DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);