2
0

gpex.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-host/gpex.h"
  35. #include "hw/qdev-properties.h"
  36. #include "migration/vmstate.h"
  37. #include "qemu/module.h"
  38. /****************************************************************************
  39. * GPEX host
  40. */
  41. static void gpex_set_irq(void *opaque, int irq_num, int level)
  42. {
  43. GPEXHost *s = opaque;
  44. qemu_set_irq(s->irq[irq_num], level);
  45. }
  46. int gpex_set_irq_num(GPEXHost *s, int index, int gsi)
  47. {
  48. if (index >= GPEX_NUM_IRQS) {
  49. return -EINVAL;
  50. }
  51. s->irq_num[index] = gsi;
  52. return 0;
  53. }
  54. static PCIINTxRoute gpex_route_intx_pin_to_irq(void *opaque, int pin)
  55. {
  56. PCIINTxRoute route;
  57. GPEXHost *s = opaque;
  58. int gsi = s->irq_num[pin];
  59. route.irq = gsi;
  60. if (gsi < 0) {
  61. route.mode = PCI_INTX_DISABLED;
  62. } else {
  63. route.mode = PCI_INTX_ENABLED;
  64. }
  65. return route;
  66. }
  67. static void gpex_host_realize(DeviceState *dev, Error **errp)
  68. {
  69. PCIHostState *pci = PCI_HOST_BRIDGE(dev);
  70. GPEXHost *s = GPEX_HOST(dev);
  71. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  72. PCIExpressHost *pex = PCIE_HOST_BRIDGE(dev);
  73. int i;
  74. pcie_host_mmcfg_init(pex, PCIE_MMCFG_SIZE_MAX);
  75. memory_region_init(&s->io_mmio, OBJECT(s), "gpex_mmio", UINT64_MAX);
  76. memory_region_init(&s->io_ioport, OBJECT(s), "gpex_ioport", 64 * 1024);
  77. sysbus_init_mmio(sbd, &pex->mmio);
  78. sysbus_init_mmio(sbd, &s->io_mmio);
  79. sysbus_init_mmio(sbd, &s->io_ioport);
  80. for (i = 0; i < GPEX_NUM_IRQS; i++) {
  81. sysbus_init_irq(sbd, &s->irq[i]);
  82. s->irq_num[i] = -1;
  83. }
  84. pci->bus = pci_register_root_bus(dev, "pcie.0", gpex_set_irq,
  85. pci_swizzle_map_irq_fn, s, &s->io_mmio,
  86. &s->io_ioport, 0, 4, TYPE_PCIE_BUS);
  87. pci_bus_set_route_irq_fn(pci->bus, gpex_route_intx_pin_to_irq);
  88. qdev_realize(DEVICE(&s->gpex_root), BUS(pci->bus), &error_fatal);
  89. }
  90. static const char *gpex_host_root_bus_path(PCIHostState *host_bridge,
  91. PCIBus *rootbus)
  92. {
  93. return "0000:00";
  94. }
  95. static void gpex_host_class_init(ObjectClass *klass, void *data)
  96. {
  97. DeviceClass *dc = DEVICE_CLASS(klass);
  98. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  99. hc->root_bus_path = gpex_host_root_bus_path;
  100. dc->realize = gpex_host_realize;
  101. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  102. dc->fw_name = "pci";
  103. }
  104. static void gpex_host_initfn(Object *obj)
  105. {
  106. GPEXHost *s = GPEX_HOST(obj);
  107. GPEXRootState *root = &s->gpex_root;
  108. object_initialize_child(obj, "gpex_root", root, TYPE_GPEX_ROOT_DEVICE);
  109. qdev_prop_set_int32(DEVICE(root), "addr", PCI_DEVFN(0, 0));
  110. qdev_prop_set_bit(DEVICE(root), "multifunction", false);
  111. }
  112. static const TypeInfo gpex_host_info = {
  113. .name = TYPE_GPEX_HOST,
  114. .parent = TYPE_PCIE_HOST_BRIDGE,
  115. .instance_size = sizeof(GPEXHost),
  116. .instance_init = gpex_host_initfn,
  117. .class_init = gpex_host_class_init,
  118. };
  119. /****************************************************************************
  120. * GPEX Root D0:F0
  121. */
  122. static const VMStateDescription vmstate_gpex_root = {
  123. .name = "gpex_root",
  124. .version_id = 1,
  125. .minimum_version_id = 1,
  126. .fields = (VMStateField[]) {
  127. VMSTATE_PCI_DEVICE(parent_obj, GPEXRootState),
  128. VMSTATE_END_OF_LIST()
  129. }
  130. };
  131. static void gpex_root_class_init(ObjectClass *klass, void *data)
  132. {
  133. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  134. DeviceClass *dc = DEVICE_CLASS(klass);
  135. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  136. dc->desc = "QEMU generic PCIe host bridge";
  137. dc->vmsd = &vmstate_gpex_root;
  138. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  139. k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_HOST;
  140. k->revision = 0;
  141. k->class_id = PCI_CLASS_BRIDGE_HOST;
  142. /*
  143. * PCI-facing part of the host bridge, not usable without the
  144. * host-facing part, which can't be device_add'ed, yet.
  145. */
  146. dc->user_creatable = false;
  147. }
  148. static const TypeInfo gpex_root_info = {
  149. .name = TYPE_GPEX_ROOT_DEVICE,
  150. .parent = TYPE_PCI_DEVICE,
  151. .instance_size = sizeof(GPEXRootState),
  152. .class_init = gpex_root_class_init,
  153. .interfaces = (InterfaceInfo[]) {
  154. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  155. { },
  156. },
  157. };
  158. static void gpex_register(void)
  159. {
  160. type_register_static(&gpex_root_info);
  161. type_register_static(&gpex_host_info);
  162. }
  163. type_init(gpex_register)