2
0

xilinx-pcie.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Xilinx PCIe host controller emulation.
  3. *
  4. * Copyright (c) 2016 Imagination Technologies
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/module.h"
  21. #include "qemu/units.h"
  22. #include "qapi/error.h"
  23. #include "hw/pci/pci_bridge.h"
  24. #include "hw/qdev-properties.h"
  25. #include "hw/irq.h"
  26. #include "hw/pci-host/xilinx-pcie.h"
  27. enum root_cfg_reg {
  28. /* Interrupt Decode Register */
  29. ROOTCFG_INTDEC = 0x138,
  30. /* Interrupt Mask Register */
  31. ROOTCFG_INTMASK = 0x13c,
  32. /* INTx Interrupt Received */
  33. #define ROOTCFG_INTMASK_INTX (1 << 16)
  34. /* MSI Interrupt Received */
  35. #define ROOTCFG_INTMASK_MSI (1 << 17)
  36. /* PHY Status/Control Register */
  37. ROOTCFG_PSCR = 0x144,
  38. /* Link Up */
  39. #define ROOTCFG_PSCR_LINK_UP (1 << 11)
  40. /* Root Port Status/Control Register */
  41. ROOTCFG_RPSCR = 0x148,
  42. /* Bridge Enable */
  43. #define ROOTCFG_RPSCR_BRIDGEEN (1 << 0)
  44. /* Interrupt FIFO Not Empty */
  45. #define ROOTCFG_RPSCR_INTNEMPTY (1 << 18)
  46. /* Interrupt FIFO Overflow */
  47. #define ROOTCFG_RPSCR_INTOVF (1 << 19)
  48. /* Root Port Interrupt FIFO Read Register 1 */
  49. ROOTCFG_RPIFR1 = 0x158,
  50. #define ROOTCFG_RPIFR1_INT_LANE_SHIFT 27
  51. #define ROOTCFG_RPIFR1_INT_ASSERT_SHIFT 29
  52. #define ROOTCFG_RPIFR1_INT_VALID_SHIFT 31
  53. /* Root Port Interrupt FIFO Read Register 2 */
  54. ROOTCFG_RPIFR2 = 0x15c,
  55. };
  56. static void xilinx_pcie_update_intr(XilinxPCIEHost *s,
  57. uint32_t set, uint32_t clear)
  58. {
  59. int level;
  60. s->intr |= set;
  61. s->intr &= ~clear;
  62. if (s->intr_fifo_r != s->intr_fifo_w) {
  63. s->intr |= ROOTCFG_INTMASK_INTX;
  64. }
  65. level = !!(s->intr & s->intr_mask);
  66. qemu_set_irq(s->irq, level);
  67. }
  68. static void xilinx_pcie_queue_intr(XilinxPCIEHost *s,
  69. uint32_t fifo_reg1, uint32_t fifo_reg2)
  70. {
  71. XilinxPCIEInt *intr;
  72. unsigned int new_w;
  73. new_w = (s->intr_fifo_w + 1) % ARRAY_SIZE(s->intr_fifo);
  74. if (new_w == s->intr_fifo_r) {
  75. s->rpscr |= ROOTCFG_RPSCR_INTOVF;
  76. return;
  77. }
  78. intr = &s->intr_fifo[s->intr_fifo_w];
  79. s->intr_fifo_w = new_w;
  80. intr->fifo_reg1 = fifo_reg1;
  81. intr->fifo_reg2 = fifo_reg2;
  82. xilinx_pcie_update_intr(s, ROOTCFG_INTMASK_INTX, 0);
  83. }
  84. static void xilinx_pcie_set_irq(void *opaque, int irq_num, int level)
  85. {
  86. XilinxPCIEHost *s = XILINX_PCIE_HOST(opaque);
  87. xilinx_pcie_queue_intr(s,
  88. (irq_num << ROOTCFG_RPIFR1_INT_LANE_SHIFT) |
  89. (level << ROOTCFG_RPIFR1_INT_ASSERT_SHIFT) |
  90. (1 << ROOTCFG_RPIFR1_INT_VALID_SHIFT),
  91. 0);
  92. }
  93. static void xilinx_pcie_host_realize(DeviceState *dev, Error **errp)
  94. {
  95. PCIHostState *pci = PCI_HOST_BRIDGE(dev);
  96. XilinxPCIEHost *s = XILINX_PCIE_HOST(dev);
  97. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  98. PCIExpressHost *pex = PCIE_HOST_BRIDGE(dev);
  99. snprintf(s->name, sizeof(s->name), "pcie%u", s->bus_nr);
  100. /* PCI configuration space */
  101. pcie_host_mmcfg_init(pex, s->cfg_size);
  102. /* MMIO region */
  103. memory_region_init(&s->mmio, OBJECT(s), "mmio", UINT64_MAX);
  104. memory_region_set_enabled(&s->mmio, false);
  105. /* dummy PCI I/O region (not visible to the CPU) */
  106. memory_region_init(&s->io, OBJECT(s), "io", 16);
  107. /* interrupt out */
  108. qdev_init_gpio_out_named(dev, &s->irq, "interrupt_out", 1);
  109. sysbus_init_mmio(sbd, &pex->mmio);
  110. sysbus_init_mmio(sbd, &s->mmio);
  111. pci->bus = pci_register_root_bus(dev, s->name, xilinx_pcie_set_irq,
  112. pci_swizzle_map_irq_fn, s, &s->mmio,
  113. &s->io, 0, 4, TYPE_PCIE_BUS);
  114. qdev_set_parent_bus(DEVICE(&s->root), BUS(pci->bus));
  115. qdev_init_nofail(DEVICE(&s->root));
  116. }
  117. static const char *xilinx_pcie_host_root_bus_path(PCIHostState *host_bridge,
  118. PCIBus *rootbus)
  119. {
  120. return "0000:00";
  121. }
  122. static void xilinx_pcie_host_init(Object *obj)
  123. {
  124. XilinxPCIEHost *s = XILINX_PCIE_HOST(obj);
  125. XilinxPCIERoot *root = &s->root;
  126. object_initialize_child(obj, "root", root, sizeof(*root),
  127. TYPE_XILINX_PCIE_ROOT, &error_abort, NULL);
  128. qdev_prop_set_int32(DEVICE(root), "addr", PCI_DEVFN(0, 0));
  129. qdev_prop_set_bit(DEVICE(root), "multifunction", false);
  130. }
  131. static Property xilinx_pcie_host_props[] = {
  132. DEFINE_PROP_UINT32("bus_nr", XilinxPCIEHost, bus_nr, 0),
  133. DEFINE_PROP_SIZE("cfg_base", XilinxPCIEHost, cfg_base, 0),
  134. DEFINE_PROP_SIZE("cfg_size", XilinxPCIEHost, cfg_size, 32 * MiB),
  135. DEFINE_PROP_SIZE("mmio_base", XilinxPCIEHost, mmio_base, 0),
  136. DEFINE_PROP_SIZE("mmio_size", XilinxPCIEHost, mmio_size, 1 * MiB),
  137. DEFINE_PROP_BOOL("link_up", XilinxPCIEHost, link_up, true),
  138. DEFINE_PROP_END_OF_LIST(),
  139. };
  140. static void xilinx_pcie_host_class_init(ObjectClass *klass, void *data)
  141. {
  142. DeviceClass *dc = DEVICE_CLASS(klass);
  143. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  144. hc->root_bus_path = xilinx_pcie_host_root_bus_path;
  145. dc->realize = xilinx_pcie_host_realize;
  146. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  147. dc->fw_name = "pci";
  148. dc->props = xilinx_pcie_host_props;
  149. }
  150. static const TypeInfo xilinx_pcie_host_info = {
  151. .name = TYPE_XILINX_PCIE_HOST,
  152. .parent = TYPE_PCIE_HOST_BRIDGE,
  153. .instance_size = sizeof(XilinxPCIEHost),
  154. .instance_init = xilinx_pcie_host_init,
  155. .class_init = xilinx_pcie_host_class_init,
  156. };
  157. static uint32_t xilinx_pcie_root_config_read(PCIDevice *d,
  158. uint32_t address, int len)
  159. {
  160. XilinxPCIEHost *s = XILINX_PCIE_HOST(OBJECT(d)->parent);
  161. uint32_t val;
  162. switch (address) {
  163. case ROOTCFG_INTDEC:
  164. val = s->intr;
  165. break;
  166. case ROOTCFG_INTMASK:
  167. val = s->intr_mask;
  168. break;
  169. case ROOTCFG_PSCR:
  170. val = s->link_up ? ROOTCFG_PSCR_LINK_UP : 0;
  171. break;
  172. case ROOTCFG_RPSCR:
  173. if (s->intr_fifo_r != s->intr_fifo_w) {
  174. s->rpscr &= ~ROOTCFG_RPSCR_INTNEMPTY;
  175. } else {
  176. s->rpscr |= ROOTCFG_RPSCR_INTNEMPTY;
  177. }
  178. val = s->rpscr;
  179. break;
  180. case ROOTCFG_RPIFR1:
  181. if (s->intr_fifo_w == s->intr_fifo_r) {
  182. /* FIFO empty */
  183. val = 0;
  184. } else {
  185. val = s->intr_fifo[s->intr_fifo_r].fifo_reg1;
  186. }
  187. break;
  188. case ROOTCFG_RPIFR2:
  189. if (s->intr_fifo_w == s->intr_fifo_r) {
  190. /* FIFO empty */
  191. val = 0;
  192. } else {
  193. val = s->intr_fifo[s->intr_fifo_r].fifo_reg2;
  194. }
  195. break;
  196. default:
  197. val = pci_default_read_config(d, address, len);
  198. break;
  199. }
  200. return val;
  201. }
  202. static void xilinx_pcie_root_config_write(PCIDevice *d, uint32_t address,
  203. uint32_t val, int len)
  204. {
  205. XilinxPCIEHost *s = XILINX_PCIE_HOST(OBJECT(d)->parent);
  206. switch (address) {
  207. case ROOTCFG_INTDEC:
  208. xilinx_pcie_update_intr(s, 0, val);
  209. break;
  210. case ROOTCFG_INTMASK:
  211. s->intr_mask = val;
  212. xilinx_pcie_update_intr(s, 0, 0);
  213. break;
  214. case ROOTCFG_RPSCR:
  215. s->rpscr &= ~ROOTCFG_RPSCR_BRIDGEEN;
  216. s->rpscr |= val & ROOTCFG_RPSCR_BRIDGEEN;
  217. memory_region_set_enabled(&s->mmio, val & ROOTCFG_RPSCR_BRIDGEEN);
  218. if (val & ROOTCFG_INTMASK_INTX) {
  219. s->rpscr &= ~ROOTCFG_INTMASK_INTX;
  220. }
  221. break;
  222. case ROOTCFG_RPIFR1:
  223. case ROOTCFG_RPIFR2:
  224. if (s->intr_fifo_w == s->intr_fifo_r) {
  225. /* FIFO empty */
  226. return;
  227. } else {
  228. s->intr_fifo_r = (s->intr_fifo_r + 1) % ARRAY_SIZE(s->intr_fifo);
  229. }
  230. break;
  231. default:
  232. pci_default_write_config(d, address, val, len);
  233. break;
  234. }
  235. }
  236. static void xilinx_pcie_root_realize(PCIDevice *pci_dev, Error **errp)
  237. {
  238. BusState *bus = qdev_get_parent_bus(DEVICE(pci_dev));
  239. XilinxPCIEHost *s = XILINX_PCIE_HOST(bus->parent);
  240. pci_set_word(pci_dev->config + PCI_COMMAND,
  241. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  242. pci_set_word(pci_dev->config + PCI_MEMORY_BASE, s->mmio_base >> 16);
  243. pci_set_word(pci_dev->config + PCI_MEMORY_LIMIT,
  244. ((s->mmio_base + s->mmio_size - 1) >> 16) & 0xfff0);
  245. pci_bridge_initfn(pci_dev, TYPE_PCI_BUS);
  246. if (pcie_endpoint_cap_v1_init(pci_dev, 0x80) < 0) {
  247. error_setg(errp, "Failed to initialize PCIe capability");
  248. }
  249. }
  250. static void xilinx_pcie_root_class_init(ObjectClass *klass, void *data)
  251. {
  252. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  253. DeviceClass *dc = DEVICE_CLASS(klass);
  254. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  255. dc->desc = "Xilinx AXI-PCIe Host Bridge";
  256. k->vendor_id = PCI_VENDOR_ID_XILINX;
  257. k->device_id = 0x7021;
  258. k->revision = 0;
  259. k->class_id = PCI_CLASS_BRIDGE_HOST;
  260. k->is_bridge = true;
  261. k->realize = xilinx_pcie_root_realize;
  262. k->exit = pci_bridge_exitfn;
  263. dc->reset = pci_bridge_reset;
  264. k->config_read = xilinx_pcie_root_config_read;
  265. k->config_write = xilinx_pcie_root_config_write;
  266. /*
  267. * PCI-facing part of the host bridge, not usable without the
  268. * host-facing part, which can't be device_add'ed, yet.
  269. */
  270. dc->user_creatable = false;
  271. }
  272. static const TypeInfo xilinx_pcie_root_info = {
  273. .name = TYPE_XILINX_PCIE_ROOT,
  274. .parent = TYPE_PCI_BRIDGE,
  275. .instance_size = sizeof(XilinxPCIERoot),
  276. .class_init = xilinx_pcie_root_class_init,
  277. .interfaces = (InterfaceInfo[]) {
  278. { INTERFACE_PCIE_DEVICE },
  279. { }
  280. },
  281. };
  282. static void xilinx_pcie_register(void)
  283. {
  284. type_register_static(&xilinx_pcie_root_info);
  285. type_register_static(&xilinx_pcie_host_info);
  286. }
  287. type_init(xilinx_pcie_register)