bus.c 15 KB

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