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