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" 20a35fe226SAleksandr Bezzubikov 21a35fe226SAleksandr Bezzubikov typedef struct PCIEPCIBridge { 22a35fe226SAleksandr Bezzubikov /*< private >*/ 23a35fe226SAleksandr Bezzubikov PCIBridge parent_obj; 24a35fe226SAleksandr Bezzubikov 25a35fe226SAleksandr Bezzubikov OnOffAuto msi; 26a35fe226SAleksandr Bezzubikov MemoryRegion shpc_bar; 27a35fe226SAleksandr Bezzubikov /*< public >*/ 28a35fe226SAleksandr Bezzubikov } PCIEPCIBridge; 29a35fe226SAleksandr Bezzubikov 30a35fe226SAleksandr Bezzubikov #define TYPE_PCIE_PCI_BRIDGE_DEV "pcie-pci-bridge" 31a35fe226SAleksandr Bezzubikov #define PCIE_PCI_BRIDGE_DEV(obj) \ 32a35fe226SAleksandr Bezzubikov OBJECT_CHECK(PCIEPCIBridge, (obj), TYPE_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 55a35fe226SAleksandr Bezzubikov pos = pci_add_capability(d, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF, errp); 56a35fe226SAleksandr Bezzubikov if (pos < 0) { 57a35fe226SAleksandr Bezzubikov goto pm_error; 58a35fe226SAleksandr Bezzubikov } 59a35fe226SAleksandr Bezzubikov d->exp.pm_cap = pos; 60a35fe226SAleksandr Bezzubikov pci_set_word(d->config + pos + PCI_PM_PMC, 0x3); 61a35fe226SAleksandr Bezzubikov 62a35fe226SAleksandr Bezzubikov pcie_cap_arifwd_init(d); 63a35fe226SAleksandr Bezzubikov pcie_cap_deverr_init(d); 64a35fe226SAleksandr Bezzubikov 65a35fe226SAleksandr Bezzubikov rc = pcie_aer_init(d, PCI_ERR_VER, 0x100, PCI_ERR_SIZEOF, errp); 66a35fe226SAleksandr Bezzubikov if (rc < 0) { 67a35fe226SAleksandr Bezzubikov goto aer_error; 68a35fe226SAleksandr Bezzubikov } 69a35fe226SAleksandr Bezzubikov 70d659d940SAleksandr Bezzubikov Error *local_err = NULL; 71a35fe226SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_OFF) { 72d659d940SAleksandr Bezzubikov rc = msi_init(d, 0, 1, true, true, &local_err); 73a35fe226SAleksandr Bezzubikov if (rc < 0) { 74d659d940SAleksandr Bezzubikov assert(rc == -ENOTSUP); 75d659d940SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_ON) { 76d659d940SAleksandr Bezzubikov error_free(local_err); 77d659d940SAleksandr Bezzubikov } else { 78d659d940SAleksandr Bezzubikov /* failed to satisfy user's explicit request for MSI */ 79d659d940SAleksandr Bezzubikov error_propagate(errp, local_err); 80a35fe226SAleksandr Bezzubikov goto msi_error; 81a35fe226SAleksandr Bezzubikov } 82a35fe226SAleksandr Bezzubikov } 83d659d940SAleksandr Bezzubikov } 84a35fe226SAleksandr Bezzubikov pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY | 85a35fe226SAleksandr Bezzubikov PCI_BASE_ADDRESS_MEM_TYPE_64, &pcie_br->shpc_bar); 86a35fe226SAleksandr Bezzubikov return; 87a35fe226SAleksandr Bezzubikov 88a35fe226SAleksandr Bezzubikov msi_error: 89a35fe226SAleksandr Bezzubikov pcie_aer_exit(d); 90a35fe226SAleksandr Bezzubikov aer_error: 91a35fe226SAleksandr Bezzubikov pm_error: 92a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 93a35fe226SAleksandr Bezzubikov cap_error: 94d659d940SAleksandr Bezzubikov shpc_cleanup(d, &pcie_br->shpc_bar); 95a35fe226SAleksandr Bezzubikov error: 96a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 97a35fe226SAleksandr Bezzubikov } 98a35fe226SAleksandr Bezzubikov 99a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_exit(PCIDevice *d) 100a35fe226SAleksandr Bezzubikov { 101a35fe226SAleksandr Bezzubikov PCIEPCIBridge *bridge_dev = PCIE_PCI_BRIDGE_DEV(d); 102a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 103a35fe226SAleksandr Bezzubikov shpc_cleanup(d, &bridge_dev->shpc_bar); 104a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 105a35fe226SAleksandr Bezzubikov } 106a35fe226SAleksandr Bezzubikov 107a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_reset(DeviceState *qdev) 108a35fe226SAleksandr Bezzubikov { 109a35fe226SAleksandr Bezzubikov PCIDevice *d = PCI_DEVICE(qdev); 110a35fe226SAleksandr Bezzubikov pci_bridge_reset(qdev); 111d659d940SAleksandr Bezzubikov if (msi_present(d)) { 112a35fe226SAleksandr Bezzubikov msi_reset(d); 113d659d940SAleksandr Bezzubikov } 114a35fe226SAleksandr Bezzubikov shpc_reset(d); 115a35fe226SAleksandr Bezzubikov } 116a35fe226SAleksandr Bezzubikov 117a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_write_config(PCIDevice *d, 118a35fe226SAleksandr Bezzubikov uint32_t address, uint32_t val, int len) 119a35fe226SAleksandr Bezzubikov { 120a35fe226SAleksandr Bezzubikov pci_bridge_write_config(d, address, val, len); 121d659d940SAleksandr Bezzubikov if (msi_present(d)) { 122a35fe226SAleksandr Bezzubikov msi_write_config(d, address, val, len); 123d659d940SAleksandr Bezzubikov } 124a35fe226SAleksandr Bezzubikov shpc_cap_write_config(d, address, val, len); 125a35fe226SAleksandr Bezzubikov } 126a35fe226SAleksandr Bezzubikov 127a35fe226SAleksandr Bezzubikov static Property pcie_pci_bridge_dev_properties[] = { 128d659d940SAleksandr Bezzubikov DEFINE_PROP_ON_OFF_AUTO("msi", PCIEPCIBridge, msi, ON_OFF_AUTO_AUTO), 129a35fe226SAleksandr Bezzubikov DEFINE_PROP_END_OF_LIST(), 130a35fe226SAleksandr Bezzubikov }; 131a35fe226SAleksandr Bezzubikov 132a35fe226SAleksandr Bezzubikov static const VMStateDescription pcie_pci_bridge_dev_vmstate = { 133a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 1349d6b9db1SPeter Xu .priority = MIG_PRI_PCI_BUS, 135a35fe226SAleksandr Bezzubikov .fields = (VMStateField[]) { 136a35fe226SAleksandr Bezzubikov VMSTATE_PCI_DEVICE(parent_obj, PCIBridge), 137a35fe226SAleksandr Bezzubikov SHPC_VMSTATE(shpc, PCIDevice, NULL), 138a35fe226SAleksandr Bezzubikov VMSTATE_END_OF_LIST() 139a35fe226SAleksandr Bezzubikov } 140a35fe226SAleksandr Bezzubikov }; 141a35fe226SAleksandr Bezzubikov 142a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_class_init(ObjectClass *klass, void *data) 143a35fe226SAleksandr Bezzubikov { 144a35fe226SAleksandr Bezzubikov PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 145a35fe226SAleksandr Bezzubikov DeviceClass *dc = DEVICE_CLASS(klass); 146a35fe226SAleksandr Bezzubikov HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 147a35fe226SAleksandr Bezzubikov 14891f4c995SDavid Gibson k->is_bridge = true; 149a35fe226SAleksandr Bezzubikov k->vendor_id = PCI_VENDOR_ID_REDHAT; 150a35fe226SAleksandr Bezzubikov k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_BRIDGE; 151a35fe226SAleksandr Bezzubikov k->realize = pcie_pci_bridge_realize; 152a35fe226SAleksandr Bezzubikov k->exit = pcie_pci_bridge_exit; 153a35fe226SAleksandr Bezzubikov k->config_write = pcie_pci_bridge_write_config; 154a35fe226SAleksandr Bezzubikov dc->vmsd = &pcie_pci_bridge_dev_vmstate; 155*4f67d30bSMarc-André Lureau device_class_set_props(dc, pcie_pci_bridge_dev_properties); 156a35fe226SAleksandr Bezzubikov dc->reset = &pcie_pci_bridge_reset; 157a35fe226SAleksandr Bezzubikov set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 15862b76563SDavid Hildenbrand hc->plug = pci_bridge_dev_plug_cb; 1598f560cdcSDavid Hildenbrand hc->unplug = pci_bridge_dev_unplug_cb; 16062b76563SDavid Hildenbrand hc->unplug_request = pci_bridge_dev_unplug_request_cb; 161a35fe226SAleksandr Bezzubikov } 162a35fe226SAleksandr Bezzubikov 163a35fe226SAleksandr Bezzubikov static const TypeInfo pcie_pci_bridge_info = { 164a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 165a35fe226SAleksandr Bezzubikov .parent = TYPE_PCI_BRIDGE, 166a35fe226SAleksandr Bezzubikov .instance_size = sizeof(PCIEPCIBridge), 167a35fe226SAleksandr Bezzubikov .class_init = pcie_pci_bridge_class_init, 168a35fe226SAleksandr Bezzubikov .interfaces = (InterfaceInfo[]) { 169a35fe226SAleksandr Bezzubikov { TYPE_HOTPLUG_HANDLER }, 17071d78767SEduardo Habkost { INTERFACE_PCIE_DEVICE }, 171a35fe226SAleksandr Bezzubikov { }, 172a35fe226SAleksandr Bezzubikov } 173a35fe226SAleksandr Bezzubikov }; 174a35fe226SAleksandr Bezzubikov 175a35fe226SAleksandr Bezzubikov static void pciepci_register(void) 176a35fe226SAleksandr Bezzubikov { 177a35fe226SAleksandr Bezzubikov type_register_static(&pcie_pci_bridge_info); 178a35fe226SAleksandr Bezzubikov } 179a35fe226SAleksandr Bezzubikov 180a35fe226SAleksandr Bezzubikov type_init(pciepci_register); 181