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" 20db1015e9SEduardo Habkost #include "qom/object.h" 21a35fe226SAleksandr Bezzubikov 22db1015e9SEduardo 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 >*/ 29db1015e9SEduardo Habkost }; 30a35fe226SAleksandr Bezzubikov 31a35fe226SAleksandr Bezzubikov #define TYPE_PCIE_PCI_BRIDGE_DEV "pcie-pci-bridge" 328063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(PCIEPCIBridge, PCIE_PCI_BRIDGE_DEV) 33a35fe226SAleksandr Bezzubikov 34a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_realize(PCIDevice *d, Error **errp) 35a35fe226SAleksandr Bezzubikov { 36a35fe226SAleksandr Bezzubikov PCIBridge *br = PCI_BRIDGE(d); 37a35fe226SAleksandr Bezzubikov PCIEPCIBridge *pcie_br = PCIE_PCI_BRIDGE_DEV(d); 38a35fe226SAleksandr Bezzubikov int rc, pos; 39a35fe226SAleksandr Bezzubikov 40a35fe226SAleksandr Bezzubikov pci_bridge_initfn(d, TYPE_PCI_BUS); 41a35fe226SAleksandr Bezzubikov 42a35fe226SAleksandr Bezzubikov d->config[PCI_INTERRUPT_PIN] = 0x1; 43a35fe226SAleksandr Bezzubikov memory_region_init(&pcie_br->shpc_bar, OBJECT(d), "shpc-bar", 44a35fe226SAleksandr Bezzubikov shpc_bar_size(d)); 45a35fe226SAleksandr Bezzubikov rc = shpc_init(d, &br->sec_bus, &pcie_br->shpc_bar, 0, errp); 46a35fe226SAleksandr Bezzubikov if (rc) { 47a35fe226SAleksandr Bezzubikov goto error; 48a35fe226SAleksandr Bezzubikov } 49a35fe226SAleksandr Bezzubikov 50a35fe226SAleksandr Bezzubikov rc = pcie_cap_init(d, 0, PCI_EXP_TYPE_PCI_BRIDGE, 0, errp); 51a35fe226SAleksandr Bezzubikov if (rc < 0) { 52a35fe226SAleksandr Bezzubikov goto cap_error; 53a35fe226SAleksandr Bezzubikov } 54a35fe226SAleksandr Bezzubikov 550681ec25SAlex Williamson pos = pci_pm_init(d, 0, errp); 56a35fe226SAleksandr Bezzubikov if (pos < 0) { 57a35fe226SAleksandr Bezzubikov goto pm_error; 58a35fe226SAleksandr Bezzubikov } 59a35fe226SAleksandr Bezzubikov pci_set_word(d->config + pos + PCI_PM_PMC, 0x3); 60a35fe226SAleksandr Bezzubikov 61a35fe226SAleksandr Bezzubikov pcie_cap_arifwd_init(d); 62a35fe226SAleksandr Bezzubikov pcie_cap_deverr_init(d); 63a35fe226SAleksandr Bezzubikov 64a35fe226SAleksandr Bezzubikov rc = pcie_aer_init(d, PCI_ERR_VER, 0x100, PCI_ERR_SIZEOF, errp); 65a35fe226SAleksandr Bezzubikov if (rc < 0) { 66a35fe226SAleksandr Bezzubikov goto aer_error; 67a35fe226SAleksandr Bezzubikov } 68a35fe226SAleksandr Bezzubikov 69d659d940SAleksandr Bezzubikov Error *local_err = NULL; 70a35fe226SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_OFF) { 71d659d940SAleksandr Bezzubikov rc = msi_init(d, 0, 1, true, true, &local_err); 72a35fe226SAleksandr Bezzubikov if (rc < 0) { 73d659d940SAleksandr Bezzubikov assert(rc == -ENOTSUP); 74d659d940SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_ON) { 75d659d940SAleksandr Bezzubikov error_free(local_err); 76d659d940SAleksandr Bezzubikov } else { 77d659d940SAleksandr Bezzubikov /* failed to satisfy user's explicit request for MSI */ 78d659d940SAleksandr Bezzubikov error_propagate(errp, local_err); 79a35fe226SAleksandr Bezzubikov goto msi_error; 80a35fe226SAleksandr Bezzubikov } 81a35fe226SAleksandr Bezzubikov } 82d659d940SAleksandr Bezzubikov } 83a35fe226SAleksandr Bezzubikov pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY | 84a35fe226SAleksandr Bezzubikov PCI_BASE_ADDRESS_MEM_TYPE_64, &pcie_br->shpc_bar); 85a35fe226SAleksandr Bezzubikov return; 86a35fe226SAleksandr Bezzubikov 87a35fe226SAleksandr Bezzubikov msi_error: 88a35fe226SAleksandr Bezzubikov pcie_aer_exit(d); 89a35fe226SAleksandr Bezzubikov aer_error: 90a35fe226SAleksandr Bezzubikov pm_error: 91a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 92a35fe226SAleksandr Bezzubikov cap_error: 93d659d940SAleksandr Bezzubikov shpc_cleanup(d, &pcie_br->shpc_bar); 94a35fe226SAleksandr Bezzubikov error: 95a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 96a35fe226SAleksandr Bezzubikov } 97a35fe226SAleksandr Bezzubikov 98a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_exit(PCIDevice *d) 99a35fe226SAleksandr Bezzubikov { 100a35fe226SAleksandr Bezzubikov PCIEPCIBridge *bridge_dev = PCIE_PCI_BRIDGE_DEV(d); 101a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 102a35fe226SAleksandr Bezzubikov shpc_cleanup(d, &bridge_dev->shpc_bar); 103a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 104a35fe226SAleksandr Bezzubikov } 105a35fe226SAleksandr Bezzubikov 106a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_reset(DeviceState *qdev) 107a35fe226SAleksandr Bezzubikov { 108a35fe226SAleksandr Bezzubikov PCIDevice *d = PCI_DEVICE(qdev); 109a35fe226SAleksandr Bezzubikov pci_bridge_reset(qdev); 110d659d940SAleksandr Bezzubikov if (msi_present(d)) { 111a35fe226SAleksandr Bezzubikov msi_reset(d); 112d659d940SAleksandr Bezzubikov } 113a35fe226SAleksandr Bezzubikov shpc_reset(d); 114a35fe226SAleksandr Bezzubikov } 115a35fe226SAleksandr Bezzubikov 116a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_write_config(PCIDevice *d, 117a35fe226SAleksandr Bezzubikov uint32_t address, uint32_t val, int len) 118a35fe226SAleksandr Bezzubikov { 119a35fe226SAleksandr Bezzubikov pci_bridge_write_config(d, address, val, len); 120d659d940SAleksandr Bezzubikov if (msi_present(d)) { 121a35fe226SAleksandr Bezzubikov msi_write_config(d, address, val, len); 122d659d940SAleksandr Bezzubikov } 123a35fe226SAleksandr Bezzubikov shpc_cap_write_config(d, address, val, len); 124a35fe226SAleksandr Bezzubikov } 125a35fe226SAleksandr Bezzubikov 126196fd15fSRichard Henderson static const Property pcie_pci_bridge_dev_properties[] = { 127d659d940SAleksandr Bezzubikov DEFINE_PROP_ON_OFF_AUTO("msi", PCIEPCIBridge, msi, ON_OFF_AUTO_AUTO), 128a35fe226SAleksandr Bezzubikov }; 129a35fe226SAleksandr Bezzubikov 130a35fe226SAleksandr Bezzubikov static const VMStateDescription pcie_pci_bridge_dev_vmstate = { 131a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 1329d6b9db1SPeter Xu .priority = MIG_PRI_PCI_BUS, 133f026c578SRichard Henderson .fields = (const VMStateField[]) { 134a35fe226SAleksandr Bezzubikov VMSTATE_PCI_DEVICE(parent_obj, PCIBridge), 135a35fe226SAleksandr Bezzubikov SHPC_VMSTATE(shpc, PCIDevice, NULL), 136a35fe226SAleksandr Bezzubikov VMSTATE_END_OF_LIST() 137a35fe226SAleksandr Bezzubikov } 138a35fe226SAleksandr Bezzubikov }; 139a35fe226SAleksandr Bezzubikov 14012d1a768SPhilippe Mathieu-Daudé static void pcie_pci_bridge_class_init(ObjectClass *klass, const void *data) 141a35fe226SAleksandr Bezzubikov { 142a35fe226SAleksandr Bezzubikov PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 143a35fe226SAleksandr Bezzubikov DeviceClass *dc = DEVICE_CLASS(klass); 144a35fe226SAleksandr Bezzubikov HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 145a35fe226SAleksandr Bezzubikov 146a35fe226SAleksandr Bezzubikov k->vendor_id = PCI_VENDOR_ID_REDHAT; 147a35fe226SAleksandr Bezzubikov k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_BRIDGE; 148a35fe226SAleksandr Bezzubikov k->realize = pcie_pci_bridge_realize; 149a35fe226SAleksandr Bezzubikov k->exit = pcie_pci_bridge_exit; 150a35fe226SAleksandr Bezzubikov k->config_write = pcie_pci_bridge_write_config; 151a35fe226SAleksandr Bezzubikov dc->vmsd = &pcie_pci_bridge_dev_vmstate; 1524f67d30bSMarc-André Lureau device_class_set_props(dc, pcie_pci_bridge_dev_properties); 153e3d08143SPeter Maydell device_class_set_legacy_reset(dc, pcie_pci_bridge_reset); 154a35fe226SAleksandr Bezzubikov set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 15562b76563SDavid Hildenbrand hc->plug = pci_bridge_dev_plug_cb; 1568f560cdcSDavid Hildenbrand hc->unplug = pci_bridge_dev_unplug_cb; 15762b76563SDavid Hildenbrand hc->unplug_request = pci_bridge_dev_unplug_request_cb; 158a35fe226SAleksandr Bezzubikov } 159a35fe226SAleksandr Bezzubikov 160a35fe226SAleksandr Bezzubikov static const TypeInfo pcie_pci_bridge_info = { 161a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 162a35fe226SAleksandr Bezzubikov .parent = TYPE_PCI_BRIDGE, 163a35fe226SAleksandr Bezzubikov .instance_size = sizeof(PCIEPCIBridge), 164a35fe226SAleksandr Bezzubikov .class_init = pcie_pci_bridge_class_init, 165*2cd09e47SPhilippe Mathieu-Daudé .interfaces = (const InterfaceInfo[]) { 166a35fe226SAleksandr Bezzubikov { TYPE_HOTPLUG_HANDLER }, 16771d78767SEduardo Habkost { INTERFACE_PCIE_DEVICE }, 168a35fe226SAleksandr Bezzubikov { }, 169a35fe226SAleksandr Bezzubikov } 170a35fe226SAleksandr Bezzubikov }; 171a35fe226SAleksandr Bezzubikov 172a35fe226SAleksandr Bezzubikov static void pciepci_register(void) 173a35fe226SAleksandr Bezzubikov { 174a35fe226SAleksandr Bezzubikov type_register_static(&pcie_pci_bridge_info); 175a35fe226SAleksandr Bezzubikov } 176a35fe226SAleksandr Bezzubikov 177a35fe226SAleksandr Bezzubikov type_init(pciepci_register); 178