pci.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (C) 2013, Red Hat Inc, Michael S. Tsirkin <mst@redhat.com>
  3. *
  4. * This work is licensed under the terms of the GNU LGPL, version 2.
  5. */
  6. #include <linux/pci_regs.h>
  7. #include "pci.h"
  8. #include "asm/pci.h"
  9. void pci_cap_walk(struct pci_dev *dev, pci_cap_handler_t handler)
  10. {
  11. uint8_t cap_offset;
  12. uint8_t cap_id;
  13. int count = 0;
  14. cap_offset = pci_config_readb(dev->bdf, PCI_CAPABILITY_LIST);
  15. while (cap_offset) {
  16. cap_id = pci_config_readb(dev->bdf, cap_offset);
  17. assert(cap_id < PCI_CAP_ID_MAX + 1);
  18. handler(dev, cap_offset, cap_id);
  19. cap_offset = pci_config_readb(dev->bdf, cap_offset + 1);
  20. /* Avoid dead loop during cap walk */
  21. assert(++count <= 255);
  22. }
  23. }
  24. void pci_msi_set_enable(struct pci_dev *dev, bool enabled)
  25. {
  26. uint16_t msi_control;
  27. uint16_t offset;
  28. offset = dev->msi_offset;
  29. msi_control = pci_config_readw(dev->bdf, offset + PCI_MSI_FLAGS);
  30. if (enabled)
  31. msi_control |= PCI_MSI_FLAGS_ENABLE;
  32. else
  33. msi_control &= ~PCI_MSI_FLAGS_ENABLE;
  34. pci_config_writew(dev->bdf, offset + PCI_MSI_FLAGS, msi_control);
  35. }
  36. bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr, uint32_t msi_data)
  37. {
  38. uint16_t msi_control;
  39. uint16_t offset;
  40. pcidevaddr_t addr;
  41. assert(dev);
  42. if (!dev->msi_offset) {
  43. printf("MSI: dev %#x does not support MSI.\n", dev->bdf);
  44. return false;
  45. }
  46. addr = dev->bdf;
  47. offset = dev->msi_offset;
  48. msi_control = pci_config_readw(addr, offset + PCI_MSI_FLAGS);
  49. pci_config_writel(addr, offset + PCI_MSI_ADDRESS_LO,
  50. msi_addr & 0xffffffff);
  51. if (msi_control & PCI_MSI_FLAGS_64BIT) {
  52. pci_config_writel(addr, offset + PCI_MSI_ADDRESS_HI,
  53. (uint32_t)(msi_addr >> 32));
  54. pci_config_writel(addr, offset + PCI_MSI_DATA_64, msi_data);
  55. } else {
  56. pci_config_writel(addr, offset + PCI_MSI_DATA_32, msi_data);
  57. }
  58. pci_msi_set_enable(dev, true);
  59. return true;
  60. }
  61. void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr)
  62. {
  63. uint16_t val = pci_config_readw(dev->bdf, PCI_COMMAND);
  64. /* No overlap is allowed */
  65. assert((set & clr) == 0);
  66. val |= set;
  67. val &= ~clr;
  68. pci_config_writew(dev->bdf, PCI_COMMAND, val);
  69. }
  70. bool pci_dev_exists(pcidevaddr_t dev)
  71. {
  72. return (pci_config_readw(dev, PCI_VENDOR_ID) != 0xffff &&
  73. pci_config_readw(dev, PCI_DEVICE_ID) != 0xffff);
  74. }
  75. /* Scan bus look for a specific device. Only bus 0 scanned for now. */
  76. pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id)
  77. {
  78. pcidevaddr_t dev;
  79. for (dev = 0; dev < PCI_DEVFN_MAX; ++dev) {
  80. if (pci_config_readw(dev, PCI_VENDOR_ID) == vendor_id &&
  81. pci_config_readw(dev, PCI_DEVICE_ID) == device_id)
  82. return dev;
  83. }
  84. return PCIDEVADDR_INVALID;
  85. }
  86. uint32_t pci_bar_mask(uint32_t bar)
  87. {
  88. return (bar & PCI_BASE_ADDRESS_SPACE_IO) ?
  89. PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK;
  90. }
  91. uint32_t pci_bar_get(struct pci_dev *dev, int bar_num)
  92. {
  93. ASSERT_BAR_NUM(bar_num);
  94. return pci_config_readl(dev->bdf, PCI_BASE_ADDRESS_0 +
  95. bar_num * 4);
  96. }
  97. static phys_addr_t __pci_bar_get_addr(struct pci_dev *dev, int bar_num)
  98. {
  99. uint32_t bar = pci_bar_get(dev, bar_num);
  100. uint32_t mask = pci_bar_mask(bar);
  101. uint64_t addr = bar & mask;
  102. phys_addr_t phys_addr;
  103. if (pci_bar_is64(dev, bar_num))
  104. addr |= (uint64_t)pci_bar_get(dev, bar_num + 1) << 32;
  105. phys_addr = pci_translate_addr(dev->bdf, addr);
  106. assert(phys_addr != INVALID_PHYS_ADDR);
  107. return phys_addr;
  108. }
  109. phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num)
  110. {
  111. ASSERT_BAR_NUM(bar_num);
  112. return dev->resource[bar_num];
  113. }
  114. void pci_bar_set_addr(struct pci_dev *dev, int bar_num, phys_addr_t addr)
  115. {
  116. int off = PCI_BASE_ADDRESS_0 + bar_num * 4;
  117. assert(addr != INVALID_PHYS_ADDR);
  118. assert(dev->resource[bar_num] != INVALID_PHYS_ADDR);
  119. ASSERT_BAR_NUM(bar_num);
  120. if (pci_bar_is64(dev, bar_num))
  121. ASSERT_BAR_NUM(bar_num + 1);
  122. else
  123. assert((addr >> 32) == 0);
  124. pci_config_writel(dev->bdf, off, (uint32_t)addr);
  125. dev->resource[bar_num] = addr;
  126. if (pci_bar_is64(dev, bar_num)) {
  127. pci_config_writel(dev->bdf, off + 4, (uint32_t)(addr >> 32));
  128. dev->resource[bar_num + 1] = dev->resource[bar_num];
  129. }
  130. }
  131. /*
  132. * To determine the amount of address space needed by a PCI device,
  133. * one must save the original value of the BAR, write a value of
  134. * all 1's to the register, and then read it back. The amount of
  135. * memory can be then determined by masking the information bits,
  136. * performing a bitwise NOT, and incrementing the value by 1.
  137. *
  138. * The following pci_bar_size_helper() and pci_bar_size() functions
  139. * implement the algorithm.
  140. */
  141. static uint32_t pci_bar_size_helper(struct pci_dev *dev, int bar_num)
  142. {
  143. int off = PCI_BASE_ADDRESS_0 + bar_num * 4;
  144. uint16_t bdf = dev->bdf;
  145. uint32_t bar, val;
  146. bar = pci_config_readl(bdf, off);
  147. pci_config_writel(bdf, off, ~0u);
  148. val = pci_config_readl(bdf, off);
  149. pci_config_writel(bdf, off, bar);
  150. return val;
  151. }
  152. phys_addr_t pci_bar_size(struct pci_dev *dev, int bar_num)
  153. {
  154. uint32_t bar, size;
  155. size = pci_bar_size_helper(dev, bar_num);
  156. if (!size)
  157. return 0;
  158. bar = pci_bar_get(dev, bar_num);
  159. size &= pci_bar_mask(bar);
  160. if (pci_bar_is64(dev, bar_num)) {
  161. phys_addr_t size64 = pci_bar_size_helper(dev, bar_num + 1);
  162. size64 = (size64 << 32) | size;
  163. return ~size64 + 1;
  164. } else {
  165. return ~size + 1;
  166. }
  167. }
  168. bool pci_bar_is_memory(struct pci_dev *dev, int bar_num)
  169. {
  170. uint32_t bar = pci_bar_get(dev, bar_num);
  171. return !(bar & PCI_BASE_ADDRESS_SPACE_IO);
  172. }
  173. bool pci_bar_is_valid(struct pci_dev *dev, int bar_num)
  174. {
  175. return dev->resource[bar_num] != INVALID_PHYS_ADDR;
  176. }
  177. bool pci_bar_is64(struct pci_dev *dev, int bar_num)
  178. {
  179. uint32_t bar = pci_bar_get(dev, bar_num);
  180. if (bar & PCI_BASE_ADDRESS_SPACE_IO)
  181. return false;
  182. return (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  183. PCI_BASE_ADDRESS_MEM_TYPE_64;
  184. }
  185. void pci_bar_print(struct pci_dev *dev, int bar_num)
  186. {
  187. phys_addr_t size, start, end;
  188. uint32_t bar;
  189. if (!pci_bar_is_valid(dev, bar_num))
  190. return;
  191. bar = pci_bar_get(dev, bar_num);
  192. size = pci_bar_size(dev, bar_num);
  193. start = pci_bar_get_addr(dev, bar_num);
  194. end = start + size - 1;
  195. if (pci_bar_is64(dev, bar_num)) {
  196. printf("BAR#%d,%d [%" PRIx64 "-%" PRIx64 " ",
  197. bar_num, bar_num + 1, start, end);
  198. } else {
  199. printf("BAR#%d [%02x-%02x ",
  200. bar_num, (uint32_t)start, (uint32_t)end);
  201. }
  202. if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
  203. printf("PIO");
  204. } else {
  205. printf("MEM");
  206. switch (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
  207. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  208. printf("32");
  209. break;
  210. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  211. printf("1M");
  212. break;
  213. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  214. printf("64");
  215. break;
  216. default:
  217. assert(0);
  218. }
  219. }
  220. if (bar & PCI_BASE_ADDRESS_MEM_PREFETCH)
  221. printf("/p");
  222. printf("]");
  223. }
  224. void pci_dev_print_id(struct pci_dev *dev)
  225. {
  226. pcidevaddr_t bdf = dev->bdf;
  227. printf("00.%02x.%1x %04x:%04x", bdf / 8, bdf % 8,
  228. pci_config_readw(bdf, PCI_VENDOR_ID),
  229. pci_config_readw(bdf, PCI_DEVICE_ID));
  230. }
  231. static void pci_cap_print(struct pci_dev *dev, int cap_offset, int cap_id)
  232. {
  233. switch (cap_id) {
  234. case PCI_CAP_ID_MSI: {
  235. uint16_t control = pci_config_readw(dev->bdf, cap_offset + PCI_MSI_FLAGS);
  236. printf("\tMSI,%s-bit capability ", control & PCI_MSI_FLAGS_64BIT ? "64" : "32");
  237. break;
  238. }
  239. default:
  240. printf("\tcapability %#04x ", cap_id);
  241. break;
  242. }
  243. printf("at offset %#04x\n", cap_offset);
  244. }
  245. void pci_dev_print(struct pci_dev *dev)
  246. {
  247. pcidevaddr_t bdf = dev->bdf;
  248. uint8_t header = pci_config_readb(bdf, PCI_HEADER_TYPE);
  249. uint8_t progif = pci_config_readb(bdf, PCI_CLASS_PROG);
  250. uint8_t subclass = pci_config_readb(bdf, PCI_CLASS_DEVICE);
  251. uint8_t class = pci_config_readb(bdf, PCI_CLASS_DEVICE + 1);
  252. int i;
  253. pci_dev_print_id(dev);
  254. printf(" type %02x progif %02x class %02x subclass %02x\n",
  255. header, progif, class, subclass);
  256. pci_cap_walk(dev, pci_cap_print);
  257. if ((header & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_NORMAL)
  258. return;
  259. for (i = 0; i < PCI_BAR_NUM; i++) {
  260. if (pci_bar_is_valid(dev, i)) {
  261. printf("\t");
  262. pci_bar_print(dev, i);
  263. printf("\n");
  264. }
  265. if (pci_bar_is64(dev, i))
  266. i++;
  267. }
  268. }
  269. void pci_print(void)
  270. {
  271. pcidevaddr_t devfn;
  272. struct pci_dev pci_dev;
  273. for (devfn = 0; devfn < PCI_DEVFN_MAX; ++devfn) {
  274. if (pci_dev_exists(devfn)) {
  275. pci_dev_init(&pci_dev, devfn);
  276. pci_dev_print(&pci_dev);
  277. }
  278. }
  279. }
  280. void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf)
  281. {
  282. int i;
  283. memset(dev, 0, sizeof(*dev));
  284. dev->bdf = bdf;
  285. for (i = 0; i < PCI_BAR_NUM; i++) {
  286. if (pci_bar_size(dev, i)) {
  287. dev->resource[i] = __pci_bar_get_addr(dev, i);
  288. if (pci_bar_is64(dev, i)) {
  289. assert(i + 1 < PCI_BAR_NUM);
  290. dev->resource[i + 1] = dev->resource[i];
  291. i++;
  292. }
  293. } else {
  294. dev->resource[i] = INVALID_PHYS_ADDR;
  295. }
  296. }
  297. }
  298. uint8_t pci_intx_line(struct pci_dev *dev)
  299. {
  300. return pci_config_readb(dev->bdf, PCI_INTERRUPT_LINE);
  301. }
  302. static void pci_cap_setup(struct pci_dev *dev, int cap_offset, int cap_id)
  303. {
  304. switch (cap_id) {
  305. case PCI_CAP_ID_MSI:
  306. dev->msi_offset = cap_offset;
  307. break;
  308. }
  309. }
  310. void pci_enable_defaults(struct pci_dev *dev)
  311. {
  312. /* Enable device DMA operations */
  313. pci_cmd_set_clr(dev, PCI_COMMAND_MASTER, 0);
  314. pci_cap_walk(dev, pci_cap_setup);
  315. }