pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <asm/io.h>
  8. #include <asm/test.h>
  9. #include <dm/test.h>
  10. #include <test/test.h>
  11. #include <test/ut.h>
  12. /* Test that sandbox PCI works correctly */
  13. static int dm_test_pci_base(struct unit_test_state *uts)
  14. {
  15. struct udevice *bus;
  16. ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
  17. return 0;
  18. }
  19. DM_TEST(dm_test_pci_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  20. /* Test that sandbox PCI bus numbering and device works correctly */
  21. static int dm_test_pci_busdev(struct unit_test_state *uts)
  22. {
  23. struct udevice *bus;
  24. struct udevice *swap;
  25. u16 vendor, device;
  26. /* Test bus#0 and its devices */
  27. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
  28. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
  29. vendor = 0;
  30. ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
  31. ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
  32. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
  33. device = 0;
  34. ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
  35. ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
  36. /* Test bus#1 and its devices */
  37. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
  38. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
  39. vendor = 0;
  40. ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
  41. ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
  42. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
  43. device = 0;
  44. ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
  45. ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
  46. return 0;
  47. }
  48. DM_TEST(dm_test_pci_busdev, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  49. /* Test that we can use the swapcase device correctly */
  50. static int dm_test_pci_swapcase(struct unit_test_state *uts)
  51. {
  52. struct udevice *swap;
  53. ulong io_addr, mem_addr;
  54. char *ptr;
  55. /* Check that asking for the device 0 automatically fires up PCI */
  56. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
  57. /* First test I/O */
  58. io_addr = dm_pci_read_bar32(swap, 0);
  59. outb(2, io_addr);
  60. ut_asserteq(2, inb(io_addr));
  61. /*
  62. * Now test memory mapping - note we must unmap and remap to cause
  63. * the swapcase emulation to see our data and response.
  64. */
  65. mem_addr = dm_pci_read_bar32(swap, 1);
  66. ptr = map_sysmem(mem_addr, 20);
  67. strcpy(ptr, "This is a TesT");
  68. unmap_sysmem(ptr);
  69. ptr = map_sysmem(mem_addr, 20);
  70. ut_asserteq_str("tHIS IS A tESt", ptr);
  71. unmap_sysmem(ptr);
  72. /* Check that asking for the device 1 automatically fires up PCI */
  73. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
  74. /* First test I/O */
  75. io_addr = dm_pci_read_bar32(swap, 0);
  76. outb(2, io_addr);
  77. ut_asserteq(2, inb(io_addr));
  78. /*
  79. * Now test memory mapping - note we must unmap and remap to cause
  80. * the swapcase emulation to see our data and response.
  81. */
  82. mem_addr = dm_pci_read_bar32(swap, 1);
  83. ptr = map_sysmem(mem_addr, 20);
  84. strcpy(ptr, "This is a TesT");
  85. unmap_sysmem(ptr);
  86. ptr = map_sysmem(mem_addr, 20);
  87. ut_asserteq_str("tHIS IS A tESt", ptr);
  88. unmap_sysmem(ptr);
  89. return 0;
  90. }
  91. DM_TEST(dm_test_pci_swapcase, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  92. /* Test that we can dynamically bind the device driver correctly */
  93. static int dm_test_pci_drvdata(struct unit_test_state *uts)
  94. {
  95. struct udevice *bus, *swap;
  96. /* Check that asking for the device automatically fires up PCI */
  97. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
  98. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
  99. ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
  100. ut_assertok(dev_of_valid(swap));
  101. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
  102. ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
  103. ut_assertok(dev_of_valid(swap));
  104. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x10, 0), &swap));
  105. ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
  106. ut_assertok(!dev_of_valid(swap));
  107. return 0;
  108. }
  109. DM_TEST(dm_test_pci_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  110. /* Test that devices on PCI bus#2 can be accessed correctly */
  111. static int dm_test_pci_mixed(struct unit_test_state *uts)
  112. {
  113. /* PCI bus#2 has both statically and dynamic declared devices */
  114. struct udevice *bus, *swap;
  115. u16 vendor, device;
  116. ulong io_addr, mem_addr;
  117. char *ptr;
  118. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus));
  119. /* Test the dynamic device */
  120. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap));
  121. vendor = 0;
  122. ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
  123. ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
  124. /* First test I/O */
  125. io_addr = dm_pci_read_bar32(swap, 0);
  126. outb(2, io_addr);
  127. ut_asserteq(2, inb(io_addr));
  128. /*
  129. * Now test memory mapping - note we must unmap and remap to cause
  130. * the swapcase emulation to see our data and response.
  131. */
  132. mem_addr = dm_pci_read_bar32(swap, 1);
  133. ptr = map_sysmem(mem_addr, 30);
  134. strcpy(ptr, "This is a TesT oN dYNAMIc");
  135. unmap_sysmem(ptr);
  136. ptr = map_sysmem(mem_addr, 30);
  137. ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr);
  138. unmap_sysmem(ptr);
  139. /* Test the static device */
  140. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap));
  141. device = 0;
  142. ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
  143. ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
  144. /* First test I/O */
  145. io_addr = dm_pci_read_bar32(swap, 0);
  146. outb(2, io_addr);
  147. ut_asserteq(2, inb(io_addr));
  148. /*
  149. * Now test memory mapping - note we must unmap and remap to cause
  150. * the swapcase emulation to see our data and response.
  151. */
  152. mem_addr = dm_pci_read_bar32(swap, 1);
  153. ptr = map_sysmem(mem_addr, 30);
  154. strcpy(ptr, "This is a TesT oN sTATIc");
  155. unmap_sysmem(ptr);
  156. ptr = map_sysmem(mem_addr, 30);
  157. ut_asserteq_str("tHIS IS A tESt On StatiC", ptr);
  158. unmap_sysmem(ptr);
  159. return 0;
  160. }
  161. DM_TEST(dm_test_pci_mixed, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  162. /* Test looking up PCI capability and extended capability */
  163. static int dm_test_pci_cap(struct unit_test_state *uts)
  164. {
  165. struct udevice *bus, *swap;
  166. int cap;
  167. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
  168. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
  169. /* look up PCI_CAP_ID_EXP */
  170. cap = dm_pci_find_capability(swap, PCI_CAP_ID_EXP);
  171. ut_asserteq(PCI_CAP_ID_EXP_OFFSET, cap);
  172. /* look up PCI_CAP_ID_PCIX */
  173. cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
  174. ut_asserteq(0, cap);
  175. /* look up PCI_CAP_ID_MSIX starting from PCI_CAP_ID_PM_OFFSET */
  176. cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_PM_OFFSET,
  177. PCI_CAP_ID_MSIX);
  178. ut_asserteq(PCI_CAP_ID_MSIX_OFFSET, cap);
  179. /* look up PCI_CAP_ID_VNDR starting from PCI_CAP_ID_EXP_OFFSET */
  180. cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_EXP_OFFSET,
  181. PCI_CAP_ID_VNDR);
  182. ut_asserteq(0, cap);
  183. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
  184. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
  185. /* look up PCI_EXT_CAP_ID_DSN */
  186. cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_DSN);
  187. ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
  188. /* look up PCI_EXT_CAP_ID_SRIOV */
  189. cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
  190. ut_asserteq(0, cap);
  191. /* look up PCI_EXT_CAP_ID_DSN starting from PCI_EXT_CAP_ID_ERR_OFFSET */
  192. cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_ERR_OFFSET,
  193. PCI_EXT_CAP_ID_DSN);
  194. ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
  195. /* look up PCI_EXT_CAP_ID_RCRB starting from PCI_EXT_CAP_ID_VC_OFFSET */
  196. cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_VC_OFFSET,
  197. PCI_EXT_CAP_ID_RCRB);
  198. ut_asserteq(0, cap);
  199. return 0;
  200. }
  201. DM_TEST(dm_test_pci_cap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  202. /* Test looking up BARs in EA capability structure */
  203. static int dm_test_pci_ea(struct unit_test_state *uts)
  204. {
  205. struct udevice *bus, *swap;
  206. void *bar;
  207. int cap;
  208. /*
  209. * use emulated device mapping function, we're not using real physical
  210. * addresses in this test
  211. */
  212. sandbox_set_enable_pci_map(true);
  213. ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
  214. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x01, 0), &swap));
  215. /* look up PCI_CAP_ID_EA */
  216. cap = dm_pci_find_capability(swap, PCI_CAP_ID_EA);
  217. ut_asserteq(PCI_CAP_ID_EA_OFFSET, cap);
  218. /* test swap case in BAR 1 */
  219. bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_0, 0);
  220. ut_assertnonnull(bar);
  221. *(int *)bar = 2; /* swap upper/lower */
  222. bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0);
  223. ut_assertnonnull(bar);
  224. strcpy(bar, "ea TEST");
  225. unmap_sysmem(bar);
  226. bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0);
  227. ut_assertnonnull(bar);
  228. ut_asserteq_str("EA test", bar);
  229. /* test magic values in BARs2, 4; BAR 3 is n/a */
  230. bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_2, 0);
  231. ut_assertnonnull(bar);
  232. ut_asserteq(PCI_EA_BAR2_MAGIC, *(u32 *)bar);
  233. bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_3, 0);
  234. ut_assertnull(bar);
  235. bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_4, 0);
  236. ut_assertnonnull(bar);
  237. ut_asserteq(PCI_EA_BAR4_MAGIC, *(u32 *)bar);
  238. return 0;
  239. }
  240. DM_TEST(dm_test_pci_ea, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  241. /* Test the dev_read_addr_pci() function */
  242. static int dm_test_pci_addr_flat(struct unit_test_state *uts)
  243. {
  244. struct udevice *swap1f, *swap1;
  245. ulong io_addr, mem_addr;
  246. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
  247. io_addr = dm_pci_read_bar32(swap1f, 0);
  248. ut_asserteq(io_addr, dev_read_addr_pci(swap1f));
  249. /*
  250. * This device has both I/O and MEM spaces but the MEM space appears
  251. * first
  252. */
  253. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
  254. mem_addr = dm_pci_read_bar32(swap1, 1);
  255. ut_asserteq(mem_addr, dev_read_addr_pci(swap1));
  256. return 0;
  257. }
  258. DM_TEST(dm_test_pci_addr_flat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT |
  259. UT_TESTF_FLAT_TREE);
  260. /*
  261. * Test the dev_read_addr_pci() function with livetree. That function is
  262. * not currently fully implemented, in that it fails to return the BAR address.
  263. * Once that is implemented this test can be removed and dm_test_pci_addr_flat()
  264. * can be used for both flattree and livetree by removing the UT_TESTF_FLAT_TREE
  265. * flag above.
  266. */
  267. static int dm_test_pci_addr_live(struct unit_test_state *uts)
  268. {
  269. struct udevice *swap1f, *swap1;
  270. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
  271. ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1f));
  272. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
  273. ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1));
  274. return 0;
  275. }
  276. DM_TEST(dm_test_pci_addr_live, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT |
  277. UT_TESTF_LIVE_TREE);
  278. /* Test device_is_on_pci_bus() */
  279. static int dm_test_pci_on_bus(struct unit_test_state *uts)
  280. {
  281. struct udevice *dev;
  282. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &dev));
  283. ut_asserteq(true, device_is_on_pci_bus(dev));
  284. ut_asserteq(false, device_is_on_pci_bus(dev_get_parent(dev)));
  285. ut_asserteq(true, device_is_on_pci_bus(dev));
  286. return 0;
  287. }
  288. DM_TEST(dm_test_pci_on_bus, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  289. /*
  290. * Test support for multiple memory regions enabled via
  291. * CONFIG_PCI_REGION_MULTI_ENTRY. When this feature is not enabled,
  292. * only the last region of one type is stored. In this test-case,
  293. * we have 2 memory regions, the first at 0x3000.0000 and the 2nd
  294. * at 0x3100.0000. A correct test results now in BAR1 located at
  295. * 0x3000.0000.
  296. */
  297. static int dm_test_pci_region_multi(struct unit_test_state *uts)
  298. {
  299. struct udevice *dev;
  300. ulong mem_addr;
  301. /* Test memory BAR1 on bus#1 */
  302. ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
  303. mem_addr = dm_pci_read_bar32(dev, 1);
  304. ut_asserteq(mem_addr, 0x30000000);
  305. return 0;
  306. }
  307. DM_TEST(dm_test_pci_region_multi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);