xilinx-pcie.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.1 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_realize(DEVICE(&s->root), BUS(pci->bus), &error_fatal);
  115. }
  116. static const char *xilinx_pcie_host_root_bus_path(PCIHostState *host_bridge,
  117. PCIBus *rootbus)
  118. {
  119. return "0000:00";
  120. }
  121. static void xilinx_pcie_host_init(Object *obj)
  122. {
  123. XilinxPCIEHost *s = XILINX_PCIE_HOST(obj);
  124. XilinxPCIERoot *root = &s->root;
  125. object_initialize_child(obj, "root", root, TYPE_XILINX_PCIE_ROOT);
  126. qdev_prop_set_int32(DEVICE(root), "addr", PCI_DEVFN(0, 0));
  127. qdev_prop_set_bit(DEVICE(root), "multifunction", false);
  128. }
  129. static const Property xilinx_pcie_host_props[] = {
  130. DEFINE_PROP_UINT32("bus_nr", XilinxPCIEHost, bus_nr, 0),
  131. DEFINE_PROP_SIZE("cfg_base", XilinxPCIEHost, cfg_base, 0),
  132. DEFINE_PROP_SIZE("cfg_size", XilinxPCIEHost, cfg_size, 32 * MiB),
  133. DEFINE_PROP_SIZE("mmio_base", XilinxPCIEHost, mmio_base, 0),
  134. DEFINE_PROP_SIZE("mmio_size", XilinxPCIEHost, mmio_size, 1 * MiB),
  135. DEFINE_PROP_BOOL("link_up", XilinxPCIEHost, link_up, true),
  136. };
  137. static void xilinx_pcie_host_class_init(ObjectClass *klass, void *data)
  138. {
  139. DeviceClass *dc = DEVICE_CLASS(klass);
  140. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  141. hc->root_bus_path = xilinx_pcie_host_root_bus_path;
  142. dc->realize = xilinx_pcie_host_realize;
  143. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  144. dc->fw_name = "pci";
  145. device_class_set_props(dc, xilinx_pcie_host_props);
  146. }
  147. static const TypeInfo xilinx_pcie_host_info = {
  148. .name = TYPE_XILINX_PCIE_HOST,
  149. .parent = TYPE_PCIE_HOST_BRIDGE,
  150. .instance_size = sizeof(XilinxPCIEHost),
  151. .instance_init = xilinx_pcie_host_init,
  152. .class_init = xilinx_pcie_host_class_init,
  153. };
  154. static uint32_t xilinx_pcie_root_config_read(PCIDevice *d,
  155. uint32_t address, int len)
  156. {
  157. XilinxPCIEHost *s = XILINX_PCIE_HOST(OBJECT(d)->parent);
  158. uint32_t val;
  159. switch (address) {
  160. case ROOTCFG_INTDEC:
  161. val = s->intr;
  162. break;
  163. case ROOTCFG_INTMASK:
  164. val = s->intr_mask;
  165. break;
  166. case ROOTCFG_PSCR:
  167. val = s->link_up ? ROOTCFG_PSCR_LINK_UP : 0;
  168. break;
  169. case ROOTCFG_RPSCR:
  170. if (s->intr_fifo_r != s->intr_fifo_w) {
  171. s->rpscr &= ~ROOTCFG_RPSCR_INTNEMPTY;
  172. } else {
  173. s->rpscr |= ROOTCFG_RPSCR_INTNEMPTY;
  174. }
  175. val = s->rpscr;
  176. break;
  177. case ROOTCFG_RPIFR1:
  178. if (s->intr_fifo_w == s->intr_fifo_r) {
  179. /* FIFO empty */
  180. val = 0;
  181. } else {
  182. val = s->intr_fifo[s->intr_fifo_r].fifo_reg1;
  183. }
  184. break;
  185. case ROOTCFG_RPIFR2:
  186. if (s->intr_fifo_w == s->intr_fifo_r) {
  187. /* FIFO empty */
  188. val = 0;
  189. } else {
  190. val = s->intr_fifo[s->intr_fifo_r].fifo_reg2;
  191. }
  192. break;
  193. default:
  194. val = pci_default_read_config(d, address, len);
  195. break;
  196. }
  197. return val;
  198. }
  199. static void xilinx_pcie_root_config_write(PCIDevice *d, uint32_t address,
  200. uint32_t val, int len)
  201. {
  202. XilinxPCIEHost *s = XILINX_PCIE_HOST(OBJECT(d)->parent);
  203. switch (address) {
  204. case ROOTCFG_INTDEC:
  205. xilinx_pcie_update_intr(s, 0, val);
  206. break;
  207. case ROOTCFG_INTMASK:
  208. s->intr_mask = val;
  209. xilinx_pcie_update_intr(s, 0, 0);
  210. break;
  211. case ROOTCFG_RPSCR:
  212. s->rpscr &= ~ROOTCFG_RPSCR_BRIDGEEN;
  213. s->rpscr |= val & ROOTCFG_RPSCR_BRIDGEEN;
  214. memory_region_set_enabled(&s->mmio, val & ROOTCFG_RPSCR_BRIDGEEN);
  215. if (val & ROOTCFG_INTMASK_INTX) {
  216. s->rpscr &= ~ROOTCFG_INTMASK_INTX;
  217. }
  218. break;
  219. case ROOTCFG_RPIFR1:
  220. case ROOTCFG_RPIFR2:
  221. if (s->intr_fifo_w == s->intr_fifo_r) {
  222. /* FIFO empty */
  223. return;
  224. } else {
  225. s->intr_fifo_r = (s->intr_fifo_r + 1) % ARRAY_SIZE(s->intr_fifo);
  226. }
  227. break;
  228. default:
  229. pci_default_write_config(d, address, val, len);
  230. break;
  231. }
  232. }
  233. static void xilinx_pcie_root_realize(PCIDevice *pci_dev, Error **errp)
  234. {
  235. BusState *bus = qdev_get_parent_bus(DEVICE(pci_dev));
  236. XilinxPCIEHost *s = XILINX_PCIE_HOST(bus->parent);
  237. pci_set_word(pci_dev->config + PCI_COMMAND,
  238. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  239. pci_set_word(pci_dev->config + PCI_MEMORY_BASE, s->mmio_base >> 16);
  240. pci_set_word(pci_dev->config + PCI_MEMORY_LIMIT,
  241. ((s->mmio_base + s->mmio_size - 1) >> 16) & 0xfff0);
  242. pci_bridge_initfn(pci_dev, TYPE_PCI_BUS);
  243. if (pcie_endpoint_cap_v1_init(pci_dev, 0x80) < 0) {
  244. error_setg(errp, "Failed to initialize PCIe capability");
  245. }
  246. }
  247. static void xilinx_pcie_root_class_init(ObjectClass *klass, void *data)
  248. {
  249. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  250. DeviceClass *dc = DEVICE_CLASS(klass);
  251. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  252. dc->desc = "Xilinx AXI-PCIe Host Bridge";
  253. k->vendor_id = PCI_VENDOR_ID_XILINX;
  254. k->device_id = 0x7021;
  255. k->revision = 0;
  256. k->class_id = PCI_CLASS_BRIDGE_HOST;
  257. k->realize = xilinx_pcie_root_realize;
  258. k->exit = pci_bridge_exitfn;
  259. device_class_set_legacy_reset(dc, pci_bridge_reset);
  260. k->config_read = xilinx_pcie_root_config_read;
  261. k->config_write = xilinx_pcie_root_config_write;
  262. /*
  263. * PCI-facing part of the host bridge, not usable without the
  264. * host-facing part, which can't be device_add'ed, yet.
  265. */
  266. dc->user_creatable = false;
  267. }
  268. static const TypeInfo xilinx_pcie_root_info = {
  269. .name = TYPE_XILINX_PCIE_ROOT,
  270. .parent = TYPE_PCI_BRIDGE,
  271. .instance_size = sizeof(XilinxPCIERoot),
  272. .class_init = xilinx_pcie_root_class_init,
  273. .interfaces = (InterfaceInfo[]) {
  274. { INTERFACE_PCIE_DEVICE },
  275. { }
  276. },
  277. };
  278. static void xilinx_pcie_register(void)
  279. {
  280. type_register_static(&xilinx_pcie_root_info);
  281. type_register_static(&xilinx_pcie_host_info);
  282. }
  283. type_init(xilinx_pcie_register)