1a35fe226SAleksandr Bezzubikov /* 2a35fe226SAleksandr Bezzubikov * QEMU Generic PCIE-PCI Bridge 3a35fe226SAleksandr Bezzubikov * 4a35fe226SAleksandr Bezzubikov * Copyright (c) 2017 Aleksandr Bezzubikov 5a35fe226SAleksandr Bezzubikov * 6a35fe226SAleksandr Bezzubikov * This work is licensed under the terms of the GNU GPL, version 2 or later. 7a35fe226SAleksandr Bezzubikov * See the COPYING file in the top-level directory. 8a35fe226SAleksandr Bezzubikov */ 9a35fe226SAleksandr Bezzubikov 10a35fe226SAleksandr Bezzubikov #include "qemu/osdep.h" 11a35fe226SAleksandr Bezzubikov #include "qapi/error.h" 120b8fa32fSMarkus Armbruster #include "qemu/module.h" 13a35fe226SAleksandr Bezzubikov #include "hw/pci/pci.h" 14a35fe226SAleksandr Bezzubikov #include "hw/pci/pci_bus.h" 15a35fe226SAleksandr Bezzubikov #include "hw/pci/pci_bridge.h" 16a35fe226SAleksandr Bezzubikov #include "hw/pci/msi.h" 17a35fe226SAleksandr Bezzubikov #include "hw/pci/shpc.h" 18a35fe226SAleksandr Bezzubikov #include "hw/pci/slotid_cap.h" 19a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 20*db1015e9SEduardo Habkost #include "qom/object.h" 21a35fe226SAleksandr Bezzubikov 22*db1015e9SEduardo Habkost struct PCIEPCIBridge { 23a35fe226SAleksandr Bezzubikov /*< private >*/ 24a35fe226SAleksandr Bezzubikov PCIBridge parent_obj; 25a35fe226SAleksandr Bezzubikov 26a35fe226SAleksandr Bezzubikov OnOffAuto msi; 27a35fe226SAleksandr Bezzubikov MemoryRegion shpc_bar; 28a35fe226SAleksandr Bezzubikov /*< public >*/ 29*db1015e9SEduardo Habkost }; 30*db1015e9SEduardo Habkost typedef struct PCIEPCIBridge PCIEPCIBridge; 31a35fe226SAleksandr Bezzubikov 32a35fe226SAleksandr Bezzubikov #define TYPE_PCIE_PCI_BRIDGE_DEV "pcie-pci-bridge" 33a35fe226SAleksandr Bezzubikov #define PCIE_PCI_BRIDGE_DEV(obj) \ 34a35fe226SAleksandr Bezzubikov OBJECT_CHECK(PCIEPCIBridge, (obj), TYPE_PCIE_PCI_BRIDGE_DEV) 35a35fe226SAleksandr Bezzubikov 36a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_realize(PCIDevice *d, Error **errp) 37a35fe226SAleksandr Bezzubikov { 38a35fe226SAleksandr Bezzubikov PCIBridge *br = PCI_BRIDGE(d); 39a35fe226SAleksandr Bezzubikov PCIEPCIBridge *pcie_br = PCIE_PCI_BRIDGE_DEV(d); 40a35fe226SAleksandr Bezzubikov int rc, pos; 41a35fe226SAleksandr Bezzubikov 42a35fe226SAleksandr Bezzubikov pci_bridge_initfn(d, TYPE_PCI_BUS); 43a35fe226SAleksandr Bezzubikov 44a35fe226SAleksandr Bezzubikov d->config[PCI_INTERRUPT_PIN] = 0x1; 45a35fe226SAleksandr Bezzubikov memory_region_init(&pcie_br->shpc_bar, OBJECT(d), "shpc-bar", 46a35fe226SAleksandr Bezzubikov shpc_bar_size(d)); 47a35fe226SAleksandr Bezzubikov rc = shpc_init(d, &br->sec_bus, &pcie_br->shpc_bar, 0, errp); 48a35fe226SAleksandr Bezzubikov if (rc) { 49a35fe226SAleksandr Bezzubikov goto error; 50a35fe226SAleksandr Bezzubikov } 51a35fe226SAleksandr Bezzubikov 52a35fe226SAleksandr Bezzubikov rc = pcie_cap_init(d, 0, PCI_EXP_TYPE_PCI_BRIDGE, 0, errp); 53a35fe226SAleksandr Bezzubikov if (rc < 0) { 54a35fe226SAleksandr Bezzubikov goto cap_error; 55a35fe226SAleksandr Bezzubikov } 56a35fe226SAleksandr Bezzubikov 57a35fe226SAleksandr Bezzubikov pos = pci_add_capability(d, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF, errp); 58a35fe226SAleksandr Bezzubikov if (pos < 0) { 59a35fe226SAleksandr Bezzubikov goto pm_error; 60a35fe226SAleksandr Bezzubikov } 61a35fe226SAleksandr Bezzubikov d->exp.pm_cap = pos; 62a35fe226SAleksandr Bezzubikov pci_set_word(d->config + pos + PCI_PM_PMC, 0x3); 63a35fe226SAleksandr Bezzubikov 64a35fe226SAleksandr Bezzubikov pcie_cap_arifwd_init(d); 65a35fe226SAleksandr Bezzubikov pcie_cap_deverr_init(d); 66a35fe226SAleksandr Bezzubikov 67a35fe226SAleksandr Bezzubikov rc = pcie_aer_init(d, PCI_ERR_VER, 0x100, PCI_ERR_SIZEOF, errp); 68a35fe226SAleksandr Bezzubikov if (rc < 0) { 69a35fe226SAleksandr Bezzubikov goto aer_error; 70a35fe226SAleksandr Bezzubikov } 71a35fe226SAleksandr Bezzubikov 72d659d940SAleksandr Bezzubikov Error *local_err = NULL; 73a35fe226SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_OFF) { 74d659d940SAleksandr Bezzubikov rc = msi_init(d, 0, 1, true, true, &local_err); 75a35fe226SAleksandr Bezzubikov if (rc < 0) { 76d659d940SAleksandr Bezzubikov assert(rc == -ENOTSUP); 77d659d940SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_ON) { 78d659d940SAleksandr Bezzubikov error_free(local_err); 79d659d940SAleksandr Bezzubikov } else { 80d659d940SAleksandr Bezzubikov /* failed to satisfy user's explicit request for MSI */ 81d659d940SAleksandr Bezzubikov error_propagate(errp, local_err); 82a35fe226SAleksandr Bezzubikov goto msi_error; 83a35fe226SAleksandr Bezzubikov } 84a35fe226SAleksandr Bezzubikov } 85d659d940SAleksandr Bezzubikov } 86a35fe226SAleksandr Bezzubikov pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY | 87a35fe226SAleksandr Bezzubikov PCI_BASE_ADDRESS_MEM_TYPE_64, &pcie_br->shpc_bar); 88a35fe226SAleksandr Bezzubikov return; 89a35fe226SAleksandr Bezzubikov 90a35fe226SAleksandr Bezzubikov msi_error: 91a35fe226SAleksandr Bezzubikov pcie_aer_exit(d); 92a35fe226SAleksandr Bezzubikov aer_error: 93a35fe226SAleksandr Bezzubikov pm_error: 94a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 95a35fe226SAleksandr Bezzubikov cap_error: 96d659d940SAleksandr Bezzubikov shpc_cleanup(d, &pcie_br->shpc_bar); 97a35fe226SAleksandr Bezzubikov error: 98a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 99a35fe226SAleksandr Bezzubikov } 100a35fe226SAleksandr Bezzubikov 101a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_exit(PCIDevice *d) 102a35fe226SAleksandr Bezzubikov { 103a35fe226SAleksandr Bezzubikov PCIEPCIBridge *bridge_dev = PCIE_PCI_BRIDGE_DEV(d); 104a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 105a35fe226SAleksandr Bezzubikov shpc_cleanup(d, &bridge_dev->shpc_bar); 106a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 107a35fe226SAleksandr Bezzubikov } 108a35fe226SAleksandr Bezzubikov 109a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_reset(DeviceState *qdev) 110a35fe226SAleksandr Bezzubikov { 111a35fe226SAleksandr Bezzubikov PCIDevice *d = PCI_DEVICE(qdev); 112a35fe226SAleksandr Bezzubikov pci_bridge_reset(qdev); 113d659d940SAleksandr Bezzubikov if (msi_present(d)) { 114a35fe226SAleksandr Bezzubikov msi_reset(d); 115d659d940SAleksandr Bezzubikov } 116a35fe226SAleksandr Bezzubikov shpc_reset(d); 117a35fe226SAleksandr Bezzubikov } 118a35fe226SAleksandr Bezzubikov 119a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_write_config(PCIDevice *d, 120a35fe226SAleksandr Bezzubikov uint32_t address, uint32_t val, int len) 121a35fe226SAleksandr Bezzubikov { 122a35fe226SAleksandr Bezzubikov pci_bridge_write_config(d, address, val, len); 123d659d940SAleksandr Bezzubikov if (msi_present(d)) { 124a35fe226SAleksandr Bezzubikov msi_write_config(d, address, val, len); 125d659d940SAleksandr Bezzubikov } 126a35fe226SAleksandr Bezzubikov shpc_cap_write_config(d, address, val, len); 127a35fe226SAleksandr Bezzubikov } 128a35fe226SAleksandr Bezzubikov 129a35fe226SAleksandr Bezzubikov static Property pcie_pci_bridge_dev_properties[] = { 130d659d940SAleksandr Bezzubikov DEFINE_PROP_ON_OFF_AUTO("msi", PCIEPCIBridge, msi, ON_OFF_AUTO_AUTO), 131a35fe226SAleksandr Bezzubikov DEFINE_PROP_END_OF_LIST(), 132a35fe226SAleksandr Bezzubikov }; 133a35fe226SAleksandr Bezzubikov 134a35fe226SAleksandr Bezzubikov static const VMStateDescription pcie_pci_bridge_dev_vmstate = { 135a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 1369d6b9db1SPeter Xu .priority = MIG_PRI_PCI_BUS, 137a35fe226SAleksandr Bezzubikov .fields = (VMStateField[]) { 138a35fe226SAleksandr Bezzubikov VMSTATE_PCI_DEVICE(parent_obj, PCIBridge), 139a35fe226SAleksandr Bezzubikov SHPC_VMSTATE(shpc, PCIDevice, NULL), 140a35fe226SAleksandr Bezzubikov VMSTATE_END_OF_LIST() 141a35fe226SAleksandr Bezzubikov } 142a35fe226SAleksandr Bezzubikov }; 143a35fe226SAleksandr Bezzubikov 144a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_class_init(ObjectClass *klass, void *data) 145a35fe226SAleksandr Bezzubikov { 146a35fe226SAleksandr Bezzubikov PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 147a35fe226SAleksandr Bezzubikov DeviceClass *dc = DEVICE_CLASS(klass); 148a35fe226SAleksandr Bezzubikov HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 149a35fe226SAleksandr Bezzubikov 15091f4c995SDavid Gibson k->is_bridge = true; 151a35fe226SAleksandr Bezzubikov k->vendor_id = PCI_VENDOR_ID_REDHAT; 152a35fe226SAleksandr Bezzubikov k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_BRIDGE; 153a35fe226SAleksandr Bezzubikov k->realize = pcie_pci_bridge_realize; 154a35fe226SAleksandr Bezzubikov k->exit = pcie_pci_bridge_exit; 155a35fe226SAleksandr Bezzubikov k->config_write = pcie_pci_bridge_write_config; 156a35fe226SAleksandr Bezzubikov dc->vmsd = &pcie_pci_bridge_dev_vmstate; 1574f67d30bSMarc-André Lureau device_class_set_props(dc, pcie_pci_bridge_dev_properties); 158a35fe226SAleksandr Bezzubikov dc->reset = &pcie_pci_bridge_reset; 159a35fe226SAleksandr Bezzubikov set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 16062b76563SDavid Hildenbrand hc->plug = pci_bridge_dev_plug_cb; 1618f560cdcSDavid Hildenbrand hc->unplug = pci_bridge_dev_unplug_cb; 16262b76563SDavid Hildenbrand hc->unplug_request = pci_bridge_dev_unplug_request_cb; 163a35fe226SAleksandr Bezzubikov } 164a35fe226SAleksandr Bezzubikov 165a35fe226SAleksandr Bezzubikov static const TypeInfo pcie_pci_bridge_info = { 166a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 167a35fe226SAleksandr Bezzubikov .parent = TYPE_PCI_BRIDGE, 168a35fe226SAleksandr Bezzubikov .instance_size = sizeof(PCIEPCIBridge), 169a35fe226SAleksandr Bezzubikov .class_init = pcie_pci_bridge_class_init, 170a35fe226SAleksandr Bezzubikov .interfaces = (InterfaceInfo[]) { 171a35fe226SAleksandr Bezzubikov { TYPE_HOTPLUG_HANDLER }, 17271d78767SEduardo Habkost { INTERFACE_PCIE_DEVICE }, 173a35fe226SAleksandr Bezzubikov { }, 174a35fe226SAleksandr Bezzubikov } 175a35fe226SAleksandr Bezzubikov }; 176a35fe226SAleksandr Bezzubikov 177a35fe226SAleksandr Bezzubikov static void pciepci_register(void) 178a35fe226SAleksandr Bezzubikov { 179a35fe226SAleksandr Bezzubikov type_register_static(&pcie_pci_bridge_info); 180a35fe226SAleksandr Bezzubikov } 181a35fe226SAleksandr Bezzubikov 182a35fe226SAleksandr Bezzubikov type_init(pciepci_register); 183