xilinx-pcie.c 10 KB

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