2
0

gpex.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * QEMU Generic PCI Express Bridge Emulation
  3. *
  4. * Copyright (C) 2015 Alexander Graf <agraf@suse.de>
  5. *
  6. * Code loosely based on q35.c.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. * Check out these documents for more information on the device:
  27. *
  28. * http://www.kernel.org/doc/Documentation/devicetree/bindings/pci/host-generic-pci.txt
  29. * http://www.firmware.org/1275/practice/imap/imap0_9d.pdf
  30. */
  31. #include "qemu/osdep.h"
  32. #include "qapi/error.h"
  33. #include "hw/irq.h"
  34. #include "hw/pci/pci_bus.h"
  35. #include "hw/pci-host/gpex.h"
  36. #include "hw/qdev-properties.h"
  37. #include "migration/vmstate.h"
  38. #include "qemu/module.h"
  39. /****************************************************************************
  40. * GPEX host
  41. */
  42. struct GPEXIrq {
  43. qemu_irq irq;
  44. int irq_num;
  45. };
  46. static void gpex_set_irq(void *opaque, int irq_num, int level)
  47. {
  48. GPEXHost *s = opaque;
  49. qemu_set_irq(s->irq[irq_num].irq, level);
  50. }
  51. int gpex_set_irq_num(GPEXHost *s, int index, int gsi)
  52. {
  53. if (index >= s->num_irqs) {
  54. return -EINVAL;
  55. }
  56. s->irq[index].irq_num = gsi;
  57. return 0;
  58. }
  59. static PCIINTxRoute gpex_route_intx_pin_to_irq(void *opaque, int pin)
  60. {
  61. PCIINTxRoute route;
  62. GPEXHost *s = opaque;
  63. int gsi = s->irq[pin].irq_num;
  64. route.irq = gsi;
  65. if (gsi < 0) {
  66. route.mode = PCI_INTX_DISABLED;
  67. } else {
  68. route.mode = PCI_INTX_ENABLED;
  69. }
  70. return route;
  71. }
  72. static int gpex_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
  73. {
  74. PCIBus *bus = pci_device_root_bus(pci_dev);
  75. return (PCI_SLOT(pci_dev->devfn) + pin) % bus->nirq;
  76. }
  77. static void gpex_host_realize(DeviceState *dev, Error **errp)
  78. {
  79. PCIHostState *pci = PCI_HOST_BRIDGE(dev);
  80. GPEXHost *s = GPEX_HOST(dev);
  81. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  82. PCIExpressHost *pex = PCIE_HOST_BRIDGE(dev);
  83. int i;
  84. s->irq = g_malloc0_n(s->num_irqs, sizeof(*s->irq));
  85. pcie_host_mmcfg_init(pex, PCIE_MMCFG_SIZE_MAX);
  86. sysbus_init_mmio(sbd, &pex->mmio);
  87. /*
  88. * Note that the MemoryRegions io_mmio and io_ioport that we pass
  89. * to pci_register_root_bus() are not the same as the
  90. * MemoryRegions io_mmio_window and io_ioport_window that we
  91. * expose as SysBus MRs. The difference is in the behaviour of
  92. * accesses to addresses where no PCI device has been mapped.
  93. *
  94. * io_mmio and io_ioport are the underlying PCI view of the PCI
  95. * address space, and when a PCI device does a bus master access
  96. * to a bad address this is reported back to it as a transaction
  97. * failure.
  98. *
  99. * io_mmio_window and io_ioport_window implement "unmapped
  100. * addresses read as -1 and ignore writes"; this is traditional
  101. * x86 PC behaviour, which is not mandated by the PCI spec proper
  102. * but expected by much PCI-using guest software, including Linux.
  103. *
  104. * In the interests of not being unnecessarily surprising, we
  105. * implement it in the gpex PCI host controller, by providing the
  106. * _window MRs, which are containers with io ops that implement
  107. * the 'background' behaviour and which hold the real PCI MRs as
  108. * subregions.
  109. */
  110. memory_region_init(&s->io_mmio, OBJECT(s), "gpex_mmio", UINT64_MAX);
  111. memory_region_init(&s->io_ioport, OBJECT(s), "gpex_ioport", 64 * 1024);
  112. if (s->allow_unmapped_accesses) {
  113. memory_region_init_io(&s->io_mmio_window, OBJECT(s),
  114. &unassigned_io_ops, OBJECT(s),
  115. "gpex_mmio_window", UINT64_MAX);
  116. memory_region_init_io(&s->io_ioport_window, OBJECT(s),
  117. &unassigned_io_ops, OBJECT(s),
  118. "gpex_ioport_window", 64 * 1024);
  119. memory_region_add_subregion(&s->io_mmio_window, 0, &s->io_mmio);
  120. memory_region_add_subregion(&s->io_ioport_window, 0, &s->io_ioport);
  121. sysbus_init_mmio(sbd, &s->io_mmio_window);
  122. sysbus_init_mmio(sbd, &s->io_ioport_window);
  123. } else {
  124. sysbus_init_mmio(sbd, &s->io_mmio);
  125. sysbus_init_mmio(sbd, &s->io_ioport);
  126. }
  127. for (i = 0; i < s->num_irqs; i++) {
  128. sysbus_init_irq(sbd, &s->irq[i].irq);
  129. s->irq[i].irq_num = -1;
  130. }
  131. pci->bus = pci_register_root_bus(dev, "pcie.0", gpex_set_irq,
  132. gpex_swizzle_map_irq_fn,
  133. s, &s->io_mmio, &s->io_ioport, 0,
  134. s->num_irqs, TYPE_PCIE_BUS);
  135. pci_bus_set_route_irq_fn(pci->bus, gpex_route_intx_pin_to_irq);
  136. qdev_realize(DEVICE(&s->gpex_root), BUS(pci->bus), &error_fatal);
  137. }
  138. static void gpex_host_unrealize(DeviceState *dev)
  139. {
  140. GPEXHost *s = GPEX_HOST(dev);
  141. g_free(s->irq);
  142. }
  143. static const char *gpex_host_root_bus_path(PCIHostState *host_bridge,
  144. PCIBus *rootbus)
  145. {
  146. return "0000:00";
  147. }
  148. static const Property gpex_host_properties[] = {
  149. /*
  150. * Permit CPU accesses to unmapped areas of the PIO and MMIO windows
  151. * (discarding writes and returning -1 for reads) rather than aborting.
  152. */
  153. DEFINE_PROP_BOOL("allow-unmapped-accesses", GPEXHost,
  154. allow_unmapped_accesses, true),
  155. DEFINE_PROP_UINT64(PCI_HOST_ECAM_BASE, GPEXHost, gpex_cfg.ecam.base, 0),
  156. DEFINE_PROP_SIZE(PCI_HOST_ECAM_SIZE, GPEXHost, gpex_cfg.ecam.size, 0),
  157. DEFINE_PROP_UINT64(PCI_HOST_PIO_BASE, GPEXHost, gpex_cfg.pio.base, 0),
  158. DEFINE_PROP_SIZE(PCI_HOST_PIO_SIZE, GPEXHost, gpex_cfg.pio.size, 0),
  159. DEFINE_PROP_UINT64(PCI_HOST_BELOW_4G_MMIO_BASE, GPEXHost,
  160. gpex_cfg.mmio32.base, 0),
  161. DEFINE_PROP_SIZE(PCI_HOST_BELOW_4G_MMIO_SIZE, GPEXHost,
  162. gpex_cfg.mmio32.size, 0),
  163. DEFINE_PROP_UINT64(PCI_HOST_ABOVE_4G_MMIO_BASE, GPEXHost,
  164. gpex_cfg.mmio64.base, 0),
  165. DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MMIO_SIZE, GPEXHost,
  166. gpex_cfg.mmio64.size, 0),
  167. DEFINE_PROP_UINT8("num-irqs", GPEXHost, num_irqs, PCI_NUM_PINS),
  168. };
  169. static void gpex_host_class_init(ObjectClass *klass, void *data)
  170. {
  171. DeviceClass *dc = DEVICE_CLASS(klass);
  172. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  173. hc->root_bus_path = gpex_host_root_bus_path;
  174. dc->realize = gpex_host_realize;
  175. dc->unrealize = gpex_host_unrealize;
  176. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  177. dc->fw_name = "pci";
  178. device_class_set_props(dc, gpex_host_properties);
  179. }
  180. static void gpex_host_initfn(Object *obj)
  181. {
  182. GPEXHost *s = GPEX_HOST(obj);
  183. GPEXRootState *root = &s->gpex_root;
  184. object_initialize_child(obj, "gpex_root", root, TYPE_GPEX_ROOT_DEVICE);
  185. qdev_prop_set_int32(DEVICE(root), "addr", PCI_DEVFN(0, 0));
  186. qdev_prop_set_bit(DEVICE(root), "multifunction", false);
  187. }
  188. static const TypeInfo gpex_host_info = {
  189. .name = TYPE_GPEX_HOST,
  190. .parent = TYPE_PCIE_HOST_BRIDGE,
  191. .instance_size = sizeof(GPEXHost),
  192. .instance_init = gpex_host_initfn,
  193. .class_init = gpex_host_class_init,
  194. };
  195. /****************************************************************************
  196. * GPEX Root D0:F0
  197. */
  198. static const VMStateDescription vmstate_gpex_root = {
  199. .name = "gpex_root",
  200. .version_id = 1,
  201. .minimum_version_id = 1,
  202. .fields = (const VMStateField[]) {
  203. VMSTATE_PCI_DEVICE(parent_obj, GPEXRootState),
  204. VMSTATE_END_OF_LIST()
  205. }
  206. };
  207. static void gpex_root_class_init(ObjectClass *klass, void *data)
  208. {
  209. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  210. DeviceClass *dc = DEVICE_CLASS(klass);
  211. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  212. dc->desc = "QEMU generic PCIe host bridge";
  213. dc->vmsd = &vmstate_gpex_root;
  214. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  215. k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_HOST;
  216. k->revision = 0;
  217. k->class_id = PCI_CLASS_BRIDGE_HOST;
  218. /*
  219. * PCI-facing part of the host bridge, not usable without the
  220. * host-facing part, which can't be device_add'ed, yet.
  221. */
  222. dc->user_creatable = false;
  223. }
  224. static const TypeInfo gpex_root_info = {
  225. .name = TYPE_GPEX_ROOT_DEVICE,
  226. .parent = TYPE_PCI_DEVICE,
  227. .instance_size = sizeof(GPEXRootState),
  228. .class_init = gpex_root_class_init,
  229. .interfaces = (InterfaceInfo[]) {
  230. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  231. { },
  232. },
  233. };
  234. static void gpex_register(void)
  235. {
  236. type_register_static(&gpex_root_info);
  237. type_register_static(&gpex_host_info);
  238. }
  239. type_init(gpex_register)