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" 12a35fe226SAleksandr Bezzubikov #include "hw/pci/pci.h" 13a35fe226SAleksandr Bezzubikov #include "hw/pci/pci_bus.h" 14a35fe226SAleksandr Bezzubikov #include "hw/pci/pci_bridge.h" 15a35fe226SAleksandr Bezzubikov #include "hw/pci/msi.h" 16a35fe226SAleksandr Bezzubikov #include "hw/pci/shpc.h" 17a35fe226SAleksandr Bezzubikov #include "hw/pci/slotid_cap.h" 18a35fe226SAleksandr Bezzubikov 19a35fe226SAleksandr Bezzubikov typedef struct PCIEPCIBridge { 20a35fe226SAleksandr Bezzubikov /*< private >*/ 21a35fe226SAleksandr Bezzubikov PCIBridge parent_obj; 22a35fe226SAleksandr Bezzubikov 23a35fe226SAleksandr Bezzubikov OnOffAuto msi; 24a35fe226SAleksandr Bezzubikov MemoryRegion shpc_bar; 25a35fe226SAleksandr Bezzubikov /*< public >*/ 26a35fe226SAleksandr Bezzubikov } PCIEPCIBridge; 27a35fe226SAleksandr Bezzubikov 28a35fe226SAleksandr Bezzubikov #define TYPE_PCIE_PCI_BRIDGE_DEV "pcie-pci-bridge" 29a35fe226SAleksandr Bezzubikov #define PCIE_PCI_BRIDGE_DEV(obj) \ 30a35fe226SAleksandr Bezzubikov OBJECT_CHECK(PCIEPCIBridge, (obj), TYPE_PCIE_PCI_BRIDGE_DEV) 31a35fe226SAleksandr Bezzubikov 32a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_realize(PCIDevice *d, Error **errp) 33a35fe226SAleksandr Bezzubikov { 34a35fe226SAleksandr Bezzubikov PCIBridge *br = PCI_BRIDGE(d); 35a35fe226SAleksandr Bezzubikov PCIEPCIBridge *pcie_br = PCIE_PCI_BRIDGE_DEV(d); 36a35fe226SAleksandr Bezzubikov int rc, pos; 37a35fe226SAleksandr Bezzubikov 38a35fe226SAleksandr Bezzubikov pci_bridge_initfn(d, TYPE_PCI_BUS); 39a35fe226SAleksandr Bezzubikov 40a35fe226SAleksandr Bezzubikov d->config[PCI_INTERRUPT_PIN] = 0x1; 41a35fe226SAleksandr Bezzubikov memory_region_init(&pcie_br->shpc_bar, OBJECT(d), "shpc-bar", 42a35fe226SAleksandr Bezzubikov shpc_bar_size(d)); 43a35fe226SAleksandr Bezzubikov rc = shpc_init(d, &br->sec_bus, &pcie_br->shpc_bar, 0, errp); 44a35fe226SAleksandr Bezzubikov if (rc) { 45a35fe226SAleksandr Bezzubikov goto error; 46a35fe226SAleksandr Bezzubikov } 47a35fe226SAleksandr Bezzubikov 48a35fe226SAleksandr Bezzubikov rc = pcie_cap_init(d, 0, PCI_EXP_TYPE_PCI_BRIDGE, 0, errp); 49a35fe226SAleksandr Bezzubikov if (rc < 0) { 50a35fe226SAleksandr Bezzubikov goto cap_error; 51a35fe226SAleksandr Bezzubikov } 52a35fe226SAleksandr Bezzubikov 53a35fe226SAleksandr Bezzubikov pos = pci_add_capability(d, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF, errp); 54a35fe226SAleksandr Bezzubikov if (pos < 0) { 55a35fe226SAleksandr Bezzubikov goto pm_error; 56a35fe226SAleksandr Bezzubikov } 57a35fe226SAleksandr Bezzubikov d->exp.pm_cap = pos; 58a35fe226SAleksandr Bezzubikov pci_set_word(d->config + pos + PCI_PM_PMC, 0x3); 59a35fe226SAleksandr Bezzubikov 60a35fe226SAleksandr Bezzubikov pcie_cap_arifwd_init(d); 61a35fe226SAleksandr Bezzubikov pcie_cap_deverr_init(d); 62a35fe226SAleksandr Bezzubikov 63a35fe226SAleksandr Bezzubikov rc = pcie_aer_init(d, PCI_ERR_VER, 0x100, PCI_ERR_SIZEOF, errp); 64a35fe226SAleksandr Bezzubikov if (rc < 0) { 65a35fe226SAleksandr Bezzubikov goto aer_error; 66a35fe226SAleksandr Bezzubikov } 67a35fe226SAleksandr Bezzubikov 68d659d940SAleksandr Bezzubikov Error *local_err = NULL; 69a35fe226SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_OFF) { 70d659d940SAleksandr Bezzubikov rc = msi_init(d, 0, 1, true, true, &local_err); 71a35fe226SAleksandr Bezzubikov if (rc < 0) { 72d659d940SAleksandr Bezzubikov assert(rc == -ENOTSUP); 73d659d940SAleksandr Bezzubikov if (pcie_br->msi != ON_OFF_AUTO_ON) { 74d659d940SAleksandr Bezzubikov error_free(local_err); 75d659d940SAleksandr Bezzubikov } else { 76d659d940SAleksandr Bezzubikov /* failed to satisfy user's explicit request for MSI */ 77d659d940SAleksandr Bezzubikov error_propagate(errp, local_err); 78a35fe226SAleksandr Bezzubikov goto msi_error; 79a35fe226SAleksandr Bezzubikov } 80a35fe226SAleksandr Bezzubikov } 81d659d940SAleksandr Bezzubikov } 82a35fe226SAleksandr Bezzubikov pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY | 83a35fe226SAleksandr Bezzubikov PCI_BASE_ADDRESS_MEM_TYPE_64, &pcie_br->shpc_bar); 84a35fe226SAleksandr Bezzubikov return; 85a35fe226SAleksandr Bezzubikov 86a35fe226SAleksandr Bezzubikov msi_error: 87a35fe226SAleksandr Bezzubikov pcie_aer_exit(d); 88a35fe226SAleksandr Bezzubikov aer_error: 89a35fe226SAleksandr Bezzubikov pm_error: 90a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 91a35fe226SAleksandr Bezzubikov cap_error: 92d659d940SAleksandr Bezzubikov shpc_cleanup(d, &pcie_br->shpc_bar); 93a35fe226SAleksandr Bezzubikov error: 94a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 95a35fe226SAleksandr Bezzubikov } 96a35fe226SAleksandr Bezzubikov 97a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_exit(PCIDevice *d) 98a35fe226SAleksandr Bezzubikov { 99a35fe226SAleksandr Bezzubikov PCIEPCIBridge *bridge_dev = PCIE_PCI_BRIDGE_DEV(d); 100a35fe226SAleksandr Bezzubikov pcie_cap_exit(d); 101a35fe226SAleksandr Bezzubikov shpc_cleanup(d, &bridge_dev->shpc_bar); 102a35fe226SAleksandr Bezzubikov pci_bridge_exitfn(d); 103a35fe226SAleksandr Bezzubikov } 104a35fe226SAleksandr Bezzubikov 105a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_reset(DeviceState *qdev) 106a35fe226SAleksandr Bezzubikov { 107a35fe226SAleksandr Bezzubikov PCIDevice *d = PCI_DEVICE(qdev); 108a35fe226SAleksandr Bezzubikov pci_bridge_reset(qdev); 109d659d940SAleksandr Bezzubikov if (msi_present(d)) { 110a35fe226SAleksandr Bezzubikov msi_reset(d); 111d659d940SAleksandr Bezzubikov } 112a35fe226SAleksandr Bezzubikov shpc_reset(d); 113a35fe226SAleksandr Bezzubikov } 114a35fe226SAleksandr Bezzubikov 115a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_write_config(PCIDevice *d, 116a35fe226SAleksandr Bezzubikov uint32_t address, uint32_t val, int len) 117a35fe226SAleksandr Bezzubikov { 118a35fe226SAleksandr Bezzubikov pci_bridge_write_config(d, address, val, len); 119d659d940SAleksandr Bezzubikov if (msi_present(d)) { 120a35fe226SAleksandr Bezzubikov msi_write_config(d, address, val, len); 121d659d940SAleksandr Bezzubikov } 122a35fe226SAleksandr Bezzubikov shpc_cap_write_config(d, address, val, len); 123a35fe226SAleksandr Bezzubikov } 124a35fe226SAleksandr Bezzubikov 125a35fe226SAleksandr Bezzubikov static Property pcie_pci_bridge_dev_properties[] = { 126d659d940SAleksandr Bezzubikov DEFINE_PROP_ON_OFF_AUTO("msi", PCIEPCIBridge, msi, ON_OFF_AUTO_AUTO), 127a35fe226SAleksandr Bezzubikov DEFINE_PROP_END_OF_LIST(), 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, 133a35fe226SAleksandr Bezzubikov .fields = (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 140a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_class_init(ObjectClass *klass, 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->is_bridge = 1; 147a35fe226SAleksandr Bezzubikov k->vendor_id = PCI_VENDOR_ID_REDHAT; 148a35fe226SAleksandr Bezzubikov k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_BRIDGE; 149a35fe226SAleksandr Bezzubikov k->realize = pcie_pci_bridge_realize; 150a35fe226SAleksandr Bezzubikov k->exit = pcie_pci_bridge_exit; 151a35fe226SAleksandr Bezzubikov k->config_write = pcie_pci_bridge_write_config; 152a35fe226SAleksandr Bezzubikov dc->vmsd = &pcie_pci_bridge_dev_vmstate; 153a35fe226SAleksandr Bezzubikov dc->props = pcie_pci_bridge_dev_properties; 154a35fe226SAleksandr Bezzubikov dc->reset = &pcie_pci_bridge_reset; 155a35fe226SAleksandr Bezzubikov set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 15662b76563SDavid Hildenbrand hc->plug = pci_bridge_dev_plug_cb; 157*8f560cdcSDavid Hildenbrand hc->unplug = pci_bridge_dev_unplug_cb; 15862b76563SDavid Hildenbrand hc->unplug_request = pci_bridge_dev_unplug_request_cb; 159a35fe226SAleksandr Bezzubikov } 160a35fe226SAleksandr Bezzubikov 161a35fe226SAleksandr Bezzubikov static const TypeInfo pcie_pci_bridge_info = { 162a35fe226SAleksandr Bezzubikov .name = TYPE_PCIE_PCI_BRIDGE_DEV, 163a35fe226SAleksandr Bezzubikov .parent = TYPE_PCI_BRIDGE, 164a35fe226SAleksandr Bezzubikov .instance_size = sizeof(PCIEPCIBridge), 165a35fe226SAleksandr Bezzubikov .class_init = pcie_pci_bridge_class_init, 166a35fe226SAleksandr Bezzubikov .interfaces = (InterfaceInfo[]) { 167a35fe226SAleksandr Bezzubikov { TYPE_HOTPLUG_HANDLER }, 16871d78767SEduardo Habkost { INTERFACE_PCIE_DEVICE }, 169a35fe226SAleksandr Bezzubikov { }, 170a35fe226SAleksandr Bezzubikov } 171a35fe226SAleksandr Bezzubikov }; 172a35fe226SAleksandr Bezzubikov 173a35fe226SAleksandr Bezzubikov static void pciepci_register(void) 174a35fe226SAleksandr Bezzubikov { 175a35fe226SAleksandr Bezzubikov type_register_static(&pcie_pci_bridge_info); 176a35fe226SAleksandr Bezzubikov } 177a35fe226SAleksandr Bezzubikov 178a35fe226SAleksandr Bezzubikov type_init(pciepci_register); 179